diff --git a/.credo.exs b/.credo.exs deleted file mode 100644 index 7e9e5465..00000000 --- a/.credo.exs +++ /dev/null @@ -1,216 +0,0 @@ -# This file contains the configuration for Credo and you are probably reading -# this after creating it with `mix credo.gen.config`. -# -# If you find anything wrong or unclear in this file, please report an -# issue on GitHub: https://github.com/rrrene/credo/issues -# -%{ - # - # You can have as many configs as you like in the `configs:` field. - configs: [ - %{ - # - # Run any config using `mix credo -C `. If no config name is given - # "default" is used. - # - name: "default", - # - # These are the files included in the analysis: - files: %{ - # - # You can give explicit globs or simply directories. - # In the latter case `**/*.{ex,exs}` will be used. - # - included: [ - "lib/", - "src/", - "test/", - "web/", - "apps/*/lib/", - "apps/*/src/", - "apps/*/test/", - "apps/*/web/" - ], - excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/", "lib/nosedrum/"] - }, - # - # Load and configure plugins here: - # - plugins: [], - # - # If you create your own checks, you must specify the source files for - # them here, so they can be loaded by Credo before running the analysis. - # - requires: [], - # - # If you want to enforce a style guide and need a more traditional linting - # experience, you can change `strict` to `true` below: - # - strict: true, - # - # To modify the timeout for parsing files, change this value: - # - parse_timeout: 5000, - # - # If you want to use uncolored output by default, you can change `color` - # to `false` below: - # - color: false, - # - # You can customize the parameters of any check by adding a second element - # to the tuple. - # - # To disable a check put `false` as second element: - # - # {Credo.Check.Design.DuplicatedCode, false} - # - checks: %{ - enabled: [ - # - ## Consistency Checks - # - {Credo.Check.Consistency.ExceptionNames, []}, - {Credo.Check.Consistency.LineEndings, []}, - {Credo.Check.Consistency.ParameterPatternMatching, []}, - {Credo.Check.Consistency.SpaceAroundOperators, []}, - {Credo.Check.Consistency.SpaceInParentheses, []}, - {Credo.Check.Consistency.TabsOrSpaces, []}, - - # - ## Design Checks - # - # You can customize the priority of any check - # Priority values are: `low, normal, high, higher` - # - # {Credo.Check.Design.AliasUsage, - # [priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]}, - # You can also customize the exit_status of each check. - # If you don't want TODO comments to cause `mix credo` to fail, just - # set this value to 0 (zero). - # - {Credo.Check.Design.TagTODO, [exit_status: 0]}, - {Credo.Check.Design.TagFIXME, []}, - - # - ## Readability Checks - # - {Credo.Check.Readability.AliasOrder, []}, - {Credo.Check.Readability.FunctionNames, []}, - {Credo.Check.Readability.LargeNumbers, []}, - {Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 130]}, - {Credo.Check.Readability.ModuleAttributeNames, []}, - {Credo.Check.Readability.ModuleNames, []}, - {Credo.Check.Readability.ParenthesesInCondition, []}, - {Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}, - {Credo.Check.Readability.PipeIntoAnonymousFunctions, []}, - {Credo.Check.Readability.PredicateFunctionNames, []}, - {Credo.Check.Readability.PreferImplicitTry, []}, - {Credo.Check.Readability.RedundantBlankLines, []}, - {Credo.Check.Readability.Semicolons, []}, - {Credo.Check.Readability.SpaceAfterCommas, []}, - {Credo.Check.Readability.StringSigils, []}, - {Credo.Check.Readability.TrailingBlankLine, []}, - {Credo.Check.Readability.TrailingWhiteSpace, []}, - {Credo.Check.Readability.UnnecessaryAliasExpansion, []}, - {Credo.Check.Readability.VariableNames, []}, - {Credo.Check.Readability.WithSingleClause, []}, - {Credo.Check.Consistency.MultiAliasImportRequireUse, []}, - {Credo.Check.Readability.ImplTrue, []}, - - # - ## Refactoring Opportunities - # - {Credo.Check.Refactor.Apply, []}, - {Credo.Check.Refactor.CondStatements, []}, - # {Credo.Check.Refactor.CyclomaticComplexity, [priority: :low]}, - {Credo.Check.Refactor.FunctionArity, []}, - # {Credo.Check.Refactor.LongQuoteBlocks, []}, - {Credo.Check.Refactor.MatchInCondition, []}, - {Credo.Check.Refactor.MapJoin, []}, - {Credo.Check.Refactor.NegatedConditionsInUnless, []}, - {Credo.Check.Refactor.NegatedConditionsWithElse, []}, - # {Credo.Check.Refactor.Nesting, []}, - {Credo.Check.Refactor.UnlessWithElse, []}, - {Credo.Check.Refactor.WithClauses, []}, - {Credo.Check.Refactor.FilterCount, []}, - {Credo.Check.Refactor.FilterFilter, []}, - {Credo.Check.Refactor.RejectReject, []}, - {Credo.Check.Refactor.RedundantWithClauseResult, []}, - - # - ## Warnings - # - {Credo.Check.Warning.ApplicationConfigInModuleAttribute, []}, - {Credo.Check.Warning.BoolOperationOnSameValues, []}, - {Credo.Check.Warning.Dbg, []}, - {Credo.Check.Warning.ExpensiveEmptyEnumCheck, []}, - {Credo.Check.Warning.IExPry, []}, - {Credo.Check.Warning.IoInspect, []}, - {Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, []}, - {Credo.Check.Warning.OperationOnSameValues, []}, - {Credo.Check.Warning.OperationWithConstantResult, []}, - {Credo.Check.Warning.RaiseInsideRescue, []}, - {Credo.Check.Warning.SpecWithStruct, []}, - {Credo.Check.Warning.WrongTestFileExtension, []}, - {Credo.Check.Warning.UnusedEnumOperation, []}, - {Credo.Check.Warning.UnusedFileOperation, []}, - {Credo.Check.Warning.UnusedKeywordOperation, []}, - {Credo.Check.Warning.UnusedListOperation, []}, - {Credo.Check.Warning.UnusedPathOperation, []}, - {Credo.Check.Warning.UnusedRegexOperation, []}, - {Credo.Check.Warning.UnusedStringOperation, []}, - {Credo.Check.Warning.UnusedTupleOperation, []}, - {Credo.Check.Warning.UnsafeExec, []}, - {Credo.Check.Warning.LeakyEnvironment, []}, - {Credo.Check.Warning.MapGetUnsafePass, []}, - {Credo.Check.Warning.MixEnv, []}, - {Credo.Check.Warning.UnsafeToAtom, []} - ], - disabled: [ - # - # Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`) - - # - # Controversial and experimental checks (opt-in, just move the check to `:enabled` - # and be sure to use `mix credo --strict` to see low priority checks) - # - {Credo.Check.Consistency.UnusedVariableNames, [force: :meaningful]}, - {Credo.Check.Readability.AliasAs, []}, - {Credo.Check.Readability.BlockPipe, []}, - {Credo.Check.Readability.ModuleDoc, []}, - {Credo.Check.Readability.MultiAlias, []}, - {Credo.Check.Design.DuplicatedCode, []}, - {Credo.Check.Design.SkipTestWithoutComment, []}, - {Credo.Check.Readability.NestedFunctionCalls, []}, - {Credo.Check.Readability.OneArityFunctionInPipe, []}, - {Credo.Check.Readability.SeparateAliasRequire, []}, - {Credo.Check.Readability.SingleFunctionToBlockPipe, []}, - {Credo.Check.Readability.SinglePipe, []}, - {Credo.Check.Readability.Specs, []}, - {Credo.Check.Readability.StrictModuleLayout, []}, - {Credo.Check.Readability.WithCustomTaggedTuple, []}, - {Credo.Check.Readability.OnePipePerLine, []}, - {Credo.Check.Refactor.ABCSize, []}, - {Credo.Check.Refactor.AppendSingleItem, []}, - {Credo.Check.Refactor.DoubleBooleanNegation, []}, - {Credo.Check.Refactor.FilterReject, []}, - {Credo.Check.Refactor.IoPuts, []}, - {Credo.Check.Refactor.MapMap, []}, - {Credo.Check.Refactor.ModuleDependencies, []}, - {Credo.Check.Refactor.NegatedIsNil, []}, - {Credo.Check.Refactor.PassAsyncInTestCases, []}, - {Credo.Check.Refactor.PipeChainStart, []}, - {Credo.Check.Refactor.RejectFilter, []}, - {Credo.Check.Refactor.VariableRebinding, []}, - {Credo.Check.Warning.LazyLogging, []} - - # {Credo.Check.Refactor.MapInto, []}, - - # - # Custom checks can be created using `mix credo.gen.check`. - # - ] - } - } - ] -} diff --git a/.cursor/rules/comment-style.mdc b/.cursor/rules/comment-style.mdc new file mode 100644 index 00000000..5dc64563 --- /dev/null +++ b/.cursor/rules/comment-style.mdc @@ -0,0 +1,149 @@ +--- +description: Enforce short, why-focused code comments across Interfold; forbid plan/step/history narration; allow richer XML doc only on HTTP action methods and wire DTOs. +alwaysApply: true +--- + +# Comment style + +Code comments in this repo exist to explain *why* the code is the way it is, +not *what* it does. Keep them short. If the code cannot self-explain, prefer +refactoring for clarity (rename, extract-method) before adding a comment. + +## Principle + +- One to three lines. If it needs more, the code probably needs a rename or a + helper, not a longer comment. +- Comment the surprising thing. If a reader could work out the intent from the + parameter names, the method name, and the types, don't restate it. +- Point at deep context rather than inlining it. Prefer + `// see InterfoldAppHost.ConfigureApiSelfHostEnv` over re-narrating the + rationale that already lives at the referenced site. + +## Never + +- **Plan / step / roadmap narration.** No `// TODO:`, no `// FIXME:`, no + `// HACK:`, no `// Step 1: ...`, no `// PATCH does not currently expose X — + in the future ...`. Deferred work goes in `docs/ROADMAP.md`, not in + comments. +- **Refactor archaeology.** No `// The sync overload this used to sit + alongside was dropped because ...`, no `// Callers no longer thread a + selector.`, no comment that only makes sense to someone who was in the + original PR review. +- **Story-telling / defensive narration.** No `// Without this we would ...`, + no `// scheme doesn't need forwarding because ...`, no "mini-essay against + a question the call site doesn't invite". +- **Redundant XML doc.** A `/// Returns the current user.` + above `public User GetCurrentUser()` adds nothing — delete it. +- **Obvious step labels in tests.** No `// Arrange`, `// Act`, `// Assert`, + no `// Setup:`, `// Verify:`, etc. The test's structure is already visible. +- **Commented-out code.** If it might come back, git remembers. + +## Do + +- Explain a non-obvious constraint in 2–4 dense lines when it's genuinely + hidden (silent widening, `System.Text.Json` ctor-selection rules, framework + foot-guns, database-driver quirks). +- Cross-reference the owning file/symbol when the deep context lives + elsewhere. Example: `// see AlterUrlQualifier.QualifyFriendship for the + origin-rewrite rules`. +- On `catch { }` and `catch (Exception) { }` that intentionally swallow, a + one-line explanation (`// best-effort cleanup, tolerate storage flake`) + is welcome — a silent swallow with no comment reads as a bug. + +## Exception — member-level, not file-level + +Two surfaces are allowed richer XML doc. The exception is *member-level*: it +does not license an entire file. Even inside the exception, the "Never" list +above still applies. + +- **HTTP action methods.** Methods on a `ControllerBase`-derived class + carrying `[HttpGet]`, `[HttpPost]`, `[HttpPut]`, `[HttpPatch]`, + `[HttpDelete]`, or a method-level `[Route]`. XML `` describing + the endpoint's contract semantics is welcome. Put behavioural nuance in + ``. No refactor archaeology. +- **Wire-contract DTOs.** `record` / `class` declarations serialised as + `[FromBody]` requests or as `Response` bodies (types under + `host/Interfold.Api/Models/**` and the actually-serialised types in + `shared/Interfold.Contracts/Models/**` — `AlterReadModel`, + `FriendshipReadModel`, request records, `SocketPayloadContracts`, + `PhxFrame`, etc.). XML `` on the type and per-property + `` documenting wire semantics is welcome. No story-telling + about why the type exists. + +Everything else — controller base classes, controller-private helpers, +non-wire enums / extensions / converters, internal models — follows the +strict style above. + +## Concrete examples from this repo + +### Trim over-long XML doc (strict style — not an endpoint / DTO) + +**Before** — `host/Interfold.Api/Controllers/Base/InterfoldControllerBase.cs`, `PrincipalId`: + +```csharp +/// +/// The authenticated principal for the current request as a +/// . The middleware guarantees the value is present and +/// scoped — any endpoint reaching this getter has already passed the JWT-sub +/// validation. widens implicitly to +/// at every persistence / repository call site, so this +/// getter's byte value flows unchanged to Postgres idempotency and Scylla PKs. +/// +protected ScopedSystemId PrincipalId +``` + +**After**: + +```csharp +/// Authenticated principal for the current request; middleware guarantees presence. +protected ScopedSystemId PrincipalId +``` + +### Delete plan / refactor-archaeology comments + +**Before** — `host/Interfold.Api/Controllers/AltersController.cs`: + +```csharp +// PATCH does not currently expose AvatarUrl as a mutable field — avatar updates go +// through the dedicated PUT .../avatar endpoints (multipart for Local, JSON for External) +// so we never observe a half-set (url-without-source) here. +var payload = new UpdateAlterCommand { ... }; + +//TODO: To ensure route works as expected - check if we delete alter journal entries, unattach from gobal journals when an alter is deleted and delete them from polls +[HttpDelete("{alterId:int}")] +public async Task Delete(...) +``` + +**After** — both comments removed. The `//TODO` intent is captured in +`docs/ROADMAP.md`. The `// PATCH does not ...` comment is refactor +archaeology. + +### Keep a dense why-comment on a wire DTO + +**Keep** — `shared/Interfold.Contracts/Models/Alter.cs`, the block above the +`[JsonConstructor]` overload: + +```csharp +// The 14-arg ctor above takes `bool?` for the flag trio so the CQL projections in +// ScyllaAlterRepository can bind `row.GetValue(...)` directly. STJ's +// deserialiser refuses that ctor because `bool? untracked` doesn't type-match the +// `bool Untracked` property — this second ctor takes plain `bool`, is marked +// [JsonConstructor], and forwards. +[JsonConstructor] +public AlterReadModel(...) +``` + +`AlterReadModel` is a wire DTO (returned as `Response` from +`AltersController`), and the comment documents a non-obvious +`System.Text.Json` constraint that the code itself cannot convey. This is +what a "kept" dense comment looks like: 5 lines, no history, no plan, no +apologising. + +## Enforcement + +- Applies to every `.cs` file in the repo. +- New `//TODO` / `//FIXME` / `//HACK` in a diff is a review block. Move the + intent to `docs/ROADMAP.md` first. +- New `// Arrange / // Act / // Assert` in a test diff is a review block. +- An XML doc block longer than ~10 lines outside the two exception surfaces + is a review block. diff --git a/.dockerignore b/.dockerignore index 61a73933..1d15af26 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,45 +1,65 @@ # This file excludes paths from the Docker build context. # # By default, Docker's build context includes all files (and folders) in the -# current directory. Even if a file isn't copied into the container it is still sent to -# the Docker daemon. +# current directory. Even if a file isn't copied into the container it is still +# sent to the Docker daemon. # # There are multiple reasons to exclude files from the build context: -# -# 1. Prevent nested folders from being copied into the container (ex: exclude -# /assets/node_modules when copying /assets) -# 2. Reduce the size of the build context and improve build time (ex. /build, /deps, /doc) -# 3. Avoid sending files containing sensitive information +# 1. Prevent local build outputs from leaking into the image (ex: bin/ obj/). +# 2. Reduce the size of the build context and improve build time. +# 3. Avoid sending files containing sensitive information. # # More information on using .dockerignore is available here: # https://docs.docker.com/engine/reference/builder/#dockerignore-file .dockerignore -# Ignore git, but keep git HEAD and refs to access current commit hash if needed: +# Ignore git, but keep git HEAD and refs to access current commit hash if +# needed: # -# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat -# d0b8727759e1e0e7aa3d41707d12376e373d5ecc +# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat +# d0b8727759e1e0e7aa3d41707d12376e373d5ecc .git !.git/HEAD !.git/refs -# Common development/test artifacts +# .NET build outputs — must stay out of the SDK stage's view so the in-image +# publish is reproducible from source instead of accidentally re-packing a +# host-side `dotnet build` output. +**/bin/ +**/obj/ +publish/ +*.user +*.suo + +# IDE / tooling caches +.idea/ +.vs/ +.vscode/ +.cursor/ +.cache/ + +# Test outputs + coverage +**/TestResults/ +coveragereport/ + +# Node tooling (none in-repo today, future-proofing) +node_modules/ + +# CI + docs aren't needed inside the image build +.github/ +docs/ + +# Legacy / unrelated artifacts left over from the Elixir-era repo /cover/ /doc/ /test/ /tmp/ .elixir_ls - -# Mix artifacts /_build/ /deps/ *.ez - -# Generated on crash by the VM erl_crash.dump - -# Static artifacts - These should be fetched and built inside the Docker image /assets/node_modules/ /priv/static/assets/ /priv/static/cache_manifest.json diff --git a/.editorconfig b/.editorconfig index b4d4a1dd..f98e1705 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,7 @@ [*.ex] indent_style = space -indent_size = 2 \ No newline at end of file +indent_size = 2 + +[*.cs] +# IDE0290: Use primary constructor +csharp_style_prefer_primary_constructors = false diff --git a/.formatter.exs b/.formatter.exs deleted file mode 100644 index 5971023f..00000000 --- a/.formatter.exs +++ /dev/null @@ -1,5 +0,0 @@ -[ - import_deps: [:ecto, :ecto_sql, :phoenix], - subdirectories: ["priv/*/migrations"], - inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}", "priv/*/seeds.exs"] -] diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..b5da6990 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +# Shell scripts must use LF (they run in Linux containers) +*.sh text eol=lf + +# Embedded in Interfold.Infrastructure.Scylla.dll; SHA-256 of the file +# bytes is the schema_migrations checksum, so CRLF on Windows checkouts +# fails startup against a Linux-built (or restored) ledger. +*.cql text eol=lf diff --git a/.github/actions/build-bootstrapper-multi-rid/action.yml b/.github/actions/build-bootstrapper-multi-rid/action.yml new file mode 100644 index 00000000..20d304fc --- /dev/null +++ b/.github/actions/build-bootstrapper-multi-rid/action.yml @@ -0,0 +1,105 @@ +name: Build multi-RID bootstrapper artefacts +description: > + Multi-arch bootstrapper publish + tar.gz + per-RID upload. `platforms` and + `rids` must be consistent (one `buildx_for` map entry per RID). Caller must + run `actions/checkout@v4` and `docker/setup-buildx-action@v3` first. Adding + a RID also needs updates in the `buildx_for` map below and in both + Dockerfiles - see the `BUILD_PLATFORMS` env comment in `.github/workflows/ci-cd.yml`. + +inputs: + platforms: + description: 'Buildx platform list. Must be consistent with `rids` via the `buildx_for` map.' + required: true + rids: + description: 'Space-separated RID subset of `linux-x64 linux-arm linux-arm64` to package + upload. Must include `linux-x64` (guardrail-stage cp below is unconditional).' + required: true + dockerfile: + description: 'Bootstrapper Dockerfile path, relative to repo root.' + required: true + artifact-suffix: + description: 'Suffix on each artefact `name`, typically the commit SHA so runs don''t collide.' + required: true + +runs: + using: composite + steps: + # Shares GHA cache scope `interfold-dotnet` with the API build in `publish` + # so SDK base image, source-tree COPY layer, and the `id=nuget` cache mount + # (exported by mode=max) dedup across both Dockerfiles. + - name: Build bootstrapper binaries (multi-RID, parallel) + uses: docker/build-push-action@v6 + with: + context: . + file: ${{ inputs.dockerfile }} + target: out + platforms: ${{ inputs.platforms }} + outputs: type=local,dest=./bootstrap-out + cache-from: type=gha,scope=interfold-dotnet + cache-to: type=gha,mode=max,scope=interfold-dotnet + + # tar.gz preserves +x (upload-artifact's zip wrapper strips it) and only + # packs RIDs the caller asked for. `buildx_for` is the sole RID <-> buildx + # dir translation; the linux-x64 guardrail-stage cp is unconditional + # because both PR and release paths always include linux-x64 in `rids`. + - name: Tar.gz binaries + stage linux-x64 for binary-size guardrail + shell: bash + env: + RIDS: ${{ inputs.rids }} + run: | + set -euo pipefail + declare -A buildx_for=( + [linux-x64]=linux_amd64 + [linux-arm64]=linux_arm64 + [linux-arm]=linux_arm_v7 + ) + for rid in $RIDS; do + buildx_dir="${buildx_for[$rid]:-}" + if [[ -z "$buildx_dir" ]]; then + echo "Unknown RID '$rid' - update buildx_for in $GITHUB_ACTION_PATH/action.yml" + exit 1 + fi + # Single-platform buildx dumps flat to `bootstrap-out/` (no arch + # subdir) - normalize into `/` so the tar `-C` path below stays + # uniform and the guardrail-stage cp finds the file at a fixed path. + if [[ ! -f "bootstrap-out/$buildx_dir/interfold-bootstrap" ]] && [[ -f bootstrap-out/interfold-bootstrap ]]; then + mkdir -p "bootstrap-out/$buildx_dir" + mv bootstrap-out/interfold-bootstrap "bootstrap-out/$buildx_dir/" + fi + chmod +x "bootstrap-out/$buildx_dir/interfold-bootstrap" + tar -czf "interfold-bootstrap-$rid.tar.gz" \ + -C "bootstrap-out/$buildx_dir" interfold-bootstrap + done + dest=tools/Interfold.Bootstrapper/bin/Release/net10.0/linux-x64/publish + mkdir -p "$dest" + cp bootstrap-out/linux_amd64/interfold-bootstrap "$dest/interfold-bootstrap" + chmod +x "$dest/interfold-bootstrap" + + # One artefact per RID so consumers download only the binary they need. + # `if:` uses `format(' {0} ', ...)` word-boundaries so `linux-arm` doesn't + # match a `linux-arm64`-only rids input as a substring. + - name: Upload bootstrapper artifact (linux-x64) + if: contains(format(' {0} ', inputs.rids), ' linux-x64 ') + uses: actions/upload-artifact@v4 + with: + name: interfold-bootstrap-linux-x64-${{ inputs.artifact-suffix }} + path: interfold-bootstrap-linux-x64.tar.gz + retention-days: 7 + if-no-files-found: error + + - name: Upload bootstrapper artifact (linux-arm) + if: contains(format(' {0} ', inputs.rids), ' linux-arm ') + uses: actions/upload-artifact@v4 + with: + name: interfold-bootstrap-linux-arm-${{ inputs.artifact-suffix }} + path: interfold-bootstrap-linux-arm.tar.gz + retention-days: 7 + if-no-files-found: error + + - name: Upload bootstrapper artifact (linux-arm64) + if: contains(format(' {0} ', inputs.rids), ' linux-arm64 ') + uses: actions/upload-artifact@v4 + with: + name: interfold-bootstrap-linux-arm64-${{ inputs.artifact-suffix }} + path: interfold-bootstrap-linux-arm64.tar.gz + retention-days: 7 + if-no-files-found: error diff --git a/.github/actions/expose-actions-runtime/action.yml b/.github/actions/expose-actions-runtime/action.yml new file mode 100644 index 00000000..a6c72371 --- /dev/null +++ b/.github/actions/expose-actions-runtime/action.yml @@ -0,0 +1,14 @@ +name: Expose GitHub Actions Runtime +description: > + Bridges ACTIONS_RUNTIME_TOKEN and ACTIONS_RESULTS_URL from the JS-action + environment onto the job-level env so TUnit's GitHub Actions reporter, + invoked in a later `run:` step, can attach test results to the job summary. + +runs: + using: composite + steps: + - uses: actions/github-script@v7 + with: + script: | + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env['ACTIONS_RUNTIME_TOKEN']); + core.exportVariable('ACTIONS_RESULTS_URL', process.env['ACTIONS_RESULTS_URL']); diff --git a/.github/actions/publish-coverage-report/action.yml b/.github/actions/publish-coverage-report/action.yml new file mode 100644 index 00000000..0a5eb27f --- /dev/null +++ b/.github/actions/publish-coverage-report/action.yml @@ -0,0 +1,37 @@ +name: Publish coverage report +description: > + Renders one ReportGenerator MarkdownSummaryGithub view of a coverage subset + and, on pull-request events, posts it as a sticky PR comment. + +inputs: + reports-glob: + description: 'Cobertura glob for ReportGenerator, e.g. `coverage-inputs/coverage-unit-*/**/*.cobertura.xml`.' + required: true + targetdir: + description: 'Output directory; SummaryGithub.md is read from here for the PR comment.' + required: true + title: + description: 'Report title, e.g. `Unit tests`.' + required: true + sticky-header: + description: 'Sticky-comment header (one per report kind, e.g. `coverage-unit`) so reports coexist instead of overwriting.' + required: true + +runs: + using: composite + steps: + - name: Coverage report (${{ inputs.title }}) + uses: danielpalme/ReportGenerator-GitHub-Action@5 + with: + reports: ${{ inputs.reports-glob }} + targetdir: ${{ inputs.targetdir }} + reporttypes: MarkdownSummaryGithub + title: ${{ inputs.title }} + + - name: PR comment (${{ inputs.title }}) + if: github.event_name == 'pull_request' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: ${{ inputs.sticky-header }} + recreate: true + path: ${{ inputs.targetdir }}/SummaryGithub.md diff --git a/.github/actions/setup-test-runner/action.yml b/.github/actions/setup-test-runner/action.yml new file mode 100644 index 00000000..07d91360 --- /dev/null +++ b/.github/actions/setup-test-runner/action.yml @@ -0,0 +1,44 @@ +name: Setup .NET test runner +description: > + Preamble for jobs that launch MTP test-host DLLs from the `build` job's + artefact. Caller must run `actions/checkout@v4` first so this composite's + own path resolves. + +inputs: + dotnet-version: + description: '.NET SDK version, e.g. `10.0.x`.' + required: true + build-artifact-name: + description: 'Name of the `build`-job artefact carrying the test-project bins, typically `dotnet-build-`.' + required: true + +runs: + using: composite + steps: + # NuGet cache required for `test-int`: Aspire's `DcpHost` reads its native + # orchestrator binary from an absolute path under + # `~/.nuget/packages/aspire.hosting.orchestration.//tools/dcp`, + # bypassing every test host's copy-local `.deps.json`. Without the cache, + # every `AspireFixture`-backed test fails at startup with FileNotFoundException. + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ inputs.dotnet-version }} + cache: true + cache-dependency-path: | + **/*.csproj + global.json + + - name: Download build output + uses: actions/download-artifact@v4 + with: + name: ${{ inputs.build-artifact-name }} + + # The artefact is a tarball (see `Package build output` in ci-cd.yml) so + # MTP test hosts keep their +x bit. + - name: Extract build output + shell: bash + run: tar -xzf dotnet-build.tar.gz + + - name: Expose GitHub Actions Runtime + uses: ./.github/actions/expose-actions-runtime diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 00000000..0b09faf6 --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,435 @@ +name: CI/CD Pipeline + +on: + pull_request: + push: + branches: [ main, develop ] + # Manual safety-net for validating arm cross-builds from a PR head branch - + # dispatch against the branch with `bootstrapper-full-rids` on (default) to + # run the full RID matrix a PR run otherwise skips. + workflow_dispatch: + inputs: + bootstrapper-full-rids: + description: 'Build all bootstrapper RIDs (linux-x64/arm/arm64). Off = fast PR-shape build (linux-x64 only).' + type: boolean + default: true + +env: + DOCKERFILE_PATH: Dockerfile + BOOTSTRAPPER_DOCKERFILE_PATH: Dockerfile.bootstrapper + # Opt every CI leg out of the auto-managed integration-test DB bench + # (see TestBenchCoordinator). CI direct-launches the MTP test-host DLLs so it + # would not read the shared runsettings anyway, but asserting `0` at the + # workflow scope makes the intent explicit and covers any future path that + # goes through `dotnet test`. + INTERFOLD_TEST_BENCH: '0' + # Release-time bootstrapper matrix. `build` downgrades to linux/amd64 alone + # on PRs (only RID CI tests need - skips QEMU-emulated arm cross-builds for + # artefacts nothing on the PR critical path consumes); workflow_dispatch + # inherits the full matrix by default. Adding a platform also needs a + # matching `buildx_for` entry + upload step in + # `.github/actions/build-bootstrapper-multi-rid/action.yml` and a + # TARGETARCH->RID branch in both `/Dockerfile` and `/Dockerfile.bootstrapper`. + BUILD_PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7 + BOOTSTRAPPER_RIDS: linux-x64 linux-arm linux-arm64 + # Runner-side .NET SDK. The bootstrapper Docker build pins its own SDK. + DOTNET_VERSION: '10.0.x' + +jobs: + # prepare: emit tests/integration/*/ as a JSON array for test-int's matrix. + # `Interfold.IntegrationTests.Shared` is included because it also hosts real + # tests (ReplayParityTests). + prepare: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Enumerate integration test projects + id: set-matrix + run: | + set -euo pipefail + projects=$(ls -d tests/integration/*/ \ + | sed 's:tests/integration/::;s:/$::' \ + | jq -R -s -c 'split("\n") | map(select(length > 0))') + echo "matrix=$projects" >> "$GITHUB_OUTPUT" + echo "Discovered integration test projects: $projects" + + # build: whole-solution compile + bootstrapper buildx (matrix size gated by + # `FULL_BOOTSTRAPPER_BUILD` below). Uploads per-RID `interfold-bootstrap-` + # tar.gz artefacts + a `dotnet-build-` artefact carrying the test-project + # bins for downstream direct-DLL launch. + build: + runs-on: ubuntu-latest + needs: prepare + env: + # Push always builds the full matrix; workflow_dispatch respects the input + # (default on). Everything else (pull_request) is a fast build. + FULL_BOOTSTRAPPER_BUILD: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.bootstrapper-full-rids) }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + # Cache-key = csproj + global.json hashes (no packages.lock.json here); + # any csproj edit busts it, still cheaper than a cold restore. + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + cache: true + cache-dependency-path: | + **/*.csproj + global.json + + # docker-container driver required for multi-arch manifest output. + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Restore + run: dotnet restore Interfold.slnx + + - name: Build (Release) + run: dotnet build Interfold.slnx --configuration Release --no-restore + + - name: Build & upload multi-RID bootstrapper artefacts + uses: ./.github/actions/build-bootstrapper-multi-rid + with: + platforms: ${{ env.FULL_BOOTSTRAPPER_BUILD == 'true' && env.BUILD_PLATFORMS || 'linux/amd64' }} + rids: ${{ env.FULL_BOOTSTRAPPER_BUILD == 'true' && env.BOOTSTRAPPER_RIDS || 'linux-x64' }} + dockerfile: ${{ env.BOOTSTRAPPER_DOCKERFILE_PATH }} + artifact-suffix: ${{ github.sha }} + + # tar.gz preserves +x that upload-artifact@v4's zip strips. Ships test- + # project bins (direct-DLL launch) and `tools/*/bin` because + # `Interfold.Bootstrapper.UnitTests` locates the managed DLL and the + # multi-RID-composite-staged linux-x64 ELF by absolute path, not projref. + - name: Package build output + shell: bash + run: | + tar -czf dotnet-build.tar.gz \ + tests/integration/*/bin \ + tests/unit/*/bin \ + tools/*/bin + + - name: Upload build output + uses: actions/upload-artifact@v4 + with: + name: dotnet-build-${{ github.sha }} + path: dotnet-build.tar.gz + retention-days: 1 + if-no-files-found: error + + # test-unit: single job, loops over every `tests/unit/*/` project. Includes + # `BinarySizeGuardrailTests`, which reads the staged linux-x64 binary from the + # build artefact - see `BinarySizeGuardrailTests.LocatePublishedBinary`. + test-unit: + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup test runner + uses: ./.github/actions/setup-test-runner + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + build-artifact-name: dotnet-build-${{ github.sha }} + + # Directly launch each test project's MTP host DLL instead of going through + # `dotnet test --no-build --no-restore` - the new .NET 10 `dotnet + # test` command regressed test-project discovery on our tarball-extracted + # trees between 10.0.301 and 10.0.302 ("No test projects were found."), and + # TUnit test hosts are self-contained MTP executables that accept the same + # `--coverage` / `--coverage-output-format` flags directly. + - name: Test (unit) + run: | + set -euo pipefail + for dir in tests/unit/*/; do + name=$(basename "$dir") + echo "::group::$name" + dotnet "$dir"bin/Release/net10.0/"$name".dll \ + --coverage --coverage-output-format cobertura + echo "::endgroup::" + done + + - name: Upload coverage (unit) + uses: actions/upload-artifact@v4 + with: + name: coverage-unit-${{ github.sha }} + path: '**/TestResults/**/*.cobertura.xml' + retention-days: 1 + if-no-files-found: error + + # TUnit's cooperative mode (default on GHA) merges every DLL launched + # inside the `Test (unit)` loop into one HTML report under + # `$RUNNER_TEMP/tunit-aggregate//merged-report.html`. Uploaded here + # so the merged report is downloadable alongside the coverage bundle. + - name: Upload merged TUnit report (unit) + if: always() + uses: actions/upload-artifact@v4 + with: + name: tunit-merged-unit-${{ github.sha }} + path: ${{ runner.temp }}/tunit-aggregate/**/merged-report.html + retention-days: 7 + if-no-files-found: warn + + # test-int: one matrix leg per project under tests/integration/. In-DinD + # concurrency inside the Bootstrapper leg auto-sizes off NCPU / MemTotal / + # DockerRootDir - see + # `tests/integration/Interfold.Bootstrapper.IntegrationTests/fixtures/DinDParallelLimit.cs`. + test-int: + runs-on: ubuntu-latest + needs: [ prepare, build ] + strategy: + fail-fast: false + matrix: + project: ${{ fromJSON(needs.prepare.outputs.matrix) }} + name: test-int (${{ matrix.project }}) + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup test runner + uses: ./.github/actions/setup-test-runner + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + build-artifact-name: dotnet-build-${{ github.sha }} + + # Direct MTP-host launch (same reasoning as `test-unit`). `defer` tells + # TUnit's aggregation reporter to persist its per-suite JSON sidecar + # (`{asm}-{os}-{tfm}.tunit-report.json`) alongside the HTML report but + # skip writing its markdown block to $GITHUB_STEP_SUMMARY. `test-int- + # summary` downloads every leg's sidecar and calls `tunit-report merge` + # on them, splicing one native TUnit summary into that job's summary tab + # instead of one block per matrix leg. See TUnit PR #6415. + # + # Two legs carry [Explicit] tests and need bespoke invocations: + # - Bootstrapper.IntegrationTests: every test class is [Explicit] (see the + # assembly's AssemblyInfo.cs); a `/*/*/*/*` treenode filter matches all + # tests, all explicit, so TUnit runs the full suite. Default (unfiltered) + # invocation would skip everything. + # - Infrastructure.IntegrationTests: MultiNodeScyllaTests alone is [Explicit] + # (7-DC ring, GB-scale RAM). Runs in two passes because TUnit's rule is + # "explicit only runs when every filtered test is explicit" — a single + # mixed filter would exclude MultiNode. The first pass produces the + # canonical sidecar; the second is renamed on-the-fly so both survive + # upload. `test-int-summary` merges them like any other leg's sidecars. + - name: Test (integration - ${{ matrix.project }}) + env: + TUNIT_AGGREGATE_REPORTS: defer + run: | + set -euo pipefail + dll="tests/integration/${{ matrix.project }}/bin/Release/net10.0/${{ matrix.project }}.dll" + case "${{ matrix.project }}" in + Interfold.Bootstrapper.IntegrationTests) + dotnet "$dll" \ + --treenode-filter '/*/*/*/*' \ + --coverage --coverage-output-format cobertura + ;; + Interfold.Infrastructure.IntegrationTests) + dotnet "$dll" \ + --coverage --coverage-output-format cobertura + # Preserve the non-explicit sidecar before the second invocation + # overwrites it. Both files are picked up by the `**/*.tunit-report.json` + # upload glob below. + results="tests/integration/${{ matrix.project }}/bin/Release/net10.0/TestResults" + for sidecar in "$results"/*.tunit-report.json; do + [ -e "$sidecar" ] || continue + mv "$sidecar" "${sidecar%.tunit-report.json}-nonexplicit.tunit-report.json" + done + dotnet "$dll" \ + --treenode-filter '/*/*/*MultiNodeScyllaTests/*' \ + --coverage --coverage-output-format cobertura + ;; + *) + dotnet "$dll" \ + --coverage --coverage-output-format cobertura + ;; + esac + + # Sidecars land in `TestResults/{asm}-{os}-{tfm}.tunit-report.json` next to + # the HTML report the engine also emits by default; `**/*.tunit-report.json` + # picks them up wherever the test host's cwd ended up. + - name: Upload TUnit report sidecar (${{ matrix.project }}) + if: always() + uses: actions/upload-artifact@v4 + with: + name: tunit-sidecar-${{ matrix.project }}-${{ github.sha }} + path: 'tests/integration/${{ matrix.project }}/**/*.tunit-report.json' + retention-days: 1 + if-no-files-found: warn + + - name: Upload coverage (integration - ${{ matrix.project }}) + uses: actions/upload-artifact@v4 + with: + name: coverage-int-${{ matrix.project }}-${{ github.sha }} + path: 'tests/integration/${{ matrix.project }}/**/TestResults/**/*.cobertura.xml' + retention-days: 1 + if-no-files-found: error + + # test-int-summary: renders one native TUnit aggregated block from every + # matrix leg's sidecar. `tunit-report merge --github-summary` splices the + # block into $GITHUB_STEP_SUMMARY between HTML-comment markers so foreign + # summary content is untouched; separate job so its Summary-tab section + # renders top-level, not nested under `coverage`. + test-int-summary: + runs-on: ubuntu-latest + needs: test-int + if: always() + steps: + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Download TUnit sidecars + uses: actions/download-artifact@v4 + with: + path: tunit-sidecars + pattern: tunit-sidecar-* + merge-multiple: true + + # Version-pinned to the same 1.61 line as the TUnit engine that produced + # the sidecars - the tool links the engine's serializer/merger/renderer + # sources directly, so drift here would silently miss fields. `--tool-path` + # instead of `--global` because setup-dotnet does not add + # `$HOME/.dotnet/tools` to `$GITHUB_PATH`, so a global install would leave + # the binary un-findable in the next step. + - name: Install tunit-report tool + run: dotnet tool install --tool-path .tunit-tools TUnit.Reporting.Tool --version '1.61.*' + + # `--output` overrides the default `/merged-report.html` so the file + # lands outside the sidecar tree and the upload step's `path:` stays a + # single glob-free string. + - name: Merge and write summary + run: | + ./.tunit-tools/tunit-report merge \ + --directory tunit-sidecars \ + --output merged-report.html \ + --github-summary + + - name: Upload merged TUnit report (integration) + if: always() + uses: actions/upload-artifact@v4 + with: + name: tunit-merged-int-${{ github.sha }} + path: merged-report.html + retention-days: 7 + if-no-files-found: warn + + # coverage: three ReportGenerator outputs - unit-only, integration-only, + # combined - so integration-only coverage (candidates for adding unit tests) + # is visible at a glance. + coverage: + runs-on: ubuntu-latest + needs: [ test-unit, test-int ] + if: always() + steps: + - name: Checkout code + uses: actions/checkout@v4 + + # `merge-multiple: false` keeps each artefact under its own `coverage-*/` + # subfolder so the three ReportGenerator globs below can address them + # independently. + - name: Download all coverage artefacts + uses: actions/download-artifact@v4 + with: + path: coverage-inputs + pattern: coverage-* + merge-multiple: false + + # Distinct `sticky-header:` per invocation so sticky-pull-request-comment + # keeps the three summaries side-by-side instead of overwriting one slot. + - name: Publish unit coverage + uses: ./.github/actions/publish-coverage-report + with: + reports-glob: 'coverage-inputs/coverage-unit-*/**/*.cobertura.xml' + targetdir: coveragereport/unit + title: 'Unit tests' + sticky-header: coverage-unit + + - name: Publish integration coverage + uses: ./.github/actions/publish-coverage-report + with: + reports-glob: 'coverage-inputs/coverage-int-*/**/*.cobertura.xml' + targetdir: coveragereport/integration + title: 'Integration tests' + sticky-header: coverage-integration + + - name: Publish combined coverage + uses: ./.github/actions/publish-coverage-report + with: + reports-glob: 'coverage-inputs/coverage-*/**/*.cobertura.xml' + targetdir: coveragereport/all + title: 'All tests' + sticky-header: coverage-all + + - name: Write coverage summary + run: | + { + cat coveragereport/unit/SummaryGithub.md + echo + cat coveragereport/integration/SummaryGithub.md + echo + cat coveragereport/all/SummaryGithub.md + } >> "$GITHUB_STEP_SUMMARY" + + # publish: multi-arch API image push, gated to pushes on `main` (tag `latest`) + # or `develop` (tag `bleeding-edge`). Shares GHA cache scope `interfold-dotnet` + # with the bootstrapper buildx in `build`. + publish: + runs-on: ubuntu-latest + needs: [ test-unit, test-int ] + if: >- + github.event_name == 'push' && + (github.ref == format('refs/heads/{0}', github.event.repository.default_branch) || + github.ref == 'refs/heads/develop') + permissions: + contents: read + packages: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # `latest` is gated to the default branch (currently `main`); `bleeding-edge` + # is gated to `develop`. The other two tags (branch name, `sha-`) + # follow docker/metadata-action defaults. + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository_owner }}/interfold-api + tags: | + type=ref,event=branch + type=sha,prefix=sha- + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} + type=raw,value=bleeding-edge,enable=${{ github.ref == 'refs/heads/develop' }} + + # provenance/sbom off so the manifest list doesn't carry BuildKit's + # `unknown/unknown` attestation entries (breaks some consumers and the + # bootstrapper's manifest inspection). + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + file: ${{ env.DOCKERFILE_PATH }} + push: true + platforms: ${{ env.BUILD_PLATFORMS }} + provenance: false + sbom: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha,scope=interfold-dotnet + cache-to: type=gha,mode=max,scope=interfold-dotnet diff --git a/.gitignore b/.gitignore index 910e1504..8158923f 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,6 @@ erl_crash.dump # Docker volumes /pgdata /msg_pgdata -/dev/scylla/data # Ignore package tarball (built via "mix hex.build"). octocon-*.tar @@ -34,4 +33,38 @@ octocon-*.tar basic.conf # Dev config (contains secrets) -/config/dev.exs \ No newline at end of file +/config/dev.exs + +# C# / .NET +**/bin/ +**/obj/ +**/*.user +**/*.suo +**/*.vs/ +.vs/ +*.ncrunch* +TestResults/ +[Pp]ublish/ +*.nuget.props +*.nuget.targets + +scripts/certs/** +scripts/keys/** + +# Launch settings (contains OAuth secrets, credentials) +**/Properties/launchSettings.json +artifacts/lifecycle-probe.ndjson + +# Internal docs (roadmap, active/archive workstreams) — not for public git yet. +# docs/configuration.md is the only tracked doc (operator/maintainer reference). +docs/* +!docs/configuration.md +docs/active/ +docs/archive/ + +# Cursor planning artifacts — local-only. +/.cursor/plans/ + +# Copilot chat instruction files — internal, keep local. +/.github/instructions/ + diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 45dc8f63..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "nostrum"] - path = nostrum - url = https://github.com/OctoconDev/nostrum diff --git a/.idea/.idea.Interfold/.idea/.gitignore b/.idea/.idea.Interfold/.idea/.gitignore new file mode 100644 index 00000000..d99d1303 --- /dev/null +++ b/.idea/.idea.Interfold/.idea/.gitignore @@ -0,0 +1,15 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.Interfold.iml +/contentModel.xml +/modules.xml +/projectSettingsUpdater.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.idea.Interfold/.idea/.name b/.idea/.idea.Interfold/.idea/.name new file mode 100644 index 00000000..c9a17c4f --- /dev/null +++ b/.idea/.idea.Interfold/.idea/.name @@ -0,0 +1 @@ +Interfold \ No newline at end of file diff --git a/.idea/.idea.Interfold/.idea/copilot.data.migration.ask2agent.xml b/.idea/.idea.Interfold/.idea/copilot.data.migration.ask2agent.xml new file mode 100644 index 00000000..1f2ea11e --- /dev/null +++ b/.idea/.idea.Interfold/.idea/copilot.data.migration.ask2agent.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.Interfold/.idea/encodings.xml b/.idea/.idea.Interfold/.idea/encodings.xml new file mode 100644 index 00000000..df87cf95 --- /dev/null +++ b/.idea/.idea.Interfold/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.Interfold/.idea/indexLayout.xml b/.idea/.idea.Interfold/.idea/indexLayout.xml new file mode 100644 index 00000000..7b08163c --- /dev/null +++ b/.idea/.idea.Interfold/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Interfold/.idea/vcs.xml b/.idea/.idea.Interfold/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/.idea.Interfold/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..a957a382 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "C#: Interfold.Api.Host Debug", + "type": "dotnet", + "request": "launch", + "projectPath": "${workspaceFolder}/hosts/Interfold.Api.Host/Interfold.Api.Host.csproj" + } + + ] +} \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props new file mode 100644 index 00000000..187023bc --- /dev/null +++ b/Directory.Packages.props @@ -0,0 +1,86 @@ + + + + true + true + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Dockerfile b/Dockerfile index 203ecf96..2aa3256f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,125 +1,30 @@ -# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian -# instead of Alpine to avoid DNS resolution issues in production. +# syntax=docker/dockerfile:1.7 # -# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu -# https://hub.docker.com/_/ubuntu?tab=tags -# -# This file is based on these images: -# -# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image -# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20230612-slim - for the release image -# - https://pkgs.org/ - resource for finding needed packages -# - Ex: hexpm/elixir:1.15.6-erlang-25.3.2.6-debian-bullseye-20230612-slim -# -ARG ELIXIR_VERSION=1.19.5 -ARG OTP_VERSION=27.0 -ARG DEBIAN_VERSION=trixie-20260112-slim - -ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}" -ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}" - -FROM ${BUILDER_IMAGE} AS builder - -# install build dependencies -RUN apt-get update -y && apt-get install -y build-essential git \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# prepare build dir +# Multi-arch image for ghcr.io/azyyyyyy/interfold-api. SDK stage is pinned to +# $BUILDPLATFORM so `dotnet publish -r linux-$ARCH` cross-publishes natively +# (no QEMU); publish now lives here, not in CI, so each per-arch runtime image +# gets its own RID assets. + +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0 AS build +ARG TARGETARCH +WORKDIR /src +COPY . . +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ + case "$TARGETARCH" in \ + amd64) RID=linux-x64 ;; \ + arm64) RID=linux-arm64 ;; \ + arm) RID=linux-arm ;; \ + *) echo "Unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \ + esac && \ + dotnet publish hosts/Interfold.Api.Host/Interfold.Api.Host.csproj \ + --configuration Release \ + --runtime "$RID" \ + --self-contained false \ + -p:UseAppHost=false \ + --output /publish + +FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final WORKDIR /app +COPY --from=build /publish/ . -# install hex + rebar -RUN mix local.hex --force && \ - mix local.rebar --force - -# set build ENV -ENV MIX_ENV="prod" - -# install mix dependencies -COPY nostrum nostrum -COPY mix.exs mix.lock ./ -RUN mix deps.get --only $MIX_ENV -RUN mkdir config - -# copy compile-time config files before we compile dependencies -# to ensure any relevant config change will trigger the dependencies -# to be re-compiled. -COPY config/config.exs config/${MIX_ENV}.exs config/ -RUN mix deps.compile - -COPY priv priv - -COPY lib lib - -COPY assets assets - -# Compile assets -RUN mix assets.deploy - -# Compile the release -RUN mix compile - -# Changes to config/runtime.exs don't require recompiling the code -COPY config/runtime.exs config/ - -COPY rel rel -RUN mix sentry.package_source_code -RUN mix release - -# start a new build stage so that the final image will only contain -# the compiled release and other runtime necessities -FROM ${RUNNER_IMAGE} - -RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses6 locales ca-certificates curl gnupg iproute2 iputils-ping jq \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# ---- BEGIN DEPLOYMENT-SPECIFIC CONFIGURATION ---- - -# Install Tailscale keyring -RUN curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.noarmor.gpg | tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null -RUN curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.tailscale-keyring.list | tee /etc/apt/sources.list.d/tailscale.list - -# Install 1Password CLI keyring -RUN curl -sS https://downloads.1password.com/linux/keys/1password.asc | \ - gpg --dearmor --output /usr/share/keyrings/1password-archive-keyring.gpg && \ - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/1password-archive-keyring.gpg] https://downloads.1password.com/linux/debian/$(dpkg --print-architecture) stable main" | \ - tee /etc/apt/sources.list.d/1password.list && \ - mkdir -p /etc/debsig/policies/AC2D62742012EA22/ && \ - curl -sS https://downloads.1password.com/linux/debian/debsig/1password.pol | \ - tee /etc/debsig/policies/AC2D62742012EA22/1password.pol && \ - mkdir -p /usr/share/debsig/keyrings/AC2D62742012EA22 && \ - curl -sS https://downloads.1password.com/linux/keys/1password.asc | \ - gpg --dearmor --output /usr/share/debsig/keyrings/AC2D62742012EA22/debsig.gpg - -# Install Tailscale and 1Password CLI -RUN apt-get update -y && apt-get install -y tailscale 1password-cli \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# ---- END DEPLOYMENT-SPECIFIC CONFIGURATION ---- - -# Set the locale -RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen - -ENV LANG="en_US.UTF-8" -ENV LANGUAGE="en_US:en" -ENV LC_ALL="en_US.UTF-8" - -RUN mkdir /mnesia - -WORKDIR "/app" -RUN chown nobody /app -RUN chown nobody /mnesia - -# set runner ENV -ENV MIX_ENV="prod" - -# Only copy the final release from the build stage -COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/octocon ./ - -RUN chmod -R +x /app/bin - -# DEPLOYMENT-SPECIFIC: Run as `root` so we can run Tailscale in the startup script -# Replace with the following on a non-official deployment: -# USER nobody -USER root - -CMD ["/app/bin/server"] +ENTRYPOINT ["dotnet", "Interfold.Api.Host.dll"] diff --git a/Dockerfile.bootstrapper b/Dockerfile.bootstrapper new file mode 100644 index 00000000..81639b4a --- /dev/null +++ b/Dockerfile.bootstrapper @@ -0,0 +1,29 @@ +# syntax=docker/dockerfile:1.7 +# +# Cross-RID build for the `interfold-bootstrap` self-contained ELF. SDK stage +# is pinned to $BUILDPLATFORM so per-arch publishes cross-compile natively +# (no QEMU). PublishProfile XMLs under +# tools/Interfold.Bootstrapper/Properties/PublishProfiles/ own the +# SelfContained / Trim / single-file settings — this file is just the wrapper. + +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0 AS build +ARG TARGETARCH +WORKDIR /src +COPY . . +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages,sharing=locked \ + case "$TARGETARCH" in \ + amd64) RID=linux-x64 ;; \ + arm64) RID=linux-arm64 ;; \ + arm) RID=linux-arm ;; \ + *) echo "Unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \ + esac && \ + dotnet publish tools/Interfold.Bootstrapper \ + --configuration Release \ + --runtime "$RID" \ + /p:PublishProfile="$RID" && \ + mkdir -p /out && \ + cp "tools/Interfold.Bootstrapper/bin/Release/net10.0/$RID/publish/interfold-bootstrap" /out/ + +# `scratch` so `--output=type=local` extracts only the binary, no rootfs noise. +FROM scratch AS out +COPY --from=build /out/interfold-bootstrap /interfold-bootstrap diff --git a/Interfold.slnx b/Interfold.slnx new file mode 100644 index 00000000..ee894168 --- /dev/null +++ b/Interfold.slnx @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Interfold.slnx.DotSettings b/Interfold.slnx.DotSettings new file mode 100644 index 00000000..89945b53 --- /dev/null +++ b/Interfold.slnx.DotSettings @@ -0,0 +1,5 @@ + + True + False + + False \ No newline at end of file diff --git a/README.md b/README.md index d768a716..36575e37 100644 --- a/README.md +++ b/README.md @@ -2,41 +2,107 @@ **Octocon is the modern, all-in-one toolkit for people with DID and OSDD to manage their disorder and express themselves.** -It's also a -wacky monolith built with [Elixir](https://elixir-lang.org/), [Phoenix](https://www.phoenixframework.org/), -and [ScyllaDB](https://www.scylladb.com/), deployed on a combination of [cloud infrastructure](https://fly.io/) and bare-metal hardware! +> [!WARNING] +> This branch is currently heavily AI generated. +> While it is functional and provides everything required for the client to work, it's still not as performant or reliable as it should be! +> A lot of [work](https://github.com/users/Azyyyyyy/projects/1) is pending to get this new backend built with the care it should have, please be patient <3 -## Project structure +It's also a wacky monolith built with [.NET](https://dotnet.microsoft.com/en-us/learn/dotnet/what-is-dotnet), [ScyllaDB](https://www.scylladb.com/) and [PostgreSQL](https://www.postgresql.org/). Designed to run on bare-metal hardware! + +## Project structure TO EDIT This repository contains the backend code for Octocon, which is structured into three main components: - **octocon**: The core Elixir application that handles the business logic, data processing, clustering, node differentiation, and other backend functionalities. - **octocon-web**: The Phoenix web application that serves the REST API, metrics, and admin dashboard. -- **octocon-discord**: The Discord bot that serves as an alternative interface for interacting with the Octocon platform, including "proxying" as alters. +- **octocon-discord**: Legacy Discord bot integration (currently being phased out and disabled by default). + +## Self-hosting (production) -## Development setup +Interfold ships a single bootstrapper binary that brings a fresh Linux box from `git clone` to a +running stack. The bootstrapper IS an Aspire AppHost — it reuses the same resource graph as the +dev `aspire run` flow, with a Docker Compose publisher and a host-side prep wrapper around it. -To set up a development environment for Octocon, you'll need to have the following prerequisites installed on a Unix-like operating system or WSL: -- [Docker](https://www.docker.com/get-started) -- [Docker Compose](https://docs.docker.com/compose/install/) -- Git +### One-shot install + +1. Download the latest release tarball (TO BE CREATED) and unpack it on a fresh Ubuntu 22.04+/Debian 12+/Fedora 40+/RHEL 9+ box: -Once installed, follow these steps to set up your development environment: -1. Clone the repository: ```bash - git clone - cd octocon + tar -xzf interfold-bootstrap-linux-x64.tar.gz + cd interfold-bootstrap ``` -2. Run the provided setup script: - ```bash - ./dev/setup.sh +2. Author `interfold.bootstrap.json` (or run the binary with no arguments to be prompted): + + ```json + { + "deployment": { + "outputDir": "./deploy", + "domains": ["api.example.com"], + "rootCaName": "Interfold Root CA", + "certYears": 5, + "trustStoreInstall": true + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "ghcr.io/azyyyyyy/interfold-api:latest", + "oauth": { + "googleClientSecret": "...", + "discordClientSecret": "" + } + } ``` - This script will ensure your system is configured correctly, build the necessary Docker images, install dependencies, and perform database migrations. -3. Start the development environment: +3. Run the bootstrapper as root: + ```bash - ./dev/bin/iex + sudo ./interfold-bootstrap bootstrap --config interfold.bootstrap.json ``` - This command will launch an interactive Elixir shell running the Octocon application inside a Docker container. + + This walks through six phases — prereqs → config → secrets → certs → publish → launch — + ending with the API's `/health/ready` returning 200. + +Generated artifacts land under `./deploy/`: + +``` +deploy/ + interfold.bootstrap.json # canonical config (re-emitted on first run) + secrets/secrets.json # mode 0600, never overwritten without --rotate-secrets + certs/{rootCA,leaf}.{crt,key,pfx} + docker-compose.yaml # emitted by the embedded AppHost + .env # bound to compose `${VAR}` references +``` + +### Operational commands + +| Command | Effect | +|---|---| +| `interfold-bootstrap` (default `bootstrap`) | Run all six phases. Idempotent on rerun. | +| `interfold-bootstrap publish` | Run config + secrets + certs + compose-emit; do not `docker compose up`. | +| `interfold-bootstrap up` | Run only `docker compose up -d` + health wait against an already-generated compose file. | +| `interfold-bootstrap rotate-secrets` | Regenerate DB/admin passwords + encryption keypair + pepper, re-emit compose, restart the API. Certs unchanged. | +| `interfold-bootstrap rotate-certs` | Regenerate root CA + leaf cert, re-install into the trust store, re-emit compose. Secrets unchanged. | + +Common flags: + +- `--config ` — point at a populated `interfold.bootstrap.json` instead of using the + default `deploy/interfold.bootstrap.json` lookup or interactive prompt. +- `--output-dir ` — override the artifact root (default `./deploy`). +- `--skip-prereqs` — skip the Docker/openssl/AIO install. Use on re-runs where the host is + already configured. +- `--non-interactive` — fail rather than prompt when config values are missing. + +### Local dev (Aspire) + +Self-hosting and dev use the same resource graph. For local development, run the Aspire +AppHost directly: + +```bash +cd csharp/Interfold.AppHost +aspire run +``` + +This brings the same Postgres + ScyllaDB + bootstrap-auth + API stack up under the Aspire +dashboard. Use `dotnet user-secrets` to populate the `Parameters:*` secrets the AppHost +guards on (see `csharp/Interfold.AppHost/InterfoldAppHost.cs`). ## Contributing @@ -51,14 +117,14 @@ While we respect your time, please note that not every contribution will be acce ## Deployment structure Octocon is designed as a distributed monolith, meaning that while the components have a clear separation of concerns, they share a common codebase which is compiled and deployed as one executable. -Octocon is generally run in a cluster of nodes, which are designed to be globally distributed across the world. One "primary" node interfaces with a generally larger database instance and runs the Discord bot, while "auxiliary" nodes interface with smaller database instances. This allows for low-latency access to the data from anywhere in the world. +Octocon is generally run in a cluster of nodes, which are designed to be globally distributed across the world. One "primary" node interfaces with a generally larger database instance and handles certain types of global state, while "auxiliary" nodes interface with smaller database instances. This allows for low-latency access to the data from anywhere in the world. When not running on Fly.io, an Octocon node knows its role in the overall cluster through an environment variable (`NODE_GROUP`), which determines which parts of the supervision tree it will run, and how it will advertise itself to its peers. In production, Octocon is configured to discover other nodes using the [libcluster](https://github.com/bitwalker/libcluster) library with a custom Tailscale strategy. All that is necessary is for each node to form a Distributed Erlang cluster; Octocon has internal logic to determine and cache each node's role in the cluster through an RPC communication step. There are 3 node groups: -- `primary`: A node running in the primary region, which interfaces with a larger database instance, runs Discord shards, and handles certain types of global state. Other nodes proxy some requests to a `primary` node. +- `primary`: A node running in the primary region, which interfaces with a larger database instance and handles certain types of global state. Other nodes proxy some requests to a `primary` node. - `auxiliary`: A node running in an auxiliary region, which interfaces with a smaller database instance. These nodes are largely only responsible for serving HTTP requests to the API. - `sidecar`: A node responsible for isolating CPU-bound tasks from the rest of the cluster, such as image processing and heavy encryption tasks. Ideally, **at least one** sidecar should be present in the cluster. If no sidecar is present, nodes will run these tasks themselves. @@ -66,44 +132,53 @@ There are 3 node groups: ## Configuration & integrations -Every Octocon node must be configured with a set of environment variables to function properly. These variables are used to connect to the database, configure the Discord bot, and set up other integrations, such as cloud object storage. - -### OAuth -- `APPLE_TEAM_ID` - Used for Sign in with Apple integration. -- `APPLE_CLIENT_ID` - Used for Sign in with Apple integration. -- `APPLE_PRIVATE_KEY_ID` - Used for Sign in with Apple integration. -- `APPLE_PRIVATE_KEY` - Used for Sign in with Apple integration. (PEM in base64 format). -- `GOOGLE_CLIENT_ID` - Used for Google OAuth integration. -- `GOOGLE_CLIENT_SECRET` - Used for Google OAuth integration. -- `DISCORD_CLIENT_ID` - Used for Discord OAuth integration. -- `DISCORD_CLIENT_SECRET` - Used for Discord OAuth integration. -### Discord -- `DISCORD_TOKEN` - The bot token for the Discord bot. -### Security -- `SECRET_KEY_BASE` - Used by Phoenix to sign/encrypt cookies and other secrets; generate with `mix phx.gen.secret`. -- `ENCRYPTION_PEPPER` - Static server-side pepper used during the end-to-end encryption process. -- `GUARDIAN_SECRET_KEY` - Used by Guardian (OAuth) for JWT authentication. -- `ENCRYPTION_PRIVATE_KEY` - The private key used for end-to-end encryption of user data (PEM in base64 format). -### Admin Dashboard -- `ADMIN_USERNAME` - The username for the admin dashboard. -- `ADMIN_PASSWORD` - The password for the admin dashboard (HTTP Basic auth). -### Storage (S3-compatible) -- `S3_ACCESS_HOST` - The URL assets are stored at (e.g. `https://cdn.octocon.app`). -- `S3_ASSET_HOST` - Same as above. -- `S3_HOST` - The storage host (e.g. `xyz.r2.cloudflarestorage.com`). -- `S3_REGION` - The region of the storage bucket (e.g. `auto` for Cloudflare R2). -- `S3_ACCESS_KEY_ID` - The access key ID for the S3-compatible storage service. -- `S3_SECRET_ACCESS_KEY` - The secret access key for the S3-compatible storage service. -- `S3_BUCKET_NAME` - The name of the bucket to store assets in. -### Monitoring/o11y -- `SENTRY_DSN` - The Sentry DSN for error tracking. -- `GRAFANA_HOST` - The URL of the Grafana instance to automatically upload dashboards. -- `GRAFANA_TOKEN` - Auth token for Grafana API access. -### Node configuration -- `NODE_GROUP` - The group this node belongs to (`primary`, `auxiliary`, or `sidecar`). -- `PRIMARY_NODE_COUNT` - Number of primary nodes in the cluster (**values other than 1 are experimental**). -- `POOL_SIZE` - Size of the database connection pool. -- `PORT` - Port to run the Phoenix web server on. -- `TAILSCALE_API_AUTHKEY` - When present, enables Tailscale-based clustering using the provided auth key. -- `NODE_LIST` - When present, enables static clustering with the provided comma-separated list of node IPs. -If neither `TAILSCALE_API_AUTHKEY` nor `NODE_LIST` are present, a single-node cluster is assumed, which will run as a `primary` node. \ No newline at end of file +> **Full reference:** [`docs/configuration.md`](docs/configuration.md) documents every +> configuration layer, every env var, the `internal.secrets` row inventory, the boot-time +> ordering between Kestrel / `SecretsBootstrapService` / migration services, and the +> rotation story. This README only covers the short operator-facing surface. + +For a self-hosted deployment, the bootstrapper writes: + +- `deploy/secrets/secrets.json` (mode 0600) — auto-generated DB passwords, encryption + pepper, JWT signing keypairs (RSA-2048 + ES256), deep-link HMAC secret, leaf PFX + password. Never overwritten without `--rotate-secrets`. +- `deploy/.env` — compose-bound subset that needs to be visible to Docker at `up` time + (DB usernames/passwords, encryption private key, encryption pepper). Re-emitted from + `secrets.json` on every bootstrapper run. +- `internal.secrets` rows inside Postgres — the durable, in-cluster source of truth for + everything the API reads at runtime (auth/OAuth secrets, JWT keys, deep-link HMAC, + leaf PFX password, Scylla credentials). Seeded by `DatabaseInitPhase`. + +The only secrets you set by hand live in [`interfold.bootstrap.json`](#one-shot-install): +OAuth client secrets (`googleClientSecret`, `discordClientSecret`, `appleClientSecret`). +Leave them empty to skip the corresponding `internal.secrets` row. + +### Env vars the bootstrapper manages (do not set by hand) + +These are written into `deploy/.env` by the publish phase. For the OAuth secrets, the +`.env` line is a placeholder — `SecretsBootstrapService` overwrites the in-memory value +at API startup from `internal.secrets`, so editing the `.env` by hand has no effect on +secrets. The OAuth client IDs ARE read directly from `.env` (they're public values +rendered into the OAuth redirect URL), so rerun the bootstrapper to change them rather +than editing `.env` by hand. + +- `OCTOCON_{GOOGLE,DISCORD,APPLE}_OAUTH_CLIENT_ID` — sourced from + `BootstrapConfig.oauth.{google,discord,apple}ClientId`; leaving a provider's ID empty + disables that provider entirely. +- `OCTOCON_{GOOGLE,DISCORD,APPLE}_OAUTH_CLIENT_SECRET` +- `OCTOCON_POSTGRES_CONNECTION` +- `OCTOCON_PERSISTENCE`, `OCTOCON_SINGLE_SCYLLA_INSTANCE` +- `OCTOCON_SCYLLA_KEYSPACE` — per-instance region identity (one of `nam`/`eur`/`sam`/`sas`/`eas`/`ocn`/`gdpr`). Sourced from `BootstrapConfig.scyllaKeyspace`; defaults to `nam`. The interactive form constrains the row to the seven valid values. +- `OCTOCON_AUTH_CALLBACK_BASE_URL` — base URL the API's OAuth callbacks redirect to. Sourced from `BootstrapConfig.apiRuntime.callbackBaseUrl`; defaults derive to `{scheme}://{domains[0]}` when blank (scheme follows `deployment.webHttps`). +- `OCTOCON_JWT_AUTHORITY` / `OCTOCON_JWT_AUDIENCE` — JWT `iss` / `aud` claims. Sourced from `BootstrapConfig.apiRuntime.jwtAuthority` / `apiRuntime.jwtAudience`; authority derives the same way as the callback URL; audience defaults to `octocon`. +- `OCTOCON_CORS_ALLOWED_ORIGINS` — comma-separated CORS allow-list. Sourced from `BootstrapConfig.apiRuntime.corsAllowedOrigins`; defaults to one entry per `deployment.domains` so a fresh bootstrap never ships with the API's "allow any origin" fallback. +- `OCTOCON_NODE_GROUP` — cluster node role (`primary` / `auxiliary` / `sidecar`). Sourced from `BootstrapConfig.cluster.nodeGroup`; defaults to `auxiliary`. Fly.io stacks override via `FLY_PROCESS_GROUP` at runtime. +- `OCTOCON_AVATAR_STORAGE_ROOT` / `OCTOCON_AVATAR_PUBLIC_BASE` — local avatar storage. Self-host compose always sets the container root to `/app/data/avatars` and bind-mounts `BootstrapConfig.storage.avatarStorageRoot` (blank → `{outputDir}/data/avatars`). `avatarPublicBase` blank means the API serves `/avatars/*` itself; set an https URL to stamp CDN links. +- `OCTOCON_OTLP_ENDPOINT` — gRPC OTLP endpoint for traces/metrics, e.g. `http://localhost:4317`. Sourced from `BootstrapConfig.observability.otlpEndpoint`; empty disables the exporter. +- `OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD` — WebSocket batched-payload flush threshold (bytes). Sourced from `BootstrapConfig.socket.batchBytesThreshold`; nullable — `null` means the API uses its compile-time default. +- `OCTOCON_DB_RETRY_ATTEMPTS` / `OCTOCON_DB_RETRY_INITIAL_DELAY_MS` / `OCTOCON_DB_RETRY_MAX_DELAY_MS` / `OCTOCON_HYDRATION_MAX_CONCURRENCY` — DB-retry strategy + per-request hydration fan-out cap. Sourced from `BootstrapConfig.persistence.*`; all four have non-null defaults that match the API's compile-time fallbacks. + +The encryption pepper, JWT signing keys, deep-link HMAC secret, and leaf PFX password +have no env-var representation at all — they live in `internal.secrets` exclusively and +are read directly by `SecretsBootstrapService` (or `Program.LoadLeafPfxPasswordFromStoreIfNeeded` +for the PFX password) at startup. \ No newline at end of file diff --git a/apis/Interfold.Alters.Api/Controllers/AltersController.cs b/apis/Interfold.Alters.Api/Controllers/AltersController.cs new file mode 100644 index 00000000..157b057c --- /dev/null +++ b/apis/Interfold.Alters.Api/Controllers/AltersController.cs @@ -0,0 +1,162 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Contracts.Models.Read; +using Interfold.Alters.Domain; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Contracts.Validation; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Alters.Api.Controllers; + +[Route("api/systems/me/alters")] +public sealed class AltersController : InterfoldControllerBase +{ + private readonly IAlterRepository _alterRepository; + private readonly CreateAlterCommandHandler _createHandler; + private readonly UpdateAlterCommandHandler _updateHandler; + private readonly DeleteAlterCommandHandler _deleteHandler; + private readonly IAvatarStorage _avatarStorage; + + public AltersController( + IAlterRepository alterRepository, + CreateAlterCommandHandler createHandler, + UpdateAlterCommandHandler updateHandler, + DeleteAlterCommandHandler deleteHandler, + IAvatarStorage avatarStorage) + { + _alterRepository = alterRepository; + _createHandler = createHandler; + _updateHandler = updateHandler; + _deleteHandler = deleteHandler; + _avatarStorage = avatarStorage; + } + + [HttpGet] + public async Task>> List(CancellationToken ct) + { + var alters = await _alterRepository.ListAsync(PrincipalId, ct); + return new SuccessResponse>(alters); + } + + [HttpGet("{alterId:int}")] + public async Task> Show([FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + var alter = await _alterRepository.GetAsync(PrincipalId, alterId, ct); + if (alter is not null) + { + alter.AvatarUrl = QualifyAvatar(alter); + } + return OkOrNotFound(alter, "Alter not found.", ErrorCodes.AlterNotFound); + } + + [HttpPost] + public async Task> Create([FromBody] CreateAlterRequest req, CancellationToken ct) + { + var principal = PrincipalId; + var envelope = BuildEnvelope(OperationIds.AlterCreate, new CreateAlterCommand(req.Name, TimeProvider.GetUtcNow())); + + return await CommandCreatedAsync( + await _createHandler.HandleAsync(envelope, ct), + async (res) => await _alterRepository.GetAsync(principal, res.AlterId, ct), + locationSelector: res => $"/api/systems/me/alters/{res.AlterId}" + ); + } + + [HttpPatch("{alterId:int}")] + public async Task Update([FromRoute][ValidAlterId] AlterId alterId, [FromBody] UpdateAlterRequest req, CancellationToken ct) + { + var fields = req.Fields?.Select(f => new AlterFieldCommand(f.Id, f.Value)).ToList(); + + var payload = new UpdateAlterCommand + { + AlterId = alterId, + Name = req.Name, + Description = req.Description, + Color = req.Color, + Pronouns = req.Pronouns, + SecurityLevel = req.SecurityLevel, + Fields = fields, + ProxyName = req.ProxyName, + Alias = req.Alias, + Untracked = req.Untracked, + Archived = req.Archived, + Pinned = req.Pinned, + UpdatedAt = TimeProvider.GetUtcNow(), + }; + + return await DispatchNoContentAsync(_updateHandler, OperationIds.AlterUpdate, payload, ct); + } + + [HttpDelete("{alterId:int}")] + public async Task Delete([FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + return await DispatchNoContentAsync(_deleteHandler, OperationIds.AlterDelete, new DeleteAlterCommand(alterId) + , ct); + } + + [HttpPut("{alterId:int}/avatar")] + [Consumes("multipart/form-data")] + public async Task UploadAvatarMultipart([FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + return await HandleAvatarUploadAsync( + async (c) => await _alterRepository.GetAsync(PrincipalId, alterId, c), + async (principal, stream, c) => await _avatarStorage.SaveAlterAvatarAsync(principal, alterId, stream, c), + async (url, c) => CommandNoContent(await _updateHandler.HandleAsync(BuildEnvelope(OperationIds.AlterAvatarUpload, new UpdateAlterCommand + { + AlterId = alterId, + AvatarUrl = url, + AvatarSource = AvatarSource.Local, + UpdatedAt = TimeProvider.GetUtcNow(), + }), c)), + _avatarStorage, + ct); + } + + /// + /// JSON sibling of : stores the supplied URL on + /// avatar_url with avatar_source = External without fetching the bytes. + /// + [HttpPut("{alterId:int}/avatar")] + [Consumes("application/json")] + public async Task UploadAvatarByUrl([FromRoute][ValidAlterId] AlterId alterId, [FromBody] AvatarUrlUploadRequest req, CancellationToken ct) + { + if (req is null) + return new ErrorResponse("Avatar URL payload required.", ErrorCodes.AvatarUrlInvalid, System.Net.HttpStatusCode.BadRequest); + + return await HandleAvatarUrlUploadAsync( + req.Url.Value, + async (c) => await _alterRepository.GetAsync(PrincipalId, alterId, c), + async (url, c) => CommandNoContent(await _updateHandler.HandleAsync(BuildEnvelope(OperationIds.AlterAvatarUpload, new UpdateAlterCommand + { + AlterId = alterId, + AvatarUrl = url, + AvatarSource = AvatarSource.External, + UpdatedAt = TimeProvider.GetUtcNow(), + }), c)), + _avatarStorage, + ct); + } + + [HttpDelete("{alterId:int}/avatar")] + public async Task DeleteAvatar([FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + return await HandleAvatarDeleteAsync( + async (c) => await _alterRepository.GetAsync(PrincipalId, alterId, c), + async (c) => CommandNoContent(await _updateHandler.HandleAsync(BuildEnvelope(OperationIds.AlterAvatarDelete, new UpdateAlterCommand + { + AlterId = alterId, + UpdatedAt = TimeProvider.GetUtcNow(), + ClearAvatar = true, + }), c)), + _avatarStorage, + ct); + } +} diff --git a/apis/Interfold.Alters.Api/DependencyInjection/AltersModuleServiceCollectionExtensions.cs b/apis/Interfold.Alters.Api/DependencyInjection/AltersModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..b563814f --- /dev/null +++ b/apis/Interfold.Alters.Api/DependencyInjection/AltersModuleServiceCollectionExtensions.cs @@ -0,0 +1,25 @@ +using Interfold.Alters.Domain; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Api.DependencyInjection; + +/// Alters feature module — the DI equivalent of the three alter-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class AltersModuleServiceCollectionExtensions +{ + /// Registers everything the Alters feature owns: the three alter + /// command-handler singletons (Create/Update/Delete) plus the + /// adapter over IAlterRepository + /// consumed by Journals and Tags handlers (spine-level abstraction so those + /// features don't back-reference Alters.Domain). IAlterRepository itself + /// is registered by the active persistence adapter (Scylla / Postgres / InMemory) + /// via AddInterfoldPersistence, so no repository registration lives here. + public static IServiceCollection AddAltersModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Alters.Api/Interfold.Alters.Api.csproj b/apis/Interfold.Alters.Api/Interfold.Alters.Api.csproj new file mode 100644 index 00000000..0a80fe2b --- /dev/null +++ b/apis/Interfold.Alters.Api/Interfold.Alters.Api.csproj @@ -0,0 +1,29 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Auth.Api/Auth/JwtEs256Validator.cs b/apis/Interfold.Auth.Api/Auth/JwtEs256Validator.cs new file mode 100644 index 00000000..a0b79382 --- /dev/null +++ b/apis/Interfold.Auth.Api/Auth/JwtEs256Validator.cs @@ -0,0 +1,253 @@ +using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Auth.Api.Helpers; +using Microsoft.IdentityModel.Tokens; + +namespace Interfold.Auth.Api.Auth; + +public sealed class ValidationResult +{ + public bool IsValid { get; } + public string? ErrorMessage { get; } + public ClaimsPrincipal? Principal { get; } + + public ValidationResult(bool isValid, string? errorMessage = null, ClaimsPrincipal? principal = null) + { + IsValid = isValid; + ErrorMessage = errorMessage; + Principal = principal; + } + + public static ValidationResult Success(ClaimsPrincipal principal) => new(true, null, principal); + public static ValidationResult Failure(string errorMessage) => new(false, errorMessage, null); +} + +/// ES256 JWT validation (signature + aud + exp). +public static class JwtEs256Validator +{ + public static Task ValidateAsync( + string token, + string keyPem, + string expectedAudience, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(token)) + return Task.FromResult(ValidationResult.Failure("Token is empty.")); + + if (string.IsNullOrWhiteSpace(keyPem)) + return Task.FromResult(ValidationResult.Failure("Verification key is empty.")); + + try + { + var parts = token.Split('.'); + if (parts.Length != 3) + return Task.FromResult(ValidationResult.Failure("Token is not a valid JWS compact token.")); + + string headerJson; + try + { + headerJson = Encoding.UTF8.GetString(parts[0].Base64UrlDecode()); + } + catch (Exception ex) + { + return Task.FromResult(ValidationResult.Failure($"Failed to decode header: {ex.Message}")); + } + + var header = JsonSerializer.Deserialize(headerJson); + if (header is null || string.IsNullOrWhiteSpace(header.Alg)) + { + return Task.FromResult(ValidationResult.Failure("Missing JWT algorithm.")); + } + + if (!string.Equals(header.Alg, JwsHeader.Es256, StringComparison.Ordinal)) + { + return Task.FromResult(ValidationResult.Failure($"Algorithm '{header.Alg}' is not supported. Only ES256 is accepted.")); + } + + var signingInput = Encoding.UTF8.GetBytes(parts[0] + "." + parts[1]); + byte[] signatureBytes; + try + { + signatureBytes = parts[2].Base64UrlDecode(); + } + catch (Exception ex) + { + return Task.FromResult(ValidationResult.Failure($"Failed to decode signature: {ex.Message}")); + } + + using var ecdsa = ECDsa.Create(); + try + { + var normalizedPem = PemUtil.NormalizePem(keyPem); + ecdsa.ImportFromPem(normalizedPem.AsSpan()); + } + catch (CryptographicException ex) + { + return Task.FromResult(ValidationResult.Failure($"Failed to import verification key: {ex.Message}")); + } + + if (!ecdsa.VerifyData( + signingInput, + signatureBytes, + HashAlgorithmName.SHA256, + DSASignatureFormat.IeeeP1363FixedFieldConcatenation)) + { + return Task.FromResult(ValidationResult.Failure("Invalid JWT signature.")); + } + + string payloadJson; + try + { + payloadJson = Encoding.UTF8.GetString(parts[1].Base64UrlDecode()); + } + catch (Exception ex) + { + return Task.FromResult(ValidationResult.Failure($"Failed to decode payload: {ex.Message}")); + } + + using var doc = JsonDocument.Parse(payloadJson); + var root = doc.RootElement; + + if (!root.TryGetProperty("aud", out var audProp) || audProp.GetString() != expectedAudience) + { + return Task.FromResult(ValidationResult.Failure("Audience mismatch.")); + } + + if (root.TryGetProperty("exp", out var expProp)) + { + if (expProp.TryGetInt64(out var expSeconds)) + { + var expTime = DateTimeOffset.FromUnixTimeSeconds(expSeconds); + if (expTime < DateTimeOffset.UtcNow) + { + return Task.FromResult(ValidationResult.Failure("Token is expired.")); + } + } + else + { + return Task.FromResult(ValidationResult.Failure("Invalid expiration format.")); + } + } + else + { + return Task.FromResult(ValidationResult.Failure("Missing expiration claim.")); + } + + var claims = new List(); + if (root.TryGetProperty("sub", out var subProp)) + { + claims.Add(new Claim(ClaimTypes.NameIdentifier, subProp.GetString() ?? string.Empty)); + } + if (root.TryGetProperty("role", out var roleProp)) + { + claims.Add(new Claim(ClaimTypes.Role, roleProp.GetString() ?? string.Empty)); + } + + var identity = new ClaimsIdentity(claims, "Jwt", ClaimTypes.NameIdentifier, ClaimTypes.Role); + var principal = new ClaimsPrincipal(identity); + + return Task.FromResult(ValidationResult.Success(principal)); + } + catch (Exception ex) + { + return Task.FromResult(ValidationResult.Failure($"Validation failed: {ex.Message}")); + } + } + + /// Signature-only validation across any of the provided keys. Throws + /// when nothing matches. + public static SecurityToken ValidateSignature(string token, string[] pems, bool useJsonWebToken) + { + if (string.IsNullOrWhiteSpace(token)) + { + throw new SecurityTokenInvalidSignatureException("Token is empty."); + } + + var parts = token.Split('.'); + if (parts.Length != 3) + { + throw new SecurityTokenInvalidSignatureException("Token is not a valid JWS compact token."); + } + + string headerJson; + try + { + headerJson = Encoding.UTF8.GetString(parts[0].Base64UrlDecode()); + } + catch (Exception ex) + { + throw new SecurityTokenInvalidSignatureException($"Failed to decode header: {ex.Message}"); + } + + var header = JsonSerializer.Deserialize(headerJson); + if (header is null || string.IsNullOrWhiteSpace(header.Alg)) + { + throw new SecurityTokenInvalidSignatureException("Missing JWT algorithm."); + } + + if (!string.Equals(header.Alg, JwsHeader.Es256, StringComparison.Ordinal)) + { + throw new SecurityTokenInvalidSignatureException($"Algorithm '{header.Alg}' is not supported. Only ES256 is accepted."); + } + + if (pems == null || pems.Length == 0) + { + throw new SecurityTokenInvalidSignatureException("No ES256 verification keys are configured."); + } + + var signingInput = Encoding.UTF8.GetBytes(parts[0] + "." + parts[1]); + byte[] signatureBytes; + try + { + signatureBytes = parts[2].Base64UrlDecode(); + } + catch (Exception ex) + { + throw new SecurityTokenInvalidSignatureException($"Failed to decode signature: {ex.Message}"); + } + + int attemptCount = 0; + foreach (var rawPem in pems) + { + attemptCount++; + using var ecdsa = ECDsa.Create(); + try + { + var normalizedPem = PemUtil.NormalizePem(rawPem); + ecdsa.ImportFromPem(normalizedPem.AsSpan()); + } + catch (CryptographicException) + { + continue; + } + + try + { + if (ecdsa.VerifyData( + signingInput, + signatureBytes, + HashAlgorithmName.SHA256, + DSASignatureFormat.IeeeP1363FixedFieldConcatenation)) + { + if (useJsonWebToken) + { + return new Microsoft.IdentityModel.JsonWebTokens.JsonWebToken(token); + } + else + { + return new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(token); + } + } + } + catch (CryptographicException) + { + continue; + } + } + + throw new SecurityTokenInvalidSignatureException( + $"Invalid JWT signature: tested {attemptCount} verification key(s) but none matched."); + } +} diff --git a/apis/Interfold.Auth.Api/Auth/OAuthChallengeServiceCollectionExtensions.cs b/apis/Interfold.Auth.Api/Auth/OAuthChallengeServiceCollectionExtensions.cs new file mode 100644 index 00000000..9e67ac20 --- /dev/null +++ b/apis/Interfold.Auth.Api/Auth/OAuthChallengeServiceCollectionExtensions.cs @@ -0,0 +1,77 @@ +namespace Interfold.Auth.Api.Auth; + +/// Registers OAuth challenge redirect schemes. Provider endpoint + scope set are +/// constants; only client_id is per-deployment. +internal static class OAuthChallengeServiceCollectionExtensions +{ + public const string DiscordSchemeName = "oauth-discord"; + public const string GoogleSchemeName = "oauth-google"; + public const string AppleSchemeName = "oauth-apple"; + + public const string DiscordEndpoint = "https://discord.com/api/oauth2/authorize"; + public const string GoogleEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"; + public const string AppleEndpoint = "https://appleid.apple.com/auth/authorize"; + + // scope=identify is the minimum ExtractProviderIdentityAsync needs. + private static readonly IReadOnlyDictionary DiscordParams = new Dictionary + { + [OAuthQueryKeys.ResponseType] = OAuthQueryKeys.Code, + [OAuthQueryKeys.Scope] = "identify", + }; + + // userinfo.email is the minimum GoogleOAuthService.ExchangeCodeForEmailAsync needs. + private static readonly IReadOnlyDictionary GoogleParams = new Dictionary + { + [OAuthQueryKeys.ResponseType] = OAuthQueryKeys.Code, + [OAuthQueryKeys.Scope] = "https://www.googleapis.com/auth/userinfo.email", + }; + + // Apple requires response_mode=form_post whenever scope includes name/email. + private static readonly IReadOnlyDictionary AppleParams = new Dictionary + { + [OAuthQueryKeys.ResponseType] = OAuthQueryKeys.Code, + [OAuthQueryKeys.ResponseMode] = "form_post", + [OAuthQueryKeys.Scope] = "name email", + }; + + /// Registers the challenge scheme for each provider whose client ID is set. + /// Missing client ID → scheme stays unregistered and the challenge falls through to 403. + /// Takes the three IDs individually so the caller reads them from + /// builder.Configuration without resolving the options monitor pre-PostConfigure. + public static IServiceCollection AddInterfoldAuthChallengeSchemes( + this IServiceCollection services, + string? discordOAuthClientId, + string? googleOAuthClientId, + string? appleOAuthClientId) + { + AddSchemeIfConfigured(services, DiscordSchemeName, DiscordEndpoint, discordOAuthClientId, DiscordParams); + AddSchemeIfConfigured(services, GoogleSchemeName, GoogleEndpoint, googleOAuthClientId, GoogleParams); + AddSchemeIfConfigured(services, AppleSchemeName, AppleEndpoint, appleOAuthClientId, AppleParams); + + return services; + } + + private static void AddSchemeIfConfigured( + IServiceCollection services, + string scheme, + string endpoint, + string? clientId, + IReadOnlyDictionary baseParameters) + { + if (string.IsNullOrWhiteSpace(clientId)) + return; + + var parameters = new Dictionary(baseParameters, StringComparer.Ordinal) + { + [OAuthQueryKeys.ClientId] = clientId, + }; + + services + .AddAuthentication() + .AddScheme(scheme, options => + { + options.AuthorizationEndpoint = endpoint; + options.AdditionalParameters = parameters; + }); + } +} diff --git a/apis/Interfold.Auth.Api/Auth/OAuthQueryKeys.cs b/apis/Interfold.Auth.Api/Auth/OAuthQueryKeys.cs new file mode 100644 index 00000000..86607e9c --- /dev/null +++ b/apis/Interfold.Auth.Api/Auth/OAuthQueryKeys.cs @@ -0,0 +1,36 @@ +namespace Interfold.Auth.Api.Auth; + +/// +/// The OAuth 2.0 / OpenID Connect query- and form-parameter names the auth flows read and +/// write. Wire-frozen: the spellings come from the OAuth spec, the providers' token +/// endpoints, and the legacy client callback contract. +/// +internal static class OAuthQueryKeys +{ + // Standard OAuth 2.0 authorization/token vocabulary + public const string Code = "code"; + public const string RedirectUri = "redirect_uri"; + public const string ClientId = "client_id"; + public const string ClientSecret = "client_secret"; + public const string GrantType = "grant_type"; + public const string ResponseType = "response_type"; + public const string ResponseMode = "response_mode"; + public const string Scope = "scope"; + public const string IdToken = "id_token"; + public const string Sub = "sub"; + + /// The grant_type value for the authorization-code exchange. + public const string AuthorizationCodeGrant = "authorization_code"; + + // Legacy/manual identity fallbacks accepted on the callback query string + public const string Uid = "uid"; + public const string DiscordIdFallback = "discord_id"; + public const string AppleIdFallback = "apple_id"; + public const string Id = "id"; + public const string Email = "email"; + + // Interfold-specific link/callback plumbing + public const string LinkToken = "link_token"; + public const string CallbackToken = "token"; + public const string CallbackId = "id"; +} diff --git a/apis/Interfold.Auth.Api/Auth/PemUtil.cs b/apis/Interfold.Auth.Api/Auth/PemUtil.cs new file mode 100644 index 00000000..e0030636 --- /dev/null +++ b/apis/Interfold.Auth.Api/Auth/PemUtil.cs @@ -0,0 +1,28 @@ +namespace Interfold.Auth.Api.Auth; + +/// +/// Utility methods for handling PEM format keys. +/// +public static class PemUtil +{ + /// + /// PEMs arrive from env vars / DB with both real and escaped line endings; collapse all + /// of them to '\n' so ECDsa.ImportFromPem accepts the result. + /// + public static string NormalizePem(string pem) + { + if (string.IsNullOrWhiteSpace(pem)) + return pem; + + // PEMs arrive from env vars / DB with both real and escaped line endings; collapse all + // of them to '\n' so ECDsa.ImportFromPem accepts the result. + var normalized = pem + .Replace(@"\r\n", "\n", StringComparison.Ordinal) + .Replace("\\r", "\n", StringComparison.Ordinal) + .Replace("\\n", "\n", StringComparison.Ordinal) + .Replace("\r\n", "\n", StringComparison.Ordinal) + .Replace("\r", "\n", StringComparison.Ordinal); + + return normalized; + } +} diff --git a/apis/Interfold.Auth.Api/Auth/RedirectChallengeAuthentication.cs b/apis/Interfold.Auth.Api/Auth/RedirectChallengeAuthentication.cs new file mode 100644 index 00000000..f99a866a --- /dev/null +++ b/apis/Interfold.Auth.Api/Auth/RedirectChallengeAuthentication.cs @@ -0,0 +1,56 @@ +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.Authentication; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Auth; + +public sealed class RedirectChallengeOptions : AuthenticationSchemeOptions +{ + public string? AuthorizationEndpoint { get; set; } + public Dictionary? AdditionalParameters { get; set; } +} + +public sealed class RedirectChallengeAuthenticationHandler : AuthenticationHandler +{ + public RedirectChallengeAuthenticationHandler( + IOptionsMonitor options, + ILoggerFactory logger, + UrlEncoder encoder) + : base(options, logger, encoder) + { + } + + protected override Task HandleAuthenticateAsync() + => Task.FromResult(AuthenticateResult.NoResult()); + + protected override Task HandleChallengeAsync(AuthenticationProperties properties) + { + if (string.IsNullOrWhiteSpace(Options.AuthorizationEndpoint)) + { + Response.StatusCode = StatusCodes.Status403Forbidden; + return Task.CompletedTask; + } + + var location = Options.AuthorizationEndpoint!; + var hasQueryString = location.Contains('?', StringComparison.Ordinal); + + if (Options.AdditionalParameters?.Count > 0) + { + foreach (var (key, value) in Options.AdditionalParameters) + { + var delimiter = hasQueryString ? "&" : "?"; + location = $"{location}{delimiter}{Uri.EscapeDataString(key)}={Uri.EscapeDataString(value)}"; + hasQueryString = true; + } + } + + if (!string.IsNullOrWhiteSpace(properties?.RedirectUri)) + { + var delimiter = hasQueryString ? "&" : "?"; + location = $"{location}{delimiter}redirect_uri={Uri.EscapeDataString(properties.RedirectUri)}"; + } + + Response.Redirect(location); + return Task.CompletedTask; + } +} diff --git a/apis/Interfold.Auth.Api/Controllers/AuthController.cs b/apis/Interfold.Auth.Api/Controllers/AuthController.cs new file mode 100644 index 00000000..7bb58209 --- /dev/null +++ b/apis/Interfold.Auth.Api/Controllers/AuthController.cs @@ -0,0 +1,187 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Controllers.Base; +using Interfold.Auth.Api.Services; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Contracts.Enums; +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Auth.Domain; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Controllers; + +[Route("auth")] +public sealed class AuthController : OAuthControllerBase +{ + private readonly AuthenticateOAuthCommandHandler _authHandler; + private readonly RecordAuthTokenCommandHandler _recordTokenHandler; + private readonly RevokeAuthTokenCommandHandler _revokeTokenHandler; + + public AuthController( + AuthenticateOAuthCommandHandler authHandler, + IOptionsMonitor authOptions, + IAuthenticationSchemeProvider schemeProvider, + GoogleOAuthService googleOAuth, + DiscordOAuthService discordOAuth, + AppleOAuthService appleOAuth, + RecordAuthTokenCommandHandler recordTokenHandler, + RevokeAuthTokenCommandHandler revokeTokenHandler) + : base(authOptions, schemeProvider, googleOAuth, discordOAuth, appleOAuth) + { + _authHandler = authHandler; + _recordTokenHandler = recordTokenHandler; + _revokeTokenHandler = revokeTokenHandler; + } + + protected override string CallbackRoutePrefix => "auth"; + + [AllowAnonymous] + [HttpGet("{provider}")] + public async Task Begin([FromRoute] string provider) + { + if (!provider.TryParseWire(out var oauthProvider)) + return UnsupportedProviderResponse(provider); + + StoreRedirectUriCookie(InterfoldCookieNames.AuthRedirectUri); + + var challenge = await IssueChallengeIfRegisteredAsync( + oauthProvider, OperationIds.QueryAuthOAuthRequest); + + if (challenge is not null) + return challenge; + + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.QueryAuthOAuthRequest.Value; + return StatusCode(StatusCodes.Status403Forbidden, string.Empty); + } + + [AllowAnonymous] + [HttpGet("{provider}/callback")] + public Task CallbackGet([FromRoute] string provider) + => Callback(provider); + + [AllowAnonymous] + [HttpPost("{provider}/callback")] + public Task CallbackPost([FromRoute] string provider) + => Callback(provider); + + /// + /// Revokes the current authenticated token (logout). + /// Requires authentication. The JTI claim from the current token is extracted and marked as revoked. + /// After revocation, the token cannot be used for subsequent requests. + /// + [Authorize] + [HttpPost("revoke")] + public async Task RevokeToken() + { + // Jti.From wraps the possibly-null JWT claim in one call so the null check operates + // on the typed Jti?, not on a bare string local. A bare string local would stay + // alive across the null-check / error-return boundary and any incidental log + // statement in that window would leak the token id verbatim through raw-string + // interpolation. + var jti = Jti.From(User.FindFirst(JwtClaimNames.Jti)?.Value); + if (jti is null) + { + return BadRequest(new ErrorResponse("Token is missing JTI claim.", ErrorCodes.InvalidToken)); + } + + await _revokeTokenHandler.HandleAsync(BuildEnvelope(OperationIds.AuthRevokeToken, new RevokeAuthTokenCommand(jti.Value)), HttpContext.RequestAborted); + + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.AuthRevokeToken.Value; + return NoContent(); + } + + private async Task Callback(string provider) + { + if (!provider.TryParseWire(out var oauthProvider)) + return UnsupportedProviderResponse(provider); + + // ExtractProviderIdentityAsync returns a ProviderIdentity? that the account repo + // dispatches on directly — no rewrap of a raw string that was just unwrapped + // inside the OAuth service. + var identity = await ExtractProviderIdentityAsync(oauthProvider); + if (identity is not { } typedIdentity) + { + return OAuthIdentityFailureResponse(); + } + + // [AllowAnonymous] endpoint: the middleware doesn't populate the principal, but + // BuildEnvelope stamps the synthetic AnonymousPrincipalId in that case (the OAuth + // handler resolves the real system id off the identity itself and never reads + // command.PrincipalId), so we can share the standard envelope shape. + var envelope = BuildEnvelope(OperationIds.AuthOAuthCallback, new AuthenticateOAuthCommand(typedIdentity)); + + var result = await _authHandler.HandleAsync(envelope, HttpContext.RequestAborted); + if (!result.Accepted) + { + return StatusCode(StatusCodes.Status403Forbidden, "Failed to authenticate. Did you use the same account to sign in before?"); + } + + var systemId = result.Result; + + var token = await IssueDeepLinkTokenAsync(systemId); + + var clientRedirectUri = Request.Cookies[InterfoldCookieNames.AuthRedirectUri]; + Response.Cookies.Delete(InterfoldCookieNames.AuthRedirectUri); + + // The client (web/desktop/mobile) is responsible for supplying its own redirect_uri + // on the initial GET /auth/{provider}?redirect_uri=... call; we store that in the + // octocon_auth_redirect_uri cookie there and read it back here after the provider + // round-trip. A missing cookie means the client either never set it or the cookie + // was dropped — that's a client bug, surface it loudly rather than papering over it + // with a server-configured fallback. + if (string.IsNullOrWhiteSpace(clientRedirectUri)) + { + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.AuthOAuthCallback.Value; + return BadRequest(new ErrorResponse( + "Missing client-supplied redirect_uri.", + ErrorCodes.MissingRedirectUri, + detail: "Pass redirect_uri on GET /auth/{provider} so the callback knows where to send the token.")); + } + + var separator = clientRedirectUri.Contains('?') ? '&' : '?'; + var redirectUrl = $"{clientRedirectUri}{separator}{OAuthQueryKeys.CallbackToken}={Uri.EscapeDataString(token)}&{OAuthQueryKeys.CallbackId}={Uri.EscapeDataString(systemId)}"; + + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.AuthOAuthCallback.Value; + return Redirect(redirectUrl); + } + + private async Task IssueDeepLinkTokenAsync(SystemId systemId) + { + var authConfig = AuthOptions.CurrentValue; + // Jti.NewJti() mints + wraps in one call so the raw JTI string doesn't live as a + // bare local across the CreateToken and RecordTokenAsync sites. Any incidental + // log or exception-with-locals between mint and wrap would emit the unredacted + // JTI; the wrapper's ToString redacts. + var jti = Jti.NewJti(); + + // Set expiry to 100 years in the future. This is practically permanent + // but avoids DateTimeOffset.MaxValue which can cause int64 overflow on validation. + // If a token is compromised, it can be revoked explicitly via POST /auth/revoke. + var now = TimeProvider.GetUtcNow(); + var expiresAt = now.AddYears(100); + + var token = AuthHelper.CreateToken(authConfig, expiresAt, now, jti, systemId); + + // Record the issued token for revocation tracking + var envelope = new CommandEnvelope( + OperationIds.AuthOAuthCallback, + Guid.NewGuid(), + ScopedSystemId.ParseScoped(systemId.Value), + GetIdempotencyKey(), + TimeProvider.GetUtcNow(), + new RecordAuthTokenCommand(jti, systemId, expiresAt) + ); + + await _recordTokenHandler.HandleAsync(envelope, HttpContext.RequestAborted); + + // JWS Compact Serialization: base64url(header).base64url(payload).base64url(signature) + return token; + } +} diff --git a/apis/Interfold.Auth.Api/Controllers/AuthLinkController.cs b/apis/Interfold.Auth.Api/Controllers/AuthLinkController.cs new file mode 100644 index 00000000..215eaf67 --- /dev/null +++ b/apis/Interfold.Auth.Api/Controllers/AuthLinkController.cs @@ -0,0 +1,173 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Controllers.Base; +using Interfold.Auth.Api.Models; +using Interfold.Auth.Api.Services; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Contracts.Enums; +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Auth.Domain; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Controllers; + +[AllowAnonymous] +[Route("auth/link")] +public sealed class AuthLinkController : OAuthControllerBase +{ + private const string LinkTokenCookieName = InterfoldCookieNames.LinkToken; + private const string RedirectUriCookieName = InterfoldCookieNames.LinkRedirectUri; + + private readonly LinkOAuthIdentityCommandHandler _linkHandler; + + public AuthLinkController( + LinkOAuthIdentityCommandHandler linkHandler, + IOptionsMonitor authOptions, + IAuthenticationSchemeProvider schemeProvider, + GoogleOAuthService googleOAuth, + DiscordOAuthService discordOAuth, + AppleOAuthService appleOAuth) + : base(authOptions, schemeProvider, googleOAuth, discordOAuth, appleOAuth) + { + _linkHandler = linkHandler; + } + + protected override string CallbackRoutePrefix => "auth/link"; + + [HttpGet("{provider}")] + public async Task Begin([FromRoute] string provider) + { + if (!provider.TryParseWire(out var oauthProvider)) + return UnsupportedProviderResponse(provider); + + // Fail-fast: the client is contractually required to pass redirect_uri on Begin so + // the callback (which reads it back from the octocon_link_redirect_uri cookie) can + // route the user home after the OAuth round-trip. Detecting it upfront avoids the + // full challenge round-trip landing in the 400 branch of RedirectToClient — the + // client sees the same error code, just before the browser leaves for the provider. + // Sibling AuthController.Begin has the same contract; the check lives here as a + // per-endpoint guard rather than base-class middleware because only the Begin shape + // carries the redirect_uri query param. + var redirectUri = Request.Query[OAuthQueryKeys.RedirectUri].ToString(); + if (string.IsNullOrWhiteSpace(redirectUri)) + { + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.QueryAuthLinkRequest.Value; + return BadRequest(new ErrorResponse( + "Missing client-supplied redirect_uri.", + ErrorCodes.MissingRedirectUri, + detail: "Pass redirect_uri on GET /auth/link/{provider} so the link callback knows where to send the result.")); + } + + StoreQueryCookie(LinkTokenCookieName, OAuthQueryKeys.LinkToken); + StoreRedirectUriCookie(RedirectUriCookieName); + + var challenge = await IssueChallengeIfRegisteredAsync(oauthProvider, OperationIds.QueryAuthLinkRequest); + + if (challenge is not null) + return challenge; + + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.QueryAuthLinkRequest.Value; + return StatusCode(StatusCodes.Status403Forbidden, string.Empty); + } + + [HttpGet("{provider}/callback")] + public Task CallbackGet([FromRoute] string provider) + => Callback(provider); + + [HttpPost("{provider}/callback")] + public Task CallbackPost([FromRoute] string provider) + => Callback(provider); + + private async Task Callback(string provider) + { + if (!provider.TryParseWire(out var oauthProvider)) + return UnsupportedProviderResponse(provider); + + // LinkToken.From wraps the query-or-cookie fallback in one call so the null check + // operates on the typed LinkToken?, not on a bare string local. The null-coalescing + // chain preserves its shape (query first, cookie second, .From consumes the + // possibly-null result). A bare string local would stay alive across the null-check + // / error-return boundary; a redirect-log or exception-message-containing-locals in + // that window would leak the token verbatim. + var linkToken = LinkToken.From(await GetValueAsync(OAuthQueryKeys.LinkToken) ?? Request.Cookies[LinkTokenCookieName]); + if (linkToken is null) + { + return StatusCode(StatusCodes.Status403Forbidden, "This link token is invalid or has expired."); + } + + var identity = await ExtractProviderIdentityAsync(oauthProvider); + if (identity is not { } typedIdentity) + { + return StatusCode(StatusCodes.Status403Forbidden, "Failed to authenticate. Did you reload the page or copy-paste the URL?"); + } + + // [AllowAnonymous] endpoint: the middleware doesn't populate the principal, but + // BuildEnvelope stamps the synthetic AnonymousPrincipalId in that case. The link + // handler resolves the real system id off the link token itself + // (ResolveSystemIdByLinkTokenAsync), so command.PrincipalId is never read. + var envelope = BuildEnvelope(OperationIds.AuthLinkCallback, new LinkOAuthIdentityCommand(linkToken.Value, typedIdentity)); + var commandResult = await _linkHandler.HandleAsync(envelope, HttpContext.RequestAborted); + if (!commandResult.Accepted || commandResult.Result is null) + { + Response.Cookies.Delete(LinkTokenCookieName); + return StatusCode(StatusCodes.Status403Forbidden, "This link token is invalid or has expired."); + } + + var result = commandResult.Result.Result; + var systemId = commandResult.Result.SystemId; + + Response.Cookies.Delete(LinkTokenCookieName); + + var redirectUri = Request.Cookies[RedirectUriCookieName]; + Response.Cookies.Delete(RedirectUriCookieName); + + Response.Headers[InterfoldHeaders.OperationId] = OperationIds.AuthLinkCallback.Value; + + return result switch + { + AccountLinkResult.Success when systemId != null => RedirectToClient(redirectUri), + AccountLinkResult.AlreadyLinked => StatusCode(StatusCodes.Status403Forbidden, new ErrorMessageResponse( + oauthProvider switch + { + OAuthProvider.Discord => "A Discord account is already linked to this account; please unlink it first.", + OAuthProvider.Google => "A Google account is already linked to this account; please unlink it first.", + OAuthProvider.Apple => "An Apple account is already linked to this account; please unlink it first.", + _ => "An account is already linked to this account; please unlink it first." + })), + AccountLinkResult.UserExists => StatusCode(StatusCodes.Status500InternalServerError, new ErrorMessageResponse( + oauthProvider switch + { + OAuthProvider.Discord => "This Discord account is already linked to another account.", + OAuthProvider.Google => "This email address is already linked to another account.", + OAuthProvider.Apple => "This Apple account is already linked to another account.", + _ => "This account is already linked to another account." + })), + _ => StatusCode(StatusCodes.Status403Forbidden, new ErrorMessageResponse("System not found")) + }; + } + + private IActionResult RedirectToClient(string? redirectUri) + { + // The client is responsible for supplying its own redirect_uri on the initial + // GET /auth/link/{provider}?redirect_uri=... call; the cookie threads it through + // to here. Absence means the client never set it — surface that loudly rather + // than papering over with a server-configured fallback. + if (string.IsNullOrWhiteSpace(redirectUri)) + { + return BadRequest(new ErrorResponse( + "Missing client-supplied redirect_uri.", + ErrorCodes.MissingRedirectUri, + detail: "Pass redirect_uri on GET /auth/link/{provider} so the link callback knows where to send the result.")); + } + + return Redirect(redirectUri); + } +} diff --git a/apis/Interfold.Auth.Api/Controllers/Base/OAuthControllerBase.cs b/apis/Interfold.Auth.Api/Controllers/Base/OAuthControllerBase.cs new file mode 100644 index 00000000..8258136c --- /dev/null +++ b/apis/Interfold.Auth.Api/Controllers/Base/OAuthControllerBase.cs @@ -0,0 +1,243 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Models; +using Interfold.Auth.Api.Services; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Contracts.Enums; +using Interfold.Auth.Contracts.Ids; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Controllers.Base; + +public abstract class OAuthControllerBase : InterfoldControllerBase +{ + private const string OAuthIdentityFailureMessage = "Failed to authenticate. Did you reload the page or copy-paste the URL?"; + + protected readonly IOptionsMonitor AuthOptions; + protected readonly IAuthenticationSchemeProvider SchemeProvider; + protected readonly GoogleOAuthService GoogleOAuth; + protected readonly DiscordOAuthService DiscordOAuth; + protected readonly AppleOAuthService AppleOAuth; + + protected OAuthControllerBase( + IOptionsMonitor authOptions, + IAuthenticationSchemeProvider schemeProvider, + GoogleOAuthService googleOAuth, + DiscordOAuthService discordOAuth, + AppleOAuthService appleOAuth) + { + AuthOptions = authOptions; + SchemeProvider = schemeProvider; + GoogleOAuth = googleOAuth; + DiscordOAuth = discordOAuth; + AppleOAuth = appleOAuth; + } + + protected abstract string CallbackRoutePrefix { get; } + + /// + /// Resolves the provider identity for the current callback request as a + /// union whose populated field carries the typed + /// identity (Discord snowflake / email / Apple sub). Returns null when no + /// identity could be extracted (missing code, missing fallback, or failed exchange). + /// The union carries the typed identity end-to-end so callers never have to switch on + /// the provider enum and rewrap a raw string. + /// + protected async Task ExtractProviderIdentityAsync(OAuthProvider provider) + { + switch (provider) + { + case OAuthProvider.Discord: + { + var code = await GetValueAsync(OAuthQueryKeys.Code); + if (!string.IsNullOrWhiteSpace(code)) + { + var redirectUri = BuildCallbackBaseUri(provider); + var discordId = await DiscordOAuth.ExchangeCodeForDiscordIdAsync(code, redirectUri, HttpContext.RequestAborted); + return discordId is { } id && !string.IsNullOrWhiteSpace(id.Value) + ? ProviderIdentity.FromDiscord(id) + : null; + } + + var fallback = await GetValueAsync(OAuthQueryKeys.Uid, OAuthQueryKeys.DiscordIdFallback, OAuthQueryKeys.Id); + return string.IsNullOrWhiteSpace(fallback) + ? null + : ProviderIdentity.FromDiscord(new(fallback)); + } + + case OAuthProvider.Google: + { + var code = await GetValueAsync(OAuthQueryKeys.Code); + if (string.IsNullOrWhiteSpace(code)) + { + var directEmail = await GetValueAsync(OAuthQueryKeys.Email); + return string.IsNullOrWhiteSpace(directEmail) + ? null + : ProviderIdentity.FromGoogle(new(directEmail)); + } + + var redirectUri = BuildCallbackBaseUri(provider); + var email = await GoogleOAuth.ExchangeCodeForEmailAsync(code, redirectUri, HttpContext.RequestAborted); + if (email is { } exchangedEmail && !string.IsNullOrWhiteSpace(exchangedEmail.Value)) + { + return ProviderIdentity.FromGoogle(exchangedEmail); + } + + var fallbackEmail = await GetValueAsync(OAuthQueryKeys.Email); + return string.IsNullOrWhiteSpace(fallbackEmail) + ? null + : ProviderIdentity.FromGoogle(new(fallbackEmail)); + } + + case OAuthProvider.Apple: + { + var code = await GetValueAsync(OAuthQueryKeys.Code); + if (!string.IsNullOrWhiteSpace(code)) + { + var redirectUri = BuildCallbackBaseUri(provider); + var appleId = await AppleOAuth.ExchangeCodeForAppleIdAsync(code, redirectUri, HttpContext.RequestAborted); + if (appleId is { } exchangedAppleId && !string.IsNullOrWhiteSpace(exchangedAppleId.Value)) + { + return ProviderIdentity.FromApple(exchangedAppleId); + } + } + + var idToken = await GetValueAsync(OAuthQueryKeys.IdToken); + var sub = AppleOAuth.ExtractSubFromJwt(idToken); + if (sub is { } tokenSub && !string.IsNullOrWhiteSpace(tokenSub.Value)) + { + return ProviderIdentity.FromApple(tokenSub); + } + + var fallback = await GetValueAsync(OAuthQueryKeys.Uid, OAuthQueryKeys.AppleIdFallback, OAuthQueryKeys.Id, OAuthQueryKeys.Sub); + return string.IsNullOrWhiteSpace(fallback) + ? null + : ProviderIdentity.FromApple(new(fallback)); + } + + default: + return null; + } + } + + protected string BuildCallbackBaseUri(OAuthProvider provider) + { + var providerKey = provider.ToWire(); + var authConfig = AuthOptions.CurrentValue; + var baseUrl = authConfig.CallbackBaseUrl ?? $"{Request.Scheme}://{Request.Host}"; + return $"{baseUrl}/{CallbackRoutePrefix}/{providerKey}/callback"; + } + + protected async Task GetValueAsync(params string[] keys) + { + foreach (var key in keys) + { + var query = Request.Query[key].ToString(); + if (!string.IsNullOrWhiteSpace(query)) + { + return query; + } + } + + if (Request.HasFormContentType) + { + var form = await Request.ReadFormAsync(HttpContext.RequestAborted); + foreach (var key in keys) + { + var value = form[key].ToString(); + if (!string.IsNullOrWhiteSpace(value)) + { + return value; + } + } + } + + return null; + } + + protected static string GetChallengeScheme(OAuthProvider provider) + { + return provider switch + { + OAuthProvider.Discord => OAuthChallengeServiceCollectionExtensions.DiscordSchemeName, + OAuthProvider.Google => OAuthChallengeServiceCollectionExtensions.GoogleSchemeName, + OAuthProvider.Apple => OAuthChallengeServiceCollectionExtensions.AppleSchemeName, + _ => throw new ArgumentOutOfRangeException(nameof(provider), provider, "Unhandled OAuthProvider."), + }; + } + + protected void StoreRedirectUriCookie(string cookieName) + { + var redirectUri = Request.Query[OAuthQueryKeys.RedirectUri].ToString(); + if (!string.IsNullOrWhiteSpace(redirectUri)) + { + Response.Cookies.Append(cookieName, redirectUri, new CookieOptions + { + HttpOnly = true, + IsEssential = true, + Secure = Request.IsHttps, + SameSite = SameSiteMode.Lax, + MaxAge = TimeSpan.FromMinutes(10) + }); + } + } + + protected void StoreQueryCookie(string cookieName, string queryKey) + { + var value = Request.Query[queryKey].ToString(); + if (!string.IsNullOrWhiteSpace(value)) + { + Response.Cookies.Append(cookieName, value, new CookieOptions + { + HttpOnly = true, + IsEssential = true, + Secure = Request.IsHttps, + SameSite = SameSiteMode.Lax, + MaxAge = TimeSpan.FromMinutes(10) + }); + } + } + + protected IActionResult UnsupportedProviderResponse(string provider) + { + return BadRequest(new UnsupportedOAuthProviderResponse( + "Unsupported OAuth provider.", + ErrorCodes.InvalidOAuthProvider, + provider)); + } + + protected IActionResult OAuthIdentityFailureResponse() + => StatusCode(StatusCodes.Status403Forbidden, OAuthIdentityFailureMessage); + + /// + /// Looks up the registered challenge scheme for the supplied provider key and returns a + /// against it, or null when the scheme isn't + /// registered (typically because the operator hasn't supplied the matching + /// OCTOCON_*_OAUTH_CLIENT_ID). The static challenge query parameters and the + /// authorization endpoint URL both come from + /// — see that type for why they're baked in rather than threaded through here. + /// + protected async Task IssueChallengeIfRegisteredAsync( + OAuthProvider provider, + OperationId operationId) + { + var challengeScheme = GetChallengeScheme(provider); + + var registeredScheme = await SchemeProvider.GetSchemeAsync(challengeScheme); + if (registeredScheme is null) + return null; + + var props = new AuthenticationProperties + { + RedirectUri = BuildCallbackBaseUri(provider) + }; + + Response.Headers[InterfoldHeaders.OperationId] = operationId.Value; + return Challenge(props, challengeScheme); + } +} diff --git a/apis/Interfold.Auth.Api/DependencyInjection/AuthModuleServiceCollectionExtensions.cs b/apis/Interfold.Auth.Api/DependencyInjection/AuthModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..868de344 --- /dev/null +++ b/apis/Interfold.Auth.Api/DependencyInjection/AuthModuleServiceCollectionExtensions.cs @@ -0,0 +1,110 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Services; +using Interfold.Auth.Api.Services.Secrets; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Domain; +using Interfold.Shared.Contracts.Configuration; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Options; +using Microsoft.IdentityModel.Tokens; + +namespace Interfold.Auth.Api.DependencyInjection; + +/// Auth feature module — the DI equivalent of the old +/// Program.cs --- Auth --- band. Consumed once from +/// Interfold.Api.Host/Program.cs; ordering constraints in doc §Phase 2 apply +/// (call after AddInterfoldOptions, before AddInterfoldPersistence). +public static class AuthModuleServiceCollectionExtensions +{ + /// Registers everything the Auth feature owns: env-bound + /// + secrets-patcher, the four command + /// handlers, the three OAuth HTTP clients, JWT bearer with the ES256 signature + /// validator, the redirect challenge schemes, and the startup-log hosted service. + /// Does not register the authorization fallback policy — that stays with the + /// composition host because it applies to every module's controllers. + public static IServiceCollection AddAuthModule(this IServiceCollection services, IConfiguration configuration) + { + // Env-bound public fields; [Required] secret fields land via + // AuthenticationSecretsPostConfigure before ValidateOnStart fires. + services.AddOptions() + .Configure(ApplyAuthentication) + .ValidateDataAnnotations() + .ValidateOnStart(); + + services.AddSingleton, AuthenticationSecretsPostConfigure>(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddHttpClient(); + services.AddHttpClient(); + services.AddHttpClient(); + + // JWTs are self-issued post-OAuth; no external OIDC issuer to validate, so aud + + // lifetime only. Read straight off configuration to avoid triggering + // .ValidateOnStart() before AuthenticationSecretsPostConfigure patches in the + // [Required] secret fields. + var jwtAudienceAtBoot = configuration[OctoconEnvKeys.JwtAudience] ?? "octocon"; + + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(); + + // Configure() defers the closure to first-request resolution — post-Build, + // post-PostConfigure, so IOptionsMonitor.CurrentValue + // returns the fully-patched config when SignatureValidator dereferences it. + services.AddOptions(JwtBearerDefaults.AuthenticationScheme) + .Configure>((options, authMonitor) => + { + options.MapInboundClaims = false; + options.TokenValidationParameters = new TokenValidationParameters + { + NameClaimType = "sub", + ValidateIssuer = false, + ValidateAudience = false, + ValidAudience = jwtAudienceAtBoot, + ValidateLifetime = true, + RequireExpirationTime = true, + ClockSkew = TimeSpan.FromMinutes(1), + ValidateIssuerSigningKey = false, + RequireSignedTokens = true, + SignatureValidator = (token, _) => + ValidateJwtTokenSignatureForBearer(token, authMonitor.CurrentValue) + }; + }); + + // Same configuration read as jwtAudienceAtBoot for the same PostConfigure reason. + services.AddInterfoldAuthChallengeSchemes( + discordOAuthClientId: configuration[OctoconEnvKeys.DiscordOAuthClientId], + googleOAuthClientId: configuration[OctoconEnvKeys.GoogleOAuthClientId], + appleOAuthClientId: configuration[OctoconEnvKeys.AppleOAuthClientId]); + + services.AddHostedService(); + + return services; + } + + // Public / env-bound fields only; secret fields land via AuthenticationSecretsPostConfigure. + // Migrated from infrastructure/Interfold.Infrastructure/DependencyInjection/ConfigurationServiceCollectionExtensions.cs + // in Phase 2 so the shared spine has no back-reference into the Auth feature. + private static void ApplyAuthentication(AuthenticationConfiguration opts, IConfiguration config) + { + opts.CallbackBaseUrl = config[OctoconEnvKeys.AuthCallbackBaseUrl]; + opts.JwtAuthority = config[OctoconEnvKeys.JwtAuthority] ?? "octocon-local"; + opts.JwtAudience = config[OctoconEnvKeys.JwtAudience] ?? opts.JwtAudience; + + // Client secrets are env-bound as fallback; internal.secrets rows override. + opts.DiscordOAuthClientId = config[OctoconEnvKeys.DiscordOAuthClientId]; + opts.DiscordOAuthClientSecret = config[OctoconEnvKeys.DiscordOAuthClientSecret]; + opts.GoogleOAuthClientId = config[OctoconEnvKeys.GoogleOAuthClientId]; + opts.GoogleOAuthClientSecret = config[OctoconEnvKeys.GoogleOAuthClientSecret]; + opts.AppleOAuthClientId = config[OctoconEnvKeys.AppleOAuthClientId]; + opts.AppleOAuthClientSecret = config[OctoconEnvKeys.AppleOAuthClientSecret]; + } + + private static SecurityToken ValidateJwtTokenSignatureForBearer( + string token, + AuthenticationConfiguration config) + => JwtEs256Validator.ValidateSignature(token, config.JwtEs256VerificationKeyPems ?? [], useJsonWebToken: true); +} diff --git a/apis/Interfold.Auth.Api/DependencyInjection/AuthStartupLogHostedService.cs b/apis/Interfold.Auth.Api/DependencyInjection/AuthStartupLogHostedService.cs new file mode 100644 index 00000000..a91c98bf --- /dev/null +++ b/apis/Interfold.Auth.Api/DependencyInjection/AuthStartupLogHostedService.cs @@ -0,0 +1,39 @@ +using Interfold.Auth.Contracts.Configuration; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.DependencyInjection; + +/// Hosted-service replacement for the ApplicationStarted lambda that used to sit +/// at the bottom of Program.cs. Subscribes to +/// so ValidateOnStart has patched [Required] secret fields before the log line fires. +internal sealed class AuthStartupLogHostedService : IHostedService +{ + private readonly IHostApplicationLifetime _lifetime; + private readonly IOptionsMonitor _authMonitor; + private readonly ILogger _logger; + + public AuthStartupLogHostedService( + IHostApplicationLifetime lifetime, + IOptionsMonitor authMonitor, + ILogger logger) + { + _lifetime = lifetime; + _authMonitor = authMonitor; + _logger = logger; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _lifetime.ApplicationStarted.Register(() => + { + var effectiveAuthConfig = _authMonitor.CurrentValue; + var verificationKeyCount = effectiveAuthConfig.JwtEs256VerificationKeyPems?.Length ?? 0; + _logger.LogInformation( + "ES256 token issuance is enabled. Verification key count: {VerificationKeyCount}.", + verificationKeyCount); + }); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/apis/Interfold.Auth.Api/Helpers/Base64Extensions.cs b/apis/Interfold.Auth.Api/Helpers/Base64Extensions.cs new file mode 100644 index 00000000..3cc2ce3b --- /dev/null +++ b/apis/Interfold.Auth.Api/Helpers/Base64Extensions.cs @@ -0,0 +1,20 @@ +namespace Interfold.Auth.Api.Helpers; + +public static class Base64Extensions +{ + public static byte[] Base64UrlDecode(this string value) + { + var padded = value.Replace('-', '+').Replace('_', '/'); + switch (padded.Length % 4) + { + case 2: + padded += "=="; + break; + case 3: + padded += "="; + break; + } + + return Convert.FromBase64String(padded); + } +} diff --git a/apis/Interfold.Auth.Api/Helpers/JwsHeader.cs b/apis/Interfold.Auth.Api/Helpers/JwsHeader.cs new file mode 100644 index 00000000..f67379bb --- /dev/null +++ b/apis/Interfold.Auth.Api/Helpers/JwsHeader.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Auth.Api.Helpers; + +/// +/// Minimal shape of a decoded JWS/JOSE header (RFC 7515 §4.1). Only the alg +/// member is consulted by the two custom ES256 signature validators (HTTP auth pipeline +/// and WebSocket join path); every other header field is ignored intentionally. +/// +/// +/// Named JwsHeader rather than JwtHeader to avoid a collision with +/// , which is transitively imported +/// by the socket handler. +/// +internal sealed record JwsHeader +{ + /// The only signature algorithm Interfold accepts. + public const string Es256 = "ES256"; + + [JsonPropertyName("alg")] + public string? Alg { get; init; } +} diff --git a/apis/Interfold.Auth.Api/Interfold.Auth.Api.csproj b/apis/Interfold.Auth.Api/Interfold.Auth.Api.csproj new file mode 100644 index 00000000..b790d58c --- /dev/null +++ b/apis/Interfold.Auth.Api/Interfold.Auth.Api.csproj @@ -0,0 +1,36 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Auth.Api/Middleware/AuthTokenRevocationMiddleware.cs b/apis/Interfold.Auth.Api/Middleware/AuthTokenRevocationMiddleware.cs new file mode 100644 index 00000000..a8ce596d --- /dev/null +++ b/apis/Interfold.Auth.Api/Middleware/AuthTokenRevocationMiddleware.cs @@ -0,0 +1,38 @@ +using System.Text.Json; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Auth.Api.Middleware; + +/// Post-authentication JTI-revocation gate. Rejects authenticated requests whose JWT +/// jti claim has been revoked (logout, incident response, background expiry cleanup) +/// with a 401 carrying . Unauthenticated and +/// authenticated-without-jti requests fall through untouched. +public sealed class AuthTokenRevocationMiddleware(RequestDelegate next) +{ + public async Task InvokeAsync(HttpContext context) + { + if (context.User?.Identity?.IsAuthenticated == true) + { + if (context.User.FindFirst(JwtClaimNames.Jti)?.Value is { } jti && !string.IsNullOrWhiteSpace(jti)) + { + var revocationRepository = context.RequestServices.GetRequiredService(); + var isTokenValid = await revocationRepository.ValidateTokenNotRevokedAsync(new Jti(jti), context.RequestAborted); + + if (!isTokenValid) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + context.Response.ContentType = "application/json"; + var error = new ErrorResponse("Token has been revoked.", ErrorCodes.TokenRevoked); + var json = JsonSerializer.Serialize(error, JsonSerializerOptions.Web); + await context.Response.WriteAsync(json, context.RequestAborted); + return; + } + } + } + + await next(context); + } +} diff --git a/apis/Interfold.Auth.Api/Models/ErrorMessageResponse.cs b/apis/Interfold.Auth.Api/Models/ErrorMessageResponse.cs new file mode 100644 index 00000000..ac75e82f --- /dev/null +++ b/apis/Interfold.Auth.Api/Models/ErrorMessageResponse.cs @@ -0,0 +1,7 @@ +namespace Interfold.Auth.Api.Models; + +/// +/// Minimal {"error": "..."} body used by the auth-link callback's failure branches. +/// Typed replacement for the previous anonymous object — identical wire shape. +/// +public sealed record ErrorMessageResponse(string Error); diff --git a/apis/Interfold.Auth.Api/Models/UnsupportedOAuthProviderResponse.cs b/apis/Interfold.Auth.Api/Models/UnsupportedOAuthProviderResponse.cs new file mode 100644 index 00000000..a648d74b --- /dev/null +++ b/apis/Interfold.Auth.Api/Models/UnsupportedOAuthProviderResponse.cs @@ -0,0 +1,10 @@ +using Interfold.Shared.Contracts; + +namespace Interfold.Auth.Api.Models; + +/// +/// 400 body for an unknown OAuth {provider} route value. Same +/// {"error","code","provider"} shape as the previous anonymous object — the +/// provider echoes the raw route string, which is exactly what failed to parse. +/// +public sealed record UnsupportedOAuthProviderResponse(string Error, ErrorCode Code, string Provider); diff --git a/apis/Interfold.Auth.Api/Services/AppleOAuthService.cs b/apis/Interfold.Auth.Api/Services/AppleOAuthService.cs new file mode 100644 index 00000000..42364333 --- /dev/null +++ b/apis/Interfold.Auth.Api/Services/AppleOAuthService.cs @@ -0,0 +1,116 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Services.OAuth; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Services; + +/// +/// Handles Apple OAuth2 backend token exchange and identity extraction. +/// +public sealed class AppleOAuthService +{ + private const string TokenEndpoint = "https://appleid.apple.com/auth/token"; + + private readonly HttpClient _httpClient; + private readonly IOptionsMonitor _authOptions; + + public AppleOAuthService(HttpClient httpClient, IOptionsMonitor authOptions) + { + _httpClient = httpClient; + _authOptions = authOptions; + } + + /// + /// Exchanges an Apple authorization code and returns the stable Apple user identifier (sub). + /// Returns null if configuration is incomplete or exchange fails. + /// + public async Task ExchangeCodeForAppleIdAsync( + string code, + string redirectUri, + CancellationToken cancellationToken = default) + { + var authConfig = _authOptions.CurrentValue; + if (string.IsNullOrWhiteSpace(authConfig.AppleOAuthClientId) || + string.IsNullOrWhiteSpace(authConfig.AppleOAuthClientSecret)) + { + return null; + } + + try + { + var tokenRequest = new Dictionary + { + { OAuthQueryKeys.GrantType, OAuthQueryKeys.AuthorizationCodeGrant }, + { OAuthQueryKeys.Code, code }, + { OAuthQueryKeys.RedirectUri, redirectUri }, + { OAuthQueryKeys.ClientId, authConfig.AppleOAuthClientId }, + { OAuthQueryKeys.ClientSecret, authConfig.AppleOAuthClientSecret } + }; + + using var content = new FormUrlEncodedContent(tokenRequest); + using var tokenResponse = await _httpClient.PostAsync(TokenEndpoint, content, cancellationToken); + + if (!tokenResponse.IsSuccessStatusCode) + { + return null; + } + + var tokenPayload = await tokenResponse.Content.ReadFromJsonAsync(cancellationToken); + return ExtractSubFromJwt(tokenPayload?.IdToken); + } + catch + { + return null; + } + } + + public AppleId? ExtractSubFromJwt(string? jwt) + { + if (string.IsNullOrWhiteSpace(jwt)) + { + return null; + } + + var parts = jwt.Split('.'); + if (parts.Length < 2) + { + return null; + } + + try + { + var payloadBytes = Base64UrlDecode(parts[1]); + var payload = JsonSerializer.Deserialize(payloadBytes); + return string.IsNullOrWhiteSpace(payload?.Sub) ? null : new AppleId(payload.Sub); + } + catch + { + return null; + } + } + + private sealed record AppleIdTokenPayload + { + [JsonPropertyName("sub")] + public string? Sub { get; init; } + } + + private static byte[] Base64UrlDecode(string input) + { + var normalized = input + .Replace('-', '+') + .Replace('_', '/'); + + var padding = 4 - (normalized.Length % 4); + if (padding is > 0 and < 4) + { + normalized = normalized + new string('=', padding); + } + + return Convert.FromBase64String(normalized); + } +} diff --git a/apis/Interfold.Auth.Api/Services/DiscordOAuthService.cs b/apis/Interfold.Auth.Api/Services/DiscordOAuthService.cs new file mode 100644 index 00000000..d1505c23 --- /dev/null +++ b/apis/Interfold.Auth.Api/Services/DiscordOAuthService.cs @@ -0,0 +1,95 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Services.OAuth; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Services; + +/// +/// Handles Discord OAuth2 backend token exchange and user info retrieval. +/// +public sealed class DiscordOAuthService +{ + private const string TokenEndpoint = "https://discord.com/api/oauth2/token"; + private const string UserInfoEndpoint = "https://discord.com/api/users/@me"; + + private readonly HttpClient _httpClient; + private readonly IOptionsMonitor _authOptions; + + public DiscordOAuthService(HttpClient httpClient, IOptionsMonitor authOptions) + { + _httpClient = httpClient; + _authOptions = authOptions; + } + + /// + /// Exchange authorization code for the Discord user ID via Discord's OAuth2 flow. + /// Returns null if configuration is incomplete or exchange fails. + /// + public async Task ExchangeCodeForDiscordIdAsync( + string code, + string redirectUri, + CancellationToken cancellationToken = default) + { + var authConfig = _authOptions.CurrentValue; + + if (string.IsNullOrWhiteSpace(authConfig.DiscordOAuthClientId) || + string.IsNullOrWhiteSpace(authConfig.DiscordOAuthClientSecret)) + { + return null; + } + + try + { + // Step 1: Exchange authorization code for access token. + // Discord requires application/x-www-form-urlencoded with HTTP Basic auth. + var tokenRequest = new Dictionary + { + { OAuthQueryKeys.GrantType, OAuthQueryKeys.AuthorizationCodeGrant }, + { OAuthQueryKeys.Code, code }, + { OAuthQueryKeys.RedirectUri, redirectUri } + }; + + using var content = new FormUrlEncodedContent(tokenRequest); + using var request = new HttpRequestMessage(HttpMethod.Post, TokenEndpoint) { Content = content }; + request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( + "Basic", + Convert.ToBase64String( + System.Text.Encoding.UTF8.GetBytes( + $"{authConfig.DiscordOAuthClientId}:{authConfig.DiscordOAuthClientSecret}"))); + + using var tokenResponse = await _httpClient.SendAsync(request, cancellationToken); + + if (!tokenResponse.IsSuccessStatusCode) + { + return null; + } + + var tokenPayload = await tokenResponse.Content.ReadFromJsonAsync(cancellationToken); + if (string.IsNullOrWhiteSpace(tokenPayload?.AccessToken)) + { + return null; + } + + // Step 2: Fetch the user's Discord ID from /users/@me. + using var userInfoRequest = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint); + userInfoRequest.Headers.Authorization = + new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenPayload.AccessToken); + + using var userInfoResponse = await _httpClient.SendAsync(userInfoRequest, cancellationToken); + + if (!userInfoResponse.IsSuccessStatusCode) + { + return null; + } + + var user = await userInfoResponse.Content.ReadFromJsonAsync(cancellationToken); + return string.IsNullOrWhiteSpace(user?.Id) ? null : new DiscordId(user.Id); + } + catch + { + return null; + } + } +} diff --git a/apis/Interfold.Auth.Api/Services/GoogleOAuthService.cs b/apis/Interfold.Auth.Api/Services/GoogleOAuthService.cs new file mode 100644 index 00000000..8e38b7da --- /dev/null +++ b/apis/Interfold.Auth.Api/Services/GoogleOAuthService.cs @@ -0,0 +1,90 @@ +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Api.Services.OAuth; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Services; + +/// +/// Handles Google OAuth2 backend token exchange and user info retrieval. +/// +public sealed class GoogleOAuthService +{ + private const string TokenEndpoint = "https://oauth2.googleapis.com/token"; + private const string UserInfoEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo"; + + private readonly HttpClient _httpClient; + private readonly IOptionsMonitor _authOptions; + + public GoogleOAuthService(HttpClient httpClient, IOptionsMonitor authOptions) + { + _httpClient = httpClient; + _authOptions = authOptions; + } + + /// + /// Exchange authorization code for email via Google's OAuth2 flow. + /// Returns null if configuration is incomplete or exchange fails. + /// + public async Task ExchangeCodeForEmailAsync( + string code, + string redirectUri, + CancellationToken cancellationToken = default) + { + var authConfig = _authOptions.CurrentValue; + // Validate configuration + if (string.IsNullOrWhiteSpace(authConfig.GoogleOAuthClientId) || + string.IsNullOrWhiteSpace(authConfig.GoogleOAuthClientSecret)) + { + return null; + } + + try + { + // Step 1: Exchange code for access token + var tokenRequest = new Dictionary + { + { OAuthQueryKeys.Code, code }, + { OAuthQueryKeys.ClientId, authConfig.GoogleOAuthClientId }, + { OAuthQueryKeys.ClientSecret, authConfig.GoogleOAuthClientSecret }, + { OAuthQueryKeys.GrantType, OAuthQueryKeys.AuthorizationCodeGrant }, + { OAuthQueryKeys.RedirectUri, redirectUri } + }; + + using var content = new FormUrlEncodedContent(tokenRequest); + using var tokenResponse = await _httpClient.PostAsync(TokenEndpoint, content, cancellationToken); + + if (!tokenResponse.IsSuccessStatusCode) + { + return null; + } + + var tokenPayload = await tokenResponse.Content.ReadFromJsonAsync(cancellationToken); + if (string.IsNullOrWhiteSpace(tokenPayload?.AccessToken)) + { + return null; + } + + // Step 2: Use access token to fetch user info + using var userInfoRequest = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint); + userInfoRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenPayload.AccessToken); + + using var userInfoResponse = await _httpClient.SendAsync(userInfoRequest, cancellationToken); + + if (!userInfoResponse.IsSuccessStatusCode) + { + return null; + } + + var userInfo = await userInfoResponse.Content.ReadFromJsonAsync(cancellationToken); + return string.IsNullOrWhiteSpace(userInfo?.Email) ? null : new Email(userInfo.Email); + } + catch + { + // Log the error and return null to allow graceful degradation + // In production, you'd want to log this properly + return null; + } + } +} diff --git a/apis/Interfold.Auth.Api/Services/OAuth/OAuthResponseDtos.cs b/apis/Interfold.Auth.Api/Services/OAuth/OAuthResponseDtos.cs new file mode 100644 index 00000000..ade2bd15 --- /dev/null +++ b/apis/Interfold.Auth.Api/Services/OAuth/OAuthResponseDtos.cs @@ -0,0 +1,73 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Auth.Api.Services.OAuth; + +// Typed DTOs for OAuth token/userinfo responses. Callers were previously walking +// JsonDocument roots with TryGetProperty; typing the shapes here means schema drift +// (Google/Discord renames a field, Apple ships a new claim) surfaces as a +// deserialization miss instead of a silent null return. +// +// PropertyNamingPolicy on the shared serializer already snake_cases outbound JSON, +// but these types deserialize inbound provider JSON which uses snake_case field +// names — the [JsonPropertyName] attributes keep the mapping explicit so no future +// policy change silently breaks OAuth. + +/// +/// Body returned by POST https://oauth2.googleapis.com/token +/// (and the Discord equivalent, which shares the same field shape). +/// +public sealed record OAuthTokenResponse +{ + [JsonPropertyName("access_token")] + public string? AccessToken { get; init; } + + [JsonPropertyName("token_type")] + public string? TokenType { get; init; } + + [JsonPropertyName("expires_in")] + public int? ExpiresIn { get; init; } + + [JsonPropertyName("refresh_token")] + public string? RefreshToken { get; init; } + + [JsonPropertyName("scope")] + public string? Scope { get; init; } + + [JsonPropertyName("id_token")] + public string? IdToken { get; init; } +} + +/// +/// Subset of Google's userinfo v2 response +/// (GET https://www.googleapis.com/oauth2/v2/userinfo) that we actually +/// consume. Extra fields returned by Google are ignored. +/// +public sealed record GoogleUserInfoResponse +{ + [JsonPropertyName("email")] + public string? Email { get; init; } + + [JsonPropertyName("id")] + public string? Id { get; init; } + + [JsonPropertyName("verified_email")] + public bool? VerifiedEmail { get; init; } +} + +/// +/// Subset of Discord's GET /users/@me response. +/// +public sealed record DiscordUserResponse +{ + [JsonPropertyName("id")] + public string? Id { get; init; } + + [JsonPropertyName("username")] + public string? Username { get; init; } + + [JsonPropertyName("global_name")] + public string? GlobalName { get; init; } + + [JsonPropertyName("avatar")] + public string? Avatar { get; init; } +} diff --git a/apis/Interfold.Auth.Api/Services/Secrets/AuthenticationSecretsPostConfigure.cs b/apis/Interfold.Auth.Api/Services/Secrets/AuthenticationSecretsPostConfigure.cs new file mode 100644 index 00000000..a9c87cd1 --- /dev/null +++ b/apis/Interfold.Auth.Api/Services/Secrets/AuthenticationSecretsPostConfigure.cs @@ -0,0 +1,68 @@ +using System.Security.Cryptography; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.Api.Services.Secrets; + +/// Layers internal.secrets rows onto the env-bound +/// . Runs post-IConfigureOptions, +/// pre-ValidateDataAnnotations so the [Required] fields +/// (, +/// , +/// , +/// ) are populated before +/// .ValidateOnStart(). Absent OAuth client secrets stay null (skips scheme +/// registration). +internal sealed class AuthenticationSecretsPostConfigure(ISecretsSnapshot snapshot) + : IPostConfigureOptions +{ + private static readonly (SecretsStoreKey Key, Action Patch)[] AuthSecretMappings = + [ + (SecretsStoreKeys.OAuthGoogleClientSecret, (auth, v) => auth.GoogleOAuthClientSecret = v), + (SecretsStoreKeys.OAuthDiscordClientSecret, (auth, v) => auth.DiscordOAuthClientSecret = v), + (SecretsStoreKeys.OAuthAppleClientSecret, (auth, v) => auth.AppleOAuthClientSecret = v), + (SecretsStoreKeys.EncryptionPepper, (auth, v) => auth.EncryptionPepper = v), + (SecretsStoreKeys.AuthDeepLinkSecret, (auth, v) => auth.DeepLinkSecret = v), + (SecretsStoreKeys.AuthJwtRsa256PrivatePem, PatchRsa256), + (SecretsStoreKeys.AuthJwtEs256PrivatePem, PatchEs256), + ]; + + public void PostConfigure(string? name, AuthenticationConfiguration options) + { + // Default bucket only — every consumer resolves it, and named buckets would smear + // secrets across logically-separate configurations. + if (name != Options.DefaultName) + { + return; + } + + foreach (var (key, patch) in AuthSecretMappings) + { + var value = snapshot.Get(key); + if (!string.IsNullOrWhiteSpace(value)) + { + patch(options, value); + } + } + } + + /// Assigns the RSA-2048 private PEM and derives the SPKI public PEM served + /// via JWKS. + private static void PatchRsa256(AuthenticationConfiguration auth, string privatePem) + { + auth.Rsa256PrivateKey = privatePem; + using var rsa = RSA.Create(); + rsa.ImportFromPem(privatePem.AsSpan()); + auth.Rsa256PublicKey = rsa.ExportSubjectPublicKeyInfoPem(); + } + + /// Assigns the ES256 private PEM and seeds the verification array with it — + /// extracts the public half at verify time. + private static void PatchEs256(AuthenticationConfiguration auth, string privatePem) + { + auth.JwtEs256PrivateKeyPem = privatePem; + auth.JwtEs256VerificationKeyPems = [privatePem]; + } +} diff --git a/apis/Interfold.Friendships.Api/Controllers/FriendRequestsController.cs b/apis/Interfold.Friendships.Api/Controllers/FriendRequestsController.cs new file mode 100644 index 00000000..4c4b07e0 --- /dev/null +++ b/apis/Interfold.Friendships.Api/Controllers/FriendRequestsController.cs @@ -0,0 +1,104 @@ +using System.Net; +using Interfold.Friendships.Api.Helpers; +using Interfold.Friendships.Contracts.Ids; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Friendships.Api.Controllers; + +[Route("api/friend-requests")] +public sealed class FriendRequestsController : InterfoldControllerBase +{ + private readonly IFriendshipRepository _repository; + private readonly SendFriendRequestCommandHandler _send; + private readonly AcceptFriendRequestCommandHandler _accept; + private readonly RejectFriendRequestCommandHandler _reject; + private readonly CancelFriendRequestCommandHandler _cancel; + + public FriendRequestsController( + IFriendshipRepository repository, + SendFriendRequestCommandHandler send, + AcceptFriendRequestCommandHandler accept, + RejectFriendRequestCommandHandler reject, + CancelFriendRequestCommandHandler cancel) + { + _repository = repository; + _send = send; + _accept = accept; + _reject = reject; + _cancel = cancel; + } + + [HttpGet] + public async Task> Index(CancellationToken ct) + { + var requests = await _repository.GetFriendRequestsAsync(PrincipalId, ct); + var incoming = requests.Incoming + .Select(r => FriendshipAvatarQualifier.QualifyFriendRequest(r, Request.Scheme, Request.Host)) + .ToArray(); + var outgoing = requests.Outgoing + .Select(r => FriendshipAvatarQualifier.QualifyFriendRequest(r, Request.Scheme, Request.Host)) + .ToArray(); + + // Same wire shape as the previous anonymous object: {"data":{"incoming":[…],"outgoing":[…]}}. + return new SuccessResponse(new FriendRequestIndexReadModel(incoming, outgoing)); + } + + // The route value is bound as FriendLookup, which accepts exactly two shapes: + // a bare (or "id:"-prefixed) system id, and a "username:"-prefixed username. Every + // other shape — Discord snowflakes, region-scoped ids (nam:...), unknown prefixes, + // blank / half inputs — fails FriendLookup.TryParse and ASP.NET Core's IParsable + // pipeline surfaces a 400 before this action runs. The command handler resolves + // Kind.Username via IFriendshipRepository.ResolveUserIdAsync. + [HttpPut("{id}")] + public async Task Send(FriendLookup id, CancellationToken ct) + { + // Semantic self-check via FriendLookupExtensions.RepresentsSameUserAs — catches the + // "client sent their own id" fast-path case without a repository hop. Kind.Id delegates + // to the SystemId primitive so raw and same-region-scoped inputs both self-reject; + // Kind.Username trivially returns false — deciding "is alice me?" requires a registry + // lookup, so SendFriendRequestCommandHandler's post-resolution guard takes over. + if (id.RepresentsSameUserAs(PrincipalId)) + return new ErrorResponse("You cannot send a friend request to yourself.", ErrorCodes.CannotSendSelf, HttpStatusCode.BadRequest); + + return await DispatchNoContentAsync(_send, OperationIds.FriendRequestSend, new SendFriendRequestCommand(id), ct); + } + + [HttpDelete("{id}")] + public async Task Cancel(SystemId id, CancellationToken ct) + { + // Semantic self-check — CancelFriendRequestCommandHandler has no downstream + // self-guard, so a bare byte compare would let a raw-id self-cancel return the + // generic friend_request:not_requested error instead of cannot_cancel_self. + if (RejectIfSelf(id, "You cannot cancel a friend request to yourself.", ErrorCodes.CannotCancelSelf) is { } reject) + return reject; + + return await DispatchNoContentAsync(_cancel, OperationIds.FriendRequestCancel, new CancelFriendRequestCommand(id), ct); + } + + [HttpPost("{id}/accept")] + public async Task Accept(SystemId id, CancellationToken ct) + { + if (RejectIfSelf(id, "You cannot accept a friend request from yourself.", ErrorCodes.CannotAcceptSelf) is { } reject) + return reject; + + return await DispatchNoContentAsync(_accept, OperationIds.FriendRequestAccept, new AcceptFriendRequestCommand(id), ct); + } + + [HttpPost("{id}/reject")] + public async Task Reject(SystemId id, CancellationToken ct) + { + if (RejectIfSelf(id, "You cannot reject a friend request from yourself.", ErrorCodes.CannotRejectSelf) is { } reject) + return reject; + + return await DispatchNoContentAsync(_reject, OperationIds.FriendRequestReject, new RejectFriendRequestCommand(id), ct); + } +} diff --git a/apis/Interfold.Friendships.Api/Controllers/FriendsController.cs b/apis/Interfold.Friendships.Api/Controllers/FriendsController.cs new file mode 100644 index 00000000..53221ed5 --- /dev/null +++ b/apis/Interfold.Friendships.Api/Controllers/FriendsController.cs @@ -0,0 +1,98 @@ +using Interfold.Friendships.Api.Helpers; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Friendships.Api.Controllers; + +[Route("api/friends")] +public sealed class FriendsController : InterfoldControllerBase +{ + private readonly IFriendshipRepository _repository; + private readonly RemoveFriendshipCommandHandler _remove; + private readonly SetFriendTrustCommandHandler _setTrust; + + public FriendsController( + IFriendshipRepository repository, + RemoveFriendshipCommandHandler remove, + SetFriendTrustCommandHandler setTrust) + { + _repository = repository; + _remove = remove; + _setTrust = setTrust; + } + + [HttpGet] + public async Task>> Index(CancellationToken ct) + { + var friendships = await _repository.ListFriendshipsAsync(PrincipalId, ct); + var qualified = friendships + .Select(f => FriendshipAvatarQualifier.QualifyFriendship(f, Request.Scheme, Request.Host)) + .ToArray(); + return new SuccessResponse>(qualified); + } + + + [HttpGet("{id}")] + public async Task> Show(SystemId id, CancellationToken ct) + { + // Semantic self-check — a bare byte compare would miss the raw route shape and + // let /api/friends/{rawId} slip past this guard. + if (RejectIfSelf( + id, + "I'm pretty sure you don't count as your own friend. (Cannot view friendship status for self.)", + ErrorCodes.CannotViewOwnFriendship) is { } reject) + return reject; + + var friendship = await _repository.GetFriendshipAsync(PrincipalId, id, ct); + return OkOrNotFound( + friendship is null ? null : FriendshipAvatarQualifier.QualifyFriendship(friendship, Request.Scheme, Request.Host), + "You are not friends with that system.", + ErrorCodes.FriendshipNotFound); + } + + [HttpDelete("{id}")] + public async Task Delete(SystemId id, CancellationToken ct) + { + // Semantic self-check — see Show handler for the same rationale. + if (RejectIfSelf( + id, + "I'm pretty sure you don't count as your own friend. (Cannot delete friendship with self.)", + ErrorCodes.CannotDeleteOwnFriendship) is { } reject) + return reject; + + return await DispatchNoContentAsync(_remove, OperationIds.FriendDelete, new RemoveFriendshipCommand(id), ct); + } + + [HttpPost("{id}/trust")] + public async Task Trust(SystemId id, CancellationToken ct) + => await SetTrustInternal(id, true, OperationIds.FriendTrust, ErrorCodes.CannotTrustSelf, ct); + + [HttpPost("{id}/untrust")] + public async Task Untrust(SystemId id, CancellationToken ct) + => await SetTrustInternal(id, false, OperationIds.FriendUntrust, ErrorCodes.CannotUntrustSelf, ct); + + private async Task SetTrustInternal( + SystemId id, + bool trusted, + OperationId operationId, + ErrorCode selfErrorCode, + CancellationToken ct) + { + var principal = PrincipalId; + // Semantic self-check — see Show handler for the same rationale. + if (principal.RepresentsSameUserAs(id)) + { + return new ErrorResponse("Cannot trust self.", selfErrorCode, System.Net.HttpStatusCode.BadRequest); + } + + return await DispatchNoContentAsync(_setTrust, operationId, new SetFriendTrustCommand(id, trusted), ct); + } +} diff --git a/apis/Interfold.Friendships.Api/DependencyInjection/FriendshipsModuleServiceCollectionExtensions.cs b/apis/Interfold.Friendships.Api/DependencyInjection/FriendshipsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..3aa07f0d --- /dev/null +++ b/apis/Interfold.Friendships.Api/DependencyInjection/FriendshipsModuleServiceCollectionExtensions.cs @@ -0,0 +1,23 @@ +using Interfold.Friendships.Domain; + +namespace Interfold.Friendships.Api.DependencyInjection; + +/// Friendships feature module — the DI equivalent of the six friendship-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class FriendshipsModuleServiceCollectionExtensions +{ + /// Registers everything the Friendships feature owns: the six friendship + /// command-handler singletons. IFriendshipRepository is registered by the active + /// persistence adapter (Scylla / Postgres / InMemory) via + /// AddInterfoldPersistence, so no repository registration lives here. + public static IServiceCollection AddFriendshipsModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Friendships.Api/Helpers/FriendshipAvatarQualifier.cs b/apis/Interfold.Friendships.Api/Helpers/FriendshipAvatarQualifier.cs new file mode 100644 index 00000000..fbb3fc23 --- /dev/null +++ b/apis/Interfold.Friendships.Api/Helpers/FriendshipAvatarQualifier.cs @@ -0,0 +1,70 @@ +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Shared.Api.Helpers; + +namespace Interfold.Friendships.Api.Helpers; + +// Feature-owned migration of the four QualifyFriendship / QualifyFriendRequest helpers +// that used to live in Interfold.Shared.Api/Helpers/AvatarUrlQualifier. Moved here as +// part of the Phase-3 Friendships slice so Interfold.Shared.Api no longer binds the +// friendship read-model cluster (which now lives in Interfold.Friendships.Contracts). +// The underlying per-avatar overloads (AvatarUrlQualifier.QualifyAvatar) stay in the +// shared spine — they're the shared primitive; the friendship-shaped composers are +// what belong to this feature. +internal static class FriendshipAvatarQualifier +{ + /// Qualifies the friend's and every fronting alter's avatar; other fields pass through. + internal static FriendshipReadModel QualifyFriendship( + FriendshipReadModel friendship, + string scheme, + HostString host) + => friendship with + { + Friend = friendship.Friend with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(friendship.Friend, scheme, host), + }, + Fronting = friendship.Fronting + .Select(f => f with + { + Alter = f.Alter with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(f.Alter, scheme, host), + }, + }) + .ToArray(), + }; + + /// Origin-string overload for socket pushers. + internal static FriendshipReadModel QualifyFriendship( + FriendshipReadModel friendship, + string? origin) + => friendship with + { + Friend = friendship.Friend with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(friendship.Friend, origin), + }, + Fronting = friendship.Fronting + .Select(f => f with + { + Alter = f.Alter with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(f.Alter, origin), + }, + }) + .ToArray(), + }; + + /// Qualifies the requester/requestee profile's avatar. + internal static FriendRequestReadModel QualifyFriendRequest( + FriendRequestReadModel request, + string scheme, + HostString host) + => request with + { + System = request.System with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(request.System, scheme, host), + }, + }; +} diff --git a/apis/Interfold.Friendships.Api/Interfold.Friendships.Api.csproj b/apis/Interfold.Friendships.Api/Interfold.Friendships.Api.csproj new file mode 100644 index 00000000..79ab454e --- /dev/null +++ b/apis/Interfold.Friendships.Api/Interfold.Friendships.Api.csproj @@ -0,0 +1,28 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Fronting.Api/Controllers/FrontingController.cs b/apis/Interfold.Fronting.Api/Controllers/FrontingController.cs new file mode 100644 index 00000000..2f29c514 --- /dev/null +++ b/apis/Interfold.Fronting.Api/Controllers/FrontingController.cs @@ -0,0 +1,152 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.ModelBinding; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Fronting.Api.Controllers; + +[Route("api/systems/me/front")] +public sealed class FrontingController : InterfoldControllerBase +{ + private readonly IFrontingRepository _repository; + private readonly StartFrontCommandHandler _startHandler; + private readonly EndFrontCommandHandler _endHandler; + private readonly BulkUpdateFrontCommandHandler _bulkUpdateHandler; + private readonly SetFrontCommandHandler _setHandler; + private readonly SetPrimaryFrontCommandHandler _primaryHandler; + private readonly DeleteFrontByIdCommandHandler _deleteByIdHandler; + private readonly UpdateFrontCommentCommandHandler _updateCommentHandler; + + public FrontingController( + IFrontingRepository repository, + StartFrontCommandHandler startHandler, + EndFrontCommandHandler endHandler, + BulkUpdateFrontCommandHandler bulkUpdateHandler, + SetFrontCommandHandler setHandler, + SetPrimaryFrontCommandHandler primaryHandler, + DeleteFrontByIdCommandHandler deleteByIdHandler, + UpdateFrontCommentCommandHandler updateCommentHandler) + { + _repository = repository; + _startHandler = startHandler; + _endHandler = endHandler; + _bulkUpdateHandler = bulkUpdateHandler; + _setHandler = setHandler; + _primaryHandler = primaryHandler; + _deleteByIdHandler = deleteByIdHandler; + _updateCommentHandler = updateCommentHandler; + } + + [HttpPost] + public async Task Update([FromBody] FrontBulkUpdateRequest req, CancellationToken ct) + { + var payload = new BulkUpdateFrontCommand( + req.Start.Select(x => new FrontStartItem(x.AlterId, x.Comment)).ToArray(), + req.End.ToArray()); + + return await DispatchNoContentAsync(_bulkUpdateHandler, OperationIds.FrontBulkUpdate, payload, ct); + } + + [HttpPost("start")] + public async Task> Start([FromBody] FrontStartRequest req, CancellationToken ct) + { + var envelope = BuildEnvelope(OperationIds.FrontStart, new StartFrontCommand(req.Id, req.Comment) + ); + + var execution = await _startHandler.HandleAsync(envelope, ct); + var response = CommandCreated(execution, r => new FrontStartedResponse(r.FrontId!.Value)); + + if (response.IsSuccess && response.AsSuccess.Data.FrontId != FrontId.Empty) + { + Response.Headers.Location = $"/api/systems/me/front/{response.AsSuccess.Data.FrontId}"; + } + + return response; + } + + [HttpPost("end")] + public async Task End([FromBody] FrontEndRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_endHandler, OperationIds.FrontEnd, new EndFrontCommand(req.Id) + , ct); + } + + [HttpPost("set")] + public async Task Set([FromBody] FrontSetRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_setHandler, OperationIds.FrontSet, new SetFrontCommand(req.Id, req.Comment) + , ct); + } + + [HttpPost("primary")] + public async Task Primary([FromBody] FrontPrimaryRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_primaryHandler, OperationIds.FrontPrimary, new SetPrimaryFrontCommand(req.Id) + , ct); + } + + [HttpGet("month")] + public async Task>> Month( + [FromQuery(Name = FrontingQueryKeys.EndAnchor)] + [UnixSecondsBinding( + ErrorCode = "invalid_end_anchor", + ErrorMessage = "Invalid end anchor. Please pass a valid Unix timestamp.")] + UnixSeconds endAnchor, + CancellationToken ct) + { + var end = endAnchor.ToDateTimeOffset(); + var start = end.AddDays(-30); + var fronts = await _repository.ListHistoryBetweenAsync(PrincipalId, start, end, ct); + return new SuccessResponse>(fronts); + } + + [HttpGet("between")] + public async Task>> Between( + [FromQuery(Name = FrontingQueryKeys.Start)] + [UnixSecondsBinding( + ErrorCode = "invalid_anchor", + ErrorMessage = "Invalid start anchor. Please pass a valid Unix timestamp.")] + UnixSeconds startAnchor, + [FromQuery(Name = FrontingQueryKeys.End)] + [UnixSecondsBinding( + ErrorCode = "invalid_anchor", + ErrorMessage = "Invalid end anchor. Please pass a valid Unix timestamp.")] + UnixSeconds endAnchor, + CancellationToken ct) + { + var fronts = await _repository.ListHistoryBetweenAsync( + PrincipalId, + startAnchor.ToDateTimeOffset(), + endAnchor.ToDateTimeOffset(), + ct); + return new SuccessResponse>(fronts); + } + + [HttpGet("{id}")] + public async Task> Show(FrontId id, CancellationToken ct) + { + var front = await _repository.GetActiveByFrontIdAsync(PrincipalId, id, ct); + return OkOrNotFound(front, "Front not found.", ErrorCodes.FrontNotFound); + } + + [HttpDelete("{id}")] + public async Task Delete(FrontId id, CancellationToken ct) + { + return await DispatchNoContentAsync(_deleteByIdHandler, OperationIds.FrontDelete, new DeleteFrontByIdCommand(id) + , ct); + } + + [HttpPost("{id}/comment")] + public async Task UpdateComment(FrontId id, [FromBody] FrontCommentRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_updateCommentHandler, OperationIds.FrontCommentUpdate, new UpdateFrontCommentCommand(id, req.Comment) + , ct); + } +} diff --git a/apis/Interfold.Fronting.Api/Controllers/FrontingQueryKeys.cs b/apis/Interfold.Fronting.Api/Controllers/FrontingQueryKeys.cs new file mode 100644 index 00000000..a871a09e --- /dev/null +++ b/apis/Interfold.Fronting.Api/Controllers/FrontingQueryKeys.cs @@ -0,0 +1,12 @@ +namespace Interfold.Fronting.Api.Controllers; + +/// +/// Query-parameter names on the fronting history endpoints. Wire-frozen (client-built +/// URIs); const because they feed [FromQuery(Name = …)] attribute arguments. +/// +internal static class FrontingQueryKeys +{ + public const string EndAnchor = "end_anchor"; + public const string Start = "start"; + public const string End = "end"; +} diff --git a/apis/Interfold.Fronting.Api/DependencyInjection/FrontingModuleServiceCollectionExtensions.cs b/apis/Interfold.Fronting.Api/DependencyInjection/FrontingModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..33f65818 --- /dev/null +++ b/apis/Interfold.Fronting.Api/DependencyInjection/FrontingModuleServiceCollectionExtensions.cs @@ -0,0 +1,24 @@ +using Interfold.Fronting.Domain; + +namespace Interfold.Fronting.Api.DependencyInjection; + +/// Fronting feature module — the DI equivalent of the seven fronting-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class FrontingModuleServiceCollectionExtensions +{ + /// Registers everything the Fronting feature owns: the seven fronting + /// command-handler singletons. IFrontingRepository is registered by the active + /// persistence adapter (Scylla / Postgres / InMemory) via + /// AddInterfoldPersistence, so no repository registration lives here. + public static IServiceCollection AddFrontingModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Fronting.Api/Interfold.Fronting.Api.csproj b/apis/Interfold.Fronting.Api/Interfold.Fronting.Api.csproj new file mode 100644 index 00000000..460feaaa --- /dev/null +++ b/apis/Interfold.Fronting.Api/Interfold.Fronting.Api.csproj @@ -0,0 +1,28 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Journals.Api/Controllers/AlterJournalsController.cs b/apis/Interfold.Journals.Api/Controllers/AlterJournalsController.cs new file mode 100644 index 00000000..1f6b5e29 --- /dev/null +++ b/apis/Interfold.Journals.Api/Controllers/AlterJournalsController.cs @@ -0,0 +1,98 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Journals.Domain; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Contracts.Validation; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Journals.Api.Controllers; + +[Route("api/systems/me/alters")] +public sealed class AlterJournalsController : InterfoldControllerBase +{ + private readonly IJournalRepository _journalRepository; + private readonly CreateAlterJournalEntryCommandHandler _create; + private readonly UpdateAlterJournalEntryCommandHandler _update; + private readonly DeleteAlterJournalEntryCommandHandler _delete; + private readonly SetAlterJournalLockedCommandHandler _setLocked; + private readonly SetAlterJournalPinnedCommandHandler _setPinned; + + public AlterJournalsController( + IJournalRepository journalRepository, + CreateAlterJournalEntryCommandHandler create, + UpdateAlterJournalEntryCommandHandler update, + DeleteAlterJournalEntryCommandHandler delete, + SetAlterJournalLockedCommandHandler setLocked, + SetAlterJournalPinnedCommandHandler setPinned) + { + _journalRepository = journalRepository; + _create = create; + _update = update; + _delete = delete; + _setLocked = setLocked; + _setPinned = setPinned; + } + + [HttpGet("{alterId:int}/journals")] + public async Task>> Index([FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + var principal = PrincipalId; + IEnumerable entries = await _journalRepository.ListAlterAsync(principal, alterId, ct); + return new SuccessResponse>(entries); + } + + [HttpGet("journals/{journalId}")] + public async Task> Show(EntryId journalId, CancellationToken ct) + { + var entry = await _journalRepository.GetAlterAsync(PrincipalId, journalId, ct); + return OkOrNotFound(entry, "Journal entry not found.", ErrorCodes.JournalEntryNotFound); + } + + [HttpPost("{alterId:int}/journals")] + public async Task> Create([FromRoute][ValidAlterId] AlterId alterId, [FromBody] CreateAlterJournalRequest req, CancellationToken ct) + { + var principal = PrincipalId; + return await DispatchCreatedAsync( + _create, + OperationIds.JournalAlterCreate, + new CreateAlterJournalEntryCommand(alterId, req.Title, TimeProvider.GetUtcNow()), + async (res) => await _journalRepository.GetAlterAsync(principal, res.EntryId, ct), + ct); + } + + [HttpPatch("journals/{journalId}")] + public async Task Update(EntryId journalId, [FromBody] UpdateAlterJournalRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_update, OperationIds.JournalAlterUpdate, new UpdateAlterJournalEntryCommand(journalId, req.Title, req.Content, req.Color, TimeProvider.GetUtcNow()) + , ct); + } + + [HttpDelete("journals/{journalId}")] + public async Task Delete(EntryId journalId, CancellationToken ct) + { + return await DispatchNoContentAsync(_delete, OperationIds.JournalAlterDelete, new DeleteAlterJournalEntryCommand(journalId) + , ct); + } + + [HttpPost("journals/{journalId}/lock")] + public Task Lock(EntryId journalId, CancellationToken ct) + => DispatchNoContentAsync(_setLocked, OperationIds.JournalAlterLock, new SetAlterJournalLockedCommand(journalId, true), ct); + + [HttpPost("journals/{journalId}/unlock")] + public Task Unlock(EntryId journalId, CancellationToken ct) + => DispatchNoContentAsync(_setLocked, OperationIds.JournalAlterUnlock, new SetAlterJournalLockedCommand(journalId, false), ct); + + [HttpPost("journals/{journalId}/pin")] + public Task Pin(EntryId journalId, CancellationToken ct) + => DispatchNoContentAsync(_setPinned, OperationIds.JournalAlterPin, new SetAlterJournalPinnedCommand(journalId, true), ct); + + [HttpPost("journals/{journalId}/unpin")] + public Task Unpin(EntryId journalId, CancellationToken ct) + => DispatchNoContentAsync(_setPinned, OperationIds.JournalAlterUnpin, new SetAlterJournalPinnedCommand(journalId, false), ct); +} \ No newline at end of file diff --git a/apis/Interfold.Journals.Api/Controllers/JournalsController.cs b/apis/Interfold.Journals.Api/Controllers/JournalsController.cs new file mode 100644 index 00000000..13b8ecbe --- /dev/null +++ b/apis/Interfold.Journals.Api/Controllers/JournalsController.cs @@ -0,0 +1,115 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Journals.Domain; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Journals.Api.Controllers; + +[Route("api/journals")] +public sealed class JournalsController : InterfoldControllerBase +{ + private readonly IJournalRepository _journalRepository; + private readonly CreateGlobalJournalEntryCommandHandler _create; + private readonly UpdateGlobalJournalEntryCommandHandler _update; + private readonly DeleteGlobalJournalEntryCommandHandler _delete; + private readonly SetGlobalJournalLockedCommandHandler _setLocked; + private readonly SetGlobalJournalPinnedCommandHandler _setPinned; + private readonly AttachAlterToGlobalJournalCommandHandler _attachAlter; + private readonly DetachAlterFromGlobalJournalCommandHandler _detachAlter; + + public JournalsController( + IJournalRepository journalRepository, + CreateGlobalJournalEntryCommandHandler create, + UpdateGlobalJournalEntryCommandHandler update, + DeleteGlobalJournalEntryCommandHandler delete, + SetGlobalJournalLockedCommandHandler setLocked, + SetGlobalJournalPinnedCommandHandler setPinned, + AttachAlterToGlobalJournalCommandHandler attachAlter, + DetachAlterFromGlobalJournalCommandHandler detachAlter) + { + _journalRepository = journalRepository; + _create = create; + _update = update; + _delete = delete; + _setLocked = setLocked; + _setPinned = setPinned; + _attachAlter = attachAlter; + _detachAlter = detachAlter; + } + + [HttpGet] + public async Task>> Index(CancellationToken ct) + { + var entries = await _journalRepository.ListGlobalAsync(PrincipalId, ct); + return new SuccessResponse>(entries); + } + + [HttpGet("{id}")] + public async Task> Show(EntryId id, CancellationToken ct) + { + var entry = await _journalRepository.GetGlobalAsync(PrincipalId, id, ct); + return OkOrNotFound(entry, "Journal entry not found.", ErrorCodes.JournalEntryNotFound); + } + + [HttpPost] + public async Task> Create([FromBody] CreateGlobalJournalRequest req, CancellationToken ct) + { + var principal = PrincipalId; + return await DispatchCreatedAsync( + _create, + OperationIds.JournalGlobalCreate, + new CreateGlobalJournalEntryCommand(req.Title), + async (res) => await _journalRepository.GetGlobalAsync(principal, res.EntryId, ct), + ct); + } + + [HttpPatch("{id}")] + public async Task Update(EntryId id, [FromBody] UpdateGlobalJournalRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_update, OperationIds.JournalGlobalUpdate, new UpdateGlobalJournalEntryCommand(id, req.Title, req.Content, req.Color) + , ct); + } + + [HttpDelete("{id}")] + public async Task Delete(EntryId id, [FromBody] DeleteGlobalJournalRequest? req, CancellationToken ct) + { + return await DispatchNoContentAsync(_delete, OperationIds.JournalGlobalDelete, new DeleteGlobalJournalEntryCommand(id) + , ct); + } + + [HttpPost("{id}/lock")] + public Task Lock(EntryId id, [FromBody] JournalActionRequest? req, CancellationToken ct) + => DispatchNoContentAsync(_setLocked, OperationIds.JournalGlobalLock, new SetGlobalJournalLockedCommand(id, true), ct); + + [HttpPost("{id}/unlock")] + public Task Unlock(EntryId id, [FromBody] JournalActionRequest? req, CancellationToken ct) + => DispatchNoContentAsync(_setLocked, OperationIds.JournalGlobalUnlock, new SetGlobalJournalLockedCommand(id, false), ct); + + [HttpPost("{id}/pin")] + public Task Pin(EntryId id, [FromBody] JournalActionRequest? req, CancellationToken ct) + => DispatchNoContentAsync(_setPinned, OperationIds.JournalGlobalPin, new SetGlobalJournalPinnedCommand(id, true), ct); + + [HttpPost("{id}/unpin")] + public Task Unpin(EntryId id, [FromBody] JournalActionRequest? req, CancellationToken ct) + => DispatchNoContentAsync(_setPinned, OperationIds.JournalGlobalUnpin, new SetGlobalJournalPinnedCommand(id, false), ct); + + [HttpPost("{id}/alter")] + public async Task AttachAlter(EntryId id, [FromBody] JournalAlterRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_attachAlter, OperationIds.JournalGlobalAttachAlter, new AttachAlterToGlobalJournalCommand(id, req.AlterId) + , ct); + } + + [HttpDelete("{id}/alter")] + public async Task DetachAlter(EntryId id, [FromBody] JournalAlterRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_detachAlter, OperationIds.JournalGlobalDetachAlter, new DetachAlterFromGlobalJournalCommand(id, req.AlterId) + , ct); + } +} \ No newline at end of file diff --git a/apis/Interfold.Journals.Api/DependencyInjection/JournalsModuleServiceCollectionExtensions.cs b/apis/Interfold.Journals.Api/DependencyInjection/JournalsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..6daad21c --- /dev/null +++ b/apis/Interfold.Journals.Api/DependencyInjection/JournalsModuleServiceCollectionExtensions.cs @@ -0,0 +1,35 @@ +using Interfold.Journals.Domain; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Api.DependencyInjection; + +/// Journals feature module — the DI equivalent of the twelve journal-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class JournalsModuleServiceCollectionExtensions +{ + /// Registers everything the Journals feature owns: the twelve journal + /// command-handler singletons (seven global + five alter) plus the + /// adapter over IJournalRepository + /// consumed by DeleteAlterCommandHandler (spine-level abstraction so + /// Alters.Domain doesn't back-reference Journals.Domain). IJournalRepository + /// itself is registered by the active persistence adapter (Scylla / Postgres / + /// InMemory) via AddInterfoldPersistence, so no repository registration + /// lives here. + public static IServiceCollection AddJournalsModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Journals.Api/Interfold.Journals.Api.csproj b/apis/Interfold.Journals.Api/Interfold.Journals.Api.csproj new file mode 100644 index 00000000..e0bb1894 --- /dev/null +++ b/apis/Interfold.Journals.Api/Interfold.Journals.Api.csproj @@ -0,0 +1,29 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Ops.Api/Controllers/NodeRoleController.cs b/apis/Interfold.Ops.Api/Controllers/NodeRoleController.cs new file mode 100644 index 00000000..4c5fb607 --- /dev/null +++ b/apis/Interfold.Ops.Api/Controllers/NodeRoleController.cs @@ -0,0 +1,34 @@ +using Interfold.Ops.Api.Models; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Ops.Api.Controllers; + +/// +/// Exposes node-role metadata for load-balancer and ops health checks. +/// +[AllowAnonymous] +[Route("health")] +public sealed class NodeRoleController : InterfoldControllerBase +{ + private readonly INodeRoleContext _nodeRole; + + public NodeRoleController(INodeRoleContext nodeRole) + { + _nodeRole = nodeRole; + } + + /// + /// Returns the current node's role and whether it owns singleton background tasks. + /// + /// Equivalent to Octocon.RPC.NodeTracker.current_group/0 from the legacy runtime. + /// + /// + [HttpGet("node-role")] + public IActionResult GetNodeRole() + { + return Ok(new NodeRoleResponse(_nodeRole.Role, _nodeRole.IsPrimary)); + } +} diff --git a/apis/Interfold.Ops.Api/Controllers/TrustController.cs b/apis/Interfold.Ops.Api/Controllers/TrustController.cs new file mode 100644 index 00000000..5527cee2 --- /dev/null +++ b/apis/Interfold.Ops.Api/Controllers/TrustController.cs @@ -0,0 +1,161 @@ +using System.Security.Cryptography.X509Certificates; +using Interfold.Shared.Contracts.Configuration; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Interfold.Ops.Api.Controllers; + +/// +/// Serves the bootstrapper-issued root CA at the IANA-registered /.well-known/ prefix +/// so unauthenticated end-user devices can fetch and trust it before they ever speak TLS to +/// the API. Three artefact shapes are exposed: +/// +/// interfold-root-ca.crt — DER (binary), application/pkix-cert by default +/// and application/x-x509-ca-cert when the client opts in via Accept +/// (the legacy MIME that triggers Safari/iOS profile flows). +/// interfold-root-ca.pem — PEM (base64-armoured), application/x-pem-file. +/// interfold-root-ca.sha256 — the SHA-256 fingerprint of the root cert in +/// uppercase colon-hex (matches openssl x509 -fingerprint -sha256). +/// +/// +/// The controller serves a hardcoded allowlist of three paths derived from +/// ; no user input ever joins a file path, so a path-traversal +/// regression here cannot read rootCA.key (which also sits at mode 0600 in the same +/// directory as defence-in-depth). When the bootstrapper hasn't supplied either path (dev +/// mode, missing bind mount), every route returns 404. +/// +/// +/// Cache strategy: Cache-Control: public, max-age=60, must-revalidate plus an +/// ETag derived from the SHA-256 fingerprint file. Rotating the root CA via +/// bootstrap --rotate-certs rewrites the sidecar fingerprint, which changes the +/// ETag, which causes downstream caches to refresh on the next conditional request. +/// +/// +[ApiController] +[AllowAnonymous] +[Route(".well-known")] +public sealed class TrustController : ControllerBase +{ + private readonly TrustOptions _options; + + public TrustController(IOptions options) + { + _options = options.Value; + } + + /// + /// Returns the root CA in DER (binary) form. PEM-on-disk → DER conversion happens + /// in-controller via so the on-disk format never + /// becomes part of the wire contract; the bootstrapper is free to write either. + /// Negotiates the legacy application/x-x509-ca-cert MIME when requested via + /// Accept for Safari/iOS-friendly profile flows; otherwise emits the modern RFC 2585 + /// application/pkix-cert. + /// + [HttpGet("interfold-root-ca.crt")] + [HttpHead("interfold-root-ca.crt")] + public async Task GetRootCaDer(CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(_options.RootCaPath) || !System.IO.File.Exists(_options.RootCaPath)) + { + return NotFound(); + } + + using var cert = X509CertificateLoader.LoadCertificateFromFile(_options.RootCaPath); + var der = cert.RawData; + + await ApplyCacheHeadersAsync(ct).ConfigureAwait(false); + + var contentType = AcceptsLegacyCaCertMime() ? "application/x-x509-ca-cert" : "application/pkix-cert"; + return File(der, contentType, "interfold-root-ca.crt"); + } + + /// + /// Returns the root CA in PEM form. The on-disk file is already PEM (CertificatePhase + /// writes via X509Certificate2.ExportCertificatePem()) so we stream the bytes + /// verbatim without re-encoding. + /// + [HttpGet("interfold-root-ca.pem")] + [HttpHead("interfold-root-ca.pem")] + public async Task GetRootCaPem(CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(_options.RootCaPath) || !System.IO.File.Exists(_options.RootCaPath)) + { + return NotFound(); + } + + var bytes = await System.IO.File.ReadAllBytesAsync(_options.RootCaPath, ct).ConfigureAwait(false); + + await ApplyCacheHeadersAsync(ct).ConfigureAwait(false); + return File(bytes, "application/x-pem-file", "interfold-root-ca.pem"); + } + + /// + /// Returns the SHA-256 fingerprint of the root CA as a single line of uppercase colon-hex + /// text. The fingerprint is what operators distribute out-of-band (Slack, email, + /// keybase) so end users can pin it against the cert they fetched from this same + /// origin — closing the trust-on-first-fetch gap. + /// + [HttpGet("interfold-root-ca.sha256")] + [HttpHead("interfold-root-ca.sha256")] + public async Task GetRootCaFingerprint(CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(_options.RootCaFingerprintPath) || + !System.IO.File.Exists(_options.RootCaFingerprintPath)) + { + return NotFound(); + } + + var bytes = await System.IO.File.ReadAllBytesAsync(_options.RootCaFingerprintPath, ct).ConfigureAwait(false); + + await ApplyCacheHeadersAsync(ct).ConfigureAwait(false); + return File(bytes, "text/plain; charset=utf-8", "interfold-root-ca.sha256"); + } + + /// + /// Stamps Cache-Control and a fingerprint-based ETag on the current + /// response. The ETag is the only mechanism that propagates a --rotate-certs + /// signal through downstream caches; without it a CDN or browser holding a stale copy + /// would keep serving the old cert until max-age expired. + /// + private async Task ApplyCacheHeadersAsync(CancellationToken ct) + { + Response.Headers["Cache-Control"] = "public, max-age=60, must-revalidate"; + + if (string.IsNullOrWhiteSpace(_options.RootCaFingerprintPath) || + !System.IO.File.Exists(_options.RootCaFingerprintPath)) + { + return; + } + + var fp = (await System.IO.File.ReadAllTextAsync(_options.RootCaFingerprintPath, ct).ConfigureAwait(false)).Trim(); + if (fp.Length > 0) + { + Response.Headers.ETag = $"\"{fp}\""; + } + } + + /// + /// True when the client explicitly opts in to the legacy + /// application/x-x509-ca-cert MIME via its Accept header. Modern Safari + /// / iOS profile install flows still trigger on this type even though it's been + /// superseded by application/pkix-cert (RFC 2585). + /// + private bool AcceptsLegacyCaCertMime() + { + foreach (var accept in Request.Headers.Accept) + { + if (string.IsNullOrEmpty(accept)) + { + continue; + } + + if (accept.Contains("application/x-x509-ca-cert", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } +} diff --git a/apis/Interfold.Ops.Api/DependencyInjection/OpsModuleServiceCollectionExtensions.cs b/apis/Interfold.Ops.Api/DependencyInjection/OpsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..299d3571 --- /dev/null +++ b/apis/Interfold.Ops.Api/DependencyInjection/OpsModuleServiceCollectionExtensions.cs @@ -0,0 +1,17 @@ +namespace Interfold.Ops.Api.DependencyInjection; + +/// Ops feature module — the facade shape has no owned handlers or +/// abstractions to register (see the deliberate absence of an Interfold.Ops.Domain +/// project and an Interfold.Ops.Contracts project — Ops has nothing to share +/// cross-module). AddOpsModule exists purely for wiring symmetry with the other +/// feature modules so Interfold.Api.Host/Program.cs reads uniformly. Consumed +/// once from the module-registration block alongside the other +/// Add<Feature>Module extensions. +public static class OpsModuleServiceCollectionExtensions +{ + /// No-op today. NodeRoleController binds INodeRoleContext + /// (registered by AddInterfoldCluster); TrustController binds + /// IOptions<TrustOptions> (registered by AddInterfoldOptions); + /// nothing here to register. + public static IServiceCollection AddOpsModule(this IServiceCollection services) => services; +} diff --git a/apis/Interfold.Ops.Api/Helpers/HealthCheckExtensions.cs b/apis/Interfold.Ops.Api/Helpers/HealthCheckExtensions.cs new file mode 100644 index 00000000..fcc11cee --- /dev/null +++ b/apis/Interfold.Ops.Api/Helpers/HealthCheckExtensions.cs @@ -0,0 +1,29 @@ +using Interfold.Shared.Contracts.Configuration; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace Interfold.Ops.Api.Helpers; + +/// +/// Convenience extensions for the pair of "-ready" + "-startup" checks the API +/// registers per backing store. Keeps the two timeouts in one place so a change +/// (e.g. bumping startup for slow migrations) doesn't drift one apart from the +/// other across the four persistence-mode branches in Program.cs. +/// +public static class HealthCheckExtensions +{ + /// Readiness checks fail fast (dependency dropped after boot). + public static readonly TimeSpan ReadyTimeout = TimeSpan.FromSeconds(5); + + /// Startup checks allow longer for cold-start migrations / initial connect. + public static readonly TimeSpan StartupTimeout = TimeSpan.FromSeconds(30); + + public static IHealthChecksBuilder AddReadyAndStartup( + this IHealthChecksBuilder builder, + string namePrefix) + where T : class, IHealthCheck + { + builder.AddCheck($"{namePrefix}-ready", tags: [HealthCheckTags.Ready], timeout: ReadyTimeout); + builder.AddCheck($"{namePrefix}-startup", tags: [HealthCheckTags.Startup], timeout: StartupTimeout); + return builder; + } +} diff --git a/apis/Interfold.Ops.Api/Interfold.Ops.Api.csproj b/apis/Interfold.Ops.Api/Interfold.Ops.Api.csproj new file mode 100644 index 00000000..35b83167 --- /dev/null +++ b/apis/Interfold.Ops.Api/Interfold.Ops.Api.csproj @@ -0,0 +1,37 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Ops.Api/Models/NodeRoleResponse.cs b/apis/Interfold.Ops.Api/Models/NodeRoleResponse.cs new file mode 100644 index 00000000..b32b98e6 --- /dev/null +++ b/apis/Interfold.Ops.Api/Models/NodeRoleResponse.cs @@ -0,0 +1,10 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Ops.Api.Models; + +/// +/// Body of GET /health/node-role. Serializes as +/// {"role":"primary","owns_singletons":true} — the role via the enum's lowercase +/// wire converter (the previous anonymous object lower-cased ToString() by hand). +/// +public sealed record NodeRoleResponse(NodeGroup Role, bool OwnsSingletons); diff --git a/apis/Interfold.Polls.Api/Controllers/PollsController.cs b/apis/Interfold.Polls.Api/Controllers/PollsController.cs new file mode 100644 index 00000000..bb9b537e --- /dev/null +++ b/apis/Interfold.Polls.Api/Controllers/PollsController.cs @@ -0,0 +1,89 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Operations; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Polls.Api.Controllers; + +[Route("api/polls")] +public sealed class PollsController : InterfoldControllerBase +{ + private readonly IPollRepository _pollRepository; + private readonly CreatePollCommandHandler _create; + private readonly UpdatePollCommandHandler _update; + private readonly DeletePollCommandHandler _delete; + + public PollsController( + IPollRepository pollRepository, + CreatePollCommandHandler create, + UpdatePollCommandHandler update, + DeletePollCommandHandler delete) + { + _pollRepository = pollRepository; + _create = create; + _update = update; + _delete = delete; + } + + [HttpGet] + public async Task>> Index(CancellationToken ct) + { + var polls = await _pollRepository.ListAsync(PrincipalId, ct); + return new SuccessResponse>(polls); + } + + [HttpGet("{id}")] + public async Task> Show(PollId id, CancellationToken ct) + { + var poll = await _pollRepository.GetAsync(PrincipalId, id, ct); + return OkOrNotFound(poll, "Poll not found.", ErrorCodes.PollNotFound); + } + + [HttpPost] + public async Task> Create([FromBody] CreatePollRequest req, CancellationToken ct) + { + var principal = PrincipalId; + // CreatePollCommandHandler owns InsertedAtUtc on the public-API path and derives it + // from the envelope's OccurredAt right before calling the repo. Passing `default` + // here keeps the hashed payload stable across retries with the same idempotency key + // (otherwise every call would stamp a fresh DateTime.UtcNow and look like a + // different request, triggering ConflictDuplicate on every replay). + return await DispatchCreatedAsync( + _create, + OperationIds.PollCreate, + new CreatePollCommand(req.Title, req.Description, req.Type ?? PollType.Vote, req.TimeEnd, InsertedAtUtc: default), + async (res) => await _pollRepository.GetAsync(principal, res.PollId, ct), + ct); + } + + [HttpPatch("{id}")] + public async Task Update(PollId id, [FromBody] UpdatePollRequest req, CancellationToken ct) + { + // time_end is tri-state (absent / null / value); an unparseable value surfaces as + // PatchValueState.Invalid so we can keep this exact error body instead of MVC's + // generic model-binding 400. + if (req.TimeEnd.IsInvalid) + { + return new ErrorResponse("Invalid time_end.", ErrorCodes.PollInvalidTimeEnd, System.Net.HttpStatusCode.BadRequest); + } + + DateTime? resolvedTimeEnd = req.TimeEnd.State == PatchValueState.Value ? req.TimeEnd.Value : null; + + return await DispatchNoContentAsync(_update, OperationIds.PollUpdate, new UpdatePollCommand(id, req.Title, req.Description, resolvedTimeEnd, req.TimeEnd.IsSet, req.Data) + , ct); + } + + [HttpDelete("{id}")] + public async Task Delete(PollId id, CancellationToken ct) + { + return await DispatchNoContentAsync(_delete, OperationIds.PollDelete, new DeletePollCommand(id) + , ct); + } +} diff --git a/apis/Interfold.Polls.Api/DependencyInjection/PollsModuleServiceCollectionExtensions.cs b/apis/Interfold.Polls.Api/DependencyInjection/PollsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..1e678cba --- /dev/null +++ b/apis/Interfold.Polls.Api/DependencyInjection/PollsModuleServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +using Interfold.Polls.Domain; + +namespace Interfold.Polls.Api.DependencyInjection; + +/// Polls feature module — the DI equivalent of the three poll-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class PollsModuleServiceCollectionExtensions +{ + /// Registers everything the Polls feature owns: the three poll command-handler + /// singletons. IPollRepository is registered by the active persistence adapter + /// (Scylla / Postgres / InMemory) via AddInterfoldPersistence, so no repository + /// registration lives here. + public static IServiceCollection AddPollsModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Polls.Api/Interfold.Polls.Api.csproj b/apis/Interfold.Polls.Api/Interfold.Polls.Api.csproj new file mode 100644 index 00000000..7b60a9c0 --- /dev/null +++ b/apis/Interfold.Polls.Api/Interfold.Polls.Api.csproj @@ -0,0 +1,28 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Settings.Api/Controllers/SettingsController.cs b/apis/Interfold.Settings.Api/Controllers/SettingsController.cs new file mode 100644 index 00000000..5b810183 --- /dev/null +++ b/apis/Interfold.Settings.Api/Controllers/SettingsController.cs @@ -0,0 +1,472 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Settings.Api.Helpers; +using Interfold.Settings.Api.Services.Export; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Contracts.Models.Read; +using Interfold.Settings.Contracts.Models.Wire; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Accounts; +using Interfold.Settings.Domain.Settings; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Api.Controllers; + +[Route("api/settings")] +public sealed class SettingsController : InterfoldControllerBase +{ + private readonly IAccountRepository _accountRepository; + private readonly ISingletonTaskOwner _singletonTaskOwner; + private readonly IAvatarStorage _avatarStorage; + private readonly IOptionsMonitor _authenticationConfiguration; + private readonly IOptionsMonitor _firebaseClientConfiguration; + + private readonly UpdateUsernameCommandHandler _usernameHandler; + private readonly UpdateDescriptionCommandHandler _descriptionHandler; + private readonly AddPushTokenCommandHandler _addPushTokenHandler; + private readonly RemovePushTokenCommandHandler _removePushTokenHandler; + private readonly SetupEncryptionCommandHandler _setupEncryptionHandler; + private readonly RecoverEncryptionCommandHandler _recoverEncryptionHandler; + private readonly ResetEncryptionCommandHandler _resetEncryptionHandler; + private readonly UploadAvatarCommandHandler _uploadAvatarHandler; + private readonly DeleteAvatarCommandHandler _deleteAvatarHandler; + private readonly ImportPkCommandHandler _importPkHandler; + private readonly ImportSpCommandHandler _importSpHandler; + private readonly UnlinkDiscordCommandHandler _unlinkDiscordHandler; + private readonly UnlinkEmailCommandHandler _unlinkEmailHandler; + private readonly UnlinkAppleCommandHandler _unlinkAppleHandler; + private readonly DeleteAccountCommandHandler _deleteAccountHandler; + private readonly WipeAltersCommandHandler _wipeAltersHandler; + private readonly WipeTagsCommandHandler _wipeTagsHandler; + private readonly CreateFieldCommandHandler _createFieldHandler; + private readonly UpdateFieldCommandHandler _updateFieldHandler; + private readonly DeleteFieldCommandHandler _deleteFieldHandler; + private readonly RelocateFieldCommandHandler _relocateFieldHandler; + private readonly CreateLinkTokenCommandHandler _createLinkTokenHandler; + private readonly IExportService _exportService; + + public SettingsController( + IAccountRepository accountRepository, + ISingletonTaskOwner singletonTaskOwner, + UpdateUsernameCommandHandler usernameHandler, + UpdateDescriptionCommandHandler descriptionHandler, + AddPushTokenCommandHandler addPushTokenHandler, + RemovePushTokenCommandHandler removePushTokenHandler, + SetupEncryptionCommandHandler setupEncryptionHandler, + RecoverEncryptionCommandHandler recoverEncryptionHandler, + ResetEncryptionCommandHandler resetEncryptionHandler, + UploadAvatarCommandHandler uploadAvatarHandler, + IAvatarStorage avatarStorage, + DeleteAvatarCommandHandler deleteAvatarHandler, + ImportPkCommandHandler importPkHandler, + ImportSpCommandHandler importSpHandler, + UnlinkDiscordCommandHandler unlinkDiscordHandler, + UnlinkEmailCommandHandler unlinkEmailHandler, + UnlinkAppleCommandHandler unlinkAppleHandler, + DeleteAccountCommandHandler deleteAccountHandler, + WipeAltersCommandHandler wipeAltersHandler, + WipeTagsCommandHandler wipeTagsHandler, + CreateFieldCommandHandler createFieldHandler, + UpdateFieldCommandHandler updateFieldHandler, + DeleteFieldCommandHandler deleteFieldHandler, + RelocateFieldCommandHandler relocateFieldHandler, + IOptionsMonitor authenticationConfiguration, + IOptionsMonitor firebaseClientConfiguration, + CreateLinkTokenCommandHandler createLinkTokenHandler, + IExportService exportService) + { + _accountRepository = accountRepository; + _singletonTaskOwner = singletonTaskOwner; + _usernameHandler = usernameHandler; + _descriptionHandler = descriptionHandler; + _addPushTokenHandler = addPushTokenHandler; + _removePushTokenHandler = removePushTokenHandler; + _setupEncryptionHandler = setupEncryptionHandler; + _recoverEncryptionHandler = recoverEncryptionHandler; + _resetEncryptionHandler = resetEncryptionHandler; + _uploadAvatarHandler = uploadAvatarHandler; + _avatarStorage = avatarStorage; + _deleteAvatarHandler = deleteAvatarHandler; + _importPkHandler = importPkHandler; + _importSpHandler = importSpHandler; + _unlinkDiscordHandler = unlinkDiscordHandler; + _unlinkEmailHandler = unlinkEmailHandler; + _unlinkAppleHandler = unlinkAppleHandler; + _deleteAccountHandler = deleteAccountHandler; + _wipeAltersHandler = wipeAltersHandler; + _wipeTagsHandler = wipeTagsHandler; + _createFieldHandler = createFieldHandler; + _updateFieldHandler = updateFieldHandler; + _deleteFieldHandler = deleteFieldHandler; + _relocateFieldHandler = relocateFieldHandler; + _createLinkTokenHandler = createLinkTokenHandler; + _authenticationConfiguration = authenticationConfiguration; + _firebaseClientConfiguration = firebaseClientConfiguration; + _exportService = exportService; + } + + /// + /// Downloads the authenticated user's export in either PluralKit v2 importable form + /// (?format=pk) or the full-fidelity Octocon backup (?format=full). + /// + /// + /// Response body is the raw JSON payload at the root, allowing PluralKit users to save the file and feed it directly to PK's + /// importer. Missing or unrecognised ?format returns 400 with + /// . + /// + [HttpGet("export")] + public async Task Export([FromQuery] ExportFormat? format, CancellationToken ct) + { + if (format is not { } fmt) + { + return BadRequest(new ErrorResponse("Invalid export format.", ErrorCodes.InvalidExportFormat, HttpStatusCode.BadRequest)); + } + + var payload = fmt switch + { + ExportFormat.Pk => (object)await _exportService.BuildPkAsync(PrincipalId, ct), + ExportFormat.Full => await _exportService.BuildFullAsync(PrincipalId, ct), + _ => throw new InvalidOperationException($"Unhandled ExportFormat: {fmt}"), + }; + + var bytes = JsonSerializer.SerializeToUtf8Bytes(payload, payload.GetType(), ExportJsonOptions.Default); + var wire = EnumWire.ToWire(fmt); + return File(bytes, "application/json", $"octocon_export_{wire}.json"); + } + + [HttpGet("link_token")] + public async Task GetLinkToken(CancellationToken ct) + { + // Token creation (write) is gated to primary nodes only — mirrors + // Octocon.Global.LinkTokenRegistry singleton in the legacy Elixir runtime. + // On auxiliary/sidecar nodes attempt a read-only lookup; if no token has + // been provisioned yet, return 503 to inform the caller to retry via a + // primary node. + var principal = PrincipalId; + if (_singletonTaskOwner.OwnsTask(SettingsTaskNames.LinkTokenRegistry)) + { + var result = await _createLinkTokenHandler.HandleAsync(BuildEnvelope(OperationIds.SettingsLinkToken, new CreateLinkTokenCommand()), ct); + return Ok(new SuccessResponse(new LinkTokenReadModel(result.Result!.Token))); + } + + var existing = await _accountRepository.GetLinkTokenAsync(principal, ct); + if (existing is null) + { + // 503 shape is intentionally non-standard (has `hint` instead of `code`) — the + // Kotlin client and legacy Elixir server both use this shape, so the typed + // record preserves it byte-for-byte. + return StatusCode(503, new LinkTokenUnavailableResponse("link_token_unavailable", "Retry on a primary node.")); + } + + return Ok(new SuccessResponse(new LinkTokenReadModel(existing.Value))); + } + + [HttpPost("username")] + public async Task UpdateUsername([FromBody] SettingsUsernameRequest req, CancellationToken ct) + { + var principal = PrincipalId; + return await DispatchNoContentAsync(_usernameHandler, OperationIds.SettingsUsernameUpdate, new UpdateUsernameCommand(req.Username) + , ct); + } + + [HttpPost("description")] + public async Task UpdateDescription([FromBody] SettingsDescriptionRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_descriptionHandler, OperationIds.SettingsDescriptionUpdate, new UpdateDescriptionCommand(req.Description) + , ct); + } + + [HttpPost("push-token")] + public async Task AddPushToken([FromBody] SettingsPushTokenRequest req, CancellationToken ct) + { + if (req.Token is not { } pushToken || string.IsNullOrWhiteSpace(pushToken.Value)) + return new ErrorResponse("Invalid push token.", ErrorCodes.InvalidPushToken, System.Net.HttpStatusCode.BadRequest); + + return await DispatchNoContentAsync(_addPushTokenHandler, OperationIds.SettingsPushTokenAdd, new AddPushTokenCommand(pushToken) + , ct); + } + + [HttpDelete("push-token")] + public async Task RemovePushToken([FromBody] SettingsPushTokenRequest req, CancellationToken ct) + { + if (req.Token is not { } pushToken || string.IsNullOrWhiteSpace(pushToken.Value)) + return new ErrorResponse("Invalid push token.", ErrorCodes.InvalidPushToken, System.Net.HttpStatusCode.BadRequest); + + return await DispatchNoContentAsync(_removePushTokenHandler, OperationIds.SettingsPushTokenRemove, new RemovePushTokenCommand(pushToken) + , ct); + } + + [HttpPost("setup-encryption")] + public async Task> SetupEncryption([FromBody] SettingsEncryptionRequest req, CancellationToken ct) + { + if (!TryResolveRecoveryCode(req.RecoveryCode.Value, out var recoveryCode, out var decryptionErrorCode)) + return new ErrorResponse("Failed to decrypt recovery code.", decryptionErrorCode, System.Net.HttpStatusCode.BadRequest); + + return await DispatchOkAsync( + _setupEncryptionHandler, + OperationIds.SettingsEncryptionSetup, + new SetupEncryptionCommand(recoveryCode), + ToEncryptionKeyResponse, + ct); + } + + [HttpPost("recover-encryption")] + public async Task> RecoverEncryption([FromBody] SettingsEncryptionRequest req, CancellationToken ct) + { + if (!TryResolveRecoveryCode(req.RecoveryCode.Value, out var recoveryCode, out var decryptionErrorCode)) + return new ErrorResponse("Failed to decrypt recovery code.", decryptionErrorCode, System.Net.HttpStatusCode.BadRequest); + + return await DispatchOkAsync( + _recoverEncryptionHandler, + OperationIds.SettingsEncryptionRecover, + new RecoverEncryptionCommand(recoveryCode), + ToEncryptionKeyResponse, + ct); + } + + [HttpPost("reset-encryption")] + public async Task ResetEncryption(CancellationToken ct) + { + return await DispatchNoContentAsync(_resetEncryptionHandler, OperationIds.SettingsEncryptionReset, new ResetEncryptionCommand() + , ct); + } + + [HttpPut("avatar")] + [Consumes("multipart/form-data")] + public async Task UploadAvatarMultipart(CancellationToken ct) + { + return await HandleAvatarUploadAsync( + async (c) => await _accountRepository.GetPublicProfileAsync(PrincipalId, c), + async (principal, stream, c) => await _avatarStorage.SaveSystemAvatarAsync(principal, stream, c), + async (url, c) => CommandNoContent(await _uploadAvatarHandler.HandleAsync(BuildEnvelope(OperationIds.SettingsAvatarUpload, new UploadAvatarCommand(url, AvatarSource.Local)), c)), + _avatarStorage, + ct); + } + + [HttpPut("avatar")] + [Consumes("application/json")] + public async Task UploadAvatarByUrl([FromBody] AvatarUrlUploadRequest req, CancellationToken ct) + { + if (req is null) + return new ErrorResponse("Avatar URL payload required.", ErrorCodes.AvatarUrlInvalid, System.Net.HttpStatusCode.BadRequest); + + return await HandleAvatarUrlUploadAsync( + req.Url.Value, + async (c) => await _accountRepository.GetPublicProfileAsync(PrincipalId, c), + async (url, c) => CommandNoContent(await _uploadAvatarHandler.HandleAsync(BuildEnvelope(OperationIds.SettingsAvatarUpload, new UploadAvatarCommand(url, AvatarSource.External)), c)), + _avatarStorage, + ct); + } + + [HttpDelete("avatar")] + public async Task DeleteAvatar(CancellationToken ct) + { + return await HandleAvatarDeleteAsync( + async (c) => await _accountRepository.GetPublicProfileAsync(PrincipalId, c), + async (c) => CommandNoContent(await _deleteAvatarHandler.HandleAsync(BuildEnvelope(OperationIds.SettingsAvatarDelete, new DeleteAvatarCommand()), c)), + _avatarStorage, + ct); + } + + /// + /// Dispatches a PluralKit import onto the background worker. Returns 202 Accepted + /// with the dispatched operation_id; the actual import outcome is pushed over the + /// WebSocket as pk_import_complete or pk_import_failed. Concurrent + /// dispatches for the same system collapse onto the in-flight operation rather + /// than starting a second importer run. + /// + [HttpPost("import-pk")] + public async Task> ImportPk([FromBody] SettingsImportRequest req, CancellationToken ct) + { + var envelope = BuildEnvelope(OperationIds.SettingsImportPk, new ImportPkCommand(req.Token) + ); + + return CommandAccepted( + await _importPkHandler.HandleAsync(envelope, ct), + r => new ImportDispatchResponse(r.OperationId, r.Status, r.StartedAt)); + } + + /// + /// Dispatches a Simply Plural import onto the background worker. Returns 202 Accepted + /// with the dispatched operation_id in <500 ms (no synchronous import work runs + /// here). The actual import outcome — including the per-import alter count for + /// successful runs — is pushed over the WebSocket as sp_import_complete or + /// sp_import_failed and the frontend already listens for those frames. + /// Concurrent dispatches for the same system collapse onto the existing in-flight + /// operation via the Cassandra LWT mutex on active_import_by_system, so a + /// browser retry, Polly retry, or double-clicked button can no longer trigger a + /// duplicate import run. + /// + [HttpPost("import-sp")] + public async Task> ImportSp([FromBody] SettingsImportRequest req, CancellationToken ct) + { + // recoveryCode is nullable at this layer, but the resolver's out param is a + // non-nullable RecoveryCode — we can't write it directly into the nullable outer + // local, so the guard splits into a nested-if. Splitting it also makes the + // "code supplied but blank" case a clean no-op: nothing to decrypt, nothing to + // return an error for. + RecoveryCode? recoveryCode = null; + if (req.RecoveryCode is { } suppliedRecoveryCode + && !string.IsNullOrWhiteSpace(suppliedRecoveryCode.Value)) + { + if (!TryResolveRecoveryCode(suppliedRecoveryCode.Value, out var resolved, out var decryptionErrorCode)) + return new ErrorResponse("Failed to decrypt recovery code.", decryptionErrorCode, System.Net.HttpStatusCode.BadRequest); + + recoveryCode = resolved; + } + + var envelope = BuildEnvelope(OperationIds.SettingsImportSp, new ImportSpCommand(req.Token, recoveryCode) + ); + + return CommandAccepted( + await _importSpHandler.HandleAsync(envelope, ct), + r => new ImportDispatchResponse(r.OperationId, r.Status, r.StartedAt)); + } + + [HttpPost("unlink_discord")] + public async Task UnlinkDiscord(CancellationToken ct) + { + return await DispatchNoContentAsync(_unlinkDiscordHandler, OperationIds.SettingsAuthUnlinkDiscord, new UnlinkDiscordCommand() + , ct); + } + + [HttpPost("unlink_email")] + public async Task UnlinkEmail(CancellationToken ct) + { + return await DispatchNoContentAsync(_unlinkEmailHandler, OperationIds.SettingsAuthUnlinkEmail, new UnlinkEmailCommand() + , ct); + } + + [HttpPost("unlink_apple")] + public async Task UnlinkApple(CancellationToken ct) + { + return await DispatchNoContentAsync(_unlinkAppleHandler, OperationIds.SettingsAuthUnlinkApple, new UnlinkAppleCommand() + , ct); + } + + [HttpPost("delete-account")] + public async Task DeleteAccount(CancellationToken ct) + { + return await DispatchNoContentAsync(_deleteAccountHandler, OperationIds.SettingsAccountDelete, new DeleteAccountCommand() + , ct); + } + + [HttpPost("wipe-alters")] + public async Task WipeAlters(CancellationToken ct) + { + return await DispatchNoContentAsync(_wipeAltersHandler, OperationIds.SettingsAltersWipe, new WipeAltersCommand() + , ct); + } + + [HttpPost("wipe-tags")] + public async Task WipeTags(CancellationToken ct) + { + return await DispatchNoContentAsync(_wipeTagsHandler, OperationIds.SettingsTagsWipe, new WipeTagsCommand() + , ct); + } + + [HttpPost("fields")] + public async Task> CreateField([FromBody] SettingsCreateFieldRequest req, CancellationToken ct) + { + // CreateFieldCommandHandler owns InsertedAtUtc on the public-API path and derives it + // from the envelope's OccurredAt right before calling the repo. Passing `default` + // here keeps the hashed payload stable across retries with the same idempotency key + // (otherwise every call would stamp a fresh DateTime.UtcNow and look like a + // different request, triggering ConflictDuplicate on every replay). + var envelope = BuildEnvelope(OperationIds.SettingsFieldCreate, new CreateFieldCommand(req.Name, req.Type ?? FieldType.Text, req.SecurityLevel ?? VisibilityLevel.Private, req.Locked ?? false, InsertedAtUtc: default) + ); + + var execution = await _createFieldHandler.HandleAsync(envelope, ct); + if (!execution.Accepted) + return ConflictToError(execution.Conflict!); + + return new SuccessResponse(new FieldCreatedResponse(execution.Result!.FieldId), System.Net.HttpStatusCode.Created, execution.Result.Replay); + } + + [HttpPatch("fields/{id}")] + public async Task UpdateField([FromRoute] FieldId id, [FromBody] SettingsUpdateFieldRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_updateFieldHandler, OperationIds.SettingsFieldUpdate, new UpdateFieldCommand(id, req.Name, req.SecurityLevel, req.Locked) + , ct); + } + + [HttpDelete("fields/{id}")] + public async Task DeleteField([FromRoute] FieldId id, CancellationToken ct) + { + return await DispatchNoContentAsync(_deleteFieldHandler, OperationIds.SettingsFieldDelete, new DeleteFieldCommand(id) + , ct); + } + + [HttpPost("fields/{id}/relocate")] + public async Task RelocateField([FromRoute] FieldId id, [FromBody] SettingsRelocateFieldRequest req, CancellationToken ct) + { + return await DispatchNoContentAsync(_relocateFieldHandler, OperationIds.SettingsFieldRelocate, new RelocateFieldCommand(id, req.Index) + , ct); + } + + /* New endpoints from here! */ + [HttpGet("public-key")] + [AllowAnonymous] + public async Task> GetPublicKey(CancellationToken ct) + { + var authenticationConfiguration = _authenticationConfiguration.CurrentValue; + return authenticationConfiguration.Rsa256PublicKey; + } + + /// + /// Serves the per-platform Firebase client-init payload the mobile / wasm apps fetch + /// at runtime. Values are PUBLIC (they previously shipped inside every distributed + /// bundle as google-services.json / GoogleService-Info.plist / hardcoded + /// web config), so the endpoint is anonymous — mirrors the public-key shape + /// above. + /// + /// Contract: + /// + /// ?platform=android|ios|web — required. Unknown / missing values return 400 with code invalid_platform. + /// Configured platform absent from internal.secrets — 503 firebase_config_unavailable. The deployment hasn't seeded the matching row via the bootstrapper's Firebase phase. + /// Cache headers: Cache-Control: public, max-age=300, must-revalidate plus a body-hash ETag. The client keeps a 7-day cache (see FirebaseConfigProvider); the server ceiling of 5 minutes bounds how long a rotated config takes to reach the service-worker install path. + /// + /// + /// + [HttpGet("firebase-config")] + [AllowAnonymous] + public async Task> GetFirebaseConfig( + [FromQuery] string? platform, + CancellationToken ct) + { + var (payload, error) = FirebaseConfigResolver.Resolve(_firebaseClientConfiguration.CurrentValue, platform); + if (error is not null) + return error; + + FirebaseConfigResolver.ApplyCacheHeaders(Response, payload!); + return new SuccessResponse(payload!); + } + + private bool TryResolveRecoveryCode(string candidate, out RecoveryCode recoveryCode, out ErrorCode errorCode) + => Helpers.RecoveryCodeResolver.TryResolve(candidate, _authenticationConfiguration.CurrentValue.Rsa256PrivateKey, out recoveryCode, out errorCode); + + /// + /// Setup / recover both project to the same wire shape (base64-encoded UTF-8 bytes + /// of the symmetric key material), and both handlers surface an + /// , so the projection lives once here and + /// both endpoints hand it to DispatchOkAsync. + /// + private static EncryptionKeyResponse ToEncryptionKeyResponse(EncryptionCommandResult result) + => new(Convert.ToBase64String(Encoding.UTF8.GetBytes(result.Key.Value))); +} diff --git a/apis/Interfold.Settings.Api/DependencyInjection/SettingsModuleServiceCollectionExtensions.cs b/apis/Interfold.Settings.Api/DependencyInjection/SettingsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..8c450562 --- /dev/null +++ b/apis/Interfold.Settings.Api/DependencyInjection/SettingsModuleServiceCollectionExtensions.cs @@ -0,0 +1,79 @@ +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Settings.Api.Services.Export; +using Interfold.Settings.Api.Services.Http; +using Interfold.Settings.Api.Services.ImportJobs; +using Interfold.Settings.Api.Services.Secrets; +using Interfold.Settings.Api.Services.SimplyPlural; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Domain; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Accounts; +using Interfold.Settings.Domain.Settings; +using Interfold.Shared.Contracts; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Api.DependencyInjection; + +/// Settings feature module — the DI equivalent of the twenty-two settings-handler +/// AddSingleton calls previously living in +/// , +/// plus the SimplyPlural import stack, the async-import job runners + drain loop, and the +/// two Firebase IPostConfigureOptions bindings that were sitting inline in +/// Interfold.Api.Host/Program.cs. Consumed once from +/// Interfold.Api.Host/Program.cs alongside the other module extensions. +public static class SettingsModuleServiceCollectionExtensions +{ + /// Registers everything the Settings feature owns: twenty-two command-handler + /// singletons (account / encryption / avatar / import-dispatch / unlink / delete-account / + /// wipe-alters / wipe-tags / field CRUD), the two + /// implementations (PkImportJobRunner and SpImportJobRunner), + /// as an drain loop, + /// plus its named + /// (with the shared + /// handler-chain), and the two + /// bindings for + /// + . The + /// registration stays inside + /// AddInterfoldCluster (infrastructure-owned single-channel implementation). + public static IServiceCollection AddSettingsModule(this IServiceCollection services) + { + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + + services.AddSingleton, FirebaseClientSecretsPostConfigure>(); + services.AddSingleton, FcmSecretsPostConfigure>(); + + services.AddTransient(); + services.AddHttpClient(HttpClientNames.SimplyPlural).AddHttpMessageHandler(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddHostedService(); + services.AddSingleton(); + services.AddSingleton(); + + return services; + } +} diff --git a/apis/Interfold.Settings.Api/Helpers/FirebaseConfigResolver.cs b/apis/Interfold.Settings.Api/Helpers/FirebaseConfigResolver.cs new file mode 100644 index 00000000..ecbd0a89 --- /dev/null +++ b/apis/Interfold.Settings.Api/Helpers/FirebaseConfigResolver.cs @@ -0,0 +1,88 @@ +using System.Net; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Contracts.Models.Wire; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Settings.Api.Helpers; + +/// Pure-function bridge from to the +/// polymorphic shape. Extracted from +/// SettingsController so mapping + cache-header logic is unit-testable without a +/// TestServer. +public static class FirebaseConfigResolver +{ + // Mirrors the controller's snake-case options so the ETag hashes the same bytes the + // client actually receives. + internal static readonly JsonSerializerOptions EtagJsonOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + WriteIndented = false, + }; + + /// Returns either a populated payload OR a shaped + /// (never both, never both null). Errors: invalid_platform/400, + /// firebase_config_unavailable/503. + public static (FirebaseClientConfigResponse? Payload, ErrorResponse? Error) Resolve( + FirebaseClientConfiguration configuration, + string? platform) + { + // Raw string so an unknown platform still yields the legacy invalid_platform 400. + if (!platform.TryParseWire(out var parsed)) + { + return (null, new ErrorResponse( + "Invalid platform. Expected one of: android, ios, web.", + ErrorCodes.InvalidPlatform, + HttpStatusCode.BadRequest)); + } + + switch (parsed) + { + case ClientPlatform.Android: + var android = configuration.Android; + if (android is null) return (null, Unavailable()); + return (new FirebaseAndroidConfigResponse( + android.ApiKey, android.ApplicationId, android.ProjectId, + android.GcmSenderId, android.StorageBucket), null); + + case ClientPlatform.Ios: + var ios = configuration.Ios; + if (ios is null) return (null, Unavailable()); + return (new FirebaseIosConfigResponse( + ios.ApiKey, ios.GoogleAppId, ios.GcmSenderId, ios.ProjectId, + ios.StorageBucket, ios.BundleId, ios.ClientId), null); + + case ClientPlatform.Web: + var web = configuration.Web; + if (web is null) return (null, Unavailable()); + return (new FirebaseWebConfigResponse( + web.ApiKey, web.AuthDomain, web.ProjectId, web.StorageBucket, + web.MessagingSenderId, web.AppId, web.VapidKey), null); + + default: + throw new InvalidOperationException( + $"Unhandled {nameof(ClientPlatform)} value '{parsed}' after wire parse."); + } + } + + /// Stamps Cache-Control: max-age=300 and a snake-case-JSON body-hash + /// ETag. 5min is the ceiling for wasm service-worker rotation pickup. + public static void ApplyCacheHeaders(HttpResponse response, FirebaseClientConfigResponse payload) + { + response.Headers["Cache-Control"] = "public, max-age=300, must-revalidate"; + + var canonical = JsonSerializer.Serialize(payload, EtagJsonOptions); + var hash = SHA256.HashData(Encoding.UTF8.GetBytes(canonical)); + var hex = Convert.ToHexString(hash); + response.Headers.ETag = $"\"{hex}\""; + } + + private static ErrorResponse Unavailable() => new( + "Firebase config is not available for the requested platform.", + ErrorCodes.FirebaseConfigUnavailable, + HttpStatusCode.ServiceUnavailable); +} diff --git a/apis/Interfold.Settings.Api/Helpers/RecoveryCodeResolver.cs b/apis/Interfold.Settings.Api/Helpers/RecoveryCodeResolver.cs new file mode 100644 index 00000000..6607af05 --- /dev/null +++ b/apis/Interfold.Settings.Api/Helpers/RecoveryCodeResolver.cs @@ -0,0 +1,98 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts; +using Microsoft.AspNetCore.WebUtilities; + +namespace Interfold.Settings.Api.Helpers; + +public static class RecoveryCodeResolver +{ + /// + /// Decrypt a compact JWE recovery-code ciphertext and return the plaintext wrapped in + /// a whose redacts. + /// Wrapping inside the resolver keeps the plaintext as a bare string only + /// inside (no logging or interpolation surface); every + /// external observation goes through the redacted ToString. + /// + public static bool TryResolve(string candidate, string privateKeyPem, out RecoveryCode recoveryCode, out ErrorCode errorCode) + { + recoveryCode = default; + if (string.IsNullOrWhiteSpace(candidate)) + { + errorCode = ErrorCodes.RecoveryCodeNotProvided; + return false; + } + if (!LooksLikeCompactJwe(candidate)) + { + errorCode = ErrorCodes.RecoveryCodeNotJwe; + return false; + } + + if (!TryLoadEncryptionPrivateKey(ref privateKeyPem) + || !TryDecryptJwe(candidate, privateKeyPem, out var plaintext)) + { + errorCode = ErrorCodes.DecryptionError; + return false; + } + + errorCode = default; + // Wrap-at-decrypt-boundary: the plaintext local dies with this method frame; every + // downstream reference goes through the redacting RecoveryCode wrapper. + recoveryCode = new(plaintext); + return !string.IsNullOrWhiteSpace(plaintext); + } + + public static bool LooksLikeCompactJwe(string token) => token.Count(ch => ch == '.') == 4; + + private static bool TryLoadEncryptionPrivateKey(ref string privateKeyPem) + { + privateKeyPem = privateKeyPem.Trim().Replace("\\n", "\n", StringComparison.Ordinal); + return privateKeyPem.Contains("BEGIN", StringComparison.Ordinal); + } + + private static bool TryDecryptJwe(string compactJwe, string privateKeyPem, out string plaintext) + { + plaintext = string.Empty; + + try + { + var parts = compactJwe.Split('.'); + if (parts.Length != 5) + return false; + + var headerJson = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(parts[0])); + using var header = JsonDocument.Parse(headerJson); + var alg = header.RootElement.TryGetProperty("alg", out var algProp) ? algProp.GetString() : null; + var enc = header.RootElement.TryGetProperty("enc", out var encProp) ? encProp.GetString() : null; + + if (!string.Equals(alg, "RSA-OAEP-256", StringComparison.Ordinal) + || !string.Equals(enc, "A256GCM", StringComparison.Ordinal)) + { + return false; + } + + var encryptedKey = WebEncoders.Base64UrlDecode(parts[1]); + var iv = WebEncoders.Base64UrlDecode(parts[2]); + var ciphertext = WebEncoders.Base64UrlDecode(parts[3]); + var tag = WebEncoders.Base64UrlDecode(parts[4]); + var aad = Encoding.ASCII.GetBytes(parts[0]); + + using var rsa = RSA.Create(); + rsa.ImportFromPem(privateKeyPem); + var cek = rsa.Decrypt(encryptedKey, RSAEncryptionPadding.OaepSHA256); + + var decrypted = new byte[ciphertext.Length]; + using var aes = new AesGcm(cek, tag.Length); + aes.Decrypt(iv, ciphertext, tag, decrypted, aad); + + plaintext = Encoding.UTF8.GetString(decrypted); + return !string.IsNullOrWhiteSpace(plaintext); + } + catch (Exception) + { + return false; + } + } +} diff --git a/apis/Interfold.Settings.Api/Interfold.Settings.Api.csproj b/apis/Interfold.Settings.Api/Interfold.Settings.Api.csproj new file mode 100644 index 00000000..906335be --- /dev/null +++ b/apis/Interfold.Settings.Api/Interfold.Settings.Api.csproj @@ -0,0 +1,46 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Settings.Api/Services/Export/ExportHelpers.cs b/apis/Interfold.Settings.Api/Services/Export/ExportHelpers.cs new file mode 100644 index 00000000..99e397ba --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Export/ExportHelpers.cs @@ -0,0 +1,66 @@ +using System.Text; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Api.Services.Export; + +// Reference parity helpers for the pk-side transforms in +// octocon/lib/octocon/accounts.ex:1273-1332. +internal static class ExportHelpers +{ + // Mirrors Elixir's `String.byte_slice(input, 0, maxBytes)`: clip on the UTF-8 byte + // boundary WITHOUT splitting a codepoint. Null collapses to empty (matches + // `(input || "") |> String.byte_slice`). + public static string ByteTruncateUtf8(string? input, int maxBytes) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + var utf8 = Encoding.UTF8; + var byteCount = utf8.GetByteCount(input); + if (byteCount <= maxBytes) + return input; + + var bytes = utf8.GetBytes(input); + var take = maxBytes; + // Walk back until we're not sitting on a UTF-8 continuation byte (10xxxxxx) — + // otherwise GetString would either throw or emit a replacement character. + while (take > 0 && (bytes[take] & 0b1100_0000) == 0b1000_0000) + { + take--; + } + return utf8.GetString(bytes, 0, take); + } + + // Mirrors Elixir's `format_import_color/1` (accounts.ex:1417): nil → nil, "#RRGGBB" → "RRGGBB", + // anything else (short forms, alpha suffixes, missing '#') → nil so PK ignores the field. + public static string? FormatImportColor(HexColor? color) + { + if (color is not { } value) + return null; + + var raw = value.Value; + // `byte_size(color) == 7` ⇒ "#" + 6 hex chars; hex validity is already enforced by + // HexColor's ctor so a length check is sufficient here. + if (raw.Length != 7 || raw[0] != '#') + return null; + + return raw[1..]; + } + + // Split on the literal "text" sentinel (Elixir's `String.split(proxy |> trim, "text", parts: 2)`). + // If "text" doesn't appear, the reference errors on the pattern-match; guard here so a bad + // row can't 500 the entire export. + public static PkProxyTag? TrySplitProxyTag(string proxy) + { + var trimmed = proxy.Trim(); + var idx = trimmed.IndexOf("text", StringComparison.Ordinal); + if (idx < 0) + return null; + + var prefix = trimmed[..idx]; + var suffix = trimmed[(idx + "text".Length)..]; + return new PkProxyTag( + ByteTruncateUtf8(prefix, 50), + ByteTruncateUtf8(suffix, 50)); + } +} diff --git a/apis/Interfold.Settings.Api/Services/Export/ExportJsonOptions.cs b/apis/Interfold.Settings.Api/Services/Export/ExportJsonOptions.cs new file mode 100644 index 00000000..0343677f --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Export/ExportJsonOptions.cs @@ -0,0 +1,17 @@ +using System.Text.Json; + +namespace Interfold.Settings.Api.Services.Export; + +// Every wire key is stamped by [JsonPropertyName] on the payload records, so no naming +// policy is applied — a missing/typo'd key breaks the pinned-fixture test loudly rather +// than being silently rewritten by SnakeCaseLower. `DefaultIgnoreCondition` is left at +// the default so the reference's null fields (color/description/parent_tag_id/…) survive +// on the wire. +public static class ExportJsonOptions +{ + public static readonly JsonSerializerOptions Default = new(JsonSerializerDefaults.Web) + { + PropertyNamingPolicy = null, + DictionaryKeyPolicy = null, + }; +} diff --git a/apis/Interfold.Settings.Api/Services/Export/ExportPayloads.cs b/apis/Interfold.Settings.Api/Services/Export/ExportPayloads.cs new file mode 100644 index 00000000..d998f21f --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Export/ExportPayloads.cs @@ -0,0 +1,104 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Settings.Api.Services.Export; + +// Every [JsonPropertyName] key is hand-picked to mirror +// accounts.ex:1273-1415 byte-for-byte. No naming policy +// is applied at the serializer boundary (ExportJsonOptions) — the intent is that any +// forgotten key or typo surfaces immediately in the pinned-fixture tests rather than +// being papered over by SnakeCaseLower. +public sealed record PkExportPayload( + [property: JsonPropertyName("version")] int Version, + [property: JsonPropertyName("name")] string? Name, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("avatar_url")] string? AvatarUrl, + [property: JsonPropertyName("switches")] IReadOnlyList Switches, + [property: JsonPropertyName("members")] IReadOnlyList Members, + [property: JsonPropertyName("groups")] IReadOnlyList Groups); + +public sealed record PkMember( + [property: JsonPropertyName("id")] string Id, + [property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("pronouns")] string Pronouns, + [property: JsonPropertyName("description")] string Description, + [property: JsonPropertyName("color")] string? Color, + [property: JsonPropertyName("avatar_url")] string? AvatarUrl, + [property: JsonPropertyName("proxy_tags")] IReadOnlyList ProxyTags, + [property: JsonPropertyName("display_name")] string? DisplayName); + +public sealed record PkProxyTag( + [property: JsonPropertyName("prefix")] string Prefix, + [property: JsonPropertyName("suffix")] string Suffix); + +public sealed record PkGroup( + [property: JsonPropertyName("id")] string Id, + [property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("color")] string? Color, + [property: JsonPropertyName("members")] IReadOnlyList Members); + +public sealed record FullExportPayload( + [property: JsonPropertyName("user")] FullExportUser User, + [property: JsonPropertyName("alters")] IReadOnlyList Alters, + [property: JsonPropertyName("fronts")] IReadOnlyList Fronts, + [property: JsonPropertyName("tags")] IReadOnlyList Tags, + [property: JsonPropertyName("polls")] IReadOnlyList Polls); + +public sealed record FullExportUser( + [property: JsonPropertyName("username")] string? Username, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("id")] string Id, + [property: JsonPropertyName("avatar_url")] string? AvatarUrl, + [property: JsonPropertyName("fields")] IReadOnlyList Fields); + +public sealed record FullExportUserField( + [property: JsonPropertyName("id")] Guid Id, + [property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("type")] FieldType Type, + [property: JsonPropertyName("locked")] bool Locked, + [property: JsonPropertyName("security_level")] VisibilityLevel SecurityLevel); + +public sealed record FullExportAlter( + [property: JsonPropertyName("id")] short Id, + [property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("pronouns")] string? Pronouns, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("color")] string? Color, + [property: JsonPropertyName("avatar_url")] string? AvatarUrl, + [property: JsonPropertyName("proxy_name")] string? ProxyName, + [property: JsonPropertyName("discord_proxies")] IReadOnlyList DiscordProxies, + [property: JsonPropertyName("fields")] IReadOnlyList Fields); + +public sealed record FullExportAlterField( + [property: JsonPropertyName("id")] Guid Id, + [property: JsonPropertyName("value")] string? Value); + +public sealed record FullExportFront( + [property: JsonPropertyName("id")] Guid Id, + [property: JsonPropertyName("alter_id")] short AlterId, + [property: JsonPropertyName("comment")] string? Comment, + [property: JsonPropertyName("time_start")] DateTimeOffset TimeStart, + [property: JsonPropertyName("time_end")] DateTimeOffset? TimeEnd); + +public sealed record FullExportTag( + [property: JsonPropertyName("id")] Guid Id, + [property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("color")] string? Color, + [property: JsonPropertyName("security_level")] VisibilityLevel SecurityLevel, + [property: JsonPropertyName("parent_tag_id")] Guid? ParentTagId, + [property: JsonPropertyName("inserted_at")] DateTime InsertedAt, + [property: JsonPropertyName("updated_at")] DateTime UpdatedAt, + [property: JsonPropertyName("alters")] IReadOnlyList Alters); + +public sealed record FullExportPoll( + [property: JsonPropertyName("id")] Guid Id, + [property: JsonPropertyName("title")] string Title, + [property: JsonPropertyName("description")] string? Description, + [property: JsonPropertyName("type")] PollType Type, + [property: JsonPropertyName("data")] JsonElement Data, + [property: JsonPropertyName("time_end")] DateTime? TimeEnd, + [property: JsonPropertyName("inserted_at")] DateTime InsertedAt, + [property: JsonPropertyName("updated_at")] DateTime UpdatedAt); diff --git a/apis/Interfold.Settings.Api/Services/Export/ExportService.cs b/apis/Interfold.Settings.Api/Services/Export/ExportService.cs new file mode 100644 index 00000000..b99cbaa3 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Export/ExportService.cs @@ -0,0 +1,166 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Api.Services.Export; + +// Composes the six repositories that back the two export shapes. Structural mirror of +// Octocon.Accounts.{gather_export_data, format_pk_export, format_full_export} — +// octocon/lib/octocon/accounts.ex:1179-1415. Ordering / byte-truncation / +// color-strip semantics live in ExportHelpers. +public sealed class ExportService : IExportService +{ + private readonly IAccountRepository _accounts; + private readonly IAlterRepository _alters; + private readonly ITagRepository _tags; + private readonly IPollRepository _polls; + private readonly IFrontingRepository _fronts; + private readonly ISettingsFieldRepository _fields; + + public ExportService( + IAccountRepository accounts, + IAlterRepository alters, + ITagRepository tags, + IPollRepository polls, + IFrontingRepository fronts, + ISettingsFieldRepository fields) + { + _accounts = accounts; + _alters = alters; + _tags = tags; + _polls = polls; + _fronts = fronts; + _fields = fields; + } + + public async Task BuildPkAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var profileTask = _accounts.GetPublicProfileAsync(systemId, cancellationToken); + var altersTask = _alters.ListAsync(systemId, cancellationToken); + var tagsTask = _tags.ListAsync(systemId, cancellationToken); + await Task.WhenAll(profileTask, altersTask, tagsTask); + + var profile = await profileTask; + var alters = await altersTask; + var tags = await tagsTask; + + var members = alters.Select(BuildPkMember).ToArray(); + var groups = tags.Select((tag, index) => BuildPkGroup(tag, index)).ToArray(); + + return new PkExportPayload( + Version: 2, + Name: profile?.Username?.Value, + Description: ExportHelpers.ByteTruncateUtf8(profile?.Description, 1000), + AvatarUrl: profile?.AvatarUrl?.Value, + Switches: Array.Empty(), + Members: members, + Groups: groups); + } + + public async Task BuildFullAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var profileTask = _accounts.GetPublicProfileAsync(systemId, cancellationToken); + var altersTask = _alters.ListAsync(systemId, cancellationToken); + var tagsTask = _tags.ListAsync(systemId, cancellationToken); + var pollsTask = _polls.ListAsync(systemId, cancellationToken); + var frontsTask = _fronts.ListAllAsync(systemId, cancellationToken); + var fieldsTask = _fields.ListAsync(systemId, cancellationToken); + await Task.WhenAll(profileTask, altersTask, tagsTask, pollsTask, frontsTask, fieldsTask); + + var profile = await profileTask; + var alters = await altersTask; + var tags = await tagsTask; + var polls = await pollsTask; + var fronts = await frontsTask; + var fields = await fieldsTask; + + var user = new FullExportUser( + Username: profile?.Username?.Value, + Description: profile?.Description, + Id: systemId.Value, + AvatarUrl: profile?.AvatarUrl?.Value, + Fields: fields.Select(f => new FullExportUserField(f.Id.Value, f.Name, f.Type, f.Locked, f.SecurityLevel)).ToArray()); + + var alterEntries = alters.Select(BuildFullAlter).ToArray(); + var frontEntries = fronts.Select(BuildFullFront).ToArray(); + var tagEntries = tags.Select(BuildFullTag).ToArray(); + var pollEntries = polls.Select(BuildFullPoll).ToArray(); + + return new FullExportPayload(user, alterEntries, frontEntries, tagEntries, pollEntries); + } + + private static PkMember BuildPkMember(AlterReadModel alter) + { + var proxyTags = alter.DiscordProxies + .Select(ExportHelpers.TrySplitProxyTag) + .Where(t => t is not null) + .Select(t => t!) + .ToArray(); + + return new PkMember( + Id: alter.Id.Value.ToString(System.Globalization.CultureInfo.InvariantCulture), + Name: ExportHelpers.ByteTruncateUtf8(alter.Name, 100), + Pronouns: ExportHelpers.ByteTruncateUtf8(alter.Pronouns, 100), + Description: ExportHelpers.ByteTruncateUtf8(alter.Description, 1000), + Color: ExportHelpers.FormatImportColor(alter.Color), + AvatarUrl: alter.AvatarUrl?.Value, + ProxyTags: proxyTags, + DisplayName: alter.ProxyName); + } + + private static PkGroup BuildPkGroup(TagReadModel tag, int index) => new( + Id: index.ToString(System.Globalization.CultureInfo.InvariantCulture), + Name: tag.Name, + Description: tag.Description, + Color: ExportHelpers.FormatImportColor(tag.Color), + Members: tag.Alters + .Select(a => a.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)) + .ToArray()); + + private static FullExportAlter BuildFullAlter(AlterReadModel alter) => new( + Id: alter.Id.Value, + Name: alter.Name, + Pronouns: alter.Pronouns, + Description: alter.Description, + Color: alter.Color?.Value, + AvatarUrl: alter.AvatarUrl?.Value, + ProxyName: alter.ProxyName, + DiscordProxies: alter.DiscordProxies, + Fields: alter.Fields.Select(f => new FullExportAlterField(f.Id.Value, f.Value)).ToArray()); + + private static FullExportFront BuildFullFront(FrontHistoryReadModel front) => new( + Id: front.Id.Value, + AlterId: front.AlterId.Value, + Comment: front.Comment, + TimeStart: front.TimeStart, + TimeEnd: front.TimeEnd); + + private static FullExportTag BuildFullTag(TagReadModel tag) => new( + Id: tag.Id.Value, + Name: tag.Name, + Description: tag.Description, + Color: tag.Color?.Value, + SecurityLevel: tag.SecurityLevel, + ParentTagId: tag.ParentTagId?.Value, + InsertedAt: tag.InsertedAt, + UpdatedAt: tag.UpdatedAt, + Alters: tag.Alters.Select(a => a.Value).ToArray()); + + private static FullExportPoll BuildFullPoll(PollReadModel poll) => new( + Id: poll.Id.Value, + Title: poll.Title, + Description: poll.Description, + Type: poll.Type, + Data: poll.Data, + TimeEnd: poll.TimeEnd, + InsertedAt: poll.InsertedAt, + UpdatedAt: poll.UpdatedAt); +} diff --git a/apis/Interfold.Settings.Api/Services/Export/IExportService.cs b/apis/Interfold.Settings.Api/Services/Export/IExportService.cs new file mode 100644 index 00000000..753eca9c --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Export/IExportService.cs @@ -0,0 +1,9 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Api.Services.Export; + +public interface IExportService +{ + Task BuildPkAsync(SystemId systemId, CancellationToken cancellationToken = default); + Task BuildFullAsync(SystemId systemId, CancellationToken cancellationToken = default); +} diff --git a/apis/Interfold.Settings.Api/Services/Http/HttpLogModels.cs b/apis/Interfold.Settings.Api/Services/Http/HttpLogModels.cs new file mode 100644 index 00000000..1d00a7dc --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Http/HttpLogModels.cs @@ -0,0 +1,55 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Api.Services.Http; + +internal sealed class RequestMeta +{ + [JsonPropertyName("Method")] + public string? Method { get; set; } + + [JsonPropertyName("Uri")] + public string? Uri { get; set; } + + [JsonPropertyName("RequestHost")] + public string? RequestHost { get; set; } + + [JsonPropertyName("NormalizedRequestPathQuery")] + public string? NormalizedRequestPathQuery { get; set; } + + [JsonPropertyName("Headers")] + public Dictionary? Headers { get; set; } + + [JsonPropertyName("ContentHeaders")] + public Dictionary? ContentHeaders { get; set; } +} + +internal sealed class ResponseMeta +{ + [JsonPropertyName("RequestMethod")] + public string? RequestMethod { get; set; } + + [JsonPropertyName("RequestUri")] + public string? RequestUri { get; set; } + + [JsonPropertyName("RequestHost")] + public string? RequestHost { get; set; } + + [JsonPropertyName("NormalizedRequestPathQuery")] + public string? NormalizedRequestPathQuery { get; set; } + + [JsonPropertyName("Status")] + public int Status { get; set; } + + [JsonPropertyName("Reason")] + public string? Reason { get; set; } + + [JsonPropertyName("Uri")] + public string? Uri { get; set; } + + [JsonPropertyName("Headers")] + public Dictionary? Headers { get; set; } + + [JsonPropertyName("ContentHeaders")] + public Dictionary? ContentHeaders { get; set; } +} + diff --git a/apis/Interfold.Settings.Api/Services/Http/HttpLoggingHandler.cs b/apis/Interfold.Settings.Api/Services/Http/HttpLoggingHandler.cs new file mode 100644 index 00000000..fdae0dfa --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Http/HttpLoggingHandler.cs @@ -0,0 +1,393 @@ +using System.Text.Json; + +namespace Interfold.Settings.Api.Services.Http; + +public class HttpLoggingHandler : DelegatingHandler +{ + private readonly string _basePath; + private readonly ILogger _logger; + private readonly bool _replayEnabled; + private readonly bool _replayStrict; + private readonly bool _enabled; + + public HttpLoggingHandler(IConfiguration configuration, ILogger logger) + { + _logger = logger; + _basePath = configuration["HttpMessageLog:BasePath"] ?? "http-messages"; + _replayEnabled = configuration.GetValue("HttpMessageLog:Replay:Enabled", false); + _replayStrict = configuration.GetValue("HttpMessageLog:Replay:Strict", false); + _enabled = configuration.GetValue("HttpMessageLog:Enabled", false); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + if (!_enabled) + { + return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + } + + try + { + var requestId = Guid.NewGuid().ToString("N"); + var uri = request.RequestUri ?? new Uri("http://unknown/"); + + var segments = uri.Segments + .Select(s => s.Trim('/')) + .Where(s => !string.IsNullOrWhiteSpace(s)) + .ToArray(); + + var host = uri.Host ?? "unknown"; + + // Layout: //// + var endpoint = segments.Length > 0 ? segments.Last() : "root"; + var folderSegments = new List { _basePath, host }; + if (segments.Length > 1) + folderSegments.AddRange(segments.Take(segments.Length - 1)); + + var requestFolder = Path.Combine(folderSegments.Concat(new[] { "request" }).ToArray()); + Directory.CreateDirectory(requestFolder); + + if (request.Content != null) + { + var reqBytes = await request.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); + + var reqExt = GetExtensionFromMediaType(request.Content.Headers.ContentType?.MediaType); + var reqFile = Path.Combine(requestFolder, $"{SanitizeFileName(endpoint)}_{DateTime.UtcNow:yyyyMMddHHmmssfff}_{requestId}.request{reqExt}"); + await File.WriteAllBytesAsync(reqFile, reqBytes, cancellationToken).ConfigureAwait(false); + + // Save metadata + var reqMeta = new RequestMeta + { + Method = request.Method.Method, + Uri = uri.ToString(), + RequestHost = uri.Host, + NormalizedRequestPathQuery = NormalizeUriForMatching(uri.ToString()), + Headers = request.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()), + ContentHeaders = request.Content.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()) + }; + var reqMetaFile = Path.ChangeExtension(reqFile, ".meta.json"); + await File.WriteAllTextAsync(reqMetaFile, JsonSerializer.Serialize(reqMeta, HttpLoggingJsonContext.Default.RequestMeta), cancellationToken).ConfigureAwait(false); + + // We already consumed the request body; hand back a fresh copy. + var newContent = new ByteArrayContent(reqBytes); + CopyHttpContentHeaders(request.Content, newContent); + request.Content = newContent; + } + else + { + var reqFile = Path.Combine(requestFolder, $"{SanitizeFileName(endpoint)}_{DateTime.UtcNow:yyyyMMddHHmmssfff}_{requestId}.request"); + var reqMeta = new RequestMeta + { + Method = request.Method.Method, + Uri = uri.ToString(), + RequestHost = uri.Host, + NormalizedRequestPathQuery = NormalizeUriForMatching(uri.ToString()), + Headers = request.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()), + ContentHeaders = null + }; + var reqMetaFile = Path.ChangeExtension(reqFile, ".meta.json"); + Directory.CreateDirectory(Path.GetDirectoryName(reqMetaFile) ?? requestFolder); + await File.WriteAllTextAsync(reqMetaFile, JsonSerializer.Serialize(reqMeta, HttpLoggingJsonContext.Default.RequestMeta), cancellationToken).ConfigureAwait(false); + } + + var response = default(HttpResponseMessage?); + if (_replayEnabled) + { + try + { + var replay = TryGetReplayResponse(folderSegments, endpoint, request.Method.Method, uri.ToString(), uri.Host); + if (replay != null) + { + _logger.LogInformation("Replaying HTTP response from disk for {Uri}", uri); + response = replay; + } + else if (_replayStrict) + { + throw new InvalidOperationException($"Replay enabled but no recorded response found for {uri}"); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to load replay response"); + if (_replayStrict) + throw; + } + } + + response ??= await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + + var responseFolder = Path.Combine(folderSegments.Concat(new[] { "response" }).ToArray()); + Directory.CreateDirectory(responseFolder); + + if (response.Content != null) + { + var respBytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); + var respExt = GetExtensionFromMediaType(response.Content.Headers.ContentType?.MediaType); + var respFile = Path.Combine(responseFolder, $"{SanitizeFileName(endpoint)}_{DateTime.UtcNow:yyyyMMddHHmmssfff}_{requestId}.response{respExt}"); + await File.WriteAllBytesAsync(respFile, respBytes, cancellationToken).ConfigureAwait(false); + + var respMeta = new ResponseMeta + { + RequestMethod = request.Method.Method, + RequestUri = uri.ToString(), + RequestHost = uri.Host, + NormalizedRequestPathQuery = NormalizeUriForMatching(uri.ToString()), + Status = (int)response.StatusCode, + Reason = response.ReasonPhrase, + Uri = uri.ToString(), + Headers = response.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()), + ContentHeaders = response.Content.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()) + }; + var respMetaFile = Path.ChangeExtension(respFile, ".meta.json"); + await File.WriteAllTextAsync(respMetaFile, JsonSerializer.Serialize(respMeta, HttpLoggingJsonContext.Default.ResponseMeta), cancellationToken).ConfigureAwait(false); + + var newRespContent = new ByteArrayContent(respBytes); + CopyHttpContentHeaders(response.Content, newRespContent); + response.Content = newRespContent; + } + else + { + var respFile = Path.Combine(responseFolder, $"{SanitizeFileName(endpoint)}_{DateTime.UtcNow:yyyyMMddHHmmssfff}_{requestId}.response"); + var respMeta = new ResponseMeta + { + RequestMethod = request.Method.Method, + RequestUri = uri.ToString(), + RequestHost = uri.Host, + NormalizedRequestPathQuery = NormalizeUriForMatching(uri.ToString()), + Status = (int)response.StatusCode, + Reason = response.ReasonPhrase, + Uri = uri.ToString(), + Headers = response.Headers.ToDictionary(h => h.Key, h => h.Value?.ToArray()), + ContentHeaders = null + }; + var respMetaFile = Path.ChangeExtension(respFile, ".meta.json"); + Directory.CreateDirectory(Path.GetDirectoryName(respMetaFile) ?? responseFolder); + await File.WriteAllTextAsync(respMetaFile, JsonSerializer.Serialize(respMeta, HttpLoggingJsonContext.Default.ResponseMeta), cancellationToken).ConfigureAwait(false); + } + + return response; + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to log HTTP request/response"); + return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + } + } + + private static void CopyHttpContentHeaders(HttpContent? source, HttpContent target) + { + if (source == null) return; + foreach (var header in source.Headers) + { + if (!target.Headers.TryAddWithoutValidation(header.Key, header.Value)) + { + try + { + target.Headers.Add(header.Key, header.Value); + } + catch + { + } + } + } + } + + private static string GetExtensionFromMediaType(string? mediaType) + { + if (string.IsNullOrWhiteSpace(mediaType)) + return ".bin"; + + var type = mediaType.Split(';')[0].Trim().ToLowerInvariant(); + return type switch + { + "application/json" or "text/json" => ".json", + "text/plain" => ".txt", + "text/html" => ".html", + "application/xml" or "text/xml" => ".xml", + + "image/png" => ".png", + "image/jpeg" or "image/jpg" => ".jpg", + "image/gif" => ".gif", + "image/webp" => ".webp", + "image/svg+xml" => ".svg", + "image/x-icon" or "image/vnd.microsoft.icon" => ".ico", + "image/bmp" => ".bmp", + "image/tiff" => ".tiff", + + _ => ".bin" + }; + } + + private static string SanitizeFileName(string name) + { + foreach (var c in Path.GetInvalidFileNameChars()) + name = name.Replace(c, '_'); + return name; + } + + // Returns the newest recorded response whose method/normalized-URI matches, or null. + private HttpResponseMessage? TryGetReplayResponse(List folderSegments, string endpoint, string requestMethod, string requestUri, string requestHost) + { + try + { + var responseFolder = Path.Combine(folderSegments.Concat(new[] { "response" }).ToArray()); + if (!Directory.Exists(responseFolder)) + return null; + + var files = Directory.EnumerateFiles(responseFolder) + .Select(f => new FileInfo(f)) + .Where(fi => fi.Name.Contains(".response")) + .OrderByDescending(fi => fi.LastWriteTimeUtc) + .ToArray(); + + if (files.Length == 0) + return null; + + FileInfo? chosen = null; + JsonElement? chosenMetaRoot = null; + + foreach (var fi in files) + { + var metaFile = Path.ChangeExtension(fi.FullName, ".meta.json"); + if (!File.Exists(metaFile)) + continue; + + try + { + var metaJson = File.ReadAllText(metaFile); + var recorded = JsonSerializer.Deserialize(metaJson, HttpLoggingJsonContext.Default.ResponseMeta); + if (recorded == null) + continue; + + var rm = recorded.RequestMethod; + var ru = recorded.RequestUri; + var rh = recorded.RequestHost; + + var normalizedRecorded = ru != null ? NormalizeUriForMatching(ru) : null; + var normalizedCurrent = NormalizeUriForMatching(requestUri); + + var recordedHostToCompare = rh; + if (string.IsNullOrEmpty(recordedHostToCompare) && ru != null) + { + if (Uri.TryCreate(ru, UriKind.Absolute, out var tmp)) + recordedHostToCompare = tmp.Host; + } + + if (!string.IsNullOrEmpty(recordedHostToCompare) && !string.Equals(recordedHostToCompare, requestHost, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + if (string.Equals(rm, requestMethod, StringComparison.OrdinalIgnoreCase) + && string.Equals(normalizedRecorded, normalizedCurrent, StringComparison.Ordinal)) + { + chosen = fi; + chosenMetaRoot = JsonDocument.Parse(metaJson).RootElement.Clone(); + break; + } + } + catch + { + } + } + + if (chosen == null || chosenMetaRoot == null) + return null; + + var metaJsonText = File.ReadAllText(Path.ChangeExtension(chosen.FullName, ".meta.json")); + var recordedMeta = JsonSerializer.Deserialize(metaJsonText, HttpLoggingJsonContext.Default.ResponseMeta); + + var status = recordedMeta?.Status ?? 200; + var reason = recordedMeta?.Reason ?? string.Empty; + + var contentBytes = File.ReadAllBytes(chosen.FullName); + var content = new ByteArrayContent(contentBytes); + + if (recordedMeta?.ContentHeaders is not null) + { + foreach (var kv in recordedMeta.ContentHeaders) + { + try + { + foreach (var v in kv.Value ?? Array.Empty()) + content.Headers.TryAddWithoutValidation(kv.Key, v); + } + catch + { + } + } + } + + var response = new HttpResponseMessage((System.Net.HttpStatusCode)status) + { + ReasonPhrase = reason, + Content = content + }; + + if (recordedMeta?.Headers is not null) + { + foreach (var kv in recordedMeta.Headers) + { + try + { + foreach (var v in kv.Value ?? Array.Empty()) + response.Headers.TryAddWithoutValidation(kv.Key, v); + } + catch + { + } + } + } + + return response; + } + catch + { + return null; + } + } + + private static string NormalizeUriForMatching(string uriString) + { + if (string.IsNullOrWhiteSpace(uriString)) + return uriString ?? string.Empty; + + if (!Uri.TryCreate(uriString, UriKind.Absolute, out var uri)) + return uriString; + + // Match by path + query only — recorded responses are host-agnostic. + var path = uri.AbsolutePath; + var query = uri.Query; + if (string.IsNullOrEmpty(query) || query.Length <= 1) + return path; + + var qs = query.TrimStart('?'); + var parts = qs.Split('&', StringSplitOptions.RemoveEmptyEntries); + var list = new List<(string Name, string Value)>(); + foreach (var p in parts) + { + var idx = p.IndexOf('='); + if (idx >= 0) + { + var n = Uri.UnescapeDataString(p.Substring(0, idx)); + var v = Uri.UnescapeDataString(p.Substring(idx + 1)); + list.Add((n, v)); + } + else + { + var n = Uri.UnescapeDataString(p); + list.Add((n, string.Empty)); + } + } + + var ordered = list.OrderBy(t => t.Name, StringComparer.Ordinal).ThenBy(t => t.Value, StringComparer.Ordinal); + var rebuilt = string.Join("&", ordered.Select(t => + { + if (string.IsNullOrEmpty(t.Value)) + return Uri.EscapeDataString(t.Name); + return Uri.EscapeDataString(t.Name) + "=" + Uri.EscapeDataString(t.Value); + })); + + return path + "?" + rebuilt; + } +} \ No newline at end of file diff --git a/apis/Interfold.Settings.Api/Services/Http/HttpLoggingJsonContext.cs b/apis/Interfold.Settings.Api/Services/Http/HttpLoggingJsonContext.cs new file mode 100644 index 00000000..3a409230 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Http/HttpLoggingJsonContext.cs @@ -0,0 +1,8 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Api.Services.Http; + +[JsonSerializable(typeof(RequestMeta))] +[JsonSerializable(typeof(ResponseMeta))] +internal partial class HttpLoggingJsonContext : JsonSerializerContext; + diff --git a/apis/Interfold.Settings.Api/Services/ImportJobs/ImportJobBackgroundService.cs b/apis/Interfold.Settings.Api/Services/ImportJobs/ImportJobBackgroundService.cs new file mode 100644 index 00000000..3e8b18d9 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/ImportJobs/ImportJobBackgroundService.cs @@ -0,0 +1,214 @@ +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Api.Services.ImportJobs; + +/// Single-consumer worker: drains , transitions +/// import_operations rows, and publishes completion/failure events for the WS pump. +/// On startup, sweeps rows stuck in older than +/// (previous-host crashes) and marks them failed with +/// code host_restart. Per-job try/catch treats escaped exceptions as failed runner +/// outcomes (error_code = "exception") so the loop survives. +public sealed class ImportJobBackgroundService : BackgroundService +{ + // Paired with the LWT mutex — every extra minute here delays new imports for the + // affected system by that much. + private static readonly TimeSpan StaleRunningThreshold = TimeSpan.FromMinutes(10); + + private readonly IImportJobQueue _queue; + private readonly IImportOperationRepository _operations; + private readonly IClusterEventBus _eventBus; + private readonly IReadOnlyDictionary _runners; + private readonly ILogger _logger; + + public ImportJobBackgroundService( + IImportJobQueue queue, + IImportOperationRepository operations, + IClusterEventBus eventBus, + IEnumerable runners, + ILogger logger) + { + _queue = queue; + _operations = operations; + _eventBus = eventBus; + // Duplicate kinds throw here rather than racing at dispatch time. + _runners = runners.ToDictionary(r => r.Kind); + _logger = logger; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("[import-worker] Starting. Registered kinds: {Kinds}", + string.Join(", ", _runners.Keys)); + + await SweepStaleRunningAsync(stoppingToken).ConfigureAwait(false); + + try + { + await foreach (var item in _queue.ReadAllAsync(stoppingToken).ConfigureAwait(false)) + { + await ProcessOneAsync(item, stoppingToken).ConfigureAwait(false); + } + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + // Normal shutdown. Any queued work is rediscovered by the next sweep. + } + + _logger.LogInformation("[import-worker] Stopped."); + } + + private async Task SweepStaleRunningAsync(CancellationToken cancellationToken) + { + try + { + var stale = await _operations.GetStaleRunningAsync(StaleRunningThreshold, cancellationToken).ConfigureAwait(false); + if (stale.Count == 0) + { + return; + } + + _logger.LogWarning("[import-worker] Startup sweep found {Count} stale running operation(s); marking failed.", stale.Count); + + foreach (var row in stale) + { + await _operations.MarkFailedAsync( + row.SystemId, + row.OperationId, + row.Kind, + errorCode: ImportErrorCode.HostRestart, + errorMessage: "Operation was running when the previous host shut down.", + cancellationToken).ConfigureAwait(false); + + // Skip the client event on a legacy unscoped row (the slot still frees). + if (ScopedSystemId.TryParseScoped(row.SystemId, out var scopedSweepId)) + { + await PublishFailureAsync(scopedSweepId, row.Kind, cancellationToken).ConfigureAwait(false); + } + else + { + _logger.LogWarning( + "[import-worker] Skipping sweep failure event for operation {OperationId} — stored system id {SystemId} is not region-scoped.", + row.OperationId, row.SystemId); + } + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // Must not block new work — a stuck sweep would pin the loop forever. + _logger.LogError(ex, "[import-worker] Startup sweep failed; continuing without it."); + } + } + + private async Task ProcessOneAsync(ImportJobItem item, CancellationToken cancellationToken) + { + if (!_runners.TryGetValue(item.Kind, out var runner)) + { + // Guard against a misregistered DI graph so the row terminates instead of + // stranding in "running". + _logger.LogError("[import-worker] No runner registered for kind={Kind} (operation={OperationId}).", + item.Kind, item.OperationId); + await _operations.MarkFailedAsync( + item.SystemId, item.OperationId, item.Kind, + errorCode: ImportErrorCode.NoRunner, + errorMessage: $"No IImportJobRunner is registered for kind '{item.Kind}'.", + cancellationToken).ConfigureAwait(false); + await PublishFailureAsync(item.SystemId, item.Kind, cancellationToken).ConfigureAwait(false); + return; + } + + await _operations.MarkRunningAsync(item.SystemId, item.OperationId, cancellationToken).ConfigureAwait(false); + + try + { + var outcome = await runner.RunAsync(item, cancellationToken).ConfigureAwait(false); + if (outcome.Success) + { + await _operations.MarkSucceededAsync( + item.SystemId, item.OperationId, item.Kind, outcome.AlterCount, cancellationToken) + .ConfigureAwait(false); + + // Pin the legacy settings-profile-updated signal so encryption-status views refresh. + if (item.Kind == ImportOperationKind.SimplyPlural) + { + await _eventBus.PublishAsync( + new SettingsProfileUpdatedEvent(item.SystemId, EmitUsernameUpdated: false), + cancellationToken).ConfigureAwait(false); + } + + await PublishSuccessAsync(item.SystemId, item.Kind, outcome.AlterCount, cancellationToken).ConfigureAwait(false); + _logger.LogInformation("[import-worker] Operation {OperationId} succeeded ({AlterCount} alters, kind={Kind}).", + item.OperationId, outcome.AlterCount, item.Kind); + } + else + { + await _operations.MarkFailedAsync( + item.SystemId, item.OperationId, item.Kind, + outcome.ErrorCode ?? ImportErrorCode.ImportFailed, + outcome.ErrorMessage, + cancellationToken).ConfigureAwait(false); + + await PublishFailureAsync(item.SystemId, item.Kind, cancellationToken).ConfigureAwait(false); + _logger.LogWarning("[import-worker] Operation {OperationId} failed (kind={Kind}, code={ErrorCode}).", + item.OperationId, item.Kind, outcome.ErrorCode); + } + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + // Terminate the row so the client sees a definitive frame; the next sweep won't double-process. + await TryMarkFailedSafelyAsync(item, ImportErrorCode.HostShutdown, "Worker was cancelled mid-job.").ConfigureAwait(false); + throw; + } + catch (Exception ex) + { + _logger.LogError(ex, "[import-worker] Operation {OperationId} threw (kind={Kind}).", item.OperationId, item.Kind); + await TryMarkFailedSafelyAsync(item, ImportErrorCode.Exception, ex.Message).ConfigureAwait(false); + } + } + + private async Task TryMarkFailedSafelyAsync(ImportJobItem item, ImportErrorCode errorCode, string errorMessage) + { + // CancellationToken.None so a host cancel still lands the terminal write and frees the slot. + try + { + await _operations.MarkFailedAsync( + item.SystemId, item.OperationId, item.Kind, errorCode, errorMessage, CancellationToken.None) + .ConfigureAwait(false); + await PublishFailureAsync(item.SystemId, item.Kind, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogError(ex, + "[import-worker] Failed to record terminal failure for operation {OperationId}; slot may remain held until sweep.", + item.OperationId); + } + } + + private ValueTask PublishSuccessAsync(ScopedSystemId systemId, ImportOperationKind kind, int alterCount, CancellationToken cancellationToken) + { + return kind switch + { + ImportOperationKind.SimplyPlural => + _eventBus.PublishAsync(new SimplyPluralImportCompletedEvent(systemId, alterCount), cancellationToken), + ImportOperationKind.PluralKit => + _eventBus.PublishAsync(new PluralKitImportCompletedEvent(systemId, alterCount), cancellationToken), + _ => ValueTask.CompletedTask, + }; + } + + private ValueTask PublishFailureAsync(ScopedSystemId systemId, ImportOperationKind kind, CancellationToken cancellationToken) + { + return kind switch + { + ImportOperationKind.SimplyPlural => + _eventBus.PublishAsync(new SimplyPluralImportFailedEvent(systemId), cancellationToken), + ImportOperationKind.PluralKit => + _eventBus.PublishAsync(new PluralKitImportFailedEvent(systemId), cancellationToken), + _ => ValueTask.CompletedTask, + }; + } +} diff --git a/apis/Interfold.Settings.Api/Services/ImportJobs/PkImportJobRunner.cs b/apis/Interfold.Settings.Api/Services/ImportJobs/PkImportJobRunner.cs new file mode 100644 index 00000000..e0f16818 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/ImportJobs/PkImportJobRunner.cs @@ -0,0 +1,29 @@ +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Settings.Api.Services.ImportJobs; + +/// +/// Stub runner that mirrors the placeholder state of the PluralKit import path. The real +/// importer hasn't landed yet (see ImportPkCommandHandler's legacy comment), but +/// keeping the runner registered means: +/// +/// +/// The async-job machinery (queue, worker, operation row, LWT mutex) is identical for SP and PK — no second wiring pass required when the real importer lands. +/// The pk_import_complete WebSocket frame fires with alter_count = 0 immediately on dispatch, which matches the existing handler's stub behaviour and keeps the contract self-consistent for any client that already listens. +/// +/// +/// When the real importer lands, replace the body with a call to +/// IPluralKitImportService.ImportAsync following the same shape as +/// . +/// +public sealed class PkImportJobRunner : IImportJobRunner +{ + public ImportOperationKind Kind => ImportOperationKind.PluralKit; + + public Task RunAsync(ImportJobItem item, CancellationToken cancellationToken = default) + { + return Task.FromResult(new ImportJobOutcome(Success: false, AlterCount: 0, + ErrorCode: ImportErrorCode.Unimplemented)); + } +} diff --git a/apis/Interfold.Settings.Api/Services/ImportJobs/SpImportJobRunner.cs b/apis/Interfold.Settings.Api/Services/ImportJobs/SpImportJobRunner.cs new file mode 100644 index 00000000..588d2177 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/ImportJobs/SpImportJobRunner.cs @@ -0,0 +1,34 @@ +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Settings.Api.Services.ImportJobs; + +/// +/// Bridges the generic contract to the concrete Simply +/// Plural importer (). One per-process registration +/// (singleton). The background worker resolves this instance when it dequeues an item +/// with . +/// +/// +/// R7: now returns +/// directly, so this runner is a pure pass-through. The +/// terminal-state contract (graceful failures must populate , +/// throws are classified as exception) is enforced by the service, not translated +/// here. +/// +/// +public sealed class SpImportJobRunner : IImportJobRunner +{ + private readonly ISimplyPluralImportService _importService; + + public SpImportJobRunner(ISimplyPluralImportService importService) + { + _importService = importService; + } + + public ImportOperationKind Kind => ImportOperationKind.SimplyPlural; + + public Task RunAsync(ImportJobItem item, CancellationToken cancellationToken = default) + => _importService.ImportAsync(item.SystemId, item.Token, item.RecoveryCode, cancellationToken); +} diff --git a/apis/Interfold.Settings.Api/Services/Secrets/FcmSecretsPostConfigure.cs b/apis/Interfold.Settings.Api/Services/Secrets/FcmSecretsPostConfigure.cs new file mode 100644 index 00000000..429adcf2 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Secrets/FcmSecretsPostConfigure.cs @@ -0,0 +1,37 @@ +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Api.Services.Secrets; + +/// +/// Post-configure step that copies the optional fcm:service_account_json row from the +/// snapshot onto . +/// +/// +/// Mirrors 's shape but has no JSON parsing — +/// the FCM Admin SDK's CredentialFactory.FromJson is the sole consumer of the raw +/// string, so this patcher just forwards the value verbatim. A missing row leaves +/// null; the IFCMService DI factory +/// treats that as "Firebase not wired" and falls back to NullFCMService — there is no +/// fail-fast here (unlike the mandatory auth secrets). +/// +/// +internal sealed class FcmSecretsPostConfigure(ISecretsSnapshot snapshot) + : IPostConfigureOptions +{ + public void PostConfigure(string? name, FcmConfiguration options) + { + if (name != Options.DefaultName) + { + return; + } + + var value = snapshot.Get(SecretsStoreKeys.FcmServiceAccountJson); + if (!string.IsNullOrWhiteSpace(value)) + { + options.ServiceAccountJson = value; + } + } +} diff --git a/apis/Interfold.Settings.Api/Services/Secrets/FirebaseClientSecretsPostConfigure.cs b/apis/Interfold.Settings.Api/Services/Secrets/FirebaseClientSecretsPostConfigure.cs new file mode 100644 index 00000000..41dba55e --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/Secrets/FirebaseClientSecretsPostConfigure.cs @@ -0,0 +1,69 @@ +using System.Text.Json; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Api.Services.Secrets; + +/// Deserialises the three optional Firebase client-init JSON rows onto +/// . Missing → property stays null (endpoint 503s); +/// malformed → boot-time throw so bad seeds surface immediately. +internal sealed class FirebaseClientSecretsPostConfigure(ISecretsSnapshot snapshot) + : IPostConfigureOptions +{ + // snake_case_lower matches the bootstrapper's normalised JSON shape. + private static readonly JsonSerializerOptions FirebaseClientJsonOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + PropertyNameCaseInsensitive = true, + }; + + private static readonly (SecretsStoreKey Key, Action Patch)[] FirebaseClientMappings = + [ + (SecretsStoreKeys.FirebaseClientAndroid, (opts, v) => + opts.Android = ParseOrThrow(SecretsStoreKeys.FirebaseClientAndroid, v)), + (SecretsStoreKeys.FirebaseClientIos, (opts, v) => + opts.Ios = ParseOrThrow(SecretsStoreKeys.FirebaseClientIos, v)), + (SecretsStoreKeys.FirebaseClientWeb, (opts, v) => + opts.Web = ParseOrThrow(SecretsStoreKeys.FirebaseClientWeb, v)), + ]; + + public void PostConfigure(string? name, FirebaseClientConfiguration options) + { + if (name != Options.DefaultName) + { + return; + } + + foreach (var (key, patch) in FirebaseClientMappings) + { + var value = snapshot.Get(key); + if (!string.IsNullOrWhiteSpace(value)) + { + patch(options, value); + } + } + } + + /// Throws on null deserialise / JsonException so a bad seed fails boot. + private static T ParseOrThrow(SecretsStoreKey key, string json) + { + try + { + var parsed = JsonSerializer.Deserialize(json, FirebaseClientJsonOptions); + if (parsed is null) + { + throw new InvalidOperationException( + $"[secrets-post-configure] internal.secrets:{key.Value} parsed to null. The row exists but does not deserialise into a {typeof(T).Name}."); + } + return parsed; + } + catch (JsonException ex) + { + throw new InvalidOperationException( + $"[secrets-post-configure] internal.secrets:{key.Value} is malformed JSON. Re-seed via the bootstrapper or fix the row manually before restarting the API.", + ex); + } + } +} diff --git a/apis/Interfold.Settings.Api/Services/SimplyPlural/SimplyPluralImportService.cs b/apis/Interfold.Settings.Api/Services/SimplyPlural/SimplyPluralImportService.cs new file mode 100644 index 00000000..d5ef6867 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/SimplyPlural/SimplyPluralImportService.cs @@ -0,0 +1,934 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Contracts.Models; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Api.Services.SimplyPlural; + +public sealed class SimplyPluralImportService : ISimplyPluralImportService +{ + private const int MaxJournalContentLength = 30_000; + + private readonly IHttpClientFactory _httpClientFactory; + private readonly IAlterRepository _alterRepository; + private readonly ITagRepository _tagRepository; + private readonly IFrontingRepository _frontingRepository; + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IAccountRepository _accountRepository; + private readonly IPollRepository _pollRepository; + private readonly IJournalRepository _journalRepository; + private readonly IAvatarStorage _avatarStorage; + private readonly IEncryptionStateRepository _encryptionStateRepository; + private readonly IOptionsMonitor _authOptions; + private readonly TimeProvider _timeProvider; + private readonly ILogger _logger; + + public SimplyPluralImportService( + IHttpClientFactory httpClientFactory, + IAlterRepository alterRepository, + ITagRepository tagRepository, + IFrontingRepository frontingRepository, + ISettingsFieldRepository fieldRepository, + IAccountRepository accountRepository, + IPollRepository pollRepository, + IJournalRepository journalRepository, + IAvatarStorage avatarStorage, + ILogger logger, + IEncryptionStateRepository encryptionStateRepository, + IOptionsMonitor authConfig, + TimeProvider timeProvider) + { + _httpClientFactory = httpClientFactory; + _alterRepository = alterRepository; + _tagRepository = tagRepository; + _frontingRepository = frontingRepository; + _fieldRepository = fieldRepository; + _accountRepository = accountRepository; + _pollRepository = pollRepository; + _journalRepository = journalRepository; + _avatarStorage = avatarStorage; + _logger = logger; + _encryptionStateRepository = encryptionStateRepository; + _authOptions = authConfig; + _timeProvider = timeProvider; + } + + public bool? WaitForAvatars { get; set; } + + public async Task ImportAsync( + SystemId systemId, + ImportToken spToken, + RecoveryCode? recoveryKey, + CancellationToken cancellationToken = default) + { + _logger.LogInformation("Starting Simply Plural import for system {SystemId}", systemId); + + // Null = no derived key, matched at the gate below. + EncryptionKeyMaterial? encryptionKey = null; + if (recoveryKey is { } providedRecoveryKey && !string.IsNullOrWhiteSpace(providedRecoveryKey.Value)) + { + var (encryptionValidation, derivedKey) = await ValidateEncryptionKeyAsync(systemId, providedRecoveryKey, cancellationToken); + if (!encryptionValidation.Success) + return encryptionValidation; + + encryptionKey = derivedKey; + } + + using var httpClient = _httpClientFactory.CreateClient(HttpClientNames.SimplyPlural); + // SP uses non-standard "Authorization: {token}" (no scheme prefix). + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", spToken.Value); + httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Interfold/spimport"); + + var systemData = await FetchAsync>(httpClient, SpApiPaths.Me(), cancellationToken); + if (systemData is null) + return new ImportJobOutcome(false, 0, ImportErrorCode.SpImportFailed, "Failed to fetch system data from Simply Plural."); + + var spSystemId = systemData.Id; + var description = systemData.Content.Desc; + + var (fieldMapping, createdFieldIds) = await ImportCustomFieldsAsync(httpClient, spSystemId, systemId, cancellationToken); + + var (alterCount, alterAssociations, avatarDownloads) = + await ImportAltersAsync(httpClient, spSystemId, systemId, fieldMapping, cancellationToken); + + var tagAssociations = await ImportTagsAsync(httpClient, spSystemId, systemId, alterAssociations, cancellationToken); + + await ImportFrontsAsync(httpClient, spSystemId, systemId, alterAssociations, cancellationToken); + + await ImportPollsAsync(httpClient, spSystemId, systemId, alterAssociations, cancellationToken); + + if (encryptionKey is { } derivedEncryptionKey) + { + await ImportNotesAsync(httpClient, spSystemId, systemId, alterAssociations, derivedEncryptionKey, cancellationToken); + } + + if (!string.IsNullOrWhiteSpace(description)) + { + var truncated = description.Length > 3000 ? description[..3000] : description; + await _accountRepository.UpdateDescriptionAsync(systemId, truncated, cancellationToken); + } + + // SP CDN avatars are queued for rehost (SP is shutting down); non-CDN URLs are + // written through with avatar_source=External so they're visible immediately. + var systemAvatarRehost = await ImportSystemAvatarAsync(systemId, systemData.Content, cancellationToken); + if (systemAvatarRehost is not null) + { + avatarDownloads.Add(systemAvatarRehost); + } + + if (WaitForAvatars == true) + { + await DownloadAvatarsAsync(systemId, avatarDownloads, cancellationToken); + } + else + { + _ = Task.Run(async () => await DownloadAvatarsAsync(systemId, avatarDownloads, CancellationToken.None), + CancellationToken.None); + } + + _logger.LogInformation("Simply Plural import complete for system {SystemId}: {AlterCount} alters imported", systemId, alterCount); + return new ImportJobOutcome(true, alterCount); + } + + private async Task<(Dictionary FieldMapping, List CreatedFieldIds)> ImportCustomFieldsAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, CancellationToken ct) + { + var fieldMapping = new Dictionary(); + var createdFieldIds = new List(); + + var customFields = await FetchAsync>>(httpClient, SpApiPaths.CustomFields(spSystemId), ct); + if (customFields is null) + return (fieldMapping, createdFieldIds); + + foreach (var field in customFields) + { + var spFieldId = field.Id; + + // SP custom fields carry no timestamps; only signal is the MongoDB ObjectId's + // first 4 bytes. Non-ObjectId ids skip rather than falling back to UtcNow. + if (!SpObjectId.TryDecodeTimestamp(spFieldId, out var insertedAtUtc)) + { + _logger.LogWarning( + "Skipping SP custom field {SpFieldId} - id is not a 24-hex ObjectId so we cannot derive its created date", + spFieldId); + continue; + } + + var name = field.Content.Name ?? "Unnamed field"; + if (name.Length > 100) name = name[..100]; + + var fieldType = MapFieldType(field.Content.Type, field.Content.SupportMarkdown); + var securityLevel = MapSecurityLevel(field.Content.Private, field.Content.PreventTrusted); + var createdId = await _fieldRepository.CreateAsync(systemId, name, fieldType, securityLevel, false, insertedAtUtc, ct); + if (createdId is not null) + { + fieldMapping[spFieldId] = createdId.Value; + createdFieldIds.Add(createdId.Value); + } + } + + return (fieldMapping, createdFieldIds); + } + + private async Task<(int AlterCount, Dictionary AlterAssociations, List AvatarDownloads)> ImportAltersAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, + Dictionary fieldMapping, CancellationToken ct) + { + var alterAssociations = new Dictionary(); + var avatarDownloads = new List(); + var alterCount = 0; + + var members = await FetchAsync>>(httpClient, SpApiPaths.Members(spSystemId), ct); + var customFronts = await FetchAsync>>(httpClient, SpApiPaths.CustomFronts(spSystemId), ct); + + var allEntries = new List<(SpMemberContent Content, string Uuid, bool IsCustomFront)>(); + + if (members is not null) + { + foreach (var member in members) + allEntries.Add((member.Content, member.Id, false)); + } + + if (customFronts is not null) + { + foreach (var cf in customFronts) + allEntries.Add((cf.Content, cf.Id, true)); + } + + foreach (var (content, uuid, isCustomFront) in allEntries) + { + var name = content.Name ?? "Unnamed alter"; + if (name.Length > 80) name = name[..80]; + if (string.IsNullOrWhiteSpace(name)) name = "Unnamed alter"; + + // Cascade date → ObjectId-encoded timestamp → UtcNow+warn. Never silently + // stamp epoch; alter creation can't be skipped (downstream lookups by uuid). + DateTimeOffset createdAt; + if (content.Date > 0) + { + createdAt = UnixTimestampToDateTimeOffset(content.Date); + } + else if (SpObjectId.TryDecodeTimestamp(uuid, out var decodedAlterAt)) + { + createdAt = new DateTimeOffset(decodedAlterAt, TimeSpan.Zero); + _logger.LogWarning( + "SP alter {SpMemberId} for system {SystemId} has no date; falling back to ObjectId-decoded creation timestamp.", + uuid, systemId); + } + else + { + createdAt = _timeProvider.GetUtcNow(); + _logger.LogWarning( + "SP alter {SpMemberId} for system {SystemId} has no date and non-decodable id; using import time as created date.", + uuid, systemId); + } + + var updatedAt = UnixTimestampToDateTimeOffset(content.LastOperationTime); + + var alterId = await _alterRepository.CreateAsync(systemId, new CreateAlterCommand(name, createdAt), ct); + if (alterId is null) + continue; + + alterAssociations[uuid] = alterId.Value; + alterCount++; + + var pronouns = content.Pronouns; + if (pronouns?.Length > 50) pronouns = pronouns[..50]; + + var desc = content.Desc; + if (desc?.Length > 3000) desc = desc[..3000]; + + var color = ParseColor(content.Color); + + List? fields = null; + if (content.Info is { Count: > 0 }) + { + fields = new List(); + foreach (var (spFieldId, value) in content.Info) + { + if (fieldMapping.TryGetValue(spFieldId, out var ourFieldId) && !string.IsNullOrWhiteSpace(value)) + fields.Add(new AlterFieldCommand(ourFieldId, value)); + } + + if (fields.Count == 0) fields = null; + } + + var securityLevel = MapSecurityLevel(content.Private, content.PreventTrusted); + + // Branches: (avatarUuid+uid) or SP CDN url → rehost; other absolute url → + // passthrough (avatar_source=External); else no avatar. + var avatarUuid = content.AvatarUuid; + var rawAvatarUrl = content.AvatarUrl; + var uid = content.Uid; + string? rehostUrl = null; + string? passthroughUrl = null; + + if (!string.IsNullOrWhiteSpace(avatarUuid) && !string.IsNullOrWhiteSpace(uid)) + { + rehostUrl = SpCdnHosts.Avatar(uid, avatarUuid); + } + else if (!string.IsNullOrWhiteSpace(rawAvatarUrl) && Uri.IsWellFormedUriString(rawAvatarUrl, UriKind.Absolute)) + { + if (IsSpCdnUrl(rawAvatarUrl)) + { + rehostUrl = rawAvatarUrl; + } + else + { + passthroughUrl = rawAvatarUrl; + } + } + + var updateCommand = new UpdateAlterCommand + { + AlterId = alterId.Value, + Description = desc, + AvatarUrl = AvatarUrl.FromNullable(passthroughUrl), + AvatarSource = passthroughUrl is null ? null : AvatarSource.External, + Color = HexColor.FromNullable(color), + Pronouns = pronouns, + SecurityLevel = securityLevel, + Fields = fields, + Untracked = isCustomFront, + Archived = content.Archived, + Pinned = false, + UpdatedAt = updatedAt, + }; + + await _alterRepository.UpdateAsync(systemId, updateCommand, ct); + + if (rehostUrl is not null) + { + avatarDownloads.Add(new AvatarDownload(rehostUrl, systemId, AvatarKind.Alter, alterId.Value)); + } + } + + return (alterCount, alterAssociations, avatarDownloads); + } + + private async Task> ImportTagsAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, + Dictionary alterAssociations, CancellationToken ct) + { + var tagAssociations = new Dictionary(); + + var groups = await FetchAsync>>(httpClient, SpApiPaths.Groups(spSystemId), ct); + if (groups is null) + return tagAssociations; + + // Pass 1: create tags (no parent). Pass 2: parents. Pass 3: alter attachments. + var tagEntries = new List<(string SpId, SpGroupContent Content)>(); + foreach (var group in groups) + { + tagEntries.Add((group.Id, group.Content)); + } + + foreach (var (spId, content) in tagEntries) + { + var name = content.Name ?? "Unnamed tag"; + if (name.Length > 100) name = name[..100]; + if (string.IsNullOrWhiteSpace(name)) name = "Unnamed tag"; + + if (!SpObjectId.TryDecodeTimestamp(spId, out var insertedAtUtc)) + { + _logger.LogWarning( + "Skipping SP group (tag) {SpGroupId} - id is not a 24-hex ObjectId so we cannot derive its created date", + spId); + continue; + } + + var createdTagId = await _tagRepository.CreateAsync(systemId, new CreateTagCommand(name, null, insertedAtUtc), ct); + if (createdTagId is not null) + { + tagAssociations[spId] = createdTagId.Value; + + var tagDesc = content.Desc; + if (tagDesc?.Length > 1000) tagDesc = tagDesc[..1000]; + var tagColor = ParseColor(content.Color); + + var tagSecurityLevel = MapSecurityLevel(content.Private, content.PreventTrusted); + + if (!string.IsNullOrWhiteSpace(tagDesc) || !string.IsNullOrWhiteSpace(tagColor) || tagSecurityLevel != VisibilityLevel.Private) + { + await _tagRepository.UpdateAsync(systemId, new UpdateTagCommand( + TagId: createdTagId.Value, + Name: null, + Color: HexColor.FromNullable(tagColor), + Description: tagDesc, + SecurityLevel: tagSecurityLevel + ), ct); + } + } + } + + foreach (var (spId, content) in tagEntries) + { + if (!tagAssociations.TryGetValue(spId, out var ourTagId)) + continue; + + var parent = content.Parent; + if (string.IsNullOrWhiteSpace(parent) || parent == SpApiPaths.RootGroupParent) + continue; + + if (tagAssociations.TryGetValue(parent, out var parentTagId)) + { + await _tagRepository.SetParentAsync(systemId, ourTagId, parentTagId, ct); + } + } + + foreach (var (spId, content) in tagEntries) + { + if (!tagAssociations.TryGetValue(spId, out var ourTagId)) + continue; + + if (content.Members is null) + continue; + + foreach (var memberUuid in content.Members) + { + if (alterAssociations.TryGetValue(memberUuid, out var alterId)) + { + await _tagRepository.AttachAlterAsync(systemId, ourTagId, alterId, ct); + } + } + } + + return tagAssociations; + } + + private async Task ImportFrontsAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, + Dictionary alterAssociations, CancellationToken ct) + { + // SP epoch: 2015-01-01. Chunk by 6 months to bound each API call. + const long startEpoch = 1_420_070_400_000; + const int monthInterval = 6; + var chunkSizeMs = (long)monthInterval * 30 * 24 * 60 * 60 * 1000; + var endTimeMs = _timeProvider.GetUtcNow().ToUnixTimeMilliseconds(); + var numberOfChunks = (int)Math.Ceiling((double)(endTimeMs - startEpoch) / chunkSizeMs); + + var seenFrontIds = new HashSet(); + + for (var i = 0; i < numberOfChunks; i++) + { + var chunkStart = startEpoch + i * chunkSizeMs; + var chunkEnd = Math.Min(startEpoch + (i + 1) * chunkSizeMs, endTimeMs); + + var fronts = await FetchAsync>>(httpClient, + SpApiPaths.FrontHistory(spSystemId, chunkStart, chunkEnd), ct); + + if (fronts is not null) + { + foreach (var front in fronts) + { + if (!seenFrontIds.Add(front.Id)) + continue; + + var memberId = front.Content.Member; + if (memberId is null || !alterAssociations.TryGetValue(memberId, out var alterId)) + continue; + + if (front.Content.StartTime <= 0 || front.Content.EndTime <= 0) + continue; + + var comment = front.Content.CustomStatus; + if (comment?.Length > 50) comment = comment[..50]; + + var spStart = DateTimeOffset.FromUnixTimeMilliseconds(front.Content.StartTime); + var spEnd = DateTimeOffset.FromUnixTimeMilliseconds(front.Content.EndTime); + + await _frontingRepository.StartAsync(systemId, alterId, comment, spStart, ct); + await _frontingRepository.EndAsync(systemId, alterId, spEnd, ct); + } + } + + await Task.Delay(200, ct); + } + + var currentFronters = await FetchAsync>>(httpClient, SpApiPaths.CurrentFronters(), ct); + if (currentFronters is not null) + { + foreach (var fronter in currentFronters) + { + var memberId = fronter.Content.Member; + if (memberId is null || !alterAssociations.TryGetValue(memberId, out var alterId)) + continue; + + var comment = fronter.Content.CustomStatus; + if (comment?.Length > 50) comment = comment[..50]; + + // Prefer startTime, else ObjectId-encoded creation second. Never fall back + // to UtcNow (the original "today date" bug). + DateTimeOffset spStart; + if (fronter.Content.StartTime > 0) + { + spStart = DateTimeOffset.FromUnixTimeMilliseconds(fronter.Content.StartTime); + } + else if (SpObjectId.TryDecodeTimestamp(fronter.Id, out var decodedFrontAt)) + { + spStart = new DateTimeOffset(decodedFrontAt, TimeSpan.Zero); + } + else + { + _logger.LogWarning( + "Skipping live SP fronter for system {SystemId} member {MemberId}: no startTime and SP front id is not a 24-hex ObjectId.", + systemId, memberId); + continue; + } + + await _frontingRepository.StartAsync(systemId, alterId, comment, spStart, ct); + } + } + } + + private async Task ImportPollsAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, + Dictionary alterAssociations, CancellationToken ct) + { + var polls = await FetchAsync>>(httpClient, SpApiPaths.Polls(spSystemId), ct); + if (polls is null) + return; + + foreach (var poll in polls) + { + // SP marks options optional even for custom polls; skip so we don't create + // zero-choice rows that can't be voted on. + if (poll.Content.Custom && (poll.Content.Options is null || poll.Content.Options.Count == 0)) + { + _logger.LogWarning( + "Skipping custom SP poll {PollId} for system {SystemId}: custom poll has no options.", + poll.Id, systemId); + continue; + } + + // Match CreatePollCommandHandler caps (title <= 100, desc <= 2000). + var title = poll.Content.Name ?? "Unnamed poll"; + if (title.Length > 100) title = title[..100]; + if (string.IsNullOrWhiteSpace(title)) title = "Unnamed poll"; + + var desc = poll.Content.Desc; + if (desc?.Length > 2000) desc = desc[..2000]; + + var type = poll.Content.Custom ? PollType.Choice : PollType.Vote; + + DateTime? timeEnd = poll.Content.EndTime > 0 + ? DateTimeOffset.FromUnixTimeMilliseconds(poll.Content.EndTime).UtcDateTime + : null; + + // Prefer ObjectId (created-second); lastOperationTime drifts on every edit. + DateTime insertedAt; + if (SpObjectId.TryDecodeTimestamp(poll.Id, out var decodedPollAt)) + { + insertedAt = decodedPollAt; + } + else if (poll.Content.LastOperationTime > 0) + { + insertedAt = DateTimeOffset.FromUnixTimeMilliseconds(poll.Content.LastOperationTime).UtcDateTime; + _logger.LogWarning( + "SP poll {PollId} for system {SystemId} has non-decodable 24-hex id; falling back to lastOperationTime for inserted_at.", + poll.Id, systemId); + } + else + { + insertedAt = _timeProvider.GetUtcNow().UtcDateTime; + _logger.LogWarning( + "SP poll {PollId} for system {SystemId} has non-decodable id and no lastOperationTime; using import time as inserted_at.", + poll.Id, systemId); + } + + var pollId = await _pollRepository.CreateAsync(systemId, new CreatePollCommand(title, desc, type, timeEnd, insertedAt), ct); + if (pollId is null) + continue; + + var (data, skippedVotes) = BuildPollData(poll.Content, alterAssociations); + if (skippedVotes > 0) + { + _logger.LogWarning( + "Dropped {Count} unmappable votes from SP poll {PollId} in system {SystemId} during import.", + skippedVotes, poll.Id, systemId); + } + + if (data.ValueKind != JsonValueKind.Undefined) + { + await _pollRepository.UpdateAsync(systemId, new UpdatePollCommand( + Id: pollId.Value, + Title: null, + Description: null, + TimeEnd: null, + HasTimeEnd: false, + Data: data + ), ct); + } + } + } + + private async Task ImportNotesAsync( + HttpClient httpClient, string spSystemId, SystemId systemId, + Dictionary alterAssociations, EncryptionKeyMaterial encryptionKey, CancellationToken ct) + { + foreach (var (spMemberId, alterId) in alterAssociations) + { + var notes = await FetchAsync>>(httpClient, SpApiPaths.Notes(spSystemId, spMemberId), ct); + if (notes is null || notes.Count == 0) + continue; + + foreach (var note in notes) + { + var title = note.Content.Title ?? "Imported note"; + if (title.Length > 100) title = title[..100]; + if (string.IsNullOrWhiteSpace(title)) title = "Imported note"; + + // Cascade Date → ObjectId → UtcNow+warn (same silent-1970 trap as alters). + DateTimeOffset createdAt; + if (note.Content.Date > 0) + { + createdAt = UnixTimestampToDateTimeOffset(note.Content.Date); + } + else if (SpObjectId.TryDecodeTimestamp(note.Id, out var decodedNoteAt)) + { + createdAt = new DateTimeOffset(decodedNoteAt, TimeSpan.Zero); + _logger.LogWarning( + "SP note {SpNoteId} for system {SystemId} alter {AlterId} has no date; falling back to ObjectId-decoded creation timestamp.", + note.Id, systemId, alterId); + } + else + { + createdAt = _timeProvider.GetUtcNow(); + _logger.LogWarning( + "SP note {SpNoteId} for system {SystemId} alter {AlterId} has no date and non-decodable id; using import time as created date.", + note.Id, systemId, alterId); + } + + var updatedAt = UnixTimestampToDateTimeOffset(note.Content.LastOperationTime); + + var entryId = await _journalRepository.CreateAlterAsync(systemId, new CreateAlterJournalEntryCommand( + AlterId: alterId, + Title: title, + CreatedAt: createdAt + ), ct); + + if (entryId is null) + continue; + + var content = PrepareNote(note.Content.Note, encryptionKey, out var successful); + var color = ParseColor(note.Content.Color); + + if (!successful && color is null) + continue; + + await _journalRepository.UpdateAlterAsync(systemId, new UpdateAlterJournalEntryCommand( + EntryId: entryId.Value, + Title: null, + Content: content, + Color: HexColor.FromNullable(color), + UpdatedAt: updatedAt + ), ct); + } + } + } + + private static string? PrepareNote(string? content, EncryptionKeyMaterial encryptionKey, out bool successful) + { + if (string.IsNullOrEmpty(content)) + { + successful = true; + return content; + } + + content = content.Length > MaxJournalContentLength + ? content[..MaxJournalContentLength] + : content; + + var encrypted = TryEncryptForClient(content, encryptionKey); + if (encrypted is null) + { + successful = false; + return null; + } + + successful = true; + return encrypted; + } + + // Mirrors client encryptData: AES-256-GCM with random IV; ciphertext + tag stored separately. + private static string? TryEncryptForClient(string plaintext, EncryptionKeyMaterial base64Key) + { + try + { + var key = Convert.FromBase64String(base64Key.Value); + if (key.Length != 32) + return null; + + var iv = new byte[12]; + RandomNumberGenerator.Fill(iv); + + var plainBytes = Encoding.UTF8.GetBytes(plaintext); + var ciphertext = new byte[plainBytes.Length]; + var tag = new byte[16]; + + using var aes = new AesGcm(key, tag.Length); + aes.Encrypt(iv, plainBytes, ciphertext, tag); + + return $"enc|{Convert.ToBase64String(iv)}|{Convert.ToBase64String(ciphertext)}|{Convert.ToBase64String(tag)}"; + } + catch + { + return null; + } + } + + private async Task<(ImportJobOutcome Result, EncryptionKeyMaterial? DerivedKey)> ValidateEncryptionKeyAsync(SystemId systemId, RecoveryCode recoveryCode, CancellationToken ct) + { + var state = await _encryptionStateRepository.GetAsync(systemId, ct); + if (state is not { Initialized: true, KeyChecksum: { } keyChecksum, Salt: { } salt } + || string.IsNullOrWhiteSpace(keyChecksum.Value)) + return (new ImportJobOutcome(false, 0, ImportErrorCode.SpImportFailed, "Encryption is not initialized for this system."), null); + + var pepper = _authOptions.CurrentValue.EncryptionPepper; + var key = EncryptionKey.DeriveKey(pepper, systemId, recoveryCode, salt); + var checksum = EncryptionKey.DeriveChecksum(key); + if (checksum != keyChecksum) + return (new ImportJobOutcome(false, 0, ImportErrorCode.SpImportFailed, "The provided encryption key is invalid."), null); + + return (new ImportJobOutcome(true, 0), key); + } + + /// Builds the client-schema poll data blob (see ) + /// and returns the count of unmappable votes (unknown voter, blank id, non-standard + /// value, or custom vote matching no option) so the caller can log one warning per + /// poll rather than one per dropped vote. + private static (JsonElement Data, int SkippedUnmappableVotes) BuildPollData( + SpPollContent poll, Dictionary alterAssociations) + { + var skippedUnmappableVotes = 0; + var responses = new List(); + + if (poll.Custom) + { + // SP custom-poll votes carry option name; mint choice ids and translate name→id. + var choices = new List(); + var choiceIdByName = new Dictionary(StringComparer.Ordinal); + foreach (var option in poll.Options ?? []) + { + var name = option.Name ?? ""; + var choiceId = new PollChoiceId(Guid.NewGuid().ToString()); + choices.Add(new PollDataChoice(choiceId, name)); + choiceIdByName.TryAdd(name, choiceId); + } + + foreach (var vote in poll.Votes ?? []) + { + if (string.IsNullOrWhiteSpace(vote.Id) || string.IsNullOrWhiteSpace(vote.Vote) + || !alterAssociations.TryGetValue(vote.Id, out var alterId) + || !choiceIdByName.TryGetValue(vote.Vote, out var choiceId)) + { + skippedUnmappableVotes++; + continue; + } + + responses.Add(new PollDataResponse(alterId, Vote: null, ChoiceId: choiceId, Comment: vote.Comment)); + } + + return (PollDataJson.ToJsonElement(new ChoicePollData(choices, responses)), skippedUnmappableVotes); + } + + foreach (var vote in poll.Votes ?? []) + { + // Drop votes with no voter, no opinion, or a non-standard vote value. + if (string.IsNullOrWhiteSpace(vote.Id) + || PollDataJson.TryParseVoteValue(vote.Vote) is not { } voteValue + || !alterAssociations.TryGetValue(vote.Id, out var alterId)) + { + skippedUnmappableVotes++; + continue; + } + + responses.Add(new PollDataResponse(alterId, Vote: voteValue, ChoiceId: null, Comment: vote.Comment)); + } + + return (PollDataJson.ToJsonElement(new VotePollData(responses, poll.AllowVeto)), skippedUnmappableVotes); + } + + private async Task DownloadAvatarsAsync(SystemId systemId, List downloads, CancellationToken cancellationToken) + { + using var httpClient = _httpClientFactory.CreateClient(HttpClientNames.SimplyPlural); + + foreach (var download in downloads) + { + try + { + using var response = await httpClient.GetAsync(download.Url, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + if (!response.IsSuccessStatusCode) + continue; + + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + + if (download.Kind == AvatarKind.System) + { + var localUrl = await _avatarStorage.SaveSystemAvatarAsync(download.SystemId, stream, cancellationToken); + await _accountRepository.UpdateAvatarAsync(download.SystemId, localUrl, AvatarSource.Local, cancellationToken); + } + else + { + var localUrl = await _avatarStorage.SaveAlterAvatarAsync(download.SystemId, download.AlterId!.Value, stream, cancellationToken); + + await _alterRepository.UpdateAsync(systemId, new UpdateAlterCommand + { + AlterId = download.AlterId!.Value, + AvatarUrl = localUrl, + AvatarSource = AvatarSource.Local, + UpdatedAt = _timeProvider.GetUtcNow(), + }, cancellationToken); + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to download avatar (kind={Kind}, alter={AlterId}) in system {SystemId}", + download.Kind, download.AlterId, download.SystemId); + } + } + } + + /// Returns a queued rehost download for SP CDN URLs, else writes a passthrough + /// via . Null when no rehost needed. + private async Task ImportSystemAvatarAsync( + SystemId systemId, + SpSystemContent content, + CancellationToken cancellationToken) + { + var avatarUuid = content.AvatarUuid; + var rawAvatarUrl = content.AvatarUrl; + var uid = content.Uid; + + if (!string.IsNullOrWhiteSpace(avatarUuid) && !string.IsNullOrWhiteSpace(uid)) + { + return new AvatarDownload(SpCdnHosts.Avatar(uid, avatarUuid), systemId, AvatarKind.System, null); + } + + if (string.IsNullOrWhiteSpace(rawAvatarUrl) || !Uri.IsWellFormedUriString(rawAvatarUrl, UriKind.Absolute)) + { + return null; + } + + if (IsSpCdnUrl(rawAvatarUrl)) + { + return new AvatarDownload(rawAvatarUrl, systemId, AvatarKind.System, null); + } + + await _accountRepository.UpdateAvatarAsync(systemId, new(rawAvatarUrl), AvatarSource.External, cancellationToken); + return null; + } + + internal static bool IsSpCdnUrl(string? url) + { + if (string.IsNullOrWhiteSpace(url)) + { + return false; + } + + foreach (var host in SpCdnHosts.All) + { + if (url.StartsWith(host, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + + private static async Task FetchAsync(HttpClient httpClient, string url, CancellationToken ct) + { + try + { + using var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false); + if (!response.IsSuccessStatusCode) + return default; + + await using var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false); + + // Prefer source-generated JsonTypeInfo (trim-safe); fall back to reflection. + var typeInfoObj = Services.SimplyPlural.SpJsonContext.Default.GetTypeInfo(typeof(T)); + if (typeInfoObj is System.Text.Json.Serialization.Metadata.JsonTypeInfo typedInfo) + { + var result = await JsonSerializer.DeserializeAsync(stream, typedInfo, ct).ConfigureAwait(false); + return result; + } + + var fallback = await JsonSerializer.DeserializeAsync(stream, cancellationToken: ct).ConfigureAwait(false); + return fallback; + } + catch + { + return default; + } + } + + // SP's update300 migration emits null supportMarkdown for legacy fields; SP defaults + // to true, so null/true → Text (markdown), false → Plaintext. + private static FieldType MapFieldType(SpFieldType spType, bool? supportMarkdown) => spType switch + { + SpFieldType.Text => (supportMarkdown ?? true) ? FieldType.Text : FieldType.Plaintext, + SpFieldType.Colour => FieldType.Colour, + SpFieldType.Date => FieldType.Date, + SpFieldType.Month => FieldType.Month, + SpFieldType.Year => FieldType.Year, + SpFieldType.MonthYear => FieldType.MonthYear, + SpFieldType.Timestamp => FieldType.Timestamp, + SpFieldType.MonthDay => FieldType.MonthDay, + _ => throw new ArgumentOutOfRangeException(nameof(spType), spType, "Unknown SP field type") + }; + + private static VisibilityLevel MapSecurityLevel(bool isPrivate, bool preventTrusted) => (isPrivate, preventTrusted) switch + { + (false, _) => VisibilityLevel.Public, + (true, false) => VisibilityLevel.TrustedOnly, + (true, true) => VisibilityLevel.Private, + }; + + private static DateTimeOffset UnixTimestampToDateTimeOffset(long unixMilliseconds) + { + var epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + var dateTime = epochTime.AddMilliseconds(unixMilliseconds); + return new DateTimeOffset(dateTime, TimeSpan.Zero); + } + + /// Canonicalise SP's mixed bare/hash hex forms via + /// so never sees + /// a non-well-formed value. + internal static string? ParseColor(string? color) => HexColor.Normalise(color); + + /// Pending rehost of an SP CDN avatar. required for + /// , unused for . + private sealed record AvatarDownload(string Url, SystemId SystemId, AvatarKind Kind, AlterId? AlterId); + + private enum AvatarKind + { + System, + Alter, + } +} diff --git a/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiModels.cs b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiModels.cs new file mode 100644 index 00000000..3295a041 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiModels.cs @@ -0,0 +1,245 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Api.Services.SimplyPlural; + +// --- Wrapper: every SP API entity comes as { "id": "...", "content": { ... } } --- + +internal sealed class SpEntity +{ + [JsonPropertyName("id")] + public string Id { get; set; } = string.Empty; + + [JsonPropertyName("content")] + public TContent Content { get; set; } = default!; +} + +// --- /me --- + +internal sealed class SpSystemContent +{ + [JsonPropertyName("desc")] + public string? Desc { get; set; } + + [JsonPropertyName("avatarUuid")] + public string? AvatarUuid { get; set; } + + [JsonPropertyName("avatarUrl")] + public string? AvatarUrl { get; set; } + + [JsonPropertyName("uid")] + public string? Uid { get; set; } +} + +// --- /customFields/{id} --- + +/// +/// Simply Plural's numeric custom-field type codes (their v1 API `type` member). +/// Deserialized straight from the wire integers 0–7; unknown future codes land as +/// undefined enum values and are rejected by MapFieldType's exhaustive switch. +/// +internal enum SpFieldType +{ + Text = 0, + Colour = 1, + Date = 2, + Month = 3, + Year = 4, + MonthYear = 5, + Timestamp = 6, + MonthDay = 7, +} + +internal sealed class SpCustomFieldContent +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("type")] + public SpFieldType Type { get; set; } + + // SP's `update300.ts` migration copies legacy field schemas into the customFields + // collection via `insertOne({ ..., supportMarkdown: field.supportMarkdown, ... })` + // where the legacy `field.supportMarkdown` is undefined for pre-markdown rows. + // Mongo stores undefined as `null`, the SP API serialises it as `"supportMarkdown":null` + [JsonPropertyName("supportMarkdown")] + public bool? SupportMarkdown { get; set; } + + [JsonPropertyName("private")] + public bool Private { get; set; } + + [JsonPropertyName("preventTrusted")] + public bool PreventTrusted { get; set; } +} + +// --- /members/{id} and /customFronts/{id} --- + +internal sealed class SpMemberContent +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("pronouns")] + public string? Pronouns { get; set; } + + [JsonPropertyName("desc")] + public string? Desc { get; set; } + + [JsonPropertyName("color")] + public string? Color { get; set; } + + [JsonPropertyName("avatarUuid")] + public string? AvatarUuid { get; set; } + + [JsonPropertyName("avatarUrl")] + public string? AvatarUrl { get; set; } + + [JsonPropertyName("uid")] + public string? Uid { get; set; } + + [JsonPropertyName("date")] + public long Date { get; set; } + + [JsonPropertyName("lastOperationTime")] + public long LastOperationTime { get; set; } + + [JsonPropertyName("info")] + public Dictionary? Info { get; set; } + + [JsonPropertyName("archived")] + public bool Archived { get; set; } + + [JsonPropertyName("private")] + public bool Private { get; set; } = true; + + [JsonPropertyName("preventTrusted")] + public bool PreventTrusted { get; set; } = true; +} + +// --- /groups/{id} --- + +internal sealed class SpGroupContent +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("desc")] + public string? Desc { get; set; } + + [JsonPropertyName("color")] + public string? Color { get; set; } + + [JsonPropertyName("parent")] + public string? Parent { get; set; } + + [JsonPropertyName("members")] + public List? Members { get; set; } + + [JsonPropertyName("private")] + public bool Private { get; set; } = true; + + [JsonPropertyName("preventTrusted")] + public bool PreventTrusted { get; set; } = true; +} + +// --- /frontHistory/{id} and /fronters/ --- + +internal sealed class SpFrontContent +{ + [JsonPropertyName("member")] + public string? Member { get; set; } + + [JsonPropertyName("startTime")] + public long StartTime { get; set; } + + [JsonPropertyName("endTime")] + public long EndTime { get; set; } + + [JsonPropertyName("customStatus")] + public string? CustomStatus { get; set; } + + [JsonPropertyName("lastOperationTime")] + public long LastOperationTime { get; set; } +} + +// --- /polls/{id} --- + +internal sealed class SpPollContent +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("desc")] + public string? Desc { get; set; } + + [JsonPropertyName("custom")] + public bool Custom { get; set; } + + [JsonPropertyName("endTime")] + public long EndTime { get; set; } + + [JsonPropertyName("allowAbstain")] + public bool AllowAbstain { get; set; } + + [JsonPropertyName("allowVeto")] + public bool AllowVeto { get; set; } + + [JsonPropertyName("options")] + public List? Options { get; set; } + + [JsonPropertyName("votes")] + public List? Votes { get; set; } + + [JsonPropertyName("lastOperationTime")] + public long LastOperationTime { get; set; } +} + +internal sealed class SpPollOption +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("color")] + public string? Color { get; set; } +} + +internal sealed class SpPollVote +{ + [JsonPropertyName("id")] + public string? Id { get; set; } + + [JsonPropertyName("vote")] + public string? Vote { get; set; } + + [JsonPropertyName("comment")] + public string? Comment { get; set; } +} + +// --- /notes/{systemId}/{memberId} --- + +internal sealed class SpNoteContent +{ + [JsonPropertyName("title")] + public string? Title { get; set; } + + [JsonPropertyName("note")] + public string? Note { get; set; } + + [JsonPropertyName("color")] + public string? Color { get; set; } + + [JsonPropertyName("date")] + public long Date { get; set; } + + [JsonPropertyName("member")] + public string? Member { get; set; } + + // Defensively nullable: legacy notes from pre-supportMarkdown SP versions can serialise + // as `null` for the same reason custom fields do (Mongo stores `undefined` -> SP API + // emits `null`). We don't read this field today, but a non-nullable `bool` would make + // the whole notes deserialisation throw and silently empty the per-alter notes page. + [JsonPropertyName("supportMarkdown")] + public bool? SupportMarkdown { get; set; } + + [JsonPropertyName("lastOperationTime")] + public long LastOperationTime { get; set; } +} \ No newline at end of file diff --git a/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiPaths.cs b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiPaths.cs new file mode 100644 index 00000000..904ad22f --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpApiPaths.cs @@ -0,0 +1,38 @@ +namespace Interfold.Settings.Api.Services.SimplyPlural; + +/// Simply Plural v1 API paths + CDN hosts + wire sentinels the importer matches +/// on. All third-party wire contracts (frozen by SP). +internal static class SpApiPaths +{ + public const string ApiBase = "https://api.apparyllis.com/v1"; + + public static string Me() => $"{ApiBase}/me"; + public static string CustomFields(string spSystemId) => $"{ApiBase}/customFields/{spSystemId}"; + public static string Members(string spSystemId) => $"{ApiBase}/members/{spSystemId}"; + public static string CustomFronts(string spSystemId) => $"{ApiBase}/customFronts/{spSystemId}"; + public static string Groups(string spSystemId) => $"{ApiBase}/groups/{spSystemId}"; + public static string FrontHistory(string spSystemId, long startTimeMs, long endTimeMs) + => $"{ApiBase}/frontHistory/{spSystemId}?startTime={startTimeMs}&endTime={endTimeMs}"; + public static string CurrentFronters() => $"{ApiBase}/fronters/"; + public static string Polls(string spSystemId) => $"{ApiBase}/polls/{spSystemId}"; + public static string Notes(string spSystemId, string spMemberId) => $"{ApiBase}/notes/{spSystemId}/{spMemberId}"; + + /// SP's Mongo convention for "no parent" on tag/group hierarchies. + public const string RootGroupParent = "root"; +} + +/// SP-owned hosts fronting the same avatars/{uid}/{uuid} bucket. All three +/// CNAME the same DigitalOcean Spaces bucket — new entries must be SP-owned too. +internal static class SpCdnHosts +{ + public const string Canonical = "https://spaces.apparyllis.com"; + + public static readonly string[] All = + { + Canonical, + "https://serve.apparyllis.com", + "https://simply-plural.sfo3.digitaloceanspaces.com", + }; + + public static string Avatar(string uid, string avatarUuid) => $"{Canonical}/avatars/{uid}/{avatarUuid}"; +} diff --git a/apis/Interfold.Settings.Api/Services/SimplyPlural/SpJsonContext.cs b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpJsonContext.cs new file mode 100644 index 00000000..03bf6009 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpJsonContext.cs @@ -0,0 +1,19 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Api.Services.SimplyPlural; + +[JsonSerializable(typeof(SpEntity))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(List>))] +[JsonSerializable(typeof(SpPollContent))] +[JsonSerializable(typeof(SpNoteContent))] +[JsonSerializable(typeof(SpEntity))] +[JsonSerializable(typeof(SpEntity))] +internal partial class SpJsonContext : JsonSerializerContext +{ +} + diff --git a/apis/Interfold.Settings.Api/Services/SimplyPlural/SpObjectId.cs b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpObjectId.cs new file mode 100644 index 00000000..3119c034 --- /dev/null +++ b/apis/Interfold.Settings.Api/Services/SimplyPlural/SpObjectId.cs @@ -0,0 +1,41 @@ +using System.Globalization; + +namespace Interfold.Settings.Api.Services.SimplyPlural; + +internal static class SpObjectId +{ + public static bool TryDecodeTimestamp(string? id, out DateTime utc) + { + utc = default; + + if (string.IsNullOrEmpty(id) || id.Length != 24) + { + return false; + } + + // First 4 bytes (8 hex chars) are a big-endian Unix-seconds timestamp. + // We also require the remaining 16 hex chars to look like hex so we don't + // happily accept "12345678" + 16 chars of garbage. + var timestampHex = id.AsSpan(0, 8); + if (!uint.TryParse(timestampHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var seconds)) + { + return false; + } + + for (var i = 8; i < 24; i++) + { + if (!IsHex(id[i])) + { + return false; + } + } + + utc = DateTimeOffset.FromUnixTimeSeconds(seconds).UtcDateTime; + return true; + } + + private static bool IsHex(char c) => + c is >= '0' and <= '9' + or >= 'a' and <= 'f' + or >= 'A' and <= 'F'; +} diff --git a/apis/Interfold.Settings.Api/SimplyPlural/SimplyPluralImportExtensions.cs b/apis/Interfold.Settings.Api/SimplyPlural/SimplyPluralImportExtensions.cs new file mode 100644 index 00000000..c323e846 --- /dev/null +++ b/apis/Interfold.Settings.Api/SimplyPlural/SimplyPluralImportExtensions.cs @@ -0,0 +1,41 @@ +using Interfold.Settings.Api.Services.Http; +using Interfold.Settings.Api.Services.ImportJobs; +using Interfold.Settings.Api.Services.SimplyPlural; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Shared.Contracts; + +namespace Interfold.Settings.Api.SimplyPlural; + +public static class SimplyPluralImportExtensions +{ + /// + /// Registers only — the pure import service + /// with no HttpClient wiring and no async job-runner. Every caller that drives an + /// import synchronously (SPDump, in-process SP import integration tests) should use + /// this instead of so they can plug their own + /// primary handler (stub, header-injecting proxy, etc.) and + /// skip the queue consumer. + /// + public static IServiceCollection AddSimplyPluralImportCore(this IServiceCollection services) + { + services.AddSingleton(); + return services; + } + + /// + /// Full API-server wiring: the core service (via ), + /// a named for with + /// the shared , and the + /// that dequeues async SP import jobs. Only Program.cs should call this; scaffolds and + /// tests should prefer and register their own + /// HttpClient shape. + /// + public static IServiceCollection AddSimplyPluralImport(this IServiceCollection services) + { + services.AddHttpClient(HttpClientNames.SimplyPlural).AddHttpMessageHandler(); + services.AddSimplyPluralImportCore(); + services.AddSingleton(); + return services; + } +} diff --git a/apis/Interfold.Shared.Api/Controllers/Base/InterfoldControllerBase.cs b/apis/Interfold.Shared.Api/Controllers/Base/InterfoldControllerBase.cs new file mode 100644 index 00000000..f7b1f257 --- /dev/null +++ b/apis/Interfold.Shared.Api/Controllers/Base/InterfoldControllerBase.cs @@ -0,0 +1,441 @@ +using System.Diagnostics; +using System.Net; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Api.Middleware; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Api.Uploads; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.Net.Http.Headers; + +namespace Interfold.Shared.Api.Controllers.Base; + +[ApiController] +[Authorize] +public abstract class InterfoldControllerBase : ControllerBase +{ + [FromServices] + public TimeProvider TimeProvider { get; init; } = TimeProvider.System; + + /// Authenticated for the current request; + /// middleware guarantees presence. + protected ScopedSystemId PrincipalId + { + get + { + if (HttpContext.Items.TryGetValue(InterfoldPrincipalMiddleware.PrincipalIdItemKey, out var value) + && value is ScopedSystemId principal) + { + return principal; + } + + throw new InvalidOperationException( + "PrincipalId is unavailable. Ensure InterfoldPrincipalMiddleware is configured."); + } + } + + + /// Header X-Interfold-Idempotency-Key if present, else a fresh GUID + /// (unkeyed requests are each their own operation). + protected IdempotencyKey GetIdempotencyKey() + { + var header = Request.Headers[InterfoldHeaders.IdempotencyKey].FirstOrDefault(); + return new(!string.IsNullOrWhiteSpace(header) ? header : Guid.NewGuid().ToString("N")); + } + + /// 400 when is the + /// current principal, else null. Uses + /// so raw and same-region-scoped shapes both self-reject with the per-op + /// Cannot*Self code rather than a downstream generic error. + protected ErrorResponse? RejectIfSelf(SystemId target, string message, ErrorCode code) + => PrincipalId.RepresentsSameUserAs(target) + ? new ErrorResponse(message, code, HttpStatusCode.BadRequest) + : null; + + /// Prepends the server origin to URLs only; + /// external URLs pass through. + protected AvatarUrl? QualifyAvatar(IAvatarBearing? bearing) + => AvatarUrlQualifier.QualifyAvatar(bearing, Request.Scheme, Request.Host); + + // QualifyFriendship / QualifyFriendRequest protected wrappers moved to + // apis/Interfold.Friendships.Api/Helpers/FriendshipAvatarQualifier.cs during the Phase-3 + // Friendships slice; the two Interfold.Api call sites (PublicSystemsController, + // FriendshipSocketEventHandlers) inline the same body against AvatarUrlQualifier.QualifyAvatar + // so this base class no longer binds the friendship read-model cluster. + + /// Returns the first file part from a multipart/form-data body. Non-multipart + /// bodies, parse failures, and mid-read IOException yield a null stream; a zero-byte + /// file flips EmptyFilePart so callers can distinguish "empty file" from + /// "no file" (same wire shape, distinct error codes). + protected async Task ResolveMultipartUploadAsync(CancellationToken ct) + { + var emptyFilePart = false; + + if (Request.Body is null) + return new AvatarUploadPayload(null, emptyFilePart); + + Request.EnableBuffering(); + + if (Request.Body.CanSeek) + Request.Body.Position = 0; + + if (!MediaTypeHeaderValue.TryParse(Request.ContentType, out var mediaType) + || !mediaType.MediaType.HasValue + || !mediaType.MediaType.Value.StartsWith("multipart/", StringComparison.OrdinalIgnoreCase)) + { + return new AvatarUploadPayload(null, emptyFilePart); + } + + var boundary = HeaderUtilities.RemoveQuotes(mediaType.Boundary).Value; + if (string.IsNullOrWhiteSpace(boundary)) + return new AvatarUploadPayload(null, emptyFilePart); + + try + { + var reader = new MultipartReader(boundary, Request.Body); + MultipartSection? section; + + while ((section = await reader.ReadNextSectionAsync(ct)) is not null) + { + if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var disposition)) + continue; + + var fileName = HeaderUtilities.RemoveQuotes(disposition.FileNameStar).Value + ?? HeaderUtilities.RemoveQuotes(disposition.FileName).Value; + + if (!string.IsNullOrWhiteSpace(fileName)) + { + var payload = new MemoryStream(); + await section.Body.CopyToAsync(payload, ct); + if (payload.Length <= 0) + { + emptyFilePart = true; + await payload.DisposeAsync(); + continue; + } + + payload.Position = 0; + return new AvatarUploadPayload(payload, emptyFilePart); + } + } + } + catch (IOException) + { + return new AvatarUploadPayload(null, emptyFilePart); + } + + return new AvatarUploadPayload(null, emptyFilePart); + } + + protected async Task HandleAvatarUploadAsync( + Func> getExistingAvatarAsync, + Func> saveToStorageAsync, + Func> updateMetadataAsync, + IAvatarStorage avatarStorage, + CancellationToken ct) + { + var principal = PrincipalId; + var upload = await ResolveMultipartUploadAsync(ct); + var avatarStream = upload.Stream; + if (avatarStream is null) + { + if (upload.EmptyFilePart) + return new ErrorResponse("Avatar file is empty.", ErrorCodes.AvatarFileEmpty, HttpStatusCode.BadRequest); + + return new ErrorResponse("No avatar file provided.", ErrorCodes.AvatarFileRequired, HttpStatusCode.BadRequest); + } + + AvatarUrl avatarUrl; + try + { + await using (avatarStream) + { + avatarUrl = await saveToStorageAsync(principal, avatarStream, ct); + } + } + catch + { + return new ErrorResponse("An error occurred while uploading the file.", ErrorCodes.UnknownError, HttpStatusCode.InternalServerError); + } + + // Shared post-save tail — see RunAvatarMetadataChangeAsync. + return await RunAvatarMetadataChangeAsync( + getExistingAvatarAsync, + c => updateMetadataAsync(avatarUrl, c), + avatarStorage, + ct); + } + + protected async Task HandleAvatarUrlUploadAsync( + string? requestUrl, + Func> getExistingAvatarAsync, + Func> updateMetadataAsync, + IAvatarStorage avatarStorage, + CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(requestUrl)) + return new ErrorResponse("Avatar URL payload required.", ErrorCodes.AvatarUrlInvalid, HttpStatusCode.BadRequest); + + if (!AvatarUrlValidator.TryNormalize(requestUrl, out var url, out var err)) + return new ErrorResponse("Invalid avatar URL.", err, HttpStatusCode.BadRequest); + + var normalisedUrl = new AvatarUrl(url); + return await RunAvatarMetadataChangeAsync( + getExistingAvatarAsync, + c => updateMetadataAsync(normalisedUrl, c), + avatarStorage, + ct); + } + + protected Task HandleAvatarDeleteAsync( + Func> getExistingAvatarAsync, + Func> updateMetadataAsync, + IAvatarStorage avatarStorage, + CancellationToken ct) + => RunAvatarMetadataChangeAsync(getExistingAvatarAsync, updateMetadataAsync, avatarStorage, ct); + + /// Shared avatar-mutation tail: best-effort read current → update metadata + /// → best-effort delete previous Local bytes on success. Read/delete errors are + /// swallowed — a stale bytes-file isn't worth failing a metadata write over. + private async Task RunAvatarMetadataChangeAsync( + Func> getExistingAvatarAsync, + Func> updateMetadataAsync, + IAvatarStorage avatarStorage, + CancellationToken ct) + { + AvatarUrl? currentAvatarUrl = null; + AvatarSource? currentAvatarSource = null; + try + { + var existingAvatar = await getExistingAvatarAsync(ct); + currentAvatarUrl = existingAvatar.AvatarUrl; + currentAvatarSource = existingAvatar.AvatarSource; + } + catch { } + + var result = await updateMetadataAsync(ct); + if (!result.IsSuccess) return result; + + if (currentAvatarSource == AvatarSource.Local && currentAvatarUrl is not null) + { + try { await avatarStorage.DeleteByUrlAsync(currentAvatarUrl, ct); } catch { } + } + + return result; + } + + /// Executes a command handler with latency / outcome / conflict metrics and + /// stamps X-Interfold-Command-Id on the response. + protected async Task ExecuteCommandAsync( + ICommandHandler handler, + CommandEnvelope envelope, + CancellationToken ct) + where TResult : ICommandResult + { + var sw = Stopwatch.StartNew(); + var result = await handler.HandleAsync(envelope, ct); + sw.Stop(); + + var opTag = new KeyValuePair("operation_id", envelope.OperationId); + + InterfoldMetrics.CommandLatencyMs.Record( + sw.Elapsed.TotalMilliseconds, + opTag); + + if (result.Accepted) + { + var outcome = result.Result!.Replay ? "replay" : "accepted"; + InterfoldMetrics.CommandsTotal.Add(1, opTag, + new KeyValuePair("outcome", outcome)); + } + else + { + InterfoldMetrics.CommandsTotal.Add(1, opTag, + new KeyValuePair("outcome", "rejected")); + + InterfoldMetrics.ConflictsTotal.Add(1, opTag, + new KeyValuePair("conflict_code", + result.Conflict!.Code.ToString())); + } + + Response.Headers[InterfoldHeaders.CommandId] = envelope.CommandId.ToString("N"); + + if (result.Accepted) + return Ok(result.Result); + + return result.Conflict!.Code switch + { + ConflictCode.ConflictDuplicate => Conflict(result.Conflict), + ConflictCode.ConflictInvariant => UnprocessableEntity(result.Conflict), + _ => StatusCode(500, new ErrorResponse( + "An unknown error occurred.", + ErrorCodes.UnknownError, + System.Net.HttpStatusCode.InternalServerError)) + }; + } + + /// Dispatches and maps to 204 on success, error on conflict. + protected async Task DispatchNoContentAsync( + ICommandHandler handler, + OperationId operationId, + TPayload payload, + CancellationToken ct) + where TResult : ICommandResult + { + var envelope = BuildEnvelope(operationId, payload); + var result = await handler.HandleAsync(envelope, ct); + return CommandNoContent(result); + } + + /// Dispatches and maps to 201 with an async-hydrated body; optional + /// stamps the Location header. + protected async Task> DispatchCreatedAsync( + ICommandHandler handler, + OperationId operationId, + TPayload payload, + Func> dataSelector, + CancellationToken ct, + Func? locationSelector = null) + where TResult : ICommandResult + { + var envelope = BuildEnvelope(operationId, payload); + var result = await handler.HandleAsync(envelope, ct); + return await CommandCreatedAsync(result, dataSelector, locationSelector); + } + + + /// Dispatches and maps to 202 on success, error on conflict. + protected async Task> DispatchAcceptedAsync( + ICommandHandler handler, + OperationId operationId, + TPayload payload, + Func dataSelector, + CancellationToken ct) + where TResult : ICommandResult + { + var envelope = BuildEnvelope(operationId, payload); + var result = await handler.HandleAsync(envelope, ct); + return CommandAccepted(result, dataSelector); + } + + + /// Maps a result to 204 or an error. + protected Response CommandNoContent(CommandExecutionResult result) + { + if (result.Accepted) + return new Response(); + + return ConflictToError(result.Conflict!); + } + + /// Maps a result to 201 carrying the selected body; Replay comes off the + /// result directly. + protected Response CommandCreated(CommandExecutionResult result, Func dataSelector) + where T : ICommandResult + { + if (result.Accepted) + return new SuccessResponse(dataSelector(result.Result!), HttpStatusCode.Created, result.Result!.Replay); + + return ConflictToError(result.Conflict!); + } + + /// Maps a result to 201 with async body hydration; a null hydration returns + /// an unknown-error response. Optional Location header via + /// . + protected async Task> CommandCreatedAsync( + CommandExecutionResult result, + Func> dataSelector, + Func? locationSelector = null) + where T : ICommandResult + { + if (!result.Accepted) + return ConflictToError(result.Conflict!); + + var data = await dataSelector(result.Result!); + if (data is null) + return new ErrorResponse("An unknown error occurred.", ErrorCodes.UnknownError, HttpStatusCode.InternalServerError); + + if (locationSelector != null) + Response.Headers.Location = locationSelector(result.Result!); + + return new SuccessResponse(data, HttpStatusCode.Created, result.Result!.Replay); + } + + /// Maps a result to 202; controller returns as soon as dispatch lands, with + /// the lifecycle continuing out-of-band (typically WebSocket frames). + protected Response CommandAccepted(CommandExecutionResult result, Func dataSelector) + { + if (result.Accepted) + return new SuccessResponse(dataSelector(result.Result!), HttpStatusCode.Accepted); + + return ConflictToError(result.Conflict!); + } + + protected ErrorResponse ConflictToError(Interfold.Shared.Contracts.Operations.ConflictResult conflict) + { + Response.Headers[InterfoldHeaders.OperationId] = conflict.OperationId.Value; + + return conflict.Code switch + { + // ResolutionHint doubles as the client-visible error code; ToWire keeps the + // exact legacy strings ("no_retry" / "manual_merge_required") on the wire. + ConflictCode.ConflictDuplicate => new ErrorResponse( + "A duplicate conflict occurred.", new ErrorCode(conflict.ResolutionHint.ToWire()), HttpStatusCode.Conflict, conflict.EntityRef.Value), + ConflictCode.ConflictInvariant => new ErrorResponse( + "The request could not be processed due to a conflict.", new ErrorCode(conflict.ResolutionHint.ToWire()), + HttpStatusCode.UnprocessableEntity, conflict.EntityRef.Value), + _ => new ErrorResponse("An unknown error occurred.", ErrorCodes.UnknownError, HttpStatusCode.InternalServerError, conflict.EntityRef.Value) + }; + } + + // Synthetic stamp for envelopes built from [AllowAnonymous] endpoints (OAuth callbacks); + // the handler resolves the real system id off the payload and must never trust this. + private static readonly ScopedSystemId AnonymousPrincipalId = ScopedSystemId.ParseScoped("nam:auth"); + + /// Non-throwing ; null on [AllowAnonymous] routes. + private ScopedSystemId? TryGetPrincipalId() + => HttpContext.Items.TryGetValue(InterfoldPrincipalMiddleware.PrincipalIdItemKey, out var value) + && value is ScopedSystemId principal + ? principal + : null; + + /// Builds an envelope for the current request; anonymous routes get the + /// synthetic stamp. + protected CommandEnvelope BuildEnvelope(OperationId operationId, TPayload payload) + => new(operationId, Guid.NewGuid(), + PrincipalId: TryGetPrincipalId() ?? AnonymousPrincipalId, + IdempotencyKey: GetIdempotencyKey(), + OccurredAt: TimeProvider.GetUtcNow(), Payload: payload); + + /// Dispatches and maps to 200 via . + protected async Task> DispatchOkAsync( + ICommandHandler handler, + OperationId operationId, + TPayload payload, + Func wireSelector, + CancellationToken ct) + where TResult : ICommandResult + { + var envelope = BuildEnvelope(operationId, payload); + var result = await handler.HandleAsync(envelope, ct); + if (result.Accepted) + return new SuccessResponse(wireSelector(result.Result!)); + + return ConflictToError(result.Conflict!); + } + + /// 200 with , 404 with the given message/code when null. + protected Response OkOrNotFound(T? value, string message, ErrorCode code) + => value is null + ? new ErrorResponse(message, code, HttpStatusCode.NotFound) + : new SuccessResponse(value); +} diff --git a/apis/Interfold.Shared.Api/DependencyInjection/SharedApiServiceCollectionExtensions.cs b/apis/Interfold.Shared.Api/DependencyInjection/SharedApiServiceCollectionExtensions.cs new file mode 100644 index 00000000..7bc6a80c --- /dev/null +++ b/apis/Interfold.Shared.Api/DependencyInjection/SharedApiServiceCollectionExtensions.cs @@ -0,0 +1,70 @@ +using System.Net; +using System.Text.Json; +using Interfold.Shared.Api.ModelBinding; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Microsoft.AspNetCore.Mvc; +using JsonOptions = Microsoft.AspNetCore.Mvc.JsonOptions; + +namespace Interfold.Shared.Api.DependencyInjection; + +/// Shared-spine DI wiring consumed by Program.cs today and by every +/// future Interfold.<Feature>.Api module tomorrow. Order-invariant with +/// AddControllers(): the caller is free to invoke either first. +public static class SharedApiServiceCollectionExtensions +{ + /// Registers the shared-spine services: MVC binder / JSON / behaviour option + /// configurators, and the . Does not call + /// AddControllers() — the composition host owns that. + public static IServiceCollection AddSharedApi(this IServiceCollection services) + { + // UnixSecondsModelBinderProvider at position 0 takes precedence over MVC's SimpleType / + // ComplexObject providers for UnixSeconds parameters. + services.Configure(mvcOptions => + { + mvcOptions.ModelBinderProviders.Insert(0, new UnixSecondsModelBinderProvider()); + }); + + services.Configure(options => + { + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower; + options.JsonSerializerOptions.Converters.Add(new UtcDateTimeConverter()); + options.JsonSerializerOptions.Converters.Add(new UtcDateTimeOffsetConverter()); + }); + + // Route DataAnnotations 400s through ErrorResponse so the Kotlin client can decode them + // (the default ValidationProblemDetails isn't supported). PostConfigure so we win + // against MvcCoreServiceCollectionExtensions.AddApiExplorer's ApiBehaviorOptionsSetup, + // which unconditionally assigns InvalidModelStateResponseFactory in its Configure and + // would otherwise clobber whichever ordering AddControllers is called with. + services.PostConfigure(options => + { + options.InvalidModelStateResponseFactory = context => + { + var firstBadField = context.ModelState + .FirstOrDefault(kv => kv.Value?.Errors.Count > 0 + && !string.IsNullOrWhiteSpace(kv.Value.Errors[0].ErrorMessage)); + + var firstError = firstBadField.Value?.Errors[0].ErrorMessage + ?? "The request payload was invalid."; + + // Prefer the binder's stashed ErrorCode so invalid_end_anchor / invalid_anchor + // survive verbatim without needing globally unique messages. + var stashedCode = context.HttpContext.Items[ + UnixSecondsBindingAttribute.ItemsKey(firstBadField.Key ?? string.Empty)] as string; + + var code = stashedCode is not null + ? new ErrorCode(stashedCode) + : ValidationErrorCodeRegistry.LookupOrDefault(firstError); + + var body = new ErrorResponse(firstError, code, HttpStatusCode.BadRequest); + return new BadRequestObjectResult(body); + }; + }); + + services.AddExceptionHandler(); + + return services; + } +} diff --git a/apis/Interfold.Shared.Api/Helpers/AvatarServingPolicy.cs b/apis/Interfold.Shared.Api/Helpers/AvatarServingPolicy.cs new file mode 100644 index 00000000..6bd6f1bc --- /dev/null +++ b/apis/Interfold.Shared.Api/Helpers/AvatarServingPolicy.cs @@ -0,0 +1,85 @@ +namespace Interfold.Shared.Api.Helpers; + +/// Decides whether the API process should serve avatar files and, if so, from +/// which physical root under which request path. Collapses AvatarStorageRoot × +/// AvatarPublicBase: +/// +/// AvatarStorageRoot blank or missing → don't serve. +/// AvatarPublicBase is an absolute non-loopback http(s) URL → +/// CDN/edge fronts the bytes → don't serve. +/// Otherwise the API is the origin; RequestPath comes from +/// AvatarPublicBase (or defaultPublicBase if blank). +/// +public static class AvatarServingPolicy +{ + /// Default request path when avatarPublicBase is blank; matches the + /// path stamps into avatar_url. + public const string DefaultPublicBase = "/avatars"; + + /// Resolves the (serve?, physical root, request path) tuple. Pure aside from + /// a single probe. + public static (bool ShouldServe, string PhysicalRoot, string RequestPath) Resolve( + string? avatarStorageRoot, + string? avatarPublicBase, + string defaultPublicBase = DefaultPublicBase) + { + if (string.IsNullOrWhiteSpace(avatarStorageRoot)) + { + return (false, string.Empty, string.Empty); + } + + if (!Directory.Exists(avatarStorageRoot)) + { + return (false, string.Empty, string.Empty); + } + + // Loopback carve-out: integration tests and same-origin dev stamp http://localhost/… + // URLs that the API itself must still serve. + if (!string.IsNullOrWhiteSpace(avatarPublicBase) + && Uri.TryCreate(avatarPublicBase, UriKind.Absolute, out var absolute) + && (absolute.Scheme == Uri.UriSchemeHttp || absolute.Scheme == Uri.UriSchemeHttps)) + { + if (IsLocalLoopbackHost(absolute.Host)) + { + var localPath = absolute.AbsolutePath.TrimEnd('/'); + if (localPath.Length == 0) + localPath = defaultPublicBase.TrimEnd('/'); + + return (true, avatarStorageRoot, localPath); + } + + return (false, string.Empty, string.Empty); + } + + var requestPath = string.IsNullOrWhiteSpace(avatarPublicBase) + ? defaultPublicBase + : avatarPublicBase!; + + if (!requestPath.StartsWith('/')) + { + requestPath = "/" + requestPath; + } + + requestPath = requestPath.TrimEnd('/'); + + // "/" collapses to "" which StaticFileOptions treats as "match every request". + // Re-anchor to the default rather than let that escape. + if (requestPath.Length == 0) + { + requestPath = defaultPublicBase.TrimEnd('/'); + } + + return (true, avatarStorageRoot, requestPath); + } + + private static bool IsLocalLoopbackHost(string host) + { + if (string.IsNullOrWhiteSpace(host)) + return false; + + return host.Equals("localhost", StringComparison.OrdinalIgnoreCase) + || host.Equals("127.0.0.1", StringComparison.Ordinal) + || host.Equals("[::1]", StringComparison.Ordinal) + || host.Equals("::1", StringComparison.Ordinal); + } +} diff --git a/apis/Interfold.Shared.Api/Helpers/AvatarUrlQualifier.cs b/apis/Interfold.Shared.Api/Helpers/AvatarUrlQualifier.cs new file mode 100644 index 00000000..131fd03d --- /dev/null +++ b/apis/Interfold.Shared.Api/Helpers/AvatarUrlQualifier.cs @@ -0,0 +1,79 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Shared.Api.Helpers; + +internal static class AvatarUrlQualifier +{ + /// Prepends the server origin to relative ; absolute + /// URLs pass through. Non-avatar callers only — avatar callers use + /// which + /// consults avatar_source. + internal static string? Qualify(string? url, string scheme, HostString host) + { + if (string.IsNullOrWhiteSpace(url)) + return url; + + if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || + url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + return url; + + var origin = $"{scheme}://{host}"; + return url.StartsWith('/') ? $"{origin}{url}" : $"{origin}/{url}"; + } + + /// Overload for callers that already hold a pre-built origin string. + internal static string? Qualify(string? url, string? origin) + { + if (string.IsNullOrWhiteSpace(url)) + return url; + + if (string.IsNullOrWhiteSpace(origin)) + return url; + + if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || + url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + return url; + + return url.StartsWith('/') ? $"{origin}{url}" : $"{origin}/{url}"; + } + + /// Source-aware: gets the origin prepended, + /// is returned verbatim, null/blank passes through. + internal static AvatarUrl? QualifyAvatar(AvatarUrl? url, AvatarSource? source, string scheme, HostString host) + { + if (url is not { } present || string.IsNullOrWhiteSpace(present.Value)) + return url; + + return source == AvatarSource.Local + ? AvatarUrl.FromNullable(Qualify(present.Value, scheme, host)) + : url; + } + + /// Origin-string overload for socket handlers that hold a pre-built origin. + internal static AvatarUrl? QualifyAvatar(AvatarUrl? url, AvatarSource? source, string? origin) + { + if (url is not { } present || string.IsNullOrWhiteSpace(present.Value)) + return url; + + return source == AvatarSource.Local + ? AvatarUrl.FromNullable(Qualify(present.Value, origin)) + : url; + } + + /// overload; null bearings pass through so + /// profile?.QualifyAvatar(...) stays a one-liner. + internal static AvatarUrl? QualifyAvatar(IAvatarBearing? bearing, string scheme, HostString host) + => bearing is null ? null : QualifyAvatar(bearing.AvatarUrl, bearing.AvatarSource, scheme, host); + + /// Origin-string overload of the qualifier. + internal static AvatarUrl? QualifyAvatar(IAvatarBearing? bearing, string? origin) + => bearing is null ? null : QualifyAvatar(bearing.AvatarUrl, bearing.AvatarSource, origin); + + // QualifyFriendship + QualifyFriendRequest overloads moved to + // apis/Interfold.Friendships.Api/Helpers/FriendshipAvatarQualifier.cs during the Phase-3 + // Friendships slice — this shared file no longer binds the friendship read-model cluster + // (which now lives in Interfold.Friendships.Contracts). The per-avatar QualifyAvatar + // primitives above stay here. +} diff --git a/apis/Interfold.Shared.Api/Helpers/AvatarUrlValidator.cs b/apis/Interfold.Shared.Api/Helpers/AvatarUrlValidator.cs new file mode 100644 index 00000000..4805f368 --- /dev/null +++ b/apis/Interfold.Shared.Api/Helpers/AvatarUrlValidator.cs @@ -0,0 +1,61 @@ +using Interfold.Shared.Contracts; + +namespace Interfold.Shared.Api.Helpers; + +/// +/// Boundary-only check for client-supplied avatar URLs. The server never fetches +/// the URL — it just stores it on avatar_url with avatar_source = External — +/// so this validator only enforces shape (absolute, http/https, length-capped). +/// +internal static class AvatarUrlValidator +{ + /// + /// Maximum stored URL length. Matches conservative defaults for URI columns + /// across the persistence backends; SP CDN URLs comfortably fit inside it. + /// + internal const int MaxLength = 2048; + + /// + /// Validates and normalises . Returns the trimmed URL on success. + /// + internal static bool TryNormalize(string? raw, out string url, out ErrorCode errorCode) + { + url = string.Empty; + errorCode = default; + + if (string.IsNullOrWhiteSpace(raw)) + { + errorCode = ErrorCodes.AvatarUrlInvalid; + return false; + } + + var trimmed = raw.Trim(); + if (trimmed.Length > MaxLength) + { + errorCode = ErrorCodes.AvatarUrlTooLong; + return false; + } + + if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var uri)) + { + errorCode = ErrorCodes.AvatarUrlInvalid; + return false; + } + + if (uri.Scheme is not ("http" or "https")) + { + errorCode = ErrorCodes.AvatarUrlInvalid; + return false; + } + + url = uri.ToString(); + if (url.Length > MaxLength) + { + errorCode = ErrorCodes.AvatarUrlTooLong; + url = string.Empty; + return false; + } + + return true; + } +} diff --git a/apis/Interfold.Shared.Api/Helpers/InterfoldMetrics.cs b/apis/Interfold.Shared.Api/Helpers/InterfoldMetrics.cs new file mode 100644 index 00000000..66a31d61 --- /dev/null +++ b/apis/Interfold.Shared.Api/Helpers/InterfoldMetrics.cs @@ -0,0 +1,81 @@ +using System.Diagnostics.Metrics; + +namespace Interfold.Shared.Api.Helpers; + +/// +/// Application-level named with the counters and histograms used +/// for Phase N observability hardening. +/// Wire via builder.Services.AddOpenTelemetry().WithMetrics(m => m.AddMeter("Interfold.Api")). +/// +public static class InterfoldMetrics +{ + public static readonly string MeterName = "Interfold.Api"; + + private static readonly Meter Meter = new(MeterName, "1.0"); + + /// + /// Total command invocations. Tags: operation_id, outcome (accepted|replay|rejected). + /// + public static readonly Counter CommandsTotal = Meter.CreateCounter( + "octocon.commands.total", + description: "Total command invocations tagged by operation_id and outcome."); + + /// + /// Command conflicts. Tags: operation_id, conflict_code. + /// + public static readonly Counter ConflictsTotal = Meter.CreateCounter( + "octocon.commands.conflicts.total", + description: "Command conflict counter tagged by operation_id and conflict_code."); + + /// + /// Command handler wall-clock latency in milliseconds. Tags: operation_id. + /// + public static readonly Histogram CommandLatencyMs = Meter.CreateHistogram( + "octocon.commands.latency_ms", + unit: "ms", + description: "Command handler latency in milliseconds."); + + /// + /// Guarded-visibility row filtering outcomes. Tags: entity_type + /// (alter|tag|fronting|field), outcome + /// (visible|filtered). + /// + public static readonly Counter GuardedFilteredTotal = Meter.CreateCounter( + "octocon.guarded.filtered.total", + description: "Guarded rows tagged by entity_type and outcome (visible|filtered)."); + + /// + /// Guarded-visibility calls that returned no rows to the viewer (edge case worth + /// surfacing separately from ordinary filtering). Tags: entity_type. + /// + public static readonly Counter GuardedNoResultsTotal = Meter.CreateCounter( + "octocon.guarded.no_results.total", + description: "Guarded reads that returned zero visible rows, tagged by entity_type."); + + /// + /// Guarded-visibility method wall-clock latency in milliseconds. Tags: + /// entity_type, method (ListGuardedAsync|GetGuardedAsync| + /// ListActiveGuardedAsync). + /// + public static readonly Histogram GuardedLatencyMs = Meter.CreateHistogram( + "octocon.guarded.latency_ms", + unit: "ms", + description: "Guarded read latency in milliseconds, tagged by entity_type and method."); + + /// + /// Guarded-visibility errors bubbled out of the friendship-lookup / field-projection + /// hot paths. Tags: entity_type, exception_type. + /// + public static readonly Counter GuardedErrorsTotal = Meter.CreateCounter( + "octocon.guarded.errors.total", + description: "Guarded-path exceptions, tagged by entity_type and exception_type."); + + /// + /// PublicSystemsController batch endpoint per-slice latency in milliseconds. + /// Tags: slice (aggregate|alter|tag|fronting). + /// + public static readonly Histogram GuardedBatchLatencyMs = Meter.CreateHistogram( + "octocon.guarded.batch.latency_ms", + unit: "ms", + description: "Batch public-systems endpoint latency in milliseconds, tagged by slice."); +} diff --git a/apis/Interfold.Shared.Api/Interfold.Shared.Api.csproj b/apis/Interfold.Shared.Api/Interfold.Shared.Api.csproj new file mode 100644 index 00000000..8e34f7aa --- /dev/null +++ b/apis/Interfold.Shared.Api/Interfold.Shared.Api.csproj @@ -0,0 +1,61 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Shared.Api/Middleware/InterfoldPrincipalMiddleware.cs b/apis/Interfold.Shared.Api/Middleware/InterfoldPrincipalMiddleware.cs new file mode 100644 index 00000000..7cac9317 --- /dev/null +++ b/apis/Interfold.Shared.Api/Middleware/InterfoldPrincipalMiddleware.cs @@ -0,0 +1,68 @@ +using System.Security.Claims; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc.Controllers; + +namespace Interfold.Shared.Api.Middleware; + +/// +/// Resolves and validates principal IDs for Interfold API controllers. +/// +public sealed class InterfoldPrincipalMiddleware(RequestDelegate next) +{ + internal const string PrincipalIdItemKey = "Interfold.PrincipalId"; + + public async Task InvokeAsync(HttpContext context) + { + if (!RequiresInterfoldPrincipal(context)) + { + await next(context); + return; + } + + var principalId = ResolvePrincipalId(context.User); + if (principalId is null) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + return; + } + + context.Items[PrincipalIdItemKey] = principalId; + await next(context); + } + + private static bool RequiresInterfoldPrincipal(HttpContext context) + { + var endpoint = context.GetEndpoint(); + if (endpoint is null) + return false; + + if (endpoint.Metadata.GetMetadata() is not null) + return false; + + var actionDescriptor = endpoint.Metadata.GetMetadata(); + return actionDescriptor is not null + && typeof(InterfoldControllerBase).IsAssignableFrom(actionDescriptor.ControllerTypeInfo.AsType()); + } + + /// + /// The JWT sub claim is the one place a scoped-system-id string crosses the + /// trust boundary into the process. TryParseScoped enforces the wire invariant here — + /// a legacy or hand-crafted token that omits the region prefix (or names an unknown + /// region) surfaces as a 401 rather than propagating an ambiguous SystemId + /// deeper into the command pipeline. Every downstream site reads a + /// from + /// HttpContext.Items[PrincipalIdItemKey] and can rely on the type to prove it. + /// + private static ScopedSystemId? ResolvePrincipalId(ClaimsPrincipal user) + { + var sub = user.FindFirst(JwtClaimNames.Sub)?.Value + ?? user.FindFirst(ClaimTypes.NameIdentifier)?.Value; + + return ScopedSystemId.TryParseScoped(sub, out var scoped) + ? scoped + : null; + } +} diff --git a/apis/Interfold.Shared.Api/Middleware/RequestCorrelationMiddleware.cs b/apis/Interfold.Shared.Api/Middleware/RequestCorrelationMiddleware.cs new file mode 100644 index 00000000..a346cabd --- /dev/null +++ b/apis/Interfold.Shared.Api/Middleware/RequestCorrelationMiddleware.cs @@ -0,0 +1,63 @@ +using System.Diagnostics; +using Interfold.Shared.Contracts; +using Microsoft.Extensions.Primitives; + +namespace Interfold.Shared.Api.Middleware; + +/// +/// Middleware that: +/// +/// Reads (or generates) a X-Interfold-Request-Id correlation ID from the incoming request. +/// Pushes RequestId, RequestPath, and RequestMethod into the log scope. +/// Echoes the X-Interfold-Request-Id header back on every response. +/// Logs the completed request at Information level with StatusCode and ElapsedMs. +/// +/// +public sealed class RequestCorrelationMiddleware(RequestDelegate next, ILogger logger) +{ + public async Task InvokeAsync(HttpContext context) + { + var requestId = GetOrGenerateRequestId(context); + + context.Response.OnStarting(() => + { + context.Response.Headers[InterfoldHeaders.RequestId] = requestId; + return Task.CompletedTask; + }); + + var sw = Stopwatch.StartNew(); + + using (logger.BeginScope(new Dictionary + { + ["RequestId"] = requestId, + ["RequestPath"] = context.Request.Path.ToString(), + ["RequestMethod"] = context.Request.Method + })) + + try + { + await next(context); + } + finally + { + sw.Stop(); + logger.LogInformation( + "HTTP {Method} {Path} → {StatusCode} in {ElapsedMs:F1} ms", + context.Request.Method, + context.Request.Path, + context.Response.StatusCode, + sw.Elapsed.TotalMilliseconds); + } + } + + private static string GetOrGenerateRequestId(HttpContext context) + { + if (context.Request.Headers.TryGetValue(InterfoldHeaders.InboundRequestId, out StringValues existing) + && !StringValues.IsNullOrEmpty(existing)) + { + return existing.ToString(); + } + + return Guid.NewGuid().ToString("N"); + } +} diff --git a/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsBindingAttribute.cs b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsBindingAttribute.cs new file mode 100644 index 00000000..00f92cfe --- /dev/null +++ b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsBindingAttribute.cs @@ -0,0 +1,21 @@ +namespace Interfold.Shared.Api.ModelBinding; + +/// Inert marker paired with [FromQuery(Name = "…")] on +/// parameters. Controls the wire-visible +/// ErrorResponse on parse failure: writes +/// as the ModelError message and stashes +/// on HttpContext.Items under , +/// which the InvalidModelStateResponseFactory prefers over the message-keyed registry. +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] +internal sealed class UnixSecondsBindingAttribute : Attribute +{ + /// Wire ErrorResponse.Code emitted on parse failure (e.g. + /// invalid_end_anchor). Raw string keeps the attribute constant-expression-friendly. + public required string ErrorCode { get; init; } + + /// Human-readable ErrorResponse.Error emitted on parse failure. + public required string ErrorMessage { get; init; } + + /// Shared key generator for the HttpContext.Items stash. + public static string ItemsKey(string fieldName) => $"UnixSecondsBinding::{fieldName}"; +} diff --git a/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinder.cs b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinder.cs new file mode 100644 index 00000000..e6ce0ae3 --- /dev/null +++ b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinder.cs @@ -0,0 +1,105 @@ +using Interfold.Shared.Api.Models; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; + +namespace Interfold.Shared.Api.ModelBinding; + +/// +/// MVC binder for query anchors. Reads the raw string from +/// the field's value provider, calls +/// , +/// and on failure adds a whose message is taken verbatim +/// from the parameter's and +/// stashes on +/// under +/// . The shared +/// InvalidModelStateResponseFactory (Program.cs) prefers that stashed code +/// when producing the final ErrorResponse, preserving the +/// invalid_end_anchor / invalid_anchor wire codes exactly. +/// +/// +/// When the attribute is missing (defensive — should not happen in production wiring) +/// the binder still fails cleanly with the raw value in the message and stashes +/// nothing, letting ValidationErrorCodeRegistry route the response to the +/// generic bad_request code. +/// +/// +internal sealed class UnixSecondsModelBinder : IModelBinder +{ + public Task BindModelAsync(ModelBindingContext bindingContext) + { + ArgumentNullException.ThrowIfNull(bindingContext); + + var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.FieldName); + if (valueProviderResult == ValueProviderResult.None) + { + var attribute = ResolveAttribute(bindingContext); + if (attribute is not null) + { + bindingContext.ModelState.TryAddModelError(bindingContext.FieldName, attribute.ErrorMessage); + bindingContext.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(bindingContext.FieldName)] + = attribute.ErrorCode; + } + else + { + bindingContext.ModelState.TryAddModelError( + bindingContext.FieldName, + $"A value for '{bindingContext.FieldName}' is required."); + } + + bindingContext.Result = ModelBindingResult.Failed(); + return Task.CompletedTask; + } + + bindingContext.ModelState.SetModelValue(bindingContext.FieldName, valueProviderResult); + var raw = valueProviderResult.FirstValue; + + if (UnixSeconds.TryParse(raw, provider: null, out var parsed)) + { + bindingContext.Result = ModelBindingResult.Success(parsed); + return Task.CompletedTask; + } + + var failureAttribute = ResolveAttribute(bindingContext); + if (failureAttribute is not null) + { + bindingContext.ModelState.TryAddModelError(bindingContext.FieldName, failureAttribute.ErrorMessage); + bindingContext.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(bindingContext.FieldName)] + = failureAttribute.ErrorCode; + } + else + { + bindingContext.ModelState.TryAddModelError( + bindingContext.FieldName, + $"The value '{raw}' is not a valid Unix timestamp."); + } + + bindingContext.Result = ModelBindingResult.Failed(); + return Task.CompletedTask; + } + + private static UnixSecondsBindingAttribute? ResolveAttribute(ModelBindingContext bindingContext) + { + if (bindingContext.ModelMetadata is DefaultModelMetadata defaultMetadata) + { + var fromMetadata = defaultMetadata.Attributes.Attributes + .OfType() + .FirstOrDefault(); + if (fromMetadata is not null) + { + return fromMetadata; + } + } + + var parameter = bindingContext.ActionContext.ActionDescriptor.Parameters + .OfType() + .FirstOrDefault(p => string.Equals(p.Name, bindingContext.ModelMetadata.ParameterName, StringComparison.Ordinal) + || string.Equals(p.Name, bindingContext.FieldName, StringComparison.Ordinal)); + + return parameter?.ParameterInfo + .GetCustomAttributes(typeof(UnixSecondsBindingAttribute), inherit: false) + .OfType() + .FirstOrDefault(); + } +} diff --git a/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinderProvider.cs b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinderProvider.cs new file mode 100644 index 00000000..abd0213f --- /dev/null +++ b/apis/Interfold.Shared.Api/ModelBinding/UnixSecondsModelBinderProvider.cs @@ -0,0 +1,27 @@ +using Interfold.Shared.Api.Models; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; + +namespace Interfold.Shared.Api.ModelBinding; + +/// +/// Wires into the MVC binder pipeline for +/// -typed action parameters. Registered by inserting an +/// instance at position 0 of MvcOptions.ModelBinderProviders in Program.cs +/// so it takes precedence over the built-in +/// / fallbacks — otherwise MVC would +/// try to shape as a complex object (looking for a +/// Value constructor argument on the query string) and emit the wrong +/// diagnostic. +/// +internal sealed class UnixSecondsModelBinderProvider : IModelBinderProvider +{ + private static readonly UnixSecondsModelBinder Instance = new(); + + public IModelBinder? GetBinder(ModelBinderProviderContext context) + { + ArgumentNullException.ThrowIfNull(context); + + return context.Metadata.ModelType == typeof(UnixSeconds) ? Instance : null; + } +} diff --git a/apis/Interfold.Shared.Api/Models/ErrorResponse.cs b/apis/Interfold.Shared.Api/Models/ErrorResponse.cs new file mode 100644 index 00000000..215e6852 --- /dev/null +++ b/apis/Interfold.Shared.Api/Models/ErrorResponse.cs @@ -0,0 +1,40 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts; + +namespace Interfold.Shared.Api.Models; + +public class ErrorResponse +{ + [SetsRequiredMembers] + public ErrorResponse( + string error, + ErrorCode code, + HttpStatusCode? statusCode = null, + string? entityRef = null, + string? detail = null) + { + Error = error; + Code = code; + StatusCode = statusCode ?? HttpStatusCode.InternalServerError; + EntityRef = entityRef; + Detail = detail; + } + + public required string Error { get; init; } + public required ErrorCode Code { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public required string? EntityRef { get; init; } + + // Optional human-readable clarification of the error, e.g. "pass redirect_uri on + // GET /auth/{provider}...". Absorbs the payload that used to live on the deleted + // OAuthRedirectErrorResponse record so both shapes share one wire contract; omitted + // from the JSON envelope when null so unrelated ErrorResponse call sites are unaffected. + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public required string? Detail { get; init; } + + [JsonIgnore] + internal HttpStatusCode StatusCode { get; init; } +} diff --git a/apis/Interfold.Shared.Api/Models/NoContent.cs b/apis/Interfold.Shared.Api/Models/NoContent.cs new file mode 100644 index 00000000..da9dc276 --- /dev/null +++ b/apis/Interfold.Shared.Api/Models/NoContent.cs @@ -0,0 +1,6 @@ +namespace Interfold.Shared.Api.Models; + +/// +/// A marker type representing an empty (204 No Content) success response. +/// +public readonly record struct NoContent; diff --git a/apis/Interfold.Shared.Api/Models/Response.cs b/apis/Interfold.Shared.Api/Models/Response.cs new file mode 100644 index 00000000..81e1b9a7 --- /dev/null +++ b/apis/Interfold.Shared.Api/Models/Response.cs @@ -0,0 +1,88 @@ +using System.Net; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using OneOf; + +namespace Interfold.Shared.Api.Models; + +public class Response : Response +{ + public Response() : base(new SuccessResponse(new NoContent(), HttpStatusCode.NoContent)) + { + } + + public Response(ErrorResponse response) : base(response) + { + } + + /// + /// Implicitly converts the specified to an . + /// + /// The error to convert. + public static implicit operator Response(ErrorResponse error) + { + return new Response(error); + } +} + +[GenerateOneOf] +public class Response : OneOfBase, ErrorResponse>, IConvertToActionResult +{ + public Response(OneOf, ErrorResponse> input) : base(input) + { + } + + /// + /// Implicitly converts the specified to an . + /// + /// The value to convert. + public static implicit operator Response(TValue value) + { + return new Response(new SuccessResponse(value)); + } + + /// + /// Implicitly converts the specified to an . + /// + /// The value to convert. + public static implicit operator Response(SuccessResponse value) + { + return new Response(value); + } + + /// + /// Implicitly converts the specified to an . + /// + /// The error to convert. + public static implicit operator Response(ErrorResponse error) + { + return new Response(error); + } + + public bool IsSuccess => IsT0; + public SuccessResponse AsSuccess => AsT0; + public bool IsError => IsT1; + public ErrorResponse AsError => AsT1; + + public IActionResult Convert() + { + if (IsSuccess) + { + var statusCode = (int)AsSuccess.StatusCode; + if (statusCode == 204) + return new StatusCodeResult(204); + + return new ObjectResult(AsSuccess) + { + DeclaredType = typeof(SuccessResponse), + StatusCode = statusCode + }; + } + + return new ObjectResult(AsError) + { + DeclaredType = typeof(ErrorResponse), + StatusCode = (int)AsError.StatusCode + }; + } +} diff --git a/apis/Interfold.Shared.Api/Models/SuccessResponse.cs b/apis/Interfold.Shared.Api/Models/SuccessResponse.cs new file mode 100644 index 00000000..814223fa --- /dev/null +++ b/apis/Interfold.Shared.Api/Models/SuccessResponse.cs @@ -0,0 +1,24 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Api.Models; + +public class SuccessResponse +{ + [SetsRequiredMembers] + public SuccessResponse(TValue data, HttpStatusCode statusCode = HttpStatusCode.OK, bool? replay = null) + { + Data = data; + StatusCode = statusCode; + Replay = replay; + } + + public required TValue Data { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? Replay { get; init; } + + [JsonIgnore] + internal HttpStatusCode StatusCode { get; init; } +} diff --git a/apis/Interfold.Shared.Api/Models/UnixSeconds.cs b/apis/Interfold.Shared.Api/Models/UnixSeconds.cs new file mode 100644 index 00000000..d8b3c8bc --- /dev/null +++ b/apis/Interfold.Shared.Api/Models/UnixSeconds.cs @@ -0,0 +1,35 @@ +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + +namespace Interfold.Shared.Api.Models; + +/// Validated Unix-seconds anchor for fronting history endpoints. Combines +/// long parse + range check. +/// Culture-invariant (Unix timestamps are integers), so the supplied +/// is ignored. +public readonly record struct UnixSeconds(long Value) : IParsable +{ + public DateTimeOffset ToDateTimeOffset() => DateTimeOffset.FromUnixTimeSeconds(Value); + + public static UnixSeconds Parse(string s, IFormatProvider? provider) + => TryParse(s, provider, out var result) + ? result + : throw new FormatException($"Value '{s}' is not a valid Unix timestamp."); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out UnixSeconds result) + { + if (long.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var seconds) + && seconds >= DateTimeOffset.MinValue.ToUnixTimeSeconds() + && seconds <= DateTimeOffset.MaxValue.ToUnixTimeSeconds()) + { + result = new UnixSeconds(seconds); + return true; + } + + result = default; + return false; + } +} diff --git a/apis/Interfold.Shared.Api/Services/AvatarStorage.cs b/apis/Interfold.Shared.Api/Services/AvatarStorage.cs new file mode 100644 index 00000000..07bc6e76 --- /dev/null +++ b/apis/Interfold.Shared.Api/Services/AvatarStorage.cs @@ -0,0 +1,165 @@ +using System.Text.RegularExpressions; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Options; + +namespace Interfold.Shared.Api.Services; + +public interface IAvatarStorage +{ + /// Persists a system avatar; returns the public URL. + Task SaveSystemAvatarAsync(SystemId systemId, Stream stream, CancellationToken cancellationToken = default); + + /// Persists an alter avatar; returns the public URL. + Task SaveAlterAvatarAsync(SystemId systemId, AlterId alterId, Stream stream, CancellationToken cancellationToken = default); + + /// Deletes the backing file when is local to this storage. + Task DeleteByUrlAsync(AvatarUrl? avatarUrl, CancellationToken cancellationToken = default); +} + +public sealed class LocalAvatarStorage : IAvatarStorage +{ + private static readonly Regex SafeSegmentPattern = new("[^a-zA-Z0-9_-]", RegexOptions.Compiled); + + private readonly IOptionsMonitor _storageOptions; + private readonly string _webRootFallback; + private readonly string _publicBaseFallback; + + // Per-access CurrentValue so config reloads apply without restart. + private string StorageRoot => _storageOptions.CurrentValue.AvatarStorageRoot ?? _webRootFallback; + private string PublicBase => _storageOptions.CurrentValue.AvatarPublicBase ?? _publicBaseFallback; + + public LocalAvatarStorage(IWebHostEnvironment environment, IOptionsMonitor storageOptions) + { + var webRoot = environment.WebRootPath; + if (string.IsNullOrWhiteSpace(webRoot)) + { + webRoot = Path.Combine(environment.ContentRootPath, "wwwroot"); + } + + _storageOptions = storageOptions; + _webRootFallback = Path.Combine(webRoot, "avatars"); + _publicBaseFallback = "/avatars"; + } + + public Task SaveSystemAvatarAsync(SystemId systemId, Stream stream, CancellationToken cancellationToken = default) + => SaveAsync(systemId, "self", stream, cancellationToken); + + public Task SaveAlterAvatarAsync(SystemId systemId, AlterId alterId, Stream stream, CancellationToken cancellationToken = default) + => SaveAsync(systemId, alterId.ToString(), stream, cancellationToken); + + public Task DeleteByUrlAsync(AvatarUrl? avatarUrl, CancellationToken cancellationToken = default) + { + if (avatarUrl is not { } url || string.IsNullOrWhiteSpace(url.Value)) + return Task.FromResult(false); + + var basePath = GetPublicBasePath(PublicBase); + var storageRoot = Path.GetFullPath(StorageRoot); + var storageRootWithSep = storageRoot.EndsWith(Path.DirectorySeparatorChar) + ? storageRoot + : storageRoot + Path.DirectorySeparatorChar; + + var urlPath = url.Value; + if (Uri.TryCreate(urlPath, UriKind.Absolute, out var absoluteUri)) + urlPath = absoluteUri.AbsolutePath; + + if (string.IsNullOrWhiteSpace(urlPath)) + return Task.FromResult(false); + + if (!urlPath.StartsWith('/')) + urlPath = "/" + urlPath; + + if (!urlPath.StartsWith(basePath + "/", StringComparison.OrdinalIgnoreCase)) + return Task.FromResult(false); + + var relativePath = urlPath[basePath.Length..].TrimStart('/').Replace('/', Path.DirectorySeparatorChar); + var fullFilePath = Path.GetFullPath(Path.Combine(storageRoot, relativePath)); + + if (!fullFilePath.StartsWith(storageRootWithSep, StringComparison.OrdinalIgnoreCase)) + return Task.FromResult(false); + + if (!File.Exists(fullFilePath)) + return Task.FromResult(false); + + File.Delete(fullFilePath); + TryDeleteEmptyParentDirectories(storageRoot, Path.GetDirectoryName(fullFilePath)); + return Task.FromResult(true); + } + + // targetId is "self" for system avatars, alterId.ToString() for alter avatars — no wrapper covers both. + private async Task SaveAsync(SystemId systemId, string targetId, Stream stream, CancellationToken cancellationToken) + { + var rawSystemId = ScopedSystemId.StripRegionPrefix(systemId); + var safeSystemId = SafeSegmentPattern.Replace(rawSystemId, "_"); + var safeTargetId = SafeSegmentPattern.Replace(targetId, "_"); + + if (stream.CanSeek) + stream.Position = 0; + + using var buffer = new MemoryStream(); + await stream.CopyToAsync(buffer, cancellationToken); + var content = buffer.ToArray(); + if (content.Length == 0) + throw new InvalidOperationException("Uploaded file has no readable content."); + + var extension = DetectExtension(content); + var fileName = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}_{Guid.NewGuid():N}{extension}"; + + var directoryPath = Path.Combine(StorageRoot, safeSystemId, safeTargetId); + Directory.CreateDirectory(directoryPath); + + var filePath = Path.Combine(directoryPath, fileName); + await File.WriteAllBytesAsync(filePath, content, cancellationToken); + + var basePath = PublicBase.TrimEnd('/'); + return new($"{basePath}/{safeSystemId}/{safeTargetId}/{fileName}"); + } + + /// Normalises to a URL-path prefix. Absolute http(s) values expose their + /// so filesystem comparisons match request paths. + private static string GetPublicBasePath(string publicBase) + { + if (Uri.TryCreate(publicBase, UriKind.Absolute, out var absolute) + && (absolute.Scheme == Uri.UriSchemeHttp || absolute.Scheme == Uri.UriSchemeHttps)) + { + return absolute.AbsolutePath.TrimEnd('/'); + } + + var path = publicBase.TrimEnd('/'); + if (path.Length > 0 && !path.StartsWith('/')) + path = "/" + path; + + return path; + } + + private static string DetectExtension(byte[] data) => data switch + { + [0xFF, 0xD8, ..] => ".jpg", + [0x89, 0x50, 0x4E, 0x47, ..] => ".png", + [0x47, 0x49, 0x46, ..] => ".gif", + [0x52, 0x49, 0x46, 0x46, _, _, _, _, 0x57, 0x45, 0x42, 0x50, ..] => ".webp", + _ => ".bin" + }; + + private static void TryDeleteEmptyParentDirectories(string storageRoot, string? directoryPath) + { + var rootFull = Path.GetFullPath(storageRoot).TrimEnd(Path.DirectorySeparatorChar); + var current = directoryPath; + + while (!string.IsNullOrWhiteSpace(current)) + { + var currentFull = Path.GetFullPath(current).TrimEnd(Path.DirectorySeparatorChar); + if (string.Equals(currentFull, rootFull, StringComparison.OrdinalIgnoreCase)) + return; + + if (!Directory.Exists(currentFull)) + return; + + if (Directory.EnumerateFileSystemEntries(currentFull).Any()) + return; + + Directory.Delete(currentFull); + current = Path.GetDirectoryName(currentFull); + } + } +} diff --git a/apis/Interfold.Shared.Api/Services/ExceptionHandler.cs b/apis/Interfold.Shared.Api/Services/ExceptionHandler.cs new file mode 100644 index 00000000..1a3666ea --- /dev/null +++ b/apis/Interfold.Shared.Api/Services/ExceptionHandler.cs @@ -0,0 +1,32 @@ +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Microsoft.AspNetCore.Diagnostics; + +namespace Interfold.Shared.Api.Services; + +public class ExceptionHandler : IExceptionHandler +{ + public async ValueTask TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) + { + // Log the exception and return a generic error response. + httpContext.Response.ContentType = "application/json"; + await httpContext.Response.WriteAsJsonAsync(CreateError(httpContext, exception), cancellationToken); + return true; + } + + private ErrorResponse CreateError(HttpContext httpContext, Exception exception) + { + if (exception is InterfoldException interfoldException) + { + httpContext.Response.StatusCode = (int?)interfoldException.HttpStatusCode ?? StatusCodes.Status500InternalServerError; + return new ErrorResponse(interfoldException.Message, interfoldException.Code); + } + + httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; + return exception switch + { + BadHttpRequestException badRequestException => new ErrorResponse(badRequestException.Message, ErrorCodes.BadRequest), + _ => new ErrorResponse($"Unhandled exception: {exception}", ErrorCodes.UnknownError) + }; + } +} diff --git a/apis/Interfold.Shared.Api/Services/Secrets/ISecretsSnapshot.cs b/apis/Interfold.Shared.Api/Services/Secrets/ISecretsSnapshot.cs new file mode 100644 index 00000000..5eb5f03e --- /dev/null +++ b/apis/Interfold.Shared.Api/Services/Secrets/ISecretsSnapshot.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.Shared.Api.Services.Secrets; + +/// Synchronous read-only view over the rows the API's +/// patchers +/// consult during options resolution. PostConfigure is sync so we can't await the store; +/// hydrates this snapshot pre-Build. +public interface ISecretsSnapshot +{ + /// Value for , or null if absent/blank/unpopulated. Never throws. + string? Get(SecretsStoreKey key); + + /// True once has returned. + bool IsPopulated { get; } +} diff --git a/apis/Interfold.Shared.Api/Services/StartupProbeConfigurationProxy.cs b/apis/Interfold.Shared.Api/Services/StartupProbeConfigurationProxy.cs new file mode 100644 index 00000000..9b7c0624 --- /dev/null +++ b/apis/Interfold.Shared.Api/Services/StartupProbeConfigurationProxy.cs @@ -0,0 +1,63 @@ +using Microsoft.Extensions.Primitives; + +namespace Interfold.Shared.Api.Services; + +/// +/// Non-owning proxy that forwards to the wrapped instance +/// but does NOT implement , so DI's disposal-capture walker skips +/// it. Used only by Program.cs's throw-away startup-probe service provider via +/// to prevent probe-SP disposal from also disposing the shared +/// owned by +/// — which otherwise breaks +/// 's later +/// ConfigureAppConfiguration with ObjectDisposedException: 'ConfigurationManager'. +/// Not for general use; do not register in builder.Services. +/// +internal sealed class StartupProbeConfigurationProxy(IConfigurationRoot inner) : IConfigurationRoot +{ + public string? this[string key] + { + get => inner[key]; + set => inner[key] = value; + } + + public IEnumerable GetChildren() => inner.GetChildren(); + + public IChangeToken GetReloadToken() => inner.GetReloadToken(); + + public IConfigurationSection GetSection(string key) => inner.GetSection(key); + + public void Reload() => inner.Reload(); + + public IEnumerable Providers => inner.Providers; + + /// + /// Returns a clone of with the + /// and registrations replaced by a shared proxy over + /// ; all other descriptors are copied verbatim. Disposing the + /// probe SP built from the clone leaves intact. + /// + public static IServiceCollection SwapInto(IServiceCollection source, IConfigurationRoot root) + { + var proxy = new StartupProbeConfigurationProxy(root); + IServiceCollection clone = new ServiceCollection(); + + foreach (var descriptor in source) + { + if (descriptor.ServiceType == typeof(IConfiguration)) + { + clone.AddSingleton(proxy); + } + else if (descriptor.ServiceType == typeof(IConfigurationRoot)) + { + clone.AddSingleton(proxy); + } + else + { + clone.Add(descriptor); + } + } + + return clone; + } +} diff --git a/apis/Interfold.Shared.Api/Services/ValidationErrorCodeRegistry.cs b/apis/Interfold.Shared.Api/Services/ValidationErrorCodeRegistry.cs new file mode 100644 index 00000000..8257595a --- /dev/null +++ b/apis/Interfold.Shared.Api/Services/ValidationErrorCodeRegistry.cs @@ -0,0 +1,44 @@ +using System.Collections.Frozen; +using Interfold.Shared.Contracts; + +namespace Interfold.Shared.Api.Services; + +/// +/// Message-to- lookup used by the +/// InvalidModelStateResponseFactory in Program.cs to preserve the +/// wire-visible ErrorResponse.Code for DataAnnotations validation failures. +/// +/// +/// Rationale: +/// does not preserve +/// subclasses — only the resulting ErrorMessage string. Any strategy that +/// keeps the current ErrorResponse { code: "invalid_alter_id" } shape has +/// to funnel through the message. Each +/// that wants a stable wire code registers a +/// "human-readable message" → ErrorCode pair here; unknown messages fall +/// back to the generic . +/// +/// +internal static class ValidationErrorCodeRegistry +{ + private static readonly FrozenDictionary Map = + new Dictionary(StringComparer.Ordinal) + { + ["Invalid alter ID."] = ErrorCodes.InvalidAlterId, + }.ToFrozenDictionary(StringComparer.Ordinal); + + /// + /// Returns the registered for + /// , or when + /// the message is null, empty, or not registered. + /// + public static ErrorCode LookupOrDefault(string? errorMessage) + { + if (string.IsNullOrEmpty(errorMessage)) + { + return ErrorCodes.BadRequest; + } + + return Map.TryGetValue(errorMessage, out var code) ? code : ErrorCodes.BadRequest; + } +} diff --git a/apis/Interfold.Shared.Api/Swagger/HealthCheckDocumentFilter.cs b/apis/Interfold.Shared.Api/Swagger/HealthCheckDocumentFilter.cs new file mode 100644 index 00000000..54c13db6 --- /dev/null +++ b/apis/Interfold.Shared.Api/Swagger/HealthCheckDocumentFilter.cs @@ -0,0 +1,67 @@ +using Interfold.Shared.Contracts.Configuration; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace Interfold.Shared.Api.Swagger; + +public class HealthCheckDocumentFilter : IDocumentFilter +{ + public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) + { + var healthyResponse = new OpenApiResponse { Description = "Healthy" }; + var unhealthyResponse = new OpenApiResponse { Description = "Unhealthy" }; + + swaggerDoc.Paths[HealthEndpoints.Live] = new OpenApiPathItem + { + Operations = + { + [OperationType.Get] = new OpenApiOperation + { + Tags = [new OpenApiTag { Name = "Health" }], + Summary = "Liveness probe", + Description = "Returns 200 if the process is running. No dependency checks.", + Responses = new OpenApiResponses + { + ["200"] = healthyResponse + } + } + } + }; + + swaggerDoc.Paths[HealthEndpoints.Ready] = new OpenApiPathItem + { + Operations = + { + [OperationType.Get] = new OpenApiOperation + { + Tags = [new OpenApiTag { Name = "Health" }], + Summary = "Readiness probe", + Description = "Returns 200 if all dependencies (Scylla, Postgres) are reachable.", + Responses = new OpenApiResponses + { + ["200"] = healthyResponse, + ["503"] = unhealthyResponse + } + } + } + }; + + swaggerDoc.Paths[HealthEndpoints.Startup] = new OpenApiPathItem + { + Operations = + { + [OperationType.Get] = new OpenApiOperation + { + Tags = [new OpenApiTag { Name = "Health" }], + Summary = "Startup probe", + Description = "Returns 200 once all dependencies are initialized. Uses longer timeouts than readiness.", + Responses = new OpenApiResponses + { + ["200"] = healthyResponse, + ["503"] = unhealthyResponse + } + } + } + }; + } +} diff --git a/apis/Interfold.Shared.Api/Swagger/MultipleContentTypeOperationFilter.cs b/apis/Interfold.Shared.Api/Swagger/MultipleContentTypeOperationFilter.cs new file mode 100644 index 00000000..c8a86ba0 --- /dev/null +++ b/apis/Interfold.Shared.Api/Swagger/MultipleContentTypeOperationFilter.cs @@ -0,0 +1,72 @@ +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace Interfold.Shared.Api.Swagger; + +public class MultipleContentTypeOperationFilter : IDocumentFilter +{ + public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) + { + // Only allow conflicts for avatar upload endpoints + var allowedConflicts = new[] + { + "api/settings/avatar", + "api/systems/me/alters/{id}/avatar" + }; + + // Find avatar upload operations and ensure they support both JSON and multipart + foreach (var path in swaggerDoc.Paths) + { + var routeLower = path.Key.ToLowerInvariant(); + if (!allowedConflicts.Any(routeLower.Contains)) + continue; + + foreach (var operation in path.Value.Operations.Values) + { + if (operation.RequestBody?.Content == null) + continue; + + // If this operation has JSON content, add multipart support + var jsonContent = operation.RequestBody.Content + .FirstOrDefault(c => c.Key.StartsWith("application/json")); + + if (jsonContent.Value == null || operation.RequestBody.Content.ContainsKey("multipart/form-data")) + { + continue; + } + + operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType + { + Schema = new OpenApiSchema + { + Type = "object", + Properties = new Dictionary + { + ["file"] = new OpenApiSchema + { + Type = "string", + Format = "binary", + Description = "The avatar file to upload" + }, + ["idempotencyKey"] = new OpenApiSchema + { + Type = "string", + Nullable = true + } + } + } + }; + + // Update operation description + if (!string.IsNullOrEmpty(operation.Description)) + { + operation.Description += "\n\nThis endpoint accepts both JSON (with avatarUrl) and multipart/form-data (with file upload)."; + } + else + { + operation.Description = "Accepts both JSON (with avatarUrl) and multipart/form-data (with file upload)."; + } + } + } + } +} diff --git a/apis/Interfold.Shared.Api/Uploads/AvatarUploadPayload.cs b/apis/Interfold.Shared.Api/Uploads/AvatarUploadPayload.cs new file mode 100644 index 00000000..fc86ef8a --- /dev/null +++ b/apis/Interfold.Shared.Api/Uploads/AvatarUploadPayload.cs @@ -0,0 +1,3 @@ +namespace Interfold.Shared.Api.Uploads; + +public sealed record AvatarUploadPayload(Stream? Stream, bool EmptyFilePart = false); diff --git a/apis/Interfold.Socket.Api/DependencyInjection/SocketModuleServiceCollectionExtensions.cs b/apis/Interfold.Socket.Api/DependencyInjection/SocketModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..163677b1 --- /dev/null +++ b/apis/Interfold.Socket.Api/DependencyInjection/SocketModuleServiceCollectionExtensions.cs @@ -0,0 +1,47 @@ +using Interfold.Socket.Api.Helpers; +using Interfold.Socket.Api.Socket; + +namespace Interfold.Socket.Api.DependencyInjection; + +/// Socket feature module — owns the Phoenix-protocol WebSocket transport +/// (WebSocketHandler + WebSocketInitialization + SocketEventPumpRunner +/// + per-feature *SocketEventHandlers), the SocketJoinRateLimiter singleton, +/// the loopback named that the endpoint-relay self-calls, +/// and the /api/socket/websocket route. Composed once from +/// Interfold.Api.Host/Program.cs alongside the other feature modules. +public static class SocketModuleServiceCollectionExtensions +{ + /// Registers the socket runtime: SocketJoinRateLimiter singleton + + /// the named with a permissive + /// TLS-validation handler (the leaf PFX served by Kestrel has no loopback SAN, and the + /// call site's guard pins outbound calls + /// to loopback — see WebSocketHandler.HandleEndpointProxyAsync). + public static IServiceCollection AddSocketModule(this IServiceCollection services) + { + services.AddSingleton(); + + services.AddHttpClient(LoopbackHttpClient.Name) + .ConfigurePrimaryHttpMessageHandler(static () => new SocketsHttpHandler + { + AllowAutoRedirect = false, + SslOptions = new System.Net.Security.SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + }, + }); + + return services; + } + + /// Maps the anonymous GET|CONNECT /api/socket/websocket upgrade route + /// to . Host must have called + /// + /// first so the upgrade middleware is in the pipeline. + public static IEndpointRouteBuilder MapSocketModule(this IEndpointRouteBuilder endpoints) + { + endpoints + .MapMethods("/api/socket/websocket", ["GET", "CONNECT"], WebSocketHandler.HandleUserSocketAsync) + .AllowAnonymous(); + return endpoints; + } +} diff --git a/apis/Interfold.Socket.Api/Helpers/LoopbackHttpClient.cs b/apis/Interfold.Socket.Api/Helpers/LoopbackHttpClient.cs new file mode 100644 index 00000000..c413bb20 --- /dev/null +++ b/apis/Interfold.Socket.Api/Helpers/LoopbackHttpClient.cs @@ -0,0 +1,21 @@ +namespace Interfold.Socket.Api.Helpers; + +/// Named for the WS endpoint-relay's self-call. Uses a +/// permissive RemoteCertificateValidationCallback: the leaf PFX served by Kestrel +/// has no loopback SAN and its private root isn't in the container trust store, so both +/// default checks fail. Safe because the call site's guard +/// keeps the client pinned to loopback destinations. +public static class LoopbackHttpClient +{ + /// client name. NEVER use for a non-loopback + /// destination — TLS validation is permissive. + public const string Name = "interfold-loopback"; + + /// Literal loopback-shape check (no name resolution): defers to + /// which covers 127.0.0.0/8, ::1, and localhost. + public static bool IsLoopbackHost(Uri uri) + { + ArgumentNullException.ThrowIfNull(uri); + return uri.IsLoopback; + } +} diff --git a/apis/Interfold.Socket.Api/Interfold.Socket.Api.csproj b/apis/Interfold.Socket.Api/Interfold.Socket.Api.csproj new file mode 100644 index 00000000..dae7f60f --- /dev/null +++ b/apis/Interfold.Socket.Api/Interfold.Socket.Api.csproj @@ -0,0 +1,64 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Socket.Api/Models/SocketAggregateJoinPayloads.cs b/apis/Interfold.Socket.Api/Models/SocketAggregateJoinPayloads.cs new file mode 100644 index 00000000..bff9e2e7 --- /dev/null +++ b/apis/Interfold.Socket.Api/Models/SocketAggregateJoinPayloads.cs @@ -0,0 +1,28 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Socket.Contracts; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Socket.Api.Models; + +// Socket aggregate join payloads. Namespace preserved as Interfold.Shared.Contracts for +// wire-compat; physical file moved out of shared/Interfold.Shared.Contracts/SocketPayloadContracts.cs +// during the Phase-3 Alters slice to break the spine-batch-DTO cycle with +// Interfold.Alters.Contracts (each binds IReadOnlyList). These migrate +// into the Socket module (Phase 3 #10); the same relocation is what lets AlterReadModel, +// TagReadModel, and FrontActiveReadModel move off the spine into their per-feature Contracts. + +public sealed record SocketJoinInitPayload( + SocketSelfReadModel System, + IReadOnlyList Alters, + IReadOnlyList Fronts, + IReadOnlyList Tags) : ISocketPayload; + +public sealed record SocketJoinBatchedPayload( + bool Batched, + SocketSelfReadModel System, + IReadOnlyList? Alters, + IReadOnlyList? Fronts, + IReadOnlyList? Tags) : ISocketPayload; + +public sealed record SocketBatchedAltersPayload(int BatchIndex, int TotalBatches, IReadOnlyList Alters) : ISocketPayload; diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/AlterSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/AlterSocketEventHandlers.cs new file mode 100644 index 00000000..e86f404d --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/AlterSocketEventHandlers.cs @@ -0,0 +1,33 @@ +using Interfold.Alters.Contracts; +using Interfold.Alters.Contracts.Events; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class AlterSocketEventHandlers +{ + public static async Task HandleAsync(AlterCreatedEvent evt, SocketPushContext context, IAlterRepository alterRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.AlterId, SocketEventNames.Alters.Created, context, alterRepository); + + public static async Task HandleAsync(AlterUpdatedEvent evt, SocketPushContext context, IAlterRepository alterRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.AlterId, SocketEventNames.Alters.Updated, context, alterRepository); + + public static async Task HandleAsync(AlterDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Alters.Deleted, new AlterDeletedSocketPayload(evt.AlterId)); + } + + private static Task HandleUpsertAsync(SystemId systemId, AlterId alterId, string eventName, SocketPushContext context, IAlterRepository alterRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => alterRepository.GetAsync(systemId, alterId, ct), + alter => + { + alter.AvatarUrl = AvatarUrlQualifier.QualifyAvatar(alter, context.RequestOrigin); + return new AlterSocketPayload(alter); + }); +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/FriendshipSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/FriendshipSocketEventHandlers.cs new file mode 100644 index 00000000..c0ed05e8 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/FriendshipSocketEventHandlers.cs @@ -0,0 +1,194 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Events; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class FriendshipSocketEventHandlers +{ + public static async Task HandleAsync(FriendshipAddedEvent evt, SocketPushContext context, IFriendshipRepository friendshipRepository) + { + if (!context.TryGetSystemTopic(evt.TargetSystemId, out var topic, out var joinRef, out var asArray)) + { + return; + } + + var friendship = await GetFriendshipWithRetryAsync(friendshipRepository, evt.TargetSystemId, evt.SystemId, context.CancellationToken); + // Inlined from the former AvatarUrlQualifier.QualifyFriendship(origin) overload — the + // shared spine helper moved out during the Phase-3 Friendships slice so + // Interfold.Shared.Api no longer binds the friendship read-model cluster; per-avatar + // qualification still uses the shared primitive. + var qualified = friendship is null + ? new FriendshipReadModel( + new FriendProfileReadModel(evt.SystemId, new Username(string.Empty), null, null, string.Empty, new DiscordId(string.Empty)), + new FriendshipModel(FriendshipLevel.Friend, context.TimeProvider.GetUtcNow()), + []) + : friendship with + { + Friend = friendship.Friend with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(friendship.Friend, context.RequestOrigin), + }, + Fronting = friendship.Fronting + .Select(f => f with + { + Alter = f.Alter with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(f.Alter, context.RequestOrigin), + }, + }) + .ToArray(), + }; + + await context.SendAsync(topic, joinRef, asArray, SocketEventNames.Friendships.Added, qualified); + } + + public static Task HandleAsync(FriendshipRemovedEvent evt, SocketPushContext context) + => SendAsync(evt.TargetSystemId, SocketEventNames.Friendships.Removed, SocketPayloadPropertyKeys.FriendId, evt.SystemId, context); + + public static Task HandleAsync(FriendshipTrustedEvent evt, SocketPushContext context) + => SendAsync(evt.TargetSystemId, SocketEventNames.Friendships.Trusted, SocketPayloadPropertyKeys.FriendId, evt.SystemId, context); + + public static Task HandleAsync(FriendshipUntrustedEvent evt, SocketPushContext context) + => SendAsync(evt.TargetSystemId, SocketEventNames.Friendships.Untrusted, SocketPayloadPropertyKeys.FriendId, evt.SystemId, context); + + public static async Task HandleAsync(FriendRequestSentEvent evt, SocketPushContext context, IFriendshipRepository friendshipRepository) + { + await SendRequestPayloadAsync( + evt.TargetSystemId, + evt.ToSystemId, + SocketEventNames.Friendships.RequestSent, + context, + friendshipRepository, + outgoing: true); + } + + public static async Task HandleAsync(FriendRequestReceivedEvent evt, SocketPushContext context, IFriendshipRepository friendshipRepository) + { + await SendRequestPayloadAsync( + evt.TargetSystemId, + evt.FromSystemId, + SocketEventNames.Friendships.RequestReceived, + context, + friendshipRepository, + outgoing: false); + } + + public static Task HandleAsync(FriendRequestRemovedFromEvent evt, SocketPushContext context) + => SendAsync(evt.TargetSystemId, SocketEventNames.Friendships.RequestRemoved, SocketPayloadPropertyKeys.SystemId, evt.FromSystemId, context); + + public static Task HandleAsync(FriendRequestRemovedToEvent evt, SocketPushContext context) + => SendAsync(evt.TargetSystemId, SocketEventNames.Friendships.RequestRemoved, SocketPayloadPropertyKeys.SystemId, evt.ToSystemId, context); + + private static async Task SendRequestPayloadAsync( + SystemId targetSystemId, + SystemId otherSystemId, + string eventName, + SocketPushContext context, + IFriendshipRepository friendshipRepository, + bool outgoing) + { + if (!context.TryGetSystemTopic(targetSystemId, out var topic, out var joinRef, out var asArray)) + { + return; + } + + var matched = await GetFriendRequestWithRetryAsync( + friendshipRepository, + targetSystemId, + otherSystemId, + outgoing, + context.CancellationToken); + + var payload = matched is null + ? new FriendRequestSocketPayload( + new FriendshipRequestModel(context.TimeProvider.GetUtcNow()), + new FriendProfileReadModel(otherSystemId, new Username(string.Empty), null, null, string.Empty, new DiscordId(string.Empty))) + : new FriendRequestSocketPayload( + matched.Request, + matched.System with { AvatarUrl = AvatarUrlQualifier.QualifyAvatar(matched.System, context.RequestOrigin) }); + + await context.SendAsync(topic, joinRef, asArray, eventName, payload); + } + + private static Task GetFriendshipWithRetryAsync( + IFriendshipRepository friendshipRepository, + SystemId targetSystemId, + SystemId friendSystemId, + CancellationToken cancellationToken) + => UntilNotNullAsync( + () => friendshipRepository.GetFriendshipAsync(targetSystemId, friendSystemId, cancellationToken), + maxAttempts: 3, + delayBetween: TimeSpan.FromMilliseconds(50), + cancellationToken); + + private static Task GetFriendRequestWithRetryAsync( + IFriendshipRepository friendshipRepository, + SystemId targetSystemId, + SystemId otherSystemId, + bool outgoing, + CancellationToken cancellationToken) + => UntilNotNullAsync( + async () => + { + var index = await friendshipRepository.GetFriendRequestsAsync(targetSystemId, cancellationToken); + return (outgoing ? index.Outgoing : index.Incoming) + .FirstOrDefault(r => SystemTopic.IdMatches(r.System?.Id, otherSystemId)); + }, + maxAttempts: 10, + delayBetween: TimeSpan.FromMilliseconds(100), + cancellationToken); + + /// + /// Poll up to times, sleeping + /// between attempts (never after the last), and return + /// the first non-null result or null if the deadline expires. Consolidates the + /// friendship-and-request eventual-consistency retry loops so the two socket-handler + /// paths can't drift on the "return null after the last attempt, don't wait after it" + /// invariant that a hand-rolled off-by-one on the delay branch could otherwise + /// re-introduce. Kept private — the socket handlers are the only caller, and callers + /// outside this file should use the domain-abstraction retries (Polly / RetryHandler) + /// rather than a naive fixed-delay poll. + /// + private static async Task UntilNotNullAsync( + Func> fetch, + int maxAttempts, + TimeSpan delayBetween, + CancellationToken cancellationToken) + where T : class + { + for (var attempt = 0; attempt < maxAttempts; attempt++) + { + var result = await fetch(); + if (result is not null) + { + return result; + } + + if (attempt < maxAttempts - 1) + { + await Task.Delay(delayBetween, cancellationToken); + } + } + + return null; + } + + private static async Task SendAsync(SystemId targetSystemId, string eventName, string payloadKey, SystemId payloadValue, SocketPushContext context) + { + ISocketPayload payload = payloadKey switch + { + SocketPayloadPropertyKeys.FriendId => new FriendIdSocketPayload(payloadValue), + SocketPayloadPropertyKeys.SystemId => new SystemIdSocketPayload(payloadValue), + _ => throw new InvalidOperationException($"Unrecognized socket payload key '{payloadKey}'.") + }; + + await context.SendIfJoinedAsync(targetSystemId, eventName, payload); + } + +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/FrontingSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/FrontingSocketEventHandlers.cs new file mode 100644 index 00000000..51e38bbb --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/FrontingSocketEventHandlers.cs @@ -0,0 +1,50 @@ +using Interfold.Alters.Contracts; +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Events; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class FrontingSocketEventHandlers +{ + public static Task HandleAsync(FrontingStartedEvent evt, SocketPushContext context, IFrontingRepository frontingRepository) + => HandleFrontUpsertAsync(evt.TargetSystemId, evt.FrontId, SocketEventNames.Fronting.Started, context, frontingRepository); + + public static async Task HandleAsync(FrontingEndedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Fronting.Ended, new AlterIdSocketPayload(evt.AlterId)); + } + + public static Task HandleAsync(FrontingSetEvent evt, SocketPushContext context, IFrontingRepository frontingRepository) + => HandleFrontUpsertAsync(evt.TargetSystemId, evt.FrontId, SocketEventNames.Fronting.Set, context, frontingRepository); + + public static Task HandleAsync(FrontingBulkUpdatedEvent evt, SocketPushContext context, IFrontingRepository frontingRepository) + => context.PushIfJoinedAsync, FrontsSocketPayload>( + evt.TargetSystemId, + SocketEventNames.Fronting.BulkUpdated, + ct => frontingRepository.ListActiveAsync(evt.TargetSystemId, ct), + fronts => new FrontsSocketPayload(fronts)); + + public static Task HandleAsync(FrontCommentUpdatedEvent evt, SocketPushContext context, IFrontingRepository frontingRepository) + => HandleFrontUpsertAsync(evt.TargetSystemId, evt.FrontId, SocketEventNames.Fronting.CommentUpdated, context, frontingRepository); + + public static async Task HandleAsync(FrontingPrimaryChangedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Fronting.PrimaryChanged, new AlterIdSocketPayload(evt.AlterId)); + } + + public static async Task HandleAsync(FrontDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Fronting.Deleted, new FrontIdSocketPayload(evt.FrontId)); + } + + private static Task HandleFrontUpsertAsync(SystemId systemId, FrontId frontId, string eventName, SocketPushContext context, IFrontingRepository frontingRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => frontingRepository.GetActiveByFrontIdAsync(systemId, frontId, ct), + front => new FrontSocketPayload(front)); +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/ImportSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/ImportSocketEventHandlers.cs new file mode 100644 index 00000000..2fdecfe8 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/ImportSocketEventHandlers.cs @@ -0,0 +1,31 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class ImportSocketEventHandlers +{ + public static Task HandleAsync(SimplyPluralImportCompletedEvent evt, SocketPushContext context) + => SendCompletedAsync(evt.TargetSystemId, SocketEventNames.Imports.SpComplete, evt.AlterCount, context); + + public static Task HandleAsync(SimplyPluralImportFailedEvent evt, SocketPushContext context) + => SendFailedAsync(evt.TargetSystemId, SocketEventNames.Imports.SpFailed, context); + + public static Task HandleAsync(PluralKitImportCompletedEvent evt, SocketPushContext context) + => SendCompletedAsync(evt.TargetSystemId, SocketEventNames.Imports.PkComplete, evt.AlterCount, context); + + public static Task HandleAsync(PluralKitImportFailedEvent evt, SocketPushContext context) + => SendFailedAsync(evt.TargetSystemId, SocketEventNames.Imports.PkFailed, context); + + private static async Task SendCompletedAsync(SystemId systemId, string eventName, int alterCount, SocketPushContext context) + { + await context.SendIfJoinedAsync(systemId, eventName, new ImportCompletedSocketPayload(alterCount)); + } + + private static async Task SendFailedAsync(SystemId systemId, string eventName, SocketPushContext context) + { + await context.SendIfJoinedAsync(systemId, eventName, new EmptyPayload()); + } +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/JournalSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/JournalSocketEventHandlers.cs new file mode 100644 index 00000000..051744cf --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/JournalSocketEventHandlers.cs @@ -0,0 +1,46 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class JournalSocketEventHandlers +{ + public static async Task HandleAsync(GlobalJournalEntryCreatedEvent evt, SocketPushContext context, IJournalRepository journalRepository) + => await HandleGlobalUpsertAsync(evt.TargetSystemId, evt.EntryId, SocketEventNames.Journals.GlobalCreated, context, journalRepository); + + public static async Task HandleAsync(GlobalJournalEntryUpdatedEvent evt, SocketPushContext context, IJournalRepository journalRepository) + => await HandleGlobalUpsertAsync(evt.TargetSystemId, evt.EntryId, SocketEventNames.Journals.GlobalUpdated, context, journalRepository); + + public static async Task HandleAsync(GlobalJournalEntryDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Journals.GlobalDeleted, new EntryDeletedSocketPayload(evt.EntryId)); + } + + public static async Task HandleAsync(AlterJournalEntryCreatedEvent evt, SocketPushContext context, IJournalRepository journalRepository) + => await HandleAlterUpsertAsync(evt.TargetSystemId, evt.EntryId, SocketEventNames.Journals.AlterCreated, context, journalRepository); + + public static async Task HandleAsync(AlterJournalEntryUpdatedEvent evt, SocketPushContext context, IJournalRepository journalRepository) + => await HandleAlterUpsertAsync(evt.TargetSystemId, evt.EntryId, SocketEventNames.Journals.AlterUpdated, context, journalRepository); + + public static async Task HandleAsync(AlterJournalEntryDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Journals.AlterDeleted, new EntryDeletedSocketPayload(evt.EntryId)); + } + + private static Task HandleGlobalUpsertAsync(SystemId systemId, EntryId entryId, string eventName, SocketPushContext context, IJournalRepository journalRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => journalRepository.GetGlobalAsync(systemId, entryId, ct), + entry => new GlobalJournalSocketPayload(entry)); + + private static Task HandleAlterUpsertAsync(SystemId systemId, EntryId entryId, string eventName, SocketPushContext context, IJournalRepository journalRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => journalRepository.GetAlterAsync(systemId, entryId, ct), + entry => new AlterJournalSocketPayload(entry)); +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/PollSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/PollSocketEventHandlers.cs new file mode 100644 index 00000000..5a7bf608 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/PollSocketEventHandlers.cs @@ -0,0 +1,29 @@ +using Interfold.Polls.Contracts; +using Interfold.Polls.Contracts.Events; +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class PollSocketEventHandlers +{ + public static async Task HandleAsync(PollCreatedEvent evt, SocketPushContext context, IPollRepository pollRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.PollId, SocketEventNames.Polls.Created, context, pollRepository); + + public static async Task HandleAsync(PollUpdatedEvent evt, SocketPushContext context, IPollRepository pollRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.PollId, SocketEventNames.Polls.Updated, context, pollRepository); + + public static async Task HandleAsync(PollDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Polls.Deleted, new PollDeletedSocketPayload(evt.PollId)); + } + + private static Task HandleUpsertAsync(SystemId systemId, PollId pollId, string eventName, SocketPushContext context, IPollRepository pollRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => pollRepository.GetAsync(systemId, pollId, ct), + poll => new PollSocketPayload(poll)); +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/SettingsSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/SettingsSocketEventHandlers.cs new file mode 100644 index 00000000..3d2d51cf --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/SettingsSocketEventHandlers.cs @@ -0,0 +1,101 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class SettingsSocketEventHandlers +{ + public static Task HandleAsync(SettingsFieldsChangedEvent evt, SocketPushContext context, ISettingsFieldRepository fieldRepository) + => context.PushIfJoinedAsync, SettingsFieldsUpdatedPayload>( + evt.TargetSystemId, + SocketEventNames.Settings.FieldsUpdated, + ct => fieldRepository.ListAsync(evt.TargetSystemId, ct), + fields => new SettingsFieldsUpdatedPayload(fields)); + + public static async Task HandleAsync( + SettingsProfileUpdatedEvent evt, + SocketPushContext context, + IAccountRepository accountRepository, + IAlterRepository alterRepository, + IFrontingRepository frontingRepository, + ISettingsFieldRepository settingsFieldRepository, + IEncryptionStateRepository encryptionStateRepository) + { + if (!context.TryGetSystemTopic(evt.TargetSystemId, out var topic, out var joinRef, out var asArray)) + { + return; + } + + var profileTask = accountRepository.GetPublicProfileAsync(evt.TargetSystemId, context.CancellationToken); + var altersTask = alterRepository.ListAsync(evt.TargetSystemId, context.CancellationToken); + var frontsTask = frontingRepository.ListActiveAsync(evt.TargetSystemId, context.CancellationToken); + var fieldsTask = settingsFieldRepository.ListAsync(evt.TargetSystemId, context.CancellationToken); + var encryptionTask = encryptionStateRepository.GetAsync(evt.TargetSystemId, context.CancellationToken); + await Task.WhenAll(profileTask, altersTask, frontsTask, fieldsTask, encryptionTask).ConfigureAwait(false); + + var profile = profileTask.Result; + + if (evt.EmitUsernameUpdated && profile?.Username is not null) + { + await context.SendAsync(topic, joinRef, asArray, SocketEventNames.Settings.UsernameUpdated, new SettingsUsernameUpdatedPayload(profile.Username.Value)); + } + + var selfData = WebSocketInitialization.BuildSelfReadModel( + evt.TargetSystemId, + profile, + altersTask.Result, + frontsTask.Result, + fieldsTask.Result, + encryptionTask.Result, + context.RequestOrigin); + + await context.SendAsync(topic, joinRef, asArray, SocketEventNames.Settings.SelfUpdated, new SettingsSelfUpdatedPayload(selfData)); + } + + public static Task HandleAsync(SettingsAccountDeletedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.AccountDeleted, context); + + public static Task HandleAsync(SettingsAltersWipedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.AltersWiped, context); + + public static Task HandleAsync(SettingsEncryptedDataWipedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.EncryptedDataWiped, context); + + public static Task HandleAsync(SettingsDiscordAccountUnlinkedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.DiscordAccountUnlinked, context); + + public static Task HandleAsync(SettingsAppleAccountUnlinkedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.AppleAccountUnlinked, context); + + public static Task HandleAsync(SettingsGoogleAccountUnlinkedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.GoogleAccountUnlinked, context); + + public static Task HandleAsync(SettingsTagsWipedSignalEvent evt, SocketPushContext context) + => HandleSignalAsync(evt.TargetSystemId, SocketEventNames.Settings.TagsWiped, context); + + private static async Task HandleSignalAsync(SystemId systemId, string eventName, SocketPushContext context) + { + await context.SendIfJoinedAsync(systemId, eventName, new EmptyPayload()); + } + + public static Task HandleAsync(SettingsDiscordAccountLinkedEvent evt, SocketPushContext context) + => SendLinkedAsync(evt.TargetSystemId, SocketEventNames.Settings.DiscordAccountLinked, new DiscordAccountLinkedPayload(evt.DiscordId), context); + + public static Task HandleAsync(SettingsGoogleAccountLinkedEvent evt, SocketPushContext context) + => SendLinkedAsync(evt.TargetSystemId, SocketEventNames.Settings.GoogleAccountLinked, new GoogleAccountLinkedPayload(evt.Email), context); + + public static Task HandleAsync(SettingsAppleAccountLinkedEvent evt, SocketPushContext context) + => SendLinkedAsync(evt.TargetSystemId, SocketEventNames.Settings.AppleAccountLinked, new AppleAccountLinkedPayload(evt.AppleId), context); + + private static async Task SendLinkedAsync(SystemId systemId, string eventName, ISocketPayload payload, SocketPushContext context) + { + await context.SendIfJoinedAsync(systemId, eventName, payload); + } +} diff --git a/apis/Interfold.Socket.Api/Socket/Handlers/TagSocketEventHandlers.cs b/apis/Interfold.Socket.Api/Socket/Handlers/TagSocketEventHandlers.cs new file mode 100644 index 00000000..5e018082 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/Handlers/TagSocketEventHandlers.cs @@ -0,0 +1,29 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Events; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Socket.Api.Socket.Handlers; + +public static class TagSocketEventHandlers +{ + public static async Task HandleAsync(TagCreatedEvent evt, SocketPushContext context, ITagRepository tagRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.TagId, SocketEventNames.Tags.Created, context, tagRepository); + + public static async Task HandleAsync(TagUpdatedEvent evt, SocketPushContext context, ITagRepository tagRepository) + => await HandleUpsertAsync(evt.TargetSystemId, evt.TagId, SocketEventNames.Tags.Updated, context, tagRepository); + + public static async Task HandleAsync(TagDeletedEvent evt, SocketPushContext context) + { + await context.SendIfJoinedAsync(evt.TargetSystemId, SocketEventNames.Tags.Deleted, new TagDeletedSocketPayload(evt.TagId)); + } + + private static Task HandleUpsertAsync(SystemId systemId, TagId tagId, string eventName, SocketPushContext context, ITagRepository tagRepository) + => context.PushIfJoinedAsync( + systemId, eventName, + ct => tagRepository.GetAsync(systemId, tagId, ct), + tag => new TagSocketPayload(tag)); +} diff --git a/apis/Interfold.Socket.Api/Socket/PhoenixInboundFrame.cs b/apis/Interfold.Socket.Api/Socket/PhoenixInboundFrame.cs new file mode 100644 index 00000000..9d84a351 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/PhoenixInboundFrame.cs @@ -0,0 +1,119 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket; + +/// Parsed inbound Phoenix frame — array ([join_ref, ref, topic, event, payload]) +/// or object ({topic, event, payload, ref, join_ref}). +/// records which so replies mirror the client's shape. Array-format null refs stay null; +/// object-format absent refs stay null while present ones normalise to string. +internal sealed record PhoenixInboundFrame( + string EventName, + string Topic, + JsonElement? Payload, + string? Reference, + string? JoinReference, + bool ReplyAsArrayFrame) +{ + public static bool TryParse(string frame, [NotNullWhen(true)] out PhoenixInboundFrame? result) + { + result = null; + + var trimmed = frame.TrimStart(); + + if (trimmed.StartsWith('[')) + { + try + { + using var arrayDoc = JsonDocument.Parse(frame); + var root = arrayDoc.RootElement; + if (root.ValueKind != JsonValueKind.Array || root.GetArrayLength() < 5) + { + return false; + } + + var joinRefElement = root[0]; + var refElement = root[1]; + var topicElement = root[2]; + var eventElement = root[3]; + + if (topicElement.ValueKind != JsonValueKind.String + || eventElement.ValueKind != JsonValueKind.String) + { + return false; + } + + result = new PhoenixInboundFrame( + EventName: eventElement.GetString() ?? string.Empty, + Topic: topicElement.GetString() ?? PhoenixEventNames.PhoenixTopic, + Payload: root[4].Clone(), + Reference: refElement.ValueKind == JsonValueKind.String ? refElement.GetString() : null, + JoinReference: joinRefElement.ValueKind == JsonValueKind.String ? joinRefElement.GetString() : null, + ReplyAsArrayFrame: true); + return true; + } + catch (JsonException) + { + return false; + } + } + + try + { + using var doc = JsonDocument.Parse(frame); + var root = doc.RootElement; + + if (root.ValueKind != JsonValueKind.Object) + { + return false; + } + + if (!root.TryGetProperty("event", out var eventProp) + || eventProp.ValueKind != JsonValueKind.String) + { + return false; + } + + var topic = PhoenixEventNames.PhoenixTopic; + if (root.TryGetProperty("topic", out var topicProp) + && topicProp.ValueKind == JsonValueKind.String) + { + topic = topicProp.GetString() ?? topic; + } + + JsonElement? payload = null; + if (root.TryGetProperty("payload", out var payloadProp)) + { + payload = payloadProp.Clone(); + } + + string? reference = null; + if (root.TryGetProperty("ref", out var refProp) + && refProp.ValueKind == JsonValueKind.String) + { + reference = refProp.GetString() ?? string.Empty; + } + + string? joinReference = null; + if (root.TryGetProperty("join_ref", out var joinRefProp) + && joinRefProp.ValueKind == JsonValueKind.String) + { + joinReference = joinRefProp.GetString() ?? string.Empty; + } + + result = new PhoenixInboundFrame( + EventName: eventProp.GetString() ?? string.Empty, + Topic: topic, + Payload: payload, + Reference: reference, + JoinReference: joinReference, + ReplyAsArrayFrame: false); + return true; + } + catch (JsonException) + { + return false; + } + } +} diff --git a/apis/Interfold.Socket.Api/Socket/SocketEventPumpRunner.cs b/apis/Interfold.Socket.Api/Socket/SocketEventPumpRunner.cs new file mode 100644 index 00000000..f9a5af1f --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/SocketEventPumpRunner.cs @@ -0,0 +1,116 @@ +using Interfold.Alters.Contracts.Events; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Contracts.Events; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts.Events; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Contracts.Events; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Socket.Api.Socket.Handlers; +using Interfold.Tags.Contracts.Events; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Socket.Api.Socket; + +public static class SocketEventPumpRunner +{ + public static Task RunAllAsync( + IClusterEventBus eventBus, + SocketPushContext context, + IFrontingRepository frontingRepository, + IAlterRepository alterRepository, + ITagRepository tagRepository, + ISettingsFieldRepository settingsFieldRepository, + IAccountRepository accountRepository, + IFriendshipRepository friendshipRepository, + IPollRepository pollRepository, + IJournalRepository journalRepository, + IEncryptionStateRepository encryptionStateRepository) + { + // All subscriptions filter on context.JoinedScopedSystemId (ITargetedClusterEvent + // → only this user's publishes fire). + return Task.WhenAll( + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context, frontingRepository)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context, frontingRepository)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context, frontingRepository)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context, frontingRepository)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FrontingSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => AlterSocketEventHandlers.HandleAsync(evt, context, alterRepository)), + SubscribeAsync(eventBus, context, evt => AlterSocketEventHandlers.HandleAsync(evt, context, alterRepository)), + SubscribeAsync(eventBus, context, evt => AlterSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => TagSocketEventHandlers.HandleAsync(evt, context, tagRepository)), + SubscribeAsync(eventBus, context, evt => TagSocketEventHandlers.HandleAsync(evt, context, tagRepository)), + SubscribeAsync(eventBus, context, evt => TagSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context, settingsFieldRepository)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context, accountRepository, alterRepository, frontingRepository, settingsFieldRepository, encryptionStateRepository)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context, friendshipRepository)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context, friendshipRepository)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context, friendshipRepository)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => FriendshipSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => PollSocketEventHandlers.HandleAsync(evt, context, pollRepository)), + SubscribeAsync(eventBus, context, evt => PollSocketEventHandlers.HandleAsync(evt, context, pollRepository)), + SubscribeAsync(eventBus, context, evt => PollSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context, journalRepository)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context, journalRepository)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context, journalRepository)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context, journalRepository)), + SubscribeAsync(eventBus, context, evt => JournalSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => SettingsSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => ImportSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => ImportSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => ImportSocketEventHandlers.HandleAsync(evt, context)), + SubscribeAsync(eventBus, context, evt => ImportSocketEventHandlers.HandleAsync(evt, context))); + } + + private static async Task SubscribeAsync( + IClusterEventBus eventBus, + SocketPushContext context, + Func handler) + where TEvent : class + { + // Wrap the per-event dispatch so a single handler exception can't kill the + // subscription (transient cancel from a retry Task.Delay, socket write during + // teardown, etc.). Cancellation still exits cleanly via MoveNextAsync. + await foreach (var evt in eventBus.SubscribeAsync(context.JoinedScopedSystemId, context.CancellationToken).ConfigureAwait(false)) + { + try + { + await handler(evt).ConfigureAwait(false); + } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + return; + } + catch (Exception ex) + { + context.Logger?.LogError( + ex, + "Socket push handler for {EventType} threw. Topics={Topics}", + typeof(TEvent).Name, + string.Join(",", context.JoinedTopics.Keys)); + } + } + } +} diff --git a/apis/Interfold.Socket.Api/Socket/SocketJoinRateLimiter.cs b/apis/Interfold.Socket.Api/Socket/SocketJoinRateLimiter.cs new file mode 100644 index 00000000..c6ae116c --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/SocketJoinRateLimiter.cs @@ -0,0 +1,31 @@ +using System.Collections.Concurrent; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Socket.Api.Socket; + +public sealed class SocketJoinRateLimiter(TimeProvider timeProvider) +{ + private readonly ConcurrentDictionary> _windows = new(StringComparer.Ordinal); + + public bool Allow(SystemId systemId) + { + var now = timeProvider.GetUtcNow(); + var queue = _windows.GetOrAdd(systemId, _ => new Queue()); + lock (queue) + { + var cutoff = now.AddSeconds(-1); + while (queue.Count > 0 && queue.Peek() < cutoff) + { + queue.Dequeue(); + } + + if (queue.Count >= 2) + { + return false; + } + + queue.Enqueue(now); + return true; + } + } +} \ No newline at end of file diff --git a/apis/Interfold.Socket.Api/Socket/SocketPushContext.cs b/apis/Interfold.Socket.Api/Socket/SocketPushContext.cs new file mode 100644 index 00000000..51f90c92 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/SocketPushContext.cs @@ -0,0 +1,115 @@ +using System.Collections.Concurrent; +using System.Net.WebSockets; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Socket.Api.Socket; + +public sealed class SocketPushContext +{ + public SocketPushContext( + WebSocket socket, + ScopedSystemId? joinedScopedSystemId, + ConcurrentDictionary joinedTopics, + ConcurrentDictionary topicJoinReference, + ConcurrentDictionary topicReplyAsArrayFrame, + SemaphoreSlim sendGate, + CancellationToken cancellationToken, + string? requestOrigin = null, + ILogger? logger = null, + TimeProvider? timeProvider = null) + { + Socket = socket; + JoinedScopedSystemId = joinedScopedSystemId; + JoinedTopics = joinedTopics; + TopicJoinReference = topicJoinReference; + TopicReplyAsArrayFrame = topicReplyAsArrayFrame; + SendGate = sendGate; + CancellationToken = cancellationToken; + RequestOrigin = requestOrigin; + Logger = logger; + TimeProvider = timeProvider ?? TimeProvider.System; + } + + public WebSocket Socket { get; } + + /// Scoped composite of the socket's principal, fed to + /// + /// so pump subscriptions filter on it. Null for anonymous / unjoined sockets (bus + /// treats null as "no filter"). + public ScopedSystemId? JoinedScopedSystemId { get; } + public ConcurrentDictionary JoinedTopics { get; } + public ConcurrentDictionary TopicJoinReference { get; } + public ConcurrentDictionary TopicReplyAsArrayFrame { get; } + public SemaphoreSlim SendGate { get; } + public CancellationToken CancellationToken { get; } + /// Origin of the upgrading HTTP request; used to qualify relative avatar URLs. + public string? RequestOrigin { get; } + + public ILogger? Logger { get; } + + /// Defaults to so unit-test scaffolds work; + /// production paths inject the DI singleton for time-freezing tests. + public TimeProvider TimeProvider { get; } + + public bool TryGetSystemTopic(SystemId systemId, out string topic, out string? joinRef, out bool asArray) + { + topic = new SystemTopic(systemId).ToWireString(); + if (!JoinedTopics.ContainsKey(topic)) + { + var matchedTopic = JoinedTopics.Keys.FirstOrDefault(t => + SystemTopic.TryParse(t, out var joined) + && SystemTopic.IdMatches(joined.Id, systemId)); + + if (matchedTopic is null) + { + joinRef = null; + asArray = false; + return false; + } + + topic = matchedTopic; + } + + TopicJoinReference.TryGetValue(topic, out joinRef); + TopicReplyAsArrayFrame.TryGetValue(topic, out asArray); + return true; + } + + public Task SendAsync(string topic, string? joinRef, bool asArray, string eventName, TPayload payload) + => WebSocketEvents.SendPhoenixPushAsync( + Socket, + topic, + joinRef, + eventName, + payload, + asArray, + CancellationToken, + SendGate); + + /// Sends when the socket has joined the target's + /// topic; no-op otherwise. + public Task SendIfJoinedAsync(SystemId targetSystemId, string eventName, TPayload payload) + { + if (!TryGetSystemTopic(targetSystemId, out var topic, out var joinRef, out var asArray)) + return Task.CompletedTask; + return SendAsync(topic, joinRef, asArray, eventName, payload); + } + + /// Fetches and pushes when both the topic is joined and + /// returns non-null. + public async Task PushIfJoinedAsync( + SystemId systemId, + string eventName, + Func> fetch, + Func wrap) + { + if (!TryGetSystemTopic(systemId, out var topic, out var joinRef, out var asArray)) + return; + + var entity = await fetch(CancellationToken).ConfigureAwait(false); + if (entity is null) + return; + + await SendAsync(topic, joinRef, asArray, eventName, wrap(entity)); + } +} diff --git a/apis/Interfold.Socket.Api/Socket/SocketWireKeys.cs b/apis/Interfold.Socket.Api/Socket/SocketWireKeys.cs new file mode 100644 index 00000000..98823f4e --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/SocketWireKeys.cs @@ -0,0 +1,20 @@ +namespace Interfold.Socket.Api.Socket; + +/// +/// Query-string parameter names on the WebSocket upgrade request. Wire-frozen — the +/// Kotlin client builds the upgrade URI with these exact spellings. +/// +internal static class SocketQueryKeys +{ + public const string Token = "token"; +} + +/// +/// JSON property names inside friendship push payloads, selecting which typed payload +/// record wraps the pushed system id. Wire-frozen — clients match on the member name. +/// +internal static class SocketPayloadPropertyKeys +{ + public const string FriendId = "friend_id"; + public const string SystemId = "system_id"; +} diff --git a/apis/Interfold.Socket.Api/Socket/SystemTopic.cs b/apis/Interfold.Socket.Api/Socket/SystemTopic.cs new file mode 100644 index 00000000..1f287e64 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/SystemTopic.cs @@ -0,0 +1,53 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Socket.Api.Socket; + +/// +/// Typed form of the Phoenix "system:{systemId}" topic. Phoenix frames keep raw +/// string topics on the wire; this type owns the parse/build rule and the +/// region-prefix-tolerant system-id comparison that was previously duplicated across +/// SocketPushContext and FriendshipSocketEventHandlers. +/// +internal readonly record struct SystemTopic(SystemId Id) +{ + public const string Prefix = "system:"; + + public string ToWireString() => $"{Prefix}{Id}"; + + public static bool TryParse(string? topic, out SystemTopic result) + { + if (!string.IsNullOrWhiteSpace(topic) + && topic.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) + && topic.Length > Prefix.Length) + { + result = new(new(topic[Prefix.Length..])); + return true; + } + + result = default; + return false; + } + + /// + /// Strips a legacy "region:" prefix (e.g. nam:abcdefg → abcdefg) + /// so ids can be compared across prefixed and unprefixed spellings. + /// + public static string ComparableId(string systemId) + => ScopedSystemId.StripRegionPrefix(systemId); + + /// Region-prefix-tolerant equality between two raw system-id spellings. + public static bool IdMatches(string? left, string? right) + { + if (string.Equals(left, right, StringComparison.Ordinal)) + { + return true; + } + + if (string.IsNullOrWhiteSpace(left) || string.IsNullOrWhiteSpace(right)) + { + return false; + } + + return string.Equals(ComparableId(left), ComparableId(right), StringComparison.Ordinal); + } +} diff --git a/apis/Interfold.Socket.Api/Socket/WebSocketEvents.cs b/apis/Interfold.Socket.Api/Socket/WebSocketEvents.cs new file mode 100644 index 00000000..a2a2f633 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/WebSocketEvents.cs @@ -0,0 +1,51 @@ +using System.Net.WebSockets; +using System.Text.Json; +using Interfold.Socket.Contracts; + +namespace Interfold.Socket.Api.Socket; + +public static class WebSocketEvents +{ + +public static async Task SendPhoenixPushAsync( + WebSocket socket, + string topic, + string? joinReference, + string eventName, + TPayload payload, + bool replyAsArrayFrame, + CancellationToken cancellationToken, + SemaphoreSlim? sendGate = null) +{ + var bytes = replyAsArrayFrame + ? PhxArrayFrame.CreateBytes(joinReference, null, topic, eventName, payload) + : new PhxFrame + { + Topic = topic, + Event = eventName, + Payload = payload, + Ref = null, + JoinRef = joinReference + }.ToBytes(); + + if (sendGate is not null) + { + await sendGate.WaitAsync(cancellationToken); + try + { + await socket.SendAsync(bytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); + } + finally + { + sendGate.Release(); + } + } + else + { + await socket.SendAsync(bytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); + } +} + +public static string SerializeSocketJson(this T value) => JsonSerializer.Serialize(value, SocketJson.Options); + +} \ No newline at end of file diff --git a/apis/Interfold.Socket.Api/Socket/WebSocketHandler.cs b/apis/Interfold.Socket.Api/Socket/WebSocketHandler.cs new file mode 100644 index 00000000..7e93caf2 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/WebSocketHandler.cs @@ -0,0 +1,787 @@ +using System.IdentityModel.Tokens.Jwt; +using System.Net.WebSockets; +using System.Security.Claims; +using System.Text; +using System.Text.Json; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Auth.Api.Auth; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Socket.Api.Helpers; +using Interfold.Socket.Api.Models; +using Interfold.Socket.Contracts; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Microsoft.Extensions.Options; +using Microsoft.IdentityModel.Tokens; + +namespace Interfold.Socket.Api.Socket; + +public static class WebSocketHandler +{ +public static async Task HandleUserSocketAsync(HttpContext context) +{ + var logger = context.RequestServices.GetRequiredService() + .CreateLogger("WebSocketHandler"); + + logger.LogInformation( + "WebSocket request received. Method: {Method}, Path: {Path}, IsWebSocketRequest: {IsWSRequest}", + context.Request.Method, + context.Request.Path, + context.WebSockets.IsWebSocketRequest); + + if (!context.WebSockets.IsWebSocketRequest) + { + logger.LogWarning("Request is not a WebSocket upgrade request"); + context.Response.StatusCode = StatusCodes.Status400BadRequest; + await context.Response.WriteAsJsonAsync( + new ErrorResponse("WebSocket upgrade required.", ErrorCodes.WebSocketUpgradeRequired)); + return; + } + + // Wrap at the boundary so accidental $"{token}" interpolations go through SocketToken.ToString() + // and get redacted. .Value is unwrapped only where the raw JWT is unavoidable. + SocketToken token = new(context.Request.Query[SocketQueryKeys.Token].ToString()); + logger.LogInformation("Token from query string length: {TokenLength}", token.Value.Length); + + if (string.IsNullOrWhiteSpace(token.Value)) + { + logger.LogWarning("Missing or empty token in query string"); + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + await context.Response.WriteAsJsonAsync( + new ErrorResponse("Missing socket token.", ErrorCodes.MissingSocketToken)); + return; + } + + using var socket = await context.WebSockets.AcceptWebSocketAsync(); + + var batchedInitThresholdBytes = context.RequestServices + .GetRequiredService>() + .CurrentValue.BatchBytesThreshold ?? 1_048_576; + var buffer = new byte[1024 * 16]; + var joinedTopics = new System.Collections.Concurrent.ConcurrentDictionary(StringComparer.Ordinal); + SystemId? joinedSystemId = null; + var topicReplyAsArrayFrame = new System.Collections.Concurrent.ConcurrentDictionary(StringComparer.Ordinal); + var topicJoinReference = new System.Collections.Concurrent.ConcurrentDictionary(StringComparer.Ordinal); + using var sendGate = new SemaphoreSlim(1, 1); + using var pushCts = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); + var eventBus = context.RequestServices.GetRequiredService(); + var rateLimiter = context.RequestServices.GetRequiredService(); + var frontingRepository = context.RequestServices.GetRequiredService(); + var alterRepository = context.RequestServices.GetRequiredService(); + var tagRepository = context.RequestServices.GetRequiredService(); + var settingsFieldRepository = context.RequestServices.GetRequiredService(); + var accountRepository = context.RequestServices.GetRequiredService(); + var friendshipRepository = context.RequestServices.GetRequiredService(); + var pollRepository = context.RequestServices.GetRequiredService(); + var journalRepository = context.RequestServices.GetRequiredService(); + var encryptionStateRepository = context.RequestServices.GetRequiredService(); + var timeProvider = context.RequestServices.GetRequiredService(); + var requestOrigin = $"{context.Request.Scheme}://{context.Request.Host}"; + + // The per-socket event pump is started on the first successful phx_join (see below). + // Deferring registration keeps each socket invisible to the bus until it actually + // identifies a target system; without it, every just-accepted socket added ~38 broadcast + // writers to the bus and the publisher had to fan every event out to all of them. + Task? socketPushTask = null; + // 0 = pump not started, 1 = started. Mutated only via Interlocked.CompareExchange so a + // burst of phx_join frames that arrives back-to-back (e.g. retried by the client during + // a flaky connection) can never race past the guard and spin up a second pump. + var pumpStarted = 0; + + while (socket.State == WebSocketState.Open) + { + var incomingText = await ReceiveSocketTextAsync(socket, buffer, context.RequestAborted); + if (incomingText is null) + { + break; + } + + if (!PhoenixInboundFrame.TryParse(incomingText, out var frame)) + { + // Unrecognised frame; close with a protocol-error code rather than + // echoing raw JSON (which is itself not a valid Phoenix frame). + await socket.CloseAsync( + WebSocketCloseStatus.InvalidPayloadData, + "invalid_phoenix_frame", + context.RequestAborted); + break; + } + + var (eventName, topic, payload, reference, joinReference, replyAsArrayFrame) = frame; + + if (string.Equals(eventName, PhoenixEventNames.Heartbeat, StringComparison.OrdinalIgnoreCase)) + { + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Ok, + response: new EmptyPayload(), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + continue; + } + + if (string.Equals(eventName, PhoenixEventNames.Join, StringComparison.OrdinalIgnoreCase)) + { + // Deserialize the typed join payload. Per-property tolerance for the ONE + // field that historically saw stray wire spellings — the platform enum — + // is enforced by TolerantWireEnumJsonConverter on PhxJoinPayload.Platform, + // so an unknown platform value round-trips to null WITHOUT throwing a + // JsonException that would take the sibling token / protocolVersion down + // with it. The outer try/catch stays as a belt-and-braces guard for + // wholly-malformed payloads (e.g. `payload: 42`) where "keep defaults" + // is a safer floor than crashing the socket loop, but a valid join with + // an unknown platform must never hit this catch — that path would zero + // out the token and turn the reply into a bogus Unauthorized (regression + // pinned by Api_UserSocketEndpoint_AllowsWebSocketUpgrade). + var joinPayload = new PhxJoinPayload(); + if (payload?.ValueKind == JsonValueKind.Object) + { + try + { + joinPayload = payload.Value.Deserialize(SocketJson.Options) ?? new PhxJoinPayload(); + } + catch (JsonException) + { + // keep defaults + } + } + + var payloadToken = joinPayload.Token; + var isReconnect = joinPayload.IsReconnect ?? false; + var forceBatch = joinPayload.ForceBatch ?? false; + var platform = joinPayload.Platform; + var protocolVersion = new Version(1, 0, 0); + var protocolSupported = joinPayload.ProtocolVersion is null + || TryParseLooseVersion(joinPayload.ProtocolVersion, out protocolVersion); + + var isSystemTopic = SystemTopic.TryParse(topic, out var requestedTopic); + // SystemId? mirrors the joinedSystemId idiom above — null means "no system + // topic on this join" and gates the downstream sub-vs-topic comparison. + SystemId? requestedSystemId = isSystemTopic ? requestedTopic.Id : null; + // scopedSub is the ScopedSystemId? parsed from the JWT sub inside the helper. + // Feeding it into SocketPushContext.JoinedScopedSystemId lets the event-pump + // subscribe with the scoped composite (matching every + // ITargetedClusterEvent.TargetSystemId), so the bus PublishAsync filter can + // compare scoped-to-scoped directly. + var (tokenAuthorized, tokenAuthFailureReason, scopedSub) = await IsSocketJoinTokenAuthorizedAsync( + context, + token, + requestedSystemId, + context.RequestAborted); + + if (!protocolSupported) + { + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Error, + response: new SocketReasonResponse(ErrorCodes.SocketReasons.UnsupportedProtocolVersion), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + } + else if (isSystemTopic + && payloadToken == token + && tokenAuthorized) + { + if (!rateLimiter.Allow(requestedTopic.Id)) + { + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Error, + response: new SocketReasonResponse(ErrorCodes.SocketReasons.RateLimited), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + continue; + } + + joinedTopics[topic] = 0; + joinedSystemId = requestedTopic.Id; + topicReplyAsArrayFrame[topic] = replyAsArrayFrame; + topicJoinReference[topic] = joinReference; + + // Start the per-socket event pump now that we know which system this socket is bound to. + // The bus filter only delivers events whose TargetSystemId matches + // JoinedScopedSystemId (scoped-to-scoped record-struct equality), so the + // pump's ~38 subscriptions only see traffic for this user. + // + // Interlocked.CompareExchange flips pumpStarted from 0 to 1 atomically and returns the + // previous value; only the thread that observed 0 actually constructs the push context + // and starts the pump. A subsequent phx_join on the same socket simply observes 1 and + // becomes a no-op rather than spinning a second pump / second SocketPushContext. + if (Interlocked.CompareExchange(ref pumpStarted, 1, 0) == 0) + { + // scopedSub (from IsSocketJoinTokenAuthorizedAsync) threads through + // SocketPushContext into every SocketEventPumpRunner subscription, so + // the bus filter compares scoped-to-scoped without a runtime + // StripRegionPrefix normalisation on either side. + var socketPushContext = new SocketPushContext( + socket, + scopedSub, + joinedTopics, + topicJoinReference, + topicReplyAsArrayFrame, + sendGate, + pushCts.Token, + requestOrigin: requestOrigin, + logger: logger, + timeProvider: timeProvider); + + socketPushTask = SocketEventPumpRunner.RunAllAsync( + eventBus, + socketPushContext, + frontingRepository, + alterRepository, + tagRepository, + settingsFieldRepository, + accountRepository, + friendshipRepository, + pollRepository, + journalRepository, + encryptionStateRepository); + } + + var initPayload = await WebSocketInitialization.BuildJoinInitPayloadAsync(context, joinedSystemId.Value, context.RequestAborted); + var useBatchedInit = false; + + if (!isReconnect) + { + var estimatedEncodedBytes = (int)(Encoding.UTF8.GetByteCount(WebSocketEvents.SerializeSocketJson(initPayload)) * 1.1); + useBatchedInit = forceBatch + || (platform == ClientPlatform.Ios + && estimatedEncodedBytes > batchedInitThresholdBytes + && protocolVersion >= new Version(2, 0, 0)); + } + + // Deliberately `object`, not ISocketPayload: System.Text.Json serializes + // interface-declared values by the interface's (empty) member set, while + // `object` triggers runtime-type serialization — which is what puts the + // payload's real properties on the wire. + object joinResponse; + if (isReconnect) + { + joinResponse = new SocketJoinReconnectPayload(initPayload.System); + } + else if (useBatchedInit) + { + joinResponse = new SocketJoinBatchedPayload( + Batched: true, + System: initPayload.System, + Alters: null, + Fronts: null, + Tags: null); + } + else + { + joinResponse = initPayload; + } + + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Ok, + response: joinResponse, + replyAsArrayFrame, + context.RequestAborted, + sendGate); + + if (useBatchedInit) + { + await WebSocketInitialization.SendBatchedInitAsync( + socket, + topic, + topicJoinReference[topic], + topicReplyAsArrayFrame[topic], + initPayload, + context.RequestAborted, + sendGate); + } + } + else + { + var unauthorizedReason = tokenAuthFailureReason ?? ErrorCodes.SocketReasons.Unauthorized; + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Error, + response: new SocketReasonResponse(unauthorizedReason), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + await socket.CloseAsync(WebSocketCloseStatus.PolicyViolation, unauthorizedReason.Value, context.RequestAborted); + } + + continue; + } + + if (string.Equals(eventName, PhoenixEventNames.Endpoint, StringComparison.OrdinalIgnoreCase)) + { + if (!joinedTopics.ContainsKey(topic)) + { + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Error, + response: new SocketReasonResponse(ErrorCodes.SocketReasons.NotJoined), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + continue; + } + + var endpointResult = await HandleEndpointProxyAsync(context, payload, token, joinedSystemId); + + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Ok, + response: endpointResult, + replyAsArrayFrame, + context.RequestAborted, + sendGate); + continue; + } + + await SendPhoenixReplyAsync( + socket, + topic, + reference, + joinReference, + status: PhoenixReplyStatus.Error, + response: new SocketReasonResponse(ErrorCodes.SocketReasons.EventNotImplemented), + replyAsArrayFrame, + context.RequestAborted, + sendGate); + } + + await pushCts.CancelAsync(); + if (socketPushTask is not null) + { + try + { + await socketPushTask; + } + catch (OperationCanceledException) + { + // Expected on socket shutdown. + } + } + + if (socket.State is WebSocketState.Open or WebSocketState.CloseReceived) + { + await socket.CloseAsync( + WebSocketCloseStatus.NormalClosure, + "socket closed", + CancellationToken.None); + } +} + +static async Task HandleEndpointProxyAsync( + HttpContext websocketContext, + JsonElement? payload, + SocketToken socketToken, + SystemId? joinedSystemId) +{ + if (payload is null || payload.Value.ValueKind != JsonValueKind.Object) + { + return new SocketEndpointProxyResponse( + System.Net.HttpStatusCode.BadRequest, + ToJsonString(new ErrorResponse( + "Invalid endpoint payload.", + ErrorCodes.SocketEndpointPayloadInvalid, + System.Net.HttpStatusCode.BadRequest))); + } + + SocketEndpointProxyRequest? proxyRequest; + try + { + proxyRequest = payload.Value.Deserialize(SocketJson.Options); + } + catch (JsonException) + { + proxyRequest = null; + } + + var method = proxyRequest?.Method ?? string.Empty; + var path = proxyRequest?.Path ?? string.Empty; + + if (string.IsNullOrWhiteSpace(method) || string.IsNullOrWhiteSpace(path)) + { + return new SocketEndpointProxyResponse( + System.Net.HttpStatusCode.BadRequest, + ToJsonString(new ErrorResponse( + "Endpoint payload must include method and path.", + ErrorCodes.SocketEndpointMethodPathRequired, + System.Net.HttpStatusCode.BadRequest))); + } + + if (!path.StartsWith("/api", StringComparison.OrdinalIgnoreCase)) + { + return new SocketEndpointProxyResponse( + System.Net.HttpStatusCode.Forbidden, + ToJsonString(new ErrorResponse( + "Socket endpoint relay is restricted to /api paths.", + ErrorCodes.SocketEndpointPathForbidden, + System.Net.HttpStatusCode.Forbidden))); + } + + // Self-call the API's own Kestrel listener rather than dialing back through + // `Request.Scheme`/`Request.Host`. In the published docker compose stack the + // request arrives via the operator-facing hostname + host-mapped port (e.g. + // `https://api.example.com:5001`), but neither is reachable from inside the + // container: the hostname is rarely in the container's DNS namespace, and the + // host-side port is the OUTSIDE of the compose port mapping (the container + // itself listens on `ASPNETCORE_HTTP_PORTS` / `ASPNETCORE_HTTPS_PORTS`, + // default 5100/5101, configurable via `Ports:api-container-http(s)`). + // Resolving via IServerAddressesFeature picks up whatever Kestrel actually + // bound to in this process, regardless of the deployment topology in front + // of it. We prefer the HTTPS binding so the call doesn't trip + // HttpsRedirection (which would 308 us at the HTTPS listener anyway), and + // dial it through the named `LoopbackHttpClient` whose permissive TLS + // validator accepts the local leaf cert despite the SAN/chain mismatches — + // see `LoopbackHttpClient` for the safety argument. + var server = websocketContext.RequestServices.GetRequiredService(); + var baseUri = ResolveLoopbackBaseUri(server.Features.Get()?.Addresses); + var targetUri = $"{baseUri}{path}"; + + if (!Uri.TryCreate(targetUri, UriKind.Absolute, out var parsedTargetUri) + || (parsedTargetUri.Scheme is "https" && !LoopbackHttpClient.IsLoopbackHost(parsedTargetUri))) + { + // Defence-in-depth: ResolveLoopbackBaseUri is unit-tested to always emit a loopback + // shape (or the TestServer fallback `http://localhost`), but the named loopback + // HttpClient skips TLS validation, so a future regression that let a non-loopback + // host through here would silently weaken every self-call. Fail fast instead. + return new SocketEndpointProxyResponse( + System.Net.HttpStatusCode.InternalServerError, + ToJsonString(new ErrorResponse( + "Socket endpoint relay resolved a non-loopback target.", + ErrorCodes.SocketEndpointProxyMisrouted, + System.Net.HttpStatusCode.InternalServerError))); + } + + using var request = new HttpRequestMessage(new HttpMethod(method), parsedTargetUri); + // Forward the outer Host so the inner controller's `Request.Host` is the operator-facing + // origin, not the loopback dial target — anything reading `Request.Host` for URL + // qualification (`QualifyAvatar`, OAuth callbacks) would otherwise emit unreachable URLs. + request.Headers.Host = websocketContext.Request.Host.Value; + // Wire-format unwrap: this is the single site in the file (paired with the JWT + // framework calls in IsSocketJoinTokenAuthorizedAsync) where SocketToken.Value is + // exposed. Interpolating the wrapper itself would emit the redacted `abcd…` form and + // authentication would fail — the actual JWT must go on the wire verbatim. + request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {socketToken.Value}"); + request.Headers.TryAddWithoutValidation("Accept", "application/json"); + + if (joinedSystemId is { } principal && !string.IsNullOrWhiteSpace(principal.Value)) + { + request.Headers.TryAddWithoutValidation(InterfoldHeaders.Principal, principal.Value); + } + + // Body is deserialized straight from the payload's JSON string (Phoenix carries + // the inner request body as a JSON-string field so we forward it byte-identical + // without re-serializing). + if (!string.IsNullOrWhiteSpace(proxyRequest?.Body) + && method is not "GET" and not "HEAD") + { + request.Content = new StringContent(proxyRequest.Body, Encoding.UTF8, "application/json"); + } + + var httpClientFactory = websocketContext.RequestServices.GetRequiredService(); + using var httpClient = httpClientFactory.CreateClient(LoopbackHttpClient.Name); + + var response = await httpClient.SendAsync(request, websocketContext.RequestAborted); + try + { + var responseBody = await response.Content.ReadAsStringAsync(websocketContext.RequestAborted); + return new SocketEndpointProxyResponse(response.StatusCode, responseBody); + } + finally + { + response.Dispose(); + } +} + +static string ToJsonString(T value) + => JsonSerializer.Serialize(value, SocketJson.Options); + +/// Loopback-safe base URL for the socket relay's self-call. Prefers https to skip +/// UseHttpsRedirection; wildcard hosts (0.0.0.0/[::]/+/*) → 127.0.0.1; empty addresses → +/// http://localhost so TestServer stays functional. HTTPS TLS validation is handled by +/// 's permissive validator (safe: loopback-only target). +internal static string ResolveLoopbackBaseUri(ICollection? addresses) +{ + const string testServerFallback = "http://localhost"; + if (addresses is null || addresses.Count == 0) return testServerFallback; + + var preferred = addresses + .OrderBy(static addr => addr.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? 0 : 1) + .FirstOrDefault(); + if (preferred is null) return testServerFallback; + + return preferred.TrimEnd('/') + .Replace("://0.0.0.0", "://127.0.0.1", StringComparison.Ordinal) + .Replace("://[::]", "://127.0.0.1", StringComparison.Ordinal) + .Replace("://+", "://127.0.0.1", StringComparison.Ordinal) + .Replace("://*", "://127.0.0.1", StringComparison.Ordinal); +} + +/// Region-prefix-tolerant equality between the scoped JWT sub and a possibly-raw +/// socket topic id. Third of three sites (after middleware and InProcessEventBus) +/// that must agree on "same principal"; scoped-sub type argument forces callers through the +/// same TryParseScoped gate the middleware uses. +internal static bool IsTokenSubjectAuthorizedForTopic( + ScopedSystemId? tokenSubject, + SystemId? requestedSystemId) +{ + if (tokenSubject is null + || requestedSystemId is null + || string.IsNullOrWhiteSpace(requestedSystemId.Value.Value)) + { + return false; + } + + return string.Equals( + tokenSubject.Value.RawId, + ScopedSystemId.StripRegionPrefix(requestedSystemId.Value.Value), + StringComparison.Ordinal); +} + +// Returns the parsed ScopedSystemId so HandleAsync can populate SocketPushContext without +// re-parsing the JWT. +static async Task<(bool IsAuthorized, ErrorCode? FailureReason, ScopedSystemId? TokenSubject)> IsSocketJoinTokenAuthorizedAsync( + HttpContext context, + SocketToken token, + SystemId? requestedSystemId, + CancellationToken cancellationToken) +{ + var authConfig = context.RequestServices + .GetRequiredService>().CurrentValue; + + var logger = context.RequestServices.GetRequiredService() + .CreateLogger("WebSocketTokenAuth"); + + if (string.IsNullOrWhiteSpace(token.Value)) + { + logger.LogWarning("Token is empty or whitespace"); + return (false, ErrorCodes.SocketReasons.MissingSocketToken, null); + } + + logger.LogInformation("Validating token. RequestedSystemId: {SystemId}", requestedSystemId); + + // .Value unwraps are the JWT framework survival points; everything else uses the + // redacted-by-default wrapper. + var handler = new JwtSecurityTokenHandler { MapInboundClaims = false }; + if (!handler.CanReadToken(token.Value)) + { + logger.LogWarning("Handler cannot read token"); + return (false, ErrorCodes.SocketReasons.InvalidSocketToken, null); + } + + logger.LogInformation("Token is readable. Verification key count: {KeyCount}", + authConfig.JwtEs256VerificationKeyPems?.Length ?? 0); + + var parameters = new TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidAudience = authConfig.JwtAudience, + ValidateLifetime = true, + RequireExpirationTime = true, + ClockSkew = TimeSpan.FromMinutes(1), + ValidateIssuerSigningKey = false, + RequireSignedTokens = true, + // Framework hands the raw string via this callback; forward to the ES256 verifier. + SignatureValidator = (frameworkRawToken, validationParameters) => + ValidateJwtTokenSignatureForSocket(frameworkRawToken, validationParameters, authConfig), + NameClaimType = JwtClaimNames.Sub + }; + + try + { + logger.LogInformation("Starting token validation"); + var principal = handler.ValidateToken(token.Value, parameters, out _); + var tokenSub = principal.FindFirstValue(JwtClaimNames.Sub); + + logger.LogInformation("Token validated. TokenSystemId: {TokenSub}, RequestedSystemId: {RequestedSub}", + tokenSub, requestedSystemId); + + // Same parse rejection matrix as InterfoldPrincipalMiddleware.ResolvePrincipalId + // so an unscoped-sub token can't authorise a socket join it would fail on HTTP. + if (!ScopedSystemId.TryParseScoped(tokenSub, out var scopedSub)) + { + logger.LogWarning("Token subject (sub) claim is missing, unscoped, or has an unknown region prefix"); + return (false, ErrorCodes.SocketReasons.InvalidSocketTokenSubject, null); + } + + if (!IsTokenSubjectAuthorizedForTopic(scopedSub, requestedSystemId)) + { + logger.LogWarning("Token subject does not match requested system ID"); + return (false, ErrorCodes.SocketReasons.UnauthorizedTopic, null); + } + + // Wrap so log sites route through Jti.ToString redaction. + var jti = Jti.From(principal.FindFirstValue(JwtClaimNames.Jti)); + if (jti is { } typedJti) + { + var revocationRepository = context.RequestServices + .GetRequiredService(); + var isTokenValid = await revocationRepository.ValidateTokenNotRevokedAsync(typedJti, cancellationToken); + if (!isTokenValid) + { + logger.LogWarning("Token has been revoked. JTI: {Jti}", typedJti); + return (false, ErrorCodes.SocketReasons.TokenRevoked, null); + } + } + + logger.LogInformation("Token authorization successful"); + return (true, null, scopedSub); + } + catch (Exception ex) + { + logger.LogWarning(ex, "WebSocket token validation failed: {ExceptionMessage}", ex.Message); + return (false, ErrorCodes.SocketReasons.InvalidSocketToken, null); + } +} + +static async Task ReceiveSocketTextAsync(WebSocket socket, byte[] buffer, CancellationToken cancellationToken) +{ + using var messageStream = new MemoryStream(); + + while (socket.State == WebSocketState.Open) + { + WebSocketReceiveResult received; + try + { + received = await socket.ReceiveAsync(buffer, cancellationToken); + } + catch (OperationCanceledException) + { + return null; + } + + if (received.MessageType == WebSocketMessageType.Close) + { + return null; + } + + if (received.MessageType != WebSocketMessageType.Text) + { + return null; + } + + await messageStream.WriteAsync(buffer.AsMemory(0, received.Count), cancellationToken); + + if (received.EndOfMessage) + { + return Encoding.UTF8.GetString(messageStream.ToArray()); + } + } + + return null; +} + +static bool TryParseLooseVersion(string? value, out Version parsed) +{ + parsed = new Version(1, 0, 0); + if (string.IsNullOrWhiteSpace(value)) + { + return true; + } + + var normalized = value.Trim(); + if (Version.TryParse(normalized, out parsed!)) + { + return true; + } + + // Phoenix clients typically use semver strings like "2.0.0"; + // allow a trailing prerelease segment by stripping from '-'. + var dashIndex = normalized.IndexOf('-'); + if (dashIndex > 0) + { + var stable = normalized[..dashIndex]; + return Version.TryParse(stable, out parsed!); + } + + return false; +} + +static SecurityToken ValidateJwtTokenSignatureForSocket( + string token, + TokenValidationParameters validationParameters, + AuthenticationConfiguration config) +{ + return JwtEs256Validator.ValidateSignature(token, config.JwtEs256VerificationKeyPems ?? [], useJsonWebToken: false); +} + + static async Task SendPhoenixReplyAsync( + WebSocket socket, + string topic, + string? reference, + string? joinReference, + PhoenixReplyStatus status, + TResponse response, + bool replyAsArrayFrame, + CancellationToken cancellationToken, + SemaphoreSlim? sendGate = null) + { + var payload = new PhoenixReplyPayload(status, response); + var bytes = replyAsArrayFrame + ? PhxArrayFrame.CreateBytes(joinReference, reference, topic, PhoenixEventNames.Reply, payload) + : new PhxFrame> + { + Topic = topic, + Event = PhoenixEventNames.Reply, + Payload = payload, + Ref = reference, + JoinRef = joinReference + }.ToBytes(); + + if (sendGate is not null) + { + await sendGate.WaitAsync(cancellationToken); + try + { + await socket.SendAsync(bytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); + } + finally + { + sendGate.Release(); + } + } + else + { + await socket.SendAsync(bytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); + } +} +} \ No newline at end of file diff --git a/apis/Interfold.Socket.Api/Socket/WebSocketInitialization.cs b/apis/Interfold.Socket.Api/Socket/WebSocketInitialization.cs new file mode 100644 index 00000000..b0e91551 --- /dev/null +++ b/apis/Interfold.Socket.Api/Socket/WebSocketInitialization.cs @@ -0,0 +1,211 @@ +using System.Net.WebSockets; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Socket.Api.Models; +using Interfold.Socket.Contracts; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Socket.Api.Socket; + +public class WebSocketInitialization +{ +public static async Task SendBatchedInitAsync( + WebSocket socket, + string topic, + string? joinReference, + bool replyAsArrayFrame, + SocketJoinInitPayload initPayload, + CancellationToken cancellationToken, + SemaphoreSlim? sendGate = null) +{ + await SendBatchedDataAsync( + socket, + topic, + joinReference, + replyAsArrayFrame, + SocketEventNames.BatchedInit.Alters, + 3000, + initPayload.Alters, + (batchIndex, totalBatches, batch) => new SocketBatchedAltersPayload(batchIndex, totalBatches, batch), + cancellationToken, + sendGate); + await SendBatchedDataAsync( + socket, + topic, + joinReference, + replyAsArrayFrame, + SocketEventNames.BatchedInit.Tags, + 1000, + initPayload.Tags, + (batchIndex, totalBatches, batch) => new SocketBatchedTagsPayload(batchIndex, totalBatches, batch), + cancellationToken, + sendGate); + await SendBatchedDataAsync( + socket, + topic, + joinReference, + replyAsArrayFrame, + SocketEventNames.BatchedInit.Fronts, + 50, + initPayload.Fronts, + (batchIndex, totalBatches, batch) => new SocketBatchedFrontsPayload(batchIndex, totalBatches, batch), + cancellationToken, + sendGate); + + await WebSocketEvents.SendPhoenixPushAsync( + socket, + topic, + joinReference, + eventName: SocketEventNames.BatchedInit.Complete, + payload: new EmptyPayload(), + replyAsArrayFrame, + cancellationToken, + sendGate); +} + +static async Task SendBatchedDataAsync( + WebSocket socket, + string topic, + string? joinReference, + bool replyAsArrayFrame, + string eventName, + int batchSize, + IReadOnlyList data, + Func, TPayload> payloadFactory, + CancellationToken cancellationToken, + SemaphoreSlim? sendGate = null) +{ + if (data.Count == 0) + { + return; + } + + var totalBatches = (int)Math.Ceiling((double)data.Count / batchSize); + for (var i = 0; i < totalBatches; i++) + { + if (i > 0) + { + await Task.Delay(50, cancellationToken); + } + + var start = i * batchSize; + var count = Math.Min(batchSize, data.Count - start); + var batchItems = data.Skip(start).Take(count).ToArray(); + + await WebSocketEvents.SendPhoenixPushAsync( + socket, + topic, + joinReference, + eventName, + payloadFactory(i + 1, totalBatches, batchItems), + replyAsArrayFrame, + cancellationToken, + sendGate); + } +} + +static async Task<( + AccountPublicProfileReadModel? profile, + IReadOnlyList alters, + IReadOnlyList fronts, + IReadOnlyList tags, + IReadOnlyList settingsFields, + EncryptionState? encryptionState)> +FetchSocketInitDataAsync( + SystemId systemId, + IAccountRepository accounts, + IAlterRepository alters, + IFrontingRepository fronting, + ITagRepository tags, + ISettingsFieldRepository settingsFields, + IEncryptionStateRepository encryptionStates, + CancellationToken ct) +{ + var profileTask = accounts.GetPublicProfileAsync(systemId, ct); + var altersTask = alters.ListAsync(systemId, ct); + var frontsTask = fronting.ListActiveAsync(systemId, ct); + var tagsTask = tags.ListAsync(systemId, ct); + var fieldsTask = settingsFields.ListAsync(systemId, ct); + var encryptionTask = encryptionStates.GetAsync(systemId, ct); + await Task.WhenAll(profileTask, altersTask, frontsTask, tagsTask, fieldsTask, encryptionTask); + return (profileTask.Result, altersTask.Result, frontsTask.Result, tagsTask.Result, fieldsTask.Result, encryptionTask.Result); +} + +public static async Task BuildJoinInitPayloadAsync( + HttpContext context, + SystemId systemId, + CancellationToken ct) +{ + await using var scope = context.RequestServices.CreateAsyncScope(); + var sp = scope.ServiceProvider; + + var (profile, alters, fronts, tags, settingsFields, encryptionState) = await FetchSocketInitDataAsync( + systemId, + sp.GetRequiredService(), + sp.GetRequiredService(), + sp.GetRequiredService(), + sp.GetRequiredService(), + sp.GetRequiredService(), + sp.GetRequiredService(), + ct); + + foreach (var alter in alters) + alter.AvatarUrl = AvatarUrlQualifier.QualifyAvatar(alter, context.Request.Scheme, context.Request.Host); + + return new SocketJoinInitPayload( + BuildSelfReadModel( + systemId, + profile, + alters, + fronts, + settingsFields, + encryptionState, + $"{context.Request.Scheme}://{context.Request.Host}"), + alters, + fronts, + tags); +} + +public static SocketSelfReadModel BuildSelfReadModel( + SystemId systemId, + AccountPublicProfileReadModel? profile, + IReadOnlyList alters, + IReadOnlyList fronts, + IReadOnlyList settingsFields, + EncryptionState? encryptionState, + string? requestOrigin) +{ + var primaryFront = fronts.FirstOrDefault(x => x.Primary)?.Front.AlterId; + + return new SocketSelfReadModel( + profile?.SystemId ?? systemId, + profile?.Username, + profile?.Description, + AvatarUrlQualifier.QualifyAvatar(profile, requestOrigin), + profile?.AvatarSource, + AccountLinkFlag.FromValuePresence(profile?.DiscordId?.Value), + AccountLinkFlag.NotLinked, + AccountLinkFlag.FromValuePresence(profile?.AppleId?.Value), + AccountLinkFlag.FromValuePresence(profile?.Email?.Value), + AutoproxyMode.Off, + false, + alters.Count, + primaryFront, + settingsFields.OrderBy(x => x.Index).ToArray(), + encryptionState?.Initialized ?? false); +} + +} \ No newline at end of file diff --git a/apis/Interfold.Systems.Api/Controllers/PublicSystemsController.cs b/apis/Interfold.Systems.Api/Controllers/PublicSystemsController.cs new file mode 100644 index 00000000..7e807c4c --- /dev/null +++ b/apis/Interfold.Systems.Api/Controllers/PublicSystemsController.cs @@ -0,0 +1,180 @@ +using System.Diagnostics; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Validation; +using Interfold.Systems.Api.Filters; +using Interfold.Systems.Contracts.Models.Read; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Systems.Api.Controllers; + +[Route("api/systems/{systemId}")] +public sealed class PublicSystemsController : InterfoldControllerBase +{ + private readonly IAccountRepository _accounts; + private readonly IAlterRepository _alters; + private readonly ITagRepository _tags; + private readonly IFrontingRepository _fronting; + private readonly IFriendshipRepository _friendships; + + public PublicSystemsController( + IAccountRepository accounts, + IAlterRepository alters, + ITagRepository tags, + IFrontingRepository fronting, + IFriendshipRepository friendships) + { + _accounts = accounts; + _alters = alters; + _tags = tags; + _fronting = fronting; + _friendships = friendships; + } + + [HttpGet] + public async Task> Show([FromRoute] SystemId systemId, CancellationToken ct) + { + var model = await _accounts.GetPublicSystemAsync(systemId, ct); + if (model is not null) + { + model = model with { AvatarUrl = QualifyAvatar(model) }; + } + + return OkOrNotFound(model, "System not found.", ErrorCodes.SystemNotFound); + } + + [HttpGet("alters")] + [SystemMustExist] + public async Task>> ListAlters([FromRoute] SystemId systemId, CancellationToken ct) + { + var alters = await _alters.ListGuardedAsync(systemId, PrincipalId, ct); + foreach (var a in alters) a.AvatarUrl = QualifyAvatar(a); + return new SuccessResponse>(alters); + } + + [HttpGet("alters/{alterId:int}")] + [SystemMustExist] + public async Task> ShowAlter([FromRoute] SystemId systemId, [FromRoute][ValidAlterId] AlterId alterId, CancellationToken ct) + { + var alter = await _alters.GetGuardedAsync(systemId, alterId, PrincipalId, ct); + if (alter is not null) + { + alter.AvatarUrl = QualifyAvatar(alter); + } + + return OkOrNotFound(alter, "Alter not found.", ErrorCodes.AlterNotFound); + } + + [HttpGet("tags")] + [SystemMustExist] + public async Task>> ListTags([FromRoute] SystemId systemId, CancellationToken ct) + { + var tags = await _tags.ListGuardedAsync(systemId, PrincipalId, ct); + return new SuccessResponse>(tags); + } + + [HttpGet("tags/{tagId}")] + [SystemMustExist] + public async Task> ShowTag([FromRoute] SystemId systemId, [FromRoute] TagId tagId, CancellationToken ct) + { + var tag = await _tags.GetGuardedAsync(systemId, tagId, PrincipalId, ct); + return OkOrNotFound(tag, "Tag not found.", ErrorCodes.TagNotFound); + } + + [HttpGet("fronting")] + [SystemMustExist] + public async Task>> ListFronting([FromRoute] SystemId systemId, CancellationToken ct) + { + var fronts = await _fronting.ListActiveGuardedAsync(systemId, PrincipalId, ct); + return new SuccessResponse>(fronts); + } + + [HttpGet("batch")] + [SystemMustExist] + public async Task> Batch([FromRoute] SystemId systemId, CancellationToken ct) + { + var principalId = PrincipalId; + // Semantic self-check. RepresentsSameUserAs compares scoped-to-scoped when both + // sides carry a region prefix, so a cross-region collision (same raw id, different + // region → different user) is correctly treated as a non-self request. + if (principalId.RepresentsSameUserAs(systemId)) + { + return new ErrorResponse( + "You cannot view your own system through this endpoint.", + ErrorCodes.InvalidEndpoint, + System.Net.HttpStatusCode.Forbidden); + } + + var aggregateSw = Stopwatch.StartNew(); + var altersTask = TimeSliceAsync("alter", _alters.ListGuardedAsync(systemId, principalId, ct)); + var tagsTask = TimeSliceAsync("tag", _tags.ListGuardedAsync(systemId, principalId, ct)); + var friendshipTask = _friendships.GetFriendshipAsync(principalId, systemId, ct); + + await Task.WhenAll(altersTask, tagsTask, friendshipTask); + InterfoldMetrics.GuardedBatchLatencyMs.Record(aggregateSw.Elapsed.TotalMilliseconds, + new KeyValuePair("slice", "aggregate")); + + var batchAlters = altersTask.Result; + foreach (var a in batchAlters) a.AvatarUrl = QualifyAvatar(a); + + var friendship = friendshipTask.Result; + if (friendship is not null) + { + // Inlined from the former InterfoldControllerBase.QualifyFriendship helper — the + // shared spine wrapper moved out during the Phase-3 Friendships slice so + // Interfold.Shared.Api no longer binds the friendship read-model cluster; per-avatar + // qualification still uses the shared primitive. + friendship = friendship with + { + Friend = friendship.Friend with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(friendship.Friend, Request.Scheme, Request.Host), + }, + Fronting = friendship.Fronting + .Select(f => f with + { + Alter = f.Alter with + { + AvatarUrl = AvatarUrlQualifier.QualifyAvatar(f.Alter, Request.Scheme, Request.Host), + }, + }) + .ToArray(), + }; + } + + return new PublicSystemBatchReadModel( + Friendship: friendship, + Tags: tagsTask.Result, + Alters: batchAlters); + } + + // Records a per-slice latency sample for the batch endpoint's parallel branches. + // Stopwatch is started before await so slice latency includes queueing when the + // scheduler holds the task off the pool. + private static async Task TimeSliceAsync(string slice, Task sliceTask) + { + var sw = Stopwatch.StartNew(); + try + { + return await sliceTask.ConfigureAwait(false); + } + finally + { + InterfoldMetrics.GuardedBatchLatencyMs.Record(sw.Elapsed.TotalMilliseconds, + new KeyValuePair("slice", slice)); + } + } + +} diff --git a/apis/Interfold.Systems.Api/DependencyInjection/SystemsModuleServiceCollectionExtensions.cs b/apis/Interfold.Systems.Api/DependencyInjection/SystemsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..e7eb08de --- /dev/null +++ b/apis/Interfold.Systems.Api/DependencyInjection/SystemsModuleServiceCollectionExtensions.cs @@ -0,0 +1,17 @@ +namespace Interfold.Systems.Api.DependencyInjection; + +/// Systems feature module — the facade shape has no owned handlers or +/// abstractions to register (see the Interfold.Systems.Domain project's +/// deliberate absence). AddSystemsModule exists purely for wiring symmetry +/// with the other feature modules so Interfold.Api.Host/Program.cs reads +/// uniformly. Consumed once from the module-registration block alongside the +/// other Add<Feature>Module extensions. +public static class SystemsModuleServiceCollectionExtensions +{ + /// No-op today. PublicSystemsController consumes five repository + /// interfaces (IAccountRepository, IAlterRepository, + /// ITagRepository, IFrontingRepository, IFriendshipRepository) + /// registered by the active persistence adapter via AddInterfoldPersistence; + /// nothing here to register. + public static IServiceCollection AddSystemsModule(this IServiceCollection services) => services; +} diff --git a/apis/Interfold.Systems.Api/Filters/SystemMustExistAttribute.cs b/apis/Interfold.Systems.Api/Filters/SystemMustExistAttribute.cs new file mode 100644 index 00000000..f08ad831 --- /dev/null +++ b/apis/Interfold.Systems.Api/Filters/SystemMustExistAttribute.cs @@ -0,0 +1,72 @@ +using System.Net; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Interfold.Systems.Api.Filters; + +/// Action filter that short-circuits with 404 +/// () when the systemId route value doesn't +/// resolve via . Consolidates the +/// repeated existence-gate prelude on PublicSystemsController read actions; do +/// NOT apply where the endpoint already looks the account up to build its response body +/// (would double the round-trip). +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +public sealed class SystemMustExistAttribute : Attribute, IAsyncActionFilter +{ + // Constant so a route-template rename surfaces as compile-time breakage on the [Route] + // rather than a silent 404 at request time. + private const string SystemIdRouteKey = "systemId"; + + public async Task OnActionExecutionAsync( + ActionExecutingContext context, + ActionExecutionDelegate next) + { + var systemId = ExtractSystemId(context); + if (systemId is null) + { + context.Result = NotFoundResult(); + return; + } + + var accounts = context.HttpContext.RequestServices.GetRequiredService(); + var profile = await accounts.GetPublicProfileAsync(systemId.Value, context.HttpContext.RequestAborted); + if (profile is null) + { + context.Result = NotFoundResult(); + return; + } + + await next(); + } + + private static SystemId? ExtractSystemId(ActionExecutingContext context) + { + // Prefer the already-bound (shape-validated) argument; fall back to the raw route. + if (context.ActionArguments.TryGetValue(SystemIdRouteKey, out var arg) && arg is SystemId bound) + { + return bound; + } + + if (context.RouteData.Values.TryGetValue(SystemIdRouteKey, out var raw) + && raw is string s + && !string.IsNullOrWhiteSpace(s)) + { + return new SystemId(s); + } + + return null; + } + + private static IActionResult NotFoundResult() => new ObjectResult(new ErrorResponse( + "System not found.", + ErrorCodes.SystemNotFound, + HttpStatusCode.NotFound)) + { + DeclaredType = typeof(ErrorResponse), + StatusCode = (int)HttpStatusCode.NotFound, + }; +} diff --git a/apis/Interfold.Systems.Api/Interfold.Systems.Api.csproj b/apis/Interfold.Systems.Api/Interfold.Systems.Api.csproj new file mode 100644 index 00000000..9cfe9b78 --- /dev/null +++ b/apis/Interfold.Systems.Api/Interfold.Systems.Api.csproj @@ -0,0 +1,49 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apis/Interfold.Tags.Api/Controllers/TagsController.cs b/apis/Interfold.Tags.Api/Controllers/TagsController.cs new file mode 100644 index 00000000..0d719b96 --- /dev/null +++ b/apis/Interfold.Tags.Api/Controllers/TagsController.cs @@ -0,0 +1,109 @@ +using Interfold.Shared.Api.Controllers.Base; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Operations; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.AspNetCore.Mvc; + +namespace Interfold.Tags.Api.Controllers; + +[Route("api/systems/me/tags")] +public sealed class TagsController : InterfoldControllerBase +{ + private readonly ITagRepository _tagRepository; + private readonly CreateTagCommandHandler _create; + private readonly UpdateTagCommandHandler _update; + private readonly DeleteTagCommandHandler _delete; + private readonly AttachAlterToTagCommandHandler _attachAlter; + private readonly DetachAlterFromTagCommandHandler _detachAlter; + private readonly SetParentTagCommandHandler _setParent; + private readonly RemoveParentTagCommandHandler _removeParent; + + public TagsController( + ITagRepository tagRepository, + CreateTagCommandHandler create, + UpdateTagCommandHandler update, + DeleteTagCommandHandler delete, + AttachAlterToTagCommandHandler attachAlter, + DetachAlterFromTagCommandHandler detachAlter, + SetParentTagCommandHandler setParent, + RemoveParentTagCommandHandler removeParent) + { + _tagRepository = tagRepository; + _create = create; + _update = update; + _delete = delete; + _attachAlter = attachAlter; + _detachAlter = detachAlter; + _setParent = setParent; + _removeParent = removeParent; + } + + [HttpPost] + public async Task> CreateTag( + [FromBody] CreateTagRequest body, + CancellationToken cancellationToken) + { + var principal = PrincipalId; + // CreateTagCommandHandler owns InsertedAtUtc on the public-API path and derives it + // from the envelope's OccurredAt right before calling the repo. Passing `default` + // here keeps the hashed payload stable across retries with the same idempotency key + // (otherwise every call would stamp a fresh DateTime.UtcNow and look like a + // different request, triggering ConflictDuplicate on every replay). + return await DispatchCreatedAsync( + _create, + OperationIds.TagCreate, + new CreateTagCommand(body.Name, body.ParentTagId, InsertedAtUtc: default), + async (res) => await _tagRepository.GetAsync(principal, res.TagId, cancellationToken), + cancellationToken); + } + + [HttpPatch("{id}")] + public async Task UpdateTag(TagId id, [FromBody] UpdateTagRequest body, CancellationToken ct) + { + return await DispatchNoContentAsync(_update, OperationIds.TagUpdate, new UpdateTagCommand(id, body.Name, body.Color, body.Description, body.SecurityLevel) + , ct); + } + + [HttpDelete("{id}")] + public async Task DeleteTag(TagId id, CancellationToken ct) + { + return await DispatchNoContentAsync(_delete, OperationIds.TagDelete, new DeleteTagCommand(id) + , ct); + } + + [HttpPost("{id}/alter")] + public async Task AttachAlter(TagId id, [FromBody] TagAlterRequest body, CancellationToken ct) + { + return await DispatchNoContentAsync(_attachAlter, OperationIds.TagAttachAlter, new AttachAlterToTagCommand(id, body.AlterId) + , ct); + } + + [HttpDelete("{id}/alter")] + public async Task DetachAlter(TagId id, [FromBody] TagAlterRequest body, CancellationToken ct) + { + return await DispatchNoContentAsync(_detachAlter, OperationIds.TagDetachAlter, new DetachAlterFromTagCommand(id, body.AlterId) + , ct); + } + + [HttpPost("{id}/parent")] + public async Task SetParent(TagId id, [FromBody] SetParentRequest body, CancellationToken ct) + { + if (body.ParentTagId is not { } parentTagId || parentTagId == TagId.Empty) + return new ErrorResponse("Invalid parent tag ID.", ErrorCodes.InvalidParentTagId, System.Net.HttpStatusCode.BadRequest); + + return await DispatchNoContentAsync(_setParent, OperationIds.TagSetParent, new SetParentTagCommand(id, parentTagId) + , ct); + } + + [HttpDelete("{id}/parent")] + public async Task RemoveParent(TagId id, CancellationToken ct) + { + return await DispatchNoContentAsync(_removeParent, OperationIds.TagRemoveParent, new RemoveParentTagCommand(id) + , ct); + } +} diff --git a/apis/Interfold.Tags.Api/DependencyInjection/TagsModuleServiceCollectionExtensions.cs b/apis/Interfold.Tags.Api/DependencyInjection/TagsModuleServiceCollectionExtensions.cs new file mode 100644 index 00000000..d8ea35d8 --- /dev/null +++ b/apis/Interfold.Tags.Api/DependencyInjection/TagsModuleServiceCollectionExtensions.cs @@ -0,0 +1,24 @@ +using Interfold.Tags.Domain; + +namespace Interfold.Tags.Api.DependencyInjection; + +/// Tags feature module — the DI equivalent of the seven tag-handler +/// AddSingleton calls previously living in . +/// Consumed once from Interfold.Api.Host/Program.cs alongside the other +/// module extensions. +public static class TagsModuleServiceCollectionExtensions +{ + /// Registers everything the Tags feature owns: the seven tag command-handler + /// singletons. ITagRepository is registered by the active persistence adapter + /// (Scylla / Postgres / InMemory) via AddInterfoldPersistence, so no repository + /// registration lives here. + public static IServiceCollection AddTagsModule(this IServiceCollection services) => + services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); +} diff --git a/apis/Interfold.Tags.Api/Interfold.Tags.Api.csproj b/apis/Interfold.Tags.Api/Interfold.Tags.Api.csproj new file mode 100644 index 00000000..c47736e8 --- /dev/null +++ b/apis/Interfold.Tags.Api/Interfold.Tags.Api.csproj @@ -0,0 +1,28 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/config/config.exs b/config/config.exs deleted file mode 100644 index b629a08d..00000000 --- a/config/config.exs +++ /dev/null @@ -1,137 +0,0 @@ -# This file is responsible for configuring your application -# and its dependencies with the aid of the Config module. -# -# This configuration file is loaded before any dependency and -# is restricted to this project. - -# General application configuration -import Config - -config :octocon, :env, config_env() - -config :octocon, - ecto_repos: [Octocon.Repo, Octocon.MessageRepo] - -config :octocon, Octocon.MessageRepo, priv: "priv/msg_repo" - -config :octocon, Octocon.Repo, - target_pools: 1, - keyspace: "global", - stacktrace: true, - show_sensitive_data_on_connection_error: true - -# Configures the endpoint -config :octocon, OctoconWeb.Endpoint, - adapter: Bandit.PhoenixAdapter, - url: [host: "localhost"], - render_errors: [ - formats: [json: OctoconWeb.ErrorJSON], - layout: false - ], - pubsub_server: Octocon.PubSub, - live_view: [signing_salt: "mCW+yBHJ"] - -# Configures Elixir's Logger -config :logger, :console, - format: "$time $metadata[$level] $message\n", - metadata: [:request_id] - -config :octocon, :logger, [ - {:handler, :sentry_handler, Sentry.LoggerHandler, - %{ - config: %{metadata: [:file, :line]} - }} -] - -# Use Jason for JSON parsing in Phoenix -config :phoenix, :json_library, Jason - -# Import environment specific config. This must remain at the bottom -# of this file so it overrides the configuration defined above. -import_config "#{config_env()}.exs" - -# Ueberauth -config :ueberauth, Ueberauth, - base_path: "/auth", - providers: [ - discord: {Ueberauth.Strategy.Discord, [default_scope: "identify email"]}, - google: {Ueberauth.Strategy.Google, []}, - apple: - {Ueberauth.Strategy.Apple, - [ - callback_methods: ["POST"], - default_scope: "email", - ignores_csrf_attack: true, - state_param_cookie_same_site: "None" - ]}, - discord_link: - {Ueberauth.Strategy.Discord, - [ - default_scope: "identify email", - request_path: "/auth/link/discord", - callback_path: "/auth/link/discord/callback" - ]}, - google_link: - {Ueberauth.Strategy.Google, - [request_path: "/auth/link/google", callback_path: "/auth/link/google/callback"]}, - apple_link: - {Ueberauth.Strategy.Apple, - [ - callback_methods: ["POST"], - default_scope: "email", - request_path: "/auth/link/apple", - callback_path: "/auth/link/apple/callback", - ignores_csrf_attack: true, - state_param_cookie_same_site: "None" - ]} - ] - -# Guardian -config :octocon, Octocon.Auth.Guardian, ttl: {261, :weeks} - -config :octocon, OctoconWeb.AuthPipeline, - module: Octocon.Auth.Guardian, - error_handler: OctoconWeb.AuthErrorHandler - -config :octocon, Oban, - repo: Octocon.MessageRepo, - plugins: [Oban.Plugins.Pruner], - queues: [ - default: 10, - # Optimization courtesy of Snickety: 2 + 2 = 4 - sp_imports: 2 + 2, - # Fuck you Snickety, we do this the cool way 😎 - pk_imports: Kernel.+(2, 2) - ] - -# Global Nostrum config -config :nostrum, - caches: %{ - guilds: OctoconDiscord.GuildCache, - presences: Nostrum.Cache.PresenceCache.NoOp, - users: Nostrum.Cache.UserCache.NoOp, - channel_guild_mapping: Nostrum.Cache.ChannelGuildMapping.NoOp, - members: Nostrum.Cache.MemberCache.NoOp - }, - # [TODO]: Manual sharding - # num_shards: :manual, - ffmpeg: false, - gateway_compression: :zstd - -config :ex_aws, - json_codec: Jason - -config :pigeon, :default_pool_size, 5 - -config :sentry, - integrations: [ - oban: [ - capture_errors: true - ] - ] - -config :hammer, - backend: - {Hammer.Backend.ETS, [expiry_ms: :timer.seconds(5), cleanup_interval_ms: :timer.minutes(10)]} - -config :libcluster, debug: true diff --git a/config/dev-template.exs b/config/dev-template.exs deleted file mode 100644 index aacc4385..00000000 --- a/config/dev-template.exs +++ /dev/null @@ -1,133 +0,0 @@ -import Config - -config :octocon, :primary_node_count, 1 - -# Discord channel scope to push slash commands to -# In development, make this equal to the ID of a test server you control -config :octocon, :nostrum_scope, 1_234_567_89_123_456_789 - -# For development, we disable any cache and enable -# debugging and code reloading. -# -# The watchers configuration can be used to run external -# watchers to your application. For example, we can use it -# to bundle .js and .css sources. -config :octocon, OctoconWeb.Endpoint, - # Binding to loopback ipv4 address prevents access from other machines. - # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. - http: [ip: {127, 0, 0, 1}, port: System.get_env("DEV_PORT") || 4000], - check_origin: false, - code_reloader: true, - debug_errors: true, - secret_key_base: "", - watchers: [] - -config :mnesia, dir: "/tmp/mnesia" - -# ## SSL Support -# -# In order to use HTTPS in development, a self-signed -# certificate can be generated by running the following -# Mix task: -# -# mix phx.gen.cert -# -# Run `mix help phx.gen.cert` for more information. -# -# The `http:` config above can be replaced with: -# -# https: [ -# port: 4001, -# cipher_suite: :strong, -# keyfile: "priv/cert/selfsigned_key.pem", -# certfile: "priv/cert/selfsigned.pem" -# ], -# -# If desired, both `http:` and `https:` keys can be -# configured to run both http and https servers on -# different ports. - -# Enable dev routes for dashboard and mailbox -config :octocon, dev_routes: true - -# Do not include metadata nor timestamps in development logs -config :logger, :console, format: "[$level] $message\n" - -# Set a higher stacktrace during development. Avoid configuring such -# in production as building large stacktraces may be expensive. -config :phoenix, :stacktrace_depth, 20 - -# Initialize plugs at runtime for faster development compilation -config :phoenix, :plug_init_mode, :runtime - -config :octocon, Octocon.Repo, - nodes: ["scylla-nam"], - load_balancing: - {Xandra.Cluster.LoadBalancingPolicy.DCAwareRoundRobin, [local_data_center: "nam"]}, - refresh_topology_interval: :timer.minutes(1), - sync_connect: :infinity, - pool_size: 10 - -config :octocon, Octocon.MessageRepo, - username: "postgres", - password: "postgres", - hostname: "msg-db", - database: "octocon_dev", - port: "5432", - stacktrace: true, - show_sensitive_data_on_connection_error: true, - pool_size: 10 - -# Discord token -config :octocon, discord_token: "TODO" - -node_group = - case System.get_env("NODE_GROUP") do - "primary" -> - :primary - - "auxiliary" -> - :auxiliary - - "sidecar" -> - :sidecar - - _ -> - :primary - end - -config :octocon, :node_group, node_group -config :octocon, :current_db_region, "nam" -config :octocon, :use_tailscale, false - -# Discord auth -config :ueberauth, Ueberauth.Strategy.Discord.OAuth, - client_id: "TODO", - client_secret: "TODO" - -# Google auth -config :ueberauth, Ueberauth.Strategy.Google.OAuth, - client_id: "TODO", - client_secret: "TODO" - -# Guardian -config :octocon, Octocon.Auth.Guardian, - issuer: "octocon", - secret_key: "Vui-nVt36x8I-8EWN8Fg5xvv588aniMu5eubeD7ZTdwIBdwt4qHDVXyL-2NjAeE1" - -config :octocon, :pepper, "6dtfTrHA" - -config :waffle, - storage: Waffle.Storage.S3, - bucket: "octocon-staging", - asset_host: "https://cdn-staging.octocon.app" - -# S3 config -config :ex_aws, - access_key_id: "TODO", - secret_access_key: "TODO" - -config :ex_aws, :s3, - scheme: "https://", - host: "TODO", - region: "auto" diff --git a/config/prod.exs b/config/prod.exs deleted file mode 100644 index 0e39483c..00000000 --- a/config/prod.exs +++ /dev/null @@ -1,18 +0,0 @@ -import Config - -config :octocon, :nostrum_scope, :global - -config :mnesia, dir: ~c"/mnesia" - -# [TODO]: Do not print debug messages in production -config :logger, level: :info - -config :sentry, - environment_name: :prod - -# tags: %{ -# env: "production" -# } - -# Runtime production configuration, including reading -# of environment variables, is done on config/runtime.exs. diff --git a/config/runtime.exs b/config/runtime.exs deleted file mode 100644 index 37bcfc87..00000000 --- a/config/runtime.exs +++ /dev/null @@ -1,369 +0,0 @@ -import Config - -tailscale_api_authkey = System.get_env("TAILSCALE_API_AUTHKEY") - -config :octocon, :use_tailscale, tailscale_api_authkey != nil and tailscale_api_authkey != "" -config :octocon, tailscale_api_authkey: tailscale_api_authkey - -if config_env() == :prod do - node_group = - case System.get_env("FLY_PROCESS_GROUP") do - "primary" -> - :primary - - "auxiliary" -> - :auxiliary - - "sidecar" -> - :sidecar - - _ -> - node_group = - System.get_env("NODE_GROUP") || - raise """ - environment variable NODE_GROUP is missing (this node is not running on Fly to auto-detect). - It should be one of: primary, auxiliary, sidecar. - """ - - String.to_atom(node_group) - end - - current_db_region = - case System.get_env("FLY_REGION") do - "fra" -> - :eur - - "iad" -> - :nam - - "syd" -> - :ocn - - "gru" -> - :sam - - "bom" -> - :sas - - "sin" -> - :eas - - nil -> - region = - System.get_env("CURRENT_DB_REGION") || - raise """ - environment variable CURRENT_DB_REGION is missing (this node is not running on Fly to auto-detect). - It should be one of: nam, eur, ocn, eas, sam, sas, gdpr. - """ - - String.to_atom(region) - end - - current_db_datacenter = - case current_db_region do - :nam -> "dedi-us-east" - :eur -> "fly-fra" - :gdpr -> "fly-fra" - :ocn -> "fly-syd" - :eas -> "fly-sin" - :sam -> "fly-gru" - :sas -> "fly-bom" - end - - config :octocon, :node_group, node_group - config :octocon, :current_db_region, current_db_region - - # [TODO]: Only :auxiliary - # [TODO]: Rename :auxiliary to :ingress - if node_group in [:primary, :auxiliary] || System.get_env("PHX_SERVER") do - config :octocon, OctoconWeb.Endpoint, server: true - else - config :octocon, OctoconWeb.Endpoint, server: false - end - - pool_size = - if node_group == :sidecar do - 2 - else - String.to_integer(System.get_env("POOL_SIZE") || "10") - end - - config :octocon, - :primary_node_count, - String.to_integer(System.get_env("PRIMARY_NODE_COUNT") || "1") - - database_password = - System.get_env("DATABASE_PASSWORD") || - raise """ - environment variable DATABASE_PASSWORD is missing. - """ - - database_contact_points = - System.get_env("DATABASE_CONTACT_POINTS") || - raise """ - environment variable DATABASE_CONTACT_POINTS is missing. - For example: 100.100.127.0,100.100.127.1,100.100.127.2 - - Whole env: #{inspect(System.get_env())} - """ - - msg_database_url = - System.get_env("MSG_DATABASE_URL") || - raise """ - environment variable MSG_DATABASE_URL is missing. - For example: ecto://USER:PASS@HOST/DATABASE - """ - - config :octocon, :proxy_db, System.get_env("OCTO_PROXY_DB") in ~w(true 1) - - config :octocon, Octocon.Repo, - nodes: String.split(database_contact_points, ","), - load_balancing: - {Xandra.Cluster.LoadBalancingPolicy.DCAwareRoundRobin, - [local_data_center: current_db_datacenter]}, - refresh_topology_interval: :timer.minutes(1), - sync_connect: :infinity, - authentication: - {Xandra.Authenticator.Password, [username: "octo", password: database_password]}, - pool_size: pool_size - - config :octocon, Octocon.MessageRepo, - url: msg_database_url, - # , socket_options: maybe_ipv6 - pool_size: String.to_integer(System.get_env("MSG_POOL_SIZE") || "10") - - # The secret key base is used to sign/encrypt cookies and other secrets. - # A default value is used in config/dev.exs and config/test.exs but you - # want to use a different value for prod and you most likely don't want - # to check this value into version control, so we use an environment - # variable instead. - secret_key_base = - System.get_env("SECRET_KEY_BASE") || - raise """ - environment variable SECRET_KEY_BASE is missing. - You can generate one by calling: mix phx.gen.secret - """ - - host = - System.get_env("PHX_HOST") || - raise """ - environment variable PHX_HOST is missing. - """ - - port = - String.to_integer( - System.get_env("PORT") || - raise(""" - environment variable PORT is missing. - """) - ) - - config :octocon, OctoconWeb.Endpoint, - url: [host: host, port: 443, scheme: "https"], - http: [ - # Enable IPv6 and bind on all interfaces. - # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. - # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html - # for details about using IPv6 vs IPv4 and loopback vs public addresses. - ip: {0, 0, 0, 0, 0, 0, 0, 0}, - port: port - ], - check_origin: [ - "//*.octocon.app", - "https://" <> host, - "wss://" <> host, - "//localhost", - "//localhost:8080", - "http://localhost", - "http://localhost:8080", - "//octocon-beta.netlify.app" - ], - secret_key_base: secret_key_base, - live_view: [ - signing_salt: - System.get_env("LIVE_VIEW_SIGNING_SALT") || - raise(""" - environment variable LIVE_VIEW_SIGNING_SALT is missing. - """) - ] - - config :ueberauth, Ueberauth.Strategy.Discord.OAuth, - client_id: - System.get_env("DISCORD_CLIENT_ID") || - raise(""" - environment variable DISCORD_CLIENT_ID is missing. - """), - client_secret: - System.get_env("DISCORD_CLIENT_SECRET") || - raise(""" - environment variable DISCORD_CLIENT_SECRET is missing. - """) - - config :ueberauth, Ueberauth.Strategy.Google.OAuth, - client_id: - System.get_env("GOOGLE_CLIENT_ID") || - raise(""" - environment variable GOOGLE_CLIENT_ID is missing. - """), - client_secret: - System.get_env("GOOGLE_CLIENT_SECRET") || - raise(""" - environment variable GOOGLE_CLIENT_SECRET is missing. - """) - - config :ueberauth, Ueberauth.Strategy.Apple.OAuth, - client_id: - System.get_env("APPLE_CLIENT_ID") || - raise(""" - environment variable APPLE_CLIENT_ID is missing. - """), - client_secret: {Octocon.Apple, :get_client_secret} - - config :octocon, - discord_token: - System.get_env("DISCORD_TOKEN") || - raise(""" - environment variable DISCORD_TOKEN is missing. - """) - - # Guardian - config :octocon, Octocon.Auth.Guardian, - issuer: "octocon", - secret_key: - System.get_env("GUARDIAN_SECRET_KEY") || - raise(""" - environment variable GUARDIAN_SECRET_KEY is missing. - """) - - config :octocon, - pepper: - System.get_env("ENCRYPTION_PEPPER") || - raise(""" - environment variable ENCRYPTION_PEPPER is missing. - """) - - config :octocon, - private_key_pem: - (System.get_env("ENCRYPTION_PRIVATE_KEY") || - raise("environment variable ENCRYPTION_PRIVATE_KEY is missing.")) - |> Base.decode64!() - - config :octocon, - apple_client_id: - System.get_env("APPLE_CLIENT_ID") || - raise(""" - environment variable APPLE_CLIENT_ID is missing. - """), - apple_private_key_id: - System.get_env("APPLE_PRIVATE_KEY_ID") || - raise(""" - environment variable APPLE_PRIVATE_KEY_ID is missing. - """), - apple_team_id: - System.get_env("APPLE_TEAM_ID") || - raise(""" - environment variable APPLE_TEAM_ID is missing. - """), - apple_private_key: - (System.get_env("APPLE_PRIVATE_KEY") || - raise("environment variable APPLE_PRIVATE_KEY is missing.")) - |> String.replace("\\n", "\n") - - config :waffle, - storage: Waffle.Storage.S3, - bucket: "octocon", - asset_host: - System.get_env("S3_ASSET_HOST") || - raise(""" - environment variable S3_ASSET_HOST is missing. - """) - - config :ex_aws, - access_key_id: - System.get_env("S3_ACCESS_KEY_ID") || - raise(""" - environment variable S3_ACCESS_KEY_ID is missing. - """), - secret_access_key: - System.get_env("S3_SECRET_ACCESS_KEY") || - raise(""" - environment variable S3_SECRET_ACCESS_KEY is missing. - """) - - config :ex_aws, :s3, - scheme: "https://", - host: - System.get_env("S3_HOST") || - raise(""" - environment variable S3_HOST is missing. - """), - region: - System.get_env("S3_REGION") || - raise(""" - environment variable S3_REGION is missing. - """) - - config :octocon, Octocon.FCM, - adapter: Pigeon.FCM, - auth: Octocon.Goth, - project_id: "octocon-fb" - - config :octocon, - fcm_service_account_json: - (System.get_env("FCM_SERVICE_ACCOUNT") || - raise(""" - environment variable FCM_SERVICE_ACCOUNT is missing. - """)) - |> Base.decode64!() - - config :octocon, Octocon.PromEx, - grafana: [ - host: - System.get_env("GRAFANA_HOST") || raise("environment variable GRAFANA_HOST is missing."), - auth_token: - System.get_env("GRAFANA_TOKEN") || raise("environment variable GRAFANA_TOKEN is missing."), - upload_dashboards_on_start: false, - folder_name: "OTP", - annotate_app_lifecycle: true - ] - - # redirect_uri: System.get_env("GOOGLE_REDIRECT_URI") - - # ## SSL Support - # - # To get SSL working, you will need to add the `https` key - # to your endpoint configuration: - # - # config :octocon, OctoconWeb.Endpoint, - # https: [ - # ..., - # port: 443, - # cipher_suite: :strong, - # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), - # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") - # ] - # - # The `cipher_suite` is set to `:strong` to support only the - # latest and more secure SSL ciphers. This means old browsers - # and clients may not be supported. You can set it to - # `:compatible` for wider support. - # - # `:keyfile` and `:certfile` expect an absolute path to the key - # and cert in disk or a relative path inside priv, for example - # "priv/ssl/server.key". For all supported SSL configuration - # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 - # - # We also recommend setting `force_ssl` in your endpoint, ensuring - # no data is ever sent via http, always redirecting to https: - # - # config :octocon, OctoconWeb.Endpoint, - # force_ssl: [hsts: true] - # - # Check `Plug.SSL` for all available options in `force_ssl`. -end - -if config_env() == :prod do - config :sentry, - dsn: System.get_env("SENTRY_DSN"), - environment_name: :prod -end diff --git a/config/test.exs b/config/test.exs deleted file mode 100644 index 2eedfe3d..00000000 --- a/config/test.exs +++ /dev/null @@ -1,14 +0,0 @@ -import Config - -# We don't run a server during test. If one is required, -# you can enable the server option below. -config :octocon, OctoconWeb.Endpoint, - http: [ip: {127, 0, 0, 1}, port: 4002], - secret_key_base: "kDwC9vMAvvat7jUYEZ3F5WFhr9RnAci7dSyh3RhdNKpTmDamfB10TFC+npVP5FHh", - server: false - -# Print only warnings and errors during test -config :logger, level: :warning - -# Initialize plugs at runtime for faster test compilation -config :phoenix, :plug_init_mode, :runtime diff --git a/db/cassandra/Dockerfile b/db/cassandra/Dockerfile new file mode 100644 index 00000000..12bc6bfc --- /dev/null +++ b/db/cassandra/Dockerfile @@ -0,0 +1,12 @@ +FROM cassandra:5 + +# Bake required cassandra.yaml overrides into the image so we don't need a runtime +# wrapper script to mutate config inside the running container. +# +# - materialized_views_enabled: required by our schema (Cassandra 4.0+ default is false) +# - authenticator/authorizer: required so the bootstrap container can create the app +# user as a non-superuser and lock the default account +RUN set -e \ + && sed -i 's/^materialized_views_enabled:.*/materialized_views_enabled: true/' /etc/cassandra/cassandra.yaml \ + && sed -i 's/^authenticator:.*/authenticator: PasswordAuthenticator/' /etc/cassandra/cassandra.yaml \ + && sed -i 's/^authorizer:.*/authorizer: CassandraAuthorizer/' /etc/cassandra/cassandra.yaml diff --git a/db/scylla/cassandra-rackdc.eas.properties b/db/scylla/cassandra-rackdc.eas.properties new file mode 100644 index 00000000..e3d6feec --- /dev/null +++ b/db/scylla/cassandra-rackdc.eas.properties @@ -0,0 +1,3 @@ +dc=eas +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.eur.properties b/db/scylla/cassandra-rackdc.eur.properties new file mode 100644 index 00000000..dfe7bc79 --- /dev/null +++ b/db/scylla/cassandra-rackdc.eur.properties @@ -0,0 +1,3 @@ +dc=eur +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.gdpr.properties b/db/scylla/cassandra-rackdc.gdpr.properties new file mode 100644 index 00000000..bdba6405 --- /dev/null +++ b/db/scylla/cassandra-rackdc.gdpr.properties @@ -0,0 +1,3 @@ +dc=gdpr +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.nam.properties b/db/scylla/cassandra-rackdc.nam.properties new file mode 100644 index 00000000..cc3df8d5 --- /dev/null +++ b/db/scylla/cassandra-rackdc.nam.properties @@ -0,0 +1,3 @@ +dc=nam +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.ocn.properties b/db/scylla/cassandra-rackdc.ocn.properties new file mode 100644 index 00000000..1974416b --- /dev/null +++ b/db/scylla/cassandra-rackdc.ocn.properties @@ -0,0 +1,3 @@ +dc=ocn +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.sam.properties b/db/scylla/cassandra-rackdc.sam.properties new file mode 100644 index 00000000..f0ca6a2a --- /dev/null +++ b/db/scylla/cassandra-rackdc.sam.properties @@ -0,0 +1,3 @@ +dc=sam +rack=rack1 +prefer_local=true diff --git a/db/scylla/cassandra-rackdc.sas.properties b/db/scylla/cassandra-rackdc.sas.properties new file mode 100644 index 00000000..1f5959cd --- /dev/null +++ b/db/scylla/cassandra-rackdc.sas.properties @@ -0,0 +1,3 @@ +dc=sas +rack=rack1 +prefer_local=true diff --git a/dev/Dockerfile b/dev/Dockerfile deleted file mode 100644 index 01ce7b48..00000000 --- a/dev/Dockerfile +++ /dev/null @@ -1,49 +0,0 @@ -ARG ELIXIR_VERSION=1.19.5 -ARG OTP_VERSION=27.0 -ARG DEBIAN_VERSION=trixie-20260112-slim - -FROM "hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}" - -ENV MIX_BUILD_ROOT=/build -ENV MIX_DEPS_PATH=/deps -ENV HOME=/home/user - -RUN mkdir /build && \ - chown nobody /build && \ - mkdir /deps && \ - chown nobody /deps && \ - mkdir /docker && \ - chown nobody /docker && \ - mkdir /home/user && \ - chown nobody /home/user - -ADD dev/docker /docker - -RUN chmod +x /docker/entrypoint.sh /docker/command.sh && \ - chown nobody /docker/entrypoint.sh /docker/command.sh - -RUN apt-get update -y && apt-get install -y build-essential git libstdc++6 openssl libncurses6 locales ca-certificates curl gnupg iproute2 iputils-ping jq \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# Set the locale -RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen - -USER nobody - -# install hex + rebar -RUN mix local.hex --force && \ - mix local.rebar --force - -ENV LANG="en_US.UTF-8" -ENV LANGUAGE="en_US:en" -ENV LC_ALL="en_US.UTF-8" - -VOLUME /app -VOLUME /build -VOLUME /deps -WORKDIR /app - -EXPOSE 4000 - -CMD ["/docker/command.sh"] -ENTRYPOINT ["/docker/entrypoint.sh"] diff --git a/dev/bin/iex b/dev/bin/iex deleted file mode 100755 index 9dfcf6c0..00000000 --- a/dev/bin/iex +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -docker compose -f docker-compose.dev.yml exec octocon-app iex -S mix phx.server diff --git a/dev/bin/setup b/dev/bin/setup deleted file mode 100755 index 54763006..00000000 --- a/dev/bin/setup +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -docker compose -f docker-compose.dev.yml exec octocon-app mix setup diff --git a/dev/docker/command.sh b/dev/docker/command.sh deleted file mode 100644 index 73ea6923..00000000 --- a/dev/docker/command.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -# mix phx.server -tail -f /dev/null \ No newline at end of file diff --git a/dev/docker/entrypoint.sh b/dev/docker/entrypoint.sh deleted file mode 100644 index 04cedebf..00000000 --- a/dev/docker/entrypoint.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -mix deps.get - -"$@" diff --git a/dev/scylla/cassandra-rackdc.eas.properties b/dev/scylla/cassandra-rackdc.eas.properties deleted file mode 100644 index 5f1a8b53..00000000 --- a/dev/scylla/cassandra-rackdc.eas.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=eas -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.eur.properties b/dev/scylla/cassandra-rackdc.eur.properties deleted file mode 100644 index cbcdad62..00000000 --- a/dev/scylla/cassandra-rackdc.eur.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=eur -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.gdpr.properties b/dev/scylla/cassandra-rackdc.gdpr.properties deleted file mode 100644 index 99851017..00000000 --- a/dev/scylla/cassandra-rackdc.gdpr.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=gdpr -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.nam.properties b/dev/scylla/cassandra-rackdc.nam.properties deleted file mode 100644 index 085d1457..00000000 --- a/dev/scylla/cassandra-rackdc.nam.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=nam -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.ocn.properties b/dev/scylla/cassandra-rackdc.ocn.properties deleted file mode 100644 index 4d38da1b..00000000 --- a/dev/scylla/cassandra-rackdc.ocn.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=ocn -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.sam.properties b/dev/scylla/cassandra-rackdc.sam.properties deleted file mode 100644 index ed4e434b..00000000 --- a/dev/scylla/cassandra-rackdc.sam.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=sam -rack=rack1 diff --git a/dev/scylla/cassandra-rackdc.sas.properties b/dev/scylla/cassandra-rackdc.sas.properties deleted file mode 100644 index 132b7c85..00000000 --- a/dev/scylla/cassandra-rackdc.sas.properties +++ /dev/null @@ -1,2 +0,0 @@ -dc=sas -rack=rack1 diff --git a/dev/scylla/scripts/init.cql b/dev/scylla/scripts/init.cql deleted file mode 100644 index 2bb44235..00000000 --- a/dev/scylla/scripts/init.cql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE KEYSPACE IF NOT EXISTS nam WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS eur WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'eur': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS sam WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'sam': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS sas WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'sas': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS eas WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'eas': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS ocn WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'ocn': '1'} AND durable_writes = true AND tablets = {'enabled': true}; - -CREATE KEYSPACE IF NOT EXISTS gdpr WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'eur': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS nam_nt WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1'} AND durable_writes = true AND tablets = {'enabled': false}; -CREATE KEYSPACE IF NOT EXISTS global WITH replication = {'class': 'org.apache.cassandra.locator.NetworkTopologyStrategy', 'nam': '1', 'eur': '1', 'sam': '1', 'sas': '1', 'eas': '1', 'ocn': '1'} AND durable_writes = true AND tablets = {'enabled': true}; -CREATE KEYSPACE IF NOT EXISTS dummy WITH replication = {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true AND tablets = {'enabled': false}; diff --git a/dev/scylla/scylla.yaml b/dev/scylla/scylla.yaml deleted file mode 100644 index 071c214d..00000000 --- a/dev/scylla/scylla.yaml +++ /dev/null @@ -1,38 +0,0 @@ -cluster_name: 'Octocon' -num_tokens: 128 -commitlog_sync: periodic -commitlog_sync_period_in_ms: 10000 -commitlog_segment_size_in_mb: 32 -schema_commitlog_segment_size_in_mb: 128 - -seed_provider: - - class_name: org.apache.cassandra.locator.SimpleSeedProvider - parameters: - - seeds: "scylla-nam,scylla-eur" - -listen_address: localhost -native_transport_port: 9042 -native_shard_aware_transport_port: 19042 -read_request_timeout_in_ms: 5000 -write_request_timeout_in_ms: 2000 -cas_contention_timeout_in_ms: 1000 -endpoint_snitch: GossipingPropertyFileSnitch -rpc_address: localhost -rpc_port: 9160 -api_port: 10000 -api_address: 127.0.0.1 -#api_address: 100.100.127.{{ env "NOMAD_ALLOC_INDEX" }} -batch_size_warn_threshold_in_kb: 128 -batch_size_fail_threshold_in_kb: 1024 -partitioner: org.apache.cassandra.dht.Murmur3Partitioner -commitlog_total_space_in_mb: -1 -murmur3_partitioner_ignore_msb_bits: 12 -strict_is_not_null_in_views: true -maintenance_socket: ignore -tablets_mode_for_new_keyspaces: enabled -rf_rack_valid_keyspaces: false -max_partition_key_restrictions_per_query: 100000 -max_clustering_key_restrictions_per_query: 100000 - -experimental_features: - - views-with-tablets \ No newline at end of file diff --git a/dev/setup.sh b/dev/setup.sh deleted file mode 100755 index 8e428976..00000000 --- a/dev/setup.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -AIO_MAX_NR=$(cat /proc/sys/fs/aio-max-nr) -REQUIRED_AIO_MAX_NR=1048576 - -# Ensure /proc/sys/fs/aio-max-nr is set to a sufficiently high value -if [ "$AIO_MAX_NR" -lt "$REQUIRED_AIO_MAX_NR" ]; then - echo "ERROR: aio-max-nr is set to $AIO_MAX_NR, which is less than the required $REQUIRED_AIO_MAX_NR. ScyllaDB will not function properly unless this is increased." - echo "On most Linux systems, you can increase this value by editing /etc/sysctl.conf and adding the line:" - echo "fs.aio-max-nr = $REQUIRED_AIO_MAX_NR" - echo "Then run 'sudo sysctl -p' to apply the changes." - exit 1 -fi - -# Ensure Docker is installed -if ! command -v docker &> /dev/null; then - echo "ERROR: Docker could not be found. Please install Docker to proceed." - exit 1 -fi - -# Start the Docker containers using the development compose file -echo "Starting Docker containers..." -docker compose -f docker-compose.dev.yml up -d - -# If config/dev.exs does not exist, copy it from the example file -if [ ! -f config/dev.exs ]; then - echo "Creating config/dev.exs from template..." - cp config/dev-template.exs config/dev.exs -fi - -echo "Giving containers a few seconds to initialize..." -sleep 10 - -# Run the setup script inside the octocon-app container -echo "Running database setup script..." -source ./dev/bin/setup - -set +x - -echo "Setup complete! You can now run an Octocon development environment with an interactive shell with './dev/bin/iex'." -echo "NOTE: You will need to add a Discord token in config/dev.exs before the app will start properly." \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml deleted file mode 100644 index 84c4f1fa..00000000 --- a/docker-compose.dev.yml +++ /dev/null @@ -1,160 +0,0 @@ -services: - scylla-nam: - image: scylladb/scylla:2025.1 - ports: - - "9042:9042" - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-nam - volumes: - - ./dev/scylla/cassandra-rackdc.nam.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/nam:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - networks: - octo_network: - ipv4_address: 172.18.1.1 - - scylla-eur: - image: scylladb/scylla:2025.1 - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-eur - volumes: - - ./dev/scylla/cassandra-rackdc.eur.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/eur:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - depends_on: - - scylla-nam - networks: - octo_network: - ipv4_address: 172.18.1.2 - - scylla-sam: - image: scylladb/scylla:2025.1 - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-sam - volumes: - - ./dev/scylla/cassandra-rackdc.sam.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/sam:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - depends_on: - - scylla-nam - - scylla-eur - networks: - octo_network: - ipv4_address: 172.18.1.3 - - scylla-sas: - image: scylladb/scylla:2025.1 - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-sas - volumes: - - ./dev/scylla/cassandra-rackdc.sas.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/sas:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - depends_on: - - scylla-nam - - scylla-eur - - scylla-sam - networks: - octo_network: - ipv4_address: 172.18.1.4 - - scylla-eas: - image: scylladb/scylla:2025.1 - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-eas - volumes: - - ./dev/scylla/cassandra-rackdc.eas.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/eas:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - depends_on: - - scylla-nam - - scylla-eur - - scylla-sam - - scylla-sas - networks: - octo_network: - ipv4_address: 172.18.1.5 - - scylla-ocn: - image: scylladb/scylla:2025.1 - restart: always - command: --seeds=172.18.1.1,172.18.1.2 --smp 1 --memory 750M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0 --broadcast-address scylla-ocn - volumes: - - ./dev/scylla/cassandra-rackdc.ocn.properties:/etc/scylla/cassandra-rackdc.properties - - ./dev/scylla/data/ocn:/var/lib/scylla - - ./dev/scylla/scylla.yaml:/etc/scylla/scylla.yaml - depends_on: - - scylla-nam - - scylla-eur - - scylla-sam - - scylla-sas - - scylla-eas - healthcheck: - test: ["CMD", "/bin/bash", "-c", "cqlsh --execute='describe keyspaces'"] - interval: 15s - timeout: 10s - retries: 20 - networks: - octo_network: - ipv4_address: 172.18.1.6 - - scylla-load-keyspaces: - image: scylladb/scylla:2025.1 - depends_on: - scylla-ocn: - condition: service_healthy - volumes: - - ./dev/scylla/scripts/init.cql:/init.cql - entrypoint: ["/bin/bash", "-c", "echo Loading Scylla keyspaces... && cqlsh scylla-ocn -f /init.cql"] - deploy: - restart_policy: - condition: on-failure - delay: 5s - max_attempts: 5 - window: 120s - networks: - octo_network: - - msg-db: - image: timescale/timescaledb:latest-pg16 - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - PGDATA: /var/lib/postgresql/data/pgdata - restart: always - ports: - - "4200:5432" - volumes: - - ./msg_pgdata:/var/lib/postgresql/data - networks: - octo_network: - - octocon-app: - container_name: octocon-app - build: - context: . - dockerfile: dev/Dockerfile - ports: - - "4000:4000" - volumes: - - app_build:/build - - app_deps:/deps - - .:/app - user: root - depends_on: - scylla-ocn: - condition: service_healthy - networks: - octo_network: - -networks: - octo_network: - driver: bridge - ipam: - driver: default - config: - - subnet: 172.18.0.0/16 - -volumes: - app_build: - app_deps: \ No newline at end of file diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 00000000..63528242 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,1093 @@ +# Interfold configuration reference + +This is the developer / architecture reference for how Interfold gets configured. If you are +self-hosting and just want the short list of env vars you must set by hand, jump to the +[README configuration block](../README.md#configuration--integrations) — it links back here +for the deep dive. + +> **Audience.** Operators reading this should already understand Docker Compose, Postgres +> roles, and Kestrel HTTPS. Maintainers extending it should already know +> `IOptionsMonitor`, `IHostedLifecycleService`, and the Aspire AppHost model. + +## TL;DR + +There are four configuration layers. Each row in a layer eventually lands on a strongly-typed +options object inside the API process or on a seeded row inside Postgres. + + +| Layer | Source of truth | Where it lives at rest | Read by | +| ---------------------------- | ------------------------------------------- | --------------------------------------------------- | ---------------------------------- | +| **1. Compile-time defaults** | C# constants and property initialisers | Source code (`Interfold.Contracts.Configuration.*`) | DI binding helpers | +| **2. Operator file** | `deploy/interfold.bootstrap.json` | Operator's working copy | `Interfold.Bootstrapper` only | +| **3. Environment variables** | `deploy/.env` (compose-bound) + process env | `OCTOCON_*`, `ASPNETCORE_*`, `Parameters:*` | API at DI bind time | +| **4. `internal.secrets`** | Postgres row `internal.secrets` table | Inside the application database (default `interfold`, configurable via `BootstrapConfig.postgresDatabase`) | API at startup via `ISecretsStore` | + + +Layers cascade right-to-left at boot: env binds first, then `SecretsBootstrapService` +overlays values from `internal.secrets` on top of `AuthenticationConfiguration`. The leaf +PFX password takes a separate one-shot Postgres lookup *before* the host is built so Kestrel +can unlock the cert when it binds the HTTPS endpoint. + +## Layer 1 — Compile-time defaults + +Every option lives on a `sealed class` in `Interfold.Contracts.Configuration`. Each property +has a default chosen for safety, not convenience — if an operator forgets to set anything, +the defaults aim for "loud failure in production, useful behaviour in dev". + +Reference: + +- `[PersistenceConfiguration](../shared/Interfold.Contracts/Configuration/PersistenceConfiguration.cs)` +— DB mode, keyspace, retry/backoff knobs. +- `[AuthenticationConfiguration](../shared/Interfold.Contracts/Configuration/AuthenticationConfiguration.cs)` +— JWT signing keys, OAuth client IDs, deep-link HMAC secret, encryption pepper, challenge +scheme metadata. +- `[ApiConfiguration](../shared/Interfold.Contracts/Configuration/ApiConfiguration.cs)` — +frontend URLs, deep-link protocol. +- `[StorageConfiguration](../shared/Interfold.Contracts/Configuration/StorageConfiguration.cs)` +— local avatar storage root + public base URL. +- `[SocketConfiguration](../shared/Interfold.Contracts/Configuration/SocketConfiguration.cs)` +— WebSocket batch flush threshold. +- `[ObservabilityConfiguration](../shared/Interfold.Contracts/Configuration/ObservabilityConfiguration.cs)` +— OTLP endpoint. +- `[ClusterConfiguration](../shared/Interfold.Contracts/Configuration/ClusterConfiguration.cs)` +— node group (Primary / Auxiliary / Sidecar). +- `[TestingConfiguration](../shared/Interfold.Contracts/Configuration/TestingConfiguration.cs)` +— test-only gating switches. + +DI binding is centralised in +`[ConfigurationServiceCollectionExtensions.AddInterfoldOptions](../infrastructure/Interfold.Infrastructure/DependencyInjection/ConfigurationServiceCollectionExtensions.cs)`. +Each `Apply`* method is the single source of truth for that section's env → options mapping. + +## Layer 2 — `interfold.bootstrap.json` + +This file is the operator-facing input to the bootstrapper. It is *not* read by the API +directly — its values flow through the bootstrapper into either `secrets.json` (auto-generated +output) or the `internal.secrets` table (seeded by `DatabaseInitPhase`). + +Shape lives on `[BootstrapConfig](../tools/Interfold.Bootstrapper/Configuration/BootstrapConfig.cs)`: + +```jsonc +{ + "deployment": { + "outputDir": "./deploy", // artifact root (relative to cwd) + "hosts": [], // required, no default — DNS names / IPv4 / IPv6 / CIDR. + // See "Hosts" subsection below for the entry shapes + // and primary-host rule. Examples: + // ["api.example.com"] (DNS only) + // ["192.168.1.42"] (LAN box, no domain) + // ["api.example.com","fe80::1"] (mixed) + // ["api.example.com","10.0.0.0/8"] (DNS + CIDR scope) + "rootCaName":"Interfold Root CA", + "certYears": 5, + "trustStoreInstall": true, // add rootCA.crt to system trust store + "includeWeb": false, // ship the octocon-web (Kotlin/Wasm UI) container in + // the generated compose? Independent from `webHttps` + // below. Default false ships an API-only stack. Set + // true with `webHttps:false` to ship the wasm UI in + // HTTP-only mode (debugging / external-TLS-proxy + // stacks); `webHttps:true` auto-promotes this to + // true regardless of what's written here. + "webHttps": false // terminate TLS at octocon-web (see "webHttps and + // the HTTP->HTTPS redirect" note below) + }, + "ports": { + "apiHttp": 5000, + "apiHttps": 5001, + "webHttp": 8080, + "webHttps": 8081 + }, + "scyllaMode": "single", // "single" | "multi" | "cassandra" + "apiImage": "ghcr.io/azyyyyyy/interfold-api:latest", + "postgresDatabase": "interfold", // Postgres application DB name; any safe identifier + // matching ^[A-Za-z_][A-Za-z0-9_]{0,62}$. Becomes the + // Database= field on OCTOCON_POSTGRES_CONNECTION and + // the target of DatabaseInitPhase's CREATE DATABASE. + "clusterName": "InterfoldCluster", // Advertised CQL cluster identity. Lands on + // CASSANDRA_CLUSTER_NAME (Cassandra) and the + // --cluster-name CLI flag (Scylla). Pure metadata, + // visible via `SELECT cluster_name FROM system.local`. + // Allowed: 1..64 chars matching [A-Za-z0-9 ._-]. + "scyllaKeyspace": "nam", // Per-instance region identity. One of: + // nam | eur | sam | sas | eas | ocn | gdpr. + // Lands on OCTOCON_SCYLLA_KEYSPACE on the API container; + // also picks which regional keyspace this stack's API + // serves (`single`/`cassandra` modes only create one, + // so this and the migration target must agree). + "apiRuntime": { + // Three of the four are derivable from `deployment` + `ports` and may be left blank — + // the bootstrapper fills them at validate-time with values computed from `hosts`, + // `webHttps`, and the matching `ports.*` slot. The interactive form pre-fills the + // prompt with the same derived value. + "callbackBaseUrl": "", // empty -> https://{primary host}[:{ports.apiHttps}]; + // lands on OCTOCON_AUTH_CALLBACK_BASE_URL. The scheme is + // always `https` because the API container terminates + // HTTPS unconditionally in self-host (Kestrel binds the + // bootstrapper-issued leaf PFX independent of `webHttps`, + // which only governs the web container). The port + // suffix is dropped when `ports.apiHttps` is 443. + // (Primary host = first non-CIDR entry in `hosts`; IPv6 + // literals are bracket-wrapped per RFC 3986.) + "jwtAuthority": "", // empty -> https://{primary host}[:{ports.apiHttps}]; + // JWT `iss` claim. Same derivation as + // `callbackBaseUrl`. + "jwtAudience": "octocon", // JWT `aud` claim; no derivation, just a default. + "corsAllowedOrigins": [] // empty -> one entry per non-CIDR `hosts` entry of the + // form `{webScheme}://{host}[:{webPort}]`, where + // `webScheme` follows `webHttps` and `webPort` is the + // matching `ports.webHttps` / `ports.webHttp` value (port + // suffix dropped when default for the scheme: 80 for + // http, 443 for https). Joined with ',' for + // OCTOCON_CORS_ALLOWED_ORIGINS. CIDR entries are + // skipped because they have no URL form. An empty list + // at the API would fall back to "allow any origin", + // which the bootstrapper actively prevents in + // production. + }, + "persistence": { + // DB-retry strategy + per-request fan-out cap. All four have non-null defaults that + // match the API's compile-time fallbacks — leaving them at the defaults reproduces + // pre-bootstrapper behaviour 1:1. ConfigPhase.Validate enforces ranges and the + // `max >= initial` cross-check. + "dbRetryAttempts": 3, // 1..100; OCTOCON_DB_RETRY_ATTEMPTS + "dbRetryInitialDelayMs": 100, // 1..60000; OCTOCON_DB_RETRY_INITIAL_DELAY_MS + "dbRetryMaxDelayMs": 1500, // 1..600000; must be >= dbRetryInitialDelayMs + "hydrationMaxConcurrency": 8 // 1..1024; OCTOCON_HYDRATION_MAX_CONCURRENCY + }, + "cluster": { + // Node role used by the API for orchestration-aware decisions. Lower-cased on the + // API side; the bootstrapper validator enforces the three canonical values upfront. + "nodeGroup": "auxiliary" // primary | auxiliary | sidecar; OCTOCON_NODE_GROUP + // Fly.io stacks override via FLY_PROCESS_GROUP at + // runtime, which wins over OCTOCON_NODE_GROUP. + }, + "storage": { + // avatarStorageRoot is a HOST path bind-mounted at /app/data/avatars inside the API + // container. Blank → {outputDir}/data/avatars (created on publish). avatarPublicBase + // blank → the API serves /avatars/* itself; set an https URL to stamp CDN URLs. + "avatarStorageRoot": "", // absolute host path; blank = {outputDir}/data/avatars + "avatarPublicBase": "" // public http(s) URL prefix; OCTOCON_AVATAR_PUBLIC_BASE + }, + "observability": { + // OTLP gRPC endpoint the API exports traces and metrics to. Empty means the OTLP + // exporter is not registered (in-process telemetry still works). When set, must + // parse as an absolute http(s) URI. + "otlpEndpoint": "" // e.g. "http://localhost:4317"; OCTOCON_OTLP_ENDPOINT + }, + "socket": { + // Nullable int — `null` (the default) means "use the API's compile-time default". + // The JSON stores literal null rather than 0 so a re-bootstrap of a hand-edited + // file doesn't accidentally set the threshold to "flush every empty payload". + "batchBytesThreshold": null // 1..16777216 when set; OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD + }, + "oauth": { + // Per-provider OAuth credentials. Rows are paired (id then secret); each provider + // needs BOTH halves to register its ASP.NET Core challenge scheme. Leaving a + // provider's `*ClientId` empty disables that provider entirely (the scheme is + // skipped at startup) regardless of whether a secret is set. + "googleClientId": "1234.apps.googleusercontent.com", // empty -> provider disabled + "googleClientSecret": "...", // empty -> row skipped + "discordClientId": "", // empty -> provider disabled + "discordClientSecret": "", // empty -> row skipped + "appleClientId": "", // empty -> provider disabled + "appleClientSecret": "" // empty -> row skipped + }, + "backup": { + // Backup + autostart preferences consumed by the `backup` and `install-service` + // subcommands. Every field defaults to a "do nothing automatically" stance — + // operators opt in here, then re-run `install-service` to materialise the matching + // systemd units. See README.md "Backups" / "systemd integration" for the operator + // walkthrough. + "enabled": false, // master toggle for the scheduled-backup timer. + // When false, install-service does NOT enable + // interfold-backup.timer. The one-shot `backup` + // subcommand always works regardless. + "schedule": "daily", // systemd OnCalendar= expression. Accepts the + // shortcuts (hourly|daily|weekly|monthly) plus + // the full "DOW YYYY-MM-DD HH:MM:SS" form + // (e.g. "Mon..Fri 03:30"). Validated at install + // time by `systemd-analyze calendar`. + "retainCount": 14, // per-component archive count to retain. After + // every successful backup the oldest archives + // (by mtime) are deleted until exactly this + // many remain. Bounded 1..1000. + "directory": "", // blank -> {outputDir}/backups. Non-blank values + // must be ABSOLUTE — systemd timer invocations + // have an unpredictable CWD, so relative paths + // would not resolve consistently. + "autostartServer": false // when true, install-service enables + // interfold.service so `docker compose up -d` + // runs on every boot after docker.service. + // Independent of `enabled`. + }, + "update": { + // Docker image update preferences consumed by the `update-images` subcommand and by + // the systemd OnSuccess= drop-in that chains updates after successful backups. Every + // field defaults to a "manual updates only, no automatic rollback" stance — flip the + // toggles you want and re-run `install-service` to materialise the systemd chain. + // See README.md "Updating images" for the operator walkthrough. + "enabled": false, // master toggle for the systemd chain. When true, + // install-service writes + // interfold-backup.service.d/50-chain-update.conf + // with an OnSuccess=interfold-update.service + // directive so every successful scheduled backup + // triggers `update-images`. Requires systemd + // >= 249 (Ubuntu 22.04+ / Debian 12+); the + // install phase preflights the version and + // refuses on older hosts. Manual `update-images` + // works regardless of this toggle. + "healthCheckTimeoutSeconds": 180, // bounded 1..3600. Post-recreate deadline + // for the pg_isready + nodetool status + + // /health/ready probes combined. Any tier + // that doesn't clear its check by the deadline + // triggers the log-and-stop / auto-restore + // branch. + "autoRestoreOnFailure": false, // when true, a failed health check invokes + // `restore --force` against the pre-update + // archives inline. Default false: the phase + // just prints a copy-pasteable restore command + // and exits non-zero — operator decides. The + // CLI --auto-restore flag overrides this + // per-invocation. + "recreateOnUpdate": true, // when true (default), the phase runs + // `docker compose up -d` after a pull so + // compose recreates containers whose image + // ID moved. Set false only if you want a + // two-step manual recreate (pull now, `up -d` + // later during a maintenance window). + "services": [] // empty -> every compose service is pulled + + // recreated. Non-empty is a whitelist + // validated against the known compose service + // names (msg-db, scylla, scylla-*, cassandra, + // interfold-api, octocon-web). Use to update + // just the API without touching Postgres or + // Scylla, for example. + } +} +``` + +OAuth **client IDs** are public values that end up in each provider's authorize-redirect URL. +The bootstrapper carries them through as plain Aspire parameters (no masking, no +`internal.secrets` round trip) — `PublishPhase.BuildEnvReplacements` writes them straight +into `deploy/.env` as `GOOGLE_OAUTH_CLIENT_ID` / `DISCORD_OAUTH_CLIENT_ID` / +`APPLE_OAUTH_CLIENT_ID`, which the API container picks up as `OCTOCON_*_OAUTH_CLIENT_ID` +via `InterfoldAppHost.ConfigureApiSelfHostEnv`. The matching client **secrets** live in +`internal.secrets` (seeded by `DatabaseInitPhase`) and are patched onto +`AuthenticationConfiguration` by `SecretsBootstrapService` at API startup — they never +appear in `.env`. + +First-time operators don't need to hand-author this file — running `interfold-bootstrap` on +a real TTY without an existing `interfold.bootstrap.json` drops into a Spectre.Console +navigable form: every field on `BootstrapConfig` is shown as a menu row with its current +value next to its label, grouped under ten section headers (Deployment / Ports / +Database / API / Cluster & telemetry / Storage / Performance tuning / OAuth credentials / +Backup & autostart / Updates). +The operator arrow-keys between rows and presses Enter to edit any field (inline validation +re-prompts on bad input, OAuth client secrets are masked in both the editor echo and the +menu row; client IDs are shown verbatim because they're public), then chooses `Confirm and +save` to write the JSON. The four derivable `apiRuntime` rows pre-fill their menu display +and prompt default with the value `ConfigPhase.ResolveDerivedDefaults` computes from +`deployment` — operators can press Enter to accept or type to override, and either way the +bootstrapper persists the resolved value. The three "disabled when blank" rows (avatar +public base, OTLP endpoint, socket batch flush threshold) render an +`` / `` marker in the menu when unset, so the unset-vs-set distinction is +visible at a glance; leaving them blank reproduces the pre-bootstrapper "env var unset" +behaviour 1:1. Avatar storage root is also blankable but means "use `{outputDir}/data/avatars`" +rather than disabling uploads. There is no separate walkthrough phase: experienced operators jump straight +to the rows they care about and Confirm; first-time operators just Enter every row +top-to-bottom. The bootstrapper writes the resulting JSON to the path above on +confirmation; `--non-interactive` and `--config ` still bypass the form for +unattended runs. + +### Hosts (`deployment.hosts`) + +The `hosts` list is the single source of truth for where the deployed API will be +reachable. The field is required (no shipped default placeholder), and each entry is one +of the following shapes: + +| Shape | Example | Used as leaf SAN? | Used as Name Constraints subtree? | URL-primary eligible? | +| --------------- | ---------------------- | ----------------- | --------------------------------- | --------------------- | +| DNS name | `api.example.com` | yes (`dNSName`) | yes (`dNSName`) | yes | +| DNS wildcard | `*.example.com` | yes (`dNSName`) | yes (suffix only) | no | +| IPv4 literal | `192.168.1.42` | yes (`iPAddress`) | yes (`iPAddress` `/32`) | yes | +| IPv6 literal | `fe80::1` | yes (`iPAddress`) | yes (`iPAddress` `/128`) | yes | +| IPv4 CIDR | `192.168.1.0/24` | no | yes (`iPAddress` + explicit mask) | no | +| IPv6 CIDR | `fe80::/64` | no | yes (`iPAddress` + explicit mask) | no | + +The **primary host** is the first non-CIDR entry. It seeds the leaf cert subject CN, the +nginx `server_name`, and the derived `apiRuntime.callbackBaseUrl` / +`apiRuntime.jwtAuthority` URLs (always `https` for the API in self-host, with the +`:{ports.apiHttps}` suffix dropped only when the operator picked 443; IPv6 literals are +bracket-wrapped per RFC 3986 §3.2.2). Wildcard DNS entries cannot be the primary because +`*.example.com` is not a single host the leaf cert can serve — list the concrete primary +alongside the wildcard. + +CIDR entries are useful when you want the root CA's Name Constraints scope to cover an +entire subnet (for example a LAN where individual leaf certs are minted later) without +listing every host. CIDR entries with host bits set beyond the mask (`192.168.1.42/24`) +are rejected at parse time with a fix-it message — the operator must canonicalise the +network address (`192.168.1.0/24`) or pin a single host (`192.168.1.42/32`). + +**LAN-only quick start (no DNS at all):** + +```jsonc +{ + "deployment": { + "hosts": ["192.168.1.42"], // the box's static LAN IP + "rootCaName":"Interfold Root CA", + "certYears": 5, + "webHttps": true + } +} +``` + +The leaf cert gets an `iPAddress` SAN for `192.168.1.42`, the root CA's Name Constraints +pin to the same `/32`, and devices on the LAN that install the root CA validate +`https://192.168.1.42/` cleanly. + +> **Shipping the `octocon-web` (wasm UI) container.** Two independent toggles under +> `deployment` control whether the wasm UI ends up in the generated compose: +> +> - `deployment.includeWeb` (default `false`) — when `true`, ship the `octocon-web` +> container in HTTP-only mode. The upstream image (`ghcr.io/azyyyyyy/octocon-wasm:latest`) +> listens on `:8080`, so the bootstrapper maps `ports.webHttp` / `ports.webHttps` onto +> `:8080` and emits an HTTP healthcheck. No leaf cert or nginx envsubst template gets +> bind-mounted in this mode — it's intended for local debugging and for stacks fronted +> by an external TLS-terminating proxy. +> - `deployment.webHttps` (default `false`) — when `true`, terminate TLS at `octocon-web` +> itself (the path the rest of this section documents). Setting this implicitly forces +> `includeWeb` on (TLS termination requires the container that performs it), so +> operators who only want TLS don't need to flip both. +> +> Combinations: +> +> | `includeWeb` | `webHttps` | Result | +> | :----------: | :--------: | :------------------------------------------------ | +> | `false` | `false` | API-only stack (default). | +> | `true` | `false` | Wasm UI shipped HTTP-only (debug / external TLS). | +> | `false` | `true` | Wasm UI shipped with TLS termination at nginx. | +> | `true` | `true` | Same as the row above (`includeWeb` is implied). | +> +> **`webHttps` and the HTTP→HTTPS redirect.** When `deployment.webHttps` is `true`, the +> `octocon-web` container terminates TLS on its internal `:443` listener and answers +> plaintext `:80` requests with a `301` to the HTTPS variant. The redirect's `Location` +> header is stitched together inside nginx as `https://$host${NGINX_HTTPS_PORT_SUFFIX}…`, +> where `NGINX_HTTPS_PORT_SUFFIX` is set by `InterfoldAppHost.Configure` to +> `:{ports.webHttps}` (or empty when the operator picked `443`). This is the bit that lets +> the default port pair (`webHttp`/`webHttps` = `8080`/`8081`) actually work end-to-end — +> without it the browser would follow the 301 to `https:///`, resolve the missing +> port to `:443`, and hit a port that the bootstrapper hasn't bound. Operators fronting +> the stack with a reverse proxy that exposes a different public port should override +> `ports.webHttps` to that public port (or move the reverse proxy off `webHttp`/`webHttps` +> entirely and skip `octocon-web`'s HTTPS termination). + +> **Interactive auto-default.** When the bootstrapper runs interactively on a fresh box +> (no `interfold.bootstrap.json` yet) it pre-fills the *Public host(s)* row with the +> device's primary unicast IP — enumerated via `NetworkInterface.GetAllNetworkInterfaces()` +> and filtered to skip loopback, tunnels, link-local, APIPA, and named virtual bridges +> (Docker, k8s overlays, Hyper-V switches, WireGuard / Tailscale tunnels). The operator +> hits Enter to accept or types a replacement. The non-interactive path (`--non-interactive` +> with a JSON file) does **not** consult the detector — a file with an empty `hosts` list +> still fails fast with a clear validation error, by design. + +> **mDNS preflight for `.local` names (Linux only).** The bootstrapper runs a two-tier +> check whenever the finalised `deployment.hosts` list contains a `.local` entry: +> +> - **Pre-prompt banner (interactive fresh-config only).** Before the hosts row appears, +> the bootstrapper detects the device's short hostname, qualifies it as +> `{hostname}.local`, and probes whether it resolves via `getent hosts` (which +> traverses `nsswitch.conf` → `mdns_minimal` → avahi). If it does, the qualified name +> joins the auto-default alongside the primary IP. If it doesn't, a banner explains +> that mDNS is unavailable and offers to install `avahi-daemon` + the platform's NSS +> mdns module (`libnss-mdns` on Debian/Ubuntu, `nss-mdns` on Fedora/RHEL). Decline the +> offer and the `.local` name is simply omitted from the pre-fill — the row is still +> editable so the operator can type any host they like. +> - **Post-fill safety gate (all `bootstrap` runs).** After the hosts list is finalised +> (either by the interactive prompt or loaded from JSON), every `.local` entry is +> re-probed. Unresolvable ones are removed from `deployment.hosts` with a warning +> naming the specific hosts + a copy-pasteable install hint, and **the current +> `bootstrap` run continues to completion** with the reduced list. It never halts on +> this — the mutation is re-persisted so subsequent runs see the pruned list without +> re-emitting the warning. Re-running `bootstrap` after installing mDNS is *optional +> recovery* to restore the stripped entries, not a required next step. +> +> `--non-interactive` skips the pre-prompt banner (there's no operator to talk to) but +> the post-fill gate still runs and applies the same strip-and-continue behaviour to +> `.local` entries in the supplied config. Non-Linux platforms short-circuit both tiers +> because `getent`'s exit-code contract doesn't translate to Windows / macOS resolvers. +> +> To enable mDNS ahead of time so the strip never fires: +> +> ```bash +> # Debian / Ubuntu +> sudo apt-get install -y avahi-daemon libnss-mdns && sudo systemctl enable --now avahi-daemon +> +> # Fedora / RHEL +> sudo dnf install -y avahi nss-mdns && sudo systemctl enable --now avahi-daemon +> ``` + +## Layer 3 — Environment variables + +Two flavours: + +- **Compose-bound** — `deploy/.env` is templated by `PublishPhase.BuildEnvReplacements` from +the AppHost's `Parameters:`* declarations. Operators usually never touch `.env` by hand; +rerunning the bootstrapper rewrites it. +- **Operator-bound** — variables the bootstrapper does *not* manage. Operators export them +via systemd, a sibling `.env.local`, or whatever orchestration they use. See the +README's [Configuration & integrations](../README.md#configuration--integrations) for the +short operator-facing list. + +### Bootstrapper-installed systemd units + +`interfold-bootstrap install-service` materialises four systemd units to +`/etc/systemd/system/` (overridable via `--systemd-unit-dir`, used by integration tests). +All four are rendered from templates embedded in the bootstrapper binary; see +`tools/Interfold.Bootstrapper/Phases/SystemdTemplates/` for the source. + +| Unit | Type | What it does | +| -------------------------- | -------------------------- | ------------ | +| `interfold.service` | `oneshot` `RemainAfterExit=yes` | Brings the compose stack up via `/usr/bin/docker compose -f {outputDir}/docker-compose.yaml up -d` after `docker.service` on boot. Deliberately does NOT shell out to `interfold-bootstrap up` — that would re-run the 5-minute `/health/ready` wait inside systemd's boot critical path. Compose's own restart policy + the API container's healthcheck handle steady-state recovery. | +| `interfold-backup.service` | `oneshot` | Runs `interfold-bootstrap backup --config {configPath} --output-dir {outputDir} --component all`. Inherits the bootstrapper's `phase=...` log line format. Operators add drop-in overrides via `/etc/systemd/system/interfold-backup.service.d/*.conf`; the bootstrapper never edits drop-ins on rerun. | +| `interfold-backup.timer` | `timer` | Fires `interfold-backup.service` on `OnCalendar={config.backup.schedule}` with `Persistent=true` so a missed run (host powered off at the scheduled time) fires on next boot. | +| `interfold-update.service` | `oneshot` | Runs `interfold-bootstrap update-images --config {configPath} --output-dir {outputDir}`. Always rendered so manual invocations always have a target service; only the `OnSuccess=` drop-in that fires it from the backup schedule is conditional on `config.update.enabled`. Never enabled independently — the drop-in is what schedules it. | + +Conditional drop-in — written only when `config.update.enabled=true`: + +| Path | Contents | +| ----------------------------------------------------------------------- | ---------------------------------------------------- | +| `/etc/systemd/system/interfold-backup.service.d/50-chain-update.conf` | `[Unit]\nOnSuccess=interfold-update.service` | + +The drop-in makes a successful `interfold-backup.service` run trigger +`interfold-update.service`. Rendered idempotently: flipping `config.update.enabled` from +`true` to `false` and re-running `install-service` deletes the drop-in (the update +`.service` file stays for manual use). The `50-` numeric prefix leaves headroom for +operator-managed higher-priority drop-ins to override the chain via a +`90-local.conf` sibling. + +Enable/disable contract: + +- `install-service --enable-autostart` runs `systemctl enable --now interfold.service` + after writing the unit. The CLI flag defaults to `config.backup.autostartServer`, so + operators who set that in `interfold.bootstrap.json` get the boot service enabled on a + plain `install-service` invocation. +- `install-service --enable-backup-timer` runs `systemctl enable --now interfold-backup.timer`. + Defaults to `config.backup.enabled`. +- `interfold-update.service` is **never** enabled directly — the `OnSuccess=` drop-in is + what schedules it. Enabling it manually would create a boot-time update pass, which is + not the design goal. +- Both flags require `systemctl` to be on PATH; if it isn't (Windows / macOS dev box, + unprivileged container) the units still get written but no enable-step runs and the + log says `systemctl not on PATH; units written but not enabled`. +- `config.update.enabled=true` requires systemd >= 249 (the minimum version for the + `OnSuccess=` directive). The install phase parses `systemctl --version`, fails fast + on older hosts with a clear error naming Ubuntu 22.04 / Debian 12 as the minimum, and + skips the preflight entirely when update is disabled. + +Validation happens at install time: `systemd-analyze verify` runs against each rendered +unit and `systemd-analyze calendar` against the schedule string. Either failing aborts +the install with the analyzer's output — the unit files stay on disk so the operator can +inspect and edit them, but no `daemon-reload` happens. + +### Full env inventory + +Variables marked **(bootstrapper-managed)** are written into `deploy/.env` by the +publish phase. Variables marked **(operator)** must be supplied by hand. + +#### Persistence + + +| Env var | Default | Notes | +| ----------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | +| `OCTOCON_PERSISTENCE` | `scylla-postgres` | `scylla-postgres` or `inmemory`. (bootstrapper-managed) | +| `OCTOCON_POSTGRES_CONNECTION` | localhost fallback | Built from `Parameters:postgres-*` (including `Parameters:postgres-db`, default `interfold`, sourced from `BootstrapConfig.postgresDatabase`). (bootstrapper-managed) | +| `OCTOCON_SCYLLA_KEYSPACE` | `nam` | Per-instance region identity. **Single source.** No store fallback. Sourced from `BootstrapConfig.scyllaKeyspace` via Aspire parameter `scylla-keyspace`; restricted to the seven canonical regional values (`nam`/`eur`/`sam`/`sas`/`eas`/`ocn`/`gdpr`). (bootstrapper-managed) | +| `OCTOCON_SINGLE_SCYLLA_INSTANCE` | `true` (`single`/`cassandra`) / `false` (`multi`) | Whether the migration service creates all regional keyspaces or just one. (bootstrapper-managed) | +| `OCTOCON_DB_RETRY_ATTEMPTS` | `3` | Sourced from `BootstrapConfig.persistence.dbRetryAttempts` via Aspire parameter `db-retry-attempts`; bounded 1..100 by `ConfigPhase.Validate`. (bootstrapper-managed) | +| `OCTOCON_DB_RETRY_INITIAL_DELAY_MS` | `100` | Sourced from `BootstrapConfig.persistence.dbRetryInitialDelayMs` via Aspire parameter `db-retry-initial-delay-ms`; bounded 1..60000 and must be `<= dbRetryMaxDelayMs`. (bootstrapper-managed) | +| `OCTOCON_DB_RETRY_MAX_DELAY_MS` | `1500` | Sourced from `BootstrapConfig.persistence.dbRetryMaxDelayMs` via Aspire parameter `db-retry-max-delay-ms`; bounded 1..600000 and must be `>= dbRetryInitialDelayMs`. (bootstrapper-managed) | +| `OCTOCON_HYDRATION_MAX_CONCURRENCY` | `8` | Sourced from `BootstrapConfig.persistence.hydrationMaxConcurrency` via Aspire parameter `hydration-max-concurrency`; bounded 1..1024. (bootstrapper-managed) | + +`OCTOCON_COMPATIBILITY_MODE` was a "Postgres isn't reachable" escape hatch that forced +idempotency + token revocation into in-memory stores. It's been removed — Postgres is now +a hard dependency (`SecretsBootstrapService` requires `ISecretsStore` to load the +encryption pepper, and the API refuses to boot without it). If the value is still in your +`.env` it's a dead row — the API no longer reads it. + + +#### Authentication / OAuth + + +| Env var | Default | Notes | +| ------------------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| `OCTOCON_AUTH_CALLBACK_BASE_URL` | *empty* | Base URL the API's OAuth callbacks redirect to. Sourced from `BootstrapConfig.apiRuntime.callbackBaseUrl` via Aspire parameter `oauth-callback-base-url`; defaults derive to `https://{primary host}[:{ports.apiHttps}]` (first non-CIDR entry of `deployment.hosts`, always `https` because the API container terminates HTTPS unconditionally in self-host, port suffix omitted when `ports.apiHttps` is 443; IPv6 literals bracket-wrapped) when the operator leaves the field blank. (bootstrapper-managed) | +| `OCTOCON_JWT_AUTHORITY` | `octocon-local` | JWT `iss` claim. Sourced from `BootstrapConfig.apiRuntime.jwtAuthority` via Aspire parameter `jwt-authority`; derives the same way as `OCTOCON_AUTH_CALLBACK_BASE_URL`. The `octocon-local` default applies only to dev / non-bootstrapped runs. (bootstrapper-managed) | +| `OCTOCON_JWT_AUDIENCE` | `octocon` | JWT `aud` claim. Sourced from `BootstrapConfig.apiRuntime.jwtAudience` via Aspire parameter `jwt-audience`. Bound into `AuthenticationConfiguration.JwtAudience` by `ConfigurationServiceCollectionExtensions.ApplyAuthentication` (previously documented as bound but the binding was missing; fixed alongside the bootstrapper wire-up). (bootstrapper-managed) | +| `OCTOCON_GOOGLE_OAUTH_CLIENT_ID` | *empty* | Sourced from `BootstrapConfig.oauth.googleClientId` via Aspire parameter `google-oauth-client-id`; empty value disables the Google scheme. (bootstrapper-managed) | +| `OCTOCON_DISCORD_OAUTH_CLIENT_ID` | *empty* | Same handling, sourced from `oauth.discordClientId`. (bootstrapper-managed) | +| `OCTOCON_APPLE_OAUTH_CLIENT_ID` | *empty* | Same handling, sourced from `oauth.appleClientId`. (bootstrapper-managed) | +| `OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET` | *null* | Placeholder only; overwritten at startup from `internal.secrets:oauth:google:client_secret`. **Do not rely on the env value.** (bootstrapper-managed) | +| `OCTOCON_DISCORD_OAUTH_CLIENT_SECRET` | *null* | Same handling. (bootstrapper-managed) | +| `OCTOCON_APPLE_OAUTH_CLIENT_SECRET` | *null* | Same handling. (bootstrapper-managed) | + + +Each provider's authorize URL, ASP.NET Core challenge scheme name, and static challenge +query parameters (scopes / `response_type` / `response_mode`) are baked into +`OAuthChallengeServiceCollectionExtensions` as constants — every provider serves a single +global URL and the scopes are functionally tied to the data the API's callback handlers +extract, so neither can move without a code change. The scheme is only registered when +the matching `OCTOCON_*_OAUTH_CLIENT_ID` is set; leaving the client ID empty disables the +provider (`/auth/` falls through to 403). The client ID itself is injected +into the redirect URL by the scheme registration, so operators never have to thread it +through any other env var. + +Notably **absent** (intentionally — these moved to `internal.secrets`): + +- `OCTOCON_AUTH_RSA_`* / `OCTOCON_AUTH_RSA_*_FILE` +- `OCTOCON_AUTH_EC_*` / `OCTOCON_AUTH_EC_*_FILE` +- `OCTOCON_AUTH_DEEP_LINK_SECRET` +- `OCTOCON_ENCRYPTION_PEPPER` — now strictly store-resident under `encryption:pepper`; `SecretsBootstrapService` refuses to start the API if the row is missing. +- `ASPNETCORE_Kestrel__Certificates__Default__Password` + +Removed in favour of in-code constants: + +- `OCTOCON_AUTH_CHALLENGE_{GOOGLE,DISCORD,APPLE}_SCHEME` (scheme name is a fixed ASP.NET Core registration key) +- `OCTOCON_AUTH_CHALLENGE_{GOOGLE,DISCORD,APPLE}_ENDPOINT` (provider authorize URLs are stable, single-region values) +- `OCTOCON_AUTH_CHALLENGE_{GOOGLE,DISCORD,APPLE}_PARAMS` (scopes + `response_type` + `response_mode` are tied to what the callback handlers consume — changing them needs a code change) + +If any of these are still set in your `.env`, they are dead values — the API no longer +reads them. + +#### CORS + +| Env var | Default | Notes | +| ------------------------------ | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `OCTOCON_CORS_ALLOWED_ORIGINS` | *empty* (= allow any in dev only) | Comma-separated allow-list of origin URLs. The API still falls back to "allow any origin" when this is unset/empty, but the bootstrapper never emits a stack with an unset value — `BootstrapConfig.apiRuntime.corsAllowedOrigins` defaults to one `{scheme}://host` entry per non-CIDR `deployment.hosts` entry (joined with `,`), routed through Aspire parameter `cors-allowed-origins`. Operators that want a different allow-list edit the list in the interactive form or the JSON. (bootstrapper-managed) | + +`OCTOCON_FRONTEND`, `OCTOCON_BETA_FRONTEND`, and `OCTOCON_DEEPLINK_ADDRESS` have all been +removed. Their CORS allow-list use moved to `OCTOCON_CORS_ALLOWED_ORIGINS`; their +post-OAuth redirect-base use went away with the new client contract: clients are now +responsible for passing `redirect_uri` on the initial `GET /auth/{provider}` (or +`GET /auth/link/{provider}`), and the API surfaces a `400 missing_redirect_uri` instead +of falling back to a server-configured default. If any of the three are still set in +your `.env`, they are dead values — the API no longer reads them. + + +#### Storage + + +| Env var | Default | Notes | +| ----------------------------- | ------- | ---------- | +| `OCTOCON_AVATAR_STORAGE_ROOT` | `/app/data/avatars` (self-host compose) | Container path the API writes uploaded avatars to. Always baked to `/app/data/avatars` by AppHost; the host directory comes from `BootstrapConfig.storage.avatarStorageRoot` (blank → `{outputDir}/data/avatars`) via a compose bind mount. (bootstrapper-managed) | +| `OCTOCON_AVATAR_PUBLIC_BASE` | *empty* (= API serves `/avatars/*`) | Public URL prefix the API uses to construct avatar URLs in responses (e.g. `https://cdn.example.com/avatars/`). Sourced from `BootstrapConfig.storage.avatarPublicBase` via Aspire parameter `avatar-public-base`; empty normalised to `null`. Non-empty values must parse as absolute http(s) URLs. (bootstrapper-managed) | + + +#### Observability + + +| Env var | Default | Notes | +| ----------------------- | ------- | ------------------------------------------------------------ | +| `OCTOCON_OTLP_ENDPOINT` | *empty* (= OTLP exporter not registered) | gRPC OTLP endpoint, e.g. `http://localhost:4317`. Sourced from `BootstrapConfig.observability.otlpEndpoint` via Aspire parameter `otlp-endpoint`; empty normalised to `null` by `ApplyObservability` so the OTLP exporter is not registered. Non-empty values must parse as absolute http(s) URIs. (bootstrapper-managed) | + + +#### Cluster + + +| Env var | Default | Notes | +| -------------------- | ----------- | ------------------------------------------------- | +| `FLY_PROCESS_GROUP` | *null* | Fly.io automatic. Wins over `OCTOCON_NODE_GROUP`. | +| `OCTOCON_NODE_GROUP` | `auxiliary` | Sourced from `BootstrapConfig.cluster.nodeGroup` via Aspire parameter `node-group`; restricted to `primary` / `auxiliary` / `sidecar` by `ConfigPhase.Validate` (lower-cased on read by `ApplyCluster`). (bootstrapper-managed) | + + +#### Socket + + +| Env var | Default | Notes | +| -------------------------------------- | ----------------------------- | ---------- | +| `OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD` | *empty* (= API uses built-in default) | Bytes threshold the API flushes a batched WebSocket payload at. Sourced from `BootstrapConfig.socket.batchBytesThreshold` (nullable int) via Aspire parameter `socket-batch-bytes-threshold`; empty/null serialises as the empty string and `ApplySocket`'s `TryParseInt` returns `null` so the API's compile-time default still applies. When set, bounded 1..16 MiB. (bootstrapper-managed) | + + +#### Kestrel (self-host) + + +| Env var | Default | Notes | +| ------------------------------------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------ | +| `ASPNETCORE_HTTP_PORTS` | `5100` | Set by AppHost from `Ports:api-container-http`. (bootstrapper-managed) | +| `ASPNETCORE_HTTPS_PORTS` | `5101` | Set by AppHost from `Ports:api-container-https`. (bootstrapper-managed) | +| `ASPNETCORE_Kestrel__Certificates__Default__Path` | `/certs/leaf.pfx` | Set by AppHost. (bootstrapper-managed) | +| *password is **not** an env var* | — | Fetched from `internal.secrets:certs:leaf_pfx_password` before Kestrel binds. See [boot ordering](#boot-ordering). | + +The API image itself (`/Dockerfile`) does not pin its listening ports — AppHost owns them +end-to-end via the env vars above, plus the matching compose `targetPort` and the +`curl http://localhost:/health/ready` healthcheck. Operators who want to run the +container standalone (outside AppHost) need to set `ASPNETCORE_URLS` themselves; otherwise +ASP.NET Core falls through to its built-in default of `http://+:8080`. + + +#### Trust artefact paths + +| Env var | Default | Notes | +| ---------------------------------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `OCTOCON_TRUST_ROOT_CA_PATH` | `/certs/rootCA.crt` | Path inside the API container to the PEM-encoded root CA. Served at `/.well-known/interfold-root-ca.{crt,pem}` by `TrustController`. Empty/missing (dev `aspire run` with no `/certs` bind mount) makes the routes 404 instead of erroring. (bootstrapper-managed) | +| `OCTOCON_TRUST_ROOT_CA_FINGERPRINT_PATH` | `/certs/rootCA.sha256.txt` | Path to the SHA-256 fingerprint sidecar (uppercase colon-hex). Served at `/.well-known/interfold-root-ca.sha256` and reused as the HTTP `ETag` on the cert routes so `rotate-certs` invalidates downstream caches automatically. Empty drops the ETag and 404s the .sha256 route. (bootstrapper-managed) | + +See [Trust distribution](#trust-distribution) below for the fetch-and-verify recipe, +the `bootstrap show-trust` command, the Name Constraints invariant, and the +unsupported-clients list. + + +#### Testing + + +| Env var | Default | Notes | +| ------------------------------------ | ----------- | ----------- | +| `OCTOCON_RUN_API_INTEGRATION` | `false` | (test-only) | +| `OCTOCON_RUN_LIVE_INTEGRATION` | `false` | (test-only) | +| `OCTOCON_TEST_SCYLLA_CONTACT_POINTS` | `127.0.0.1` | (test-only) | +| `OCTOCON_TEST_SCYLLA_USERNAME` | `cassandra` | (test-only) | +| `OCTOCON_TEST_SCYLLA_PASSWORD` | `cassandra` | (test-only) | +| `OCTOCON_TEST_REGION` | `nam` | (test-only) | + + +## Trust distribution + +The bootstrapper issues its own root CA and signs every leaf cert with it. Devices that +don't already trust the root must install it before they can talk TLS to the API; the +combination of a `/.well-known/` download surface, a SHA-256 fingerprint OOB channel, +and a critical Name Constraints extension on the root cap the blast radius of both +"untrusted certificate" UX failures and a worst-case CA-key compromise. + +### Artefacts (`deploy/certs/`) + +| File | Purpose | Mode | +| --------------------- | -------------------------------------------------------------------------------------------------------- | ---- | +| `rootCA.crt` | Public root CA cert (PEM). Bind-mounted into the API at `/certs/rootCA.crt`. | 0644 | +| `rootCA.key` | CA private key (PKCS#8 PEM). Never leaves the host; the bind mount is RO and the API process can't read it because the file is owned-read-only. | 0600 | +| `rootCA.sha256.txt` | SHA-256 fingerprint of `rootCA.crt` in uppercase colon-hex (matches `openssl x509 -fingerprint -sha256`). | 0644 | +| `leaf.crt` / `leaf.key` / `leaf.pfx` | Leaf cert + key. PFX password lives in `internal.secrets:certs:leaf_pfx_password`. | 0644 | + +### Endpoints (`/.well-known/interfold-root-ca.*`) + +`[TrustController](../apis/Interfold.Ops.Api/Controllers/TrustController.cs)` serves a +hard-coded allowlist of three routes off the IANA `.well-known` prefix: + +| Route | Content-Type | Body | +| ---------------------------------- | ----------------------------------------------------------------------------------------- | --------------------------------------------------- | +| `interfold-root-ca.crt` | `application/pkix-cert` (default) / `application/x-x509-ca-cert` (when `Accept` opts in) | Root CA in DER | +| `interfold-root-ca.pem` | `application/x-pem-file` | Root CA in PEM (the verbatim on-disk bytes) | +| `interfold-root-ca.sha256` | `text/plain; charset=utf-8` | One line: SHA-256 fingerprint in uppercase colon-hex | + +All three return `Cache-Control: public, max-age=60, must-revalidate` and an `ETag` +derived from the fingerprint file. `rotate-certs` rewrites the fingerprint, which +changes the ETag, which invalidates downstream caches on the next conditional request. + +Returns 404 on every route when the bootstrapper hasn't supplied trust paths (dev mode +with no `/certs` bind mount, or a deployment that explicitly disables them). Hardcoded +allowlist: no user input ever joins a file path, `rootCA.key` is not on the allowlist, +and the file's 0600 owner-only mode means even a path-traversal regression here cannot +read it from the API process. + +### Operator fingerprint surface + +`bootstrap show-trust` is a read-only command that loads the on-disk root CA and prints: + +```text +Root CA: Interfold Root CA + Path: /…/deploy/certs/rootCA.crt + SHA-256: AA:BB:CC:…:99 + Not after: 2030-01-01 12:00:00 UTC + Distribute: curl -fSL http://:5000/.well-known/interfold-root-ca.crt -o rootCA.crt + Verify: openssl x509 -in rootCA.crt -noout -fingerprint -sha256 + (compare the printed SHA256 Fingerprint to the value above) +``` + +The same block prints at the end of every `bootstrap` / `bootstrap rotate-certs` +invocation. `show-trust` short-circuits in `Orchestrator.RunAsync` before any other +phase runs, so it's safe to invoke from an ops jumphost without an +`interfold.bootstrap.json` and won't touch the running stack. + +### End-user fetch-and-verify recipe + +The whole point of the fingerprint is to let an end user verify the cert they just +downloaded came from the operator and wasn't substituted by a network attacker: + +```bash +# 1. Operator broadcasts the fingerprint via Slack / email / Keybase / etc. +EXPECTED="AA:BB:CC:DD:…:99" + +# 2. User fetches the cert. Plain HTTP is fine here — the SHA-256 is what makes this safe. +curl -fSL http://api.example.com:5000/.well-known/interfold-root-ca.crt -o rootCA.crt + +# 3. User computes the fingerprint and compares character-for-character. +openssl x509 -in rootCA.crt -noout -fingerprint -sha256 +# SHA256 Fingerprint=AA:BB:CC:DD:…:99 + +# 4. ONLY after the fingerprints match: install into the device trust store. +sudo cp rootCA.crt /usr/local/share/ca-certificates/interfold-root-ca.crt +sudo update-ca-certificates +``` + +The OOB channel (Slack / email / Signal) is load-bearing. This is the standard +**trust-on-first-use (TOFU)** bootstrap pattern — the same shape SSH host-key +fingerprints, GPG key-signing parties, and Signal safety numbers all use: the in-band +download supplies the bytes, the out-of-band channel supplies the *authenticity +binding*. Strip the OOB step and the scheme reduces to a "leap of faith" — an attacker +who can MITM the plain-HTTP request can substitute a different root CA and the user has +no in-band way to detect it. TOFU explicitly does not defend against an active attacker +present at first contact ([RFC 7435 §1.1][rfc7435-1-1]); the OOB fingerprint comparison +is what closes that gap. [RFC 4949][rfc4949] formalises both terms — its "out-of-band" +tutorial calls out distributing "a root key" as the canonical example, which is +literally what we're doing here. + +[rfc7435-1-1]: https://www.rfc-editor.org/rfc/rfc7435#section-1.1 +[rfc4949]: https://www.rfc-editor.org/rfc/rfc4949 + +### Name Constraints invariant + +The root CA carries a critical Name Constraints extension (RFC 5280 §4.2.1.10) whose +`permittedSubtrees` is the operator's `deployment.hosts` list: + +- **DNS** entries (`api.example.com`, `*.example.com`) emit a `dNSName` permittedSubtree. + Wildcard entries collapse to their suffix because dNSName subtree semantics already + cover sub-labels and `*` is not a legal IA5String value. +- **IPv4 / IPv6** literals emit an `iPAddress` permittedSubtree with an all-ones mask + (`/32` for IPv4, `/128` for IPv6) — the leaf cert carries a matching `iPAddress` SAN so + a client browsing `https://192.168.1.42` validates cleanly. +- **CIDR** entries (`192.168.1.0/24`, `fe80::/64`) emit an `iPAddress` permittedSubtree + with the operator-supplied prefix. CIDR widens the *root CA's permitted scope* without + itself appearing on any leaf SAN — the operator must still list the specific hosts they + want the leaf cert to serve. + +A leaked CA private key therefore cannot mint a trusted cert for any host outside the +operator's configured set — every device that installed this root will reject the +impostor at chain validation. The blast radius of a key compromise is the operator's +host set, not the public internet. + +Trade-off: a handful of older or embedded TLS stacks don't support Name Constraints +and will reject the leaf chain entirely. Known unsupported clients: + +- Android < 7 +- Java < 8u101 +- Several embedded TLS stacks (consult device docs) + +Modern curl, browsers, OpenSSL ≥ 1.0.1, and .NET ≥ 6 handle Name Constraints +correctly. There is no opt-out: the constraint is critical so a non-supporting client +would otherwise silently treat the extension as absent, defeating the security +property entirely. + +### Rotation + +`bootstrap rotate-certs` regenerates the root CA, leaf, and fingerprint. Operator +consequences: + +1. Every client device that previously installed the old root must install the new + one. `CertificatePhase` prints a prominent two-line warning to that effect when it + runs in rotate mode, immediately after the trust-info block so the new fingerprint + is right above the call-to-action. +2. Cached `.well-known` responses pick up the new bytes on the next conditional + request because the ETag changes with the fingerprint. +3. The CA private key permissions (0600) are re-applied even on idempotent reruns, + so upgrading from a pre-hardening install backfills the lockdown without needing + an explicit rotate. `rootCA.sha256.txt` is similarly backfilled on first run after + upgrading, with no regeneration of the CA. + +## Layer 4 — `internal.secrets` + +The `internal.secrets` table is the durable, in-cluster source of truth for everything +sensitive that the API needs at runtime. It is: + +- created by `DatabaseInitPhase` inside the bootstrapper, owned by the `_admin` role; +- read-only granted to the app `interfold` user; +- seeded once on first bootstrap and re-seeded whenever the bootstrapper runs (writes are +idempotent — empty values are skipped to avoid clobbering operator-set rows). + +Row inventory (see `[SeedKeys.cs](../shared/Interfold.DatabaseBootstrap/SeedKeys.cs)`): + + +| Key | Origin | Consumer | Empty-skip? | +| ----------------------------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ----------- | +| `oauth:google:client_secret` | `BootstrapConfig.OAuth.GoogleClientSecret` | `SecretsBootstrapService` → `AuthenticationConfiguration.GoogleOAuthClientSecret` | yes | +| `oauth:discord:client_secret` | `BootstrapConfig.OAuth.DiscordClientSecret` | `SecretsBootstrapService` → `AuthenticationConfiguration.DiscordOAuthClientSecret` | yes | +| `oauth:apple:client_secret` | `BootstrapConfig.OAuth.AppleClientSecret` | `SecretsBootstrapService` → `AuthenticationConfiguration.AppleOAuthClientSecret` | yes | +| `encryption:pepper` | `GeneratedSecrets.EncryptionPepper` | `SecretsBootstrapService` → `AuthenticationConfiguration.EncryptionPepper` | no | +| `postgres:admin_username` | constant `interfold_admin` | `PostgresMigrationService` (DDL connection) | no | +| `postgres:admin_password` | `GeneratedSecrets.PostgresAdminPassword` | `PostgresMigrationService` | no | +| `scylla:admin_username` | `GeneratedSecrets.ScyllaUser + "_admin"` | `ScyllaMigrationService` (keyspace DDL) | no | +| `scylla:admin_password` | `GeneratedSecrets.ScyllaAdminPassword` | `ScyllaMigrationService` | no | +| `scylla:contact_points` | AppHost-resolved Scylla host | `ScyllaSessionProvider` / health checker | no | +| `scylla:local_datacenter` | `nam` | `ScyllaSessionProvider` | no | +| `scylla:username` | `GeneratedSecrets.ScyllaUser` | `ScyllaSessionProvider` (app session) | no | +| `scylla:password` | `GeneratedSecrets.ScyllaPassword` | `ScyllaSessionProvider` | no | +| `scylla:port` | `Ports.scylla` (default `9042`) | `ScyllaSessionProvider` | no | +| `auth:jwt_rsa256_private_pem` | `GeneratedSecrets.JwtRsa256PrivateKeyPem` | `SecretsBootstrapService.PatchRsa256` — populates `Rsa256PrivateKey` + derives `Rsa256PublicKey` | yes | +| `auth:jwt_es256_private_pem` | `GeneratedSecrets.JwtEs256PrivateKeyPem` | `SecretsBootstrapService.PatchEs256` — populates `JwtEs256PrivateKeyPem` + seeds `JwtEs256VerificationKeyPems[0]` | yes | +| `auth:deep_link_secret` | `GeneratedSecrets.DeepLinkSecret` | `SecretsBootstrapService` → `AuthenticationConfiguration.DeepLinkSecret` | yes | +| `certs:leaf_pfx_password` | `GeneratedSecrets.LeafPfxPassword` | `SecretsPreBuildLoader` — injected into `IConfiguration[Kestrel:Certificates:Default:Password]` before host build | yes | +| `firebase:client:android` | `FirebasePhase` parses `google-services.json` | `SecretsBootstrapService` → `FirebaseClientConfiguration.Android` — served by `GET /api/settings/firebase-config?platform=android` | yes | +| `firebase:client:ios` | `FirebasePhase` parses `GoogleService-Info.plist` | `SecretsBootstrapService` → `FirebaseClientConfiguration.Ios` — served by `GET /api/settings/firebase-config?platform=ios` | yes | +| `firebase:client:web` | `FirebasePhase` reads `firebase-web-config.json` | `SecretsBootstrapService` → `FirebaseClientConfiguration.Web` — served by `GET /api/settings/firebase-config?platform=web` | yes | +| `fcm:service_account_json` | `FirebasePhase` reads verbatim from operator-supplied file | `IFCMService` DI factory in `ClusterServiceCollectionExtensions` — absent row → `NullFCMService` fallback, present → `FirebaseFCMService` | yes | + + +> **Deliberately absent:** there is no `scylla:keyspace` row. Keyspace is per-node region +> identity and must come from the deployment env (`OCTOCON_SCYLLA_KEYSPACE`), not from a +> shared cluster row. + +The OAuth client-IDs do **not** appear in this table by design. They are public values; the +asymmetric split (IDs in env, secrets in store) is intentional and the seed list reflects it. + +### Firebase / FCM + +Push notifications are optional — a deployment can ship without any of the four Firebase +rows seeded and the whole flow degrades gracefully: + +- The three `firebase:client:*` rows carry the **public** per-platform init payloads that + the mobile / wasm clients fetch at runtime via `GET /api/settings/firebase-config?platform=…` + instead of baking the values into each build. They are safe to hand out anonymously — + they authorise nothing on their own. An absent row makes the endpoint return `503 + firebase_config_unavailable` for that platform; the client's `FirebaseConfigProvider` + falls back to its offline behaviour (no push, but the rest of the app still works). +- `fcm:service_account_json` carries the **private** FCM v1 service-account credential + the API uses to *send* notifications. This is a full Google Cloud IAM credential and + must be protected as such. When absent, the `IFCMService` DI factory selects + `NullFCMService` so `FrontNotifierBackgroundService` no-ops the send path — no + fronting-change push, no crash, no operator action required. + +Operators supply the source files via `BootstrapConfig.firebase.*`: + +| Field | File the operator points at | How to obtain it | +| ------------------------------------ | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| `firebase.androidConfigPath` | `google-services.json` | Firebase Console → Project Settings → General → Your apps → Android → *google-services.json* | +| `firebase.iosConfigPath` | `GoogleService-Info.plist` | Firebase Console → Project Settings → General → Your apps → iOS → *GoogleService-Info.plist* | +| `firebase.webConfigPath` | flat `firebase-web-config.json` (matches the client DTO 1:1) | Firebase Console → Project Settings → General → Your apps → Web → *SDK setup and configuration* + inject the VAPID key from Cloud Messaging → Web configuration → Web Push certificates | +| `firebase.serviceAccountPath` | FCM v1 service-account credential JSON | Firebase Console → Project Settings → Service accounts → *Generate new private key* | + +The `firebase-phase` in the bootstrapper (`Interfold.Bootstrapper/Phases/FirebasePhase.cs`) +ingests each configured file, reshapes the platform-specific inputs into the snake-case +JSON the API deserialises, and threads the four resulting seed strings into +`PostgresSeedOptions.FirebaseAndroidClientJson`/`FirebaseIosClientJson`/ +`FirebaseWebClientJson`/`FcmServiceAccountJson`. `PostgresSeeder.BootstrapAsync` then +writes them as `internal.secrets` rows in the same idempotent pass as the OAuth secrets. + +Every path is optional — leaving one blank is the supported "skip this input" shape. +A non-empty path that doesn't resolve to a file OR that fails to parse is a hard +bootstrap failure, so a typo surfaces at bootstrap time rather than at first API +request. Store the source files with the same 0600 filesystem mode as +`secrets/secrets.json` — the FCM service-account credential in particular is a +long-lived Google Cloud IAM key. + +## Boot ordering + +The order of operations on a self-hosted API container start is: + +```mermaid +sequenceDiagram + autonumber + participant Operator + participant Compose as Docker Compose + participant API as API Program.cs + participant PG as Postgres (internal.secrets) + participant Host as ASP.NET Core host + participant Kestrel + participant SBS as SecretsBootstrapService
(IHostedLifecycleService) + participant Mig as Migration services + + Operator->>Compose: docker compose up -d + Compose->>API: start container, exec dotnet Interfold.Api + API->>API: build IConfiguration
(env + appsettings) + API->>PG: SELECT value WHERE key='certs:leaf_pfx_password' + PG-->>API: leaf PFX password + API->>API: inject into Kestrel:Certificates:Default:Password + API->>Host: builder.Build() + Host->>Kestrel: bind HTTPS endpoint (loads leaf.pfx with the password) + Host->>SBS: StartingAsync + SBS->>PG: load auth:* + oauth:* + encryption:pepper rows + SBS->>SBS: patch IOptionsMonitor + Host->>Mig: StartingAsync (Postgres + Scylla migrations) + Mig->>PG: read admin credentials, run DDL + Host->>API: StartedAsync; serve traffic +``` + + + +Two ordering invariants are critical: + +1. **Kestrel ↔ leaf PFX password.** Kestrel reads + `Kestrel:Certificates:Default:Password` out of `IConfiguration` *during* `builder.Build()` + (specifically, when it binds the HTTPS endpoint). `IHostedLifecycleService.StartingAsync` + runs *after* the host is built, which is too late. Hence the dedicated `NpgsqlConnection` + query in `Program.cs` *before* `builder.Build()`. The failure mode if Postgres is + unreachable here is "the API fails to start before binding" — louder than a missing + cert at request time, and the deliberate trade-off documented in the source comment. +2. `**SecretsBootstrapService` ↔ migration services.** Migration services need the admin + credentials from `internal.secrets` (`postgres:admin_password`, `scylla:admin_`*). They + read those directly via `ISecretsStore`, so they don't actually depend on + `SecretsBootstrapService`. But the auth options consumed by controllers (JWT private + keys, deep-link secret) *do* depend on `SecretsBootstrapService` having patched them + first. .NET's `IHostedLifecycleService.StartingAsync` runs all registered services + concurrently, but the API doesn't accept requests until `StartedAsync` returns, so any + controller that reads `IOptionsMonitor` already sees + patched values. + +## Migration ledger + +Both database providers track which embedded migrations have already been applied so +subsequent startups skip already-applied files instead of relying purely on `IF NOT EXISTS` +guards in the SQL/CQL. The ledger also detects post-deploy edits to applied files +(SHA-256 checksum drift) and refuses to start the API until the drift is resolved. + +### Where the ledger lives + +| Provider | Table | Scope key | Created by | +| -------- | ---------------------------------------------------------------- | ---------------------- | ----------------------------------------------------- | +| Postgres | `internal.schema_migrations` (`version` primary key) | filename only | `PostgresMigrationService.EnsureLedgerAsync` | +| Scylla | `global.schema_migrations` (`PRIMARY KEY (scope, version)`) | `(keyspace, filename)` | `ScyllaMigrationService.EnsureLedgerAsync` | + +Each row records `checksum` (hex SHA-256 of the embedded resource bytes), `applied_at`, +`duration_ms`, and `applied_by` (the assembly informational version of the runner that +wrote the row). For Scylla the `scope` column is the regional keyspace name for the per- +region templates (`001_create_interfold_keyspaces.cql`, +`002_create_interfold_schema.templated.cql`) and `grants:` for the GRANT loop +(version = `grants_v1`, bump that constant in +`[ScyllaMigrationService.cs](../infrastructure/Interfold.Infrastructure.Scylla/ScyllaMigrationService.cs)` +when the grant set changes). + +### Bootstrap order + +- **Postgres:** the runner acquires the session-level + `pg_advisory_lock(MigrationAdvisoryLockId)` first, then `CREATE SCHEMA IF NOT EXISTS + internal` + `CREATE TABLE IF NOT EXISTS internal.schema_migrations` *before* querying + the ledger. Each not-yet-applied migration body and its ledger insert run inside the + same `NpgsqlTransaction` so a partial failure leaves no orphan row. +- **Scylla:** the runner renders `000_create_singleton_keyspaces.cql` unconditionally to + create `global` / `nam_nt` / `dummy` (the only "always-run" bootstrap step, untracked + because `global` is the precondition for the ledger itself), then creates + `global.schema_migrations` and reads it. Per-keyspace files (`001_*`, + `002_*.templated.cql`) are then rendered and applied per regional keyspace, with each + ledger insert issued as `INSERT IF NOT EXISTS` so concurrent migrators converge on a + single row. + +### Drift behaviour + +If a recorded migration's recomputed checksum no longer matches the row, the runner +throws `InvalidOperationException` mentioning the version and both checksums and the API +refuses to start. Migration files are immutable once applied. To intentionally repurpose +an existing version (e.g., you re-wrote the file and want to mark it re-applied), pick the +appropriate path: + +- **Force re-record (no DB change required):** update the ledger checksum to the new file + hash before restart. The runner will see the row exists with the new checksum and skip + the file body next start. Compute the new checksum locally with `sha256sum` and: + - Postgres: + `UPDATE internal.schema_migrations SET checksum = '' WHERE version = '';` + - Scylla: + `UPDATE global.schema_migrations SET checksum = '' WHERE scope = '' AND version = '';` +- **Force re-run from scratch (rare):** delete the ledger row(s) for the file. The runner + will treat the file as new on the next start, run the body, and re-insert the row. Only + do this if every statement in the file remains idempotent (`CREATE … IF NOT EXISTS`, + etc.) — the runner does not roll back the schema before re-applying. + +Both rewrites require admin credentials (the app user has no access to `internal.secrets`- +adjacent tables or `global.schema_migrations`); use the same `postgres:admin_*` / +`scylla:admin_*` rows the runner consumes. + +## Rotation story + +Three independent rotation surfaces, each with a single command: + + +| Rotation | Command | What changes | What stays | +| ----------------- | --------------------------------------------------- | ------------------------------------------------------------------------------------ | --------------------------------------------------------------------- | +| **Secrets** | `interfold-bootstrap rotate-secrets` | All DB passwords, encryption pepper, JWT RSA + ES256 keypairs, deep-link HMAC secret | Certs (root CA + leaf), leaf PFX password (preserved across rotation) | +| **Certs** | `interfold-bootstrap rotate-certs` | Root CA + leaf cert/key, leaf PFX wrapper (re-encrypted with the existing password) | All secrets | +| **OAuth secrets** | edit `interfold.bootstrap.json` → rerun `bootstrap` | Only the OAuth secrets you changed | Everything else | + + +Mechanics: + +- `rotate-secrets` deliberately preserves `LeafPfxPassword` (see `SecretsPhase.RunAsync`) +because the wrapped PFX bytes don't change — re-wrapping with a fresh password would +invalidate Kestrel's load with no security benefit. +- Backfill on idempotent reruns: if a `secrets.json` file from before the JWT-keys-in-store +migration is re-read without `--rotate-secrets`, `SecretsPhase` backfills any missing +fields (`DeepLinkSecret`, `JwtRsa256PrivateKeyPem`, `JwtEs256PrivateKeyPem`, +`PostgresAdminPassword`, `ScyllaAdminPassword`) so the next bootstrap pass writes them +into `internal.secrets`. +- OAuth client *secrets* can be rotated without touching the bootstrapper by issuing +`UPDATE internal.secrets SET value = '...' WHERE key = 'oauth::client_secret';` +followed by an API restart. The bootstrapper will catch up on the next run. + +## Local dev vs self-host vs tests + + +| Concern | Local dev (`dotnet run --project hosts/Interfold.AppHost`) | Self-host (`interfold-bootstrap`) | Integration tests | +| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | +| Encryption pepper | `Parameters:dev-encryption-pepper` (`GenerateParameterDefault`, persisted to AppHost user-secrets) → `internal.secrets:encryption:pepper` via dev-seed hook | `GeneratedSecrets.EncryptionPepper` → `internal.secrets:encryption:pepper` | Postgres fixtures seed `"TEST"` into the row via `PostgresSeedOptions`; the in-memory store is pre-seeded with `"TEST"` for inmemory mode | +| JWT keys | Generated per-run in `DevSeedHostedService` (RSA-2048 + P-256), written into `internal.secrets` on the first seed — reruns hit the seeder's idempotency short-circuit and reuse the row | Generated lazily by `SecretsPhase` and round-tripped through Postgres | Seeded into the real store for Scylla/Postgres fixtures; in-memory store pre-seeded with `TestDbCredentials` PEMs for inmemory mode | +| Deep-link secret | `Parameters:dev-deep-link-secret` (`GenerateParameterDefault`, persisted to AppHost user-secrets) → `internal.secrets:auth:deep_link_secret` via dev-seed hook | `GeneratedSecrets.DeepLinkSecret` → `internal.secrets:auth:deep_link_secret` | `TestDbCredentials.DeepLinkSecret` via `PostgresSeedOptions` | +| Admin passwords | `Parameters:dev-postgres-admin-password` / `Parameters:dev-scylla-admin-password` (both `GenerateParameterDefault`, persisted to AppHost user-secrets) | `GeneratedSecrets.PostgresAdminPassword` / `ScyllaAdminPassword` | `TestDbCredentials.Postgres/Scylla AdminPassword` | +| Leaf PFX password | *no leaf PFX in dev* (ASP.NET dev cert) | `internal.secrets:certs:leaf_pfx_password`, loaded by `SecretsPreBuildLoader` | not exercised | +| OAuth secrets | Empty in the dev-seed row (`OAuthChallenge` extensions disable the provider for that row) | `internal.secrets:oauth:*:client_secret` | empty / `"TEST"` | + +The `Parameters:dev-*` names above are AppHost-private (declared in +[`DevSeedParameterNames`](../hosts/Interfold.AppHost/DevSeed/DevSeedParameterNames.cs)) — +they never round-trip through the bootstrapper's `PublishPhase.BuildEnvReplacements` so +they can't leak into the emitted `.env`. To rotate any of them, run +`dotnet user-secrets clear --project hosts/Interfold.AppHost` and wipe the persistent +Postgres volume; a fresh AppHost run will re-generate and re-seed. Wiping user-secrets +without wiping the DB leaves stale-encrypted data behind — the same trade-off +`rotate-secrets` documents for prod (see `docs/ROADMAP.md`). + + +Tests centralise the test-only material in +`[TestDbCredentials](../tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestDbCredentials.cs)` +— a single source of lazy-generated in-process keypairs and deterministic passwords. The +real DB fixtures seed those values into `internal.secrets` via `PostgresSeedOptions`; the +in-memory `WebApplicationFactory` instead drives the production env-var seed path by +pushing the same PEMs + pepper into the factory's configuration provider (see the +constructor of +`[InterfoldWebApplicationFactory](../tests/integration/Interfold.IntegrationTests.Shared/TestServices/InterfoldWebApplicationFactory.cs)`). +External runners (e.g. the Kotlin Testcontainers harness) set them as +`OCTOCON_INMEMORY_SECRETS_SEED__*` env vars on the container; the .NET +`EnvironmentVariablesConfigurationProvider` rewrites the `__` separator to the config-key +delimiter `:` on load, so the in-memory `ISecretsStore` registration in +`[InMemoryServiceCollectionExtensions](../infrastructure/Interfold.Infrastructure.InMemory/InMemoryServiceCollectionExtensions.cs)` +looks them up via `IConfiguration` under the `:`-form key +(`OCTOCON_INMEMORY_SECRETS_SEED:ENCRYPTION_PEPPER`, …) and seeds the store. The +in-process test fixture writes the same `:`-form keys into its +`FactoryConfigurationProvider` so both code paths land on the identical lookup, and a +dedicated regression test +(`Api_InMemorySecretsSeed_PatchesAuthFromRealEnvVars`) additionally mutates the +operator-facing `__`-form env vars via `Environment.SetEnvironmentVariable` to lock the +real env-var ingestion path end-to-end. Either way, signing in `CreateToken` and +verification on the server side use the same PEMs. + +## Common gotchas + +- `**OCTOCON_SCYLLA_KEYSPACE` is required for correct routing.** It used to fall back to +`internal.secrets:scylla:keyspace`; that row no longer exists. The bootstrapper now +manages this env var end-to-end (sourced from `BootstrapConfig.scyllaKeyspace`, +defaulted to `nam`, validated against the seven regional values), so a bootstrap-emitted +stack always ships with an explicit value. The gotcha survives for operators running the +API container *outside* the bootstrapper-produced compose stack: if the env is missing in +that path, the API defaults to `"nam"` in code — correct for a single-region deployment +but the *wrong* answer for a `eur` or `gdpr` node. Failure mode is "wrong region", not +"crash". +- **Don't put `ASPNETCORE_Kestrel__Certificates__Default__Password` back in `.env`.** It +is intentionally not generated. If you set it, `SecretsPreBuildLoader` +honours it as an override (legacy escape hatch), but you've now bypassed +`internal.secrets` and rotation via `rotate-secrets` will not propagate. +- `**internal.secrets:encryption:pepper` must exist before the API starts.** It's the +one row `SecretsBootstrapService` enforces — if the value is missing or empty the API +refuses to boot rather than failing on the first encryption request with an opaque +`NullReferenceException`. `DatabaseInitPhase` seeds it from `GeneratedSecrets.EncryptionPepper`; +tests pass `"TEST"` through `PostgresSeedOptions` or `InMemorySecretsStore.Seed`. The +pepper no longer has an env-var fallback — the previous `OCTOCON_ENCRYPTION_PEPPER` is dead. +- **OAuth client IDs in env, secrets in store.** Client IDs in env are public and that's +fine. Putting client *secrets* in env (other than the bootstrapper-written placeholders) +is a foot-gun: the env value is overwritten at startup, so an operator who manually +edits `.env` will be confused when their change has no effect. +- **The `/keys` bind mount is gone.** Earlier versions mounted `secrets/keys/*.pem` into +`/keys` inside the container. Those PEMs are no longer written to disk; the API reads +the same material from `internal.secrets`. Remove any old `/keys` mounts from custom +compose overrides. + diff --git a/fly.grafana.toml b/fly.grafana.toml deleted file mode 100644 index 44e535b0..00000000 --- a/fly.grafana.toml +++ /dev/null @@ -1,36 +0,0 @@ -# fly.toml app configuration file generated for octo-grafana on 2024-07-25T10:26:38-04:00 -# -# See https://fly.io/docs/reference/configuration/ for information about how to use this file. -# - -app = 'octo-grafana' -primary_region = 'iad' - -[build] - image = 'grafana/grafana:11.1.1' - -[env] - GF_INSTALL_PLUGINS = 'grafana-worldmap-panel,grafana-clock-panel' - -[[mounts]] - source = 'grafana_storage' - destination = '/var/lib/grafana' - -[http_service] - internal_port = 3000 - force_https = true - auto_stop_machines = false - auto_start_machines = false - min_machines_running = 1 - -[[http_service.checks]] - grace_period = '10s' - interval = '30s' - method = 'GET' - timeout = '5s' - path = '/login' - -[[vm]] - memory = '512mb' - cpu_kind = 'shared' - cpus = 1 diff --git a/fly.toml b/fly.toml deleted file mode 100644 index 2ab194e7..00000000 --- a/fly.toml +++ /dev/null @@ -1,72 +0,0 @@ -# fly.toml app configuration file generated for octo on 2024-03-01T05:53:04Z -# -# See https://fly.io/docs/reference/configuration/ for information about how to use this file. -# - -app = 'octo' -primary_region = 'iad' -kill_signal = 'SIGTERM' -swap_size_mb = 512 - -[build] - -[deploy] -#release_command = '/app/bin/migrate' - -[env] -RELEASE_COOKIE = 'octo-cookie' -PRIMARY_REGION = 'iad' -PRIMARY_NODE_COUNT = '1' -PHX_HOST = 'octo.fly.dev' -PORT = '8080' - -[processes] -# primary = "/app/bin/server" -auxiliary = "/app/bin/server" -sidecar = "/app/bin/server" - -[http_service] -internal_port = 8080 -force_https = true -auto_start_machines = true -min_machines_running = 0 -# processes = ['primary', 'auxiliary'] -processes = ['auxiliary'] - -[http_service.concurrency] -type = 'connections' -hard_limit = 1000 -soft_limit = 1000 - -#[mounts] -#source = 'octo_mnesia' -#destination = '/mnesia' -#processes = ['primary'] - -[metrics] -port = 9001 -path = '/' -# processes = ['primary', 'auxiliary', 'sidecar'] -processes = ['auxiliary', 'sidecar'] - -[[vm]] -memory = '1gb' -cpu_kind = 'shared' -cpus = 1 -# processes = ['primary', 'auxiliary', 'sidecar'] -processes = ['auxiliary'] - -[[vm]] -memory = '1gb' -cpu_kind = 'shared' -cpus = 2 -processes = ['sidecar'] - -[[services]] -internal_port = 41641 -protocol = 'udp' -# processes = ['primary', 'auxiliary', 'sidecar'] -processes = ['auxiliary', 'sidecar'] - -[[services.ports]] -port = 41641 diff --git a/global.json b/global.json new file mode 100644 index 00000000..da193f37 --- /dev/null +++ b/global.json @@ -0,0 +1,12 @@ +{ + "sdk": { + "version": "10.*", + "rollForward": "latestFeature" + }, + "msbuild-sdks": { + "Aspire.AppHost.Sdk": "13.4.6" + }, + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/hosts/Interfold.Api.Host/Interfold.Api.Host.csproj b/hosts/Interfold.Api.Host/Interfold.Api.Host.csproj new file mode 100644 index 00000000..10b1768f --- /dev/null +++ b/hosts/Interfold.Api.Host/Interfold.Api.Host.csproj @@ -0,0 +1,40 @@ + + + net10.0 + enable + enable + d42494f5-0e2a-4d45-9ba3-b1750919ff1b + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hosts/Interfold.Api.Host/Program.cs b/hosts/Interfold.Api.Host/Program.cs new file mode 100644 index 00000000..2da7670e --- /dev/null +++ b/hosts/Interfold.Api.Host/Program.cs @@ -0,0 +1,403 @@ +using Interfold.Alters.Api.DependencyInjection; +using Interfold.Api.Host.Services.Secrets; +using Interfold.Auth.Api.DependencyInjection; +using Interfold.Auth.Api.Middleware; +using Interfold.Friendships.Api.DependencyInjection; +using Interfold.Fronting.Api.DependencyInjection; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.InMemory; +using Interfold.Infrastructure.Postgres; +using Interfold.Infrastructure.Scylla; +using Interfold.Journals.Api.DependencyInjection; +using Interfold.Ops.Api.DependencyInjection; +using Interfold.Ops.Api.Helpers; +using Interfold.Polls.Api.DependencyInjection; +using Interfold.ServiceDefaults; +using Interfold.Settings.Api.DependencyInjection; +using Interfold.Shared.Api.DependencyInjection; +using Interfold.Shared.Api.Helpers; +using Interfold.Shared.Api.Middleware; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Api.Swagger; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Socket.Api.DependencyInjection; +using Interfold.Systems.Api.DependencyInjection; +using Interfold.Tags.Api.DependencyInjection; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; + +var builder = WebApplication.CreateBuilder(args); + +// --- Aspire ServiceDefaults (OTel, resilience, service discovery) --- +builder.AddServiceDefaults(); + +// Fetches every internal.secrets row the API's PostConfigure patchers need before host build. +var secretsSnapshot = SecretsPreBuildLoader.Load(builder.Configuration); + +ScyllaServiceCollectionExtensions.Register(); +InMemoryServiceCollectionExtensions.Register(); +PostgresServiceCollectionExtensions.Register(); + +// Register typed options BEFORE the startup snapshots below so tests can override them +// via the FactoryConfigurationProvider without a bespoke Bind*() helper. +builder.Services.AddInterfoldOptions(); + +// Auth feature module owns AuthenticationConfiguration binding + PostConfigure + JWT bearer +// + OAuth challenge schemes + the four auth handler singletons + the ES256 startup log. +// Must run BEFORE AddInterfoldPersistence — persistence ValidateOnStart reads +// AuthenticationConfiguration.EncryptionPepper. +builder.Services.AddAuthModule(builder.Configuration); + +// Alters feature module owns the three alter command-handler singletons. +// Order-independent — no options binding, no config reads. +builder.Services.AddAltersModule(); + +// Friendships feature module owns the six friendship command-handler singletons. +// Order-independent — no options binding, no config reads. +builder.Services.AddFriendshipsModule(); + +// Fronting feature module owns the seven fronting command-handler singletons. +// Order-independent — no options binding, no config reads. +builder.Services.AddFrontingModule(); + +// Journals feature module owns the twelve journal command-handler singletons +// (seven global + five alter). Order-independent — no options binding, no config reads. +builder.Services.AddJournalsModule(); + +// Tags feature module owns the seven tag command-handler singletons. +// Order-independent — no options binding, no config reads. +builder.Services.AddTagsModule(); + +// Polls feature module owns the three poll command-handler singletons. +// Order-independent — no options binding, no config reads. +builder.Services.AddPollsModule(); + +// Settings feature module owns twenty-two settings/encryption/avatar/import/field +// command-handler singletons, the two IImportJobRunner implementations + the +// ImportJobBackgroundService drain loop, the SimplyPlural HttpClient + import service, +// and the two Firebase IPostConfigureOptions bindings. Must run BEFORE +// AddInterfoldPersistence when the runtime depends on the FirebaseClient/Fcm secrets +// snapshot — the PostConfigure patchers read from ISecretsSnapshot registered further +// down, and the ordering matches AddAuthModule's rationale above. +builder.Services.AddSettingsModule(); + +// Socket feature module owns the Phoenix-protocol WebSocket transport, per-feature socket +// event handlers, SocketJoinRateLimiter, the LoopbackHttpClient named HttpClient, and the +// /api/socket/websocket route wiring (MapSocketModule below). +builder.Services.AddSocketModule(); + +// Systems feature module is a pure read facade (no owned handlers, no .Domain project); +// AddSystemsModule is a no-op today and exists for wiring symmetry with the other modules. +builder.Services.AddSystemsModule(); + +// Ops feature module hosts NodeRoleController + TrustController; no owned handlers, +// no .Domain project, no .Contracts project (nothing cross-module to share). +// AddOpsModule is a no-op today and exists for wiring symmetry with the other modules. +builder.Services.AddOpsModule(); + +// AuthenticationConfiguration is deliberately absent from this probe: its patchers pull +// from the pre-Build secrets snapshot; probing would trip [Required] before the patches +// land. ASP0000 is intentional — the alternative is hand-maintained Bind*() helpers that +// reintroduce the drift the options pipeline exists to eliminate. +// The probe SP swaps in a non-owning IConfiguration proxy so disposing it doesn't dispose +// the shared ConfigurationManager (which WebApplicationFactory reuses in tests). +PersistenceConfiguration persistenceConfig; +CorsOptions corsOptions; +ClusterConfiguration clusterConfig; +var probeServices = StartupProbeConfigurationProxy.SwapInto(builder.Services, builder.Configuration); +#pragma warning disable ASP0000 +using (var probeProvider = probeServices.BuildServiceProvider(validateScopes: false)) +#pragma warning restore ASP0000 +{ + persistenceConfig = probeProvider.GetRequiredService>().Value; + corsOptions = probeProvider.GetRequiredService>().Value; + clusterConfig = probeProvider.GetRequiredService>().Value; +} + +// Allow-list from OCTOCON_CORS_ALLOWED_ORIGINS; blank = allow-any (dev-only). +var configuredCorsOrigins = corsOptions.AllowedOrigins.ToArray(); + +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy(policy => + { + if (configuredCorsOrigins.Length > 0) + { + policy.WithOrigins(configuredCorsOrigins) + .AllowAnyHeader() + .AllowAnyMethod(); + return; + } + + policy.SetIsOriginAllowed(_ => true) + .AllowAnyHeader() + .AllowAnyMethod(); + }); +}); + +// Register BEFORE persistence so the pre-Build secrets snapshot is visible when migration +// services and ValidationHostedService (via [Required] + ValidateOnStart) read secrets. +// The two IPostConfigureOptions bindings that patched FirebaseClientConfiguration + +// FcmConfiguration from this snapshot moved into AddSettingsModule with the Firebase + +// FCM types they patch. +builder.Services.AddSingleton(secretsSnapshot); +builder.Services.AddSingleton(secretsSnapshot); + +builder.Services.AddInterfoldCluster(clusterConfig.NodeGroup); +builder.Services.AddInterfoldPersistence(persistenceConfig.Mode, persistenceConfig); +builder.Services.AddInterfoldDomainHandlers(); + +// Readiness fails fast; startup allows longer for cold-start migrations; naming convention +// lives in HealthCheckExtensions.AddReadyAndStartup. +var healthChecks = builder.Services.AddHealthChecks(); + +if (persistenceConfig.Mode == PersistenceMode.ScyllaPostgres) +{ + healthChecks + .AddReadyAndStartup("scylla") + .AddReadyAndStartup("postgres"); +} +builder.Services.AddSingleton(); +builder.Services.AddSingleton(TimeProvider.System); + +// HttpLoggingHandler + AddSimplyPluralImport + the two IImportJobRunner singletons + +// ImportJobBackgroundService all moved into AddSettingsModule (Phase-3 Settings slice). +// SocketJoinRateLimiter + LoopbackHttpClient named client both moved into AddSocketModule +// (Phase-3 Socket slice #10) — see apis/Interfold.Socket.Api/DependencyInjection. + +// Cross-cutting authorization policy applies to every module's controllers, so it stays on +// the composition host rather than moving into AddAuthModule. +builder.Services.AddAuthorizationBuilder() + .SetFallbackPolicy(new AuthorizationPolicyBuilder() + .RequireAuthenticatedUser() + .Build()); + +builder.Services.AddHsts(options => +{ + options.ExcludedHosts.Add("api.octocon.app"); +}); + +builder.Services.AddHttpsRedirection(options => +{ + options.RedirectStatusCode = 308; // Permanent Redirect +}); + +// --- OpenTelemetry --- +// The InterfoldMetrics custom meter is registered on top of ServiceDefaults OTel config. +builder.Services + .AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddMeter(InterfoldMetrics.MeterName); + }); + +// AddSharedApi configures JsonOptions (snake_case + UTC), inserts UnixSecondsModelBinderProvider, +// wires the ErrorResponse InvalidModelStateResponseFactory, and registers the ExceptionHandler. +builder.Services.AddSharedApi(); +builder.Services.AddControllers(); + +// --- Swagger/OpenAPI --- +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(options => +{ + options.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Interfold API", + Version = "v1", + Description = $"Interfold API - Contract Version: {InterfoldContractVersions.Current}" + }); + + options.ResolveConflictingActions(apiDescriptions => + { + var first = apiDescriptions.First(); + var route = first.RelativePath?.ToLowerInvariant(); + + // Sentinelise route params so renaming (e.g. {id}→{alterId}) doesn't silently drop + // routes off the allow-list and crash Swagger doc generation. + var normalisedRoute = route is null + ? null + : System.Text.RegularExpressions.Regex.Replace(route, @"\{[^/{}]+\}", "{*}"); + + // Only allow conflicts for avatar upload endpoints (multipart vs JSON siblings). + var allowedConflicts = new[] + { + "api/settings/avatar", + "api/systems/me/alters/{*}/avatar" + }; + + if (allowedConflicts.All(allowed => normalisedRoute?.Contains(allowed) != true)) + { + var actionNames = string.Join(", ", apiDescriptions.Select(d => $"{d.ActionDescriptor.DisplayName}")); + throw new NotSupportedException( + $"Conflicting actions detected for route '{route}': {actionNames}. " + + "If this is intentional, add the route to the allowedConflicts list in Program.cs."); + } + + return first; + }); + + options.DocumentFilter(); + options.DocumentFilter(); + + // Add JWT Bearer authentication to Swagger UI + options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Description = "JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below.", + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer" + }); + + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + Array.Empty() + } + }); +}); + +var app = builder.Build(); + +app.UseExceptionHandler("/error"); + +// Buffer avatar multipart PUTs so source-validation can re-read Request.Body post-bind; +// scoped to the two avatar routes only. +app.Use(async (context, next) => +{ + if (HttpMethods.IsPut(context.Request.Method) + && context.Request.HasFormContentType) + { + var path = context.Request.Path.Value ?? string.Empty; + if (path.Equals("/api/settings/avatar", StringComparison.OrdinalIgnoreCase) + || (path.StartsWith("/api/systems/me/alters/", StringComparison.OrdinalIgnoreCase) + && path.EndsWith("/avatar", StringComparison.OrdinalIgnoreCase))) + { + context.Request.EnableBuffering(); + } + } + + await next(); +}); + +app.UseMiddleware(); + +// Phase N: correlation ID propagation and structured request logging. +app.UseMiddleware(); + +// --- Swagger UI --- +app.UseSwagger(); +app.UseSwaggerUI(options => +{ + options.SwaggerEndpoint("/swagger/v1/swagger.json", "Interfold API v1"); + options.RoutePrefix = "swagger"; +}); + +app.Use(async (ctx, next) => +{ + ctx.Response.OnStarting(() => + { + ctx.Response.Headers[InterfoldHeaders.Contract] = InterfoldContractVersions.Current; + return Task.CompletedTask; + }); + await next(); +}); + +if (!app.Environment.IsDevelopment()) +{ + app.UseHsts(); + // /.well-known stays plain HTTP so clients can fetch the root CA before trusting HTTPS. + app.UseWhen( + static ctx => !ctx.Request.Path.StartsWithSegments("/.well-known"), + static branch => branch.UseHttpsRedirection()); +} +app.UseCors(); +app.UseAuthentication(); +app.UseMiddleware(); +app.UseStaticFiles(); + +// Serve avatars per AvatarServingPolicy. Hand-rolled (not a second UseStaticFiles) so the +// serve-side and LocalAvatarStorage's URL-stamp side read the same IOptionsMonitor snapshot. +var avatarContentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(); +app.Use(async (context, next) => +{ + var storageMonitor = context.RequestServices.GetRequiredService>(); + var current = storageMonitor.CurrentValue; + var (shouldServe, physicalRoot, requestPath) = AvatarServingPolicy.Resolve( + current.AvatarStorageRoot, current.AvatarPublicBase); + + if (!shouldServe + || !context.Request.Path.StartsWithSegments(requestPath, StringComparison.Ordinal, out var remainingPath) + || !(HttpMethods.IsGet(context.Request.Method) || HttpMethods.IsHead(context.Request.Method))) + { + await next(); + return; + } + + var relative = remainingPath.Value?.TrimStart('/') ?? string.Empty; + if (relative.Length == 0 || relative.Contains("..", StringComparison.Ordinal)) + { + await next(); + return; + } + + var rootFull = Path.GetFullPath(physicalRoot); + var rootWithSep = rootFull.EndsWith(Path.DirectorySeparatorChar) + ? rootFull + : rootFull + Path.DirectorySeparatorChar; + var fullPath = Path.GetFullPath(Path.Combine(rootFull, relative)); + if (!fullPath.StartsWith(rootWithSep, StringComparison.OrdinalIgnoreCase)) + { + await next(); + return; + } + + if (!File.Exists(fullPath)) + { + await next(); + return; + } + + if (!avatarContentTypeProvider.TryGetContentType(fullPath, out var contentType)) + { + await next(); + return; + } + + var fi = new FileInfo(fullPath); + context.Response.ContentType = contentType; + context.Response.ContentLength = fi.Length; + // Safe long-term cache: LocalAvatarStorage embeds a fresh timestamp+Guid on every write. + context.Response.Headers.CacheControl = "public, max-age=86400"; + if (HttpMethods.IsHead(context.Request.Method)) + { + return; + } + await context.Response.SendFileAsync(fullPath, context.RequestAborted); +}); + +app.UseWebSockets(new WebSocketOptions +{ + KeepAliveInterval = TimeSpan.FromSeconds(30) +}); + +app.UseAuthorization(); + +app.MapDefaultEndpoints(); + +app.MapSocketModule(); +app.MapControllers(); + +app.Run(); +return 0; \ No newline at end of file diff --git a/hosts/Interfold.Api.Host/Properties/launchSettings.template.json b/hosts/Interfold.Api.Host/Properties/launchSettings.template.json new file mode 100644 index 00000000..21c9b855 --- /dev/null +++ b/hosts/Interfold.Api.Host/Properties/launchSettings.template.json @@ -0,0 +1,96 @@ +{ + // Copy this file to launchSettings.json and customise. See docs/configuration.md for the + // full reference. Anything marked should be moved out of source + // control via `dotnet user-secrets set "" "" --project hosts/Interfold.Api.Host`. + "profiles": { + "Interfold.Api": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + // --- ASP.NET Core host --- + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_URLS": "http://api.octocon.app;https://api.octocon.app", + + // --- Persistence --- + // Mode: 'inmemory' for solo-API dev; 'scylla-postgres' against a real cluster + // (typically via `aspire run` instead of launching the API directly). + "OCTOCON_PERSISTENCE": "inmemory", + // Only read in scylla-postgres mode. Defaults to localhost if unset. + "OCTOCON_POSTGRES_CONNECTION": "", + // Per-node region identity. Drives the Scylla session keyspace AND new-account routing. + // Values: nam | eur | ocn | eas | sam | sas | gdpr. No store fallback. + "OCTOCON_SCYLLA_KEYSPACE": "nam", + // Dev/test override for Scylla contact points + port. In production these are read + // from internal.secrets (scylla:contact_points / scylla:port). + "OCTOCON_SCYLLA_CONTACT_POINTS": "localhost", + "OCTOCON_SCYLLA_PORT": "9042", + // 'true' keeps ScyllaMigrationService creating only the keyspace named above — + // matches the dev/single-node profile. + "OCTOCON_SINGLE_SCYLLA_INSTANCE": "true", + // Transient-failure retry strategy for DB operations. Defaults shown. + "OCTOCON_DB_RETRY_ATTEMPTS": "3", + "OCTOCON_DB_RETRY_INITIAL_DELAY_MS": "100", + "OCTOCON_DB_RETRY_MAX_DELAY_MS": "1500", + // Caps the fan-out for friendship profile/fronting hydration. Default 8. + "OCTOCON_HYDRATION_MAX_CONCURRENCY": "8", + + // --- Authentication --- + // Issuer claim emitted on outbound tokens. (The encryption pepper, JWT signing keys, + // and deep-link HMAC secret are all sourced from internal.secrets at startup — see + // SecretsBootstrapService.) + "OCTOCON_JWT_AUTHORITY": "octocon-local", + // Base URL OAuth callbacks return to (must match the redirect URI registered with + // each provider). + "OCTOCON_AUTH_CALLBACK_BASE_URL": "https://api.octocon.app", + + // --- OAuth providers --- + // The provider authorize URL, the ASP.NET Core challenge scheme name, and the static + // challenge query parameters (scopes / response_type / response_mode) are all baked + // into the API as constants — see OAuthChallengeServiceCollectionExtensions. The + // client_id is injected from these env vars at scheme-registration time, so leaving + // a *_OAUTH_CLIENT_ID empty disables the provider entirely (the scheme won't + // register and the controller falls through to 403). Client secrets are sensitive: + // in self-host they're stored in internal.secrets via SecretsBootstrapService; for + // solo-API dev set them via user-secrets so they aren't committed. + "OCTOCON_GOOGLE_OAUTH_CLIENT_ID": "", + "OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET": "", + + "OCTOCON_DISCORD_OAUTH_CLIENT_ID": "", + "OCTOCON_DISCORD_OAUTH_CLIENT_SECRET": "", + + // Apple's client_secret is a short-lived JWT minted from the team private key — + // pre-mint outside the bootstrapper and paste it here (or via user-secrets) for + // solo dev. In self-host it lives in internal.secrets:oauth:apple:client_secret. + "OCTOCON_APPLE_OAUTH_CLIENT_ID": "", + "OCTOCON_APPLE_OAUTH_CLIENT_SECRET": "", + + // --- CORS --- + // Comma-separated allow-list. Empty / unset falls back to "allow any origin", which + // is fine for solo-API dev but should always be set explicitly in production. + // (Redirect destinations after OAuth login are now supplied by the client via the + // redirect_uri query param on GET /auth/{provider}; there is no server-side + // fallback for the post-login redirect base any more.) + "OCTOCON_CORS_ALLOWED_ORIGINS": "http://localhost:8080", + + // --- Storage --- + // Local filesystem root + public CDN base for uploaded avatars. Empty disables uploads. + "OCTOCON_AVATAR_STORAGE_ROOT": "", + "OCTOCON_AVATAR_PUBLIC_BASE": "", + + // --- Observability --- + // OTLP gRPC endpoint (e.g. http://localhost:4317). Empty keeps metrics/traces in-process. + "OCTOCON_OTLP_ENDPOINT": "", + + // --- Cluster --- + // Resolution order: FLY_PROCESS_GROUP -> OCTOCON_NODE_GROUP -> "auxiliary". + // Values: primary | auxiliary | sidecar. Solo dev typically runs as 'primary'. + "OCTOCON_NODE_GROUP": "primary", + + // --- Sockets --- + // Outbound WebSocket batch flush threshold in bytes. Empty uses the built-in default. + "OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD": "" + }, + "launchUrl": "https://api.octocon.app/swagger/index.html" + } + } +} diff --git a/hosts/Interfold.Api.Host/Services/Secrets/SecretsPreBuildLoader.cs b/hosts/Interfold.Api.Host/Services/Secrets/SecretsPreBuildLoader.cs new file mode 100644 index 00000000..49dae527 --- /dev/null +++ b/hosts/Interfold.Api.Host/Services/Secrets/SecretsPreBuildLoader.cs @@ -0,0 +1,190 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Secrets; +using Npgsql; +using System.Security.Cryptography; + +namespace Interfold.Api.Host.Services.Secrets; + +/// Populates the API's before +/// WebApplicationBuilder.Build(). Postgres branch: batched read on a bare Npgsql +/// connection (ISecretsStore isn't built yet). InMemory branch: reads +/// OCTOCON_INMEMORY_SECRETS_SEED:* env vars. Rewrites the pg connection string +/// via so parallel factory builds reuse a +/// dedicated pool instead of the fixture's pinned 5-slot app pool. +internal static class SecretsPreBuildLoader +{ + // Every row read by AuthenticationSecretsPostConfigure / + // FirebaseClientSecretsPostConfigure / FcmSecretsPostConfigure — keep in sync. + private static readonly SecretsStoreKey[] SnapshotKeys = + [ + SecretsStoreKeys.OAuthGoogleClientSecret, + SecretsStoreKeys.OAuthDiscordClientSecret, + SecretsStoreKeys.OAuthAppleClientSecret, + SecretsStoreKeys.EncryptionPepper, + SecretsStoreKeys.AuthDeepLinkSecret, + SecretsStoreKeys.AuthJwtRsa256PrivatePem, + SecretsStoreKeys.AuthJwtEs256PrivatePem, + + SecretsStoreKeys.FirebaseClientAndroid, + SecretsStoreKeys.FirebaseClientIos, + SecretsStoreKeys.FirebaseClientWeb, + + SecretsStoreKeys.FcmServiceAccountJson, + ]; + + public static SecretsSnapshot Load(IConfigurationBuilder cfg) + { + var config = cfg.Build(); + var snapshot = new SecretsSnapshot(); + + if (EnumWireExtensions.ParsePersistenceMode(config[OctoconEnvKeys.Persistence]) == PersistenceMode.ScyllaPostgres) + { + var pgConn = config[OctoconEnvKeys.PostgresConnection]; + if (string.IsNullOrWhiteSpace(pgConn)) + { + throw new InvalidOperationException("Postgres connection string is not configured."); + } + + var rows = FetchFromPostgres(pgConn); + snapshot.Populate(BuildSnapshotBuffer(rows)); + var leafPfxPassword = rows.GetValueOrDefault(SecretsStoreKeys.CertsLeafPfxPassword.Value); + ApplyLeafPfxPasswordIfNeeded(cfg, config, pgConnPresent: true, leafPfxPassword); + } + else + { + snapshot.Populate(BuildInMemorySeedBuffer(config)); + ApplyLeafPfxPasswordIfNeeded(cfg, config, pgConnPresent: false, leafPfxPassword: null); + } + + return snapshot; + } + + private static Dictionary BuildSnapshotBuffer(Dictionary rows) + { + var buffer = new Dictionary(SnapshotKeys.Length); + foreach (var key in SnapshotKeys) + { + buffer[key] = rows.GetValueOrDefault(key.Value); + } + return buffer; + } + + private static Dictionary BuildInMemorySeedBuffer(IConfigurationRoot config) => new() + { + [SecretsStoreKeys.EncryptionPepper] = config[OctoconEnvKeys.InMemorySecretsSeedEncryptionPepper], + [SecretsStoreKeys.AuthJwtEs256PrivatePem] = config[OctoconEnvKeys.InMemorySecretsSeedAuthJwtEs256PrivatePem], + [SecretsStoreKeys.AuthDeepLinkSecret] = config[OctoconEnvKeys.InMemorySecretsSeedAuthDeepLinkSecret], + [SecretsStoreKeys.AuthJwtRsa256PrivatePem] = config[OctoconEnvKeys.InMemorySecretsSeedAuthJwtRsa256PrivatePem], + }; + + /// Batched read of every snapshot row plus the leaf PFX password. Failures + /// throw fail-fast. + private static Dictionary FetchFromPostgres(string pgConn) + { + var rows = new Dictionary(StringComparer.Ordinal); + var loaderConn = WithDedicatedPoolIdentity(pgConn); + LoaderGate.Wait(); + try + { + using var conn = new NpgsqlConnection(loaderConn); + conn.Open(); + using var cmd = new NpgsqlCommand( + "SELECT key, value FROM internal.secrets WHERE key = ANY(@keys)", conn); + + var keys = new string[SnapshotKeys.Length + 1]; + for (var i = 0; i < SnapshotKeys.Length; i++) + { + keys[i] = SnapshotKeys[i].Value; + } + keys[^1] = SecretsStoreKeys.CertsLeafPfxPassword.Value; + cmd.Parameters.AddWithValue("keys", keys); + + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + rows[reader.GetString(0)] = reader.IsDBNull(1) ? null : reader.GetString(1); + } + } + catch (Exception ex) + { + throw new InvalidOperationException( + "Failed to fetch startup secrets from internal.secrets. Ensure Postgres is " + + "reachable at startup and that DatabaseInitPhase has seeded the required rows.", ex); + } + finally + { + // Release only after Dispose has returned the physical connection to the pool, + // otherwise the next waiter can Open() while this checkout is still live and + // trip MaxPoolSize. + LoaderGate.Release(); + } + + return rows; + } + + // Distinct application_name → own Npgsql pool identity and easier pg_stat_activity triage. + internal const string LoaderApplicationName = "octocon-secrets-preload"; + + // Caps in-flight loader checkouts. Matched by LoaderGate so TUnit parallel factory + // builds queue instead of exhausting the pool (Npgsql's default 15s wait then throws). + internal const int LoaderMaxPoolSize = 10; + + // Open/checkout wait under a contended shared Postgres (solution-wide `dotnet test`). + internal const int LoaderTimeoutSeconds = 60; + + private static readonly SemaphoreSlim LoaderGate = new(LoaderMaxPoolSize, LoaderMaxPoolSize); + + /// Rewrites onto a dedicated pooled identity + /// (distinct Application Name, bounded Maximum Pool Size). Overwrite is + /// intentional — a fixture Maximum Pool Size=5 would otherwise be inherited + /// and the loader would contend with the app pool. + internal static string WithDedicatedPoolIdentity(string pgConn) + { + var builder = new NpgsqlConnectionStringBuilder(pgConn) + { + ApplicationName = LoaderApplicationName, + MaxPoolSize = LoaderMaxPoolSize, + Timeout = LoaderTimeoutSeconds, + Pooling = true, + }; + return builder.ConnectionString; + } + + /// Self-host only. No-op unless AppHost injects a Kestrel default-cert path. + /// PFX path set + no Postgres → throw; path set + row missing/empty → throw. + private static void ApplyLeafPfxPasswordIfNeeded( + IConfigurationBuilder cfg, + IConfigurationRoot config, + bool pgConnPresent, + string? leafPfxPassword) + { + var pfxPath = config["Kestrel:Certificates:Default:Path"] + ?? Environment.GetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path"); + if (string.IsNullOrWhiteSpace(pfxPath)) return; + + // Operator-pinned env password wins over the store lookup for local dev / recovery. + var existingPassword = config["Kestrel:Certificates:Default:Password"]; + if (!string.IsNullOrWhiteSpace(existingPassword)) return; + + if (!pgConnPresent) + { + throw new InvalidOperationException( + "Kestrel default-cert path is set but OCTOCON_POSTGRES_CONNECTION is missing; " + + "cannot fetch certs:leaf_pfx_password from internal.secrets."); + } + + if (string.IsNullOrEmpty(leafPfxPassword)) + { + throw new InvalidOperationException( + "Row internal.secrets[certs:leaf_pfx_password] is missing or empty; " + + "re-run the bootstrapper so SecretsPhase + DatabaseInitPhase seed it."); + } + + cfg.AddInMemoryCollection(new Dictionary + { + ["Kestrel:Certificates:Default:Password"] = leafPfxPassword, + }); + } +} diff --git a/hosts/Interfold.Api.Host/Services/Secrets/SecretsSnapshot.cs b/hosts/Interfold.Api.Host/Services/Secrets/SecretsSnapshot.cs new file mode 100644 index 00000000..6d5d9146 --- /dev/null +++ b/hosts/Interfold.Api.Host/Services/Secrets/SecretsSnapshot.cs @@ -0,0 +1,34 @@ +using System.Collections.Immutable; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.Api.Host.Services.Secrets; + +/// Thread-safe in-memory . Written exactly once by +/// pre-Build; reads are lock-free dictionary lookups. +/// Immutable backing so the populated map publishes via a single reference assignment. +internal sealed class SecretsSnapshot : ISecretsSnapshot +{ + private ImmutableDictionary _values = ImmutableDictionary.Empty; + + public bool IsPopulated { get; private set; } + + public string? Get(SecretsStoreKey key) + => _values.TryGetValue(key.Value, out var value) ? value : null; + + /// Blank/null values are filtered so patchers see "row absent" and "row empty" + /// identically — partially-seeded deployments behave like unseeded ones. + internal void Populate(IReadOnlyDictionary values) + { + var builder = ImmutableDictionary.CreateBuilder(StringComparer.Ordinal); + foreach (var (key, value) in values) + { + if (!string.IsNullOrWhiteSpace(value)) + { + builder[key.Value] = value; + } + } + _values = builder.ToImmutable(); + IsPopulated = true; + } +} diff --git a/hosts/Interfold.Api.Host/appsettings.Development.json b/hosts/Interfold.Api.Host/appsettings.Development.json new file mode 100644 index 00000000..34f00ef1 --- /dev/null +++ b/hosts/Interfold.Api.Host/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "Microsoft.AspNetCore": "Information" + } + } +} diff --git a/hosts/Interfold.Api.Host/appsettings.json b/hosts/Interfold.Api.Host/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/hosts/Interfold.Api.Host/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/lib/octocon_discord/command_utils.ex b/hosts/Interfold.Api.Host/data/avatars/.gitkeep similarity index 100% rename from lib/octocon_discord/command_utils.ex rename to hosts/Interfold.Api.Host/data/avatars/.gitkeep diff --git a/hosts/Interfold.AppHost/AppHost.cs b/hosts/Interfold.AppHost/AppHost.cs new file mode 100644 index 00000000..b6117e17 --- /dev/null +++ b/hosts/Interfold.AppHost/AppHost.cs @@ -0,0 +1,5 @@ +using Interfold.AppHost; + +var builder = DistributedApplication.CreateBuilder(args); +InterfoldAppHost.Configure(builder); +builder.Build().Run(); diff --git a/hosts/Interfold.AppHost/AppHostHelpers.cs b/hosts/Interfold.AppHost/AppHostHelpers.cs new file mode 100644 index 00000000..6c9d6e67 --- /dev/null +++ b/hosts/Interfold.AppHost/AppHostHelpers.cs @@ -0,0 +1,76 @@ +using System.Net.Sockets; +using Aspire.Hosting.Docker.Resources.ServiceNodes; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace Interfold.AppHost; + +/// Raw TCP-connect factory for the AppHost dashboard +/// readiness gates on non-HTTP containers (Postgres, Cassandra-only CQL). Scylla uses +/// instead. +internal static class HostPortTcpProbe +{ + public static Func CreateCheck(int port, string host = "localhost") + => () => + { + try + { + using var tcp = new TcpClient(); + tcp.Connect(host, port); + return HealthCheckResult.Healthy(); + } + catch { return HealthCheckResult.Unhealthy(); } + }; +} + +/// Factory helpers for compose objects. Passes the +/// command string through untouched so callers can rely on the $$VAR escape trick +/// (see the CQL probes in ). +internal static class ComposeHealthcheck +{ + public static Healthcheck CmdShell( + string command, + string interval, + string timeout, + int retries, + string startPeriod) + => new() + { + Test = ["CMD-SHELL", command], + Interval = interval, + Timeout = timeout, + Retries = retries, + StartPeriod = startPeriod, + }; + + public static Healthcheck Cmd( + string interval, + string timeout, + int retries, + string startPeriod, + params string[] command) + => new() + { + Test = ["CMD", .. command], + Interval = interval, + Timeout = timeout, + Retries = retries, + StartPeriod = startPeriod, + }; +} + +/// Pairs WithVolume with . A +/// named volume without persistent lifetime silently re-creates on every +/// docker compose up, blowing away the data it was meant to preserve. +internal static class PersistentResourceExtensions +{ + public static IResourceBuilder AsPersistent( + this IResourceBuilder builder, + string volumeName, + string containerPath) + where T : ContainerResource + { + return builder + .WithVolume(volumeName, containerPath) + .WithLifetime(ContainerLifetime.Persistent); + } +} diff --git a/hosts/Interfold.AppHost/AppHostRepoPaths.cs b/hosts/Interfold.AppHost/AppHostRepoPaths.cs new file mode 100644 index 00000000..d254d178 --- /dev/null +++ b/hosts/Interfold.AppHost/AppHostRepoPaths.cs @@ -0,0 +1,27 @@ +namespace Interfold.AppHost; + +/// Repo-root-relative paths the AppHost graph bind-mounts. Absolute paths are +/// required — relative ../../db/... breaks when the AppHost cwd is the repo root +/// or a test bin directory. +public static class AppHostRepoPaths +{ + public static string ScyllaRackDcProperties(string repoRoot, string regionWire) => + Path.GetFullPath(Path.Combine(repoRoot, "db", "scylla", $"cassandra-rackdc.{regionWire}.properties")); + + /// Walks up from until it finds + /// Interfold.slnx. + public static string ResolveRepoRoot() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null) + { + if (File.Exists(Path.Combine(dir.FullName, "Interfold.slnx"))) + return dir.FullName; + dir = dir.Parent; + } + + throw new InvalidOperationException( + $"Could not locate 'Interfold.slnx' walking up from '{AppContext.BaseDirectory}'. " + + "The AppHost needs the repo root for Scylla rackdc bind mounts."); + } +} diff --git a/hosts/Interfold.AppHost/AspireResourceFailureDiagnostics.cs b/hosts/Interfold.AppHost/AspireResourceFailureDiagnostics.cs new file mode 100644 index 00000000..e71fd771 --- /dev/null +++ b/hosts/Interfold.AppHost/AspireResourceFailureDiagnostics.cs @@ -0,0 +1,167 @@ +using System.Diagnostics; +using System.Text; +using System.Text.RegularExpressions; + +namespace Interfold.AppHost; + +/// Turns opaque Aspire FailedToStart transitions into actionable messages by +/// inspecting Docker state/logs and known rackdc bind-mount failure signatures. +public static class AspireResourceFailureDiagnostics +{ + private const double RecommendedDockerMemoryGiB = 8.0; + + private static readonly Regex MountNotShared = new( + @"not shared from the host|File Sharing", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex MountSourceMissing = new( + @"invalid mount config|bind source path does not exist|no such file or directory|cannot find the file|CreateFile.*rackdc", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex OutOfMemory = new( + @"cannot allocate memory|out of memory|OOM|Killed|memory cgroup", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + /// Best-effort diagnosis after failed to start. + /// Never throws. + public static async Task DescribeAsync( + string resourceName, + string? repoRoot = null, + CancellationToken cancellationToken = default) + { + var sb = new StringBuilder(); + sb.Append($"Aspire resource '{resourceName}' entered FailedToStart before reaching Running."); + + if (string.Equals(resourceName, "scylla", StringComparison.OrdinalIgnoreCase) + && repoRoot is not null) + { + var rackdc = AppHostRepoPaths.ScyllaRackDcProperties(repoRoot, "nam"); + if (!File.Exists(rackdc)) + { + sb.AppendLine(); + sb.Append( + $" Scylla rackdc bind-mount source is missing at '{rackdc}'. " + + "The AppHost graph uses GossipingPropertyFileSnitch and requires " + + "db/scylla/cassandra-rackdc..properties — ensure the repo tree is intact."); + return sb.ToString(); + } + + sb.AppendLine(); + sb.Append($" Expected rackdc mount source: '{rackdc}'."); + } + + var containerName = ResolveContainerName(resourceName); + var (inspectExit, inspectOut, inspectErr) = await RunDockerAsync( + ["inspect", "--format", "{{.State.Status}} {{.State.Error}}", containerName], + cancellationToken).ConfigureAwait(false); + + var (logsExit, logsOut, logsErr) = await RunDockerAsync( + ["logs", "--tail", "80", containerName], + cancellationToken).ConfigureAwait(false); + + var blob = string.Join('\n', new[] { inspectOut, inspectErr, logsOut, logsErr }); + + if (MountNotShared.IsMatch(blob)) + { + sb.AppendLine(); + sb.Append( + " Docker rejected the Scylla rackdc bind mount: the source path is outside Docker Desktop's " + + "file-sharing allowlist (Settings → Resources → File sharing). Add the repo directory or drive, " + + "then restart Docker Desktop."); + return sb.ToString(); + } + + if (MountSourceMissing.IsMatch(blob)) + { + sb.AppendLine(); + sb.Append( + " Docker could not bind-mount the Scylla rackdc properties file. The AppHost must use an " + + "absolute repo-root path (see AppHostRepoPaths.ScyllaRackDcProperties) — relative ../../db/... " + + "paths break when the AppHost cwd is the repo root or a test bin directory. " + + "GossipingPropertyFileSnitch + rackdc mounts are required for multi-DC topology."); + if (!string.IsNullOrWhiteSpace(logsErr)) + { + sb.AppendLine(); + sb.Append(" docker stderr: ").Append(TrimForMessage(logsErr)); + } + return sb.ToString(); + } + + if (OutOfMemory.IsMatch(blob)) + { + sb.AppendLine(); + sb.Append( + " Container exited under memory pressure. Parallel integration runs need Docker Desktop " + + $"memory ≥ {RecommendedDockerMemoryGiB:0.#} GiB when the shared bench and " + + "Infrastructure's legacy Aspire host both start Scylla + Cassandra."); + return sb.ToString(); + } + + if (inspectExit == 0 && !string.IsNullOrWhiteSpace(inspectOut)) + { + sb.AppendLine(); + sb.Append(" docker inspect: ").Append(TrimForMessage(inspectOut)); + } + + if (logsExit == 0 && !string.IsNullOrWhiteSpace(logsOut)) + { + sb.AppendLine(); + sb.Append(" docker logs (tail): ").Append(TrimForMessage(logsOut)); + } + else if (!string.IsNullOrWhiteSpace(logsErr)) + { + sb.AppendLine(); + sb.Append(" docker logs stderr: ").Append(TrimForMessage(logsErr)); + } + + return sb.ToString(); + } + + private static string ResolveContainerName(string resourceName) => + resourceName switch + { + "msg-db" => TestBenchContainerNames.Postgres, + "scylla" => TestBenchContainerNames.Scylla, + "cassandra" => TestBenchContainerNames.Cassandra, + _ when resourceName.StartsWith("scylla-", StringComparison.Ordinal) => + $"interfold-test-bench-{resourceName}", + _ => resourceName, + }; + + private static string TrimForMessage(string text) + { + var trimmed = text.Trim(); + return trimmed.Length <= 1200 ? trimmed : trimmed[^1200..]; + } + + private static async Task<(int Exit, string Stdout, string Stderr)> RunDockerAsync( + IEnumerable args, + CancellationToken cancellationToken) + { + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var arg in args) + psi.ArgumentList.Add(arg); + + try + { + using var proc = Process.Start(psi); + if (proc is null) + return (-1, string.Empty, "failed to spawn docker"); + + var stdoutTask = proc.StandardOutput.ReadToEndAsync(cancellationToken); + var stderrTask = proc.StandardError.ReadToEndAsync(cancellationToken); + await proc.WaitForExitAsync(cancellationToken).ConfigureAwait(false); + return (proc.ExitCode, await stdoutTask.ConfigureAwait(false), await stderrTask.ConfigureAwait(false)); + } + catch (Exception ex) + { + return (-1, string.Empty, ex.Message); + } + } +} diff --git a/hosts/Interfold.AppHost/AvatarsPaths.cs b/hosts/Interfold.AppHost/AvatarsPaths.cs new file mode 100644 index 00000000..057fcf11 --- /dev/null +++ b/hosts/Interfold.AppHost/AvatarsPaths.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.AppHost; + +/// Filesystem wire contract for uploaded avatars. The bootstrapper stages the +/// host-side directory and fills the bind-mount source from +/// BootstrapConfig.storage.avatarStorageRoot; the emitted compose always mounts +/// that host path at . +internal static class AvatarsPaths +{ + /// Host-side avatars directory, relative to the emitted compose file. + public const string HostDir = "../../data/avatars"; + + /// In-container mount point for . + public const string ContainerDir = ContainerMountPaths.InterfoldAvatars; +} diff --git a/hosts/Interfold.AppHost/CertsPaths.cs b/hosts/Interfold.AppHost/CertsPaths.cs new file mode 100644 index 00000000..f297e115 --- /dev/null +++ b/hosts/Interfold.AppHost/CertsPaths.cs @@ -0,0 +1,28 @@ +namespace Interfold.AppHost; + +/// Filesystem wire contract for TLS material. The bootstrapper's CertificatePhase +/// writes the host-side files, the emitted compose bakes the container paths in, and +/// operator runbooks reference them — renaming any of these breaks existing deployments. +internal static class CertsPaths +{ + /// Host-side certs directory, relative to the emitted compose file. + public const string HostDir = "../../certs"; + + /// In-container mount point for . + public const string ContainerDir = "/certs"; + + /// Leaf PFX consumed by Kestrel as the default HTTPS certificate. + public const string LeafPfx = "/certs/leaf.pfx"; + + /// Leaf certificate (PEM) consumed by the nginx TLS server block. + public const string LeafCrt = "/certs/leaf.crt"; + + /// Leaf private key (PEM) consumed by the nginx TLS server block. + public const string LeafKey = "/certs/leaf.key"; + + /// Root CA certificate served by TrustController's well-known routes. + public const string RootCaCrt = "/certs/rootCA.crt"; + + /// Root CA SHA-256 fingerprint file; also sources the ETag on the cert routes. + public const string RootCaFingerprint = "/certs/rootCA.sha256.txt"; +} diff --git a/hosts/Interfold.AppHost/ComposeDependencyCondition.cs b/hosts/Interfold.AppHost/ComposeDependencyCondition.cs new file mode 100644 index 00000000..d695e89a --- /dev/null +++ b/hosts/Interfold.AppHost/ComposeDependencyCondition.cs @@ -0,0 +1,12 @@ +namespace Interfold.AppHost; + +/// +/// The depends_on condition spellings the compose graph assigns. Values are part of +/// the Docker Compose specification (condition: under depends_on) — not ours +/// to rename. +/// +internal static class ComposeDependencyCondition +{ + /// Wait for the dependency's healthcheck to pass, not merely for its container to start. + public const string ServiceHealthy = "service_healthy"; +} diff --git a/hosts/Interfold.AppHost/ContainerEnvNames.cs b/hosts/Interfold.AppHost/ContainerEnvNames.cs new file mode 100644 index 00000000..42fe7ca7 --- /dev/null +++ b/hosts/Interfold.AppHost/ContainerEnvNames.cs @@ -0,0 +1,47 @@ +namespace Interfold.AppHost; + +/// Env-var names the AppHost graph sets on third-party containers. Also read +/// inside compose healthcheck / shell one-liners, so C# +/// registration and in-container $VAR expansion share these constants. Names are +/// dictated by upstream entrypoints — not ours to rename. +internal static class ContainerEnvNames +{ + // --- Postgres / TimescaleDB (official entrypoint + timescaledb-tune init script) --- + public const string PostgresUser = "POSTGRES_USER"; + public const string PostgresPassword = "POSTGRES_PASSWORD"; + public const string PostgresInitDbArgs = "POSTGRES_INITDB_ARGS"; + public const string PgData = "PGDATA"; + public const string PgCtlTimeout = "PGCTLTIMEOUT"; + public const string TsTuneMemory = "TS_TUNE_MEMORY"; + public const string TsTuneNumCpus = "TS_TUNE_NUM_CPUS"; + + // --- CQL clients (cqlsh reads these inside both Scylla and Cassandra containers) --- + public const string CqlshUser = "CQLSH_USER"; + public const string CqlshPassword = "CQLSH_PASSWORD"; + + // --- Cassandra (official entrypoint) --- + public const string CassandraClusterName = "CASSANDRA_CLUSTER_NAME"; + public const string CassandraListenAddress = "CASSANDRA_LISTEN_ADDRESS"; + public const string CassandraBroadcastAddress = "CASSANDRA_BROADCAST_ADDRESS"; + public const string CassandraBroadcastRpcAddress = "CASSANDRA_BROADCAST_RPC_ADDRESS"; + public const string CassandraRpcAddress = "CASSANDRA_RPC_ADDRESS"; + public const string CassandraEndpointSnitch = "CASSANDRA_ENDPOINT_SNITCH"; + public const string CassandraNumTokens = "CASSANDRA_NUM_TOKENS"; + public const string CassandraDc = "CASSANDRA_DC"; + public const string CassandraRack = "CASSANDRA_RACK"; + public const string MaxHeapSize = "MAX_HEAP_SIZE"; + public const string HeapNewSize = "HEAP_NEWSIZE"; + + // --- Interfold API container (ASP.NET Core hosting + app secrets) --- + public const string EncryptionPrivateKey = "ENCRYPTION_PRIVATE_KEY"; + public const string AspNetCoreHttpPorts = "ASPNETCORE_HTTP_PORTS"; + public const string AspNetCoreHttpsPorts = "ASPNETCORE_HTTPS_PORTS"; + public const string AspNetCoreKestrelDefaultCertPath = "ASPNETCORE_Kestrel__Certificates__Default__Path"; + + // --- Octocon web (official nginx image's envsubst-on-templates entrypoint) --- + public const string NginxServerName = "NGINX_SERVER_NAME"; + public const string NginxSslCertFile = "NGINX_SSL_CERT_FILE"; + public const string NginxSslKeyFile = "NGINX_SSL_KEY_FILE"; + public const string NginxHttpsPortSuffix = "NGINX_HTTPS_PORT_SUFFIX"; + public const string NginxEnvsubstFilter = "NGINX_ENVSUBST_FILTER"; +} diff --git a/hosts/Interfold.AppHost/DevSeed/DevSeedContext.cs b/hosts/Interfold.AppHost/DevSeed/DevSeedContext.cs new file mode 100644 index 00000000..f25dd303 --- /dev/null +++ b/hosts/Interfold.AppHost/DevSeed/DevSeedContext.cs @@ -0,0 +1,46 @@ +using Aspire.Hosting.ApplicationModel; +using Microsoft.Extensions.Configuration; + +namespace Interfold.AppHost.DevSeed; + +/// +/// Concrete resource / parameter references the needs at +/// runtime, resolved once during and +/// injected via the DI singleton. Kept as a POCO — the hosted service uses it read-only. +/// +/// +/// Every Aspire-managed secret is exposed as its so the +/// hosted service can call — that path always +/// returns the current effective value, whereas only sees +/// GenerateParameterDefault outputs on subsequent runs (after they're persisted to +/// user-secrets). Static / operator-set values (usernames, db name) still come from +/// configuration since they never regenerate. +/// +internal sealed class DevSeedContext( + DevSeedResource seedResource, + IResourceWithEndpoints msgDbResource, + IResourceWithEndpoints? cqlBackendResource, + ParameterResource postgresInitPassword, + ParameterResource postgresAppPassword, + ParameterResource scyllaAppPassword, + ParameterResource encryptionPepper, + ParameterResource deepLinkSecret, + ParameterResource postgresAdminPassword, + ParameterResource scyllaAdminPassword, + IConfiguration configuration) +{ + public DevSeedResource SeedResource { get; } = seedResource; + public IResourceWithEndpoints MsgDbResource { get; } = msgDbResource; + /// First CQL backend the AppHost mapped a host port to (Scylla node 0 in the + /// default graph; Cassandra in the cassandra-only launch profile). Null when neither is + /// enabled — the hosted service skips the CQL seed step. + public IResourceWithEndpoints? CqlBackendResource { get; } = cqlBackendResource; + public ParameterResource PostgresInitPassword { get; } = postgresInitPassword; + public ParameterResource PostgresAppPassword { get; } = postgresAppPassword; + public ParameterResource ScyllaAppPassword { get; } = scyllaAppPassword; + public ParameterResource EncryptionPepper { get; } = encryptionPepper; + public ParameterResource DeepLinkSecret { get; } = deepLinkSecret; + public ParameterResource PostgresAdminPassword { get; } = postgresAdminPassword; + public ParameterResource ScyllaAdminPassword { get; } = scyllaAdminPassword; + public IConfiguration Configuration { get; } = configuration; +} diff --git a/hosts/Interfold.AppHost/DevSeed/DevSeedExtensions.cs b/hosts/Interfold.AppHost/DevSeed/DevSeedExtensions.cs new file mode 100644 index 00000000..a8c69f60 --- /dev/null +++ b/hosts/Interfold.AppHost/DevSeed/DevSeedExtensions.cs @@ -0,0 +1,86 @@ +using Aspire.Hosting.ApplicationModel; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Interfold.AppHost.DevSeed; + +/// +/// Wires the dev-mode seed pipeline into an . +/// Only ever called from the RunMode branch of InterfoldAppHost.Configure; publish +/// mode never sees any of these registrations so the bootstrapper's own compose-exec seed +/// path stays authoritative for self-hosted deployments. +/// +internal static class DevSeedExtensions +{ + /// + /// Registers the four generated dev-secret parameters, adds a marker + /// that consumers can WaitFor(...), and hooks the + /// that drives the actual Postgres/Scylla seed. + /// + /// Aspire-managed transient bootstrap password (used to + /// authenticate against msg-db as db_init before PostgresSeeder mints the app roles). + /// Aspire-managed application role password + /// (Parameters:postgres-password) — passed through to PostgresSeeder so the + /// app role created here matches what ConfigureApiCommon hands the API. + /// Aspire-managed CQL app role password + /// (Parameters:scylla-password) — same story as + /// but for ScyllaSeeder. + /// First CQL backend the AppHost mapped a host port to. Null when + /// neither Scylla nor Cassandra are in the graph — the CQL seed step is skipped. + /// The so the caller can pass it + /// to apiProject.WaitFor(...). + public static IResourceBuilder AddDevSeedPipeline( + this IDistributedApplicationBuilder builder, + IResourceBuilder msgDb, + IResourceBuilder? cqlBackend, + IResourceBuilder postgresInitPassword, + IResourceBuilder postgresAppPassword, + IResourceBuilder scyllaAppPassword) + { + // GenerateParameterDefault + persist:true writes the value into the AppHost's + // dotnet user-secrets store on first Build(), so subsequent runs reuse it — matches + // the postgres-init-password pattern already in the graph. + var encryptionPepper = builder.AddParameter( + DevSeedParameterNames.EncryptionPepper, + new GenerateParameterDefault { MinLength = 32 }, + secret: true, persist: true); + var deepLinkSecret = builder.AddParameter( + DevSeedParameterNames.DeepLinkSecret, + new GenerateParameterDefault { MinLength = 32 }, + secret: true, persist: true); + var postgresAdminPassword = builder.AddParameter( + DevSeedParameterNames.PostgresAdminPassword, + new GenerateParameterDefault { MinLength = 32 }, + secret: true, persist: true); + var scyllaAdminPassword = builder.AddParameter( + DevSeedParameterNames.ScyllaAdminPassword, + new GenerateParameterDefault { MinLength = 32 }, + secret: true, persist: true); + + var seedResourceBuilder = builder.AddResource(new DevSeedResource("db-seed")); + + var context = new DevSeedContext( + seedResource: seedResourceBuilder.Resource, + msgDbResource: msgDb.Resource, + cqlBackendResource: cqlBackend?.Resource, + postgresInitPassword: postgresInitPassword.Resource, + postgresAppPassword: postgresAppPassword.Resource, + scyllaAppPassword: scyllaAppPassword.Resource, + encryptionPepper: encryptionPepper.Resource, + deepLinkSecret: deepLinkSecret.Resource, + postgresAdminPassword: postgresAdminPassword.Resource, + scyllaAdminPassword: scyllaAdminPassword.Resource, + configuration: builder.Configuration); + + builder.Services.AddSingleton(context); + builder.Services.AddHostedService(); + // Aspire's OrchestratorHostService.StartAsync blocks until every WaitFor-gated + // resource unblocks — with the default sequential IHost startup, our + // BackgroundService.StartAsync never runs, db-seed never flips to Running, and + // interfold-api stays Waiting forever. Opting into concurrent startup lets our + // hosted service fire in parallel and publish the state the orchestrator is waiting on. + builder.Services.Configure(o => o.ServicesStartConcurrently = true); + + return seedResourceBuilder; + } +} diff --git a/hosts/Interfold.AppHost/DevSeed/DevSeedHostedService.cs b/hosts/Interfold.AppHost/DevSeed/DevSeedHostedService.cs new file mode 100644 index 00000000..98f6fcfb --- /dev/null +++ b/hosts/Interfold.AppHost/DevSeed/DevSeedHostedService.cs @@ -0,0 +1,236 @@ +using System.Security.Cryptography; +using Aspire.Hosting.ApplicationModel; +using Interfold.DatabaseBootstrap; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Interfold.AppHost.DevSeed; + +/// +/// Runs the in-process Postgres + Scylla seed after msg-db (and, when +/// Scylla, the first Scylla node) reach +/// , then flips +/// to Running so any +/// apiProject.WaitFor(seedResource) unblocks. Publish mode never registers this +/// service (guard lives on the extension method), so the bootstrapper's +/// DatabaseInitPhase stays authoritative for self-hosted deployments. +/// +/// +/// The seeders themselves are idempotent (see PostgresSeeder.AppRoleAlreadyConfigured +/// / ScyllaSeeder.AdminRoleAlreadyConfigured). Persistent-container reruns hit the +/// short-circuit and cost only a couple of probe round-trips. The JWT PEMs generated below +/// are wasted on repeat runs for the same reason — they're only ever written into +/// internal.secrets on the first pass. +/// +internal sealed class DevSeedHostedService( + DevSeedContext context, + ResourceNotificationService notifications, + ILogger logger) + : BackgroundService +{ + // The msg-db / scylla containers can be slow to reach Running on a fresh image pull. + // Give the whole seed budget the same 10min ceiling the bootstrapper's Postgres readiness + // probe uses; individual sub-steps have tighter caps inside DbInitHelper. + private static readonly TimeSpan SeedBudget = TimeSpan.FromMinutes(10); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using var budgetCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); + budgetCts.CancelAfter(SeedBudget); + var ct = budgetCts.Token; + + try + { + await notifications.PublishUpdateAsync(context.SeedResource, + snap => snap with { State = new ResourceStateSnapshot(KnownResourceStates.Starting, "") }); + + logger.LogInformation("Dev seed: waiting for msg-db to reach Running"); + await notifications.WaitForResourceAsync(context.MsgDbResource.Name, KnownResourceStates.Running, ct); + + var pgEndpoint = context.MsgDbResource.GetEndpoint("postgres"); + if (!pgEndpoint.IsAllocated) + { + throw new InvalidOperationException( + "msg-db reached Running but the 'postgres' endpoint is not allocated. " + + "This shouldn't happen — check the AppHost resource graph."); + } + + // GenerateParameterDefault outputs land in user-secrets on Save (post-Build), so + // on a first-run cold start Configuration[PostgresInitPassword] is still null. + // GetValueAsync always returns the in-memory value the AppHost handed to the + // msg-db container, first run or not. + var initPassword = await context.PostgresInitPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Postgres init password parameter resolved to null."); + var initConnectionString = BuildInitConnectionString(pgEndpoint.Host, pgEndpoint.Port, initPassword); + logger.LogInformation("Dev seed: waiting for Postgres to accept connections at {Host}:{Port}", + pgEndpoint.Host, pgEndpoint.Port); + await InProcessSeedWaits.WaitForPostgresAsync( + initConnectionString, + // 3 consecutive successes matches the bootstrapper's readiness gate — filters out + // the socket-open-but-still-crash-recovering window on cold TimescaleDB starts. + new PostgresReadinessOptions(TimeSpan.FromMinutes(6), 3), + ct); + + var cqlEndpoint = await ResolveCqlEndpointIfIncludedAsync(ct); + + var pgOptions = await BuildPostgresSeedOptionsAsync(cqlEndpoint, initPassword, ct); + logger.LogInformation("Dev seed: bootstrapping Postgres roles + internal.secrets"); + var pgExecutor = new NpgsqlPostgresExecutor(initConnectionString); + await PostgresSeeder.BootstrapAsync(pgExecutor, pgOptions, NoOpDatabaseInitLogger.Instance, ct); + + if (cqlEndpoint is not null) + { + logger.LogInformation("Dev seed: waiting for CQL backend at {Host}:{Port}", + cqlEndpoint.Host, cqlEndpoint.Port); + await InProcessSeedWaits.WaitForScyllaAsync(cqlEndpoint.Host, cqlEndpoint.Port, ct); + + var scyllaOptions = await BuildScyllaSeedOptionsAsync(ct); + logger.LogInformation("Dev seed: bootstrapping CQL roles"); + // ScyllaSeeder works against Cassandra too — same CREATE ROLE / ALTER ROLE surface, + // and Cassandra ships with the same cassandra/cassandra bootstrap superuser. + var scyllaExecutor = new DataStaxScyllaExecutor(cqlEndpoint.Host, cqlEndpoint.Port); + await ScyllaSeeder.BootstrapAsync(scyllaExecutor, scyllaOptions, NoOpDatabaseInitLogger.Instance, ct); + } + + await notifications.PublishUpdateAsync(context.SeedResource, + snap => snap with { State = new ResourceStateSnapshot(KnownResourceStates.Running, "Success") }); + logger.LogInformation("Dev seed: complete"); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + // Host shutting down mid-seed — don't spam the dashboard with a stack trace. + await notifications.PublishUpdateAsync(context.SeedResource, + snap => snap with { State = new ResourceStateSnapshot(KnownResourceStates.Exited, "Cancelled") }); + } + catch (Exception ex) + { + logger.LogError(ex, "Dev seed failed; the API resource's WaitFor(db-seed) will not unblock."); + await notifications.PublishUpdateAsync(context.SeedResource, + snap => snap with { State = new ResourceStateSnapshot(KnownResourceStates.FailedToStart, "Error") }); + throw; + } + } + + private async Task ResolveCqlEndpointIfIncludedAsync(CancellationToken ct) + { + if (context.CqlBackendResource is null) return null; + + // Multi-node Scylla stages joins so later nodes may still be Joining when this fires; + // the first node (which owns the host-mapped CQL endpoint) is the one we care about. + await notifications.WaitForResourceAsync(context.CqlBackendResource.Name, KnownResourceStates.Running, ct); + var endpoint = context.CqlBackendResource.GetEndpoint("cql"); + if (!endpoint.IsAllocated) + { + throw new InvalidOperationException( + $"CQL resource '{context.CqlBackendResource.Name}' reached Running but the 'cql' endpoint " + + "is not allocated. This shouldn't happen — check the AppHost resource graph."); + } + return endpoint; + } + + private static string BuildInitConnectionString(string host, int port, string initPassword) + { + // db_init is the transient bootstrap superuser the msg-db container starts with. We + // connect against the default `postgres` maintenance DB so PostgresSeeder can create + // the app database with the admin role as owner. + return $"Host={host};Port={port};Username={PostgresRoles.Init};Password={initPassword};Database=postgres;SSL Mode=Disable"; + } + + private async Task BuildPostgresSeedOptionsAsync( + EndpointReference? cqlEndpoint, string initPassword, CancellationToken ct) + { + // Static / operator-set values (usernames, db name) come from configuration because + // they never regenerate; passwords come from ParameterResource because + // GenerateParameterDefault outputs aren't in IConfiguration on the first run. + var appUser = context.Configuration[AppHostParameterKeys.PostgresUser] + ?? throw new InvalidOperationException( + $"Missing configuration for '{AppHostParameterKeys.PostgresUser}'."); + var defaultDb = context.Configuration[AppHostParameterKeys.PostgresDb] ?? "interfold"; + var scyllaAppUser = context.Configuration[AppHostParameterKeys.ScyllaUser] + ?? throw new InvalidOperationException( + $"Missing configuration for '{AppHostParameterKeys.ScyllaUser}'."); + + var appPassword = await context.PostgresAppPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Postgres app password parameter resolved to null."); + var scyllaAppPassword = await context.ScyllaAppPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Scylla app password parameter resolved to null."); + var postgresAdminPassword = await context.PostgresAdminPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Postgres admin password parameter resolved to null."); + var scyllaAdminPassword = await context.ScyllaAdminPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Scylla admin password parameter resolved to null."); + var encryptionPepper = await context.EncryptionPepper.GetValueAsync(ct) + ?? throw new InvalidOperationException("Encryption pepper parameter resolved to null."); + var deepLinkSecret = await context.DeepLinkSecret.GetValueAsync(ct) + ?? throw new InvalidOperationException("Deep-link secret parameter resolved to null."); + + // JWT PEMs regenerate on every AppHost start — the seeder short-circuits on subsequent + // runs so only the first run actually writes them into internal.secrets. Wiping msg-db + // volumes invalidates every issued JWT for the same reason a `rotate-secrets` does. + var (rsaPem, es256Pem) = GenerateJwtPems(); + + // Falls back to 9042 (the container-internal CQL port) when no CQL backend is enabled — + // the API path that reads scylla:port never runs in that case. + var scyllaPort = cqlEndpoint?.Port ?? 9042; + + return new PostgresSeedOptions( + InitUser: PostgresRoles.Init, + InitPassword: initPassword, + AppUser: appUser, + AppPassword: appPassword, + // "{app}_admin" mirrors DatabaseInitPhase.BuildPostgresSeedOptions so dev + self-host + // stay wire-compatible with any tooling that reads admin creds out of internal.secrets. + AdminUser: $"{appUser}_admin", + AdminPassword: postgresAdminPassword, + DefaultDatabase: defaultDb, + GoogleOAuthClientSecret: string.Empty, + DiscordOAuthClientSecret: string.Empty, + AppleOAuthClientSecret: string.Empty, + EncryptionPepper: encryptionPepper, + ScyllaContactPoints: cqlEndpoint?.Host ?? "127.0.0.1", + ScyllaLocalDatacenter: ScyllaKeyspace.Nam.ToWire(), + ScyllaAppUser: scyllaAppUser, + ScyllaAppPassword: scyllaAppPassword, + ScyllaPort: scyllaPort, + ScyllaAdminUser: $"{scyllaAppUser}_admin", + ScyllaAdminPassword: scyllaAdminPassword, + JwtRsa256PrivateKeyPem: rsaPem, + JwtEs256PrivateKeyPem: es256Pem, + DeepLinkSecret: deepLinkSecret, + LeafPfxPassword: string.Empty, + // Never scramble in dev — persistent-container reruns still need db_init to + // authenticate for the idempotency probe (bootstrapper only scrambles in prod). + ScrambleInitUserPassword: false); + } + + private async Task BuildScyllaSeedOptionsAsync(CancellationToken ct) + { + var scyllaAppUser = context.Configuration[AppHostParameterKeys.ScyllaUser] + ?? throw new InvalidOperationException( + $"Missing configuration for '{AppHostParameterKeys.ScyllaUser}'."); + var scyllaAppPassword = await context.ScyllaAppPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Scylla app password parameter resolved to null."); + var scyllaAdminPassword = await context.ScyllaAdminPassword.GetValueAsync(ct) + ?? throw new InvalidOperationException("Scylla admin password parameter resolved to null."); + + return new ScyllaSeedOptions( + AppUser: scyllaAppUser, + AppPassword: scyllaAppPassword, + AdminUser: $"{scyllaAppUser}_admin", + AdminPassword: scyllaAdminPassword, + // Locking cassandra/cassandra matches self-host behaviour so a dev-mode operator + // who copies compose flags across gets the same security profile. + LockDefaultCassandra: true); + } + + private static (string Rsa256Pem, string Es256Pem) GenerateJwtPems() + { + using var rsa = RSA.Create(2048); + var rsaPem = rsa.ExportPkcs8PrivateKeyPem(); + + using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256); + var esPem = ecdsa.ExportPkcs8PrivateKeyPem(); + return (rsaPem, esPem); + } +} diff --git a/hosts/Interfold.AppHost/DevSeed/DevSeedParameterNames.cs b/hosts/Interfold.AppHost/DevSeed/DevSeedParameterNames.cs new file mode 100644 index 00000000..e4c66a5b --- /dev/null +++ b/hosts/Interfold.AppHost/DevSeed/DevSeedParameterNames.cs @@ -0,0 +1,19 @@ +namespace Interfold.AppHost.DevSeed; + +/// +/// Aspire parameter names for dev-mode-only secrets consumed by . +/// These are AppHost-private (never crossed to Interfold.Bootstrapper.PublishPhase) — +/// self-hosted deployments generate the equivalent values inside SecretsPhase and +/// seed them straight into internal.secrets. Constants live here (not on +/// ) precisely +/// because they must never appear in the emitted compose .env. +/// +internal static class DevSeedParameterNames +{ + // Aspire's AddParameter takes the bare name (no "Parameters:" prefix). GenerateParameterDefault + // writes the value into `dotnet user-secrets` under the AppHost's UserSecretsId on first Build(). + public const string EncryptionPepper = "dev-encryption-pepper"; + public const string DeepLinkSecret = "dev-deep-link-secret"; + public const string PostgresAdminPassword = "dev-postgres-admin-password"; + public const string ScyllaAdminPassword = "dev-scylla-admin-password"; +} diff --git a/hosts/Interfold.AppHost/DevSeed/DevSeedResource.cs b/hosts/Interfold.AppHost/DevSeed/DevSeedResource.cs new file mode 100644 index 00000000..513655de --- /dev/null +++ b/hosts/Interfold.AppHost/DevSeed/DevSeedResource.cs @@ -0,0 +1,12 @@ +using Aspire.Hosting.ApplicationModel; + +namespace Interfold.AppHost.DevSeed; + +/// +/// Marker so apiProject.WaitFor(seedResource) can gate on +/// the dev-mode Postgres + Scylla seed completing. Owns no runtime state — the actual +/// wait+seed loop lives in , which publishes the +/// // +/// transitions on this resource. +/// +internal sealed class DevSeedResource(string name) : Resource(name); diff --git a/hosts/Interfold.AppHost/DockerExecCqlProbe.cs b/hosts/Interfold.AppHost/DockerExecCqlProbe.cs new file mode 100644 index 00000000..de53d589 --- /dev/null +++ b/hosts/Interfold.AppHost/DockerExecCqlProbe.cs @@ -0,0 +1,173 @@ +using System.Diagnostics; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace Interfold.AppHost; + +/// Runs a readiness probe against a Scylla container via docker exec. Feeds +/// the per-node {name}-cql health checks in +/// so each node's start gates on the previous node's CQL listener actually accepting +/// traffic. Credentials stay in the container's env (CQLSH_USER/CQLSH_PASSWORD) so no +/// secrets flow through the AppHost's argv. Container name is resolved per-probe by +/// docker ps filter — the DCP side-channel doesn't fire reliably under +/// Aspire.Hosting.Testing's in-process host (see multinode-stagger-debug.log). +internal static class DockerExecCqlProbe +{ + // 10s is a comfortable ceiling for the slowest CI: docker exec cold-start (~50–200ms) + // + cqlsh handshake (~500–1500ms) + nodetool fallback of the same order. + private static readonly TimeSpan ProbeTimeout = TimeSpan.FromSeconds(10); + + // Single $ is correct here (docker exec bypasses compose YAML — the compose-level probe + // uses $$ to escape past compose's own interpolation pass). + private const string ReadinessScript = + $"cqlsh -u \"${ContainerEnvNames.CqlshUser}\" -p \"${ContainerEnvNames.CqlshPassword}\" -e 'DESCRIBE CLUSTER' >/dev/null 2>&1 " + + "|| nodetool status | grep -q '^UN'"; + + /// Resolves the runtime container name for + /// and runs the probe. Returns Unhealthy while the container hasn't been allocated yet + /// (Aspire retries on its polling interval). + public static async Task RunAsync(string resourceName, CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(resourceName)) + { + return HealthCheckResult.Unhealthy("resource name is empty"); + } + + var (containerName, lookupError) = await ResolveContainerNameAsync(resourceName, cancellationToken).ConfigureAwait(false); + if (containerName is null) + { + return HealthCheckResult.Unhealthy(lookupError ?? "container not yet allocated"); + } + + return await RunExecProbeAsync(containerName, cancellationToken).ConfigureAwait(false); + } + + private static async Task<(string? ContainerName, string? Error)> ResolveContainerNameAsync(string resourceName, CancellationToken cancellationToken) + { + // DCP stamps `aspire-resource-name=` on every container (stable across + // Aspire 9.x / 13.x). Prefix match is the fallback for orchestrator builds that don't. + var labelMatch = await DockerPsAsync( + ["--filter", $"label=aspire-resource-name={resourceName}", "--format", "{{.Names}}"], + cancellationToken).ConfigureAwait(false); + if (labelMatch.Exit == 0 && !string.IsNullOrWhiteSpace(labelMatch.Stdout)) + { + var first = labelMatch.Stdout.Trim().Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + if (!string.IsNullOrWhiteSpace(first)) + { + return (first, null); + } + } + + var prefixMatch = await DockerPsAsync( + ["--filter", $"name=^{resourceName}-", "--format", "{{.Names}}"], + cancellationToken).ConfigureAwait(false); + if (prefixMatch.Exit == 0 && !string.IsNullOrWhiteSpace(prefixMatch.Stdout)) + { + var first = prefixMatch.Stdout.Trim().Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + if (!string.IsNullOrWhiteSpace(first)) + { + return (first, null); + } + } + + if (labelMatch.Exit != 0) + { + return (null, $"docker ps (label) exit={labelMatch.Exit}: {labelMatch.Stderr.Trim()}"); + } + if (prefixMatch.Exit != 0) + { + return (null, $"docker ps (prefix) exit={prefixMatch.Exit}: {prefixMatch.Stderr.Trim()}"); + } + return (null, $"no running container matches resource '{resourceName}'"); + } + + private static async Task RunExecProbeAsync(string containerName, CancellationToken cancellationToken) + { + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + psi.ArgumentList.Add("exec"); + psi.ArgumentList.Add(containerName); + psi.ArgumentList.Add("sh"); + psi.ArgumentList.Add("-c"); + psi.ArgumentList.Add(ReadinessScript); + + var (exit, stdout, stderr) = await RunDockerProcessAsync(psi, cancellationToken).ConfigureAwait(false); + if (exit == 0) + { + return HealthCheckResult.Healthy(); + } + if (exit == -1) + { + return HealthCheckResult.Unhealthy(stderr); + } + + var detail = stderr.Length > 0 ? stderr : stdout; + var trimmed = detail.Length > 256 ? detail[..256] : detail; + return HealthCheckResult.Unhealthy($"docker exec exit={exit}: {trimmed.Trim()}"); + } + + private static async Task<(int Exit, string Stdout, string Stderr)> DockerPsAsync( + IEnumerable args, CancellationToken cancellationToken) + { + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + psi.ArgumentList.Add("ps"); + foreach (var a in args) + { + psi.ArgumentList.Add(a); + } + return await RunDockerProcessAsync(psi, cancellationToken).ConfigureAwait(false); + } + + private static async Task<(int Exit, string Stdout, string Stderr)> RunDockerProcessAsync( + ProcessStartInfo psi, CancellationToken cancellationToken) + { + using var probeCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + probeCts.CancelAfter(ProbeTimeout); + + Process? proc = null; + try + { + proc = Process.Start(psi); + if (proc is null) + { + return (-1, string.Empty, "failed to spawn docker"); + } + + var stdoutTask = proc.StandardOutput.ReadToEndAsync(probeCts.Token); + var stderrTask = proc.StandardError.ReadToEndAsync(probeCts.Token); + await proc.WaitForExitAsync(probeCts.Token).ConfigureAwait(false); + var stdout = await stdoutTask.ConfigureAwait(false); + var stderr = await stderrTask.ConfigureAwait(false); + return (proc.ExitCode, stdout, stderr); + } + catch (OperationCanceledException) when (probeCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) + { + TryKill(proc); + return (-1, string.Empty, $"docker probe timed out after {ProbeTimeout.TotalSeconds:F0}s"); + } + catch (Exception ex) + { + TryKill(proc); + return (-1, string.Empty, $"docker probe failed: {ex.GetType().Name}: {ex.Message}"); + } + } + + private static void TryKill(Process? proc) + { + if (proc is null || proc.HasExited) + { + return; + } + try { proc.Kill(entireProcessTree: true); } catch { } + } +} diff --git a/hosts/Interfold.AppHost/ImageRef.cs b/hosts/Interfold.AppHost/ImageRef.cs new file mode 100644 index 00000000..8601ec0e --- /dev/null +++ b/hosts/Interfold.AppHost/ImageRef.cs @@ -0,0 +1,20 @@ +namespace Interfold.AppHost; + +/// Parsed image reference (name + tag). Tags only — SHA digests would need a +/// different code path. +internal readonly record struct ImageRef(string Image, string Tag) +{ + /// Parses ghcr.io/foo/bar:1.2.3-style refs. Missing tag → latest. + public static ImageRef Parse(string reference) + { + // Tag separator is the LAST colon after the LAST slash — any earlier colon belongs + // to a registry-hostname port (e.g. localhost:5000/foo:tag). + var lastSlash = reference.LastIndexOf('/'); + var lastColon = reference.LastIndexOf(':'); + if (lastColon > lastSlash && lastColon > 0) + { + return new ImageRef(reference[..lastColon], reference[(lastColon + 1)..]); + } + return new ImageRef(reference, "latest"); + } +} diff --git a/hosts/Interfold.AppHost/Interfold.AppHost.csproj b/hosts/Interfold.AppHost/Interfold.AppHost.csproj new file mode 100644 index 00000000..38381708 --- /dev/null +++ b/hosts/Interfold.AppHost/Interfold.AppHost.csproj @@ -0,0 +1,28 @@ + + + + Exe + net10.0 + enable + enable + 9f2459e8-120e-4c06-8ae5-392685a34c40 + + + + + + + + + + + + + + + diff --git a/hosts/Interfold.AppHost/InterfoldAppHost.cs b/hosts/Interfold.AppHost/InterfoldAppHost.cs new file mode 100644 index 00000000..9fa79ef2 --- /dev/null +++ b/hosts/Interfold.AppHost/InterfoldAppHost.cs @@ -0,0 +1,751 @@ +using Aspire.Hosting.Docker.Resources.ComposeNodes; +using Interfold.AppHost.DevSeed; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Microsoft.Extensions.DependencyInjection; +// Aspire.Hosting.ApplicationModel also defines PersistenceMode; alias to disambiguate. +using PersistenceMode = Interfold.Shared.Contracts.PersistenceMode; + +namespace Interfold.AppHost; + +/// Interfold distributed-application resource graph. Consumed by the dev +/// Interfold.AppHost executable (`aspire run`) and by the self-hosting +/// Interfold.Bootstrapper which pre-populates +/// with generated secrets before calling . +public static class InterfoldAppHost +{ + // Health-check names must match between AddHealthChecks().AddCheck and WithHealthCheck + // or the annotation silently never resolves. + private const string MsgDbHealthCheckName = "msg-db-health"; + private const string ScyllaHealthCheckName = "scylla-health"; + + /// Per-node CQL readiness gate name ({resource}-cql). + private static string ScyllaCqlCheckName(string resourceName) => $"{resourceName}-cql"; + + private const string PostgresEndpointName = "postgres"; + private const string CqlEndpointName = "cql"; + private const string HttpEndpointName = "http"; + private const string HttpsEndpointName = "https"; + + /// Scheme for raw TCP endpoints (Postgres, CQL). HTTP/HTTPS go through the + /// typed overloads. + private const string TcpScheme = "tcp"; + + private const string ComposeEnvironmentName = "docker-compose"; + + /// Aspire's default dashboard service name ({environment}-dashboard). + private const string DashboardComposeServiceName = ComposeEnvironmentName + "-dashboard"; + + // Canonical port-slot defaults (mirrored by appsettings.json's Ports block). + private const int DefaultPostgresPort = 4200; + private const int DefaultScyllaPort = 9042; + private const int DefaultCassandraPort = 9043; + private const int DefaultApiHttpPort = 5000; + private const int DefaultApiHttpsPort = 5001; + private const int DefaultApiContainerHttpPort = 5100; + private const int DefaultApiContainerHttpsPort = 5101; + private const int DefaultWebHttpPort = 8080; + private const int DefaultWebHttpsPort = 8081; + + /// CQL cluster-name fallback for dev `aspire run`; the bootstrapper always overrides. + private const string DefaultClusterName = "InterfoldCluster"; + + /// Registers the full Interfold resource graph. Does not call + /// Build() or Run(). + public static void Configure(IDistributedApplicationBuilder builder) + { + int Port(string key, int fallback) => int.TryParse(builder.Configuration[key], out var p) ? p : fallback; + + // ParamName keeps parameter declaration and IConfiguration override on the same spelling. + static string ParamName(string key) => AppHostParameterKeys.ToParameterName(key); + var postgresPort = Port(AppHostParameterKeys.PortsPostgres, DefaultPostgresPort); + var scyllaPort = Port(AppHostParameterKeys.PortsScylla, DefaultScyllaPort); + // Cassandra owns its own port so SharedDbFixture can publish both CQL backends + // side-by-side; legacy Cassandra-only mode falls back to scyllaPort further down. + var cassandraPort = Port(AppHostParameterKeys.PortsCassandra, DefaultCassandraPort); + var apiHttpPort = Port(AppHostParameterKeys.PortsApiHttp, DefaultApiHttpPort); + var apiHttpsPort = Port(AppHostParameterKeys.PortsApiHttps, DefaultApiHttpsPort); + // Must match the API image's EXPOSE/ARG defaults (5100/5101, see /Dockerfile); + // rebuilding the image with different ports means updating both Ports:api-container-* + // entries so targetPort, ASPNETCORE_*_PORTS, and the compose healthcheck URL all align. + var apiContainerHttpPort = Port(AppHostParameterKeys.PortsApiContainerHttp, DefaultApiContainerHttpPort); + var apiContainerHttpsPort = Port(AppHostParameterKeys.PortsApiContainerHttps, DefaultApiContainerHttpsPort); + var webHttpPort = Port(AppHostParameterKeys.PortsWebHttp, DefaultWebHttpPort); + var webHttpsPort = Port(AppHostParameterKeys.PortsWebHttps, DefaultWebHttpsPort); + + // Bench mode is the auto-managed cross-project DB fixture (see + // TestBenchCoordinator + BenchSharedDb). When on it forces api/web/dashboard off, + // switches on persistent-containers, pins stable container names on the three DB + // resources, and — via TestBenchReadyEmitter — writes a machine-readable readiness + // line to stdout after WaitForResourcesAsync so the launcher can exit cleanly. + var testBenchMode = BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.TestBenchMode], fallback: false); + + var includeApi = testBenchMode + ? false + : BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludeApi], fallback: true); + var persistentContainers = testBenchMode + ? true + : BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.PersistentContainers], fallback: true); + + // Both CQL backends can be on simultaneously (SharedDbFixture uses this to + // exercise Scylla + Cassandra under one Aspire host). scylla-topology only + // affects layout when include-scylla=true. + var includeScylla = BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludeScylla], fallback: true); + var includeCassandra = BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludeCassandra], fallback: false); + var scyllaTopology = ScyllaTopologyExtensions.Parse(builder.Configuration[AppHostParameterKeys.ScyllaTopology]); + var isMultiScylla = scyllaTopology == ScyllaTopology.Multi; + + // Resolve regions ONCE so health-check registrations and node resources can't diverge. + // Multi: one node per ScyllaKeyspace. Single: one NAM node under the un-suffixed name. + ScyllaKeyspace[] scyllaRegions = isMultiScylla ? Enum.GetValues() : [ScyllaKeyspace.Nam]; + var isMultiScyllaNode = scyllaRegions.Length > 1; + + // CQL cluster identity — Cassandra reads CASSANDRA_CLUSTER_NAME, Scylla takes it + // via --cluster-name (immune to image bake-time changes). Pure metadata. + var clusterName = builder.Configuration[AppHostParameterKeys.ClusterName] ?? DefaultClusterName; + + // Off only for MultiNodeScyllaFixture (which shares SharedDbFixture's msg-db + // instead of spinning up a redundant second Postgres). + var includePostgres = BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludePostgres], fallback: true); + + if (includeApi && !includePostgres) + { + throw new InvalidOperationException( + "Parameters:include-api=true requires Parameters:include-postgres=true (the API depends on msg-db)."); + } + + // Skip host-port TCP probes in test mode where Aspire randomises host ports — + // WaitFor() then falls back to Running state. + if (includeApi) + { + builder.Services.AddHealthChecks() + .AddCheck(MsgDbHealthCheckName, HostPortTcpProbe.CreateCheck(postgresPort)) + // scylla-health only matters when Cassandra owns Ports:scylla alone; when + // include-scylla=true, the per-node {name}-cql checks below take over. + .AddCheck(ScyllaHealthCheckName, HostPortTcpProbe.CreateCheck(scyllaPort)); + } + + // Per-node CQL readiness gates (always on). Each node's WithHealthCheck below flips + // WaitFor(previousNode) from "Running" to "Healthy", which serialises Raft joins — + // Scylla 2026.1's topology coordinator admits one joiner at a time. + // We poll `docker ps` per-probe (not via a BackgroundService watching + // ResourceNotificationService) because Aspire.Hosting.Testing's in-process host + // silently drops hosted services in multi-fixture sessions (multinode-stagger-debug.log). + if (includeScylla) + { + var hcBuilder = builder.Services.AddHealthChecks(); + foreach (var region in scyllaRegions) + { + var resourceName = ComposeServices.ToScyllaNodeName(region, multiNode: isMultiScyllaNode); + hcBuilder.AddAsyncCheck(ScyllaCqlCheckName(resourceName), async ct => + await DockerExecCqlProbe.RunAsync(resourceName, ct).ConfigureAwait(false)); + } + } + + // Bootstrapper sets include-dashboard=false so self-hosted production stacks + // don't pull the nightly aspire-dashboard image; dev `aspire run` keeps it. + // Bench mode also forces it off — the launcher spawns a short-lived AppHost that + // exits after emitting readiness, so pulling the dashboard image is pure overhead. + var includeDashboard = testBenchMode + ? false + : BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludeDashboard], fallback: true); + builder.AddDockerComposeEnvironment(ComposeEnvironmentName) + .WithDashboard(includeDashboard) + .ConfigureComposeFile(compose => + { + compose.AddNetwork(new Network { Name = ComposeNetworks.Scylla, Driver = "bridge" }); + compose.AddNetwork(new Network { Name = ComposeNetworks.Postgres, Driver = "bridge" }); + compose.AddNetwork(new Network { Name = ComposeNetworks.Api, Driver = "bridge" }); + + // Dashboard must reach the API for OTLP. + if (compose.Services.TryGetValue(DashboardComposeServiceName, out var dashboard)) + { + dashboard.Networks.Add(ComposeNetworks.Api); + } + }); + + var postgresUser = builder.AddParameter(ParamName(AppHostParameterKeys.PostgresUser)); + var postgresPassword = builder.AddParameter(ParamName(AppHostParameterKeys.PostgresPassword), secret: true); + var postgresDb = builder.AddParameter(ParamName(AppHostParameterKeys.PostgresDb), "interfold", publishValueAsDefault: true); + // Transient init credential. db_init is a disposable cluster-owner that + // DatabaseInitPhase uses once to mint the real roles, then scrambles. The .env value + // is intentionally stale by the time the API starts. Default via + // GenerateParameterDefault so dev `aspire run` and TUnit.Aspire don't need a pre-seed; + // publish mode overrides via PublishPhase.BuildEnvReplacements. + var postgresInitPassword = builder.AddParameter(ParamName(AppHostParameterKeys.PostgresInitPassword), + new GenerateParameterDefault { MinLength = 24 }, secret: true, persist: true); + var scyllaUser = builder.AddParameter(ParamName(AppHostParameterKeys.ScyllaUser)); + var scyllaPassword = builder.AddParameter(ParamName(AppHostParameterKeys.ScyllaPassword), secret: true); + // The _admin superuser is minted by DatabaseInitPhase into internal.secrets and + // is never surfaced here. + // GenerateParameterDefault covers the dev `aspire run` case (bootstrapper always injects + // a real PEM in publish mode, config wins over the default). The generated value is a + // random alphanumeric string — RecoveryCodeResolver.TryLoadEncryptionPrivateKey checks + // for "BEGIN" and short-circuits, so JWE-encrypted recovery codes silently 400 in dev. + // That's the accepted trade-off for the no-config F5 target. + var encryptionPrivateKey = builder.AddParameter(ParamName(AppHostParameterKeys.EncryptionPrivateKey), + new GenerateParameterDefault { MinLength = 32 }, secret: true, persist: true); + + // Public OAuth client IDs — empty default disables the corresponding provider (see + // OAuthChallengeServiceCollectionExtensions). Attached to the container path only so + // the dev project path keeps reading them from launchSettings.json / user-secrets. + var googleOAuthClientId = builder.AddParameter(ParamName(AppHostParameterKeys.GoogleOAuthClientId), "", publishValueAsDefault: true); + var discordOAuthClientId = builder.AddParameter(ParamName(AppHostParameterKeys.DiscordOAuthClientId), "", publishValueAsDefault: true); + var appleOAuthClientId = builder.AddParameter(ParamName(AppHostParameterKeys.AppleOAuthClientId), "", publishValueAsDefault: true); + + // API runtime parameters managed end-to-end by the bootstrapper (ConfigPhase prompts, + // ConfigureApiSelfHostEnv pipes to the container as OCTOCON_*). + var scyllaKeyspace = builder.AddParameter(ParamName(AppHostParameterKeys.ScyllaKeyspace), ScyllaKeyspace.Nam.ToWire(), publishValueAsDefault: true); + var oauthCallbackBaseUrl = builder.AddParameter(ParamName(AppHostParameterKeys.OAuthCallbackBaseUrl), "", publishValueAsDefault: true); + var jwtAuthority = builder.AddParameter(ParamName(AppHostParameterKeys.JwtAuthority), "", publishValueAsDefault: true); + var jwtAudience = builder.AddParameter(ParamName(AppHostParameterKeys.JwtAudience), "octocon", publishValueAsDefault: true); + var corsAllowedOrigins = builder.AddParameter(ParamName(AppHostParameterKeys.CorsAllowedOrigins), "", publishValueAsDefault: true); + + // Operator tuning — every default matches the API's compile-time fallback so a + // fresh bootstrap reproduces "env var unset" behaviour. Empty for the nullable + // knobs (avatars, OTLP, socket threshold) — ApplyStorage / ApplyObservability + // normalise empty → null. + var nodeGroup = builder.AddParameter(ParamName(AppHostParameterKeys.NodeGroup), "auxiliary", publishValueAsDefault: true); + var avatarPublicBase = builder.AddParameter(ParamName(AppHostParameterKeys.AvatarPublicBase), "", publishValueAsDefault: true); + var otlpEndpoint = builder.AddParameter(ParamName(AppHostParameterKeys.OtlpEndpoint), "", publishValueAsDefault: true); + var socketBatchBytesThreshold = builder.AddParameter(ParamName(AppHostParameterKeys.SocketBatchBytesThreshold), "", publishValueAsDefault: true); + var dbRetryAttempts = builder.AddParameter(ParamName(AppHostParameterKeys.DbRetryAttempts), "3", publishValueAsDefault: true); + var dbRetryInitialDelayMs = builder.AddParameter(ParamName(AppHostParameterKeys.DbRetryInitialDelayMs), "100", publishValueAsDefault: true); + var dbRetryMaxDelayMs = builder.AddParameter(ParamName(AppHostParameterKeys.DbRetryMaxDelayMs), "1500", publishValueAsDefault: true); + var hydrationMaxConcurrency = builder.AddParameter(ParamName(AppHostParameterKeys.HydrationMaxConcurrency), "8", publishValueAsDefault: true); + + // Reject well-known default passwords in dev mode. Each check is gated on the + // matching include-* toggle so opt-out fixtures don't need placeholder creds. Publish + // mode relies on the operator's shell guard. + if (!builder.ExecutionContext.IsPublishMode) + { + if (includeScylla) + { + var scyllaPasswordValue = builder.Configuration[AppHostParameterKeys.ScyllaPassword]; + if (string.IsNullOrEmpty(scyllaPasswordValue) || scyllaPasswordValue == "cassandra") + { + throw new InvalidOperationException( + "SCYLLA_PASSWORD must be set to a non-default value. " + + "The well-known default 'cassandra' is not allowed.\n" + + "Fix: dotnet user-secrets set \"Parameters:scylla-password\" \"\" " + + "--project hosts/Interfold.AppHost"); + } + + var scyllaUserValue = builder.Configuration[AppHostParameterKeys.ScyllaUser]; + if (string.IsNullOrEmpty(scyllaUserValue) || scyllaUserValue == "cassandra") + { + throw new InvalidOperationException( + "SCYLLA_USER must be set to a non-default value. " + + "The well-known default 'cassandra' is not allowed.\n" + + "Fix: dotnet user-secrets set \"Parameters:scylla-user\" \"\" " + + "--project hosts/Interfold.AppHost"); + } + } + + if (includePostgres) + { + var postgresPasswordValue = builder.Configuration[AppHostParameterKeys.PostgresPassword]; + if (string.IsNullOrEmpty(postgresPasswordValue) || postgresPasswordValue == "postgres") + { + throw new InvalidOperationException( + "POSTGRES_PASSWORD must be set to a non-default value. " + + "The well-known default 'postgres' is not allowed.\n" + + "Fix: dotnet user-secrets set \"Parameters:postgres-password\" \"\" " + + "--project hosts/Interfold.AppHost"); + } + + var postgresUserValue = builder.Configuration[AppHostParameterKeys.PostgresUser]; + if (string.IsNullOrEmpty(postgresUserValue) || postgresUserValue == "postgres") + { + throw new InvalidOperationException( + "POSTGRES_USER must be set to a non-default value. " + + "The well-known default 'postgres' is not allowed.\n" + + "Fix: dotnet user-secrets set \"Parameters:postgres-user\" \"\" " + + "--project hosts/Interfold.AppHost"); + } + } + } + + // Postgres (TimescaleDB). Skipped when include-postgres=false. First-boot pattern: + // POSTGRES_USER=db_init (disposable bootstrap superuser — can't demote OID 10 per + // postgres' commands/user.c::AlterRole), POSTGRES_PASSWORD is the transient init + // credential DatabaseInitPhase scrambles after seeding, no POSTGRES_DB (the app DB is + // created owned by the admin role, not db_init, for consistent DDL gating). + IResourceBuilder? msgDb = null; + if (includePostgres) + { + msgDb = builder.AddContainer(ComposeServices.Postgres, "timescale/timescaledb", "latest-pg18") + .WithContainerNetworkAlias(ComposeServices.Postgres) + .WithEnvironment(ContainerEnvNames.PostgresUser, PostgresRoles.Init) + .WithEnvironment(ContainerEnvNames.PostgresPassword, postgresInitPassword) + .WithEnvironment(ContainerEnvNames.PgData, ContainerMountPaths.PostgresPgData) + // Force scram-sha-256 for every host entry — the initdb default appends a + // `host all all 127.0.0.1/32 trust` line first and pg_hba.conf is first-match-wins, + // so without this loopback TCP connections silently bypass the db_init password + // scramble. `local all all trust` still applies to Unix sockets, which is how + // DatabaseInitPhase mints the app + admin roles pre-scramble. + .WithEnvironment(ContainerEnvNames.PostgresInitDbArgs, "--auth-host=scram-sha-256") + // pg_ctl's 60s default isn't enough on slow DinD disks — the timescaledb-tune + // init script triggers a fast-shutdown checkpoint before postgres re-execs in + // normal mode, and a slow checkpoint kills the container before WaitForPostgresAsync + // sees normal mode. 300s buys headroom. + .WithEnvironment(ContainerEnvNames.PgCtlTimeout, "300") + // Bypass timescaledb-tune's cgroup-v2 memory autodetection (missing + // /sys/fs/cgroup/memory.max on some Docker Desktop / restricted hosts panics + // the init script). Conservative defaults; operators can override via + // docker-compose.override.yaml. + .WithEnvironment(ContainerEnvNames.TsTuneMemory, "1GB") + .WithEnvironment(ContainerEnvNames.TsTuneNumCpus, "2") + .WithEndpoint(port: postgresPort, targetPort: 5432, name: PostgresEndpointName, scheme: TcpScheme) + .PublishAsDockerComposeService((_, service) => + { + service.Networks = [ComposeNetworks.Postgres]; + service.Healthcheck = ComposeHealthcheck.CmdShell( + $"pg_isready -U ${ContainerEnvNames.PostgresUser} -d postgres", + interval: "10s", timeout: "5s", retries: 10, startPeriod: "15s"); + }); + if (includeApi) + msgDb.WithHealthCheck(MsgDbHealthCheckName); + if (testBenchMode) + { + // Bench mode is deliberately volume-less: the container's own filesystem + // holds transient DB state, ContainerLifetime.Persistent keeps it alive across + // AppHost restarts (hot-attach path), and TestBenchIdleReaper's docker-rm-f + // cleans everything up in one call. Sharing the ComposeVolumes.PostgresData + // volume ("msg_pgdata") cross-contaminates with any local bootstrapper compose + // stack and preserves stale db_init passwords across bench reboots. + msgDb.WithLifetime(ContainerLifetime.Persistent); + // Proxyless — docker binds host:14200 → container:5432 directly, no Aspire + // reverse proxy. The launcher AppHost exits right after TestBenchReadyEmitter + // signals ready; a proxied endpoint would die with it and leave the container + // reachable only on a random docker-assigned ephemeral port. Aspire 13.4+ makes + // persistent-resource endpoints proxyless by default, but we set it explicitly + // for compatibility with older SDKs on the workstation. + msgDb.WithEndpoint(PostgresEndpointName, e => e.IsProxied = false); + // Legacy per-project fixtures shard tests across their own Postgres, so the + // image default max_connections=100 was safe. Bench mode collapses every leaf + // integration project onto ONE Postgres: SecretsPreBuildLoader pools up to 10 + // conns per test-host process, the app pool adds 5 more, and a solution-wide + // `dotnet test` spawns many hosts concurrently. That trips PostgresErrorCode + // 53300 ("too many clients already") during factory build. 500 buys headroom + // with tiny shared-memory overhead (~50MB extra) and doesn't require touching + // shared_buffers. Passed via `postgres -c`; docker-entrypoint.sh forwards CMD + // args to the postgres binary verbatim. + msgDb.WithArgs("-c", "max_connections=500"); + } + else if (persistentContainers) + { + msgDb.AsPersistent(ComposeVolumes.PostgresData, ContainerMountPaths.PostgresData); + } + // Pin the docker container name in bench mode so subsequent AppHost launcher + // processes reuse the same container instead of colliding on host port 14200. + if (testBenchMode) + msgDb.WithContainerName(TestBenchContainerNames.Postgres); + } + + // API waits on each included CQL backend before starting. + var cqlEndpointOwners = new List>(); + + if (includeScylla) + { + IResourceBuilder? previousNode = null; + + foreach (var region in scyllaRegions) + { + var regionWire = region.ToWire(); + var name = ComposeServices.ToScyllaNodeName(region, multiNode: isMultiScyllaNode); + var seeds = previousNode is null + ? $"--seeds={name}" + : $"--seeds={ComposeServices.ToScyllaNodeName(scyllaRegions[0], multiNode: true)},{ComposeServices.ToScyllaNodeName(scyllaRegions[1], multiNode: true)}"; + + var nodeArgs = new List + { + seeds, "--smp", "1", "--memory", "750M", "--overprovisioned", "1", + "--developer-mode", "1", "--authenticator", "PasswordAuthenticator", + "--authorizer", "CassandraAuthorizer", + "--endpoint-snitch", "GossipingPropertyFileSnitch", + "--api-address", "0.0.0.0", "--broadcast-address", name, + "--cluster-name", clusterName, + }; + if (isMultiScylla) + { + // Ring-delay 60s (vs 30s default) — extra settle time for the Raft + // topology coordinator between multi-DC joiners. + nodeArgs.Add("--ring-delay-ms"); + nodeArgs.Add("60000"); + } + + var node = builder.AddContainer(name, "scylladb/scylla", "2026.1") + .WithContainerNetworkAlias(name) + .WithArgs([.. nodeArgs]) + .WithBindMount($"../../db/scylla/cassandra-rackdc.{regionWire}.properties", "/etc/scylla/cassandra-rackdc.properties", isReadOnly: true) + .WithEnvironment(ContainerEnvNames.CqlshUser, scyllaUser) + .WithEnvironment(ContainerEnvNames.CqlshPassword, scyllaPassword) + .PublishAsDockerComposeService((_, service) => + { + service.Networks = [ComposeNetworks.Scylla]; + // Probe the actual CQL endpoint (not `nodetool status`) so + // depends_on:service_healthy really means "CQL reachable as the app user" + // — on slow DinD hosts the CQL listener can lag NORMAL mode by 60–90s. + // `$$VAR` is deliberate: docker-compose interpolates ${VAR} at parse time + // against the host shell, `$$VAR` escapes to a literal that the + // container's /bin/sh expands against the container env. + service.Healthcheck = ComposeHealthcheck.CmdShell( + $"cqlsh -u \"$${ContainerEnvNames.CqlshUser}\" -p \"$${ContainerEnvNames.CqlshPassword}\" -e 'DESCRIBE CLUSTER' >/dev/null 2>&1", + interval: "15s", timeout: "10s", retries: 20, startPeriod: "30s"); + }); + + if (testBenchMode && !isMultiScyllaNode) + { + // Volume-less by design; see the Postgres branch above for rationale. + node.WithLifetime(ContainerLifetime.Persistent); + } + else if (persistentContainers) + { + node.AsPersistent( + isMultiScyllaNode ? ComposeVolumes.ScyllaRegionData(regionWire) : ComposeVolumes.ScyllaData, + ContainerMountPaths.ScyllaData); + } + // Bench mode is single-node only (guarded above by testBenchMode forcing + // includeApi/Web/Dashboard off; scylla-topology stays single). Pin the + // container name so cross-process reuse works on the fixed 19042 port. + if (testBenchMode && !isMultiScyllaNode) + { + node.WithContainerName(TestBenchContainerNames.Scylla); + // Proxyless — see the Postgres branch above. + node.WithEndpoint(CqlEndpointName, e => e.IsProxied = false); + } + + // HealthCheckAnnotation flips WaitFor(previousNode) from "Running" to + // "Healthy" — serialises multi-DC joins so Raft doesn't ban concurrent joiners. + node.WithHealthCheck(ScyllaCqlCheckName(name)); + + if (previousNode is null) + { + node.WithEndpoint(port: scyllaPort, targetPort: 9042, name: CqlEndpointName, scheme: TcpScheme); + cqlEndpointOwners.Add(node); + } + else + { + node.WaitFor(previousNode); + } + + previousNode = node; + } + } + + if (includeCassandra) + { + // Built from db/cassandra/Dockerfile (FROM cassandra:5). The image bakes the + // required cassandra.yaml overrides (materialized_views_enabled + Password + // Authenticator/Authorizer) so we skip the runtime wrapper + chown that used to + // break on GHA. Endpoint port: standalone Cassandra owns Ports:scylla (legacy), + // co-hosted with Scylla it takes Ports:cassandra so the CQL listeners don't collide. + var cassandraEndpointPort = includeScylla ? cassandraPort : scyllaPort; + + var cassandra = builder.AddDockerfile(ComposeServices.Cassandra, "../../db/cassandra") + .WithContainerNetworkAlias(ComposeServices.Cassandra) + .WithEnvironment(ContainerEnvNames.CassandraClusterName, clusterName) + .WithEnvironment(ContainerEnvNames.CassandraListenAddress, ComposeServices.Cassandra) + .WithEnvironment(ContainerEnvNames.CassandraBroadcastAddress, ComposeServices.Cassandra) + .WithEnvironment(ContainerEnvNames.CassandraBroadcastRpcAddress, ComposeServices.Cassandra) + .WithEnvironment(ContainerEnvNames.CassandraRpcAddress, "0.0.0.0") + .WithEnvironment(ContainerEnvNames.CassandraEndpointSnitch, "GossipingPropertyFileSnitch") + .WithEnvironment(ContainerEnvNames.CassandraNumTokens, "16") + .WithEnvironment(ContainerEnvNames.CassandraDc, ScyllaKeyspace.Nam.ToWire()) + .WithEnvironment(ContainerEnvNames.CassandraRack, "rack1") + .WithEnvironment(ContainerEnvNames.MaxHeapSize, "512M") + .WithEnvironment(ContainerEnvNames.HeapNewSize, "256M") + .WithEnvironment(ContainerEnvNames.CqlshUser, scyllaUser) + .WithEnvironment(ContainerEnvNames.CqlshPassword, scyllaPassword) + .WithEndpoint(port: cassandraEndpointPort, targetPort: 9042, name: CqlEndpointName, scheme: TcpScheme) + .PublishAsDockerComposeService((_, service) => + { + service.Networks = [ComposeNetworks.Scylla]; + service.Healthcheck = ComposeHealthcheck.CmdShell( + $"cqlsh -u \"$${ContainerEnvNames.CqlshUser}\" -p \"$${ContainerEnvNames.CqlshPassword}\" -e 'describe cluster' || nodetool status | grep -q '^UN'", + interval: "15s", timeout: "10s", retries: 20, startPeriod: "30s"); + }); + + if (includeApi && !includeScylla) + { + // Attach scylla-health only when Cassandra owns Ports:scylla. + cassandra.WithHealthCheck(ScyllaHealthCheckName); + } + if (testBenchMode) + { + // Volume-less by design; see the Postgres branch above for rationale. + cassandra.WithLifetime(ContainerLifetime.Persistent); + } + else if (persistentContainers) + { + cassandra.AsPersistent(ComposeVolumes.CassandraData, ContainerMountPaths.CassandraData); + } + if (testBenchMode) + { + cassandra.WithContainerName(TestBenchContainerNames.Cassandra); + // Proxyless — see the Postgres branch above. + cassandra.WithEndpoint(CqlEndpointName, e => e.IsProxied = false); + } + + cqlEndpointOwners.Add(cassandra); + } + + // Seed ownership by mode: + // - Publish: DatabaseInitPhase in the bootstrapper (docker compose exec). + // - Tests (include-api=false): SharedDbFixture drives DbInitHelper directly. + // - RunMode dev (include-api=true): DevSeedHostedService, wired below. + // First cqlEndpointOwners entry is scylla node 0 in the default flow, cassandra when + // the user picked the cassandra launch profile; mixed scylla+cassandra is test-only + // so we only ever seed one backend. + IResourceBuilder? devSeedResource = null; + if (!builder.ExecutionContext.IsPublishMode && includeApi && includePostgres) + { + var firstCqlBackend = cqlEndpointOwners.Count > 0 ? cqlEndpointOwners[0] : null; + // Aspire persists GenerateParameterDefault outputs to user-secrets, but only + // after Build() completes — on first run IConfiguration doesn't yet see them. + // Threading the ParameterResource lets the hosted service read the effective + // value via GetValueAsync in both first-run and subsequent-run cases. + devSeedResource = builder.AddDevSeedPipeline( + msgDb!, firstCqlBackend, + postgresInitPassword, postgresPassword, scyllaPassword); + } + + // Interfold API: pre-built image for self-hosting (Parameters:api-image), csproj build + // for dev (`aspire run`). + if (includeApi) + { + // Guaranteed non-null by the includeApi && !includePostgres guard above. + var msgDbResource = msgDb!; + var pgEndpoint = msgDbResource.GetEndpoint(PostgresEndpointName); + + void ConfigureApiCommon(IResourceBuilder api) + { + api.WithEnvironment(OctoconEnvKeys.Persistence, PersistenceMode.ScyllaPostgres.ToWire()) + .WithEnvironment(OctoconEnvKeys.SingleScyllaInstance, BoolWire.ToWireValue(!isMultiScylla)) + .WithEnvironment(OctoconEnvKeys.PostgresConnection, + ReferenceExpression.Create($"Host={pgEndpoint.Property(EndpointProperty.Host)};Port={pgEndpoint.Property(EndpointProperty.Port)};Database={postgresDb};Username={postgresUser};Password={postgresPassword}")) + .WithEnvironment(ContainerEnvNames.EncryptionPrivateKey, encryptionPrivateKey); + } + + // Dev-project path only. ConfigureApiSelfHostEnv covers the container path via the + // operator-provided Parameters:jwt-authority etc.; the dev path has no operator so + // we stamp localhost-derived defaults driven by the AppHost's own port allocation. + // Empty CORS falls back to "any origin" in the API — noisy in browser devtools but + // functional; we still narrow it here so socket + fetch calls behave the same as prod. + void ConfigureApiDevEnv(IResourceBuilder api) + { + var apiHttpsUrl = $"https://localhost:{apiHttpsPort}"; + var webOrigins = + $"https://localhost:{webHttpsPort},http://localhost:{webHttpPort}"; + api.WithEnvironment(OctoconEnvKeys.JwtAuthority, apiHttpsUrl) + .WithEnvironment(OctoconEnvKeys.JwtAudience, "octocon") + .WithEnvironment(OctoconEnvKeys.AuthCallbackBaseUrl, apiHttpsUrl) + .WithEnvironment(OctoconEnvKeys.CorsAllowedOrigins, webOrigins) + .WithEnvironment(OctoconEnvKeys.ScyllaKeyspace, ScyllaKeyspace.Nam.ToWire()); + } + + // Self-hosting only. Bind-mounts /certs (root CA + leaf PFX from CertificatePhase) + // read-only; everything else (JWT keys, pepper, OAuth secrets, leaf PFX password) + // lives in internal.secrets and is loaded by SecretsBootstrapService / Program.cs. + // Captures apiContainerHttpPort/apiContainerHttpsPort so it can't be static. + void ConfigureApiSelfHostEnv(IResourceBuilder api) + { + api.WithBindMount(CertsPaths.HostDir, CertsPaths.ContainerDir, isReadOnly: true) + // Host path filled by PublishPhase from BootstrapConfig.storage.avatarStorageRoot. + .WithBindMount(AvatarsPaths.HostDir, AvatarsPaths.ContainerDir, isReadOnly: false) + // Override the SDK's HTTP 8080 default so ASPNETCORE_*_PORTS matches + // targetPort — HTTPS terminates inside the container using the leaf PFX. + .WithEnvironment(ContainerEnvNames.AspNetCoreHttpPorts, apiContainerHttpPort.ToString()) + .WithEnvironment(ContainerEnvNames.AspNetCoreHttpsPorts, apiContainerHttpsPort.ToString()) + // Kestrel default-endpoint cert (docs: aspnetcore/fundamentals/servers/kestrel/endpoints#configure-https). + // Password moved to internal.secrets:certs:leaf_pfx_password (loaded by Program.cs). + .WithEnvironment(ContainerEnvNames.AspNetCoreKestrelDefaultCertPath, CertsPaths.LeafPfx) + // TrustController serves /.well-known/interfold-root-ca.{crt,pem,sha256} + // from these paths (inside the /certs bind mount). Blank → 404. The sha256 + // file also drives the HTTP ETag so --rotate-certs invalidates caches. + .WithEnvironment(OctoconEnvKeys.TrustRootCaPath, CertsPaths.RootCaCrt) + .WithEnvironment(OctoconEnvKeys.TrustRootCaFingerprintPath, CertsPaths.RootCaFingerprint) + // Container path only — dev project path still reads launchSettings/user-secrets. + // Empty per-provider ID disables that provider (see OAuthChallenge extensions). + .WithEnvironment(OctoconEnvKeys.GoogleOAuthClientId, googleOAuthClientId) + .WithEnvironment(OctoconEnvKeys.DiscordOAuthClientId, discordOAuthClientId) + .WithEnvironment(OctoconEnvKeys.AppleOAuthClientId, appleOAuthClientId) + // API runtime config from BootstrapConfig (ConfigPhase). See + // docs/configuration.md gotcha #1 — OCTOCON_SCYLLA_KEYSPACE default 'nam' + // is wrong for any non-NAM regional stack. Empty CORS falls back to + // "any origin" in Program.cs (production foot-gun; bootstrapper derives + // a default from Hosts). + .WithEnvironment(OctoconEnvKeys.ScyllaKeyspace, scyllaKeyspace) + .WithEnvironment(OctoconEnvKeys.AuthCallbackBaseUrl, oauthCallbackBaseUrl) + .WithEnvironment(OctoconEnvKeys.JwtAuthority, jwtAuthority) + .WithEnvironment(OctoconEnvKeys.JwtAudience, jwtAudience) + .WithEnvironment(OctoconEnvKeys.CorsAllowedOrigins, corsAllowedOrigins) + // Operator tuning — every default matches the API's compile-time fallback. + // Empty for the nullable knobs (avatars, OTLP, socket threshold); + // ApplyStorage / ApplyObservability normalise empty → null. + .WithEnvironment(OctoconEnvKeys.NodeGroup, nodeGroup) + .WithEnvironment(OctoconEnvKeys.AvatarStorageRoot, ContainerMountPaths.InterfoldAvatars) + .WithEnvironment(OctoconEnvKeys.AvatarPublicBase, avatarPublicBase) + .WithEnvironment(OctoconEnvKeys.OtlpEndpoint, otlpEndpoint) + .WithEnvironment(OctoconEnvKeys.SocketBatchBytesThreshold, socketBatchBytesThreshold) + .WithEnvironment(OctoconEnvKeys.DbRetryAttempts, dbRetryAttempts) + .WithEnvironment(OctoconEnvKeys.DbRetryInitialDelayMs, dbRetryInitialDelayMs) + .WithEnvironment(OctoconEnvKeys.DbRetryMaxDelayMs, dbRetryMaxDelayMs) + .WithEnvironment(OctoconEnvKeys.HydrationMaxConcurrency, hydrationMaxConcurrency); + } + + var apiImageRef = builder.Configuration[AppHostParameterKeys.ApiImage]; + if (!string.IsNullOrEmpty(apiImageRef)) + { + var apiImage = ImageRef.Parse(apiImageRef); + var apiContainer = builder.AddContainer(ComposeServices.InterfoldApi, apiImage.Image, apiImage.Tag) + .WithHttpEndpoint(port: apiHttpPort, targetPort: apiContainerHttpPort, name: HttpEndpointName) + .WithHttpsEndpoint(port: apiHttpsPort, targetPort: apiContainerHttpsPort, name: HttpsEndpointName) + .WithHttpHealthCheck(HealthEndpoints.Ready, endpointName: HttpEndpointName) + .WithExternalHttpEndpoints() + .WaitFor(msgDbResource) + .PublishAsDockerComposeService(ApiComposeServicePublisher(apiContainerHttpPort)); + ConfigureApiCommon(apiContainer); + ConfigureApiSelfHostEnv(apiContainer); + foreach (var owner in cqlEndpointOwners) + apiContainer.WaitFor(owner); + // Symmetry with the AddProject branch — bootstrapper's publish never sees + // devSeedResource so this is a no-op in publish mode. + if (devSeedResource is not null) + apiContainer.WaitFor(devSeedResource); + } + else + { + var apiProject = builder.AddProject(ComposeServices.InterfoldApi) + .WithHttpEndpoint(port: apiHttpPort, targetPort: apiContainerHttpPort, name: HttpEndpointName) + .WithHttpsEndpoint(port: apiHttpsPort, targetPort: apiContainerHttpsPort, name: HttpsEndpointName) + .WithHttpHealthCheck(HealthEndpoints.Ready, endpointName: HttpEndpointName) + .WithExternalHttpEndpoints() + .WaitFor(msgDbResource) + .PublishAsDockerComposeService(ApiComposeServicePublisher(apiContainerHttpPort)); + ConfigureApiCommon(apiProject); + ConfigureApiDevEnv(apiProject); + foreach (var owner in cqlEndpointOwners) + apiProject.WaitFor(owner); + if (devSeedResource is not null) + apiProject.WaitFor(devSeedResource); + } + } + + var includeWeb = testBenchMode + ? false + : BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.IncludeWeb], fallback: true); + // Opt-in HTTPS termination via nginx's envsubst-on-templates entrypoint — bootstrapper + // bind-mounts the leaf cert/key + a generated template. Dev leaves this off. + var webTls = BoolWire.ParseToggle(builder.Configuration[AppHostParameterKeys.WebTls], fallback: false); + if (includeWeb) + { + var web = builder.AddContainer(ComposeServices.OctoconWeb, "ghcr.io/azyyyyyy/octocon-wasm", "latest") + .WithContainerNetworkAlias(ComposeServices.OctoconWeb); + + if (webTls) + { + // PublishPhase feeds server_name in via config (picked from Deployment.Hosts). + // `_` is the nginx catch-all for dev callers who flip the toggle without it. + var serverName = builder.Configuration[AppHostParameterKeys.WebServerName]; + if (string.IsNullOrWhiteSpace(serverName)) serverName = "_"; + + web = web + .WithHttpEndpoint(port: webHttpPort, targetPort: 80, name: HttpEndpointName) + .WithHttpsEndpoint(port: webHttpsPort, targetPort: 443, name: HttpsEndpointName) + .WithBindMount(CertsPaths.HostDir, CertsPaths.ContainerDir, isReadOnly: true) + .WithBindMount( + "../../web/nginx/default.conf.template", + "/etc/nginx/templates/default.conf.template", + isReadOnly: true) + .WithEnvironment(ContainerEnvNames.NginxServerName, serverName) + .WithEnvironment(ContainerEnvNames.NginxSslCertFile, CertsPaths.LeafCrt) + .WithEnvironment(ContainerEnvNames.NginxSslKeyFile, CertsPaths.LeafKey) + // Precomputed `:` (or empty for 443) so the :80 → :443 redirect + // lands on the bound host port (webHttpsPort, default 8081) instead of + // an unbound 443 the browser would infer. + .WithEnvironment( + ContainerEnvNames.NginxHttpsPortSuffix, + webHttpsPort == 443 ? string.Empty : $":{webHttpsPort}") + // Restrict envsubst to NGINX_* so upstream $host/$uri stay untouched. + .WithEnvironment(ContainerEnvNames.NginxEnvsubstFilter, "^NGINX_") + .WithHttpHealthCheck("/", endpointName: HttpEndpointName) + // Must be AFTER the endpoint registrations — Aspire's external-endpoint + // pass walks the current endpoint set, so calling this earlier produces + // `expose:` with no `ports:` in the emitted compose. + .WithExternalHttpEndpoints() + .PublishAsDockerComposeService((_, service) => + { + service.Networks = [ComposeNetworks.Api]; + // -k: leaf is signed by the private root CA the container doesn't trust. + service.Healthcheck = ComposeHealthcheck.Cmd( + interval: "15s", timeout: "5s", retries: 5, startPeriod: "10s", + command: ["curl", "-kf", "https://localhost:443/"]); + }); + } + else + { + // Upstream octocon-wasm image only listens on :8080 (Dockerfile.wasm bakes it + // into /etc/nginx/conf.d/default.conf). Publishing web-https here would shadow + // web-http with a second port serving plaintext, so Ports:web-https is unused + // in this branch. + web = web + .WithHttpEndpoint(port: webHttpPort, targetPort: 8080, name: HttpEndpointName) + .WithHttpHealthCheck("/", endpointName: HttpEndpointName) + .WithExternalHttpEndpoints() + .PublishAsDockerComposeService((_, service) => + { + service.Networks = [ComposeNetworks.Api]; + service.Healthcheck = ComposeHealthcheck.Cmd( + interval: "15s", timeout: "5s", retries: 5, startPeriod: "10s", + command: ["curl", "-f", "http://localhost:8080/"]); + }); + } + _ = web; + } + + // Bench mode: register a hosted service that waits for every DB resource to reach + // Running, prints a machine-readable readiness line, then requests app shutdown so + // the launcher's Process.WaitForExitAsync returns deterministically. Containers are + // pinned to Persistent lifetime + stable names above so they survive this exit. + if (testBenchMode) + { + builder.Services.AddSingleton(new TestBenchReadyEmitterOptions( + PostgresPort: postgresPort, + ScyllaPort: includeScylla ? scyllaPort : null, + CassandraPort: includeCassandra ? (includeScylla ? cassandraPort : scyllaPort) : null, + ScyllaResourceName: includeScylla + ? ComposeServices.ToScyllaNodeName(ScyllaKeyspace.Nam, multiNode: false) + : null, + CassandraResourceName: includeCassandra ? ComposeServices.Cassandra : null, + PostgresResourceName: ComposeServices.Postgres)); + builder.Services.AddHostedService(); + } + } + + private static void PromoteDependenciesToHealthy(Service service, params string[] dependencyNames) + { + foreach (var dep in dependencyNames) + { + if (service.DependsOn.TryGetValue(dep, out var composeDep)) + composeDep.Condition = ComposeDependencyCondition.ServiceHealthy; + } + } + + private static Action ApiComposeServicePublisher(int apiContainerHttpPort) + { + return (_, service) => + { + service.Networks = [ComposeNetworks.Scylla, ComposeNetworks.Postgres, ComposeNetworks.Api]; + service.Healthcheck = ComposeHealthcheck.Cmd( + interval: "15s", timeout: "5s", retries: 10, startPeriod: "20s", + command: ["curl", "-f", $"http://localhost:{apiContainerHttpPort}{HealthEndpoints.Ready}"]); + // Gate on msg-db + scylla being CQL-ready (not just Running). Without this the + // bootstrapper's `up` command races the API into scylla before gossip-bootstrap + // finishes and ScyllaMigrationService.StartingAsync crashes with + // "connection refused on 9042". + PromoteDependenciesToHealthy(service, ComposeServices.Postgres, ComposeServices.ScyllaSingle, ComposeServices.Cassandra); + }; + } +} diff --git a/hosts/Interfold.AppHost/Properties/launchSettings.json b/hosts/Interfold.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..24db756a --- /dev/null +++ b/hosts/Interfold.AppHost/Properties/launchSettings.json @@ -0,0 +1,52 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "single-scylla": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17182;http://localhost:15277", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21246", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22223", + "Parameters__include-scylla": "true", + "Parameters__include-cassandra": "false", + "Parameters__scylla-topology": "single", + "Parameters__include-web": "true" + } + }, + "multi-scylla": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17182;http://localhost:15277", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21246", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22223", + "Parameters__include-scylla": "true", + "Parameters__include-cassandra": "false", + "Parameters__scylla-topology": "multi", + "Parameters__include-web": "true" + } + }, + "cassandra": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17182;http://localhost:15277", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21246", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22223", + "Parameters__include-scylla": "false", + "Parameters__include-cassandra": "true", + "Parameters__include-web": "true" + } + } + } +} diff --git a/hosts/Interfold.AppHost/TestBenchContainerNames.cs b/hosts/Interfold.AppHost/TestBenchContainerNames.cs new file mode 100644 index 00000000..bcb82794 --- /dev/null +++ b/hosts/Interfold.AppHost/TestBenchContainerNames.cs @@ -0,0 +1,12 @@ +namespace Interfold.AppHost; + +/// Fixed container names for test-bench mode (WithContainerName in +/// and docker rm -f in the idle reaper). +public static class TestBenchContainerNames +{ + public const string Postgres = "interfold-test-bench-pg"; + public const string Scylla = "interfold-test-bench-scylla"; + public const string Cassandra = "interfold-test-bench-cassandra"; + + public static readonly IReadOnlyList All = [Postgres, Scylla, Cassandra]; +} diff --git a/hosts/Interfold.AppHost/TestBenchReadyEmitter.cs b/hosts/Interfold.AppHost/TestBenchReadyEmitter.cs new file mode 100644 index 00000000..dca95f1c --- /dev/null +++ b/hosts/Interfold.AppHost/TestBenchReadyEmitter.cs @@ -0,0 +1,103 @@ +using Aspire.Hosting.ApplicationModel; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Interfold.AppHost; + +/// Configuration for . Ports are the host-mapped +/// values allocated; resource names are the same +/// Aspire resource ids passed to builder.AddContainer so +/// +/// matches. A null resource name means the backend is not included this run. +internal sealed record TestBenchReadyEmitterOptions( + int PostgresPort, + int? ScyllaPort, + int? CassandraPort, + string PostgresResourceName, + string? ScyllaResourceName, + string? CassandraResourceName); + +/// Bench-mode-only hosted service that waits for every included DB resource to +/// reach , writes a machine-readable readiness +/// line to stdout, then requests app shutdown. TestBenchCoordinator's launcher reads the +/// line and knows the persistent-lifetime containers are up; the AppHost process is +/// allowed to exit because the containers survive it. +internal sealed class TestBenchReadyEmitter : BackgroundService +{ + // Sentinel string TestBenchCoordinator matches against on stdout. Keep in sync with + // TestBenchCoordinator.ReadyLinePrefix. + private const string ReadyLinePrefix = "[test-bench] ready"; + + private readonly ResourceNotificationService _notifications; + private readonly IHostApplicationLifetime _lifetime; + private readonly TestBenchReadyEmitterOptions _options; + private readonly ILogger _logger; + + public TestBenchReadyEmitter( + ResourceNotificationService notifications, + IHostApplicationLifetime lifetime, + TestBenchReadyEmitterOptions options, + ILogger logger) + { + _notifications = notifications; + _lifetime = lifetime; + _options = options; + _logger = logger; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + try + { + await WaitRunningOrThrowAsync(_options.PostgresResourceName, stoppingToken).ConfigureAwait(false); + + if (_options.ScyllaResourceName is not null) + await WaitRunningOrThrowAsync(_options.ScyllaResourceName, stoppingToken).ConfigureAwait(false); + + if (_options.CassandraResourceName is not null) + await WaitRunningOrThrowAsync(_options.CassandraResourceName, stoppingToken).ConfigureAwait(false); + + var parts = new List { $"pg={_options.PostgresPort}" }; + if (_options.ScyllaPort is int sp) parts.Add($"scylla={sp}"); + if (_options.CassandraPort is int cp) parts.Add($"cassandra={cp}"); + Console.WriteLine($"{ReadyLinePrefix} {string.Join(' ', parts)}"); + await Console.Out.FlushAsync(stoppingToken).ConfigureAwait(false); + + // Persistent-lifetime containers survive; shutting down releases the launcher. + _lifetime.StopApplication(); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + } + catch (Exception ex) + { + _logger.LogError(ex, "TestBenchReadyEmitter failed to observe DB resources; launcher will exit."); + // Surface the failure on stderr so TestBenchCoordinator's exit-before-ready + // path includes the FailedToStart cause instead of an empty 10-minute timeout. + Console.Error.WriteLine($"[test-bench] ready-emitter failed: {ex.Message}"); + await Console.Error.FlushAsync(CancellationToken.None).ConfigureAwait(false); + _lifetime.StopApplication(); + throw; + } + } + + /// Races Running against FailedToStart so a wedged Scylla (typical on + /// Docker Desktop with <8 GiB RAM) fails the launcher in seconds instead of hanging + /// until LauncherTimeout. + private async Task WaitRunningOrThrowAsync(string resourceName, CancellationToken ct) + { + var running = _notifications.WaitForResourceAsync(resourceName, KnownResourceStates.Running, ct); + var failed = _notifications.WaitForResourceAsync(resourceName, KnownResourceStates.FailedToStart, ct); + var completed = await Task.WhenAny(running, failed).ConfigureAwait(false); + if (completed == failed) + { + await failed.ConfigureAwait(false); + var detail = await AspireResourceFailureDiagnostics + .DescribeAsync(resourceName, AppHostRepoPaths.ResolveRepoRoot(), ct) + .ConfigureAwait(false); + throw new InvalidOperationException(detail); + } + + await running.ConfigureAwait(false); + } +} diff --git a/hosts/Interfold.AppHost/appsettings.Development.json b/hosts/Interfold.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/hosts/Interfold.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/hosts/Interfold.AppHost/appsettings.json b/hosts/Interfold.AppHost/appsettings.json new file mode 100644 index 00000000..a4626b78 --- /dev/null +++ b/hosts/Interfold.AppHost/appsettings.json @@ -0,0 +1,31 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "Parameters": { + "include-scylla": true, + "include-postgres": true, + "include-cassandra": false, + "scylla-topology": "single", + "include-web": true, + "postgres-user": "postgres_customuser", + "scylla-user": "cassandra_customuser", + "scylla-password": "cassandra_custompassword", + "postgres-password": "postgres_custompassword" + }, + "Ports": { + "postgres": 4200, + "scylla": 9042, + "cassandra": 9043, + "api-http": 5000, + "api-https": 5001, + "api-container-http": 5100, + "api-container-https": 5101, + "web-http": 8080, + "web-https": 8081 + } +} diff --git a/hosts/Interfold.AppHost/aspire.config.json b/hosts/Interfold.AppHost/aspire.config.json new file mode 100644 index 00000000..21455ad4 --- /dev/null +++ b/hosts/Interfold.AppHost/aspire.config.json @@ -0,0 +1,5 @@ +{ + "appHost": { + "path": "Interfold.AppHost.csproj" + } +} diff --git a/hosts/Interfold.ServiceDefaults/Extensions.cs b/hosts/Interfold.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..2d1da511 --- /dev/null +++ b/hosts/Interfold.ServiceDefaults/Extensions.cs @@ -0,0 +1,152 @@ +using Interfold.Shared.Contracts.Configuration; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Http.Resilience; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; +using Polly; + +namespace Interfold.ServiceDefaults; + +public static class Extensions +{ + private const string OtlpEndpointEnvName = "OTEL_EXPORTER_OTLP_ENDPOINT"; + + // Applied by ConfigureResilienceForGetOnly. Sampling duration satisfies the + // framework constraint SamplingDuration >= 2 * AttemptTimeout. + private static readonly TimeSpan ResilienceAttemptTimeout = TimeSpan.FromSeconds(30); + private static readonly TimeSpan ResilienceTotalRequestTimeout = TimeSpan.FromMinutes(2); + private static readonly TimeSpan CircuitBreakerSamplingDuration = TimeSpan.FromMinutes(1); + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + http.AddStandardResilienceHandler().Configure(ConfigureResilienceForGetOnly); + http.AddServiceDiscovery(); + }); + + return builder; + } + + /// Retries and the circuit breaker only fire for idempotent GET. Non-GET + /// verbs are unsafe to replay after a per-attempt timeout (the origin can't tell the + /// retry from a fresh call — see the Pi 4 SP-import triple-run in + /// scripts/diagnostics/raspi_dump.txt). AttemptTimeout / TotalRequestTimeout + /// still apply universally (no ShouldHandle hook), so keep them long enough for + /// legitimate long-running mutations but bounded. + public static void ConfigureResilienceForGetOnly(HttpStandardResilienceOptions options) + { + var defaultRetryShould = options.Retry.ShouldHandle; + options.Retry.ShouldHandle = args => + { + var request = args.Context.GetRequestMessage(); + if (request is null || request.Method != HttpMethod.Get) + { + return new ValueTask(false); + } + return defaultRetryShould(args); + }; + + var defaultBreakerShould = options.CircuitBreaker.ShouldHandle; + options.CircuitBreaker.ShouldHandle = args => + { + var request = args.Context.GetRequestMessage(); + if (request is null || request.Method != HttpMethod.Get) + { + return new ValueTask(false); + } + return defaultBreakerShould(args); + }; + + options.AttemptTimeout.Timeout = ResilienceAttemptTimeout; + options.TotalRequestTimeout.Timeout = ResilienceTotalRequestTimeout; + options.CircuitBreaker.SamplingDuration = CircuitBreakerSamplingDuration; + } + + /// OTel logging, metrics, and tracing. Kept public to match the Aspire + /// ServiceDefaults template — operators wrap their own composition around this call. + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpoints.PathPrefix) + ) + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration[OtlpEndpointEnvName]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + return builder; + } + + /// Built-in self liveness check tagged . + /// Kept public for the Aspire template convention. + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + .AddCheck(HealthCheckNames.Self, () => HealthCheckResult.Healthy(), [HealthCheckTags.Live]); + + return builder; + } + + public static IEndpointRouteBuilder MapDefaultEndpoints(this IEndpointRouteBuilder endpoints) + { + endpoints.MapHealthChecks(HealthEndpoints.Live, new HealthCheckOptions + { + Predicate = _ => false + }).AllowAnonymous().ShortCircuit(); + + endpoints.MapHealthChecks(HealthEndpoints.Ready, new HealthCheckOptions + { + Predicate = check => check.Tags.Contains(HealthCheckTags.Ready) + }).AllowAnonymous().ShortCircuit(); + + endpoints.MapHealthChecks(HealthEndpoints.Startup, new HealthCheckOptions + { + Predicate = check => check.Tags.Contains(HealthCheckTags.Startup) + }).AllowAnonymous().ShortCircuit(); + + return endpoints; + } +} diff --git a/hosts/Interfold.ServiceDefaults/Interfold.ServiceDefaults.csproj b/hosts/Interfold.ServiceDefaults/Interfold.ServiceDefaults.csproj new file mode 100644 index 00000000..fe5b1013 --- /dev/null +++ b/hosts/Interfold.ServiceDefaults/Interfold.ServiceDefaults.csproj @@ -0,0 +1,26 @@ + + + + net10.0 + enable + enable + true + + + + + + + + + + + + + + + + + + + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/InMemoryIdempotencyStore.cs b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryIdempotencyStore.cs new file mode 100644 index 00000000..da5f0dc0 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryIdempotencyStore.cs @@ -0,0 +1,41 @@ +using System.Collections.Concurrent; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory; + +public sealed class InMemoryIdempotencyStore : IIdempotencyStore +{ + private readonly ConcurrentDictionary _store = new(); + + public Task FindAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default + ) + { + var key = BuildKey(principalId, operationId, idempotencyKey); + _store.TryGetValue(key, out var value); + return Task.FromResult(value); + } + + public Task SaveAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + string payloadHash, + string outcomeHash, + string? outcomePayload, + CancellationToken cancellationToken = default + ) + { + var key = BuildKey(principalId, operationId, idempotencyKey); + _store[key] = new IdempotencyMatch(payloadHash, outcomeHash, outcomePayload); + return Task.CompletedTask; + } + + private static string BuildKey(SystemId principalId, OperationId operationId, IdempotencyKey idempotencyKey) => + $"{principalId.Value}:{operationId.Value}:{idempotencyKey.Value}"; +} \ No newline at end of file diff --git a/infrastructure/Interfold.Infrastructure.InMemory/InMemoryRegionContext.cs b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryRegionContext.cs new file mode 100644 index 00000000..6e147d78 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryRegionContext.cs @@ -0,0 +1,31 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory; + +public sealed class InMemoryRegionContext : IRegionContext +{ + private static readonly ScyllaKeyspace[] Regions = Enum.GetValues(); + + public ScyllaKeyspace CurrentRegion { get; } + + public InMemoryRegionContext(ScyllaKeyspace currentRegion = ScyllaKeyspace.Nam) + { + CurrentRegion = currentRegion; + } + + public ScyllaKeyspace ResolveUserRegion(SystemId systemId) + { + // default(SystemId) has Value == null. Mirror ScyllaKeyspaceResolver's fallback. + if (string.IsNullOrWhiteSpace(systemId)) + { + return CurrentRegion; + } + + // Strip the region prefix so raw and scoped shapes hash to the same region. + var normalized = ScopedSystemId.StripRegionPrefix(systemId); + var index = Math.Abs(normalized.GetHashCode(StringComparison.Ordinal)) % Regions.Length; + return Regions[index]; + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/InMemorySecretsStore.cs b/infrastructure/Interfold.Infrastructure.InMemory/InMemorySecretsStore.cs new file mode 100644 index 00000000..69932341 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/InMemorySecretsStore.cs @@ -0,0 +1,41 @@ +using System.Collections.Concurrent; +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.Infrastructure.InMemory; + +public sealed class InMemorySecretsStore : ISecretsStore +{ + private readonly ConcurrentDictionary _secrets = new(); + + public Task GetAsync(SecretsStoreKey key, CancellationToken cancellationToken = default) + { + _secrets.TryGetValue(key.Value, out var entry); + return Task.FromResult(entry?.Value); + } + + public Task GetRequiredAsync(SecretsStoreKey key, CancellationToken cancellationToken = default) + { + if (!_secrets.TryGetValue(key.Value, out var entry)) + throw new InvalidOperationException($"Required secret '{key.Value}' not found in secrets store."); + return Task.FromResult(entry.Value); + } + + public Task> ListAsync(CancellationToken cancellationToken = default) + { + IReadOnlyList result = _secrets.Values + .OrderBy(e => e.Key.Value) + .ToList(); + return Task.FromResult(result); + } + + /// + /// Internal helper for test seeding. Not part of the ISecretsStore contract. + /// + public void Seed(SecretsStoreKey key, string value) + { + var now = DateTimeOffset.UtcNow; + _secrets.AddOrUpdate(key.Value, + _ => new SecretEntry(key, value, "in-memory", now, now, null, null), + (_, existing) => existing with { Value = value, UpdatedAt = now }); + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/InMemoryServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryServiceCollectionExtensions.cs new file mode 100644 index 00000000..bfa723ad --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryServiceCollectionExtensions.cs @@ -0,0 +1,65 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.InMemory; + +public static class InMemoryServiceCollectionExtensions +{ + private static readonly Action Registration = PersistenceRegistration.Create(PersistenceMode.InMemory, AddInMemoryPersistence); + public static void Register() => Registration(); + + private static IServiceCollection AddInMemoryPersistence( + IServiceCollection services, + PersistenceConfiguration options) + { + return services + .AddSingleton(_ => new InMemoryRegionContext( + options.ScyllaKeyspace)) + // Seeded from OCTOCON_INMEMORY_SECRETS_SEED__* so external runners can bootstrap + // the published image without an in-process hook. Blanks are skipped silently — + // SecretsBootstrapService is the single fail-fast for mandatory rows. + .AddSingleton(sp => + { + var seed = sp.GetRequiredService>().Value; + var store = new InMemorySecretsStore(); + Seed(store, SecretsStoreKeys.EncryptionPepper, seed.EncryptionPepper); + Seed(store, SecretsStoreKeys.AuthJwtEs256PrivatePem, seed.AuthJwtEs256PrivatePem); + Seed(store, SecretsStoreKeys.AuthDeepLinkSecret, seed.AuthDeepLinkSecret); + Seed(store, SecretsStoreKeys.AuthJwtRsa256PrivatePem, seed.AuthJwtRsa256PrivatePem); + return store; + }) + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + } + + private static void Seed(InMemorySecretsStore store, SecretsStoreKey secretKey, string? value) + { + if (!string.IsNullOrWhiteSpace(value)) + store.Seed(secretKey, value); + } +} \ No newline at end of file diff --git a/infrastructure/Interfold.Infrastructure.InMemory/InMemoryStorageKeys.cs b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryStorageKeys.cs new file mode 100644 index 00000000..cfeb5afe --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/InMemoryStorageKeys.cs @@ -0,0 +1,50 @@ +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.InMemory; + +// Shared key/normalization helpers for the InMemory repositories. Normalisation strips +// a legacy "region:" prefix (mirrors ScyllaKeyspaceResolver.NormalizeSystemId); +// system partition keys are "{region}:{systemId}". +internal static class InMemoryStorageKeys +{ + // Returns a wrapper so callers can key dictionaries directly instead of on string. + public static SystemId Normalize(SystemId systemId) + => new(ScopedSystemId.StripRegionPrefix(systemId.Value)); + + // Compose is idempotent — an already-scoped SystemId won't become "nam:nam:abcdefg". + public static ScopedSystemId ForSystem(IRegionContext regionContext, SystemId systemId) + { + var region = regionContext.ResolveUserRegion(systemId); + return ScopedSystemId.Compose(region, systemId); + } + + // Public read → null; self → TrustedFriend; else defer to the (optional) friendship + // repo. Null friendships is the "InMemory instance without a friend graph" branch. + public static async Task ResolveFriendshipLevelAsync( + SystemId systemId, + SystemId? viewerSystemId, + IFriendshipRepository? friendships, + CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(viewerSystemId?.Value)) + { + return null; + } + + if (ScopedSystemId.StripRegionPrefix(systemId) == + ScopedSystemId.StripRegionPrefix(viewerSystemId.Value)) + { + return FriendshipLevel.TrustedFriend; + } + + if (friendships is null) + { + return null; + } + + return await friendships.GetFriendshipLevelAsync(systemId, viewerSystemId, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Interfold.Infrastructure.InMemory.csproj b/infrastructure/Interfold.Infrastructure.InMemory/Interfold.Infrastructure.InMemory.csproj new file mode 100644 index 00000000..7c08b41f --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Interfold.Infrastructure.InMemory.csproj @@ -0,0 +1,27 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAccountRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAccountRepository.cs new file mode 100644 index 00000000..7875a0eb --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAccountRepository.cs @@ -0,0 +1,385 @@ +using System.Collections.Concurrent; +using System.Security.Cryptography; +using System.Text; +using Interfold.Auth.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Contracts.Ids; +using Interfold.Systems.Contracts.Models.Read; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryAccountRepository : IAccountRepository +{ + // Matches the Scylla port's 5-minute link-token expiry. + private static readonly TimeSpan LinkTokenTtl = TimeSpan.FromMinutes(5); + + private readonly record struct LinkTokenEntry(ScopedSystemId Scoped, DateTimeOffset ExpiresAt); + + // Identity-side reverse dicts key on raw string to preserve + // StringComparer.OrdinalIgnoreCase (needed for email); the wrappers are ordinal-strict. + private readonly ConcurrentDictionary _usernameBySystem = new(); + private readonly ConcurrentDictionary _descriptionBySystem = new(); + private readonly ConcurrentDictionary _avatarBySystem = new(); + private readonly ConcurrentDictionary _avatarSourceBySystem = new(); + private readonly ConcurrentDictionary _linkTokenBySystem = new(); + private readonly ConcurrentDictionary _systemByLinkToken = new(); + private readonly ConcurrentDictionary _discordBySystem = new(); + private readonly ConcurrentDictionary _emailBySystem = new(); + private readonly ConcurrentDictionary _appleBySystem = new(); + private readonly ConcurrentDictionary _systemByDiscord = new(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _systemByEmail = new(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _systemByApple = new(StringComparer.OrdinalIgnoreCase); + + private readonly IEncryptionStateRepository? _encryptionStates; + private readonly IRegionContext _regionContext; + private readonly TimeProvider _timeProvider; + + public InMemoryAccountRepository( + IRegionContext regionContext, + IEncryptionStateRepository? encryptionStates = null, + TimeProvider? timeProvider = null) + { + _regionContext = regionContext; + _encryptionStates = encryptionStates; + _timeProvider = timeProvider ?? TimeProvider.System; + } + + public Task UpdateUsernameAsync(SystemId systemId, Username username, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + _usernameBySystem[systemKey] = username; + return Task.FromResult(true); + } + + public Task UpdateDescriptionAsync(SystemId systemId, string description, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + _descriptionBySystem[systemKey] = description; + return Task.FromResult(true); + } + + public Task UpdateAvatarAsync(SystemId systemId, AvatarUrl avatarUrl, AvatarSource source, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + _avatarBySystem[systemKey] = avatarUrl; + _avatarSourceBySystem[systemKey] = source; + return Task.FromResult(true); + } + + public Task ClearAvatarAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + _avatarBySystem.TryRemove(systemKey, out _); + _avatarSourceBySystem.TryRemove(systemKey, out _); + return Task.FromResult(true); + } + + public Task GetOrCreateLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var scoped = ResolveScoped(systemId); + var now = _timeProvider.GetUtcNow(); + + // Deterministic derivation: same system → same token (integration-test contract). + // Wrapping inside GetOrAdd keeps the derived hash off the stack as a bare string. + var token = _linkTokenBySystem.GetOrAdd(systemKey, static key => + { + var hash = SHA256.HashData(Encoding.UTF8.GetBytes(key.Value)); + return new(Convert.ToHexString(hash)[..32].ToLowerInvariant()); + }); + + _systemByLinkToken[token] = new LinkTokenEntry(scoped, now.Add(LinkTokenTtl)); + + return Task.FromResult(token); + } + + public Task GetLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!_linkTokenBySystem.TryGetValue(systemKey, out var token)) + { + return Task.FromResult(null); + } + + // Honour TTL so "get" never returns a token Resolve would then refuse. + if (_systemByLinkToken.TryGetValue(token, out var entry) && entry.ExpiresAt > _timeProvider.GetUtcNow()) + { + return Task.FromResult(token); + } + + ScrubLinkToken(systemKey, token); + return Task.FromResult(null); + } + + public Task ResolveSystemIdByLinkTokenAsync(LinkToken linkToken, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(linkToken.Value)) + { + return Task.FromResult(null); + } + + // On miss/expired, scrub both sides — deterministic derivation would re-adopt + // a dangling pointer. + if (_systemByLinkToken.TryGetValue(linkToken, out var entry) && entry.ExpiresAt > _timeProvider.GetUtcNow()) + { + return Task.FromResult(entry.Scoped.AsSystemId()); + } + + ScrubLinkToken(linkTokenValue: linkToken); + return Task.FromResult(null); + } + + public Task ClearLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (_linkTokenBySystem.TryRemove(systemKey, out var token)) + { + _systemByLinkToken.TryRemove(token, out _); + } + + return Task.FromResult(true); + } + + public Task TryFindSystemIdByDiscordIdAsync(DiscordId discordId, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(discordId.Value)) + { + return Task.FromResult(null); + } + + return Task.FromResult( + _systemByDiscord.TryGetValue(discordId.Value, out var scopedSystemId) + ? scopedSystemId.AsSystemId() + : null); + } + + // ProviderIdentity dispatch → shared FindOrCreate/Link/Unlink helpers below. + public Task FindOrCreateSystemIdAsync(ProviderIdentity identity, CancellationToken cancellationToken = default) + => identity.MatchOrThrow( + discordId => FindOrCreateIdentifier(discordId, _discordBySystem, _systemByDiscord, static id => id.Value), + email => FindOrCreateIdentifier(email, _emailBySystem, _systemByEmail, static e => e.Value), + appleId => FindOrCreateIdentifier(appleId, _appleBySystem, _systemByApple, static id => id.Value)); + + public Task LinkIdentityToUserAsync(SystemId systemId, ProviderIdentity identity, CancellationToken cancellationToken = default) + => identity.MatchOrThrow( + discordId => Task.FromResult(LinkIdentifier(systemId, discordId, _discordBySystem, _systemByDiscord, static id => id.Value)), + email => Task.FromResult(LinkIdentifier(systemId, email, _emailBySystem, _systemByEmail, static e => e.Value)), + appleId => Task.FromResult(LinkIdentifier(systemId, appleId, _appleBySystem, _systemByApple, static id => id.Value))); + + public Task UnlinkDiscordAsync(SystemId systemId, CancellationToken cancellationToken = default) + => Task.FromResult(UnlinkIdentifier(systemId, _discordBySystem, _systemByDiscord, static id => id.Value)); + + public Task UnlinkEmailAsync(SystemId systemId, CancellationToken cancellationToken = default) + => Task.FromResult(UnlinkIdentifier(systemId, _emailBySystem, _systemByEmail, static e => e.Value)); + + public Task UnlinkAppleAsync(SystemId systemId, CancellationToken cancellationToken = default) + => Task.FromResult(UnlinkIdentifier(systemId, _appleBySystem, _systemByApple, static id => id.Value)); + + public Task DeleteAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + _usernameBySystem.TryRemove(systemKey, out _); + _descriptionBySystem.TryRemove(systemKey, out _); + _avatarBySystem.TryRemove(systemKey, out _); + _avatarSourceBySystem.TryRemove(systemKey, out _); + if (_linkTokenBySystem.TryRemove(systemKey, out var token)) + { + _systemByLinkToken.TryRemove(token, out _); + } + + UnlinkIdentifier(systemId, _discordBySystem, _systemByDiscord, static id => id.Value); + UnlinkIdentifier(systemId, _emailBySystem, _systemByEmail, static e => e.Value); + UnlinkIdentifier(systemId, _appleBySystem, _systemByApple, static id => id.Value); + + return Task.FromResult(true); + } + + public Task GetPublicProfileAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + Username? username = _usernameBySystem.TryGetValue(systemKey, out var u) ? u : null; + var description = _descriptionBySystem.TryGetValue(systemKey, out var d) ? d : null; + AvatarUrl? avatarUrl = _avatarBySystem.TryGetValue(systemKey, out var a) ? a : null; + AvatarSource? avatarSource = _avatarSourceBySystem.TryGetValue(systemKey, out var s) ? s : null; + DiscordId? discordId = _discordBySystem.TryGetValue(systemKey, out var discord) ? discord : null; + Email? email = _emailBySystem.TryGetValue(systemKey, out var e) ? e : null; + AppleId? appleId = _appleBySystem.TryGetValue(systemKey, out var apple) ? apple : null; + + if (username is null && description is null && avatarUrl is null && discordId is null && email is null && appleId is null) + { + return Task.FromResult(null); + } + + return Task.FromResult( + new AccountPublicProfileReadModel( + systemId, + username, + description, + avatarUrl, + avatarSource, + discordId, + email, + appleId)); + } + + public Task GetPublicSystemAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + Username? username = _usernameBySystem.TryGetValue(systemKey, out var u) ? u : null; + var description = _descriptionBySystem.TryGetValue(systemKey, out var d) ? d : null; + AvatarUrl? avatarUrl = _avatarBySystem.TryGetValue(systemKey, out var a) ? a : null; + AvatarSource? avatarSource = _avatarSourceBySystem.TryGetValue(systemKey, out var s) ? s : null; + + // Presence check must agree with GetPublicProfileAsync — otherwise + // PublicSystemsController.Show 404s rows SystemMustExistAttribute lets through. + var hasIdentity = username is not null + || description is not null + || avatarUrl is not null + || _discordBySystem.ContainsKey(systemKey) + || _emailBySystem.ContainsKey(systemKey) + || _appleBySystem.ContainsKey(systemKey); + + if (!hasIdentity) + { + return Task.FromResult(null); + } + + return Task.FromResult( + new PublicSystemReadModel( + Id: systemId, + AvatarUrl: avatarUrl, + AvatarSource: avatarSource, + Username: username, + Description: description)); + } + + private ScopedSystemId ResolveScoped(SystemId systemId) + => ScopedSystemId.Compose(_regionContext.ResolveUserRegion(systemId), systemId); + + // Drop a link-token from both maps. Lazy scrub guards against dangling reverse-map + // pointers that deterministic-hash tokens would otherwise re-adopt. + private void ScrubLinkToken(ScopedSystemId? systemKey = null, LinkToken? linkTokenValue = null) + { + if (linkTokenValue is { } token) + { + _systemByLinkToken.TryRemove(token, out _); + foreach (var kvp in _linkTokenBySystem) + { + if (kvp.Value == token) + { + _linkTokenBySystem.TryRemove(kvp.Key, out _); + break; + } + } + } + + if (systemKey is { } key && _linkTokenBySystem.TryRemove(key, out var storedToken)) + { + _systemByLinkToken.TryRemove(storedToken, out _); + } + } + + private Task FindOrCreateIdentifier( + TIdentity identifier, + ConcurrentDictionary identifierBySystem, + ConcurrentDictionary systemByIdentifier, + Func extractRawValue) + where TIdentity : struct + { + var rawValue = extractRawValue(identifier); + if (string.IsNullOrWhiteSpace(rawValue)) + { + return Task.FromResult(null); + } + + if (systemByIdentifier.TryGetValue(rawValue, out var scopedSystemId)) + { + return Task.FromResult(scopedSystemId.AsSystemId()); + } + + var newSystemId = Guid.NewGuid().ToString("N"); + var scopedNew = ScopedSystemId.Compose(_regionContext.ResolveUserRegion(new(newSystemId)), newSystemId); + identifierBySystem[scopedNew] = identifier; + systemByIdentifier[rawValue] = scopedNew; + + EnsureEncryptionSaltForSystem(scopedNew); + return Task.FromResult(scopedNew.AsSystemId()); + } + + // Idempotent success matches the Scylla adapter — "nothing to unlink" is not an error. + private bool UnlinkIdentifier( + SystemId systemId, + ConcurrentDictionary identifierBySystem, + ConcurrentDictionary systemByIdentifier, + Func extractRawValue) + where TIdentity : struct + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (identifierBySystem.TryRemove(systemKey, out var identifier)) + { + var rawValue = extractRawValue(identifier); + if (!string.IsNullOrWhiteSpace(rawValue)) + { + systemByIdentifier.TryRemove(rawValue, out _); + } + } + + return true; + } + + private AccountLinkResult LinkIdentifier( + SystemId systemId, + TIdentity identifier, + ConcurrentDictionary identifierBySystem, + ConcurrentDictionary systemByIdentifier, + Func extractRawValue) + where TIdentity : struct + { + var rawValue = extractRawValue(identifier); + if (string.IsNullOrWhiteSpace(rawValue)) + { + return AccountLinkResult.UserNotFound; + } + + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var scopedSystemId = ResolveScoped(systemId); + + if (_usernameBySystem.ContainsKey(systemKey) is false && + _descriptionBySystem.ContainsKey(systemKey) is false && + _avatarBySystem.ContainsKey(systemKey) is false && + _linkTokenBySystem.ContainsKey(systemKey) is false) + { + return AccountLinkResult.UserNotFound; + } + + if (identifierBySystem.TryGetValue(systemKey, out var existing) + && !string.IsNullOrWhiteSpace(extractRawValue(existing))) + { + return AccountLinkResult.AlreadyLinked; + } + + if (systemByIdentifier.TryGetValue(rawValue, out var owner) && owner != scopedSystemId) + { + return AccountLinkResult.UserExists; + } + + identifierBySystem[systemKey] = identifier; + systemByIdentifier[rawValue] = scopedSystemId; + return AccountLinkResult.Success; + } + + // Scoped param pins the "caller resolved to scoped composite" invariant at the type level. + private void EnsureEncryptionSaltForSystem(ScopedSystemId scoped) + { + if (_encryptionStates is null) + return; + + // EncryptionSalt.NewRandom() keeps the base64 salt off the stack as a bare string. + _ = _encryptionStates.UpsertAsync(scoped.AsSystemId(), false, null, EncryptionSalt.NewRandom(), CancellationToken.None); + } +} + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAlterRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAlterRepository.cs new file mode 100644 index 00000000..3061ef36 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAlterRepository.cs @@ -0,0 +1,391 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryAlterRepository : IAlterRepository +{ + private sealed class AlterState + { + public required AlterId AlterId { get; init; } + public string? Alias { get; set; } + public AvatarUrl? AvatarUrl { get; set; } + public AvatarSource? AvatarSource { get; set; } + public string? Description { get; set; } + public HexColor? Color { get; set; } + public string? Pronouns { get; set; } + public string? ProxyName { get; set; } + public string Name { get; set; } = string.Empty; + // Matches Scylla insert semantics: alters are Private until owner opens visibility. + public VisibilityLevel VisibilityLevel { get; set; } = VisibilityLevel.Private; + public Dictionary Fields { get; } = new(); + public bool Untracked { get; set; } + public bool Archived { get; set; } + public bool Pinned { get; set; } + public List DiscordProxies { get; } = new(); + public DateTime InsertedAt { get; set; } + public DateTime UpdatedAt { get; set; } + } + + private readonly IRegionContext _regionContext; + private readonly ConcurrentDictionary> _bySystem = new(); + // checked((short)…) on increment traps overflow instead of handing back an already-used id. + private readonly ConcurrentDictionary _nextIdBySystem = new(); + private readonly IFriendshipRepository _friendships; + private readonly ISettingsFieldRepository _settingsFields; + private readonly IAlterFieldDefinitions _alterFieldDefinitions; + private readonly IPollRepository _polls; + private readonly ILogger _logger; + + public InMemoryAlterRepository( + IRegionContext regionContext, + IFriendshipRepository friendships, + ISettingsFieldRepository settingsFields, + IAlterFieldDefinitions alterFieldDefinitions, + IPollRepository polls, + ILogger logger) + { + _regionContext = regionContext; + _friendships = friendships; + _settingsFields = settingsFields; + _alterFieldDefinitions = alterFieldDefinitions; + _polls = polls; + _logger = logger; + } + + public Task CreateAsync( + SystemId systemId, + CreateAlterCommand command, + CancellationToken cancellationToken = default + ) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _bySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + AlterId next = new(_nextIdBySystem.AddOrUpdate(systemKey, (short)1, (_, current) => checked((short)(current + 1)))); + + var now = command.CreatedAt.UtcDateTime; + var created = store.TryAdd(next, new AlterState + { + AlterId = next, + Name = command.Name, + InsertedAt = now, + UpdatedAt = now, + }); + + return Task.FromResult(created ? next : null); + } + + public Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + var exists = TryGetStore(systemId, out var store) && store.ContainsKey(alterId); + return Task.FromResult(exists); + } + + public Task UpdateAsync( + SystemId systemId, + UpdateAlterCommand command, + CancellationToken cancellationToken = default + ) + { + if (!TryGetAlter(systemId, command.AlterId, out var existing)) + { + return Task.FromResult(false); + } + + if (!string.IsNullOrWhiteSpace(command.Alias)) + { + existing.Alias = command.Alias; + } + + if (!string.IsNullOrWhiteSpace(command.Name)) + { + existing.Name = command.Name; + } + + if (command.Description is not null) + { + existing.Description = command.Description; + } + + if (command.Color is not null) + { + existing.Color = command.Color; + } + + if (command.Pronouns is not null) + { + existing.Pronouns = command.Pronouns; + } + + if (command.ProxyName is not null) + { + existing.ProxyName = command.ProxyName; + } + + if (command.SecurityLevel is not null) + { + existing.VisibilityLevel = command.SecurityLevel.Value; + } + + if (command.ClearAvatar) + { + existing.AvatarUrl = null; + existing.AvatarSource = null; + } + else if (command.AvatarUrl is not null) + { + // Domain handler rejects half-set; treat AvatarSource as required here. + existing.AvatarUrl = command.AvatarUrl; + existing.AvatarSource = command.AvatarSource ?? AvatarSource.Local; + } + + if (command.Fields is not null) + { + foreach (var field in command.Fields) + { + existing.Fields[field.Id] = field.Value; + } + } + + if (command.Untracked is not null) + { + existing.Untracked = command.Untracked.Value; + } + + if (command.Archived is not null) + { + existing.Archived = command.Archived.Value; + } + + if (command.Pinned is not null) + { + existing.Pinned = command.Pinned.Value; + } + + existing.UpdatedAt = command.UpdatedAt.UtcDateTime; + return Task.FromResult(true); + } + + public async Task DeleteAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + { + return false; + } + + var removed = store.TryRemove(alterId, out _); + if (removed) + { + await _polls.RemoveAlterFromPollsAsync(systemId, alterId, cancellationToken); + } + + return removed; + } + + public async Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + { + return Array.Empty(); + } + + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + + var rows = store.Values + .OrderBy(x => x.AlterId.Value) + .Select(x => MapAlterReadModel(x, definitions)) + .ToArray(); + + return rows; + } + + public async Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var friendshipLevel = await InMemoryStorageKeys.ResolveFriendshipLevelAsync(systemId, viewerSystemId, _friendships, cancellationToken); + var definitions = await _alterFieldDefinitions.ListVisibleAsync(systemId, friendshipLevel, cancellationToken); + + var ownerId = InMemoryStorageKeys.Normalize(systemId).Value; + if (!TryGetStore(systemId, out var store)) + { + GuardedInstrumentation.RecordList(_logger, "alter", nameof(ListGuardedAsync), viewerSystemId, ownerId, totalCount: 0, visibleCount: 0, sw.Elapsed.TotalMilliseconds); + return Array.Empty(); + } + + var totalCount = store.Values.Count; + var rows = store.Values + .Where(x => x.VisibilityLevel.CanBeViewedBy(friendshipLevel)) + .OrderBy(x => x.AlterId.Value) + .Select(x => new BareAlter( + x.AlterId, + x.Name, + x.AvatarUrl, + x.AvatarSource, + x.Color, + x.Pronouns, + x.Description, + AlterFieldProjection.ResolveGuardedFields(x.Fields, definitions, _logger))) + .ToArray(); + + GuardedInstrumentation.RecordList(_logger, "alter", nameof(ListGuardedAsync), viewerSystemId, ownerId, totalCount, rows.Length, sw.Elapsed.TotalMilliseconds); + return rows; + } + + public async Task GetAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + if (!TryGetAlter(systemId, alterId, out var alter)) + { + return null; + } + + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + + return MapAlterReadModel(alter, definitions); + } + + public async Task GetGuardedAsync( + SystemId systemId, + AlterId alterId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var ownerId = InMemoryStorageKeys.Normalize(systemId).Value; + var friendshipLevel = await InMemoryStorageKeys.ResolveFriendshipLevelAsync(systemId, viewerSystemId, _friendships, cancellationToken); + var definitions = await _alterFieldDefinitions.ListVisibleAsync(systemId, friendshipLevel, cancellationToken); + if (!TryGetAlter(systemId, alterId, out var alter)) + { + GuardedInstrumentation.RecordGet(_logger, "alter", nameof(GetGuardedAsync), viewerSystemId, ownerId, alterId.Value.ToString(), found: false, filtered: false, sw.Elapsed.TotalMilliseconds); + return null; + } + + if (!alter.VisibilityLevel.CanBeViewedBy(friendshipLevel)) + { + GuardedInstrumentation.RecordGet(_logger, "alter", nameof(GetGuardedAsync), viewerSystemId, ownerId, alterId.Value.ToString(), found: false, filtered: true, sw.Elapsed.TotalMilliseconds); + return null; + } + + var result = new BareAlter( + alter.AlterId, + alter.Name, + alter.AvatarUrl, + alter.AvatarSource, + alter.Color, + alter.Pronouns, + alter.Description, + AlterFieldProjection.ResolveGuardedFields(alter.Fields, definitions, _logger)); + GuardedInstrumentation.RecordGet(_logger, "alter", nameof(GetGuardedAsync), viewerSystemId, ownerId, alterId.Value.ToString(), found: true, filtered: false, sw.Elapsed.TotalMilliseconds); + return result; + } + + public Task AliasTakenByOtherAsync( + SystemId systemId, + AlterId alterId, + string alias, + CancellationToken cancellationToken = default + ) + { + if (!TryGetStore(systemId, out var store)) + { + return Task.FromResult(false); + } + + var taken = store.Values.Any(a => + a.AlterId != alterId && + !string.IsNullOrWhiteSpace(a.Alias) && + string.Equals(a.Alias, alias, StringComparison.OrdinalIgnoreCase) + ); + + return Task.FromResult(taken); + } + + internal void RemoveFieldValuesForSystem(Guid fieldId, ScopedSystemId systemKey) + { + if (!TryGetStore(systemKey, out var store)) + return; + + FieldId fieldKey = new(fieldId); + foreach (var kv in store) + { + var state = kv.Value; + lock (state.Fields) + { + if (state.Fields.ContainsKey(fieldKey)) + { + state.Fields.Remove(fieldKey); + } + } + } + } + + internal void RemoveFieldValuesForSystem(SystemId systemId, Guid fieldId) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + RemoveFieldValuesForSystem(fieldId, systemKey); + } + + private static AlterReadModel MapAlterReadModel( + AlterState alter, + IReadOnlyList definitions) + { + return new AlterReadModel( + alter.AlterId, + alter.Name, + alter.Description, + alter.AvatarUrl, + alter.AvatarSource, + alter.Color, + alter.Pronouns, + alter.VisibilityLevel, + AlterFieldProjection.ResolveOwnerFields(alter.Fields, definitions), + alter.ProxyName, + alter.Alias, + alter.Untracked, + alter.Archived, + alter.Pinned, + alter.DiscordProxies.ToArray(), + alter.InsertedAt, + alter.UpdatedAt); + } + + private bool TryGetStore(SystemId systemId, out ConcurrentDictionary store) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return TryGetStore(systemKey, out store); + } + + private bool TryGetStore(ScopedSystemId systemKey, out ConcurrentDictionary store) + => _bySystem.TryGetValue(systemKey, out store!); + + private bool TryGetAlter(SystemId systemId, AlterId alterId, out AlterState alter) + { + if (TryGetStore(systemId, out var store) && store.TryGetValue(alterId, out alter!)) + { + return true; + } + + alter = null!; + return false; + } +} + + + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAuthTokenRevocationRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAuthTokenRevocationRepository.cs new file mode 100644 index 00000000..8fbc801e --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryAuthTokenRevocationRepository.cs @@ -0,0 +1,78 @@ +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory.Repository; + +/// In-memory JWT revocation store. Static-keyed so tokens survive +/// WebApplicationFactory host recreation during parallel test runs. +public sealed class InMemoryAuthTokenRevocationRepository : IAuthTokenRevocationRepository +{ + private static readonly Lock s_lock = new(); + private static readonly Dictionary s_tokens = new(); + + private sealed record TokenRecord( + Jti Jti, + SystemId SystemId, + DateTimeOffset IssuedAt, + DateTimeOffset ExpiresAt, + DateTimeOffset? RevokedAt = null + ); + + public Task RecordTokenAsync( + Jti jti, + SystemId systemId, + DateTimeOffset expiresAt, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + ArgumentException.ThrowIfNullOrWhiteSpace(systemId, nameof(systemId)); + + lock (s_lock) + { + s_tokens[jti] = new TokenRecord( + Jti: jti, + SystemId: InMemoryStorageKeys.Normalize(systemId), + IssuedAt: DateTimeOffset.UtcNow, + ExpiresAt: expiresAt, + RevokedAt: null + ); + } + + return Task.CompletedTask; + } + + public Task ValidateTokenNotRevokedAsync( + Jti jti, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + + lock (s_lock) + { + if (!s_tokens.TryGetValue(jti, out var record)) + { + return Task.FromResult(false); + } + + var isValid = record.RevokedAt is null && record.ExpiresAt > DateTimeOffset.UtcNow; + return Task.FromResult(isValid); + } + } + + public Task RevokeTokenAsync( + Jti jti, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + + lock (s_lock) + { + if (s_tokens.TryGetValue(jti, out var record) && record.RevokedAt is null) + { + s_tokens[jti] = record with { RevokedAt = DateTimeOffset.UtcNow }; + } + } + + return Task.CompletedTask; + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryEncryptionStateRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryEncryptionStateRepository.cs new file mode 100644 index 00000000..c0e5f2dc --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryEncryptionStateRepository.cs @@ -0,0 +1,31 @@ +using System.Collections.Concurrent; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryEncryptionStateRepository : IEncryptionStateRepository +{ + private readonly ConcurrentDictionary _states = new(); + + public Task GetAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + _states.TryGetValue(normalizedSystemId, out var state); + return Task.FromResult(state); + } + + public Task UpsertAsync(SystemId systemId, bool initialized, KeyChecksum? keyChecksum, EncryptionSalt? salt, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + + _states.TryGetValue(normalizedSystemId, out EncryptionState? value); + _states[normalizedSystemId] = new EncryptionState(Initialized: initialized, KeyChecksum: keyChecksum, + Salt: salt ?? value?.Salt); + + return Task.FromResult(true); + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFriendshipRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFriendshipRepository.cs new file mode 100644 index 00000000..dc798541 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFriendshipRepository.cs @@ -0,0 +1,338 @@ +using System.Collections.Concurrent; +using Interfold.Friendships.Contracts.Ids; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryFriendshipRepository : IFriendshipRepository +{ + private sealed class FriendshipState + { + public required SystemId FriendSystemId { get; init; } + public required FriendshipLevel Level { get; set; } + public required DateTimeOffset Since { get; init; } + } + + private sealed class RequestState + { + public required SystemId OtherSystemId { get; init; } + public required DateTimeOffset DateSent { get; init; } + } + + private readonly ConcurrentDictionary> _friendships = new(); + private readonly ConcurrentDictionary> _outgoingRequests = new(); + + // accounts parameter kept for DI-signature stability; unused after the Discord + // dispatch lane was removed. + public InMemoryFriendshipRepository(IAccountRepository? accounts = null) + { + _ = accounts; + } + + public Task ResolveUserIdAsync(FriendLookup lookup, CancellationToken cancellationToken = default) + { + // Mirrors ScyllaFriendshipRepository. InMemory has no username reverse-index, so + // Username collapses to null (surfaces as the 422 friend_request:no_user). Adding + // a FriendLookupKind requires an explicit branch here — silent-null would hide it. + SystemId? result = lookup.Kind switch + { + FriendLookupKind.Id => InMemoryStorageKeys.Normalize(new SystemId(lookup.Value)), + FriendLookupKind.Username => null, + _ => throw new ArgumentOutOfRangeException(nameof(lookup), lookup.Kind, + $"Unhandled FriendLookupKind '{lookup.Kind}' in ResolveUserIdAsync."), + }; + return Task.FromResult(result); + } + + public Task GetFriendshipLevelAsync(SystemId systemId, SystemId? viewerSystemId, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(viewerSystemId?.Value)) + { + return Task.FromResult(null); + } + + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedViewerId = InMemoryStorageKeys.Normalize(viewerSystemId.Value); + + if (normalizedSystemId == normalizedViewerId || !TryGetFriendshipState(normalizedSystemId, normalizedViewerId, out var state)) + { + return Task.FromResult(null); + } + + return Task.FromResult(state.Level); + } + + public Task> ListFriendshipsAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + if (!TryGetFriendStore(normalizedSystemId, out var store)) + { + return Task.FromResult>(Array.Empty()); + } + + var list = store.Values + .OrderByDescending(x => x.Since) + .Select(x => new FriendshipReadModel( + new FriendProfileReadModel(x.FriendSystemId, null, null, null, null, null), + new FriendshipModel( + x.Level, + x.Since), + Array.Empty())) + .ToList(); + + return Task.FromResult>(list); + } + + public Task GetFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedFriendId = InMemoryStorageKeys.Normalize(friendSystemId); + + if (!TryGetFriendshipState(normalizedSystemId, normalizedFriendId, out var state)) + { + return Task.FromResult(null); + } + + return Task.FromResult(new FriendshipReadModel( + new FriendProfileReadModel(state.FriendSystemId, null, null, null, null, null), + new FriendshipModel( + state.Level, + state.Since), + Array.Empty())); + } + + public Task RemoveFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedFriendId = InMemoryStorageKeys.Normalize(friendSystemId); + + if (!TryGetFriendStore(normalizedSystemId, out var userStore) || !userStore.TryRemove(normalizedFriendId, out _)) + { + return Task.FromResult(false); + } + + if (_friendships.TryGetValue(normalizedFriendId, out var peerStore)) + { + peerStore.TryRemove(normalizedSystemId, out _); + } + + return Task.FromResult(true); + } + + public Task SetTrustedAsync(SystemId systemId, SystemId friendSystemId, bool trusted, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedFriendId = InMemoryStorageKeys.Normalize(friendSystemId); + + if (!TryGetFriendshipState(normalizedSystemId, normalizedFriendId, out var state)) + { + return Task.FromResult(false); + } + + state.Level = trusted ? FriendshipLevel.TrustedFriend : FriendshipLevel.Friend; + return Task.FromResult(true); + } + + public Task GetFriendRequestsAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + + var outgoing = _outgoingRequests.TryGetValue(normalizedSystemId, out var outStore) + ? outStore.Values + .OrderByDescending(r => r.DateSent) + .Select(r => new FriendRequestReadModel( + new FriendProfileReadModel(r.OtherSystemId, null, null, null, null, null), + new FriendshipRequestModel(r.DateSent))) + .ToList() + : new List(); + + var incoming = _outgoingRequests + .SelectMany(kvp => kvp.Value.Values.Select(r => (From: kvp.Key, Request: r))) + .Where(x => x.Request.OtherSystemId == normalizedSystemId) + .OrderByDescending(x => x.Request.DateSent) + .Select(x => new FriendRequestReadModel( + new FriendProfileReadModel(x.From, null, null, null, null, null), + new FriendshipRequestModel(x.Request.DateSent))) + .ToList(); + + return Task.FromResult(new FriendRequestIndexReadModel(incoming, outgoing)); + } + + public Task SendRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedTargetId = InMemoryStorageKeys.Normalize(targetSystemId); + + if (IsFriends(normalizedSystemId, normalizedTargetId)) + { + return Task.FromResult(SendFriendRequestOutcome.AlreadyFriends); + } + + if (HasOutgoingRequest(normalizedSystemId, normalizedTargetId)) + { + return Task.FromResult(SendFriendRequestOutcome.AlreadySent); + } + + if (HasOutgoingRequest(normalizedTargetId, normalizedSystemId)) + { + LinkFriends(normalizedSystemId, normalizedTargetId); + RemoveRequest(normalizedTargetId, normalizedSystemId); + RemoveRequest(normalizedSystemId, normalizedTargetId); + return Task.FromResult(SendFriendRequestOutcome.Accepted); + } + + var store = _outgoingRequests.GetOrAdd(normalizedSystemId, _ => new ConcurrentDictionary()); + store[normalizedTargetId] = new RequestState + { + OtherSystemId = normalizedTargetId, + DateSent = DateTimeOffset.UtcNow + }; + + return Task.FromResult(SendFriendRequestOutcome.Sent); + } + + public Task AcceptRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedSourceId = InMemoryStorageKeys.Normalize(sourceSystemId); + + if (IsFriends(normalizedSystemId, normalizedSourceId)) + { + return Task.FromResult(FriendRequestMutationOutcome.AlreadyFriends); + } + + if (!HasOutgoingRequest(normalizedSourceId, normalizedSystemId)) + { + return Task.FromResult(FriendRequestMutationOutcome.NotRequested); + } + + LinkFriends(normalizedSystemId, normalizedSourceId); + RemoveRequest(normalizedSourceId, normalizedSystemId); + RemoveRequest(normalizedSystemId, normalizedSourceId); + return Task.FromResult(FriendRequestMutationOutcome.Ok); + } + + public Task RejectRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedSourceId = InMemoryStorageKeys.Normalize(sourceSystemId); + + if (IsFriends(normalizedSystemId, normalizedSourceId)) + { + return Task.FromResult(FriendRequestMutationOutcome.AlreadyFriends); + } + + if (!HasOutgoingRequest(normalizedSourceId, normalizedSystemId)) + { + return Task.FromResult(FriendRequestMutationOutcome.NotRequested); + } + + RemoveRequest(normalizedSourceId, normalizedSystemId); + return Task.FromResult(FriendRequestMutationOutcome.Ok); + } + + public Task CancelRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var normalizedTargetId = InMemoryStorageKeys.Normalize(targetSystemId); + + if (IsFriends(normalizedSystemId, normalizedTargetId)) + { + return Task.FromResult(FriendRequestMutationOutcome.AlreadyFriends); + } + + if (!HasOutgoingRequest(normalizedSystemId, normalizedTargetId)) + { + return Task.FromResult(FriendRequestMutationOutcome.NotRequested); + } + + RemoveRequest(normalizedSystemId, normalizedTargetId); + return Task.FromResult(FriendRequestMutationOutcome.Ok); + } + + public Task> DeleteAllForSystemAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + var friendIds = new List(); + + if (_friendships.TryRemove(normalizedSystemId, out var friends)) + { + foreach (var friendId in friends.Keys) + { + friendIds.Add(friendId); + if (_friendships.TryGetValue(friendId, out var peerStore)) + { + peerStore.TryRemove(normalizedSystemId, out _); + } + } + } + + _outgoingRequests.TryRemove(normalizedSystemId, out _); + + // In-memory scan for incoming requests targeting this user. + foreach (var requesterId in _outgoingRequests.Keys) + { + if (_outgoingRequests.TryGetValue(requesterId, out var requests)) + { + requests.TryRemove(normalizedSystemId, out _); + } + } + + return Task.FromResult>(friendIds.ToArray()); + } + + private bool IsFriends(SystemId systemId, SystemId friendSystemId) + => TryGetFriendStore(systemId, out var store) && store.ContainsKey(friendSystemId); + + private bool HasOutgoingRequest(SystemId fromSystemId, SystemId toSystemId) + => _outgoingRequests.TryGetValue(fromSystemId, out var store) && store.ContainsKey(toSystemId); + + private void RemoveRequest(SystemId fromSystemId, SystemId toSystemId) + { + if (_outgoingRequests.TryGetValue(fromSystemId, out var store)) + { + store.TryRemove(toSystemId, out _); + } + } + + private void LinkFriends(SystemId left, SystemId right) + { + var now = DateTimeOffset.UtcNow; + + var leftStore = _friendships.GetOrAdd(left, _ => new ConcurrentDictionary()); + leftStore[right] = new FriendshipState + { + FriendSystemId = right, + Level = FriendshipLevel.Friend, + Since = now + }; + + var rightStore = _friendships.GetOrAdd(right, _ => new ConcurrentDictionary()); + rightStore[left] = new FriendshipState + { + FriendSystemId = left, + Level = FriendshipLevel.Friend, + Since = now + }; + } + + private bool TryGetFriendStore(SystemId systemId, out ConcurrentDictionary store) + => _friendships.TryGetValue(systemId, out store!); + + private bool TryGetFriendshipState(SystemId systemId, SystemId friendSystemId, out FriendshipState state) + { + if (TryGetFriendStore(systemId, out var store) && store.TryGetValue(friendSystemId, out state!)) + { + return true; + } + + state = null!; + return false; + } +} + + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFrontingRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFrontingRepository.cs new file mode 100644 index 00000000..5d6516c8 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryFrontingRepository.cs @@ -0,0 +1,379 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryFrontingRepository : IFrontingRepository +{ + private sealed class FrontState + { + public required FrontId FrontId { get; init; } + public required AlterId AlterId { get; init; } + public string? Comment { get; set; } + public required DateTimeOffset StartedAt { get; init; } + } + + private sealed class FrontHistoryState + { + public required FrontId FrontId { get; init; } + public required AlterId AlterId { get; init; } + public string? Comment { get; set; } + public required DateTimeOffset StartedAt { get; init; } + public DateTimeOffset? EndedAt { get; set; } + } + + private readonly IRegionContext _regionContext; + private readonly IFriendshipRepository _friendships; + private readonly IAlterRepository _alters; + private readonly ILogger _logger; + private readonly ConcurrentDictionary> _activeBySystem = new(); + private readonly ConcurrentDictionary> _historyBySystem = new(); + private readonly ConcurrentDictionary _primaryBySystem = new(); + private readonly object _sync = new(); + + public InMemoryFrontingRepository(IRegionContext regionContext, IFriendshipRepository friendships, IAlterRepository alters, ILogger logger) + { + _regionContext = regionContext; + _friendships = friendships; + _alters = alters; + _logger = logger; + } + + public Task IsFrontingAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + var active = TryGetActiveSet(systemKey, out var set) && set.ContainsKey(alterId); + return Task.FromResult(active); + } + } + + public Task StartAsync( + SystemId systemId, + AlterId alterId, + string? comment, + DateTimeOffset startedAt, + CancellationToken cancellationToken = default + ) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + var set = _activeBySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + if (set.ContainsKey(alterId)) + { + return Task.FromResult(null); + } + + FrontId frontId = new(Guid.NewGuid()); + set[alterId] = new FrontState + { + FrontId = frontId, + AlterId = alterId, + Comment = comment, + StartedAt = startedAt + }; + + var history = _historyBySystem.GetOrAdd(systemKey, _ => new List()); + history.Add(new FrontHistoryState + { + FrontId = frontId, + AlterId = alterId, + Comment = comment, + StartedAt = set[alterId].StartedAt, + EndedAt = null + }); + + return Task.FromResult(frontId); + } + } + + public Task EndAsync(SystemId systemId, AlterId alterId, DateTimeOffset endedAt, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetActiveSet(systemKey, out var set)) + { + return Task.FromResult(false); + } + + var removed = set.TryRemove(alterId, out var removedFront); + if (removed && _primaryBySystem.TryGetValue(systemKey, out var currentPrimary) && currentPrimary == alterId) + { + _primaryBySystem[systemKey] = null; + } + + if (removed && removedFront is not null && TryGetHistory(systemKey, out var history)) + { + var historical = history.LastOrDefault( + x => x.FrontId == removedFront.FrontId && + x.EndedAt is null); + if (historical is not null) + { + historical.EndedAt = endedAt; + } + } + + return Task.FromResult(removed); + } + } + + public Task SetPrimaryAsync(SystemId systemId, AlterId? alterId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (alterId is { } value) + { + if (!TryGetActiveSet(systemKey, out var set) || !set.ContainsKey(value)) + { + return Task.FromResult(false); + } + } + + _primaryBySystem[systemKey] = alterId; + return Task.FromResult(true); + } + } + + public async Task> ListActiveAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + IReadOnlyCollection activeFronts; + AlterId? primaryId; + + lock (_sync) + { + if (!TryGetActiveSet(systemKey, out var set)) + return Array.Empty(); + + activeFronts = set.Values.ToArray(); + _primaryBySystem.TryGetValue(systemKey, out primaryId); + } + + var results = new List(activeFronts.Count); + foreach (var x in activeFronts.OrderByDescending(f => f.StartedAt)) + { + var alterModel = await _alters.GetAsync(systemId, x.AlterId, cancellationToken); + var bareAlter = alterModel is not null + ? new BareAlter(x.AlterId, alterModel.Name, alterModel.AvatarUrl, alterModel.AvatarSource, alterModel.Color, alterModel.Pronouns, alterModel.Description, alterModel.Fields ?? Array.Empty()) + : BareAlter.CreatePlaceholder(x.AlterId); + + results.Add(new FrontActiveReadModel( + bareAlter, + new FrontHistoryReadModel(x.FrontId, x.AlterId, x.Comment, x.StartedAt, null, systemId), + primaryId == x.AlterId)); + } + + return results; + } + + public async Task> ListActiveGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var ownerId = InMemoryStorageKeys.Normalize(systemId).Value; + var friendshipLevel = await InMemoryStorageKeys.ResolveFriendshipLevelAsync(systemId, viewerSystemId, _friendships, cancellationToken); + if (!VisibilityLevel.Public.CanBeViewedBy(friendshipLevel)) + { + GuardedInstrumentation.RecordList(_logger, "fronting", nameof(ListActiveGuardedAsync), viewerSystemId, ownerId, totalCount: 0, visibleCount: 0, sw.Elapsed.TotalMilliseconds); + return Array.Empty(); + } + + var all = await ListActiveAsync(systemId, cancellationToken); + if (all.Count == 0) + { + GuardedInstrumentation.RecordList(_logger, "fronting", nameof(ListActiveGuardedAsync), viewerSystemId, ownerId, totalCount: 0, visibleCount: 0, sw.Elapsed.TotalMilliseconds); + return all; + } + + var guardedAlters = await _alters.ListGuardedAsync(systemId, viewerSystemId, cancellationToken); + var visibleIds = guardedAlters.Select(a => a.Id).ToHashSet(); + + var visible = all.Where(front => visibleIds.Contains(front.Alter.Id)).ToArray(); + GuardedInstrumentation.RecordList(_logger, "fronting", nameof(ListActiveGuardedAsync), viewerSystemId, ownerId, all.Count, visible.Length, sw.Elapsed.TotalMilliseconds); + return visible; + } + + public Task> ListHistoryBetweenAsync( + SystemId systemId, + DateTimeOffset startInclusive, + DateTimeOffset endInclusive, + CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetHistory(systemKey, out var history)) + { + return Task.FromResult>(Array.Empty()); + } + + var results = history + .Where(x => x.StartedAt >= startInclusive && x.StartedAt <= endInclusive) + .OrderByDescending(x => x.StartedAt) + .Select(x => new FrontHistoryReadModel(x.FrontId, x.AlterId, x.Comment, x.StartedAt, x.EndedAt, systemId)) + .ToArray(); + + return Task.FromResult>(results); + } + } + + public Task> ListAllAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetHistory(systemKey, out var history)) + { + return Task.FromResult>(Array.Empty()); + } + + var results = history + .OrderByDescending(x => x.StartedAt) + .Select(x => new FrontHistoryReadModel(x.FrontId, x.AlterId, x.Comment, x.StartedAt, x.EndedAt, systemId)) + .ToArray(); + + return Task.FromResult>(results); + } + } + + public Task GetActiveByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetActiveSet(systemKey, out var set)) + return Task.FromResult(null); + + _primaryBySystem.TryGetValue(systemKey, out var primary); + + var found = set.Values.FirstOrDefault(x => x.FrontId == frontId); + if (found is null) + return Task.FromResult(null); + + return Task.FromResult(new FrontActiveReadModel( + BareAlter.CreatePlaceholder(found.AlterId), + new FrontHistoryReadModel(found.FrontId, found.AlterId, found.Comment, found.StartedAt, null, systemId), + primary == found.AlterId)); + } + } + + public Task GetHistoryEntryByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetHistory(systemKey, out var history)) + return Task.FromResult(null); + + var entry = history.FirstOrDefault(x => x.FrontId == frontId); + if (entry is null) + return Task.FromResult(null); + + return Task.FromResult(new FrontHistoryReadModel( + entry.FrontId, entry.AlterId, entry.Comment, entry.StartedAt, entry.EndedAt, systemId)); + } + } + + public async Task EndByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + var found = await GetActiveByFrontIdAsync(systemId, frontId, cancellationToken); + if (found is null) + return false; + + return await EndAsync(systemId, found.Front.AlterId, DateTimeOffset.UtcNow, cancellationToken); + } + + public Task DeleteFrontByIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetHistory(systemKey, out var history)) + return Task.FromResult(false); + + var entry = history.FirstOrDefault(x => x.FrontId == frontId); + if (entry is null) + return Task.FromResult(false); + + history.Remove(entry); + + if (TryGetActiveSet(systemKey, out var active) + && active.TryGetValue(entry.AlterId, out var frontState) + && frontState.FrontId == entry.FrontId) + { + active.TryRemove(entry.AlterId, out _); + + if (_primaryBySystem.TryGetValue(systemKey, out var primary) && primary == entry.AlterId) + _primaryBySystem[systemKey] = null; + } + + return Task.FromResult(true); + } + } + + public Task UpdateCommentByFrontIdAsync(SystemId systemId, FrontId frontId, string comment, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + + lock (_sync) + { + if (!TryGetActiveSet(systemKey, out var set)) + return Task.FromResult(false); + + var found = set.Values.FirstOrDefault(x => x.FrontId == frontId); + if (found is null) + return Task.FromResult(false); + + found.Comment = comment; + + if (TryGetHistory(systemKey, out var history)) + { + var historical = history.LastOrDefault( + x => x.FrontId == frontId && x.EndedAt is null); + if (historical is not null) + { + historical.Comment = comment; + } + } + + return Task.FromResult(true); + } + } + + private bool TryGetActiveSet(ScopedSystemId systemKey, out ConcurrentDictionary set) + => _activeBySystem.TryGetValue(systemKey, out set!); + + private bool TryGetHistory(ScopedSystemId systemKey, out List history) + => _historyBySystem.TryGetValue(systemKey, out history!); + +} + + + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryImportOperationRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryImportOperationRepository.cs new file mode 100644 index 00000000..1268afe0 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryImportOperationRepository.cs @@ -0,0 +1,213 @@ +using System.Collections.Concurrent; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory.Repository; + +/// In-memory — Cassandra's LWT mutex +/// is emulated via . Same +/// contract as the Scylla port so InMemory-backed +/// integration tests are a meaningful proxy for the production semantics. +public sealed class InMemoryImportOperationRepository : IImportOperationRepository +{ + private readonly ConcurrentDictionary<(SystemId SystemId, ImportOperationId OperationId), Row> _operations = new(); + private readonly ConcurrentDictionary<(SystemId SystemId, ImportOperationKind Kind), ImportOperationId> _active = new(); + + public Task TryClaimAsync( + SystemId systemId, + ImportOperationKind kind, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + ImportOperationId newOperationId = new(Guid.NewGuid()); + var key = (systemId, kind); + + // In-memory equivalent of Cassandra LWT IF NOT EXISTS. + if (!_active.TryAdd(key, newOperationId)) + { + var existing = _active[key]; + return Task.FromResult(new ImportOperationClaim(existing, IsNew: false)); + } + + var now = DateTimeOffset.UtcNow; + var row = new Row + { + SystemId = systemId, + OperationId = newOperationId, + Kind = kind, + Status = ImportOperationStatus.Queued, + StartedAt = now, + IdempotencyKey = idempotencyKey, + }; + _operations[(systemId, newOperationId)] = row; + + return Task.FromResult(new ImportOperationClaim(newOperationId, IsNew: true)); + } + + public Task MarkRunningAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (_operations.TryGetValue((systemId, operationId), out var row)) + { + // Queued -> Running only; re-pickup of a Running row is a no-op (idempotent retry). + lock (row) + { + if (row.Status == ImportOperationStatus.Queued) + { + row.Status = ImportOperationStatus.Running; + } + } + } + + return Task.CompletedTask; + } + + public Task MarkSucceededAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + int alterCount, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (_operations.TryGetValue((systemId, operationId), out var row)) + { + lock (row) + { + row.Status = ImportOperationStatus.Succeeded; + row.FinishedAt = DateTimeOffset.UtcNow; + row.AlterCount = alterCount; + } + } + + ReleaseSlot(systemId, kind, operationId); + return Task.CompletedTask; + } + + public Task MarkFailedAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + ImportErrorCode errorCode, + string? errorMessage, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (_operations.TryGetValue((systemId, operationId), out var row)) + { + lock (row) + { + row.Status = ImportOperationStatus.Failed; + row.FinishedAt = DateTimeOffset.UtcNow; + row.ErrorCode = errorCode; + row.ErrorMessage = errorMessage; + } + } + + ReleaseSlot(systemId, kind, operationId); + return Task.CompletedTask; + } + + public Task GetByIdAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (!_operations.TryGetValue((systemId, operationId), out var row)) + { + return Task.FromResult(null); + } + + return Task.FromResult(Snapshot(row)); + } + + public Task GetActiveOperationIdAsync( + SystemId systemId, + ImportOperationKind kind, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (_active.TryGetValue((systemId, kind), out var id)) + { + return Task.FromResult(id); + } + + return Task.FromResult(null); + } + + public Task> GetStaleRunningAsync( + TimeSpan olderThan, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + var cutoff = DateTimeOffset.UtcNow - olderThan; + var stale = new List(); + foreach (var row in _operations.Values) + { + ImportOperationSnapshot? snapshot; + lock (row) + { + snapshot = row.Status == ImportOperationStatus.Running && row.StartedAt < cutoff + ? Snapshot(row) + : null; + } + + if (snapshot is not null) + { + stale.Add(snapshot); + } + } + + return Task.FromResult>(stale); + } + + // Conditional-swap release — mirrors Scylla's DELETE … IF operation_id = ? so a stale + // terminal call can't evict a different in-flight operation that took the slot after. + private void ReleaseSlot(SystemId systemId, ImportOperationKind kind, ImportOperationId operationId) + { + var key = (systemId, kind); + var pair = new KeyValuePair<(SystemId, ImportOperationKind), ImportOperationId>(key, operationId); + ((ICollection>)_active).Remove(pair); + } + + private static ImportOperationSnapshot Snapshot(Row row) => + new(row.SystemId, + row.OperationId, + row.Kind, + row.Status, + row.StartedAt, + row.FinishedAt, + row.AlterCount, + row.ErrorCode, + row.ErrorMessage, + row.IdempotencyKey); + + // Lock-on-self guards status-machine transitions; Snapshot takes the same lock. + private sealed class Row + { + public required SystemId SystemId { get; init; } + public required ImportOperationId OperationId { get; init; } + public required ImportOperationKind Kind { get; init; } + public ImportOperationStatus Status { get; set; } + public DateTimeOffset StartedAt { get; init; } + public DateTimeOffset? FinishedAt { get; set; } + public int? AlterCount { get; set; } + public ImportErrorCode? ErrorCode { get; set; } + public string? ErrorMessage { get; set; } + public required IdempotencyKey IdempotencyKey { get; init; } + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryJournalRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryJournalRepository.cs new file mode 100644 index 00000000..7c411a75 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryJournalRepository.cs @@ -0,0 +1,400 @@ +using System.Collections.Concurrent; +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryJournalRepository : IJournalRepository +{ + private sealed class EntryState + { + public required EntryId EntryId { get; init; } + public required SystemId UserId { get; init; } + public required string Title { get; set; } + public string? Content { get; set; } + public HexColor? Color { get; set; } + public required DateTime InsertedAt { get; init; } + public DateTime UpdatedAt { get; set; } + } + + private sealed class AlterEntryState + { + public required EntryId EntryId { get; init; } + public required SystemId UserId { get; init; } + public required AlterId AlterId { get; init; } + public required string Title { get; set; } + public string? Content { get; set; } + public HexColor? Color { get; set; } + public bool Pinned { get; set; } + public bool Locked { get; set; } + public required DateTime InsertedAt { get; init; } + public DateTime UpdatedAt { get; set; } + } + + private readonly IRegionContext _regionContext; + private readonly ConcurrentDictionary> _bySystem = new(); + private readonly ConcurrentDictionary> _stateBySystem = new(); + private readonly ConcurrentDictionary<(ScopedSystemId System, EntryId EntryId), ConcurrentDictionary> _entryAlters = new(); + private readonly ConcurrentDictionary> _alterEntriesBySystem = new(); + + public InMemoryJournalRepository(IRegionContext regionContext) + { + _regionContext = regionContext; + } + + public Task CreateGlobalAsync(SystemId systemId, CreateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _bySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + EntryId id = new(Guid.NewGuid()); + var now = DateTime.UtcNow; + + store[id] = new EntryState + { + EntryId = id, + UserId = systemId, + Title = command.Title, + Content = null, + Color = null, + InsertedAt = now, + UpdatedAt = now + }; + + return Task.FromResult(id); + } + + public Task ExistsGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + var exists = GlobalEntryExists(systemId, entryId); + return Task.FromResult(exists); + } + + public Task UpdateGlobalAsync(SystemId systemId, UpdateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default) + { + if (!TryGetGlobalEntry(systemId, command.EntryId, out var entry)) + return Task.FromResult(false); + + if (command.Title is not null) entry.Title = command.Title; + if (command.Content is not null) entry.Content = command.Content; + if (command.Color is not null) entry.Color = command.Color; + entry.UpdatedAt = DateTime.UtcNow; + + return Task.FromResult(true); + } + + public Task DeleteGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetGlobalStore(systemId, out var store)) + return Task.FromResult(false); + + var removed = store.TryRemove(entryId, out _); + if (removed) + { + if (_stateBySystem.TryGetValue(systemKey, out var stateStore)) + stateStore.TryRemove(entryId, out _); + + _entryAlters.TryRemove(GetEntryKey(systemId, entryId), out _); + } + + return Task.FromResult(removed); + } + + public Task SetGlobalLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!GlobalEntryExists(systemId, entryId)) + return Task.FromResult(false); + + var stateStore = _stateBySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + var current = stateStore.GetOrAdd(entryId, _ => (false, false)); + stateStore[entryId] = (current.Pinned, locked); + return Task.FromResult(true); + } + + public Task SetGlobalPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!GlobalEntryExists(systemId, entryId)) + return Task.FromResult(false); + + var stateStore = _stateBySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + var current = stateStore.GetOrAdd(entryId, _ => (false, false)); + stateStore[entryId] = (pinned, current.Locked); + return Task.FromResult(true); + } + + public Task AttachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default) + { + if (!GlobalEntryExists(systemId, entryId)) + return Task.FromResult(false); + + var key = GetEntryKey(systemId, entryId); + var alters = _entryAlters.GetOrAdd(key, _ => new ConcurrentDictionary()); + alters[alterId] = true; + return Task.FromResult(true); + } + + public Task DetachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default) + { + var key = GetEntryKey(systemId, entryId); + if (!TryGetEntryAlterMap(key, out var alters)) + return Task.FromResult(false); + + return Task.FromResult(alters.TryRemove(alterId, out _)); + } + + public Task CreateAlterAsync(SystemId systemId, CreateAlterJournalEntryCommand command, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _alterEntriesBySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + EntryId entryId = new(Guid.NewGuid()); + var now = DateTime.UtcNow; + + store[entryId] = new AlterEntryState + { + EntryId = entryId, + UserId = systemId, + AlterId = command.AlterId, + Title = command.Title, + Content = null, + Color = null, + Pinned = false, + Locked = false, + InsertedAt = now, + UpdatedAt = now + }; + + return Task.FromResult(entryId); + } + + public Task GetAlterRefAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + if (!TryGetAlterEntry(systemId, entryId, out var entry)) + return Task.FromResult(null); + + return Task.FromResult(new AlterJournalRef(entry.EntryId, entry.AlterId)); + } + + public Task UpdateAlterAsync(SystemId systemId, UpdateAlterJournalEntryCommand command, CancellationToken cancellationToken = default) + { + if (!TryGetAlterEntry(systemId, command.EntryId, out var entry)) + return Task.FromResult(false); + + if (command.Title is not null) entry.Title = command.Title; + if (command.Content is not null) entry.Content = command.Content; + if (command.Color is not null) entry.Color = command.Color; + entry.UpdatedAt = DateTime.UtcNow; + + return Task.FromResult(true); + } + + public Task DeleteAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + if (!TryGetAlterStore(systemId, out var store)) + return Task.FromResult(false); + + return Task.FromResult(store.TryRemove(entryId, out _)); + } + + public Task DeleteAllForAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + var removedAlterJournals = 0; + + if (TryGetAlterStore(systemId, out var store)) + { + // Snapshot before mutation — enumerating a ConcurrentDictionary during writes is undefined. + var toRemove = store.Values.Where(e => e.AlterId == alterId).Select(e => e.EntryId).ToArray(); + foreach (var entryId in toRemove) + { + if (store.TryRemove(entryId, out _)) + { + removedAlterJournals++; + } + } + } + + // Sweep every entry's alter map so the detached alter is dropped from every attached + // global entry (mirrors ScyllaJournalRepository.global_journal_alters cleanup). + foreach (var alters in _entryAlters.Values) + { + alters.TryRemove(alterId, out _); + } + + return Task.FromResult(removedAlterJournals); + } + + public Task SetAlterLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default) + => SetAlterFlagAsync(systemId, entryId, static (entry, value) => entry.Locked = value, locked); + + public Task SetAlterPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default) + => SetAlterFlagAsync(systemId, entryId, static (entry, value) => entry.Pinned = value, pinned); + + public Task> ListAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + if (!TryGetAlterStore(systemId, out var store)) + return Task.FromResult>(Array.Empty()); + + var entries = store.Values + .Where(e => e.AlterId == alterId) + .OrderByDescending(e => e.InsertedAt) + .Select(MapAlterJournalReadModel) + .ToArray(); + + return Task.FromResult>(entries); + } + + public Task GetAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + if (!TryGetAlterEntry(systemId, entryId, out var entry)) + return Task.FromResult(null); + + return Task.FromResult(MapAlterJournalReadModel(entry)); + } + + private Task SetAlterFlagAsync( + SystemId systemId, + EntryId entryId, + Action apply, + bool value) + { + if (!TryGetAlterEntry(systemId, entryId, out var entry)) + { + return Task.FromResult(false); + } + + apply(entry, value); + entry.UpdatedAt = DateTime.UtcNow; + return Task.FromResult(true); + } + + private bool TryGetAlterEntry(SystemId systemId, EntryId entryId, out AlterEntryState entry) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (_alterEntriesBySystem.TryGetValue(systemKey, out var store) + && store.TryGetValue(entryId, out entry!)) + { + return true; + } + + entry = null!; + return false; + } + + public Task> ListGlobalAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + if (!TryGetGlobalStore(systemId, out var store)) + return Task.FromResult>(Array.Empty()); + + // Sort by wire form (lowercase "N" hex) — Guid.CompareTo reorders differently. + var entries = store.Values + .OrderByDescending(e => e.EntryId.Value.ToString("N"), StringComparer.Ordinal) + .Select(e => + { + var (pinned, locked) = GetGlobalState(systemId, e.EntryId); + var alterIds = GetGlobalAlterIds(systemId, e.EntryId); + return MapJournalReadModel(e, locked, pinned, alterIds); + }) + .ToArray(); + + return Task.FromResult>(entries); + } + + public Task GetGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + if (!TryGetGlobalEntry(systemId, entryId, out var entry)) + return Task.FromResult(null); + + var (pinned, locked) = GetGlobalState(systemId, entryId); + var alterIds = GetGlobalAlterIds(systemId, entryId); + return Task.FromResult(MapJournalReadModel(entry, locked, pinned, alterIds)); + } + + private (bool Pinned, bool Locked) GetGlobalState(SystemId systemId, EntryId entryId) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (_stateBySystem.TryGetValue(systemKey, out var stateStore) && + stateStore.TryGetValue(entryId, out var state)) + return state; + return (false, false); + } + + private IReadOnlyList GetGlobalAlterIds(SystemId systemId, EntryId entryId) + { + var key = GetEntryKey(systemId, entryId); + if (!TryGetEntryAlterMap(key, out var alters)) + return Array.Empty(); + return alters.Keys.ToArray(); + } + + private (ScopedSystemId System, EntryId EntryId) GetEntryKey(SystemId systemId, EntryId entryId) + => (InMemoryStorageKeys.ForSystem(_regionContext, systemId), entryId); + + private static AlterJournalReadModel MapAlterJournalReadModel(AlterEntryState entry) + { + return new AlterJournalReadModel( + entry.EntryId, + entry.UserId, + entry.AlterId, + entry.Title, + entry.Content, + entry.Color, + entry.Locked, + entry.Pinned, + entry.InsertedAt, + entry.UpdatedAt + ); + } + + private static JournalReadModel MapJournalReadModel(EntryState entry, bool locked, bool pinned, IReadOnlyList alterIds) + { + return new JournalReadModel( + entry.EntryId, + entry.UserId, + entry.Title, + entry.Content, + entry.Color, + locked, + pinned, + entry.InsertedAt, + entry.UpdatedAt, + alterIds + ); + } + + private bool TryGetGlobalStore(SystemId systemId, out ConcurrentDictionary store) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return _bySystem.TryGetValue(systemKey, out store!); + } + + private bool TryGetAlterStore(SystemId systemId, out ConcurrentDictionary store) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return _alterEntriesBySystem.TryGetValue(systemKey, out store!); + } + + private bool TryGetEntryAlterMap( + (ScopedSystemId System, EntryId EntryId) key, + out ConcurrentDictionary alters) + => _entryAlters.TryGetValue(key, out alters!); + + private bool TryGetGlobalEntry(SystemId systemId, EntryId entryId, out EntryState entry) + { + if (TryGetGlobalStore(systemId, out var store) && store.TryGetValue(entryId, out entry!)) + { + return true; + } + + entry = null!; + return false; + } + + private bool GlobalEntryExists(SystemId systemId, EntryId entryId) + => TryGetGlobalStore(systemId, out var store) && store.ContainsKey(entryId); +} + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryNotificationTokenRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryNotificationTokenRepository.cs new file mode 100644 index 00000000..07586182 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryNotificationTokenRepository.cs @@ -0,0 +1,89 @@ +using System.Collections.Concurrent; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryNotificationTokenRepository : INotificationTokenRepository +{ + private readonly ConcurrentDictionary> _tokensBySystem = new(); + private readonly ConcurrentDictionary _tokenOwners = new(); + private readonly IFriendshipRepository _friendshipRepository; + + public InMemoryNotificationTokenRepository(IFriendshipRepository friendshipRepository) + { + _friendshipRepository = friendshipRepository; + } + + public Task AddAsync(SystemId systemId, PushToken token, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + PushToken normalizedToken = new(token.Value.Trim()); + _tokenOwners[normalizedToken] = normalizedSystemId; + + var systemTokens = _tokensBySystem.GetOrAdd(normalizedSystemId, _ => new ConcurrentDictionary()); + systemTokens[normalizedToken] = 1; + + return Task.FromResult(true); + } + + public Task RemoveAsync(PushToken token, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + PushToken normalizedToken = new(token.Value.Trim()); + if (_tokenOwners.TryRemove(normalizedToken, out var ownerSystemId) && + _tokensBySystem.TryGetValue(ownerSystemId, out var systemTokens)) + { + systemTokens.TryRemove(normalizedToken, out _); + } + + return Task.FromResult(true); + } + + // Friends with zero registered tokens are omitted so callers can iterate without a + // Count > 0 guard. Mirrors the Scylla port so backends stay swappable via OCTOCON_PERSISTENCE. + public async Task> ListTokensForFriendsOfAsync( + SystemId systemId, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + var normalizedSystemId = InMemoryStorageKeys.Normalize(systemId); + if (string.IsNullOrWhiteSpace(normalizedSystemId)) + return Array.Empty(); + + var friendships = await _friendshipRepository.ListFriendshipsAsync(normalizedSystemId, cancellationToken); + if (friendships.Count == 0) + return Array.Empty(); + + var groups = new List(friendships.Count); + var seenFriends = new HashSet(); + foreach (var friendship in friendships) + { + if (friendship.Friend is null) + continue; + + var friendId = InMemoryStorageKeys.Normalize(friendship.Friend.Id); + if (string.IsNullOrWhiteSpace(friendId) || !seenFriends.Add(friendId)) + continue; + + if (!_tokensBySystem.TryGetValue(friendId, out var tokens)) + continue; + + var distinctTokens = tokens.Keys + .Where(t => !string.IsNullOrWhiteSpace(t.Value)) + .Distinct() + .ToArray(); + if (distinctTokens.Length == 0) + continue; + + groups.Add(new FriendNotificationTokens(friendId, distinctTokens)); + } + + return groups; + } +} diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryPollRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryPollRepository.cs new file mode 100644 index 00000000..9d99452f --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryPollRepository.cs @@ -0,0 +1,156 @@ +using System.Collections.Concurrent; +using System.Text.Json; +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryPollRepository : IPollRepository +{ + private sealed class PollState + { + public required PollId PollId { get; init; } + public required SystemId UserId { get; init; } + public required string Title { get; set; } + public string? Description { get; set; } + public required PollType Type { get; set; } + public JsonElement Data { get; set; } = JsonElement.Parse("{}"); + public DateTime? TimeEnd { get; set; } + public DateTime InsertedAt { get; set; } + public DateTime UpdatedAt { get; set; } + } + + private readonly IRegionContext _regionContext; + private readonly ConcurrentDictionary> _bySystem = new(); + + public InMemoryPollRepository(IRegionContext regionContext) + { + _regionContext = regionContext; + } + + public Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.FromResult>(Array.Empty()); + + // Sort by wire form (lowercase "N" hex) — Guid.CompareTo reorders differently. + var list = store.Values + .Select(ToReadModel) + .OrderBy(p => p.Id.Value.ToString("N"), StringComparer.Ordinal) + .ToList(); + + return Task.FromResult>(list); + } + + public Task GetAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + if (!TryGetPoll(systemId, pollId, out var poll)) + return Task.FromResult(null); + + return Task.FromResult(ToReadModel(poll)); + } + + public Task CreateAsync(SystemId systemId, CreatePollCommand command, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _bySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + PollId id = new(Guid.NewGuid()); + + store[id] = new PollState + { + PollId = id, + UserId = systemId, + Title = command.Title, + Description = command.Description, + Type = command.Type, + TimeEnd = command.TimeEnd, + InsertedAt = command.InsertedAtUtc, + UpdatedAt = DateTime.UtcNow + }; + + return Task.FromResult(id); + } + + public Task ExistsAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + var exists = TryGetStore(systemId, out var store) && store.ContainsKey(pollId); + return Task.FromResult(exists); + } + + public Task UpdateAsync(SystemId systemId, UpdatePollCommand command, CancellationToken cancellationToken = default) + { + if (!TryGetPoll(systemId, command.Id, out var poll)) + return Task.FromResult(false); + + if (command.Title is not null) poll.Title = command.Title; + if (command.Description is not null) poll.Description = command.Description; + if (command.HasTimeEnd) poll.TimeEnd = command.TimeEnd; + if (command.Data is not null) poll.Data = command.Data.Value; + poll.UpdatedAt = DateTime.UtcNow; + + return Task.FromResult(true); + } + + public Task DeleteAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.FromResult(false); + + return Task.FromResult(store.TryRemove(pollId, out _)); + } + + // See PollDataJson for the blob shape (top-level "responses" array of alter votes). + public Task RemoveAlterFromPollsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.CompletedTask; + + foreach (var poll in store.Values) + { + if (PollDataJson.TryRemoveAlterResponses(poll.Data, alterId, out var newData)) + { + poll.Data = newData; + poll.UpdatedAt = DateTime.UtcNow; + } + } + + return Task.CompletedTask; + } + + private bool TryGetStore(SystemId systemId, out ConcurrentDictionary store) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return _bySystem.TryGetValue(systemKey, out store!); + } + + private bool TryGetPoll(SystemId systemId, PollId pollId, out PollState poll) + { + if (TryGetStore(systemId, out var store) && store.TryGetValue(pollId, out poll!)) + { + return true; + } + + poll = null!; + return false; + } + + private static PollReadModel ToReadModel(PollState state) + => new( + state.PollId, + state.UserId, + state.Title, + state.Description, + state.Type, + state.Data, + state.TimeEnd, + state.InsertedAt, + state.UpdatedAt + ); +} + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemorySettingsFieldRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemorySettingsFieldRepository.cs new file mode 100644 index 00000000..5c9388ba --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemorySettingsFieldRepository.cs @@ -0,0 +1,164 @@ +using System.Collections.Concurrent; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemorySettingsFieldRepository : ISettingsFieldRepository +{ + private readonly IRegionContext _regionContext; + private readonly IServiceProvider _serviceProvider; + private readonly ConcurrentDictionary> _bySystem = new(); + + public InMemorySettingsFieldRepository(IRegionContext regionContext, IServiceProvider serviceProvider) + { + _regionContext = regionContext; + _serviceProvider = serviceProvider; + } + + public Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + { + return Task.FromResult>(Array.Empty()); + } + + lock (store) + { + var result = store + .OrderBy(x => x.Index) + .Select(x => x) + .ToArray(); + return Task.FromResult>(result); + } + } + + public Task CreateAsync( + SystemId systemId, + string name, + FieldType type, + VisibilityLevel securityLevel, + bool locked, + DateTime insertedAtUtc, + CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _bySystem.GetOrAdd(systemKey, _ => new List()); + FieldId fieldId = new(Guid.NewGuid()); + + lock (store) + { + store.Add(new SettingsFieldReadModel(fieldId, name, type, securityLevel, locked, store.Count, insertedAtUtc)); + } + + return Task.FromResult(fieldId); + } + + public Task UpdateAsync( + SystemId systemId, + FieldId fieldId, + string? name, + VisibilityLevel? securityLevel, + bool? locked, + CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.FromResult(false); + + lock (store) + { + var index = store.FindIndex(x => x.Id == fieldId); + if (index < 0) + { + return Task.FromResult(false); + } + + var existing = store[index]; + store[index] = existing with + { + Name = name ?? existing.Name, + SecurityLevel = securityLevel ?? existing.SecurityLevel, + Locked = locked ?? existing.Locked + }; + } + + return Task.FromResult(true); + } + + public Task DeleteAsync(SystemId systemId, FieldId fieldId, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.FromResult(false); + + lock (store) + { + var index = store.FindIndex(x => x.Id == fieldId); + if (index < 0) + { + return Task.FromResult(false); + } + + store.RemoveAt(index); + Reindex(store); + } + + // Cascade delete field values from alters in the in-memory alter repository (if present) + try + { + var alterRepo = _serviceProvider?.GetService(); + if (alterRepo is InMemoryAlterRepository regional) + { + regional.RemoveFieldValuesForSystem(systemId, fieldId.Value); + } + } + catch + { + // Best-effort cascade; don't fail deletion if alters update isn't available + } + + return Task.FromResult(true); + } + + public Task RelocateAsync(SystemId systemId, FieldId fieldId, int index, CancellationToken cancellationToken = default) + { + if (!TryGetStore(systemId, out var store)) + return Task.FromResult(false); + + lock (store) + { + var oldIndex = store.FindIndex(x => x.Id == fieldId); + if (oldIndex < 0) + { + return Task.FromResult(false); + } + + var field = store[oldIndex]; + store.RemoveAt(oldIndex); + + var boundedIndex = Math.Max(0, Math.Min(index, store.Count)); + store.Insert(boundedIndex, field); + Reindex(store); + return Task.FromResult(true); + } + } + + private static void Reindex(List fields) + { + for (var i = 0; i < fields.Count; i++) + { + fields[i] = fields[i] with { Index = i }; + } + } + + private bool TryGetStore(SystemId systemId, out List store) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return _bySystem.TryGetValue(systemKey, out store!); + } +} + diff --git a/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryTagRepository.cs b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryTagRepository.cs new file mode 100644 index 00000000..ca900af5 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.InMemory/Repository/InMemoryTagRepository.cs @@ -0,0 +1,299 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using Interfold.Alters.Contracts.Models; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Observability; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.Logging; + +namespace Interfold.Infrastructure.InMemory.Repository; + +public sealed class InMemoryTagRepository : ITagRepository +{ + private sealed record TagState( + TagId TagId, + TagId? ParentTagId, + string Name, + DateTime InsertedAt, + DateTime UpdatedAt, + HexColor? Color = null, + string? Description = null, + VisibilityLevel SecurityLevel = VisibilityLevel.Private + ); + + private readonly IRegionContext _regionContext; + private readonly IFriendshipRepository? _friendships; + private readonly ILogger _logger; + private readonly ConcurrentDictionary> _bySystem = new(); + private readonly ConcurrentDictionary<(ScopedSystemId System, TagId TagId), ConcurrentDictionary> _alterMemberships = new(); + + public InMemoryTagRepository(IRegionContext regionContext, ILogger logger) + { + _regionContext = regionContext; + _logger = logger; + } + + public InMemoryTagRepository(IRegionContext regionContext, IFriendshipRepository friendships, ILogger logger) + { + _regionContext = regionContext; + _friendships = friendships; + _logger = logger; + } + + public Task CreateAsync( + SystemId systemId, + CreateTagCommand command, + CancellationToken cancellationToken = default + ) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var store = _bySystem.GetOrAdd(systemKey, _ => new ConcurrentDictionary()); + + if (command.ParentTagId is { } parent && parent != TagId.Empty && !store.ContainsKey(parent)) + return Task.FromResult(null); + + TagId id = new(Guid.NewGuid()); + store[id] = new TagState(id, command.ParentTagId, command.Name, command.InsertedAtUtc, DateTime.UtcNow); + return Task.FromResult(id); + } + + public Task ExistsAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + var exists = _bySystem.TryGetValue(systemKey, out var store) && store.ContainsKey(tagId); + return Task.FromResult(exists); + } + + public Task UpdateAsync(SystemId systemId, UpdateTagCommand command, CancellationToken cancellationToken = default) + { + if (!TryGetTag(systemId, command.TagId, out var store, out var existing)) + return Task.FromResult(false); + + store[command.TagId] = existing with + { + Name = command.Name ?? existing.Name, + UpdatedAt = DateTime.UtcNow, + Color = command.Color ?? existing.Color, + Description = command.Description ?? existing.Description, + SecurityLevel = command.SecurityLevel ?? existing.SecurityLevel + }; + return Task.FromResult(true); + } + + public Task DeleteAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetStore(systemKey, out var store)) + return Task.FromResult(false); + + var removed = store.TryRemove(tagId, out _); + if (removed) + _alterMemberships.TryRemove((systemKey, tagId), out _); + return Task.FromResult(removed); + } + + public Task AttachAlterAsync(SystemId systemId, TagId tagId, AlterId alterId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetStore(systemKey, out var store) || !store.ContainsKey(tagId)) + return Task.FromResult(false); + + var members = _alterMemberships.GetOrAdd((systemKey, tagId), _ => new ConcurrentDictionary()); + members[BareAlter.CreatePlaceholder(alterId)] = true; + return Task.FromResult(true); + } + + public Task DetachAlterAsync(SystemId systemId, TagId tagId, AlterId alterId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetAlterMembers(systemKey, tagId, out var members)) + return Task.FromResult(false); + + return Task.FromResult(members.Remove(members.FirstOrDefault(x => x.Key.Id == alterId).Key, out _)); + } + + public Task GetParentIdAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + if (TryGetTag(systemId, tagId, out _, out var state)) + return Task.FromResult(state.ParentTagId); + + return Task.FromResult(null); + } + + public Task SetParentAsync(SystemId systemId, TagId tagId, TagId parentTagId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetStore(systemKey, out var store)) + return Task.FromResult(false); + + if (!store.TryGetValue(tagId, out var existing) || !store.ContainsKey(parentTagId)) + return Task.FromResult(false); + + store[tagId] = existing with { ParentTagId = parentTagId, UpdatedAt = DateTime.UtcNow }; + return Task.FromResult(true); + } + + public Task RemoveParentAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + if (!TryGetTag(systemId, tagId, out var store, out var existing)) + return Task.FromResult(false); + + store[tagId] = existing with { ParentTagId = null, UpdatedAt = DateTime.UtcNow }; + return Task.FromResult(true); + } + + public Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetStore(systemKey, out var store)) + return Task.FromResult>(Array.Empty()); + + // Sort by wire form (lowercase "N" hex) — Guid.CompareTo reorders differently. + var rows = store.Values + .OrderBy(x => x.TagId.Value.ToString("N"), StringComparer.Ordinal) + .Select(x => MapTagReadModel(x, GetAlterIds(systemKey, x.TagId), systemId)) + .ToArray(); + + return Task.FromResult>(rows); + } + + public async Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var ownerId = InMemoryStorageKeys.Normalize(systemId).Value; + var friendshipLevel = await InMemoryStorageKeys.ResolveFriendshipLevelAsync(systemId, viewerSystemId, _friendships, cancellationToken); + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetStore(systemKey, out var store)) + { + GuardedInstrumentation.RecordList(_logger, "tag", nameof(ListGuardedAsync), viewerSystemId, ownerId, totalCount: 0, visibleCount: 0, sw.Elapsed.TotalMilliseconds); + return Array.Empty(); + } + + var totalCount = store.Values.Count; + var rows = store.Values + .Where(x => x.SecurityLevel.CanBeViewedBy(friendshipLevel)) + .OrderBy(x => x.TagId.Value.ToString("N"), StringComparer.Ordinal) + .Select(x => MapTagPublicReadModel(x, GetAlters(systemKey, x.TagId), systemId)) + .ToArray(); + + GuardedInstrumentation.RecordList(_logger, "tag", nameof(ListGuardedAsync), viewerSystemId, ownerId, totalCount, rows.Length, sw.Elapsed.TotalMilliseconds); + return rows; + } + + public Task GetAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetTag(systemKey, tagId, out var store, out var tag)) + return Task.FromResult(null); + + return Task.FromResult(MapTagReadModel(tag, GetAlterIds(systemKey, tag.TagId), systemId)); + } + + public async Task GetGuardedAsync( + SystemId systemId, + TagId tagId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var ownerId = InMemoryStorageKeys.Normalize(systemId).Value; + var friendshipLevel = await InMemoryStorageKeys.ResolveFriendshipLevelAsync(systemId, viewerSystemId, _friendships, cancellationToken); + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + if (!TryGetTag(systemKey, tagId, out var store, out var tag)) + { + GuardedInstrumentation.RecordGet(_logger, "tag", nameof(GetGuardedAsync), viewerSystemId, ownerId, tagId.Value.ToString("N"), found: false, filtered: false, sw.Elapsed.TotalMilliseconds); + return null; + } + + if (!tag.SecurityLevel.CanBeViewedBy(friendshipLevel)) + { + GuardedInstrumentation.RecordGet(_logger, "tag", nameof(GetGuardedAsync), viewerSystemId, ownerId, tagId.Value.ToString("N"), found: false, filtered: true, sw.Elapsed.TotalMilliseconds); + return null; + } + + var result = MapTagPublicReadModel(tag, GetAlters(systemKey, tag.TagId), systemId); + GuardedInstrumentation.RecordGet(_logger, "tag", nameof(GetGuardedAsync), viewerSystemId, ownerId, tagId.Value.ToString("N"), found: true, filtered: false, sw.Elapsed.TotalMilliseconds); + return result; + } + + private IReadOnlyList GetAlterIds(ScopedSystemId systemKey, TagId tagId) + { + if (!TryGetAlterMembers(systemKey, tagId, out var members)) + return Array.Empty(); + + return members.Keys.OrderBy(x => x.Id.Value).Select(x => x.Id).ToArray(); + } + + private IReadOnlyList GetAlters(ScopedSystemId systemKey, TagId tagId) + { + if (!TryGetAlterMembers(systemKey, tagId, out var members)) + return Array.Empty(); + + return members.Keys.OrderBy(x => x.Id.Value).ToArray(); + } + + private static TagReadModel MapTagReadModel(TagState tag, IReadOnlyList alterIds, SystemId systemId) + { + return new TagReadModel( + tag.TagId, + tag.Name, + tag.Color, + tag.Description, + tag.ParentTagId, + alterIds, + tag.InsertedAt, + tag.UpdatedAt, + tag.SecurityLevel, + systemId + ); + } + + private static TagPublicReadModel MapTagPublicReadModel(TagState tag, IReadOnlyList alters, SystemId systemId) + { + return new TagPublicReadModel( + tag.TagId, + tag.Name, + tag.Color, + tag.Description, + tag.ParentTagId, + alters, + tag.InsertedAt, + tag.UpdatedAt, + tag.SecurityLevel, + systemId + ); + } + + private bool TryGetStore(ScopedSystemId systemKey, out ConcurrentDictionary store) + => _bySystem.TryGetValue(systemKey, out store!); + + private bool TryGetTag(SystemId systemId, TagId tagId, out ConcurrentDictionary store, out TagState tag) + { + var systemKey = InMemoryStorageKeys.ForSystem(_regionContext, systemId); + return TryGetTag(systemKey, tagId, out store, out tag); + } + + private bool TryGetTag(ScopedSystemId systemKey, TagId tagId, out ConcurrentDictionary store, out TagState tag) + { + if (TryGetStore(systemKey, out store) && store.TryGetValue(tagId, out tag!)) + { + return true; + } + + tag = null!; + return false; + } + + private bool TryGetAlterMembers(ScopedSystemId systemKey, TagId tagId, out ConcurrentDictionary members) + => _alterMemberships.TryGetValue((systemKey, tagId), out members!); +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/AuthTokenRevocationRepository.cs b/infrastructure/Interfold.Infrastructure.Postgres/AuthTokenRevocationRepository.cs new file mode 100644 index 00000000..2cb89f26 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/AuthTokenRevocationRepository.cs @@ -0,0 +1,94 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +/// Postgres-backed — one row per +/// issued JWT keyed on JTI so tokens can be revoked without refresh tokens. +public sealed class AuthTokenRevocationRepository : IAuthTokenRevocationRepository +{ + private readonly IPostgresConnectionFactory _connectionFactory; + private readonly PersistenceConfiguration _options; + + public AuthTokenRevocationRepository( + IPostgresConnectionFactory connectionFactory, + IOptions options) + { + _connectionFactory = connectionFactory; + _options = options.Value; + } + + public async Task RecordTokenAsync( + Jti jti, + SystemId systemId, + DateTimeOffset expiresAt, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + ArgumentException.ThrowIfNullOrWhiteSpace(systemId.Value, nameof(systemId)); + + await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _connectionFactory.OpenConnectionAsync(cancellationToken); + await using var command = new NpgsqlCommand(@" + INSERT INTO auth_tokens (jti, system_id, issued_at, expires_at, revoked_at) + VALUES (@jti, @system_id, NOW(), @expires_at, NULL) + ON CONFLICT (jti) DO NOTHING", connection); + + command.Parameters.AddWithValue("jti", jti.Value); + command.Parameters.AddWithValue("system_id", systemId.Value); + command.Parameters.AddWithValue("expires_at", expiresAt.UtcDateTime); + + await command.ExecuteNonQueryAsync(cancellationToken); + }, _options, cancellationToken); + } + + public async Task ValidateTokenNotRevokedAsync( + Jti jti, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + + return await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _connectionFactory.OpenConnectionAsync(cancellationToken); + await using var command = new NpgsqlCommand(@" + SELECT 1 + FROM auth_tokens + WHERE jti = @jti + AND revoked_at IS NULL + AND expires_at > NOW() + LIMIT 1", connection); + + command.Parameters.AddWithValue("jti", jti.Value); + + await using var reader = await command.ExecuteReaderAsync(cancellationToken); + return await reader.ReadAsync(cancellationToken); + }, _options, cancellationToken); + } + + public async Task RevokeTokenAsync( + Jti jti, + CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(jti.Value, nameof(jti)); + + await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _connectionFactory.OpenConnectionAsync(cancellationToken); + await using var command = new NpgsqlCommand(@" + UPDATE auth_tokens + SET revoked_at = NOW() + WHERE jti = @jti + AND revoked_at IS NULL", connection); + + command.Parameters.AddWithValue("jti", jti.Value); + + await command.ExecuteNonQueryAsync(cancellationToken); + }, _options, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/Interfold.Infrastructure.Postgres.csproj b/infrastructure/Interfold.Infrastructure.Postgres/Interfold.Infrastructure.Postgres.csproj new file mode 100644 index 00000000..cb74b5a9 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/Interfold.Infrastructure.Postgres.csproj @@ -0,0 +1,17 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + diff --git a/infrastructure/Interfold.Infrastructure.Postgres/Migrations/000_create_secrets_table.sql b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/000_create_secrets_table.sql new file mode 100644 index 00000000..2a28fa40 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/000_create_secrets_table.sql @@ -0,0 +1,14 @@ +-- Secrets table for storing OAuth credentials, encryption keys, etc. +-- Accessed at startup to patch configuration before the app accepts traffic. + +CREATE SCHEMA IF NOT EXISTS internal; + +CREATE TABLE IF NOT EXISTS internal.secrets ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL, + created_by TEXT NOT NULL DEFAULT 'bootstrap', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + expires_at TIMESTAMPTZ, + rotated_from TEXT +); diff --git a/infrastructure/Interfold.Infrastructure.Postgres/Migrations/001_grant_secrets_access.sql b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/001_grant_secrets_access.sql new file mode 100644 index 00000000..745db43b --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/001_grant_secrets_access.sql @@ -0,0 +1,16 @@ +-- Grant app user read-only access to the internal schema and secrets table. +-- The bootstrap script creates the app user before migrations run. +-- Only SELECT is granted — writes require the admin account. +DO $$ +DECLARE + r RECORD; +BEGIN + FOR r IN + SELECT rolname FROM pg_roles + WHERE rolcanlogin AND NOT rolsuper AND rolname NOT LIKE '%_admin' + LOOP + EXECUTE format('GRANT USAGE ON SCHEMA internal TO %I', r.rolname); + EXECUTE format('GRANT SELECT ON internal.secrets TO %I', r.rolname); + END LOOP; +END +$$; diff --git a/infrastructure/Interfold.Infrastructure.Postgres/Migrations/002_create_octocon_idempotency.sql b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/002_create_octocon_idempotency.sql new file mode 100644 index 00000000..c665e03d --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/002_create_octocon_idempotency.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS octocon_idempotency ( + principal_id text NOT NULL, + operation_id text NOT NULL, + idempotency_key text NOT NULL, + payload_hash text NOT NULL, + outcome_hash text NOT NULL, + outcome_payload text NULL, + created_at timestamptz NOT NULL, + PRIMARY KEY (principal_id, operation_id, idempotency_key) +); diff --git a/infrastructure/Interfold.Infrastructure.Postgres/Migrations/003_create_auth_tokens.sql b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/003_create_auth_tokens.sql new file mode 100644 index 00000000..c54e6a31 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/Migrations/003_create_auth_tokens.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS auth_tokens ( + jti TEXT PRIMARY KEY, + system_id TEXT NOT NULL, + issued_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ +); + +CREATE INDEX IF NOT EXISTS idx_auth_tokens_system_id ON auth_tokens(system_id); +CREATE INDEX IF NOT EXISTS idx_auth_tokens_expires_at ON auth_tokens(expires_at); +CREATE INDEX IF NOT EXISTS idx_auth_tokens_revoked_at_not_null ON auth_tokens(revoked_at) + WHERE revoked_at IS NOT NULL; diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresConnectionFactory.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresConnectionFactory.cs new file mode 100644 index 00000000..c2493f61 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresConnectionFactory.cs @@ -0,0 +1,31 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +public interface IPostgresConnectionFactory +{ + Task OpenConnectionAsync(CancellationToken cancellationToken = default); +} + +public sealed class PostgresConnectionFactory : IPostgresConnectionFactory +{ + private readonly PersistenceConfiguration _options; + + public PostgresConnectionFactory(IOptions options) + { + _options = options.Value; + } + + public async Task OpenConnectionAsync(CancellationToken cancellationToken = default) + { + return await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + var connection = new NpgsqlConnection(_options.PostgresConnectionString); + await connection.OpenAsync(cancellationToken); + return connection; + }, _options, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresHealthChecker.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresHealthChecker.cs new file mode 100644 index 00000000..7c090f20 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresHealthChecker.cs @@ -0,0 +1,55 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +public sealed class PostgresHealthChecker : IHealthCheck +{ + private readonly IPostgresConnectionFactory _postgresConnectionFactory; + private readonly PersistenceConfiguration _options; + + public PostgresHealthChecker( + IPostgresConnectionFactory postgresConnectionFactory, + IOptions options + ) + { + _postgresConnectionFactory = postgresConnectionFactory; + _options = options.Value; + } + + public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) + { + try + { + await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _postgresConnectionFactory.OpenConnectionAsync(cancellationToken); + + await using var pingCommand = new NpgsqlCommand("SELECT 1", connection); + await pingCommand.ExecuteScalarAsync(cancellationToken); + + await using var tableCommand = new NpgsqlCommand( + "SELECT to_regclass('public.octocon_idempotency')::text", + connection + ); + + var table = await tableCommand.ExecuteScalarAsync(cancellationToken) as string; + if (string.IsNullOrWhiteSpace(table)) + { + throw new InvalidOperationException( + "Postgres table 'octocon_idempotency' was not found. Run the SQL bootstrap script first." + ); + } + }, _options, cancellationToken); + + return new HealthCheckResult(HealthStatus.Healthy, "Connected and required table exists."); + } + catch (Exception ex) + { + return new HealthCheckResult(HealthStatus.Unhealthy, exception: ex); + } + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresIdempotencyStore.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresIdempotencyStore.cs new file mode 100644 index 00000000..fd216f50 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresIdempotencyStore.cs @@ -0,0 +1,107 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +public sealed class PostgresIdempotencyStore : IIdempotencyStore +{ + private readonly IPostgresConnectionFactory _connectionFactory; + private readonly PersistenceConfiguration _options; + + public PostgresIdempotencyStore( + IPostgresConnectionFactory connectionFactory, + IOptions options + ) + { + _connectionFactory = connectionFactory; + _options = options.Value; + } + + public async Task FindAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default + ) + { + return await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _connectionFactory.OpenConnectionAsync(cancellationToken); + await using var command = new NpgsqlCommand(@" + SELECT payload_hash, outcome_hash, outcome_payload + FROM octocon_idempotency + WHERE principal_id = @principal_id + AND operation_id = @operation_id + AND idempotency_key = @idempotency_key", connection); + + command.Parameters.AddWithValue("principal_id", principalId.Value); + command.Parameters.AddWithValue("operation_id", operationId.Value); + command.Parameters.AddWithValue("idempotency_key", idempotencyKey.Value); + + await using var reader = await command.ExecuteReaderAsync(cancellationToken); + if (!await reader.ReadAsync(cancellationToken)) + { + return null; + } + + var payloadHash = reader.GetString(0); + var outcomeHash = reader.GetString(1); + var outcomePayload = await reader.IsDBNullAsync(2, cancellationToken) ? null : reader.GetString(2); + + return new IdempotencyMatch(payloadHash, outcomeHash, outcomePayload); + }, _options, cancellationToken); + } + + public async Task SaveAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + string payloadHash, + string outcomeHash, + string? outcomePayload, + CancellationToken cancellationToken = default + ) + { + await DatabaseTransientRetry.ExecutePostgresAsync(async () => + { + await using var connection = await _connectionFactory.OpenConnectionAsync(cancellationToken); + await using var command = new NpgsqlCommand(@" + INSERT INTO octocon_idempotency ( + principal_id, + operation_id, + idempotency_key, + payload_hash, + outcome_hash, + outcome_payload, + created_at + ) VALUES ( + @principal_id, + @operation_id, + @idempotency_key, + @payload_hash, + @outcome_hash, + @outcome_payload, + NOW() + ) + ON CONFLICT (principal_id, operation_id, idempotency_key) + DO UPDATE + SET payload_hash = EXCLUDED.payload_hash, + outcome_hash = EXCLUDED.outcome_hash, + outcome_payload = EXCLUDED.outcome_payload", connection); + + command.Parameters.AddWithValue("principal_id", principalId.Value); + command.Parameters.AddWithValue("operation_id", operationId.Value); + command.Parameters.AddWithValue("idempotency_key", idempotencyKey.Value); + command.Parameters.AddWithValue("payload_hash", payloadHash); + command.Parameters.AddWithValue("outcome_hash", outcomeHash); + command.Parameters.AddWithValue("outcome_payload", (object?)outcomePayload ?? DBNull.Value); + + await command.ExecuteNonQueryAsync(cancellationToken); + }, _options, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresMigrationService.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresMigrationService.cs new file mode 100644 index 00000000..e8b4234a --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresMigrationService.cs @@ -0,0 +1,203 @@ +using System.Diagnostics; +using System.Reflection; +using System.Security.Cryptography; +using System.Text; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +/// Applies embedded SQL migrations at startup under +/// using admin credentials from +/// . Ledger is internal.schema_migrations keyed on +/// version + SHA-256 checksum; migration body and ledger insert commit together, so a +/// partial failure leaves no orphan row. Content drift on a previously-applied file +/// fails fast. +public sealed class PostgresMigrationService( + IOptions options, + ISecretsStore secretsStore, + ILogger logger) : IHostedLifecycleService +{ + // Serialises concurrent hosts sharing the same DB. + private const long MigrationAdvisoryLockId = 8675309_2024_0001; + + public Task StartingAsync(CancellationToken cancellationToken) => + MigrateAsync(options.Value, secretsStore, logger, cancellationToken); + + /// Idempotent migration entry point. Exposed as a static so + /// SharedDbFixture can migrate once per test session instead of on every host build. + public static async Task MigrateAsync( + PersistenceConfiguration options, + ISecretsStore secretsStore, + ILogger logger, + CancellationToken cancellationToken) + { + var adminUsername = await secretsStore.GetAsync(SecretsStoreKeys.PostgresAdminUsername, cancellationToken); + var adminPassword = await secretsStore.GetAsync(SecretsStoreKeys.PostgresAdminPassword, cancellationToken); + if (string.IsNullOrWhiteSpace(adminUsername) || + string.IsNullOrWhiteSpace(adminPassword) || + string.IsNullOrWhiteSpace(options.PostgresConnectionString)) + { + logger.LogInformation("[pg-migrate] No admin credentials in secrets store — skipping migrations."); + return; + } + + var connBuilder = new NpgsqlConnectionStringBuilder(options.PostgresConnectionString); + connBuilder.Username = adminUsername; + connBuilder.Password = adminPassword; + var adminConnectionString = connBuilder.ConnectionString; + + logger.LogInformation("[pg-migrate] Applying PostgreSQL schema migrations..."); + + await using var connection = new NpgsqlConnection(adminConnectionString); + await connection.OpenAsync(cancellationToken); + + await using (var lockCmd = new NpgsqlCommand("SELECT pg_advisory_lock(@key)", connection)) + { + lockCmd.Parameters.AddWithValue("key", MigrationAdvisoryLockId); + await lockCmd.ExecuteNonQueryAsync(cancellationToken); + } + + try + { + await EnsureLedgerAsync(connection, cancellationToken); + var applied = await LoadAppliedAsync(connection, cancellationToken); + + var migrations = GetMigrationScripts(); + var appliedBy = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + var newlyApplied = 0; + var skipped = 0; + + foreach (var (name, sql) in migrations) + { + var checksum = ComputeChecksum(sql); + + if (applied.TryGetValue(name, out var existingChecksum)) + { + if (!string.Equals(existingChecksum, checksum, StringComparison.Ordinal)) + { + throw new InvalidOperationException( + $"[pg-migrate] Checksum mismatch for migration '{name}': " + + $"recorded={existingChecksum}, actual={checksum}. Migration files must not be " + + "edited after they have been applied. Restore the original file or, if the change " + + "is intentional, manually update internal.schema_migrations.checksum for this row."); + } + + logger.LogDebug("[pg-migrate] Skipping {Migration}, already applied.", name); + skipped++; + continue; + } + + logger.LogInformation("[pg-migrate] Applying: {Migration}", name); + var stopwatch = Stopwatch.StartNew(); + + await using var tx = await connection.BeginTransactionAsync(cancellationToken); + try + { + await using (var cmd = new NpgsqlCommand(sql, connection, tx)) + { + await cmd.ExecuteNonQueryAsync(cancellationToken); + } + + stopwatch.Stop(); + + await using (var insertCmd = new NpgsqlCommand( + "INSERT INTO internal.schema_migrations " + + "(version, checksum, duration_ms, applied_by) " + + "VALUES (@version, @checksum, @duration_ms, @applied_by)", + connection, tx)) + { + insertCmd.Parameters.AddWithValue("version", name); + insertCmd.Parameters.AddWithValue("checksum", checksum); + insertCmd.Parameters.AddWithValue("duration_ms", (int)stopwatch.ElapsedMilliseconds); + insertCmd.Parameters.AddWithValue("applied_by", appliedBy); + await insertCmd.ExecuteNonQueryAsync(cancellationToken); + } + + await tx.CommitAsync(cancellationToken); + newlyApplied++; + } + catch + { + await tx.RollbackAsync(cancellationToken); + throw; + } + } + + logger.LogInformation( + "[pg-migrate] Applied {New} new migration(s), skipped {Skipped} already-applied.", + newlyApplied, skipped); + } + finally + { + await using var unlockCmd = new NpgsqlCommand("SELECT pg_advisory_unlock(@key)", connection); + unlockCmd.Parameters.AddWithValue("key", MigrationAdvisoryLockId); + await unlockCmd.ExecuteNonQueryAsync(cancellationToken); + } + } + + public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + private static List<(string Name, string Sql)> GetMigrationScripts() + { + var assembly = Assembly.GetExecutingAssembly(); + var prefix = "Interfold.Infrastructure.Postgres.Migrations."; + + return assembly.GetManifestResourceNames() + .Where(n => n.StartsWith(prefix, StringComparison.Ordinal) && n.EndsWith(".sql", StringComparison.Ordinal)) + .OrderBy(n => n, StringComparer.Ordinal) + .Select(n => + { + using var stream = assembly.GetManifestResourceStream(n)!; + using var reader = new StreamReader(stream); + return (Name: n[prefix.Length..], Sql: reader.ReadToEnd()); + }) + .ToList(); + } + + // Ledger table is bootstrapped by the runner (not a .sql migration) so the first + // run can record 000_* correctly. + private static async Task EnsureLedgerAsync(NpgsqlConnection connection, CancellationToken cancellationToken) + { + const string ddl = """ + CREATE SCHEMA IF NOT EXISTS internal; + CREATE TABLE IF NOT EXISTS internal.schema_migrations ( + version TEXT PRIMARY KEY, + checksum TEXT NOT NULL, + applied_at TIMESTAMPTZ NOT NULL DEFAULT now(), + duration_ms INTEGER NOT NULL, + applied_by TEXT + ); + """; + + await using var cmd = new NpgsqlCommand(ddl, connection); + await cmd.ExecuteNonQueryAsync(cancellationToken); + } + + private static async Task> LoadAppliedAsync( + NpgsqlConnection connection, CancellationToken cancellationToken) + { + var applied = new Dictionary(StringComparer.Ordinal); + await using var cmd = new NpgsqlCommand( + "SELECT version, checksum FROM internal.schema_migrations", connection); + await using var reader = await cmd.ExecuteReaderAsync(cancellationToken); + while (await reader.ReadAsync(cancellationToken)) + { + applied[reader.GetString(0)] = reader.GetString(1); + } + return applied; + } + + internal static string ComputeChecksum(string sql) + { + return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(sql))); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresSecretsStore.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresSecretsStore.cs new file mode 100644 index 00000000..f4484e14 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresSecretsStore.cs @@ -0,0 +1,46 @@ +using Interfold.Shared.Contracts.Secrets; +using Npgsql; + +namespace Interfold.Infrastructure.Postgres; + +public sealed class PostgresSecretsStore(IPostgresConnectionFactory connectionFactory) : ISecretsStore +{ + public async Task GetAsync(SecretsStoreKey key, CancellationToken cancellationToken = default) + { + await using var conn = await connectionFactory.OpenConnectionAsync(cancellationToken); + await using var cmd = new NpgsqlCommand("SELECT value FROM internal.secrets WHERE key = @key", conn); + cmd.Parameters.AddWithValue("key", key.Value); + var result = await cmd.ExecuteScalarAsync(cancellationToken); + return result as string; + } + + public async Task GetRequiredAsync(SecretsStoreKey key, CancellationToken cancellationToken = default) + { + var value = await GetAsync(key, cancellationToken); + return value ?? throw new InvalidOperationException($"Required secret '{key.Value}' not found in secrets store."); + } + + public async Task> ListAsync(CancellationToken cancellationToken = default) + { + await using var conn = await connectionFactory.OpenConnectionAsync(cancellationToken); + await using var cmd = new NpgsqlCommand( + "SELECT key, value, created_by, created_at, updated_at, expires_at, rotated_from FROM internal.secrets ORDER BY key", + conn); + await using var reader = await cmd.ExecuteReaderAsync(cancellationToken); + + var results = new List(); + while (await reader.ReadAsync(cancellationToken)) + { + results.Add(new SecretEntry( + Key: new SecretsStoreKey(reader.GetString(0)), + Value: reader.GetString(1), + CreatedBy: reader.GetString(2), + CreatedAt: reader.GetDateTime(3), + UpdatedAt: reader.GetDateTime(4), + ExpiresAt: reader.IsDBNull(5) ? null : reader.GetDateTime(5), + RotatedFrom: reader.IsDBNull(6) ? null : reader.GetString(6))); + } + + return results; + } +} diff --git a/infrastructure/Interfold.Infrastructure.Postgres/PostgresServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure.Postgres/PostgresServiceCollectionExtensions.cs new file mode 100644 index 00000000..e87405ba --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Postgres/PostgresServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Infrastructure.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Infrastructure.Postgres; + +public static class PostgresServiceCollectionExtensions +{ + private static readonly Action Registration = PersistenceRegistration.Create(PersistenceMode.ScyllaPostgres, AddPostgresPersistence); + public static void Register() => Registration(); + + private static IServiceCollection AddPostgresPersistence( + IServiceCollection services, + PersistenceConfiguration options) + { + return services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddHostedService(); + } +} \ No newline at end of file diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Fixups/HexColorFixupService.cs b/infrastructure/Interfold.Infrastructure.Scylla/Fixups/HexColorFixupService.cs new file mode 100644 index 00000000..4af1bc8f --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Fixups/HexColorFixupService.cs @@ -0,0 +1,187 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Fixups; + +/// One-shot data fixup that normalises legacy color columns across +/// every colour-bearing table so can rely on +/// as its precondition. Runs once per +/// (keyspace, table) pair via a ledger row in global.schema_migrations with +/// scope hex_color_fixup:{keyspace}:{table}; on a fresh cluster the ledger row +/// is still written so subsequent boots skip the scan. +public sealed class HexColorFixupService( + IOptions options, + IScyllaSessionProvider sessionProvider, + IScyllaConfigResolver configResolver, + ILogger logger) : IHostedLifecycleService +{ + private const string LedgerTableFqn = "global.schema_migrations"; + private const string FixupVersion = "v1"; + + // Bump to invalidate previously-recorded fixup runs when normalisation changes. + private const string FixupChecksum = "hex_color_fixup_v1_normalise_bare_hex_or_null"; + + // alter_journals_by_alter is a denormalisation of alter_journals and must be fixed + // up independently so the two tables stay in sync. + private static readonly ColorTable[] ColorTables = + [ + new("alters", ["user_id", "id"]), + new("tags", ["user_id", "id"]), + new("alter_journals", ["user_id", "id", "alter_id"]), + new("alter_journals_by_alter", ["user_id", "alter_id", "id"]), + new("global_journals", ["user_id", "id"]), + ]; + + private static readonly string[] RegionalKeyspaces = + Enum.GetValues() + .Select(EnumWire.ToWire) + .ToArray(); + + public async Task StartingAsync(CancellationToken cancellationToken) + { + // ScyllaMigrationService runs first and owns the reachability decision — a session + // failure here is expected on in-memory / bootstrap-only runs. + ISession session; + try + { + session = await sessionProvider.GetSessionAsync(cancellationToken); + } + catch (Exception ex) + { + logger.LogInformation(ex, + "[hex-color-fixup] Skipping: no Scylla session available. This is expected in in-memory / bootstrap-only runs."); + return; + } + + var keyspaces = TargetKeyspaces(); + var applied = await LoadAppliedFixupScopesAsync(session, keyspaces, cancellationToken); + + foreach (var keyspace in keyspaces) + { + foreach (var table in ColorTables) + { + var scope = $"hex_color_fixup:{keyspace}:{table.Name}"; + if (applied.TryGetValue(scope, out var existingChecksum) + && string.Equals(existingChecksum, FixupChecksum, StringComparison.Ordinal)) + { + logger.LogDebug("[hex-color-fixup] Skipping '{Scope}' — already applied.", scope); + continue; + } + + var (normalised, nulled) = await FixupTableAsync(session, keyspace, table, cancellationToken); + await RecordFixupAsync(session, scope, cancellationToken); + logger.LogInformation( + "[hex-color-fixup] {Scope}: normalised {Normalised} row(s), nulled {Nulled} row(s).", + scope, normalised, nulled); + } + } + } + + public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + private string[] TargetKeyspaces() => + options.Value.IsSingleScyllaInstance + ? [configResolver.GetKeyspace()] + : RegionalKeyspaces; + + private async Task<(int normalised, int nulled)> FixupTableAsync( + ISession session, + string keyspace, + ColorTable table, + CancellationToken cancellationToken) + { + var pkList = string.Join(", ", table.PrimaryKeyColumns); + var selectStmt = new SimpleStatement( + $"SELECT {pkList}, color FROM {keyspace}.{table.Name}"); + selectStmt.SetPageSize(500); + + var rows = await session.ExecuteAsync(selectStmt); + + var normalised = 0; + var nulled = 0; + foreach (var row in rows) + { + cancellationToken.ThrowIfCancellationRequested(); + + var existing = row.GetValue("color"); + if (existing is null) + continue; + + var canonical = HexColor.Normalise(existing); + if (string.Equals(canonical, existing, StringComparison.Ordinal)) + continue; + + var pkValues = table.PrimaryKeyColumns.Select(pk => row.GetValue(pk)).ToArray(); + var whereClause = string.Join(" AND ", table.PrimaryKeyColumns.Select(pk => $"{pk} = ?")); + var updateArgs = new object?[pkValues.Length + 1]; + // canonical may be null; that intentionally nulls the color column. + updateArgs[0] = canonical; + Array.Copy(pkValues, 0, updateArgs, 1, pkValues.Length); + + var updateStmt = new SimpleStatement( + $"UPDATE {keyspace}.{table.Name} SET color = ? WHERE {whereClause}", + updateArgs); + + await session.ExecuteAsync(updateStmt); + + if (canonical is null) nulled++; + else normalised++; + } + + return (normalised, nulled); + } + + // Probes each expected scope by exact key (no ALLOW FILTERING); N = keyspaces * tables. + private static async Task> LoadAppliedFixupScopesAsync( + ISession session, + string[] keyspaces, + CancellationToken cancellationToken) + { + var applied = new Dictionary(StringComparer.Ordinal); + foreach (var keyspace in keyspaces) + { + foreach (var table in ColorTables) + { + var scope = $"hex_color_fixup:{keyspace}:{table.Name}"; + var stmt = new SimpleStatement( + $"SELECT checksum FROM {LedgerTableFqn} WHERE scope = ? AND version = ?", + scope, FixupVersion); + var rs = await session.ExecuteAsync(stmt); + var row = rs.FirstOrDefault(); + if (row is not null) + { + applied[scope] = row.GetValue("checksum") ?? string.Empty; + } + + cancellationToken.ThrowIfCancellationRequested(); + } + } + + return applied; + } + + // IF NOT EXISTS keeps this race-safe against concurrent boots. + private static async Task RecordFixupAsync( + ISession session, + string scope, + CancellationToken cancellationToken) + { + var stmt = new SimpleStatement( + $"INSERT INTO {LedgerTableFqn} (scope, version, checksum, applied_at, duration_ms, applied_by) " + + "VALUES (?, ?, ?, ?, ?, ?) IF NOT EXISTS", + scope, FixupVersion, FixupChecksum, DateTimeOffset.UtcNow, 0, "hex-color-fixup"); + await session.ExecuteAsync(stmt); + cancellationToken.ThrowIfCancellationRequested(); + } + + private sealed record ColorTable(string Name, string[] PrimaryKeyColumns); +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Interfold.Infrastructure.Scylla.csproj b/infrastructure/Interfold.Infrastructure.Scylla/Interfold.Infrastructure.Scylla.csproj new file mode 100644 index 00000000..3e9a9fa4 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Interfold.Infrastructure.Scylla.csproj @@ -0,0 +1,42 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/000_create_singleton_keyspaces.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/000_create_singleton_keyspaces.cql new file mode 100644 index 00000000..622746a4 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/000_create_singleton_keyspaces.cql @@ -0,0 +1,18 @@ +-- Bootstrap-only: creates the cluster-wide singleton keyspaces (`global`, `nam_nt`, +-- `dummy`) so global.schema_migrations can exist before any other migration runs and +-- so these keyspaces never need to be re-rendered per region. Applied unconditionally +-- on every startup (untracked); every statement is idempotent. +-- +-- Placeholders: +-- {{GLOBAL_REPLICATION}} Replication map for the global keyspace +-- {{NAM_NT_REPLICATION}} Replication map for the nam_nt keyspace +-- {{TABLETS_CLAUSE}} Tablets setting clause (empty on Cassandra, enabled on Scylla multi-DC) +-- {{TABLETS_CLAUSE_DISABLED}} Tablets-disabled clause (Scylla only; empty on Cassandra) + +CREATE KEYSPACE IF NOT EXISTS global WITH replication = {{GLOBAL_REPLICATION}} AND durable_writes = true{{TABLETS_CLAUSE}}; +CREATE KEYSPACE IF NOT EXISTS nam_nt WITH replication = {{NAM_NT_REPLICATION}} AND durable_writes = true{{TABLETS_CLAUSE_DISABLED}}; +CREATE KEYSPACE IF NOT EXISTS dummy WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true{{TABLETS_CLAUSE_DISABLED}}; + +ALTER KEYSPACE global WITH replication = {{GLOBAL_REPLICATION}}{{TABLETS_CLAUSE}}; +ALTER KEYSPACE nam_nt WITH replication = {{NAM_NT_REPLICATION}}{{TABLETS_CLAUSE_DISABLED}}; +ALTER KEYSPACE dummy WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}{{TABLETS_CLAUSE_DISABLED}}; diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/001_create_interfold_keyspaces.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/001_create_interfold_keyspaces.cql new file mode 100644 index 00000000..9035fe65 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/001_create_interfold_keyspaces.cql @@ -0,0 +1,16 @@ +-- Regional keyspace creation template for Octocon. +-- +-- This file is rendered and applied once per regional keyspace by +-- ScyllaMigrationService at startup. The cluster-wide singleton keyspaces +-- (global, nam_nt, dummy) live in 000_create_singleton_keyspaces.cql and are +-- created exactly once during bootstrap; this file only creates the per-region +-- {{KEYSPACE}}. +-- +-- Placeholders: +-- {{KEYSPACE}} Regional keyspace name (nam, eur, sam, sas, eas, ocn, gdpr) +-- {{KEYSPACE_REPLICATION}} Replication map for the current regional keyspace +-- {{TABLETS_CLAUSE}} Tablets setting clause (empty on Cassandra) + +CREATE KEYSPACE IF NOT EXISTS {{KEYSPACE}} WITH replication = {{KEYSPACE_REPLICATION}} AND durable_writes = true{{TABLETS_CLAUSE}}; + +ALTER KEYSPACE {{KEYSPACE}} WITH replication = {{KEYSPACE_REPLICATION}}{{TABLETS_CLAUSE}}; diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql new file mode 100644 index 00000000..8789a3e7 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql @@ -0,0 +1,487 @@ +-- ===================== +-- Schema Template: Replace {{KEYSPACE}} with regional keyspace (nam, eur, ocn, eas, sam, sas, gdpr) +-- Applied by ScyllaMigrationService at startup. +-- ===================== + +-- ===================== +-- UDTs +-- ===================== +CREATE TYPE IF NOT EXISTS {{KEYSPACE}}.discord_server_settings ( + guild_id text, + proxying_disabled boolean, + autoproxy_mode smallint, + latched_alter int +); + +CREATE TYPE IF NOT EXISTS {{KEYSPACE}}.discord_settings ( + system_tag text, + show_system_tag boolean, + case_insensitive_proxies boolean, + show_pronouns boolean, + ids_as_proxies boolean, + silent_proxying boolean, + use_proxy_delay boolean, + global_autoproxy_mode smallint, + global_latched_alter int, + server_settings list> +); + +CREATE TYPE IF NOT EXISTS {{KEYSPACE}}.field ( + id uuid, + name text, + type smallint, + locked boolean, + security_level smallint +); + +CREATE TYPE IF NOT EXISTS {{KEYSPACE}}.alter_field ( + id uuid, + value text +); + +CREATE TYPE IF NOT EXISTS nam.server_settings_data ( + log_channel text, + force_system_tags boolean, + proxy_disabled_users list +); + +-- ===================== +-- USERS TABLE +-- avatar_source: 0 = Local (self-hosted via IAvatarStorage), 1 = External (passthrough URL). +-- Null when avatar_url is null. +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users ( + id text, + email text, + discord_id text, + apple_id text, + google_id text, + username text, + description text, + avatar_url text, + avatar_source smallint, + lifetime_alter_count int, + primary_front int, + last_proxy_id smallint, + discord_settings frozen<{{KEYSPACE}}.discord_settings>, + fields list>, + salt text, + encryption_initialized boolean, + encryption_key_checksum text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (id) +); + +-- ===================== +-- USER IDENTITY LOOKUP TABLES (denormalized, replaces secondary indexes) +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users_by_discord_id ( + discord_id text PRIMARY KEY, + user_id text +); + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users_by_email ( + email text PRIMARY KEY, + user_id text +); + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users_by_username ( + username text PRIMARY KEY, + user_id text +); + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users_by_apple_id ( + apple_id text PRIMARY KEY, + user_id text +); + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.users_by_google_id ( + google_id text PRIMARY KEY, + user_id text +); + +-- ===================== +-- ALTERS TABLE +-- avatar_source: 0 = Local (self-hosted via IAvatarStorage), 1 = External (passthrough URL). +-- Null when avatar_url is null. +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alters ( + user_id text, + id smallint, + alias text, + name text, + pronouns text, + description text, + avatar_url text, + avatar_source smallint, + security_level smallint, + extra_images list, + color text, + discord_proxies list, + proxy_name text, + fields list>, + untracked boolean, + archived boolean, + pinned boolean, + last_fronted timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id) +); + +-- ===================== +-- ALTERS_BY_ALIAS LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alters_by_alias ( + user_id text, + alias text, + alter_id smallint, + PRIMARY KEY (user_id, alias) +); + +-- ===================== +-- TAGS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.tags ( + user_id text, + id uuid, + parent_tag_id uuid, + name text, + description text, + color text, + security_level smallint, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id) +); + +-- ===================== +-- ALTER_TAGS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alter_tags ( + user_id text, + tag_id uuid, + alter_id smallint, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, tag_id, alter_id) +); + +-- ===================== +-- ALTER_TAGS_BY_ALTER LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alter_tags_by_alter ( + user_id text, + alter_id smallint, + tag_id uuid, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY ((user_id, alter_id), tag_id) +); + +-- ===================== +-- POLLS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.polls ( + user_id text, + id uuid, + title text, + description text, + type smallint, + data text, + time_end timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id) +); + +-- ===================== +-- GLOBAL_JOURNALS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.global_journals ( + user_id text, + id uuid, + title text, + content text, + color text, + pinned boolean, + locked boolean, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id) +); + +-- ===================== +-- GLOBAL_JOURNAL_ALTERS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.global_journal_alters ( + user_id text, + global_journal_id uuid, + alter_id smallint, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, global_journal_id, alter_id) +); + +-- ===================== +-- CURRENT_FRONTS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.current_fronts ( + user_id text, + alter_id smallint, + id uuid, + comment text, + time_start timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, alter_id) +); + +-- ===================== +-- ALTER_JOURNALS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alter_journals ( + user_id text, + id uuid, + alter_id smallint, + title text, + content text, + color text, + pinned boolean, + locked boolean, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id, alter_id) +); + +-- ===================== +-- ALTER_JOURNALS_BY_ALTER LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.alter_journals_by_alter ( + user_id text, + alter_id smallint, + id uuid, + title text, + content text, + color text, + pinned boolean, + locked boolean, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY ((user_id, alter_id), id) +); + +-- ===================== +-- FRONTS TABLE +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.fronts ( + id uuid, + user_id text, + alter_id smallint, + comment text, + time_start timestamp, + time_end timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, id, time_start) +); + +-- ===================== +-- FRONTS_BY_ALTER LOOKUP TABLE (denormalized, replaces materialized view) +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.fronts_by_alter ( + user_id text, + alter_id smallint, + id uuid, + comment text, + time_start timestamp, + time_end timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY ((user_id, alter_id), id, time_start) +); + +-- ===================== +-- FRONTS_BY_TIME LOOKUP TABLE (denormalized, replaces materialized view) +-- Populated only when a front is closed (time_end is set). +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.fronts_by_time ( + user_id text, + time_start timestamp, + time_end timestamp, + id uuid, + alter_id smallint, + comment text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, time_start, time_end, id) +); + +-- ===================== +-- FRONTS_BY_END_TIME LOOKUP TABLE (denormalized, replaces materialized view) +-- Populated only when a front is closed (time_end is set). +-- ===================== +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.fronts_by_end_time ( + user_id text, + time_end timestamp, + time_start timestamp, + id uuid, + alter_id smallint, + comment text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, time_end, time_start, id) +); + +-- ===================== +-- SERVER_SETTINGS TABLE (Nam-specific) +-- ===================== +CREATE TABLE IF NOT EXISTS nam.server_settings ( + guild_id text, + data frozen, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (guild_id) +); + +-- ===================== +-- CHANNEL_BLACKLISTS TABLE (Nam-specific) +-- ===================== +CREATE TABLE IF NOT EXISTS nam.channel_blacklists ( + channel_id text, + guild_id text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (channel_id) +); + +-- ===================== +-- CHANNEL_BLACKLISTS_BY_GUILD_ID LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS nam.channel_blacklists_by_guild_id ( + guild_id text, + channel_id text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (guild_id, channel_id) +); + +-- ===================== +-- GLOBAL KEYSPACE TABLES +-- ===================== +CREATE TABLE IF NOT EXISTS global.user_registry ( + user_id text, + discord_id text, + email text, + username text, + apple_id text, + google_id text, + region text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id) +); + +-- ===================== +-- GLOBAL USER_REGISTRY LOOKUP TABLES (denormalized, replaces secondary indexes) +-- ===================== +CREATE TABLE IF NOT EXISTS global.user_registry_by_discord_id ( + discord_id text PRIMARY KEY, + user_id text, + region text +); + +CREATE TABLE IF NOT EXISTS global.user_registry_by_email ( + email text PRIMARY KEY, + user_id text, + region text +); + +CREATE TABLE IF NOT EXISTS global.user_registry_by_username ( + username text PRIMARY KEY, + user_id text, + region text +); + +CREATE TABLE IF NOT EXISTS global.user_registry_by_apple_id ( + apple_id text PRIMARY KEY, + user_id text, + region text +); + +CREATE TABLE IF NOT EXISTS global.user_registry_by_google_id ( + google_id text PRIMARY KEY, + user_id text, + region text +); + +CREATE TABLE IF NOT EXISTS global.notification_tokens ( + user_id text, + push_token text, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, push_token) +); + +-- ===================== +-- NOTIFICATION_TOKENS_BY_PUSH_TOKEN LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS global.notification_tokens_by_push_token ( + push_token text, + user_id text, + PRIMARY KEY (push_token, user_id) +); + +CREATE TABLE IF NOT EXISTS global.friendships ( + user_id text, + friend_id text, + level smallint, + since timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (user_id, friend_id) +); + +-- ===================== +-- FRIENDSHIPS_BY_FRIEND_ID LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS global.friendships_by_friend_id ( + friend_id text, + user_id text, + level smallint, + since timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (friend_id, user_id) +); + +CREATE TABLE IF NOT EXISTS global.friend_requests ( + from_id text, + to_id text, + date_sent timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (from_id, to_id) +); + +-- ===================== +-- FRIEND_REQUESTS_BY_TO_ID LOOKUP TABLE (denormalized, replaces secondary index) +-- ===================== +CREATE TABLE IF NOT EXISTS global.friend_requests_by_to_id ( + to_id text, + from_id text, + date_sent timestamp, + inserted_at timestamp, + updated_at timestamp, + PRIMARY KEY (to_id, from_id) +); + +-- ===================== +-- METRICS KEYSPACE (nam_nt) +-- ===================== +CREATE TABLE IF NOT EXISTS nam_nt.metrics_counts ( + key text, + value counter, + PRIMARY KEY (key) +); diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/003_field_udt_timestamps.templated.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/003_field_udt_timestamps.templated.cql new file mode 100644 index 00000000..29c7ef0e --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/003_field_udt_timestamps.templated.cql @@ -0,0 +1,11 @@ +-- ===================== +-- Schema Template: Replace {{KEYSPACE}} with regional keyspace (nam, eur, ocn, eas, sam, sas, gdpr) +-- Applied by ScyllaMigrationService at startup, after 002_create_interfold_schema. +-- Adds the per-field created/updated timestamps onto the `field` UDT so the Simply Plural +-- importer can preserve a real created date decoded from the SP MongoDB ObjectId, and so +-- public Update/Relocate paths can bump a per-field `updated_at`. Both columns are nullable +-- to honour pre-migration rows (they read back as null and surface as null on the read model). +-- ===================== + +ALTER TYPE {{KEYSPACE}}.field ADD inserted_at timestamp; +ALTER TYPE {{KEYSPACE}}.field ADD updated_at timestamp; diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/004_import_operations.templated.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/004_import_operations.templated.cql new file mode 100644 index 00000000..973c3ba3 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/004_import_operations.templated.cql @@ -0,0 +1,50 @@ +-- ===================== +-- Schema Template: Replace {{KEYSPACE}} with regional keyspace (nam, eur, ocn, eas, sam, sas, gdpr) +-- Applied by ScyllaMigrationService at startup, after 003_field_udt_timestamps. +-- +-- Async-import job state. Two tables, kept consistent by the application layer: +-- +-- * import_operations — append-only history of every import that has ever been +-- dispatched for a system. Rows transition through queued -> +-- running -> (succeeded | failed). The clustering order keeps +-- the most recent operation at the head of the partition so +-- an operator can `SELECT * FROM import_operations WHERE +-- system_id = '…' LIMIT 5` to see what just happened without +-- paginating. +-- +-- * active_import_by_system — single row per (system_id, kind) holding the operation_id +-- currently in flight. Used as a per-system Cassandra LWT +-- mutex: a new dispatcher tries `INSERT IF NOT EXISTS`; if +-- another import is already active the row write fails +-- cleanly and the dispatcher reuses the existing operation_id +-- instead of starting a duplicate run. The row is removed +-- (`DELETE IF operation_id = ?`) by the worker when the +-- operation terminates, freeing the slot for the next click. +-- +-- This pair is the load-bearing mutual-exclusion mechanism that prevents the duplicate-SP-import +-- bug from reappearing if a future caller (browser, retry middleware, another microservice) +-- happens to dispatch the same import twice in flight. Cassandra LWT (Paxos) gives us a real +-- linearizable check without needing a separate lock service. +-- ===================== + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.import_operations ( + system_id text, + operation_id timeuuid, + kind text, + status text, + started_at timestamp, + finished_at timestamp, + alter_count int, + error_code text, + error_message text, + idempotency_key text, + PRIMARY KEY (system_id, operation_id) +) WITH CLUSTERING ORDER BY (operation_id DESC); + +CREATE TABLE IF NOT EXISTS {{KEYSPACE}}.active_import_by_system ( + system_id text, + kind text, + operation_id timeuuid, + started_at timestamp, + PRIMARY KEY (system_id, kind) +); diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/005_marker_color_fixup.templated.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/005_marker_color_fixup.templated.cql new file mode 100644 index 00000000..b9f794cb --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/005_marker_color_fixup.templated.cql @@ -0,0 +1,16 @@ +-- ===================== +-- Schema Marker: Replace {{KEYSPACE}} with regional keyspace (nam, eur, ocn, eas, sam, sas, gdpr) +-- Applied by ScyllaMigrationService at startup, after 004_import_operations. +-- +-- This file adds no DDL. Its sole purpose is to bump the migration ledger to the +-- version at which HexColor became strict, so the HexColorFixupService +-- (registered as an IHostedLifecycleService after ScyllaMigrationService) can +-- detect whether the ledger row for its own per-table per-keyspace fixup scope +-- has been recorded and skip the scan on subsequent startups. +-- +-- Any color-bearing table (alters, tags, alter_journals, alter_journals_by_alter, +-- global_journals) that carried a non-well-formed value (bare 6-char hex, empty +-- string, arbitrary spellings) before this migration is normalised in-place by +-- the fixup service. Well-formed rows are untouched; unrecoverable values are +-- set to NULL and logged. +-- ===================== diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Migrations/006_add_primary_front_alter.templated.cql b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/006_add_primary_front_alter.templated.cql new file mode 100644 index 00000000..5485ab29 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Migrations/006_add_primary_front_alter.templated.cql @@ -0,0 +1,31 @@ +-- ===================== +-- Schema Template: Replace {{KEYSPACE}} with regional keyspace (nam, eur, ocn, eas, sam, sas, gdpr) +-- Applied by ScyllaMigrationService at startup, immediately followed by +-- BackfillPrimaryFrontIntToAlterAsync which copies every non-null users.primary_front +-- (int) value into users.primary_front_alter (smallint). +-- +-- Why the "_alter" suffix instead of just narrowing the existing column: +-- Cassandra/Scylla forbids re-adding a dropped column with a different type — an +-- entry is kept in system_schema.dropped_columns to protect old SSTables from being +-- reinterpreted under an incompatible type. That closes both the "DROP + re-ADD as +-- smallint" trick and the "RENAME to primary_front" shortcut (RENAME is limited to +-- primary-key columns). The only safe path is to introduce a new column name whose +-- type we control from day one. +-- +-- Why the physical storage type is smallint (short) instead of int: +-- AlterId is a strong ID backed by a short. Keeping the DB type identical to the +-- runtime type removes the widening/narrowing helpers we used to carry (see AlterId +-- XML docs) and makes GetValue("primary_front_alter") a direct match. +-- +-- The legacy users.primary_front (int) column is intentionally NOT dropped by this +-- migration. A future release can drop it once we're confident no external tool still +-- reads it. Because we're never trying to re-use the "primary_front" name with a +-- different type, deferring the DROP is a pure cleanliness concern, not a correctness +-- one. +-- +-- Idempotence: the ledger row (scope=, version=006_add_primary_front_alter…) +-- short-circuits re-runs. A crash between the ALTER landing and the ledger row being +-- written is absorbed by ExecuteStatements' "column already exists" handler. +-- ===================== + +ALTER TABLE {{KEYSPACE}}.users ADD primary_front_alter smallint; diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterFieldUdt.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterFieldUdt.cs new file mode 100644 index 00000000..adb3da2c --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterFieldUdt.cs @@ -0,0 +1,7 @@ +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class AlterFieldUdt +{ + public Guid Id { get; set; } + public string? Value { get; set; } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterRowMappers.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterRowMappers.cs new file mode 100644 index 00000000..2b7ba057 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/AlterRowMappers.cs @@ -0,0 +1,48 @@ +using Cassandra; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class AlterRowMappers +{ + public static BareAlter MapBareAlter(Row row, IReadOnlyList definitions) + { + return new BareAlter( + new(row.GetValue("id")), + row.GetValue("name"), + AvatarUrl.FromNullable(row.GetValue("avatar_url")), + row.GetValue("avatar_source").FromCodeOrNull(), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("pronouns"), + row.GetValue("description"), + AlterFieldProjection.ResolveGuardedFields(row.GetValue?>("fields")?.ToDictionary(x => new FieldId(x.Id), x => x.Value), definitions)); + } + + public static AlterReadModel MapAlterReadModel(Row row, IReadOnlyList definitions) + { + return new AlterReadModel( + new(row.GetValue("id")), + row.GetValue("name"), + row.GetValue("description"), + AvatarUrl.FromNullable(row.GetValue("avatar_url")), + row.GetValue("avatar_source").FromCodeOrNull(), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("pronouns"), + row.GetValue("security_level").FromStorage(), + AlterFieldProjection.ResolveOwnerFields(row.GetValue?>("fields")?.ToDictionary(x => new FieldId(x.Id), x => x.Value), definitions), + row.GetValue("proxy_name"), + row.GetValue("alias"), + row.GetValue("untracked"), + row.GetValue("archived"), + row.GetValue("pinned"), + row.GetValue?>("discord_proxies")?.ToArray() ?? System.Array.Empty(), + row.GetValue("inserted_at") ?? default, + row.GetValue("updated_at") ?? default + ); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/FrontingRowMappers.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/FrontingRowMappers.cs new file mode 100644 index 00000000..9895872f --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/FrontingRowMappers.cs @@ -0,0 +1,20 @@ +using Cassandra; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class FrontingRowMappers +{ + public static FrontHistoryReadModel MapFrontHistoryReadModel(Row row, SystemId systemId, DateTimeOffset? timeEnd = null) + { + return new FrontHistoryReadModel( + new(row.GetValue("id")), + new(row.GetValue("alter_id")), + row.GetValue("comment"), + row.GetValue("time_start") ?? DateTimeOffset.UtcNow, + timeEnd, + systemId + ); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/JournalRowMappers.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/JournalRowMappers.cs new file mode 100644 index 00000000..521a0c5f --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/JournalRowMappers.cs @@ -0,0 +1,40 @@ +using Cassandra; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class JournalRowMappers +{ + public static AlterJournalReadModel MapAlterJournalReadModel(Row row) + { + return new AlterJournalReadModel( + new(row.GetValue("id")), + new(row.GetValue("user_id")), + new(row.GetValue("alter_id")), + row.GetValue("title"), + row.GetValue("content"), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("locked"), + row.GetValue("pinned"), + row.GetValue("inserted_at"), + row.GetValue("updated_at") + ); + } + + public static JournalReadModel MapJournalReadModel(Row row, AlterId[] alterIds) + { + return new JournalReadModel( + new(row.GetValue("id")), + new(row.GetValue("user_id")), + row.GetValue("title"), + row.GetValue("content"), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("locked"), + row.GetValue("pinned"), + row.GetValue("inserted_at"), + row.GetValue("updated_at"), + alterIds + ); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAccountRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAccountRepository.cs new file mode 100644 index 00000000..e82b8d1e --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAccountRepository.cs @@ -0,0 +1,603 @@ +using System.Collections.Concurrent; +using Cassandra; +using Interfold.Auth.Contracts.Ids; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Infrastructure.Persistence; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Systems.Contracts.Models.Read; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaAccountRepository : IAccountRepository +{ + // Closed OAuth-provider column enum; misspellings become build errors and every + // CQL string flows through ColumnName as the single translation point. + private enum ProviderColumn + { + Discord, + Email, + Apple, + } + + private static string ColumnName(ProviderColumn column) => column switch + { + ProviderColumn.Discord => "discord_id", + ProviderColumn.Email => "email", + ProviderColumn.Apple => "apple_id", + _ => throw new ArgumentOutOfRangeException(nameof(column), column, "Unknown provider column"), + }; + + private readonly record struct LinkTokenEntry(ScopedSystemId Scoped, DateTimeOffset ExpiresAt); + + private static readonly TimeSpan LinkTokenTtl = TimeSpan.FromMinutes(5); + private readonly object _linkTokenLock = new(); + // Reverse map keys on the typed LinkToken (not its string value) so no accidental + // toString-then-dict-key path can bypass the redacting wrapper. + private readonly ConcurrentDictionary _linkTokenBySystem = new(); + private readonly ConcurrentDictionary _systemByLinkToken = new(); + + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + + public ScyllaAccountRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options + ) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + } + + public async Task UpdateUsernameAsync(SystemId systemId, Username username, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var oldRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT username FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId))).FirstOrDefault(); + var oldUsername = oldRow?.GetValue("username"); + + var batch = new BatchStatement(); + if (oldRow is null) + { + // First touch: mint the regional users row so public guarded reads (ShowAlter etc.) + // don't 404 system_not_found. InMemory does this implicitly via its username map. + batch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.users (id, username, inserted_at, updated_at) VALUES (?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + username.Value)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.user_registry (user_id, username, region, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + username.Value, + keyspace)); + } + else + { + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.users SET username = ?, updated_at = toTimestamp(now()) WHERE id = ?", + username.Value, normalizedSystemId)); + batch.Add(new SimpleStatement( + $"UPDATE {ScyllaGlobalKeyspace.Name}.user_registry SET username = ?, updated_at = toTimestamp(now()) WHERE user_id = ?", + username.Value, normalizedSystemId)); + } + + if (!string.IsNullOrWhiteSpace(oldUsername)) + { + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.users_by_username WHERE username = ?", oldUsername)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.user_registry_by_username WHERE username = ?", oldUsername)); + } + + if (!string.IsNullOrWhiteSpace(username)) + { + batch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.users_by_username (username, user_id) VALUES (?, ?)", + username.Value, normalizedSystemId)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.user_registry_by_username (username, user_id, region) VALUES (?, ?, ?)", + username.Value, normalizedSystemId, keyspace)); + } + + await session.ExecuteAsync(batch); + return true; + }, cancellationToken); + } + + public async Task UpdateDescriptionAsync(SystemId systemId, string description, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var statement = new SimpleStatement( + $"UPDATE {keyspace}.users SET description = ?, updated_at = toTimestamp(now()) WHERE id = ?", + description, + normalizedSystemId + ); + + await session.ExecuteAsync(statement); + return true; + }, cancellationToken); + } + + public async Task UpdateAvatarAsync(SystemId systemId, AvatarUrl avatarUrl, AvatarSource source, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var statement = new SimpleStatement( + $"UPDATE {keyspace}.users SET avatar_url = ?, avatar_source = ?, updated_at = toTimestamp(now()) WHERE id = ?", + avatarUrl.Value, + (short)source, + normalizedSystemId + ); + + await session.ExecuteAsync(statement); + return true; + }, cancellationToken); + } + + public async Task ClearAvatarAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Null both columns together so observers can never see a half-cleared state. + var statement = new SimpleStatement( + $"UPDATE {keyspace}.users SET avatar_url = ?, avatar_source = ?, updated_at = toTimestamp(now()) WHERE id = ?", + null, + null, + normalizedSystemId + ); + + await session.ExecuteAsync(statement); + return true; + }, cancellationToken); + } + + public Task GetOrCreateLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + // Compose is idempotent on already-scoped inputs. + var scoped = ScopedSystemId.Compose(keyspace, normalizedSystemId); + var now = DateTimeOffset.UtcNow; + + lock (_linkTokenLock) + { + if (_linkTokenBySystem.TryGetValue(scoped, out var existingToken) + && _systemByLinkToken.TryGetValue(existingToken, out var existingEntry) + && existingEntry.ExpiresAt > now) + { + return Task.FromResult(existingToken); + } + + if (!string.IsNullOrWhiteSpace(existingToken.Value)) + { + _linkTokenBySystem.TryRemove(scoped, out _); + _systemByLinkToken.TryRemove(existingToken, out _); + } + + LinkToken token = new(Guid.NewGuid().ToString()); + _linkTokenBySystem[scoped] = token; + _systemByLinkToken[token] = new LinkTokenEntry(scoped, now.Add(LinkTokenTtl)); + + return Task.FromResult(token); + } + } + + public Task GetLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + var scoped = ScopedSystemId.Compose(keyspace, normalizedSystemId); + var now = DateTimeOffset.UtcNow; + + lock (_linkTokenLock) + { + if (_linkTokenBySystem.TryGetValue(scoped, out var token) + && _systemByLinkToken.TryGetValue(token, out var entry) + && entry.ExpiresAt > now) + { + return Task.FromResult(token); + } + + if (!string.IsNullOrWhiteSpace(token.Value)) + { + _linkTokenBySystem.TryRemove(scoped, out _); + _systemByLinkToken.TryRemove(token, out _); + } + + return Task.FromResult(null); + } + } + + public Task ResolveSystemIdByLinkTokenAsync(LinkToken linkToken, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(linkToken.Value)) + { + return Task.FromResult(null); + } + + var now = DateTimeOffset.UtcNow; + lock (_linkTokenLock) + { + if (_systemByLinkToken.TryGetValue(linkToken, out var entry) && entry.ExpiresAt > now) + { + return Task.FromResult(entry.Scoped.AsSystemId()); + } + + _systemByLinkToken.TryRemove(linkToken, out _); + foreach (var item in _linkTokenBySystem) + { + if (item.Value == linkToken) + { + _linkTokenBySystem.TryRemove(item.Key, out _); + break; + } + } + + return Task.FromResult(null); + } + } + + public Task ClearLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + var scoped = ScopedSystemId.Compose(keyspace, normalizedSystemId); + + lock (_linkTokenLock) + { + if (_linkTokenBySystem.TryRemove(scoped, out var token)) + { + _systemByLinkToken.TryRemove(token, out _); + } + + return Task.FromResult(true); + } + } + + // Every public entry point below dispatches to a typed-ProviderColumn helper. `.Value` + // is the only unwrap available on Cat B PII wrappers (no implicit widen through boxing). + + public Task TryFindSystemIdByDiscordIdAsync(DiscordId discordId, CancellationToken cancellationToken = default) + => TryFindSystemIdByRegistryColumnAsync(ProviderColumn.Discord, discordId.Value, cancellationToken); + + // Consolidated OAuth-login dispatch — the pattern-match lives once, not per-controller. + public Task FindOrCreateSystemIdAsync(ProviderIdentity identity, CancellationToken cancellationToken = default) + => identity.MatchOrThrow( + discordId => FindOrCreateSystemIdByRegistryColumnAsync(ProviderColumn.Discord, discordId.Value, cancellationToken), + email => FindOrCreateSystemIdByRegistryColumnAsync(ProviderColumn.Email, email.Value, cancellationToken), + appleId => FindOrCreateSystemIdByRegistryColumnAsync(ProviderColumn.Apple, appleId.Value, cancellationToken)); + + public Task LinkIdentityToUserAsync(SystemId systemId, ProviderIdentity identity, CancellationToken cancellationToken = default) + => identity.MatchOrThrow( + discordId => LinkIdentityAsync(systemId, ProviderColumn.Discord, discordId.Value, cancellationToken), + email => LinkIdentityAsync(systemId, ProviderColumn.Email, email.Value, cancellationToken), + appleId => LinkIdentityAsync(systemId, ProviderColumn.Apple, appleId.Value, cancellationToken)); + + public Task UnlinkDiscordAsync(SystemId systemId, CancellationToken cancellationToken = default) + => UnlinkIdentityAsync(systemId, ProviderColumn.Discord, cancellationToken); + + public Task UnlinkEmailAsync(SystemId systemId, CancellationToken cancellationToken = default) + => UnlinkIdentityAsync(systemId, ProviderColumn.Email, cancellationToken); + + public Task UnlinkAppleAsync(SystemId systemId, CancellationToken cancellationToken = default) + => UnlinkIdentityAsync(systemId, ProviderColumn.Apple, cancellationToken); + + public async Task DeleteAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var userRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT id, discord_id, email, username, apple_id, google_id FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId))).FirstOrDefault(); + + if (userRow is null) + { + return true; + } + + var deleteBatch = new BatchStatement(); + deleteBatch.Add(new SimpleStatement($"DELETE FROM {keyspace}.users WHERE id = ?", normalizedSystemId)); + deleteBatch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.user_registry WHERE user_id = ?", normalizedSystemId)); + + var identityColumns = new[] { "discord_id", "email", "username", "apple_id", "google_id" }; + foreach (var col in identityColumns) + { + var value = userRow.GetValue(col); + if (!string.IsNullOrWhiteSpace(value)) + { + deleteBatch.Add(new SimpleStatement($"DELETE FROM {keyspace}.users_by_{col} WHERE {col} = ?", value)); + deleteBatch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.user_registry_by_{col} WHERE {col} = ?", value)); + } + } + + await session.ExecuteAsync(deleteBatch); + + return true; + }, cancellationToken); + } + + public async Task GetPublicProfileAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var profileQuery = new SimpleStatement( + $"SELECT username, avatar_url, avatar_source, description, discord_id, email, apple_id FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId + ); + + var profile = (await session.ExecuteAsync(profileQuery)).FirstOrDefault(); + if (profile is null) + { + return null; + } + + return new AccountPublicProfileReadModel( + new(normalizedSystemId), + profile.GetValue("username") is { } username ? new Username(username) : null, + profile.GetValue("description"), + AvatarUrl.FromNullable(profile.GetValue("avatar_url")), + profile.GetValue("avatar_source").FromCodeOrNull(), + profile.GetValue("discord_id") is { } discordId ? new DiscordId(discordId) : null, + profile.GetValue("email") is { } email ? new Email(email) : null, + profile.GetValue("apple_id") is { } appleId ? new AppleId(appleId) : null); + }, cancellationToken); + } + + public async Task GetPublicSystemAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Public wire projection drops discord/email/apple. + var profileQuery = new SimpleStatement( + $"SELECT username, avatar_url, avatar_source, description FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId + ); + + var profile = (await session.ExecuteAsync(profileQuery)).FirstOrDefault(); + if (profile is null) + { + return null; + } + + return new PublicSystemReadModel( + Id: new SystemId(normalizedSystemId), + AvatarUrl: AvatarUrl.FromNullable(profile.GetValue("avatar_url")), + AvatarSource: profile.GetValue("avatar_source").FromCodeOrNull(), + Username: profile.GetValue("username") is { } username ? new Username(username) : null, + Description: profile.GetValue("description")); + }, cancellationToken); + } + + // Returns scoped `{region}:{userId}` — every downstream cache expects the scoped shape. + private async Task TryFindSystemIdByRegistryColumnAsync(ProviderColumn column, string value, CancellationToken cancellationToken) + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + var columnName = ColumnName(column); + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var query = new SimpleStatement( + $"SELECT user_id, region FROM {ScyllaGlobalKeyspace.Name}.user_registry_by_{columnName} WHERE {columnName} = ? LIMIT 1", + value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is not null) + { + var userId = NormalizeRegistryUserId(new(row.GetValue("user_id"))); + var region = row.GetValue("region") ?? _keyspaceResolver.DefaultKeyspace; + return ScopedSystemId.Compose(region, userId).AsSystemId(); + } + + return null; + }, _options, cancellationToken); + } + + private async Task FindOrCreateSystemIdByRegistryColumnAsync(ProviderColumn column, string value, CancellationToken cancellationToken) + { + var existing = await TryFindSystemIdByRegistryColumnAsync(column, value, cancellationToken); + if (existing is { } typedExisting && !string.IsNullOrWhiteSpace(typedExisting)) + { + return typedExisting; + } + + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + var columnName = ColumnName(column); + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + + const string idChars = "abcdefghijklmnopqrstuvwxyz"; + + var newRegion = _keyspaceResolver.DefaultKeyspace; + var newUserId = Random.Shared.GetString(idChars, 7); + var keyspace = newRegion; + + // Regional + global rows written in one batch to reduce split-write orphans. + // Four column names -> four binds (two params + two literal toTimestamp(now())). + var createUserBatch = new BatchStatement() + .Add(new SimpleStatement( + $"INSERT INTO {keyspace}.users (id, {columnName}, inserted_at, updated_at) VALUES (?, ?, toTimestamp(now()), toTimestamp(now()))", + newUserId, + value + )) + .Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.user_registry (user_id, {columnName}, region, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + newUserId, + value, + newRegion + )) + .Add(new SimpleStatement( + $"INSERT INTO {keyspace}.users_by_{columnName} ({columnName}, user_id) VALUES (?, ?)", + value, + newUserId + )) + .Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.user_registry_by_{columnName} ({columnName}, user_id, region) VALUES (?, ?, ?)", + value, + newUserId, + newRegion + )); + + await session.ExecuteAsync(createUserBatch); + + return ScopedSystemId.Compose(newRegion, newUserId).AsSystemId(); + }, _options, cancellationToken); + } + + // StripRegionPrefix removes only one leading region tag per call; fixed-point to + // catch legacy double-prefixed rows like "nam:nam:abcdefg". + private string NormalizeRegistryUserId(SystemId userId) + { + var normalized = _keyspaceResolver.NormalizeSystemId(userId); + for (var i = 0; i < 2; i++) + { + var next = _keyspaceResolver.NormalizeSystemId(new(normalized)); + if (next == normalized) + { + break; + } + + normalized = next; + } + + return normalized; + } + + private async Task LinkIdentityAsync(SystemId systemId, ProviderColumn column, string value, CancellationToken cancellationToken) + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + if (string.IsNullOrWhiteSpace(value)) + { + return AccountLinkResult.UserNotFound; + } + + var columnName = ColumnName(column); + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + + var owner = await TryFindSystemIdByRegistryColumnAsync(column, value, cancellationToken); + if (owner is { } typedOwner && !string.IsNullOrWhiteSpace(typedOwner)) + { + var normalizedOwner = NormalizeRegistryUserId(typedOwner); + if (normalizedOwner != normalizedSystemId) + { + return AccountLinkResult.UserExists; + } + } + + var userRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT id, {columnName} FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId))).FirstOrDefault(); + + if (userRow is null) + { + return AccountLinkResult.UserNotFound; + } + + var alreadyLinked = userRow.GetValue(columnName); + if (!string.IsNullOrWhiteSpace(alreadyLinked)) + { + return AccountLinkResult.AlreadyLinked; + } + + var linkBatch = new BatchStatement(); + linkBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.users SET {columnName} = ?, updated_at = toTimestamp(now()) WHERE id = ?", + value, + normalizedSystemId)); + linkBatch.Add(new SimpleStatement( + $"UPDATE {ScyllaGlobalKeyspace.Name}.user_registry SET {columnName} = ?, updated_at = toTimestamp(now()) WHERE user_id = ?", + value, + normalizedSystemId)); + linkBatch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.users_by_{columnName} ({columnName}, user_id) VALUES (?, ?)", + value, + normalizedSystemId)); + linkBatch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.user_registry_by_{columnName} ({columnName}, user_id, region) VALUES (?, ?, ?)", + value, + normalizedSystemId, + keyspace)); + await session.ExecuteAsync(linkBatch); + + return AccountLinkResult.Success; + }, _options, cancellationToken); + } + + private async Task UnlinkIdentityAsync(SystemId systemId, ProviderColumn column, CancellationToken cancellationToken) + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var columnName = ColumnName(column); + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + + var oldRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT {columnName} FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId))).FirstOrDefault(); + var oldValue = oldRow?.GetValue(columnName); + + var unlinkBatch = new BatchStatement(); + unlinkBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.users SET {columnName} = ?, updated_at = toTimestamp(now()) WHERE id = ?", + null, + normalizedSystemId)); + unlinkBatch.Add(new SimpleStatement( + $"UPDATE {ScyllaGlobalKeyspace.Name}.user_registry SET {columnName} = ?, updated_at = toTimestamp(now()) WHERE user_id = ?", + null, + normalizedSystemId)); + + if (!string.IsNullOrWhiteSpace(oldValue)) + { + unlinkBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.users_by_{columnName} WHERE {columnName} = ?", + oldValue)); + unlinkBatch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.user_registry_by_{columnName} WHERE {columnName} = ?", + oldValue)); + } + + await session.ExecuteAsync(unlinkBatch); + return true; + }, _options, cancellationToken); + } + +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAlterRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAlterRepository.cs new file mode 100644 index 00000000..20641dd1 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaAlterRepository.cs @@ -0,0 +1,534 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using Cassandra; +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Observability; +using Interfold.Polls.Domain.Abstractions.Repository; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaAlterRepository : IAlterRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly ISettingsFieldRepository _settingsFields; + private readonly IAlterFieldDefinitions _alterFieldDefinitions; + private readonly IPollRepository _pollRepository; + private readonly PersistenceConfiguration _options; + private readonly ILogger _logger; + private static readonly ConcurrentDictionary<(int ClusterId, string Keyspace), byte> UdtMappings = new(); + + public ScyllaAlterRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + ISettingsFieldRepository settingsFields, + IAlterFieldDefinitions alterFieldDefinitions, + IPollRepository pollRepository, + IOptions options, + ILogger logger + ) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _settingsFields = settingsFields; + _alterFieldDefinitions = alterFieldDefinitions; + _pollRepository = pollRepository; + _options = options.Value; + _logger = logger; + } + + public async Task CreateAsync(SystemId systemId, CreateAlterCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var nextIdQuery = new SimpleStatement( + $"SELECT id FROM {keyspace}.alters WHERE user_id = ? ORDER BY id DESC LIMIT 1", + normalizedSystemId + ); + + var rows = await session.ExecuteAsync(nextIdQuery); + var current = rows.FirstOrDefault()?.GetValue("id") ?? (short)0; + var next = (short)(current + 1); + var createdAt = command.CreatedAt.ToUniversalTime(); + + // Always stamp security_level — the strict read path rejects null. + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.alters (user_id, id, name, alias, security_level, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + next, + command.Name, + null, + (short)VisibilityLevel.Private, + createdAt, + createdAt + ); + + await session.ExecuteAsync(insert); + return new(next); + }, cancellationToken); + } + + public async Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, scope => ExistsAsync(scope, alterId), cancellationToken); + } + + private static Task ExistsAsync(ScyllaScope scope, AlterId alterId) + => ScyllaExistsQueries.RowExistsAsync(scope.Session, scope.Keyspace, "alters", "id", scope.NormalizedSystemId, alterId.Value); + + public async Task UpdateAsync(SystemId systemId, UpdateAlterCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var updatedAt = command.UpdatedAt.ToUniversalTime(); + + var batch = new BatchStatement(); + + var exists = await ExistsAsync(scope, command.AlterId); + if (!exists) + { + return false; + } + + UpdateIfNotNull(batch, keyspace, command, "name", command.Name, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "description", command.Description, normalizedSystemId, updatedAt); + if (command.ClearAvatar) + { + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET avatar_url = ?, avatar_source = ?, updated_at = ? WHERE user_id = ? AND id = ?", + null, + null, + updatedAt, + normalizedSystemId, + command.AlterId.Value + )); + } + else if (command.AvatarUrl is { } avatarUrl) + { + // avatar_url + avatar_source move together; domain handler rejects half-set. + var sourceShort = (short)(command.AvatarSource ?? AvatarSource.Local); + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET avatar_url = ?, avatar_source = ?, updated_at = ? WHERE user_id = ? AND id = ?", + avatarUrl.Value, + sourceShort, + updatedAt, + normalizedSystemId, + command.AlterId.Value + )); + } + UpdateIfNotNull(batch, keyspace, command, "color", command.Color?.Value, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "pronouns", command.Pronouns, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "security_level", (short?)command.SecurityLevel, normalizedSystemId, updatedAt); + + if (command.Fields is not null) + { + EnsureAlterFieldUdtMapping(session, keyspace); + + var currentRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT fields FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + command.AlterId.Value + ))).FirstOrDefault(); + + var merged = (currentRow?.GetValue?>("fields") ?? []) + .ToDictionary(x => x.Id, x => x.Value); + + foreach (var f in command.Fields) + { + merged[f.Id] = f.Value; + } + + var udts = merged + .Select(kvp => new AlterFieldUdt { Id = kvp.Key, Value = kvp.Value }) + .ToList(); + + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET fields = ?, updated_at = ? WHERE user_id = ? AND id = ?", + udts, + updatedAt, + normalizedSystemId, + command.AlterId.Value + )); + } + + UpdateIfNotNull(batch, keyspace, command, "proxy_name", command.ProxyName, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "untracked", command.Untracked, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "archived", command.Archived, normalizedSystemId, updatedAt); + UpdateIfNotNull(batch, keyspace, command, "pinned", command.Pinned, normalizedSystemId, updatedAt); + + if (command.Alias is not null) + { + var oldAliasRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT alias FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, command.AlterId.Value))).FirstOrDefault(); + var oldAlias = oldAliasRow?.GetValue("alias"); + + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET alias = ?, updated_at = ? WHERE user_id = ? AND id = ?", + command.Alias, updatedAt, normalizedSystemId, command.AlterId.Value)); + + if (!string.IsNullOrWhiteSpace(oldAlias)) + { + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alters_by_alias WHERE user_id = ? AND alias = ?", + normalizedSystemId, oldAlias)); + } + + var newAlias = command.Alias as string; + if (!string.IsNullOrWhiteSpace(newAlias)) + { + batch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.alters_by_alias (user_id, alias, alter_id) VALUES (?, ?, ?)", + normalizedSystemId, newAlias, command.AlterId.Value)); + } + } + + await session.ExecuteAsync(batch); + + return true; + }, cancellationToken); + } + + private void UpdateIfNotNull(BatchStatement batch, string keyspace, UpdateAlterCommand command, string field, object? value, string normalizedSystemId, DateTimeOffset updatedAt) + { + if (value is not null) + { + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET {field} = ?, updated_at = ? WHERE user_id = ? AND id = ?", + value, + updatedAt, + normalizedSystemId, + command.AlterId.Value + )); + } + } + + public async Task DeleteAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var alterIdShort = alterId.Value; + + var exists = await ExistsAsync(scope, alterId); + if (!exists) + { + return false; + } + + var deleteBatch = new BatchStatement(); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alters WHERE user_id = ? AND id = ?", + normalizedSystemId, + alterIdShort)); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterIdShort)); + await session.ExecuteAsync(deleteBatch); + + var frontsTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id, time_start FROM {keyspace}.fronts_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterIdShort)); + + var journalEntriesTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id FROM {keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterIdShort)); + + var tagsTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT tag_id FROM {keyspace}.alter_tags_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterIdShort)); + + var globalJournalAltersTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT global_journal_id FROM {keyspace}.global_journal_alters WHERE user_id = ? AND alter_id = ? ALLOW FILTERING", + normalizedSystemId, + alterIdShort)); + + var aliasTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT alias FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + alterIdShort)); + + var removePollsTask = _pollRepository.RemoveAlterFromPollsAsync(systemId, alterId, cancellationToken); + + await Task.WhenAll( + frontsTask, + journalEntriesTask, + tagsTask, + globalJournalAltersTask, + removePollsTask, + aliasTask + ); + + var frontRows = await frontsTask; + var journalEntryRows = await journalEntriesTask; + var membershipRows = await tagsTask; + var globalJournalAlterRows = await globalJournalAltersTask; + var aliasRow = (await aliasTask).FirstOrDefault(); + var alias = aliasRow?.GetValue("alias"); + + if (frontRows.Any()) + { + var frontBatch = new BatchStatement(); + foreach (var frontRow in frontRows) + { + var frontId = frontRow.GetValue("id"); + var timeStart = frontRow.GetValue("time_start"); + frontBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts WHERE user_id = ? AND id = ? AND time_start = ?", + normalizedSystemId, frontId, timeStart)); + frontBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_alter WHERE user_id = ? AND alter_id = ? AND id = ? AND time_start = ?", + normalizedSystemId, alterIdShort, frontId, timeStart)); + // Delete unconditionally — closed-front-only rows are a no-op miss. + frontBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_time WHERE user_id = ? AND time_start = ? AND time_end = ? AND id = ?", + normalizedSystemId, timeStart, DateTimeOffset.MaxValue, frontId)); + frontBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_end_time WHERE user_id = ? AND time_end = ? AND time_start = ? AND id = ?", + normalizedSystemId, DateTimeOffset.MaxValue, timeStart, frontId)); + } + await session.ExecuteAsync(frontBatch); + } + + if (journalEntryRows.Any()) + { + var journalBatch = new BatchStatement(); + foreach (var row in journalEntryRows) + { + var journalId = row.GetValue("id"); + journalBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_journals WHERE user_id = ? AND id = ? AND alter_id = ?", + normalizedSystemId, journalId, alterIdShort)); + journalBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ? AND id = ?", + normalizedSystemId, alterIdShort, journalId)); + } + await session.ExecuteAsync(journalBatch); + } + + // Clear primary_front_alter if it points at the alter we're deleting. + var currentPrimary = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + if (currentPrimary == new AlterId(alterIdShort)) + { + await session.ExecuteAsync(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = null WHERE id = ?", + normalizedSystemId)); + } + + if (membershipRows.Any()) + { + var tagBatch = new BatchStatement(); + foreach (var row in membershipRows) + { + var tagId = row.GetValue("tag_id"); + tagBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ? AND alter_id = ?", + normalizedSystemId, tagId, alterIdShort)); + } + // Single-partition delete on alter_tags_by_alter. + tagBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, alterIdShort)); + await session.ExecuteAsync(tagBatch); + } + + if (globalJournalAlterRows.Any()) + { + var gjaBatch = new BatchStatement(); + foreach (var row in globalJournalAlterRows) + { + gjaBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ? AND alter_id = ?", + normalizedSystemId, + row.GetValue("global_journal_id"), + alterIdShort)); + } + await session.ExecuteAsync(gjaBatch); + } + + if (!string.IsNullOrWhiteSpace(alias)) + { + await session.ExecuteAsync(new SimpleStatement( + $"DELETE FROM {keyspace}.alters_by_alias WHERE user_id = ? AND alias = ?", + normalizedSystemId, alias)); + } + + return true; + }, cancellationToken); + } + + public async Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + EnsureAlterFieldUdtMapping(session, keyspace); + + var query = new SimpleStatement( + $"SELECT id, name, alias, fields, security_level, color, pronouns, avatar_url, avatar_source, pinned, archived, untracked, description, proxy_name, discord_proxies, inserted_at, updated_at FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId + ); + + var rows = await session.ExecuteAsync(query); + return rows + .Select(row => AlterRowMappers.MapAlterReadModel(row, definitions)) + .OrderBy(x => x.Id.Value) + .ToArray(); + }, cancellationToken); + } + + public async Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var result = await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var friendshipLevel = await ScyllaSharedQueries.ResolveFriendshipLevelAsync(session, _keyspaceResolver, new(normalizedSystemId), viewerSystemId, _logger); + EnsureAlterFieldUdtMapping(session, keyspace); + var definitions = await _alterFieldDefinitions.ListVisibleAsync(systemId, friendshipLevel, cancellationToken); + + var query = new SimpleStatement( + $"SELECT id, name, avatar_url, avatar_source, color, description, pronouns, pinned, security_level, fields FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId + ); + + var rows = (await session.ExecuteAsync(query)).ToArray(); + var visible = rows + .Where(row => row.GetValue("security_level").FromStorage().CanBeViewedBy(friendshipLevel)) + .Select(row => AlterRowMappers.MapBareAlter(row, definitions)) + .OrderBy(x => x.Id.Value) + .ToArray(); + + return (Total: rows.Length, Visible: (IReadOnlyList)visible, NormalizedSystemId: normalizedSystemId); + }, cancellationToken); + + GuardedInstrumentation.RecordList(_logger, "alter", nameof(ListGuardedAsync), viewerSystemId, result.NormalizedSystemId, result.Total, result.Visible.Count, sw.Elapsed.TotalMilliseconds); + return result.Visible; + } + + public async Task GetAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + EnsureAlterFieldUdtMapping(session, keyspace); + + var query = new SimpleStatement( + $"SELECT id, name, alias, fields, security_level, color, pronouns, avatar_url, avatar_source, pinned, archived, untracked, description, proxy_name, discord_proxies, inserted_at, updated_at FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + alterId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null + ? null + : AlterRowMappers.MapAlterReadModel(row, definitions); + }, cancellationToken); + } + + public async Task GetGuardedAsync( + SystemId systemId, + AlterId alterId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var result = await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var friendshipLevel = await ScyllaSharedQueries.ResolveFriendshipLevelAsync(session, _keyspaceResolver, new(normalizedSystemId), viewerSystemId, _logger); + EnsureAlterFieldUdtMapping(session, keyspace); + var definitions = await _alterFieldDefinitions.ListVisibleAsync(systemId, friendshipLevel, cancellationToken); + + var query = new SimpleStatement( + $"SELECT id, name, avatar_url, avatar_source, description, color, pronouns, pinned, security_level, fields FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + alterId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return (Alter: (BareAlter?)null, Filtered: false, NormalizedSystemId: normalizedSystemId); + } + + var securityLevel = row.GetValue("security_level").FromStorage(); + if (!securityLevel.CanBeViewedBy(friendshipLevel)) + { + return (Alter: (BareAlter?)null, Filtered: true, NormalizedSystemId: normalizedSystemId); + } + + return (Alter: (BareAlter?)AlterRowMappers.MapBareAlter(row, definitions), Filtered: false, NormalizedSystemId: normalizedSystemId); + }, cancellationToken); + + GuardedInstrumentation.RecordGet(_logger, "alter", nameof(GetGuardedAsync), viewerSystemId, result.NormalizedSystemId, alterId.Value.ToString(), found: result.Alter is not null, filtered: result.Filtered, sw.Elapsed.TotalMilliseconds); + return result.Alter; + } + + public async Task AliasTakenByOtherAsync( + SystemId systemId, + AlterId alterId, + string alias, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, alias FROM {keyspace}.alters WHERE user_id = ? AND alias = ?", + normalizedSystemId, + alias + ); + + var rows = await session.ExecuteAsync(query); + return rows.Any(row => row.GetValue("id") != alterId.Value); + }, cancellationToken); + } + + public static void EnsureAlterFieldUdtMapping(ISession session, string keyspace) + { + var key = (System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(session.Cluster), keyspace); + if (UdtMappings.ContainsKey(key)) + { + return; + } + + session.UserDefinedTypes.Define( + UdtMap.For("alter_field", keyspace) + .Map(f => f.Id, "id") + .Map(f => f.Value, "value")); + + UdtMappings.TryAdd(key, 0); + } + +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaEncryptionStateRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaEncryptionStateRepository.cs new file mode 100644 index 00000000..f3b15eac --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaEncryptionStateRepository.cs @@ -0,0 +1,83 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Settings.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaEncryptionStateRepository : IEncryptionStateRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + + public ScyllaEncryptionStateRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + } + + public async Task GetAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Bind the raw string — wrapper structs have no driver serializer and user-defined + // implicit conversions don't fire through the params-object[] boxing path. + var query = new SimpleStatement( + $"SELECT encryption_initialized, encryption_key_checksum, salt FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null + ? null + : new EncryptionState( + row.GetValue("encryption_initialized") ?? false, + KeyChecksum.FromNullable(row.GetValue("encryption_key_checksum")), + EncryptionSalt.FromNullable(row.GetValue("salt"))); + }, cancellationToken); + } + + public async Task UpsertAsync(SystemId systemId, bool initialized, KeyChecksum? keyChecksum, EncryptionSalt? salt, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + SimpleStatement statement; + if (salt is not { } newSalt) + { + statement = new SimpleStatement( + $"UPDATE {keyspace}.users SET encryption_initialized = ?, encryption_key_checksum = ?, updated_at = toTimestamp(now()) WHERE id = ?", + initialized, + keyChecksum?.Value, + normalizedSystemId + ); + } + else + { + statement = new SimpleStatement( + $"UPDATE {keyspace}.users SET encryption_initialized = ?, encryption_key_checksum = ?, salt = ?, updated_at = toTimestamp(now()) WHERE id = ?", + initialized, + keyChecksum?.Value, + newSalt.Value, + normalizedSystemId + ); + } + + await session.ExecuteAsync(statement); + return true; + }, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaExistsQueries.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaExistsQueries.cs new file mode 100644 index 00000000..c7e1ff98 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaExistsQueries.cs @@ -0,0 +1,18 @@ +using Cassandra; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class ScyllaExistsQueries +{ + public static async Task RowExistsAsync(ISession session, string keyspace, string table, string idColumn, string normalizedSystemId, object idValue) + { + var query = new SimpleStatement( + $"SELECT {idColumn} FROM {keyspace}.{table} WHERE user_id = ? AND {idColumn} = ? LIMIT 1", + normalizedSystemId, + idValue + ); + + var rows = await session.ExecuteAsync(query); + return rows.Any(); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipDenormalizedTable.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipDenormalizedTable.cs new file mode 100644 index 00000000..58146390 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipDenormalizedTable.cs @@ -0,0 +1,40 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class ScyllaFriendshipDenormalizedTable +{ + public static void AddInsertStatements(BatchStatement batch, string systemId, string otherSystemId, short friendLevel) + { + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friendships (user_id, friend_id, level, since, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + systemId, otherSystemId, friendLevel)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friendships (user_id, friend_id, level, since, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + otherSystemId, systemId, friendLevel)); + // Maintain friendships_by_friend_id denormalized table + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friendships_by_friend_id (friend_id, user_id, level, since, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + otherSystemId, systemId, friendLevel)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friendships_by_friend_id (friend_id, user_id, level, since, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + systemId, otherSystemId, friendLevel)); + } + + public static void AddDeleteStatements(BatchStatement batch, string systemId, string otherSystemId) + { + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ?", + systemId, otherSystemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ?", + otherSystemId, systemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friendships_by_friend_id WHERE friend_id = ? AND user_id = ?", + otherSystemId, systemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friendships_by_friend_id WHERE friend_id = ? AND user_id = ?", + systemId, otherSystemId)); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipRepository.cs new file mode 100644 index 00000000..f9d816b1 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFriendshipRepository.cs @@ -0,0 +1,695 @@ +using Cassandra; +using Interfold.Friendships.Contracts.Ids; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaFriendshipRepository : IFriendshipRepository +{ + // Derived from ScyllaKeyspace so the region list can't drift from the typed vocabulary. + private static readonly string[] CanonicalRegions = + Enum.GetValues().Select(k => k.ToWire()).ToArray(); + + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly IScyllaScopeResolver _scopeResolver; + + public ScyllaFriendshipRepository( + IScyllaSessionProvider sessionProvider, + IScyllaKeyspaceResolver keyspaceResolver, + IScyllaScopeResolver scopeResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _scopeResolver = scopeResolver; + } + + public async Task ResolveUserIdAsync(FriendLookup lookup, CancellationToken cancellationToken = default) + { + // FriendLookup arrives constrained to Kind.Id / Kind.Username by the wire boundary. + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + return await ResolveUserIdInScyllaAsync(session, lookup, cancellationToken); + }, cancellationToken); + } + + public async Task GetFriendshipLevelAsync(SystemId systemId, SystemId? viewerSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + if (string.IsNullOrWhiteSpace(viewerSystemId?.Value)) + { + return null; + } + + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedViewerSystemId = _keyspaceResolver.NormalizeSystemId(viewerSystemId.Value); + + if (normalizedSystemId == normalizedViewerSystemId) + { + return FriendshipLevel.TrustedFriend; + } + + var session = scope.Session; + var query = new SimpleStatement( + $"SELECT level FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ? LIMIT 1", + normalizedSystemId, + normalizedViewerSystemId); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null ? null : (FriendshipLevel?)row.GetValue("level").FromCode(); + }, cancellationToken); + } + + public async Task> ListFriendshipsAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync>(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var profileHydrationConcurrency = _options.HydrationMaxConcurrency; + + var query = new SimpleStatement( + $"SELECT friend_id, level, since FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ?", + normalizedSystemId); + + var rows = await session.ExecuteAsync(query); + var result = await ConcurrentProjection.SelectWithConcurrencyAsync( + rows, + profileHydrationConcurrency, + async row => + { + SystemId friendId = new(row.GetValue("friend_id")); + var level = row.GetValue("level").FromCode(); + var since = row.GetValue("since") ?? DateTimeOffset.UtcNow; + + var profileTask = GetFriendProfileAsync(session, friendId); + var frontingTask = GetFrontingAsync(session, friendId, new(normalizedSystemId)); + await Task.WhenAll(profileTask, frontingTask); + + return new FriendshipReadModel( + await profileTask, + new FriendshipModel(level, since), + await frontingTask); + }, + cancellationToken); + + return result.OrderByDescending(x => x.Friendship.Since).ToList(); + }, cancellationToken); + } + + public async Task GetFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedFriendSystemId = _keyspaceResolver.NormalizeSystemId(friendSystemId); + + var query = new SimpleStatement( + $"SELECT friend_id, level, since FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ? LIMIT 1", + normalizedSystemId, + normalizedFriendSystemId); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return null; + } + + var since = row.GetValue("since") ?? DateTimeOffset.UtcNow; + var level = row.GetValue("level").FromCode(); + var profile = await GetFriendProfileAsync(session, new(normalizedFriendSystemId)); + var fronting = await GetFrontingAsync(session, new(normalizedFriendSystemId), new(normalizedSystemId)); + + return new FriendshipReadModel( + profile, + new FriendshipModel(level, since), + fronting); + }, cancellationToken); + } + + public async Task RemoveFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedFriendId = _keyspaceResolver.NormalizeSystemId(friendSystemId); + + var exists = await ExistsFriendshipAsync(session, normalizedSystemId, normalizedFriendId); + if (!exists) + { + return false; + } + + var removeBatch = new BatchStatement(); + ScyllaFriendshipDenormalizedTable.AddDeleteStatements(removeBatch, normalizedSystemId, normalizedFriendId); + await session.ExecuteAsync(removeBatch); + + return true; + }, cancellationToken); + } + + public async Task SetTrustedAsync(SystemId systemId, SystemId friendSystemId, bool trusted, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedFriendSystemId = _keyspaceResolver.NormalizeSystemId(friendSystemId); + + var exists = await ExistsFriendshipAsync(session, normalizedSystemId, normalizedFriendSystemId); + if (!exists) + { + return false; + } + + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"UPDATE {ScyllaGlobalKeyspace.Name}.friendships SET level = ? WHERE user_id = ? AND friend_id = ?", + (short)(trusted ? FriendshipLevel.TrustedFriend : FriendshipLevel.Friend), + normalizedSystemId, + normalizedFriendSystemId)); + batch.Add(new SimpleStatement( + $"UPDATE {ScyllaGlobalKeyspace.Name}.friendships_by_friend_id SET level = ? WHERE friend_id = ? AND user_id = ?", + (short)(trusted ? FriendshipLevel.TrustedFriend : FriendshipLevel.Friend), + normalizedFriendSystemId, + normalizedSystemId)); + await session.ExecuteAsync(batch); + + return true; + }, cancellationToken); + } + + public async Task GetFriendRequestsAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var profileHydrationConcurrency = _options.HydrationMaxConcurrency; + + var incomingTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT from_id, date_sent FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ?", + normalizedSystemId)); + + var outgoingTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT to_id, date_sent FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ?", + normalizedSystemId)); + + await Task.WhenAll(incomingTask, outgoingTask); + + var incomingRows = await incomingTask; + var outgoingRows = await outgoingTask; + + var incoming = await ConcurrentProjection.SelectWithConcurrencyAsync( + incomingRows, + profileHydrationConcurrency, + async row => + { + SystemId sourceSystemId = new(row.GetValue("from_id")); + var profile = await GetFriendProfileAsync(session, sourceSystemId); + return new FriendRequestReadModel(profile, new FriendshipRequestModel(row.GetValue("date_sent") ?? DateTimeOffset.UtcNow)); + }, + cancellationToken); + + var outgoing = await ConcurrentProjection.SelectWithConcurrencyAsync( + outgoingRows, + profileHydrationConcurrency, + async row => + { + SystemId targetSystemId = new(row.GetValue("to_id")); + var profile = await GetFriendProfileAsync(session, targetSystemId); + return new FriendRequestReadModel(profile, new FriendshipRequestModel(row.GetValue("date_sent") ?? DateTimeOffset.UtcNow)); + }, + cancellationToken); + + return new FriendRequestIndexReadModel( + incoming.OrderByDescending(x => x.Request.DateSent).ToList(), + outgoing.OrderByDescending(x => x.Request.DateSent).ToList()); + }, cancellationToken); + } + + public async Task SendRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedTargetSystemId = _keyspaceResolver.NormalizeSystemId(targetSystemId); + + if (await ResolveUserIdInScyllaAsync(session, normalizedTargetSystemId, cancellationToken) is not { } resolvedTargetUserId) + { + return SendFriendRequestOutcome.NoUser; + } + normalizedTargetSystemId = resolvedTargetUserId.Value; + + if (await ExistsFriendshipAsync(session, normalizedSystemId, normalizedTargetSystemId)) + { + return SendFriendRequestOutcome.AlreadyFriends; + } + + if (await ExistsRequestAsync(session, normalizedSystemId, normalizedTargetSystemId)) + { + return SendFriendRequestOutcome.AlreadySent; + } + + if (await ExistsRequestAsync(session, normalizedTargetSystemId, normalizedSystemId)) + { + await LinkFriendsAndClearRequestsAsync(session, normalizedSystemId, normalizedTargetSystemId); + return SendFriendRequestOutcome.Accepted; + } + + await CreateRequestAsync(session, normalizedSystemId, normalizedTargetSystemId); + return SendFriendRequestOutcome.Sent; + }, cancellationToken); + } + + public async Task AcceptRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedSourceSystemId = _keyspaceResolver.NormalizeSystemId(sourceSystemId); + + if (await ResolveUserIdInScyllaAsync(session, normalizedSourceSystemId, cancellationToken) is not { } resolvedSourceUserId) + { + return FriendRequestMutationOutcome.NoUser; + } + normalizedSourceSystemId = resolvedSourceUserId.Value; + + if (await ExistsFriendshipAsync(session, normalizedSystemId, normalizedSourceSystemId)) + { + return FriendRequestMutationOutcome.AlreadyFriends; + } + + if (!await ExistsRequestAsync(session, normalizedSourceSystemId, normalizedSystemId)) + { + return FriendRequestMutationOutcome.NotRequested; + } + + await LinkFriendsAndClearRequestsAsync(session, normalizedSystemId, normalizedSourceSystemId); + return FriendRequestMutationOutcome.Ok; + }, cancellationToken); + } + + public async Task RejectRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedSourceSystemId = _keyspaceResolver.NormalizeSystemId(sourceSystemId); + + if (await ResolveUserIdInScyllaAsync(session, normalizedSourceSystemId, cancellationToken) is not { } resolvedSourceUserId) + { + return FriendRequestMutationOutcome.NoUser; + } + normalizedSourceSystemId = resolvedSourceUserId.Value; + + if (await ExistsFriendshipAsync(session, normalizedSystemId, normalizedSourceSystemId)) + { + return FriendRequestMutationOutcome.AlreadyFriends; + } + + if (!await ExistsRequestAsync(session, normalizedSourceSystemId, normalizedSystemId)) + { + return FriendRequestMutationOutcome.NotRequested; + } + + await DeleteRequestAsync(session, normalizedSourceSystemId, normalizedSystemId); + return FriendRequestMutationOutcome.Ok; + }, cancellationToken); + } + + public async Task CancelRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var normalizedTargetSystemId = _keyspaceResolver.NormalizeSystemId(targetSystemId); + + if (await ResolveUserIdInScyllaAsync(session, normalizedTargetSystemId, cancellationToken) is not { } resolvedTargetUserId) + { + return FriendRequestMutationOutcome.NoUser; + } + normalizedTargetSystemId = resolvedTargetUserId.Value; + + if (await ExistsFriendshipAsync(session, normalizedSystemId, normalizedTargetSystemId)) + { + return FriendRequestMutationOutcome.AlreadyFriends; + } + + if (!await ExistsRequestAsync(session, normalizedSystemId, normalizedTargetSystemId)) + { + return FriendRequestMutationOutcome.NotRequested; + } + + await DeleteRequestAsync(session, normalizedSystemId, normalizedTargetSystemId); + return FriendRequestMutationOutcome.Ok; + }, cancellationToken); + } + + public async Task> DeleteAllForSystemAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync>(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + + var friendsTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT friend_id FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ?", + normalizedSystemId)); + + var outgoingRequestsTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT to_id FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ?", + normalizedSystemId)); + + var incomingRequestsTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT from_id FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ?", + normalizedSystemId)); + + await Task.WhenAll(friendsTask, outgoingRequestsTask, incomingRequestsTask); + + var friendRows = await friendsTask; + var outgoingRows = await outgoingRequestsTask; + var incomingRows = await incomingRequestsTask; + + var batch = new BatchStatement(); + var friendIds = new List(); + foreach (var row in friendRows) + { + SystemId friendId = new(row.GetValue("friend_id")); + friendIds.Add(friendId); + ScyllaFriendshipDenormalizedTable.AddDeleteStatements(batch, normalizedSystemId, friendId.Value); + } + + foreach (var row in outgoingRows) + { + var targetId = row.GetValue("to_id"); + batch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ?", normalizedSystemId, targetId)); + batch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ? AND from_id = ?", targetId, normalizedSystemId)); + } + + foreach (var row in incomingRows) + { + var sourceId = row.GetValue("from_id"); + batch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ?", sourceId, normalizedSystemId)); + batch.Add(new SimpleStatement($"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ? AND from_id = ?", normalizedSystemId, sourceId)); + } + + if (!batch.IsEmpty) + { + await session.ExecuteAsync(batch); + } + + return (IReadOnlyList)friendIds.ToArray(); + }, cancellationToken); + } + + + private static Task ExistsFriendshipAsync(ISession session, string userId, string friendId) + => ScyllaExistsQueries.RowExistsAsync(session, ScyllaGlobalKeyspace.Name, "friendships", "friend_id", userId, friendId); + + private async Task ResolveUserIdInScyllaAsync( + ISession session, + string input, + CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(input)) + { + return null; + } + + input = input.Trim(); + + // Send/Accept/Reject/Cancel pass post-normalisation bare ids which parse as Kind.Id. + // Anything that slips past FriendLookup falls through to user_registry.user_id with + // the whole input, matching the region-context fallback. + if (!FriendLookup.TryParse(input, provider: null, out var handle)) + { + return await LookupByUserIdAsync(session, input); + } + + return handle.Kind switch + { + FriendLookupKind.Username => await LookupByUsernameFanoutAsync(session, handle.Value), + FriendLookupKind.Id => await LookupByUserIdAsync(session, handle.Value), + // A new FriendLookupKind needs an explicit routing lane; silently falling + // through would mis-route and produce phantom nulls. + _ => throw new ArgumentOutOfRangeException(nameof(handle), handle.Kind, + $"Unhandled FriendLookupKind '{handle.Kind}' in ResolveUserIdInScyllaAsync."), + }; + } + + private Task ResolveUserIdInScyllaAsync( + ISession session, + FriendLookup lookup, + CancellationToken cancellationToken) + => ResolveUserIdInScyllaAsync(session, lookup.OriginalValue, cancellationToken); + + private static async Task LookupByUserIdAsync(ISession session, string userId) + { + var directQuery = new SimpleStatement( + $"SELECT user_id FROM {ScyllaGlobalKeyspace.Name}.user_registry WHERE user_id = ? LIMIT 1", + userId); + var directRow = (await session.ExecuteAsync(directQuery)).FirstOrDefault(); + return directRow is null + ? null + : new SystemId(directRow.GetValue("user_id")); + } + + private static async Task LookupByUsernameFanoutAsync(ISession session, string username) + { + var existingKeyspaces = await GetExistingRegionalKeyspacesAsync(session); + + // users_by_username is per-region with no global reverse index; skip + // unavailable/missing keyspaces so a partial cluster still resolves what it can. + foreach (var region in existingKeyspaces.Where(CanonicalRegions.Contains)) + { + try + { + var userQuery = new SimpleStatement( + $"SELECT user_id FROM {region}.users_by_username WHERE username = ? LIMIT 1", + username); + var userRow = (await session.ExecuteAsync(userQuery)).FirstOrDefault(); + if (userRow != null) + { + return new(userRow.GetValue("user_id")); + } + } + catch (UnavailableException) + { + continue; + } + catch (InvalidQueryException) + { + continue; + } + } + + return null; + } + + private static async Task> GetExistingRegionalKeyspacesAsync(ISession session) + { + var keyspacesQuery = new SimpleStatement("SELECT keyspace_name FROM system_schema.keyspaces"); + var keyspaceRows = await session.ExecuteAsync(keyspacesQuery); + + return keyspaceRows + .Select(row => row.GetValue("keyspace_name").ToLowerInvariant()) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + } + + private static async Task ExistsRequestAsync(ISession session, string fromId, string toId) + { + var query = new SimpleStatement( + $"SELECT to_id FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ? LIMIT 1", + fromId, + toId); + + return (await session.ExecuteAsync(query)).Any(); + } + + private static async Task CreateRequestAsync(ISession session, string fromId, string toId) + { + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friend_requests (from_id, to_id, date_sent, inserted_at, updated_at) VALUES (?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + fromId, toId)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id (to_id, from_id, date_sent, inserted_at, updated_at) VALUES (?, ?, toTimestamp(now()), toTimestamp(now()), toTimestamp(now()))", + toId, fromId)); + await session.ExecuteAsync(batch); + } + + private static async Task DeleteRequestAsync(ISession session, string fromId, string toId) + { + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ?", + fromId, toId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ? AND from_id = ?", + toId, fromId)); + await session.ExecuteAsync(batch); + } + + private static async Task LinkFriendsAndClearRequestsAsync(ISession session, string systemId, string otherSystemId) + { + var friendLevel = (short)FriendshipLevel.Friend; + var batch = new BatchStatement(); + ScyllaFriendshipDenormalizedTable.AddInsertStatements(batch, systemId, otherSystemId, friendLevel); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ?", + systemId, otherSystemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests WHERE from_id = ? AND to_id = ?", + otherSystemId, systemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ? AND from_id = ?", + otherSystemId, systemId)); + batch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.friend_requests_by_to_id WHERE to_id = ? AND from_id = ?", + systemId, otherSystemId)); + await session.ExecuteAsync(batch); + } + + private async Task GetFriendProfileAsync(ISession session, SystemId friendSystemId) + { + var resolvedRegion = await ResolveUserRegionAsync(session, friendSystemId); + if (resolvedRegion is not { } typedRegion) + { + return new FriendProfileReadModel(friendSystemId, null, null, null, null, null); + } + + var region = typedRegion.ToWire(); + var profileQuery = new SimpleStatement( + $"SELECT username, avatar_url, avatar_source, description, discord_id FROM {region}.users WHERE id = ? LIMIT 1", + friendSystemId.Value); + + var profileRow = (await session.ExecuteAsync(profileQuery)).FirstOrDefault(); + + return new FriendProfileReadModel( + friendSystemId, + profileRow?.GetValue("username") is { } username ? new Username(username) : null, + AvatarUrl.FromNullable(profileRow?.GetValue("avatar_url")), + profileRow is not null ? profileRow.GetValue("avatar_source").FromCodeOrNull() : null, + profileRow?.GetValue("description"), + profileRow?.GetValue("discord_id") is { } discordId ? new DiscordId(discordId) : null); + } + + private async Task> GetFrontingAsync(ISession session, SystemId friendSystemId, SystemId viewerSystemId) + { + var resolvedRegion = await ResolveUserRegionAsync(session, friendSystemId); + if (resolvedRegion is not { } typedRegion) + { + return []; + } + + var regionalKeyspace = typedRegion.ToWire(); + + // Friend's own view of the friendship — they own their alter visibility. + var levelTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT level FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ? LIMIT 1", + friendSystemId.Value, + viewerSystemId.Value)); + var activeTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id, comment FROM {regionalKeyspace}.current_fronts WHERE user_id = ?", + friendSystemId.Value)); + + var altersTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id, name, avatar_url, avatar_source, pronouns, color, description, extra_images, security_level FROM {regionalKeyspace}.alters WHERE user_id = ?", + friendSystemId.Value)); + + await Task.WhenAll(levelTask, activeTask, altersTask); + + var levelRow = (await levelTask).FirstOrDefault(); + FriendshipLevel? friendshipLevel = levelRow is null ? null : levelRow.GetValue("level").FromCode(); + var activeRows = await activeTask; + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, regionalKeyspace, friendSystemId.Value); + var alterRows = await altersTask; + + var alterMap = alterRows + .Where(row => CanViewAlter(friendshipLevel, row.GetValue("security_level"))) + .ToDictionary( + row => row.GetValue("id"), + row => ( + Name: row.GetValue("name"), + AvatarUrl: AvatarUrl.FromNullable(row.GetValue("avatar_url")), + AvatarSource: row.GetValue("avatar_source").FromCodeOrNull(), + Pronouns: row.GetValue("pronouns"), + Color: HexColor.FromNullable(row.GetValue("color")), + Description: row.GetValue("description"), + ExtraImages: (IReadOnlyList)(row.GetValue?>("extra_images")?.Select(url => new AvatarUrl(url)).ToList() ?? []))); + + return activeRows + .Select(row => + { + var alterIdShort = row.GetValue("alter_id"); + var alterId = new AlterId(alterIdShort); + if (!alterMap.TryGetValue(alterIdShort, out var alter)) + { + return null; + } + return new FriendFrontingReadModel( + new FriendFrontingAlterReadModel( + alterId, + alter.Name, + alter.Pronouns, + alter.Description, + [], + alter.AvatarUrl, + alter.AvatarSource, + alter.ExtraImages, + alter.Color), + new FriendFrontingFrontReadModel(alterId, row.GetValue("comment")), + primaryAlterId == alterId); + }) + .Where(x => x is not null) + .OrderBy(x => x!.Alter.Id.Value) + .ToList()!; + } + + private static bool CanViewAlter(FriendshipLevel? friendshipLevel, short? securityLevel) + => securityLevel.FromStorage().CanBeViewedBy(friendshipLevel); + + // Returns null when the registry row is absent or carries an unknown region value — + // callers treat both as "profile unavailable" rather than guessing a keyspace. + private static async Task ResolveUserRegionAsync(ISession session, SystemId userId) + { + var regionQuery = new SimpleStatement( + $"SELECT region FROM {ScyllaGlobalKeyspace.Name}.user_registry WHERE user_id = ? LIMIT 1", + userId.Value); + + var row = (await session.ExecuteAsync(regionQuery)).FirstOrDefault(); + var raw = row?.GetValue("region"); + if (string.IsNullOrWhiteSpace(raw)) + { + return null; + } + + try + { + return EnumWireExtensions.ParseScyllaKeyspace(raw); + } + catch (InvalidOperationException) + { + return null; + } + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingDenormalizedTable.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingDenormalizedTable.cs new file mode 100644 index 00000000..e77f6078 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingDenormalizedTable.cs @@ -0,0 +1,73 @@ +using Cassandra; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class ScyllaFrontingDenormalizedTable +{ + public static void AddFrontCloseStatements( + BatchStatement batch, + string keyspace, + string systemId, + Guid frontId, + short alterId, + DateTimeOffset startedAt, + DateTimeOffset endedAt, + string? comment, + AlterId? primaryAlterId) + { + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.fronts SET time_end = ?, updated_at = ? WHERE user_id = ? AND id = ? AND time_start = ?", + endedAt, + endedAt, + systemId, + frontId, + startedAt)); + + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ?", + systemId, + alterId)); + + // Maintain fronts_by_alter (update time_end) + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.fronts_by_alter SET time_end = ?, updated_at = ? WHERE user_id = ? AND alter_id = ? AND id = ? AND time_start = ?", + endedAt, + endedAt, + systemId, + alterId, + frontId, + startedAt)); + + // Insert into fronts_by_time (only on close) + batch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.fronts_by_time (user_id, time_start, time_end, id, alter_id, comment, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + systemId, + startedAt, + endedAt, + frontId, + alterId, + comment, + endedAt, + endedAt)); + + // Insert into fronts_by_end_time (only on close) + batch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.fronts_by_end_time (user_id, time_end, time_start, id, alter_id, comment, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + systemId, + endedAt, + startedAt, + frontId, + alterId, + comment, + endedAt, + endedAt)); + + if (primaryAlterId == new AlterId(alterId)) + { + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = null, updated_at = toTimestamp(now()) WHERE id = ?", + systemId)); + } + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingRepository.cs new file mode 100644 index 00000000..38e3524d --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaFrontingRepository.cs @@ -0,0 +1,569 @@ +using System.Diagnostics; +using Cassandra; +using Interfold.Alters.Contracts.Models; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using static Interfold.Infrastructure.Scylla.Repository.ScyllaAlterRepository; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaFrontingRepository : IFrontingRepository +{ + private sealed record CurrentFrontRow(Guid FrontId, short AlterId, DateTimeOffset StartedAt, string? Comment); + + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly ILogger _logger; + private readonly ISettingsFieldRepository _settingsFields; + + public ScyllaFrontingRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options, + ILogger logger, + ISettingsFieldRepository settingsFields + ) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _logger = logger; + _settingsFields = settingsFields; + } + + public async Task IsFrontingAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + return await ScyllaExistsQueries.RowExistsAsync(session, keyspace, "current_fronts", "alter_id", normalizedSystemId, alterId.Value); + }, cancellationToken); + } + + public async Task StartAsync( + SystemId systemId, + AlterId alterId, + string? comment, + DateTimeOffset startedAt, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var frontGuid = Guid.NewGuid(); + + var startBatch = new BatchStatement(); + startBatch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.current_fronts (user_id, alter_id, id, comment, time_start, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + alterId.Value, + frontGuid, + comment, + startedAt, + startedAt, + startedAt + )); + startBatch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.fronts (user_id, id, alter_id, comment, time_start, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + frontGuid, + alterId.Value, + comment, + startedAt, + startedAt, + startedAt + )); + startBatch.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.fronts_by_alter (user_id, alter_id, id, comment, time_start, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + alterId.Value, + frontGuid, + comment, + startedAt, + startedAt, + startedAt + )); + // fronts_by_time / fronts_by_end_time land on close, not on start. + await session.ExecuteAsync(startBatch); + return new(frontGuid); + }, cancellationToken); + } + + public async Task EndAsync(SystemId systemId, AlterId alterId, DateTimeOffset endedAt, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var current = await GetCurrentFrontRowAsync(session, keyspace, normalizedSystemId, alterId); + if (current is null) + { + return false; + } + + // Prefetch primary_front_alter — closing it also clears the primary. + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + + var endBatch = new BatchStatement(); + ScyllaFrontingDenormalizedTable.AddFrontCloseStatements( + endBatch, + keyspace, + normalizedSystemId, + current.FrontId, + alterId.Value, + current.StartedAt, + endedAt, + current.Comment, + primaryAlterId); + + await session.ExecuteAsync(endBatch); + + return true; + }, cancellationToken); + } + + public async Task SetPrimaryAsync(SystemId systemId, AlterId? alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + if (alterId is { } id) + { + var frontingRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ? LIMIT 1", + normalizedSystemId, + id.Value))).FirstOrDefault(); + if (frontingRow is null) + { + return false; + } + + await session.ExecuteAsync(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = ?, updated_at = toTimestamp(now()) WHERE id = ?", + id.Value, + normalizedSystemId)); + + return true; + } + + await session.ExecuteAsync(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = null, updated_at = toTimestamp(now()) WHERE id = ?", + normalizedSystemId)); + + return true; + }, cancellationToken); + } + + public async Task> ListActiveAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Register UDT mapping before SELECTing the fields UDT column. + EnsureAlterFieldUdtMapping(session, keyspace); + + var activeTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id, id, comment, time_start FROM {keyspace}.current_fronts WHERE user_id = ?", + normalizedSystemId + )); + + var altersTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id, name, avatar_url, avatar_source, description, color, fields, pronouns, pinned FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId)); + + await Task.WhenAll(activeTask, altersTask); + + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + + var rows = await activeTask; + var alterRows = await altersTask; + + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + + var alterById = alterRows.ToDictionary( + row => new AlterId(row.GetValue("id")), + row => AlterRowMappers.MapBareAlter(row, definitions)); + + return (IReadOnlyList)rows + .Select(row => + { + FrontId frontId = new(row.GetValue("id")); + AlterId alterId = new(row.GetValue("alter_id")); + var timeStart = row.GetValue("time_start") ?? DateTimeOffset.UtcNow; + var front = FrontingRowMappers.MapFrontHistoryReadModel(row, new(normalizedSystemId)); + + if (!alterById.TryGetValue(alterId, out var alter)) + { + alter = BareAlter.CreatePlaceholder(alterId); + } + + return new FrontActiveReadModel(alter, front, primaryAlterId == alterId); + }) + .OrderByDescending(x => x.Front.TimeStart) + .ToArray(); + }, cancellationToken); + } + + public async Task> ListActiveGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var result = await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var friendshipLevel = await ScyllaSharedQueries.ResolveFriendshipLevelAsync(session, _keyspaceResolver, new(normalizedSystemId), viewerSystemId, _logger); + + var all = await ListActiveAsync(systemId, cancellationToken); + if (all.Count == 0) + { + return (Total: 0, Visible: (IReadOnlyList)all, NormalizedSystemId: normalizedSystemId); + } + + var securityRows = await session.ExecuteAsync(new SimpleStatement( + $"SELECT id, security_level FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId)); + + var visibilityByAlterId = securityRows.ToDictionary( + row => new AlterId(row.GetValue("id")), + row => row.GetValue("security_level").FromStorage()); + + var filtered = all + .Where(front => visibilityByAlterId.TryGetValue(front.Alter.Id, out var level) && level.CanBeViewedBy(friendshipLevel)) + .ToArray(); + + return (Total: all.Count, Visible: (IReadOnlyList)filtered, NormalizedSystemId: normalizedSystemId); + }, cancellationToken); + + GuardedInstrumentation.RecordList(_logger, "fronting", nameof(ListActiveGuardedAsync), viewerSystemId, result.NormalizedSystemId, result.Total, result.Visible.Count, sw.Elapsed.TotalMilliseconds); + return result.Visible; + } + + public async Task> ListHistoryBetweenAsync( + SystemId systemId, + DateTimeOffset startInclusive, + DateTimeOffset endInclusive, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var historyQuery = new SimpleStatement( + $"SELECT id, alter_id, comment, time_start, time_end FROM {keyspace}.fronts_by_time WHERE user_id = ? AND time_start >= ? AND time_start <= ?", + normalizedSystemId, + startInclusive, + endInclusive + ); + + var historicalRows = await session.ExecuteAsync(historyQuery); + var historical = historicalRows + .Select(row => FrontingRowMappers.MapFrontHistoryReadModel(row, new(normalizedSystemId), row.GetValue("time_end"))) + .Where(x => x.TimeEnd != null) + .ToList(); + + return (IReadOnlyList)historical + .DistinctBy(x => x.Id) + .OrderByDescending(x => x.TimeStart) + .ToArray(); + }, cancellationToken); + } + + public async Task> ListAllAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Base `fronts` partition key = user_id → no ALLOW FILTERING needed. Corrupt rows + // (null time_start) fall through the row-mapper's DateTimeOffset.UtcNow fallback; + // drop them here before they leak into export output. + var query = new SimpleStatement( + $"SELECT id, alter_id, comment, time_start, time_end FROM {keyspace}.fronts WHERE user_id = ?", + normalizedSystemId); + + var rows = await session.ExecuteAsync(query); + return (IReadOnlyList)rows + .Where(row => row.GetValue("time_start") is not null) + .Select(row => FrontingRowMappers.MapFrontHistoryReadModel(row, new(normalizedSystemId), row.GetValue("time_end"))) + .DistinctBy(x => x.Id) + .OrderByDescending(x => x.TimeStart) + .ToArray(); + }, cancellationToken); + } + + public async Task GetActiveByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var frontRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.fronts WHERE user_id = ? AND id = ? LIMIT 1 ALLOW FILTERING", + normalizedSystemId, + frontId.Value))).FirstOrDefault(); + + if (frontRow is null) + return null; + + var alterId = frontRow.GetValue("alter_id"); + + EnsureAlterFieldUdtMapping(session, keyspace); + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken); + + var currentTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id, alter_id, comment, time_start FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ? LIMIT 1", + normalizedSystemId, alterId)); + + var alterTask = session.ExecuteAsync(new SimpleStatement( + $"SELECT id, name, avatar_url, avatar_source, description, color, fields, pronouns, pinned FROM {keyspace}.alters WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, alterId)); + + await Task.WhenAll(currentTask, alterTask); + + var currentRow = (await currentTask).FirstOrDefault(); + if (currentRow is null || currentRow.GetValue("id") != frontId.Value) + return null; + + var alterRow = (await alterTask).FirstOrDefault(); + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + + BareAlter alter; + if (alterRow is null) + { + alter = BareAlter.CreatePlaceholder(new(alterId)); + } + else + { + alter = AlterRowMappers.MapBareAlter(alterRow, definitions); + } + + var front = FrontingRowMappers.MapFrontHistoryReadModel(currentRow, new(normalizedSystemId)); + + return new FrontActiveReadModel(alter, front, primaryAlterId == new AlterId(alterId)); + }, cancellationToken); + } + + public async Task GetHistoryEntryByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var row = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT id, alter_id, comment, time_start, time_end FROM {keyspace}.fronts WHERE user_id = ? AND id = ? LIMIT 1 ALLOW FILTERING", + normalizedSystemId, + frontId.Value))).FirstOrDefault(); + + if (row is null) + return null; + + return FrontingRowMappers.MapFrontHistoryReadModel(row, new(normalizedSystemId), row.GetValue("time_end")); + }, cancellationToken); + } + + public async Task EndByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var current = await GetActiveFrontReferenceByFrontIdAsync(session, keyspace, normalizedSystemId, frontId.Value); + if (current is null) + { + return false; + } + + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + + var endedAt = DateTimeOffset.UtcNow; + + var endBatch = new BatchStatement(); + ScyllaFrontingDenormalizedTable.AddFrontCloseStatements( + endBatch, + keyspace, + normalizedSystemId, + current.FrontId, + current.AlterId, + current.StartedAt, + endedAt, + current.Comment, + primaryAlterId); + + await session.ExecuteAsync(endBatch); + return true; + }, cancellationToken); + } + + public async Task DeleteFrontByIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var row = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id, time_start, time_end FROM {keyspace}.fronts WHERE user_id = ? AND id = ? LIMIT 1 ALLOW FILTERING", + normalizedSystemId, + frontId.Value))).FirstOrDefault(); + + if (row is null) + return false; + + var alterId = new AlterId(row.GetValue("alter_id")); + var timeStart = row.GetValue("time_start"); + var timeEnd = row.GetValue("time_end"); + var frontGuid = frontId.Value; + + var currentRow = await GetCurrentFrontRowAsync(session, keyspace, normalizedSystemId, alterId); + var primaryAlterId = await ScyllaSharedQueries.LoadPrimaryFrontAlterAsync(session, keyspace, normalizedSystemId); + + var deleteBatch = new BatchStatement(); + + if (currentRow is not null && currentRow.FrontId == frontGuid) + { + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, alterId.Value)); + + if (primaryAlterId == alterId) + { + deleteBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = null, updated_at = toTimestamp(now()) WHERE id = ?", + normalizedSystemId)); + } + } + + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts WHERE user_id = ? AND id = ? AND time_start = ?", + normalizedSystemId, frontGuid, timeStart)); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_alter WHERE user_id = ? AND alter_id = ? AND id = ? AND time_start = ?", + normalizedSystemId, alterId.Value, frontGuid, timeStart)); + // *_by_time siblings only exist for closed fronts. + if (timeEnd.HasValue) + { + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_time WHERE user_id = ? AND time_start = ? AND time_end = ? AND id = ?", + normalizedSystemId, timeStart, timeEnd.Value, frontGuid)); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.fronts_by_end_time WHERE user_id = ? AND time_end = ? AND time_start = ? AND id = ?", + normalizedSystemId, timeEnd.Value, timeStart, frontGuid)); + } + + await session.ExecuteAsync(deleteBatch); + + return true; + }, cancellationToken); + } + + public async Task UpdateCommentByFrontIdAsync(SystemId systemId, FrontId frontId, string comment, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var current = await GetActiveFrontReferenceByFrontIdAsync(session, keyspace, normalizedSystemId, frontId.Value); + if (current is null) + return false; + + var commentBatch = new BatchStatement(); + commentBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.current_fronts SET comment = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND alter_id = ?", + comment, + normalizedSystemId, + current.AlterId + )); + commentBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.fronts SET comment = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ? AND time_start = ?", + comment, + normalizedSystemId, + current.FrontId, + current.StartedAt)); + commentBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.fronts_by_alter SET comment = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND alter_id = ? AND id = ? AND time_start = ?", + comment, normalizedSystemId, current.AlterId, current.FrontId, current.StartedAt)); + await session.ExecuteAsync(commentBatch); + return true; + }, cancellationToken); + } + + private async Task GetActiveFrontReferenceByFrontIdAsync( + ISession session, + string keyspace, + string normalizedSystemId, + Guid frontId) + { + var frontRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.fronts WHERE user_id = ? AND id = ? LIMIT 1 ALLOW FILTERING", + normalizedSystemId, + frontId))).FirstOrDefault(); + + if (frontRow is null) + { + return null; + } + + var alterId = new AlterId(frontRow.GetValue("alter_id")); + var current = await GetCurrentFrontRowAsync(session, keyspace, normalizedSystemId, alterId); + if (current is null || current.FrontId != frontId) + { + return null; + } + + return current; + } + + // Raw short AlterId mirrors the CQL row shape; callers wrap in new AlterId(...) at read time. + private async Task GetCurrentFrontRowAsync( + ISession session, + string keyspace, + string normalizedSystemId, + AlterId alterId) + { + var query = new SimpleStatement( + $"SELECT id, alter_id, time_start, comment FROM {keyspace}.current_fronts WHERE user_id = ? AND alter_id = ? LIMIT 1", + normalizedSystemId, + alterId.Value); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return null; + } + + var timeStart = row.GetValue("time_start"); + if (timeStart is null) + { + // Corrupt row — refuse to silently stamp today's date into fronts_by_time on + // the next EndAsync. Treat as not-current and warn loudly. + _logger.LogWarning( + "current_fronts row for user {SystemId} alter {AlterId} has null time_start; treating as not-current.", + normalizedSystemId, alterId.Value); + return null; + } + + return new CurrentFrontRow( + row.GetValue("id"), + row.GetValue("alter_id"), + timeStart.Value, + row.GetValue("comment")); + } + +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaImportOperationRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaImportOperationRepository.cs new file mode 100644 index 00000000..6a5b30f2 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaImportOperationRepository.cs @@ -0,0 +1,294 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.Repository; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +/// Cassandra / ScyllaDB port of . Uses +/// LWT (Paxos) on active_import_by_system for the per-system claim mutex; terminal +/// transitions release the slot with DELETE … IF operation_id = ? so a stale +/// restart-sweep call can't evict a fresh in-flight operation. +/// is Guid-backed on the wire and timeuuid on disk — cast at the bind sites. +public sealed class ScyllaImportOperationRepository : IImportOperationRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly ILogger _logger; + + public ScyllaImportOperationRepository( + IScyllaSessionProvider sessionProvider, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options, + IScyllaScopeResolver scopeResolver, + ILogger logger) + { + _sessionProvider = sessionProvider; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _scopeResolver = scopeResolver; + _logger = logger; + } + + public async Task TryClaimAsync( + SystemId systemId, + ImportOperationKind kind, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + var now = DateTimeOffset.UtcNow; + var newOperationId = TimeUuid.NewId(); + var kindWire = kind.ToWire(); + + // Paxos INSERT IF NOT EXISTS — accepting a few hundred ms latency in exchange + // for a strong per-system dedupe guarantee. + var claim = new SimpleStatement( + $"INSERT INTO {keyspace}.active_import_by_system " + + "(system_id, kind, operation_id, started_at) VALUES (?, ?, ?, ?) IF NOT EXISTS", + normalizedSystemId, kindWire, newOperationId, now.UtcDateTime); + + var claimResult = await session.ExecuteAsync(claim); + var claimRow = claimResult.FirstOrDefault(); + // LWT result: one row with [applied] plus the existing column values on false. + var applied = claimRow?.GetValue("[applied]") ?? false; + + if (!applied) + { + // Collapse duplicate dispatch onto the existing operation id. + ImportOperationId existingId = new(claimRow!.GetValue("operation_id").ToGuid()); + _logger.LogInformation( + "[import-ops] Collapsed duplicate dispatch for system={SystemId} kind={Kind} onto operation_id={OperationId}.", + normalizedSystemId, kindWire, existingId); + return new ImportOperationClaim(existingId, IsNew: false); + } + + // History row is a plain INSERT — operation_id is a fresh TimeUuid so the + // (system_id, operation_id) pair cannot collide. + var historyInsert = new SimpleStatement( + $"INSERT INTO {keyspace}.import_operations " + + "(system_id, operation_id, kind, status, started_at, idempotency_key) " + + "VALUES (?, ?, ?, ?, ?, ?)", + normalizedSystemId, newOperationId, kindWire, + ImportOperationStatus.Queued.ToWire(), + now.UtcDateTime, idempotencyKey.Value); + await session.ExecuteAsync(historyInsert); + + return new ImportOperationClaim(new(newOperationId.ToGuid()), IsNew: true); + }, cancellationToken); + } + + public async Task MarkRunningAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default) + { + await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + + // Conditional update: re-pickup of a Running row is a no-op. + var update = new SimpleStatement( + $"UPDATE {keyspace}.import_operations SET status = ? " + + "WHERE system_id = ? AND operation_id = ? IF status = ?", + ImportOperationStatus.Running.ToWire(), + normalizedSystemId, (TimeUuid)operationId.Value, + ImportOperationStatus.Queued.ToWire()); + await session.ExecuteAsync(update); + return true; + }, cancellationToken); + } + + public async Task MarkSucceededAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + int alterCount, + CancellationToken cancellationToken = default) + { + await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + var now = DateTimeOffset.UtcNow; + + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.import_operations SET status = ?, finished_at = ?, alter_count = ? " + + "WHERE system_id = ? AND operation_id = ?", + ImportOperationStatus.Succeeded.ToWire(), now.UtcDateTime, alterCount, + normalizedSystemId, (TimeUuid)operationId.Value)); + await session.ExecuteAsync(batch); + + await ReleaseSlot(session, keyspace, normalizedSystemId, kind.ToWire(), (TimeUuid)operationId.Value); + return true; + }, cancellationToken); + } + + public async Task MarkFailedAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + ImportErrorCode errorCode, + string? errorMessage, + CancellationToken cancellationToken = default) + { + await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + var now = DateTimeOffset.UtcNow; + + var update = new SimpleStatement( + $"UPDATE {keyspace}.import_operations SET status = ?, finished_at = ?, error_code = ?, error_message = ? " + + "WHERE system_id = ? AND operation_id = ?", + ImportOperationStatus.Failed.ToWire(), now.UtcDateTime, errorCode.ToWire(), errorMessage, + normalizedSystemId, (TimeUuid)operationId.Value); + await session.ExecuteAsync(update); + + await ReleaseSlot(session, keyspace, normalizedSystemId, kind.ToWire(), (TimeUuid)operationId.Value); + return true; + }, cancellationToken); + } + + public async Task GetByIdAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + + var query = new SimpleStatement( + $"SELECT system_id, operation_id, kind, status, started_at, finished_at, " + + $"alter_count, error_code, error_message, idempotency_key " + + $"FROM {keyspace}.import_operations WHERE system_id = ? AND operation_id = ? LIMIT 1", + normalizedSystemId, (TimeUuid)operationId.Value); + + var rows = await session.ExecuteAsync(query); + var row = rows.FirstOrDefault(); + return row is null ? null : MapRow(row); + }, cancellationToken); + } + + public async Task GetActiveOperationIdAsync( + SystemId systemId, + ImportOperationKind kind, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var session = scope.Session; + var keyspace = scope.Keyspace; + var normalizedSystemId = scope.NormalizedSystemId; + + var query = new SimpleStatement( + $"SELECT operation_id FROM {keyspace}.active_import_by_system WHERE system_id = ? AND kind = ?", + normalizedSystemId, kind.ToWire()); + + var rows = await session.ExecuteAsync(query); + var row = rows.FirstOrDefault(); + return row is null ? (ImportOperationId?)null : new ImportOperationId(row.GetValue("operation_id").ToGuid()); + }, cancellationToken); + } + + public async Task> GetStaleRunningAsync( + TimeSpan olderThan, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync>(async scope => + { + var session = scope.Session; + var keyspace = _keyspaceResolver.DefaultKeyspace; + var cutoff = DateTimeOffset.UtcNow - olderThan; + + // ALLOW FILTERING is fine — small table, startup-only query. + var query = new SimpleStatement( + $"SELECT system_id, operation_id, kind, status, started_at, finished_at, " + + $"alter_count, error_code, error_message, idempotency_key " + + $"FROM {keyspace}.import_operations " + + "WHERE status = ? AND started_at < ? ALLOW FILTERING", + ImportOperationStatus.Running.ToWire(), cutoff.UtcDateTime); + + var rows = await session.ExecuteAsync(query); + var list = new List(); + foreach (var row in rows) + { + list.Add(MapRow(row)); + } + return (IReadOnlyList)list; + }, cancellationToken); + } + + // IF operation_id = ? guards against evicting a fresh in-flight operation that took + // the slot after our terminal transition; [applied]=false is expected for sweeps. + private static async Task ReleaseSlot( + ISession session, + string keyspace, + string normalizedSystemId, + string kind, + TimeUuid operationId) + { + var delete = new SimpleStatement( + $"DELETE FROM {keyspace}.active_import_by_system " + + "WHERE system_id = ? AND kind = ? IF operation_id = ?", + normalizedSystemId, kind, operationId); + await session.ExecuteAsync(delete); + } + + private static ImportOperationSnapshot MapRow(Row row) + { + var statusText = row.GetValue("status"); + var status = statusText.TryParseWire(out var parsed) + ? parsed + : ImportOperationStatus.Queued; + + var kindText = row.GetValue("kind"); + var kind = kindText.TryParseWire(out var parsedKind) + ? parsedKind + : throw new InvalidOperationException( + $"[import-ops] Encountered unknown kind '{kindText}' in import_operations row. " + + "Data schema drift — add the new value to ImportOperationKind before rolling this migration."); + + var startedAt = new DateTimeOffset(row.GetValue("started_at"), TimeSpan.Zero); + var finishedAtRaw = row.GetValue("finished_at"); + DateTimeOffset? finishedAt = finishedAtRaw.HasValue + ? new DateTimeOffset(finishedAtRaw.Value, TimeSpan.Zero) + : null; + + var errorCodeText = row.GetValue("error_code"); + ImportErrorCode? errorCode = errorCodeText.TryParseWire(out var parsedErrorCode) + ? parsedErrorCode + : null; + + return new ImportOperationSnapshot( + new(row.GetValue("system_id")), + new(row.GetValue("operation_id").ToGuid()), + kind, + status, + startedAt, + finishedAt, + row.GetValue("alter_count"), + errorCode, + row.GetValue("error_message"), + new(row.GetValue("idempotency_key"))); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaJournalRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaJournalRepository.cs new file mode 100644 index 00000000..96bb2c81 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaJournalRepository.cs @@ -0,0 +1,568 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Journals.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaJournalRepository : IJournalRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + + public ScyllaJournalRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + } + + public async Task CreateGlobalAsync(SystemId systemId, CreateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var entryId = Guid.NewGuid(); + + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.global_journals (user_id, id, title, content, color, pinned, locked, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + entryId, + command.Title, + null, + null, + false, + false + ); + + await session.ExecuteAsync(insert); + return new(entryId); + }, cancellationToken); + } + + public async Task ExistsGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + await ExistsGlobalCoreAsync(scope, entryId), cancellationToken); + } + + private static Task ExistsGlobalCoreAsync(ScyllaScope scope, EntryId entryId) + => ScyllaExistsQueries.RowExistsAsync(scope.Session, scope.Keyspace, "global_journals", "id", scope.NormalizedSystemId, entryId.Value); + + public async Task UpdateGlobalAsync(SystemId systemId, UpdateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsGlobalCoreAsync(scope, command.EntryId); + if (!exists) + return false; + + var updateBatch = new BatchStatement(); + + if (command.Title is not null) + { + var q = new SimpleStatement( + $"UPDATE {keyspace}.global_journals SET title = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.Title, + normalizedSystemId, + command.EntryId.Value + ); + updateBatch.Add(q); + } + + if (command.Content is not null) + { + var q = new SimpleStatement( + $"UPDATE {keyspace}.global_journals SET content = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.Content, + normalizedSystemId, + command.EntryId.Value + ); + updateBatch.Add(q); + } + + if (command.Color is { } color) + { + var q = new SimpleStatement( + $"UPDATE {keyspace}.global_journals SET color = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + color.Value, + normalizedSystemId, + command.EntryId.Value + ); + updateBatch.Add(q); + } + + if (!updateBatch.IsEmpty) + { + await session.ExecuteAsync(updateBatch); + } + + return true; + }, cancellationToken); + } + + public async Task DeleteGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsGlobalCoreAsync(scope, entryId); + if (!exists) + return false; + + var deleteBatch = new BatchStatement(); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.global_journals WHERE user_id = ? AND id = ?", + normalizedSystemId, + entryId.Value + )); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ?", + normalizedSystemId, + entryId.Value + )); + await session.ExecuteAsync(deleteBatch); + + return true; + }, cancellationToken); + } + + public Task SetGlobalLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default) + => SetGlobalFlagAsync(systemId, entryId, JournalFlag.Locked, locked, cancellationToken); + + public Task SetGlobalPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default) + => SetGlobalFlagAsync(systemId, entryId, JournalFlag.Pinned, pinned, cancellationToken); + + private async Task SetGlobalFlagAsync(SystemId systemId, EntryId entryId, JournalFlag flag, bool value, CancellationToken cancellationToken) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var exists = await ExistsGlobalCoreAsync(scope, entryId); + if (!exists) + return false; + + var upsert = new SimpleStatement( + $"UPDATE {scope.Keyspace}.global_journals SET {FlagToColumn(flag)} = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + value, + scope.NormalizedSystemId, + entryId.Value + ); + await scope.Session.ExecuteAsync(upsert); + + return true; + }, cancellationToken); + } + + public async Task AttachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsGlobalCoreAsync(scope, entryId); + if (!exists) + return false; + + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.global_journal_alters (user_id, global_journal_id, alter_id, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + entryId.Value, + alterId.Value + ); + await session.ExecuteAsync(insert); + + return true; + }, cancellationToken); + } + + public async Task DetachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var edgeExistsQuery = new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ? AND alter_id = ? LIMIT 1", + normalizedSystemId, + entryId.Value, + alterId.Value + ); + + var edgeRows = await session.ExecuteAsync(edgeExistsQuery); + if (!edgeRows.Any()) + return false; + + var delete = new SimpleStatement( + $"DELETE FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ? AND alter_id = ?", + normalizedSystemId, + entryId.Value, + alterId.Value + ); + await session.ExecuteAsync(delete); + + return true; + }, cancellationToken); + } + + public async Task CreateAlterAsync(SystemId systemId, CreateAlterJournalEntryCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var entryId = Guid.NewGuid(); + + var timestamp = command.CreatedAt.ToUniversalTime(); + + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.alter_journals (user_id, id, alter_id, title, content, color, pinned, locked, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + entryId, + command.AlterId.Value, + command.Title, + null, + null, + false, + false, + timestamp, + timestamp + ); + + var insertLookup = new SimpleStatement( + $"INSERT INTO {keyspace}.alter_journals_by_alter (user_id, alter_id, id, title, content, color, pinned, locked, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + normalizedSystemId, + command.AlterId.Value, + entryId, + command.Title, + null, + null, + false, + false, + timestamp, + timestamp + ); + + var batch = new BatchStatement(); + batch.Add(insert); + batch.Add(insertLookup); + await session.ExecuteAsync(batch); + return new(entryId); + }, cancellationToken); + } + + public async Task GetAlterRefAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + await GetAlterRefCoreAsync(scope, entryId, cancellationToken), cancellationToken); + } + + // In-scope variant used by the alter-scoped writers so we don't stack a second + // IScyllaScopeResolver retry envelope inside the outer one. + private static async Task GetAlterRefCoreAsync( + ScyllaScope scope, EntryId entryId, CancellationToken cancellationToken) + { + var query = new SimpleStatement( + $"SELECT id, alter_id FROM {scope.Keyspace}.alter_journals WHERE user_id = ? AND id = ? ALLOW FILTERING", + scope.NormalizedSystemId, + entryId.Value + ); + + var row = (await scope.Session.ExecuteAsync(query)).FirstOrDefault(); + return row is null + ? null + : new AlterJournalRef(new(row.GetValue("id")), new(row.GetValue("alter_id"))); + } + + public async Task UpdateAlterAsync(SystemId systemId, UpdateAlterJournalEntryCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var reference = await GetAlterRefCoreAsync(scope, command.EntryId, cancellationToken); + if (reference is null) + return false; + + var timestamp = command.UpdatedAt.ToUniversalTime(); + var updateBatch = new BatchStatement(); + + if (command.Title is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals SET title = ?, updated_at = ? WHERE user_id = ? AND id = ? AND alter_id = ?", + command.Title, timestamp, scope.NormalizedSystemId, reference.EntryId.Value, reference.AlterId.Value)); + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals_by_alter SET title = ?, updated_at = ? WHERE user_id = ? AND alter_id = ? AND id = ?", + command.Title, timestamp, scope.NormalizedSystemId, reference.AlterId.Value, reference.EntryId.Value)); + } + + if (command.Content is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals SET content = ?, updated_at = ? WHERE user_id = ? AND id = ? AND alter_id = ?", + command.Content, timestamp, scope.NormalizedSystemId, reference.EntryId.Value, reference.AlterId.Value)); + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals_by_alter SET content = ?, updated_at = ? WHERE user_id = ? AND alter_id = ? AND id = ?", + command.Content, timestamp, scope.NormalizedSystemId, reference.AlterId.Value, reference.EntryId.Value)); + } + + if (command.Color is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals SET color = ?, updated_at = ? WHERE user_id = ? AND id = ? AND alter_id = ?", + command.Color?.Value, timestamp, scope.NormalizedSystemId, reference.EntryId.Value, reference.AlterId.Value)); + updateBatch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals_by_alter SET color = ?, updated_at = ? WHERE user_id = ? AND alter_id = ? AND id = ?", + command.Color?.Value, timestamp, scope.NormalizedSystemId, reference.AlterId.Value, reference.EntryId.Value)); + } + + if (!updateBatch.IsEmpty) + await scope.Session.ExecuteAsync(updateBatch); + + return true; + }, cancellationToken); + } + + public async Task DeleteAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var reference = await GetAlterRefCoreAsync(scope, entryId, cancellationToken); + if (reference is null) + return false; + + var delete = new BatchStatement(); + delete.Add(new SimpleStatement( + $"DELETE FROM {scope.Keyspace}.alter_journals WHERE user_id = ? AND id = ? AND alter_id = ?", + scope.NormalizedSystemId, + reference.EntryId.Value, + reference.AlterId.Value + )); + delete.Add(new SimpleStatement( + $"DELETE FROM {scope.Keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ? AND id = ?", + scope.NormalizedSystemId, + reference.AlterId.Value, + reference.EntryId.Value + )); + await scope.Session.ExecuteAsync(delete); + + return true; + }, cancellationToken); + } + + public Task SetAlterLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default) + => SetAlterFlagAsync(systemId, entryId, JournalFlag.Locked, locked, cancellationToken); + + public Task SetAlterPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default) + => SetAlterFlagAsync(systemId, entryId, JournalFlag.Pinned, pinned, cancellationToken); + + private async Task SetAlterFlagAsync(SystemId systemId, EntryId entryId, JournalFlag flag, bool value, CancellationToken cancellationToken) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var reference = await GetAlterRefCoreAsync(scope, entryId, cancellationToken); + if (reference is null) + return false; + + var column = FlagToColumn(flag); + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals SET {column} = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ? AND alter_id = ?", + value, scope.NormalizedSystemId, reference.EntryId.Value, reference.AlterId.Value)); + batch.Add(new SimpleStatement( + $"UPDATE {scope.Keyspace}.alter_journals_by_alter SET {column} = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND alter_id = ? AND id = ?", + value, scope.NormalizedSystemId, reference.AlterId.Value, reference.EntryId.Value)); + await scope.Session.ExecuteAsync(batch); + + return true; + }, cancellationToken); + } + + public async Task> ListAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, user_id, alter_id, title, content, color, pinned, locked, inserted_at, updated_at FROM {keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterId.Value + ); + + var rows = await session.ExecuteAsync(query); + return rows + .Select(row => JournalRowMappers.MapAlterJournalReadModel(row)) + .OrderByDescending(e => e.InsertedAt) + .ToArray(); + }, cancellationToken); + } + + public async Task GetAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, user_id, alter_id, title, content, color, pinned, locked, inserted_at, updated_at FROM {keyspace}.alter_journals WHERE user_id = ? AND id = ? ALLOW FILTERING", + normalizedSystemId, + entryId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null + ? null + : JournalRowMappers.MapAlterJournalReadModel(row); + }, cancellationToken); + } + + public async Task> ListGlobalAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var entriesQuery = new SimpleStatement( + $"SELECT id, user_id, title, content, color, pinned, locked, inserted_at, updated_at FROM {keyspace}.global_journals WHERE user_id = ?", + normalizedSystemId + ); + var entryRows = await session.ExecuteAsync(entriesQuery); + + var result = new List(); + foreach (var row in entryRows) + { + var id = row.GetValue("id"); + var altersQuery = new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ?", + normalizedSystemId, + id + ); + var alterRows = await session.ExecuteAsync(altersQuery); + var alterIds = alterRows.Select(r => new AlterId(r.GetValue("alter_id"))).ToArray(); + + result.Add(JournalRowMappers.MapJournalReadModel(row, alterIds)); + } + + // Sort by wire form (lowercase "N" hex); Guid.CompareTo would reorder differently. + return (IReadOnlyList)result + .OrderByDescending(e => e.Id.Value.ToString("N"), StringComparer.Ordinal) + .ToArray(); + }, cancellationToken); + } + + public async Task GetGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var entryQuery = new SimpleStatement( + $"SELECT id, user_id, title, content, color, pinned, locked, inserted_at, updated_at FROM {keyspace}.global_journals WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + entryId.Value + ); + var entryRow = (await session.ExecuteAsync(entryQuery)).FirstOrDefault(); + if (entryRow is null) + return null; + + var altersQuery = new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ?", + normalizedSystemId, + entryId.Value + ); + var alterIds = (await session.ExecuteAsync(altersQuery)) + .Select(r => new AlterId(r.GetValue("alter_id"))) + .ToArray(); + + return JournalRowMappers.MapJournalReadModel(entryRow, alterIds); + }, cancellationToken); + } + + public async Task DeleteAllForAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + // Single-partition read on (user_id, alter_id). + var listQuery = new SimpleStatement( + $"SELECT id FROM {keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ?", + normalizedSystemId, + alterId.Value); + var entryIds = (await session.ExecuteAsync(listQuery)) + .Select(r => r.GetValue("id")) + .ToArray(); + + // Client-side filter — the partition is small, ALLOW FILTERING isn't worth the + // server-side burn for the same row reads. + var attachmentsQuery = new SimpleStatement( + $"SELECT global_journal_id, alter_id FROM {keyspace}.global_journal_alters WHERE user_id = ?", + normalizedSystemId); + var attachedGlobals = (await session.ExecuteAsync(attachmentsQuery)) + .Where(r => new AlterId(r.GetValue("alter_id")) == alterId) + .Select(r => r.GetValue("global_journal_id")) + .ToArray(); + + if (entryIds.Length == 0 && attachedGlobals.Length == 0) + { + return 0; + } + + // All deletes share user_id, so this is a single-coordinator batch. + var batch = new BatchStatement(); + + foreach (var entryId in entryIds) + { + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_journals WHERE user_id = ? AND id = ? AND alter_id = ?", + normalizedSystemId, + entryId, + alterId.Value)); + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_journals_by_alter WHERE user_id = ? AND alter_id = ? AND id = ?", + normalizedSystemId, + alterId.Value, + entryId)); + } + + foreach (var globalJournalId in attachedGlobals) + { + batch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.global_journal_alters WHERE user_id = ? AND global_journal_id = ? AND alter_id = ?", + normalizedSystemId, + globalJournalId, + alterId.Value)); + } + + await session.ExecuteAsync(batch); + return entryIds.Length; + }, cancellationToken); + } + + // Closed enum instead of a raw string column parameter so no caller can slip a + // CQL-injection-adjacent identifier into the UPDATE. + private enum JournalFlag + { + Locked, + Pinned, + } + + private static string FlagToColumn(JournalFlag flag) => flag switch + { + JournalFlag.Locked => "locked", + JournalFlag.Pinned => "pinned", + _ => throw new ArgumentOutOfRangeException(nameof(flag), flag, "Unknown JournalFlag."), + }; +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaNotificationTokenRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaNotificationTokenRepository.cs new file mode 100644 index 00000000..16d2f7fa --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaNotificationTokenRepository.cs @@ -0,0 +1,132 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.Persistence; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaNotificationTokenRepository : INotificationTokenRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly IScyllaScopeResolver _scopeResolver; + + public ScyllaNotificationTokenRepository( + IScyllaSessionProvider sessionProvider, + IScyllaKeyspaceResolver keyspaceResolver, + IScyllaScopeResolver scopeResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _scopeResolver = scopeResolver; + } + + public async Task AddAsync(SystemId systemId, PushToken token, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + var now = DateTimeOffset.UtcNow; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + + var batch = new BatchStatement(); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.notification_tokens (user_id, push_token, inserted_at, updated_at) VALUES (?, ?, ?, ?)", + normalizedSystemId, token.Value, now.UtcDateTime, now.UtcDateTime)); + batch.Add(new SimpleStatement( + $"INSERT INTO {ScyllaGlobalKeyspace.Name}.notification_tokens_by_push_token (push_token, user_id) VALUES (?, ?)", + token.Value, normalizedSystemId)); + + await session.ExecuteAsync(batch); + return true; + }, cancellationToken); + } + + public async Task RemoveAsync(PushToken token, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync(async scope => + { + var session = scope.Session; + + var findByToken = await session.PrepareAsync($@" + SELECT user_id FROM {ScyllaGlobalKeyspace.Name}.notification_tokens_by_push_token + WHERE push_token = ?"); + + var rows = await session.ExecuteAsync(findByToken.Bind(token.Value)); + + var deleteBatch = new BatchStatement(); + foreach (var row in rows) + { + var userId = row.GetValue("user_id"); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.notification_tokens WHERE user_id = ? AND push_token = ?", + userId, token.Value)); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {ScyllaGlobalKeyspace.Name}.notification_tokens_by_push_token WHERE push_token = ? AND user_id = ?", + token.Value, userId)); + } + + if (!deleteBatch.IsEmpty) + { + await session.ExecuteAsync(deleteBatch); + } + + return true; + }, cancellationToken); + } + + // Reads global.friendships for the friend-id list, then multi-partition selects + // global.notification_tokens per friend with concurrency capped by + // PersistenceConfiguration.HydrationMaxConcurrency. Both tables partition on + // user_id, so each per-friend fetch is single-partition. Friends with zero tokens + // are omitted from the result so callers can iterate without a guard. + public async Task> ListTokensForFriendsOfAsync( + SystemId systemId, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteGlobalAsync>(async scope => + { + var session = scope.Session; + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + + var friendRows = await session.ExecuteAsync(new SimpleStatement( + $"SELECT friend_id FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ?", + normalizedSystemId)); + + var friendIds = friendRows.Select(r => r.GetValue("friend_id")) + .Where(id => !string.IsNullOrWhiteSpace(id)) + .Distinct(StringComparer.Ordinal) + .ToArray(); + + if (friendIds.Length == 0) + return (IReadOnlyList)Array.Empty(); + + var perFriendGroups = await ConcurrentProjection.SelectWithConcurrencyAsync( + friendIds, + _options.HydrationMaxConcurrency, + async friendId => + { + var tokenRows = await session.ExecuteAsync(new SimpleStatement( + $"SELECT push_token FROM {ScyllaGlobalKeyspace.Name}.notification_tokens WHERE user_id = ?", + friendId)); + var tokens = tokenRows.Select(r => r.GetValue("push_token")) + .Where(t => !string.IsNullOrWhiteSpace(t)) + .Distinct(StringComparer.Ordinal) + .Select(t => new PushToken(t)) + .ToArray(); + return new FriendNotificationTokens(new(friendId), tokens); + }, + cancellationToken); + + return (IReadOnlyList)perFriendGroups + .Where(g => g.Tokens.Count > 0) + .ToArray(); + }, cancellationToken); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaPollRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaPollRepository.cs new file mode 100644 index 00000000..770cc89a --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaPollRepository.cs @@ -0,0 +1,216 @@ +using System.Text.Json; +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaPollRepository : IPollRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + + public ScyllaPollRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + } + + public async Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, user_id, title, description, type, data, time_end, inserted_at, updated_at FROM {keyspace}.polls WHERE user_id = ?", + normalizedSystemId + ); + + var rows = await session.ExecuteAsync(query); + // VERIFIED: 2026-03-17 Elixir polls.ex get_polls() has no explicit sort → database order (ascending). Matches C# OrderBy. + // Sort key is the wire form (lowercase "N" hex) to keep list ordering byte-identical + // to the historic string-backed PollId — Guid.CompareTo bytewise reorders differently. + return rows.Select(ToReadModel).OrderBy(p => p.Id.Value.ToString("N"), StringComparer.Ordinal).ToList(); + }, cancellationToken); + } + + public async Task GetAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, user_id, title, description, type, data, time_end, inserted_at, updated_at FROM {keyspace}.polls WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + pollId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null ? null : ToReadModel(row); + }, cancellationToken); + } + + public async Task CreateAsync(SystemId systemId, CreatePollCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var pollGuid = Guid.NewGuid(); + + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.polls (user_id, id, title, description, type, data, time_end, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, toTimestamp(now()))", + normalizedSystemId, + pollGuid, + command.Title, + command.Description, + (short)command.Type, + "{}", + command.TimeEnd, + command.InsertedAtUtc + ); + + await session.ExecuteAsync(insert); + return new(pollGuid); + }, cancellationToken); + } + + public async Task ExistsAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, scope => ExistsAsync(scope, pollId), cancellationToken); + } + + private static Task ExistsAsync(ScyllaScope scope, PollId pollId) + => ScyllaExistsQueries.RowExistsAsync(scope.Session, scope.Keyspace, "polls", "id", scope.NormalizedSystemId, pollId.Value); + + public async Task UpdateAsync(SystemId systemId, UpdatePollCommand command, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsAsync(scope, command.Id); + if (!exists) + return false; + + var updateBatch = new BatchStatement(); + + if (command.Title is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.polls SET title = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.Title, + normalizedSystemId, + command.Id.Value)); + } + + if (command.Description is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.polls SET description = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.Description, + normalizedSystemId, + command.Id.Value)); + } + + if (command.HasTimeEnd) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.polls SET time_end = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.TimeEnd, + normalizedSystemId, + command.Id.Value)); + } + + if (command.Data is not null) + { + updateBatch.Add(new SimpleStatement( + $"UPDATE {keyspace}.polls SET data = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + command.Data.Value.GetRawText(), + normalizedSystemId, + command.Id.Value)); + } + + if (!updateBatch.IsEmpty) + { + await session.ExecuteAsync(updateBatch); + } + + return true; + }, cancellationToken); + } + + public async Task DeleteAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsAsync(scope, pollId); + if (!exists) + return false; + + var delete = new SimpleStatement( + $"DELETE FROM {keyspace}.polls WHERE user_id = ? AND id = ?", + normalizedSystemId, + pollId.Value + ); + await session.ExecuteAsync(delete); + return true; + }, cancellationToken); + } + + // The data blob's confirmed shape (see PollDataJson) keeps per-alter votes in the + // top-level `responses` array as {"alter_id":,...} entries; deleting an alter + // filters those entries while leaving every other member untouched. + public async Task RemoveAlterFromPollsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + var polls = await ListAsync(systemId, cancellationToken); + + foreach (var poll in polls) + { + if (!PollDataJson.TryRemoveAlterResponses(poll.Data, alterId, out var newData)) + { + continue; + } + + await UpdateAsync(systemId, new UpdatePollCommand( + poll.Id, + null, + null, + null, + false, + newData + ), cancellationToken); + } + } + + private static PollReadModel ToReadModel(Row row) + => new( + new(row.GetValue("id")), + new(row.GetValue("user_id")), + row.GetValue("title"), + row.GetValue("description"), + row.GetValue("type").FromCode(), + JsonSerializer.Deserialize(row.GetValue("data") ?? "{}"), + row.GetValue("time_end"), + row.GetValue("inserted_at"), + row.GetValue("updated_at") + ); +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSettingsFieldRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSettingsFieldRepository.cs new file mode 100644 index 00000000..0fb22831 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSettingsFieldRepository.cs @@ -0,0 +1,323 @@ +using System.Collections.Concurrent; +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaSettingsFieldRepository : ISettingsFieldRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private static readonly ConcurrentDictionary<(int ClusterId, string Keyspace), byte> UdtMappings = new(); + + public ScyllaSettingsFieldRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + } + + public async Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var fields = await LoadFieldsWithMappingAsync(session, keyspace, normalizedSystemId); + if (fields is null) + { + return Array.Empty(); + } + + var result = fields + .Select((field, index) => new SettingsFieldReadModel( + new(field.Id), + field.Name, + field.Type.FromCode(), + field.SecurityLevel.FromStorage(), + field.Locked, + index, + field.InsertedAt?.UtcDateTime)) + .ToArray(); + + return (IReadOnlyList)result; + }, cancellationToken); + } + + public async Task CreateAsync( + SystemId systemId, + string name, + FieldType type, + VisibilityLevel securityLevel, + bool locked, + DateTime insertedAtUtc, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var fields = await LoadFieldsWithMappingAsync(session, keyspace, normalizedSystemId) ?? []; + + var fieldId = Guid.NewGuid(); + fields.Add(CreateFieldUdt(session, keyspace, fieldId, name, type, securityLevel, locked, insertedAtUtc)); + + await session.ExecuteAsync(BuildPersistFieldsStatement(keyspace, fields, normalizedSystemId)); + + return new(fieldId); + }, cancellationToken); + } + + public async Task UpdateAsync( + SystemId systemId, + FieldId fieldId, + string? name, + VisibilityLevel? securityLevel, + bool? locked, + CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var fields = await LoadFieldsWithMappingAsync(session, keyspace, normalizedSystemId); + if (fields is null) + { + return false; + } + + var updated = false; + for (var i = 0; i < fields.Count; i++) + { + if (fields[i].Id != fieldId.Value) + { + continue; + } + + if (name is not null) + { + fields[i].Name = name; + } + + if (securityLevel is not null) + { + fields[i].SecurityLevel = (short)securityLevel.Value; + } + + if (locked is not null) + { + fields[i].Locked = locked.Value; + } + + fields[i].UpdatedAt = DateTimeOffset.UtcNow; + updated = true; + break; + } + + if (!updated) + { + return false; + } + + await session.ExecuteAsync(BuildPersistFieldsStatement(keyspace, fields, normalizedSystemId)); + + return true; + }, cancellationToken); + } + + public async Task DeleteAsync(SystemId systemId, FieldId fieldId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var fields = await LoadFieldsWithMappingAsync(session, keyspace, normalizedSystemId); + if (fields is null) + { + return false; + } + + var removed = fields.RemoveAll(f => f.Id == fieldId.Value) > 0; + if (!removed) + { + return false; + } + + // Clear field values off every alter first so no leaked data survives the field delete. + var batch = await RemoveFieldValuesFromAltersAsync(session, keyspace, normalizedSystemId, fieldId.Value); + batch.Add(BuildPersistFieldsStatement(keyspace, fields, normalizedSystemId)); + + await session.ExecuteAsync(batch); + + return true; + }, cancellationToken); + } + + public async Task RelocateAsync(SystemId systemId, FieldId fieldId, int index, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var fields = await LoadFieldsWithMappingAsync(session, keyspace, normalizedSystemId); + if (fields is null) + { + return false; + } + + var currentIndex = fields.FindIndex(f => f.Id == fieldId.Value); + if (currentIndex < 0) + { + return false; + } + + var field = fields[currentIndex]; + field.UpdatedAt = DateTimeOffset.UtcNow; + fields.RemoveAt(currentIndex); + + var boundedIndex = Math.Max(0, Math.Min(index, fields.Count)); + fields.Insert(boundedIndex, field); + + await session.ExecuteAsync(BuildPersistFieldsStatement(keyspace, fields, normalizedSystemId)); + + return true; + }, cancellationToken); + } + + private static async Task?> LoadFieldsAsync(ISession session, string keyspace, string normalizedSystemId) + { + var query = new SimpleStatement( + $"SELECT fields FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return null; + } + + return row.GetValue?>("fields")?.ToList() ?? []; + } + + private static async Task?> LoadFieldsWithMappingAsync(ISession session, string keyspace, string normalizedSystemId) + { + EnsureFieldUdtMapping(session, keyspace); + return await LoadFieldsAsync(session, keyspace, normalizedSystemId); + } + + private static UserFieldUdt CreateFieldUdt( + ISession session, + string keyspace, + Guid id, + string name, + FieldType type, + VisibilityLevel securityLevel, + bool locked, + DateTime insertedAtUtc) + { + EnsureFieldUdtMapping(session, keyspace); + // SpecifyKind(Utc) defends against a future caller handing us a Local DateTime. + var insertedAtOffset = new DateTimeOffset(DateTime.SpecifyKind(insertedAtUtc, DateTimeKind.Utc), TimeSpan.Zero); + return new UserFieldUdt + { + Id = id, + Name = name, + Type = (short)type, + Locked = locked, + SecurityLevel = (short)securityLevel, + InsertedAt = insertedAtOffset, + UpdatedAt = insertedAtOffset + }; + } + + private static SimpleStatement BuildPersistFieldsStatement( + string keyspace, + IReadOnlyCollection fields, + string normalizedSystemId) + => new( + $"UPDATE {keyspace}.users SET fields = ?, updated_at = toTimestamp(now()) WHERE id = ?", + fields, + normalizedSystemId); + + private static void EnsureFieldUdtMapping(ISession session, string keyspace) + { + var key = (System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(session.Cluster), keyspace); + if (UdtMappings.ContainsKey(key)) + { + return; + } + + session.UserDefinedTypes.Define( + UdtMap + .For("field", keyspace) + .Map(f => f.Id, "id") + .Map(f => f.Name, "name") + .Map(f => f.Type, "type") + .Map(f => f.Locked, "locked") + .Map(f => f.SecurityLevel, "security_level") + .Map(f => f.InsertedAt, "inserted_at") + .Map(f => f.UpdatedAt, "updated_at")); + + UdtMappings.TryAdd(key, 0); + } + + private static async Task RemoveFieldValuesFromAltersAsync( + ISession session, + string keyspace, + string normalizedSystemId, + Guid fieldId) + { + ScyllaAlterRepository.EnsureAlterFieldUdtMapping(session, keyspace); + + var rows = await session.ExecuteAsync(new SimpleStatement( + $"SELECT id, fields FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId)); + + var batch = new BatchStatement(); + + foreach (var row in rows) + { + var alterId = row.GetValue("id"); + var currentFields = row.GetValue?>("fields")?.ToList(); + if (currentFields is null || currentFields.Count == 0) + { + continue; + } + + var removedAny = currentFields.RemoveAll(x => x.Id == fieldId) > 0; + if (!removedAny) + { + continue; + } + + batch.Add(new SimpleStatement( + $"UPDATE {keyspace}.alters SET fields = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + currentFields, + normalizedSystemId, + alterId)); + } + + return batch; + } + + // Driver UDT serializer needs DateTimeOffset for `timestamp` columns; Nullable + // throws InvalidTypeException on write. Public API stays DateTime/DateTime?. + private sealed class UserFieldUdt + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public short Type { get; set; } + public bool Locked { get; set; } + public short SecurityLevel { get; set; } + public DateTimeOffset? InsertedAt { get; set; } + public DateTimeOffset? UpdatedAt { get; set; } + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSharedQueries.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSharedQueries.cs new file mode 100644 index 00000000..0cc23379 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaSharedQueries.cs @@ -0,0 +1,67 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; + +namespace Interfold.Infrastructure.Scylla.Repository; + +/// Query helpers shared by the viewer-aware read paths in the alter, tag, and +/// fronting repositories. +internal static class ScyllaSharedQueries +{ + /// Viewer's friendship level toward : null + /// viewer → null, self-view → , otherwise + /// the global.friendships row's level (null when not friends). + public static async Task ResolveFriendshipLevelAsync( + ISession session, + IScyllaKeyspaceResolver keyspaceResolver, + SystemId ownerSystemId, + SystemId? viewerSystemId, + ILogger? logger = null) + { + if (string.IsNullOrWhiteSpace(viewerSystemId?.Value)) + { + return null; + } + + var normalizedViewerSystemId = keyspaceResolver.NormalizeSystemId(viewerSystemId.Value); + if (keyspaceResolver.NormalizeSystemId(ownerSystemId) == normalizedViewerSystemId) + { + return FriendshipLevel.TrustedFriend; + } + + try + { + var query = new SimpleStatement( + $"SELECT level FROM {ScyllaGlobalKeyspace.Name}.friendships WHERE user_id = ? AND friend_id = ? LIMIT 1", + ownerSystemId.Value, + normalizedViewerSystemId); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row is null ? null : row.GetValue("level").FromCode(); + } + catch (Exception ex) + { + GuardedMetrics.ErrorsTotal.Add(1, + new KeyValuePair("entity_type", "friendship"), + new KeyValuePair("exception_type", ex.GetType().Name)); + logger?.LogError(ex, + "Guarded friendship lookup failed: owner={OwnerSystemId}, viewer={ViewerSystemId}", + ownerSystemId.Value, normalizedViewerSystemId); + throw; + } + } + + public static async Task LoadPrimaryFrontAlterAsync(ISession session, string keyspace, string normalizedSystemId) + { + var primaryRow = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT primary_front_alter FROM {keyspace}.users WHERE id = ? LIMIT 1", + normalizedSystemId))).FirstOrDefault(); + + return primaryRow?.GetValue("primary_front_alter") is { } primaryShort + ? new AlterId(primaryShort) + : null; + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaTagRepository.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaTagRepository.cs new file mode 100644 index 00000000..3ca14042 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/ScyllaTagRepository.cs @@ -0,0 +1,565 @@ +using System.Diagnostics; +using Cassandra; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Observability; +using Interfold.Infrastructure.Persistence; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla.Repository; + +public sealed class ScyllaTagRepository : ITagRepository +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaScopeResolver _scopeResolver; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly IAlterRepository _alterRepository; + private readonly ILogger _logger; + + public ScyllaTagRepository( + IScyllaSessionProvider sessionProvider, + IScyllaScopeResolver scopeResolver, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options, + IAlterRepository alterRepository, + ILogger logger + ) + { + _sessionProvider = sessionProvider; + _scopeResolver = scopeResolver; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _alterRepository = alterRepository; + _logger = logger; + } + + public async Task CreateAsync( + SystemId systemId, + CreateTagCommand command, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + Guid? parentTagId = null; + + if (command.ParentTagId is { } requestedParentTagId && requestedParentTagId != TagId.Empty) + { + parentTagId = requestedParentTagId.Value; + } + + if (parentTagId is not null) + { + var parentCheck = new SimpleStatement( + $"SELECT id FROM {keyspace}.tags WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + parentTagId + ); + + var parentRows = await session.ExecuteAsync(parentCheck); + if (!parentRows.Any()) + return null; + } + + var tagGuid = Guid.NewGuid(); + + // Stamp security_level so read-back never sees null — same invariant as ScyllaAlterRepository.CreateAsync / ScyllaAlterRepositoryUdtNullTests. + var insert = new SimpleStatement( + $"INSERT INTO {keyspace}.tags (user_id, id, parent_tag_id, name, description, color, security_level, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, toTimestamp(now()))", + normalizedSystemId, + tagGuid, + parentTagId, + command.Name, + null, + null, + (short)VisibilityLevel.Private, + command.InsertedAtUtc + ); + + await session.ExecuteAsync(insert); + return new(tagGuid); + }, cancellationToken); + } + + public async Task ExistsAsync( + SystemId systemId, + TagId tagId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, scope => + ExistsAsync(scope, tagId), + cancellationToken); + } + + private static Task ExistsAsync(ScyllaScope scope, TagId tagId) + => ScyllaExistsQueries.RowExistsAsync(scope.Session, scope.Keyspace, "tags", "id", scope.NormalizedSystemId, tagId.Value); + + public async Task UpdateAsync( + SystemId systemId, + UpdateTagCommand command, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsAsync(scope, command.TagId); + if (!exists) + { + return false; + } + + var setClauses = new List(); + var values = new List(); + + if (command.Name is not null) + { + setClauses.Add("name = ?"); + values.Add(command.Name); + } + + if (command.Color is { } color) + { + setClauses.Add("color = ?"); + values.Add(color.Value); + } + + if (command.Description is not null) + { + setClauses.Add("description = ?"); + values.Add(command.Description); + } + + if (command.SecurityLevel is not null) + { + setClauses.Add("security_level = ?"); + values.Add((short)command.SecurityLevel.Value); + } + + if (setClauses.Count == 0) + { + return true; + } + + setClauses.Add("updated_at = toTimestamp(now())"); + values.Add(normalizedSystemId); + values.Add(command.TagId.Value); + + var update = new SimpleStatement( + $"UPDATE {keyspace}.tags SET {string.Join(", ", setClauses)} WHERE user_id = ? AND id = ?", + [.. values]); + + await session.ExecuteAsync(update); + + return true; + }, cancellationToken); + } + + public async Task DeleteAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsAsync(scope, tagId); + if (!exists) + { + return false; + } + + var deleteBatch = new BatchStatement(); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.tags WHERE user_id = ? AND id = ?", + normalizedSystemId, + tagId.Value + )); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ?", + normalizedSystemId, + tagId.Value + )); + + // Clean up alter_tags_by_alter: find all alters with this tag and remove reverse entries + var tagAlters = await session.ExecuteAsync(new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ?", + normalizedSystemId, tagId.Value)); + foreach (var tagAlterRow in tagAlters) + { + var aid = tagAlterRow.GetValue("alter_id"); + deleteBatch.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags_by_alter WHERE user_id = ? AND alter_id = ? AND tag_id = ?", + normalizedSystemId, aid, tagId.Value)); + } + + await session.ExecuteAsync(deleteBatch); + + return true; + }, cancellationToken); + } + + public async Task AttachAlterAsync( + SystemId systemId, + TagId tagId, + AlterId alterId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var tagExists = await ExistsAsync(scope, tagId); + if (!tagExists) + { + return false; + } + + var insert = new BatchStatement(); + insert.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.alter_tags (user_id, tag_id, alter_id, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + tagId.Value, + alterId.Value + )); + insert.Add(new SimpleStatement( + $"INSERT INTO {keyspace}.alter_tags_by_alter (user_id, alter_id, tag_id, inserted_at, updated_at) VALUES (?, ?, ?, toTimestamp(now()), toTimestamp(now()))", + normalizedSystemId, + alterId.Value, + tagId.Value + )); + await session.ExecuteAsync(insert); + + return true; + }, cancellationToken); + } + + public async Task DetachAlterAsync( + SystemId systemId, + TagId tagId, + AlterId alterId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var edgeExistsQuery = new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ? AND alter_id = ? LIMIT 1", + normalizedSystemId, + tagId.Value, + alterId.Value + ); + + var edgeRows = await session.ExecuteAsync(edgeExistsQuery); + if (!edgeRows.Any()) + { + return false; + } + + var delete = new BatchStatement(); + delete.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ? AND alter_id = ?", + normalizedSystemId, + tagId.Value, + alterId.Value + )); + delete.Add(new SimpleStatement( + $"DELETE FROM {keyspace}.alter_tags_by_alter WHERE user_id = ? AND alter_id = ? AND tag_id = ?", + normalizedSystemId, + alterId.Value, + tagId.Value + )); + await session.ExecuteAsync(delete); + + return true; + }, cancellationToken); + } + + public async Task GetParentIdAsync( + SystemId systemId, + TagId tagId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT parent_tag_id FROM {keyspace}.tags WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + tagId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return ToTagId(row?.GetValue("parent_tag_id")); + }, cancellationToken); + } + + public async Task SetParentAsync( + SystemId systemId, + TagId tagId, + TagId parentTagId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var childExists = await ExistsAsync(scope, tagId); + var parentExists = await ExistsAsync(scope, parentTagId); + if (!childExists || !parentExists) + { + return false; + } + + var update = new SimpleStatement( + $"UPDATE {keyspace}.tags SET parent_tag_id = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + parentTagId.Value, + normalizedSystemId, + tagId.Value + ); + await session.ExecuteAsync(update); + + return true; + }, cancellationToken); + } + + public async Task RemoveParentAsync( + SystemId systemId, + TagId tagId, + CancellationToken cancellationToken = default + ) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var exists = await ExistsAsync(scope, tagId); + if (!exists) + { + return false; + } + + var update = new SimpleStatement( + $"UPDATE {keyspace}.tags SET parent_tag_id = ?, updated_at = toTimestamp(now()) WHERE user_id = ? AND id = ?", + null, + normalizedSystemId, + tagId.Value + ); + await session.ExecuteAsync(update); + + return true; + }, cancellationToken); + } + + public async Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, name, color, description, parent_tag_id, inserted_at, updated_at, security_level, user_id FROM {keyspace}.tags WHERE user_id = ?", + normalizedSystemId + ); + + var rows = await session.ExecuteAsync(query); + var tags = new List(); + + foreach (var row in rows) + { + var alterIds = await GetAlterIdsAsync(session, keyspace, normalizedSystemId, row.GetValue("id")); + tags.Add(TagRowMappers.MapTagReadModel(row, alterIds)); + } + + // VERIFIED: 2026-03-17 Elixir tags.ex get_tags() has no explicit sort → database order (ascending). Matches C# OrderBy. + // Sort key is the wire form (lowercase "N" hex) to keep list ordering byte-identical + // to the historic string-backed TagId — Guid.CompareTo bytewise reorders differently. + return tags.OrderBy(x => x.Id.Value.ToString("N"), StringComparer.Ordinal).ToArray(); + }, cancellationToken); + } + + public async Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var result = await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var friendshipLevel = await ScyllaSharedQueries.ResolveFriendshipLevelAsync(session, _keyspaceResolver, new(normalizedSystemId), viewerSystemId, _logger); + var hydrationConcurrency = _options.HydrationMaxConcurrency; + + var query = new SimpleStatement( + $"SELECT id, name, color, description, parent_tag_id, inserted_at, updated_at, security_level, user_id FROM {keyspace}.tags WHERE user_id = ?", + normalizedSystemId + ); + + var rows = (await session.ExecuteAsync(query)).ToArray(); + var totalCount = rows.Length; + var tags = new List(); + + foreach (var row in rows) + { + var visibility = row.GetValue("security_level").FromStorage(); + if (!visibility.CanBeViewedBy(friendshipLevel)) + { + continue; + } + + var alterIds = await GetGuardedAlterIdsAsync(session, keyspace, normalizedSystemId, row.GetValue("id"), friendshipLevel); + var alters = alterIds.Count == 0 + ? Array.Empty() + : (await ConcurrentProjection.SelectWithConcurrencyAsync( + alterIds, + hydrationConcurrency, + id => _alterRepository.GetGuardedAsync(systemId, id, viewerSystemId, cancellationToken), + cancellationToken)) + .Where(x => x != null) + .ToArray(); + tags.Add(TagRowMappers.MapTagPublicReadModel(row, alters!)); + } + + var ordered = tags.OrderBy(x => x.Id.Value.ToString("N"), StringComparer.Ordinal).ToArray(); + return (Total: totalCount, Visible: (IReadOnlyList)ordered, NormalizedSystemId: normalizedSystemId); + }, cancellationToken); + + GuardedInstrumentation.RecordList(_logger, "tag", nameof(ListGuardedAsync), viewerSystemId, result.NormalizedSystemId, result.Total, result.Visible.Count, sw.Elapsed.TotalMilliseconds); + return result.Visible; + } + + public async Task GetAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default) + { + return await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + + var query = new SimpleStatement( + $"SELECT id, name, color, description, parent_tag_id, inserted_at, updated_at, security_level, user_id FROM {keyspace}.tags WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + tagId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return null; + } + + var alterIds = await GetAlterIdsAsync(session, keyspace, normalizedSystemId, tagId.Value); + return TagRowMappers.MapTagReadModel(row, alterIds); + }, cancellationToken); + } + + public async Task GetGuardedAsync( + SystemId systemId, + TagId tagId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default) + { + var sw = Stopwatch.StartNew(); + var result = await _scopeResolver.ExecuteAsync(systemId, async scope => + { + var (session, keyspace, normalizedSystemId) = scope; + var friendshipLevel = await ScyllaSharedQueries.ResolveFriendshipLevelAsync(session, _keyspaceResolver, new(normalizedSystemId), viewerSystemId, _logger); + var hydrationConcurrency = _options.HydrationMaxConcurrency; + + var query = new SimpleStatement( + $"SELECT id, name, color, description, parent_tag_id, inserted_at, updated_at, security_level, user_id FROM {keyspace}.tags WHERE user_id = ? AND id = ? LIMIT 1", + normalizedSystemId, + tagId.Value + ); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + if (row is null) + { + return (Tag: (TagPublicReadModel?)null, Filtered: false, NormalizedSystemId: normalizedSystemId); + } + + var visibility = row.GetValue("security_level").FromStorage(); + if (!visibility.CanBeViewedBy(friendshipLevel)) + { + return (Tag: (TagPublicReadModel?)null, Filtered: true, NormalizedSystemId: normalizedSystemId); + } + + var alterIds = await GetGuardedAlterIdsAsync(session, keyspace, normalizedSystemId, tagId.Value, friendshipLevel); + var alters = alterIds.Count == 0 + ? Array.Empty() + : (await ConcurrentProjection.SelectWithConcurrencyAsync( + alterIds, + hydrationConcurrency, + id => _alterRepository.GetGuardedAsync(systemId, id, viewerSystemId, cancellationToken), + cancellationToken)) + .Where(x => x != null) + .ToArray(); + return (Tag: (TagPublicReadModel?)TagRowMappers.MapTagPublicReadModel(row, alters!), Filtered: false, NormalizedSystemId: normalizedSystemId); + }, cancellationToken); + + GuardedInstrumentation.RecordGet(_logger, "tag", nameof(GetGuardedAsync), viewerSystemId, result.NormalizedSystemId, tagId.Value.ToString("N"), found: result.Tag is not null, filtered: result.Filtered, sw.Elapsed.TotalMilliseconds); + return result.Tag; + } + + private static async Task> GetAlterIdsAsync(ISession session, string keyspace, string normalizedSystemId, Guid tagId) + { + var query = new SimpleStatement( + $"SELECT alter_id FROM {keyspace}.alter_tags WHERE user_id = ? AND tag_id = ?", + normalizedSystemId, + tagId + ); + + var rows = await session.ExecuteAsync(query); + return rows.Select(x => new AlterId(x.GetValue("alter_id"))).OrderBy(x => x.Value).ToArray(); + } + + private static async Task> GetGuardedAlterIdsAsync( + ISession session, + string keyspace, + string normalizedSystemId, + Guid tagId, + FriendshipLevel? friendshipLevel) + { + var alterIds = await GetAlterIdsAsync(session, keyspace, normalizedSystemId, tagId); + if (alterIds.Count == 0) + { + return alterIds; + } + + var query = new SimpleStatement( + $"SELECT id, security_level FROM {keyspace}.alters WHERE user_id = ?", + normalizedSystemId); + + var rows = await session.ExecuteAsync(query); + var visible = rows + .Select(row => new + { + AlterId = new AlterId(row.GetValue("id")), + Visibility = row.GetValue("security_level").FromStorage() + }) + .Where(x => alterIds.Contains(x.AlterId) && x.Visibility.CanBeViewedBy(friendshipLevel)) + .Select(x => x.AlterId) + .OrderBy(x => x.Value) + .ToArray(); + + return visible; + } + + private static TagId? ToTagId(Guid? guid) + => guid is null ? null : new TagId(guid.Value); +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/Repository/TagRowMappers.cs b/infrastructure/Interfold.Infrastructure.Scylla/Repository/TagRowMappers.cs new file mode 100644 index 00000000..dbdd43df --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/Repository/TagRowMappers.cs @@ -0,0 +1,43 @@ +using Cassandra; +using Interfold.Alters.Contracts.Models; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Infrastructure.Scylla.Repository; + +internal static class TagRowMappers +{ + public static TagReadModel MapTagReadModel(Row row, IReadOnlyList alterIds) + { + return new TagReadModel( + new(row.GetValue("id")), + row.GetValue("name"), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("description"), + row.GetValue("parent_tag_id") is { } parentId ? new TagId(parentId) : null, + alterIds, + row.GetValue("inserted_at").UtcDateTime, + row.GetValue("updated_at").UtcDateTime, + row.GetValue("security_level").FromStorage(), + new(row.GetValue("user_id")) + ); + } + + public static TagPublicReadModel MapTagPublicReadModel(Row row, IReadOnlyList alters) + { + return new TagPublicReadModel( + new(row.GetValue("id")), + row.GetValue("name"), + HexColor.FromNullable(row.GetValue("color")), + row.GetValue("description"), + row.GetValue("parent_tag_id") is { } parentId ? new TagId(parentId) : null, + alters, + row.GetValue("inserted_at").UtcDateTime, + row.GetValue("updated_at").UtcDateTime, + row.GetValue("security_level").FromStorage(), + new(row.GetValue("user_id")) + ); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaConfigResolver.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaConfigResolver.cs new file mode 100644 index 00000000..d3e3a821 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaConfigResolver.cs @@ -0,0 +1,95 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +/// Unified resolver for Scylla connection values. Contact points and port honour +/// so integration tests can retarget a host-published +/// port; credentials come exclusively from ; the keyspace is +/// the per-node region identity sourced directly from +/// (env → IOptions → resolver). +public interface IScyllaConfigResolver +{ + Task GetContactPointsAsync(CancellationToken ct = default); + + /// Local datacenter for DC-aware routing. Defaults to datacenter1. + Task GetDatacenterAsync(CancellationToken ct = default); + + /// App-user username. null disables password auth on the client. + Task GetUsernameAsync(CancellationToken ct = default); + + /// App-user password. Empty when the store row is unset. + Task GetPasswordAsync(CancellationToken ct = default); + + /// Regional keyspace wire value (e.g. nam, eur). + string GetKeyspace(); + + /// Scylla TCP port. Override wins over the store row; falls back to 9042. + Task GetPortAsync(CancellationToken ct = default); +} + +/// +public sealed class ScyllaConfigResolver : IScyllaConfigResolver +{ + private readonly ISecretsStore _secretsStore; + private readonly ScyllaOverrideOptions _overrides; + private readonly PersistenceConfiguration _persistence; + + public ScyllaConfigResolver( + ISecretsStore secretsStore, + IOptions overrides, + IOptions persistence) + { + _secretsStore = secretsStore; + _overrides = overrides.Value; + _persistence = persistence.Value; + } + + public async Task GetContactPointsAsync(CancellationToken ct = default) + { + if (_overrides.ContactPoints is { Count: > 0 } configOverride) + { + return configOverride.ToArray(); + } + + var raw = await _secretsStore.GetAsync(SecretsStoreKeys.ScyllaContactPoints, ct); + return string.IsNullOrWhiteSpace(raw) + ? ["127.0.0.1"] + : raw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + } + + public async Task GetDatacenterAsync(CancellationToken ct = default) + { + return await _secretsStore.GetAsync(SecretsStoreKeys.ScyllaLocalDatacenter, ct) + ?? "datacenter1"; + } + + public async Task GetUsernameAsync(CancellationToken ct = default) + { + return await _secretsStore.GetAsync(SecretsStoreKeys.ScyllaUsername, ct); + } + + public async Task GetPasswordAsync(CancellationToken ct = default) + { + return await _secretsStore.GetAsync(SecretsStoreKeys.ScyllaPassword, ct) + ?? string.Empty; + } + + public string GetKeyspace() + { + return _persistence.ScyllaKeyspace.ToWire(); + } + + public async Task GetPortAsync(CancellationToken ct = default) + { + if (_overrides.Port is { } port) + { + return port; + } + + var raw = await _secretsStore.GetAsync(SecretsStoreKeys.ScyllaPort, ct); + return int.TryParse(raw, out var storePort) ? storePort : 9042; + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaHealthChecker.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaHealthChecker.cs new file mode 100644 index 00000000..2968b7b1 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaHealthChecker.cs @@ -0,0 +1,116 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +public class ScyllaHealthChecker : IHealthCheck +{ + private const string GlobalKeyspace = "global"; + + private static readonly string[] RequiredGlobalScyllaTables = + [ + "user_registry", + "notification_tokens", + "friendships", + "friend_requests" + ]; + + private static readonly string[] RequiredRegionalScyllaTables = + [ + "users", + "alters", + "tags", + "alter_tags", + "polls", + "fronts", + "current_fronts", + "global_journals", + "global_journal_alters", + "alter_journals" + ]; + + private readonly IScyllaSessionProvider _scyllaSessionProvider; + private readonly PersistenceConfiguration _options; + private readonly IScyllaConfigResolver _configResolver; + + public ScyllaHealthChecker( + IScyllaSessionProvider scyllaSessionProvider, + IOptions options, + IScyllaConfigResolver configResolver) + { + _scyllaSessionProvider = scyllaSessionProvider; + _options = options.Value; + _configResolver = configResolver; + } + + public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) + { + try + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var session = await _scyllaSessionProvider.GetSessionAsync(cancellationToken); + + var regionalKeyspace = _configResolver.GetKeyspace(); + + var missingGlobal = await GetMissingTablesAsync( + session, + GlobalKeyspace, + RequiredGlobalScyllaTables); + + var missingRegional = await GetMissingTablesAsync( + session, + regionalKeyspace, + RequiredRegionalScyllaTables); + + if (missingGlobal.Count <= 0 && missingRegional.Count <= 0) + { + return new HealthCheckResult(HealthStatus.Healthy, "Connected and required keyspace/tables exist."); + } + + var details = new List(2); + if (missingGlobal.Count > 0) + { + details.Add($"{GlobalKeyspace}: {string.Join(", ", missingGlobal)}"); + } + + if (missingRegional.Count > 0) + { + details.Add($"{regionalKeyspace}: {string.Join(", ", missingRegional)}"); + } + + return new HealthCheckResult(HealthStatus.Degraded, + $"Scylla is reachable but missing canonical tables ({string.Join(" | ", details)}). Run the canonical CQL bootstrap script first."); + }, _options, cancellationToken); + } + catch (Exception ex) + { + return new HealthCheckResult(HealthStatus.Unhealthy, exception: ex); + } + } + + private static async Task> GetMissingTablesAsync(ISession session, string keyspace, IEnumerable tableNames) + { + var query = new SimpleStatement( + "SELECT table_name FROM system_schema.tables WHERE keyspace_name = ?", + keyspace + ); + + var rows = await session.ExecuteAsync(query).ConfigureAwait(false); + var existingTables = rows.Select(r => r.GetValue("table_name")).ToHashSet(StringComparer.OrdinalIgnoreCase); + + var missingTables = new List(); + foreach (var tableName in tableNames) + { + if (!existingTables.Contains(tableName)) + { + missingTables.Add(tableName); + } + } + + return missingTables; + } +} \ No newline at end of file diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaKeyspaceResolver.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaKeyspaceResolver.cs new file mode 100644 index 00000000..863cc12c --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaKeyspaceResolver.cs @@ -0,0 +1,54 @@ +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Scylla; + +/// CQL-boundary resolver for regional/global keyspaces and the region-strip +/// normalisation applied to every system id before bind. The return +/// type on is a compile-time firewall — widening it back +/// to would compile but fail at bind time with the DataStax +/// "Unknown Cassandra target type" exception. +public interface IScyllaKeyspaceResolver +{ + string DefaultKeyspace { get; } + + /// Regional keyspace that owns . Interpolated + /// verbatim into CQL text at the call site. + string ResolveRegionalKeyspace(SystemId systemId); + + string ResolveGlobalKeyspace(); + + /// Region-stripped raw string for direct CQL bind. Keep the primitive + /// return type — see for why. + string NormalizeSystemId(SystemId systemId); +} + +public sealed class ScyllaKeyspaceResolver : IScyllaKeyspaceResolver +{ + private readonly IRegionContext _regionContext; + + public ScyllaKeyspaceResolver(IRegionContext regionContext) + { + _regionContext = regionContext; + } + + // Unwrap ScyllaKeyspace to the lowercase wire name once, at the CQL boundary. + public string DefaultKeyspace => _regionContext.CurrentRegion.ToWire(); + + public string ResolveRegionalKeyspace(SystemId systemId) + { + if (string.IsNullOrWhiteSpace(systemId)) + return DefaultKeyspace; + + // Canonicalise scoped and raw ids to the same keyspace — otherwise a JWT-scoped + // principal and its public-route sibling would land in different keyspaces. + return _regionContext.ResolveUserRegion(new(NormalizeSystemId(systemId))).ToWire(); + } + + public string ResolveGlobalKeyspace() => ScyllaGlobalKeyspace.Name; + + public string NormalizeSystemId(SystemId systemId) + => ScopedSystemId.StripRegionPrefix(systemId); +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaMigrationService.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaMigrationService.cs new file mode 100644 index 00000000..b8b17ba0 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaMigrationService.cs @@ -0,0 +1,689 @@ +using System.Diagnostics; +using System.Reflection; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +/// Applies embedded CQL migrations at startup under +/// . Creates keyspaces, schema, and +/// DML grants under admin credentials from . Handles +/// SimpleStrategy (single-node) and NetworkTopologyStrategy (multi-DC). Ledger is +/// global.schema_migrations keyed on (scope, version) + SHA-256 checksum; +/// singleton keyspaces (global, nam_nt, dummy) are bootstrapped +/// unconditionally because the ledger table lives in global. Templated +/// per-region migrations are tracked once per keyspace so a later region add re-runs +/// only against the new keyspace. +public sealed partial class ScyllaMigrationService( + IOptions options, + ISecretsStore secretsStore, + IScyllaConfigResolver configResolver, + ILogger logger) : IHostedLifecycleService +{ + // Derived from ScyllaKeyspace so the regional list can't drift from IRegionContext. + private static readonly string[] RegionalKeyspaces = + Enum.GetValues() + .Select(Interfold.Shared.Contracts.Enums.EnumWire.ToWire) + .ToArray(); + + private static readonly string[] SingletonKeyspaces = ["global", "nam_nt", "dummy"]; + + private const string LedgerKeyspace = "global"; + private const string LedgerTable = "schema_migrations"; + private const string LedgerTableFqn = $"{LedgerKeyspace}.{LedgerTable}"; + private const string SingletonsMigration = "000_create_singleton_keyspaces.cql"; + private const string KeyspacesMigration = "001_create_interfold_keyspaces.cql"; + private const string SchemaMigration = "002_create_interfold_schema.templated.cql"; + private const string FieldTimestampsMigration = "003_field_udt_timestamps.templated.cql"; + private const string ImportOperationsMigration = "004_import_operations.templated.cql"; + private const string ColorFixupMarkerMigration = "005_marker_color_fixup.templated.cql"; + private const string PrimaryFrontAddAlterMigration = "006_add_primary_front_alter.templated.cql"; + private const string GrantsVersion = "grants_v1"; + + // Sequenced immediately after 006's ALTER; bump the checksum to force per-keyspace re-run. + private const string PrimaryFrontBackfillVersion = "v1"; + private const string PrimaryFrontIntToAlterChecksum = "primary_front_int_to_alter_v1"; + + // Bump GrantsVersion whenever this string changes so grants re-apply across all scopes. + private const string GrantTemplate = "GRANT SELECT ON KEYSPACE {ks} TO {user};GRANT MODIFY ON KEYSPACE {ks} TO {user}"; + + private string? _adminUsername; + private string? _adminPassword; + private string[]? _contactPoints; + private string? _datacenter; + private string? _appUsername; + private string? _keyspace; + private int _port; + + public async Task StartingAsync(CancellationToken cancellationToken) + { + _adminUsername = await secretsStore.GetAsync(SecretsStoreKeys.ScyllaAdminUsername, cancellationToken); + _adminPassword = await secretsStore.GetAsync(SecretsStoreKeys.ScyllaAdminPassword, cancellationToken); + + if (string.IsNullOrWhiteSpace(_adminUsername) || + string.IsNullOrWhiteSpace(_adminPassword)) + { + logger.LogInformation("[scylla-migrate] No admin credentials in secrets store — skipping migrations."); + return; + } + + _contactPoints = await configResolver.GetContactPointsAsync(cancellationToken); + _datacenter = await configResolver.GetDatacenterAsync(cancellationToken); + _appUsername = await configResolver.GetUsernameAsync(cancellationToken); + _port = await configResolver.GetPortAsync(cancellationToken); + + // Keyspace is the per-node region identity — env-only, never store-shared. + _keyspace = configResolver.GetKeyspace(); + + logger.LogInformation("[scylla-migrate] Applying ScyllaDB schema migrations..."); + + // Retry LIST ROLES — Cassandra's PasswordAuthenticator can accept anonymous + // connections briefly after startup, before auth is enforced. + var cluster = BuildCluster(); + var session = await cluster.ConnectAsync(); + + for (var authAttempt = 0; authAttempt < 10; authAttempt++) + { + try + { + await session.ExecuteAsync(new SimpleStatement("LIST ROLES")); + logger.LogInformation("[scylla-migrate] Authenticated as '{User}'.", _adminUsername); + break; + } + catch (UnauthorizedException) + { + if (authAttempt >= 9) throw; + logger.LogWarning("[scylla-migrate] Connection is anonymous — auth not ready, retrying in 3s (attempt {Attempt}/10)...", + authAttempt + 1); + session.Dispose(); + await cluster.ShutdownAsync(); + await Task.Delay(3000); + cluster = BuildCluster(); + session = await cluster.ConnectAsync(); + } + catch (AuthenticationException) + { + if (authAttempt >= 9) throw; + logger.LogWarning("[scylla-migrate] Auth rejected — admin role may not exist yet, retrying in 3s (attempt {Attempt}/10)...", + authAttempt + 1); + session.Dispose(); + await cluster.ShutdownAsync(); + await Task.Delay(3000); + cluster = BuildCluster(); + session = await cluster.ConnectAsync(); + } + } + + try + { + var visibleDcs = await DiscoverDatacenters(session); + var isScylla = await DetectScyllaDb(session); + logger.LogInformation("[scylla-migrate] Visible datacenters: {DCs}, database: {Db}", + string.Join(", ", visibleDcs), isScylla ? "ScyllaDB" : "Cassandra"); + + // Untracked bootstrap — the ledger table lives in `global`, so `global` must exist first. + await EnsureSingletonKeyspacesAsync(session, visibleDcs, isScylla); + await EnsureLedgerAsync(session); + var applied = await LoadAppliedAsync(session); + + await ApplyKeyspaces(session, visibleDcs, isScylla, applied); + await ApplyTemplatedMigrationPerKeyspace(session, applied, SchemaMigration); + await ApplyTemplatedMigrationPerKeyspace(session, applied, FieldTimestampsMigration); + await ApplyTemplatedMigrationPerKeyspace(session, applied, ImportOperationsMigration); + await ApplyTemplatedMigrationPerKeyspace(session, applied, ColorFixupMarkerMigration); + + // primary_front (int) -> primary_front_alter (smallint): same-name DROP+ADD of a + // different type is server-rejected, RENAME is PK-only, so we use a new column + // and backfill inline before traffic can bind to it. + await ApplyTemplatedMigrationPerKeyspace(session, applied, PrimaryFrontAddAlterMigration); + await BackfillPrimaryFrontIntToAlterAsync(session, applied, cancellationToken); + + await GrantPermissions(session, applied); + } + finally + { + session.Dispose(); + await cluster.ShutdownAsync(); + } + + logger.LogInformation("[scylla-migrate] All migrations applied."); + + _adminUsername = null; + _adminPassword = null; + logger.LogInformation("[scylla-migrate] Admin credentials cleared from memory."); + } + + public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + /// Idempotent migration entry point. Exposed as a static so + /// SharedDbFixture can migrate once per test session per backend instead of on + /// every host build. + public static Task MigrateAsync( + PersistenceConfiguration options, + ISecretsStore secretsStore, + IScyllaConfigResolver configResolver, + ILogger logger, + CancellationToken cancellationToken) + { + var service = new ScyllaMigrationService(Options.Create(options), secretsStore, configResolver, logger); + return service.StartingAsync(cancellationToken); + } + + private Cluster BuildCluster() => + Cluster.Builder() + .AddContactPoints(_contactPoints!) + .WithPort(_port) + .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy(_datacenter!)) + .WithCredentials(_adminUsername, _adminPassword) + .WithQueryTimeout(30000) + .WithSocketOptions(new SocketOptions() + .SetConnectTimeoutMillis(15000) + // 60s covers per-CREATE TABLE latency on contended hosts (e.g. self-hosted DinD). + .SetReadTimeoutMillis(60000) + .SetKeepAlive(true)) + .Build(); + + // --- DC Discovery --- + + private async Task> DiscoverDatacenters(ISession session) + { + var dcs = new HashSet(StringComparer.OrdinalIgnoreCase); + + var localRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.local")); + foreach (var row in localRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) dcs.Add(dc.ToLowerInvariant()); + } + + var peerRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.peers")); + foreach (var row in peerRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) dcs.Add(dc.ToLowerInvariant()); + } + + dcs.IntersectWith(RegionalKeyspaces); + return dcs; + } + + // --- Replication Strategies (mirrors scylla-load-keyspaces.sh) --- + + private static bool IsMultiDc(HashSet dcs) => dcs.Count > 1; + + private static string SimpleReplication() => + "{'class': 'SimpleStrategy', 'replication_factor': '1'}"; + + private static string NtsReplication(HashSet visibleDcs, params string[] preferredDcs) + { + var actualDcs = preferredDcs.Where(visibleDcs.Contains).ToArray(); + if (actualDcs.Length == 0) actualDcs = [visibleDcs.First()]; + var pairs = string.Join(", ", actualDcs.Select(dc => $"'{dc}': '1'")); + return $"{{'class': 'NetworkTopologyStrategy', {pairs}}}"; + } + + private static string RegionalReplicationFor(string keyspace, HashSet dcs) + { + if (!IsMultiDc(dcs)) return SimpleReplication(); + + return keyspace switch + { + "nam" => NtsReplication(dcs, "nam", "eur", "sam"), + "eur" => dcs.Contains("eur") ? NtsReplication(dcs, "nam", "eur", "sas") : NtsReplication(dcs, "nam", "sam", "sas"), + "sam" => NtsReplication(dcs, "nam", "sam", "eas"), + "sas" => dcs.Contains("sas") ? NtsReplication(dcs, "nam", "sas", "ocn") : NtsReplication(dcs, "nam", "sas", "gdpr"), + "eas" => dcs.Contains("eas") ? NtsReplication(dcs, "nam", "eas", "ocn") : NtsReplication(dcs, "nam", "eas", "gdpr"), + "ocn" => dcs.Contains("ocn") ? NtsReplication(dcs, "nam", "ocn", "gdpr") : NtsReplication(dcs, "nam", "ocn", "sas"), + "gdpr" => dcs.Contains("gdpr") ? NtsReplication(dcs, "nam", "gdpr", "eur") : NtsReplication(dcs, "nam", "eur", "ocn"), + _ => NtsReplication(dcs, "nam", "eur", "sam") + }; + } + + private static string GlobalReplication(HashSet dcs) + { + if (!IsMultiDc(dcs)) return SimpleReplication(); + + // Global replicates to every available DC. + var allDcs = new[] { "nam", "eur", "sam", "sas", "eas", "ocn", "gdpr" } + .Where(dcs.Contains) + .ToArray(); + return NtsReplication(dcs, allDcs); + } + + private static string NamNtReplication(HashSet dcs) + { + if (!IsMultiDc(dcs)) return SimpleReplication(); + return NtsReplication(dcs, "nam"); + } + + // --- Database Detection --- + + private static async Task DetectScyllaDb(ISession session) + { + try + { + var rs = await session.ExecuteAsync(new SimpleStatement("SELECT cluster_name FROM system.local")); + // system_schema.scylla_tables is ScyllaDB-only. + await session.ExecuteAsync(new SimpleStatement( + "SELECT keyspace_name FROM system_schema.scylla_tables LIMIT 1")); + return true; + } + catch + { + return false; + } + } + + // --- Singleton Keyspace Bootstrap (untracked) --- + + // Untracked: `global` must exist before the ledger table itself does. + private async Task EnsureSingletonKeyspacesAsync(ISession session, HashSet dcs, bool isScylla) + { + var cqlTemplate = GetEmbeddedResource(SingletonsMigration); + var (tabletsClause, tabletsClauseDisabled) = ComputeTabletsClauses(dcs, isScylla); + var rendered = cqlTemplate + .Replace("{{GLOBAL_REPLICATION}}", GlobalReplication(dcs)) + .Replace("{{NAM_NT_REPLICATION}}", NamNtReplication(dcs)) + .Replace("{{TABLETS_CLAUSE_DISABLED}}", tabletsClauseDisabled) + .Replace("{{TABLETS_CLAUSE}}", tabletsClause); + + logger.LogInformation("[scylla-migrate] Bootstrapping singleton keyspaces ({Singletons})...", + string.Join(", ", SingletonKeyspaces)); + await ExecuteStatements(session, rendered); + } + + // --- Keyspace Creation --- + + private async Task ApplyKeyspaces( + ISession session, + HashSet dcs, + bool isScylla, + Dictionary<(string Scope, string Version), string> applied) + { + var cqlTemplate = GetEmbeddedResource(KeyspacesMigration); + var checksum = ComputeChecksum(cqlTemplate); + var (tabletsClause, _) = ComputeTabletsClauses(dcs, isScylla); + + var keyspaces = TargetKeyspaces(); + + foreach (var keyspace in keyspaces) + { + if (ShouldSkip(applied, keyspace, KeyspacesMigration, checksum)) + { + logger.LogDebug("[scylla-migrate] Skipping {Migration} for '{Keyspace}', already applied.", + KeyspacesMigration, keyspace); + continue; + } + + var regionalRepl = RegionalReplicationFor(keyspace, dcs); + var rendered = cqlTemplate + .Replace("{{KEYSPACE}}", keyspace) + .Replace("{{KEYSPACE_REPLICATION}}", regionalRepl) + .Replace("{{TABLETS_CLAUSE}}", tabletsClause); + + logger.LogInformation("[scylla-migrate] Creating keyspace '{Region}' (replication={Repl})...", + keyspace, regionalRepl); + var stopwatch = Stopwatch.StartNew(); + await ExecuteStatements(session, rendered); + stopwatch.Stop(); + + await RecordMigrationAsync(session, keyspace, KeyspacesMigration, checksum, + (int)stopwatch.ElapsedMilliseconds); + } + } + + // --- Templated Per-Keyspace Migrations --- + + // Single seam for every keyspace-scoped migration — render {{KEYSPACE}}, skip via + // ledger checksum, record via RecordMigrationAsync. Envelope-level changes (retry, + // telemetry, error mapping) belong here. + private async Task ApplyTemplatedMigrationPerKeyspace( + ISession session, + Dictionary<(string Scope, string Version), string> applied, + string migrationFilename) + { + var cqlTemplate = GetEmbeddedResource(migrationFilename); + var checksum = ComputeChecksum(cqlTemplate); + + var keyspaces = TargetKeyspaces(); + + foreach (var keyspace in keyspaces) + { + if (ShouldSkip(applied, keyspace, migrationFilename, checksum)) + { + logger.LogDebug("[scylla-migrate] Skipping {Migration} for '{Keyspace}', already applied.", + migrationFilename, keyspace); + continue; + } + + var rendered = cqlTemplate.Replace("{{KEYSPACE}}", keyspace); + logger.LogInformation("[scylla-migrate] Applying {Migration} to keyspace '{Keyspace}'...", + migrationFilename, keyspace); + var stopwatch = Stopwatch.StartNew(); + await ExecuteStatements(session, rendered); + stopwatch.Stop(); + + await RecordMigrationAsync(session, keyspace, migrationFilename, checksum, + (int)stopwatch.ElapsedMilliseconds); + } + } + + // --- Primary Front Backfill --- + + // Copies legacy int primary_front into the smallint sibling added by 006. + // Ledger-guarded per keyspace + skips already-populated rows, so crash-restart resumes + // on the null tail rather than repeating work. + private async Task BackfillPrimaryFrontIntToAlterAsync( + ISession session, + Dictionary<(string Scope, string Version), string> applied, + CancellationToken cancellationToken) + { + foreach (var keyspace in TargetKeyspaces()) + { + var scope = $"primary_front_backfill_int_to_alter:{keyspace}"; + if (ShouldSkip(applied, scope, PrimaryFrontBackfillVersion, PrimaryFrontIntToAlterChecksum)) + { + logger.LogDebug("[scylla-migrate] Skipping {Scope}, already applied.", scope); + continue; + } + + var stopwatch = Stopwatch.StartNew(); + var selectStmt = new SimpleStatement( + $"SELECT id, primary_front, primary_front_alter FROM {keyspace}.users"); + selectStmt.SetPageSize(500); + var rows = await session.ExecuteAsync(selectStmt); + + var copied = 0; + foreach (var row in rows) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (row.GetValue("primary_front_alter") is not null) + continue; + + var legacy = row.GetValue("primary_front"); + if (legacy is null) + continue; + + if (legacy is < short.MinValue or > short.MaxValue) + { + throw new InvalidDataException( + $"users.primary_front value {legacy} is outside " + + $"[{short.MinValue}, {short.MaxValue}]; a legacy row escaped the " + + $"smallint invariant. Investigate before re-running the migration."); + } + + var userId = row.GetValue("id"); + var smallintValue = (short)legacy.Value; + await session.ExecuteAsync(new SimpleStatement( + $"UPDATE {keyspace}.users SET primary_front_alter = ? WHERE id = ?", + smallintValue, + userId)); + copied++; + } + + stopwatch.Stop(); + logger.LogInformation("[scylla-migrate] {Scope}: copied {Copied} row(s).", scope, copied); + + await RecordMigrationAsync(session, scope, PrimaryFrontBackfillVersion, + PrimaryFrontIntToAlterChecksum, (int)stopwatch.ElapsedMilliseconds); + } + } + + // --- Permission Grants --- + + private async Task GrantPermissions( + ISession session, + Dictionary<(string Scope, string Version), string> applied) + { + var appUser = _appUsername; + if (string.IsNullOrWhiteSpace(appUser)) + { + logger.LogWarning("[scylla-migrate] No app user configured — skipping permission grants."); + return; + } + + if (string.Equals(appUser, _adminUsername, StringComparison.OrdinalIgnoreCase)) + { + logger.LogInformation("[scylla-migrate] App user is the admin user — skipping permission grants."); + return; + } + + logger.LogInformation("[scylla-migrate] Granting DML permissions to '{AppUser}'...", appUser); + + var grantChecksum = ComputeChecksum(GrantTemplate); + var keyspaces = TargetKeyspaces() + .Concat(SingletonKeyspaces) + .ToArray(); + + foreach (var keyspace in keyspaces) + { + var scope = $"grants:{keyspace}"; + if (ShouldSkip(applied, scope, GrantsVersion, grantChecksum)) + { + logger.LogDebug("[scylla-migrate] Skipping grants for '{Keyspace}', already applied.", keyspace); + continue; + } + + var stopwatch = Stopwatch.StartNew(); + await GrantKeyspacePermissions(session, keyspace, appUser); + stopwatch.Stop(); + + await RecordMigrationAsync(session, scope, GrantsVersion, grantChecksum, + (int)stopwatch.ElapsedMilliseconds); + } + } + + private async Task GrantKeyspacePermissions(ISession session, string keyspace, string user) + { + var grants = new[] + { + $"GRANT SELECT ON KEYSPACE {keyspace} TO '{user}'", + $"GRANT MODIFY ON KEYSPACE {keyspace} TO '{user}'" + }; + + foreach (var stmt in grants) + { + try + { + await session.ExecuteAsync(new SimpleStatement(stmt)); + } + catch (InvalidQueryException ex) when (ex.Message.Contains("already exists", StringComparison.OrdinalIgnoreCase)) + { + // Permission already granted — idempotent + } + } + } + + // --- Ledger Helpers --- + + // Presumes `global` has already been bootstrapped by EnsureSingletonKeyspacesAsync. + private static async Task EnsureLedgerAsync(ISession session) + { + var ddl = $$""" + CREATE TABLE IF NOT EXISTS {{LedgerTableFqn}} ( + scope text, + version text, + checksum text, + applied_at timestamp, + duration_ms int, + applied_by text, + PRIMARY KEY (scope, version) + ) + """; + await session.ExecuteAsync(new SimpleStatement(ddl)); + } + + private static async Task> LoadAppliedAsync(ISession session) + { + var applied = new Dictionary<(string, string), string>(); + var rs = await session.ExecuteAsync(new SimpleStatement( + $"SELECT scope, version, checksum FROM {LedgerTableFqn}")); + foreach (var row in rs) + { + var scope = row.GetValue("scope"); + var version = row.GetValue("version"); + var checksum = row.GetValue("checksum"); + applied[(scope, version)] = checksum; + } + return applied; + } + + // Throws on checksum drift so an edited-in-place migration cannot silently rebase. + private static bool ShouldSkip( + Dictionary<(string Scope, string Version), string> applied, + string scope, + string version, + string checksum) + { + if (!applied.TryGetValue((scope, version), out var existing)) return false; + if (string.Equals(existing, checksum, StringComparison.Ordinal)) return true; + + throw new InvalidOperationException( + $"[scylla-migrate] Checksum mismatch for migration '{version}' (scope='{scope}'): " + + $"recorded={existing}, actual={checksum}. Migration files must not be edited after " + + $"they have been applied. Restore the original file or, if the change is intentional, " + + $"manually update {LedgerTableFqn}.checksum for this row."); + } + + private async Task RecordMigrationAsync( + ISession session, + string scope, + string version, + string checksum, + int durationMs) + { + var appliedBy = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + var stmt = new SimpleStatement( + $"INSERT INTO {LedgerTableFqn} (scope, version, checksum, applied_at, duration_ms, applied_by) " + + "VALUES (?, ?, ?, ?, ?, ?) IF NOT EXISTS", + scope, version, checksum, DateTimeOffset.UtcNow, durationMs, appliedBy); + + var rs = await session.ExecuteAsync(stmt); + var row = rs.FirstOrDefault(); + // [applied]=false means a concurrent migrator inserted the same row first. Safe to ignore: + // if the existing checksum differs we'll catch it on the next startup via ShouldSkip. + if (row is not null && !row.GetValue("[applied]")) + { + logger.LogDebug("[scylla-migrate] Ledger row for ({Scope}, {Version}) already inserted by concurrent run.", + scope, version); + } + } + + internal static string ComputeChecksum(string content) => + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(content))); + + private string[] TargetKeyspaces() => + options.Value.IsSingleScyllaInstance + ? [_keyspace!] + : RegionalKeyspaces; + + private static (string TabletsClause, string TabletsClauseDisabled) ComputeTabletsClauses( + HashSet dcs, bool isScylla) + { + var tabletsClause = isScylla && IsMultiDc(dcs) + ? " AND tablets = {'enabled': true}" + : ""; + var tabletsClauseDisabled = isScylla + ? " AND tablets = {'enabled': false}" + : ""; + return (tabletsClause, tabletsClauseDisabled); + } + + // --- CQL Execution --- + + private Task ExecuteStatements(ISession session, string cql) + => ExecuteStatementsStatic(session, cql, logger); + + // Swallows AlreadyExistsException and column-added/removed InvalidQueryException so + // a boot that crashed between an ALTER landing and its ledger row being written can + // re-run on the next boot without exploding. + private static async Task ExecuteStatementsStatic(ISession session, string cql, ILogger logger) + { + var statements = SplitCqlStatements(cql); + foreach (var stmt in statements) + { + logger.LogDebug("[scylla-migrate] Executing: {Stmt}", stmt[..Math.Min(stmt.Length, 80)]); + try + { + await session.ExecuteAsync(new SimpleStatement(stmt)); + } + catch (AlreadyExistsException) + { + // Idempotent — keyspace/table/type/index already exists + } + catch (InvalidQueryException ex) when (IsColumnNotFound(ex)) + { + // Idempotent — DROP {column} already applied on an earlier boot. + logger.LogDebug("[scylla-migrate] Skipping DROP for missing column: {Message}", ex.Message); + } + catch (InvalidQueryException ex) when (IsColumnAlreadyExists(ex)) + { + // Target Scylla version SyntaxErrors on `ADD IF NOT EXISTS`, so this + // catch is how ALTER ... ADD migrations stay re-runnable across crashes. + logger.LogDebug("[scylla-migrate] Skipping ADD for existing column: {Message}", ex.Message); + } + } + } + + private static bool IsColumnNotFound(InvalidQueryException ex) + { + var msg = ex.Message; + return msg.Contains("column", StringComparison.OrdinalIgnoreCase) + && (msg.Contains("does not exist", StringComparison.OrdinalIgnoreCase) + || msg.Contains("not found", StringComparison.OrdinalIgnoreCase) + || msg.Contains("undefined", StringComparison.OrdinalIgnoreCase) + || msg.Contains("unknown", StringComparison.OrdinalIgnoreCase)); + } + + private static bool IsColumnAlreadyExists(InvalidQueryException ex) + { + var msg = ex.Message; + // Version-dependent wording; match intent, not the exact string. + return (msg.Contains("column", StringComparison.OrdinalIgnoreCase) + || msg.Contains("conflicts with", StringComparison.OrdinalIgnoreCase)) + && (msg.Contains("already exists", StringComparison.OrdinalIgnoreCase) + || msg.Contains("conflicts with an existing", StringComparison.OrdinalIgnoreCase)); + } + + private static List SplitCqlStatements(string cql) + { + return StatementSplitter().Split(cql) + .Select(StripCommentLines) + .Where(s => s.Length > 0) + .ToList(); + } + + private static string StripCommentLines(string chunk) + { + var lines = chunk.Split('\n') + .Select(l => l.TrimEnd()) + .Where(l => !l.TrimStart().StartsWith("--", StringComparison.Ordinal) && l.Length > 0); + return string.Join('\n', lines).Trim(); + } + + [GeneratedRegex(@";\s*$", RegexOptions.Multiline)] + private static partial Regex StatementSplitter(); + + private static string GetEmbeddedResource(string filename) + { + var assembly = Assembly.GetExecutingAssembly(); + var resourceName = $"Interfold.Infrastructure.Scylla.Migrations.{filename}"; + + using var stream = assembly.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException($"Embedded resource not found: {resourceName}"); + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaScopeResolver.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaScopeResolver.cs new file mode 100644 index 00000000..939cf1ef --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaScopeResolver.cs @@ -0,0 +1,115 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +public record ScyllaScope(ISession Session, string Keyspace, string NormalizedSystemId); + +public record ScyllaGlobalScope(ISession Session, string Keyspace); + +public interface IScyllaScopeResolver +{ + Task ResolveAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task ExecuteAsync( + SystemId systemId, + Func operation, + CancellationToken cancellationToken = default); + + Task ExecuteAsync( + SystemId systemId, + Func> operation, + CancellationToken cancellationToken = default); + + Task ExecuteGlobalAsync( + Func operation, + CancellationToken cancellationToken = default); + + Task ExecuteGlobalAsync( + Func> operation, + CancellationToken cancellationToken = default); +} + + +public sealed class ScyllaScopeResolver : IScyllaScopeResolver +{ + private readonly IScyllaSessionProvider _sessionProvider; + private readonly IScyllaKeyspaceResolver _keyspaceResolver; + private readonly PersistenceConfiguration _options; + private readonly ILogger _logger; + + public ScyllaScopeResolver( + IScyllaSessionProvider sessionProvider, + IScyllaKeyspaceResolver keyspaceResolver, + IOptions options, + ILogger logger) + { + _sessionProvider = sessionProvider; + _keyspaceResolver = keyspaceResolver; + _options = options.Value; + _logger = logger; + } + + public async Task ResolveAsync(SystemId systemId, CancellationToken cancellationToken = default) + { + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var normalizedSystemId = _keyspaceResolver.NormalizeSystemId(systemId); + var keyspace = _keyspaceResolver.ResolveRegionalKeyspace(systemId); + + return new ScyllaScope(session, keyspace, normalizedSystemId); + } + + public Task ExecuteAsync( + SystemId systemId, + Func operation, + CancellationToken cancellationToken = default) + { + return DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var scope = await ResolveAsync(systemId, cancellationToken); + await operation(scope); + }, _options, cancellationToken, _logger); + } + + public Task ExecuteAsync( + SystemId systemId, + Func> operation, + CancellationToken cancellationToken = default) + { + return DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var scope = await ResolveAsync(systemId, cancellationToken); + return await operation(scope); + }, _options, cancellationToken, _logger); + } + + public Task ExecuteGlobalAsync( + Func operation, + CancellationToken cancellationToken = default) + { + return DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var keyspace = _keyspaceResolver.ResolveGlobalKeyspace(); + var scope = new ScyllaGlobalScope(session, keyspace); + await operation(scope); + }, _options, cancellationToken, _logger); + } + + public Task ExecuteGlobalAsync( + Func> operation, + CancellationToken cancellationToken = default) + { + return DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + var keyspace = _keyspaceResolver.ResolveGlobalKeyspace(); + var scope = new ScyllaGlobalScope(session, keyspace); + return await operation(scope); + }, _options, cancellationToken, _logger); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaServiceCollectionExtensions.cs new file mode 100644 index 00000000..101a34c7 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaServiceCollectionExtensions.cs @@ -0,0 +1,52 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.Scylla.Fixups; +using Interfold.Infrastructure.Scylla.Repository; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Infrastructure.Scylla; + +public static class ScyllaServiceCollectionExtensions +{ + private static readonly Action Registration = PersistenceRegistration.Create(PersistenceMode.ScyllaPostgres, AddScyllaPersistence); + public static void Register() => Registration(); + + private static IServiceCollection AddScyllaPersistence( + IServiceCollection services, + PersistenceConfiguration options) + { + var pipeline = services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddHostedService() + // Ordering matters: migrations first (grants + column additions), then fixup + // uses the app-user session to normalise legacy color rows. + .AddHostedService(); + + return pipeline; + } +} \ No newline at end of file diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaSessionProvider.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaSessionProvider.cs new file mode 100644 index 00000000..53a0d4a6 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaSessionProvider.cs @@ -0,0 +1,60 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +public interface IScyllaSessionProvider +{ + Task GetSessionAsync(CancellationToken cancellationToken = default); +} + +public sealed class ScyllaSessionProvider : IScyllaSessionProvider +{ + private readonly PersistenceConfiguration _options; + private readonly IScyllaConfigResolver _configResolver; + private readonly Lazy> _session; + + public ScyllaSessionProvider( + IOptions options, + IScyllaConfigResolver configResolver) + { + _options = options.Value; + _configResolver = configResolver; + _session = new Lazy>(ConnectAsync); + } + + public Task GetSessionAsync(CancellationToken cancellationToken = default) => _session.Value; + + private async Task ConnectAsync() + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var contactPoints = await _configResolver.GetContactPointsAsync(); + var datacenter = await _configResolver.GetDatacenterAsync(); + var username = await _configResolver.GetUsernameAsync(); + var password = await _configResolver.GetPasswordAsync(); + var keyspace = _configResolver.GetKeyspace(); + var port = await _configResolver.GetPortAsync(); + + var builder = Cluster.Builder() + .AddContactPoints(contactPoints) + .WithPort(port) + .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy(datacenter)) + .WithReconnectionPolicy(new ExponentialReconnectionPolicy(1000, 30000)) + .WithQueryTimeout(10000) + .WithSocketOptions(new SocketOptions() + .SetConnectTimeoutMillis(10000) + .SetKeepAlive(true)); + + if (!string.IsNullOrWhiteSpace(username)) + { + builder = builder.WithCredentials(username, password); + } + + var cluster = builder.Build(); + return await cluster.ConnectAsync(keyspace); + }, _options); + } +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/ScyllaUserRegistryRegionContext.cs b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaUserRegistryRegionContext.cs new file mode 100644 index 00000000..a144cdd9 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/ScyllaUserRegistryRegionContext.cs @@ -0,0 +1,204 @@ +using System.Collections.Concurrent; +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Persistence; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.Scylla; + +/// backed by global.user_registry with a +/// bounded in-process cache. Falls back to the locally configured default region when +/// the registry row is absent or the Scylla session is not yet ready. +public sealed class ScyllaUserRegistryRegionContext : IRegionContext +{ + private const int MaxCacheSize = 1024; + + private readonly IScyllaSessionProvider _sessionProvider; + private readonly PersistenceConfiguration _options; + private readonly ILogger _logger; + private readonly ConcurrentDictionary _cache = new(StringComparer.Ordinal); + private readonly Lazy _currentRegion; + + public ScyllaKeyspace CurrentRegion => _currentRegion.Value; + + public ScyllaUserRegistryRegionContext( + IScyllaSessionProvider sessionProvider, + IOptions options, + ILogger logger) + { + _sessionProvider = sessionProvider; + _options = options.Value; + _logger = logger; + // ScyllaKeyspace on the options is the enum-typed single source of truth. + _currentRegion = new Lazy(() => _options.ScyllaKeyspace); + } + + public ScyllaKeyspace ResolveUserRegion(SystemId systemId) => ResolveUserRegionCore(systemId); + + // String-typed core shared with the async / RegisterRegion siblings; the interface + // only exposes the typed SystemId overload. + private ScyllaKeyspace ResolveUserRegionCore(string systemId) + { + if (string.IsNullOrWhiteSpace(systemId)) + return CurrentRegion; + + var (cacheKey, handle) = HandleForLookup(systemId); + + if (_cache.TryGetValue(cacheKey, out var cached)) + return cached; + + // Sync path: driver never marshals back to a captured SynchronizationContext, + // so GetAwaiter().GetResult() cannot deadlock a request thread. + try + { + var region = LookupAsync(handle, systemId).GetAwaiter().GetResult(); + if (TryParseRegion(cacheKey, region, out var parsed)) + { + StoreInCache(cacheKey, parsed); + return parsed; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Region lookup for system {SystemId} failed; falling back to default region.", cacheKey); + } + + return CurrentRegion; + } + + /// Async variant for hot paths that already have an async context. + /// Falls back to when the registry row is absent. + public async Task ResolveUserRegionAsync( + string systemId, + CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(systemId)) + return CurrentRegion; + + var (cacheKey, handle) = HandleForLookup(systemId); + + if (_cache.TryGetValue(cacheKey, out var cached)) + return cached; + + var region = await LookupAsync(handle, systemId, cancellationToken); + if (TryParseRegion(cacheKey, region, out var parsed)) + { + StoreInCache(cacheKey, parsed); + return parsed; + } + + return CurrentRegion; + } + + /// Populates the cache for a user whose home region is already known + /// (e.g. after account registration). + public void RegisterRegion(string systemId, ScyllaKeyspace region) + { + if (string.IsNullOrWhiteSpace(systemId)) + return; + + var (cacheKey, _) = HandleForLookup(systemId); + StoreInCache(cacheKey, region); + } + + private bool TryParseRegion(string systemId, string? raw, out ScyllaKeyspace region) + { + region = default; + if (string.IsNullOrWhiteSpace(raw)) + return false; + + try + { + region = EnumWireExtensions.ParseScyllaKeyspace(raw); + return true; + } + catch (InvalidOperationException ex) + { + // Never poison the cache or crash the caller on a corrupt registry row. + _logger.LogWarning(ex, + "Registry region '{Region}' for system {SystemId} is not a known keyspace; falling back to default region.", + raw, systemId); + return false; + } + } + + private async Task LookupAsync( + UserRegistryLookup? handle, + string originalInput, + CancellationToken cancellationToken = default) + { + return await DatabaseTransientRetry.ExecuteScyllaAsync(async () => + { + var session = await _sessionProvider.GetSessionAsync(cancellationToken); + + // handle == null when TryParse rejected the input — route the whole original + // string through user_id so a rejected handle stays an opaque bare-id lookup. + var (column, value) = handle switch + { + { Kind: UserRegistryLookupKind.Username } h => (UserRegistryLookupColumn.Username, h.RawId), + { Kind: UserRegistryLookupKind.Discord } h => (UserRegistryLookupColumn.DiscordId, h.RawId), + { Kind: UserRegistryLookupKind.Region } h => (UserRegistryLookupColumn.UserId, h.RawId), + { Kind: UserRegistryLookupKind.Id } h => (UserRegistryLookupColumn.UserId, h.RawId), + _ => (UserRegistryLookupColumn.UserId, originalInput), + }; + + var query = new SimpleStatement( + $"SELECT region FROM {ScyllaGlobalKeyspace.Name}.user_registry WHERE {ColumnName(column)} = ? LIMIT 1", + value); + + var row = (await session.ExecuteAsync(query)).FirstOrDefault(); + return row?.GetValue("region"); + }, _options, cancellationToken, _logger); + } + + private void StoreInCache(string key, ScyllaKeyspace region) + { + if (_cache.Count >= MaxCacheSize) + { + // Coarse eviction — clear on overflow. Good enough until memory pressure warrants a true LRU. + _cache.Clear(); + } + + _cache[key] = region; + } + + // Cache key collapses id-shaped inputs onto RawId ("abcdefg", "nam:abcdefg", "id:abcdefg" + // all identify the same user_registry row); username / Discord keys keep their prefix. + private static (string cacheKey, UserRegistryLookup? handle) HandleForLookup(string systemId) + { + if (!UserRegistryLookup.TryParse(systemId, out var parsed)) + { + return (systemId, null); + } + + var cacheKey = parsed.Kind switch + { + UserRegistryLookupKind.Region => parsed.RawId, + UserRegistryLookupKind.Id => parsed.RawId, + _ => parsed.OriginalValue, + }; + + return (cacheKey, parsed); + } + + // Ties the UserRegistryLookupKind switch to the CQL column universe so a new lookup + // kind can't drift from the SQL builder. + private enum UserRegistryLookupColumn + { + Username, + DiscordId, + UserId, + } + + private static string ColumnName(UserRegistryLookupColumn column) => column switch + { + UserRegistryLookupColumn.Username => "username", + UserRegistryLookupColumn.DiscordId => "discord_id", + UserRegistryLookupColumn.UserId => "user_id", + _ => throw new ArgumentOutOfRangeException(nameof(column), column, "Unknown user-registry lookup column."), + }; +} diff --git a/infrastructure/Interfold.Infrastructure.Scylla/UserRegistryLookup.cs b/infrastructure/Interfold.Infrastructure.Scylla/UserRegistryLookup.cs new file mode 100644 index 00000000..328d37bb --- /dev/null +++ b/infrastructure/Interfold.Infrastructure.Scylla/UserRegistryLookup.cs @@ -0,0 +1,100 @@ +using System.Diagnostics.CodeAnalysis; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Infrastructure.Scylla; + +/// Which user_registry column a parsed handle routes to. Wire prefixes +/// map 1:1 onto this enum via . +internal enum UserRegistryLookupKind +{ + /// Region-scoped principal id, e.g. "nam:abcdefg". + Region, + + /// Username lookup, e.g. "username:alice". + Username, + + /// Discord snowflake lookup, e.g. "discord:1234567". + Discord, + + /// Bare 7-char system id or explicit "id:" prefix; both route to + /// user_registry.user_id. Id is the strict "this is a system id, not a + /// username or Discord snowflake" assertion. + Id, +} + +/// Pre-resolution handle to a user_registry row, parsed from a raw +/// client-supplied string. Scylla-only — friend-request routing uses the tighter +/// . +/// +/// Strict rejection of unknown non-region prefixes. Inputs like +/// "xxx:abcdefg" return from . +/// Callers must treat a rejected input as an opaque bare id and query the registry +/// with the whole string (not the after-colon slice). +internal readonly record struct UserRegistryLookup +{ + public UserRegistryLookupKind Kind { get; } + + /// Lookup value: after-colon slice for prefixed inputs, whole input for + /// bare . + public string RawId { get; } + + /// The untouched input, retained for logging / audit. + public string OriginalValue { get; } + + private UserRegistryLookup(UserRegistryLookupKind kind, string rawId, string originalValue) + { + Kind = kind; + RawId = rawId; + OriginalValue = originalValue; + } + + /// Parse a raw handle string. Never throws; returns + /// for null / blank / bare-prefix ("nam:") / unknown-prefix inputs. + public static bool TryParse([NotNullWhen(true)] string? input, out UserRegistryLookup handle) + { + handle = default; + if (string.IsNullOrWhiteSpace(input)) + { + return false; + } + + var separator = input.IndexOf(':'); + if (separator < 0) + { + handle = new UserRegistryLookup(UserRegistryLookupKind.Id, input, input); + return true; + } + + // Reject ":foo" / "foo:" so the client sees a real error instead of a silent bare-id lookup. + if (separator == 0 || separator >= input.Length - 1) + { + return false; + } + + var prefix = input[..separator]; + var afterColon = input[(separator + 1)..]; + + // Region prefixes win — they are the canonical scoped-id shape. + if (EnumWire.TryParse(prefix, out _)) + { + handle = new UserRegistryLookup(UserRegistryLookupKind.Region, afterColon, input); + return true; + } + + // Discriminator prefixes are wire-lowercase; case-insensitive client, strict allow-list. + switch (prefix.ToLowerInvariant()) + { + case "id": + handle = new UserRegistryLookup(UserRegistryLookupKind.Id, afterColon, input); + return true; + case "username": + handle = new UserRegistryLookup(UserRegistryLookupKind.Username, afterColon, input); + return true; + case "discord": + handle = new UserRegistryLookup(UserRegistryLookupKind.Discord, afterColon, input); + return true; + default: + return false; + } + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/FirebaseFCMService.cs b/infrastructure/Interfold.Infrastructure/Coordination/FirebaseFCMService.cs new file mode 100644 index 00000000..74642959 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/FirebaseFCMService.cs @@ -0,0 +1,303 @@ +using FirebaseAdmin; +using FirebaseAdmin.Messaging; +using Google.Apis.Auth.OAuth2; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Secrets; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Polly; +using Polly.Retry; +using Interfold.Shared.Contracts; + +namespace Interfold.Infrastructure.Coordination; + +/// Real FCM v1 sender for the fronting-changed push flow. DI wires this only +/// when the primary role is active and fcm:service_account_json is present; +/// otherwise takes over. +public sealed class FirebaseFCMService : IFCMService, IDisposable +{ + // Process-local registry key for FirebaseApp.Create/GetInstance. + private const string FirebaseAppName = "interfold-fcm"; + + // FCM v1 multicast cap: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send + private const int FcmMulticastMax = 500; + + private readonly IOptions _options; + private readonly INotificationTokenRepository _tokens; + private readonly IAccountRepository _accounts; + private readonly IAlterRepository _alters; + private readonly ILogger _logger; + private readonly ResiliencePipeline _resilience; + private readonly SemaphoreSlim _initGate = new(1, 1); + private FirebaseMessaging? _messaging; + private FirebaseApp? _app; + + public FirebaseFCMService( + IOptions options, + INotificationTokenRepository tokens, + IAccountRepository accounts, + IAlterRepository alters, + ILogger logger) + { + _options = options; + _tokens = tokens; + _accounts = accounts; + _alters = alters; + _logger = logger; + _resilience = BuildResiliencePipeline(logger); + } + + public async Task NotifyFrontingChangedAsync( + SystemId systemId, + IReadOnlyList currentAlterIds, + CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(systemId.Value)) + return; + + var friendGroups = await _tokens.ListTokensForFriendsOfAsync(systemId, cancellationToken).ConfigureAwait(false); + if (friendGroups.Count == 0) + { + _logger.LogDebug("[fcm] system={SystemId} has no friend tokens to notify.", systemId); + return; + } + + var messaging = await EnsureMessagingAsync(cancellationToken).ConfigureAwait(false); + if (messaging is null) + return; + + var profile = await _accounts.GetPublicProfileAsync(systemId, cancellationToken).ConfigureAwait(false); + var frontingDisplayName = profile?.Username is { } username && !string.IsNullOrWhiteSpace(username.Value) + ? username.Value + : "A friend"; + + // Relative path — each client resolves against its own origin. + var deepLink = $"/system/{Uri.EscapeDataString(systemId.Value)}"; + var frontEnded = currentAlterIds.Count == 0; + var currentAlterSet = frontEnded ? null : new HashSet(currentAlterIds); + + foreach (var friendGroup in friendGroups) + { + IReadOnlyList visibleIds; + string body; + if (frontEnded) + { + visibleIds = Array.Empty(); + body = "No one is fronting"; + } + else + { + var guarded = await _alters + .ListGuardedAsync(systemId, friendGroup.FriendSystemId, cancellationToken) + .ConfigureAwait(false); + var visibleAlters = guarded + .Where(a => currentAlterSet!.Contains(a.Id)) + .ToArray(); + + if (visibleAlters.Length == 0) + { + // Skip rather than leak "something changed" via an empty payload. + _logger.LogDebug( + "[fcm] Skipping friend={FriendSystemId} for system={SystemId}: no visible fronting alters.", + friendGroup.FriendSystemId, systemId); + continue; + } + + visibleIds = visibleAlters.Select(a => a.Id).ToArray(); + body = string.Join(", ", visibleAlters.Select(a => a.Name)); + } + + var alterCsv = string.Join(",", visibleIds); + var data = new Dictionary(StringComparer.Ordinal) + { + [FcmPayloadKeys.Type] = FcmNotificationTypes.FrontingChanged, + [FcmPayloadKeys.SystemId] = systemId.Value, + [FcmPayloadKeys.AlterIds] = alterCsv, + [FcmPayloadKeys.DeepLink] = deepLink, + }; + + foreach (var chunk in friendGroup.Tokens.Chunk(FcmMulticastMax)) + { + var message = new MulticastMessage + { + Tokens = chunk.Select(t => t.Value).ToArray(), + Notification = new Notification + { + Title = $"Front update: {frontingDisplayName}", + Body = body, + }, + Data = data, + Webpush = new WebpushConfig + { + FcmOptions = new WebpushFcmOptions { Link = deepLink }, + }, + }; + + BatchResponse? response; + try + { + response = await _resilience.ExecuteAsync( + async ct => await messaging.SendEachForMulticastAsync(message, ct).ConfigureAwait(false), + cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + throw; + } + catch (Exception ex) + { + _logger.LogWarning(ex, + "[fcm] Multicast send failed for system={SystemId} friend={FriendSystemId} tokens={TokenCount}", + systemId, friendGroup.FriendSystemId, chunk.Length); + continue; + } + + await PruneInvalidTokensAsync(chunk, response, cancellationToken).ConfigureAwait(false); + } + } + } + + // Prune tokens flagged as permanently invalid (Unregistered / InvalidArgument); other + // failures are logged and left in place so the next flush retries. + private async Task PruneInvalidTokensAsync( + IReadOnlyList tokens, + BatchResponse response, + CancellationToken cancellationToken) + { + if (response.FailureCount == 0) + return; + + for (var i = 0; i < response.Responses.Count; i++) + { + var single = response.Responses[i]; + if (single.IsSuccess) continue; + + var token = tokens[i]; + + if (single.Exception is FirebaseMessagingException fmEx && + (fmEx.MessagingErrorCode == MessagingErrorCode.Unregistered || + fmEx.MessagingErrorCode == MessagingErrorCode.InvalidArgument)) + { + try + { + await _tokens.RemoveAsync(token, cancellationToken).ConfigureAwait(false); + _logger.LogInformation( + "[fcm] Pruned invalid token ({Code}). token_prefix={TokenPrefix}", + fmEx.MessagingErrorCode, TokenPrefix(token.Value)); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + _logger.LogWarning(ex, + "[fcm] Failed to prune invalid token. token_prefix={TokenPrefix}", + TokenPrefix(token.Value)); + } + } + else + { + _logger.LogWarning(single.Exception, + "[fcm] Per-token delivery failure. token_prefix={TokenPrefix} code={Code}", + TokenPrefix(token.Value), + (single.Exception as FirebaseMessagingException)?.MessagingErrorCode); + } + } + } + + private static string TokenPrefix(string token) => + string.IsNullOrEmpty(token) ? "***" + : token.Length <= 6 ? "***" + : token[..6]; + + // FirebaseApp.Create throws on duplicate name — gate first-use and treat + // "already exists" as a race we lost. + private async Task EnsureMessagingAsync(CancellationToken ct) + { + var existing = Volatile.Read(ref _messaging); + if (existing is not null) return existing; + + await _initGate.WaitAsync(ct).ConfigureAwait(false); + try + { + if (_messaging is not null) return _messaging; + + var serviceAccountJson = _options.Value.ServiceAccountJson; + if (string.IsNullOrWhiteSpace(serviceAccountJson)) + { + // Defensive: the snapshot is startup-populated and shouldn't change mid-process. + _logger.LogError( + "[fcm] {Key} disappeared between DI-graph build and first send. " + + "Restart the API after re-seeding the row, or drop it and let the factory pick NullFCMService.", + SecretsStoreKeys.FcmServiceAccountJson); + return null; + } + + var options = new AppOptions + { + Credential = CredentialFactory + .FromJson(serviceAccountJson) + .ToGoogleCredential(), + }; + + try + { + _app = FirebaseApp.Create(options, FirebaseAppName); + } + catch (ArgumentException) + { + _app = FirebaseApp.GetInstance(FirebaseAppName); + } + + _messaging = FirebaseMessaging.GetMessaging(_app); + _logger.LogInformation("[fcm] Firebase Admin initialised for FCM v1 send (app={AppName}).", FirebaseAppName); + return _messaging; + } + finally + { + _initGate.Release(); + } + } + + // Hand-rolled — FirebaseAdmin owns its HttpClient so Microsoft.Extensions.Http.Resilience + // can't wrap it. Retries only the two transient FCM codes + transport failures. + private static ResiliencePipeline BuildResiliencePipeline(ILogger logger) => + new ResiliencePipelineBuilder() + .AddRetry(new RetryStrategyOptions + { + ShouldHandle = new PredicateBuilder() + .Handle(ex => + ex.MessagingErrorCode == MessagingErrorCode.Unavailable || + ex.MessagingErrorCode == MessagingErrorCode.Internal) + .Handle() + .Handle(), + BackoffType = DelayBackoffType.Exponential, + UseJitter = true, + MaxRetryAttempts = 3, + Delay = TimeSpan.FromMilliseconds(500), + MaxDelay = TimeSpan.FromSeconds(5), + OnRetry = args => + { + logger.LogWarning(args.Outcome.Exception, + "[fcm] Transient send failure, retrying attempt {Attempt} in {Delay}ms.", + args.AttemptNumber + 1, args.RetryDelay.TotalMilliseconds); + return ValueTask.CompletedTask; + }, + }) + .Build(); + + public void Dispose() + { + _initGate.Dispose(); + try + { + _app?.Delete(); + } + catch + { + // Double-dispose race — harmless at shutdown. + } + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/FrontNotifierBackgroundService.cs b/infrastructure/Interfold.Infrastructure/Coordination/FrontNotifierBackgroundService.cs new file mode 100644 index 00000000..3a3b9f59 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/FrontNotifierBackgroundService.cs @@ -0,0 +1,79 @@ +using System.Collections.Concurrent; +using Interfold.Fronting.Contracts.Events; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// Primary-node background service that batches fronting-state-change signals +/// and flushes them every 5 s (mirrors legacy Octocon.Global.FrontNotifier). +public sealed class FrontNotifierBackgroundService( + IClusterEventBus eventBus, + IFrontingRepository frontingRepository, + IFCMService fcmService, + ILogger logger) + : BackgroundService +{ + private readonly ConcurrentDictionary _pending = new(); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + logger.LogInformation("FrontNotifierBackgroundService starting (primary node)."); + + var subscription = ConsumeEventsAsync(stoppingToken); + var flush = FlushLoopAsync(stoppingToken); + + await Task.WhenAll(subscription, flush).ConfigureAwait(false); + + logger.LogInformation("FrontNotifierBackgroundService stopped."); + } + + private async Task ConsumeEventsAsync(CancellationToken ct) + { + await foreach (var evt in eventBus.SubscribeAsync(ct).ConfigureAwait(false)) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + _pending.AddOrUpdate(evt.TargetSystemId, now, (_, _) => now); + } + } + + private async Task FlushLoopAsync(CancellationToken ct) + { + using var timer = new PeriodicTimer(TimeSpan.FromSeconds(5)); + + while (await timer.WaitForNextTickAsync(ct).ConfigureAwait(false)) + { + var cutoff = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - 10_000; + + foreach (var systemId in _pending.Keys) + { + if (_pending.TryGetValue(systemId, out var ts) && ts < cutoff && + _pending.TryRemove(systemId, out _)) + { + await FlushSystemAsync(systemId, ct).ConfigureAwait(false); + } + } + } + } + + private async Task FlushSystemAsync(SystemId systemId, CancellationToken ct) + { + try + { + var active = await frontingRepository.ListActiveAsync(systemId, ct).ConfigureAwait(false); + var alterIds = active.Select(f => f.Alter.Id).ToList(); + + await fcmService.NotifyFrontingChangedAsync(systemId, alterIds, ct).ConfigureAwait(false); + + logger.LogDebug( + "FrontNotifier flushed: system={SystemId}, alters={AlterCount}", systemId, alterIds.Count); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogError(ex, "FrontNotifier flush failed for system={SystemId}", systemId); + } + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/InProcessEventBus.cs b/infrastructure/Interfold.Infrastructure/Coordination/InProcessEventBus.cs new file mode 100644 index 00000000..a59c4e40 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/InProcessEventBus.cs @@ -0,0 +1,181 @@ +using System.Collections.Concurrent; +using System.Threading.Channels; +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// Single-node backed by +/// . Per-event-type subscription bags; targeted +/// subscriptions filtered at publish time. Channels use +/// AllowSynchronousContinuations = false so publisher threads never run subscriber +/// continuations inline (keeps HTTP-request threads free of socket-push latency). +public sealed class InProcessEventBus : IClusterEventBus, IDisposable +{ + private interface ITopicBag + { + void CompleteAll(); + } + + private sealed record Subscription(ChannelWriter Writer, ScopedSystemId? TargetSystemId) + where TEvent : class; + + private sealed class TopicBag : ITopicBag where TEvent : class + { + // Keyed by writer so an enumerator can self-remove on disposal without a separate id. + public readonly ConcurrentDictionary, Subscription> Subscriptions = new(); + + public void CompleteAll() + { + foreach (var sub in Subscriptions.Values) + { + sub.Writer.TryComplete(); + } + + Subscriptions.Clear(); + } + } + + private readonly ConcurrentDictionary _topics = new(); + + private TopicBag GetOrCreateBag() where TEvent : class + => (TopicBag)_topics.GetOrAdd(typeof(TEvent), _ => new TopicBag()); + + public async ValueTask PublishAsync(TEvent evt, CancellationToken ct = default) + where TEvent : class + { + if (!_topics.TryGetValue(typeof(TEvent), out var bag)) + { + return; + } + + var topicBag = (TopicBag)bag; + var targetedEvent = evt as ITargetedClusterEvent; + + foreach (var subscription in topicBag.Subscriptions.Values) + { + if (subscription.TargetSystemId is not null + && targetedEvent is not null + && subscription.TargetSystemId.Value != targetedEvent.TargetSystemId) + { + continue; + } + + try + { + await subscription.Writer.WriteAsync(evt, ct).ConfigureAwait(false); + } + catch (ChannelClosedException) + { + topicBag.Subscriptions.TryRemove(subscription.Writer, out _); + } + } + } + + /// Broadcast subscribe — instance-method overload so test fixtures that hold + /// the concrete type don't need to cast to . + public IAsyncEnumerable SubscribeAsync(CancellationToken ct = default) + where TEvent : class + => SubscribeAsync(targetSystemId: null, ct); + + public IAsyncEnumerable SubscribeAsync( + ScopedSystemId? targetSystemId, + CancellationToken ct = default) + where TEvent : class + { + var channel = Channel.CreateUnbounded(new UnboundedChannelOptions + { + SingleReader = true, + SingleWriter = false, + // Async-only continuations — a slow subscriber must not stall the publisher's + // foreach loop. + AllowSynchronousContinuations = false + }); + + var topicBag = GetOrCreateBag(); + topicBag.Subscriptions.TryAdd(channel.Writer, new Subscription(channel.Writer, targetSystemId)); + + return new ChannelAsyncEnumerable(channel, topicBag, ct); + } + + private sealed class ChannelAsyncEnumerable : IAsyncEnumerable + where TEvent : class + { + private readonly Channel _channel; + private readonly TopicBag _topicBag; + private readonly CancellationToken _ct; + + public ChannelAsyncEnumerable(Channel channel, TopicBag topicBag, CancellationToken ct) + { + _channel = channel; + _topicBag = topicBag; + _ct = ct; + } + + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(_ct, cancellationToken); + return new ChannelAsyncEnumerator(_channel, _topicBag, linkedCts); + } + + private sealed class ChannelAsyncEnumerator : IAsyncEnumerator, IAsyncDisposable + { + private readonly Channel _channel; + private readonly TopicBag _topicBag; + private readonly ChannelReader _reader; + private readonly ChannelWriter _writer; + private readonly CancellationTokenSource _cts; + private IAsyncEnumerator? _enumerator; + private bool _disposed; + + public ChannelAsyncEnumerator(Channel channel, TopicBag topicBag, CancellationTokenSource cts) + { + _channel = channel; + _topicBag = topicBag; + _reader = channel.Reader; + _writer = channel.Writer; + _cts = cts; + _enumerator = _reader.ReadAllAsync(_cts.Token).GetAsyncEnumerator(); + } + + public TEvent Current => _enumerator!.Current; + + public async ValueTask MoveNextAsync() + { + if (_disposed) return false; + try + { + return await _enumerator!.MoveNextAsync().ConfigureAwait(false); + } + catch (OperationCanceledException) + { + return false; + } + } + + public async ValueTask DisposeAsync() + { + if (_disposed) return; + _disposed = true; + _topicBag.Subscriptions.TryRemove(_writer, out _); + _writer.TryComplete(); + if (_enumerator != null) + { + await _enumerator.DisposeAsync().ConfigureAwait(false); + _enumerator = null; + } + _cts.Cancel(); + _cts.Dispose(); + } + } + } + + public void Dispose() + { + foreach (var bag in _topics.Values) + bag.CompleteAll(); + + _topics.Clear(); + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/InProcessImportJobQueue.cs b/infrastructure/Interfold.Infrastructure/Coordination/InProcessImportJobQueue.cs new file mode 100644 index 00000000..83a2c126 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/InProcessImportJobQueue.cs @@ -0,0 +1,47 @@ +using System.Runtime.CompilerServices; +using System.Threading.Channels; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Infrastructure.Coordination; + +/// Single-channel . Bounded at +/// ; enqueues wait rather than drop (a silent drop would +/// strand a claimed operation row without a worker run). Per-system serialisation lives +/// in the LWT mutex on active_import_by_system, so one channel + one worker is +/// sufficient. +public sealed class InProcessImportJobQueue : IImportJobQueue, IAsyncDisposable +{ + private const int DefaultCapacity = 256; + + private readonly Channel _channel; + + public InProcessImportJobQueue() + { + _channel = Channel.CreateBounded(new BoundedChannelOptions(DefaultCapacity) + { + SingleReader = true, + SingleWriter = false, + FullMode = BoundedChannelFullMode.Wait, + }); + } + + public ValueTask EnqueueAsync(ImportJobItem item, CancellationToken cancellationToken = default) + { + return _channel.Writer.WriteAsync(item, cancellationToken); + } + + public async IAsyncEnumerable ReadAllAsync( + [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + await foreach (var item in _channel.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false)) + { + yield return item; + } + } + + public ValueTask DisposeAsync() + { + _channel.Writer.TryComplete(); + return ValueTask.CompletedTask; + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/NodeRoleContext.cs b/infrastructure/Interfold.Infrastructure/Coordination/NodeRoleContext.cs new file mode 100644 index 00000000..a0048ba2 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/NodeRoleContext.cs @@ -0,0 +1,10 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// Stateless — role resolved once at startup. +public sealed class NodeRoleContext(NodeGroup role) : INodeRoleContext +{ + public NodeGroup Role { get; } = role; +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/NullFCMService.cs b/infrastructure/Interfold.Infrastructure/Coordination/NullFCMService.cs new file mode 100644 index 00000000..b4b9d4aa --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/NullFCMService.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Logging; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// No-op for deployments without a configured FCM +/// credential. Debug-level log so the notification pipeline is testable without real tokens. +public sealed class NullFCMService(ILogger logger) : IFCMService +{ + public Task NotifyFrontingChangedAsync( + SystemId systemId, + IReadOnlyList currentAlterIds, + CancellationToken cancellationToken = default) + { + logger.LogDebug( + "FCM stub: fronting changed for system={SystemId}, alters=[{AlterIds}]", + systemId, string.Join(",", currentAlterIds)); + + return Task.CompletedTask; + } +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/NullSingletonTaskOwner.cs b/infrastructure/Interfold.Infrastructure/Coordination/NullSingletonTaskOwner.cs new file mode 100644 index 00000000..f2128bbc --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/NullSingletonTaskOwner.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// No-op for auxiliary/sidecar nodes. +public sealed class NullSingletonTaskOwner : ISingletonTaskOwner +{ + public bool OwnsTask(SingletonTaskName taskName) => false; + + public Task RunIfOwnerAsync( + SingletonTaskName taskName, + Func work, + CancellationToken cancellationToken = default) + => Task.CompletedTask; +} diff --git a/infrastructure/Interfold.Infrastructure/Coordination/PrimaryOnlySingletonTaskOwner.cs b/infrastructure/Interfold.Infrastructure/Coordination/PrimaryOnlySingletonTaskOwner.cs new file mode 100644 index 00000000..c7197d4e --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Coordination/PrimaryOnlySingletonTaskOwner.cs @@ -0,0 +1,17 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Infrastructure.Coordination; + +/// for primary nodes — always owns, runs +/// work inline (mirrors the legacy Horde DynamicSupervisor pattern). +public sealed class PrimaryOnlySingletonTaskOwner : ISingletonTaskOwner +{ + public bool OwnsTask(SingletonTaskName taskName) => true; + + public Task RunIfOwnerAsync( + SingletonTaskName taskName, + Func work, + CancellationToken cancellationToken = default) + => work(cancellationToken); +} diff --git a/infrastructure/Interfold.Infrastructure/DependencyInjection/ClusterServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure/DependencyInjection/ClusterServiceCollectionExtensions.cs new file mode 100644 index 00000000..906bd251 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/DependencyInjection/ClusterServiceCollectionExtensions.cs @@ -0,0 +1,65 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Coordination; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Infrastructure.DependencyInjection; + +public static partial class ServiceCollectionExtensions +{ + /// Registers node-role and cluster coordination services. Call after + /// in Program.cs. + public static IServiceCollection AddInterfoldCluster( + this IServiceCollection services, + NodeGroup role) + { + services.AddSingleton(new NodeRoleContext(role)); + + services.AddSingleton(); + + // Per-system mutex lives in IImportOperationRepository (LWT on + // active_import_by_system), so one channel + one worker is sufficient. + services.AddSingleton(); + + // Both implementations are registered concretely so the factory below can pick. + services.AddSingleton(); + services.AddSingleton(); + + // Primary + seeded fcm:service_account_json → real sender; otherwise no-op. + services.AddSingleton(sp => + { + if (role != NodeGroup.Primary) + return sp.GetRequiredService(); + + var serviceAccountJson = sp.GetRequiredService>().Value.ServiceAccountJson; + + if (string.IsNullOrWhiteSpace(serviceAccountJson)) + { + sp.GetRequiredService>().LogInformation( + "[fcm] internal.secrets:{Key} not seeded — using NullFCMService (push disabled).", + SecretsStoreKeys.FcmServiceAccountJson); + return sp.GetRequiredService(); + } + + return sp.GetRequiredService(); + }); + + ISingletonTaskOwner taskOwner = role == NodeGroup.Primary + ? new PrimaryOnlySingletonTaskOwner() + : new NullSingletonTaskOwner(); + + services.AddSingleton(taskOwner); + + if (role == NodeGroup.Primary) + { + services.AddHostedService(); + } + + return services; + } +} diff --git a/infrastructure/Interfold.Infrastructure/DependencyInjection/ConfigurationServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure/DependencyInjection/ConfigurationServiceCollectionExtensions.cs new file mode 100644 index 00000000..0d8c7ba1 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/DependencyInjection/ConfigurationServiceCollectionExtensions.cs @@ -0,0 +1,193 @@ +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.DependencyInjection; + +/// Registers the Interfold strongly-typed options graph. Startup-only options +/// use ; live-reloadable ones are wired through +/// via . +public static class ConfigurationServiceCollectionExtensions +{ + public static IServiceCollection AddInterfoldOptions(this IServiceCollection services) + { + services.AddOptions() + .Configure(ApplyCluster) + .ValidateDataAnnotations() + .ValidateOnStart(); + + // Cross-field IValidatableObject fires at ValidateOnStart so malformed retry + // knobs / missing Postgres connection strings fail at boot, not first query. + services.AddOptions() + .Configure(ApplyPersistence) + .ValidateDataAnnotations() + .ValidateOnStart(); + + // Platform variants land via FirebaseClientSecretsPostConfigure; a malformed + // internal.secrets row surfaces at boot as an InvalidOperationException. + services.AddOptions() + .Configure(ApplyFirebaseClient) + .ValidateDataAnnotations() + .ValidateOnStart(); + + // No [Required] — absent internal.secrets row is a supported state (→ NullFCMService). + // ValidateOnStart still runs so FcmSecretsPostConfigure fires at boot. + services.AddOptions() + .Configure((opts, config) => config.GetSection(FcmConfiguration.SectionName).Bind(opts)) + .ValidateOnStart(); + + services.AddOptions() + .Configure(ApplyObservability) + .ValidateDataAnnotations() + .ValidateOnStart(); + + services.AddOptions() + .Configure(ApplyTrust) + .ValidateDataAnnotations() + .ValidateOnStart(); + + AddLiveReloadable(services, ApplyStorage) + .ValidateDataAnnotations() + .ValidateOnStart(); + + AddLiveReloadable(services, ApplySocket) + .ValidateDataAnnotations() + .ValidateOnStart(); + + // Startup-only — the ASP.NET Core default CorsPolicyProvider does not rebuild + // policies on reload. Per-entry [AbsoluteHttpUri] validation runs at boot. + services.AddOptions() + .Configure(ApplyCors) + .ValidateDataAnnotations() + .ValidateOnStart(); + + services.AddOptions() + .Configure(ApplyScyllaOverride) + .ValidateDataAnnotations() + .ValidateOnStart(); + + services.AddOptions() + .Configure(ApplyInMemorySecretsSeed) + .ValidateDataAnnotations() + .ValidateOnStart(); + + return services; + } + + // Configure() alone doesn't subscribe IOptionsMonitor to reload + // tokens — an IOptionsChangeTokenSource is needed to bridge the two. + private static OptionsBuilder AddLiveReloadable( + IServiceCollection services, + Action apply) + where TOptions : class + { + var builder = services.AddOptions().Configure(apply); + services.AddSingleton>(sp => + new ConfigurationChangeTokenSource( + Options.DefaultName, sp.GetRequiredService())); + return builder; + } + + internal static void ApplyCluster(ClusterConfiguration opts, IConfiguration config) + { + // Guard against OCTOCON_NODE_GROUP="" short-circuiting the FLY_PROCESS_GROUP + // fallback — EnvironmentVariablesConfigurationProvider surfaces "" as a real value. + opts.NodeGroup = EnumWireExtensions.ParseNodeGroup( + NullIfEmpty(config[OctoconEnvKeys.FlyProcessGroup]) + ?? NullIfEmpty(config[OctoconEnvKeys.NodeGroup])); + } + + internal static void ApplyPersistence(PersistenceConfiguration opts, IConfiguration config) + { + var keyspace = EnumWireExtensions.ParseScyllaKeyspace( + config[OctoconEnvKeys.ScyllaKeyspace] ?? "nam"); + opts.Mode = EnumWireExtensions.ParsePersistenceMode(config[OctoconEnvKeys.Persistence]); + opts.ScyllaKeyspace = keyspace; + opts.PostgresConnectionString = config[OctoconEnvKeys.PostgresConnection] + ?? "Host=localhost;Port=5432;Database=interfold;Username=interfold;Password=interfold"; + opts.IsSingleScyllaInstance = bool.TryParse(config[OctoconEnvKeys.SingleScyllaInstance], out var singleKs) && singleKs; + opts.DbRetryAttempts = TryParseInt(config[OctoconEnvKeys.DbRetryAttempts]) ?? 3; + opts.DbRetryInitialDelay = TimeSpan.FromMilliseconds( + TryParseInt(config[OctoconEnvKeys.DbRetryInitialDelayMs]) ?? 100); + opts.DbRetryMaxDelay = TimeSpan.FromMilliseconds( + TryParseInt(config[OctoconEnvKeys.DbRetryMaxDelayMs]) ?? 1500); + opts.HydrationMaxConcurrency = TryParseInt(config[OctoconEnvKeys.HydrationMaxConcurrency]) ?? 8; + } + + // Every platform variant is populated by FirebaseClientSecretsPostConfigure; nothing to + // bind from env. + private static void ApplyFirebaseClient(FirebaseClientConfiguration opts, IConfiguration config) + { + opts.Android = null; + opts.Ios = null; + opts.Web = null; + } + + private static void ApplyObservability(ObservabilityConfiguration opts, IConfiguration config) + { + // Empty "" would otherwise reach the exporter SDK as a bogus endpoint; null skips. + opts.OtlpEndpoint = NullIfEmpty(config[OctoconEnvKeys.OtlpEndpoint]); + } + + private static void ApplyTrust(TrustOptions opts, IConfiguration config) + { + opts.RootCaPath = NullIfEmpty(config[OctoconEnvKeys.TrustRootCaPath]); + opts.RootCaFingerprintPath = NullIfEmpty(config[OctoconEnvKeys.TrustRootCaFingerprintPath]); + } + + private static void ApplyStorage(StorageConfiguration opts, IConfiguration config) + { + opts.AvatarStorageRoot = NullIfEmpty(config[OctoconEnvKeys.AvatarStorageRoot]); + opts.AvatarPublicBase = NullIfEmpty(config[OctoconEnvKeys.AvatarPublicBase]); + } + + private static void ApplySocket(SocketConfiguration opts, IConfiguration config) + { + opts.BatchBytesThreshold = TryParseInt(config[OctoconEnvKeys.SocketBatchBytesThreshold]); + } + + // Trailing slashes are stripped for parity with ASP.NET Core's exact-string CORS matcher. + private static void ApplyCors(CorsOptions opts, IConfiguration config) + { + opts.AllowedOrigins = (config[OctoconEnvKeys.CorsAllowedOrigins] ?? string.Empty) + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(static origin => origin.TrimEnd('/')) + .Where(static origin => !string.IsNullOrWhiteSpace(origin)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + + private static void ApplyScyllaOverride(ScyllaOverrideOptions opts, IConfiguration config) + { + var contactPointsRaw = NullIfEmpty(config[OctoconEnvKeys.ScyllaContactPoints]); + opts.ContactPoints = contactPointsRaw is null + ? null + : contactPointsRaw + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + opts.Port = TryParseInt(config[OctoconEnvKeys.ScyllaPort]); + } + + private static void ApplyInMemorySecretsSeed(InMemorySecretsSeedOptions opts, IConfiguration config) + { + opts.EncryptionPepper = NullIfEmpty(config[OctoconEnvKeys.InMemorySecretsSeedEncryptionPepper]); + opts.AuthJwtEs256PrivatePem = NullIfEmpty(config[OctoconEnvKeys.InMemorySecretsSeedAuthJwtEs256PrivatePem]); + opts.AuthDeepLinkSecret = NullIfEmpty(config[OctoconEnvKeys.InMemorySecretsSeedAuthDeepLinkSecret]); + opts.AuthJwtRsa256PrivatePem = NullIfEmpty(config[OctoconEnvKeys.InMemorySecretsSeedAuthJwtRsa256PrivatePem]); + } + + private static int? TryParseInt(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return null; + + return int.TryParse(value, out var result) ? result : null; + } + + // Bridge for env-var="" landing as "" instead of null in IConfiguration. + private static string? NullIfEmpty(string? value) => + string.IsNullOrWhiteSpace(value) ? null : value; +} diff --git a/infrastructure/Interfold.Infrastructure/DependencyInjection/PersistenceRegistration.cs b/infrastructure/Interfold.Infrastructure/DependencyInjection/PersistenceRegistration.cs new file mode 100644 index 00000000..da64d079 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/DependencyInjection/PersistenceRegistration.cs @@ -0,0 +1,19 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Infrastructure.DependencyInjection; + +public static class PersistenceRegistration +{ + public static Action Create(PersistenceMode mode, Func factory) + { + var registration = new Lazy(() => + { + ServiceCollectionExtensions.AddPersistenceMode(mode, factory); + return true; + }); + + return () => _ = registration.Value; + } +} diff --git a/infrastructure/Interfold.Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs b/infrastructure/Interfold.Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..22b3cf2d --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs @@ -0,0 +1,92 @@ +using System.Collections.Concurrent; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Infrastructure.DependencyInjection; + +public static partial class ServiceCollectionExtensions +{ + private static readonly ConcurrentDictionary>> PersistenceReg = []; + private static readonly Lock PersistenceRegLock = new(); + + internal static void AddPersistenceMode(PersistenceMode persistenceMode, Func reg) + { + lock (PersistenceRegLock) + { + var regs = PersistenceReg.GetOrAdd(persistenceMode, _ => []); + regs.Add(reg); + } + } + + /// Registers persistence adapters for and layers the + /// caller's overrides onto the + /// pipeline. Mode-registration lambdas still take a synchronous snapshot because a few + /// (e.g. InMemoryRegionContext) capture ScyllaKeyspace at registration time. + public static IServiceCollection AddInterfoldPersistence( + this IServiceCollection services, + PersistenceMode mode, + PersistenceConfiguration configuration + ) => AddInterfoldPersistence(services, mode, cfg => + { + cfg.Mode = configuration.Mode; + cfg.ScyllaKeyspace = configuration.ScyllaKeyspace; + cfg.PostgresConnectionString = configuration.PostgresConnectionString; + cfg.IsSingleScyllaInstance = configuration.IsSingleScyllaInstance; + cfg.DbRetryAttempts = configuration.DbRetryAttempts; + cfg.DbRetryInitialDelay = configuration.DbRetryInitialDelay; + cfg.DbRetryMaxDelay = configuration.DbRetryMaxDelay; + cfg.HydrationMaxConcurrency = configuration.HydrationMaxConcurrency; + }); + + public static IServiceCollection AddInterfoldPersistence( + this IServiceCollection services, + PersistenceMode mode, + Action? configure = null + ) + { + // Env-bound values live in IOptions; caller overrides + // are captured here for the mode-registration lambdas. + var options = new PersistenceConfiguration(); + configure?.Invoke(options); + + // Layer caller overrides onto IOptions so both views agree. + if (configure is not null) + { + services.PostConfigure(configure); + } + + if (!Enum.IsDefined(mode)) + { + throw new InvalidOperationException($"Unsupported persistence mode: {mode}"); + } + + if (PersistenceReg.TryGetValue(mode, out var list)) + { + foreach (Func func in list) + { + func(services, options); + } + + return services; + } + + throw new InvalidOperationException($"Persistence mode has not yet been implemented: {mode}"); + } + + // Every domain command-handler singleton previously registered here has moved to its + // owning feature module's AddModule extension: + // Alters -> Interfold.Alters.Api.DependencyInjection.AddAltersModule + // Auth -> Interfold.Auth.Api.DependencyInjection.AddAuthModule + // Fronting -> Interfold.Fronting.Api.DependencyInjection.AddFrontingModule + // Friendships -> Interfold.Friendships.Api.DependencyInjection.AddFriendshipsModule + // Journals -> Interfold.Journals.Api.DependencyInjection.AddJournalsModule + // Polls -> Interfold.Polls.Api.DependencyInjection.AddPollsModule + // Settings -> Interfold.Settings.Api.DependencyInjection.AddSettingsModule + // Tags -> Interfold.Tags.Api.DependencyInjection.AddTagsModule + // AddInterfoldDomainHandlers itself is retained as a no-op for wiring symmetry with the + // per-feature AddModule extensions and to give a place for future cross-cutting + // domain-handler registrations should they ever exist again. + public static IServiceCollection AddInterfoldDomainHandlers(this IServiceCollection services) => services; +} + diff --git a/infrastructure/Interfold.Infrastructure/Interfold.Infrastructure.csproj b/infrastructure/Interfold.Infrastructure/Interfold.Infrastructure.csproj new file mode 100644 index 00000000..463a4f4e --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Interfold.Infrastructure.csproj @@ -0,0 +1,51 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/infrastructure/Interfold.Infrastructure/Persistence/ConcurrentProjection.cs b/infrastructure/Interfold.Infrastructure/Persistence/ConcurrentProjection.cs new file mode 100644 index 00000000..6ecc4a3b --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Persistence/ConcurrentProjection.cs @@ -0,0 +1,29 @@ +namespace Interfold.Infrastructure.Persistence; + +public static class ConcurrentProjection +{ + public static async Task> SelectWithConcurrencyAsync( + IEnumerable source, + int maxConcurrency, + Func> selector, + CancellationToken cancellationToken) + { + var concurrency = Math.Max(1, maxConcurrency); + using var semaphore = new SemaphoreSlim(concurrency); + + var tasks = source.Select(async item => + { + await semaphore.WaitAsync(cancellationToken); + try + { + return await selector(item); + } + finally + { + semaphore.Release(); + } + }); + + return (await Task.WhenAll(tasks)).ToList(); + } +} diff --git a/infrastructure/Interfold.Infrastructure/Persistence/DatabaseTransientRetry.cs b/infrastructure/Interfold.Infrastructure/Persistence/DatabaseTransientRetry.cs new file mode 100644 index 00000000..cef3db38 --- /dev/null +++ b/infrastructure/Interfold.Infrastructure/Persistence/DatabaseTransientRetry.cs @@ -0,0 +1,122 @@ +using Cassandra; +using Interfold.Shared.Contracts.Configuration; +using Microsoft.Extensions.Logging; +using Npgsql; + +namespace Interfold.Infrastructure.Persistence; + +internal static class DatabaseTransientRetry +{ + private const int MaxBackoffExponent = 8; + + public static Task ExecuteScyllaAsync( + Func operation, + PersistenceConfiguration options, + CancellationToken cancellationToken = default, + ILogger? logger = null + ) => ExecuteAsync(operation, options, IsScyllaTransient, cancellationToken, logger); + + public static Task ExecuteScyllaAsync( + Func> operation, + PersistenceConfiguration options, + CancellationToken cancellationToken = default, + ILogger? logger = null + ) => ExecuteAsync(operation, options, IsScyllaTransient, logger, cancellationToken); + + public static Task ExecutePostgresAsync( + Func operation, + PersistenceConfiguration options, + CancellationToken cancellationToken = default, + ILogger? logger = null + ) => ExecuteAsync(operation, options, IsPostgresTransient, cancellationToken, logger); + + public static Task ExecutePostgresAsync( + Func> operation, + PersistenceConfiguration options, + CancellationToken cancellationToken = default, + ILogger? logger = null + ) => ExecuteAsync(operation, options, IsPostgresTransient, logger, cancellationToken); + + private static async Task ExecuteAsync( + Func operation, + PersistenceConfiguration options, + Func isTransient, + CancellationToken cancellationToken, + ILogger? logger + ) + { + await ExecuteAsync(async () => + { + await operation(); + return true; + }, options, isTransient, logger, cancellationToken); + } + + private static async Task ExecuteAsync(Func> operation, + PersistenceConfiguration options, + Func isTransient, + ILogger? logger, + CancellationToken cancellationToken) + { + var attempts = Math.Max(1, options.DbRetryAttempts); + var initialDelay = options.DbRetryInitialDelay <= TimeSpan.Zero + ? TimeSpan.FromMilliseconds(1) + : options.DbRetryInitialDelay; + var maxDelay = options.DbRetryMaxDelay < initialDelay + ? initialDelay + : options.DbRetryMaxDelay; + + for (var attempt = 1; ; attempt++) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + return await operation(); + } + catch (Exception ex) when (attempt < attempts && isTransient(ex)) + { + var delay = ComputeBackoff(attempt, initialDelay, maxDelay); + logger?.LogWarning(ex, + "Transient database error on attempt {Attempt}/{MaxAttempts}; retrying in {DelayMs} ms.", + attempt, attempts, delay.TotalMilliseconds); + await Task.Delay(delay, cancellationToken); + } + catch (Exception ex) + { + logger?.LogError(ex, + "Database operation failed on attempt {Attempt}/{MaxAttempts}.", + attempt, attempts); + throw; + } + } + } + + private static TimeSpan ComputeBackoff(int attempt, TimeSpan initialDelay, TimeSpan maxDelay) + { + var exponent = Math.Min(attempt - 1, MaxBackoffExponent); + var baseTicks = initialDelay.Ticks * (1L << exponent); + var cappedTicks = Math.Min(baseTicks, maxDelay.Ticks); + // Jitter [capped/2, capped] to spread reconnect storms. + var lower = cappedTicks / 2L; + var jitteredTicks = Random.Shared.NextInt64(lower, cappedTicks + 1); + return TimeSpan.FromTicks(jitteredTicks); + } + + private static bool IsScyllaTransient(Exception exception) => exception switch + { + NoHostAvailableException => true, + OperationTimedOutException => true, + ReadTimeoutException => true, + WriteTimeoutException => true, + TimeoutException => true, + _ => false + }; + + private static bool IsPostgresTransient(Exception exception) => exception switch + { + NpgsqlException npgsqlException => npgsqlException.IsTransient || npgsqlException.InnerException is System.IO.EndOfStreamException, + TimeoutException => true, + _ => false + }; +} diff --git a/lib/cluster/strategy/tailscale.ex b/lib/cluster/strategy/tailscale.ex deleted file mode 100644 index 00e88d23..00000000 --- a/lib/cluster/strategy/tailscale.ex +++ /dev/null @@ -1,110 +0,0 @@ -defmodule Cluster.Strategy.Tailscale do - @moduledoc """ - Cluster strategy for connecting Elixir nodes over Tailscale. Modified to consider devices by a given tag instead of their hostname. - - config :libcluster, - topologies: [ - tailscale: [ - strategy: #{__MODULE__}, - config: [ - authkey: "", - tailnet: "", - tag: "", - appname: "" - ] - ] - ] - - """ - use GenServer - alias Cluster.Strategy.State - - @polling_interval 30_000 - - def start_link(args), do: GenServer.start_link(__MODULE__, args) - - @impl GenServer - def init([%State{meta: nil} = state]) do - init([%State{state | :meta => MapSet.new()}]) - end - - def init([%State{} = state]) do - {:ok, load(state)} - end - - @impl GenServer - def handle_info(:timeout, state) do - handle_info(:load, state) - end - - def handle_info(:load, %State{} = state) do - {:noreply, load(state)} - end - - def handle_info(_, state) do - {:noreply, state} - end - - defp load(%State{} = state) do - nodes = - state - |> get_nodes() - |> disconnect_nodes(state) - |> connect_nodes(state) - - Process.send_after(self(), :load, @polling_interval) - %{state | meta: nodes} - end - - defp get_nodes(%State{config: config}) do - tag = Keyword.fetch!(config, :tag) - appname = Keyword.fetch!(config, :appname) - - Octocon.Utils.Tailscale.list_devices() - |> Enum.filter(fn device -> - ("tag:" <> tag) in (device["tags"] || []) && device["connectedToControl"] == true - end) - |> Enum.map(&List.first(&1["addresses"])) - |> Enum.map(&"#{appname}@#{&1}") - |> Enum.map(&String.to_atom/1) - |> MapSet.new() - end - - defp disconnect_nodes(nodes, %State{} = state) do - removed = MapSet.difference(state.meta, nodes) - - case Cluster.Strategy.disconnect_nodes( - state.topology, - state.disconnect, - state.list_nodes, - MapSet.to_list(removed) - ) do - :ok -> - nodes - - {:error, bad_nodes} -> - # Add back the nodes we couldn't remove - Enum.reduce(bad_nodes, nodes, fn {n, _}, acc -> - MapSet.put(acc, n) - end) - end - end - - defp connect_nodes(nodes, %State{} = state) do - case Cluster.Strategy.connect_nodes( - state.topology, - state.connect, - state.list_nodes, - MapSet.to_list(nodes) - ) do - :ok -> - nodes - - {:error, bad_nodes} -> - # Remove the nodes we couldn't add - Enum.reduce(bad_nodes, nodes, fn {n, _}, acc -> - MapSet.delete(acc, n) - end) - end - end -end diff --git a/lib/nosedrum.ex b/lib/nosedrum.ex deleted file mode 100644 index 251e09f3..00000000 --- a/lib/nosedrum.ex +++ /dev/null @@ -1,2 +0,0 @@ -defmodule Nosedrum do -end diff --git a/lib/nosedrum/application_command.ex b/lib/nosedrum/application_command.ex deleted file mode 100644 index 478f96dd..00000000 --- a/lib/nosedrum/application_command.ex +++ /dev/null @@ -1,293 +0,0 @@ -defmodule Nosedrum.ApplicationCommand do - @moduledoc """ - The application command behaviour specifies the interface that a slash, user, - or message command module should implement. - - Like regular commands, application command modules are stateless on their own. Implementations of the callbacks - defined by this behaviour are invoked from other modules/functions, notably a `Nosedrum.Storage`. - - The types defined in this module reflect the official - [Application Command docs](https://discord.com/developers/docs/interactions/application-commands). - - ## Example Slash Command - This command echos the passed message back to the user. - ```elixir - # In your application command module file, e.g. ./lib/my_app/commands/echo.ex - defmodule MyApp.Commands.Echo do - @behaviour Nosedrum.ApplicationCommand - - @impl true - def description() do - "Echos a message." - end - - @impl true - def command(interaction) do - [%{name: "message", value: message}] = interaction.data.options - [ - content: message, - ephemeral?: true - ] - end - - @impl true - def type() do - :slash - end - - @impl true - def options() do - [ - %{ - type: :string, - name: "message", - description: "The message for the bot to echo.", - required: true - } - ] - end - end - ``` - - ```elixir - # In your Nostrum.Consumer file, e.g. ./lib/my_app/consumer.ex - defmodule MyApp.Consumer do - use Nostrum.Consumer - - # ... - - # You may use `:global` instead of a guild id at GUILD_ID_HERE, but note - # that global commands could take up to an hour to become available. - def handle_event({:READY, _data, _ws_state}) do - case Nosedrum.Storage.Dispatcher.add_command("echo", MyApp.Commands.Echo, GUILD_ID_HERE) do - {:ok, _} -> IO.puts("Registered Echo command.") - e -> IO.inspect(e, label: "An error occurred registering the Echo command") - end - end - - def handle_event({:INTERACTION_CREATE, interaction, _ws_state}) do - Nosedrum.Storage.Dispatcher.handle_interaction(interaction) - end - end - ``` - - You will also need to start the `Nosedrum.Storage.Dispatcher` as part of - your supervision tree, for example, by adding this to your application start - function: - - ```elixir - # ./lib/my_app/application.ex - defmodule MyApp.Application do - # ... - def start(type, args) do - children = [ - # ... - {Nosedrum.Storage.Dispatcher, name: Nosedrum.Storage.Dispatcher}, - ] - - options = [strategy: :rest_for_one, name: MyApp.Supervisor] - Supervisor.start_link(children, options) - end - end - ``` - """ - @moduledoc since: "0.4.0" - - alias Nostrum.Struct.{Embed, Interaction} - - @typedoc """ - Called by `Nosedrum.Storage.followup/2` after deferring an interaction response. - - The callback should return a response similar to `c:command/1`, excluding the `:type`, `:tts?`, and `:ephemeral?` - options. - """ - @type callback :: - {(... -> response()), args :: list()} | {module(), fn_name :: atom(), args :: list()} - @typedoc """ - A value of the `:type` field in a `c:command/1` return value. See - `t:response/0` for more details. - """ - @type response_type :: - :channel_message_with_source - | :deferred_channel_message_with_source - | :deferred_update_message - | {:deferred_channel_message_with_source, callback()} - | {:deferred_update_message, callback()} - | :pong - | :update_message - - @typedoc """ - A field in a keyword list interaction response. - - Special notes: - - `:type` is required, unless `:content` or `:embeds` is present, in which case it defaults to - `:channel_message_with_source`. - - `:allowed_mentions` is a list that should contain "users", "roles", and/or "everyone", or be empty. - """ - @type response_field :: - {:type, response_type} - | {:content, String.t()} - | {:embeds, [Embed.t()]} - | {:components, [map()]} - | {:ephemeral?, boolean()} - | {:tts?, boolean()} - | {:allowed_mentions, [String.t()] | []} - - @typedoc """ - A keyword list of fields to include in the interaction response, after running the `c:command/1` callback. - - If `:type` is not specified, it will default to `:channel_message_with_source`, though one of - either `:embeds` or `:content` must be present. - - If you are deferring an interaction response with `:deferred_channel_message_with_source` or - `:deferred_update_message`, you should also supply a callback under `:type` in the form of - `{:deferred_*, callback_tuple}` (See the Deferred Response Example below for more details on `callback_tuple`). - - ## Example - - ```elixir - def command(interaction) do - # Since `:content` is included, Nosedrum will infer `type: :channel_message_with_source` - [ - content: "Hello, world!", - ephemeral?: true, - allowed_mentions: ["users", "roles"] - ] - end - ``` - - ## Deferred Response Example - - In order to avoid a potential race condition when deferring, you should supply a callback function for Nosedrum - to call only after the initial response succeeds. The callback should take the form of `{anonymous_fn, args}`, or an - MFA (Module, Function, Args) tuple, like `{MyCommand, :followup_fn, [interaction, extra_arg]}` - - ```elixir - @impl Nosedrum.ApplicationCommand - def command(interaction) do - [ - type: {:deferred_channel_message_with_source, {&expensive_calculation/1, [interaction]}} - ] - end - - defp expensive_calculation(interaction) do - # ... do expensive things - [ - content: "Hello, I've been edited in after the original interaction response" - ] - end - ``` - """ - @type response :: [response_field] - - @typedoc """ - An option (argument) for an application command. - - See callback `c:options/0` documentation for examples. - """ - @type option :: %{ - optional(:required) => true | false, - optional(:choices) => [choice], - optional(:options) => [option], - type: - :sub_command - | :sub_command_group - | :string - | :integer - | :boolean - | :user - | :channel - | :role - | :mentionable - | :number, - name: String.t(), - description: String.t() - } - - @typedoc """ - A choice for an option. - - See callback `c:options/0` documentation for examples. - """ - @type choice :: %{ - name: String.t(), - value: String.t() | number() - } - - @doc """ - Returns one of `:slash`, `:message`, or `:user`, indicating what kind of application command this module represents. - """ - @callback type() :: :slash | :message | :user - - @doc """ - Returns a description of the command. Used when registering the command with Discord. This is what the user will see - in the autofill command-selection menu. - - ## Example - ```elixir - def description, do: "This is a command description." - ``` - """ - @callback description() :: String.t() - - @doc """ - An optional callback that returns a list of options (arguments) that the - command takes. Used when registering the command with Discord. Only valid for - CHAT_INPUT application commands, aka slash commands. - - Read more in the official - [Application Command documentation](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure). - - ## Example options callback for a "/role" command - ```elixir - def options, do: - [ - %{ - type: :user, - name: "user", - description: "The user to assign the role to.", - required: true # Defaults to false if not specified. - }, - %{ - type: :role, - name: "role", - description: "The role to be assigned.", - required: true, - choices: [ - %{ - name: "Event Notifications", - value: 123456789123456789 # A role ID, passed to your `command/1` callback via the Interaction struct. - }, - %{ - name: "Announcements", - value: 123456789123456790 - } - ] - } - ] - ``` - """ - @callback options() :: [option] - - @doc """ - Execute the command invoked by the given `t:Nostrum.Struct.Interaction.t/0`. Returns a `t:response/0` - - ## Example - ```elixir - defmodule MyApp.MyCommand do - @behaviour Nosedrum.ApplicationCommand - - # ... - - @impl true - def command(interaction) do - %{name: opt_name} = List.first(interaction.data.options) - [content: "Hello World \#{opt_name}!"] - end - end - ``` - """ - @callback command(interaction :: Interaction.t()) :: response - - @optional_callbacks [options: 0] -end diff --git a/lib/nosedrum/component_handler.ex b/lib/nosedrum/component_handler.ex deleted file mode 100644 index 41120b3e..00000000 --- a/lib/nosedrum/component_handler.ex +++ /dev/null @@ -1,88 +0,0 @@ -defmodule Nosedrum.ComponentHandler do - @moduledoc """ - Handles incoming interactions triggered by interacting with a component. - - ## Register components - Before the component handler can handle incoming interactions, you must - register some [custom ids](https://discord.com/developers/docs/interactions/message-components#custom-id) - with the `c:Nosedrum.ComponentHandler.register_components/3` callback. The arguments for the callback - are one or more custom ids, a module or a pid that will handle the interaction - and additional data, that will be passed on to the module or pid that handles - the interaction. The additional data can be any arbitrary term. - - ### Module handlers - The registered module should implement the `Nosedrum.ComponentInteraction` - behaviour. The component handler will call the `c:Nosedrum.ComponentInteraction.message_component_interaction/2` - callback of the module when a matching interaction is found. - - ### Process handlers - Once a pid is registered, it will receive a message of the type `t:message_component_interaction/0` - every time a matching interaction is found. - - ## Handle incomming interactions - The `c:Nosedrum.ComponentHandler.handle_component_interaction/1` callback will look up the correct module or - pid and relay the interaction and additional data. The recommended place to - handle these interactions would be when handling the `:INTERACTION_CREATE` - event in the consumer. The following example uses the - `Nosedrum.ComponentHandler.ETS` implementation: - - ```elixir - # The ready event would be a possible place where you could register static - # component handlers. - def handle_event({:READY, _, _}) do - Nosedrum.ComponentHandler.ETS.register_component(["next_button", "previous_button"], - MyApp.ButtonHandler, nil) - end - - # Handle the interaction create in your consumer module. - def handle_event({:INTERACTION_CREATE, interaction, _}) do - case interaction.type do - 1 -> Nostrum.Api.create_interaction_response(interaction, %{type: :sub_command}) - 2 -> Nosedrum.Storage.Dispatcher.handle_interaction(interaction) - x when x in 3..5 -> Nosedrum.ComponentHandler.ETS.handle_component_interaction(interaction) - end - end - ``` - - ```elixir - # Start the Component handler in your Application. Ideally before your Consumer. - defmodule MyApp.Application do - # ... - def start(type, args) do - children = [ - # ... - Nosedrum.ComponentHandler.ETS - ] - - options = [strategy: :rest_for_one, name: MyApp.Supervisor] - Supervisor.start_link(children, options) - end - end - ``` - - ```elixir - # A simple module for handling component interactions - defmodule MyApp.ButtonHandler do - @behaviour Nosedrum.ComponentInteraction - - def message_component_interaction(interaction, _) do - case interaction.data.custom_id do - "next_button" -> [content: "The next button was clicked"] - "prev_button" -> [content: "The previous button was clicked"] - end - end - end - ``` - """ - - @type custom_ids :: - Nostrum.Struct.Component.custom_id() | [Nostrum.Struct.Component.custom_id()] - @type component_handler :: module() | pid() - @type additional_data :: term() - @type message_component_interaction :: - {:message_component_interaction, Nostrum.Struct.Interaction.t(), additional_data()} - - @callback register_components(custom_ids, component_handler, additional_data()) :: :ok - @callback handle_component_interaction(Nostrum.Struct.Interaction.t()) :: - :ok | {:error, Nostrum.Error.ApiError.t() | :not_found} -end diff --git a/lib/nosedrum/component_handler/ets.ex b/lib/nosedrum/component_handler/ets.ex deleted file mode 100644 index b1d66f80..00000000 --- a/lib/nosedrum/component_handler/ets.ex +++ /dev/null @@ -1,100 +0,0 @@ -defmodule Nosedrum.ComponentHandler.ETS do - @moduledoc """ - ETS-based implementation of a `Nosedrum.ComponentHandler`. - - When a pid is registered as a handler for components it will be monitored - and automatically removed once the process exits. Registered module handlers - on the other hand are never cleared. You can access the named ETS-table manually - to remove any keys if necessary. - - ## Options - * :name - name used for registering the process under and also the name of the ETS-table. - Must be a atom, because it will be passed to `:ets.new/2` as name. Defaults to `Nosedrum.ComponentHandler.ETS` - > ### Note {: .info} - > When using a different name than the default you must pass it as first argument of - > the callbacks. - > - > ```elixir - > # Assuming name was set to MyApp.ComponentHandler - > Nosedrum.Storage.ETS.register_components(MyApp.ComponentHandler, - > ["a", "b", "c"], MyApp.ComponentHandlers.ABCHandler) - > ``` - - """ - - @doc false - use GenServer - - @behaviour Nosedrum.ComponentHandler - - alias Nosedrum.Storage - - @impl Nosedrum.ComponentHandler - def register_components(server \\ __MODULE__, component_ids, module, additional_data) do - GenServer.call(server, {:register_components, component_ids, module, additional_data}) - end - - @impl Nosedrum.ComponentHandler - def handle_component_interaction( - server \\ __MODULE__, - %Nostrum.Struct.Interaction{} = interaction - ) do - component_id = interaction.data.custom_id - - case :ets.match(server, {component_id, :"$1", :"$2"}) do - [[pid, additional_data]] when is_pid(pid) -> - send(pid, {:message_component_interaction, interaction, additional_data}) - :ok - - [[module, additional_data]] when is_atom(module) -> - with response <- module.message_component_interaction(interaction, additional_data), - :ok <- Storage.respond(interaction, response), - {_defer_type, callback_tuple} <- Keyword.get(response, :type) do - Storage.followup(interaction, callback_tuple) - end - - [] -> - {:error, :not_found} - end - end - - @doc false - def start_link(opts) do - name = Keyword.get(opts, :name, __MODULE__) - GenServer.start_link(__MODULE__, opts, name: name) - end - - @impl GenServer - def init(opts) do - name = Keyword.get(opts, :name, __MODULE__) - table = :ets.new(name, [:named_table, :public, :ordered_set, read_concurrency: true]) - {:ok, table} - end - - @impl GenServer - def handle_call( - {:register_components, component_ids, component_handler, additional_data}, - _from, - table - ) do - if is_pid(component_handler) do - # Get notified then a stateful component handler exists - Process.monitor(component_handler) - end - - entries = - component_ids - |> List.wrap() - |> Enum.map(&{&1, component_handler, additional_data}) - - :ets.insert(table, entries) - {:reply, :ok, table} - end - - @impl GenServer - def handle_info({:DOWN, _, :process, pid, _}, table) do - # Remove stateful component handlers when the process exits - :ets.match_delete(table, {:_, pid}) - {:noreply, table} - end -end diff --git a/lib/nosedrum/component_interaction.ex b/lib/nosedrum/component_interaction.ex deleted file mode 100644 index 31ee941c..00000000 --- a/lib/nosedrum/component_interaction.ex +++ /dev/null @@ -1,20 +0,0 @@ -defmodule Nosedrum.ComponentInteraction do - @moduledoc """ - Behaviour for processing an interaction triggered from a message component. - - Modules implementing this behaviour can be registered to a command handler - via `c:Nosedrum.ComponentHandler.register_components/3`. See the module - documentation for `Nosedrum.ComponentHandler` for more information. - """ - - @doc """ - Handle message component interactions. - - Behaves the same way as the `Nosedrum.ApplicationCommand.commmand/1` callback. - """ - @callback message_component_interaction( - interaction :: Nostrum.Struct.Interaction.t(), - Nosedrum.ComponentHandler.additional_data() - ) :: - Nosedrum.ApplicationCommand.response() -end diff --git a/lib/nosedrum/converters.ex b/lib/nosedrum/converters.ex deleted file mode 100644 index 11709f37..00000000 --- a/lib/nosedrum/converters.ex +++ /dev/null @@ -1,89 +0,0 @@ -defmodule Nosedrum.Converters do - @moduledoc """ - Conversion from command arguments to various types. - - This module provides an interface to the individual converter modules. - Most converter functions related to Discord itself take a `guild_id` - which is used for loading the guild from the cache. If the guild could - not be load from the cache, implementations will usually attempt to - fetch the relevant data from the API. - """ - - alias Nostrum.Error.ApiError - alias Nostrum.Snowflake - alias Nostrum.Struct.Guild.Role - alias Nostrum.Struct.{Channel, Guild} - - @typedoc """ - Additional options that were used when searching the cache. - - ## Items - - - `:case_insensitive`: the search was done without respect to string casing, - for example, a role conversion with `ilike` specified. - - - `:not_exact`: the search was not exact, for example, a member search by - name without discriminator. - """ - @typedoc since: "0.6.0" - @type option :: :case_insensitive | :not_exact - - @typedoc """ - Collection of options that were used when searching the cache. - """ - @typedoc since: "0.6.0" - @type options :: [option] - - @typedoc """ - Specifies the reason for why a converter operation failed. - - ## Items - - The reason can be two things: - - - Nosedrum could interpret the input as a direct snowflake and tried to fetch - it both via the cache and via the API. If both fetches fail (which is very - unlikely to happen), `:uncached_and_fetch_error` can be returned to signify - that the given snowflake is both uncached and could not be fetched from the - API. The error from nostrum is also returned in this case. - - - The more common case is that the user input could simply not be found. - Nosedrum will tell you whether this was a search by `:id` or `:name`, and the - `query` field contains the parsed query as interpreted by the converter. You - can use this to provide user feedback, but make sure to escape it properly, - in case it is a string. - - You will likely want to handle the latter case. - """ - @typedoc since: "0.6.0" - @type reason :: - {:not_found, {:by, :id | :name, parsed_query :: String.t() | Snowflake.t(), options}} - | {:uncached_and_fetch_error, ApiError.t()} - - @doc """ - Convert the given `text` to a `t:Nostrum.Struct.Channel.t/0`. - - Lookup is attempted in the following order: - - by direct ID, such as `9999` - - by mention, such as `<#9999>` - - by name, such as `mod-log` - """ - @spec to_channel(String.t(), Guild.id()) :: {:ok, Channel.t()} | {:error, reason} - defdelegate to_channel(text, guild_id), to: __MODULE__.Channel, as: :into - - @doc """ - Convert the given `text` to a `t:Nostrum.Struct.Guild.Role.t/0`. - - Lookup is attempted in the following order: - - by direct ID, such as `5555` - - by mention, such as `<@&5555>` - - by name, such as `Bots` - - The optional `ilike` argument determines whether the role name - search should be case-insensitive. This is useful if your users - are lazy and you want to save them from holding down an extra - button on their keyboard. - """ - @spec to_role(String.t(), Guild.id(), boolean()) :: {:ok, Role.t()} | {:error, reason} - defdelegate to_role(text, guild_id, ilike \\ false), to: __MODULE__.Role, as: :into -end diff --git a/lib/nosedrum/converters/channel.ex b/lib/nosedrum/converters/channel.ex deleted file mode 100644 index 7071f2f4..00000000 --- a/lib/nosedrum/converters/channel.ex +++ /dev/null @@ -1,97 +0,0 @@ -defmodule Nosedrum.Converters.Channel do - @moduledoc false - - alias Nosedrum.Converters - alias Nostrum.Api - alias Nostrum.Cache.GuildCache - alias Nostrum.Struct.Channel - - @doc """ - Convert a Discord channel mention to an ID. - This also works if the given string is just the ID. - - ## Examples - - iex> Nosedrum.Converters.Channel.channel_mention_to_id("<#10101010>") - {:ok, 10101010} - iex> Nosedrum.Converters.Channel.channel_mention_to_id("<#101010>") - {:ok, 101010} - iex> Nosedrum.Converters.Channel.channel_mention_to_id("91203") - {:ok, 91203} - iex> Nosedrum.Converters.Channel.channel_mention_to_id("not valid") - {:error, "not a valid channel ID"} - """ - @spec channel_mention_to_id(String.t()) :: {:ok, pos_integer()} | {:error, String.t()} - def channel_mention_to_id(text) do - maybe_id = - text - |> String.trim_leading("<#") - |> String.trim_trailing(">") - - case Integer.parse(maybe_id) do - {value, ""} -> {:ok, value} - _ -> {:error, "not a valid channel ID"} - end - end - - # Attempt to find a channel within the given `channels` - # matching the given `text`. - # The lookup strategy is as follows: - # - Channel ID - # - Channel mention - # - Channel name - @spec find_channel( - [Nostrum.Struct.Channel.t()], - String.t() - ) :: Nostrum.Struct.Channel.t() | Converters.reason() - defp find_channel(channels, query) do - case channel_mention_to_id(query) do - {:ok, requested_id} -> - Enum.find_value( - channels, - {:not_found, {:by, :id, requested_id, []}}, - fn - %{id: ^requested_id} = channel -> channel - _other -> nil - end - ) - - {:error, _reason} -> - Enum.find_value( - channels, - {:not_found, {:by, :name, query, []}}, - fn - %Channel{name: ^query} = channel -> channel - _other -> nil - end - ) - end - end - - defp okify({:not_found, _reason} = result), do: {:error, result} - defp okify({_id, channel}), do: {:ok, channel} - defp okify(channel), do: {:ok, channel} - - @spec into(String.t(), Nostrum.Snowflake.t()) :: - {:ok, Nostrum.Struct.Channel.t()} | {:error, Converters.reason()} - def into(text, guild_id) do - case GuildCache.get(guild_id) do - {:ok, guild} -> - guild.channels - |> Map.values() - |> find_channel(text) - |> okify - - {:error, _reason} -> - case Api.Guild.channels(guild_id) do - {:ok, channels} -> - channels - |> find_channel(text) - |> okify - - {:error, reason} -> - {:error, {:uncached_and_fetch_error, reason}} - end - end - end -end diff --git a/lib/nosedrum/converters/role.ex b/lib/nosedrum/converters/role.ex deleted file mode 100644 index 12448210..00000000 --- a/lib/nosedrum/converters/role.ex +++ /dev/null @@ -1,112 +0,0 @@ -defmodule Nosedrum.Converters.Role do - @moduledoc false - - alias Nosedrum.Converters - alias Nostrum.Api - alias Nostrum.Cache.GuildCache - - @doc """ - Convert a Discord role mention to an ID. - This also works if the given string is just the ID. - - ## Examples - - iex> Nosedrum.Converters.Role.role_mention_to_id("<@&10101010>") - {:ok, 10101010} - iex> Nosedrum.Converters.Role.role_mention_to_id("<@&101010>") - {:ok, 101010} - iex> Nosedrum.Converters.Role.role_mention_to_id("91203") - {:ok, 91203} - iex> Nosedrum.Converters.Role.role_mention_to_id("not valid") - {:error, "not a valid role ID"} - """ - @spec role_mention_to_id(String.t()) :: {:ok, pos_integer()} | :error - def role_mention_to_id(text) do - maybe_id = - text - |> String.trim_leading("<@&") - |> String.trim_trailing(">") - - case Integer.parse(maybe_id) do - {value, ""} -> {:ok, value} - _ -> :error - end - end - - @spec find_by_name( - [Nostrum.Struct.Guild.Role.t()], - String.t(), - boolean() - ) :: Nostrum.Struct.Guild.Role.t() | {:error, Converters.reason()} - defp find_by_name(roles, name, case_insensitive) do - if case_insensitive do - downcased_name = String.downcase(name) - - error_return = { - :error, - {:not_found, {:by, :name, downcased_name, [:case_insensitive]}} - } - - Enum.find_value( - roles, - error_return, - fn - # credo:disable-for-next-line - %{name: name} = role -> if String.downcase(name) == downcased_name, do: {:ok, role} - _other -> nil - end - ) - else - error_return = {:error, {:not_found, {:by, :name, name, []}}} - - Enum.find_value( - roles, - error_return, - fn - %{name: ^name} = role -> {:ok, role} - _other -> nil - end - ) - end - end - - @spec find_role( - [Nostrum.Struct.Guild.Role.t()], - String.t(), - boolean - ) :: {:ok, Nostrum.Struct.Guild.Role.t()} | {:error, Converters.reason()} - def find_role(roles, text, ilike) do - case role_mention_to_id(text) do - # We have a direct snowflake given. Try to find an exact match. - {:ok, id} -> - error_return = {:error, {:not_found, {:by, :id, id, []}}} - - Enum.find_value(roles, error_return, fn - %{id: ^id} = role -> {:ok, role} - _other -> nil - end) - - # We do not have a snowflake given, assume it's a name and search through the roles by name. - :error -> - find_by_name(roles, text, ilike) - end - end - - @spec into(String.t(), Nostrum.Snowflake.t(), boolean) :: - {:ok, Nostrum.Struct.Guild.Role.t()} | {:error, String.t()} - def into(text, guild_id, ilike) do - case GuildCache.get(guild_id) do - {:ok, guild} -> - find_role(Map.values(guild.roles), text, ilike) - - {:error, _reason} -> - case Api.Guild.roles(guild_id) do - {:ok, roles} -> - find_role(roles, text, ilike) - - {:error, _reason} -> - {:error, "This guild is not in the cache, nor could it be fetched from the API."} - end - end - end -end diff --git a/lib/nosedrum/helpers.ex b/lib/nosedrum/helpers.ex deleted file mode 100644 index 5da97479..00000000 --- a/lib/nosedrum/helpers.ex +++ /dev/null @@ -1,41 +0,0 @@ -defmodule Nosedrum.Helpers do - @moduledoc "User interaction helpers that don't fit into their own module." - - @doc """ - Escape `@everyone` and `@here` mentions in the given `content` - with a zero-width space character. - - ## Example - - iex> Nosedrum.Helpers.escape_server_mentions("hello world") - "hello world" - iex> Nosedrum.Helpers.escape_server_mentions("hello @everyone @here") - "hello @\u200Beveryone @\u200Bhere" # No space to be seen, but no mention either! - """ - @spec escape_server_mentions(String.t()) :: String.t() - def escape_server_mentions(content) do - content - |> String.replace("@everyone", "@\u200Beveryone") - |> String.replace("@here", "@\u200Bhere") - end - - @doc """ - Try to split the given `text` using `OptionParser.split/1` and fall back to - `String.split/1` if not applicable. - - `OptionParser.split/1` raises a `RuntimeError` if the text it is given - contains opening quotation marks that were not closed again. In other cases, - it is desirable to split a string into quoted parts, for example: - - iex> Nosedrum.Helpers.quoted_split("echo \\"hello bot\\"") - ["echo", "hello bot"] - iex> Nosedrum.Helpers.quoted_split("echo \\"hello bot") - ["echo", "\\"hello", "bot"] - """ - @spec quoted_split(String.t()) :: [String.t()] - def quoted_split(text) do - OptionParser.split(text) - rescue - _ in RuntimeError -> String.split(text) - end -end diff --git a/lib/nosedrum/message_cache.ex b/lib/nosedrum/message_cache.ex deleted file mode 100644 index ac3e7eaf..00000000 --- a/lib/nosedrum/message_cache.ex +++ /dev/null @@ -1,48 +0,0 @@ -defmodule Nosedrum.MessageCache do - @moduledoc """ - A cache for recent messages sent on guilds. - - Usage of implementations of this module is useful when your bot needs - to access recently sent messages without hogging the API. One use case - would be bots with an integrated mod log that want to show a before & - after comparison of edited messages. - """ - - alias Nostrum.Struct.{Guild, Message} - - @doc """ - Retrieve a single message for the given `guild_id` with the specified `message_id` - from the cache process identified by `cache`. - - If no message with the given ID is cached, `nil` is returned. - """ - @callback get(guild_id :: Guild.id(), message_id :: Message.id(), cache :: any()) :: - Message.t() | nil | {:error, String.t()} - - @doc """ - Retrieve up to `limit` messages for the given `guild_id` from the cache - process identified by `cache`. If `limit` is `:infinity`, return all - messages for that guild from the cache. - - If the guild is not cached, an empty message list is returned. - """ - @callback recent_in_guild( - guild_id :: Guild.id(), - limit :: pos_integer | :infinity, - cache :: any() - ) :: [Message.t()] - - @doc """ - Consume the given `message` in the cache process identified by `cache`. - - Whether this is done asynchronously or synchronously depends on the implementation. - """ - @callback consume(message :: Message.t(), cache :: any()) :: any() - - @doc """ - Update the given `message` in the cache, if present. - - Whether this is done asynchronously or synchronously depends on the implementation. - """ - @callback update(message :: Message.t(), cache :: any()) :: any() -end diff --git a/lib/nosedrum/message_cache/agent.ex b/lib/nosedrum/message_cache/agent.ex deleted file mode 100644 index b33e9a3b..00000000 --- a/lib/nosedrum/message_cache/agent.ex +++ /dev/null @@ -1,145 +0,0 @@ -defmodule Nosedrum.MessageCache.Agent do - @default_limit_per_guild 200 - @doc_modname __MODULE__ |> Atom.to_string() |> String.trim_leading("Elixir.") - @moduledoc """ - An `Agent`-based message cache. - - Note that using this cache across your entire bot can easily lead to - the cache becoming a bottleneck, since an agent is a single process. - Instead, you might want to use this cache across several smaller guilds - to avoid overloading a single process with events from multiple guilds. - - ## Usage - - defmodule MyBot.Application do - use Application - - def start(type, args) do - children = [ - %{ - id: MessageCache, - start: {#{@doc_modname}, :start_link, [[name: #{@doc_modname}]]} - } - ] - end - end - - If using multiple instances of this agent, you can pass the agent process - reference as the last argument to the individual functions that this - implementation provides. By default, this uses `#{@doc_modname}` as the - agent reference, requiring you to specify the `name:` in your application - callback. - - ## Configuration - By default, the cache keeps up to #{@default_limit_per_guild} messages in - the cache per guild. To configure this, you can pass the `limit:` option - as an argument to the process, such as: - - defmodule MyBot.Application do - use Application - - def start(type, args) do - children = [ - %{ - id: MessageCache, - start: {#{@doc_modname}, :start_link, [[name: #{@doc_modname}, limit: 50]]} - } - ] - end - end - - This will ensure that no more than 50 messages are kept in cache per guild. - """ - - @behaviour Nosedrum.MessageCache - - @doc false - use Agent - - @doc false - @spec start_link(GenServer.options()) :: Agent.on_start() - def start_link(options) do - {limit, gen_options} = Keyword.pop(options, :limit, @default_limit_per_guild) - initial_state = {%{}, false, limit} - Agent.start_link(fn -> initial_state end, gen_options) - end - - @impl true - def get(guild_id, message_id, cache \\ __MODULE__) do - Agent.get(cache, fn state -> - state - |> elem(0) - |> Map.get(guild_id, []) - |> Enum.find(&(&1.id == message_id)) - end) - end - - @impl true - def recent_in_guild(guild_id, limit, cache \\ __MODULE__) - - def recent_in_guild(guild_id, :infinity, cache) do - Agent.get(cache, fn state -> - state - |> elem(0) - |> Map.get(guild_id, []) - end) - end - - def recent_in_guild(guild_id, limit, cache) do - Agent.get(cache, fn state -> - state - |> elem(0) - |> Map.get(guild_id, []) - |> Enum.slice(0..(limit - 1)) - end) - end - - @impl true - def consume(msg, cache \\ __MODULE__) do - Agent.get_and_update( - cache, - fn {messages, has_hit_limit, limit} = state -> - hits_limit_after_insertion = - has_hit_limit or length(Map.get(messages, msg.guild_id, [])) >= limit - - updated_messages = - Map.update( - messages, - msg.guild_id, - [msg], - &if(hits_limit_after_insertion, - do: [msg | Enum.drop(&1, -1)], - else: [msg | &1] - ) - ) - - # Only recalculate the length of the messages if we haven't already hit the - # limit to avoid unnecessary list traversal. - updated_state = {updated_messages, hits_limit_after_insertion, limit} - {state, updated_state} - end - ) - end - - @impl true - def update(msg, cache \\ __MODULE__) do - Agent.get_and_update( - cache, - fn {messages, _has_hit_limit, _limit} = state -> - with guild_msgs when guild_msgs != nil <- Map.get(messages, msg.guild_id, []), - cached_idx when cached_idx != nil <- - Enum.find_index(guild_msgs, &(&1.id == msg.id)) do - updated_messages = %{ - messages - | msg.guild_id => List.replace_at(guild_msgs, cached_idx, msg) - } - - updated_state = put_elem(state, 0, updated_messages) - {state, updated_state} - else - _err -> {state, state} - end - end - ) - end -end diff --git a/lib/nosedrum/message_cache/ets.ex b/lib/nosedrum/message_cache/ets.ex deleted file mode 100644 index 97c25fde..00000000 --- a/lib/nosedrum/message_cache/ets.ex +++ /dev/null @@ -1,126 +0,0 @@ -defmodule Nosedrum.MessageCache.ETS do - @default_table :nosedrum_message_cache - @default_table_options [ - {:read_concurrency, true}, - {:write_concurrency, true}, - :ordered_set, - :public, - :named_table - ] - @moduledoc """ - An ETS-table based message cache. - - This module does not implement any cache expiration: by default, the - table fills up infinitely. One possibility of keeping messages under control - would be to delete all messages sent before a certain date. Since the key - of table entries are message IDs, you can use - [`:ets.select_delete/2`](http://erlang.org/doc/man/ets.html#select_delete-2) - to accomplish this. For example, to delete all messages older than one day, - you could use the following construct: - - now = DateTime.utc_now() - one_day_ago = DateTime.add(now, -1, :day) - snowflake = Nostrum.Snowflake.from_datetime!(one_day_ago) - match_spec = [{{:"$1", :_, :_, :_, :_}, [{:<, :"$1", snowflake}], [true]}] - :ets.select_delete(:#{@default_table}, match_spec) - - For deleting in different ways such as by guild ID, use - [`:ets.fun2ms/1`](http://erlang.org/doc/man/ets.html#fun2ms-1) to generate match - specifications from functions. - - You can use the `GenServer.call(cache_process, :tid)` to obtain the internal - table ID when not using a named table. - """ - - # Perhaps, someday, check match specs with `map_get`: - # - # def recent_in_guild(guild_id, :infinity, table_ref) do - # :ets.select_reverse(table_ref, [ - # {{:"$1", :"$2"}, [{:==, {:map_get, :"$2", :guild_id}, guild_id}], [:"$2"]} - # ]) - # end - - @behaviour Nosedrum.MessageCache - - @doc false - use GenServer - - @doc """ - Initialize the ETS message cache. - - By default, the table used for storing messages is a named table with - the name `:#{@default_table}`. The table reference is stored internally - as the state of this process, the public-facing API functions default - to using the table name to access the module. - """ - def start_link( - table_name \\ @default_table, - table_options \\ @default_table_options, - gen_options - ) do - GenServer.start_link(__MODULE__, {table_name, table_options}, gen_options) - end - - @impl true - def handle_call(:tid, _, tid) do - {:reply, tid, tid} - end - - @impl true - @doc false - def init({table_name, table_options}) do - tid = :ets.new(table_name, table_options) - - {:ok, tid} - end - - @impl true - def get(_guild_id, message_id, table_ref \\ @default_table) do - case :ets.lookup(table_ref, message_id) do - [] -> nil - [{_message_id, _guild_id, message}] -> message - end - end - - @impl true - def recent_in_guild(guild_id, limit, table_ref \\ @default_table) - - def recent_in_guild(guild_id, :infinity, table_ref) do - :ets.select_reverse(table_ref, [ - {{:"$1", :"$2", :"$3"}, [{:==, :"$2", guild_id}], [:"$3"]} - ]) - end - - def recent_in_guild(guild_id, limit, table_ref) do - selection = - :ets.select_reverse( - table_ref, - [{{:"$1", :"$2", :"$3"}, [{:==, :"$2", guild_id}], [:"$3"]}], - limit - ) - - case selection do - :"$end_of_table" -> [] - {matches, _continuation} -> matches - end - end - - @doc """ - See `c:Nosedrum.MessageCache.consume/2`. - Returns the result of `:ets.insert`. - """ - @impl true - def consume(message, table_ref \\ @default_table) do - :ets.insert(table_ref, {message.id, message.guild_id, message}) - end - - @impl true - @doc """ - See `c:Nosedrum.MessageCache.update/2`. - Returns the result of `:ets.insert/2`. - """ - def update(message, table_ref \\ @default_table) do - # Erlang table column indices start from 1, so 5 references the content here. - :ets.insert(table_ref, {message.id, message.guild_id, message}) - end -end diff --git a/lib/nosedrum/storage.ex b/lib/nosedrum/storage.ex deleted file mode 100644 index 7bc98962..00000000 --- a/lib/nosedrum/storage.ex +++ /dev/null @@ -1,252 +0,0 @@ -defmodule Nosedrum.Storage do - @moduledoc """ - `Storage`s keep track of your Application Command names and their associated modules. - - A `Storage` handles incoming `t:Nostrum.Struct.Interaction.t/0`s, invoking - `c:Nosedrum.ApplicationCommand.command/1` callbacks and responding to the Interaction. - - In addition to tracking commands locally for the bot, a `Storage` is - responsible for registering an Application Command with Discord when `c:add_command/4` - or `c:remove_command/4` is called. - """ - @moduledoc since: "0.4.0" - alias Nostrum.Struct.{Guild, Interaction} - - require Logger - - @callback_type_map %{ - pong: 1, - channel_message_with_source: 4, - deferred_channel_message_with_source: 5, - deferred_update_message: 6, - update_message: 7, - modal: 9 - } - - @flag_map %{ - ephemeral?: 64 - } - - @type command_scope :: :global | Guild.id() | [Guild.id()] - - @typedoc """ - Defines a structure of commands, subcommands, subcommand groups. - - **Note** that Discord only supports nesting 3 levels deep, like `command -> subcommand group -> subcommand`. - - ## Example path: - ```elixir - %{ - {"castle", MyApp.CastleCommand.description()} => - %{ - {"prepare", "Prepare the castle for an attack."} => [], - {"open", "Open up the castle for traders and visitors."} => [], - # ... - } - } - ``` - - ## References - - Official Documentation: - https://discord.com/developers/docs/interactions/application-commands#subcommands-and-subcommand-groups - """ - @type application_command_path :: - %{ - {group_name :: String.t(), group_desc :: String.t()} => [ - application_command_path | [Nosedrum.ApplicationCommand.option()] - ] - } - - @typedoc """ - The name or pid of the Storage process. - """ - @type name_or_pid :: atom() | pid() - - @doc """ - Handle an Application Command invocation. - - This callback should be invoked upon receiving an interaction via the `:INTERACTION_CREATE` event. - - ## Example using `Nosedrum.Storage.Dispatcher`: - ```elixir - # In your `Nostrum.Consumer` file: - def handle_event({:INTERACTION_CREATE, interaction, _ws_state}) do - IO.puts "Got interaction" - Nosedrum.Storage.Dispatcher.handle_interaction(interaction) - end - ``` - - ## Return value - - Returns `:ok` on success, or `{:ok, t:Nostrum.Struct.Message.t()}` when deferring and the supplied callback - completes with a successful edit of the original response. `{:error, reason}` is returned otherwise. - """ - @callback handle_interaction(interaction :: Interaction.t(), name_or_pid) :: - :ok - | {:ok, Nostrum.Struct.Message.t()} - | {:error, :unknown_command} - | Nostrum.Api.error() - - @doc """ - Queues a new command to be registered under the given name or application command path. Queued commands are - added to the internal dispatch storage, and will be registered in bulk upon calling process_queue/1 - - If any command already exists, it will be overwritten. - - ## Return value - Returns `:ok` if successful, and `{:error, reason}` otherwise. - """ - @callback queue_command( - name_or_path :: String.t() | application_command_path, - command_module :: module, - name_or_pid - ) :: :ok | {:error, Nostrum.Error.ApiError.t()} - - @doc """ - Add a new command under the given name or application command path. - - If the command already exists, it will be overwritten. - - When adding many commands, it is recommended to use queue_command, and to call process_queue once all - commands are queued for registration. - - ## Return value - Returns `:ok` if successful, and `{:error, reason}` otherwise. - """ - @callback add_command( - name_or_path :: String.t() | application_command_path, - command_module :: module, - scope :: command_scope, - name_or_pid - ) :: :ok | {:error, Nostrum.Error.ApiError.t()} - - @doc """ - Remove the command under the given name or application command path. - - ## Return value - - Returns `:ok` if successful, and `{:error, reason}` otherwise. - - If the command does not exist, no error should be returned. - """ - @callback remove_command( - name_or_path :: String.t() | application_command_path, - command_id :: Nostrum.Snowflake.t(), - scope :: command_scope, - name_or_pid - ) :: :ok | {:error, Nostrum.Error.ApiError.t()} - - @doc """ - Register all currently queued commands to discord, making them available for use - - Global commands can take up to 1 hour to be made available. Guild commands are available immediately. - - ## Return value - Returns `:ok` if successful, and `{:error, reason}` otherwise. - """ - @callback process_queue( - scope :: command_scope, - name_or_pid - ) :: :ok | {:error, Nostrum.Error.ApiError.t()} - - @doc """ - Responds to an Interaction with the given `t:Nosedrum.ApplicationCommand.response/0`. - - ## Return value - - Returns `:ok` if successful, and a `t:Nostrum.Api.error/0` otherwise. - """ - @spec respond(Interaction.t(), Nosedrum.ApplicationCommand.response()) :: - :ok | Nostrum.Api.error() - def respond(interaction, command_response) do - type = - command_response - |> Keyword.get(:type, :channel_message_with_source) - |> convert_callback_type() - - data = - command_response - |> Keyword.take([ - :content, - :embeds, - :components, - :tts?, - :allowed_mentions, - :custom_id, - :title, - :flags - ]) - |> Map.new() - |> put_flags(command_response) - - res = %{ - type: type, - data: data - } - - response = Nostrum.Api.create_interaction_response(interaction, res) - - case response do - :ok -> - :ok - - {:error, e} -> - Logger.error("Error responding to interaction:\n#{inspect(e)}") - Logger.error("Interaction data:") - Logger.error(interaction) - Logger.error("Response data:") - Logger.error(res) - end - - response - end - - @doc """ - Edits an interaction with a follow up response. - - The response is obtained by running the given function/MFA tuple, see - `t:Nosedrum.ApplicationCommand.callback/0`. - - ## Return value - - Returns `{:ok, `t:Nostrum.Struct.Message.t()`}` if successful, and a `t:Nostrum.Api.error/0` otherwise. - """ - @spec followup(Interaction.t(), Nosedrum.ApplicationCommand.callback()) :: - {:ok, Nostrum.Struct.Message.t()} | Nostrum.Api.error() - def followup(interaction, callback_tuple) do - followup_response = - case callback_tuple do - {callback, args} -> apply(callback, args) - {module, callback, args} -> apply(module, callback, args) - end - - data = - followup_response - |> Keyword.take([:content, :embeds, :components, :allowed_mentions]) - |> Map.new() - - Nostrum.Api.Interaction.edit_response(interaction, data) - end - - defp convert_callback_type({type, _fn}) do - convert_callback_type(type) - end - - defp convert_callback_type(type) do - Map.get(@callback_type_map, type) - end - - defp put_flags(data_map, command_response) do - if Map.has_key?(data_map, :flags) do - data_map - else - Enum.reduce(@flag_map, data_map, fn {flag, value}, data_map_acc -> - if command_response[flag] do - Map.put(data_map_acc, :flags, value) - else - data_map_acc - end - end) - end - end -end diff --git a/lib/nosedrum/storage/dispatcher.ex b/lib/nosedrum/storage/dispatcher.ex deleted file mode 100644 index 45943edb..00000000 --- a/lib/nosedrum/storage/dispatcher.ex +++ /dev/null @@ -1,323 +0,0 @@ -defmodule Nosedrum.Storage.Dispatcher do - @moduledoc """ - An implementation of `Nosedrum.Storage`, dispatching Application Command Interactions to the appropriate modules. - """ - @moduledoc since: "0.4.0" - @behaviour Nosedrum.Storage - - use GenServer - - alias Nosedrum.Storage - alias Nostrum.Struct.Interaction - - @option_type_map %{ - sub_command: 1, - sub_command_group: 2, - string: 3, - integer: 4, - boolean: 5, - user: 6, - channel: 7, - role: 8, - mentionable: 9, - number: 10, - attachment: 11 - } - - ## Api - def start_link(opts) do - GenServer.start_link(__MODULE__, %{}, name: Keyword.get(opts, :name, __MODULE__)) - end - - @impl true - def handle_interaction(%Interaction{} = interaction, id \\ __MODULE__) do - with {:ok, module} <- GenServer.call(id, {:fetch, interaction}), - response <- module.command(interaction), - :ok <- Storage.respond(interaction, response), - {_defer_type, callback_tuple} <- Keyword.get(response, :type) do - Storage.followup(interaction, callback_tuple) - else - :error -> - {:error, :unknown_command} - - # the response type was not a callback tuple, no need to follow up - res_type when is_atom(res_type) -> - :ok - - {:error, reason} -> - {:error, reason} - end - end - - @impl true - def process_queue(scope, id \\ __MODULE__) do - GenServer.call(id, {:process_queue, scope}) - end - - @impl true - def queue_command(path, command, id \\ __MODULE__) do - command_name = - if is_binary(path) do - path - else - path - |> Enum.take(1) - |> List.first() - |> unwrap_key() - end - - GenServer.call(id, {:queue, command_name, command}) - end - - @impl true - def add_command(path, command, scope, id \\ __MODULE__) do - payload = build_payload(path, command) - - command_name = - if is_binary(path) do - path - else - path - |> Enum.take(1) - |> List.first() - |> unwrap_key() - end - - GenServer.call(id, {:add, payload, command_name, command, scope}) - end - - @impl true - def remove_command(name, command_id, scope, id \\ __MODULE__) do - GenServer.call(id, {:remove, name, command_id, scope}) - end - - ## Impl - @impl true - def init(init_arg) do - {:ok, init_arg} - end - - def handle_call({:process_queue, :global}, _from, commands) do - command_list = - Enum.map(commands, fn {p, c} -> - build_payload(p, c) - end) - - case Nostrum.Api.ApplicationCommand.bulk_overwrite_global_commands(command_list) do - {:ok, _} = response -> - {:reply, response, commands} - - error -> - {:reply, {:error, error}, commands} - end - end - - def handle_call({:process_queue, guild_id}, _from, commands) do - command_list = - Enum.map(commands, fn {p, c} -> - build_payload(p, c) - end) - - case Nostrum.Api.ApplicationCommand.bulk_overwrite_guild_commands(guild_id, command_list) do - {:ok, _} = response -> - {:reply, response, commands} - - error -> - {:reply, {:error, error}, commands} - end - end - - @impl true - def handle_call({:queue, name, command}, _from, commands) do - {:reply, :ok, Map.put(commands, name, command)} - end - - @impl true - def handle_call({:add, payload, name, command, :global}, _from, commands) do - case Nostrum.Api.ApplicationCommand.create_global_command(payload) do - {:ok, _} = response -> - {:reply, response, Map.put(commands, name, command)} - - error -> - {:reply, {:error, error}, commands} - end - end - - @impl true - def handle_call({:add, payload, name, command, guild_id_list}, _from, commands) - when is_list(guild_id_list) do - res = - Enum.reduce(guild_id_list, {[], []}, fn guild_id, {errors, responses} -> - case Nostrum.Api.ApplicationCommand.create_guild_command(guild_id, payload) do - {:ok, _} = response -> - {errors, [response | responses]} - - error -> - {[error | errors], responses} - end - end) - - {:reply, res, Map.put(commands, name, command)} - end - - @impl true - def handle_call({:add, payload, name, command, guild_id}, _from, commands) do - case Nostrum.Api.ApplicationCommand.create_guild_command(guild_id, payload) do - {:ok, _} = response -> - {:reply, response, Map.put(commands, name, command)} - - error -> - {:reply, {:error, error}, commands} - end - end - - @impl true - def handle_call({:remove, name, command_id, :global}, _from, commands) do - case Nostrum.Api.ApplicationCommand.delete_global_command(command_id) do - :ok = response -> - {:reply, response, Map.delete(commands, name)} - - error -> - {:reply, {:error, error}, commands} - end - end - - @impl true - def handle_call({:remove, name, command_id, guild_id_list}, _from, commands) - when is_list(guild_id_list) do - res = - Enum.reduce(guild_id_list, {[], []}, fn guild_id, {errors, responses} -> - case Nostrum.Api.ApplicationCommand.delete_guild_command(guild_id, command_id) do - :ok -> - {errors, [:ok | responses]} - - error -> - {[error | errors], responses} - end - end) - - {:reply, res, Map.delete(commands, name)} - end - - @impl true - def handle_call({:remove, name, command_id, guild_id}, _from, commands) do - case Nostrum.Api.ApplicationCommand.delete_guild_command(guild_id, command_id) do - :ok = response -> - {:reply, response, Map.delete(commands, name)} - - error -> - {:reply, {:error, error}, commands} - end - end - - @impl true - def handle_call({:fetch, %Interaction{data: %{name: name}}}, _from, commands) do - {:reply, Map.fetch(commands, name), commands} - end - - defp build_payload(name, command) when is_binary(name) do - Code.ensure_loaded(command) - - options = - if function_exported?(command, :options, 0) do - command.options() - |> parse_option_types() - else - [] - end - - %{ - type: parse_type(command.type()), - name: name - } - |> put_type_specific_fields(command, options) - end - - # This seems like a hacky way to unwrap the outer list... - defp build_payload(path, command) do - build_payload({path, command}) - |> List.first() - end - - defp build_payload({path, command}) when is_map(path) do - Enum.map(path, fn {{name, desc}, value} -> - if get_depth(path) == 3 do - %{ - name: name, - description: desc, - options: build_payload({value, command}) - } - else - # Determine type based on depth of the remaining path. 2 is SUB_COMMAND_GROUP and 1 is SUB_COMMAND - type = (get_depth(value) == 2 && 2) || 1 - - %{ - type: type, - name: name, - description: desc, - options: build_payload({value, command}) - } - end - end) - end - - defp build_payload({options, _command}) when is_list(options) do - parse_option_types(options) - end - - defp parse_type(type) do - Map.fetch!( - %{ - slash: 1, - user: 2, - message: 3 - }, - type - ) - end - - defp parse_option_types(options) do - Enum.map(options, fn - map when is_map_key(map, :type) -> - # Map.update!(map, :type, &Map.fetch!(@option_type_map, &1)) - updated_map = Map.update!(map, :type, &Map.fetch!(@option_type_map, &1)) - - if is_map_key(updated_map, :options) do - parsed_options = parse_option_types(updated_map[:options]) - Map.replace!(updated_map, :options, parsed_options) - else - updated_map - end - - map -> - map - end) - end - - defp get_depth(path, depth) do - Enum.reduce(path, 0, fn - {_key, map}, cur_max when is_list(map) -> - max(cur_max, get_depth(map, depth + 1)) - - _, cur_max -> - max(cur_max, depth) - end) - end - - defp get_depth(path) do - get_depth(path, 1) - end - - defp unwrap_key({{key, _}, _}), do: key - defp unwrap_key({key, _}), do: key - - defp put_type_specific_fields(payload, command, options) do - if command.type() == :slash do - payload - |> Map.put(:description, command.description()) - |> Map.put(:options, options) - else - payload - end - end -end diff --git a/lib/nosedrum/text_command/invoker.ex b/lib/nosedrum/text_command/invoker.ex deleted file mode 100644 index cffa4aa3..00000000 --- a/lib/nosedrum/text_command/invoker.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule Nosedrum.TextCommand.Invoker do - @moduledoc """ - Invoker modules process messages from Discord. - - They determine the following things: - - whether the message is a valid command - - whether the author is permitted to issue the command. - When both of these conditions are met, the command callback function is invoked. - """ - - @doc """ - Called by consumers when a message arrives. - - This is the main entry point for invokers: from here on they - check whether the message could contain a valid command based - on their configured prefix and ask the selected `Nosedrum.TextCommand.Storage` - whether a command exists. If it exists, they proceed to invoke it - using only the arguments to the message, with bot prefix and command - invocation removed from the message. - - The second argument, `storage`, determines which storage implementation the - command invoker should use. A command invoker implementation can supply - this argument by default if applicable. - - `storage_process` is passed along to the given `storage` and determines - which storage process, ETS table, or similar is used. - """ - @callback handle_message( - message :: Nostrum.Struct.Message.t(), - storage :: Nostrum.Storage, - reference :: any() - ) :: - any() -end diff --git a/lib/nosedrum/text_command/invoker/split.ex b/lib/nosedrum/text_command/invoker/split.ex deleted file mode 100644 index 91739a3b..00000000 --- a/lib/nosedrum/text_command/invoker/split.ex +++ /dev/null @@ -1,175 +0,0 @@ -defmodule Nosedrum.TextCommand.Invoker.Split do - @moduledoc """ - An `OptionParser.split/1`-based command processor. - - This parser supports command prefixes configured via the `nosedrum.prefix` - configuration variable. You can specify a single prefix (as a string): - - config :nosedrum, - prefix: "!" - - Or a multiple prefixes (as a list of strings): - - config :nosedrum, - prefix: ["!", "?"] - - If multiple prefixes are specified, the first match in the list will be used. - For example, if the prefixes are `["a", "ab"]`, then the message `"ab foo"` - will invoke the command `b`, with the argument `foo`. However, if the prefixes - were ordered `["ab", "a"]`, the message `"abfoo"` would invoke the command - `foo` with no arguments. - - The default prefix is `.`, and the prefix are looked up at compilation time - due to the nature of Elixir's binary matching. This means that if you change - your prefix, you need to recompile this module, usually using - `mix deps.compile --force nosedrum`. - - This invoker checks predicates and returns predicate failures to the caller. - """ - - @behaviour Nosedrum.TextCommand.Invoker - - # This must be looked up at compilation time due to the nature of Elixir's - # binary matching. Also, SPEEEEEEEEEEEEEED!! - @prefix Application.compile_env(:nosedrum, :prefix, ".") - - alias Nosedrum.Helpers - alias Nosedrum.TextCommand.Predicates - alias Nostrum.Struct.Message - - @spec remove_prefix(String.t()) :: String.t() | :not_found - if is_list(@prefix) do - defp remove_prefix(message) do - with( - # Find the prefix that was used in the message, going through the list of prefixes - real_prefix when real_prefix != :not_found <- - Enum.find( - @prefix, - :not_found, - &String.starts_with?(message, &1) - ), - # Then, just remove the prefix from the message - prefix_length <- byte_size(real_prefix), - content <- - message - |> binary_part(prefix_length, byte_size(message) - prefix_length) - ) do - content - else - _no_match -> :not_found - end - end - else - defp remove_prefix(message) do - # credo:disable-for-next-line - with @prefix <> content <- message do - content - else - _no_match -> :not_found - end - end - end - - @doc """ - Handle the given message. - - This involves checking whether the message starts with the given prefix, splitting - the message into command and arguments, looking up a candidate command, and finally - resolving it and invoking the module. - - ## Arguments - - - `message`: The message to handle. - - `storage`: The storage implementation the command invoker should use. - - `storage_process`: The storage process, ETS table, or similar that is used by - the storage process. For instance, this allows you to use different ETS tables - for the `Nosedrum.TextCommand.Storage.ETS` module if you wish. - - ## Return value - - Returns `:ignored` if one of the following applies: - - The message does not start with the configured prefix. - - The message only contains the configured prefix. - - No command could be looked up that matches the command the message invokes. - - ## Examples - - iex> Nosedrum.TextCommand.Invoker.Split.handle_message(%{content: "foo"}) - :ignored - iex> Nosedrum.TextCommand.Invoker.Split.handle_message(%{content: "."}) - :ignored - """ - @spec handle_message(Message.t(), module, atom() | pid()) :: - :ignored - | {:error, {:unknown_subcommand, String.t(), :known, [String.t() | :default]}} - | {:error, :predicate, {:error | :noperm, any()}} - | any() - def handle_message( - message, - storage \\ Nosedrum.TextCommand.Storage.ETS, - storage_process \\ :nosedrum_commands - ) do - with content when content != :not_found <- remove_prefix(message.content), - [command | args] <- Helpers.quoted_split(content), - cog when cog != nil <- storage.lookup_command(command, storage_process) do - handle_command(cog, message, args) - else - _mismatch -> - :ignored - end - end - - @spec parse_args(module(), [String.t()]) :: [String.t()] | any() - defp parse_args(command_module, args) do - if function_exported?(command_module, :parse_args, 1) do - command_module.parse_args(args) - else - args - end - end - - @spec invoke(module(), Message.t(), [String.t()]) :: - any() | {:error, :predicate, {:noperm | :error, any()}} - defp invoke(command_module, msg, args) do - case Predicates.evaluate(msg, command_module.predicates()) do - :passthrough -> - command_module.command(msg, parse_args(command_module, args)) - - {atom, _reason} = response when atom in [:noperm, :error] -> - {:error, :predicate, response} - end - end - - @spec handle_command(map() | module(), Message.t(), [String.t()]) :: - :ignored - | {:error, {:unknown_subcommand, String.t(), :known, [String.t() | :default]}} - | {:error, :predicate, {:error | :noperm, any()}} - | any() - defp handle_command(command_map, msg, original_args) when is_map(command_map) do - maybe_subcommand = List.first(original_args) - - case Map.fetch(command_map, maybe_subcommand) do - {:ok, subcommand} -> - # If we have at least one subcommand, that means `original_args` - # needs to at least contain one element, so `args` is either empty - # or the rest of the arguments excluding the subcommand name. - [_subcommand | args] = original_args - # Recursively traverse down to a command module via the `is_map/1` - # guard clause attached to this function head. - handle_command(subcommand, msg, args) - - :error -> - # Does the command group have a default command to invoke? - if Map.has_key?(command_map, :default) do - # If yes, invoke it with all arguments. - invoke(command_map.default, msg, original_args) - else - {:error, {:unknown_subcommand, maybe_subcommand, :known, Map.keys(command_map)}} - end - end - end - - defp handle_command(command_module, msg, args) do - invoke(command_module, msg, args) - end -end diff --git a/lib/nosedrum/text_command/predicates.ex b/lib/nosedrum/text_command/predicates.ex deleted file mode 100644 index 0d0d4300..00000000 --- a/lib/nosedrum/text_command/predicates.ex +++ /dev/null @@ -1,146 +0,0 @@ -defmodule Nosedrum.TextCommand.Predicates do - @moduledoc """ - Built-in command predicates and predicate evaluation. - - `c:Nosedrum.TextCommand.predicates/0` allows commands to specify a set of - conditions that must be met before a command is invoked, effectively allowing - you to deny access to certain access. For instance, allowing every user on - your server to issue a ban command may not be desirable. - - For using the `c:Nosedrum.TextCommand.predicates/0` callback, you can either - define your own predicates or use [those provided by this - module](#predicates). - """ - @moduledoc since: "0.2.0" - - alias Nostrum.Cache.GuildCache - alias Nostrum.Cache.MemberCache - alias Nostrum.Permission - alias Nostrum.Struct.Guild.Member - alias Nostrum.Struct.Message - - @all_permissions Permission.all() - - @typedoc """ - The result of a predicate evaluation, determining whether the command - invocation should proceed or abort. - - The values represent the following: - - `:passthrough` if the predicate permits the command to be invoked. - - `{:noperm, any()}` if the predicate does not permit the command to be invoked. - - `{:error, any()}` if the predicate could not determine its result. - - On `:passthrough`, invokers will continue with predicate checking and - finally invoke the command. On `:noperm` or `:error`, command execution - is aborted. - """ - @typedoc since: "0.2.0" - @type evaluation_result :: :passthrough | {:noperm, any()} | {:error, any()} - - @typedoc """ - A condition that must pass before a command is invoked. - - ## Example - - defmodule MyBot.Predicates do - alias Nostrum.Struct.Message - - def guild_only(%Message{guild_id: nil}) do - {:error, "This command can only be used on guilds."} - end - - def guild_only(_msg), do: :passthrough - end - """ - @typedoc since: "0.2.0" - @type predicate :: (Message.t() -> evaluation_result) - - @doc """ - Lazily evaluate all given `predicates` and return the result. - - While usually used by command invokers, you can use this function if you need - to manually evaluate the result of predicates. - - ## Return value - If all predicates returned `:passthrough`, `:passthrough` is returned. - Otherwise, the error of the first predicate returning one is returned. - """ - @doc section: :evaluation - @doc since: "0.2.0" - @spec evaluate(Message.t(), [predicate]) :: evaluation_result - def evaluate(message, predicates) do - predicates - |> Stream.map(& &1.(message)) - |> Enum.find(:passthrough, &match?({kind, _reason} when kind in [:error, :noperm], &1)) - end - - @doc """ - Check whether the command was invoked on a guild. - - Note that `has_permission/1` already checks whether a command was invoked - on a guild and this predicate does not need to be stack with it. - """ - @doc section: :predicates - @doc since: "0.2.0" - def guild_only(%Message{guild_id: nil}), do: {:error, "this command can only be used on guilds"} - def guild_only(_), do: :passthrough - - @doc """ - Check whether the message author has the given `permission`. - - This does not directly return an `t:evaluation_result/0`: it returns a - function allowing you to use this in your `c:Nosedrum.TextCommand.predicates/0` - callback. - - When evaluation fails, an error with a description of the required permission - is returned, or another error when the permission could not be checked on the - author - for example, because the guild or member was not found in the cache. - - ## Example - - defmodule MyBot.Cogs.Ban do - @behaviour Nosedrum.TextCommand - - def usage, do: ["ban "] - def description, do: "Ban the given `member`." - def predicates, do: [Nosedrum.TextCommand.Predicates.has_permission(:ban_members)] - def command(msg, [target]) do - # ... 🔨 - end - end - """ - @doc section: :predicates - @doc since: "0.2.0" - @spec has_permission(Permission.t()) :: predicate - def has_permission(permission) when permission in @all_permissions do - fn msg -> - with {:is_on_guild, true} <- {:is_on_guild, msg.guild_id != nil}, - {:ok, guild} <- GuildCache.get(msg.guild_id), - {:member, {:ok, member}} <- - {:member, MemberCache.get(msg.guild_id, msg.author.id)}, - {:has_permission, true} <- - {:has_permission, permission in Member.guild_permissions(member, guild)} do - :passthrough - else - {:error, _reason} -> - {:error, "❌ this guild is not in the cache, can't check perms"} - - {:has_permission, false} -> - permission_string = - permission - |> Atom.to_string() - |> String.upcase() - - {:noperm, "🚫 you need the `#{permission_string}` permission to do that"} - - {:is_on_guild, false} -> - {:noperm, "🚫 this command can only be used on guilds"} - - {:member, {:error, :not_found}} -> - {:error, "❌ you're not in the guild member cache, can't check perms"} - end - end - end - - # vim: textwidth=80 sw=2 ts=2: -end diff --git a/lib/nosedrum/text_command/storage.ex b/lib/nosedrum/text_command/storage.ex deleted file mode 100644 index c84e8aa2..00000000 --- a/lib/nosedrum/text_command/storage.ex +++ /dev/null @@ -1,75 +0,0 @@ -defmodule Nosedrum.TextCommand.Storage do - @moduledoc """ - Storages contain commands and are used by command invokers to look up commands. - - How you start a storage is up to the module itself - what is - expected is that storage modules implement the behaviours - documented in this module. - - The public-facing API of storage modules takes an optional argument, - the storage process or other information used to identify the storage - such as an ETS table name. - """ - - @typedoc """ - A single command module or mapping of subcommand names to command groups. - - In addition to subcommand names, the key `:default` can be specified by - the module. `:default` should be invoked when none of the subcommands in the - map match. - """ - @type command_group :: - module - | %{optional(:default) => command_group, required(String.t()) => command_group} - - @typedoc """ - The "invocation path" of the command. - - The public-facing API of storage modules should use this in order - to allow users to identify the command they want to operate on. - - ## Usage - To identify a single command, use a single element list, such as `["echo"]`. - To identify a subcommand, use a pair, such as `["infraction", "search"]`. - To identify the default subcommand invoked when no matching subcommand is - found, specify the group name first, then `:default`, such as - `["tags", :default]`. - """ - @type command_path :: [String.t() | :default, ...] - - @doc """ - Look up a command group under the specified `name`. - - If the command was not found, `nil` should be returned. - """ - @callback lookup_command(name :: String.t(), storage :: reference) :: command_group | nil - - @doc """ - Add a new command under the given `path`. - - If a command has the c:Nosedrum.TextCommand.aliases/0 callback defined, - they will also be added under `path`. If the command already exists, - no error should be returned. - """ - @callback add_command(path :: command_path, command :: module, storage :: reference) :: - :ok | {:error, String.t()} - - @doc """ - Remove the command under the given `path`. - - If a command has the c:Nosedrum.TextCommand.aliases/0 callback defined, - they will also be removed under `path`. If the command does not exist, - no error should be returned. - """ - @callback remove_command(path :: command_path, storage :: reference) :: - :ok | {:error, String.t()} - - @doc """ - Return a mapping of command names to `t:command_group/0`s. - - For top-level commands, the value should be a string, otherwise, - a mapping of subcommand names to subcommand modules as described - on `t:command_group/0`s documentation should be returned. - """ - @callback all_commands(storage :: reference) :: %{String.t() => command_group} -end diff --git a/lib/nosedrum/text_command/storage/ets.ex b/lib/nosedrum/text_command/storage/ets.ex deleted file mode 100644 index 5e742419..00000000 --- a/lib/nosedrum/text_command/storage/ets.ex +++ /dev/null @@ -1,153 +0,0 @@ -defmodule Nosedrum.TextCommand.Storage.ETS do - @moduledoc """ - An implementation of the `Nosedrum.TextCommand.Storage` behaviour based on ETS tables. - - This module needs to be configured as part of your supervision tree as it - spins up a `GenServer` which owns the command table. If you want to obtain - the table ID of the internal ETS table, send a call with the message `:tid`. - """ - @behaviour Nosedrum.TextCommand.Storage - @default_table :nosedrum_commands - @default_table_options [{:read_concurrency, true}, :ordered_set, :public, :named_table] - - @doc false - use GenServer - - @spec put_nested_command(map(), [String.t()], module()) :: - Nosedrum.TextCommand.Storage.command_group() - defp put_nested_command(acc, [name], command) do - if function_exported?(command, :aliases, 0) do - Enum.reduce(command.aliases(), acc, &Map.put(&2, &1, command)) - else - acc - end - |> Map.put(name, command) - end - - defp put_nested_command(acc, [name | path], command), - do: Map.put(acc, name, put_nested_command(Map.get(acc, name, %{}), path, command)) - - @impl true - def add_command(path, command, table_ref \\ @default_table) - - def add_command([name], command, table_ref) do - if function_exported?(command, :aliases, 0) do - Enum.each(command.aliases, &:ets.insert(table_ref, {&1, command})) - end - - :ets.insert(table_ref, {name, command}) - - :ok - end - - def add_command([name | path], command, table_ref) do - case lookup_command(name, table_ref) do - maybe_map when is_map(maybe_map) or is_nil(maybe_map) -> - cog = put_nested_command(maybe_map || %{}, path, command) - :ets.insert(table_ref, {name, cog}) - :ok - - module when not is_map(module) -> - {:error, "command `#{name} is a top-level command, cannot add subcommand at `#{path}"} - end - end - - @spec empty_cog?({String.t() | :default, map() | module()}) :: boolean() - defp empty_cog?({_key, module}) when is_atom(module), do: false - defp empty_cog?({_key, %{}}), do: true - defp empty_cog?({_key, cog}), do: Enum.all?(cog, &empty_cog?/1) - - @impl true - def remove_command(path, table_ref \\ @default_table) - - def remove_command([name], table_ref) do - command = lookup_command(name, table_ref) - - if function_exported?(command, :aliases, 0) do - Enum.each(command.aliases(), &:ets.delete(table_ref, {&1, command})) - end - - :ets.delete(table_ref, name) - - :ok - end - - def remove_command([name | path], table_ref) do - case lookup_command(name, table_ref) do - nil -> - :ok - - module when not is_map(module) -> - {:error, - "command `#{name}` is a top-level command, cannot remove subcommand at `#{path}`"} - - map -> - {dropped_cog, updated_cog} = pop_in(map, path) - - updated_cog = - if function_exported?(dropped_cog, :aliases, 0) do - Map.drop(updated_cog, dropped_cog.aliases()) - else - updated_cog - end - - case Enum.reject(updated_cog, &empty_cog?/1) do - [] -> - :ets.delete(table_ref, name) - :ok - - entries -> - mapped = Map.new(entries) - :ets.insert(table_ref, {name, mapped}) - :ok - end - end - end - - @impl true - def lookup_command(name, table_ref \\ @default_table) do - case :ets.lookup(table_ref, name) do - [] -> - nil - - [{_name, command}] -> - command - end - end - - @impl true - def all_commands(table_ref \\ @default_table) do - table_ref - |> :ets.tab2list() - |> Enum.reduce(%{}, fn {name, cog}, acc -> Map.put(acc, name, cog) end) - end - - @doc """ - Initialize the ETS command storage. - - By default, the table used for storing commands is a named table with - the name `#{@default_table}`. The table reference is stored internally - as the state of this process, the public-facing API functions default - to using the table name to access the module. - """ - def start_link( - table_name \\ @default_table, - table_options \\ @default_table_options, - gen_options - ) do - GenServer.start_link(__MODULE__, {table_name, table_options}, gen_options) - end - - @impl true - @doc false - def init({table_name, table_options}) do - tid = :ets.new(table_name, table_options) - - {:ok, tid} - end - - @impl true - def handle_call(:tid, _, tid) do - {:reply, tid, tid} - end -end diff --git a/lib/nosedrum/text_command/text_command.ex b/lib/nosedrum/text_command/text_command.ex deleted file mode 100644 index c59e6e09..00000000 --- a/lib/nosedrum/text_command/text_command.ex +++ /dev/null @@ -1,143 +0,0 @@ -defmodule Nosedrum.TextCommand do - @moduledoc """ - The command behaviour specifies the interface that a command module should implement. - - Command modules are stateless by themselves, if you want to capture state, drop it in - a different delegated module. - - Commands by themselves do not implement any logic for determining whether they were - invoked. This is done by a command processor module, which also maps command strings - to the command modules to be invoked. - - ## Example - - defmodule MyBot.Cogs.Echo do - @behaviour Nosedrum.Command - @moduledoc false - - @impl true - def usage, do: ["echo "] - - @impl true - def description, do: "display a line of text" - - @impl true - def parse_args(args), do: Enum.join(args, " ") - - @impl true - def predicates, do: [] - - @impl true - def command(msg, "") do - response = "Need something to echo" - {:ok, _msg} = Nostrum.Api.create_message(msg.channel_id, response) - end - - def command(msg, text) do - {:ok, _msg} = Nostrum.Api.create_message(msg.channel_id, text) - end - end - """ - - alias Nostrum.Struct.Message - - @doc """ - Return a list of possible ways to use the command (or its subcommands). - - This is separate from the `c:description/0` callback since the command - processor is expected to prepend usage strings with its configured prefix. - - ## Example - - @impl true - def usage do - [ - "clean ", - "clean " - ] - end - """ - @callback usage() :: [String.t()] - - @doc """ - Return a description of the command and how to use it. - - ## Example - - @impl true - def description, - do: "Cleanup messages." - """ - @callback description() :: String.t() - - @doc """ - Return a list of predicates that must pass before this command is invoked. - - This is expected to be invoked by the command processor. Predicates retrieve - the message which should be checked for allowance to invoke the command. - - Predicates are evaluated lazily: For instance, you can have a command which - uses a predicate ensuring the command can only be invoked on guilds (for - example, by checking whether `msg.guild_id == nil`), and predicates - afterwards do not need to check whether the command was invoked on a guild. - - ## Return value - - If the predicate allows the invoking user to issue the command, it should - return `:passthrough`. If the user has no permission to execute the command, - a pair in the form `{:noperm, reason}` should be returned, where `reason` is - a concise description of why the user is not allowed to execute the command. - `{:error, reason}` should be returned when the predicate was not able to - determine allowance of the invoking user. - - ## Example - - def is_bot(message) do - if message.author.bot do - :passthrough - else - {:noperm, "sorry, only bots allowed"} - end - end - - @impl true - def predicates, do: [&is_bot/1] - """ - @callback predicates() :: [ - (Message.t() -> :passthrough | {:noperm, String.t()} | {:error, String.t()}) - ] - - @doc """ - An optional callback that can be used to parse the arguments into something more usable. - - For example, one might want to use `OptionParser` along with the - arguments to create a more customized command. - This command receives the command arguments (without any prefix, command, - or if applicable, subcommand), and should return whatever the - `c:command/2` function should be passed as the `args` argument. - """ - @callback parse_args(args :: [String.t()]) :: any() - - @doc """ - Execute the command invoked by the given `t:Nostrum.Struct.Message.t/0`. - - The second parameter is a list of arguments with the command - (or subcommand) along with the bot prefix removed. - If the command defines `c:parse_args/1`, the returned value of - that function will be passed instead (marked as `any()` here). - The return value of this function is unused. - """ - @callback command(msg :: Message.t(), args :: [String.t()] | any()) :: any() - - @doc """ - An optional callback that returns a list of aliases for the command. - - `Nosedrum.TextCommand.Storage` implementations use this function when adding/removing a command - with `add_command/2` or `remove_command/2`. If any of the aliases returned conflict - with existing commands/aliases, they will be overwritten. - """ - @doc since: "0.4.0" - @callback aliases() :: [String.t()] - - @optional_callbacks [parse_args: 1, aliases: 0] -end diff --git a/lib/octocon.ex b/lib/octocon.ex deleted file mode 100644 index 6b758971..00000000 --- a/lib/octocon.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon do - @moduledoc """ - Octocon keeps the contexts that define your domain - and business logic. - - Contexts are also responsible for managing your data, regardless - if it comes from the database, an external API or others. - """ -end diff --git a/lib/octocon/accounts.ex b/lib/octocon/accounts.ex deleted file mode 100644 index bad10d5d..00000000 --- a/lib/octocon/accounts.ex +++ /dev/null @@ -1,1420 +0,0 @@ -defmodule Octocon.Accounts do - @moduledoc """ - The Accounts context. - - This module represents the data layer for working with user accounts. Almost all operations - require a system "identity", which is a tuple of the form: - - - `{:system, system_id}`: References a user by their system ID (7-character, alphanumeric lowercase string). - - `{:discord, discord_id}`: References a user by their Discord ID. - """ - - import Ecto.Query, warn: false - - alias Octocon.Accounts.{ - Field, - User, - UserRegistry - } - - alias Octocon.Alters.Alter - - alias Octocon.Journals.{ - AlterJournalEntry, - GlobalJournalAlters, - GlobalJournalEntry - } - - alias Octocon.Repo - - alias OctoconWeb.SystemJSON - - defp unwrap_system_identity_where(system_identity, extra \\ []) do - case system_identity do - {:system, system_id} -> [id: system_id] |> Keyword.merge(extra) - {:discord, discord_id} -> [discord_id: discord_id] |> Keyword.merge(extra) - {:username, username} -> [username: username] |> Keyword.merge(extra) - {:email, email} -> [email: email] |> Keyword.merge(extra) - {:apple, apple_id} -> [apple_id: apple_id] |> Keyword.merge(extra) - {:google, google_id} -> [google_id: google_id] |> Keyword.merge(extra) - end - end - - # def unwrap_system_identity_where(system_identity, extra \\ []) do - # case system_identity do - # {:system, system_id} -> - # [id: system_id] |> Keyword.merge(extra) - - # {:discord, _} = identity -> - # [id: Accounts.id_from_system_identity(identity, :system)] - # |> Keyword.merge(extra) - # end - # end - - def get_user_registry(system_identity) do - query = - case system_identity do - {:system, system_id} -> - UserRegistry - |> where([ur], ur.user_id == ^system_id) - - {:discord, discord_id} -> - UserRegistry - |> where([ur], ur.discord_id == ^discord_id) - - {:email, email} -> - UserRegistry - |> where([ur], ur.email == ^email) - - {:username, username} -> - UserRegistry - |> where([ur], ur.username == ^username) - - {:apple, apple_id} -> - UserRegistry - |> where([ur], ur.apple_id == ^apple_id) - - {:google, google_id} -> - UserRegistry - |> where([ur], ur.google_id == ^google_id) - end - |> select([ur], ur) - - Repo.one_global(query) - end - - def delete_user_registry(system_identity) do - case get_user_registry(system_identity) do - nil -> - {:error, :not_found} - - user_registry -> - Repo.delete_global(user_registry) - end - end - - def user_exists?(system_identity) do - get_user_registry(system_identity) != nil - end - - def region_for_user(system_identity) do - case get_user_registry(system_identity) do - nil -> nil - %UserRegistry{region: region} -> String.to_existing_atom(region) - end - end - - @doc """ - Returns the total number of users in the database. - """ - def count do - Octocon.Repo.aggregate(UserRegistry, :count, prefix: :global, consistency: :local_one) - end - - @doc """ - Given a system identity, returns the desired ID. This does not query the database if the desired ID type was already given. - - ## Examples - - iex> id_from_system_identity({:system, "abcdefg"}, :system) - "abcdefg" - - iex> id_from_system_identity({:discord, "123456789"}, :discord) - "123456789" - - iex> id_from_system_identity({:system, "abcdefg"}, :discord) - "123456789" # This will **query the database** for the Discord ID of the system with the ID "abcdefg". - - iex> id_from_system_identity({:discord, "123456789"}, :system) - "abcdefg" # This will **query the database** for the system ID of the user with the Discord ID "123456789". - """ - def id_from_system_identity(system_identity, type) - - # {:user, user_id} is an alias for {:system, user_id} - def id_from_system_identity({:user, user_id}, type), - do: id_from_system_identity({:system, user_id}, type) - - # When we're asking for the type we already have, just return the given ID. - def id_from_system_identity({type, id}, type), do: id - - def id_from_system_identity(system_identity, type) do - registry = get_user_registry(system_identity) - - if registry == nil do - nil - else - case type do - :system -> registry.user_id - :discord -> registry.discord_id - :email -> registry.email - :username -> registry.username - :apple -> registry.apple_id - :google -> registry.google_id - end - end - end - - @doc """ - Given a system identity, returns the `Octocon.Accounts.User` struct associated with it. Returns `nil` if no user is found. - """ - def get_user(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - User - |> where(^where) - - Repo.one_regional(query, {:user, system_identity}) - end - - @doc """ - Given a system identity, returns the user associated with it. Raises an `Ecto.NoResultsError` if no user is found. - """ - def get_user!(system_identity) do - case get_user(system_identity) do - nil -> raise Ecto.NoResultsError - user -> user - end - end - - @doc """ - Given a username, returns the `Octocon.Accounts.User` struct associated with it. Returns `nil` if no user is found. - """ - def get_user_by_username(username) do - query = - from u in User, - where: u.username == ^username - - Repo.one_regional(query, {:user, {:username, username}}) - end - - @doc """ - Given a username, returns the ID of the user associated with it. Returns `nil` if no user is found. - """ - def get_user_id_by_username(username) do - id_from_system_identity({:username, username}, :system) - end - - def allocate_uuid do - uuid = Octocon.Accounts.User.generate_uuid() - - case region_for_user({:system, uuid}) do - nil -> - uuid - - _ -> - allocate_uuid() - end - end - - def register_user(user_id, region, attrs \\ %{}) do - %UserRegistry{ - user_id: user_id, - region: to_string(region) - } - |> UserRegistry.changeset(attrs) - |> Repo.insert_global() - end - - def update_user_registry(user_id, attrs \\ %{}) do - case get_user_registry({:system, user_id}) do - nil -> - {:error, :not_found} - - user_registry -> - user_registry - |> UserRegistry.changeset(attrs) - |> Repo.update_global() - end - end - - @doc """ - Creates a user given the provided `email` address and extra `attrs`. - """ - def create_user_from_email(email, attrs \\ %{}) do - if user_exists?({:email, email}) do - {:error, :user_exists} - else - current_region = Octocon.ClusterUtils.current_db_region() - uuid = allocate_uuid() - - user = - email - |> User.create_from_email_changeset(uuid, attrs) - |> Repo.insert_regional({:region, current_region}) - - case user do - {:ok, user_struct} = result -> - register_user(user_struct.id, current_region, %{ - email: user_struct.email - }) - - result - - err -> - err - end - end - end - - @doc """ - Creates a user given the provided `discord_id` and extra `attrs`. - """ - def create_user_from_discord(discord_id, attrs \\ %{}) do - if user_exists?({:discord, discord_id}) do - {:error, :user_exists} - else - current_region = Octocon.ClusterUtils.current_db_region() - uuid = allocate_uuid() - OctoconDiscord.Cache.Proxy.invalidate(discord_id) - - user = - discord_id - |> User.create_from_discord_changeset(uuid, attrs) - |> Repo.insert_regional({:region, current_region}) - - case user do - {:ok, user_struct} = result -> - register_user(user_struct.id, current_region, %{ - discord_id: user_struct.discord_id - }) - - result - - err -> - err - end - end - end - - @doc """ - Creates a user given the provided `apple_id` and extra `attrs`. - """ - def create_user_from_apple(apple_id, attrs \\ %{}) do - if user_exists?({:apple, apple_id}) do - {:error, :user_exists} - else - current_region = Octocon.ClusterUtils.current_db_region() - uuid = allocate_uuid() - - user = - apple_id - |> User.create_from_apple_changeset(uuid, attrs) - |> Repo.insert_regional({:region, current_region}) - - case user do - {:ok, user_struct} = result -> - register_user(user_struct.id, current_region, %{ - apple_id: user_struct.apple_id - }) - - result - - err -> - err - end - end - end - - @doc """ - Links a Discord ID to an existing user. - - This will return: - - {:error, changeset} when the Discord ID is already linked to another account - - {:error, :already_linked} when the user is already linked to a Discord account - """ - def link_discord_to_user(%User{} = user, discord_id) do - cond do - user.discord_id != nil -> - {:error, :already_linked} - - user_exists?({:discord, discord_id}) -> - {:error, :user_exists} - - true -> - user - |> User.update_changeset(%{discord_id: discord_id}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - OctoconDiscord.Cache.Proxy.invalidate(discord_id) - - spawn(fn -> - update_user_registry(user.id, %{discord_id: discord_id}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "discord_account_linked", %{ - discord_id: discord_id - }) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def link_discord_to_user(system_identity, discord_id) do - user = get_user!(system_identity) - link_discord_to_user(user, discord_id) - end - - def update_username(system_identity, username) do - user = get_user!(system_identity) - - cond do - user.username == username -> - {:error, :already_linked} - - user_exists?({:username, username}) -> - {:error, :user_exists} - - true -> - user - |> User.update_changeset(%{username: username}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{username: username}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "username_updated", %{ - username: username - }) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - @doc """ - Links an email to an existing user. Accepts either a `User` struct directly or a system identity. - - This will return: - - {:error, changeset} when the email is already linked to another account. - - {:error, :already_linked} when the user is already linked to an email. - - {:error, :cannot_unlink} when the user is not linked to a Discord account (all accounts must be linked to at least one authentication method). - """ - def link_email_to_user(%User{} = user, email) do - cond do - user.email != nil -> - {:error, :already_linked} - - user_exists?({:email, email}) -> - {:error, :user_exists} - - true -> - user - |> User.update_changeset(%{email: email}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{email: email}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "google_account_linked", %{ - email: email - }) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def link_email_to_user(system_identity, email) do - user = get_user!(system_identity) - link_email_to_user(user, email) - end - - @doc """ - Links an Apple account to an existing user. Accepts either a `User` struct directly or a system identity. - - This will return: - - {:error, changeset} when the Apple user ID is already linked to another account. - - {:error, :already_linked} when the user is already linked to an Apple account. - - {:error, :cannot_unlink} when the user is not linked to a different account type (all accounts must be linked to at least one authentication method). - """ - def link_apple_to_user(%User{} = user, apple_id) do - cond do - user.apple_id != nil -> - {:error, :already_linked} - - user_exists?({:apple, apple_id}) -> - {:error, :user_exists} - - true -> - user - |> User.update_changeset(%{apple_id: apple_id}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{apple_id: apple_id}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "apple_account_linked", %{ - apple_id: apple_id - }) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def link_apple_to_user(system_identity, email) do - user = get_user!(system_identity) - link_apple_to_user(user, email) - end - - @doc """ - Unlinks an email from an existing user. Accepts either a `User` struct directly or a system identity. - - This will return: - - {:error, :not_linked} when the user is not linked to an email. - - {:error, :cannot_unlink} when the user is not linked to a Discord account (all accounts must be linked to at least one authentication method). - """ - def unlink_email_from_user(%User{} = user) do - cond do - user.email == nil -> - {:error, :not_linked} - - user.discord_id == nil && user.apple_id == nil -> - {:error, :cannot_unlink} - - true -> - user - |> User.update_changeset(%{email: nil}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{email: nil}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "google_account_unlinked", %{}) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def unlink_email_from_user(system_identity) do - user = get_user!(system_identity) - unlink_email_from_user(user) - end - - @doc """ - Unlinks a Discord ID from an existing user. Accepts either a `User` struct directly or a system identity. - - This will return: - - {:error, :not_linked} when the user is not linked to a Discord account. - - {:error, :cannot_unlink} when the user is not linked to another account type (all accounts must be linked to at least one authentication method). - """ - def unlink_discord_from_user(%User{} = user) do - cond do - user.discord_id == nil -> - {:error, :not_linked} - - user.email == nil && user.apple_id == nil -> - {:error, :cannot_unlink} - - true -> - user - |> User.update_changeset(%{discord_id: nil}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{discord_id: nil}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "discord_account_unlinked", %{}) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def unlink_discord_from_user(system_identity) do - user = get_user!(system_identity) - unlink_discord_from_user(user) - end - - @doc """ - Unlinks an Apple account from an existing user. Accepts either a `User` struct directly or a system identity. - - This will return: - - {:error, :not_linked} when the user is not linked to an Apple account. - - {:error, :cannot_unlink} when the user is not linked to another account type (all accounts must be linked to at least one authentication method). - """ - def unlink_apple_from_user(%User{} = user) do - cond do - user.apple_id == nil -> - {:error, :not_linked} - - user.email == nil && user.discord_id == nil -> - {:error, :cannot_unlink} - - true -> - user - |> User.update_changeset(%{apple_id: nil}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - update_user_registry(user.id, %{apple_id: nil}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "apple_account_unlinked", %{}) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - end - - def unlink_apple_from_user(system_identity) do - user = get_user!(system_identity) - unlink_apple_from_user(user) - end - - @doc """ - Updates a `Octocon.Accounts.User` struct with the provided `attrs`. - """ - def update_user(%User{} = user, attrs) do - user - |> User.update_changeset(attrs) - |> Repo.update_regional({:user, {:system, user.id}}) - |> case do - {:ok, value} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "self_updated", %{ - data: SystemJSON.data_me(value) - }) - end) - - {:ok, value} - - {:error, changeset} -> - {:error, changeset} - end - end - - @doc """ - Returns the primary front of the user associated with the provided system identity. May be `nil` if no primary front is set. - """ - def get_primary_front(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - User - |> where(^where) - |> select([u], u.primary_front) - - Repo.one_regional(query, {:user, system_identity}) - end - - def get_proxy_cache_data(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - User - |> where(^where) - |> select([u], [ - :primary_front, - :discord_settings - ]) - - Repo.one_regional(query, {:user, system_identity}) - end - - @doc """ - Sets the primary front of the user associated with the provided system identity. May be `nil` to unset the primary front. Accepts - either a `User` struct directly or a system identity. - - This must be used in place of `update_user/2` to ensure that the primary front is properly updated in the Discord cache. - """ - def set_primary_front(identifier, alter_id) - - def set_primary_front(%User{} = user, alter_id) do - result = update_user(user, %{primary_front: alter_id || nil}) - - if match?({:ok, _}, result) do - spawn(fn -> - OctoconDiscord.Cache.Proxy.update_primary_front(user.discord_id, alter_id || nil) - - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "primary_front", %{ - alter_id: alter_id || nil - }) - end) - end - - result - end - - def set_primary_front(system_identity, alter_id) do - get_user!(system_identity) - |> set_primary_front(alter_id) - end - - def update_discord_settings(%User{} = user, attrs) do - settings = user.discord_settings || %Octocon.Accounts.DiscordSettings{} - - old_attrs = - settings - |> Map.from_struct() - |> Map.put( - :server_settings, - (Map.get(settings, :server_settings) || []) - |> Enum.map(&Map.from_struct/1) - ) - - result = - user - |> User.update_changeset(%{ - discord_settings: Map.merge(old_attrs, attrs) - }) - |> Repo.update_regional({:user, {:system, user.id}}) - - if match?({:ok, _}, result) do - OctoconDiscord.Cache.Proxy.invalidate(user.discord_id) - end - - result - end - - def update_discord_settings(system_identity, attrs) do - get_user!(system_identity) - |> update_discord_settings(attrs) - end - - def update_server_settings(%User{} = user, guild_id, settings) when is_binary(guild_id) do - settings = Map.drop(settings, [:guild_id]) - - old_discord_settings = - (user.discord_settings || %Octocon.Accounts.DiscordSettings{}) |> Map.from_struct() - - old_settings = - ((user.discord_settings || %Octocon.Accounts.DiscordSettings{}).server_settings || []) - |> Enum.map(&Map.from_struct/1) - - result = - if Enum.any?(old_settings, fn server -> - server.guild_id == guild_id - end) do - old_settings - |> Enum.map(fn server -> - if server.guild_id == guild_id do - Map.merge(server, settings) - else - server - end - end) - else - [Map.merge(%{guild_id: guild_id}, settings) | old_settings] - end - |> then(fn new_settings -> - User.update_changeset(user, %{ - discord_settings: Map.merge(old_discord_settings, %{server_settings: new_settings}) - }) - end) - |> Repo.update_regional({:user, {:system, user.id}}) - - if match?({:ok, _}, result) do - OctoconDiscord.Cache.Proxy.invalidate(user.discord_id) - end - - result - end - - def update_server_settings(system_identity, guild_id, settings) do - get_user!(system_identity) - |> update_server_settings(guild_id, settings) - end - - @doc """ - Deletes the user associated with the provided system identity. - """ - def delete_user(system_identity) do - user = get_user!(system_identity) - - case Repo.delete_regional(user, {:user, {:system, user.id}}) do - {:error, _} -> - {:error, :not_deleted} - - {:ok, _} -> - spawn(fn -> - delete_user_registry({:system, user.id}) - delete_user_data(user.id) - - OctoconDiscord.Cache.Proxy.invalidate(user.discord_id) - OctoconDiscord.Autocomplete.invalidate_all(system_identity) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "account_deleted", %{}) - end) - - :ok - end - end - - defp delete_user_data(system_id) do - from( - a in Alter, - where: a.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - at in Octocon.Tags.AlterTag, - where: at.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - f in Octocon.Fronts.Front, - where: f.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - f in Octocon.Fronts.CurrentFront, - where: f.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - aj in Octocon.Journals.AlterJournalEntry, - where: aj.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - aj in Octocon.Journals.GlobalJournalEntry, - where: aj.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - p in Octocon.Polls.Poll, - where: p.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - t in Octocon.Tags.Tag, - where: t.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - gw in Octocon.Journals.GlobalJournalAlters, - where: gw.user_id == ^system_id - ) - |> Repo.delete_all_regional({:user, {:system, system_id}}) - - from( - f in Octocon.Friendships.Friendship, - where: f.user_id == ^system_id - ) - |> Repo.delete_all_global() - - # Delete reciprocal friendships (Scylla cannot directly DELETE from a secondary index) - from( - f in Octocon.Friendships.Friendship, - where: f.friend_id == ^system_id - ) - |> Repo.all_global() - |> Enum.map(& &1.user_id) - |> Enum.each(fn user_id -> - from( - f in Octocon.Friendships.Friendship, - where: f.user_id == ^user_id and f.friend_id == ^system_id - ) - |> Repo.delete_all_global() - end) - - from( - f in Octocon.Friendships.Request, - where: f.from_id == ^system_id - ) - |> Repo.delete_all_global() - - # Delete reciprocal friend requests (Scylla cannot directly DELETE from a secondary index) - from( - f in Octocon.Friendships.Request, - where: f.to_id == ^system_id - ) - |> Repo.all_global() - |> Enum.map(& &1.from_id) - |> Enum.each(fn from_id -> - from( - f in Octocon.Friendships.Request, - where: f.from_id == ^from_id and f.to_id == ^system_id - ) - |> Repo.delete_all_global() - end) - end - - @doc """ - Wipes all alters associated with the user associated with the provided system identity. - - This also resets the user's lifetime alter count to 0, so the next alter will be assigned ID 1. - """ - def wipe_alters(system_identity) do - OctoconDiscord.Cache.Proxy.invalidate(system_identity) - - user = get_user!(system_identity) - - q1 = - from a in Alter, - where: a.user_id == ^user.id - - q2 = - from f in Octocon.Fronts.Front, - where: f.user_id == ^user.id - - q3 = - from f in Octocon.Journals.AlterJournalEntry, - where: f.user_id == ^user.id - - q4 = - from f in Octocon.Journals.GlobalJournalAlters, - where: f.user_id == ^user.id - - q5 = - from f in Octocon.Tags.AlterTag, - where: f.user_id == ^user.id - - q6 = - from f in Octocon.Fronts.CurrentFront, - where: f.user_id == ^user.id - - [q1, q2, q3, q4, q5, q6] - |> Enum.each(fn query -> - Repo.delete_all_regional(query, {:user, {:system, user.id}}) - end) - - user - |> User.update_changeset(%{primary_front: nil, lifetime_alter_count: 0}) - |> Repo.update_regional({:user, {:system, user.id}}) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "alters_wiped", %{}) - - OctoconDiscord.Autocomplete.Alter.invalidate(system_identity) - OctoconDiscord.Autocomplete.Front.invalidate(system_identity) - end) - - :ok - rescue - e -> {:error, e} - end - - def wipe_tags(system_identity) do - user = get_user!(system_identity) - - q1 = - from t in Octocon.Tags.Tag, - where: t.user_id == ^user.id - - q2 = - from at in Octocon.Tags.AlterTag, - where: at.user_id == ^user.id - - [q1, q2] - |> Enum.each(fn query -> - Repo.delete_all_regional(query, {:user, {:system, user.id}}) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "tags_wiped", %{}) - end) - - :ok - rescue - e -> {:error, e} - end - - @doc """ - Builds a changeset based on the given `Octocon.Accounts.User` struct and `attrs` to change. - """ - def change_user(%User{} = user, attrs \\ %{}) do - User.update_changeset(user, attrs) - end - - @doc false - def get_user_proxy_map_old(system_identity) do - where = unwrap_system_identity_where(system_identity) - - user = from(u in User, where: ^where) |> Repo.one_regional({:user, system_identity}) - - alters = - from( - a in Alter, - where: a.user_id == ^user.id, - select: struct(a, [:id, :discord_proxies]) - ) - |> Repo.all_regional({:user, {:system, user.id}}) - |> Enum.filter(fn alter -> alter.discord_proxies != [] && alter.discord_proxies != nil end) - - Enum.reduce(alters, %{}, fn %{id: alter_id, discord_proxies: proxies}, acc -> - # Skip if proxies is nil or empty - Enum.reduce(proxies, acc, fn proxy, map -> - Map.put(map, proxy, {user.id, alter_id}) - end) - end) - end - - @doc """ - Wipes all custom fields for the user with the provided system identity. - """ - def wipe_fields(system_identity) do - user = get_user!(system_identity) - - user - |> User.update_changeset(%{fields: []}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - @doc """ - Edits an existing custom field for the user with the provided system identity. - """ - def edit_field(system_identity, id, data) do - user = get_user!(system_identity) - - fields = - (user.fields || []) - |> Enum.map(fn field -> - if field.id == id do - struct(field, data) - else - field - end - end) - - user - |> User.update_changeset(%{fields: fields}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - @doc """ - Adds a new custom field to the user with the provided system identity. - """ - def add_field(system_identity, data) do - user = get_user!(system_identity) - - fields = - (user.fields || []) ++ - [ - struct( - %Field{ - id: Ecto.UUID.generate(), - locked: false - }, - data - ) - ] - - user - |> User.update_changeset(%{fields: fields}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - def add_bulk_fields(system_identity, data) do - user = get_user!(system_identity) - - fields = (user.fields || []) ++ data - - user - |> User.update_changeset(%{fields: fields}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - @doc """ - Removes an existing custom field from the user with the provided system identity. - """ - def remove_field(system_identity, id) do - user = get_user!(system_identity) - - fields = - (user.fields || []) - |> Enum.reject(fn field -> field.id == id end) - - user - |> User.update_changeset(%{fields: fields}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - @doc """ - Relocates an existing custom field for the user with the provided system identity to the desired `index`. - """ - def relocate_field(system_identity, id, index) do - user = get_user!(system_identity) - - old_fields = user.fields || [] - field = Enum.find(old_fields, fn field -> field.id == id end) - - fields = - old_fields - |> Enum.reject(&(&1.id == id)) - |> List.insert_at(index, field) - - user - |> User.update_changeset(%{fields: fields}) - |> Repo.update_regional({:user, {:system, user.id}}) - |> wrap_fields_broadcast(system_identity) - end - - @doc """ - Returns the custom fields for the user with the provided system identity. - """ - def get_user_fields(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - User - |> where(^where) - |> select([u], u.fields) - - Repo.one_regional(query, {:user, system_identity}) - end - - defp wrap_fields_broadcast({:ok, _} = result, system_identity) do - spawn(fn -> - user_id = id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!("system:#{user_id}", "fields_updated", %{ - fields: - get_user_fields(system_identity) |> Enum.map(&Map.drop(&1, [:__meta__, :__struct__])) - }) - end) - - result - end - - defp wrap_fields_broadcast({:error, _} = result, _), do: result - - def wipe_encrypted_data(system_identity) do - alias Octocon.Journals.{AlterJournalEntry, GlobalJournalAlters, GlobalJournalEntry} - - user = get_user!(system_identity) - specifier = {:user, {:system, user.id}} - - {:ok, _} = - user - |> User.update_changeset(%{ - encryption_initialized: false, - encryption_key_checksum: nil - }) - |> Repo.update_regional(specifier) - - from( - ja in GlobalJournalAlters, - where: ja.user_id == ^user.id - ) - |> Repo.delete_all_regional(specifier) - - from( - j in GlobalJournalEntry, - where: j.user_id == ^user.id - ) - |> Repo.delete_all_regional(specifier) - - from( - j in AlterJournalEntry, - where: j.user_id == ^user.id - ) - |> Repo.delete_all_regional(specifier) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "encrypted_data_wiped", %{}) - end) - - :ok - rescue - e -> {:error, e} - end - - def gather_export_data(system_identity) do - user = get_user!(system_identity) - - identifier = {:user, {:system, user.id}} - - alters = - from(a in Alter, - where: a.user_id == ^user.id, - select: - struct(a, [ - :id, - :name, - :description, - :pronouns, - :pinned, - :avatar_url, - :untracked, - :archived, - :alias, - :proxy_name, - :color, - :security_level, - :fields, - :discord_proxies, - :inserted_at, - :updated_at - ]) - ) - |> Repo.all_regional(identifier) - - fronts = - from(f in Octocon.Fronts.Front, - where: f.user_id == ^user.id, - select: struct(f, [:id, :alter_id, :comment, :time_start, :time_end]) - ) - |> Repo.all_regional(identifier) - |> Enum.filter(fn front -> front.alter_id != nil && front.time_start != nil end) - - alter_tags = - from(at in Octocon.Tags.AlterTag, - where: at.user_id == ^user.id, - select: struct(at, [:tag_id, :alter_id]) - ) - |> Repo.all_regional(identifier) - |> Enum.filter(fn at -> at.alter_id != nil && at.tag_id != nil end) - |> Enum.group_by(& &1.tag_id) - - tags = - from(t in Octocon.Tags.Tag, - where: t.user_id == ^user.id, - select: - struct(t, [ - :id, - :name, - :description, - :color, - :security_level, - :parent_tag_id, - :inserted_at, - :updated_at - ]) - ) - |> Repo.all_regional(identifier) - |> Enum.map(fn tag -> - %Octocon.Tags.Tag{tag | alters: Map.get(alter_tags, tag.id, [])} - end) - - polls = - from(p in Octocon.Polls.Poll, - where: p.user_id == ^user.id, - select: - struct(p, [ - :id, - :title, - :description, - :type, - :data, - :time_end, - :inserted_at, - :updated_at - ]) - ) - |> Repo.all_regional(identifier) - - %{ - user: user, - alters: alters, - alter_tags: alter_tags, - fronts: fronts, - tags: tags, - polls: polls - } - end - - def format_pk_export(%{ - user: %{ - username: username, - description: description, - avatar_url: avatar_url - }, - alters: alters, - tags: tags - }) do - %{ - "version" => 2, - "name" => username, - "description" => (description || "") |> String.byte_slice(0, 1000), - "avatar_url" => avatar_url, - "switches" => [], - "members" => - alters - |> Enum.map(fn %Octocon.Alters.Alter{} = alter -> - %{ - "id" => to_string(alter.id), - "name" => (alter.name || "") |> String.byte_slice(0, 100), - "pronouns" => (alter.pronouns || "") |> String.byte_slice(0, 100), - "description" => (alter.description || "") |> String.byte_slice(0, 1000), - "color" => format_import_color(alter.color), - "avatar_url" => alter.avatar_url, - "proxy_tags" => - (alter.discord_proxies || []) - |> Enum.map(fn proxy -> - [prefix, suffix] = - proxy - |> String.trim() - |> String.split("text", parts: 2) - - %{ - "prefix" => prefix |> String.byte_slice(0, 50), - "suffix" => suffix |> String.byte_slice(0, 50) - } - end), - "display_name" => alter.proxy_name - } - end), - "groups" => - tags - |> Enum.with_index() - |> Enum.map(fn {%Octocon.Tags.Tag{} = tag, index} -> - %{ - "id" => to_string(index), - "name" => tag.name, - "description" => tag.description, - "color" => format_import_color(tag.color), - "members" => - tag.alters - |> Enum.map(fn alter_tag -> - to_string(alter_tag.alter_id) - end) - } - end) - } - |> Jason.encode!() - end - - def format_full_export(%{ - user: user, - alters: alters, - tags: tags, - fronts: fronts, - polls: polls - }) do - user = Map.take(user, [:username, :description, :id, :avatar_url, :fields]) - - user = - if user.fields == nil or user.fields == [], - do: user, - else: %{ - user - | fields: - user.fields - |> Enum.map(fn field -> - Map.take(field, [:id, :name, :type, :locked, :security_level]) - end) - } - - %{ - "user" => user, - "alters" => - Enum.map(alters, fn alter -> - alter = - Map.take(alter, [ - :id, - :name, - :pronouns, - :description, - :color, - :avatar_url, - :proxy_name, - :discord_proxies, - :fields - ]) - - if alter.fields == nil or alter.fields == [], - do: alter, - else: %{ - alter - | fields: alter.fields |> Enum.map(fn field -> Map.take(field, [:id, :value]) end) - } - end), - "fronts" => - Enum.map(fronts, fn front -> - Map.take(front, [:id, :alter_id, :comment, :time_start, :time_end]) - end), - "tags" => - Enum.map(tags, fn tag -> - Map.take(tag, [ - :id, - :name, - :description, - :color, - :security_level, - :parent_tag_id, - :inserted_at, - :updated_at - ]) - |> Map.put( - :alters, - Enum.map(tag.alters, fn alter_tag -> alter_tag.alter_id end) - ) - end), - "polls" => - Enum.map(polls, fn poll -> - Map.take(poll, [ - :id, - :title, - :description, - :type, - :data, - :time_end, - :inserted_at, - :updated_at - ]) - end) - } - |> Jason.encode!() - end - - defp format_import_color(nil), do: nil - defp format_import_color("#" <> rest = color) when byte_size(color) == 7, do: rest - defp format_import_color(_), do: nil -end diff --git a/lib/octocon/accounts/discord_settings.ex b/lib/octocon/accounts/discord_settings.ex deleted file mode 100644 index bc320bf3..00000000 --- a/lib/octocon/accounts/discord_settings.ex +++ /dev/null @@ -1,60 +0,0 @@ -defmodule Octocon.Accounts.DiscordSettings do - @moduledoc false - - use Ecto.Schema - - import Ecto.Changeset - import Exandra, only: [embedded_type: 3] - - @primary_key false - embedded_schema do - field :system_tag, :string, default: nil - field :show_system_tag, :boolean, default: false - - field :case_insensitive_proxies, :boolean, default: false - field :show_pronouns, :boolean, default: true - field :ids_as_proxies, :boolean, default: false - field :silent_proxying, :boolean, default: false - field :use_proxy_delay, :boolean, default: false - - field :global_autoproxy_mode, Ecto.Enum, values: [off: 0, front: 1, latch: 2], default: :off - field :global_latched_alter, :integer, default: nil - - embedded_type(:server_settings, Octocon.Accounts.ServerSettings, cardinality: :many) - end - - def changeset(data, attrs \\ %{}) do - data - |> cast(attrs, [ - :system_tag, - :show_system_tag, - :case_insensitive_proxies, - :show_pronouns, - :ids_as_proxies, - :silent_proxying, - :server_settings, - :use_proxy_delay, - :global_autoproxy_mode, - :global_latched_alter - ]) - |> validate_length(:system_tag, max: 20) - end - - def server_settings_map(%__MODULE__{server_settings: server_settings}) do - (server_settings || []) - |> Enum.map(fn %Octocon.Accounts.ServerSettings{ - guild_id: guild_id, - proxying_disabled: proxying_disabled, - autoproxy_mode: autoproxy_mode, - latched_alter: latched_alter - } -> - {guild_id, - %{ - proxying_disabled: proxying_disabled, - autoproxy_mode: autoproxy_mode, - latched_alter: latched_alter - }} - end) - |> Enum.into(%{}) - end -end diff --git a/lib/octocon/accounts/field.ex b/lib/octocon/accounts/field.ex deleted file mode 100644 index 775a81ae..00000000 --- a/lib/octocon/accounts/field.ex +++ /dev/null @@ -1,42 +0,0 @@ -defmodule Octocon.Accounts.Field do - @moduledoc """ - A field represents a "custom field" in a system. It consists of: - - - A name (string, up to 100 characters) - - A type (enum, one of `:text`, `:number`, or `:boolean`) - - A security level (enum, one of `:public`, `:friends_only`, `:trusted_only`, or `:private`) - - Alters loosely reference fields by their ID in their own `fields` map, which - is to be cross-referenced with the account fields by a frontend client. - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key {:id, Ecto.UUID, autogenerate: true} - - embedded_schema do - field :name, :string - field :type, Ecto.Enum, values: [text: 0, number: 1, boolean: 2], default: :text - field :locked, :boolean, default: false - - field :security_level, Ecto.Enum, - values: [public: 0, friends_only: 1, trusted_only: 2, private: 3], - default: :private - end - - @doc """ - Builds a changeset based on the given `Octocon.Accounts.Field` struct and `attrs` to change. - """ - def changeset(struct, attrs \\ %{}) - - def changeset(nil, attrs), do: changeset(%__MODULE__{}, attrs) - - def changeset(%__MODULE__{} = field, attrs) do - field - |> cast(attrs, [:name, :type, :security_level, :locked]) - |> validate_required([:name, :type, :security_level]) - |> validate_length(:name, min: 1, max: 100) - |> validate_inclusion(:type, [:text, :number, :boolean]) - |> validate_inclusion(:security_level, [:public, :friends_only, :trusted_only, :private]) - end -end diff --git a/lib/octocon/accounts/server_settings.ex b/lib/octocon/accounts/server_settings.ex deleted file mode 100644 index 6cacf20f..00000000 --- a/lib/octocon/accounts/server_settings.ex +++ /dev/null @@ -1,21 +0,0 @@ -defmodule Octocon.Accounts.ServerSettings do - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - embedded_schema do - field :guild_id, :string - - field :proxying_disabled, :boolean, default: false - - field :autoproxy_mode, Ecto.Enum, values: [off: 0, front: 1, latch: 2], default: :off - field :latched_alter, :integer, default: nil - end - - def changeset(data, attrs \\ %{}) do - data - |> cast(attrs, [:guild_id, :proxying_disabled, :autoproxy_mode, :latched_alter]) - |> validate_required([:guild_id]) - end -end diff --git a/lib/octocon/accounts/user.ex b/lib/octocon/accounts/user.ex deleted file mode 100644 index afef4ae4..00000000 --- a/lib/octocon/accounts/user.ex +++ /dev/null @@ -1,144 +0,0 @@ -defmodule Octocon.Accounts.User do - @moduledoc """ - A user represents a single Octocon account. It consists of: - - - An ID (7-character alphanumeric lowercase string) - - An email address (optional) - - A Discord ID (optional) - - A username (optional) - - An avatar URL (optional) - - A lifetime alter count (integer, used to generate new alter IDs without conflicts) - - A primary front (integer ID of the primary fronting alter or nil) - - An autoproxy mode (enum, one of `:off`, `:front`, or `:latch`) - - A system tag (optional, a short string to identify the system, used on Discord) - - A flag to show the system tag on Discord (boolean) - - A flag to enable case-insensitive proxying (boolean) - - A flag to show pronouns on proxied messages (boolean) - - A user MUST have either an email address, a Discord ID, or both, but not neither. - """ - use Ecto.Schema - import Ecto.Changeset - import Exandra, only: [embedded_type: 3] - - @primary_key false - - schema "users" do - field :id, :string, primary_key: true - field :region, :string, virtual: true - - field :discord_id, :string - field :apple_id, :string - field :google_id, :string - field :email, :string - - field :username, :string - - field :avatar_url, :string - field :description, :string - - field :lifetime_alter_count, :integer, default: 0 - field :primary_front, :integer - - embedded_type(:discord_settings, Octocon.Accounts.DiscordSettings, cardinality: :one) - embedded_type(:fields, Octocon.Accounts.Field, cardinality: :many) - - field :salt, :string - field :encryption_initialized, :boolean, default: false - field :encryption_key_checksum, :string - - timestamps() - end - - defp global_validations(changeset) do - changeset - |> validate_format(:id, ~r/^[a-z]{7}$/) - |> validate_format(:email, ~r/@/) - |> validate_format(:discord_id, ~r/^\d{17,22}$/) - |> validate_length(:description, max: 3000) - # Dear christ help me - |> validate_format(:username, ~r/^[a-zA-Z0-9]([a-zA-Z0-9_\-.]{3,22})[a-zA-Z0-9]$/) - # Make username not able to look like a system id (primary key) - |> validate_format(:username, ~r/^(?:(?![a-z]{7}).*)$/) - |> validate_inclusion(:primary_front, 1..32_767) - |> unique_constraint(:email) - |> unique_constraint(:discord_id) - |> unique_constraint(:apple_id) - |> unique_constraint(:google_id) - |> unique_constraint(:username) - end - - @doc """ - Builds a changeset based on the given `Octocon.Accounts.User` struct and `attrs` to change. - """ - def update_changeset(user, attrs \\ %{}) do - user - |> cast(attrs, [ - :id, - :email, - :discord_id, - :apple_id, - :avatar_url, - :username, - :lifetime_alter_count, - :primary_front, - :description, - :discord_settings, - :fields, - :salt, - :encryption_initialized, - :encryption_key_checksum - ]) - |> global_validations() - end - - @doc """ - Builds a changeset to create a new user with the given `discord_id` and extra `attrs`. - """ - def create_from_discord_changeset(discord_id, uuid, attrs) do - %__MODULE__{ - discord_id: to_string(discord_id), - id: uuid, - salt: generate_salt() - } - |> cast(attrs, [:id, :email, :discord_id, :username]) - |> validate_required([:id, :discord_id]) - |> global_validations() - end - - @doc """ - Builds a changeset to create a new user with the given `apple_id` and extra `attrs`. - """ - def create_from_apple_changeset(apple_id, uuid, attrs) do - %__MODULE__{ - apple_id: to_string(apple_id), - id: uuid, - salt: generate_salt() - } - |> cast(attrs, [:id, :email, :apple_id, :username]) - |> validate_required([:id, :apple_id]) - |> global_validations() - end - - @doc """ - Builds a changeset to create a new user with the given `email` and extra `attrs`. - """ - def create_from_email_changeset(email, uuid, attrs) do - %__MODULE__{ - email: email, - id: uuid, - salt: generate_salt() - } - |> cast(attrs, [:id, :email, :discord_id, :username]) - |> validate_required([:id, :email]) - |> global_validations() - end - - def generate_uuid do - Nanoid.generate(7, "abcdefghijklmnopqrstuvwxyz") - end - - defp generate_salt do - :crypto.strong_rand_bytes(32) |> Base.encode64() - end -end diff --git a/lib/octocon/accounts/user_region.ex b/lib/octocon/accounts/user_region.ex deleted file mode 100644 index 2eafff30..00000000 --- a/lib/octocon/accounts/user_region.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule Octocon.Accounts.UserRegistry do - @moduledoc false - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - schema "user_registry" do - field :user_id, :string, primary_key: true - field :discord_id, :string - field :email, :string - field :username, :string - field :apple_id, :string - field :google_id, :string - - field :region, :string - - timestamps() - end - - def changeset(user, attrs \\ %{}) do - user - |> cast(attrs, [ - :user_id, - :discord_id, - :email, - :username, - :apple_id, - :google_id, - :region - ]) - end -end diff --git a/lib/octocon/alters.ex b/lib/octocon/alters.ex deleted file mode 100644 index 140ef2e0..00000000 --- a/lib/octocon/alters.ex +++ /dev/null @@ -1,596 +0,0 @@ -defmodule Octocon.Alters do - @moduledoc """ - The Alters context. - - This module represents the data layer for working with alters. Almost all operations - require an alter "identity", which is a tuple of the form: - - - `{:id, alter_id}`: References an alter by their alter ID (integer up to 32,767). - - `{:alias, alter_alias}`: References an alter by their unique alias. - - Additionally, most operations require a system identity. See `Octocon.Accounts` for more - information on system identities. - """ - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters.Alter, - Friendships, - Repo - } - - alias Octocon.Alters.Field, as: AlterField - - @all_fields Alter.__struct__() - |> Map.from_struct() - |> Map.drop(Alter.dropped_fields()) - |> Map.keys() - - @bare_fields [:id, :name, :avatar_url, :pronouns, :color, :security_level, :alias, :pinned] - - def bare_fields, do: @bare_fields - - defp unwrap_system_identity_where(system_identity, extra \\ []) do - case system_identity do - {:system, system_id} -> - [user_id: system_id] |> Keyword.merge(extra) - - {_, _} = identity -> - [user_id: Accounts.id_from_system_identity(identity, :system)] - |> Keyword.merge(extra) - end - end - - defp unwrap_alter_identity_where(alter_identity) do - case alter_identity do - {:id, alter_id} -> [id: alter_id] - {:alias, aliaz} when aliaz != nil -> [alias: aliaz] - end - end - - @doc """ - Checks if an alter alias is already taken. - """ - def alias_taken?(system_identity, aliaz) do - where = unwrap_system_identity_where(system_identity, alias: aliaz) - - query = - Alter - |> where(^where) - - case Repo.all_regional(query, {:user, system_identity}) do - [] -> false - _ -> true - end - end - - @doc """ - Resolves an alter identity to an alter ID. - """ - def resolve_alter(system_identity, alter_identity) - - def resolve_alter(nil, _), do: false - def resolve_alter(_, nil), do: false - - def resolve_alter(system_identity, alter_identity) do - where = - unwrap_system_identity_where(system_identity, unwrap_alter_identity_where(alter_identity)) - - query = - Alter - |> where(^where) - |> select([a], a.id) - - case Repo.one_regional(query, {:user, system_identity}) do - nil -> false - id -> id - end - end - - def resolve_alter_id_dumb(_system_identity, {:id, alter_id}), do: alter_id - - def resolve_alter_id_dumb(system_identity, {:alias, aliaz}) do - where = unwrap_system_identity_where(system_identity, alias: aliaz) - - query = - Alter - |> where(^where) - |> select([a], a.id) - - Repo.one_regional(query, {:user, system_identity}) - end - - @doc """ - Returns the total number of alters in the database. - """ - def count do - Repo.region_list() - |> Enum.map(&Repo.aggregate(Alter, :count, prefix: &1)) - |> Enum.sum() - end - - @doc """ - Gets an alter by their identity. If no `fields` are provided, all struct fields are returned. - - Provide a `fields` list to only return the specified fields to save on bandwidth. - """ - def get_alter_by_id(system_identity, alter_identity, fields \\ @all_fields) do - where = - unwrap_system_identity_where(system_identity, unwrap_alter_identity_where(alter_identity)) - - query = - Alter - |> where(^where) - |> select([a], struct(a, ^fields)) - - case Repo.one_regional(query, {:user, system_identity}) do - nil -> - case alter_identity do - {:id, _} -> {:error, :no_alter_id} - {:alias, _} -> {:error, :no_alter_alias} - end - - alter -> - {:ok, alter} - end - end - - @doc """ - Gets an alter by their identity. If no `fields` are provided, all struct fields are returned. - - Provide a `fields` list to only return the specified fields to save on bandwidth. - - Raises an error if the alter is not found. - """ - def get_alter_by_id!(system_identity, alter_identity, fields \\ @all_fields) do - case get_alter_by_id(system_identity, alter_identity, fields) do - {:ok, alter} -> alter - {:error, :no_alter_id} -> raise "Alter not found with ID" - {:error, :no_alter_alias} -> raise "Alter not found with alias" - end - end - - @doc """ - Returns all alters associated with the given system identity. If no `fields` are provided, - all struct fields are returned. - - Provide a `fields` list to only return the specified fields to save on bandwidth. - """ - def get_alters_by_id(system_identity, fields \\ @all_fields) do - where = unwrap_system_identity_where(system_identity) - - Alter - |> where(^where) - |> select([a], struct(a, ^fields)) - |> Repo.all_regional({:user, system_identity}) - |> Enum.sort_by(& &1.id, :asc) - end - - @doc """ - Returns all alters with the given IDs associated with the given system identity. If no `fields` are - provided, all struct fields are returned. - - `alter_ids` MUST be a list of alter IDs (i.e. integers), NOT alter identities. - - Provide a `fields` list to only return the specified fields to save on bandwidth. - """ - def get_alters_by_id_bounded(system_identity, alter_ids, fields \\ @all_fields) do - where = unwrap_system_identity_where(system_identity) - - Alter - |> where(^where) - |> where([a], a.id in ^alter_ids) - |> select([a], struct(a, ^fields)) - |> Repo.all_regional({:user, system_identity}) - |> Enum.sort_by(& &1.id, :asc) - end - - @doc """ - Gets an alter by their identity. - - This function is guarded by the caller's friendship level with the system. For example, an alter with - a security level of `:trusted_only` can only be viewed by a caller with a friendship level of `:trusted_friend`. - """ - def get_alter_guarded(system_identity, alter_identity, caller_identity) do - friendship_level = Friendships.get_friendship_level(system_identity, caller_identity) - user_fields = Accounts.get_user_fields(system_identity) - - alter = - get_alter_by_id(system_identity, alter_identity, [ - :security_level, - :name, - :pronouns, - :description, - :color, - :fields, - :avatar_url, - :discord_proxies, - :id - ]) - - # Pretend that the alter doesn't exist if the caller is not friends with the system - security_level = - case alter do - {:ok, alter} -> alter.security_level - {:error, :no_alter_id} -> :private - {:error, :no_alter_alias} -> :private - end - - if can_view_entity?(friendship_level, security_level) and alter != {:error, :no_alter_id} and - alter != {:error, :no_alter_alias} do - {:ok, alter} = alter - fields = get_guarded_fields(user_fields, alter.fields || [], friendship_level) - {:ok, %{alter | fields: fields}} - else - :error - end - end - - @doc """ - Gets all alters associated with the given system identity. - - This function is guarded by the caller's friendship level with the system. For example, an alter with - a security level of `:trusted_only` can only be viewed by a caller with a friendship level of `:trusted_friend`. - """ - def get_alters_guarded(system_identity, caller_identity) do - friendship_level = Friendships.get_friendship_level(system_identity, caller_identity) - user_fields = Accounts.get_user_fields(system_identity) - - get_alters_by_id(system_identity) - |> Enum.filter(&can_view_entity?(friendship_level, &1.security_level)) - |> Enum.map(fn alter -> - fields = get_guarded_fields(user_fields, alter.fields || [], friendship_level) - %{alter | fields: fields} - end) - end - - @doc false - def get_alters_guarded_bare_batch(system_identity, friendship_level, alter_ids) do - get_alters_by_id_bounded(system_identity, alter_ids, @bare_fields) - |> Stream.filter(&can_view_entity?(friendship_level, &1.security_level)) - |> Enum.map(&Map.drop(&1, [:security_level])) - end - - defp get_guarded_fields(user_fields, alter_fields, friendship_level) do - user_fields - |> Stream.filter(fn user_field -> - can_view_entity?(friendship_level, user_field.security_level) - end) - |> Stream.filter(fn user_field -> - Enum.any?(alter_fields, &(&1.id == user_field.id)) - end) - |> Enum.map(fn user_field -> - alter_field = Enum.find(alter_fields, &(&1.id == user_field.id)) - - %{ - id: user_field.id, - name: user_field.name, - type: user_field.type, - value: alter_field.value - } - end) - end - - def get_random_alter(system_identity, fields \\ @all_fields) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - all_alter_ids = - from( - a in Alter, - where: a.user_id == ^system_id, - select: a.id - ) - |> Repo.all_regional({:user, system_identity}) - - if all_alter_ids == [] do - nil - else - random_alter_id = Enum.random(all_alter_ids) - - where = - unwrap_system_identity_where(system_identity, id: random_alter_id) - - query = - Alter - |> where(^where) - |> select([a], struct(a, ^fields)) - - case Repo.one_regional(query, {:user, system_identity}) do - nil -> nil - alter -> {:ok, alter} - end - end - end - - @doc """ - Given the friendship level and security level of an entity, returns whether the entity can be viewed. - - - A security level of `:public` can be viewed by anyone. - - A security level of `:friends_only` can be viewed by a friendship level of `:friend` or `:trusted_friend`. - - A security level of `:trusted_only` can be viewed by a friendship level of `:trusted_friend`. - - A security level of `:private` can never be viewed externally. - """ - def can_view_entity?(friendship_level, security_level) - - def can_view_entity?(_, :public), do: true - - def can_view_entity?(:friend, :friends_only), do: true - - def can_view_entity?(:trusted_friend, target) when target in [:friends_only, :trusted_only], - do: true - - def can_view_entity?(_, _), do: false - - @doc false - def create_alter_internal(user, attrs, force_id \\ nil) do - alter_id = if force_id == nil, do: user.lifetime_alter_count + 1, else: force_id - - case Accounts.update_user(user, %{lifetime_alter_count: alter_id}) do - {:ok, _user} -> - {:ok, alter} = - change_alter(%Alter{user_id: user.id, id: alter_id}, attrs) - |> Repo.insert_regional({:user, {:system, user.id}}) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "alter_created", %{ - alter: alter |> OctoconWeb.System.AlterJSON.data_me() - }) - end) - - {:ok, alter_id, get_alter_by_id!({:system, user.id}, {:id, alter_id})} - - {:error, _changeset} -> - {:error, :database} - end - rescue - _ -> - if force_id == nil do - expected_alter_id = get_highest_alter_id({:system, user.id}) + 1 - - if user.lifetime_alter_count + 1 != expected_alter_id + 1 do - # Retry with the correct alter ID - create_alter_internal(user, attrs, expected_alter_id) - else - {:error, :database} - end - end - end - - def get_highest_alter_id(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - Alter - |> where(^where) - |> select([a], a.id) - |> order_by([a], desc: a.id) - |> limit(1) - - case Repo.one_regional(query, {:user, system_identity}) do - nil -> 0 - id -> id - end - end - - def create_alter(system_identity, attrs \\ %{}, force_id \\ nil) do - case Accounts.get_user(system_identity) do - nil -> - {:error, :no_user} - - user -> - alter_id = if force_id == nil, do: user.lifetime_alter_count + 1, else: force_id - - if get_alter_by_id(system_identity, {:id, alter_id}) != {:error, :no_alter_id} do - raise "Alter ID conflict" - end - - case Accounts.update_user(user, %{lifetime_alter_count: alter_id}) do - {:ok, _user} -> - {:ok, alter} = - change_alter(%Alter{user_id: user.id, id: alter_id}, attrs) - |> Repo.insert_regional({:user, {:system, user.id}}) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{user.id}", "alter_created", %{ - alter: alter |> OctoconWeb.System.AlterJSON.data_me() - }) - end) - - OctoconDiscord.Autocomplete.Alter.invalidate(system_identity) - - {:ok, alter_id, get_alter_by_id!({:system, user.id}, {:id, alter_id})} - - {:error, _changeset} -> - {:error, :database} - end - end - rescue - _ -> - if force_id == nil do - expected_alter_id = get_highest_alter_id(system_identity) + 1 - user = Accounts.get_user(system_identity) - - if user != nil and user.lifetime_alter_count + 1 != expected_alter_id + 1 do - # Retry with the correct alter ID - create_alter(system_identity, attrs, expected_alter_id) - else - {:error, :database} - end - else - {:error, :database} - end - end - - @doc """ - Creates a new alter given a system identity and a map of `attrs`. - - Raises an error if a user is not found with the given `system_identity`. - """ - def create_alter!(system_identity, attrs \\ %{}) do - case create_alter(system_identity, attrs) do - {:ok, _, alter} -> - alter - - {:error, :no_user} -> - raise RuntimeError, "User not found with system_identity #{inspect(system_identity)}" - - {:error, :database} -> - raise RuntimeError, "Database error" - end - end - - @doc """ - Deletes an alter given a system identity and an alter identity. - """ - def delete_alter(system_identity, alter_identity) do - alter_id = resolve_alter(system_identity, alter_identity) - system_id = Accounts.id_from_system_identity(system_identity, :system) - - if alter_id != false do - where = - unwrap_system_identity_where(system_identity, unwrap_alter_identity_where(alter_identity)) - - query = - Alter - |> where(^where) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - spawn(fn -> - system_identity = {:system, system_id} - Octocon.Journals.delete_alter_journal_entries(system_identity, alter_id) - Octocon.Tags.delete_alter_tags(system_identity, alter_id) - Octocon.Fronts.delete_alter_fronts(system_identity, alter_id) - end) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "alter_deleted", %{ - alter_id: alter_id - }) - - OctoconDiscord.Autocomplete.Alter.invalidate(system_identity) - end) - - spawn(fn -> - Octocon.Utils.nuke_existing_avatars!(system_id, alter_id) - end) - - :ok - - _ -> - {:error, :database} - end - else - case alter_identity do - {:id, _} -> {:error, :no_alter_id} - {:alias, _} -> {:error, :no_alter_alias} - end - end - end - - @doc """ - Updates an alter given a system identity, an alter identity, and a map of `attrs`. - """ - def update_alter(system_identity, alter_identity, attrs) do - alter_id = resolve_alter(system_identity, alter_identity) - system_id = Accounts.id_from_system_identity(system_identity, :system) - - if alter_id != false do - base_struct = %Alter{user_id: system_id, id: alter_id} - - fields = - if attrs[:fields] == nil do - nil - else - attrs[:fields] - |> Enum.map(fn field -> - %AlterField{ - id: field["id"], - value: field["value"] - } - end) - end - - attrs = - if fields == nil do - attrs - else - Map.put(attrs, :fields, fields) - end - - changeset = change_alter(base_struct, attrs) - - if changeset.valid? do - alter_id = resolve_alter_id_dumb(system_identity, alter_identity) - - query = - Alter - |> where([a], a.id == ^alter_id and a.user_id == ^system_id) - |> update(set: ^Keyword.new(attrs)) - - case Repo.update_all_regional(query, [], {:user, system_identity}) do - {0, _} -> - {:error, :no_alter} - - {1, _} -> - spawn(fn -> - alter = get_alter_by_id!(system_identity, {:id, alter_id}) - - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "alter_updated", %{ - alter: - alter - |> OctoconWeb.System.AlterJSON.data_me() - }) - - if Map.has_key?(attrs, :name) or Map.has_key?(attrs, :alias) do - OctoconDiscord.Autocomplete.Alter.invalidate(system_identity) - end - end) - - :ok - - _ -> - {:error, :database} - end - else - {:error, :changeset} - end - else - case alter_identity do - {:id, _} -> {:error, :no_alter_id} - {:alias, _} -> {:error, :no_alter_alias} - end - end - end - - @doc """ - Updates an alter given a system identity, an alter identity, and a map of `attrs`. - """ - def update_alter!(system_identity, alter_identity, attrs) do - case update_alter(system_identity, alter_identity, attrs) do - :ok -> - :ok - - {:error, :no_alter_id} -> - raise "Alter not found with ID" - - {:error, :no_alter_alias} -> - raise "Alter not found with alias" - - {:error, :database} -> - raise "Database error" - - {:error, :changeset} -> - raise "Invalid changeset" - end - end - - @doc """ - Builds a changeset based on the given `Octocon.Alters.Alter` struct and `attrs` to change. - """ - def change_alter(%Alter{} = alter, attrs \\ %{}) do - Alter.changeset(alter, attrs) - end -end diff --git a/lib/octocon/alters/alter.ex b/lib/octocon/alters/alter.ex deleted file mode 100644 index d52fda4b..00000000 --- a/lib/octocon/alters/alter.ex +++ /dev/null @@ -1,119 +0,0 @@ -defmodule Octocon.Alters.Alter do - @moduledoc """ - An alter represents a single alter within a system (user). It consists of: - - - An ID (integer up to 32,767) - - An associated user ID (7-character alphanumeric lowercase string) - - A name (optional, up to 80 characters) - - Pronouns (optional, up to 50 characters) - - A description (optional, up to 3,000 characters) - - A security level (enum, one of `:public`, `:friends_only`, `:trusted_only`, or `:private`) - - An alias (optional, up to 80 characters; used for Discord) - - An avatar URL (optional) - - Extra images (optional, up to 3 URLs, currently unused) - - A color (optional, a hex color code, e.g. `#ff0000`) - - Whether the alter is untracked (optional, a boolean) - - Whether the alter is archived (optional, a boolean) - - Discord proxies (optional, an array of strings in the format `prefixtextsuffix`) - - Custom fields (optional, a map of `Octocon.Accounts.Field` IDs to values) - """ - - use Ecto.Schema - - import Ecto.Changeset - import Exandra, only: [embedded_type: 3] - - @primary_key false - - @dropped_fields [ - :__meta__, - :fronts, - :user, - :global_journals, - :tags - ] - - schema "alters" do - field :user_id, :string, primary_key: true - field :id, :id, primary_key: true - - field :name, :string - field :pronouns, :string - field :description, :string - - field :alias, :string - - field :security_level, Ecto.Enum, - values: [public: 0, friends_only: 1, trusted_only: 2, private: 3], - default: :private - - field :avatar_url, :string - field :extra_images, {:array, :string}, default: [] - field :color, :string - - field :untracked, :boolean, default: false - field :archived, :boolean, default: false - field :pinned, :boolean, default: false - field :last_fronted, :utc_datetime - - # Discord - field :discord_proxies, {:array, :string} - field :proxy_name, :string - - embedded_type(:fields, Octocon.Alters.Field, cardinality: :many) - - timestamps() - end - - defp global_validations(changeset) do - changeset - |> validate_format(:user_id, ~r/^[a-z]{7}$/) - |> validate_format(:color, ~r/^#[0-9a-fA-F]{6}$/) - |> validate_required(:id) - |> validate_required(:user_id) - |> validate_length(:name, max: 80) - |> validate_length(:proxy_name, max: 80) - |> validate_length(:pronouns, max: 50) - |> validate_length(:description, max: 3000) - |> validate_length(:extra_images, max: 3) - # |> validate_length(:alias, max: 80) - |> validate_format(:alias, ~r/^(?![\s\d])[^\n]{1,80}$/) - |> foreign_key_constraint(:user_id) - - # |> unique_constraint([:user_id, :alias], name: :alters_user_id_alias_index) - - # |> unique_constraint([:user_id, :id]) - end - - @doc """ - Returns the list of fields that are not to be included during serialization (mostly Ecto metadata). - """ - def dropped_fields, do: @dropped_fields - - @doc """ - Builds a changeset based on the given `Octocon.Alters.Alter` struct and `attrs` to change. - """ - def changeset(alter, attrs) do - alter - |> cast(attrs, [ - :id, - :user_id, - :name, - :pronouns, - :description, - :security_level, - :alias, - :avatar_url, - :extra_images, - :color, - :untracked, - :archived, - :discord_proxies, - :proxy_name, - :pinned, - :fields, - :last_fronted - ]) - |> global_validations() - end -end diff --git a/lib/octocon/alters/field.ex b/lib/octocon/alters/field.ex deleted file mode 100644 index ffb39a17..00000000 --- a/lib/octocon/alters/field.ex +++ /dev/null @@ -1,22 +0,0 @@ -defmodule Octocon.Alters.Field do - @moduledoc """ - A field value associating an `Octocon.Accounts.Field` with a value for the given alter. - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key {:id, Ecto.UUID, autogenerate: false} - - embedded_schema do - field :value, :string - end - - @doc """ - Builds a changeset based on the given `Octocon.Alters.Field` struct and `attrs` to change. - """ - def changeset(%__MODULE__{} = field, attrs \\ %{}) do - field - |> cast(attrs, [:value]) - |> validate_required([:value]) - end -end diff --git a/lib/octocon/apple.ex b/lib/octocon/apple.ex deleted file mode 100644 index 4ebfae5a..00000000 --- a/lib/octocon/apple.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule Octocon.Apple do - @expiration_sec 86_400 * 180 - - defp generate_secret do - secret = - UeberauthApple.generate_client_secret(%{ - client_id: Application.fetch_env!(:octocon, :apple_client_id), - expires_in: @expiration_sec, - key_id: Application.fetch_env!(:octocon, :apple_private_key_id), - team_id: Application.fetch_env!(:octocon, :apple_team_id), - private_key: Application.fetch_env!(:octocon, :apple_private_key) - }) - - :persistent_term.put(__MODULE__, {secret, System.os_time(:second) + @expiration_sec}) - end - - def get_client_secret(_config \\ []) do - case :persistent_term.get(__MODULE__, nil) do - nil -> - generate_secret() - - {secret, exp_time} -> - if exp_time < System.os_time(:second) do - generate_secret() - else - secret - end - end - end -end diff --git a/lib/octocon/application.ex b/lib/octocon/application.ex deleted file mode 100644 index 9b77e4c0..00000000 --- a/lib/octocon/application.ex +++ /dev/null @@ -1,146 +0,0 @@ -defmodule Octocon.Application do - @moduledoc """ - The OTP application for Octocon. - - This module is responsible for starting the Octocon application and its supervision tree. - - The application has different behaviors depending on the environment it is running in. Clustered nodes - are split into one of three types: - - - `auxiliary` nodes, which are responsible for running an API endpoint (including distributed Phoenix channels) - and connect to a read replica of the database. - - `primary` nodes, which inherit all responsibilites of `auxiliary` nodes, but also run additional services - such as Oban background jobs and Discord bot shards. - - `sidecar` nodes, which act serve as dedicated, isolated environments for `primary` nodes to run CPU-intensive - tasks such as image processing and encryption. - - Auxiliary nodes can be run anywhere in the world, while primary (and sidecar) nodes are only run in a single location - in North America to have the lowest latency to Discord's servers (currently, Fly.io's `iad` region in Virginia). - """ - - use Application - - require Logger - - @impl Application - def start(_type, _args) do - Logger.add_handlers(:octocon) - group = Octocon.RPC.NodeTracker.current_group() - Logger.warning("Starting node of type: #{group}") - - children = - global_children() ++ - group_children(group) ++ - [ - # Web endpoint - OctoconWeb.Endpoint, - {Bandit, plug: OctoconWeb.MetricsPlug, port: 9001} - ] - - opts = [strategy: :one_for_one, name: Octocon.Supervisor] - Supervisor.start_link(children, opts) - end - - # Tell Phoenix to update the endpoint configuration - # whenever the application is updated. - @impl Application - def config_change(changed, _new, removed) do - OctoconWeb.Endpoint.config_change(changed, removed) - :ok - end - - defp global_children do - [ - # Telemetry - OctoconWeb.Telemetry, - Octocon.PromEx, - - # Distribution - generate_libcluster_child(), - Octocon.RPC.NodeTracker, - Octocon.UserRegistryCache, - Octocon.Repo, - - # PubSub system - {Phoenix.PubSub, name: Octocon.PubSub}, - - # Finch (HTTP client) - {Finch, - name: Octocon.Finch, - pools: %{ - :default => [size: 10], - "https://cdn.discordapp.com" => [size: 32, count: 4] - }} - ] - |> List.flatten() - end - - defp group_children(:primary) do - if Application.get_env(:octocon, :env) == :prod do - goth_source = - Application.get_env(:octocon, :fcm_service_account_json) - |> Jason.decode!() - - [ - {Goth, name: Octocon.Goth, source: {:service_account, goth_source}}, - Octocon.FCM - ] - else - [] - end ++ - [ - # TODO - Octocon.MessageRepo, - {Task, fn -> :mnesia.start() end}, - Octocon.Primary.Supervisor, - Octocon.Global.Supervisor, - OctoconDiscord.Supervisor - ] - end - - defp group_children(_), do: [] - - defp generate_libcluster_child do - node_list = Application.get_env(:octocon, :node_list, nil) - use_tailscale = Application.get_env(:octocon, :use_tailscale, false) - - topologies = - cond do - use_tailscale -> - [ - tailscale: [ - strategy: Cluster.Strategy.Tailscale, - config: [ - tag: "beam", - appname: "octo" - ] - ] - ] - - node_list != nil && node_list != [] -> - [ - static: [ - strategy: Cluster.Strategy.Epmd, - config: [hosts: node_list] - ] - ] - - true -> - nil - end - - if topologies != nil do - [ - {Cluster.Supervisor, [topologies, [name: Octocon.ClusterSupervisor]]} - ] - else - Logger.warning("No clustering strategy configured, running as a single node!") - - Logger.warning( - "Sidecar requests will also be executed locally. This may severely impact performance/latency." - ) - - [] - end - end -end diff --git a/lib/octocon/auth/guardian.ex b/lib/octocon/auth/guardian.ex deleted file mode 100644 index fc8f253f..00000000 --- a/lib/octocon/auth/guardian.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule Octocon.Auth.Guardian do - @moduledoc """ - Guardian configuration for Octocon. - - This module is used by `Guardian.Plug` plugs to authenticate and authorize - requests, and provides helper utilities for generating and validating JWTs. - """ - - use Guardian, otp_app: :octocon - - def subject_for_token(id, claims) - - @spec subject_for_token(any, any) :: {:error, :reason_for_error} | {:ok, binary} - def subject_for_token(id, _claims) when is_binary(id) do - sub = to_string(id) - {:ok, sub} - end - - def subject_for_token(_, _) do - {:error, :no_id_in_claims} - end - - def resource_from_claims(%{"sub" => id}) do - {:ok, id} - end - - def resource_from_claims(_claims) do - {:error, :no_id_in_claims} - end -end diff --git a/lib/octocon/cachex_child.ex b/lib/octocon/cachex_child.ex deleted file mode 100644 index 1d319eb2..00000000 --- a/lib/octocon/cachex_child.ex +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Octocon.CachexChild do - defmacro __using__(opts) do - quote do - @cachex_opts unquote(opts) - - def child_spec(extra_opts \\ []) do - Supervisor.child_spec( - {Cachex, Keyword.merge(@cachex_opts, extra_opts)}, - id: __MODULE__ - ) - end - end - end -end diff --git a/lib/octocon/channel_blacklists.ex b/lib/octocon/channel_blacklists.ex deleted file mode 100644 index 21e01c80..00000000 --- a/lib/octocon/channel_blacklists.ex +++ /dev/null @@ -1,74 +0,0 @@ -defmodule Octocon.ChannelBlacklists do - @moduledoc """ - The ChannelBlacklists context. - """ - - @region_specifier {:region, :nam} - - import Ecto.Query, warn: false - alias Octocon.Repo - - alias Octocon.ChannelBlacklists.ChannelBlacklist - - @doc """ - Returns a list of **all** channel blacklist entries. This is a dangerous, long-running operation and should be used - with caution. - """ - def list_channel_blacklists do - Repo.all_regional(ChannelBlacklist, @region_specifier) - end - - @doc false - def list_channel_blacklists_bare do - Repo.all_regional(from(c in ChannelBlacklist, select: c.channel_id), @region_specifier) - end - - @doc """ - Gets all channel blacklists for a given guild. - """ - def list_channel_blacklists_by_guild(guild_id) do - Repo.all_regional( - from(c in ChannelBlacklist, where: c.guild_id == ^to_string(guild_id)), - @region_specifier - ) - end - - @doc """ - Given a Discord channel ID, returns whether or not that channel is blacklisted. - - This should always be backed by a `OctoconDiscord.Cache.ChannelBlacklists` cache, as it is a latency-sensitive operation. - """ - def channel_blacklisted?(channel_id) do - from(c in ChannelBlacklist, where: c.channel_id == ^to_string(channel_id)) - |> Repo.all_regional(@region_specifier) - |> case do - [] -> false - _ -> true - end - end - - @doc """ - Creates a channel blacklist given the desired `attrs`. - """ - def create_channel_blacklist(attrs \\ %{}) do - %ChannelBlacklist{} - |> change_channel_blacklist(attrs) - |> Repo.insert_regional(@region_specifier) - end - - @doc """ - Deletes a channel blacklist given the entire struct. - """ - def delete_channel_blacklist(%ChannelBlacklist{} = channel_blacklist) do - Repo.delete_regional(channel_blacklist, @region_specifier) - rescue - _ in Ecto.StaleEntryError -> :ok - end - - @doc """ - Builds a changeset based on the given `Octocon.ChannelBlacklists.ChannelBlacklist` struct and `attrs` to change. - """ - def change_channel_blacklist(%ChannelBlacklist{} = channel_blacklist, attrs \\ %{}) do - ChannelBlacklist.changeset(channel_blacklist, attrs) - end -end diff --git a/lib/octocon/channel_blacklists/channel_blacklist.ex b/lib/octocon/channel_blacklists/channel_blacklist.ex deleted file mode 100644 index 7f1b98b4..00000000 --- a/lib/octocon/channel_blacklists/channel_blacklist.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule Octocon.ChannelBlacklists.ChannelBlacklist do - @moduledoc """ - A channel blacklist entry. This is used to prevent the Octocon bot from proxying messages - in a given channel. - - Storing the guild ID here is not strictly necessary, but is useful for database indexing by guild. - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "channel_blacklists" do - field :channel_id, :string, primary_key: true - field :guild_id, :string - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.ChannelBlacklists.ChannelBlacklist` struct and `attrs` to change. - """ - def changeset(channel_blacklist, attrs) do - channel_blacklist - |> cast(attrs, [:guild_id, :channel_id]) - |> validate_required([:guild_id, :channel_id]) - |> validate_format(:guild_id, ~r/^\d{17,22}$/) - |> validate_format(:channel_id, ~r/^\d{17,22}$/) - end -end diff --git a/lib/octocon/cluster_utils.ex b/lib/octocon/cluster_utils.ex deleted file mode 100644 index a24a337b..00000000 --- a/lib/octocon/cluster_utils.ex +++ /dev/null @@ -1,91 +0,0 @@ -defmodule Octocon.ClusterUtils do - @moduledoc """ - Utility functions for working with the currently running node cluster. - """ - - alias Octocon.RPC.NodeTracker - - @doc """ - Check if the current node is a primary node. - """ - def primary?, do: NodeTracker.primary?() - - def current_db_region do - Application.get_env(:octocon, :current_db_region) - end - - @doc """ - Get a list of all primary nodes in the cluster. - - If `include_self` is `true`, the current node will be included in the list if it is a primary node. Otherwise, the current node will be excluded. - """ - def primary_nodes(include_self \\ false) - - def primary_nodes(false) do - NodeTracker.primary_nodes() - end - - def primary_nodes(true) do - other_nodes = primary_nodes(false) - - if primary?() do - [Node.self() | other_nodes] - else - other_nodes - end - end - - @doc """ - Run the given function on all primary nodes in the cluster. If the current node is a primary node, the function will be run locally as well - without any RPC overhead. - """ - def run_on_all_primary_nodes(fun) do - # If we are a primary node, run the function locally as well - if NodeTracker.primary?() do - fun.() - end - - primary_nodes() - |> Task.async_stream(fn node -> - NodeTracker.rpc(node, fun) - end) - |> Enum.map(fn - {:ok, result} -> {:ok, result} - {:error, reason} -> {:error, reason} - end) - end - - def run_on_sidecar(fun, opts \\ []) do - if NodeTracker.sidecar_exists?() do - NodeTracker.rpc_group(:sidecar, fun, opts) - else - # if :force_sidecar in opts do - # raise "No sidecar node available" - # end - - # fun.() - {:error, :no_sidecar} - end - end - - def run_on_primary(fun, opts \\ []) do - NodeTracker.rpc_group(:primary, fun, opts) - end - - def run_on_primary(m, f, a) do - NodeTracker.rpc_group(:primary, {m, f, a}) - end - - @doc """ - Get the number of desired functional (non-standby) primary nodes in the cluster. - """ - def primary_node_count do - Application.get_env(:octocon, :primary_node_count, 1) - end - - def check_primary(node) do - Octocon.RPC.NodeTracker.rpc(node, fn -> - NodeTracker.primary?() - end) - end -end diff --git a/lib/octocon/fcm.ex b/lib/octocon/fcm.ex deleted file mode 100644 index fdd08721..00000000 --- a/lib/octocon/fcm.ex +++ /dev/null @@ -1,175 +0,0 @@ -defmodule Octocon.FCM do - @moduledoc """ - Utility module for sending push notifications to users using `Pigeon.Dispatcher`. - """ - use Pigeon.Dispatcher, otp_app: :octocon - - require Logger - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters, - Alters.Alter, - Friendships.Friendship, - NotificationTokens, - NotificationTokens.NotificationToken, - Repo - } - - @size_limit 500 - - @doc """ - Pushes a notification to a user's friends, notifying them of the alter front statuses that have been modified. - """ - def push_friends_alters(user_id, alter_ids) do - {title, image, data} = gen_batch_notifications(user_id, alter_ids) - - merger = - if image == nil do - %{} - else - %{"image" => image} - end - - data - |> Enum.flat_map(fn {tokens, {friend_id, message}} -> - Enum.map(tokens, fn token -> - { - Pigeon.FCM.Notification.new( - {:token, token}, - %{ - "title" => title, - "body" => message - } - |> Map.merge(merger) - ), - gen_callback(friend_id) - } - end) - end) - |> Enum.each(fn {notification, callback} -> - push(notification, on_response: callback) - end) - end - - defp gen_callback(friend_id) do - fn %Pigeon.FCM.Notification{response: response, error: error, target: {:token, token}} = - notification -> - cond do - response == :success -> - Logger.debug("Push successful") - - response == :not_started -> - Logger.warning("Push notification was sent, but FCM service wasn't started") - - error != nil -> - %{"details" => [%{"errorCode" => code}]} = error - - case code do - "UNREGISTERED" -> - Logger.debug("Token for #{friend_id} looks stale (UNREGISTERED), removing") - NotificationTokens.invalidate_notification_token({:system, friend_id}, token) - - other -> - Logger.error( - "Push notification error (deep) (#{friend_id}): #{inspect(other)} | #{inspect(notification)}" - ) - end - - true -> - Logger.error( - "Push notification error (#{friend_id}): :#{inspect(response)} | #{inspect(notification)}" - ) - end - end - end - - defp gen_batch_notifications(user_id, alter_ids) do - user = Accounts.get_user!({:system, user_id}) - username = user.username || user.id - - image = - case user.avatar_url do - nil -> nil - "" -> nil - url -> url - end - - title = "Front update: " <> username - - alters_query = - from( - a in Alter, - where: a.user_id == ^user_id and a.id in ^alter_ids, - select: a - ) - - friends_query = - from( - f in Friendship, - where: f.user_id == ^user_id, - select: {f.friend_id, f.level} - ) - - alters = Repo.all_regional(alters_query, {:user, {:system, user.id}}) - friends = Repo.all_global(friends_query) - - friend_ids = Enum.map(friends, &elem(&1, 0)) - - notification_tokens_query = - from( - n in NotificationToken, - where: n.user_id in ^friend_ids, - select: {n.user_id, n.push_token} - ) - - notification_tokens = - Repo.all_global(notification_tokens_query) - |> Enum.group_by(&elem(&1, 0), &elem(&1, 1)) - - data = - friends - |> Stream.map(fn {id, level} -> - { - id, - Map.get(notification_tokens, id, []), - level - } - end) - |> Stream.filter(fn {_id, tokens, _level} -> - tokens != [] - end) - |> Stream.map(fn {id, tokens, level} -> - visible_alters = - alters - |> Stream.filter(fn alter -> - Alters.can_view_entity?(level, alter.security_level) - end) - |> Enum.map_join(", ", & &1.name) - |> then(fn alters -> - case alters do - "" -> - "No one is fronting" - - alters -> - case String.length(alters) do - length when length > @size_limit -> - alters - |> String.slice(0..(@size_limit - 3)) - |> Kernel.<>("\n...") - - _ -> - alters - end - end - end) - - {tokens, {id, visible_alters}} - end) - |> Enum.into(%{}) - - {title, image, data} - end -end diff --git a/lib/octocon/friendships.ex b/lib/octocon/friendships.ex deleted file mode 100644 index b0736455..00000000 --- a/lib/octocon/friendships.ex +++ /dev/null @@ -1,845 +0,0 @@ -defmodule Octocon.Friendships do - @moduledoc """ - The Friendships context. - - This module represents the data layer for working with friendships between users. - - Most operations require system identities. See `Octocon.Accounts` for more information on system identities. - """ - - @query_concurrency 10 - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters.Alter, - Friendships.Friendship, - Friendships.Request, - Fronts, - Fronts.CurrentFront, - Repo - } - - @doc """ - Returns the friendship status between two users with the given identities, or `nil` if no friendship exists. - """ - def get_friendship(user_identity, friend_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - friendship = - from( - f in Friendship, - where: f.user_id == ^user_id and f.friend_id == ^friend_id, - select: f - ) - |> Repo.one_global() - - friend = - from( - u in Octocon.Accounts.User, - where: u.id == ^friend_id, - select: struct(u, [:username, :description, :discord_id, :id, :avatar_url]) - ) - |> Repo.one_regional({:user, {:system, friend_id}}) - - if friendship == nil do - nil - else - %{ - friendship: friendship, - friend: friend - } - end - end - - @doc """ - Returns the friendship status between two users with the given identities, or `nil` if no friendship exists. - - This function also includes a list of all fronting alters for the friend, guarded by the current friendship level. - """ - def get_friendship_guarded(user_identity, friend_identity) do - case get_friendship(user_identity, friend_identity) do - nil -> - nil - - friendship -> - Map.put( - friendship, - :fronting, - Fronts.currently_fronting_guarded( - friend_identity, - user_identity - ) - ) - end - end - - @doc """ - Returns a list of all friendships for the given user identity. - """ - def list_friendships(user_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - - friendships = - from( - f in Friendship, - where: f.user_id == ^user_id, - select: f - ) - |> Repo.all_global() - - friend_data = - friendships - |> Task.async_stream( - fn %{friend_id: id} -> - query = - from( - u in Octocon.Accounts.User, - where: u.id == ^id, - select: struct(u, [:avatar_url, :username, :discord_id, :id]) - ) - - Repo.one_regional(query, {:user, {:system, id}}) - end, - max_concurrency: @query_concurrency - ) - |> Enum.filter(fn {:ok, user} -> user != nil end) - |> Enum.into(%{}, fn {:ok, user} -> {user.id, user} end) - - friendships - |> Enum.map(fn friendship -> - %{ - friendship: friendship, - friend: Map.get(friend_data, friendship.friend_id) - } - end) - |> Enum.filter(fn f -> f.friend != nil end) - |> Enum.sort_by(& &1.friendship.since, {:desc, DateTime}) - end - - @doc """ - Returns a list of all friendships for the given user identity, as well as a list of all fronting alters for each friend. - """ - def list_friendships_guarded(user_identity) do - friendships = list_friendships(user_identity) - friendship_ids = Enum.map(friendships, fn f -> f.friend.id end) - - fronts = - friendship_ids - |> Task.async_stream( - fn id -> - query = - from( - f in CurrentFront, - where: f.user_id == ^id, - select: struct(f, [:id, :user_id, :alter_id, :comment, :time_start]) - ) - - Repo.all_regional(query, {:user, {:system, id}}) - end, - max_concurrency: @query_concurrency - ) - |> Enum.map(fn {:ok, fronts} -> fronts end) - |> List.flatten() - |> Enum.map(&CurrentFront.to_front/1) - - alters = - friendship_ids - |> Task.async_stream( - fn id -> - alter_ids = Enum.filter(fronts, fn f -> f.user_id == id end) |> Enum.map(& &1.alter_id) - - query = - from( - a in Alter, - where: a.user_id == ^id and a.id in ^alter_ids, - select: - struct(a, [ - :id, - :user_id, - :name, - :avatar_url, - :pronouns, - :color, - :security_level, - :description - ]) - ) - - Repo.all_regional(query, {:user, {:system, id}}) - end, - max_concurrency: @query_concurrency - ) - |> Enum.map(fn {:ok, alters} -> alters end) - |> Enum.into([]) - |> List.flatten() - - fronts = - fronts - |> Enum.map(fn front -> - %{ - front: front, - alter: - alters - |> Enum.find(fn alter -> - alter.id == front.alter_id and alter.user_id == front.user_id - end) - } - end) - - friendships - |> Task.async_stream( - fn friendship -> - fronting = - fronts - |> Enum.filter(fn front -> - front.front.user_id == friendship.friend.id - end) - |> then(fn fronts -> - friendship_level = - get_friendship({:system, friendship.friend.id}, user_identity).friendship.level - - Fronts.currently_fronting_hoisted( - {:system, friendship.friend.id}, - friendship_level, - fronts - ) - end) - - Map.put( - friendship, - :fronting, - fronting - ) - end, - max_concurrency: @query_concurrency - ) - |> Enum.map(fn {:ok, friendships} -> friendships end) - |> Enum.into([]) - end - - @doc """ - Returns whether or not a friendship exists between two users with the given identities. - """ - def friendship_exists?(left_identity, right_identity) do - left_id = Accounts.id_from_system_identity(left_identity, :system) - right_id = Accounts.id_from_system_identity(right_identity, :system) - - from( - f in Friendship, - where: f.user_id == ^left_id and f.friend_id == ^right_id - ) - |> Repo.one_global() - |> case do - nil -> false - _ -> true - end - end - - @doc """ - Creates a new friendship entry between two users with the given identities. - """ - def create_friendship(attrs \\ %{}) do - %Friendship{} - |> Friendship.changeset(attrs) - |> Repo.insert_global() - end - - @doc """ - Returns the friendship level between two users with the given identities. - """ - def get_friendship_level(left_identity, right_identity) - when is_nil(left_identity) == nil or is_nil(right_identity), - do: :none - - def get_friendship_level(left_identity, right_identity) do - left_id = Accounts.id_from_system_identity(left_identity, :system) - right_id = Accounts.id_from_system_identity(right_identity, :system) - - level = - from( - f in Friendship, - where: f.user_id == ^left_id and f.friend_id == ^right_id, - select: f.level - ) - |> Repo.one_global() - - case level do - nil -> :none - level -> level - end - end - - @doc """ - Updates the `Octocon.Friendships.Friendship` struct with the given attributes. - """ - def update_friendship(%Friendship{} = friendship, attrs) do - friendship - |> Friendship.changeset(attrs) - |> Repo.update_global() - end - - def delete_friendship(%Friendship{} = friendship) do - Repo.delete_global(friendship) - Repo.delete_global(%Friendship{user_id: friendship.friend_id, friend_id: friendship.user_id}) - end - - @doc """ - Builds a changeset based on the given `Octocon.Friendships.Friendship` struct and `attrs` to change. - """ - def change_friendship(%Friendship{} = friendship, attrs \\ %{}) do - Friendship.changeset(friendship, attrs) - end - - @doc """ - Returns the friend request between two users with the given identities, or `nil` if no request exists. - """ - def get_friend_request(from_identity, to_identity) do - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - from( - r in Request, - where: r.from_id == ^from_id and r.to_id == ^to_id - ) - |> Repo.one_global() - end - - @doc """ - Returns whether or not a friend request has been sent from one user to another. - """ - def friend_request_exists?(from_identity, to_identity) do - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - from( - r in Request, - where: r.from_id == ^from_id and r.to_id == ^to_id - ) - |> Repo.all_global() - |> case do - [] -> false - _ -> true - end - end - - @doc """ - Links two users as friends, creating bidirectional friendship entries between them. - """ - def link_friends(from_identity, to_identity) do - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - %Friendship{ - user_id: from_id, - friend_id: to_id, - since: DateTime.utc_now(:second) - } - |> change_friendship() - |> Repo.insert_global() - - %Friendship{ - user_id: to_id, - friend_id: from_id, - since: DateTime.utc_now(:second) - } - |> change_friendship() - |> Repo.insert_global() - - delete_friend_requests(from_identity, to_identity) - - :ok - end - - @doc """ - Trusts a friend, upgrading their friendship level to `:trusted_friend`. - """ - def trust_friend(user_identity, friend_identity) do - case get_friendship(user_identity, friend_identity) do - nil -> - {:error, :not_friends} - - %{friendship: friendship} -> - case update_friendship(friendship, %{level: :trusted_friend}) do - {:ok, _} -> - spawn(fn -> - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - OctoconWeb.Endpoint.broadcast!("system:#{user_id}", "friend_trusted", %{ - friend_id: friend_id - }) - end) - - :ok - - {:error, _} -> - {:error, :database} - end - end - end - - @doc """ - Untrusts a friend, downgrading their friendship level to `:friend`. - """ - def untrust_friend(user_identity, friend_identity) do - case get_friendship(user_identity, friend_identity) do - nil -> - {:error, :not_friends} - - %{friendship: friendship} -> - case update_friendship(friendship, %{level: :friend}) do - {:ok, _} -> - spawn(fn -> - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - OctoconWeb.Endpoint.broadcast!("system:#{user_id}", "friend_untrusted", %{ - friend_id: friend_id - }) - end) - - :ok - - {:error, _} -> - {:error, :database} - end - end - end - - @doc """ - Removes a friendship between two users with the given identities. - """ - def remove_friendship(user_identity, friend_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - if friendship_exists?(user_identity, friend_identity) do - from( - f in Friendship, - where: f.user_id == ^user_id and f.friend_id == ^friend_id - ) - |> Repo.delete_all_global() - - from( - f in Friendship, - where: f.user_id == ^friend_id and f.friend_id == ^user_id - ) - |> Repo.delete_all_global() - - [ - {friend_id, user_id}, - {user_id, friend_id} - ] - |> Enum.each(fn {to, removed} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{to}", "friend_removed", %{ - friend_id: removed - }) - - OctoconDiscord.Autocomplete.Friend.invalidate({:system, to}) - end) - end) - - :ok - else - {:error, :not_friends} - end - end - - @doc """ - Accepts a friend request from one user to another, linking them as friends. - """ - def accept_request(from_identity, to_identity) do - cond do - friendship_exists?(from_identity, to_identity) -> - {:error, :already_friends} - - friend_request_exists?(from_identity, to_identity) -> - case link_friends(from_identity, to_identity) do - :ok -> - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - OctoconDiscord.Utils.send_dm( - {:system, from_id}, - ":white_check_mark: Friend request accepted", - "The system **#{to_id}** has accepted your friend request." - ) - - [ - {from_id, to_id}, - {to_id, from_id} - ] - |> Enum.each(fn {to, accepted} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{to}", - "friend_added", - get_friendship_guarded({:system, to}, {:system, accepted}) - |> OctoconWeb.FriendJSON.data() - ) - - OctoconWeb.Endpoint.broadcast!("system:#{to}", "friend_request_removed", %{ - system_id: accepted - }) - end) - end) - - spawn(fn -> - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, from_id}, - :outgoing - ) - - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, to_id}, - :incoming - ) - - OctoconDiscord.Autocomplete.Friend.invalidate({:system, from_id}) - OctoconDiscord.Autocomplete.Friend.invalidate({:system, to_id}) - end) - - :ok - - _ -> - {:error, :database} - end - - Accounts.get_user({:system, Accounts.id_from_system_identity(to_identity, :system)}) == nil -> - {:error, :no_user} - - true -> - {:error, :not_requested} - end - end - - @doc """ - Rejects a friend request from one user to another, removing the request. - """ - def reject_request(from_identity, to_identity, _send_dm \\ false) do - cond do - friendship_exists?(from_identity, to_identity) -> - {:error, :already_friends} - - friend_request_exists?(from_identity, to_identity) -> - delete_friend_requests(from_identity, to_identity) - - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - [ - {from_id, to_id}, - {to_id, from_id} - ] - |> Enum.each(fn {to, removed} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{to}", "friend_request_removed", %{ - system_id: removed - }) - end) - end) - - spawn(fn -> - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, to_id}, - :incoming - ) - - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, from_id}, - :outgoing - ) - end) - - :ok - - Accounts.get_user({:system, Accounts.id_from_system_identity(to_identity, :system)}) == nil -> - {:error, :no_user} - - true -> - {:error, :not_requested} - end - end - - @doc """ - Cancels a friend request from one user to another, removing the request. - """ - def cancel_request(from_identity, to_identity), - do: reject_request(from_identity, to_identity, true) - - @doc """ - Sends a friend request from one user to another, creating a new request entry. - """ - def send_request(from_identity, to_identity) do - cond do - friendship_exists?(from_identity, to_identity) -> - {:error, :already_friends} - - friend_request_exists?(from_identity, to_identity) -> - {:error, :already_sent_request} - - friend_request_exists?(to_identity, from_identity) -> - case link_friends(from_identity, to_identity) do - :ok -> {:ok, :accepted} - _ -> {:error, :database} - end - - true -> - from_id = Accounts.id_from_system_identity(from_identity, :system) - to_id = Accounts.id_from_system_identity(to_identity, :system) - - cond do - to_id == nil or not Accounts.user_exists?({:system, to_id}) -> - {:error, :no_user} - - to_id == from_id -> - {:error, :no_user} - - true -> - req = - %Request{ - from_id: from_id, - to_id: to_id, - date_sent: DateTime.utc_now(:second) - } - |> change_friend_request() - |> Repo.insert_global() - - case req do - {:ok, _} -> - OctoconDiscord.Utils.send_dm( - {:system, to_id}, - ":mailbox_with_mail: New friend request!", - "The system **#{from_id}** has sent you a friend request." - ) - - spawn(fn -> - %{request: request, from: from} = - get_incoming_friend_request(from_identity, to_identity) - - OctoconWeb.Endpoint.broadcast!( - "system:#{to_id}", - "friend_request_received", - %{ - request: request, - system: from - } - |> OctoconWeb.FriendRequestJSON.data() - ) - end) - - spawn(fn -> - %{request: request, to: to} = - get_outgoing_friend_request(from_identity, to_identity) - - OctoconWeb.Endpoint.broadcast!( - "system:#{from_id}", - "friend_request_sent", - %{ - request: request, - system: to - } - |> OctoconWeb.FriendRequestJSON.data() - ) - end) - - spawn(fn -> - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, to_id}, - :incoming - ) - - OctoconDiscord.Autocomplete.FriendRequest.invalidate( - {:system, from_id}, - :outgoing - ) - end) - - {:ok, :sent} - - {:error, error} -> - {:error, error} - end - end - end - end - - @doc """ - Gets the outgoing friend request from one user to another, if it exists. - """ - def get_outgoing_friend_request(user_identity, friend_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - request = - from(r in Request, - where: r.from_id == ^user_id and r.to_id == ^friend_id - ) - |> Repo.one_global() - - if request == nil do - nil - else - user = - from( - u in Octocon.Accounts.User, - where: u.id == ^request.to_id, - select: struct(u, [:username, :discord_id, :id, :avatar_url]) - ) - |> Repo.one_regional({:user, {:system, request.to_id}}) - - %{ - request: request, - to: user - } - end - end - - @doc """ - Gets the incoming friend request from one user to another, if it exists. - """ - def get_incoming_friend_request(user_identity, friend_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - friend_id = Accounts.id_from_system_identity(friend_identity, :system) - - request = - from(r in Request, - where: r.from_id == ^user_id and r.to_id == ^friend_id - ) - |> Repo.one_global() - - if request == nil do - nil - else - user = - from( - u in Octocon.Accounts.User, - where: u.id == ^request.from_id, - select: struct(u, [:username, :discord_id, :id, :avatar_url]) - ) - |> Repo.one_regional({:user, {:system, request.from_id}}) - - %{ - request: request, - from: user - } - end - end - - @doc """ - Gets all outgoing friend requests associated with the given user identity. - """ - def outgoing_friend_requests(user_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - - requests = - from( - r in Request, - where: r.from_id == ^user_id, - select: r - ) - |> Repo.all_global() - - to_ids = Enum.map(requests, & &1.to_id) - - users = - to_ids - |> Enum.map(fn id -> - query = - from( - u in Octocon.Accounts.User, - where: u.id == ^id, - select: struct(u, [:username, :discord_id, :id, :avatar_url]) - ) - - {query, {:system, id}} - end) - |> Enum.map(fn {query, system_identity} -> - Repo.one_regional(query, {:user, system_identity}) - end) - |> Enum.into(%{}, fn user -> {user.id, user} end) - - requests - |> Enum.map(fn request -> - %{ - request: request, - to: Map.get(users, request.to_id) - } - end) - |> Enum.sort_by(& &1.request.date_sent, {:desc, DateTime}) - end - - @doc """ - Gets all incoming friend requests associated with the given user identity. - """ - def incoming_friend_requests(user_identity) do - user_id = Accounts.id_from_system_identity(user_identity, :system) - - requests = - from( - r in Request, - where: r.to_id == ^user_id, - select: r - ) - |> Repo.all_global() - - from_ids = Enum.map(requests, & &1.from_id) - - users = - from_ids - |> Enum.map(fn id -> - query = - from( - u in Octocon.Accounts.User, - where: u.id == ^id, - select: struct(u, [:username, :discord_id, :id, :avatar_url]) - ) - - {query, {:system, id}} - end) - |> Enum.map(fn {query, system_identity} -> - Repo.one_regional(query, {:user, system_identity}) - end) - |> Enum.into(%{}, fn user -> {user.id, user} end) - - requests - |> Enum.map(fn request -> - %{ - request: request, - from: Map.get(users, request.from_id) - } - end) - |> Enum.sort_by(& &1.request.date_sent, {:desc, DateTime}) - end - - @doc """ - Deletes all friend requests between two users with the given identities. - """ - def delete_friend_requests(left_identity, right_identity) do - left_id = Accounts.id_from_system_identity(left_identity, :system) - right_id = Accounts.id_from_system_identity(right_identity, :system) - - from( - r in Request, - where: r.from_id == ^left_id and r.to_id == ^right_id - ) - |> Repo.delete_all_global() - - from( - r in Request, - where: r.from_id == ^right_id and r.to_id == ^left_id - ) - |> Repo.delete_all_global() - end - - @doc """ - Builds a changeset based on the given `Octocon.Friendships.Request` struct and `attrs` to change. - """ - def change_friend_request(%Request{} = request, attrs \\ %{}) do - Request.changeset(request, attrs) - end -end diff --git a/lib/octocon/friendships/friendship.ex b/lib/octocon/friendships/friendship.ex deleted file mode 100644 index 04517ab5..00000000 --- a/lib/octocon/friendships/friendship.ex +++ /dev/null @@ -1,47 +0,0 @@ -defmodule Octocon.Friendships.Friendship do - @moduledoc """ - A friendship between two users. This is used to track a relationship between users, and is always - bidirectional (i.e. if a `Friendship` exists between user A and user B, there is also a `Friendship` - between user B and user A). It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - A friend ID (7-character alphanumeric lowercase string) - - A level (enum, one of `:friend` or `:trusted_friend`) - - A timestamp of when the friendship was established - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "friendships" do - field :user_id, :string, primary_key: true - field :friend_id, :string, primary_key: true - field :level, Ecto.Enum, values: [friend: 0, trusted_friend: 1], default: :friend - - field :since, :utc_datetime - - # belongs_to :user, Octocon.Accounts.User, - # foreign_key: :user_id, - # references: :id, - # define_field: false - - # belongs_to :friend, Octocon.Accounts.User, - # foreign_key: :friend_id, - # references: :id, - # define_field: false - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Friendships.Friendship` struct and `attrs` to change. - """ - def changeset(friendship, attrs) do - friendship - |> cast(attrs, [:user_id, :friend_id, :level]) - |> validate_required([:user_id, :friend_id, :level]) - |> validate_format(:user_id, ~r/^[a-z]{7}$/) - |> validate_format(:friend_id, ~r/^[a-z]{7}$/) - end -end diff --git a/lib/octocon/friendships/request.ex b/lib/octocon/friendships/request.ex deleted file mode 100644 index f730806f..00000000 --- a/lib/octocon/friendships/request.ex +++ /dev/null @@ -1,45 +0,0 @@ -defmodule Octocon.Friendships.Request do - @moduledoc """ - A friend request between two users. It consists of: - - - A sender (from) ID (7-character alphanumeric lowercase string) - - A recipient (to) ID (7-character alphanumeric lowercase string) - - A timestamp of when the request was sent - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "friend_requests" do - field :from_id, :string, primary_key: true - field :to_id, :string, primary_key: true - - field :date_sent, :utc_datetime - - # belongs_to :from, Octocon.Accounts.User, - # foreign_key: :from_id, - # references: :id, - # define_field: false - - # belongs_to :to, Octocon.Accounts.User, - # foreign_key: :to_id, - # references: :id, - # define_field: false - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Friendships.Request` struct and `attrs` to change. - """ - def changeset(request, attrs) do - request - |> cast(attrs, [:from_id, :to_id]) - |> validate_required([:from_id, :to_id]) - |> validate_format(:from_id, ~r/^[a-z]{7}$/) - |> validate_format(:to_id, ~r/^[a-z]{7}$/) - |> foreign_key_constraint(:from_id) - |> foreign_key_constraint(:to_id) - end -end diff --git a/lib/octocon/fronts.ex b/lib/octocon/fronts.ex deleted file mode 100644 index 1ea245be..00000000 --- a/lib/octocon/fronts.ex +++ /dev/null @@ -1,624 +0,0 @@ -defmodule Octocon.Fronts do - @moduledoc """ - The Fronts context. - """ - - import Ecto.Query, warn: false - - alias OctoconWeb.System.FrontingJSON - - alias Octocon.{ - Accounts, - Repo - } - - alias Octocon.{ - Accounts, - Alters, - Alters.Alter, - Friendships - } - - alias Octocon.Fronts.{ - CurrentFront, - Front, - FrontByAlter, - FrontByEndTime, - FrontByTime - } - - def get_by_id(system_identity, id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - front = - from( - f in Front, - where: f.id == ^id and f.user_id == ^system_id, - select: f - ) - |> Repo.one_regional({:user, system_identity}) - - if front == nil do - nil - else - alter = - from( - a in Alter, - where: a.id == ^front.alter_id and a.user_id == ^system_id, - select: struct(a, [:id, :name, :avatar_url, :pronouns, :color, :security_level]) - ) - |> Repo.one_regional({:user, system_identity}) - - %{ - front: front, - alter: alter, - primary: Accounts.get_primary_front({:system, system_id}) == id - } - end - end - - def delete_front(system_identity, id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - %{front: front} = get_by_id(system_identity, id) - - delete_query = - from( - f in Front, - where: f.user_id == ^system_id and f.id == ^id and f.time_start == ^front.time_start - ) - - result = Repo.delete_all_regional(delete_query, {:user, system_identity}) - - if match?({1, _}, result) do - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "front_deleted", %{ - front_id: id - }) - end) - end - - result - end - - def currently_fronting(system_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - primary_front = Accounts.get_primary_front({:system, system_id}) - - fronts = - from( - f in CurrentFront, - where: f.user_id == ^system_id - ) - |> Repo.all_regional({:user, system_identity}) - |> Enum.sort_by(& &1.time_start, {:desc, DateTime}) - |> Enum.filter(fn front -> front.alter_id != nil end) - |> Enum.map(&CurrentFront.to_front/1) - - alters = - from( - a in Alter, - where: a.user_id == ^system_id and a.id in ^Enum.map(fronts, & &1.alter_id), - select: - struct(a, [:id, :name, :avatar_url, :pronouns, :color, :security_level, :description]) - ) - |> Repo.all_regional({:user, system_identity}) - |> Enum.into(%{}, fn alter -> {alter.id, alter} end) - - fronts - |> Enum.map(fn front -> - alter = Map.get(alters, front.alter_id, %{}) - %{front: front, alter: alter} - end) - |> Enum.filter(fn x -> x.alter != %{} end) - |> sort_fronts(primary_front) - end - - def currently_fronting_ids(system_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - from( - f in CurrentFront, - where: f.user_id == ^system_id, - select: f.alter_id - ) - |> Repo.all_regional({:user, system_identity}) - end - - def currently_fronting_guarded(system_identity, caller_identity) do - friendship_level = Friendships.get_friendship_level(system_identity, caller_identity) - - system_id = Accounts.id_from_system_identity(system_identity, :system) - - primary_front = Accounts.get_primary_front({:system, system_id}) - - currently_fronting({:system, system_id}) - |> Enum.filter(fn %{alter: %{security_level: security_level}} -> - Alters.can_view_entity?(friendship_level, security_level) - end) - |> sort_fronts(primary_front) - end - - def currently_fronting_hoisted(system_identity, friendship_level, currently_fronting) do - primary_front = Accounts.get_primary_front(system_identity) - - currently_fronting - |> Enum.filter(fn %{alter: %{security_level: security_level}} -> - Alters.can_view_entity?(friendship_level, security_level) - end) - |> sort_fronts(primary_front) - end - - defp sort_fronts(fronts, primary_front) do - if primary_front == nil do - fronts - |> Enum.map(fn front -> Map.put(front, :primary, false) end) - else - fronts - |> Enum.map(fn - %{front: front, alter: alter} -> - %{front: front, alter: alter, primary: alter.id == primary_front} - end) - |> Enum.sort(fn - %{primary: true, front: %{time_start: a_time_start}}, - %{primary: true, front: %{time_start: b_time_start}} -> - a_time_start > b_time_start - - %{primary: true, front: %{time_start: _}} = _, - %{primary: false, front: %{time_start: _}} = _ -> - true - - %{primary: false, front: %{time_start: _}} = _, - %{primary: true, front: %{time_start: _}} = _ -> - false - - %{primary: false, front: %{time_start: a_time_start}}, - %{primary: false, front: %{time_start: b_time_start}} -> - a_time_start > b_time_start - end) - end - end - - def fronting?(system_identity, alter_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - if alter_id == false do - false - else - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id == ^alter_id, - select: f - ) - |> Repo.all_regional({:user, system_identity}) - |> case do - [] -> false - _ -> true - end - end - end - - def fronted_between(system_identity, time_start, time_end) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - # Query 1: time_start within range (original table) - q1 = - from f in FrontByTime, - where: - f.user_id == ^system_id and - f.time_start >= ^time_start and f.time_start <= ^time_end, - select: f - - # Query 2: time_end within range (use FrontByEndTime for efficiency) - q2 = - from f in FrontByEndTime, - where: - f.user_id == ^system_id and - f.time_end >= ^time_start and f.time_end <= ^time_end, - select: f - - # Query 3: entries spanning entire range (still use FrontByTime) - q3 = - from f in FrontByTime, - hints: ["ALLOW FILTERING"], - where: - f.user_id == ^system_id and - f.time_start <= ^time_start and f.time_end >= ^time_end, - select: f - - results = - ((Repo.all_regional(q1, {:user, system_identity}) |> Enum.map(&FrontByTime.to_front/1)) ++ - (Repo.all_regional(q2, {:user, system_identity}) |> Enum.map(&FrontByEndTime.to_front/1)) ++ - (Repo.all_regional(q3, {:user, system_identity}) |> Enum.map(&FrontByTime.to_front/1))) - |> Enum.uniq_by(& &1.id) - |> Enum.filter(fn front -> front.time_end != nil && front.alter_id != nil end) - |> Enum.sort_by(& &1.time_start, {:desc, DateTime}) - - results - end - - def fronted_for_month(system_identity, end_anchor \\ DateTime.utc_now()) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - # Compute start of the one-month window - start_date = DateTime.add(end_anchor, -30 * 24 * 60 * 60, :second) - end_date = end_anchor - - # Query 1: time_start within window (original table) - q1 = - from f in FrontByTime, - where: - f.user_id == ^system_id and - f.time_start >= ^start_date and f.time_start <= ^end_date, - select: f - - # Query 2: time_end within window (MV keyed by time_end) - q2 = - from f in FrontByEndTime, - where: - f.user_id == ^system_id and - f.time_end >= ^start_date and f.time_end <= ^end_date, - select: f - - results = - ((Repo.all_regional(q1, {:user, system_identity}) |> Enum.each(&FrontByTime.to_front/1)) ++ - (Repo.all_regional(q2, {:user, system_identity}) - |> Enum.each(&FrontByEndTime.to_front/1))) - |> Enum.uniq_by(& &1.id) - |> Enum.filter(fn front -> front.alter_id != nil end) - |> Enum.sort_by(& &1.time_start, {:desc, DateTime}) - - results - end - - def longest_current_fronter(system_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - currently_fronting = currently_fronting(system_identity) - - if currently_fronting == [] do - nil - else - fronter = - currently_fronting - |> Enum.min_by(fn %{front: front} -> front.time_start end) - - Map.put( - fronter, - :primary, - Accounts.get_primary_front({:system, system_id}) == fronter.alter.id - ) - end - end - - def end_front(system_identity, alter_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - front = - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id == ^alter_id, - select: f - ) - |> Repo.one_regional({:user, system_identity}) - |> CurrentFront.to_front() - - case front do - nil -> - {:error, :not_fronting} - - _ -> - result = - Repo.update_all_regional( - from( - f in Front, - where: - f.user_id == ^system_id and f.id == ^front.id and - f.time_start == ^front.time_start - ), - [set: [time_end: DateTime.utc_now(:second)]], - {:user, system_identity} - ) - - case result do - {1, _} -> - Repo.delete_all_regional( - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id == ^alter_id - ), - {:user, system_identity} - ) - - if Accounts.get_primary_front({:system, system_id}) == alter_id do - Accounts.set_primary_front({:system, system_id}, nil) - end - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "fronting_ended", %{ - alter_id: alter_id - }) - - OctoconDiscord.Autocomplete.Front.invalidate(system_identity) - end) - - spawn(fn -> - Octocon.ClusterUtils.run_on_primary(fn -> - Octocon.Global.FrontNotifier.remove(system_id, alter_id) - end) - end) - - :ok - - {0, _} -> - {:error, :not_set} - - _ -> - {:error, :unknown} - end - end - end - - def start_front(system_identity, alter_identity, comment \\ "") do - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - cond do - alter_id == false -> - {:error, :no_alter} - - fronting?({:system, system_id}, {:id, alter_id}) -> - {:error, :already_fronting} - - true -> - id = Ecto.UUID.generate() - - insertion = - %Front{ - id: id, - user_id: system_id, - alter_id: alter_id, - comment: comment, - time_start: DateTime.utc_now(:second) - } - |> Repo.insert_regional({:user, system_identity}) - - %CurrentFront{ - id: id, - user_id: system_id, - alter_id: alter_id, - comment: comment, - time_start: DateTime.utc_now(:second) - } - |> Repo.insert_regional({:user, system_identity}) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "fronting_started", %{ - front: get_by_id({:system, system_id}, id) |> FrontingJSON.data_me() - }) - end) - - spawn(fn -> - Octocon.ClusterUtils.run_on_primary(fn -> - Octocon.Global.FrontNotifier.add(system_id, alter_id) - end) - - OctoconDiscord.Autocomplete.Front.invalidate(system_identity) - end) - - insertion - end - end - - def update_comment(system_identity, front_id, comment) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - case get_by_id({:system, system_id}, front_id) do - nil -> - {:error, :no_front} - - front -> - case change_front(front.front, %{comment: comment}) do - %Ecto.Changeset{valid?: true} = changeset -> - Repo.update_regional(changeset, {:user, system_identity}) - - if front.front.time_end == nil do - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id == ^front.front.alter_id - ) - |> Repo.update_all_regional( - [set: [comment: comment]], - {:user, system_identity} - ) - end - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "front_updated", %{ - front: get_by_id({:system, system_id}, front_id) |> FrontingJSON.data_me() - }) - end) - - :ok - - %Ecto.Changeset{} = _changeset -> - {:error, :changeset} - end - end - end - - def bulk_update_fronts(system_identity, start_fronts, end_fronts) - when is_list(start_fronts) and is_list(end_fronts) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - now = DateTime.utc_now(:second) - - if start_fronts != [] do - Repo.insert_all_regional( - Front, - Enum.map(start_fronts, fn %{"id" => alter_id} = data -> - %{ - id: Ecto.UUID.generate(), - user_id: system_id, - alter_id: alter_id, - comment: data["comment"] || "", - time_start: now - } - end), - {:user, system_identity} - ) - end - - if end_fronts != [] do - current_front_ids = - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id in ^end_fronts, - select: {f.id, f.time_start} - ) - |> Repo.all() - - current_front_ids - |> Enum.each(fn {id, time_start} -> - from( - f in Front, - where: f.user_id == ^system_id and f.id == ^id and f.time_start == ^time_start - ) - |> Repo.update_all_regional( - [set: [time_end: now]], - {:user, system_identity} - ) - end) - - Repo.delete_all_regional( - from(f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id in ^end_fronts - ), - {:user, system_identity} - ) - - if Accounts.get_primary_front({:system, system_id}) in end_fronts do - Accounts.set_primary_front({:system, system_id}, nil) - end - end - end - - def set_front(system_identity, alter_identity, comment \\ "") do - # Sets an alter to front and ends all existing fronts - - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - cond do - alter_id == false -> - {:error, :no_alter} - - fronting?({:system, system_id}, {:id, alter_id}) -> - {:error, :already_fronting} - - true -> - id = Ecto.UUID.generate() - now = DateTime.utc_now(:second) - - region_specifier = {:user, system_identity} - - current_front_ids = - from( - f in CurrentFront, - where: f.user_id == ^system_id, - select: {f.id, f.time_start} - ) - |> Repo.all_regional(region_specifier) - - from(f in CurrentFront, - where: f.user_id == ^system_id - ) - |> Repo.delete_all_regional(region_specifier) - - current_front_ids - |> Enum.each(fn {id, time_start} -> - from( - f in Front, - where: f.user_id == ^system_id and f.id == ^id and f.time_start == ^time_start - ) - |> Repo.update_all_regional( - [set: [time_end: now]], - region_specifier - ) - end) - - front = - %Front{ - id: id, - user_id: system_id, - alter_id: alter_id, - comment: comment, - time_start: now, - time_end: nil - } - |> Repo.insert_regional(region_specifier) - - %CurrentFront{ - id: id, - user_id: system_id, - alter_id: alter_id, - comment: comment, - time_start: now - } - |> Repo.insert_regional(region_specifier) - - Accounts.set_primary_front({:system, system_id}, nil) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "fronting_set", %{ - front: get_by_id({:system, system_id}, id) |> FrontingJSON.data_me() - }) - - OctoconDiscord.Autocomplete.Front.invalidate(system_identity) - end) - - spawn(fn -> - Octocon.ClusterUtils.run_on_primary(fn -> - Octocon.Global.FrontNotifier.set(system_id, alter_id) - end) - end) - - {:ok, front} - end - end - - def delete_alter_fronts(system_identity, alter_id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - fronts = - from( - f in FrontByAlter, - where: f.user_id == ^system_id and f.alter_id == ^alter_id, - select: {f.id, f.time_start} - ) - |> Repo.all_regional({:user, system_identity}) - - front_ids = Enum.map(fronts, &elem(&1, 0)) - front_time_starts = Enum.map(fronts, &elem(&1, 1)) - - from( - f in CurrentFront, - where: f.user_id == ^system_id and f.alter_id == ^alter_id - ) - |> Repo.delete_all_regional({:user, system_identity}) - - from( - f in Front, - where: f.user_id == ^system_id and f.id in ^front_ids and f.time_start in ^front_time_starts - ) - |> Repo.delete_all_regional({:user, system_identity}) - - :ok - end - - def change_front(%Front{} = front, attrs) do - Front.changeset(front, attrs) - end -end diff --git a/lib/octocon/fronts/front.ex b/lib/octocon/fronts/front.ex deleted file mode 100644 index 9ae3d059..00000000 --- a/lib/octocon/fronts/front.ex +++ /dev/null @@ -1,174 +0,0 @@ -defmodule Octocon.Fronts.Front do - @moduledoc """ - A front entry for an alter. This is used to track a single instance of an alter's front status. It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - An alter ID (integer up to 32,767) - - A comment (optional, up to 50 characters) - - A start time (UTC datetime) - - An end time (UTC datetime, nil if the alter is currently fronting) - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "fronts" do - field :user_id, :string, primary_key: true - field :id, Ecto.UUID, primary_key: true - field :alter_id, :integer - field :comment, :string, default: "" - - field :time_start, :utc_datetime, primary_key: true - field :time_end, :utc_datetime - - # belongs_to :user, Octocon.Accounts.User, - # type: :string, - # foreign_key: :user_id, - # define_field: false - - # belongs_to :alter, Octocon.Alters.Alter, - # type: :integer, - # foreign_key: :alter_id, - # define_field: false - end - - @doc """ - Builds a changeset based on the given `Octocon.Fronts.Front` struct and `attrs` to change. - """ - def changeset(front, attrs) do - front - |> cast(attrs, [:user_id, :alter_id, :comment]) - |> validate_required([:user_id, :alter_id]) - |> validate_length(:comment, max: 50) - end - - @doc """ - Builds a changeset to create a new front entry based on the given `user_id`, `alter_id`, and extra `attrs`. - """ - def create_changeset(user_id, alter_id, attrs) do - %__MODULE__{user_id: user_id, alter_id: alter_id, time_start: DateTime.utc_now(:second)} - |> cast(attrs, [:user_id, :alter_id, :comment, :time_start]) - |> validate_required([:user_id, :alter_id, :time_start]) - |> validate_length(:comment, max: 50) - end -end - -defmodule Octocon.Fronts.CurrentFront do - @moduledoc false - use Ecto.Schema - - @primary_key false - - schema "current_fronts" do - field :id, Ecto.UUID - field :user_id, :string - field :alter_id, :integer - field :comment, :string, default: "" - - field :time_start, :utc_datetime - end - - def to_front(%__MODULE__{} = current_front) do - %Octocon.Fronts.Front{ - id: current_front.id, - user_id: current_front.user_id, - alter_id: current_front.alter_id, - comment: current_front.comment, - time_start: current_front.time_start, - time_end: nil - } - end - - def to_front(nil), do: nil -end - -defmodule Octocon.Fronts.FrontByAlter do - @moduledoc false - use Ecto.Schema - - @primary_key false - - schema "fronts_by_alter" do - field :id, Ecto.UUID - field :user_id, :string - field :alter_id, :integer - field :comment, :string, default: "" - - field :time_start, :utc_datetime - field :time_end, :utc_datetime - end - - def to_front(%__MODULE__{} = front) do - %Octocon.Fronts.Front{ - id: front.id, - user_id: front.user_id, - alter_id: front.alter_id, - comment: front.comment, - time_start: front.time_start, - time_end: front.time_end - } - end - - def to_front(nil), do: nil -end - -defmodule Octocon.Fronts.FrontByTime do - @moduledoc false - use Ecto.Schema - - @primary_key false - - schema "fronts_by_time" do - field :id, Ecto.UUID - field :user_id, :string - field :alter_id, :integer - field :comment, :string, default: "" - - field :time_start, :utc_datetime - field :time_end, :utc_datetime - end - - def to_front(%__MODULE__{} = front) do - %Octocon.Fronts.Front{ - id: front.id, - user_id: front.user_id, - alter_id: front.alter_id, - comment: front.comment, - time_start: front.time_start, - time_end: front.time_end - } - end - - def to_front(nil), do: nil -end - -defmodule Octocon.Fronts.FrontByEndTime do - @moduledoc false - use Ecto.Schema - - @primary_key false - - schema "fronts_by_end_time" do - field :id, Ecto.UUID - field :user_id, :string - field :alter_id, :integer - field :comment, :string, default: "" - - field :time_start, :utc_datetime - field :time_end, :utc_datetime - end - - def to_front(%__MODULE__{} = front) do - %Octocon.Fronts.Front{ - id: front.id, - user_id: front.user_id, - alter_id: front.alter_id, - comment: front.comment, - time_start: front.time_start, - time_end: front.time_end - } - end - - def to_front(nil), do: nil -end diff --git a/lib/octocon/global/front_notifier.ex b/lib/octocon/global/front_notifier.ex deleted file mode 100644 index ac12638b..00000000 --- a/lib/octocon/global/front_notifier.ex +++ /dev/null @@ -1,116 +0,0 @@ -defmodule Octocon.Global.FrontNotifier do - @moduledoc false - alias Octocon.Fronts - - use GenServer - - require Logger - - @via {:via, Horde.Registry, {Octocon.Primary.HordeRegistry, __MODULE__}} - - def start_link([]) do - case GenServer.start_link(__MODULE__, [], name: @via) do - {:ok, pid} -> - {:ok, pid} - - {:error, {:already_started, pid}} -> - Logger.info("Global.FrontNotifier already started at #{inspect(pid)}, returning :ignore") - :ignore - end - end - - def add(system_id, alter_id) do - GenServer.call(@via, {:add, system_id, alter_id}) - end - - def remove(system_id, alter_id) do - GenServer.call(@via, {:remove, system_id, alter_id}) - end - - def set(system_id, alter_id) do - GenServer.call(@via, {:set, system_id, alter_id}) - end - - @impl GenServer - def init([]) do - Process.send_after(self(), :flush, :timer.seconds(5)) - {:ok, %{}} - end - - @impl GenServer - def handle_call({:remove, system_id, alter_id}, _, state) do - ensured = ensure_exists(system_id, state) - - {_, alters} = Map.get(ensured, system_id) - - {:reply, :ok, - %{ - ensured - | system_id => { - get_time(), - Enum.reject(alters, &(&1 == alter_id)) - } - }} - end - - @impl GenServer - def handle_call({:add, system_id, alter_id}, _, state) do - ensured = ensure_exists(system_id, state) - - {_, alters} = Map.get(ensured, system_id) - - {:reply, :ok, - %{ - ensured - | system_id => { - get_time(), - [alter_id | alters] - } - }} - end - - @impl GenServer - def handle_call({:set, system_id, alter_id}, _, state) do - {:reply, :ok, - Map.put(state, system_id, { - get_time(), - [alter_id] - })} - end - - @impl GenServer - def handle_info(:flush, state) do - current_time = get_time() - - {evict, keep} = - state - |> Enum.reduce({[], []}, fn {system_id, {time, alters}}, {evict, keep} -> - # If the alters have been in the state for more than 10 seconds - if current_time - time > 10_000 do - {[{system_id, alters} | evict], keep} - else - {evict, [{system_id, {time, alters}} | keep]} - end - end) - - Enum.each(evict, fn {system_id, alters} -> - Octocon.FCM.push_friends_alters(system_id, alters) - end) - - new_state = Enum.into(keep, %{}) - - Process.send_after(self(), :flush, :timer.seconds(5)) - - {:noreply, new_state} - end - - defp ensure_exists(system_id, state) do - if Map.has_key?(state, system_id) do - state - else - Map.put(state, system_id, {get_time(), Fronts.currently_fronting_ids({:system, system_id})}) - end - end - - defp get_time, do: :os.system_time(:millisecond) -end diff --git a/lib/octocon/global/link_token_registry.ex b/lib/octocon/global/link_token_registry.ex deleted file mode 100644 index 6dceb721..00000000 --- a/lib/octocon/global/link_token_registry.ex +++ /dev/null @@ -1,121 +0,0 @@ -defmodule Octocon.Global.LinkTokenRegistry do - @moduledoc false - - use GenServer - require Logger - - @inverse_table __MODULE__.Inverse - - @via {:via, Horde.Registry, {Octocon.Primary.HordeRegistry, __MODULE__}} - - # def child_spec(opts) do - # name = Keyword.get(opts, :name, __MODULE__) - - # %{ - # id: "#{__MODULE__}_#{name}", - # start: {__MODULE__, :start_link, [name]} - # } - # end - - def start_link([]) do - case GenServer.start_link(__MODULE__, [], name: @via) do - {:ok, pid} -> - {:ok, pid} - - {:error, {:already_started, pid}} -> - Logger.info( - "Global.LinkTokenRegistry already started at #{inspect(pid)}, returning :ignore" - ) - - :ignore - end - end - - def put(system_id) do - GenServer.call(@via, {:put, system_id}) - end - - def get(link_token) do - GenServer.call(@via, {:get, link_token}) - end - - def delete(link_token) do - GenServer.call(@via, {:delete, link_token}) - end - - @impl GenServer - def init([]) do - :ets.new(__MODULE__, [ - :named_table, - :set, - :private - ]) - - :ets.new(@inverse_table, [ - :named_table, - :set, - :private - ]) - - {:ok, %{}} - end - - @impl GenServer - def handle_call({:put, system_id}, _, state) do - link_token = Ecto.UUID.generate() - - case :ets.lookup(@inverse_table, system_id) do - [] -> - # No existing link token for this system_id - :ok - - [{_, existing_link_token}] -> - # Already exists, overwrite - Logger.info("Overwriting existing link token: #{existing_link_token} with: #{link_token}") - :ets.delete(__MODULE__, existing_link_token) - :ets.delete(@inverse_table, system_id) - end - - :ets.insert(__MODULE__, {link_token, system_id}) - :ets.insert(@inverse_table, {system_id, link_token}) - - Process.send_after(self(), {:flush, link_token, system_id}, :timer.minutes(5)) - - {:reply, link_token, state} - end - - @impl GenServer - def handle_call({:get, link_token}, _, state) do - case :ets.lookup(__MODULE__, link_token) do - [] -> - {:reply, nil, state} - - [{_, system_id}] -> - {:reply, system_id, state} - end - end - - @impl GenServer - def handle_call({:delete, link_token}, _, state) do - case :ets.lookup(__MODULE__, link_token) do - [] -> - :ok - - [{_, system_id}] -> - :ets.delete(__MODULE__, link_token) - :ets.delete(@inverse_table, system_id) - end - - {:reply, :ok, state} - end - - @impl GenServer - def handle_info({:flush, link_token, system_id}, state) do - Logger.debug("Flushing link token: #{link_token}") - - :ets.delete(__MODULE__, link_token) - :ets.delete(@inverse_table, system_id) - - {:noreply, state} - end -end diff --git a/lib/octocon/global/supervisor.ex b/lib/octocon/global/supervisor.ex deleted file mode 100644 index c067609c..00000000 --- a/lib/octocon/global/supervisor.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Octocon.Global.Supervisor do - use Supervisor - - require Logger - - def start_link(_), do: Supervisor.start_link(__MODULE__, [], name: __MODULE__) - - def init([]) do - children = [ - Supervisor.child_spec({Task, fn -> init_global_front_notifier() end}, - id: :init_global_front_notifier - ), - Supervisor.child_spec({Task, fn -> init_global_link_token_registry() end}, - id: :init_global_link_token_registry - ) - ] - - Supervisor.init(children, strategy: :one_for_one) - end - - def init_global_front_notifier do - Logger.info("Starting Global.FrontNotifier") - - Horde.DynamicSupervisor.start_child( - Octocon.Primary.HordeSupervisor, - Octocon.Global.FrontNotifier - ) - end - - def init_global_link_token_registry do - Logger.info("Starting Global.LinkTokenRegistry") - - Horde.DynamicSupervisor.start_child( - Octocon.Primary.HordeSupervisor, - Octocon.Global.LinkTokenRegistry - ) - end -end diff --git a/lib/octocon/journals.ex b/lib/octocon/journals.ex deleted file mode 100644 index 8fbd583d..00000000 --- a/lib/octocon/journals.ex +++ /dev/null @@ -1,455 +0,0 @@ -defmodule Octocon.Journals do - @moduledoc false - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters, - Repo - } - - alias Octocon.Journals.{ - AlterJournalEntry, - GlobalJournalAlters, - GlobalJournalEntry - } - - alias OctoconWeb.AlterJournalJSON, as: AlterRenderer - alias OctoconWeb.GlobalJournalJSON, as: GlobalRenderer - - @all_global_fields GlobalJournalEntry.__struct__() - |> Map.drop([:__meta__, :__struct__, :user, :alters]) - |> Map.keys() - - @bare_global_fields @all_global_fields -- [:content] - - def unwrap_system_identity_where(system_identity, extra \\ []) do - case system_identity do - {:system, system_id} -> - [user_id: system_id] |> Keyword.merge(extra) - - {:discord, _} = identity -> - [user_id: Accounts.id_from_system_identity(identity, :system)] - |> Keyword.merge(extra) - end - end - - def get_global_journal_entry(system_identity, entry_id) do - where = unwrap_system_identity_where(system_identity) - - entry = - GlobalJournalEntry - |> where(^where) - |> where(id: ^entry_id) - |> select([j], struct(j, ^@all_global_fields)) - |> Repo.one_regional({:user, system_identity}) - - alters = - GlobalJournalAlters - |> where(^where) - |> where([gja], gja.global_journal_id == ^entry_id) - |> select([gja], gja.alter_id) - |> Repo.all_regional({:user, system_identity}) - - entry - |> case do - nil -> nil - entry -> %{entry | alters: alters} - end - end - - def get_global_journal_entries(system_identity, opts \\ []) do - where = unwrap_system_identity_where(system_identity) - - fields = - Keyword.get(opts, :fields, :all) - |> case do - :bare -> - @bare_global_fields - - :all -> - @all_global_fields - - other when is_list(other) and other != [] -> - other - - _ -> - raise ArgumentError, - "Invalid fields: expected :bare, :all or a non-empty list of field atoms" - end - - entries = - GlobalJournalEntry - |> where(^where) - |> select([j], struct(j, ^fields)) - |> Repo.all_regional({:user, system_identity}) - |> Enum.sort_by(& &1.inserted_at, {:desc, NaiveDateTime}) - - entry_ids = Enum.map(entries, & &1.id) - - alters = - GlobalJournalAlters - |> where(^where) - |> where([gja], gja.global_journal_id in ^entry_ids) - |> select([gja], %{global_journal_id: gja.global_journal_id, alter_id: gja.alter_id}) - |> Repo.all_regional({:user, system_identity}) - |> Enum.group_by(& &1.global_journal_id, & &1.alter_id) - - entries - |> Enum.map(fn entry -> - alters = Map.get(alters, entry.id, []) - %{entry | alters: alters} - end) - end - - def create_global_journal_entry(system_identity, title) do - case Accounts.id_from_system_identity(system_identity, :system) do - nil -> - {:error, :not_found} - - system_id -> - id = Ecto.UUID.generate() - - result = - %GlobalJournalEntry{ - id: id, - user_id: system_id - } - |> GlobalJournalEntry.changeset(%{title: title}) - |> Repo.insert_regional({:user, system_identity}) - - case result do - {:ok, entry} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "global_journal_entry_created", - %{ - entry: GlobalRenderer.data(%{entry | alters: []}) - } - ) - end) - - {:ok, entry} - - _ -> - {:error, :changeset} - end - end - end - - def attach_alter_to_global_entry(system_identity, entry_id, alter_identity) do - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :alter_not_found} - - alter_id -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - %GlobalJournalAlters{ - user_id: system_id, - global_journal_id: entry_id, - alter_id: alter_id - } - |> GlobalJournalAlters.changeset() - |> Repo.insert_regional({:user, system_identity}) - |> case do - {:ok, _} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "global_journal_entry_updated", - %{ - entry: - GlobalRenderer.data(get_global_journal_entry({:system, system_id}, entry_id)) - } - ) - end) - - :ok - - _ -> - {:error, :changeset} - end - end - end - - def detach_alter_from_global_entry(system_identity, entry_id, alter_identity) do - where = unwrap_system_identity_where(system_identity, global_journal_id: entry_id) - - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :alter_not_found} - - alter_id -> - query = - GlobalJournalAlters - |> where(^where) - |> where(alter_id: ^alter_id) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "global_journal_entry_updated", - %{ - entry: - GlobalRenderer.data(get_global_journal_entry({:system, system_id}, entry_id)) - } - ) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - end - - def update_global_journal_entry(system_identity, entry_id, attrs) do - case get_global_journal_entry(system_identity, entry_id) do - nil -> - {:error, :not_found} - - entry -> - result = - entry - |> GlobalJournalEntry.changeset(attrs) - |> Repo.update_regional({:user, system_identity}) - - case result do - {:ok, bare_entry} -> - system_id = bare_entry.user_id - - entry = get_global_journal_entry({:system, system_id}, entry_id) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{bare_entry.user_id}", - "global_journal_entry_updated", - %{ - entry: GlobalRenderer.data(entry) - } - ) - end) - - {:ok, entry} - - _ -> - {:error, :changeset} - end - end - end - - # [TODO]: Delete alter references - def delete_global_journal_entry(system_identity, entry_id) do - where = unwrap_system_identity_where(system_identity, id: entry_id) - - query = - GlobalJournalEntry - |> where(^where) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - query = - GlobalJournalAlters - |> where(^unwrap_system_identity_where(system_identity)) - |> where(global_journal_id: ^entry_id) - - Repo.delete_all_regional(query, {:user, system_identity}) - - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "global_journal_entry_deleted", - %{ - entry_id: entry_id - } - ) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - - @all_alter_fields AlterJournalEntry.__struct__() - |> Map.drop([:__meta__, :__struct__, :user, :alter]) - |> Map.keys() - - @bare_alter_fields @all_alter_fields -- [:content] - - def get_alter_journal_entries(system_identity, alter_identity, opts \\ []) do - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :no_alter} - - alter_id -> - where = unwrap_system_identity_where(system_identity) - - fields = - Keyword.get(opts, :fields, :all) - |> case do - :bare -> - @bare_alter_fields - - :all -> - @all_alter_fields - - other when is_list(other) and other != [] -> - other - - _ -> - raise ArgumentError, - "Invalid fields: expected :bare, :all or a non-empty list of field atoms" - end - - AlterJournalEntry - |> where(^where) - |> where(alter_id: ^alter_id) - |> select([j], struct(j, ^fields)) - |> Repo.all_regional({:user, system_identity}) - |> Enum.sort_by(& &1.inserted_at, {:desc, NaiveDateTime}) - end - end - - def get_alter_journal_entry(system_identity, entry_id) do - where = unwrap_system_identity_where(system_identity, id: entry_id) - - AlterJournalEntry - |> where(^where) - |> Repo.one_regional({:user, system_identity}) - end - - def create_alter_journal_entry(system_identity, alter_identity, title) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :no_alter} - - alter_id -> - id = Ecto.UUID.generate() - - %AlterJournalEntry{ - id: id, - user_id: system_id, - alter_id: alter_id - } - |> AlterJournalEntry.changeset(%{title: title}) - |> Repo.insert_regional({:user, system_identity}) - |> case do - {:ok, entry} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "alter_journal_entry_created", - %{ - entry: AlterRenderer.data(entry) - } - ) - end) - - {:ok, entry} - - _ -> - {:error, :changeset} - end - end - end - - def delete_alter_journal_entry(system_identity, entry_id) do - where = unwrap_system_identity_where(system_identity, id: entry_id) - - query = - AlterJournalEntry - |> where(^where) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "alter_journal_entry_deleted", - %{ - entry_id: entry_id - } - ) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - - def update_alter_journal_entry(system_identity, entry_id, attrs) do - case get_alter_journal_entry(system_identity, entry_id) do - nil -> - {:error, :not_found} - - entry -> - result = - entry - |> AlterJournalEntry.changeset(attrs) - |> Repo.update_regional({:user, system_identity}) - - case result do - {:ok, entry} -> - system_id = entry.user_id - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "alter_journal_entry_updated", - %{ - entry: AlterRenderer.data(entry) - } - ) - end) - - {:ok, entry} - - _ -> - {:error, :changeset} - end - end - end - - def delete_alter_journal_entries(system_identity, alter_id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - query = - from aje in AlterJournalEntry, - hints: ["ALLOW FILTERING"], - where: aje.user_id == ^system_id and aje.alter_id == ^alter_id - - entries = Repo.all_regional(query, {:user, system_identity}) - - ids = Enum.map(entries, & &1.id) - - delete_query = - from aje in AlterJournalEntry, - where: aje.user_id == ^system_id and aje.id in ^ids - - case Repo.delete_all_regional(delete_query, {:user, system_identity}) do - {count, _} when count > 0 -> - :ok - - _ -> - {:error, :not_found} - end - end -end diff --git a/lib/octocon/journals/alter_journal_entry.ex b/lib/octocon/journals/alter_journal_entry.ex deleted file mode 100644 index 29f74a2c..00000000 --- a/lib/octocon/journals/alter_journal_entry.ex +++ /dev/null @@ -1,50 +0,0 @@ -defmodule Octocon.Journals.AlterJournalEntry do - @moduledoc """ - An alter-specific journal entry. It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - An alter ID (integer up to 32,767) - - A title (up to 100 characters) - - Content (up to 20,000 characters) - - A color (optional, a hexadecimal color code, e.g. `#ff0000`) - """ - - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "alter_journals" do - field :user_id, :string, primary_key: true - field :id, Ecto.UUID, primary_key: true - field :alter_id, :integer, primary_key: true - - field :title, :string - field :content, :string - field :color, :string - field :pinned, :boolean, default: false - field :locked, :boolean, default: false - - # belongs_to :user, Octocon.Accounts.User, - # foreign_key: :user_id, - # define_field: false - - # belongs_to :alter, Octocon.Alters.Alter, - # foreign_key: :alter_id, - # define_field: false - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Journals.AlterJournalEntry` struct and `attrs` to change. - """ - def changeset(alter_journal_entry, attrs) do - alter_journal_entry - |> cast(attrs, [:title, :content, :color, :pinned, :locked]) - |> validate_length(:title, max: 100) - |> validate_length(:content, max: 50_000) - |> validate_format(:color, ~r/^#[0-9a-fA-F]{6}$/) - |> validate_required([:title]) - end -end diff --git a/lib/octocon/journals/global_journal_alters.ex b/lib/octocon/journals/global_journal_alters.ex deleted file mode 100644 index d38c63d3..00000000 --- a/lib/octocon/journals/global_journal_alters.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule Octocon.Journals.GlobalJournalAlters do - @moduledoc """ - A join table entry associating an alter with a global journal entry. - """ - - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "global_journal_alters" do - field :user_id, :string, primary_key: true - field :global_journal_id, Ecto.UUID, primary_key: true - field :alter_id, :integer, primary_key: true - - # belongs_to :global_journal, Octocon.Journals.GlobalJournalEntry, - # foreign_key: :global_journal_id, - # define_field: false - - # belongs_to :alter, Octocon.Alters.Alter, - # foreign_key: :alter_id, - # define_field: false - end - - @doc """ - Builds a changeset based on the given `Octocon.Journals.GlobalJournalAlters` struct and `attrs` to change. - """ - def changeset(global_journal_entry, attrs \\ %{}) do - global_journal_entry - |> cast(attrs, [:global_journal_id, :alter_id]) - |> foreign_key_constraint(:global_journal_id) - |> foreign_key_constraint(:alter_id) - |> unique_constraint([:global_journal_id, :alter_id]) - |> validate_required([:user_id, :global_journal_id, :alter_id]) - end -end diff --git a/lib/octocon/journals/global_journal_entry.ex b/lib/octocon/journals/global_journal_entry.ex deleted file mode 100644 index 6aef7bfc..00000000 --- a/lib/octocon/journals/global_journal_entry.ex +++ /dev/null @@ -1,43 +0,0 @@ -defmodule Octocon.Journals.GlobalJournalEntry do - @moduledoc """ - A journal entry that is "global" (i.e. not tied to a specific alter). It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - A title (up to 100 characters) - - Content (up to 20,000 characters) - - A color (optional, a hexadecimal color code, e.g. `#ff0000`) - - A list of alter IDs that are associated with this journal entry (virtual, populated by the data layer) - """ - - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "global_journals" do - field :user_id, :string, primary_key: true - field :id, Ecto.UUID, primary_key: true - - field :title, :string - field :content, :string - field :color, :string - field :pinned, :boolean, default: false - field :locked, :boolean, default: false - - field :alters, {:array, :integer}, virtual: true - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Journals.GlobalJournalEntry` struct and `attrs` to change. - """ - def changeset(global_journal_entry, attrs) do - global_journal_entry - |> cast(attrs, [:title, :content, :color, :pinned, :locked]) - |> validate_length(:title, max: 100) - |> validate_length(:content, max: 50_000) - |> validate_format(:color, ~r/^#[0-9a-fA-F]{6}$/) - |> validate_required([:title]) - end -end diff --git a/lib/octocon/message_repo.ex b/lib/octocon/message_repo.ex deleted file mode 100644 index 2a4b4b95..00000000 --- a/lib/octocon/message_repo.ex +++ /dev/null @@ -1,5 +0,0 @@ -defmodule Octocon.MessageRepo do - use Ecto.Repo, - otp_app: :octocon, - adapter: Ecto.Adapters.Postgres -end diff --git a/lib/octocon/messages.ex b/lib/octocon/messages.ex deleted file mode 100644 index 4a637755..00000000 --- a/lib/octocon/messages.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule Octocon.Messages do - @moduledoc """ - The Messages context. - """ - - import Ecto.Query, warn: false - alias Octocon.MessageRepo, as: Repo - - alias Octocon.Messages.Message - - def insert_message(attrs) do - Octocon.RPC.NodeTracker.rpc_primary({__MODULE__, :insert_message_internal, [attrs]}) - end - - def insert_message_internal(attrs) do - %Message{} - |> Message.changeset(attrs) - |> Repo.insert() - end - - def lookup_message(message_id) do - Octocon.RPC.NodeTracker.rpc_primary({__MODULE__, :lookup_message_internal, [message_id]}) - end - - def lookup_message_internal(message_id) do - message_timestamp = Nostrum.Snowflake.creation_time(message_id) - - query = - from m in Message, - where: m.message_id == ^to_string(message_id), - where: m.timestamp == ^message_timestamp, - limit: 1 - - Repo.one(query) - end -end diff --git a/lib/octocon/messages/message.ex b/lib/octocon/messages/message.ex deleted file mode 100644 index 54ec2eee..00000000 --- a/lib/octocon/messages/message.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule Octocon.Messages.Message do - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "messages" do - field :timestamp, :utc_datetime - field :message_id, :string - field :author_id, :string - field :system_id, :string - field :alter_id, :integer - end - - def changeset(message, attrs) do - message - |> cast(attrs, [ - :timestamp, - :message_id, - :author_id, - :system_id, - :alter_id - ]) - |> validate_required([ - :timestamp, - :message_id, - :author_id, - :system_id, - :alter_id - ]) - end -end diff --git a/lib/octocon/notification_tokens.ex b/lib/octocon/notification_tokens.ex deleted file mode 100644 index 8b53a66d..00000000 --- a/lib/octocon/notification_tokens.ex +++ /dev/null @@ -1,138 +0,0 @@ -defmodule Octocon.NotificationTokens do - @moduledoc """ - The NotificationTokens context. - """ - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters, - Alters.Alter, - Friendships.Friendship, - NotificationTokens.NotificationToken, - Repo - } - - @doc """ - Gets a single notification_token. - - Raises `Ecto.NoResultsError` if the Notification token does not exist. - - ## Examples - - iex> get_notification_token!(123) - %NotificationToken{} - - iex> get_notification_token!(456) - ** (Ecto.NoResultsError) - - """ - def get_notification_tokens(system_identity) do - user_id = Accounts.id_from_system_identity(system_identity, :system) - - from(n in NotificationToken, - where: n.user_id == ^user_id, - select: n - ) - |> Repo.all_global() - end - - def batch_notifications(system_identity, alter_ids) do - user_id = Accounts.id_from_system_identity(system_identity, :system) - - # Load all alters belonging to the user and filtered by alter_ids - alters = - from( - a in Alter, - where: a.user_id == ^user_id and a.id in ^alter_ids, - select: a - ) - |> Repo.all_regional({:user, system_identity}) - - # Load all friendships for the user - friendships = - from( - f in Friendship, - where: f.user_id == ^user_id, - select: {f.friend_id, f.level} - ) - |> Repo.all_global() - - friend_ids = Enum.map(friendships, fn {id, _level} -> id end) - - # Load all notification tokens for those friends - tokens = - from( - n in NotificationToken, - where: n.user_id in ^friend_ids, - select: n - ) - |> Repo.all_global() - - # Group tokens by user_id - token_map = Enum.group_by(tokens, & &1.user_id) - - # Build final notification map - friendships - |> Enum.map(fn {friend_id, level} -> - # Get tokens for this friend - tokens = Map.get(token_map, friend_id, []) |> Enum.map(& &1.push_token) - - # Filter alters visible at this security level - visible_alters = - alters - |> Enum.filter(fn alter -> - Alters.can_view_entity?(level, alter.security_level) - end) - |> Enum.map_join(", ", & &1.name) - - visible_alters = - cond do - visible_alters == "" -> "No one is fronting" - String.length(visible_alters) > 150 -> String.slice(visible_alters, 0..150) <> "\n..." - true -> visible_alters - end - - {tokens, visible_alters} - end) - |> Enum.reject(fn {tokens, _} -> tokens == [] end) - |> Enum.into(%{}) - end - - def get_tokens_for_users(system_identities) do - user_ids = Enum.map(system_identities, &Accounts.id_from_system_identity(&1, :system)) - - from(n in NotificationToken, - where: n.user_id in ^user_ids, - select: n - ) - |> Repo.all_global() - |> Enum.group_by(& &1.user_id) - end - - def add_notification_token(system_identity, token) do - user_id = Accounts.id_from_system_identity(system_identity, :system) - - changeset = - NotificationToken.changeset(%NotificationToken{}, %{user_id: user_id, push_token: token}) - - Repo.insert_global(changeset) - end - - def invalidate_notification_token(system_identity, token) do - user_id = Accounts.id_from_system_identity(system_identity, :system) - - from(n in NotificationToken, - where: n.user_id == ^user_id and n.push_token == ^token - ) - |> Repo.delete_all_global() - end - - def invalidate_notification_token(token) do - from(n in NotificationToken, - where: n.push_token == ^token - ) - |> Repo.delete_all_global() - end -end diff --git a/lib/octocon/notification_tokens/notification_token.ex b/lib/octocon/notification_tokens/notification_token.ex deleted file mode 100644 index 1f4db172..00000000 --- a/lib/octocon/notification_tokens/notification_token.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Octocon.NotificationTokens.NotificationToken do - @moduledoc """ - A token used to send mobile push notifications to a user. It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - A push token (a unique string identifying the device to be sent to Firebase Cloud Messaging) - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "notification_tokens" do - field :user_id, :string, primary_key: true - field :push_token, :string, primary_key: true - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.NotificationTokens.NotificationToken` struct and `attrs` to change. - """ - def changeset(notification_token, attrs) do - notification_token - |> cast(attrs, [:user_id, :push_token]) - |> validate_required([:user_id, :push_token]) - end -end diff --git a/lib/octocon/polls.ex b/lib/octocon/polls.ex deleted file mode 100644 index 82bfa29b..00000000 --- a/lib/octocon/polls.ex +++ /dev/null @@ -1,146 +0,0 @@ -defmodule Octocon.Polls do - @moduledoc false - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Repo - } - - alias Octocon.Polls.Poll - alias OctoconWeb.PollJSON - - defp unwrap_system_identity_where(system_identity, extra \\ []) do - case system_identity do - {:system, system_id} -> - [user_id: system_id] |> Keyword.merge(extra) - - {:discord, _} = identity -> - [user_id: Accounts.id_from_system_identity(identity, :system)] - |> Keyword.merge(extra) - end - end - - def get_polls(system_identity) do - where = unwrap_system_identity_where(system_identity) - - query = - Poll - |> where(^where) - |> select([p], p) - - Repo.all_regional(query, {:user, system_identity}) - end - - def get_poll(system_identity, poll_id) do - where = unwrap_system_identity_where(system_identity, id: poll_id) - - query = - Poll - |> where(^where) - |> select([p], p) - - Repo.one_regional(query, {:user, system_identity}) - end - - def create_poll(system_identity, attrs) do - case Accounts.id_from_system_identity(system_identity, :system) do - nil -> - {:error, :not_found} - - system_id -> - id = Ecto.UUID.generate() - - result = - %Poll{ - id: id, - user_id: system_id, - data: %{} - } - |> change_poll(attrs) - |> Repo.insert_regional({:user, system_identity}) - - case result do - {:ok, poll} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "poll_created", - %{ - poll: PollJSON.data(poll) - } - ) - end) - - {:ok, poll} - - _ -> - {:error, :changeset} - end - end - end - - def delete_poll(system_identity, poll_id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - where = unwrap_system_identity_where({:system, system_id}, id: poll_id) - - query = - Poll - |> where(^where) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "poll_deleted", - %{poll_id: poll_id} - ) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - - def update_poll(system_identity, poll_id, attrs) do - case get_poll(system_identity, poll_id) do - nil -> - {:error, :not_found} - - poll -> - result = - poll - |> change_poll(attrs) - |> Repo.update() - - case result do - {:ok, poll} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{poll.user_id}", - "poll_updated", - %{ - poll: PollJSON.data(poll) - } - ) - end) - - {:ok, poll} - - _ -> - {:error, :changeset} - end - end - end - - @doc """ - Builds a changeset based on the given `Octocon.Polls.Poll` struct and `attrs` to change. - """ - def change_poll(%Poll{} = poll, attrs \\ %{}) do - Poll.changeset(poll, attrs) - end -end diff --git a/lib/octocon/polls/poll.ex b/lib/octocon/polls/poll.ex deleted file mode 100644 index 363c5a9f..00000000 --- a/lib/octocon/polls/poll.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule Octocon.Polls.Poll do - use Ecto.Schema - - import Ecto.Changeset - - @primary_key false - - schema "polls" do - field :user_id, :string, primary_key: true - field :id, Ecto.UUID, primary_key: true - - field :title, :string - field :description, :string - field :type, Ecto.Enum, values: [vote: 0, choice: 1], default: :vote - field :data, :map - - field :time_end, :utc_datetime - - # belongs_to :user, Octocon.Accounts.User, - # foreign_key: :user_id, - # define_field: false - - timestamps() - end - - @doc false - def changeset(poll, attrs) do - poll - |> cast(attrs, [:user_id, :title, :description, :type, :data, :time_end]) - |> validate_length(:title, max: 100) - |> validate_length(:description, max: 2000) - |> validate_required([:title, :type, :data]) - end -end diff --git a/lib/octocon/primary/horde_registry.ex b/lib/octocon/primary/horde_registry.ex deleted file mode 100644 index 7af3cc67..00000000 --- a/lib/octocon/primary/horde_registry.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Octocon.Primary.HordeRegistry do - use Horde.Registry - - require Logger - - alias Octocon.ClusterUtils - - def start_link(_) do - Horde.Registry.start_link(__MODULE__, [keys: :unique], name: __MODULE__) - end - - def init(init_arg) do - [members: members()] - |> Keyword.merge(init_arg) - |> Horde.Registry.init() - end - - # Only nodes marked as primary should be part of the registry - defp members do - nodes = - ClusterUtils.primary_nodes(true) - |> Enum.map(fn node -> {__MODULE__, node} end) - - Logger.info("Valid nodes (registry): #{inspect(nodes)}") - - nodes - end -end diff --git a/lib/octocon/primary/horde_supervisor.ex b/lib/octocon/primary/horde_supervisor.ex deleted file mode 100644 index d53da76d..00000000 --- a/lib/octocon/primary/horde_supervisor.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule Octocon.Primary.HordeSupervisor do - use Horde.DynamicSupervisor - - require Logger - - alias Octocon.ClusterUtils - - def start_link(_) do - Horde.DynamicSupervisor.start_link( - __MODULE__, - [strategy: :one_for_one], - name: __MODULE__ - ) - end - - def init(init_arg) do - [members: members()] - |> Keyword.merge(init_arg) - |> Horde.DynamicSupervisor.init() - end - - # Only nodes in the primary group should be part of the supervisor - defp members do - nodes = - ClusterUtils.primary_nodes(true) - |> Enum.map(fn node -> {__MODULE__, node} end) - - Logger.info("Valid nodes (supervisor): #{inspect(nodes)}") - - nodes - end -end diff --git a/lib/octocon/primary/node_listener.ex b/lib/octocon/primary/node_listener.ex deleted file mode 100644 index 78985563..00000000 --- a/lib/octocon/primary/node_listener.ex +++ /dev/null @@ -1,49 +0,0 @@ -defmodule Octocon.Primary.NodeListener do - use GenServer - - alias Octocon.ClusterUtils - - require Logger - - def start_link(_), do: GenServer.start_link(__MODULE__, []) - - def init(_) do - :net_kernel.monitor_nodes(true, node_type: :visible) - {:ok, nil} - end - - def handle_info({:nodeup, node, _node_type}, state) do - Logger.info("Node up: #{node}") - - update_nodes() - - {:noreply, state} - end - - def handle_info({:nodedown, node, _node_type}, state) do - Logger.info("Node down: #{node}") - - update_nodes() - - {:noreply, state} - end - - def update_nodes do - nodes_incl_self = ClusterUtils.primary_nodes(true) - - # nodes = Enum.filter(nodes_incl_self, fn node -> node != Node.self() end) - - set_horde_members(Octocon.Primary.HordeRegistry, nodes_incl_self) - set_horde_members(Octocon.Primary.HordeSupervisor, nodes_incl_self) - end - - defp set_horde_members(name, nodes) do - members = - nodes - |> Enum.map(fn node -> {name, node} end) - - Logger.info("Valid nodes: #{inspect(members)}") - - :ok = Horde.Cluster.set_members(name, members) - end -end diff --git a/lib/octocon/primary/supervisor.ex b/lib/octocon/primary/supervisor.ex deleted file mode 100644 index 458073a3..00000000 --- a/lib/octocon/primary/supervisor.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Octocon.Primary.Supervisor do - use Supervisor - - def start_link(_), do: Supervisor.start_link(__MODULE__, [], name: __MODULE__) - - def init([]) do - children = [ - Octocon.Primary.NodeListener, - Octocon.Primary.HordeRegistry, - Octocon.Primary.HordeSupervisor - ] - - Supervisor.init(children, strategy: :one_for_one) - end -end diff --git a/lib/octocon/prom_ex.ex b/lib/octocon/prom_ex.ex deleted file mode 100644 index 581fd5e7..00000000 --- a/lib/octocon/prom_ex.ex +++ /dev/null @@ -1,90 +0,0 @@ -defmodule Octocon.PromEx do - @moduledoc """ - Be sure to add the following to finish setting up PromEx: - - 1. Update your configuration (config.exs, dev.exs, prod.exs, releases.exs, etc) to - configure the necessary bit of PromEx. Be sure to check out `PromEx.Config` for - more details regarding configuring PromEx: - ``` - config :octocon, Octocon.PromEx, - disabled: false, - manual_metrics_start_delay: :no_delay, - drop_metrics_groups: [], - grafana: :disabled, - metrics_server: :disabled - ``` - - 2. Add this module to your application supervision tree. It should be one of the first - things that is started so that no Telemetry events are missed. For example, if PromEx - is started after your Repo module, you will miss Ecto's init events and the dashboards - will be missing some data points: - ``` - def start(_type, _args) do - children = [ - Octocon.PromEx, - - ... - ] - - ... - end - ``` - - 3. Update your `endpoint.ex` file to expose your metrics (or configure a standalone - server using the `:metrics_server` config options). Be sure to put this plug before - your `Plug.Telemetry` entry so that you can avoid having calls to your `/metrics` - endpoint create their own metrics and logs which can pollute your logs/metrics given - that Prometheus will scrape at a regular interval and that can get noisy: - ``` - defmodule OctoconWeb.Endpoint do - use Phoenix.Endpoint, otp_app: :octocon - - ... - - plug PromEx.Plug, prom_ex_module: Octocon.PromEx - - ... - end - ``` - - 4. Update the list of plugins in the `plugins/0` function return list to reflect your - application's dependencies. Also update the list of dashboards that are to be uploaded - to Grafana in the `dashboards/0` function. - """ - - use PromEx, otp_app: :octocon - - alias PromEx.Plugins - - @impl PromEx - def plugins do - [ - # PromEx built-in plugins - Plugins.Application, - Plugins.Beam, - {Plugins.Phoenix, router: OctoconWeb.Router, endpoint: OctoconWeb.Endpoint}, - Plugins.Ecto, - Plugins.Oban - ] - end - - @impl PromEx - def dashboard_assigns do - [ - datasource_id: "prometheus_on_fly", - default_selected_interval: "30s" - ] - end - - @impl PromEx - def dashboards do - [ - # PromEx built in Grafana dashboards - {:prom_ex, "application.json"}, - {:prom_ex, "beam.json"}, - {:prom_ex, "phoenix.json"}, - {:prom_ex, "ecto.json"}, - {:prom_ex, "oban.json"} - ] - end -end diff --git a/lib/octocon/release.ex b/lib/octocon/release.ex deleted file mode 100644 index 72cc0845..00000000 --- a/lib/octocon/release.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Octocon.Release do - @moduledoc """ - Used for executing DB release tasks when run in production without Mix - installed. - """ - @app :octocon - - def migrate do - load_app() - - for repo <- repos() do - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) - end - end - - def rollback(repo, version) do - load_app() - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) - end - - defp repos do - Application.fetch_env!(@app, :ecto_repos) - end - - defp load_app do - Application.load(@app) - end -end diff --git a/lib/octocon/repo.ex b/lib/octocon/repo.ex deleted file mode 100644 index 78af5bf1..00000000 --- a/lib/octocon/repo.ex +++ /dev/null @@ -1,252 +0,0 @@ -defmodule Octocon.Repo.Macros do - @moduledoc false - - alias Octocon.UserRegistryCache - - defmacro create_global_shim(verb_string) do - # credo:disable-for-lines:2 - verb = String.to_atom(verb_string) - func_name = String.to_atom("#{verb}_global") - - quote bind_quoted: [func_name: func_name, verb: verb] do - def unquote(func_name)(struct_or_changeset, opts \\ []) do - consistency = Keyword.get(opts, :consistency, :local_one) - - __MODULE__.unquote(verb)( - struct_or_changeset, - opts - |> Keyword.put(:prefix, "global") - |> Keyword.put(:consistency, consistency) - ) - end - end - end - - defmacro create_nam_nt_shim(verb_string) do - # credo:disable-for-lines:2 - verb = String.to_atom(verb_string) - func_name = String.to_atom("#{verb}_nam_nt") - - quote bind_quoted: [func_name: func_name, verb: verb] do - def unquote(func_name)(struct_or_changeset, opts \\ []) do - consistency = Keyword.get(opts, :consistency, :one) - - __MODULE__.unquote(verb)( - struct_or_changeset, - opts - |> Keyword.put(:prefix, "nam_nt") - |> Keyword.put(:consistency, consistency) - ) - end - end - end - - defmacro create_regional_shim(verb_string) do - # credo:disable-for-lines:2 - verb = String.to_atom(verb_string) - func_name = String.to_atom("#{verb}_regional") - - quote bind_quoted: [func_name: func_name, verb: verb] do - def unquote(func_name)(struct_or_changeset, identifier, opts \\ []) - - def unquote(func_name)(struct_or_changeset, {:region, region}, opts) - when is_binary(region) do - unquote(func_name)( - struct_or_changeset, - {:region, String.to_existing_atom(region)}, - opts - ) - end - - def unquote(func_name)(struct_or_changeset, {:region, region}, opts) - when is_atom(region) and region in @regions do - consistency = Octocon.Repo.Macros.consistency_from_opts(opts, region) - - __MODULE__.unquote(verb)( - struct_or_changeset, - opts - |> Keyword.put(:prefix, to_string(region)) - |> Keyword.put(:consistency, consistency) - ) - end - - def unquote(func_name)(struct_or_changeset, {:user, system_identity}, opts) do - region = UserRegistryCache.get_region(system_identity) - - if region == nil do - nil - else - unquote(func_name)( - struct_or_changeset, - {:region, region}, - opts - ) - end - end - end - end - - def consistency_from_opts(opts, region) do - current_region = Octocon.ClusterUtils.current_db_region() - - cond do - Keyword.has_key?(opts, :consistency) -> - Keyword.get(opts, :consistency) - - region == :gdpr -> - :one - - current_region == :nam or region == current_region -> - :local_one - - true -> - :one - end - end -end - -defmodule Octocon.Repo do - @regions [:nam, :eur, :ocn, :eas, :sam, :sas, :gdpr] - @funs [ - "insert", - "update", - "delete", - "delete_all", - "get", - "one", - "all", - "aggregate" - ] - - use Ecto.Repo, - otp_app: :octocon, - adapter: Exandra - - require Octocon.Repo.Macros - alias Octocon.Repo.Macros - - alias Octocon.UserRegistryCache - - def region_list, do: @regions - - Enum.each(@funs, fn fun -> - Code.eval_quoted( - quote do - Macros.create_global_shim(unquote(fun)) - Macros.create_nam_nt_shim(unquote(fun)) - Macros.create_regional_shim(unquote(fun)) - end, - [], - __ENV__ - ) - end) - - ### Manual shims - - def exists_regional?(struct_or_changeset, identifier, opts \\ []) - - def exists_regional?(struct_or_changeset, {:region, region}, opts) - when is_binary(region) do - exists_regional?( - struct_or_changeset, - String.to_existing_atom(region), - opts - ) - end - - def exists_regional?(struct_or_changeset, {:region, region}, opts) - when is_atom(region) and region in @regions do - consistency = Macros.consistency_from_opts(opts, region) - - __MODULE__.exists?( - struct_or_changeset, - opts - |> Keyword.put(:prefix, to_string(region)) - |> Keyword.put(:consistency, consistency) - ) - end - - def exists_regional?(struct_or_changeset, {:user, system_identity}, opts) do - region = UserRegistryCache.get_region(system_identity) - - if region == nil do - nil - else - exists_regional?(struct_or_changeset, {:region, region}, opts) - end - end - - def exists_global?(struct_or_changeset, opts \\ []) do - __MODULE__.exists?(struct_or_changeset, Keyword.put(opts, :prefix, "global")) - end - - def exists_nam_nt?(struct_or_changeset, opts \\ []) do - __MODULE__.exists?(struct_or_changeset, Keyword.put(opts, :prefix, "nam_nt")) - end - - def update_all_regional(struct_or_changeset, updates, identifier, opts \\ []) - - def update_all_regional(struct_or_changeset, updates, {:region, region}, opts) - when is_atom(region) and region in @regions do - consistency = Macros.consistency_from_opts(opts, region) - - __MODULE__.update_all( - struct_or_changeset, - updates, - opts - |> Keyword.put(:prefix, to_string(region)) - |> Keyword.put(:consistency, consistency) - ) - end - - def update_all_regional(struct_or_changeset, updates, {:user, system_identity}, opts) do - region = UserRegistryCache.get_region(system_identity) - - if region == nil do - nil - else - update_all_regional(struct_or_changeset, updates, {:region, region}, opts) - end - end - - def update_all_global(struct_or_changeset, updates, opts \\ []) do - __MODULE__.update_all(struct_or_changeset, updates, Keyword.put(opts, :prefix, "global")) - end - - def update_all_nam_nt(struct_or_changeset, updates, opts \\ []) do - __MODULE__.update_all(struct_or_changeset, updates, Keyword.put(opts, :prefix, "nam_nt")) - end - - def insert_all_regional(module, inserts, identifier, opts \\ []) - - def insert_all_regional(module, inserts, {:region, region}, opts) - when is_atom(region) and region in @regions do - consistency = Macros.consistency_from_opts(opts, region) - - __MODULE__.insert_all( - module, - inserts, - opts - |> Keyword.put(:prefix, to_string(region)) - |> Keyword.put(:consistency, consistency) - ) - end - - def insert_all_regional(module, inserts, {:user, system_identity}, opts) do - region = UserRegistryCache.get_region(system_identity) - - if region == nil do - nil - else - insert_all_regional(module, inserts, {:region, region}, opts) - end - end - - def insert_all_global(module, inserts, opts \\ []) do - __MODULE__.insert_all(module, inserts, Keyword.put(opts, :prefix, "global")) - end - - def insert_all_nam_nt(module, inserts, opts \\ []) do - __MODULE__.insert_all(module, inserts, Keyword.put(opts, :prefix, "nam_nt")) - end -end diff --git a/lib/octocon/rpc/node_tracker.ex b/lib/octocon/rpc/node_tracker.ex deleted file mode 100644 index 33d8cdd7..00000000 --- a/lib/octocon/rpc/node_tracker.ex +++ /dev/null @@ -1,198 +0,0 @@ -defmodule Octocon.RPC.NodeTracker do - @moduledoc false - - use GenServer - require Logger - - def start_link(opts) do - GenServer.start_link(__MODULE__, opts, name: __MODULE__) - end - - defp group_nodes(group) do - case :ets.lookup(__MODULE__, group) do - [{^group, nodes}] -> nodes - [] -> [] - end - end - - def primary_nodes, do: group_nodes(:primary) - def auxiliary_nodes, do: group_nodes(:auxiliary) - def sidecar_nodes, do: group_nodes(:sidecar) - - def sidecar_exists?, do: sidecar_nodes() != [] - - def current_group do - Application.get_env(:octocon, :node_group) - end - - def primary?, do: current_group() == :primary - - @doc """ - Asks a node what group it is in. - - Returns `:error` if RPC is not supported on remote node. - """ - def group(node) do - rpc(node, {__MODULE__, :current_group, []}) - end - - def rpc_group(group, func, opts \\ []) - - def rpc_group(group, func, opts) - when (is_function(func, 0) or is_tuple(func)) and - is_list(opts) do - if group == current_group() do - invoke(func) - else - timeout = Keyword.get(opts, :timeout, 5_000) - available_nodes = group_nodes(group) - - if Enum.empty?(available_nodes), - do: raise(ArgumentError, "No node found running in group #{inspect(group)}") - - node = Enum.random(available_nodes) - - rpc(node, func, timeout) - end - end - - def rpc_group(region, {mod, func, args}, opts) - when is_atom(mod) and is_list(args) and is_list(opts) do - rpc_group(region, fn -> apply(mod, func, args) end, opts) - end - - def rpc_primary(func, opts \\ []) - - def rpc_primary(func, opts) when is_function(func, 0) do - rpc_group(:primary, func, opts) - end - - def rpc_primary({module, func, args}, opts) do - rpc_group(:primary, {module, func, args}, opts) - end - - defp invoke(func) when is_function(func, 0), do: func.() - defp invoke({mod, func, args}), do: apply(mod, func, args) - - @doc """ - Executes the function on the remote node and waits for the response. - - Exits after `timeout` milliseconds. - """ - def rpc(node, func, timeout \\ 5000) do - case erpc_call(node, func, timeout) do - {:ok, result} -> - result - - {:error, {:erpc, :timeout}} -> - exit(:timeout) - - {:error, {:erpc, reason}} -> - {:error, {:erpc, reason}} - - {:error, {:throw, value}} -> - throw(value) - - {:error, {:exit, reason}} -> - exit(reason) - - {:error, {_exception, reason, stack}} -> - reraise(reason, stack) - end - end - - defp erpc_call(node, {mod, func, args}, timeout) do - {:ok, :erpc.call(node, mod, func, args, timeout)} - catch - :throw, value -> {:error, {:throw, value}} - :exit, reason -> {:error, {:exit, reason}} - :error, {:erpc, reason} -> {:error, {:erpc, reason}} - :error, {exception, reason, stack} -> {:error, {exception, reason, stack}} - end - - defp erpc_call(node, func, timeout) when is_function(func, 0) do - {:ok, :erpc.call(node, func, timeout)} - catch - :throw, value -> {:error, {:throw, value}} - :exit, reason -> {:error, {:exit, reason}} - :error, {:erpc, reason} -> {:error, {:erpc, reason}} - :error, {exception, reason, stack} -> {:error, {exception, reason, stack}} - end - - ## RPC calls run on local node - - def init(_opts) do - :ets.new(__MODULE__, [:named_table, :public, read_concurrency: true]) - - # Monitor new node up/down activity - :global_group.monitor_nodes(true) - {:ok, %{nodes: MapSet.new()}, {:continue, :get_node_groups}} - end - - def handle_continue(:get_node_groups, state) do - new_state = - Enum.reduce(Node.list(:visible), state, fn node_name, acc -> - put_node(acc, node_name) - end) - - {:noreply, new_state} - end - - def handle_info({:nodeup, node_name}, state) do - Logger.debug("nodeup #{node_name}") - - # Only react/track visible nodes (hidden ones are for IEx, etc) - if node_name in Node.list(:visible) do - {:noreply, put_node(state, node_name)} - else - {:noreply, state} - end - end - - def handle_info({:nodedown, node_name}, state) do - Logger.debug("nodedown #{node_name}") - {:noreply, drop_node(state, node_name)} - end - - @doc false - def put_node(state, node_name) do - group = group(node_name) - - Logger.info("Discovered node #{inspect(node_name)} in group #{group}") - group_nodes = group_nodes(group) - :ets.insert(__MODULE__, {group, [node_name | group_nodes]}) - - %{state | nodes: MapSet.put(state.nodes, {node_name, group})} - end - - @doc false - def drop_node(state, node_name) do - # Find the node information for the node going down. - case get_node(state, node_name) do - {^node_name, group} -> - Logger.info("Dropping node #{inspect(node_name)} in group #{group}") - group_nodes = group_nodes(group) - # Remove the node from the known regions and update the local cache - new_groups = Enum.reject(group_nodes, fn n -> n == node_name end) - :ets.insert(__MODULE__, {group, new_groups}) - - # Remove the node entry from the GenServer's state - new_nodes = - Enum.reduce(state.nodes, state.nodes, fn - {^node_name, ^group}, acc -> MapSet.delete(acc, {node_name, group}) - {_node, _group}, acc -> acc - end) - - # Return the new state - %{state | nodes: new_nodes} - - # Node is not known to us. Ignore it. - nil -> - state - end - end - - defp get_node(state, name) do - Enum.find(state.nodes, fn {n, _group} -> n == name end) - end -end diff --git a/lib/octocon/server_settings.ex b/lib/octocon/server_settings.ex deleted file mode 100644 index d58a50fe..00000000 --- a/lib/octocon/server_settings.ex +++ /dev/null @@ -1,60 +0,0 @@ -defmodule Octocon.ServerSettings do - @moduledoc """ - The ServerSettings context. - """ - - import Ecto.Query, warn: false - alias Octocon.Repo - - alias Octocon.ServerSettings.{ - ServerSettingsData, - ServerSettingsEntry - } - - def get_server_settings(guild_id) do - query = from(s in ServerSettingsEntry, where: s.guild_id == ^guild_id) - - Repo.one_regional(query, {:region, :nam}) - end - - def get_server_settings_data(guild_id) do - query = from(s in ServerSettingsEntry, where: s.guild_id == ^guild_id, select: s.data) - - Repo.one_regional(query, {:region, :nam}) - end - - def create_server_settings(guild_id) do - %ServerSettingsEntry{} - |> ServerSettingsEntry.changeset(%{guild_id: guild_id, data: %{}}) - |> Repo.insert_regional({:region, :nam}) - end - - def edit_server_settings(guild_id, attrs) do - settings = get_server_settings(guild_id) - - case settings do - nil -> - {:error, :not_found} - - settings -> - data = struct(settings.data || %ServerSettingsData{}, attrs) - - settings - |> ServerSettingsEntry.changeset(%{data: data}) - |> Repo.update_regional({:region, :nam}) - end - end - - def delete_server_settings(guild_id) do - query = from(s in ServerSettingsEntry, where: s.guild_id == ^guild_id) - - case Repo.delete_all_regional(query, {:region, :nam}) do - {1, _} -> :ok - _ -> :error - end - end - - def change_server_settings(%ServerSettingsEntry{} = server_settings_entry, attrs \\ %{}) do - ServerSettingsEntry.changeset(server_settings_entry, attrs) - end -end diff --git a/lib/octocon/server_settings/server_settings_data.ex b/lib/octocon/server_settings/server_settings_data.ex deleted file mode 100644 index 37ab5505..00000000 --- a/lib/octocon/server_settings/server_settings_data.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Octocon.ServerSettings.ServerSettingsData do - @moduledoc """ - The data field of a server settings entry for a given Discord guild. It consists of: - - - A log channel ID (a 17-22 character numeric string) - - A flag to force system tags on all proxied messages - - A list of user IDs (17-22 character numeric strings) for whom proxying is disabled - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - embedded_schema do - field :log_channel, :string - field :force_system_tags, :boolean, default: false - - field :proxy_disabled_users, {:array, :string}, default: [] - end - - @doc """ - Builds a changeset based on the given `Octocon.ServerSettings.ServerSettingsData` struct and `attrs` to change. - """ - def changeset(data, attrs \\ %{}) do - data - |> cast(attrs, [:log_channel, :force_system_tags, :proxy_disabled_users]) - |> validate_required([:force_system_tags]) - end -end diff --git a/lib/octocon/server_settings/server_settings_entry.ex b/lib/octocon/server_settings/server_settings_entry.ex deleted file mode 100644 index 2bc0108e..00000000 --- a/lib/octocon/server_settings/server_settings_entry.ex +++ /dev/null @@ -1,33 +0,0 @@ -defmodule Octocon.ServerSettings.ServerSettingsEntry do - @moduledoc """ - A server settings entry for a given Discord guild. It consists of: - - - A Discord guild ID (a 17-22 character numeric string) - - A data field (an embedded schema containing server settings) - - This data currently includes: - - A log channel ID (a 17-22 character numeric string) - - A flag to force system tags on all proxied messages - """ - use Ecto.Schema - import Ecto.Changeset - import Exandra, only: [embedded_type: 2] - - @primary_key false - - schema "server_settings" do - field :guild_id, :string, primary_key: true - embedded_type(:data, Octocon.ServerSettings.ServerSettingsData) - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.ServerSettings.ServerSettingsEntry` struct and `attrs` to change. - """ - def changeset(%__MODULE__{} = server_settings_entry, attrs \\ %{}) do - server_settings_entry - |> cast(attrs, [:guild_id, :data]) - |> validate_required([:guild_id]) - end -end diff --git a/lib/octocon/tags.ex b/lib/octocon/tags.ex deleted file mode 100644 index 72d02955..00000000 --- a/lib/octocon/tags.ex +++ /dev/null @@ -1,547 +0,0 @@ -defmodule Octocon.Tags do - @moduledoc false - - import Ecto.Query, warn: false - - alias Octocon.{ - Accounts, - Alters, - Friendships, - Repo - } - - alias Octocon.Tags.{ - AlterTag, - Tag - } - - alias OctoconWeb.System.TagJSON, as: TagRenderer - - def unwrap_system_identity_where(system_identity, extra \\ []) do - case system_identity do - {:system, system_id} -> - [user_id: system_id] |> Keyword.merge(extra) - - {:discord, _} = identity -> - [user_id: Accounts.id_from_system_identity(identity, :system)] - |> Keyword.merge(extra) - end - end - - def get_tag(system_identity, tag_id) do - where = unwrap_system_identity_where(system_identity) - - tag = - Tag - |> where(^where) - |> where([t], t.id == ^tag_id) - |> select([t], t) - |> Repo.one_regional({:user, system_identity}) - - tag_alters = - AlterTag - |> where(^where) - |> where([at], at.tag_id == ^tag_id) - |> select([at], at.alter_id) - |> Repo.all_regional({:user, system_identity}) - - if tag == nil do - nil - else - %{tag | alters: tag_alters} - end - rescue - _ in Ecto.Query.CastError -> - nil - end - - # Also returns guarded list of containing alters - def get_tag_guarded(system_identity, tag_id, caller_identity) do - tag = get_tag(system_identity, tag_id) - - friendship_level = Friendships.get_friendship_level(system_identity, caller_identity) - - cond do - tag == nil -> - nil - - Alters.can_view_entity?(friendship_level, tag.security_level) -> - process_guarded_tag(system_identity, tag, friendship_level) - - true -> - nil - end - end - - defp process_guarded_tag(_system_identity, tag, _friendship_level) - when is_map(tag) and tag.alters == [] do - tag - end - - defp process_guarded_tag(system_identity, tag, friendship_level) do - alters = Alters.get_alters_guarded_bare_batch(system_identity, friendship_level, tag.alters) - %{tag | alters: alters} - end - - def get_tags(system_identity) do - where = unwrap_system_identity_where(system_identity) - - tags = - Tag - |> where(^where) - |> select([t], t) - |> Repo.all_regional({:user, system_identity}) - - tag_ids = Enum.map(tags, & &1.id) - - tag_alters = - AlterTag - |> where(^where) - |> where([at], at.tag_id in ^tag_ids) - |> select([at], struct(at, [:tag_id, :alter_id])) - |> Repo.all_regional({:user, system_identity}) - |> Enum.group_by(& &1.tag_id, & &1.alter_id) - - tags - |> Enum.map(fn tag -> - %{tag | alters: Map.get(tag_alters, tag.id, [])} - end) - end - - def get_tags_guarded(system_identity, caller_identity) do - where = unwrap_system_identity_where(system_identity) - - tags = - Tag - |> where(^where) - |> select([t], t) - |> Repo.all_regional({:user, system_identity}) - - friendship_level = Friendships.get_friendship_level(system_identity, caller_identity) - - tags - |> Enum.filter(fn tag -> Alters.can_view_entity?(friendship_level, tag.security_level) end) - |> Enum.map(fn tag -> %{tag | alters: []} end) - end - - def get_tags_for_alter(system_identity, alter_identity) do - alter_id = - case Alters.resolve_alter(system_identity, alter_identity) do - false -> nil - id -> id - end - - if alter_id == nil do - [] - else - where = unwrap_system_identity_where(system_identity) - - tag_ids = - AlterTag - |> where(^where) - |> where([at], at.alter_id == ^alter_id) - |> select([at], at.tag_id) - |> Repo.all_regional({:user, system_identity}) - - tags = - Tag - |> where(^where) - |> where([t], t.id in ^tag_ids) - |> select([t], t) - |> Repo.all_regional({:user, system_identity}) - - tags - end - end - - def get_public_tags_for_alter(system_identity, alter_identity) do - get_tags_for_alter(system_identity, alter_identity) - |> Enum.filter(fn tag -> tag.security_level == :public end) - end - - def create_tag(system_identity, name) do - case Accounts.id_from_system_identity(system_identity, :system) do - nil -> - {:error, :not_found} - - system_id -> - id = Ecto.UUID.generate() - - result = - %Tag{ - id: id, - user_id: system_id - } - |> Tag.changeset(%{name: name}) - |> Repo.insert_regional({:user, system_identity}) - - case result do - {:ok, tag} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_created", - %{ - tag: TagRenderer.data_me(%{tag | alters: []}) - } - ) - - OctoconDiscord.Autocomplete.Tag.invalidate(system_identity) - end) - - {:ok, tag} - - _ -> - {:error, :changeset} - end - end - end - - def create_tag(system_identity, name, parent_tag_id) do - case Accounts.id_from_system_identity(system_identity, :system) do - nil -> - {:error, :not_found} - - system_id -> - case get_tag(system_identity, parent_tag_id) do - nil -> - {:error, :not_found} - - parent_tag when parent_tag.user_id != system_id -> - {:error, :not_found} - - _ -> - id = Ecto.UUID.generate() - - result = - %Tag{ - id: id, - user_id: system_id - } - |> Tag.changeset(%{name: name, parent_tag_id: parent_tag_id}) - |> Repo.insert_regional({:user, system_identity}) - - case result do - {:ok, tag} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_created", - %{ - tag: TagRenderer.data_me(%{tag | alters: []}) - } - ) - - OctoconDiscord.Autocomplete.Tag.invalidate(system_identity) - end) - - {:ok, tag} - - _ -> - {:error, :changeset} - end - end - end - end - - def attach_alter_to_tag(system_identity, tag_id, alter_identity) do - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :alter_not_found} - - alter_id -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - %AlterTag{ - user_id: system_id, - tag_id: tag_id, - alter_id: alter_id - } - |> AlterTag.changeset() - |> Repo.insert_regional({:user, system_identity}) - |> case do - {:ok, _} -> - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_updated", - %{tag: TagRenderer.data_me(get_tag({:system, system_id}, tag_id))} - ) - end) - - :ok - - _ -> - {:error, :changeset} - end - end - end - - def detach_alter_from_tag(system_identity, tag_id, alter_identity) do - where = unwrap_system_identity_where(system_identity, tag_id: tag_id) - - case Alters.resolve_alter(system_identity, alter_identity) do - false -> - {:error, :alter_not_found} - - alter_id -> - query = - AlterTag - |> where(^where) - |> where(alter_id: ^alter_id) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_updated", - %{tag: TagRenderer.data_me(get_tag({:system, system_id}, tag_id))} - ) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - end - - def set_parent_tag(system_identity, tag_id, parent_tag_id) do - tag = Task.async(fn -> get_tag(system_identity, tag_id) end) - parent = Task.async(fn -> get_tag(system_identity, parent_tag_id) end) - - if tag_id == parent_tag_id do - {:error, :tag_cycle} - else - case {Task.await(tag), Task.await(parent)} do - {tag, parent} when tag == nil or parent == nil -> - {:error, :not_found} - - {tag, parent} when tag.user_id != parent.user_id -> - {:error, :not_found} - - {tag, _parent} -> - if can_set_parent?(system_identity, tag_id, parent_tag_id) do - tag - |> Tag.changeset(%{parent_tag_id: parent_tag_id}) - |> Repo.update_regional({:user, system_identity}) - |> case do - {:ok, _} -> - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_updated", - %{tag: TagRenderer.data_me(get_tag({:system, system_id}, tag_id))} - ) - end) - - :ok - - _ -> - {:error, :changeset} - end - else - {:error, :tag_cycle} - end - end - end - end - - def can_set_parent?(system_identity, child_id, new_parent_id) do - walk_up(system_identity, new_parent_id, child_id) - end - - defp walk_up(_system_identity, nil, _child_id), do: true - - defp walk_up(_system_identity, current_id, child_id) when current_id == child_id do - false - end - - defp walk_up(system_identity, current_id, child_id) do - where = unwrap_system_identity_where(system_identity) - - query = - Tag - |> where(^where) - |> where([t], t.id == ^current_id) - |> select([t], t.parent_tag_id) - - case Repo.one_regional(query, {:user, system_identity}) do - nil -> - true - - parent_id -> - walk_up(system_identity, parent_id, child_id) - end - end - - def remove_parent_tag(system_identity, tag_id) do - case get_tag(system_identity, tag_id) do - nil -> - {:error, :not_found} - - tag -> - tag - |> Tag.changeset(%{parent_tag_id: nil}) - |> Repo.update_regional({:user, system_identity}) - |> case do - {:ok, _} -> - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_updated", - %{tag: TagRenderer.data_me(get_tag({:system, system_id}, tag_id))} - ) - end) - - :ok - - _ -> - {:error, :changeset} - end - end - end - - def update_tag(system_identity, tag_id, attrs) do - case get_tag(system_identity, tag_id) do - nil -> - {:error, :not_found} - - tag -> - result = - tag - |> Tag.changeset(Map.drop(attrs, [:parent_tag_id])) - |> Repo.update_regional({:user, system_identity}) - - case result do - {:ok, bare_tag} -> - system_id = bare_tag.user_id - - tag = get_tag({:system, system_id}, tag_id) - - spawn(fn -> - OctoconWeb.Endpoint.broadcast!( - "system:#{bare_tag.user_id}", - "tag_updated", - %{tag: TagRenderer.data_me(tag)} - ) - - if Map.has_key?(attrs, :name) do - OctoconDiscord.Autocomplete.Tag.invalidate(system_identity) - end - end) - - {:ok, tag} - - _ -> - {:error, :changeset} - end - end - rescue - _ in Ecto.Query.CastError -> - {:error, :not_found} - end - - def delete_tag(system_identity, tag_id) do - where = unwrap_system_identity_where(system_identity) - - query = - Tag - |> where(^where) - |> where([t], t.id == ^tag_id) - - case Repo.delete_all_regional(query, {:user, system_identity}) do - {1, _} -> - AlterTag - |> where(^where) - |> where([at], at.tag_id == ^tag_id) - |> Repo.delete_all_regional({:user, system_identity}) - - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - - OctoconWeb.Endpoint.broadcast!( - "system:#{system_id}", - "tag_deleted", - %{tag_id: tag_id} - ) - - OctoconDiscord.Autocomplete.Tag.invalidate(system_identity) - end) - - :ok - - _ -> - {:error, :not_found} - end - end - - def delete_alter_tags(system_identity, alter_id) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - query = - from( - at in AlterTag, - hints: ["ALLOW FILTERING"], - where: at.user_id == ^system_id and at.alter_id == ^alter_id - ) - - alter_tags = Repo.all_regional(query, {:user, system_identity}) - - ids = Enum.map(alter_tags, & &1.tag_id) - - delete_query = - from( - at in AlterTag, - where: at.user_id == ^system_id and at.tag_id in ^ids and at.alter_id == ^alter_id - ) - - case Repo.delete_all_regional(delete_query, {:user, system_identity}) do - {0, _} -> - {:error, :not_found} - - {count, _} when count > 0 -> - # system_id = Accounts.id_from_system_identity(system_identity, :system) - # spawn(fn -> - # OctoconWeb.Endpoint.broadcast!( - # "system:#{system_id}", - # "alter_tags_deleted", - # %{alter_id: alter_id} - # ) - # end) - - :ok - end - end - - def get_random_tag(system_identity) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - all_tag_ids = - from( - t in Tag, - where: t.user_id == ^system_id, - select: t.id - ) - |> Repo.all_regional({:user, system_identity}) - - if all_tag_ids == [] do - nil - else - random_tag_id = Enum.random(all_tag_ids) - - case get_tag(system_identity, random_tag_id) do - nil -> nil - tag -> {:ok, tag} - end - end - end -end diff --git a/lib/octocon/tags/alter_tag.ex b/lib/octocon/tags/alter_tag.ex deleted file mode 100644 index 19295a43..00000000 --- a/lib/octocon/tags/alter_tag.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule Octocon.Tags.AlterTag do - @moduledoc """ - A join table entry associating a tag with an alter. - """ - - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "alter_tags" do - field :user_id, :string, primary_key: true - field :tag_id, Ecto.UUID, primary_key: true - field :alter_id, :integer, primary_key: true - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Tags.AlterTag` struct and `attrs` to change. - """ - def changeset(alter_tag, attrs \\ %{}) do - alter_tag - |> cast(attrs, [:tag_id, :alter_id]) - |> foreign_key_constraint(:tag_id) - |> foreign_key_constraint(:alter_id) - |> unique_constraint([:tag_id, :alter_id]) - |> validate_required([:user_id, :tag_id, :alter_id]) - end -end diff --git a/lib/octocon/tags/tag.ex b/lib/octocon/tags/tag.ex deleted file mode 100644 index f20aaeb8..00000000 --- a/lib/octocon/tags/tag.ex +++ /dev/null @@ -1,55 +0,0 @@ -defmodule Octocon.Tags.Tag do - @moduledoc """ - A tag (folder) for categorizing alters. Each alter can be associated with multiple tags. It consists of: - - - A user ID (7-character alphanumeric lowercase string) - - A tag ID (UUID) - - A name (up to 100 characters) - - A color (a hexadecimal color code, e.g. `#ff0000`) - - A security level (enum, one of `:public`, `:friends_only`, `:trusted_only`, or `:private`) - - A list of alter IDs that are associated with this tag (virtual, populated by the data layer) - """ - use Ecto.Schema - import Ecto.Changeset - - @primary_key false - - schema "tags" do - field :user_id, :string, primary_key: true - field :id, Ecto.UUID, primary_key: true - - field :name, :string - field :description, :string - field :color, :string - - field :security_level, Ecto.Enum, - values: [public: 0, friends_only: 1, trusted_only: 2, private: 3], - default: :private - - field :parent_tag_id, Ecto.UUID - - field :alters, {:array, :integer}, virtual: true - - # belongs_to :user, Octocon.Accounts.User, - # foreign_key: :user_id, - # define_field: false - - # belongs_to :parent_tag, __MODULE__, - # foreign_key: :parent_tag_id, - # define_field: false - - timestamps() - end - - @doc """ - Builds a changeset based on the given `Octocon.Tags.Tag` struct and `attrs` to change. - """ - def changeset(tag, attrs) do - tag - |> cast(attrs, [:name, :color, :description, :security_level, :parent_tag_id]) - |> validate_length(:name, max: 100) - |> validate_length(:description, max: 1000) - |> validate_format(:color, ~r/^#[0-9a-fA-F]{6}$/) - |> validate_required([:name]) - end -end diff --git a/lib/octocon/user_registry_cache.ex b/lib/octocon/user_registry_cache.ex deleted file mode 100644 index 66373bad..00000000 --- a/lib/octocon/user_registry_cache.ex +++ /dev/null @@ -1,47 +0,0 @@ -defmodule Octocon.UserRegistryCache do - @moduledoc false - - import Cachex.Spec - - use Octocon.CachexChild, - name: __MODULE__, - hooks: [ - hook( - module: Cachex.Limit.Scheduled, - args: {20_000, [], [frequency: :timer.seconds(30)]} - ) - ] - - def get_region(system_identity) do - Cachex.fetch!( - __MODULE__, - system_identity, - &cache_function/1 - ) - end - - def invalidate(system_identity) do - user = Octocon.Accounts.get_user!(system_identity) - - Cachex.del(__MODULE__, {:system, user.id}) - - [ - {:discord, user.discord_id}, - {:apple, user.apple_id}, - {:google, user.google_id}, - {:email, user.email} - ] - |> Enum.filter(fn {_, id} -> id != nil end) - |> Enum.each(fn id -> Cachex.del(__MODULE__, id) end) - end - - def cache_function(system_identity) do - region = Octocon.Accounts.region_for_user(system_identity) - - if region == nil do - {:ignore, nil} - else - {:commit, region} - end - end -end diff --git a/lib/octocon/utils.ex b/lib/octocon/utils.ex deleted file mode 100644 index 23c6b20b..00000000 --- a/lib/octocon/utils.ex +++ /dev/null @@ -1,31 +0,0 @@ -defmodule Octocon.Utils do - alias ExAws.S3 - - def nuke_existing_avatars!(system_id, folder) do - bucket = Application.fetch_env!(:waffle, :bucket) - path = "uploads/avatars/#{system_id}/#{folder}/" - - objects = - S3.list_objects_v2(bucket, prefix: path) - |> ExAws.stream!() - |> Stream.map(fn object -> object.key end) - |> Enum.to_list() - - S3.delete_all_objects(bucket, objects) - |> ExAws.request!() - end - - def nuke_system_avatars!(system_id) do - bucket = Application.fetch_env!(:waffle, :bucket) - path = "uploads/avatars/#{system_id}/" - - objects = - S3.list_objects_v2(bucket, prefix: path) - |> ExAws.stream!() - |> Stream.map(fn object -> object.key end) - |> Enum.to_list() - - S3.delete_all_objects(bucket, objects) - |> ExAws.request!() - end -end diff --git a/lib/octocon/utils/alter.ex b/lib/octocon/utils/alter.ex deleted file mode 100644 index ab617804..00000000 --- a/lib/octocon/utils/alter.ex +++ /dev/null @@ -1,67 +0,0 @@ -defmodule Octocon.Utils.Alter do - @moduledoc """ - Utility functions for working with alters. - """ - - alias Octocon.{ - Accounts, - Alters, - Utils - } - - alias OctoconWeb.Uploaders.Avatar - - @doc """ - Uploads an avatar for the given alter to the Octocon CDN. - - ## Arguments - - - `system_identity` (tuple): The identity of the system to upload the avatar for. - - `alter_identity` (tuple): The identity of the alter to upload the avatar for. - - `url` (binary): The URL of the avatar to download and re-upload to the Octocon CDN. - """ - def upload_avatar(system_identity, alter_identity, url) do - random_id = Nanoid.generate(30) - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - avatar_scope = %{ - system_id: system_id, - alter_id: alter_id, - random_id: random_id - } - - Utils.nuke_existing_avatars!(system_id, alter_id) - - to_store = - if String.starts_with?(url, "http") do - url - else - # Convert file URL to an actual binary to be sent to the sidecar node. - - %{ - filename: "primary.webp", - binary: File.read!(url) - } - end - - result = - Octocon.ClusterUtils.run_on_sidecar(fn -> Avatar.store({to_store, avatar_scope}) end, - timeout: 10_000 - ) - - case result do - {:ok, _} -> - avatar_url = Avatar.url({"primary.webp", avatar_scope}, :primary) - - Alters.update_alter( - {:system, system_id}, - {:id, alter_id}, - %{avatar_url: avatar_url} - ) - - _ -> - {:error, :unknown} - end - end -end diff --git a/lib/octocon/utils/import.ex b/lib/octocon/utils/import.ex deleted file mode 100644 index 60f9d64a..00000000 --- a/lib/octocon/utils/import.ex +++ /dev/null @@ -1,184 +0,0 @@ -defmodule Octocon.Utils.Import do - alias Octocon.Alters.Alter - - alias Octocon.Tags.{ - AlterTag, - Tag - } - - def naive_datetime_to_datetime(naive_datetime) do - {:ok, datetime} = - Exandra.dumpers(:naive_datetime, nil) - |> List.first() - |> then(fn dumper -> dumper.(naive_datetime) end) - - datetime - end - - def alter_to_insert_query( - %Alter{ - user_id: user_id, - id: id, - name: name, - proxy_name: proxy_name, - discord_proxies: discord_proxies, - pronouns: pronouns, - description: description, - alias: aliaz, - pinned: pinned, - archived: archived, - untracked: untracked, - last_fronted: last_fronted, - color: color, - fields: fields, - security_level: security_level, - inserted_at: inserted_at, - updated_at: updated_at - }, - region - ) do - query = - "INSERT INTO #{region}.alters (user_id, id, name, proxy_name, discord_proxies, pronouns, description, alias, pinned, archived, untracked, last_fronted, color, fields, security_level, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - - parsed_fields = - Enum.map(fields, fn %{id: id, value: value} -> %{"id" => id, "value" => value} end) - - values = [ - user_id, - id, - name, - proxy_name, - discord_proxies, - pronouns, - description, - aliaz, - pinned, - archived, - untracked, - last_fronted, - color, - parsed_fields, - security_level, - inserted_at, - updated_at - ] - - {query, values} - end - - def tag_to_insert_query( - %Tag{ - user_id: user_id, - id: id, - name: name, - description: description, - color: color, - security_level: security_level, - parent_tag_id: parent_tag_id, - inserted_at: inserted_at, - updated_at: updated_at - }, - region - ) do - query = - "INSERT INTO #{region}.tags (user_id, id, name, description, color, security_level, parent_tag_id, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" - - values = [ - user_id, - id, - name, - description, - color, - security_level, - parent_tag_id, - inserted_at, - updated_at - ] - - {query, values} - end - - def alter_tag_to_insert_query( - %AlterTag{ - user_id: user_id, - tag_id: tag_id, - alter_id: alter_id, - inserted_at: inserted_at, - updated_at: updated_at - }, - region - ) do - query = - "INSERT INTO #{region}.alter_tags (user_id, alter_id, tag_id, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?)" - - values = [ - user_id, - alter_id, - tag_id, - inserted_at, - updated_at - ] - - {query, values} - end - - def front_to_insert_query( - %Octocon.Fronts.Front{ - id: id, - user_id: user_id, - alter_id: alter_id, - comment: comment, - time_start: time_start, - time_end: time_end - }, - region - ) do - query = - "INSERT INTO #{region}.fronts (id, user_id, alter_id, comment, time_start, time_end) VALUES (?, ?, ?, ?, ?, ?)" - - values = [ - id, - user_id, - alter_id, - comment, - time_start, - time_end - ] - - {query, values} - end - - def current_front_to_insert_query( - %Octocon.Fronts.CurrentFront{ - id: id, - user_id: user_id, - alter_id: alter_id, - comment: comment, - time_start: time_start - }, - region - ) do - query = - "INSERT INTO #{region}.current_fronts (id, user_id, alter_id, comment, time_start) VALUES (?, ?, ?, ?, ?)" - - values = [ - id, - user_id, - alter_id, - comment, - time_start - ] - - {query, values} - end - - def execute_batch(queries) do - queries - |> Enum.chunk_every(500) - |> Enum.each(fn chunk -> - batch = %Exandra.Batch{queries: chunk} - - :ok = Exandra.execute_batch(Octocon.Repo, batch, consistency: :one) - end) - end -end diff --git a/lib/octocon/utils/tailscale.ex b/lib/octocon/utils/tailscale.ex deleted file mode 100644 index aea42920..00000000 --- a/lib/octocon/utils/tailscale.ex +++ /dev/null @@ -1,31 +0,0 @@ -defmodule Octocon.Utils.Tailscale do - @moduledoc false - - @endpoint "api.tailscale.com/api/v2" - @tailnet "octocondev.org.github" - - def list_devices do - case get("/tailnet/#{@tailnet}/devices") do - {:ok, devices} -> - devices["devices"] - - _ -> - [] - end - end - - def get(path) do - authkey = Application.get_env(:octocon, :tailscale_api_authkey) - - case :httpc.request(:get, {~c"https://#{authkey}:@#{@endpoint}/#{path}", []}, [], []) do - {:ok, {{_version, 200, _status}, _headers, body}} -> - {:ok, Jason.decode!(body)} - - {:ok, {{_version, code, status}, _headers, body}} -> - {:warn, [code, status, body]} - - {:error, reason} -> - {:error, reason} - end - end -end diff --git a/lib/octocon/utils/user.ex b/lib/octocon/utils/user.ex deleted file mode 100644 index 02f8b136..00000000 --- a/lib/octocon/utils/user.ex +++ /dev/null @@ -1,58 +0,0 @@ -defmodule Octocon.Utils.User do - @moduledoc """ - Utility functions for working with users. - """ - - alias Octocon.{ - Accounts, - Utils - } - - alias OctoconWeb.Uploaders.UserAvatar - - @doc """ - Uploads an avatar for the given system to the Octocon CDN. - - ## Arguments - - - `system` (struct): The system to upload the avatar for. - - `url` (binary): The URL of the avatar to download and re-upload to the Octocon CDN. - """ - def upload_avatar(system, url) do - random_id = Nanoid.generate(30) - - avatar_scope = %{ - system_id: system.id, - random_id: random_id - } - - Utils.nuke_existing_avatars!(system.id, "self") - - to_store = - if String.starts_with?(url, "http") do - url - else - # Convert file URL to an actual binary to be sent to the sidecar node. - - %{ - filename: "primary.webp", - binary: File.read!(url) - } - end - - result = - Octocon.ClusterUtils.run_on_sidecar(fn -> UserAvatar.store({to_store, avatar_scope}) end, - timeout: 10_000 - ) - - case result do - {:ok, _} -> - avatar_url = UserAvatar.url({"primary.webp", avatar_scope}, :primary) - - Accounts.update_user(system, %{avatar_url: avatar_url}) - - _ -> - {:error, :unknown} - end - end -end diff --git a/lib/octocon/workers/plural_kit_import_worker.ex b/lib/octocon/workers/plural_kit_import_worker.ex deleted file mode 100644 index 2f5c0de3..00000000 --- a/lib/octocon/workers/plural_kit_import_worker.ex +++ /dev/null @@ -1,292 +0,0 @@ -defmodule Octocon.Workers.PluralKitImportWorker do - @moduledoc """ - An Oban worker that imports alters from PluralKit into a system with the given ID. - - ## Arguments - - - `system_id` (binary): The ID of the system to import alters into. - - `pk_token` (binary): The PluralKit API token to use for the request. - """ - - import Octocon.Utils.Import - - require Logger - - alias Octocon.{ - Accounts, - Alters, - Alters.Alter, - Tags.AlterTag, - Tags.Tag - } - - alias OctoconWeb.Uploaders.Avatar - - @pk_endpoint URI.parse("https://api.pluralkit.me/v2/") - - def perform(%{ - "system_id" => system_id, - "pk_token" => pk_token - }) do - Logger.info("Performing PluralKit import for user #{system_id}") - - user_region = Octocon.UserRegistryCache.get_region({:system, system_id}) - - user = Accounts.get_user!({:system, system_id}) - start_count = user.lifetime_alter_count + 1 - - {:ok, %{body: self_body}} = send_pk_request(:get, "/systems/@me", pk_token) - - %{ - "description" => description, - "tag" => system_tag - } = Jason.decode!(self_body) - - {alters, alter_associations, avatars} = - get_alters(pk_token, user_region, system_id, start_count) - - alter_count = length(alters) - - tags = get_tags(pk_token, user_region, system_id) - alter_tags = generate_alter_tags(tags, alter_associations, user_region, system_id) - - alters - |> Enum.map(fn {_alter, query} -> query end) - |> execute_batch() - - tags - |> Enum.map(fn {{_tag, query}, _members} -> query end) - |> execute_batch() - - alter_tags - |> execute_batch() - - Accounts.update_user( - user, - %{ - lifetime_alter_count: user.lifetime_alter_count + alter_count, - description: default_if_empty(description, 3000, user.description) - } - ) - - existing_system_tag = - (user.discord_settings || %Octocon.Accounts.DiscordSettings{}) - |> Map.get(:system_tag, nil) - - Accounts.update_discord_settings( - user, - %{ - system_tag: - default_if_empty( - system_tag, - 20, - existing_system_tag - ) - } - ) - - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "pk_import_complete", %{ - alter_count: alter_count - }) - - OctoconDiscord.Utils.send_dm( - {:system, system_id}, - "Import complete (PluralKit)", - "#{alter_count} alters have been successfully imported from PluralKit. They have been assigned IDs #{start_count} - #{start_count + alter_count - 1}. It may take a while longer for their avatars to be processed.\n\n**Note:** This process should only be completed once; doing it again will result in duplicate alters." - ) - - spawn(fn -> - system_identity = {:system, system_id} - OctoconDiscord.Cache.Proxy.invalidate(system_identity) - OctoconDiscord.Autocomplete.invalidate_all(system_identity) - end) - - spawn(fn -> - Task.async_stream( - avatars, - fn {avatar_url, avatar_scope} -> - case Octocon.ClusterUtils.run_on_sidecar( - fn -> Avatar.store({avatar_url, avatar_scope}) end, - timeout: 10_000 - ) do - {:ok, _} -> - octo_url = Avatar.url({"primary.webp", avatar_scope}, :primary) - - Alters.update_alter( - {:system, avatar_scope.system_id}, - {:id, avatar_scope.alter_id}, - %{avatar_url: octo_url} - ) - - _ -> - # Avatar doesn't exist; stale reference on PK's end? - :ok - end - end, - max_concurrency: 2, - ordered: false, - timeout: :timer.seconds(10), - on_timeout: :kill_task - ) - |> Stream.run() - end) - - :ok - rescue - e -> - Logger.error("Error importing PluralKit alters") - Logger.error(Exception.format(:error, e, __STACKTRACE__)) - - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "pk_import_failed", %{}) - - {:error, e} - end - - defp get_alters(pk_token, user_region, system_id, start_count) do - {:ok, %{body: alters_body}} = send_pk_request(:get, "/systems/@me/members", pk_token) - - {alters, uuids, avatars} = - Jason.decode!(alters_body) - |> Stream.with_index(start_count) - |> Stream.map(fn {alter, index} -> - parse_alter(system_id, alter, index) - end) - |> Stream.map(fn {alter, uuid, avatar} -> - {{alter, alter_to_insert_query(alter, user_region)}, uuid, avatar} - end) - |> Enum.reduce({[], [], []}, fn - {alter, uuid, nil}, {alters, uuids, avatars} -> - {[alter | alters], [uuid | uuids], avatars} - - {alter, uuid, avatar}, {alters, uuids, avatars} -> - {[alter | alters], [uuid | uuids], [avatar | avatars]} - end) - - alter_associations = - uuids - |> Enum.zip(Enum.map(alters, fn {alter, _query} -> alter.id end)) - |> Enum.into(%{}) - - {alters, alter_associations, avatars} - end - - defp get_tags(pk_token, user_region, system_id) do - {:ok, %{body: groups_body}} = - send_pk_request(:get, "/systems/@me/groups?with_members=true", pk_token) - - Jason.decode!(groups_body) - |> Stream.map(fn group -> parse_tag(system_id, group) end) - |> Enum.map(fn {tag, members} -> - { - {tag, tag_to_insert_query(tag, user_region)}, - members - } - end) - end - - defp generate_alter_tags(tags, alter_associations, user_region, system_id) do - tags - |> Enum.map(fn {{tag, _}, members} -> - { - tag.id, - Enum.map(members, fn member -> Map.get(alter_associations, member) end) - } - end) - |> Enum.map(fn {tag_id, member_ids} -> - member_ids - |> Enum.map(fn alter_id -> - %AlterTag{ - user_id: system_id, - alter_id: alter_id, - tag_id: tag_id, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - } - |> alter_tag_to_insert_query(user_region) - end) - end) - |> List.flatten() - end - - defp send_pk_request(method, endpoint, token) do - uri = URI.append_path(@pk_endpoint, endpoint) - - res = - Finch.build(method, uri, [ - {"Content-Type", "application/json"}, - {"User-Agent", "Octocon/pkimport; contact = contact@octocon.app"}, - {"Authorization", token} - ]) - |> Finch.request(Octocon.Finch) - - res - end - - defp parse_alter(system_id, alter, id) do - { - %Alter{ - user_id: system_id, - id: id, - name: default_if_empty(alter["name"], 80, "Unnamed alter"), - proxy_name: default_if_empty(alter["display_name"], 80), - discord_proxies: - if(alter["proxy_tags"], - do: - alter["proxy_tags"] - |> Enum.map(fn tag -> (tag["prefix"] || "") <> "text" <> (tag["suffix"] || "") end), - else: nil - ), - pronouns: default_if_empty(alter["pronouns"], 50), - description: default_if_empty(alter["description"], 2000), - alias: nil, - pinned: false, - archived: false, - untracked: false, - last_fronted: nil, - color: parse_color(alter["color"]), - fields: [], - security_level: 3, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - }, - alter["uuid"], - if alter["avatar_url"] do - random_id = Nanoid.generate(30) - - { - alter["avatar_url"], - %{ - system_id: system_id, - alter_id: id, - random_id: random_id - } - } - else - nil - end - } - end - - defp parse_tag(system_id, tag) do - {%Tag{ - user_id: system_id, - id: Ecto.UUID.generate(), - name: default_if_empty(tag["name"], 100, "Unnamed tag"), - description: default_if_empty(tag["description"], 1000), - color: parse_color(tag["color"]), - security_level: 3, - parent_tag_id: nil, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - }, tag["members"]} - end - - defp default_if_empty(string, max, default \\ nil) - - defp default_if_empty(nil, _max, default), do: default - defp default_if_empty(string, _max, default) when string == "", do: default - defp default_if_empty(string, max, _default), do: string |> String.slice(0..max) - - defp parse_color(nil), do: nil - defp parse_color(color), do: "#" <> color -end diff --git a/lib/octocon/workers/simply_plural_import_worker.ex b/lib/octocon/workers/simply_plural_import_worker.ex deleted file mode 100644 index 8b3df379..00000000 --- a/lib/octocon/workers/simply_plural_import_worker.ex +++ /dev/null @@ -1,454 +0,0 @@ -defmodule Octocon.Workers.SimplyPluralImportWorker do - @moduledoc """ - An Oban worker that imports alters from Simply Plural into a system with the given ID. - - ## Arguments - - - `system_id` (binary): The ID of the system to import alters into. - - `sp_token` (binary): The Simply Plural API token to use for the request. - """ - - import Octocon.Utils.Import - - require Logger - - alias Octocon.{ - Accounts, - Alters, - Alters.Alter, - Fronts.CurrentFront, - Fronts.Front, - Tags.AlterTag, - Tags.Tag - } - - alias OctoconWeb.Uploaders.Avatar - - @sp_endpoint URI.parse("https://api.apparyllis.com/v1/") - @cdn_endpoint URI.parse("https://spaces.apparyllis.com/") - - @start_epoch 1_420_070_400_000 - - def perform(%{"system_id" => system_id, "sp_token" => sp_token}) do - Logger.info("Performing Simply Plural import for user #{system_id}") - - %{ - "id" => id, - "content" => %{ - "desc" => description - } - } = get_system_data(sp_token) - - user_region = Octocon.UserRegistryCache.get_region({:system, system_id}) - - user = Accounts.get_user!({:system, system_id}) - start_count = user.lifetime_alter_count + 1 - - {custom_fields, field_associations} = get_custom_fields(id, sp_token) - - {alters, alter_associations, avatars} = - get_alters(id, sp_token, user_region, system_id, start_count, field_associations) - - alter_count = length(alters) - - tags = get_tags(id, sp_token, user_region, system_id) - alter_tags = generate_alter_tags(tags, alter_associations, user_region, system_id) - - {fronts, current_fronts} = - get_fronts(id, sp_token, user_region, system_id, alter_associations) - - alters - |> Enum.map(fn {_alter, query} -> query end) - |> execute_batch() - - tags - |> Enum.map(fn {{_tag, query}, _members} -> query end) - |> execute_batch() - - alter_tags - |> execute_batch() - - fronts - |> Enum.map(fn {_front, query} -> query end) - |> execute_batch() - - current_fronts - |> Enum.map(fn {_current_front, query} -> query end) - |> execute_batch() - - Accounts.update_user( - user, - %{ - lifetime_alter_count: user.lifetime_alter_count + alter_count, - description: default_if_empty(description, 3000, user.description) - } - ) - - Accounts.add_bulk_fields( - {:system, system_id}, - Enum.map(custom_fields, fn {_field_id, field} -> field end) - ) - - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "sp_import_complete", %{ - alter_count: alter_count - }) - - OctoconDiscord.Utils.send_dm( - {:system, system_id}, - "Import complete (Simply Plural)", - "#{alter_count} alters have been successfully imported from Simply Plural. They have been assigned IDs #{start_count} - #{start_count + alter_count - 1}.\n\n**Note:** This process should only be completed once; doing it again will result in duplicate alters." - ) - - spawn(fn -> - system_identity = {:system, system_id} - OctoconDiscord.Cache.Proxy.invalidate(system_identity) - OctoconDiscord.Autocomplete.invalidate_all(system_identity) - end) - - spawn(fn -> - Task.async_stream( - avatars, - fn {avatar_url, avatar_scope} -> - case Octocon.ClusterUtils.run_on_sidecar( - fn -> Avatar.store({avatar_url, avatar_scope}) end, - timeout: 10_000 - ) do - {:ok, _} -> - octo_url = Avatar.url({"primary.webp", avatar_scope}, :primary) - - Alters.update_alter( - {:system, avatar_scope.system_id}, - {:id, avatar_scope.alter_id}, - %{avatar_url: octo_url} - ) - - _ -> - # Avatar doesn't exist; stale reference on SP's end? - :ok - end - end, - # NOTE: Potentially replace with schedulers_online on a beefier server? - max_concurrency: 2, - ordered: false, - timeout: :timer.seconds(10), - on_timeout: :kill_task - ) - |> Stream.run() - end) - - :ok - rescue - e -> - Logger.error("Error importing Simply Plural alters") - Logger.error(Exception.format(:error, e, __STACKTRACE__)) - - OctoconWeb.Endpoint.broadcast!("system:#{system_id}", "sp_import_failed", %{}) - - {:error, e} - end - - defp get_system_data(token) do - {:ok, %{body: body}} = send_sp_request(:get, "/me", token) - Jason.decode!(body) - end - - defp get_custom_fields(id, sp_token) do - {:ok, %{body: custom_fields_body}} = send_sp_request(:get, "/customFields/#{id}", sp_token) - - custom_fields = - Jason.decode!(custom_fields_body) - |> Enum.map(fn field -> - %{ - "id" => field_id, - "content" => %{ - "name" => name - } - } = field - - {field_id, - %Octocon.Accounts.Field{ - id: Ecto.UUID.generate(), - name: name |> default_if_empty(100, "Unnamed field"), - type: :text, - locked: false, - security_level: :private - }} - end) - - field_associations = - custom_fields - |> Enum.map(fn {field_id, field} -> {field_id, field.id} end) - |> Enum.into(%{}) - - {custom_fields, field_associations} - end - - defp get_alters(id, sp_token, user_region, system_id, start_count, field_associations) do - {:ok, %{body: custom_fronts_body}} = send_sp_request(:get, "/customFronts/#{id}", sp_token) - {:ok, %{body: alters_body}} = send_sp_request(:get, "/members/#{id}", sp_token) - - {alters, uuids, avatars} = - ((Jason.decode!(alters_body) |> Enum.map(fn alter -> Map.put(alter, "type", :alter) end)) ++ - (Jason.decode!(custom_fronts_body) - |> Enum.map(fn cf -> Map.put(cf, "type", :custom_front) end))) - |> Stream.map(&{&1["content"], &1["id"], &1["type"]}) - |> Stream.with_index(start_count) - |> Stream.map(fn {{alter, uuid, type}, index} -> - {alter, avatar} = parse_alter(system_id, alter, type, index, field_associations) - {alter, uuid, avatar} - end) - |> Stream.map(fn {alter, uuid, avatar} -> - {{alter, alter_to_insert_query(alter, user_region)}, uuid, avatar} - end) - |> Enum.reduce({[], [], []}, fn - {alter, uuid, nil}, {alters, uuids, avatars} -> - {[alter | alters], [uuid | uuids], avatars} - - {alter, uuid, avatar}, {alters, uuids, avatars} -> - {[alter | alters], [uuid | uuids], [avatar | avatars]} - end) - - alter_associations = - uuids - |> Enum.zip(Enum.map(alters, fn {alter, _query} -> alter.id end)) - |> Enum.into(%{}) - - {alters, alter_associations, avatars} - end - - defp get_tags(id, sp_token, user_region, system_id) do - {:ok, %{body: groups_body}} = send_sp_request(:get, "/groups/#{id}", sp_token) - tags_response = Jason.decode!(groups_body) - - tag_ids = - tags_response - |> Enum.map(fn tag -> {tag["id"], Ecto.UUID.generate()} end) - |> Enum.into(%{}) - - tags_response - |> Stream.map(fn tag -> - parse_tag(system_id, tag["content"], Map.get(tag_ids, tag["id"]), tag_ids) - end) - |> Enum.map(fn {tag, members} -> - { - {tag, tag_to_insert_query(tag, user_region)}, - members - } - end) - end - - defp generate_alter_tags(tags, alter_associations, user_region, system_id) do - tags - |> Enum.map(fn {{tag, _}, members} -> - { - tag.id, - Enum.map(members, fn member -> Map.get(alter_associations, member) end) - } - end) - |> Enum.map(fn {tag_id, member_ids} -> - Enum.map(member_ids, fn alter_id -> - %AlterTag{ - user_id: system_id, - alter_id: alter_id, - tag_id: tag_id, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - } - |> alter_tag_to_insert_query(user_region) - end) - end) - |> List.flatten() - end - - defp get_fronts(id, sp_token, user_region, system_id, alter_associations) do - import_month_interval = Application.get_env(:octocon, :sp_import_fronts_month_interval, 6) - end_time = DateTime.utc_now() |> DateTime.to_unix(:millisecond) - chunk_size = import_month_interval * 30 * 24 * 60 * 60 * 1000 - - number_of_requests = (end_time - @start_epoch) |> div(chunk_size) |> ceil() - - fronts = - 0..number_of_requests - |> Enum.flat_map(fn i -> - start_time = @start_epoch + i * chunk_size - end_time = min(@start_epoch + (i + 1) * chunk_size, end_time) - - {:ok, %{body: fronts_body}} = - send_sp_request( - :get, - "/frontHistory/#{id}?startTime=#{start_time}&endTime=#{end_time}", - sp_token - ) - - result = Jason.decode!(fronts_body) - - Process.sleep(200) - - result - end) - |> Enum.uniq_by(fn front -> front["id"] end) - |> Enum.map(fn %{ - "content" => - %{ - "member" => member_id, - "startTime" => start_time, - "endTime" => end_time - } = front - } -> - %Front{ - id: Ecto.UUID.generate(), - user_id: system_id, - alter_id: Map.get(alter_associations, member_id), - time_start: - DateTime.from_unix!(start_time, :millisecond) - |> DateTime.to_naive() - |> naive_datetime_to_datetime(), - time_end: - DateTime.from_unix!(end_time, :millisecond) - |> DateTime.to_naive() - |> naive_datetime_to_datetime(), - comment: default_if_empty(front["customStatus"], 50) - } - end) - |> Enum.filter(fn fronts -> fronts.alter_id != nil end) - - {:ok, %{body: fronters_body}} = send_sp_request(:get, "/fronters/", sp_token) - - {fronts, current_fronts} = - fronters_body - |> Jason.decode!() - |> Enum.map(fn %{"content" => %{"member" => member_id, "startTime" => start_time} = front} -> - id = Ecto.UUID.generate() - alter_id = Map.get(alter_associations, member_id) - comment = default_if_empty(front["customStatus"], 50) - - time_start = - DateTime.from_unix!(start_time, :millisecond) - |> DateTime.to_naive() - |> naive_datetime_to_datetime() - - { - %Front{ - id: id, - user_id: system_id, - alter_id: alter_id, - time_start: time_start, - comment: comment, - time_end: nil - }, - %CurrentFront{ - id: id, - user_id: system_id, - alter_id: alter_id, - time_start: time_start, - comment: comment - } - } - end) - |> Enum.filter(fn {front, _} -> front.alter_id != nil end) - |> Enum.reduce({fronts, []}, fn - {front, current_front}, {fronts, current_fronts} -> - {[front | fronts], [current_front | current_fronts]} - end) - - { - Enum.map(fronts, &{&1, front_to_insert_query(&1, user_region)}), - Enum.map(current_fronts, &{&1, current_front_to_insert_query(&1, user_region)}) - } - end - - defp send_sp_request(method, endpoint, token) do - uri = URI.append_path(@sp_endpoint, endpoint) - - res = - Finch.build(method, uri, [ - {"User-Agent", "Octocon/spimport; contact = contact@octocon.app"}, - {"Authorization", token} - ]) - |> Finch.request(Octocon.Finch) - - res - end - - defp parse_alter(system_id, alter, type, id, field_associations) do - { - %Alter{ - user_id: system_id, - id: id, - name: default_if_empty(alter["name"], 80, "Unnamed alter"), - pronouns: default_if_empty(alter["pronouns"], 50), - description: default_if_empty(alter["desc"], 3000), - color: parse_color(alter["color"]), - alias: nil, - pinned: false, - archived: false, - untracked: type == :custom_front, - last_fronted: nil, - fields: - (alter["info"] || - []) - |> Enum.map(fn {field_id, value} -> - octocon_field_id = Map.get(field_associations, field_id) - - %Octocon.Alters.Field{ - id: octocon_field_id, - value: value - } - end) - |> Enum.filter(fn field -> field.id != nil end), - security_level: 3, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - }, - if alter["avatarUuid"] != nil and String.length(alter["avatarUuid"]) != 0 do - random_id = Nanoid.generate(30) - - avatar_url = - @cdn_endpoint - |> URI.merge("/avatars/#{alter["uid"]}/#{alter["avatarUuid"]}") - |> URI.to_string() - - { - avatar_url, - %{ - system_id: system_id, - alter_id: id, - random_id: random_id - } - } - else - nil - end - } - end - - defp parse_tag(system_id, tag, tag_id, tag_ids) do - {%Tag{ - user_id: system_id, - id: tag_id, - name: default_if_empty(tag["name"], 100, "Unnamed tag"), - description: default_if_empty(tag["desc"], 1000), - color: parse_color(tag["color"]), - security_level: 3, - parent_tag_id: - case tag["parent"] do - nil -> nil - "root" -> nil - parent_id -> Map.get(tag_ids, parent_id) - end, - inserted_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime(), - updated_at: NaiveDateTime.utc_now(:second) |> naive_datetime_to_datetime() - }, tag["members"]} - end - - defp default_if_empty(string, max, default \\ nil) - - defp default_if_empty(nil, _max, default), do: default - defp default_if_empty(string, _max, default) when string == "", do: default - defp default_if_empty(string, max, _default), do: string |> String.slice(0..max) - - defp parse_color("#" <> _ = color) when byte_size(color) == 7, do: color - defp parse_color(color) when byte_size(color) == 6, do: color - defp parse_color(color) when color == "", do: nil - defp parse_color(_color), do: nil -end diff --git a/lib/octocon_discord.ex b/lib/octocon_discord.ex deleted file mode 100644 index ff3b6751..00000000 --- a/lib/octocon_discord.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule OctoconDiscord do - @moduledoc false - - def get_desired_shards do - Nostrum.Util.gateway() |> elem(1) - end -end diff --git a/lib/octocon_discord/autocomplete.ex b/lib/octocon_discord/autocomplete.ex deleted file mode 100644 index 1ed8f696..00000000 --- a/lib/octocon_discord/autocomplete.ex +++ /dev/null @@ -1,238 +0,0 @@ -defmodule OctoconDiscord.Autocomplete do - @moduledoc false - - use Supervisor - - require Logger - - @cache_associations %{ - "alter" => OctoconDiscord.Autocomplete.Alter, - "tag" => OctoconDiscord.Autocomplete.Tag, - "friend" => OctoconDiscord.Autocomplete.Friend, - "request" => OctoconDiscord.Autocomplete.FriendRequest, - "front" => OctoconDiscord.Autocomplete.Front - } - - def start_link(_), do: Supervisor.start_link(__MODULE__, [], name: __MODULE__) - - def init([]) do - children = Map.values(@cache_associations) - - Supervisor.init(children, strategy: :one_for_one) - end - - defmacro __using__(_opts) do - quote do - import Cachex.Spec - - use Octocon.CachexChild, - name: __MODULE__, - hooks: [ - hook( - module: Cachex.Limit.Scheduled, - args: {2000, [], [frequency: :timer.seconds(30)]} - ) - ], - expiration: expiration(default: :timer.minutes(5)) - - require Logger - - import OctoconDiscord.Autocomplete, - only: [format_name_for_search: 1, generate_autocomplete_responses: 2, top_percent: 2] - - @timeout :timer.seconds(5) - - def get_autocomplete_responses(discord_id, prefix) - when (is_binary(discord_id) or is_tuple(discord_id)) and is_binary(prefix) and - byte_size(prefix) <= 20 do - Task.async(fn -> do_fetch(discord_id, prefix) end) - |> Task.await(@timeout) - end - - defp do_fetch(key, prefix) do - index = - Cachex.fetch!( - __MODULE__, - key, - OctoconDiscord.Autocomplete.wrap_cache_function(__MODULE__, is_tuple(key)) - ) - - if index == nil do - [] - else - generate_autocomplete_responses(index, prefix) - end - end - - def invalidate(system_identity, supplementary \\ nil) - - def invalidate({:discord, discord_id}, nil) do - delete_cache_key(to_string(discord_id)) - end - - def invalidate({:discord, discord_id}, supplementary) do - delete_cache_key({to_string(discord_id), supplementary}) - end - - def invalidate(system_identity, supplementary) do - case Octocon.Accounts.get_user(system_identity) do - %{discord_id: discord_id} when discord_id != nil -> - invalidate({:discord, discord_id}, supplementary) - - _ -> - {:ok, true} - end - end - - defp delete_cache_key(key) do - Octocon.ClusterUtils.run_on_all_primary_nodes(fn -> - Cachex.del(__MODULE__, key) - end) - end - end - end - - def generate_autocomplete_responses(search_index, prefix, id_type \\ :string) - - def generate_autocomplete_responses(%{ids: ids}, "", id_type) do - ids - |> Map.keys() - |> Enum.sort_by(fn {_, name} -> String.downcase(name) end) - |> Enum.take(10) - |> Enum.map(fn {id, display_name} -> - format_discord_result(display_name, id, id_type) - end) - end - - def generate_autocomplete_responses(search_index, prefix, id_type) - when is_binary(prefix) do - prefix = String.slice(prefix, 0..20) - - {time, result} = - :timer.tc(fn -> - search_index - # |> Radix.more(format_name_for_search(prefix)) - |> Search.search(prefix, prefix?: true, fuzzy?: true) - # |> top_percent(50) - |> Enum.take(10) - |> Enum.map(fn %{id: {id, display_name}} -> - format_discord_result(display_name, id, id_type) - end) - end) - - Logger.info("Autocomplete search took #{time} µs") - - result - rescue - e -> - Logger.error("Error generating autocomplete responses:") - Logger.error(Exception.format(:error, e, __STACKTRACE__)) - - Logger.info("Search index: #{inspect(search_index)}") - Logger.info("Prefix: #{prefix}") - - [] - end - - defp format_discord_result(display_name, id, id_type) do - value = - case id_type do - :string -> to_string(id) - :integer -> id - end - - %{ - name: display_name, - value: value - } - end - - def wrap_cache_function(cache_module, false) do - fn key -> - case Octocon.Accounts.get_user({:discord, key}) do - nil -> - {:ignore, nil} - - user -> - cache_module.cache_function(user) - end - end - end - - def wrap_cache_function(cache_module, true) do - fn key -> - {discord_id, supplementary_key} = key - - case Octocon.Accounts.get_user({:discord, discord_id}) do - nil -> - {:ignore, nil} - - user -> - cache_module.cache_function(user, supplementary_key) - end - end - end - - def format_name_for_search(name) do - name - |> String.trim() - |> String.downcase() - end - - def dispatch(%{user: %{id: discord_id}, data: %{name: command, options: options}} = interaction) do - focused_option = - Enum.find(flatten_leaf_options(options), fn opt -> Map.get(opt, :focused, false) end) - - command = - if command == "friend" do - case options do - [%{name: "request"} | _] -> "request" - _ -> command - end - else - command - end - - @cache_associations[command].handle_interaction( - to_string(discord_id), - focused_option, - interaction - ) - rescue - e -> - Logger.error("Error handling autocomplete interaction: #{inspect(e)}") - [] - end - - def invalidate_all(system_identity) do - discord_id = Octocon.Accounts.id_from_system_identity(system_identity, :discord) - - if discord_id != nil do - Map.values(@cache_associations) - |> Enum.each(fn cache -> cache.invalidate(system_identity) end) - end - - :ok - end - - defp flatten_leaf_options(options) do - Enum.flat_map(options, fn - %{options: nil} = option -> - [option] - - %{options: children} -> - flatten_leaf_options(children) - end) - end - - def top_percent(list, percentage) do - count = - list - |> length() - |> Kernel.*(percentage / 100) - |> Float.ceil() - |> trunc() - - Enum.take(list, count) - end -end diff --git a/lib/octocon_discord/autocomplete/alter.ex b/lib/octocon_discord/autocomplete/alter.ex deleted file mode 100644 index 2b63bb90..00000000 --- a/lib/octocon_discord/autocomplete/alter.ex +++ /dev/null @@ -1,53 +0,0 @@ -defmodule OctoconDiscord.Autocomplete.Alter do - @moduledoc false - - use OctoconDiscord.Autocomplete - - require Logger - - def cache_function(user) do - alters = - Octocon.Alters.get_alters_by_id({:system, user.id}, [:id, :name, :alias]) - |> Enum.map(fn %{id: id, name: name, alias: aliaz} -> - id_suffix = " (#{id})" - remaining_length = 100 - byte_size(id_suffix) - - display_name = - ((name || "Unnamed alter") - |> String.trim() - |> String.slice(0..remaining_length)) <> id_suffix - - res = %{id: {id, display_name}, name: format_name_for_search(display_name), alter_id: id} - - if aliaz == nil do - res - else - res |> Map.put(:alias, aliaz) - end - end) - - case alters do - [] -> - {:ignore, nil} - - _ -> - {time, index} = - :timer.tc(fn -> - Search.new(fields: [:name, :alter_id, :alias]) |> Search.add!(alters) - end) - - Logger.info("Built alter autocomplete index for user #{user.id} in #{time} µs") - {:commit, index} - end - end - - def handle_interaction(discord_id, focused_option, _interaction) do - case focused_option do - %{name: "id", value: prefix} -> - get_autocomplete_responses(discord_id, prefix) - - _ -> - [] - end - end -end diff --git a/lib/octocon_discord/autocomplete/friend.ex b/lib/octocon_discord/autocomplete/friend.ex deleted file mode 100644 index 64fed303..00000000 --- a/lib/octocon_discord/autocomplete/friend.ex +++ /dev/null @@ -1,43 +0,0 @@ -defmodule OctoconDiscord.Autocomplete.Friend do - @moduledoc false - - use OctoconDiscord.Autocomplete - - def cache_function(user) do - friends = - Octocon.Friendships.list_friendships({:system, user.id}) - |> Enum.map(fn %{friend: %{id: friend_id, username: username}} -> - display_name = - if username == nil do - friend_id - else - id_suffix = " (#{friend_id})" - remaining_length = 100 - byte_size(id_suffix) - - (username - |> String.trim() - |> String.slice(0..remaining_length)) <> id_suffix - end - - %{id: {friend_id, display_name}, name: format_name_for_search(display_name)} - end) - - case friends do - [] -> - {:ignore, nil} - - _ -> - {:commit, Search.new(fields: [:name]) |> Search.add!(friends)} - end - end - - def handle_interaction(discord_id, focused_option, _interaction) do - case focused_option do - %{name: "system-id", value: prefix} -> - get_autocomplete_responses(discord_id, prefix) - - _ -> - [] - end - end -end diff --git a/lib/octocon_discord/autocomplete/friend_request.ex b/lib/octocon_discord/autocomplete/friend_request.ex deleted file mode 100644 index 26e1a23b..00000000 --- a/lib/octocon_discord/autocomplete/friend_request.ex +++ /dev/null @@ -1,78 +0,0 @@ -defmodule OctoconDiscord.Autocomplete.FriendRequest do - @moduledoc false - - use OctoconDiscord.Autocomplete - - def cache_function(user, :incoming) do - requests = - Octocon.Friendships.incoming_friend_requests({:system, user.id}) - |> Enum.map(fn %{from: %{id: friend_id, username: username}} -> - display_name = - if username == nil do - friend_id - else - id_suffix = " (#{friend_id})" - remaining_length = 100 - byte_size(id_suffix) - - (username - |> String.trim() - |> String.slice(0..remaining_length)) <> id_suffix - end - - %{id: {friend_id, display_name}, name: format_name_for_search(display_name)} - end) - - case requests do - [] -> - {:ignore, nil} - - _ -> - {:commit, Search.new(fields: [:name]) |> Search.add!(requests)} - end - end - - def cache_function(user, :outgoing) do - requests = - Octocon.Friendships.outgoing_friend_requests({:system, user.id}) - |> Enum.map(fn %{to: %{id: friend_id, username: username}} -> - display_name = - if username == nil do - friend_id - else - id_suffix = " (#{friend_id})" - remaining_length = 100 - byte_size(id_suffix) - - (username - |> String.trim() - |> String.slice(0..remaining_length)) <> id_suffix - end - - %{id: {friend_id, display_name}, name: format_name_for_search(display_name)} - end) - - case requests do - [] -> - {:ignore, nil} - - _ -> - {:commit, Search.new(fields: [:name]) |> Search.add!(requests)} - end - end - - def handle_interaction(discord_id, focused_option, %{data: %{options: options}}) do - incoming_commands = ~w[accept reject] - - [%{options: [%{name: command_name} | []]}] = options - - case focused_option do - %{name: "system-id", value: prefix} -> - is_incoming = command_name in incoming_commands - supplementary_key = if is_incoming, do: :incoming, else: :outgoing - - get_autocomplete_responses({discord_id, supplementary_key}, prefix) - - _ -> - [] - end - end -end diff --git a/lib/octocon_discord/autocomplete/front.ex b/lib/octocon_discord/autocomplete/front.ex deleted file mode 100644 index c75f7d9b..00000000 --- a/lib/octocon_discord/autocomplete/front.ex +++ /dev/null @@ -1,55 +0,0 @@ -defmodule OctoconDiscord.Autocomplete.Front do - @moduledoc false - - use OctoconDiscord.Autocomplete - - def cache_function(user) do - fronting = - Octocon.Fronts.currently_fronting({:system, user.id}) - |> Enum.map(fn %{alter: %{id: id, name: name}} -> - id_suffix = " (#{id})" - remaining_length = 100 - byte_size(id_suffix) - - display_name = - ((name || "Unnamed alter") - |> String.trim() - |> String.slice(0..remaining_length)) <> id_suffix - - %{id: {id, display_name}, name: format_name_for_search(display_name), alter_id: id} - end) - - case fronting do - [] -> - {:ignore, nil} - - _ -> - {:commit, Search.new(fields: [:name, :alter_id]) |> Search.add!(fronting)} - end - end - - def handle_interaction(discord_id, focused_option, %{data: %{options: [%{name: command} | _]}}) do - autocomplete_provider = - if command in ["add", "set"] do - fn prefix -> - currently_fronting = - get_autocomplete_responses(discord_id, "") |> Enum.map(& &1.value) - - OctoconDiscord.Autocomplete.Alter.get_autocomplete_responses( - discord_id, - prefix - ) - |> Enum.filter(fn %{value: alter_id} -> alter_id not in currently_fronting end) - end - else - fn prefix -> get_autocomplete_responses(discord_id, prefix) end - end - - case focused_option do - %{name: "id", value: prefix} -> - autocomplete_provider.(prefix) - - _ -> - [] - end - end -end diff --git a/lib/octocon_discord/autocomplete/tag.ex b/lib/octocon_discord/autocomplete/tag.ex deleted file mode 100644 index 32f03df7..00000000 --- a/lib/octocon_discord/autocomplete/tag.ex +++ /dev/null @@ -1,41 +0,0 @@ -defmodule OctoconDiscord.Autocomplete.Tag do - @moduledoc false - - use OctoconDiscord.Autocomplete - - def cache_function(user) do - tags = - Octocon.Tags.get_tags({:system, user.id}) - |> Enum.map(fn %{id: id, name: name} -> - # [TODO]: Handle duplicates - - display_name = - name - |> String.trim() - |> String.slice(0..100) - - %{id: {id, display_name}, name: format_name_for_search(display_name)} - end) - - case tags do - [] -> - {:ignore, nil} - - _ -> - {:commit, Search.new(fields: [:name]) |> Search.add!(tags)} - end - end - - def handle_interaction(discord_id, focused_option, _interaction) do - case focused_option do - %{name: name, value: prefix} when name in ["tag", "parent_tag"] -> - get_autocomplete_responses(discord_id, prefix) - - %{name: "alter", value: prefix} -> - OctoconDiscord.Autocomplete.Alter.get_autocomplete_responses(discord_id, prefix) - - _ -> - [] - end - end -end diff --git a/lib/octocon_discord/bot_supervisor.ex b/lib/octocon_discord/bot_supervisor.ex deleted file mode 100644 index 6013cba8..00000000 --- a/lib/octocon_discord/bot_supervisor.ex +++ /dev/null @@ -1,57 +0,0 @@ -defmodule OctoconDiscord.BotSupervisor do - use Supervisor - - require Logger - - @via {:via, Horde.Registry, {Octocon.Primary.HordeRegistry, __MODULE__}} - - def start_link(_init_arg) do - case Supervisor.start_link(__MODULE__, [], name: @via) do - {:ok, pid} -> - {:ok, pid} - - {:error, {:already_started, pid}} -> - Logger.warning( - "OctoconDiscord.BotSupervisor already started at #{inspect(pid)}, returning :ignore" - ) - - :ignore - end - end - - def init([]) do - children = [ - {Nostrum.Bot, bot_options()} - ] - - Supervisor.init(children, strategy: :one_for_one) - end - - def get_bot_pid do - case Horde.Registry.lookup(Octocon.Primary.HordeRegistry, __MODULE__) do - [] -> - :error - - [{pid, _}] -> - [{_, bot_pid, _, _}] = Supervisor.which_children(pid) - {:ok, bot_pid} - end - end - - def bot_options do - %{ - name: OctoconDiscord.Bot, - consumer: OctoconDiscord.Consumer, - intents: [ - :guild_webhooks, - :guilds, - :guild_messages, - :guild_message_reactions, - :direct_messages, - :direct_message_reactions, - :message_content - ], - wrapped_token: fn -> Application.get_env(:octocon, :discord_token) end - } - end -end diff --git a/lib/octocon_discord/cache.ex b/lib/octocon_discord/cache.ex deleted file mode 100644 index 3ae9e25e..00000000 --- a/lib/octocon_discord/cache.ex +++ /dev/null @@ -1,16 +0,0 @@ -defmodule OctoconDiscord.Cache do - use Supervisor - - @caches [ - __MODULE__.ChannelBlacklists, - __MODULE__.Proxy, - __MODULE__.ServerSettings, - __MODULE__.Webhooks - ] - - def start_link(_), do: Supervisor.start_link(__MODULE__, [], name: __MODULE__) - - def init([]) do - Supervisor.init(@caches, strategy: :one_for_one) - end -end diff --git a/lib/octocon_discord/cache/channel_blacklists.ex b/lib/octocon_discord/cache/channel_blacklists.ex deleted file mode 100644 index 9a3d5e60..00000000 --- a/lib/octocon_discord/cache/channel_blacklists.ex +++ /dev/null @@ -1,106 +0,0 @@ -defmodule OctoconDiscord.Cache.ChannelBlacklists do - @doc """ - Manages the channel blacklist. - - [TODO]: Rework this to use the database instead of holding everything in ETS if memory pressure becomes an issue. - """ - alias Octocon.ChannelBlacklists - alias Octocon.ChannelBlacklists.ChannelBlacklist - use GenServer - require Logger - - # Client - - @doc false - def start_link(_init_arg) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - @doc """ - Adds a channel to the blacklist. - """ - def add(guild_id, channel_id) when is_binary(guild_id) and is_binary(channel_id) do - if :ets.lookup(__MODULE__, channel_id) != [] do - {:error, :already_blacklisted} - else - :ets.insert(__MODULE__, {channel_id, []}) - - ChannelBlacklists.create_channel_blacklist(%{guild_id: guild_id, channel_id: channel_id}) - - :ok - end - end - - @doc """ - Removes a channel from the blacklist. - """ - def remove(channel_id) when is_binary(channel_id) do - if :ets.lookup(__MODULE__, channel_id) == [] do - {:error, :not_blacklisted} - else - :ets.delete(__MODULE__, channel_id) - ChannelBlacklists.delete_channel_blacklist(%ChannelBlacklist{channel_id: channel_id}) - - :ok - end - end - - @doc """ - Checks if a channel is blacklisted. - """ - def blacklisted?(channel_id, parent_id) - - def blacklisted?(channel_id, nil) when is_binary(channel_id) do - :ets.lookup(__MODULE__, channel_id) != [] - end - - def blacklisted?(channel_id, parent_id) - when is_binary(channel_id) and is_binary(parent_id) do - :ets.lookup(__MODULE__, channel_id) != [] or :ets.lookup(__MODULE__, parent_id) != [] - end - - @doc """ - Gets all blacklisted channels for a guild. - """ - def get_all_for_guild(guild_id) when is_binary(guild_id) do - ChannelBlacklists.list_channel_blacklists_by_guild(guild_id) - end - - # Server - - @doc false - @impl GenServer - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: true, - decentralized_counters: true - ]) - - {:ok, [], {:continue, :load_blacklists}} - end - - @impl GenServer - def handle_continue(:load_blacklists, state) do - # [TODO]: Replace this with actual waiting - Process.sleep(:timer.seconds(5)) - - channels = ChannelBlacklists.list_channel_blacklists_bare() - - :ets.insert( - __MODULE__, - channels - |> Enum.map(fn channel_id -> {channel_id, []} end) - ) - - {:noreply, state} - end - - @impl GenServer - def handle_info(_, state) do - {:noreply, state} - end -end diff --git a/lib/octocon_discord/cache/proxy.ex b/lib/octocon_discord/cache/proxy.ex deleted file mode 100644 index 4a7f960e..00000000 --- a/lib/octocon_discord/cache/proxy.ex +++ /dev/null @@ -1,216 +0,0 @@ -defmodule OctoconDiscord.Cache.Proxy do - use GenServer - require Logger - - alias Octocon.{ - Accounts, - ClusterUtils - } - - # Client - - def start_link(_init_arg) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def get(discord_id, require_proxies \\ true) when is_binary(discord_id) do - unless Octocon.RPC.NodeTracker.primary?() do - raise "Cache.Proxy should only be called on the primary region" - end - - result = lookup_local(discord_id) - - cond do - result == nil -> - get_persisted_data(discord_id, require_proxies) - - result == :no_account -> - Logger.debug("Memory cache: got gravestone for user #{discord_id}") - {:error, :no_user} - - require_proxies && result.proxies == nil -> - Logger.debug("Proxies evicted for #{discord_id}, revalidating") - - Process.send_after(__MODULE__, {:evict_proxies, discord_id}, :timer.minutes(5)) - - data = %{result | proxies: build_existing_proxies(discord_id)} - insert(discord_id, data) - - {:ok, data} - - true -> - {:ok, result} - end - end - - defp lookup_local(discord_id) when is_binary(discord_id) do - case :ets.lookup(__MODULE__, discord_id) do - [] -> - Logger.debug("Memory cache miss for #{discord_id}") - nil - - [{_, data}] -> - Logger.debug("Memory cache hit for #{discord_id}") - data - end - end - - defp get_persisted_data(discord_id, include_proxies) do - system_id = Accounts.id_from_system_identity({:discord, discord_id}, :system) - - if system_id == nil do - Logger.debug("No system identity found for #{discord_id}, persisting gravestone") - - insert(discord_id, :no_account) - - {:error, :no_user} - else - if include_proxies do - Process.send_after(__MODULE__, {:evict_proxies, discord_id}, :timer.minutes(5)) - end - - %{discord_settings: settings, primary_front: primary_front} = - Octocon.Accounts.get_proxy_cache_data({:discord, discord_id}) - - settings = settings || %Octocon.Accounts.DiscordSettings{} - - data = %{ - settings: - settings - |> Map.put( - :server_settings, - Octocon.Accounts.DiscordSettings.server_settings_map(settings) - ), - primary_front: primary_front, - system_id: system_id, - proxies: if(include_proxies, do: build_existing_proxies(discord_id), else: nil) - } - - insert(discord_id, data) - - {:ok, data} - end - end - - def nuke_cache_internal do - :ets.delete_all_objects(__MODULE__) - end - - def nuke_cache do - ClusterUtils.run_on_all_primary_nodes(fn -> - OctoconDiscord.Cache.Proxy.nuke_cache_internal() - end) - end - - def invalidate_internal(discord_id) when is_binary(discord_id) do - :ets.delete(__MODULE__, discord_id) - end - - @doc """ - Invalidates the cache for a Discord user. Takes either a system identity or a Discord ID (binary). - """ - def invalidate(system_identity) - - # User doesn't have a Discord ID - def invalidate(nil), do: :ok - - def invalidate(system_identity) when is_tuple(system_identity) do - discord_id = Accounts.id_from_system_identity(system_identity, :discord) - - if discord_id != nil do - ClusterUtils.run_on_all_primary_nodes(fn -> - OctoconDiscord.Cache.Proxy.invalidate_internal(discord_id) - end) - end - - :ok - end - - def invalidate(discord_id) when is_binary(discord_id) do - ClusterUtils.run_on_all_primary_nodes(fn -> - OctoconDiscord.Cache.Proxy.invalidate_internal(discord_id) - end) - - :ok - end - - def update_primary_front(discord_id, alter_id), do: update(discord_id, :primary_front, alter_id) - - def insert(discord_id, data) do - :ets.insert(__MODULE__, {discord_id, data}) - end - - def evict_proxies(discord_id) do - update_internal(discord_id, :proxies, nil) - end - - def update(nil, _, _), do: :ok - - def update(discord_id, key, value) do - ClusterUtils.run_on_all_primary_nodes(fn -> - OctoconDiscord.Cache.Proxy.update_internal(discord_id, key, value) - end) - - :ok - end - - def update_internal(discord_id, key, value) do - result = lookup_local(to_string(discord_id)) - - if result != nil do - Logger.debug("Updating cached data for #{discord_id}") - - insert(discord_id, Map.put(result, key, value)) - end - - :ok - end - - def build_existing_proxies(discord_id) do - proxy_map = Octocon.Accounts.get_user_proxy_map_old({:discord, discord_id}) - - proxy_list = - proxy_map - |> Enum.reduce([], fn {proxy, {system_id, alter_id}}, acc -> - [prefix, suffix] = - proxy - |> String.trim() - |> String.split("text", parts: 2) - - [ - {{prefix, suffix, String.length(proxy)}, {system_id, alter_id}} - | acc - ] - end) - |> Enum.sort(fn {{_, _, a}, _}, {{_, _, b}, _} -> a > b end) - - proxy_list - end - - # Server - - @impl GenServer - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, []} - end - - @impl GenServer - def handle_info({:evict_proxies, discord_id}, state) do - evict_proxies(discord_id) - {:noreply, state} - end - - @impl GenServer - def handle_info(_, state) do - {:noreply, state} - end -end diff --git a/lib/octocon_discord/cache/server_settings.ex b/lib/octocon_discord/cache/server_settings.ex deleted file mode 100644 index 4dd2dfad..00000000 --- a/lib/octocon_discord/cache/server_settings.ex +++ /dev/null @@ -1,87 +0,0 @@ -defmodule OctoconDiscord.Cache.ServerSettings do - alias Octocon.ServerSettings, as: Persistence - - import Cachex.Spec - - use Octocon.CachexChild, - name: __MODULE__, - hooks: [ - hook(module: Cachex.Limit.Scheduled, args: {5000, [], [frequency: :timer.seconds(30)]}) - ], - expiration: expiration(default: :timer.minutes(10)) - - def get_settings(guild_id) do - guild_id = to_string(guild_id) - Cachex.fetch!(__MODULE__, guild_id, &OctoconDiscord.Cache.ServerSettings.cache_function/1) - end - - def create_settings(guild_id) do - guild_id = to_string(guild_id) - - case Persistence.create_server_settings(guild_id) do - {:ok, _} -> - Cachex.update(__MODULE__, guild_id, %{}) - :ok - - error -> - error - end - end - - def edit_settings(guild_id, data) do - guild_id = to_string(guild_id) - - case Persistence.edit_server_settings(guild_id, data) do - {:ok, struct} -> - settings = Map.from_struct(struct.data) - Cachex.update(__MODULE__, guild_id, settings) - - :ok - - error -> - error - end - end - - def delete_settings(guild_id) do - guild_id = to_string(guild_id) - - case Persistence.delete_server_settings(guild_id) do - :ok -> - invalidate_cache(guild_id) - :ok - - error -> - error - end - end - - def invalidate_cache(guild_id) do - guild_id = to_string(guild_id) - Cachex.del!(__MODULE__, guild_id) - end - - def cache_function(guild_id) do - guild_id = to_string(guild_id) - - case Persistence.get_server_settings_data(guild_id) do - nil -> - {:ignore, nil} - - settings -> - Cachex.update(__MODULE__, guild_id, settings) - {:commit, Map.from_struct(settings)} - end - - # {:ok, webhooks} = Api.get_channel_webhooks(channel_id) - - # case Enum.find(webhooks, fn webhook -> webhook.name == @proxy_name end) do - # webhook when is_map(webhook) -> - # {:commit, %{id: webhook.id, token: webhook.token}} - - # nil -> - # {:ok, webhook} = Api.create_webhook(channel_id, %{name: @proxy_name}) - # {:commit, %{id: webhook.id, token: webhook.token}} - # end - end -end diff --git a/lib/octocon_discord/cache/webhooks.ex b/lib/octocon_discord/cache/webhooks.ex deleted file mode 100644 index 3a723430..00000000 --- a/lib/octocon_discord/cache/webhooks.ex +++ /dev/null @@ -1,50 +0,0 @@ -defmodule OctoconDiscord.Cache.Webhooks do - @moduledoc false - - import Cachex.Spec - - use Octocon.CachexChild, - name: __MODULE__, - hooks: [ - hook(module: Cachex.Limit.Scheduled, args: {5000, [], [frequency: :timer.seconds(30)]}) - ], - expiration: expiration(default: :timer.minutes(10)) - - alias Nostrum.Api - - @proxy_name "Octocon Proxy" - @timeout :timer.seconds(5) - - def get_webhook(channel_id) do - Task.async(fn -> do_fetch(channel_id) end) - |> Task.await(@timeout) - end - - defp do_fetch(channel_id) do - case Cachex.fetch!( - __MODULE__, - channel_id, - &cache_function/1 - ) do - %{id: _, token: _} = result -> result - _ -> nil - end - end - - def cache_function(channel_id) do - case Api.Channel.webhooks(channel_id) do - {:ok, webhooks} -> - case Enum.find(webhooks, fn webhook -> webhook.name == @proxy_name end) do - webhook when is_map(webhook) -> - {:commit, %{id: webhook.id, token: webhook.token}} - - nil -> - {:ok, webhook} = Api.Webhook.create(channel_id, %{name: @proxy_name}) - {:commit, %{id: webhook.id, token: webhook.token}} - end - - _ -> - {:ignore, nil} - end - end -end diff --git a/lib/octocon_discord/commands.ex b/lib/octocon_discord/commands.ex deleted file mode 100644 index 99df2005..00000000 --- a/lib/octocon_discord/commands.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule OctoconDiscord.Commands do - defmacro __using__(_opts) do - quote do - import OctoconDiscord.Utils - - import OctoconDiscord.Utils.{ - Commands, - Components, - CV2 - } - end - end -end diff --git a/lib/octocon_discord/commands/admin.ex b/lib/octocon_discord/commands/admin.ex deleted file mode 100644 index a18a33c3..00000000 --- a/lib/octocon_discord/commands/admin.ex +++ /dev/null @@ -1,350 +0,0 @@ -defmodule OctoconDiscord.Commands.Admin do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias OctoconDiscord.Cache - - @subcommands %{ - "channel-blacklist" => &__MODULE__.channel_blacklist/2, - "log-channel" => &__MODULE__.log_channel/2, - "view-settings" => &__MODULE__.view_settings/2, - "force-system-tags" => &__MODULE__.force_system_tags/2 - } - - @blacklist_subcommands %{ - "add" => &__MODULE__.channel_blacklist_add/2, - "remove" => &__MODULE__.channel_blacklist_remove/2, - "list" => &__MODULE__.channel_blacklist_list/2 - } - - @log_channel_subcommands %{ - "set" => &__MODULE__.log_channel_set/2, - "remove" => &__MODULE__.log_channel_remove/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Manages this server's settings." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{ - data: %{ - resolved: resolved, - options: [ - %{ - name: name, - options: options - } - ] - }, - guild_id: guild_id, - member: member, - user: %{ - id: discord_id - } - } = interaction - - @subcommands[name].( - %{resolved: resolved, discord_id: discord_id, guild_id: guild_id, member: member}, - options - ) - end - - def channel_blacklist(context, options) do - subcommand = hd(options) - - @blacklist_subcommands[subcommand.name].( - context, - subcommand.options - ) - end - - def channel_blacklist_add(%{guild_id: guild_id} = context, options) do - ensure_permissions(context, fn -> - channel = get_command_option(options, "channel") - - case Cache.ChannelBlacklists.add(to_string(guild_id), to_string(channel)) do - :ok -> - success_component("Added channel <##{channel}> to this server's proxy blacklist.") - - {:error, :already_blacklisted} -> - error_component("This channel is already blacklisted.") - end - end) - end - - def channel_blacklist_remove(context, options) do - ensure_permissions(context, fn -> - channel = get_command_option(options, "channel") - - case Cache.ChannelBlacklists.remove(to_string(channel)) do - :ok -> - success_component("Removed channel <##{channel}> from this server's proxy blacklist.") - - {:error, :not_blacklisted} -> - error_component("This channel is not currently blacklisted.") - end - end) - end - - def channel_blacklist_list(%{guild_id: guild_id} = context, _options) do - ensure_permissions(context, fn -> - case Cache.ChannelBlacklists.get_all_for_guild(to_string(guild_id)) do - [] -> - error_component("This server has no blacklisted channels.") - - channels -> - [ - components: [ - container( - [ - text("## Blacklisted channels"), - separator(spacing: :large), - text( - Enum.map_join(channels, "\n", fn channel -> - "- <##{channel.channel_id}>" - end) - ) - ], - %{accent_color: hex_to_int("#3F3793")} - ) - ], - flags: cv2_flags() - ] - end - end) - end - - def log_channel(context, options) do - subcommand = hd(options) - - @log_channel_subcommands[subcommand.name].( - context, - subcommand.options - ) - end - - def log_channel_set(%{guild_id: guild_id} = context, options, skip \\ false) do - callback = fn -> - channel = get_command_option(options, "channel") - - case Cache.ServerSettings.edit_settings(to_string(guild_id), %{ - log_channel: to_string(channel) - }) do - :ok -> - success_component("Set <##{channel}> as this server's log channel.") - - {:error, :not_found} -> - case Cache.ServerSettings.create_settings(to_string(guild_id)) do - :ok -> - log_channel_set(context, options) - - _ -> - error_component("An error occurred while setting the log channel.") - end - - _ -> - error_component("An error occurred while setting the log channel.") - end - end - - if skip do - callback.() - else - ensure_permissions(context, callback) - end - end - - def log_channel_remove(%{guild_id: guild_id} = context, _options) do - ensure_permissions(context, fn -> - case Cache.ServerSettings.edit_settings(to_string(guild_id), %{log_channel: nil}) do - :ok -> - success_component("Removed the log channel for this server.") - - {:error, :not_found} -> - error_component("This server has no log channel set.") - - _ -> - error_component("An error occurred while removing the log channel.") - end - end) - end - - def force_system_tags(%{guild_id: guild_id} = context, options) do - ensure_permissions(context, fn -> - case Cache.ServerSettings.get_settings(to_string(guild_id)) do - nil -> - case Cache.ServerSettings.create_settings(to_string(guild_id)) do - :ok -> - force_system_tags(context, options) - - _ -> - error_component("An error occurred while fetching this server's settings.") - end - - settings -> - new_value = not settings.force_system_tags - - case Cache.ServerSettings.edit_settings(to_string(guild_id), %{ - force_system_tags: new_value - }) do - :ok -> - success_component( - "Toggled forcing system tags to **#{if(new_value, do: "on", else: "off")}**." - ) - - _ -> - error_component("An error occurred while toggling system tags.") - end - end - end) - end - - def view_settings(%{guild_id: guild_id} = context, options, skip \\ false) do - callback = fn -> - case Cache.ServerSettings.get_settings(to_string(guild_id)) do - nil -> - case Cache.ServerSettings.create_settings(to_string(guild_id)) do - :ok -> - view_settings(context, options, true) - - _ -> - error_component("An error occurred while fetching this server's settings.") - end - - settings -> - log_channel = settings.log_channel - force_system_tags = settings.force_system_tags - - [ - components: [ - container( - [ - text("## Server settings"), - separator(spacing: :large), - text("**Force system tags:** #{if(force_system_tags, do: "Yes", else: "No")}"), - text( - "**Log channel:** #{if(log_channel, do: "<##{log_channel}>", else: "None")}" - ) - ], - %{accent_color: hex_to_int("#3F3793")} - ) - ], - flags: cv2_flags() - ] - end - end - - if skip do - callback.() - else - ensure_permissions(context, callback) - end - end - - defp ensure_permissions(%{guild_id: guild_id, member: member}, callback) do - %{ - id: id, - owner_id: owner_id, - roles: roles - } = get_cached_guild(guild_id) - - guild = %Nostrum.Struct.Guild{ - id: id, - owner_id: owner_id, - roles: roles - } - - permissions = Nostrum.Struct.Guild.Member.guild_permissions(member, guild) - - if Enum.member?(permissions, :manage_guild) or Enum.member?(permissions, :administrator) do - callback.() - else - error_component("You don't have permission to do that.") - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "channel-blacklist", - description: "Manages channel proxy blacklists.", - type: :sub_command_group, - options: [ - %{ - name: "add", - description: "Adds a channel to this server's blacklist.", - type: :sub_command, - options: [ - %{ - name: "channel", - type: :channel, - description: "The channel to add.", - required: true - } - ] - }, - %{ - name: "remove", - description: "Removes a channel from this server's blacklist.", - type: :sub_command, - options: [ - %{ - name: "channel", - type: :channel, - description: "The channel to remove.", - required: true - } - ] - }, - %{ - name: "list", - description: "Lists all channels in this server's blacklist.", - type: :sub_command - } - ] - }, - %{ - name: "log-channel", - description: "Manages the log channel for this server.", - type: :sub_command_group, - options: [ - %{ - name: "set", - description: "Sets the log channel for this server.", - type: :sub_command, - options: [ - %{ - name: "channel", - type: :channel, - description: "The channel to set as the log channel.", - required: true - } - ] - }, - %{ - name: "remove", - description: "Removes the log channel for this server.", - type: :sub_command - } - ] - }, - %{ - name: "force-system-tags", - description: "Toggles whether system tags are forced on this server.", - type: :sub_command - }, - %{ - name: "view-settings", - description: "Views this server's settings.", - type: :sub_command - } - ] -end diff --git a/lib/octocon_discord/commands/alter.ex b/lib/octocon_discord/commands/alter.ex deleted file mode 100644 index 17a42e41..00000000 --- a/lib/octocon_discord/commands/alter.ex +++ /dev/null @@ -1,707 +0,0 @@ -defmodule OctoconDiscord.Commands.Alter do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias OctoconDiscord.Components.AlterPaginator - - alias Octocon.{ - Accounts, - Alters - } - - @subcommands %{ - "create" => &__MODULE__.create/2, - "delete" => &__MODULE__.delete/2, - "view" => &__MODULE__.view/2, - "avatar" => &__MODULE__.Avatar.command/2, - "random" => &__MODULE__.random/2, - "remove-alias" => &__MODULE__.remove_alias/2, - "remove-proxy-name" => &__MODULE__.remove_proxy_name/2, - "extra-images" => &__MODULE__.extra_images/2, - "security" => &__MODULE__.security/2, - "list" => &__MODULE__.list/2, - "edit" => &__MODULE__.edit/2, - "proxy" => &__MODULE__.Proxy.command/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Manages your system's alters." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{resolved: resolved, system_identity: {:discord, discord_id}, discord_id: discord_id}, - options - ) - end) - end - - def create(%{system_identity: system_identity}, options) do - name = get_command_option(options, "name") - - case Alters.create_alter(system_identity, %{name: name}) do - {:ok, id, _} -> - success_component( - "Successfully created alter **#{name}**! Their ID is **#{id}**. You can view their profile with `/alter view id:#{id}`.\n\n**Note:** This alter is currently private. You can change this with `/alter security id:#{id}`." - ) - - {:error, _} -> - error_component( - "Whoops! An unknown error occurred while creating the alter. Please try again." - ) - end - end - - def delete(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - case Alters.delete_alter(system_identity, alter_identity) do - :ok -> - success_component( - "Successfully deleted alter with ID/alias **#{elem(alter_identity, 1)}**!" - ) - - {:error, :no_alter_id} -> - error_component("You don't have an alter with ID **#{elem(alter_identity, 1)}**.") - - {:error, :no_alter_alias} -> - error_component("You don't have an alter with alias **#{elem(alter_identity, 1)}**.") - - {:error, _} -> - error_component("An unknown error occurred while deleting the alter. Please try again.") - end - end) - end - - def view(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - case Alters.get_alter_by_id(system_identity, alter_identity) do - {:ok, alter} -> - show = get_show_option(options) - - [ - components: alter_component(alter, show), - flags: cv2_flags(!show) - ] - - {:error, :no_alter_id} -> - error_component("You don't have an alter with ID **#{elem(alter_identity, 1)}**.") - - {:error, :no_alter_alias} -> - error_component("You don't have an alter with alias **#{elem(alter_identity, 1)}**.") - end - end) - end - - def random(%{system_identity: system_identity}, options) do - case Alters.get_random_alter(system_identity) do - nil -> - error_component("You don't have any alters to choose from.") - - {:ok, alter} -> - show = get_show_option(options) - - [ - components: alter_component(alter, show), - flags: cv2_flags(!show) - ] - end - end - - def list(%{system_identity: system_identity}, options) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - sort = - case get_command_option(options, "sort") do - nil -> :id - "id" -> :id - "alphabetical" -> :alphabetical - end - - alters = - Alters.get_alters_by_id({:system, system_id}, [ - :id, - :name, - :pronouns, - :discord_proxies, - :alias - ]) - - sorted_alters = - case sort do - :id -> alters - :alphabetical -> alters |> Enum.sort_by(& &1.name) - end - - AlterPaginator.handle_init(system_id, sorted_alters, length(sorted_alters)) - end - - def update_alter( - %{system_identity: system_identity}, - alter_identity, - options, - success_text, - embed_alter \\ true - ) do - case options do - map when map_size(map) == 0 -> - error_component("You must provide at least one field to update.") - - _ -> - case Alters.update_alter(system_identity, alter_identity, options) do - :ok -> - [ - components: - if embed_alter do - [ - success_component_raw(success_text), - alter_component( - Alters.get_alter_by_id!(system_identity, alter_identity), - false - ) - ] - else - [success_component_raw(success_text)] - end - |> List.flatten(), - flags: cv2_flags() - ] - - {:error, :no_alter_id} -> - error_component("You don't have an alter with ID **#{elem(alter_identity, 1)}**.") - - {:error, :no_alter_alias} -> - error_component("You don't have an alter with alias **#{elem(alter_identity, 1)}**.") - - {:error, _} -> - error_component( - "An unknown error occurred while updating the alter. Please try again." - ) - end - end - end - - def edit(%{system_identity: system_identity} = context, options) do - with_id_or_alias(options, fn alter_identity -> - to_update = - %{ - name: get_command_option(options, "name"), - pronouns: get_command_option(options, "pronouns"), - description: get_command_option(options, "description"), - proxy_name: get_command_option(options, "proxy-name"), - color: get_command_option(options, "color"), - alias: get_command_option(options, "alias") - } - |> Map.filter(fn {_, v} -> v != nil end) - - # This is ugly, but it works. - try do - if Map.has_key?(to_update, :alias) do - if Alters.alias_taken?(system_identity, to_update[:alias]) do - throw("You already have an alter with the alias **#{to_update[:alias]}**.") - end - - case validate_alias(to_update[:alias]) do - {:error, error} -> throw(error) - {:alias, _} -> :ok - end - end - - if Map.has_key?(to_update, :color) do - case validate_hex_color(to_update[:color]) do - :error -> - throw("Invalid color. Please provide a valid hex code.") - - {:ok, new_color} -> - update_alter( - context, - alter_identity, - Map.put(to_update, :color, "#" <> new_color), - "Successfully edited alter with ID/alias **#{elem(alter_identity, 1)}**!" - ) - end - else - update_alter( - context, - alter_identity, - to_update, - "Successfully edited alter with ID/alias **#{elem(alter_identity, 1)}**!" - ) - end - catch - e -> error_component(e) - end - end) - end - - def security(context, options) do - with_id_or_alias(options, fn alter_identity -> - security_level = get_command_option(options, "level") |> String.to_existing_atom() - - update_alter( - context, - alter_identity, - %{security_level: security_level}, - "Successfully updated alter's security level!", - true - ) - end) - end - - def remove_alias(context, options) do - with_id_or_alias(options, fn alter_identity -> - update_alter( - context, - alter_identity, - %{alias: nil}, - "Successfully removed alias from alter!", - false - ) - end) - end - - def remove_proxy_name(context, options) do - with_id_or_alias(options, fn alter_identity -> - update_alter( - context, - alter_identity, - %{proxy_name: nil}, - "Successfully removed proxy name from alter!", - false - ) - end) - end - - def extra_images(_context, _options) do - error_component("This command is not yet implemented.") - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "create", - description: "Creates a new alter.", - type: :sub_command, - options: [ - %{ - name: "name", - type: :string, - max_length: 80, - description: "The name of the alter to create.", - required: false - } - ] - }, - %{ - name: "delete", - description: "Deletes an existing alter.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - max_length: 80, - description: "The ID (or alias) of the alter to delete.", - required: true, - autocomplete: true - } - ] - }, - %{ - name: "view", - description: "Views an existing alter.", - type: :sub_command, - options: - [ - %{ - name: "id", - type: :string, - max_length: 80, - description: "The ID (or alias) of the alter to view.", - required: true, - autocomplete: true - } - ] - |> add_show_option() - }, - %{ - name: "random", - description: "Views a random alter.", - type: :sub_command, - options: add_show_option([]) - }, - %{ - name: "list", - description: "Lists all of your alters.", - type: :sub_command, - options: [ - %{ - name: "sort", - description: "The method to sort your alters by.", - type: :string, - choices: [ - %{name: "ID", value: "id"}, - %{name: "Alphabetical", value: "alphabetical"} - ] - } - ] - }, - %{ - name: "security", - description: "Manages an alter's security level.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to update.", - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "level", - type: :string, - description: "The security level to set the alter to.", - required: true, - choices: [ - %{name: "Private", value: "private"}, - %{name: "Trusted friends only", value: "trusted_only"}, - %{name: "Friends only", value: "friends_only"}, - %{name: "Public", value: "public"} - ] - } - ] - }, - %{ - name: "avatar", - description: "Manages an alter's avatar.", - type: :sub_command_group, - options: [ - %{ - name: "set", - description: "Sets an alter's avatar to the attached image.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - max_length: 80, - description: "The ID (or alias) of the alter to set the avatar of.", - required: true, - autocomplete: true - }, - %{ - name: "avatar", - type: :attachment, - description: "The image to set.", - required: true - } - ] - }, - %{ - name: "remove", - description: "Removes an alter's avatar.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to remove the avatar of.", - max_length: 80, - required: true, - autocomplete: true - } - ] - } - ] - }, - %{ - name: "proxy", - description: "Manages an alter's chat proxies.", - type: :sub_command_group, - options: [ - %{ - name: "set", - description: "Removes all existing proxies and sets a new proxy.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - max_length: 80, - description: "The ID (or alias) of the alter to set the proxy of.", - required: true, - autocomplete: true - }, - %{ - name: "prefix", - type: :string, - max_length: 30, - description: "The prefix of the proxy to set.", - required: false - }, - %{ - name: "suffix", - type: :string, - max_length: 30, - description: "The suffix of the proxy to set.", - required: false - } - ] - }, - %{ - name: "add", - description: "Adds a new proxy to an alter.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to add a new proxy to.", - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "prefix", - type: :string, - max_length: 30, - description: "The prefix of the proxy to add.", - required: false - }, - %{ - name: "suffix", - type: :string, - max_length: 30, - description: "The suffix of the proxy to add.", - required: false - } - ] - }, - %{ - name: "remove", - description: "Removes a proxy from an alter.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to remove the proxy from.", - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "prefix", - type: :string, - max_length: 30, - description: "The prefix of the proxy to remove.", - required: false - }, - %{ - name: "suffix", - type: :string, - max_length: 30, - description: "The suffix of the proxy to remove.", - required: false - } - ] - }, - %{ - name: "clear", - description: "Clears all proxies from an alter.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to clear the proxies from.", - max_length: 80, - required: true, - autocomplete: true - } - ] - } - ] - }, - # %{ - # name: "extra-images", - # description: "Manages an alter's extra images.", - # type: :sub_command_group, - # options: [ - # %{ - # name: "add-url", - # description: "Adds an extra image to an alter with a URL.", - # type: :sub_command, - # options: [ - # %{ - # name: "id", - # type: :integer, - # description: "The ID of the alter to add an extra image to.", - # required: true - # }, - # %{ - # name: "url", - # type: :string, - # max_length: 2000, - # description: "The URL of the extra image to add.", - # required: true - # } - # ] - # }, - # %{ - # name: "add", - # description: "Adds an extra image to an alter with an attachment.", - # type: :sub_command, - # options: [ - # %{ - # name: "id", - # type: :integer, - # description: "The ID of the alter to add an extra image to.", - # required: true - # }, - # %{ - # name: "image", - # type: :attachment, - # description: "The image to add.", - # required: true - # } - # ] - # }, - # %{ - # name: "remove", - # description: "Removes an extra image from an alter.", - # type: :sub_command, - # options: [ - # %{ - # name: "id", - # type: :integer, - # description: "The ID of the alter to remove an extra image from.", - # required: true - # }, - # %{ - # name: "index", - # type: :integer, - # description: "The index of the extra image to remove.", - # required: true - # } - # ] - # }, - # %{ - # name: "list", - # description: "Lists an alter's extra images.", - # type: :sub_command, - # options: [ - # %{ - # name: "id", - # type: :integer, - # description: "The ID of the alter to list extra images from.", - # required: true - # } - # ] - # } - # ] - # }, - %{ - name: "remove-alias", - description: "Removes an alter's alias.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to remove the alias from.", - max_length: 80, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "remove-proxy-name", - description: "Removes an alter's proxy name.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to remove the proxy name from.", - max_length: 80, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "edit", - description: "Edits an existing alter.", - type: :sub_command, - options: [ - %{ - name: "id", - type: :string, - description: "The ID (or alias) of the alter to update.", - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "name", - type: :string, - max_length: 80, - description: "The new name of the alter.", - required: false - }, - %{ - name: "pronouns", - type: :string, - max_length: 50, - description: "The new pronouns of the alter.", - required: false - }, - %{ - name: "description", - type: :string, - max_length: 3000, - description: "The new description of the alter.", - required: false - }, - %{ - name: "proxy-name", - type: :string, - max_length: 80, - description: "The new proxy name of the alter.", - required: false - }, - %{ - name: "color", - type: :string, - min_length: 6, - max_length: 7, - description: "The new color (hex code) of the alter.", - required: false - }, - %{ - name: "alias", - type: :string, - max_length: 80, - description: "The new alias of the alter.", - required: false - } - ] - } - ] -end diff --git a/lib/octocon_discord/commands/alter/avatar.ex b/lib/octocon_discord/commands/alter/avatar.ex deleted file mode 100644 index 9879a532..00000000 --- a/lib/octocon_discord/commands/alter/avatar.ex +++ /dev/null @@ -1,99 +0,0 @@ -defmodule OctoconDiscord.Commands.Alter.Avatar do - @moduledoc false - - use OctoconDiscord.Commands - - import Octocon.Utils.Alter, only: [upload_avatar: 3] - - alias Octocon.{ - Accounts, - Alters - } - - @subcommands %{ - "set" => &__MODULE__.set_attachment/2, - # "set-url" => &__MODULE__.set_url/2, - "remove" => &__MODULE__.remove/2 - } - - def command(%{system_identity: system_identity} = context, options) do - subcommand = hd(options) - - with_id_or_alias(subcommand.options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - @subcommands[subcommand.name].( - context - |> Map.put(:alter_identity, {:id, alter_id}), - subcommand.options - ) - else - case alter_identity do - {:id, alter_id} -> - error_component("You don't have an alter with ID **#{alter_id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with alias **#{aliaz}**.") - end - end - end) - end - - def set_attachment( - %{resolved: resolved, system_identity: system_identity, alter_identity: alter_identity}, - options - ) do - avatar_id = get_command_option(options, "avatar") - attachment = resolved.attachments[avatar_id] - - cond do - attachment.height == nil or attachment.width == nil -> - error_component( - "That file doesn't appear to be a valid image. Please provide an image under 20 MB." - ) - - attachment.size > 20_000_000 -> - error_component( - "The image you provided is too large. Please provide an image that is less than 20 MB." - ) - - true -> - case upload_avatar(system_identity, alter_identity, attachment.url) do - :ok -> - success_component("Successfully set alter's avatar!") - - _ -> - error_component( - "An unknown error occurred while processing the image. Please try again." - ) - end - end - end - - # def set_url(_context, _options) do - # error_component("This command is not yet implemented.") - # end - - def remove( - %{system_identity: system_identity, alter_identity: alter_identity} = context, - _options - ) do - result = - OctoconDiscord.Commands.Alter.update_alter( - context, - alter_identity, - %{avatar_url: ""}, - "Successfully removed alter's avatar!", - false - ) - - spawn(fn -> - system_id = Accounts.id_from_system_identity(system_identity, :system) - alter_id = Alters.resolve_alter(system_identity, alter_identity) - Octocon.Utils.nuke_existing_avatars!(system_id, alter_id) - end) - - result - end -end diff --git a/lib/octocon_discord/commands/alter/proxy.ex b/lib/octocon_discord/commands/alter/proxy.ex deleted file mode 100644 index 8da2fae2..00000000 --- a/lib/octocon_discord/commands/alter/proxy.ex +++ /dev/null @@ -1,153 +0,0 @@ -defmodule OctoconDiscord.Commands.Alter.Proxy do - @moduledoc false - - use OctoconDiscord.Commands - - require Logger - - alias Octocon.Alters - alias OctoconDiscord.Cache - - @subcommands %{ - "set" => &__MODULE__.set/2, - "add" => &__MODULE__.add/2, - "remove" => &__MODULE__.remove/2, - "clear" => &__MODULE__.clear/2 - } - - def command(%{system_identity: system_identity} = context, options) do - subcommand = hd(options) - - with_id_or_alias(subcommand.options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - @subcommands[subcommand.name].( - context - |> Map.put(:alter_identity, {:id, alter_id}), - subcommand.options - ) - else - case alter_identity do - {:id, alter_id} -> - error_component("You don't have an alter with ID **#{alter_id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with the alias **#{aliaz}**.") - end - end - end) - end - - def remove( - %{ - system_identity: system_identity, - alter_identity: alter_identity, - discord_id: discord_id - } = context, - options - ) do - prefix = get_command_option(options, "prefix") || "" - suffix = get_command_option(options, "suffix") || "" - - if String.length(prefix) == 0 && String.length(suffix) == 0 do - error_component("You must provide a prefix or suffix for your proxy.") - else - proxy = prefix <> "text" <> suffix - - proxies = - Alters.get_alter_by_id!(system_identity, alter_identity, [:discord_proxies]) - |> Map.get(:discord_proxies, []) - - if Enum.member?(proxies, proxy) do - Cache.Proxy.evict_proxies(discord_id) - - OctoconDiscord.Commands.Alter.update_alter( - context, - alter_identity, - %{discord_proxies: Enum.reject(proxies, fn p -> p == proxy end)}, - "Successfully removed proxy `#{proxy}` from alter!", - true - ) - else - error_component("That alter doesn't have a proxy with that prefix and suffix.") - end - end - end - - def add( - %{ - system_identity: system_identity, - alter_identity: alter_identity, - discord_id: discord_id - } = context, - options - ) do - validate_proxy(context, options, fn proxy -> - existing_proxies = - Alters.get_alter_by_id!(system_identity, alter_identity, [:discord_proxies]) - |> Map.get(:discord_proxies, []) || [] - - Cache.Proxy.evict_proxies(discord_id) - - OctoconDiscord.Commands.Alter.update_alter( - context, - alter_identity, - %{ - discord_proxies: [proxy | existing_proxies] - }, - "Successfully added proxy `#{proxy}` to alter!", - true - ) - end) - end - - def set(%{alter_identity: alter_identity, discord_id: discord_id} = context, options) do - validate_proxy(context, options, fn proxy -> - Cache.Proxy.evict_proxies(discord_id) - - OctoconDiscord.Commands.Alter.update_alter( - context, - alter_identity, - %{discord_proxies: [proxy]}, - "Successfully set proxy to `#{proxy}` for alter!", - true - ) - end) - end - - def clear(%{alter_identity: alter_identity, discord_id: discord_id} = context, _options) do - Cache.Proxy.evict_proxies(to_string(discord_id)) - - OctoconDiscord.Commands.Alter.update_alter( - context, - alter_identity, - %{discord_proxies: []}, - "Successfully removed all proxies from alter!", - true - ) - end - - defp validate_proxy(%{discord_id: discord_id}, options, callback) do - prefix = get_command_option(options, "prefix") || "" - suffix = get_command_option(options, "suffix") || "" - - if String.length(prefix) == 0 && String.length(suffix) == 0 do - error_component("You must provide a prefix or suffix for your proxy.") - else - proxy_exists? = - Cache.Proxy.get(discord_id) - |> elem(1) - |> Map.get(:proxies, []) - |> Enum.any?(fn {{pre, suf, _}, _} -> - prefix == pre && suffix == suf - end) - - if proxy_exists? do - error_component("One of your alters already has a proxy with that prefix and suffix.") - else - callback.(prefix <> "text" <> suffix) - end - end - end -end diff --git a/lib/octocon_discord/commands/autoproxy.ex b/lib/octocon_discord/commands/autoproxy.ex deleted file mode 100644 index 4fd8701b..00000000 --- a/lib/octocon_discord/commands/autoproxy.ex +++ /dev/null @@ -1,81 +0,0 @@ -defmodule OctoconDiscord.Commands.Autoproxy do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Accounts - - alias OctoconDiscord.Cache - - @autoproxy_descriptions %{ - off: "Autoproxying is now disabled.", - front: - "You will now automatically proxy as your current **main fronter** if applicable. If not, you will proxy as the **longest current fronter**.", - latch: - "You will now automatically proxy as the last alter to send a message. *This will take effect the next time you proxy.*" - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Changes your server-specific autoproxy settings." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{guild_id: guild_id, user: %{id: discord_id}} = interaction - guild_id = to_string(guild_id) - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: options}} = interaction - system_identity = {:discord, discord_id} - - mode = get_command_option(options, "mode") - # This atom cast should be safe because Discord should only send us valid options - mode_atom = String.to_existing_atom(mode) - - new_settings = - %{ - autoproxy_mode: mode_atom - } - |> then(fn settings -> - if mode_atom != :latch do - Map.put(settings, :latched_alter, nil) - else - settings - end - end) - - case Accounts.update_server_settings(system_identity, guild_id, new_settings) do - {:ok, _} -> - Cache.Proxy.invalidate(discord_id) - - success_component( - "Server autoproxy mode set to `#{mode |> String.capitalize()}`.\n\n#{@autoproxy_descriptions[mode_atom]}\n\n⚠️ Please note that this setting only applies to the server you just ran this command in. If you wish to set your autoproxy mode globally, please use `/global-autoproxy`." - ) - - {:error, _} -> - error_component("An unknown error occurred while updating your autoproxy mode.") - end - end) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "mode", - type: :string, - description: "The mode to set your autoproxy to.", - required: true, - choices: [ - %{name: "off", value: "off"}, - %{name: "front", value: "front"}, - %{name: "latch", value: "latch"} - ] - } - ] -end diff --git a/lib/octocon_discord/commands/bot_info.ex b/lib/octocon_discord/commands/bot_info.ex deleted file mode 100644 index e324c6ee..00000000 --- a/lib/octocon_discord/commands/bot_info.ex +++ /dev/null @@ -1,180 +0,0 @@ -defmodule OctoconDiscord.Commands.BotInfo do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - # @commit_hash System.cmd("git", ["rev-parse", "HEAD"]) |> elem(0) |> String.trim() - # @commit_hash_short String.slice(@commit_hash, 0..6) - - alias Octocon.{ - Accounts, - Alters - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Views the Octocon bot's metrics." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - spawn(fn -> - process_count = :erlang.system_info(:process_count) - process_limit = :erlang.system_info(:process_limit) - process_percentage = (process_count / process_limit) |> Kernel.*(100) |> Float.ceil(2) - - average_shard_latency = - Nostrum.Util.get_all_shard_latencies() - |> Map.values() - |> Enum.filter(fn latency -> latency != nil && latency != 0 end) - |> case do - [] -> - "Unknown" - - latencies -> - (Enum.sum(latencies) / Enum.count(latencies)) - |> round() - |> to_string() - |> Kernel.<>("ms") - end - - beam_uptime = - ((:erlang.statistics(:wall_clock) |> elem(0)) / 1000) - |> floor() - |> Timex.Duration.from_seconds() - |> Timex.format_duration(:humanized) - - # [_, {_, _, scheduler_utilization} | _rest] = :scheduler.utilization(2) - - memory_data = :memsup.get_system_memory_data() - - # System.schedulers() is a crude way of measuring the number of cores on the machine - d = System.schedulers() * 256 - - load_to_percent = fn load -> - 100 * (1 - d / (d + load)) - end - - cpu_usage = - "#{:cpu_sup.avg1() |> load_to_percent.() |> Float.ceil(2)}% / #{:cpu_sup.avg5() |> load_to_percent.() |> Float.ceil(2)}% / #{:cpu_sup.avg15() |> load_to_percent.() |> Float.ceil(2)}%" - - total_memory = - Keyword.get(memory_data, :total_memory) - |> Kernel./(1024 * 1024) - |> Float.ceil(2) - - available_memory = - Keyword.get(memory_data, :available_memory) - |> Kernel./(1024 * 1024) - |> Float.ceil(2) - - used_memory = (total_memory - available_memory) |> Float.ceil(2) - - guild_count = :mnesia.table_info(Nostrum.Cache.GuildCache.Mnesia.table(), :size) - - [ - embeds: [ - %Nostrum.Struct.Embed{ - title: "Octocon Bot Info", - color: hex_to_int("#3F3793"), - fields: [ - %Nostrum.Struct.Embed.Field{ - name: "Local node information", - value: " ", - inline: false - }, - %Nostrum.Struct.Embed.Field{ - name: "Processes", - value: "#{process_count} / #{process_limit}\n(#{process_percentage}%)", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "CPU (1/5/15 mins.)", - value: "#{cpu_usage}", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Memory", - value: - "#{used_memory} MB / #{total_memory} MB\n(#{Float.ceil(used_memory / total_memory * 100, 2)}%)", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Uptime", - value: beam_uptime, - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Global information", - value: " ", - inline: false - }, - %Nostrum.Struct.Embed.Field{ - name: "Shard count", - value: "#{Nostrum.Util.gateway() |> elem(1)}", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Avg shard latency", - value: average_shard_latency, - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Node count", - value: "#{Enum.count(Node.list()) + 1}", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: " ", - value: " ", - inline: false - }, - %Nostrum.Struct.Embed.Field{ - name: "Guilds", - value: "#{guild_count}", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Systems", - value: "#{Accounts.count()}", - inline: true - }, - %Nostrum.Struct.Embed.Field{ - name: "Alters", - value: "#{Alters.count()}", - inline: true - } - # %Nostrum.Struct.Embed.Field{ - # name: " ", - # value: " ", - # inline: false - # }, - # %Nostrum.Struct.Embed.Field{ - # name: "Commit hash", - # value: "[#{@commit_hash_short}](https://github.com/OctoconDev/octocon/commit/#{@commit_hash})", - # inline: true - # }, - ] - } - ] - ] - |> Map.new() - |> then(&Nostrum.Api.Interaction.edit_response(interaction, &1)) - end) - - [ - embeds: [ - %Nostrum.Struct.Embed{ - title: "Fetching bot info...", - color: hex_to_int("#3F3793") - } - ] - ] - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, do: [] -end diff --git a/lib/octocon_discord/commands/danger.ex b/lib/octocon_discord/commands/danger.ex deleted file mode 100644 index cc3edc35..00000000 --- a/lib/octocon_discord/commands/danger.ex +++ /dev/null @@ -1,81 +0,0 @@ -defmodule OctoconDiscord.Commands.Danger do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias OctoconDiscord.Components.{ - DeleteAccountHandler, - WipeAltersHandler, - WipeTagsHandler - } - - @subcommands %{ - "wipe-alters" => &__MODULE__.wipe_alters/2, - "wipe-tags" => &__MODULE__.wipe_tags/2, - "delete-account" => &__MODULE__.delete_account/2 - } - - @impl Nosedrum.ApplicationCommand - def description, - do: "Danger zone! These commands are irreversible and should be used with caution." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{resolved: resolved, system_identity: {:discord, discord_id}}, - options - ) - end) - end - - def delete_account(%{system_identity: system_identity}, _options) do - DeleteAccountHandler.handle_init(system_identity) - - success_component("Check your DMs!") - end - - def wipe_alters(%{system_identity: system_identity}, _options) do - WipeAltersHandler.handle_init(system_identity) - - success_component("Check your DMs!") - end - - def wipe_tags(%{system_identity: system_identity}, _options) do - WipeTagsHandler.handle_init(system_identity) - - success_component("Check your DMs!") - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "delete-account", - description: "WARNING: Deletes your system and all associated data.", - type: :sub_command - }, - %{ - name: "wipe-alters", - description: - "WARNING: Wipes all alters from your system, but keeps your account and settings.", - type: :sub_command - }, - %{ - name: "wipe-tags", - description: - "WARNING: Wipes all tags from your system, but keeps your account and settings.", - type: :sub_command - } - ] -end diff --git a/lib/octocon_discord/commands/export.ex b/lib/octocon_discord/commands/export.ex deleted file mode 100644 index 69b4b952..00000000 --- a/lib/octocon_discord/commands/export.ex +++ /dev/null @@ -1,81 +0,0 @@ -defmodule OctoconDiscord.Commands.Export do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Accounts - - @impl Nosedrum.ApplicationCommand - def description, do: "Exports your Octocon data." - - @impl Nosedrum.ApplicationCommand - def command(%{data: %{options: options}} = interaction) do - %{ - id: discord_id - # avatar: avatar_hash - } = interaction.user - - discord_id = to_string(discord_id) - system_identity = {:discord, discord_id} - - # credo:disable-for-next-line - if not Accounts.user_exists?(system_identity) do - error_component("You do not have an Octocon account.") - else - data = Octocon.Accounts.gather_export_data(system_identity) - format = get_command_option(options, "format") - - data = - case format do - "pk" -> - data |> Octocon.Accounts.format_pk_export() - - "full" -> - data |> Octocon.Accounts.format_full_export() - end - - {:ok, channel} = Nostrum.Api.User.create_dm(Integer.parse(discord_id) |> elem(0)) - - Nostrum.Api.Message.create(channel.id, %{ - content: """ - # Octocon data export - Your export data is attached as a JSON file to this message. - - #{if format == "pk" do - "Use the `pk;import` command in PluralKit to import this data into your PluralKit account." - else - "This data is not currently importable to any other platform. It is intended to be used in the event that another platform implements Octocon imports." - end} - """, - files: [ - %{ - name: "octocon_export_#{format}.json", - body: data - } - ] - }) - - success_component("Check your DMs.") - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "format", - description: "The format to export your data in.", - type: :string, - required: true, - choices: [ - %{name: "PluralKit", value: "pk"}, - %{name: "Full JSON", value: "full"} - ] - } - ] -end diff --git a/lib/octocon_discord/commands/friend.ex b/lib/octocon_discord/commands/friend.ex deleted file mode 100644 index a59698b6..00000000 --- a/lib/octocon_discord/commands/friend.ex +++ /dev/null @@ -1,337 +0,0 @@ -defmodule OctoconDiscord.Commands.Friend do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - require Logger - - alias Octocon.Friendships - - @subcommands %{ - "request" => &__MODULE__.Request.command/2, - "remove" => &__MODULE__.remove/2, - "list" => &__MODULE__.list/2, - "trust" => &__MODULE__.trust/2, - "untrust" => &__MODULE__.untrust/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Manages your friends." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{resolved: resolved, system_identity: {:discord, discord_id}}, - options - ) - end) - end - - def remove(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, decorator -> - remove_friend(context, identity, decorator) - end) - end - - def list(%{system_identity: system_identity}, _options) do - case Friendships.list_friendships(system_identity) do - [] -> - error_component("You have no friends (yet!). Add some with `/friend add`!") - - friendships -> - [ - components: [ - container([ - text("## Your friends (#{length(friendships)})"), - separator(spacing: :large), - text( - Enum.map_join(friendships, "\n", fn %{friend: friend, friendship: %{level: level}} -> - "- **#{friend.username || friend.id}** (#{case friend.discord_id do - nil -> "" - id -> "<@#{id}>" - end}#{case level do - :trusted_friend -> "; :star:" - :friend -> "" - end})" - end) - ), - separator(spacing: :large), - text("*⭐ = Trusted friend*") - ]) - ], - flags: cv2_flags() - ] - end - end - - def trust(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, decorator -> - trust_friend(context, identity, decorator) - end) - end - - def untrust(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, decorator -> - untrust_friend(context, identity, decorator) - end) - end - - defp trust_friend(%{system_identity: system_identity}, target_identity, decorator) do - case Friendships.trust_friend(system_identity, target_identity) do - :ok -> - success_component("#{decorator} is now a trusted friend!") - - {:error, :not_friends} -> - error_component("You are not friends with #{decorator}.") - - {:error, _} -> - error_component("An unknown error occurred while trusting the friend. Please try again.") - end - end - - defp untrust_friend(%{system_identity: system_identity}, target_identity, decorator) do - case Friendships.untrust_friend(system_identity, target_identity) do - :ok -> - success_component("#{decorator} is no longer a trusted friend!") - - {:error, :not_friends} -> - error_component("You are not friends with #{decorator}.") - - {:error, _} -> - error_component( - "An unknown error occurred while untrusting the friend. Please try again." - ) - end - end - - defp remove_friend(%{system_identity: system_identity}, target_identity, decorator) do - case Friendships.remove_friendship(system_identity, target_identity) do - :ok -> - success_component("You are no longer friends with #{decorator}!") - - {:error, :not_friends} -> - error_component("You are not friends with #{decorator}.") - - {:error, _} -> - error_component( - "An unknown error occurred while removing the friendship. Please try again." - ) - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "request", - type: :sub_command_group, - description: "Manages your friend requests.", - options: [ - %{ - name: "send", - description: - "Sends a friend request to a system by their ID, Discord ping, or username.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system to send a friend request to.", - required: false - }, - %{ - name: "username", - type: :string, - min_length: 5, - max_length: 16, - description: "The username of the system to send a friend request to.", - required: false - }, - %{ - name: "discord", - description: "The Discord ping of the user to send a friend request to.", - type: :user, - required: false - } - ] - }, - %{ - name: "accept", - description: - "Accepts a friend request from a system by their ID, Discord ping, or username.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system whose friend request to accept.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user whose friend request to accept.", - type: :user, - required: false - } - ] - }, - %{ - name: "reject", - description: - "Rejects a friend request from a system by their ID, Discord ping, or username.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system whose friend request to reject.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user whose friend request to reject.", - type: :user, - required: false - } - ] - }, - %{ - name: "cancel", - description: - "Cancels a friend request to a system by their ID, Discord ping, or username.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system whose friend request to cancel.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user whose friend request to cancel.", - type: :user, - required: false - } - ] - }, - %{ - name: "list", - description: "Lists your incoming and outgoing friend requests.", - type: :sub_command - } - ] - }, - %{ - name: "remove", - description: "Removes a friend.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system to remove as a friend.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user to remove as a friend.", - type: :user, - required: false - } - ] - }, - %{ - name: "list", - description: "Lists your friends.", - type: :sub_command - }, - %{ - name: "trust", - description: "Turns a friend into a \"trusted friend\".", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system to trust.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user to trust.", - type: :user, - required: false - } - ] - }, - %{ - name: "untrust", - description: "Turns a \"trusted friend\" into a regular friend.", - type: :sub_command, - options: [ - %{ - name: "system-id", - type: :string, - min_length: 7, - max_length: 7, - description: "The ID of the system to untrust.", - required: false, - autocomplete: true - }, - %{ - name: "discord", - description: "The Discord ping of the user to untrust.", - type: :user, - required: false - } - ] - } - ] -end diff --git a/lib/octocon_discord/commands/friend/request.ex b/lib/octocon_discord/commands/friend/request.ex deleted file mode 100644 index ba858b8e..00000000 --- a/lib/octocon_discord/commands/friend/request.ex +++ /dev/null @@ -1,221 +0,0 @@ -defmodule OctoconDiscord.Commands.Friend.Request do - @moduledoc false - - use OctoconDiscord.Commands - - alias Octocon.Friendships - - @subcommands %{ - "send" => &__MODULE__.send/2, - "accept" => &__MODULE__.accept/2, - "reject" => &__MODULE__.reject/2, - "cancel" => &__MODULE__.cancel/2, - "list" => &__MODULE__.list/2 - } - - def command(context, options) do - subcommand = hd(options) - - @subcommands[subcommand.name].( - context, - subcommand.options - ) - end - - def send(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, decorator -> - send_friend_request(context, identity, decorator) - end) - end - - def accept(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord") - } - - system_id_from_opts(opts, fn identity, decorator -> - accept_friend_request(context, identity, decorator) - end) - end - - def reject(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord") - } - - system_id_from_opts(opts, fn identity, decorator -> - reject_friend_request(context, identity, decorator) - end) - end - - def cancel(context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord") - } - - system_id_from_opts(opts, fn identity, decorator -> - cancel_friend_request(context, identity, decorator) - end) - end - - def list(%{system_identity: system_identity}, _options) do - incoming_requests = Friendships.incoming_friend_requests(system_identity) - outgoing_requests = Friendships.outgoing_friend_requests(system_identity) - - if incoming_requests == [] and outgoing_requests == [] do - error_component( - "You don't have any incoming or outgoing friend requests. Add a friend with `/friend add`!" - ) - else - incoming_component = - if incoming_requests != [] do - [ - container( - [ - text("## Incoming friend requests (#{length(incoming_requests)})"), - separator(spacing: :large), - Enum.map(incoming_requests, fn - %{ - request: %{from_id: from_id}, - from: %{username: username, discord_id: discord_id} - } -> - text( - "- **#{username || from_id}**#{case discord_id do - nil -> "" - _ -> " (<@#{discord_id}>)" - end}" - ) - end) - ] - |> List.flatten() - ) - ] - else - [] - end - - outgoing_component = - if outgoing_requests != [] do - [ - container( - [ - text("## Outgoing friend requests (#{length(outgoing_requests)})"), - separator(spacing: :large), - Enum.map(outgoing_requests, fn - %{ - request: %{to_id: to_id}, - to: %{username: username, discord_id: discord_id} - } -> - text( - "- **#{username || to_id}**#{case discord_id do - nil -> "" - _ -> " (<@#{discord_id}>)" - end}" - ) - end) - ] - |> List.flatten() - ) - ] - else - [] - end - - [ - components: incoming_component ++ outgoing_component, - flags: cv2_flags() - ] - end - end - - defp send_friend_request(%{system_identity: system_identity}, to_identity, decorator) do - case Friendships.send_request(system_identity, to_identity) do - {:ok, :sent} -> - success_component("Sent a friend request to #{decorator}.") - - {:ok, :accepted} -> - success_component( - "You are now friends with #{decorator}!\n\nIf you'd like to add them as a trusted friend, use `/friend trust`." - ) - - {:error, :already_friends} -> - error_component("You are already friends with #{decorator}.") - - {:error, :already_sent_request} -> - error_component("You have already sent a friend request to #{decorator}.") - - {:error, :no_user} -> - error_component("That system #{decorator} does not exist.") - - {:error, _} -> - error_component( - "An unknown error occurred while sending the friend request. Please try again." - ) - end - end - - defp accept_friend_request(%{system_identity: system_identity}, from_identity, decorator) do - case Friendships.accept_request(from_identity, system_identity) do - :ok -> - success_component( - "You are now friends with #{decorator}!\n\nIf you'd like to add them as a trusted friend, use `/friend trust`." - ) - - {:error, :not_requested} -> - error_component("You do not have an incoming friend request from #{decorator}.") - - {:error, :no_user} -> - error_component("The system #{decorator} does not exist.") - - {:error, _} -> - error_component( - "An unknown error occurred while accepting the friend request. Please try again." - ) - end - end - - defp reject_friend_request(%{system_identity: system_identity}, from_identity, decorator) do - case Friendships.reject_request(from_identity, system_identity) do - :ok -> - success_component("You rejected the friend request from #{decorator}.") - - {:error, :not_requested} -> - error_component("You do not have an incoming friend request from #{decorator}.") - - {:error, :no_user} -> - error_component("The system #{decorator} does not exist.") - - {:error, _} -> - error_component( - "An unknown error occurred while rejecting the friend request. Please try again." - ) - end - end - - defp cancel_friend_request(%{system_identity: system_identity}, to_identity, decorator) do - case Friendships.cancel_request(system_identity, to_identity) do - :ok -> - success_component("You canceled the friend request to #{decorator}.") - - {:error, :not_requested} -> - error_component("You do not have an outgoing friend request to #{decorator}.") - - {:error, :no_user} -> - error_component("The system #{decorator} does not exist.") - - {:error, _} -> - error_component( - "An unknown error occurred while cancelling the friend request. Please try again." - ) - end - end -end diff --git a/lib/octocon_discord/commands/front.ex b/lib/octocon_discord/commands/front.ex deleted file mode 100644 index 21811e62..00000000 --- a/lib/octocon_discord/commands/front.ex +++ /dev/null @@ -1,335 +0,0 @@ -defmodule OctoconDiscord.Commands.Front do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.{ - Accounts, - Alters, - Fronts - } - - @subcommands %{ - "set" => &__MODULE__.set/2, - "end" => &__MODULE__.endd/2, - "add" => &__MODULE__.add/2, - "view" => &__MODULE__.view/2, - "main" => &__MODULE__.main/2, - "remove-main" => &__MODULE__.remove_main/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Manages which alters are in front." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{resolved: resolved, system_identity: {:discord, discord_id}}, - options - ) - end) - end - - def view(%{system_identity: system_identity}, _options) do - currently_fronting = Fronts.currently_fronting(system_identity) - - if currently_fronting == [] do - error_component( - "No alters are fronting! Use `/front add` to add alters to front, or `/front set` to set a single alter to front." - ) - else - [ - components: [ - container( - [ - text("## Currently fronting alters (#{length(currently_fronting)})"), - separator(spacing: :large), - Enum.map(currently_fronting, fn %{front: front, alter: alter, primary: primary} -> - inserted_at = - front.time_start |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_unix() - - text(""" - #{if primary, do: ":star: ", else: ""}**#{alter.name || "Unnamed alter"}**#{case alter.pronouns do - nil -> "" - pronouns -> " (#{pronouns})" - end} - - ID: `#{alter.id}`#{case alter.alias do - nil -> "" - alias -> " • Alias: `#{alias}`" - end} - - Fronting since: - #{if front.comment && front.comment != "", - do: "- Comment: #{front.comment}", - else: ""} - """) - end), - separator(spacing: :large), - text("*⭐ = Main front*") - ] - |> List.flatten() - ) - ], - flags: cv2_flags() - ] - end - end - - def set(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - comment = get_command_option(options, "comment") || "" - set_main? = get_command_option(options, "set-main") || false - - case Fronts.set_front(system_identity, alter_identity, comment) do - {:ok, _} -> - if set_main? do - Accounts.set_primary_front(system_identity, alter_id) - end - - success_component( - "This alter is now fronting. All other alters have been removed from front." - ) - - {:error, :already_fronting} -> - error_component("This alter is already fronting.") - - {:error, _} -> - error_component("An unknown error occurred.") - end - else - case alter_identity do - {:id, id} -> - error_component("You don't have an alter with ID **#{id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with alias **#{aliaz}**.") - end - end - end) - end - - def add(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - comment = get_command_option(options, "comment") || "" - set_main? = get_command_option(options, "set-main") || false - - case Fronts.start_front(system_identity, alter_identity, comment) do - {:ok, _} -> - if set_main? do - Accounts.set_primary_front(system_identity, alter_id) - end - - success_component("This alter is now fronting.") - - {:error, :already_fronting} -> - error_component("This alter is already fronting.") - - {:error, _} -> - error_component("An unknown error occurred.") - end - else - case alter_identity do - {:id, id} -> - error_component("You don't have an alter with ID **#{id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with alias **#{aliaz}**.") - end - end - end) - end - - def endd(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - case Fronts.end_front(system_identity, alter_identity) do - :ok -> - success_component("Alter with ID **#{alter_id}** was removed from front.") - - {:error, :not_fronting} -> - error_component("Alter with ID **#{alter_id}** is not currently fronting.") - - {:error, _} -> - error_component("An unknown error occurred.") - end - else - case alter_identity do - {:id, id} -> - error_component("You don't have an alter with ID **#{id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with alias **#{aliaz}**.") - end - end - end) - end - - def main(%{system_identity: system_identity}, options) do - with_id_or_alias(options, fn alter_identity -> - alter_id = Alters.resolve_alter(system_identity, alter_identity) - - if alter_id != false do - if Fronts.fronting?(system_identity, alter_identity) do - Accounts.set_primary_front(system_identity, alter_id) - - success_component("The alter with ID **#{alter_id}** is now set as main front.") - else - should_front = get_command_option(options, "add-to-front") || false - - if should_front do - add(%{system_identity: system_identity}, [ - %Nostrum.Struct.ApplicationCommandInteractionDataOption{ - name: "set-main", - value: true, - type: 5 - } - | options - ]) - else - error_component( - "That alter is not currently fronting.\n\n-# Hint: rerun this command with the `add-to-front` option to add the alter to front *and* set them as main front in one go!" - ) - end - end - else - case alter_identity do - {:id, id} -> - error_component("You don't have an alter with ID **#{id}**.") - - {:alias, aliaz} -> - error_component("You don't have an alter with alias **#{aliaz}**.") - end - end - end) - end - - def remove_main(%{system_identity: system_identity}, _options) do - Accounts.set_primary_front(system_identity, nil) - success_component("Removed main front.") - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "view", - description: "Views your currently fronting alters.", - type: :sub_command - }, - %{ - name: "add", - description: "Adds an alter to front.", - type: :sub_command, - options: [ - %{ - name: "id", - description: "The ID (or alias) of the alter to add to front.", - type: :string, - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "comment", - description: "An optional comment to add to the front.", - type: :string, - max_length: 50, - required: false - }, - %{ - name: "set-main", - description: "Whether to set the alter as main front.", - type: :boolean, - required: false - } - ] - }, - %{ - name: "end", - description: "Removes an alter from front.", - type: :sub_command, - options: [ - %{ - name: "id", - description: "The ID (or alias) of the alter to end fronting.", - type: :string, - max_length: 80, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "set", - description: "Sets an alter as front, replacing all other alters.", - type: :sub_command, - options: [ - %{ - name: "id", - description: "The ID (or alias) of the alter to set to front.", - type: :string, - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "comment", - description: "An optional comment to add to the front.", - type: :string, - max_length: 50, - required: false - }, - %{ - name: "set-main", - description: "Whether to set the alter as main front.", - type: :boolean, - required: false - } - ] - }, - %{ - name: "main", - description: "Sets the main fronting alter.", - type: :sub_command, - options: [ - %{ - name: "id", - description: "The ID (or alias) of the alter to set as main front.", - type: :string, - max_length: 80, - required: true, - autocomplete: true - }, - %{ - name: "add-to-front", - description: "Whether to add the alter to front if they are not already fronting.", - type: :boolean, - required: false - } - ] - }, - %{ - name: "remove-main", - description: "Removes the main fronting alter.", - type: :sub_command - } - ] -end diff --git a/lib/octocon_discord/commands/global_autoproxy.ex b/lib/octocon_discord/commands/global_autoproxy.ex deleted file mode 100644 index 935d2a1a..00000000 --- a/lib/octocon_discord/commands/global_autoproxy.ex +++ /dev/null @@ -1,80 +0,0 @@ -defmodule OctoconDiscord.Commands.GlobalAutoproxy do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Accounts - - alias OctoconDiscord.Cache - - @autoproxy_descriptions %{ - off: "Global autoproxying is now disabled.", - front: - "You will now automatically proxy as your current **main fronter** if applicable. If not, you will proxy as the **longest current fronter**.", - latch: - "You will now automatically proxy as the last alter to send a message. *This will take effect the next time you proxy.*" - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Changes your global autoproxy settings." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: options}} = interaction - system_identity = {:discord, discord_id} - - mode = get_command_option(options, "mode") - # This atom cast should be safe because Discord should only send us valid options - mode_atom = String.to_existing_atom(mode) - - new_settings = - %{ - global_autoproxy_mode: mode_atom - } - |> then(fn settings -> - if mode_atom != :latch do - Map.put(settings, :global_latched_alter, nil) - else - settings - end - end) - - case Accounts.update_discord_settings(system_identity, new_settings) do - {:ok, _} -> - Cache.Proxy.invalidate(discord_id) - - success_component( - "**Global** autoproxy mode set to `#{mode |> String.capitalize()}`.\n\n#{@autoproxy_descriptions[mode_atom]}\n\n⚠️ Please note that this setting applies to ***all*** servers with the Octocon bot. If you consider this to be a privacy issue, please use `/autoproxy` instead to change your autoproxy mode for this specific server." - ) - - {:error, _} -> - error_component("An unknown error occurred while updating your autoproxy mode.") - end - end) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "mode", - type: :string, - description: "The mode to set your global autoproxy to.", - required: true, - choices: [ - %{name: "off", value: "off"}, - %{name: "front", value: "front"}, - %{name: "latch", value: "latch"} - ] - } - ] -end diff --git a/lib/octocon_discord/commands/help.ex b/lib/octocon_discord/commands/help.ex deleted file mode 100644 index 092785d1..00000000 --- a/lib/octocon_discord/commands/help.ex +++ /dev/null @@ -1,23 +0,0 @@ -defmodule OctoconDiscord.Commands.Help do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias OctoconDiscord.Components.HelpHandler - - @impl Nosedrum.ApplicationCommand - def description, do: "Displays an interactive guide on how to use the Octocon bot." - - @impl Nosedrum.ApplicationCommand - def command(_interaction) do - HelpHandler.handle_init() - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - # @impl Nosedrum.ApplicationCommand - # def options, do: [] -end diff --git a/lib/octocon_discord/commands/messages/delete_proxied_message.ex b/lib/octocon_discord/commands/messages/delete_proxied_message.ex deleted file mode 100644 index 5c04977a..00000000 --- a/lib/octocon_discord/commands/messages/delete_proxied_message.ex +++ /dev/null @@ -1,66 +0,0 @@ -defmodule OctoconDiscord.Commands.Messages.DeleteProxiedMessage do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Messages - - alias Nostrum.Api - - @impl Nosedrum.ApplicationCommand - def description, do: "Deletes a proxied message." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{ - data: %{ - resolved: %Nostrum.Struct.ApplicationCommandInteractionDataResolved{ - messages: messages - } - }, - channel_id: channel_id, - user: %{ - id: user_id - } - } = interaction - - [ - {message_id, - %Nostrum.Struct.Message{ - author: %Nostrum.Struct.User{ - bot: is_bot - } - }} - ] = - messages - |> Enum.map(& &1) - - if is_bot do - case Messages.lookup_message(message_id) do - nil -> - error_component( - "This message either:\n\n- Was not proxied by Octocon.\n- Is more than 6 months old." - ) - - message -> - try_delete_message(user_id, channel_id, message) - end - else - error_component("You can only do this with messages proxied by Octocon.") - end - end - - defp try_delete_message(user_id, channel_id, %Messages.Message{} = message) do - if message.author_id == to_string(user_id) do - Api.Message.delete(channel_id, String.to_integer(message.message_id)) - success_component("Message deleted!") - else - error_component("You can only delete your own messages.") - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :message -end diff --git a/lib/octocon_discord/commands/messages/ping_account.ex b/lib/octocon_discord/commands/messages/ping_account.ex deleted file mode 100644 index 24b7a5b8..00000000 --- a/lib/octocon_discord/commands/messages/ping_account.ex +++ /dev/null @@ -1,76 +0,0 @@ -defmodule OctoconDiscord.Commands.Messages.PingAccount do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Messages - - @impl Nosedrum.ApplicationCommand - def description, do: "Pings the account associated with a proxied message." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{ - data: %{ - resolved: %Nostrum.Struct.ApplicationCommandInteractionDataResolved{ - messages: messages - } - }, - user: %{ - id: user_id - }, - guild_id: guild_id, - channel_id: channel_id - } = interaction - - [ - {message_id, - %Nostrum.Struct.Message{ - author: %Nostrum.Struct.User{ - bot: is_bot - } - }} - ] = - messages - |> Enum.map(& &1) - - if is_bot do - case Messages.lookup_message(message_id) do - nil -> - error_component( - "This message either:\n\n- Was not proxied by Octocon.\n- Is more than 6 months old." - ) - - message -> - permalink = "https://discord.com/channels/#{guild_id}/#{channel_id}/#{message_id}" - - [ - components: [ - text("<@#{message.author_id}>"), - container( - [ - text("### :bell: You've been pinged!"), - text( - if to_string(user_id) == to_string(message.author_id) do - "You have pinged yourself from a [proxied message](#{permalink})." - else - "<@#{user_id}> has pinged you from a [proxied message](#{permalink})." - end - ) - ], - %{accent_color: hex_to_int("#3F3793")} - ) - ], - flags: cv2_flags(false) - ] - end - else - error_component("You can only do this with messages proxied by Octocon.") - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :message -end diff --git a/lib/octocon_discord/commands/messages/reproxy.ex b/lib/octocon_discord/commands/messages/reproxy.ex deleted file mode 100644 index 76020ad9..00000000 --- a/lib/octocon_discord/commands/messages/reproxy.ex +++ /dev/null @@ -1,66 +0,0 @@ -defmodule OctoconDiscord.Commands.Messages.Reproxy do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Messages - - @impl Nosedrum.ApplicationCommand - def description, do: "Reproxies a message." - - @impl Nosedrum.ApplicationCommand - def command(%{ - data: %{ - resolved: %Nostrum.Struct.ApplicationCommandInteractionDataResolved{ - messages: messages - } - }, - user: %{id: user_id}, - guild_id: guild_id - }) do - [ - {message_id, - %Nostrum.Struct.Message{ - author: %Nostrum.Struct.User{ - bot: is_bot - } - } = raw_message} - ] = - messages - |> Enum.map(& &1) - - if is_bot do - case Messages.lookup_message(message_id) do - nil -> - error_component( - "This message either:\n\n- Was not proxied by Octocon.\n- Is more than 6 months old." - ) - - db_message -> - try_reproxy_message( - user_id, - %Nostrum.Struct.Message{raw_message | guild_id: guild_id}, - db_message - ) - end - else - error_component("You can only do this with messages proxied by Octocon.") - end - end - - defp try_reproxy_message(user_id, raw_message, db_message) do - if db_message.author_id == to_string(user_id) do - OctoconDiscord.Components.ReproxyHandler.handle_init(user_id, raw_message, db_message) - else - error_component("You can only reproxy your own messages.") - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :message - - # @impl Nosedrum.ApplicationCommand - # def options, do: [] -end diff --git a/lib/octocon_discord/commands/messages/who_is_this.ex b/lib/octocon_discord/commands/messages/who_is_this.ex deleted file mode 100644 index 510d754e..00000000 --- a/lib/octocon_discord/commands/messages/who_is_this.ex +++ /dev/null @@ -1,110 +0,0 @@ -defmodule OctoconDiscord.Commands.Messages.WhoIsThis do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.{ - Accounts, - Alters, - Messages - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Displays information about a proxied message." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{ - data: %{ - resolved: %Nostrum.Struct.ApplicationCommandInteractionDataResolved{ - messages: messages - } - }, - user: %{ - id: user_id - } - } = interaction - - [ - {message_id, - %Nostrum.Struct.Message{ - author: %Nostrum.Struct.User{ - bot: is_bot - } - }} - ] = - messages - |> Enum.map(& &1) - - if is_bot do - case Messages.lookup_message(message_id) do - nil -> - error_component( - "This message either:\n\n- Was not proxied by Octocon.\n- Is more than 6 months old." - ) - - message -> - display_message_data(to_string(user_id), message) - end - else - error_component("You can only do this with messages proxied by Octocon.") - end - end - - defp display_message_data(user_id, %Messages.Message{} = message) do - case Accounts.get_user({:discord, message.author_id}) do - nil -> - error_component( - "This user's Octocon account was deleted.\n\nHowever, this message was sent by the following Discord user: <@#{message.author_id}>\n\nWho had the following system ID: `#{message.system_id}`" - ) - - target_user -> - caller_user = Accounts.get_user({:discord, user_id}) - - [ - components: - ([ - generate_system_component(target_user, caller_user) - ] ++ maybe_alter_component(target_user.id, message.alter_id, caller_user)) - |> List.flatten(), - flags: cv2_flags() - ] - end - end - - defp generate_system_component(target_user, nil) do - system_component_raw(target_user, false) - end - - defp generate_system_component(target_user, caller_user) do - system_component_raw(target_user, target_user.id == caller_user.id) - end - - def maybe_alter_component(system_id, alter_id, caller_user) when caller_user.id == system_id do - case Alters.get_alter_by_id({:system, system_id}, {:id, alter_id}) do - {:error, _} -> - [] - - {:ok, alter} -> - alter_component(alter, false) - end - end - - def maybe_alter_component(system_id, alter_id, caller_user) do - caller_identity = - case caller_user do - nil -> nil - user -> {:system, user.id} - end - - case Alters.get_alter_guarded({:system, system_id}, {:id, alter_id}, caller_identity) do - :error -> [] - {:ok, alter} -> alter_component(alter, true) - end - end - - @impl Nosedrum.ApplicationCommand - def type, do: :message -end diff --git a/lib/octocon_discord/commands/octo.ex b/lib/octocon_discord/commands/octo.ex deleted file mode 100644 index 55c31bfd..00000000 --- a/lib/octocon_discord/commands/octo.ex +++ /dev/null @@ -1,47 +0,0 @@ -defmodule OctoconDiscord.Commands.Octo.Context do - defstruct [ - :initial_interaction, - :guild_id, - :discord_id, - :system_identity - ] -end - -defmodule OctoconDiscord.Commands.Octo do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - @impl Nosedrum.ApplicationCommand - def description, do: "Displays an all-in-one interface to interact with Octocon." - - @impl Nosedrum.ApplicationCommand - def command(_interaction) do - # %{guild_id: guild_id, user: %{id: discord_id}} = interaction - # guild_id = to_string(guild_id) - # discord_id = to_string(discord_id) - - # ensure_registered(discord_id, fn -> - # system_identity = {:discord, discord_id} - - # context = %__MODULE__.Context{ - # initial_interaction: interaction, - # guild_id: guild_id, - # discord_id: discord_id, - # system_identity: system_identity - # } - - # success_component("Test") - # end) - - error_component("This command is under construction.") - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, do: [] -end diff --git a/lib/octocon_discord/commands/register.ex b/lib/octocon_discord/commands/register.ex deleted file mode 100644 index bc1ba7b6..00000000 --- a/lib/octocon_discord/commands/register.ex +++ /dev/null @@ -1,25 +0,0 @@ -defmodule OctoconDiscord.Commands.Register do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Accounts - - @impl Nosedrum.ApplicationCommand - def description, do: "Creates a system under your Discord account." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - error_component( - "Octocon is shutting down. Sign-ups are disabled. Please see our Discord for more information: https://discord.com/invite/octocon" - ) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - # @impl Nosedrum.ApplicationCommand - # def options, do: [] -end diff --git a/lib/octocon_discord/commands/settings.ex b/lib/octocon_discord/commands/settings.ex deleted file mode 100644 index 5309c6c8..00000000 --- a/lib/octocon_discord/commands/settings.ex +++ /dev/null @@ -1,424 +0,0 @@ -defmodule OctoconDiscord.Commands.Settings do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.Accounts - - alias Octocon.Workers.{ - PluralKitImportWorker, - SimplyPluralImportWorker - } - - alias OctoconDiscord.Cache - - @subcommands %{ - "username" => &__MODULE__.username/2, - "remove-username" => &__MODULE__.remove_username/2, - "avatar" => &__MODULE__.Avatar.command/2, - "system-tag" => &__MODULE__.system_tag/2, - "remove-system-tag" => &__MODULE__.remove_system_tag/2, - "show-system-tag" => &__MODULE__.show_system_tag/2, - "proxy-case-sensitivity" => &__MODULE__.proxy_case_sensitivity/2, - "proxy-show-pronouns" => &__MODULE__.proxy_show_pronouns/2, - "ids-as-proxies" => &__MODULE__.ids_as_proxies/2, - "toggle-proxy-delay" => &__MODULE__.toggle_proxy_delay/2, - "toggle-silent-proxying" => &__MODULE__.toggle_silent_proxying/2, - "toggle-proxying" => &__MODULE__.toggle_proxying/2, - "import-pk" => &__MODULE__.import_pk/2, - "import-sp" => &__MODULE__.import_sp/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Modifies your system's settings." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, guild_id: guild_id, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{ - resolved: resolved, - system_identity: {:discord, discord_id}, - discord_id: discord_id, - guild_id: to_string(guild_id) - }, - options - ) - end) - end - - def username(%{system_identity: system_identity}, options) do - username = get_command_option(options, "username") - - case Accounts.update_username(system_identity, username) do - {:error, :already_linked} -> - error_component("Your username is already set to `#{username}`.") - - {:error, :user_exists} -> - error_component("The username `#{username}` is already taken.") - - {:error, _} -> - error_component( - "The username `#{username}` is invalid. It must satisfy the following criteria:\n\n- Between 5-16 characters\n- Only contains letters, numbers, dashes, and underscores\n- Does not start or end with a symbol\n- Does not consist of seven lowercase letters in a row (like a system ID)" - ) - - {:ok, _} -> - success_component("Your username has been changed to `#{username}`.") - end - end - - def remove_username(%{system_identity: system_identity}, _options) do - user = Accounts.get_user!(system_identity) - - case Accounts.update_user(user, %{username: nil}) do - {:ok, _} -> - success_component("Your username has been removed.") - - {:error, _} -> - error_component("An unknown error occurred while removing your username.") - end - end - - def system_tag(%{system_identity: system_identity}, options) do - tag = get_command_option(options, "tag") - - case Accounts.update_discord_settings(system_identity, %{system_tag: tag}) do - {:ok, _} -> - success_component("Your system tag has been changed to `#{tag}`.") - - {:error, %Ecto.Changeset{}} -> - error_component( - "The system tag `#{tag}` is invalid. It must satisfy the following criteria:\n\n- Between 1-8 characters" - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your system tag.") - end - end - - def remove_system_tag(%{system_identity: system_identity}, _options) do - case Accounts.update_discord_settings(system_identity, %{system_tag: nil}) do - {:ok, _} -> - success_component("Your system tag has been removed.") - - {:error, _} -> - error_component("An unknown error occurred while removing your system tag.") - end - end - - def show_system_tag(%{system_identity: system_identity, discord_id: discord_id}, _options) do - {:ok, %{settings: %{show_system_tag: show_system_tag}}} = Cache.Proxy.get(discord_id, false) - - new_value = not show_system_tag - - case Accounts.update_discord_settings(system_identity, %{show_system_tag: new_value}) do - {:ok, _} -> - success_component( - "Your system tag will now #{if new_value, do: "be", else: "no longer be"} shown when proxying. Servers can override this!" - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your system tag visibility.") - end - end - - def proxy_case_sensitivity( - %{system_identity: system_identity, discord_id: discord_id}, - _options - ) do - {:ok, %{settings: %{case_insensitive_proxies: case_insensitive_proxies}}} = - Cache.Proxy.get(discord_id, false) - - new_value = not case_insensitive_proxies - - case Accounts.update_discord_settings(system_identity, %{case_insensitive_proxies: new_value}) do - {:ok, _} -> - success_component( - "Proxying will now #{if new_value, do: "be", else: "no longer be"} case-insensitive." - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your proxy case sensitivity.") - end - end - - def proxy_show_pronouns(%{system_identity: system_identity, discord_id: discord_id}, _options) do - {:ok, %{settings: %{show_pronouns: show_pronouns}}} = Cache.Proxy.get(discord_id, false) - - new_value = not show_pronouns - - case Accounts.update_discord_settings(system_identity, %{show_pronouns: new_value}) do - {:ok, _} -> - success_component("Proxying will now #{if new_value, do: "show", else: "hide"} pronouns.") - - {:error, _} -> - error_component("An unknown error occurred while changing your proxy pronoun visibility.") - end - end - - def ids_as_proxies(%{system_identity: system_identity, discord_id: discord_id}, _options) do - {:ok, %{settings: %{ids_as_proxies: ids_as_proxies}}} = Cache.Proxy.get(discord_id, false) - - new_value = not ids_as_proxies - - case Accounts.update_discord_settings(system_identity, %{ids_as_proxies: new_value}) do - {:ok, _} -> - success_component( - "Alter IDs and aliases will now #{if new_value, do: "be", else: "no longer be"} automatically used as proxies.#{if new_value, do: "\n\nFor example, the message `1-Hello, world!` will be proxied as the alter with ID `1`, and the message `Atlas-Hello, world!` will be proxied as the alter with alias `Atlas`", else: ""}" - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your IDs as proxies setting.") - end - end - - def toggle_proxy_delay(%{system_identity: system_identity, discord_id: discord_id}, _options) do - {:ok, %{settings: %{use_proxy_delay: use_proxy_delay}}} = Cache.Proxy.get(discord_id, false) - - new_value = not use_proxy_delay - - case Accounts.update_discord_settings(system_identity, %{use_proxy_delay: new_value}) do - {:ok, _} -> - success_component( - if(new_value, - do: - "Octocon will now introduce a slight delay before proxying your message. This will prevent a Discord bug that causes duplicate \"ghost\" messages to appear when proxying.", - else: "Octocon will no longer introduce a slight delay before proxying your message." - ) - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your proxy delay setting.") - end - end - - def toggle_silent_proxying( - %{system_identity: system_identity, discord_id: discord_id}, - _options - ) do - {:ok, %{settings: %{silent_proxying: silent_proxying}}} = Cache.Proxy.get(discord_id, false) - - new_value = not silent_proxying - - case Accounts.update_discord_settings(system_identity, %{silent_proxying: new_value}) do - {:ok, _} -> - success_component( - if(new_value, - do: - "Octocon will now tell Discord to prevent proxied messages from sending push notifications.", - else: "Proxied messages will now send push notifications as normal." - ) - ) - - {:error, _} -> - error_component("An unknown error occurred while changing your proxy delay setting.") - end - end - - def toggle_proxying( - %{system_identity: system_identity, discord_id: discord_id, guild_id: guild_id}, - _options - ) do - {:ok, %{settings: %{server_settings: server_settings}}} = Cache.Proxy.get(discord_id, false) - - new_value = - not (Map.get(server_settings, guild_id, %{proxying_disabled: false}) - |> Map.get(:proxying_disabled)) - - case Accounts.update_server_settings(system_identity, guild_id, %{ - proxying_disabled: new_value - }) do - {:ok, _} -> - success_component( - "Proxying has been #{if new_value, do: "disabled", else: "enabled"} in this server." - ) - - _ -> - error_component("An error occurred while disabling proxying.") - end - end - - def import_pk(%{system_identity: system_identity}, options) do - pk_token = get_command_option(options, "token") - - system_id = Accounts.id_from_system_identity(system_identity, :system) - - spawn(fn -> - PluralKitImportWorker.perform(%{"system_id" => system_id, "pk_token" => pk_token}) - end) - - success_component( - "Octocon is attempting to import your alters from PluralKit. This may take a while; you will receive a DM from the Octocon bot when it is complete." - ) - end - - def import_sp(%{system_identity: system_identity}, options) do - sp_token = get_command_option(options, "token") - system_id = Accounts.id_from_system_identity(system_identity, :system) - - spawn(fn -> - SimplyPluralImportWorker.perform(%{"system_id" => system_id, "sp_token" => sp_token}) - end) - - success_component( - "Octocon is attempting to import your alters from Simply Plural. This may take a while; you will receive a DM from the Octocon bot when it is complete." - ) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "username", - description: "Changes your system's username.", - type: :sub_command, - options: [ - %{ - name: "username", - description: "The new username.", - type: :string, - min_length: 5, - max_length: 16, - required: true - } - ] - }, - %{ - name: "remove-username", - description: "Removes your system's username.", - type: :sub_command - }, - %{ - name: "avatar", - description: "Manages your system-wide avatar (profile picture).", - type: :sub_command_group, - options: [ - %{ - name: "set", - description: "Sets your system-wide avatar to the attached image.", - type: :sub_command, - options: [ - %{ - name: "avatar", - type: :attachment, - description: "The image to set.", - required: true - } - ] - }, - # %{ - # name: "set-url", - # description: "Sets your system-wide avatar to the provided URL.", - # type: :sub_command, - # options: [ - # %{ - # name: "avatar", - # type: :string, - # max_length: 2000, - # description: "The URL of the avatar to set.", - # required: true - # } - # ] - # }, - %{ - name: "remove", - description: "Removes your system-wide avatar.", - type: :sub_command - } - ] - }, - %{ - name: "system-tag", - description: "Changes your system tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - description: "The new system tag.", - type: :string, - min_length: 1, - max_length: 20, - required: true - } - ] - }, - %{ - name: "remove-system-tag", - description: "Removes your system tag.", - type: :sub_command - }, - %{ - name: "show-system-tag", - description: - "Toggles whether your system tag is shown when proxying. Servers can override this!", - type: :sub_command - }, - %{ - name: "proxy-case-sensitivity", - description: "Toggles whether proxying is case-insensitive.", - type: :sub_command - }, - %{ - name: "proxy-show-pronouns", - description: - "Toggles whether proxies automatically show an alter's pronouns in parentheses.", - type: :sub_command - }, - %{ - name: "ids-as-proxies", - description: "Toggles whether alter IDs and aliases are automatically used as proxies.", - type: :sub_command - }, - %{ - name: "toggle-proxy-delay", - description: "Toggles whether Octocon introduces a slight delay before proxying.", - type: :sub_command - }, - %{ - name: "toggle-silent-proxying", - description: "Toggles whether Octocon tells Discord to prevent push notifications.", - type: :sub_command - }, - %{ - name: "toggle-proxying", - description: "Toggles whether proxying is enabled in this server.", - type: :sub_command - }, - %{ - name: "import-pk", - description: "Imports your alters from PluralKit.", - type: :sub_command, - options: [ - %{ - name: "token", - description: "Your PluralKit token.", - type: :string, - required: true - } - ] - }, - %{ - name: "import-sp", - description: "Imports your alters from Simply Plural.", - type: :sub_command, - options: [ - %{ - name: "token", - description: "Your Simply Plural token (must have \"read\" permissions).", - type: :string, - required: true - } - ] - } - ] -end diff --git a/lib/octocon_discord/commands/settings/avatar.ex b/lib/octocon_discord/commands/settings/avatar.ex deleted file mode 100644 index dd058b5d..00000000 --- a/lib/octocon_discord/commands/settings/avatar.ex +++ /dev/null @@ -1,70 +0,0 @@ -defmodule OctoconDiscord.Commands.Settings.Avatar do - @moduledoc false - - use OctoconDiscord.Commands - - import Octocon.Utils.User, only: [upload_avatar: 2] - - alias Octocon.Accounts - - @subcommands %{ - "set" => &__MODULE__.set_attachment/2, - "set-url" => &__MODULE__.set_url/2, - "remove" => &__MODULE__.remove/2 - } - - def command(%{system_identity: system_identity} = context, options) do - subcommand = hd(options) - system = Accounts.get_user(system_identity) - - @subcommands[subcommand.name].( - context - |> Map.put(:system, system), - subcommand.options - ) - end - - def set_attachment( - %{resolved: resolved, system: system}, - options - ) do - avatar_id = get_command_option(options, "avatar") - attachment = resolved.attachments[avatar_id] - - cond do - attachment.height == nil or attachment.width == nil -> - error_component( - "That file doesn't appear to be a valid image. Please provide an image under 20 MB." - ) - - attachment.size > 20_000_000 -> - error_component( - "The image you provided is too large. Please provide an image that is less than 20 MB." - ) - - true -> - case upload_avatar(system, attachment.url) do - {:ok, _} -> - success_component("Your avatar has been updated.") - - {:error, _} -> - error_component("An unknown error occurred updating your avatar. Please try again.") - end - end - end - - def set_url(_context, _options) do - error_component("This command is not yet implemented.") - end - - def remove(%{system: system}, _options) do - case Accounts.update_user(system, %{avatar_url: nil}) do - {:ok, _} -> - Octocon.Utils.nuke_existing_avatars!(system.id, "self") - success_component("Your avatar has been removed.") - - {:error, _} -> - error_component("An unknown error occurred removing your avatar. Please try again.") - end - end -end diff --git a/lib/octocon_discord/commands/system.ex b/lib/octocon_discord/commands/system.ex deleted file mode 100644 index 21e4d5ad..00000000 --- a/lib/octocon_discord/commands/system.ex +++ /dev/null @@ -1,255 +0,0 @@ -defmodule OctoconDiscord.Commands.System do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias Octocon.{ - Accounts, - Alters, - Fronts - } - - @subcommands %{ - "me" => &__MODULE__.me/2, - "view" => &__MODULE__.view/2, - "alter" => &__MODULE__.alter/2, - "fronting" => &__MODULE__.fronting/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Views information about your or another user's system." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{ - data: %{resolved: resolved, options: [%{name: name, options: options}]}, - user: %{id: discord_id} - } = interaction - - discord_id = to_string(discord_id) - - callback = fn -> - @subcommands[name].(%{resolved: resolved, discord_id: discord_id}, options) - end - - # `/system view` can be used by unregistered users - case name do - "view" -> callback.() - _ -> ensure_registered(discord_id, callback) - end - end - - def me(%{discord_id: discord_id}, _options) do - system = Accounts.get_user!({:discord, discord_id}) - system_component(system, true) - end - - def view(_context, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, _ -> - system = Accounts.get_user!(identity) - system_component(system, false) - end) - end - - def alter(%{discord_id: discord_id}, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - alter_id = get_command_option(options, "alter-id") - - system_id_from_opts(opts, fn identity, _ -> - if alter_id_valid?(alter_id) do - case Alters.get_alter_guarded(identity, {:id, alter_id}, {:discord, discord_id}) do - :error -> - error_component( - "Could not access this alter. You may not have permission to view them." - ) - - {:ok, alter} -> - [ - components: - [ - text( - "**NOTE:** This alter's information is only visible to you. You probably shouldn't share this with anyone else." - ), - alter_component(alter, true) - ] - |> List.flatten(), - flags: cv2_flags() - ] - end - else - error_component("**#{alter_id}** is not a valid alter ID.") - end - end) - end - - def fronting(%{discord_id: discord_id}, options) do - opts = %{ - system_id: get_command_option(options, "system-id"), - discord_id: get_command_option(options, "discord"), - username: get_command_option(options, "username") - } - - system_id_from_opts(opts, fn identity, decorator -> - currently_fronting = - Fronts.currently_fronting_guarded(identity, {:discord, discord_id}) - - if currently_fronting == [] do - error_component( - "No alters are currently fronting in that system, or you do not have permission to view them." - ) - else - [ - components: [ - container( - [ - text( - "## Currently fronting in system #{decorator} (#{length(currently_fronting)})\n*Note: You may not have permission to view all fronting alters.*" - ), - separator(spacing: :large), - Enum.map(currently_fronting, fn %{front: front, alter: alter, primary: primary} -> - inserted_at = - front.time_start |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_unix() - - text(""" - #{if primary, do: ":star: ", else: ""}**#{alter.name || "Unnamed alter"}**#{case alter.pronouns do - nil -> "" - pronouns -> " (#{pronouns})" - end} - - ID: `#{alter.id}` - - Fronting since: - #{if front.comment && front.comment != "", - do: "- Comment: #{front.comment}", - else: ""} - """) - end), - separator(spacing: :large), - text("*⭐ = Main front*") - ] - |> List.flatten() - ) - ], - flags: cv2_flags() - ] - end - end) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "me", - description: "Views information about your system.", - type: :sub_command - }, - %{ - name: "view", - description: "Views information about another user's system.", - type: :sub_command, - options: [ - %{ - name: "system-id", - description: "The ID of the system to view.", - type: :string, - min_length: 7, - max_length: 7, - required: false - }, - %{ - name: "discord", - description: "The Discord ping of the user to view.", - type: :user, - required: false - }, - %{ - name: "username", - description: "The username of the user to view.", - type: :string, - min_length: 5, - max_length: 16, - required: false - } - ] - }, - %{ - name: "alter", - description: "Views information about another system's alter.", - type: :sub_command, - options: [ - %{ - name: "alter-id", - description: "The ID of the alter to view.", - type: :integer, - required: true - }, - %{ - name: "system-id", - description: "The ID of the system whose alter to view.", - type: :string, - min_length: 7, - max_length: 7, - required: false - }, - %{ - name: "discord", - description: "The Discord ping of the user whose alter to view.", - type: :user, - required: false - }, - %{ - name: "username", - description: "The username of the user whose alter to view.", - type: :string, - min_length: 5, - max_length: 16, - required: false - } - ] - }, - %{ - name: "fronting", - description: "Views the currently fronting alters of another system.", - type: :sub_command, - options: [ - %{ - name: "system-id", - description: "The ID of the system whose fronting alters to view.", - type: :string, - min_length: 7, - max_length: 7, - required: false - }, - %{ - name: "discord", - description: "The Discord ping of the user whose fronting alters to view.", - type: :user, - required: false - }, - %{ - name: "username", - description: "The username of the user whose fronting alters to view.", - type: :string, - min_length: 5, - max_length: 16, - required: false - } - ] - } - ] -end diff --git a/lib/octocon_discord/commands/tag.ex b/lib/octocon_discord/commands/tag.ex deleted file mode 100644 index b63f8b17..00000000 --- a/lib/octocon_discord/commands/tag.ex +++ /dev/null @@ -1,518 +0,0 @@ -defmodule OctoconDiscord.Commands.Tag do - @moduledoc false - - use OctoconDiscord.Commands - - @behaviour Nosedrum.ApplicationCommand - - alias OctoconDiscord.Components.TagPaginator - - alias Octocon.{ - Accounts, - Tags - } - - @subcommands %{ - "create" => &__MODULE__.create/2, - "delete" => &__MODULE__.delete/2, - "view" => &__MODULE__.view/2, - "security" => &__MODULE__.security/2, - "list" => &__MODULE__.list/2, - "set-parent" => &__MODULE__.set_parent/2, - "remove-parent" => &__MODULE__.remove_parent/2, - "edit" => &__MODULE__.edit/2, - "random" => &__MODULE__.random/2, - "add-alter" => &__MODULE__.add_alter/2, - "remove-alter" => &__MODULE__.remove_alter/2 - } - - @impl Nosedrum.ApplicationCommand - def description, do: "Manages your system's tags." - - @impl Nosedrum.ApplicationCommand - def command(interaction) do - %{data: %{resolved: resolved}, user: %{id: discord_id}} = interaction - discord_id = to_string(discord_id) - - ensure_registered(discord_id, fn -> - %{data: %{options: [%{name: name, options: options}]}} = interaction - - @subcommands[name].( - %{resolved: resolved, system_identity: {:discord, discord_id}, discord_id: discord_id}, - options - ) - end) - end - - def create(%{system_identity: system_identity}, options) do - name = get_command_option(options, "name") - - case Tags.create_tag(system_identity, name) do - {:ok, _} -> - success_component( - "Successfully created tag **#{name}**! You can view it with `/tag view`.\n\n**Note:** This tag is currently private. You can change this with `/tag security`." - ) - - {:error, _} -> - error_component("An unknown error occurred while creating the tag. Please try again.") - end - end - - def delete(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - - case Tags.delete_tag(system_identity, tag_id) do - :ok -> - success_component("Successfully deleted tag!") - - {:error, _} -> - error_component("An unknown error occurred while deleting the tag. Please try again.") - end - end - - def view(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - - case Tags.get_tag(system_identity, tag_id) do - nil -> - error_component("That tag does not exist.") - - tag -> - show = get_show_option(options) - - alters = - Octocon.Alters.get_alters_by_id_bounded(system_identity, tag.alters, [ - :id, - :name, - :pronouns, - :security_level - ]) - - [ - components: tag_component(tag, alters, show), - flags: cv2_flags(!show) - ] - end - end - - def random(%{system_identity: system_identity}, options) do - case Tags.get_random_tag(system_identity) do - nil -> - error_component("You don't have any tags to choose from.") - - {:ok, tag} -> - show = get_show_option(options) - - alters = - Octocon.Alters.get_alters_by_id_bounded(system_identity, tag.alters, [ - :id, - :name, - :pronouns, - :security_level - ]) - - [ - components: tag_component(tag, alters, show), - flags: cv2_flags(!show) - ] - end - end - - def list(%{system_identity: system_identity}, _options) do - system_id = Accounts.id_from_system_identity(system_identity, :system) - - tags = - Tags.get_tags({:system, system_id}) - |> Enum.sort_by(& &1.name) - - TagPaginator.handle_init(system_id, tags, length(tags)) - end - - def set_parent(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - parent_tag_id = get_command_option(options, "parent_tag") - - case Tags.set_parent_tag(system_identity, tag_id, parent_tag_id) do - :ok -> - success_component("Successfully updated the tag's parent tag!") - - {:error, :not_found} -> - error_component("The specified tag does not exist.") - - {:error, :tag_cycle} -> - error_component( - "Setting that parent tag would create a cycle. Please choose a different tag." - ) - - {:error, _} -> - error_component("An unknown error occurred while updating the tag. Please try again.") - end - end - - def remove_parent(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - - case Tags.remove_parent_tag(system_identity, tag_id) do - :ok -> - success_component("Successfully removed the tag's parent tag!") - - {:error, :not_found} -> - error_component("The specified tag does not exist.") - - {:error, _} -> - error_component("An unknown error occurred while updating the tag. Please try again.") - end - end - - def add_alter(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - idalias = get_command_option(options, "alter") - - with_id_or_alias(idalias, fn alter_identity -> - case Tags.attach_alter_to_tag(system_identity, tag_id, alter_identity) do - :ok -> - success_component("Successfully added the alter to the tag!") - - {:error, :alter_not_found} -> - error_component("You don't have an alter with the ID or alias **#{idalias}**.") - - {:error, _} -> - error_component("An unknown error occurred while updating the tag. Please try again.") - end - end) - end - - def remove_alter(%{system_identity: system_identity}, options) do - tag_id = get_command_option(options, "tag") - idalias = get_command_option(options, "alter") - - with_id_or_alias(idalias, fn alter_identity -> - case Tags.detach_alter_from_tag(system_identity, tag_id, alter_identity) do - :ok -> - success_component("Successfully added the alter to the tag!") - - {:error, :alter_not_found} -> - error_component("You don't have an alter with the ID or alias **#{idalias}**.") - - {:error, _} -> - error_component("An unknown error occurred while updating the tag. Please try again.") - end - end) - end - - def update_tag( - %{system_identity: system_identity}, - tag_id, - options, - success_text, - embed_tag \\ true - ) do - case options do - map when map_size(map) == 0 -> - error_component("You must provide at least one field to update.") - - _ -> - case Tags.update_tag(system_identity, tag_id, options) do - {:ok, tag} -> - [ - components: - if embed_tag do - alters = - Octocon.Alters.get_alters_by_id_bounded(system_identity, tag.alters, [ - :id, - :name, - :pronouns, - :security_level - ]) - - [ - success_component_raw(success_text), - tag_component( - tag, - alters, - false - ) - ] - else - [success_component_raw(success_text)] - end - |> List.flatten(), - flags: cv2_flags() - ] - - {:error, :not_found} -> - error_component("You don't have a tag with ID **#{tag_id}**.") - - {:error, _} -> - error_component("An unknown error occurred while updating the tag. Please try again.") - end - end - end - - def edit(context, options) do - tag_id = get_command_option(options, "tag") - - to_update = - %{ - name: get_command_option(options, "name"), - description: get_command_option(options, "description"), - color: get_command_option(options, "color") - } - |> Map.filter(fn {_, v} -> v != nil end) - - # This is ugly, but it works. - try do - if Map.has_key?(to_update, :color) do - case validate_hex_color(to_update[:color]) do - :error -> - throw("Invalid color. Please provide a valid hex code.") - - {:ok, new_color} -> - update_tag( - context, - tag_id, - Map.put(to_update, :color, "#" <> new_color), - "Successfully edited tag!" - ) - end - else - update_tag( - context, - tag_id, - to_update, - "Successfully edited tag!" - ) - end - catch - e -> error_component(e) - end - end - - def security(context, options) do - security_level = get_command_option(options, "level") |> String.to_existing_atom() - - tag_id = get_command_option(options, "tag") - - update_tag( - context, - tag_id, - %{security_level: security_level}, - "Successfully updated this tag's security level!", - true - ) - end - - @impl Nosedrum.ApplicationCommand - def type, do: :slash - - @impl Nosedrum.ApplicationCommand - def options, - do: [ - %{ - name: "create", - description: "Creates a new tag.", - type: :sub_command, - options: [ - %{ - name: "name", - type: :string, - max_length: 100, - description: "The name of the tag to create.", - required: true - } - ] - }, - %{ - name: "delete", - description: "Deletes an existing tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - max_length: 36, - description: "The tag to delete.", - required: true, - autocomplete: true - } - ] - }, - %{ - name: "view", - description: "Views an existing tag.", - type: :sub_command, - options: - [ - %{ - name: "tag", - type: :string, - max_length: 36, - description: "The tag to view.", - required: true, - autocomplete: true - } - ] - |> add_show_option() - }, - %{ - name: "random", - description: "Views a random tag.", - type: :sub_command, - options: add_show_option([]) - }, - %{ - name: "list", - description: "Lists all of your tags.", - type: :sub_command, - options: [] - }, - %{ - name: "security", - description: "Manages a tag's security level.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - }, - %{ - name: "level", - type: :string, - description: "The security level to set the tag to.", - required: true, - choices: [ - %{name: "Private", value: "private"}, - %{name: "Trusted friends only", value: "trusted_only"}, - %{name: "Friends only", value: "friends_only"}, - %{name: "Public", value: "public"} - ] - } - ] - }, - %{ - name: "edit", - description: "Edits an existing tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - }, - %{ - name: "name", - type: :string, - max_length: 80, - description: "The new name of the tag.", - required: false - }, - %{ - name: "description", - type: :string, - max_length: 3000, - description: "The new description of the tag.", - required: false - }, - %{ - name: "color", - type: :string, - min_length: 6, - max_length: 7, - description: "The new color (hex code) of the tag.", - required: false - } - ] - }, - %{ - name: "set-parent", - description: "Sets the parent tag of an existing tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - }, - %{ - name: "parent_tag", - type: :string, - description: "The new parent tag.", - max_length: 36, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "remove-parent", - description: "Removes the parent tag of an existing tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "add-alter", - description: "Adds an alter to a tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - }, - %{ - name: "alter", - type: :string, - description: "The ID (or alias) of the alter to add to the tag.", - max_length: 80, - required: true, - autocomplete: true - } - ] - }, - %{ - name: "remove-alter", - description: "Removes an alter from a tag.", - type: :sub_command, - options: [ - %{ - name: "tag", - type: :string, - description: "The tag to update.", - max_length: 36, - required: true, - autocomplete: true - }, - %{ - name: "alter", - type: :string, - description: "The ID (or alias) of the alter to remove from the tag.", - max_length: 80, - required: true, - autocomplete: true - } - ] - } - ] -end diff --git a/lib/octocon_discord/components.ex b/lib/octocon_discord/components.ex deleted file mode 100644 index aa64a2d1..00000000 --- a/lib/octocon_discord/components.ex +++ /dev/null @@ -1,46 +0,0 @@ -defmodule OctoconDiscord.Components do - @moduledoc false - - use Supervisor - - @dispatchers %{ - "alter" => __MODULE__.AlterHandler, - "alter-pag" => __MODULE__.AlterPaginator, - "tag" => __MODULE__.TagHandler, - "tag-pag" => __MODULE__.TagPaginator, - "wipe-alters" => __MODULE__.WipeAltersHandler, - "wipe-tags" => __MODULE__.WipeTagsHandler, - "delete-account" => __MODULE__.DeleteAccountHandler, - "help" => __MODULE__.HelpHandler, - "reproxy" => __MODULE__.ReproxyHandler - } - - @dispatcher_servers [ - __MODULE__.AlterPaginator, - __MODULE__.TagPaginator, - __MODULE__.WipeAltersHandler, - __MODULE__.WipeTagsHandler, - __MODULE__.DeleteAccountHandler, - __MODULE__.HelpHandler, - __MODULE__.ReproxyHandler - ] - - def start_link(_), do: Supervisor.start_link(__MODULE__, [], name: __MODULE__) - - def init([]) do - Supervisor.init(@dispatcher_servers, strategy: :one_for_one) - end - - def dispatch(interaction) do - [type, action, uid] = String.split(interaction.data.custom_id, "|") - - uid = - case Integer.parse(uid) do - {int, ""} -> int - {_int, _rest} -> uid - :error -> uid - end - - Map.get(@dispatchers, type).handle_interaction(action, uid, interaction) - end -end diff --git a/lib/octocon_discord/components/alter_handler.ex b/lib/octocon_discord/components/alter_handler.ex deleted file mode 100644 index 0ea750e1..00000000 --- a/lib/octocon_discord/components/alter_handler.ex +++ /dev/null @@ -1,102 +0,0 @@ -defmodule OctoconDiscord.Components.AlterHandler do - import OctoconDiscord.Utils.Components - - alias Nostrum.Api - alias Octocon.Fronts - - def handle_interaction("view", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - alter = - Octocon.Alters.get_alter_by_id!(system_identity, {:id, alter_id}) - - send_response(alter_component(alter, false), interaction) - end - - def handle_interaction("addfront", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - case Fronts.start_front(system_identity, {:id, alter_id}) do - {:ok, _} -> - handle_interaction("view", alter_id, interaction) - - {:error, :already_fronting} -> - error_component_raw("This alter is already fronting.") - |> send_response(interaction) - - {:error, _} -> - error_component_raw("An unknown error occurred.") - |> send_response(interaction) - end - end - - def handle_interaction("removefront", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - case Fronts.end_front(system_identity, {:id, alter_id}) do - :ok -> - handle_interaction("view", alter_id, interaction) - - {:error, :not_fronting} -> - error_component_raw("This alter is not currently fronting.") - |> send_response(interaction) - - {:error, _} -> - error_component_raw("An unknown error occurred.") - |> send_response(interaction) - end - end - - def handle_interaction("setfront", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - case Fronts.set_front(system_identity, {:id, alter_id}) do - {:ok, _} -> - handle_interaction("view", alter_id, interaction) - - {:error, :already_fronting} -> - error_component_raw("This alter is already fronting.") - |> send_response(interaction) - - {:error, _} -> - error_component_raw("An unknown error occurred.") - |> send_response(interaction) - end - end - - def handle_interaction("setprimary", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - if Fronts.fronting?(system_identity, {:id, alter_id}) do - Octocon.Accounts.set_primary_front(system_identity, alter_id) - - handle_interaction("view", alter_id, interaction) - else - error_component_raw("This alter is not currently fronting.") - |> send_response(interaction) - end - end - - def handle_interaction("removeprimary", alter_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - if Fronts.fronting?(system_identity, {:id, alter_id}) do - Octocon.Accounts.set_primary_front(system_identity, nil) - - handle_interaction("view", alter_id, interaction) - else - error_component_raw("This alter is not currently fronting.") - |> send_response(interaction) - end - end - - defp send_response(components, interaction) do - Api.Interaction.create_response(interaction, %{ - type: 7, - data: %{ - components: if(is_list(components), do: components, else: [components]), - flags: OctoconDiscord.Utils.CV2.cv2_flags() - } - }) - end -end diff --git a/lib/octocon_discord/components/alter_paginator.ex b/lib/octocon_discord/components/alter_paginator.ex deleted file mode 100644 index 479e938d..00000000 --- a/lib/octocon_discord/components/alter_paginator.ex +++ /dev/null @@ -1,74 +0,0 @@ -defmodule OctoconDiscord.Components.AlterPaginator do - @moduledoc false - use OctoconDiscord.Components.Paginator, - noun: "alter", - interaction_id: "alter-pag", - create_command: "alter create", - list_command: "alter list" - - alias OctoconDiscord.Utils.Emojis - - def generate_page( - page_alters, - %{ - items_length: alters_length, - current_page: current_page, - total_pages: total_pages - } - ) do - container( - [ - text( - "## Your alters (#{alters_length})\n\nClick an alter's button to view more details." - ), - separator(spacing: :large), - Enum.map(page_alters, fn alter -> - [ - section( - [ - text(""" - **#{alter.name || "Unnamed alter"}**#{case alter.pronouns do - nil -> "" - pronouns -> " (#{pronouns})" - end} - - ID: `#{alter.id}`#{case alter.alias do - nil -> "" - alias -> " • Alias: `#{alias}`" - end} - #{case alter.discord_proxies do - [] -> "" - nil -> "" - proxies -> "- Proxies: #{Enum.map_join(proxies, " • ", fn proxy -> "`#{proxy}`" end)}" - end} - """) - ], - button( - "alter|view|#{alter.id}", - :secondary, - emoji: Emojis.component_emoji(Emojis.open()) - ) - ) - ] - end), - if total_pages > 1 do - [ - separator(spacing: :large), - text("Page #{current_page}/#{total_pages}") - ] - else - [] - end - ] - |> List.flatten() - ) - end - - def map_items(alters) do - alters - |> Enum.map(fn alter -> - Map.take(alter, [:id, :name, :pronouns, :discord_proxies, :alias]) - end) - end - - def generate_extra_data(_alters), do: nil -end diff --git a/lib/octocon_discord/components/delete_account_handler.ex b/lib/octocon_discord/components/delete_account_handler.ex deleted file mode 100644 index 6bf51e54..00000000 --- a/lib/octocon_discord/components/delete_account_handler.ex +++ /dev/null @@ -1,125 +0,0 @@ -defmodule OctoconDiscord.Components.DeleteAccountHandler do - @moduledoc false - use GenServer - - import OctoconDiscord.Utils.CV2 - - alias Nostrum.Api - alias Octocon.Accounts - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def handle_init(system_identity) do - user = Accounts.get_user!(system_identity) - - uid = :erlang.unique_integer([:positive]) - - data = %{ - system_id: user.id, - uid: uid, - confirmations_left: 3 - } - - :ets.insert(__MODULE__, {uid, data}) - - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(5)) - - OctoconDiscord.Utils.send_dm(user, generate_response(data)) - end - - defp generate_response(%{ - system_id: _system_id, - uid: uid, - confirmations_left: confirmations_left - }) - when confirmations_left > 0 do - [ - components: [ - container( - [ - text("### Delete account"), - text( - "**WARNING**: This command will **permanently** delete your system, as well as all associated data and settings.\n\nTo confirm, click the button below a total of **3** times." - ) - ], - %{accent_color: 0xFF0000} - ), - action_row([ - button( - "delete-account|confirm|#{uid}", - :danger, - label: "Confirm (#{3 - confirmations_left}/3)", - emoji: %{name: "⚠️"} - ) - ]) - ], - flags: cv2_flags(false) - ] - end - - defp generate_response(%{ - system_id: system_id, - uid: _uid, - confirmations_left: _confirmations_left - }) do - Accounts.delete_user({:system, system_id}) - - [ - components: [ - container( - [ - text("### :wave: Success!"), - text( - "**Your system has been deleted**. We hope you enjoyed using Octocon!\n\nIf you'd like to create a new account, you can run the `/register` command.\n\nIf you have any feedback, questions, or concerns, feel free to join our community server: https://octocon.app/discord" - ) - ], - %{accent_color: 0x00FF00} - ) - ], - flags: cv2_flags(false) - ] - end - - def handle_interaction("confirm", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | confirmations_left: old_data.confirmations_left - 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end -end diff --git a/lib/octocon_discord/components/help_handler.ex b/lib/octocon_discord/components/help_handler.ex deleted file mode 100644 index 2541dfc0..00000000 --- a/lib/octocon_discord/components/help_handler.ex +++ /dev/null @@ -1,189 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler do - @moduledoc false - use GenServer - - import OctoconDiscord.Utils.Components - - alias Nostrum.Api - - alias OctoconDiscord.Components.HelpHandler.Pages - - @pages %{ - root: Pages.Root, - command_list: Pages.CommandList, - help: Pages.CommandList.Help, - register: Pages.CommandList.Register, - settings_root: Pages.CommandList.Settings, - settings_import_pk: Pages.CommandList.Settings.ImportPk, - settings_import_sp: Pages.CommandList.Settings.ImportSp, - settings_username: Pages.CommandList.Settings.Username, - settings_avatar: Pages.CommandList.Settings.Avatar, - settings_system_tag: Pages.CommandList.Settings.SystemTag, - settings_show_system_tag: Pages.CommandList.Settings.ShowSystemTag, - settings_proxy_case_sensitivity: Pages.CommandList.Settings.ProxyCaseSensitivity, - settings_proxy_show_pronouns: Pages.CommandList.Settings.ProxyShowPronouns, - settings_ids_as_aliases: Pages.CommandList.Settings.IdsAsAliases, - alter_root: Pages.CommandList.Alter, - alter_create: Pages.CommandList.Alter.Create, - alter_view: Pages.CommandList.Alter.View, - alter_edit: Pages.CommandList.Alter.Edit, - alter_delete: Pages.CommandList.Alter.Delete, - alter_list: Pages.CommandList.Alter.List, - alter_security: Pages.CommandList.Alter.Security, - alter_avatar: Pages.CommandList.Alter.Avatar, - alter_proxy: Pages.CommandList.Alter.Proxy, - alter_remove_alias: Pages.CommandList.Alter.RemoveAlias, - front_root: Pages.CommandList.Front, - front_add: Pages.CommandList.Front.Add, - front_set: Pages.CommandList.Front.Set, - front_end: Pages.CommandList.Front.End, - front_view: Pages.CommandList.Front.View, - front_main: Pages.CommandList.Front.Main, - front_remove_main: Pages.CommandList.Front.RemoveMain, - friend_root: Pages.CommandList.Friend, - friend_list: Pages.CommandList.Friend.List, - friend_list_requests: Pages.CommandList.Friend.ListRequests, - friend_add: Pages.CommandList.Friend.Add, - friend_remove: Pages.CommandList.Friend.Remove, - friend_accept: Pages.CommandList.Friend.Accept, - friend_reject: Pages.CommandList.Friend.Reject, - friend_cancel: Pages.CommandList.Friend.Cancel, - friend_trust: Pages.CommandList.Friend.Trust, - friend_untrust: Pages.CommandList.Friend.Untrust, - autoproxy: Pages.CommandList.Autoproxy, - global_autoproxy: Pages.CommandList.GlobalAutoproxy, - admin_root: Pages.CommandList.Admin, - admin_channel_blacklist: Pages.CommandList.Admin.ChannelBlacklist, - admin_view_settings: Pages.CommandList.Admin.ViewSettings, - admin_force_system_tags: Pages.CommandList.Admin.ForceSystemTags, - admin_log_channel: Pages.CommandList.Admin.LogChannel, - danger_root: Pages.CommandList.Danger, - danger_wipe_alters: Pages.CommandList.Danger.WipeAlters, - danger_delete_account: Pages.CommandList.Danger.DeleteAccount, - bot_info: Pages.CommandList.BotInfo, - faq_root: Pages.Faq, - faq_get_started: Pages.Faq.GetStarted, - faq_import_alters: Pages.Faq.ImportAlters, - faq_commands_private: Pages.Faq.CommandsPrivate, - faq_invite_octocon: Pages.Faq.InviteOctocon, - faq_who_can_see: Pages.Faq.WhoCanSee, - faq_ids_aliases: Pages.Faq.IdsAliases, - faq_autoproxy: Pages.Faq.Autoproxy, - faq_delete_alters: Pages.Faq.DeleteAlters, - faq_another_question: Pages.Faq.AnotherQuestion, - resources: Pages.Resources - } - - def pages, do: @pages - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def handle_init do - uid = :erlang.unique_integer([:positive]) - - data = %{ - current_page: :root, - uid: uid - } - - :ets.insert(__MODULE__, {uid, data}) - - # [TODO]: Possibly clean up correlations after a while? - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(10)) - - generate_response(data) - |> Keyword.put(:ephemeral?, true) - end - - defp generate_response(%{current_page: current_page, uid: uid}) do - page = Map.get(@pages, current_page) - - [ - embeds: page.embeds(), - components: page.components(uid) - ] - end - - def handle_interaction("nav", uid, interaction) do - page = interaction.data.values |> hd() |> String.to_existing_atom() - - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: page - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> create_expired_response(interaction) - end - - def handle_interaction("nav-" <> page, uid, interaction) do - page_atom = String.to_existing_atom(page) - - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: page_atom - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> create_expired_response(interaction) - end - - defp create_expired_response(interaction) do - Api.create_interaction_response(interaction, %{ - type: 7, - data: - error_component("This help interface has expired. Please run `/help` again.") - |> Enum.into(%{}) - |> Map.drop([:flags]) - |> Map.put(:components, nil) - }) - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - @impl GenServer - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - @impl GenServer - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end -end diff --git a/lib/octocon_discord/components/help_handler/pages.ex b/lib/octocon_discord/components/help_handler/pages.ex deleted file mode 100644 index f89eb2ba..00000000 --- a/lib/octocon_discord/components/help_handler/pages.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages do - defmacro __using__(_opts) do - quote do - alias OctoconDiscord.{ - Utils, - Utils.Emojis - } - - alias Nostrum.Struct.{ - Embed, - Emoji - } - - alias Nostrum.Struct.Component.Button - - def map_emoji(%Nostrum.Struct.Emoji{id: id, name: name}) do - %{id: id, name: name} - end - - def back_button(page, uid) do - Button.interaction_button( - "Back", - "help|nav-#{page}|#{uid}", - emoji: Emojis.back(), - style: 2 - ) - end - end - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list.ex b/lib/octocon_discord/components/help_handler/pages/command_list.ex deleted file mode 100644 index efe300e9..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list.ex +++ /dev/null @@ -1,135 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "help", - description: "Interactive help guide", - category: false, - nav_page: "help" - }, - %{ - name: "register", - description: "Register a new account using Discord", - category: false, - nav_page: "register" - }, - %{ - name: "settings", - description: "Manage your settings and preferences", - category: true, - nav_page: "settings_root" - }, - %{ - name: "alter", - description: "Manage your alters and their profiles", - category: true, - nav_page: "alter_root" - }, - %{ - name: "front", - description: "Manage your front status", - category: true, - nav_page: "front_root" - }, - %{ - name: "friend", - description: "Manage your friends and friend requests", - category: true, - nav_page: "friend_root" - }, - %{ - name: "autoproxy", - description: "Manage your server-specific autoproxy settings", - category: false, - nav_page: "autoproxy" - }, - %{ - name: "global-autoproxy", - description: "Manage your global autoproxy settings", - category: false, - nav_page: "global_autoproxy" - }, - %{ - name: "admin", - description: "Tools for server administrators and moderators", - category: true, - nav_page: "admin_root" - }, - %{ - name: "danger", - description: "Dangerous commands that require extra confirmation", - category: true, - nav_page: "danger_root" - }, - %{ - name: "bot-info", - description: "Shows technical info about the Octocon bot", - category: false, - nav_page: "bot_info" - } - ] - - # # Joining this at compile-time avoids unnecessary work at runtime - # @commands_joined @commands - # |> Enum.map(fn %{name: name, description: description, category: category} -> - # """ - # ### #{if category, do: "#{Emojis.folder()} ", else: ""}/#{name} - # > #{description} - # """ - # end) - # |> Enum.join() - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} Command list", - color: Utils.hex_to_int("#3F3793"), - description: """ - Octocon uses `/slash commands` to interact with the bot. Choose a command or category below to view more information! - ### Note - > All commands are **ephemeral**, meaning they only show up for you and are not visible to others. - > - > You can use Octocon commands in public without worrying about other people seeing your data! - - #{Emojis.folder()} *= Category* - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{ - name: name, - description: description, - nav_page: nav_page, - category: category - } -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(if category, do: Emojis.folder(), else: Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/admin.ex b/lib/octocon_discord/components/help_handler/pages/command_list/admin.ex deleted file mode 100644 index 66c8a076..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/admin.ex +++ /dev/null @@ -1,68 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Admin do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "view-settings", - description: "Views the server's admin settings", - nav_page: "admin_view_settings" - }, - %{ - name: "channel-blacklist", - description: "Manages channels that can't be proxied in", - nav_page: "admin_channel_blacklist" - }, - %{ - name: "log-channel", - description: "Manages the proxy log channel for this server", - nav_page: "admin_log_channel" - }, - %{ - name: "force-system-tags", - description: "Toggles whether system tags are forced on this server", - nav_page: "admin_force_system_tags" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/admin`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/admin` command group contains utilities for server administrators. Choose a subcommand below to learn more about it! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/admin/channel_blacklist.ex b/lib/octocon_discord/components/help_handler/pages/command_list/admin/channel_blacklist.ex deleted file mode 100644 index ecc68535..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/admin/channel_blacklist.ex +++ /dev/null @@ -1,48 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Admin.ChannelBlacklist do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/admin channel-blacklist`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/admin channel-blacklist` command group manages this server's channel blacklist. Channels (or entire categories) in this blacklist cannot be proxied in. - ## #{Emojis.slashcommand()} `/admin channel-blacklist list` - Views which channels/categories are in the channel blacklist. - ### Usage - ``` - /admin channel-blacklist list - ``` - ## #{Emojis.slashcommand()} `/admin channel-blacklist add` - Adds a channel or category to the channel blacklist. - ### Usage - ``` - /admin channel-blacklist add - ``` - ### Parameters - - `channel`: The channel or category to add to the channel blacklist. - ## #{Emojis.slashcommand()} `/admin channel-blacklist remove` - Removes a channel or category from the channel blacklist. - ### Usage - ``` - /admin channel-blacklist remove - ``` - ### Parameters - - `channel`: The channel or category to remove from the channel blacklist. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("admin_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/admin/force_system_tags.ex b/lib/octocon_discord/components/help_handler/pages/command_list/admin/force_system_tags.ex deleted file mode 100644 index 904caf15..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/admin/force_system_tags.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Admin.ForceSystemTags do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/admin force-system-tags`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/admin force-system-tags` command toggles whether system tags are forced to be used on this server when proxying. This **overrides** every user's personal setting! - ### Usage - ``` - /admin force-system-tags - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("admin_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/admin/log_channel.ex b/lib/octocon_discord/components/help_handler/pages/command_list/admin/log_channel.ex deleted file mode 100644 index 7a822c38..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/admin/log_channel.ex +++ /dev/null @@ -1,40 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Admin.LogChannel do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/admin log-channel`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/admin log-channel` command group manages this server's log channel. Whenever a message is proxied, a log message detailing the message and its sender's Discord account is sent to this channel. - ## #{Emojis.slashcommand()} `/admin log-channel set` - Sets the log channel for this server. - ### Usage - ``` - /admin log-channel set - ``` - ### Parameters - - `channel`: The channel to set as the log channel. - ## #{Emojis.slashcommand()} `/admin log-channel remove` - Removes the log channel for this server. - ### Usage - ``` - /admin channel-blacklist remove - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("admin_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/admin/view_settings.ex b/lib/octocon_discord/components/help_handler/pages/command_list/admin/view_settings.ex deleted file mode 100644 index c44ab8db..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/admin/view_settings.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Admin.ViewSettings do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/admin view-settings`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/admin view-settings` command views this server's current admin settings. - ### Usage - ``` - /admin view-settings - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("admin_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter.ex deleted file mode 100644 index bbcf2d87..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter.ex +++ /dev/null @@ -1,100 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "create", - description: "Creates a new alter", - nav_page: "alter_create" - }, - %{ - name: "view", - description: "Views an alter's profile", - nav_page: "alter_view" - }, - %{ - name: "edit", - description: "Edits an alter's profile", - nav_page: "alter_edit" - }, - %{ - name: "delete", - description: "Deletes an alter", - nav_page: "alter_delete" - }, - %{ - name: "list", - description: "Lists all of your alters", - nav_page: "alter_list" - }, - %{ - name: "security", - description: "Manages an alter's security level", - nav_page: "alter_security" - }, - %{ - name: "avatar", - description: "Manages an alter's avatar (profile picture)", - nav_page: "alter_avatar" - }, - %{ - name: "proxy", - description: "Manages an alter's proxies", - nav_page: "alter_proxy" - }, - %{ - name: "remove-alias", - description: "Removes an alter's alias", - nav_page: "alter_remove_alias" - }, - %{ - name: "remove-proxy-name", - description: "Removes an alter's proxy name", - nav_page: "alter_remove_proxy_name" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/alter`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter` command group manages your alters. Choose a subcommand below to learn more about it! - - **Note**: Most alter commands require you to specify an alter. You can do this by their numerical ID **or** their *alias*. Aliases are unique alphanumeric IDs you can assign to your alters to make Discord commands easier to use. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/avatar.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/avatar.ex deleted file mode 100644 index 7f3c5cd8..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/avatar.ex +++ /dev/null @@ -1,43 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.Avatar do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/alter avatar`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter avatar` command group modifies an alter's avatar (profile picture). - ## #{Emojis.slashcommand()} `/alter avatar set` - Sets an image as an alter's new avatar. - ### Usage - ``` - /alter avatar set - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose avatar to set. - - `avatar`: The image to set as the alter's new avatar. Must be a PNG, JPEG, or WebP file. - ## #{Emojis.slashcommand()} `/settings avatar remove` - Removes an alter's current avatar. - ### Usage - ``` - /alter avatar remove - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose avatar to remove. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/create.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/create.ex deleted file mode 100644 index e7dac1b6..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/create.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.Create do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter create`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter create` command creates a new alter. - ### Usage - ``` - /alter create - ``` - ### Parameters - - `name`: The name of the new alter. Must be below 80 characters. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/delete.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/delete.ex deleted file mode 100644 index 93f62b66..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/delete.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.Delete do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter delete`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter delete` command deletes an existing alter. - ### Usage - ``` - /alter delete - ``` - ### Parameters - - `id`: The ID (or alias) of the alter to delete. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/list.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/list.ex deleted file mode 100644 index 22422958..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/list.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.List do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter list`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter list` command views of a list of your alters. - - If you have more than 20 alters, they will be paginated. You can use the buttons below the list to navigate between pages. - ### Usage - ``` - /alter list [sort] - ``` - ### Parameters - - `sort` (optional): The sorting method to use for the list. Can be one of the following: - - `ID`: Sort by ID (default) - - `Alphabetical`: Sort alphabetically by name - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/proxy.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/proxy.ex deleted file mode 100644 index 1edcc4a9..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/proxy.ex +++ /dev/null @@ -1,66 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.Proxy do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/alter proxy`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter proxy` command group manages an alter's chat proxies. - - Proxies allow you to send messages as an alter in a Discord channel. For example, if an alter has a proxy of `a-text`, typing `a-Hello, there!` will send `Hello, there!` as that alter in the current channel. - ## #{Emojis.slashcommand()} `/alter proxy set` - Removes all existing proxies from an alter, and sets a new one. - ### Usage - ``` - /alter proxy set [prefix] [suffix] - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose proxy to set. - - `prefix` (optional): The prefix to use for the proxy. - - `suffix` (optional): The suffix to use for the proxy. - ## #{Emojis.slashcommand()} `/alter proxy add` - Adds a new proxy to an alter. - ### Usage - ``` - /alter proxy add [prefix] [suffix] - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose proxy to add. - - `prefix` (optional): The prefix to use for the proxy. - - `suffix` (optional): The suffix to use for the proxy. - ## #{Emojis.slashcommand()} `/alter proxy remove` - Removes an existing proxy from an alter. - ### Usage - ``` - /alter proxy remove [prefix] [suffix] - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose proxy to remove. - - `prefix` (optional): The prefix of the proxy to remove. - - `suffix` (optional): The suffix of the proxy to remove. - ## #{Emojis.slashcommand()} `/alter proxy clear` - Clears all existing proxies from an alter. - ### Usage - ``` - /alter proxy clear - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose proxies to clear. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_alias.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_alias.ex deleted file mode 100644 index 78c954cf..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_alias.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.RemoveAlias do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter remove-alias`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter remove-alias` command removes an existing alias from an alter. - ### Usage - ``` - /alter remove-alias - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose alias to delete. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_proxy_name.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_proxy_name.ex deleted file mode 100644 index d4c78a3a..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/remove_proxy_name.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.RemoveProxyName do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter remove-proxy-name`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter remove-proxy-name` command removes an existing proxy name from an alter. - ### Usage - ``` - /alter remove-proxy-name - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose proxy name to delete. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/security.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/security.ex deleted file mode 100644 index 06662711..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/security.ex +++ /dev/null @@ -1,37 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.Security do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter security`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter security` command sets an alter's security level. Security levels determine who can view an alter's profile. - ### Usage - ``` - /alter security - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose security level to set. See the FAQ for more information about IDs and aliases. - - `level`: The new security level. Must be one of the following: - - `Private`: Only you can view the alter's profile. **This is the default.** - - `Trusted friends only`: **Trusted** friends can also view the alter's profile. - - `Friends only`: **All friends** can also view the alter's profile. - - `Public`: Anyone can view the alter's profile. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/alter/view.ex b/lib/octocon_discord/components/help_handler/pages/command_list/alter/view.ex deleted file mode 100644 index 6aa1ded4..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/alter/view.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Alter.View do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/alter view`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/alter view` command views an alter's profile, including their description, avatar, and proxies. - ### Usage - ``` - /alter view - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose profile to view. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("alter_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/autoproxy.ex b/lib/octocon_discord/components/help_handler/pages/command_list/autoproxy.ex deleted file mode 100644 index f417d349..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/autoproxy.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Autoproxy do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/autoproxy`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/autoproxy` command changes your **server-specific** autoproxy mode. There are three modes: - - - **None**: No messages will be autoproxied (default). - - **Latch**: Messages will be autoproxied as the *alter who proxied last*. - - **Front**: Messages will be autoproxied as the *alter set as main front*. If no main front is set, messages will be proxied as the *current longest-fronting alter*. - - If you wish to set your autoproxy mode *globally* (across all servers the Octocon bot has been added to), please use `/global-autoproxy` instead. - ### Usage - ``` - /autoproxy - ``` - ### Parameters - - `mode`: The autoproxy mode to set. Must be one of `none`, `latch`, or `front`. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/bot_info.ex b/lib/octocon_discord/components/help_handler/pages/command_list/bot_info.ex deleted file mode 100644 index ecb2ca15..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/bot_info.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.BotInfo do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/bot-info`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/bot-info` command shows technical information and metrics about the Octocon bot. - - - **"Node count"** is the number of nodes (servers) in the Octocon bot's cluster. - - **"Shard count"** is the number of shards the bot is using. Discord distributes servers across shards to reduce load. - - **Guilds** are Discord's internal name for servers. - ### Usage - ``` - /bot-info - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/danger.ex b/lib/octocon_discord/components/help_handler/pages/command_list/danger.ex deleted file mode 100644 index c78f49a9..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/danger.ex +++ /dev/null @@ -1,58 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Danger do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "wipe-alters", - description: "Wipes all alters from your account", - nav_page: "danger_wipe_alters" - }, - %{ - name: "delete-account", - description: "Deletes your Octocon account", - nav_page: "danger_delete_account" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/danger`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/danger` command group contains **dangerous, irreversible** commands that require additional confirmation. Choose a subcommand below to learn more about it! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/danger/delete_account.ex b/lib/octocon_discord/components/help_handler/pages/command_list/danger/delete_account.ex deleted file mode 100644 index 919f9329..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/danger/delete_account.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Danger.DeleteAccount do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/danger delete-account`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/danger delete-account` command **irreversibly** deletes the Octocon account associated with your Discord account. Upon running it, you will receive a DM from the Octocon bot with a confirmation prompt. - - This will also reset your account's alter count, so the next alter you create will have the ID `1`. - ### Usage - ``` - /danger delete-account - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("danger_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/danger/wipe_alters.ex b/lib/octocon_discord/components/help_handler/pages/command_list/danger/wipe_alters.ex deleted file mode 100644 index e2e75dc0..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/danger/wipe_alters.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Danger.WipeAlters do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/danger wipe-alters`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/danger wipe-alters` command **irreversibly** wipes all alters from your account. Upon running it, you will receive a DM from the Octocon bot with a confirmation prompt. - - This will also reset your account's alter count, so the next alter you create will have the ID `1`. - ### Usage - ``` - /danger wipe-alters - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("danger_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend.ex deleted file mode 100644 index dd0f7c2b..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend.ex +++ /dev/null @@ -1,93 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "list", - description: "Lists all your friends", - nav_page: "friend_list" - }, - %{ - name: "list-requests", - description: "Lists all your friend requests", - nav_page: "friend_list_requests" - }, - %{ - name: "add", - description: "Sends a friend request to another user", - nav_page: "friend_add" - }, - %{ - name: "remove", - description: "Removes a friend from your friends list", - nav_page: "friend_remove" - }, - %{ - name: "accept", - description: "Accepts a friend request", - nav_page: "friend_accept" - }, - %{ - name: "reject", - description: "Rejects a friend request", - nav_page: "friend_reject" - }, - %{ - name: "cancel", - description: "Cancels an outgoing friend request", - nav_page: "friend_cancel" - }, - %{ - name: "trust", - description: "Trusts a friend", - nav_page: "friend_trust" - }, - %{ - name: "untrust", - description: "Untrusts a friend", - nav_page: "friend_untrust" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/friend`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend` command group manages your friends and friend requests. Choose a subcommand below to learn more about it! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/accept.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/accept.ex deleted file mode 100644 index 8cddbec5..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/accept.ex +++ /dev/null @@ -1,35 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Accept do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend accept`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend accept` command accepts an incoming friend request from another user. - ### Usage - ``` - /front accept [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the user whose friend request to accept. - - `username`: The username of the user whose friend request to accept. - - `discord`: The Discord ping of the user whose friend request to accept. - **Note**: You must provide exactly **one** of the above parameters! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/add.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/add.ex deleted file mode 100644 index 06e4feb6..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/add.ex +++ /dev/null @@ -1,35 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Add do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend add`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend add` command sends a friend request to another user. - ### Usage - ``` - /front add [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the user to send a friend request to. - - `username`: The username of the user to send a friend request to. - - `discord`: The Discord ping of the user to send a friend request to. - **Note**: You must provide exactly **one** of the above parameters! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/cancel.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/cancel.ex deleted file mode 100644 index ebf50e69..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/cancel.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Cancel do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend cancel`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend cancel` command cancels an outgoing friend request to another user. - ### Usage - ``` - /front cancel [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the user whose friend request to cancel. - - `username`: The username of the user whose friend request to cancel. - **Note**: You must provide either a system ID **or** a username! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/list.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/list.ex deleted file mode 100644 index ab356360..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/list.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.List do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend list`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend list` command views a list of all your friends. - ### Usage - ``` - /front list - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/list_requests.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/list_requests.ex deleted file mode 100644 index 41f2a911..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/list_requests.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.ListRequests do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend list-requests`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend list-requests` command views a list of all your incoming and outgoing friend requests. - ### Usage - ``` - /front list-requests - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/reject.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/reject.ex deleted file mode 100644 index 6cacf15c..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/reject.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Reject do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend reject`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend reject` command rejects an incoming friend request from another user. - ### Usage - ``` - /front reject [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the user whose friend request to reject. - - `username`: The username of the user whose friend request to reject. - **Note**: You must provide either a system ID **or** a username! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/remove.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/remove.ex deleted file mode 100644 index 60c03d52..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/remove.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Remove do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend remove`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend remove` command removes a friend from your friends list. - ### Usage - ``` - /front remove [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the friend to remove. - - `username`: The username of the friend to remove. - **Note**: You must provide either a system ID **or** a username! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/trust.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/trust.ex deleted file mode 100644 index 50417fca..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/trust.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Trust do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend trust`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend trust` command sets a friend as a "trusted friend." - - Trusted friends are able to view all alters, tags, etc. that you have set to "Trusted friends only." **Friends do not know whether they are trusted or not!** - ### Usage - ``` - /front trust [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the friend to trust. - - `username`: The username of the friend to trust. - **Note**: You must provide either a system ID **or** a username! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/friend/untrust.ex b/lib/octocon_discord/components/help_handler/pages/command_list/friend/untrust.ex deleted file mode 100644 index 35d9d952..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/friend/untrust.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Friend.Untrust do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/friend untrust`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/friend untrust` command removes a friend's "trusted friend" status. - - Trusted friends are able to view all alters, tags, etc. that you have set to "Trusted friends only." **Friends do not know whether they are trusted or not!** - ### Usage - ``` - /front untrust [system-id | username] - ``` - ### Parameters - - `system-id`: The system ID (7-character lowercase string) of the friend to untrust. - - `username`: The username of the friend to untrust. - **Note**: You must provide either a system ID **or** a username! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("friend_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front.ex deleted file mode 100644 index 3b9f552e..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front.ex +++ /dev/null @@ -1,80 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "view", - description: "Views your currently fronting alters", - nav_page: "front_view" - }, - %{ - name: "set", - description: "Sets an alter as front (replacing others)", - nav_page: "front_set" - }, - %{ - name: "add", - description: "Adds an alter to front", - nav_page: "front_add" - }, - %{ - name: "end", - description: "Ends an alter's current front", - nav_page: "front_end" - }, - %{ - name: "main", - description: "Sets an alter as main front", - nav_page: "front_main" - }, - %{ - name: "remove_main", - description: "Removes an alter as main front", - nav_page: "front_remove_main" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/front`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front` command group manages your front status. Choose a subcommand below to learn more about it! - - **Note**: Most front commands require you to specify an alter. You can do this by their numerical ID **or** their *alias*. Aliases are unique alphanumeric IDs you can assign to your alters to make Discord commands easier to use. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/add.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/add.ex deleted file mode 100644 index 7ac1cf76..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/add.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.Add do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front add`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front add` command adds an alter to front. - ### Usage - ``` - /front add [comment] [set-main] - ``` - ### Parameters - - `id`: The ID (or alias) of the alter to add to front. See the FAQ for more information about IDs and aliases. - - `comment`: **Optional**. A comment to add to this front. - - `set-main`: **Optional**. If present, this alter will also be set as main front. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/end.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/end.ex deleted file mode 100644 index a689efb0..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/end.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.End do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front end`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front end` command ends an alter's current front. - ### Usage - ``` - /front end - ``` - ### Parameters - - `id`: The ID (or alias) of the alter whose front to end. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/primary.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/primary.ex deleted file mode 100644 index 3dc001b3..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/primary.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.Main do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front main`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front main` command sets a currently fronting alter as main front. This will make this alter appear at the top of the front list, and will proxy as them by default when `/autoproxy` is set to `Front` mode. - ### Usage - ``` - /front main - ``` - ### Parameters - - `id`: The ID (or alias) of the alter to set as main front. See the FAQ for more information about IDs and aliases. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/remove_primary.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/remove_primary.ex deleted file mode 100644 index 8bdf17e8..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/remove_primary.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.RemoveMain do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front remove-main`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front remove-main` command removes the currently set main fronter. - ### Usage - ``` - /front remove-main - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/set.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/set.ex deleted file mode 100644 index 0b859efa..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/set.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.Set do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front set`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front set` command sets an alter as front. This will replace any other alters currently fronting, as if you had used `/front end` on all of them first. - ### Usage - ``` - /front set [comment] [set-main] - ``` - ### Parameters - - `id`: The ID (or alias) of the alter to set as front. See the FAQ for more information about IDs and aliases. - - `comment`: **Optional**. A comment to add to this front. - - `set-main`: **Optional**. If present, this alter will also be set as main front. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/front/view.ex b/lib/octocon_discord/components/help_handler/pages/command_list/front/view.ex deleted file mode 100644 index e1540456..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/front/view.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Front.View do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/front view`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/front view` command views a list of alters that are currently fronting. - ### Usage - ``` - /front view - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("front_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/global_autoproxy.ex b/lib/octocon_discord/components/help_handler/pages/command_list/global_autoproxy.ex deleted file mode 100644 index 8ce7b6e2..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/global_autoproxy.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.GlobalAutoproxy do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/global-autoproxy`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/global-autoproxy` command changes your **global** autoproxy mode. There are three modes: - - - **None**: No messages will be autoproxied (default). - - **Latch**: Messages will be autoproxied as the *alter who proxied last*. - - **Front**: Messages will be autoproxied as the *alter set as main front*. If no main front is set, messages will be proxied as the *current longest-fronting alter*. - - ⚠️ **Global autoproxying can be considered a privacy issue!** If you wish to set your autoproxy mode *for a specific server*, please use `/autoproxy` instead. - ### Usage - ``` - /autoproxy - ``` - ### Parameters - - `mode`: The autoproxy mode to set. Must be one of `none`, `latch`, or `front`. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/help.ex b/lib/octocon_discord/components/help_handler/pages/command_list/help.ex deleted file mode 100644 index dda49c6e..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/help.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Help do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/help`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/help` command displays an interactive guide on how to use the Octocon bot. You're looking at it right now! - ### Usage - ``` - /help - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/register.ex b/lib/octocon_discord/components/help_handler/pages/command_list/register.ex deleted file mode 100644 index c9db30b4..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/register.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Register do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/register`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/register` command creates a new Octocon account using your Discord account. You'll have to run this command before you can use most of Octocon's features! - ### Usage - ``` - /register - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings.ex deleted file mode 100644 index 45815a7a..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings.ex +++ /dev/null @@ -1,93 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings do - use OctoconDiscord.Components.HelpHandler.Pages - - @commands [ - %{ - name: "import-pk", - description: "Imports data from PluralKit", - nav_page: "settings_import_pk" - }, - %{ - name: "import-sp", - description: "Imports data from Simply Plural", - nav_page: "settings_import_sp" - }, - %{ - name: "username", - description: "Changes your system's username", - nav_page: "settings_username" - }, - %{ - name: "avatar", - description: "Manages your system-wide avatar", - nav_page: "settings_avatar" - }, - %{ - name: "system-tag", - description: "Changes your system tag", - nav_page: "settings_system_tag" - }, - %{ - name: "show-system-tag", - description: "Sets whether your system tag tag is shown", - nav_page: "settings_show_system_tag" - }, - %{ - name: "proxy-case-sensitivity", - description: "Sets whether proxying is case-insensitive", - nav_page: "settings_proxy_case_sensitivity" - }, - %{ - name: "proxy-show-pronouns", - description: "Sets whether pronouns show in proxies", - nav_page: "settings_proxy_show_pronouns" - }, - %{ - name: "ids-as-aliases", - description: "Toggles whether alter IDs/aliases can be used as automatic proxy prefixes", - nav_page: "settings_ids_as_aliases" - } - ] - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/settings`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings` command group configures various settings for your system, as well as import data from other platforms. Choose a subcommand below to learn more about it! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @commands - |> Enum.map(fn %{name: name, description: description, nav_page: nav_page} -> - %{ - label: "/" <> name, - value: nav_page, - description: description, - emoji: map_emoji(Emojis.slashcommand()) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("command_list", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/avatar.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/avatar.ex deleted file mode 100644 index c6233b23..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/avatar.ex +++ /dev/null @@ -1,40 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.Avatar do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.folder()} `/settings avatar`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings avatar` command group modifies your system's avatar (profile picture). - ## #{Emojis.slashcommand()} `/settings avatar set` - Sets an image as your new avatar. - ### Usage - ``` - /settings avatar set - ``` - ### Parameters - - `avatar`: The image to set as your new avatar. Must be a PNG, JPEG, or WebP file. - ## #{Emojis.slashcommand()} `/settings avatar remove` - Removes your current avatar. - ### Usage - ``` - /settings avatar remove - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/ids_as_aliases.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/ids_as_aliases.ex deleted file mode 100644 index 3394fd39..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/ids_as_aliases.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.IdsAsAliases do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings ids-as-aliases`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings ids-as-aliases` command toggles whether or not alter IDs and aliases can be used as automatic proxy prefixes. - - When enabled, you can send a message with an alter's ID or alias followed by a dash (`-`) to proxy as that alter. - - For example, if you have an alter with the ID `1` and alias `Atlas`, sending `1-Hello, world!` or `Atlas-Hello, world!` will proxy "Hello, world!" as that alter. - ### Usage - ``` - /settings ids-as-aliases - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_pk.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_pk.ex deleted file mode 100644 index 8519cd83..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_pk.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.ImportPk do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings import-pk`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings import-pk` command imports existing alters from PluralKit, including their names, descriptions, pronouns, and avatars. - - If an alter has a display name set in PluralKit, it will also be imported as a **proxy name** in Octocon. - ### Usage - ``` - /settings import-pk - ``` - ### Parameters - - `token`: Your PluralKit token. You can get this by DMing PluralKit with the `pk;token` command and copying the random string it gives you. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_sp.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_sp.ex deleted file mode 100644 index d6f0d8c4..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/import_sp.ex +++ /dev/null @@ -1,41 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.ImportSp do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings import-sp`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings import-sp` command imports existing alters from Simply Plural, including their names, descriptions, pronouns, and avatars. - ### Usage - ``` - /settings import-sp - ``` - ### Parameters - - `token`: Your Simply Plural token. You can get this by opening the Simply Plural app and navigating to the following page: - - **Settings** -> **Account** -> **Tokens** - - From here: - - Click the **Add Token** button - - Enable the **Read** permission - - Click the **Add Token** button at the bottom of the screen - - Tap **Yes** 3 times to confirm - - Click the copy button on the left of the token to copy it to your clipboard - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_case_sensitivity.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_case_sensitivity.ex deleted file mode 100644 index 3e6e1e0e..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_case_sensitivity.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.ProxyCaseSensitivity do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings proxy-case-sensitivity`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings proxy-case-sensitivity` command toggles whether or not your alter's proxies are case-sensitive. - - For example, if you have an alter with the proxy `a-text`, and you have this setting enabled, that alter can also proxy as `A-text`. - ### Usage - ``` - /settings proxy-case-sensitivity - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_show_pronouns.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_show_pronouns.ex deleted file mode 100644 index 230cf060..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/proxy_show_pronouns.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.ProxyShowPronouns do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings proxy-show-pronouns`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings proxy-show-pronouns` command toggles whether or not an alter's pronouns are automatically shown as part of their display name when you proxy. - - This is useful to disable if you have alters with pronouns in their names, which is common after importing from PluralKit. - ### Usage - ``` - /settings proxy-show-pronouns - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/show_system_tag.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/show_system_tag.ex deleted file mode 100644 index 9a8fb459..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/show_system_tag.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.ShowSystemTag do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings show-system-tag`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings show-system-tag` command toggles whether or not your system tag is shown when you proxy. - - **Note**: Servers can override this setting. - ### Usage - ``` - /settings show-system-tag - ``` - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/system_tag.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/system_tag.ex deleted file mode 100644 index 41388b56..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/system_tag.ex +++ /dev/null @@ -1,37 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.SystemTag do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings system-tag`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings system-tag` command changes your system's system tag. System tags are short bits of text that can be added to the end of a proxied alter's name to identify you. - - **Note**: System tags aren't shown by default! Use the `/settings show-system-tag` command to enable them. Servers can override this setting. - - Your new system tag must satisfy the following criteria: - - Between 1-20 characters. - ### Usage - ``` - /settings system-tag - ``` - ### Parameters - - `tag`: The new system tag to set. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/command_list/settings/username.ex b/lib/octocon_discord/components/help_handler/pages/command_list/settings/username.ex deleted file mode 100644 index 00923340..00000000 --- a/lib/octocon_discord/components/help_handler/pages/command_list/settings/username.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.CommandList.Settings.Username do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.slashcommand()} `/settings username`", - color: Utils.hex_to_int("#3F3793"), - description: """ - The `/settings username` command changes your system's username. Usernames can be used to identify your system to other users in place of your system ID. - - Your new username must satisfy the following criteria: - - Between 5-16 characters - - Only contains letters, numbers, dashes, and underscores - - Does not start or end with a symbol - - Does not consist of seven lowercase letters in a row (like a system ID) - ### Usage - ``` - /settings username - ``` - ### Parameters - - `username`: The new username to set. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("settings_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq.ex b/lib/octocon_discord/components/help_handler/pages/faq.ex deleted file mode 100644 index f602746a..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq.ex +++ /dev/null @@ -1,103 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq do - use OctoconDiscord.Components.HelpHandler.Pages - - @questions [ - %{ - name: "How do I get started with the bot?", - emoji: Emojis.one(), - nav_page: "faq_get_started" - }, - %{ - name: "Can I import my alters from PluralKit or Simply Plural?", - emoji: Emojis.two(), - nav_page: "faq_import_alters" - }, - %{ - name: "Are my commands private?", - emoji: Emojis.three(), - nav_page: "faq_commands_private" - }, - %{ - name: "How do I invite Octocon to my server?", - emoji: Emojis.four(), - nav_page: "faq_invite_octocon" - }, - %{ - name: "Who can see my alters/tags?", - emoji: Emojis.five(), - nav_page: "faq_who_can_see" - }, - %{ - name: "How do IDs and aliases work?", - emoji: Emojis.six(), - nav_page: "faq_ids_aliases" - }, - %{ - name: "How does autoproxy work?", - emoji: Emojis.seven(), - nav_page: "faq_autoproxy" - }, - %{ - name: "Can I delete all of my alters at once?", - emoji: Emojis.eight(), - nav_page: "faq_delete_alters" - }, - %{ - name: "I have another question!", - emoji: Emojis.nine(), - nav_page: "faq_another_question" - } - ] - - # Joining this at compile-time avoids unnecessary work at runtime - @questions_joined @questions - |> Enum.map_join("\n", fn %{name: name, emoji: emoji} -> - "### #{emoji} #{name}" - end) - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} FAQ", - color: Utils.hex_to_int("#3F3793"), - description: """ - Have a question about how to use Octocon? Check out the FAQ below! Use the dropdown menu to navigate to a specific question. - #{@questions_joined} - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: - @questions - |> Enum.map(fn %{ - name: name, - emoji: emoji, - nav_page: nav_page - } -> - %{ - label: name, - value: nav_page, - emoji: map_emoji(emoji) - } - end) - } - ] - }, - %{ - type: 1, - components: [ - back_button("root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/another_question.ex b/lib/octocon_discord/components/help_handler/pages/faq/another_question.ex deleted file mode 100644 index ee0f9c80..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/another_question.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.AnotherQuestion do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} I have another question!", - color: Utils.hex_to_int("#3F3793"), - description: """ - If your question isn't answered here, feel free to ask in our [support server](https://octocon.app/discord)! We're happy to help. :smile: - - If you're looking for help on a specific command, you can view the `Command list` section of this help interface for more information! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/autoproxy.ex b/lib/octocon_discord/components/help_handler/pages/faq/autoproxy.ex deleted file mode 100644 index b961fb64..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/autoproxy.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.Autoproxy do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} How does autoproxy work?", - color: Utils.hex_to_int("#3F3793"), - description: """ - Autoproxy is a feature in Octocon that allows you to automatically proxy as an alter when you send a message, without having to specify their proxy every time. There are three autoproxy modes: - - - **None**: No messages will be autoproxied (default). - - **Latch**: Messages will be autoproxied as the *alter who proxied last*. - - **Front**: Messages will be autoproxied as the *alter set as main front*. If no main front is set, messages will be proxied as the *current longest-fronting alter*. - - You can change your autoproxy mode using one of the following commands: - - `/autoproxy` sets your server-specific autoproxy mode, which only applies to the server you ran the command in. - - `/global-autoproxy` sets your global autoproxy mode, which applies to ***all servers*** the Octocon bot has been added to. - - ⚠️ Please note that **global autoproxying can be considered a privacy issue!** - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/commands_private.ex b/lib/octocon_discord/components/help_handler/pages/faq/commands_private.ex deleted file mode 100644 index 82a6f56e..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/commands_private.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.CommandsPrivate do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} Are my commands private?", - color: Utils.hex_to_int("#3F3793"), - description: """ - Yes! Unlike many other proxy bots, Octocon uses **ephemeral** slash commands. This means that when you run a command, **only you** can see the response. - - This is a key feature of Octocon that ensures your privacy and security. You can use Octocon commands in public without worrying about other people seeing your data or spamming your friends! - - Additionally, all data in Octocon is private by default. No one can see your alters, tags, or other data unless you explicitly share it with them by editing their security level. See the corresponding FAQ entry for more information on security levels! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/delete_alters.ex b/lib/octocon_discord/components/help_handler/pages/faq/delete_alters.ex deleted file mode 100644 index 48cf4a01..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/delete_alters.ex +++ /dev/null @@ -1,30 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.DeleteAlters do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} Can I delete all of my alters at once?", - color: Utils.hex_to_int("#3F3793"), - description: """ - Yes! You can delete all of your alters at once by running the `/danger wipe-alters` command. Be careful, though; this action is **irreversible**! - - Once you run the command, check your DMs for a confirmation message. - - If you'd instead like to delete your entire account, you can run the `/danger delete-account` command. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/get_started.ex b/lib/octocon_discord/components/help_handler/pages/faq/get_started.ex deleted file mode 100644 index acaf3709..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/get_started.ex +++ /dev/null @@ -1,40 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.GetStarted do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} How do I get started with the bot?", - color: Utils.hex_to_int("#3F3793"), - description: """ - First, you'll have to create an account! You can do this with the `/register` command. - - After you've registered, you can either import alters from PluralKit or Simply Plural (see the respective FAQ entry for more details), or create alters from scratch with the `/alter create` command. - - If you're ever confused on how to use a command, you can view the `Command list` section of this help interface for more information! - ### Quick start - - `/register` - Create a new account - - `/alter create name:Atlas` - Create an alter with the name "Atlas" - - `/alter edit id:1 alias:Atlas pronouns:they/he` - Edit the new alter's alias and pronouns - - `/alter view id:Atlas` - View the new alter's information (using the alias you just set) - - `/front add id:Atlas` - Add the new alter to front - - `/alter proxy add id:Atlas prefix:g-` - Add a proxy tag to the new alter - - `g-Hello, there!` - Proxy as the alter with the proxy you just created - ### We're here to help! - If you have any questions, feel free to join our [support server](https://octocon.app/discord)! We're happy to help. :smile: - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/ids_aliases.ex b/lib/octocon_discord/components/help_handler/pages/faq/ids_aliases.ex deleted file mode 100644 index f93145bf..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/ids_aliases.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.IdsAliases do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} How do IDs and aliases work?", - color: Utils.hex_to_int("#3F3793"), - description: """ - In Octocon, every alter is identified by a numerical ID. Notably, this ID is unique on a by-system basis, but not globally. This means that two different users `Atlas` and `Prometheus` can *both* have an alter with the ID `1`. - - IDs are used to reference alters in commands and other contexts. For example, the `/alter edit` command accepts an `id` parameter to specify which alter to edit. - ### Aliases - Aliases are a way to give your alters a more human-readable ID. You can set an alias for an alter using the `/alter edit` command with the `alias` parameter. For example, `/alter edit id:1 alias:Atlas` would set the alias of the alter with ID `1` to `Atlas`. - - Once an alter has an alias, you can use it in place of their numerical ID in all commands. In the above example, `/alter view id:Atlas` would view the alter with ID `1`. - - If you'd like to remove an alter's alias, you can do so with the `/alter remove-alias` command. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/import_alters.ex b/lib/octocon_discord/components/help_handler/pages/faq/import_alters.ex deleted file mode 100644 index 53ca12e6..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/import_alters.ex +++ /dev/null @@ -1,43 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.ImportAlters do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} Can I import my alters from PluralKit or Simply Plural?", - color: Utils.hex_to_int("#3F3793"), - description: """ - Yep! You can import your alters from PluralKit or Simply Plural with the `/settings import-pk` and `/settings import-sp` commands, respectively. - ### PluralKit - To import your alters from PluralKit, you'll need your PluralKit "token" first. To receive this, DM the PluralKit bot with the command `pk;token`, then copy the long, random string it gives you. - - Then, run the `/settings import-pk token:your-token-here` command to import your alters. You should receive a DM soon after when the import is complete! Avatars (profile pictures) will take a little longer. - ### Simply Plural - To import your alters from Simply Plural, you'll need a "token" from Simply Plural first. You can get this by opening the Simply Plural app and navigating to the following page: - - **Settings** -> **Account** -> **Tokens** - - From here: - - Click the **Add Token** button - - Enable the **Read** permission - - Click the **Add Token** button at the bottom of the screen - - Tap **Yes** 3 times to confirm - - Click the copy button on the left of the token to copy it to your clipboard - - Then, run the `/settings import-sp token:your-token-here` command to import your alters. You should receive a DM soon after when the import is complete! Avatars (profile pictures) will take a little longer. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/invite_octocon.ex b/lib/octocon_discord/components/help_handler/pages/faq/invite_octocon.ex deleted file mode 100644 index ec71bc04..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/invite_octocon.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.InviteOctocon do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} How do I invite Octocon to my server?", - color: Utils.hex_to_int("#3F3793"), - description: """ - So long as you have the proper permissions, you can invite Octocon to your server by clicking on my profile and then the `+ Add App` button! - - Octocon also provides some features meant for server administrators, like a proxy log channel and the ability to force the use of system tags. See the `/admin` command group in the `Command list` section of this help interface for more information! - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/faq/who_can_see.ex b/lib/octocon_discord/components/help_handler/pages/faq/who_can_see.ex deleted file mode 100644 index ab3c4435..00000000 --- a/lib/octocon_discord/components/help_handler/pages/faq/who_can_see.ex +++ /dev/null @@ -1,37 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Faq.WhoCanSee do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} Who can see my alters/tags?", - color: Utils.hex_to_int("#3F3793"), - description: """ - Unlike many alternatives, **all data is private by default** in Octocon. This is to ensure your privacy and security. No one can see your alters, tags, or other data unless you explicitly share it with them. - ### Security levels - If you'd like to share your alters/tags with someone, you can do so by editing their **security level**. There are four security levels in Octocon: - - - **Private**: No one but you can see this data (default). - - **Trusted friends only**: Only **trusted** friends (see below) can see this data. - - **Friends only**: All friends can see this data. - - **Public**: Anyone can see this data, even if they're not your friend. - - When in doubt, please keep your alters and tags private! It's best practice to avoid sharing your data with people you don't trust, a concept that Octocon leans heavily into. - ### Trusted friends - Octocon models two levels of friendship: *normal* friends and *trusted* friends. If you'd like to mark a friend as a trusted friend, use the `/friend trust` command. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("faq_root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/resources.ex b/lib/octocon_discord/components/help_handler/pages/resources.ex deleted file mode 100644 index bd027d18..00000000 --- a/lib/octocon_discord/components/help_handler/pages/resources.ex +++ /dev/null @@ -1,37 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Resources do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "#{Emojis.faq()} Resources", - color: Utils.hex_to_int("#3F3793"), - description: """ - Here are some resources that you might find helpful: - ### Official links - - [Website](https://octocon.app) - - [Support server](https://octocon.app/discord) - - [GitHub organization](https://github.com/OctoconDev) - - [Documentation](https://octocon.app/docs) - ### DID/OSDD resources - **Note**: The resources here are not affiliated with Octocon. - - - [did-research.org](https://did-research.org) - - > Please be wary of misinformation in the online system community, especially on social media platforms. Also, be wary of resources on platforms like Carrd.co; they are **very** likely to contain pseudoscience, unverified original research, and misinformation. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - back_button("root", uid) - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/help_handler/pages/root.ex b/lib/octocon_discord/components/help_handler/pages/root.ex deleted file mode 100644 index 0a796258..00000000 --- a/lib/octocon_discord/components/help_handler/pages/root.ex +++ /dev/null @@ -1,58 +0,0 @@ -defmodule OctoconDiscord.Components.HelpHandler.Pages.Root do - use OctoconDiscord.Components.HelpHandler.Pages - - def embeds do - [ - %Embed{ - title: "Welcome to the Octocon help interface!", - color: Utils.hex_to_int("#3F3793"), - description: """ - This interactive guide will help you navigate the various features of the Octocon bot. - - What can I help you with today? - ### #{Emojis.slashcommand()} Command list - > View a list of all available `/slash commands` and how they're used. - ### #{Emojis.faq()} FAQ - > Get answers to frequently asked questions about Octocon. - ### #{Emojis.resources()} Resources - > Find links to the Octocon website, GitHub repository, and other useful resources. - """ - } - ] - end - - def components(uid) do - [ - %{ - type: 1, - components: [ - %{ - type: 3, - custom_id: "help|nav|#{uid}", - options: [ - %{ - label: "Command list", - value: "command_list", - text: "View a list of all available `/slash commands` and how they're used.", - emoji: map_emoji(Emojis.slashcommand()) - }, - %{ - label: "FAQ", - value: "faq_root", - text: "Get answers to frequently asked questions about Octocon.", - emoji: map_emoji(Emojis.faq()) - }, - %{ - label: "Resources", - value: "resources", - text: - "Find links to the Octocon website, GitHub repository, and other useful resources.", - emoji: map_emoji(Emojis.resources()) - } - ] - } - ] - } - ] - end -end diff --git a/lib/octocon_discord/components/paginator.ex b/lib/octocon_discord/components/paginator.ex deleted file mode 100644 index 3c75d8bd..00000000 --- a/lib/octocon_discord/components/paginator.ex +++ /dev/null @@ -1,268 +0,0 @@ -defmodule OctoconDiscord.Components.Paginator do - defmacro __using__(opts) do - page_size = Keyword.get(opts, :page_size, 10) - noun = Keyword.get(opts, :noun) - interaction_id = Keyword.get(opts, :interaction_id) - create_command = Keyword.get(opts, :create_command) - list_command = Keyword.get(opts, :list_command) - - quote do - use GenServer - - alias Nostrum.Api - - import OctoconDiscord.Utils.{ - Components, - CV2 - } - - alias OctoconDiscord.Utils.Emojis - - @page_size unquote(page_size) - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def page_size, do: @page_size - - def handle_init(_system_id, [], 0) do - error_component( - "You don't have any #{unquote(noun)}s, yet. Create one with `/#{unquote(create_command)}`!" - ) - end - - def handle_init(system_id, items, items_length) when items_length <= @page_size do - generate_response( - %{ - system_id: system_id, - items: [ - map_items(items) - ], - extra_data: generate_extra_data(items), - items_length: items_length, - uid: nil, - current_page: 1, - total_pages: 1 - }, - false - ) - |> Keyword.put(:flags, cv2_flags()) - end - - def handle_init(system_id, items, items_length) do - uid = :erlang.unique_integer([:positive]) - - chunked_items = - items - |> map_items() - |> Enum.chunk_every(@page_size) - - total_pages = ceil(items_length / @page_size) - - data = %{ - system_id: system_id, - items: chunked_items, - extra_data: generate_extra_data(items), - items_length: items_length, - uid: uid, - current_page: 1, - total_pages: total_pages - } - - :ets.insert(__MODULE__, {uid, data}) - - # [TODO]: Possibly clean up correlations after a while? - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(5)) - - generate_response(data) - |> Keyword.put(:flags, cv2_flags()) - end - - def generate_response( - %{ - system_id: _system_id, - uid: uid, - items: items, - items_length: items_length, - current_page: current_page, - total_pages: total_pages - } = context, - include_components \\ true - ) do - page_items = Enum.at(items, current_page - 1) - - [ - components: - [ - generate_page(page_items, context), - if include_components do - pagination_buttons(context) - else - [] - end - ] - |> List.flatten() - ] - end - - def handle_interaction("first", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> send_expired_response(interaction) - end - - def handle_interaction("prev", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: old_data.current_page - 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> send_expired_response(interaction) - end - - def handle_interaction("next", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: old_data.current_page + 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> send_expired_response(interaction) - end - - def handle_interaction("last", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | current_page: old_data.total_pages - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - rescue - _ -> send_expired_response(interaction) - end - - defp send_expired_response(interaction) do - Api.create_interaction_response(interaction, %{ - type: 7, - data: - error_component( - "This list has expired. Please run `/#{unquote(list_command)}` again." - ) - |> Enum.into(%{}) - |> Map.drop([:flags]) - }) - end - - def pagination_buttons(%{ - uid: uid, - current_page: current_page, - total_pages: total_pages - }) do - prev_enabled = current_page > 1 - next_enabled = current_page < total_pages - - action_row([ - button( - "#{unquote(interaction_id)}|first|#{uid}", - :secondary, - label: "First", - emoji: Emojis.component_emoji(Emojis.first()), - disabled: !prev_enabled - ), - button( - "#{unquote(interaction_id)}|prev|#{uid}", - :secondary, - label: "Previous", - emoji: Emojis.component_emoji(Emojis.back()), - disabled: !prev_enabled - ), - button( - "#{unquote(interaction_id)}|next|#{uid}", - :secondary, - label: "Next", - emoji: Emojis.component_emoji(Emojis.forward()), - disabled: !next_enabled - ), - button( - "#{unquote(interaction_id)}|last|#{uid}", - :secondary, - label: "Last", - emoji: Emojis.component_emoji(Emojis.last()), - disabled: !next_enabled - ) - ]) - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end - end - end -end diff --git a/lib/octocon_discord/components/reproxy_handler.ex b/lib/octocon_discord/components/reproxy_handler.ex deleted file mode 100644 index 97b7785d..00000000 --- a/lib/octocon_discord/components/reproxy_handler.ex +++ /dev/null @@ -1,259 +0,0 @@ -defmodule OctoconDiscord.Components.ReproxyHandler do - @moduledoc false - use GenServer - - import OctoconDiscord.Proxy - import OctoconDiscord.Utils.Components - - alias Nostrum.Api - - alias Octocon.Alters - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def handle_init(user_id, raw_message, db_message) do - uid = :erlang.unique_integer([:positive]) - - data = %{ - user_id: user_id, - raw_message: raw_message, - db_message: db_message - } - - :ets.insert(__MODULE__, {uid, data}) - - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(10)) - - [ - type: :modal, - title: "Reproxy message", - custom_id: "reproxy|confirm|#{uid}", - components: [ - text_input( - id: "text", - label: "New text", - min_length: 1, - max_length: 2000, - placeholder: "Leave blank to reproxy with the same text." - ), - text_input( - id: "alter", - label: "Alter ID/alias", - min_length: 1, - max_length: 80, - placeholder: "Leave blank to reproxy as the same alter." - ) - ] - ] - end - - def handle_interaction("confirm", uid, interaction) do - data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - responses = parse_responses(interaction) - - result = - try_reproxy_message(data, responses) - |> Enum.into(%{}) - |> Map.put(:flags, 64) - - :ets.delete(__MODULE__, uid) - - a = %{ - type: 4, - data: result - } - - Api.create_interaction_response(interaction, a) - end - - defp try_reproxy_message( - %{ - raw_message: raw_message, - db_message: db_message - } = data, - %{text: text, alter: idalias} - ) do - system_id = db_message.system_id - - new_content = - if String.trim(text) == "" do - raw_message.content - else - text - end - - OctoconDiscord.Utils.with_id_or_alias( - idalias, - fn - nil -> - alter_id = db_message.alter_id - - edit_message(data, %{ - system_id: system_id, - alter_id: alter_id, - text: new_content - }) - - alter_identity -> - alter_id = Alters.resolve_alter({:system, system_id}, alter_identity) - - if alter_id == false do - error_component( - "You don't have an alter with the ID or alias `#{alter_identity |> elem(1)}`." - ) - else - reproxy_message(data, %{ - system_id: system_id, - alter_id: alter_id, - text: new_content - }) - end - end, - true - ) - end - - defp edit_message( - %{ - user_id: user_id, - raw_message: raw_message - }, - %{ - system_id: system_id, - alter_id: alter_id, - text: text - } - ) do - case with_proxy_prerequisites( - user_id, - raw_message, - fn %{ - message: message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - } -> - send_proxy_message( - %{ - webhook: webhook, - message: %{message | content: text, author: %{message.author | id: user_id}}, - alter: {system_id, alter_id}, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - true, - fn id, token, data -> - Api.Webhook.edit_message( - id, - token, - message.id, - Map.put(data, :embeds, raw_message.embeds) - ) - end - ) - end - ) do - :no_proxy -> error_component("Failed to reproxy the message.") - :ok -> success_component("Message reproxied!") - end - end - - defp reproxy_message( - %{ - user_id: user_id, - raw_message: raw_message - }, - %{ - system_id: system_id, - alter_id: alter_id, - text: text - } - ) do - case with_proxy_prerequisites(user_id, raw_message, fn %{ - message: message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - } -> - send_proxy_message( - %{ - webhook: webhook, - message: %{message | content: text, author: %{message.author | id: user_id}}, - alter: {system_id, alter_id}, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - end) do - :no_proxy -> error_component("Failed to reproxy the message.") - :ok -> success_component("Message reproxied!") - end - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end - - defp parse_responses(%{data: %{components: components}}) do - components - |> Stream.map(&List.first(&1.components)) - |> Stream.map(fn %{custom_id: id, value: value} -> - # credo:disable-for-next-line - {String.to_atom(id), value} - end) - |> Enum.into(%{}) - end - - defp text_input(opts) do - type = - case Keyword.get(opts, :type, :short) do - :short -> 1 - :long -> 2 - end - - %{ - type: 1, - components: [ - %{ - type: 4, - custom_id: Keyword.get(opts, :id), - style: type, - label: Keyword.get(opts, :label), - min_length: Keyword.get(opts, :min_length), - max_length: Keyword.get(opts, :max_length), - placeholder: Keyword.get(opts, :placeholder), - required: false - } - ] - } - end -end diff --git a/lib/octocon_discord/components/tag_handler.ex b/lib/octocon_discord/components/tag_handler.ex deleted file mode 100644 index bb34d2d5..00000000 --- a/lib/octocon_discord/components/tag_handler.ex +++ /dev/null @@ -1,36 +0,0 @@ -defmodule OctoconDiscord.Components.TagHandler do - import OctoconDiscord.Utils.Components - - alias Nostrum.Api - - alias Octocon.{ - Alters, - Tags - } - - def handle_interaction("view", tag_id, interaction) do - system_identity = {:discord, to_string(interaction.user.id)} - - tag = Tags.get_tag(system_identity, tag_id) - - alters = - Alters.get_alters_by_id_bounded(system_identity, tag.alters, [ - :id, - :name, - :pronouns, - :security_level - ]) - - send_response(tag_component(tag, alters), interaction) - end - - defp send_response(components, interaction) do - Api.Interaction.create_response(interaction, %{ - type: 7, - data: %{ - components: if(is_list(components), do: components, else: [components]), - flags: OctoconDiscord.Utils.CV2.cv2_flags() - } - }) - end -end diff --git a/lib/octocon_discord/components/tag_paginator.ex b/lib/octocon_discord/components/tag_paginator.ex deleted file mode 100644 index bc7c66df..00000000 --- a/lib/octocon_discord/components/tag_paginator.ex +++ /dev/null @@ -1,97 +0,0 @@ -defmodule OctoconDiscord.Components.TagPaginator do - @moduledoc false - use OctoconDiscord.Components.Paginator, - noun: "tag", - interaction_id: "tag-pag", - create_command: "tag create", - list_command: "tag list" - - alias OctoconDiscord.Utils.Emojis - - def generate_page( - page_tags, - %{ - items_length: tags_length, - current_page: current_page, - total_pages: total_pages - } - ) do - container( - [ - text("## Your tags (#{tags_length})\n\nClick a tag's button to view more details."), - separator(spacing: :large), - Enum.map(page_tags, fn tag -> - [ - section( - [ - text(""" - **#{tag.name || "Unnamed tag"}** - """) - ], - button( - "tag|view|#{tag.id}", - :secondary, - emoji: Emojis.component_emoji(Emojis.open()) - ) - ) - ] - end), - if total_pages > 1 do - [ - separator(spacing: :large), - text("Page #{current_page}/#{total_pages}") - ] - else - [] - end - ] - |> List.flatten() - ) - end - - def map_items(tags) do - tags = - Enum.map(tags, fn tag -> - Map.take(tag, [:id, :name, :description, :color, :security_level, :parent_tag_id]) - end) - - valid_ids = - tags - |> Enum.map(& &1.id) - |> MapSet.new() - - tags = - Enum.map(tags, fn tag -> - if is_nil(tag.parent_tag_id) or MapSet.member?(valid_ids, tag.parent_tag_id) do - tag - else - %{tag | parent_tag_id: nil} - end - end) - - children = - Enum.group_by(tags, & &1.parent_tag_id) - - children - |> Map.get(nil, []) - |> Enum.sort_by(& &1.name) - |> Enum.flat_map(&flatten(&1, children, 0)) - end - - defp flatten(tag, children_map, depth) do - prefix = String.duplicate("⎯", depth) <> if depth > 0, do: ">", else: "" - - current = - Map.update!(tag, :name, fn name -> - "#{prefix} #{name}" |> String.trim() - end) - - children_map - |> Map.get(tag.id, []) - |> Enum.sort_by(& &1.name) - |> Enum.flat_map(&flatten(&1, children_map, depth + 1)) - |> then(&[current | &1]) - end - - def generate_extra_data(_tags), do: nil -end diff --git a/lib/octocon_discord/components/wipe_alters_handler.ex b/lib/octocon_discord/components/wipe_alters_handler.ex deleted file mode 100644 index 33ae6964..00000000 --- a/lib/octocon_discord/components/wipe_alters_handler.ex +++ /dev/null @@ -1,129 +0,0 @@ -defmodule OctoconDiscord.Components.WipeAltersHandler do - @moduledoc false - use GenServer - - import OctoconDiscord.Utils.CV2 - - alias Nostrum.Api - - alias Octocon.Accounts - alias OctoconDiscord.Utils - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def handle_init(system_identity) do - user = Accounts.get_user!(system_identity) - - uid = :erlang.unique_integer([:positive]) - - data = %{ - system_id: user.id, - uid: uid, - confirmations_left: 3 - } - - :ets.insert(__MODULE__, {uid, data}) - - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(5)) - - Utils.send_dm(user, generate_response(data)) - end - - defp generate_response(%{ - system_id: _system_id, - uid: uid, - confirmations_left: confirmations_left - }) - when confirmations_left > 0 do - [ - components: [ - container( - [ - text("### Wipe alters"), - text( - "**WARNING**: This command will **permanently** wipe all alters from your system, but keep your account and settings.\n\nTo confirm, click the button below a total of **3** times." - ) - ], - %{accent_color: 0xFF0000} - ), - action_row([ - button( - "wipe-alters|confirm|#{uid}", - :danger, - label: "Confirm (#{3 - confirmations_left}/3)", - emoji: %{name: "⚠️"} - ) - ]) - ], - flags: cv2_flags(false) - ] - end - - defp generate_response(%{ - system_id: system_id, - uid: _uid, - confirmations_left: _confirmations_left - }) do - Accounts.wipe_alters({:system, system_id}) - - [ - components: [ - container( - [ - text("### :broom: Success!"), - text( - "Your alters have been wiped, and your alter IDs have been reset (your next alter will have the ID `1`)." - ) - ], - %{ - accent_color: 0x00FF00 - } - ) - ], - flags: cv2_flags(false) - ] - end - - def handle_interaction("confirm", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | confirmations_left: old_data.confirmations_left - 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end -end diff --git a/lib/octocon_discord/components/wipe_tags_handler.ex b/lib/octocon_discord/components/wipe_tags_handler.ex deleted file mode 100644 index fd145d4d..00000000 --- a/lib/octocon_discord/components/wipe_tags_handler.ex +++ /dev/null @@ -1,127 +0,0 @@ -defmodule OctoconDiscord.Components.WipeTagsHandler do - @moduledoc false - use GenServer - - import OctoconDiscord.Utils.CV2 - - alias Nostrum.Api - - alias Octocon.Accounts - alias OctoconDiscord.Utils - - def start_link([]) do - GenServer.start_link(__MODULE__, [], name: __MODULE__) - end - - def handle_init(system_identity) do - user = Accounts.get_user!(system_identity) - - uid = :erlang.unique_integer([:positive]) - - data = %{ - system_id: user.id, - uid: uid, - confirmations_left: 3 - } - - :ets.insert(__MODULE__, {uid, data}) - - Process.send_after(__MODULE__, {:drop, uid}, :timer.minutes(5)) - - Utils.send_dm(user, generate_response(data)) - end - - defp generate_response(%{ - system_id: _system_id, - uid: uid, - confirmations_left: confirmations_left - }) - when confirmations_left > 0 do - [ - components: [ - container( - [ - text("### Wipe tags"), - text( - "**WARNING**: This command will **permanently** wipe all tags from your system, but keep your account and settings.\n\nTo confirm, click the button below a total of **3** times." - ) - ], - %{accent_color: 0xFF0000} - ), - action_row([ - button( - "wipe-tags|confirm|#{uid}", - :danger, - label: "Confirm (#{3 - confirmations_left}/3)", - emoji: %{name: "⚠️"} - ) - ]) - ], - flags: cv2_flags(false) - ] - end - - defp generate_response(%{ - system_id: system_id, - uid: _uid, - confirmations_left: _confirmations_left - }) do - Accounts.wipe_tags({:system, system_id}) - - [ - components: [ - container( - [ - text("### :broom: Success!"), - text("Your tags have been wiped.") - ], - %{ - accent_color: 0x00FF00 - } - ) - ], - flags: cv2_flags(false) - ] - end - - def handle_interaction("confirm", uid, interaction) do - old_data = - :ets.lookup(__MODULE__, uid) - |> hd() - |> elem(1) - - data = %{ - old_data - | confirmations_left: old_data.confirmations_left - 1 - } - - :ets.insert(__MODULE__, {uid, data}) - - Api.create_interaction_response(interaction, %{ - type: 7, - data: generate_response(data) |> Enum.into(%{}) - }) - end - - def drop(uid) do - :ets.delete(__MODULE__, uid) - end - - def init([]) do - :ets.new(__MODULE__, [ - :set, - :named_table, - :public, - read_concurrency: true, - write_concurrency: :auto, - decentralized_counters: true - ]) - - {:ok, %{}} - end - - def handle_info({:drop, uid}, state) do - :ets.delete(__MODULE__, uid) - {:noreply, state} - end -end diff --git a/lib/octocon_discord/consumer.ex b/lib/octocon_discord/consumer.ex deleted file mode 100644 index 53cf2015..00000000 --- a/lib/octocon_discord/consumer.ex +++ /dev/null @@ -1,140 +0,0 @@ -defmodule OctoconDiscord.Consumer do - @moduledoc false - - @behaviour Nostrum.Consumer - require Logger - - import OctoconDiscord.Utils.{ - Components, - CV2 - } - - alias OctoconDiscord.{ - Commands, - Components - } - - alias OctoconDiscord.Events.MessageCreate - - @commands %{ - # "register" => Commands.Register, - "help" => Commands.Help, - "system" => Commands.System, - "settings" => Commands.Settings, - "alter" => Commands.Alter, - "tag" => Commands.Tag, - "autoproxy" => Commands.Autoproxy, - "global-autoproxy" => Commands.GlobalAutoproxy, - "danger" => Commands.Danger, - "bot-info" => Commands.BotInfo, - "friend" => Commands.Friend, - "admin" => Commands.Admin, - "front" => Commands.Front, - "export" => Commands.Export, - # "octo" => Commands.Octo, - "❓ Who is this?" => Commands.Messages.WhoIsThis, - "🔔 Ping account" => Commands.Messages.PingAccount, - "❌ Delete proxied message" => Commands.Messages.DeleteProxiedMessage, - "🔄 Reproxy message" => Commands.Messages.Reproxy - } - - def handle_event({:READY, %{shard: {this_shard, shard_count}}, _ws_state}) do - unless :persistent_term.get(:commands_initialized, false) do - Logger.info( - "First shard is READY; bulk-registering all application commands (#{map_size(@commands)})..." - ) - - scope = Application.get_env(:octocon, :nostrum_scope) - - Enum.each(@commands, fn {name, module} -> - Nosedrum.Storage.Dispatcher.queue_command(name, module) - end) - - case Nosedrum.Storage.Dispatcher.process_queue(scope) do - {:ok, _} -> - Logger.info("Registered all commands!") - - {:error, e} -> - Logger.error("Failed to register all commands: #{inspect(e)}") - end - - :persistent_term.put(:commands_initialized, true) - end - - OctoconDiscord.StatusUpdater.bump({this_shard, shard_count}) - - :ok - end - - def handle_event({:INTERACTION_CREATE, %{type: type} = interaction, _ws_state}) - when type in [3, 5] do - Components.dispatch(interaction) - rescue - e -> - create_error_response(interaction) - reraise e, __STACKTRACE__ - end - - def handle_event({:INTERACTION_CREATE, %{type: 4} = interaction, _ws_state}) do - Nostrum.Api.Interaction.create_response( - interaction, - %{ - type: 8, - data: %{ - choices: OctoconDiscord.Autocomplete.dispatch(interaction) - } - } - ) - rescue - e -> - Nostrum.Api.Interaction.create_response(interaction, %{ - type: 8, - data: %{ - choices: [] - } - }) - - reraise e, __STACKTRACE__ - end - - def handle_event({:INTERACTION_CREATE, interaction, _ws_state}) do - Nosedrum.Storage.Dispatcher.handle_interaction(interaction) - rescue - e -> - create_error_response(interaction) - reraise e, __STACKTRACE__ - end - - def handle_event({:MESSAGE_CREATE, data, _ws_state}) do - # IO.inspect(data, limit: :infinity) - MessageCreate.handle(data) - end - - def handle_event(_data) do - :ok - end - - defp create_error_response(interaction) do - Nostrum.Api.Interaction.create_response(interaction, %{ - type: 4, - data: %{ - components: [ - error_component_raw("An error occurred while processing your command.") - ], - flags: cv2_flags() - } - }) - end - - # def handle_event({:MESSAGE_DELETE, data, _ws_state}) do - # MessageDelete.handle(data) - # end - - # def handle_event({:MESSAGE_UPDATE, data, _ws_state}) do - # MessageUpdate.handle(data) - # end - - # def handle_event({:MESSAGE_REACTION_ADD, data, _ws_state}) do - # MessageReactionAdd.handle(data) - # end -end diff --git a/lib/octocon_discord/events/message_create.ex b/lib/octocon_discord/events/message_create.ex deleted file mode 100644 index 705a156d..00000000 --- a/lib/octocon_discord/events/message_create.ex +++ /dev/null @@ -1,362 +0,0 @@ -defmodule OctoconDiscord.Events.MessageCreate do - @moduledoc """ - Handles MESSAGE_CREATE events from Discord, mostly for proxying messages. - """ - - require Logger - - alias Octocon.{ - Alters, - Fronts - } - - alias OctoconDiscord.Utils - - import OctoconDiscord.Proxy - - @accepted_message_types [ - # DEFAULT - 0, - # REPLY - 19 - ] - - def handle(%{content: "octo:meow", channel_id: channel_id}) do - Nostrum.Api.Message.create(channel_id, content: "Nya~!") - end - - # Ignore PluralKit commands - def handle(%{content: "pk;" <> _}) do - :ok - end - - def handle(%{content: "Pk;" <> _}) do - :ok - end - - # Ignore PluralKit commands (alternative prefix) - def handle(%{content: "pk!" <> _}) do - :ok - end - - def handle(%{content: "Pk!" <> _}) do - :ok - end - - # Ignore Tupperbox commands - def handle(%{content: "tul!" <> _}) do - :ok - end - - def handle(%{content: "\\\\" <> _}) do - # [TODO]: When using two backslashes, ignore the message AND clear the autoproxy - :ok - end - - # Ignore messages that start with "\" - def handle(%{content: "\\" <> _}) do - :ok - end - - def handle(message) - when message.author.bot == nil and - message.guild_id != nil and - message.type in @accepted_message_types and - not (message.content == "" and message.sticker_items != nil) do - with_proxy_prerequisites(message.author.id, message, fn context -> - proxy_message(context) - end) - end - - # Ignore other message types silently (pins, bot messages, etc.) - def handle(_message), do: :ok - - defp proxy_message( - %{ - message: %Nostrum.Struct.Message{} = message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - } = context - ) do - # Check if the message matches a manual proxy first... - matched_proxy = - get_proxy(%{ - proxy_list: proxy_data.proxies, - message: message.content, - proxy_data: proxy_data - }) - - case matched_proxy do - nil -> - # ...if not, check autoproxy - try_global_autoproxy(context) || try_server_autoproxy(context) - - {new_message, data} -> - # Update latched alter if necessary - cond do - proxy_data.settings.global_autoproxy_mode == :latch -> - Octocon.Accounts.update_discord_settings( - {:system, proxy_data.system_id}, - %{global_latched_alter: elem(data, 1)} - ) - - proxy_data.settings.server_settings.autoproxy_mode == :latch -> - Octocon.Accounts.update_server_settings( - {:system, proxy_data.system_id}, - to_string(proxy_data.settings.server_settings.guild_id), - %{latched_alter: elem(data, 1)} - ) - - true -> - :ok - end - - unless new_message == "" do - send_proxy_message( - %{ - webhook: webhook, - message: %Nostrum.Struct.Message{message | content: new_message}, - alter: data, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - end - end - end - - defp try_global_autoproxy( - %{ - message: message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - } = context - ) do - case proxy_data.settings.global_autoproxy_mode do - :off -> - # Autoproxy disabled, ignore - false - - :front -> - try_front_autoproxy(context) - true - - :latch -> - case proxy_data.settings.global_latched_alter do - nil -> - # No one has proxied yet, ignore - :ok - - alter_id -> - # There's a global latched alter; proxy as them - system_id = proxy_data.system_id - - try do - send_proxy_message( - %{ - webhook: webhook, - message: message, - alter: {system_id, alter_id}, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - rescue - e in OctoconDiscord.Proxy.InvalidAlterError -> - Logger.debug(e.message) - - Octocon.Accounts.update_discord_settings( - {:system, proxy_data.system_id}, - %{global_latched_alter: nil} - ) - end - end - - true - end - end - - defp try_server_autoproxy( - %{ - message: message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - } = context - ) do - case proxy_data.settings.server_settings.autoproxy_mode do - :off -> - # Autoproxy disabled, ignore - :ok - - :front -> - try_front_autoproxy(context) - - :latch -> - case proxy_data.settings.server_settings.latched_alter do - nil -> - # No one has proxied yet, ignore - :ok - - alter_id -> - # There's a latched alter on this server; proxy as them - system_id = proxy_data.system_id - - try do - send_proxy_message( - %{ - webhook: webhook, - message: message, - alter: {system_id, alter_id}, - # Map.take(proxy_data, [:system_tag, :show_system_tag, :show_proxy_pronouns]) - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - rescue - e in OctoconDiscord.Proxy.InvalidAlterError -> - Logger.debug(e.message) - - Octocon.Accounts.update_server_settings( - {:system, proxy_data.system_id}, - to_string(proxy_data.settings.server_settings.guild_id), - %{latched_alter: nil} - ) - end - end - end - end - - defp try_front_autoproxy(%{ - message: message, - webhook: webhook, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }) do - Logger.debug("Trying front autoproxy") - system_id = proxy_data.system_id - - case proxy_data.primary_front do - nil -> - Logger.debug("No primary front") - # If there's no primary fronter, find the longest current fronter - case Fronts.longest_current_fronter({:system, system_id}) do - nil -> - Logger.debug("No fronters") - :ok - - %{alter: %{id: alter_id}} -> - Logger.debug("Found longest current fronter: #{alter_id}") - - send_proxy_message( - %{ - webhook: webhook, - message: message, - alter: {system_id, alter_id}, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - end - - alter_id -> - Logger.debug("Found primary front: #{alter_id}") - - send_proxy_message( - %{ - webhook: webhook, - message: message, - alter: {system_id, alter_id}, - proxy_data: proxy_data, - thread_id: thread_id, - server_settings: server_settings - }, - false - ) - end - end - - defp get_proxy(%{ - proxy_list: proxy_list, - message: message, - proxy_data: %{ - system_id: system_id, - settings: %{ - case_insensitive_proxies: case_insensitive_proxies, - ids_as_proxies: ids_as_proxies - } - } - }) do - proxy = - if case_insensitive_proxies do - proxy_list - |> Enum.find(fn {{prefix, suffix, _}, _} -> - String.starts_with?(String.downcase(message), String.downcase(prefix)) && - String.ends_with?(String.downcase(message), String.downcase(suffix)) - end) - else - proxy_list - |> Enum.find(fn {{prefix, suffix, _}, _} -> - String.starts_with?(message, prefix) && - String.ends_with?(message, suffix) - end) - end - - case proxy do - nil -> - if ids_as_proxies do - case String.split(message, "-", parts: 2) do - [_] -> nil - # If "IDs as proxies" mode is enabled, we'll try to get an ID/alias from the message - [prefix, rest] -> parse_idalias(system_id, prefix, rest) - end - else - nil - end - - {{prefix, suffix, _}, data} -> - # Matched a manual proxy! - new_message = - message - |> String.slice(String.length(prefix)..-(String.length(suffix) + 1)//1) - - {new_message, data} - end - end - - # Parses an ID or alias from a message when "IDs as proxies" is enabled into an alter identity - defp parse_idalias(system_id, prefix, message) do - cond do - Utils.alter_id_valid?(prefix) -> - try_get_alter(system_id, {:id, Utils.parse_id!(prefix)}, message) - - Utils.alter_alias_valid?(prefix) -> - try_get_alter(system_id, {:alias, prefix}, message) - - true -> - nil - end - end - - # Tries to get an alter by ID or alias, returning nil if the alter doesn't exist - defp try_get_alter(system_id, alter_identity, message) do - case Alters.resolve_alter({:system, system_id}, alter_identity) do - false -> nil - alter_id -> {message, {system_id, alter_id}} - end - end -end diff --git a/lib/octocon_discord/guild_cache.ex b/lib/octocon_discord/guild_cache.ex deleted file mode 100644 index 53d91213..00000000 --- a/lib/octocon_discord/guild_cache.ex +++ /dev/null @@ -1,355 +0,0 @@ -defmodule OctoconDiscord.GuildCache do - @moduledoc """ - An Mnesia-based cache for guilds. - - Modified to only cache specific keys of the guild struct. - """ - - use Supervisor - - @behaviour Nostrum.Cache.GuildCache - - @base_table_name :nostrum_guilds - - alias Nostrum.{ - Bot, - Cache.GuildCache, - Struct.Channel, - Struct.Emoji, - Struct.Guild, - Struct.Sticker, - Util - } - - @doc "Start the supervisor." - def start_link(opts) do - Supervisor.start_link(__MODULE__, opts) - end - - @impl Supervisor - @doc "Set up the cache's Mnesia table." - def init(opts) do - # credo:disable-for-next-line - table_name = :"#{@base_table_name}_#{Keyword.fetch!(opts, :name)}" - - table_options = [ - attributes: [:id, :data], - record_name: record_name() - ] - - case :mnesia.create_table(table_name, table_options) do - {:atomic, :ok} -> :ok - {:aborted, {:already_exists, _tab}} -> :ok - end - - Supervisor.init([], strategy: :one_for_one) - end - - @doc "Retrieve the Mnesia table name used for the cache." - @spec table :: atom() - def table do - # credo:disable-for-next-line - :"#{@base_table_name}_#{Bot.fetch_bot_name()}" - end - - defp record_name, do: :nostrum_guild - - @doc "Drop the table used for caching." - @spec teardown() :: {:atomic, :ok} | {:aborted, term()} - def teardown, do: :mnesia.delete_table(table()) - - @doc "Clear any objects in the cache." - @spec clear() :: :ok - def clear do - {:atomic, :ok} = :mnesia.clear_table(table()) - :ok - end - - @impl GuildCache - @doc "Get a guild from the cache." - def get(guild_id) do - :mnesia.activity(:sync_transaction, fn -> - case :mnesia.read(table(), guild_id) do - [{_tag, _id, guild}] -> {:ok, guild} - [] -> {:error, :not_found} - end - end) - end - - @impl GuildCache - def all do - ms = [{{:_, :_, :"$1"}, [], [:"$1"]}] - - Stream.resource( - fn -> :mnesia.select(table(), ms, 100, :read) end, - fn items -> - case items do - {matches, cont} -> - {matches, :mnesia.select(cont)} - - :"$end_of_table" -> - {:halt, nil} - end - end, - fn _cont -> :ok end - ) - end - - # Used by dispatch - - @impl GuildCache - @doc "Create a guild from upstream data." - def create(payload) do - guild = Guild.to_struct(payload) |> strip_guild() - record = {record_name(), guild.id, guild} - writer = fn -> :mnesia.write(table(), record, :write) end - :ok = :mnesia.activity(:sync_transaction, writer) - guild - end - - @impl GuildCache - @doc "Update the given guild in the cache." - def update(payload) do - new_guild = Guild.to_struct(payload) |> strip_guild() - - old_guild = - :mnesia.activity(:sync_transaction, fn -> - case :mnesia.read(table(), new_guild.id, :write) do - [{_tag, _id, old_guild} = entry] -> - updated_guild = Guild.merge(old_guild, new_guild) - - :mnesia.write(table(), put_elem(entry, 2, updated_guild), :write) - old_guild - - [] -> - nil - end - end) - - {old_guild, new_guild} - end - - @impl GuildCache - @doc "Remove the given guild from the cache." - def delete(guild_id) do - :mnesia.activity(:sync_transaction, fn -> - case :mnesia.read(table(), guild_id, :write) do - [{_tag, _guild_id, guild}] -> - :mnesia.delete(table(), guild_id, :write) - guild - - _ -> - nil - end - end) - end - - @impl GuildCache - @doc "Create the given channel for the given guild in the cache." - def channel_create(guild_id, channel) do - new_channel = Channel.to_struct(channel) |> Map.take([:id, :type, :parent_id]) - - update_guild!(guild_id, fn guild -> - new_channels = Map.put(guild.channels, channel.id, new_channel) - {%{guild | channels: new_channels}, :ok} - end) - - new_channel - end - - @impl GuildCache - @doc "Delete the channel from the given guild in the cache." - def channel_delete(guild_id, channel_id) do - old_channel = - update_guild!(guild_id, fn guild -> - {popped, new_channels} = Map.pop(guild.channels, channel_id) - {%{guild | channels: new_channels}, popped} - end) - - if old_channel, do: old_channel, else: :noop - end - - @doc "Update the channel on the given guild in the cache." - @impl GuildCache - def channel_update(guild_id, channel) do - update_guild!(guild_id, fn guild -> - {old, new, new_channels} = - upsert(guild.channels, channel.id, channel |> Map.take([:id, :type, :parent_id])) - - {%{guild | channels: new_channels}, {old, new}} - end) - end - - @impl GuildCache - @doc "Update the emoji list for the given guild in the cache." - def emoji_update(_guild_id, payload) do - casted = Util.cast(payload, {:list, {:struct, Emoji}}) - {[], casted} - end - - @impl GuildCache - @doc "Update the sticker list for the given guild in the cache." - def stickers_update(_guild_id, stickers) do - casted = Util.cast(stickers, {:list, {:struct, Sticker}}) - {[], casted} - end - - @impl GuildCache - @doc "Create the given role in the given guild in the cache." - def role_create(guild_id, payload) do - update_guild!(guild_id, fn guild -> - permissions = payload.permissions |> String.to_integer() - new_role = %{id: payload.id, permissions: permissions} - {_old, new, new_roles} = upsert(guild.roles, payload.id, new_role) - {%{guild | roles: new_roles}, {guild_id, new}} - end) - end - - @doc "Delete the given role from the given guild in the cache." - @impl GuildCache - def role_delete(guild_id, role_id) do - update_guild!(guild_id, fn guild -> - {popped, new_roles} = Map.pop(guild.roles, role_id) - result = if popped, do: {guild_id, popped}, else: :noop - {%{guild | roles: new_roles}, result} - end) - end - - @impl GuildCache - @doc "Update the given role in the given guild in the cache." - def role_update(guild_id, role) do - update_guild!(guild_id, fn guild -> - permissions = role.permissions |> String.to_integer() - new_role = %{id: role.id, permissions: permissions} - {old, new_role, new_roles} = upsert(guild.roles, role.id, new_role) - new_guild = %{guild | roles: new_roles} - {new_guild, {guild_id, old, new_role}} - end) - end - - @impl GuildCache - @doc "Update guild voice states with the given voice state in the cache." - def voice_state_update(guild_id, _payload) do - {guild_id, []} - end - - @doc "Increment the guild member count by one." - @impl GuildCache - def member_count_up(guild_id) do - # May not be `update_guild!` for the case where guild intent is off. - update_guild(guild_id, fn guild -> - {%{guild | member_count: guild.member_count + 1}, true} - end) - - true - end - - @doc "Decrement the guild member count by one." - @impl GuildCache - def member_count_down(guild_id) do - # May not be `update_guild!` for the case where guild intent is off. - update_guild(guild_id, fn guild -> - {%{guild | member_count: guild.member_count - 1}, true} - end) - - true - end - - defp update_guild(guild_id, updater) do - :mnesia.activity( - :sync_transaction, - fn -> - case :mnesia.read(table(), guild_id, :write) do - [{_tag, _id, old_guild} = entry] -> - {new_guild, result} = updater.(old_guild) - :mnesia.write(table(), put_elem(entry, 2, new_guild), :write) - result - - [] -> - nil - end - end - ) - end - - defp update_guild!(guild_id, updater) do - :mnesia.activity( - :sync_transaction, - fn -> - [{_tag, _id, old_guild} = entry] = :mnesia.read(table(), guild_id, :write) - {new_guild, result} = updater.(old_guild) - :mnesia.write(table(), put_elem(entry, 2, new_guild), :write) - result - end - ) - end - - defp strip_guild(%Nostrum.Struct.Guild{ - id: id, - name: name, - member_count: member_count, - owner_id: owner_id, - roles: roles, - threads: threads, - channels: channels - }) do - channels = - if channels == nil do - nil - else - channels - |> Enum.map(fn {id, channel} -> {id, Map.take(channel, [:id, :type, :parent_id])} end) - |> Map.new() - end - - threads = - if threads == nil do - nil - else - threads - |> Enum.map(fn {id, thread} -> {id, Map.take(thread, [:id, :type, :parent_id])} end) - |> Map.new() - end - - roles = - if roles == nil do - nil - else - roles - |> Enum.map(fn {id, role} -> {id, Map.take(role, [:id, :permissions])} end) - |> Map.new() - end - - %{ - id: id, - name: name, - member_count: member_count, - owner_id: owner_id, - roles: roles, - threads: threads, - channels: channels - } - end - - @impl GuildCache - @doc "Wrap queries in a transaction." - def wrap_query(fun) do - :mnesia.activity(:sync_transaction, fun) - end - - defp upsert(map, key, new) do - if Map.has_key?(map, key) do - old = Map.get(map, key) - - new = - old - |> Map.merge(new) - - new_map = Map.put(map, key, new) - - {old, new, new_map} - else - {nil, new, Map.put(map, key, new)} - end - end -end diff --git a/lib/octocon_discord/proxy.ex b/lib/octocon_discord/proxy.ex deleted file mode 100644 index cd4165f6..00000000 --- a/lib/octocon_discord/proxy.ex +++ /dev/null @@ -1,556 +0,0 @@ -defmodule OctoconDiscord.Proxy.InvalidAlterError do - defexception [:message] -end - -defmodule OctoconDiscord.Proxy do - @moduledoc false - - require Logger - - import OctoconDiscord.Utils.CV2 - - alias Octocon.{ - Alters, - Messages - } - - alias OctoconDiscord.{Cache, Utils} - - alias Nostrum.Api - - @proxy_delay_ms 850 - @proxy_delete_delay_ms @proxy_delay_ms + 300 - - # https://discord.com/developers/docs/resources/channel#channel-object-channel-types - @thread_like_channel_types [ - # ANNOUNCEMENT_THREAD - 10, - # PUBLIC_THREAD - 11, - # PRIVATE_THREAD - 12 - ] - - # Checks whether a message is in a thread or not - # This uses the guild cache, but may send an API request if the guild isn't cached on this node for some reason - defp check_thread(%Nostrum.Struct.Message{guild_id: guild_id, channel_id: channel_id}) do - guild = Utils.get_cached_guild(guild_id) - - case Map.get(guild.threads, channel_id) do - nil -> - channel = Map.get(guild.channels, channel_id) - - if channel.type in @thread_like_channel_types do - {channel.parent_id, channel.parent_id, channel_id} - else - # Definitely not a thread - {channel_id, channel.parent_id, nil} - end - - thread -> - {thread.parent_id, thread.parent_id, thread.id} - end - end - - def with_proxy_prerequisites(user_id, message, fun) do - # Cache.Proxy is also responsible for keeping track of users who don't have an account - case Cache.Proxy.get(to_string(user_id)) do - {:error, :no_user} -> - # User doesn't have an Octocon account, ignore - :no_proxy - - {:ok, proxy_data} -> - {channel_id, parent_id, thread_id} = check_thread(message) - - # Fast path: if the channel is blacklisted, don't bother checking anything else - unless Cache.ChannelBlacklists.blacklisted?( - to_string(channel_id), - to_string(parent_id) - ) do - webhook = Cache.Webhooks.get_webhook(channel_id) - - if webhook == nil do - :no_proxy - else - settings_template = %Octocon.Accounts.ServerSettings{ - guild_id: to_string(message.guild_id) - } - - server_proxy_settings = - proxy_data.settings.server_settings - |> Map.get(to_string(message.guild_id), nil) - |> case do - nil -> - settings_template - - server_settings -> - settings_template - |> Map.merge(server_settings) - end - - if server_proxy_settings.proxying_disabled do - :no_proxy - else - server_settings = Cache.ServerSettings.get_settings(message.guild_id) - - fun.(%{ - message: message, - webhook: webhook, - proxy_data: - proxy_data - |> Map.put( - :settings, - Map.merge(proxy_data.settings, %{server_settings: server_proxy_settings}) - ), - thread_id: thread_id, - server_settings: server_settings - }) - end - end - end - end - end - - # Sends a proxied message to Discord - def send_proxy_message( - %{ - webhook: webhook, - message: message, - alter: {system_id, alter_id}, - proxy_data: %{ - settings: %{ - system_tag: system_tag, - show_system_tag: show_system_tag, - show_pronouns: show_pronouns, - use_proxy_delay: use_proxy_delay, - silent_proxying: silent_proxying - } - }, - thread_id: thread_id, - server_settings: server_settings - }, - is_reproxy, - proxy_fun \\ fn id, token, data -> Nostrum.Api.Webhook.execute(id, token, data, true) end - ) do - alter = - case Alters.get_alter_by_id({:system, system_id}, {:id, alter_id}, [ - :name, - :avatar_url, - :pronouns, - :color, - :proxy_name - ]) do - {:ok, alter} -> - alter - - {:error, :no_alter_id} -> - raise OctoconDiscord.Proxy.InvalidAlterError, - "Alter with ID #{alter_id} does not exist." - end - - final_tag = - cond do - system_tag == nil -> "" - server_settings[:force_system_tags] == true or show_system_tag == true -> " #{system_tag}" - true -> "" - end - - parsed_pronouns = - if show_pronouns == false do - "" - else - case alter.pronouns do - nil -> "" - "" -> "" - pronouns -> " (#{pronouns})" - end - end - - base_name = alter.proxy_name || alter.name - - name_length = String.length(base_name) - tag_length = String.length(final_tag) - - truncated_pronouns = - parsed_pronouns - |> String.slice(0..(80 - tag_length - name_length - 1)) - - final_username = "#{base_name}#{truncated_pronouns}#{final_tag}" - - webhook_data = %{ - content: message.content, - username: final_username, - avatar_url: alter.avatar_url, - thread_id: thread_id, - embeds: - case message.message_reference do - nil -> - nil - - %{message_id: message_id} -> - case Api.Message.get(message.channel_id, message_id) do - {:ok, reply} -> - [build_reply_embed(message, reply, alter.color)] - - _ -> - nil - end - end - } - - webhook_data = - if silent_proxying do - Map.put(webhook_data, :flags, Bitwise.bsl(1, 12)) - else - webhook_data - end - - context = %{ - system_id: system_id, - alter_id: alter_id, - author_id: to_string(message.author.id), - use_proxy_delay: use_proxy_delay, - silent_proxying: silent_proxying - } - - case message.attachments do - [] -> - # If we have no attachments, we're done - send_proxy_message_raw( - %{ - webhook: webhook, - message: message, - webhook_data: webhook_data, - server_settings: server_settings, - context: context - }, - is_reproxy, - proxy_fun - ) - - files -> - # Otherwise, we need to download the files and send them along with the message - send_proxy_message_with_files( - %{ - webhook: webhook, - message: message, - webhook_data: webhook_data, - files: files, - server_settings: server_settings, - context: context - }, - is_reproxy, - proxy_fun - ) - end - end - - # Sends a proxied message to Discord with files - defp send_proxy_message_with_files( - %{ - webhook: webhook, - message: message, - webhook_data: webhook_data, - files: files, - server_settings: server_settings, - context: context - }, - is_reproxy, - proxy_fun - ) do - attachments = - files - |> Stream.filter(fn file -> file.size < 20_000_000 end) - |> Task.async_stream( - fn %{filename: filename, url: url} = attachment -> - req = - Finch.build(:get, url) - |> Finch.request(Octocon.Finch) - - case req do - {:ok, %{body: body}} -> - file_data = %{name: filename, body: body} - - attachment_data = - cond do - filename == "voice-message.ogg" -> - %{ - filename: "voice-message.ogg", - content_type: "audio/ogg", - waveform: attachment.waveform, - duration_secs: attachment.duration_secs - } - - String.contains?(attachment.content_type, "image") -> - %{ - filename: filename, - content_type: attachment.content_type, - title: attachment.title, - description: attachment.description - } - - true -> - %{ - filename: filename, - content_type: attachment.content_type, - title: attachment.title - } - end - - {:ok, {file_data, attachment_data}} - - {:error, error} -> - {:error, error} - end - end, - timeout: :timer.seconds(15), - max_concurrency: 4 - ) - |> Stream.filter(fn - {:ok, {:ok, _}} -> true - _ -> false - end) - |> Stream.map(fn {:ok, {:ok, data}} -> data end) - |> Enum.to_list() - - is_voice_message = - Enum.any?(attachments, fn {%{name: name}, _} -> name == "voice-message.ogg" end) - - webhook_data = - webhook_data - |> Map.put(:files, Enum.map(attachments, fn {file_data, _} -> file_data end)) - |> Map.put( - :flags, - if attachments == [] do - Map.get(webhook_data, :flags) - else - # This is a bit hacky, but it lets us tell Discord that we're sending a voice message - if is_voice_message do - Bitwise.|||(Bitwise.bsl(1, 12), Bitwise.bsl(1, 13)) - else - Map.get(webhook_data, :flags) - end - end - ) - |> Map.put( - :attachments, - attachments - |> Enum.with_index() - |> Enum.map(fn {{_, attachment_data}, index} -> Map.put(attachment_data, :id, index) end) - ) - - # Delegate to `send_proxy_message_raw` with the updated webhook data - send_proxy_message_raw( - %{ - webhook: webhook, - message: message, - webhook_data: webhook_data, - server_settings: server_settings, - context: context - }, - is_reproxy, - proxy_fun - ) - end - - # Sends a proxied message to Discord with the given webhook data - defp send_proxy_message_raw( - %{ - webhook: webhook, - message: message, - webhook_data: webhook_data, - server_settings: server_settings, - context: context - }, - is_reproxy, - proxy_fun - ) do - webhook_task = - Task.async(fn -> - if !is_reproxy && context.use_proxy_delay do - Process.sleep(@proxy_delay_ms) - end - - result_message = proxy_fun.(webhook.id, webhook.token, webhook_data) - - case result_message do - {:ok, %{id: message_id}} -> - unless is_reproxy do - spawn(fn -> - attrs = - context - |> Map.put(:message_id, to_string(message_id)) - |> Map.put(:timestamp, Nostrum.Snowflake.creation_time(message_id)) - - Messages.insert_message(attrs) - end) - - spawn(fn -> - log_proxy_message( - message, - message_id, - server_settings - ) - end) - end - - {:error, error} -> - Logger.error("Failed to send proxy message: #{inspect(error)}") - end - end) - - delete_task = - Task.async(fn -> - if !is_reproxy && context.use_proxy_delay do - Process.sleep(@proxy_delete_delay_ms) - end - - unless is_reproxy do - Api.Message.delete(message.channel_id, message.id) - end - end) - - # Bail if Discord rate-limits us or otherwise fails to send the message - # This is especially useful to avoid holding attachments in RAM for too long - Task.await_many([webhook_task, delete_task], :timer.seconds(10)) - end - - # Logs a proxied message to the log channel - # If no log channel is set, or if the server doesn't have any settings in the database, ignore - defp log_proxy_message(_, _, nil), do: :ok - defp log_proxy_message(_, _, %{log_channel: nil}), do: :ok - - # Otherwise, proceed with logging - defp log_proxy_message( - %{ - guild_id: guild_id, - content: content, - channel_id: channel_id, - author: %{id: author_id, avatar: avatar_hash} - }, - message_id, - %{log_channel: log_channel} - ) do - permalink = "https://discord.com/channels/#{guild_id}/#{channel_id}/#{message_id}" - - truncated_content = - cond do - content == nil or content == "" -> - "*This message did not have any text.*" - - String.length(content) > 500 -> - content - |> String.slice(0..500) - |> Kernel.<>("\n...") - - true -> - content - end - - creation_time = Nostrum.Snowflake.creation_time(message_id) |> DateTime.to_unix() - - result = - Api.Message.create( - String.to_integer(log_channel), - %{ - components: [ - container( - [ - section( - [ - text("### Message proxied"), - text(truncated_content) - ], - thumbnail(Utils.get_avatar_url(author_id, avatar_hash)) - ), - separator(spacing: :large), - text("**Author:** <@#{author_id}>"), - text("**Sent at:** ()") - ], - %{accent_color: Utils.hex_to_int("#3F3793")} - ), - action_row([ - link_button(permalink, label: "Jump to message") - ]) - ], - allowed_mentions: :none, - flags: cv2_flags(false) |> Bitwise.bor(Bitwise.bsl(1, 12)) - } - ) - - case result do - {:ok, _} -> - :ok - - {:error, error} -> - Logger.error("Failed to log proxied message: #{inspect(error)}; #{inspect(log_channel)}") - end - end - - defp build_reply_embed( - message, - %{ - author: reply_author, - content: reply_content, - id: reply_id, - channel_id: reply_channel_id - }, - color - ) - when is_nil(reply_content) or reply_content == "" do - reply_embed( - reply_author, - "[Reply to:](https://discord.com/channels/#{message.guild_id}/#{reply_channel_id}/#{reply_id}) *This message did not have any text.*", - color - ) - end - - # Recreate replies as embeds - # [TODO]: Replace with components - defp build_reply_embed( - message, - %{ - author: reply_author, - content: reply_content, - id: reply_id, - channel_id: reply_channel_id - }, - color - ) do - truncated_content = - if String.length(reply_content) > 75 do - trimmed = - reply_content - |> String.slice(0..75) - - spoiler_count = - trimmed - |> String.split("||") - |> length() - |> then(&Kernel.-(&1, 1)) - - # If the spoiler count is odd, we need to add a spoiler tag to the end - # This ensures spoilered content isn't accidentally revealed when truncating - trimmed <> if rem(spoiler_count, 2) == 0, do: "...", else: "||..." - else - reply_content - end - - reply_embed( - reply_author, - "[Reply to:](https://discord.com/channels/#{message.guild_id}/#{reply_channel_id}/#{reply_id}) #{truncated_content}", - color - ) - end - - defp reply_embed(%{id: id, avatar: avatar, username: username}, text, color) do - %Nostrum.Struct.Embed{ - author: %Nostrum.Struct.Embed.Author{ - icon_url: Utils.get_avatar_url(id, avatar), - name: "#{username} ↩️" - }, - description: text, - color: Utils.hex_to_int(color) - } - end -end diff --git a/lib/octocon_discord/shard_manager.ex b/lib/octocon_discord/shard_manager.ex deleted file mode 100644 index 5b4e5d03..00000000 --- a/lib/octocon_discord/shard_manager.ex +++ /dev/null @@ -1,90 +0,0 @@ -defmodule OctoconDiscord.ShardManager do - @moduledoc """ - Manages a subset of shards for the Octocon Discord bot. - """ - - use GenServer - - require Logger - - def child_spec({id, _, _, _} = init_arg) do - %{ - id: "#{__MODULE__}-#{id}", - start: {__MODULE__, :start_link, [init_arg]}, - shutdown: 10_000 - } - end - - def start_link({id, _, _, _} = init_arg) do - case GenServer.start_link(__MODULE__, init_arg, name: via_tuple(id)) do - {:ok, pid} -> - {:ok, pid} - - {:error, {:already_started, pid}} -> - Logger.info( - "ShardManager with ID #{id} already started at #{inspect(pid)}, returning :ignore" - ) - - :ignore - end - end - - @impl GenServer - def init(init_arg) do - Logger.info("Starting ShardManager with ID #{inspect(init_arg)}") - Process.flag(:trap_exit, true) - {:ok, init_arg, {:continue, :connect_shards}} - end - - @impl GenServer - def handle_continue(:connect_shards, state) do - connect_shards(state) - - {:noreply, state} - end - - @impl GenServer - def terminate(_reason, {id, _, _, _} = state) do - # [TODO]: Shard handoff à la https://github.com/ElixirSeattle/tanx/tree/master/apps/tanx/lib/tanx/game/manager.ex (https://www.youtube.com/watch?v=nLApFANtkHs) - Logger.info("Terminating ShardManager with ID #{id}") - disconnect_shards(state) - - :ok - end - - @impl GenServer - def handle_info({:EXIT, _pid, reason}, state) do - {:stop, reason, state} - end - - defp connect_shards({id, start_shard, end_shard, desired_shards}) do - Logger.info( - "Connecting shards for ID #{id}; desired shards #{desired_shards}; start shard #{start_shard}; end shard #{end_shard}" - ) - - for shard <- start_shard..end_shard do - Nostrum.Shard.Supervisor.connect( - shard, - desired_shards, - OctoconDiscord.BotSupervisor.bot_options() - ) - end - end - - defp disconnect_shards({id, start_shard, end_shard, desired_shards}) do - Logger.info( - "Disconnecting shards for ID #{id}; desired shards #{desired_shards}; start shard #{start_shard}; end shard #{end_shard}" - ) - - for shard <- start_shard..end_shard do - Logger.debug("Disconnecting shard #{shard}") - data = Nostrum.Shard.Supervisor.disconnect(shard) - Logger.debug("Disconnected shard #{shard}: #{inspect(data)}") - data - end - end - - def via_tuple(id) do - {:via, Horde.Registry, {Octocon.Primary.HordeRegistry, "#{__MODULE__}-#{id}"}} - end -end diff --git a/lib/octocon_discord/status_updater.ex b/lib/octocon_discord/status_updater.ex deleted file mode 100644 index 3f39ecd9..00000000 --- a/lib/octocon_discord/status_updater.ex +++ /dev/null @@ -1,87 +0,0 @@ -defmodule OctoconDiscord.StatusUpdater do - @moduledoc """ - Manages a subset of shards for the Octocon Discord bot. - """ - - use GenServer - - require Logger - - @via {:via, Horde.Registry, {Octocon.Primary.HordeRegistry, __MODULE__}} - - @interval :timer.minutes(5) - - def start_link([]) do - case GenServer.start_link(__MODULE__, [], name: @via) do - {:ok, pid} -> - {:ok, pid} - - {:error, {:already_started, pid}} -> - Logger.info( - "OctoconDiscord.StatusUpdater already started at #{inspect(pid)}, returning :ignore" - ) - - :ignore - end - end - - def start do - GenServer.call(@via, :start) - end - - def bump({this_shard, shard_count}) do - GenServer.cast(@via, {:bump, {this_shard, shard_count}}) - end - - @impl GenServer - def init([]) do - {:ok, {:stopped, 0}} - end - - @impl GenServer - def handle_cast({:bump, {this_shard, shard_count}}, {:stopped, bump_count}) do - Logger.info("Received bump from shard #{this_shard}/#{shard_count}.") - - new_count = bump_count + 1 - - if new_count >= shard_count do - Logger.info("All shards have bumped; starting OctoconDiscord.StatusUpdater.") - - Process.send_after(self(), :update, :timer.seconds(30)) - - {:noreply, :started} - else - {:noreply, {:stopped, new_count}} - end - end - - @impl GenServer - def handle_info(:update, state) do - update_status() - Process.send_after(self(), :update, @interval) - - {:noreply, state} - end - - defp update_status do - Logger.info("Updating Discord status...") - - guild_count = :mnesia.table_info(Nostrum.Cache.GuildCache.Mnesia.table(), :size) - - Nostrum.Api.Self.update_status( - :online, - {:custom, "/help | In #{format_number(guild_count)} servers!"} - ) - end - - # E.g. 12345678 to "12,345,678" - def format_number(num) do - num - |> :erlang.integer_to_list() - |> Enum.reverse() - |> Enum.chunk_every(3) - |> Enum.map(&Enum.reverse/1) - |> Enum.reverse() - |> Enum.join(",") - end -end diff --git a/lib/octocon_discord/supervisor.ex b/lib/octocon_discord/supervisor.ex deleted file mode 100644 index 364c0861..00000000 --- a/lib/octocon_discord/supervisor.ex +++ /dev/null @@ -1,72 +0,0 @@ -defmodule OctoconDiscord.Supervisor do - @moduledoc """ - Root supervisor for the Discord portion of the Octocon application. This supervisor is guaranteed to be running on a primary node. - """ - use Supervisor - - require Logger - - def start_link(_init_arg) do - Supervisor.start_link(__MODULE__, [], name: __MODULE__) - end - - @impl Supervisor - def init([]) do - children = [ - # Cachex-backed and custom ETS-based cache managers - OctoconDiscord.Cache, - - # Autocomplete handlers - OctoconDiscord.Autocomplete, - - # Component handlers - OctoconDiscord.Components, - Nostrum.Application, - Supervisor.child_spec({Task, fn -> start_status_updater() end}, id: :start_status_updater), - - # Gateway events - Supervisor.child_spec({Task, fn -> start_unique_consumer() end}, - id: :start_unique_consumer - ), - - # Application commands - {Nosedrum.Storage.Dispatcher, name: Nosedrum.Storage.Dispatcher} - ] - - Supervisor.init(children, strategy: :one_for_one) - end - - # defp init_shards do - # node_count = ClusterUtils.primary_node_count() - # desired_shards = OctoconDiscord.get_desired_shards() - - # for i <- 1..node_count do - # # If desired_shards is 100, and we have 4 nodes, we want to start shards 0-24 on node 1, 25-49 on node 2, etc. - # start_shard = div((i - 1) * desired_shards, node_count) - # end_shard = div(i * desired_shards, node_count) - 1 - - # Horde.DynamicSupervisor.start_child( - # Octocon.Primary.HordeSupervisor, - # {OctoconDiscord.ShardManager, {i, start_shard, end_shard, desired_shards}} - # ) - # end - # end - - def start_unique_consumer do - Logger.info("Starting unique consumer") - - Horde.DynamicSupervisor.start_child( - Octocon.Primary.HordeSupervisor, - OctoconDiscord.BotSupervisor - ) - end - - defp start_status_updater do - Logger.info("Starting status updater") - - Horde.DynamicSupervisor.start_child( - Octocon.Primary.HordeSupervisor, - OctoconDiscord.StatusUpdater - ) - end -end diff --git a/lib/octocon_discord/utils.ex b/lib/octocon_discord/utils.ex deleted file mode 100644 index 47ed5c8f..00000000 --- a/lib/octocon_discord/utils.ex +++ /dev/null @@ -1,182 +0,0 @@ -defmodule OctoconDiscord.Utils do - @moduledoc """ - Various utility functions for Octocon's Discord frontend. - """ - - require Logger - - alias Nostrum.Api - alias Octocon.Accounts - alias Octocon.Accounts.User - - alias OctoconDiscord.Utils.{ - Commands, - Components, - CV2 - } - - @alias_regex ~r/^(?![\s\d])[^\n]{1,80}$/ - - def get_cached_guild(id), - do: wrap_cache_call(id, &Nostrum.Cache.GuildCache.get/1, &Api.Guild.get/1) - - defp wrap_cache_call(id, cache_function, api_function) do - case cache_function.(id) do - {:ok, data} -> - # Logger.debug("Discord guild cache hit") - data - - {:error, _} -> - # Logger.debug("Discord guild cache miss") - {:ok, api_result} = api_function.(id) - api_result - end - end - - def parse_id!(id) when is_integer(id), do: id - def parse_id!(id) when is_binary(id), do: Integer.parse(id) |> elem(0) - - def alter_id_valid?(id) when is_integer(id) and id > 0 and id < 32_768, do: true - - def alter_id_valid?(id) when is_binary(id) do - case Integer.parse(id) do - {int, ""} when int > 0 and int < 32_768 -> true - _ -> false - end - end - - def alter_id_valid?(_), do: false - - def alter_alias_valid?(aliaz), do: String.match?(aliaz, @alias_regex) - - def validate_alter_id(alter_id, callback) do - if alter_id_valid?(alter_id) do - callback.() - else - Components.error_component("You don't have an alter with ID **#{alter_id}**.") - end - end - - def validate_alias(aliaz) do - if String.match?(aliaz, @alias_regex) do - {:alias, aliaz} - else - {:error, - "Invalid alias. An alias must meet the following criteria:\n\n- Between 1-80 characters\n- Cannot consist of just a number\n-Cannot start with a space"} - end - end - - def parse_idalias(idalias, allow_nil \\ false) do - if idalias == nil or String.trim(idalias) == "" do - if allow_nil do - nil - else - {:error, "You must provide an alter ID (or alias) to use this command."} - end - else - if alter_id_valid?(idalias) do - {:id, parse_id!(idalias)} - else - validate_alias(idalias) - end - end - end - - def with_id_or_alias(options_or_idalias, callback, allow_nil \\ false) - - def with_id_or_alias(options, callback, allow_nil) when is_list(options) do - idalias = Commands.get_command_option(options, "id") - - with_id_or_alias(to_string(idalias), callback, allow_nil) - end - - def with_id_or_alias(idalias, callback, allow_nil) when is_binary(idalias) do - case parse_idalias(idalias, allow_nil) do - {:error, error} -> Components.error_component(error) - result -> callback.(result) - end - end - - def get_avatar_url(discord_id, avatar_hash), - do: "https://cdn.discordapp.com/avatars/#{discord_id}/#{avatar_hash}.png" - - def hex_to_int(nil), do: hex_to_int("#3F3793") - - def hex_to_int(hex) do - hex - |> String.downcase() - |> String.replace_leading("#", "") - |> String.to_integer(16) - end - - def validate_hex_color(color) do - if String.match?(color, ~r/^#?[0-9A-Fa-f]{6}$/i) do - {:ok, String.replace_leading(color, "#", "") |> String.upcase()} - else - :error - end - end - - def send_dm(%User{} = user, title, message) do - spawn(fn -> - Octocon.ClusterUtils.run_on_primary(fn -> - unless user.discord_id == nil do - {:ok, channel} = Api.User.create_dm(Integer.parse(user.discord_id) |> elem(0)) - - Api.Message.create(channel.id, %{ - components: [ - CV2.container( - [ - CV2.text("### #{title}"), - CV2.text(message) - ], - %{accent_color: hex_to_int("#3F3793")} - ) - ], - flags: CV2.cv2_flags(false) - }) - end - end) - end) - end - - def send_dm(system_identity, title, message) do - user = Accounts.get_user!(system_identity) - send_dm(user, title, message) - end - - def send_dm(%User{} = user, options) do - spawn(fn -> - unless user.discord_id == nil do - {:ok, channel} = Api.User.create_dm(Integer.parse(user.discord_id) |> elem(0)) - - Api.Message.create(channel.id, options) - end - end) - end - - def send_dm(system_identity, options) when is_map(options) do - user = Accounts.get_user!(system_identity) - send_dm(user, options) - end - - def get_guild_data do - Nostrum.Cache.GuildCache.fold([], fn guild, acc -> - [ - %{ - name: guild.name, - member_count: guild.member_count, - id: guild.id - } - | acc - ] - end) - |> Enum.sort_by(& &1.member_count, :desc) - end - - def security_level_to_string(nil), do: "Unknown" - def security_level_to_string(:private), do: "Private" - def security_level_to_string(:trusted_only), do: "Trusted friends only" - def security_level_to_string(:friends_only), do: "Friends only" - def security_level_to_string(:public), do: "Public" -end diff --git a/lib/octocon_discord/utils/commands.ex b/lib/octocon_discord/utils/commands.ex deleted file mode 100644 index 399d631d..00000000 --- a/lib/octocon_discord/utils/commands.ex +++ /dev/null @@ -1,86 +0,0 @@ -defmodule OctoconDiscord.Utils.Commands do - import OctoconDiscord.Utils.Components - - alias Octocon.Accounts - - def get_command_option(options, name) do - case Enum.find(options, fn %{name: option} -> option == name end) do - nil -> nil - option -> Map.get(option, :value) - end - end - - def register_message, - do: - error_component( - "You're not registered. Use the `/register` command or link your Discord account to your existing system." - ) - - def ensure_registered(discord_id, callback) do - if Accounts.user_exists?({:discord, discord_id}) do - callback.() - else - register_message() - end - end - - def add_show_option(options) do - options ++ - [ - %{ - name: "show", - description: "Show this message to the entire channel instead of just you.", - type: :boolean, - required: false - } - ] - end - - def get_show_option(options) do - case get_command_option(options, "show") do - nil -> false - value -> value - end - end - - def system_id_from_opts(opts, callback) do - num_opts = Enum.count(Map.keys(opts)) - num_nil = Map.values(opts) |> Enum.count(&is_nil/1) - - cond do - num_nil == num_opts -> - error_component("You must specify a system ID, Discord ping, or username.") - - num_nil != num_opts - 1 -> - error_component("You must *only* specify a system ID, Discord ping, *or* username.") - - opts.system_id -> - if Accounts.user_exists?({:system, opts.system_id}) do - callback.({:system, opts.system_id}, "**#{opts.system_id}**") - else - error_component("A system does not exist with ID **#{opts.system_id}**.") - end - - opts.discord_id -> - discord_id = to_string(opts.discord_id) - - if Accounts.user_exists?({:discord, discord_id}) do - callback.({:discord, discord_id}, "<@#{discord_id}>") - else - error_component("A system does not exist with that Discord account.") - end - - opts.username -> - case Accounts.get_user_id_by_username(opts.username) do - nil -> - error_component("A system does not exist with username **#{opts.username}**.") - - system_id -> - callback.({:system, system_id}, "**#{opts.username}**") - end - - true -> - error_component("An unknown error occurred.") - end - end -end diff --git a/lib/octocon_discord/utils/components.ex b/lib/octocon_discord/utils/components.ex deleted file mode 100644 index 8fc2ccd9..00000000 --- a/lib/octocon_discord/utils/components.ex +++ /dev/null @@ -1,375 +0,0 @@ -defmodule OctoconDiscord.Utils.Components do - alias Octocon.Accounts - - alias OctoconDiscord.Utils - alias OctoconDiscord.Utils.Emojis - - import OctoconDiscord.Utils.CV2 - - def error_component_raw(error) do - container( - [ - text("### :x: Whoops!"), - text(error) - ], - %{ - accent_color: 0xFF0000 - } - ) - end - - def error_component(error, ephemeral? \\ true), - do: [ - components: [error_component_raw(error)], - flags: cv2_flags(ephemeral?) - ] - - def success_component_raw(success) do - container( - [ - text("### :white_check_mark: Success!"), - text(success) - ], - %{ - accent_color: 0x00FF00 - } - ) - end - - def success_component(success, ephemeral? \\ true), - do: [ - components: [success_component_raw(success)], - flags: cv2_flags(ephemeral?) - ] - - def system_component_raw(system, self?) do - discord_settings = system.discord_settings || %Accounts.DiscordSettings{} - - normalized_description = - (system.description || "") - |> String.replace("\\n", "\n") - |> String.trim() - - description = - case String.length(normalized_description) do - 0 -> - "*This system does not have a description.*" - - length when length > 1500 -> - normalized_description - |> String.slice(0..1500) - |> Kernel.<>("\n...") - - _ -> - normalized_description - end - - upper_text = [ - text("## Information for <@#{system.discord_id}>"), - text(description) - ] - - container( - [ - case system.avatar_url do - url when url != nil and url != "" -> - section(upper_text, thumbnail(url)) - - _ -> - upper_text - end, - separator(spacing: :large), - text("**ID:** `#{system.id}`"), - text("**Username:** #{if system.username, do: system.username, else: "None"}"), - text( - "**Discord:** #{if system.discord_id, do: "<@#{system.discord_id}>", else: "Not linked"}" - ), - if self? do - [ - separator(spacing: :large), - text("**Email linked:** #{if system.email, do: "Yes", else: "No"}"), - text( - "**System tag:** #{if discord_settings.system_tag, do: discord_settings.system_tag, else: "None"}" - ) - ] - else - [] - end - ] - |> List.flatten() - ) - end - - def system_component(system, self?) do - [ - components: [system_component_raw(system, self?)], - flags: cv2_flags() - ] - end - - def alter_component(alter, guarded \\ false) do - normalized_description = - (alter.description || "") - |> String.replace("\\n", "\n") - |> String.trim() - - description = - case String.length(normalized_description) do - 0 -> - "*This alter does not have a description.*" - - length when length > 1500 -> - normalized_description - |> String.slice(0..1500) - |> Kernel.<>("\n...") - - _ -> - normalized_description - end - - fronts = if guarded, do: [], else: Octocon.Fronts.currently_fronting({:system, alter.user_id}) - - tags = - if guarded, - do: Octocon.Tags.get_public_tags_for_alter({:system, alter.user_id}, {:id, alter.id}), - else: Octocon.Tags.get_tags_for_alter({:system, alter.user_id}, {:id, alter.id}) - - is_fronting = - not guarded and alter.id in (fronts |> Enum.map(& &1.front.alter_id)) - - is_primary = - not guarded and - alter.id == - fronts - |> Enum.find(fn f -> f.primary end) - |> case do - nil -> nil - front -> front.front.alter_id - end - - fronting_text = - cond do - not is_fronting -> - nil - - is_primary -> - "\n\n⏫ • Currently fronting! (Main)" - - true -> - "\n\n⬆️ • Currently fronting!" - end - - upper_text = - [ - text( - "## #{alter.name || "Unnamed alter"}#{if alter.pronouns && alter.pronouns != "", do: " (#{alter.pronouns})", else: ""}" - ), - if(fronting_text != nil, do: text(fronting_text), else: []), - text(description) - ] - |> List.flatten() - - [ - container( - [ - case alter.avatar_url do - url when url != nil and url != "" -> - section(upper_text, thumbnail(url)) - - _ -> - upper_text - end, - separator(spacing: :large), - text(""" - **ID:** `#{alter.id}`#{case alter.alias do - nil -> "" - alias -> " • **Alias:** `#{alias}`" - end} - """), - case alter.proxy_name do - nil -> - [] - - "" -> - [] - - proxy_name -> - text("**Proxy name:** #{proxy_name}") - end, - text(""" - **Proxies:**\n#{case alter.discord_proxies do - [] -> "None" - nil -> "None" - proxies -> Enum.map_join(proxies, "\n", fn proxy -> "- `#{proxy}`" end) - end} - """), - if tags == [] do - [] - else - [ - separator(spacing: :large), - text(""" - **Tags:**\n#{tags |> Enum.sort_by(& &1.name) |> Enum.map_join("\n", fn tag -> "- #{tag.name}" end)} - """) - ] - end, - if guarded do - [] - else - inserted_at = - alter.inserted_at |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_unix() - - # updated_at = alter.updated_at |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_unix() - - [ - separator(spacing: :large), - text("**Security level:** #{Utils.security_level_to_string(alter.security_level)}"), - text("**Created:** ()") - # **Last updated:** () - ] - end - ] - |> List.flatten(), - %{accent_color: Utils.hex_to_int(alter.color)} - ), - cond do - guarded -> - [] - - is_fronting -> - primary_command = if is_primary, do: "removeprimary", else: "setprimary" - - action_row([ - button("alter|removefront|#{alter.id}", :secondary, - label: "Remove from front", - emoji: %{name: "⬇️"} - ), - button("alter|#{primary_command}|#{alter.id}", :secondary, - label: if(is_primary, do: "Unset as main front", else: "Set as main front"), - emoji: %{name: if(is_primary, do: "⏬", else: "⏫")} - ) - ]) - - true -> - action_row([ - button("alter|addfront|#{alter.id}", :secondary, - label: "Add to front", - emoji: %{name: "⬆️"} - ), - button("alter|setfront|#{alter.id}", :secondary, - label: "Set as front", - emoji: %{name: "📍"} - ) - ]) - end, - if guarded do - [] - else - action_row([]) - - [] - end - ] - |> List.flatten() - end - - def tag_component(tag, alters, guarded \\ false) do - normalized_description = - (tag.description || "") - |> String.replace("\\n", "\n") - |> String.trim() - - description = - case String.length(normalized_description) do - 0 -> - "*This tag does not have a description.*" - - length when length > 1500 -> - normalized_description - |> String.slice(0..1500) - |> Kernel.<>("\n...") - - _ -> - normalized_description - end - - upper_text = - [ - text("## #{tag.name || "Unnamed tag"}"), - text(description) - ] - - inserted_at = - tag.inserted_at |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_unix() - - [ - container( - [ - upper_text, - separator(spacing: :large), - text(""" - **Short ID:** `#{String.slice(tag.id, 0..7)}` - """), - case tag.parent_tag_id do - nil -> - [] - - parent_tag_id -> - case Octocon.Tags.get_tag({:system, tag.user_id}, parent_tag_id) do - nil -> - [] - - parent_tag -> - tag_text = text("**Parent tag:** #{parent_tag.name}") - - if guarded do - [tag_text] - else - section( - [text("**Parent tag:** #{parent_tag.name}")], - button( - "tag|view|#{parent_tag.id}", - :secondary, - emoji: Emojis.component_emoji(Emojis.open()) - ) - ) - end - end - end, - if alters == [] do - text("**Alters**:\nNone") - else - filtered_alters = - if guarded do - Enum.filter(alters, fn alter -> alter.security_level == :public end) - else - alters - end - - if filtered_alters == [] do - text("**Alters:**\nNone") - else - text(""" - **Alters:**\n#{filtered_alters |> Enum.sort_by(& &1.name) |> Enum.map_join("\n", - fn alter -> "- #{alter.name}#{if alter.pronouns && alter.pronouns != "", do: " (#{alter.pronouns})", else: ""}" end)} - """) - end - end, - if guarded do - [] - else - [ - separator(spacing: :large), - text("**Security level:** #{Utils.security_level_to_string(tag.security_level)}"), - text("**Created:** ()") - # **Last updated:** () - ] - end - ] - |> List.flatten(), - %{accent_color: Utils.hex_to_int(tag.color)} - ) - ] - |> List.flatten() - end -end diff --git a/lib/octocon_discord/utils/cv2.ex b/lib/octocon_discord/utils/cv2.ex deleted file mode 100644 index 72e02665..00000000 --- a/lib/octocon_discord/utils/cv2.ex +++ /dev/null @@ -1,142 +0,0 @@ -defmodule OctoconDiscord.Utils.CV2 do - def container(components, options \\ []) do - %{ - type: 17, - components: components - } - |> merge_options(options) - end - - def section(components, accessory, options \\ []) do - %{ - type: 9, - components: components, - accessory: accessory - } - |> merge_options(options) - end - - def separator(options \\ []) do - divider = Keyword.get(options, :divider, true) - - spacing = - Keyword.get(options, :spacing, :small) - |> case do - :small -> 1 - :large -> 2 - end - - %{ - type: 14, - divider: divider, - spacing: spacing - } - end - - def thumbnail(url, options \\ []) do - %{ - type: 11, - media: %{url: url} - } - |> merge_options(options) - end - - def action_row(components, options \\ []) do - %{ - type: 1, - components: components - } - |> merge_options(options) - end - - def string_select(id, options_list, options \\ []) do - %{ - type: 3, - custom_id: id, - options: options_list - } - |> merge_options(options) - end - - def user_select(id, options \\ []) do - %{ - type: 5, - custom_id: id - } - |> merge_options(options) - end - - def role_select(id, options \\ []) do - %{ - type: 6, - custom_id: id - } - |> merge_options(options) - end - - def mentionable_select(id, options \\ []) do - %{ - type: 7, - custom_id: id - } - |> merge_options(options) - end - - def channel_select(id, options \\ []) do - %{ - type: 8, - custom_id: id - } - |> merge_options(options) - end - - def text(text, options \\ []) do - %{ - type: 10, - content: text - } - |> merge_options(options) - end - - def button(id, style, options \\ []) do - style = - case style do - :primary -> 1 - :secondary -> 2 - :success -> 3 - :danger -> 4 - :link -> 5 - :premium -> 6 - num when is_integer(num) and num in 1..6 -> num - _ -> 2 - end - - %{ - type: 2, - custom_id: id, - style: style - } - |> merge_options(options) - end - - def link_button(url, options \\ []) do - %{ - type: 2, - style: 5, - url: url - } - |> merge_options(options) - end - - def cv2_flags(ephemeral \\ true) do - if ephemeral do - Bitwise.bor(Bitwise.bsl(1, 15), Bitwise.bsl(1, 6)) - else - Bitwise.bsl(1, 15) - end - end - - defp merge_options(defaults, options) do - Map.merge(defaults, Enum.into(options, %{})) - end -end diff --git a/lib/octocon_discord/utils/emojis.ex b/lib/octocon_discord/utils/emojis.ex deleted file mode 100644 index 37d5669b..00000000 --- a/lib/octocon_discord/utils/emojis.ex +++ /dev/null @@ -1,28 +0,0 @@ -defmodule OctoconDiscord.Utils.Emojis do - @moduledoc false - - alias Nostrum.Struct.Emoji - - def one, do: %Emoji{name: "number1", id: 1_255_764_961_020_350_534} - def two, do: %Emoji{name: "number2", id: 1_255_765_058_387_181_588} - def three, do: %Emoji{name: "number3", id: 1_255_764_964_568_862_721} - def four, do: %Emoji{name: "number4", id: 1_255_764_946_399_006_751} - def five, do: %Emoji{name: "number5", id: 1_255_764_947_976_327_270} - def six, do: %Emoji{name: "number6", id: 1_255_764_949_179_830_414} - def seven, do: %Emoji{name: "number7", id: 1_255_764_950_060_634_172} - def eight, do: %Emoji{name: "number8", id: 1_255_764_951_218_393_109} - def nine, do: %Emoji{name: "number9", id: 1_255_764_952_149_659_658} - def resources, do: %Emoji{name: "resources", id: 1_255_765_008_676_032_542} - def faq, do: %Emoji{name: "faq", id: 1_255_765_434_872_107_018} - def slashcommand, do: %Emoji{name: "slashcommand", id: 1_255_764_957_375_758_388} - def folder, do: %Emoji{name: "folder", id: 1_255_840_880_980_529_152} - def first, do: %Emoji{name: "first", id: 1_467_695_298_037_158_122} - def back, do: %Emoji{name: "back", id: 1_467_695_301_962_895_566} - def forward, do: %Emoji{name: "forward", id: 1_467_695_300_381_507_595} - def last, do: %Emoji{name: "last", id: 1_467_695_299_152_580_701} - def open, do: %Emoji{name: "open", id: 1_467_702_233_771_675_821} - - def component_emoji(%Emoji{name: name, id: id}) do - %{name: name, id: id} - end -end diff --git a/lib/octocon_web.ex b/lib/octocon_web.ex deleted file mode 100644 index 4561b4fd..00000000 --- a/lib/octocon_web.ex +++ /dev/null @@ -1,65 +0,0 @@ -defmodule OctoconWeb do - @moduledoc """ - The entrypoint for defining your web interface, such - as controllers, components, channels, and so on. - - This can be used in your application as: - - use OctoconWeb, :controller - use OctoconWeb, :html - - The definitions below will be executed for every controller, - component, etc, so keep them short and clean, focused - on imports, uses and aliases. - - Do NOT define functions inside the quoted expressions - below. Instead, define additional modules and import - those modules here. - """ - - def static_paths, do: ~w(robots.txt .well-known) - - def router do - quote do - use Phoenix.Router, helpers: false - - # Import common connection and controller functions to use in pipelines - import Plug.Conn - import Phoenix.Controller - end - end - - def channel do - quote do - use Phoenix.Channel - end - end - - def controller do - quote do - use Phoenix.Controller, - formats: [:html, :json], - layouts: [html: OctoconWeb.Layouts] - - import Plug.Conn - - unquote(verified_routes()) - end - end - - def verified_routes do - quote do - use Phoenix.VerifiedRoutes, - endpoint: OctoconWeb.Endpoint, - router: OctoconWeb.Router, - statics: OctoconWeb.static_paths() - end - end - - @doc """ - When used, dispatch to the appropriate controller/view/etc. - """ - defmacro __using__(which) when is_atom(which) do - apply(__MODULE__, which, []) - end -end diff --git a/lib/octocon_web/auth_error_controller.ex b/lib/octocon_web/auth_error_controller.ex deleted file mode 100644 index 5dee25db..00000000 --- a/lib/octocon_web/auth_error_controller.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule OctoconWeb.AuthErrorHandler do - @moduledoc false - import Plug.Conn - - @behaviour Guardian.Plug.ErrorHandler - - @impl Guardian.Plug.ErrorHandler - def auth_error(conn, {type, _reason}, _opts) do - body = Jason.encode!(%{error: "auth: " <> to_string(type)}) - - conn - |> put_resp_content_type("application/json") - |> send_resp(401, body) - end -end diff --git a/lib/octocon_web/auth_pipeline.ex b/lib/octocon_web/auth_pipeline.ex deleted file mode 100644 index 747a0911..00000000 --- a/lib/octocon_web/auth_pipeline.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule OctoconWeb.AuthPipeline do - @moduledoc false - use Guardian.Plug.Pipeline, otp_app: :octocon - - plug Guardian.Plug.VerifyHeader, scheme: "Bearer", claims: %{"typ" => "access"} - plug Guardian.Plug.LoadResource, allow_blank: true -end diff --git a/lib/octocon_web/controllers/alter_journal_controller.ex b/lib/octocon_web/controllers/alter_journal_controller.ex deleted file mode 100644 index 66913a44..00000000 --- a/lib/octocon_web/controllers/alter_journal_controller.ex +++ /dev/null @@ -1,201 +0,0 @@ -defmodule OctoconWeb.AlterJournalController do - use OctoconWeb, :controller - - alias Octocon.Journals - - def index(conn, %{"id" => alter_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.get_alter_journal_entries({:system, system_id}, {:id, alter_id}, fields: :bare) do - {:error, :no_alter} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred", code: "unknown_error"}) - - entries -> - conn - |> render(:index, entries: entries) - end - end - - def show(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.get_alter_journal_entry({:system, system_id}, journal_id) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - entry -> - conn - |> render(:show, entry: entry) - end - end - - def create(conn, %{"id" => alter_id, "title" => title}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.create_alter_journal_entry({:system, system_id}, {:id, alter_id}, title) do - {:error, :no_alter} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid journal entry attributes.", - code: "invalid_journal_entry_attributes" - }) - - {:ok, entry} -> - conn - |> put_status(:created) - |> render(:show, entry: entry) - end - end - - def delete(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.delete_alter_journal_entry({:system, system_id}, journal_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def update(conn, %{"journal_id" => journal_id} = attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "title", - "content", - "color" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - if map_size(attrs) == 0 do - conn - |> put_status(:bad_request) - |> json(%{ - error: "No valid journal entry attributes provided.", - code: "no_journal_attributes" - }) - else - case Journals.update_alter_journal_entry({:system, system_id}, journal_id, attrs) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid journal entry attributes.", - code: "invalid_journal_attributes" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def lock(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_alter_journal_entry({:system, system_id}, journal_id, %{locked: true}) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unlock(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_alter_journal_entry({:system, system_id}, journal_id, %{locked: false}) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def pin(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_alter_journal_entry({:system, system_id}, journal_id, %{pinned: true}) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unpin(conn, %{"journal_id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_alter_journal_entry({:system, system_id}, journal_id, %{pinned: false}) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end -end diff --git a/lib/octocon_web/controllers/alter_journal_json.ex b/lib/octocon_web/controllers/alter_journal_json.ex deleted file mode 100644 index 54016b86..00000000 --- a/lib/octocon_web/controllers/alter_journal_json.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule OctoconWeb.AlterJournalJSON do - def index(%{entries: entries}) do - %{data: Enum.map(entries, &data/1)} - end - - def show(%{entry: entry}) do - %{data: data(entry)} - end - - def data(entry) do - entry - |> Map.from_struct() - |> Map.drop([:__meta__, :alter, :user]) - end -end diff --git a/lib/octocon_web/controllers/auth_controller.ex b/lib/octocon_web/controllers/auth_controller.ex deleted file mode 100644 index 7eb75eea..00000000 --- a/lib/octocon_web/controllers/auth_controller.ex +++ /dev/null @@ -1,172 +0,0 @@ -defmodule OctoconWeb.AuthController do - use OctoconWeb, :controller - - alias Octocon.Accounts - alias Octocon.Auth.Guardian - - plug :put_metadata - plug Ueberauth - - @request_paths [ - "/auth/discord", - "/auth/google" - ] - - def put_metadata( - %{ - params: %{ - "platform" => platform, - "version_code" => version_code, - "is_beta" => is_beta - } - } = conn, - _params - ) do - if conn.request_path in @request_paths do - metadata = - Jason.encode!(%{ - "platform" => to_string(platform), - "version_code" => to_string(version_code), - "is_beta" => to_string(is_beta) - }) - - conn - |> put_session(:metadata, metadata) - else - conn - end - end - - def put_metadata(conn, _params) do - if conn.request_path in @request_paths do - conn - |> put_session( - :metadata, - Jason.encode!(%{ - "platform" => "unknown", - "version_code" => "unknown", - "is_beta" => "false" - }) - ) - else - conn - end - end - - def request(conn, _params) do - conn - |> send_resp(403, "") - end - - def callback(%{assigns: %{ueberauth_auth: %{uid: discord_id}}} = conn, %{ - "provider" => "discord" - }) do - user = - case Accounts.get_user_registry({:discord, discord_id}) do - nil -> raise "Signups disabled" - user -> user - end - - user_id = - case user do - %Octocon.Accounts.User{id: user_id} -> user_id - %{user_id: user_id} -> user_id - end - - {:ok, token, _claims} = Guardian.encode_and_sign(user_id) - - metadata = get_session(conn, :metadata) |> Jason.decode!() - - url_params = "?token=#{token}&id=#{user_id}" - - redirect_url = - case metadata do - %{"platform" => "wasm"} -> "https://octocon.app/app" - _ -> "https://octocon.app/deep/auth/token" - end - |> Kernel.<>(url_params) - - redirect(conn, external: redirect_url) - rescue - _ -> - conn - |> put_status(403) - |> text( - "Octocon is shutting down. Sign-ups are disabled. Please see our Discord for more information: https://discord.com/invite/octocon" - ) - end - - def callback(%{assigns: %{ueberauth_auth: %{info: %{email: email}}}} = conn, %{ - "provider" => "google" - }) do - user = - case Accounts.get_user_registry({:email, email}) do - nil -> raise "Signups disabled" - user -> user - end - - user_id = - case user do - %Octocon.Accounts.User{id: user_id} -> user_id - %{user_id: user_id} -> user_id - end - - {:ok, token, _claims} = Guardian.encode_and_sign(user_id) - - metadata = get_session(conn, :metadata) |> Jason.decode!() - - url_params = "?token=#{token}&id=#{user_id}" - - redirect_url = - case metadata do - %{"platform" => "wasm", "is_beta" => "true"} -> "https://beta.octocon.app/app" - %{"platform" => "wasm", "is_beta" => "false"} -> "https://octocon.app/app" - _ -> "https://octocon.app/deep/auth/token" - end - |> Kernel.<>(url_params) - - redirect(conn, external: redirect_url) - rescue - _ -> - conn - |> put_status(403) - |> text( - "Octocon is shutting down. Sign-ups are disabled. Please see our Discord for more information: https://discord.com/invite/octocon" - ) - end - - def callback(%{assigns: %{ueberauth_auth: %{uid: apple_id}}} = conn, %{ - "provider" => "apple" - }) do - user = - case Accounts.get_user_registry({:apple, apple_id}) do - nil -> raise "Signups disabled" - user -> user - end - - user_id = - case user do - %Octocon.Accounts.User{id: user_id} -> user_id - %{user_id: user_id} -> user_id - end - - {:ok, token, _claims} = Guardian.encode_and_sign(user_id) - - url_params = "?token=#{token}&id=#{user_id}" - - redirect(conn, external: "https://octocon.app/deep/auth/token#{url_params}") - rescue - _ -> - conn - |> put_status(403) - |> text( - "Octocon is shutting down. Sign-ups are disabled. Please see our Discord for more information: https://discord.com/invite/octocon" - ) - end - - def callback(conn, _params) do - conn - |> put_status(403) - |> text("Failed to authenticate. Did you reload the page or copy-paste the URL?") - end -end diff --git a/lib/octocon_web/controllers/auth_link_controller.ex b/lib/octocon_web/controllers/auth_link_controller.ex deleted file mode 100644 index 509654ba..00000000 --- a/lib/octocon_web/controllers/auth_link_controller.ex +++ /dev/null @@ -1,128 +0,0 @@ -defmodule OctoconWeb.AuthLinkController do - use OctoconWeb, :controller - - require Logger - - alias Octocon.Global.LinkTokenRegistry - - plug :put_link_token - plug Ueberauth, providers: [:google_link, :discord_link, :apple_link] - - def request(conn, _params) do - conn - |> send_resp(403, "") - end - - def put_link_token(%{params: %{"link_token" => link_token}} = conn, _params) do - Logger.info("Link token injected into session: #{link_token}") - - conn - |> put_session(:link_token, link_token) - end - - def put_link_token(conn, _params) do - conn - end - - def callback(%{assigns: %{ueberauth_auth: auth_data}} = conn, %{"provider" => provider}) do - link_token = get_session(conn, :link_token) - Logger.info("Link token received: #{link_token}") - - case Octocon.ClusterUtils.run_on_primary(fn -> - LinkTokenRegistry.get(link_token) - end) do - nil -> - conn - |> delete_session(:link_token) - |> put_status(403) - |> text("This link token is invalid or has expired.") - - system_id when is_binary(system_id) -> - Logger.info("Recovered ID: #{system_id}") - - Octocon.ClusterUtils.run_on_primary(fn -> - LinkTokenRegistry.delete(link_token) - end) - - try_link_account_wrapper(provider, conn, system_id, auth_data) - end - end - - def callback(conn, _params) do - conn - |> put_status(403) - |> text("Failed to authenticate. Did you reload the page or copy-paste the URL?") - end - - def try_link_account_wrapper(provider, conn, system_id, auth_data) do - case Octocon.Accounts.get_user({:system, system_id}) do - nil -> - conn - |> put_status(403) - |> json(%{error: "System not found"}) - - user -> - try_link_account(provider, conn, user, auth_data) - end - end - - def try_link_account("discord", conn, user, %{uid: discord_id}) do - case Octocon.Accounts.link_discord_to_user(user, to_string(discord_id)) do - {:error, :already_linked} -> - conn - |> put_status(403) - |> json(%{ - error: "A Discord account is already linked to this account; please unlink it first." - }) - - {:error, :user_exists} -> - conn - |> put_status(500) - |> json(%{error: "This Discord account is already linked to another account."}) - - {:ok, _} -> - conn - |> redirect(external: "https://octocon.app/deep/link_success/discord") - end - end - - def try_link_account("google", conn, user, %{info: %{email: email}}) do - case Octocon.Accounts.link_email_to_user(user, email) do - {:error, :already_linked} -> - conn - |> put_status(403) - |> json(%{ - error: "A Google account is already linked to this account; please unlink it first." - }) - - {:error, :user_exists} -> - conn - |> put_status(500) - |> json(%{error: "This email address is already linked to another account."}) - - {:ok, _} -> - conn - |> redirect(external: "https://octocon.app/deep/link_success/google") - end - end - - def try_link_account("apple", conn, user, %{info: %{uid: apple_id}}) do - case Octocon.Accounts.link_apple_to_user(user, apple_id) do - {:error, :already_linked} -> - conn - |> put_status(403) - |> json(%{ - error: "An Apple account is already linked to this account; please unlink it first." - }) - - {:error, :user_exists} -> - conn - |> put_status(500) - |> json(%{error: "This Apple account is already linked to another account."}) - - {:ok, _} -> - conn - |> redirect(external: "https://octocon.app/deep/link_success/apple") - end - end -end diff --git a/lib/octocon_web/controllers/auth_link_token_controller.ex b/lib/octocon_web/controllers/auth_link_token_controller.ex deleted file mode 100644 index 3466903c..00000000 --- a/lib/octocon_web/controllers/auth_link_token_controller.ex +++ /dev/null @@ -1,20 +0,0 @@ -defmodule OctoconWeb.AuthLinkTokenController do - use OctoconWeb, :controller - - require Logger - - alias Octocon.Global.LinkTokenRegistry - - def get(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - link_token = - Octocon.ClusterUtils.run_on_primary(fn -> LinkTokenRegistry.put(system_id) end) - - Logger.info("Link token generated for #{system_id}: #{link_token}") - - conn - |> put_status(200) - |> json(%{data: %{token: link_token}}) - end -end diff --git a/lib/octocon_web/controllers/changeset_json.ex b/lib/octocon_web/controllers/changeset_json.ex deleted file mode 100644 index 60f6d819..00000000 --- a/lib/octocon_web/controllers/changeset_json.ex +++ /dev/null @@ -1,16 +0,0 @@ -defmodule OctoconWeb.ChangesetJSON do - @doc """ - Renders changeset errors. - """ - def error(%{changeset: changeset}) do - # When encoded, the changeset returns its errors - # as a JSON object. So we just pass it forward. - %{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)} - end - - defp translate_error({msg, opts}) do - Enum.reduce(opts, msg, fn {key, value}, acc -> - String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end) - end) - end -end diff --git a/lib/octocon_web/controllers/error_json.ex b/lib/octocon_web/controllers/error_json.ex deleted file mode 100644 index 59994b78..00000000 --- a/lib/octocon_web/controllers/error_json.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule OctoconWeb.ErrorJSON do - # If you want to customize a particular status code, - # you may add your own clauses, such as: - # - # def render("500.json", _assigns) do - # %{errors: %{detail: "Internal Server Error"}} - # end - - # By default, Phoenix returns the status message from - # the template name. For example, "404.json" becomes - # "Not Found". - def render(template, _assigns) do - %{error: Phoenix.Controller.status_message_from_template(template)} - end -end diff --git a/lib/octocon_web/controllers/fallback_controller.ex b/lib/octocon_web/controllers/fallback_controller.ex deleted file mode 100644 index c15ca913..00000000 --- a/lib/octocon_web/controllers/fallback_controller.ex +++ /dev/null @@ -1,24 +0,0 @@ -defmodule OctoconWeb.FallbackController do - @moduledoc """ - Translates controller action results into valid `Plug.Conn` responses. - - See `Phoenix.Controller.action_fallback/1` for more details. - """ - use OctoconWeb, :controller - - # This clause handles errors returned by Ecto's insert/update/delete. - def call(conn, {:error, %Ecto.Changeset{} = changeset}) do - conn - |> put_status(:unprocessable_entity) - |> put_view(json: OctoconWeb.ChangesetJSON) - |> render(:error, changeset: changeset) - end - - # This clause is an example of how to handle resources that cannot be found. - def call(conn, {:error, :not_found}) do - conn - |> put_status(:not_found) - |> put_view(html: OctoconWeb.ErrorHTML, json: OctoconWeb.ErrorJSON) - |> render(:"404") - end -end diff --git a/lib/octocon_web/controllers/friend_controller.ex b/lib/octocon_web/controllers/friend_controller.ex deleted file mode 100644 index 5367d173..00000000 --- a/lib/octocon_web/controllers/friend_controller.ex +++ /dev/null @@ -1,166 +0,0 @@ -defmodule OctoconWeb.FriendController do - use OctoconWeb, :controller - - import OctoconWeb.Utils.Systems - - alias Octocon.Friendships - - action_fallback OctoconWeb.FallbackController - - def index(conn, _) do - system_id = conn.private[:guardian_default_resource] - friendships = Friendships.list_friendships_guarded({:system, system_id}) - - render(conn, :index, friendships: friendships) - end - - def show(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "I'm pretty sure you don't count as your own friend. (Cannot view friendship status for self.)", - code: "cannot_view_own_friendship" - }) - - {:other, system} -> - case Friendships.get_friendship_guarded({:system, caller_id}, {:system, system.id}) do - nil -> - conn - |> put_status(:not_found) - |> json(%{ - error: "You are not friends with that system.", - code: "friendship_not_found" - }) - - friendship -> - render(conn, :show, friendship: friendship) - end - end - end - - def delete(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "I'm pretty sure you don't count as your own friend. (Cannot delete friendship with self.)", - code: "cannot_delete_own_friendship" - }) - - {:other, system} -> - case Friendships.remove_friendship({:system, caller_id}, {:system, system.id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :not_friends} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "You are not friends with that system.", - code: "friendship_not_found" - }) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{ - error: "An unknown error occurred.", - code: "unknown_error" - }) - end - end - end - - def trust(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "I'm pretty sure you don't count as your own friend. (Cannot trust self.)", - code: "cannot_trust_self" - }) - - {:other, system} -> - case Friendships.trust_friend({:system, caller_id}, {:system, system.id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :not_friends} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "You are not friends with that system.", - code: "friendship_not_found" - }) - - {:error, :database} -> - conn - |> put_status(:internal_server_error) - |> json(%{ - error: "An unknown error occurred.", - code: "unknown_error" - }) - end - end - end - - def untrust(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "I'm pretty sure you don't count as your own friend. (Cannot untrust self.)", - code: "cannot_untrust_self" - }) - - {:other, system} -> - case Friendships.untrust_friend({:system, caller_id}, {:system, system.id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :not_friends} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "You are not friends with that system.", - code: "friendship_not_found" - }) - - {:error, :database} -> - conn - |> put_status(:internal_server_error) - |> json(%{ - error: "An unknown error occurred.", - code: "unknown_error" - }) - end - end - end -end diff --git a/lib/octocon_web/controllers/friend_json.ex b/lib/octocon_web/controllers/friend_json.ex deleted file mode 100644 index 5945e73e..00000000 --- a/lib/octocon_web/controllers/friend_json.ex +++ /dev/null @@ -1,33 +0,0 @@ -defmodule OctoconWeb.FriendJSON do - def index(%{friendships: friendships}) do - %{data: Enum.map(friendships, &data/1)} - end - - def show(%{friendship: friendship}) do - %{data: data(friendship)} - end - - def data(%{friend: friend, friendship: friendship, fronting: fronting}) do - %{ - friendship: %{ - level: friendship.level, - since: friendship.since - }, - friend: %{ - id: friend.id, - avatar_url: friend.avatar_url, - username: friend.username, - description: friend.description, - discord_id: friend.discord_id - }, - fronting: - Enum.map(fronting, fn %{alter: alter, front: front, primary: primary} -> - %{ - alter: OctoconWeb.System.AlterJSON.data(alter), - front: Map.take(front, [:comment, :alter_id]), - primary: primary - } - end) - } - end -end diff --git a/lib/octocon_web/controllers/friend_request_controller.ex b/lib/octocon_web/controllers/friend_request_controller.ex deleted file mode 100644 index edaba075..00000000 --- a/lib/octocon_web/controllers/friend_request_controller.ex +++ /dev/null @@ -1,204 +0,0 @@ -defmodule OctoconWeb.FriendRequestController do - use OctoconWeb, :controller - - import OctoconWeb.Utils.Systems - - alias Octocon.Friendships - - action_fallback OctoconWeb.FallbackController - - def index(conn, _) do - system_id = conn.private[:guardian_default_resource] - incoming = Friendships.incoming_friend_requests({:system, system_id}) - outgoing = Friendships.outgoing_friend_requests({:system, system_id}) - - render(conn, :index, requests: %{incoming: incoming, outgoing: outgoing}) - end - - def accept(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You cannot accept a friend request from yourself.", - code: "cannot_accept_self" - }) - - {:other, system} -> - case Friendships.accept_request({:system, system.id}, {:system, caller_id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :already_friends} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You are already friends with this system.", - code: "already_friends" - }) - - {:error, :not_requested} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "No friend request found from system \"#{system.id}\".", - code: "not_requested" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def reject(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You cannot reject a friend request from yourself.", - code: "cannot_reject_self" - }) - - {:other, system} -> - case Friendships.reject_request({:system, system.id}, {:system, caller_id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :already_friends} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You are already friends with this system.", - code: "already_friends" - }) - - {:error, :not_requested} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "No friend request found from system \"#{system.id}\".", - code: "not_requested" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def cancel(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You cannot cancel a friend request to yourself.", - code: "cannot_cancel_self" - }) - - {:other, system} -> - case Friendships.cancel_request({:system, caller_id}, {:system, system.id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :already_friends} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You are already friends with this system.", - code: "already_friends" - }) - - {:error, :not_requested} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "No friend request found to system \"#{system.id}\".", - code: "not_requested" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def send(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You cannot send a friend request to yourself.", - code: "cannot_send_self" - }) - - {:other, system} -> - case Friendships.send_request({:system, caller_id}, {:system, system.id}) do - {:ok, :accepted} -> - send_resp(conn, :no_content, "") - - {:ok, :sent} -> - send_resp(conn, :no_content, "") - - {:error, :already_friends} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You are already friends with this system.", - code: "already_friends" - }) - - {:error, :already_sent_request} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "You have already sent a friend request to this system.", - code: "already_sent_request" - }) - - {:error, :no_user} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "That system does not exist.", - code: "no_user" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end -end diff --git a/lib/octocon_web/controllers/friend_request_json.ex b/lib/octocon_web/controllers/friend_request_json.ex deleted file mode 100644 index c44df68a..00000000 --- a/lib/octocon_web/controllers/friend_request_json.ex +++ /dev/null @@ -1,52 +0,0 @@ -defmodule OctoconWeb.FriendRequestJSON do - def index(%{requests: %{incoming: incoming, outgoing: outgoing}}) do - %{ - data: %{ - incoming: - incoming - |> Enum.map(fn %{request: request, from: from} -> - %{ - request: %{ - date_sent: request.date_sent - }, - system: %{ - id: from.id, - username: from.username, - discord_id: from.discord_id, - avatar_url: from.avatar_url - } - } - end), - outgoing: - outgoing - |> Enum.map(fn %{request: request, to: to} -> - %{ - request: %{ - date_sent: request.date_sent - }, - system: %{ - id: to.id, - username: to.username, - discord_id: to.discord_id, - avatar_url: to.avatar_url - } - } - end) - } - } - end - - def data(%{request: request, system: system}) do - %{ - request: %{ - date_sent: request.date_sent - }, - system: %{ - id: system.id, - username: system.username, - discord_id: system.discord_id, - avatar_url: system.avatar_url - } - } - end -end diff --git a/lib/octocon_web/controllers/global_journal_controller.ex b/lib/octocon_web/controllers/global_journal_controller.ex deleted file mode 100644 index 6417f8e5..00000000 --- a/lib/octocon_web/controllers/global_journal_controller.ex +++ /dev/null @@ -1,291 +0,0 @@ -defmodule OctoconWeb.GlobalJournalController do - use OctoconWeb, :controller - - alias Octocon.Journals - - def index(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - Journals.get_global_journal_entries({:system, system_id}, fields: :bare) - |> then(fn entries -> render(conn, :index, entries: entries) end) - end - - def show(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.get_global_journal_entry({:system, system_id}, journal_id) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - entry -> - conn - |> render(:show, entry: entry) - end - end - - def create(conn, %{"title" => title}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.create_global_journal_entry({:system, system_id}, title) do - {:error, :not_found} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid journal entry attributes.", - code: "invalid_journal_entry_attributes" - }) - - {:ok, entry} -> - conn - |> put_status(:created) - |> render(:show, entry: entry) - end - end - - def delete(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.delete_global_journal_entry({:system, system_id}, journal_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def update(conn, %{"id" => journal_id} = attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "title", - "content", - "color" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - if map_size(attrs) == 0 do - conn - |> put_status(:bad_request) - |> json(%{ - error: "No valid journal entry attributes provided.", - code: "no_journal_attributes" - }) - else - case Journals.update_global_journal_entry({:system, system_id}, journal_id, attrs) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid journal entry attributes.", - code: "invalid_journal_attributes" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def lock(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_global_journal_entry({:system, system_id}, journal_id, %{locked: true}) do - {:ok, _entry} -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unlock(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_global_journal_entry({:system, system_id}, journal_id, %{locked: false}) do - {:ok, _entry} -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def pin(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_global_journal_entry({:system, system_id}, journal_id, %{pinned: true}) do - {:ok, _entry} -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unpin(conn, %{"id" => journal_id}) do - system_id = conn.private[:guardian_default_resource] - - case Journals.update_global_journal_entry({:system, system_id}, journal_id, %{pinned: false}) do - {:ok, _entry} -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Journal entry not found.", code: "journal_entry_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def attach_alter(conn, %{"id" => journal_id, "alter_id" => alter_id}) - when is_integer(alter_id) do - system_id = conn.private[:guardian_default_resource] - - case Journals.attach_alter_to_global_entry({:system, system_id}, journal_id, {:id, alter_id}) do - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :alter_not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def attach_alter(conn, %{"id" => journal_id, "alter_id" => alter_id}) - when is_binary(alter_id) do - case parse_integer(alter_id) do - {:ok, parsed_id} -> - attach_alter(conn, %{"id" => journal_id, "alter_id" => parsed_id}) - - :error -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - end - end - - def detach_alter(conn, %{"id" => journal_id, "alter_id" => alter_id}) - when is_integer(alter_id) do - system_id = conn.private[:guardian_default_resource] - - case Journals.detach_alter_from_global_entry( - {:system, system_id}, - journal_id, - {:id, alter_id} - ) do - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :alter_not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "Journal entry not found or that alter is not attached.", - code: "journal_entry_or_alter_not_found" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def detach_alter(conn, %{"id" => journal_id, "alter_id" => alter_id}) - when is_binary(alter_id) do - case parse_integer(alter_id) do - {:ok, parsed_id} -> - detach_alter(conn, %{"id" => journal_id, "alter_id" => parsed_id}) - - :error -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - end - end - - def parse_integer(value) do - case Integer.parse(value) do - :error -> :error - {id, _} when id < 0 or id > 32_767 -> :error - {id, _} -> {:ok, id} - end - end -end diff --git a/lib/octocon_web/controllers/global_journal_json.ex b/lib/octocon_web/controllers/global_journal_json.ex deleted file mode 100644 index e19ced0d..00000000 --- a/lib/octocon_web/controllers/global_journal_json.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule OctoconWeb.GlobalJournalJSON do - def index(%{entries: entries}) do - %{data: Enum.map(entries, &data/1)} - end - - def show(%{entry: entry}) do - %{data: data(entry)} - end - - def data(entry) do - entry - |> Map.from_struct() - |> Map.drop([:__meta__, :user]) - end -end diff --git a/lib/octocon_web/controllers/heartbeat_controller.ex b/lib/octocon_web/controllers/heartbeat_controller.ex deleted file mode 100644 index de969ca0..00000000 --- a/lib/octocon_web/controllers/heartbeat_controller.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule OctoconWeb.HeartbeatController do - use OctoconWeb, :controller - - def index(conn, _params) do - conn - |> put_status(:ok) - |> json(%{response: "ACK"}) - end -end diff --git a/lib/octocon_web/controllers/poll_controller.ex b/lib/octocon_web/controllers/poll_controller.ex deleted file mode 100644 index e008423d..00000000 --- a/lib/octocon_web/controllers/poll_controller.ex +++ /dev/null @@ -1,123 +0,0 @@ -defmodule OctoconWeb.PollController do - use OctoconWeb, :controller - - alias Octocon.Polls - - def index(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - Polls.get_polls({:system, system_id}) - |> then(fn polls -> render(conn, :index, polls: polls) end) - end - - def show(conn, %{"id" => poll_id}) do - system_id = conn.private[:guardian_default_resource] - - case Polls.get_poll({:system, system_id}, poll_id) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Poll not found.", code: "poll_not_found"}) - - entry -> - conn - |> render(:show, entry: entry) - end - end - - def create(conn, attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "title", - "description", - "type", - "time_end" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - case Polls.create_poll({:system, system_id}, attrs) do - {:error, :not_found} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid poll attributes.", - code: "invalid_poll_attributes" - }) - - {:ok, entry} -> - conn - |> put_status(:created) - |> render(:show, entry: entry) - end - end - - def delete(conn, %{"id" => poll_id}) do - system_id = conn.private[:guardian_default_resource] - - case Polls.delete_poll({:system, system_id}, poll_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Poll not found.", code: "poll_not_found"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def update(conn, %{"id" => poll_id} = attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "title", - "description", - "time_end", - "data" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - if map_size(attrs) == 0 do - conn - |> put_status(:bad_request) - |> json(%{ - error: "No valid poll attributes provided.", - code: "no_poll_attributes" - }) - else - case Polls.update_poll({:system, system_id}, poll_id, attrs) do - {:ok, _entry} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Poll not found.", code: "poll_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid poll attributes.", - code: "invalid_poll_attributes" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end -end diff --git a/lib/octocon_web/controllers/poll_json.ex b/lib/octocon_web/controllers/poll_json.ex deleted file mode 100644 index 20b2d236..00000000 --- a/lib/octocon_web/controllers/poll_json.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule OctoconWeb.PollJSON do - def index(%{polls: polls}) do - %{data: Enum.map(polls, &data/1)} - end - - def show(%{entry: entry}) do - %{data: data(entry)} - end - - def data(entry) do - entry - |> Map.from_struct() - |> Map.drop([:__meta__, :user]) - end -end diff --git a/lib/octocon_web/controllers/settings_controller.ex b/lib/octocon_web/controllers/settings_controller.ex deleted file mode 100644 index 36299b30..00000000 --- a/lib/octocon_web/controllers/settings_controller.ex +++ /dev/null @@ -1,547 +0,0 @@ -defmodule OctoconWeb.SettingsController do - use OctoconWeb, :controller - - alias Octocon.{ - Accounts, - NotificationTokens - } - - alias Octocon.Utils.User, as: UserUtils - - alias Octocon.Workers.{ - PluralKitImportWorker, - SimplyPluralImportWorker - } - - def reset_encryption(conn, %{}) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.wipe_encrypted_data({:system, system_id}) do - :ok -> - conn - |> send_resp(:no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def recover_encryption(conn, %{"recovery_code" => encrypted_recovery_code}) do - system_id = conn.private[:guardian_default_resource] - user = Accounts.get_user!({:system, system_id}) - - case Hammer.check_rate("encryption_key:#{system_id}", :timer.seconds(5), 1) do - {:allow, _count} -> - Octocon.ClusterUtils.run_on_sidecar( - fn -> - case decrypt_recovery_code(encrypted_recovery_code) do - {:ok, recovery_code} -> - old_checksum = user.encryption_key_checksum - - key = generate_encryption_key(user, recovery_code) - - new_checksum = - :crypto.hash(:sha256, key) - |> Base.encode64() - |> String.slice(0..8) - - if old_checksum == new_checksum do - conn - |> put_status(:ok) - |> json(%{data: %{key: Base.encode64(key)}}) - else - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid recovery code.", - code: "invalid_recovery_code" - }) - end - - {:error, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Failed to decrypt recovery code.", - code: "decryption_error" - }) - end - end, - timeout: 10_000 - ) - - _ -> - conn - |> put_status(:too_many_requests) - |> json(%{ - error: - "You have requested too many encryption key recoveries. Please wait a few seconds before trying again.", - code: "rate_limited" - }) - end - end - - def setup_encryption(conn, %{ - "recovery_code" => encrypted_recovery_code - # "public_key" => public_key_pem - }) do - system_id = conn.private[:guardian_default_resource] - user = Accounts.get_user!({:system, system_id}) - - case Hammer.check_rate("encryption_key:#{system_id}", :timer.seconds(5), 1) do - {:allow, _count} -> - case decrypt_recovery_code(encrypted_recovery_code) do - {:ok, recovery_code} -> - # case encrypt_key_with_public_key(user, recovery_code, public_key_pem) do - # {:ok, encrypted_key} -> - # if is_init do - # Accounts.update_user(user, %{encryption_initialized: true}) - # end - - # conn - # |> put_status(:ok) - # |> json(%{data: %{key: encrypted_key}}) - - # {:error, e} -> - # conn - # |> put_status(:internal_server_error) - # |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - # end - - key = generate_encryption_key(user, recovery_code) - - # Only take a few bytes for the checksum - key_checksum = - :crypto.hash(:sha256, key) - |> Base.encode64() - |> String.slice(0..8) - - Accounts.update_user(user, %{ - encryption_initialized: true, - encryption_key_checksum: key_checksum - }) - - conn - |> put_status(:ok) - |> json(%{data: %{key: Base.encode64(key)}}) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "Failed to decrypt recovery code.", code: "decryption_error"}) - end - - _ -> - conn - |> put_status(:too_many_requests) - |> json(%{ - error: "You have requested too many encryption keys. Please wait before trying again.", - code: "rate_limited" - }) - end - end - - defp generate_encryption_key(%Octocon.Accounts.User{id: user_id, salt: salt}, recovery_code) do - pepper = Application.get_env(:octocon, :pepper) - hash_data = pepper <> user_id <> recovery_code - hash = :crypto.hash(:sha256, hash_data) - - hash - |> Argon2.Base.hash_password(salt, t_cost: 12, hash_len: 32, format: :raw_hash) - |> Base.decode16!(case: :lower) - |> Base.encode64() - end - - defp decrypt_recovery_code(encrypted_recovery_code) do - private_key_pem = Application.get_env(:octocon, :private_key_pem) - - private_jwk = JOSE.JWK.from_pem(private_key_pem) - - {recovery_code, _jwe} = JOSE.JWE.block_decrypt(private_jwk, encrypted_recovery_code) - - {:ok, recovery_code} - rescue - e -> {:error, e} - end - - def update_username(conn, %{"username" => username}) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.update_username({:system, system_id}, username) do - {:error, :already_linked} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Your username is already set to \"#{username}\".", - code: "username_already_set" - }) - - {:error, :user_exists} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "The username \"#{username}\" is already taken.", - code: "username_taken" - }) - - {:error, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "The username \"#{username}\" is invalid. It must satisfy the following criteria:\n\n- Between 5-16 characters\n- Only contains letters, numbers, dashes, and underscores\n- Does not start or end with a symbol\n- Does not consist of seven lowercase letters in a row (like a system ID)", - code: "username_invalid" - }) - - {:ok, _} -> - conn - |> put_status(:ok) - |> json(%{message: "Your username has been changed to \"#{username}\"."}) - end - end - - def clear_username(conn, _params) do - system_id = conn.private[:guardian_default_resource] - user = Accounts.get_user({:system, system_id}) - - case Accounts.update_user(user, %{username: nil}) do - {:ok, _} -> - conn - |> put_status(:ok) - |> json(%{message: "Your username has been cleared."}) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def create_custom_field(conn, %{"name" => name, "type" => type}) do - system_id = conn.private[:guardian_default_resource] - - atom_type = - try do - String.to_existing_atom(type) - rescue - _ -> :text - end - - case Accounts.add_field({:system, system_id}, %{name: name, type: atom_type}) do - {:ok, _} -> - conn - |> send_resp(:created, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def remove_custom_field(conn, %{"id" => id}) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.remove_field({:system, system_id}, id) do - {:ok, _} -> - conn - |> send_resp(:no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def edit_custom_field(conn, %{"id" => id} = params) do - system_id = conn.private[:guardian_default_resource] - - attrs = - %{} - |> then(fn attrs -> - if Map.has_key?(params, "name") do - Map.put(attrs, :name, params["name"]) - else - attrs - end - end) - |> then(fn attrs -> - if Map.has_key?(params, "security_level") do - try do - Map.put(attrs, :security_level, String.to_existing_atom(params["security_level"])) - rescue - _ -> attrs - end - else - attrs - end - end) - |> then(fn attrs -> - if Map.has_key?(params, "locked") do - Map.put(attrs, :locked, params["locked"]) - else - attrs - end - end) - - case Accounts.edit_field({:system, system_id}, id, attrs) do - {:ok, _} -> - conn - |> send_resp(:no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def relocate_custom_field(conn, %{"id" => id, "index" => index}) when is_binary(index) do - case Integer.parse(index) do - :error -> - conn - |> put_status(:bad_request) - |> json(%{error: "The index must be an integer.", code: "invalid_index"}) - - {idx, _} when idx < 0 -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "The index must be greater than or equal to zero.", - code: "invalid_index" - }) - - {idx, _} -> - relocate_custom_field(conn, %{"id" => id, "index" => idx}) - end - end - - def relocate_custom_field(conn, %{"id" => id, "index" => index}) when is_number(index) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.relocate_field({:system, system_id}, id, index) do - {:ok, _} -> - conn - |> send_resp(:no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def upload_avatar(conn, %{"file" => %Plug.Upload{} = file}) do - system_id = conn.private[:guardian_default_resource] - system = Accounts.get_user!({:system, system_id}) - - case UserUtils.upload_avatar(system, file.path) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An error occurred while uploading the file.", code: "unknown_error"}) - end - end - - def delete_avatar(conn, _params) do - system_id = conn.private[:guardian_default_resource] - system = Accounts.get_user!({:system, system_id}) - - case Accounts.update_user(system, %{avatar_url: nil}) do - {:ok, _} -> - Octocon.Utils.nuke_existing_avatars!(system_id, "self") - send_resp(conn, :no_content, "") - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def import_pk(conn, %{"token" => token}) do - system_id = conn.private[:guardian_default_resource] - - PluralKitImportWorker.perform(%{"system_id" => system_id, "pk_token" => String.trim(token)}) - - send_resp(conn, :no_content, "") - end - - def import_sp(conn, %{"token" => token}) do - system_id = conn.private[:guardian_default_resource] - - SimplyPluralImportWorker.perform(%{ - "system_id" => system_id, - "sp_token" => String.trim(token) - }) - - send_resp(conn, :no_content, "") - end - - def add_push_token(conn, %{"token" => token}) do - system_id = conn.private[:guardian_default_resource] - - case NotificationTokens.add_notification_token({:system, system_id}, token) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "Failed to synchronize your push notification token."}) - end - end - - def invalidate_push_token(conn, %{"token" => token}) do - case NotificationTokens.invalidate_notification_token(token) do - {1, _} -> - send_resp(conn, :no_content, "") - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "Failed to desynchronize your push notification token."}) - end - end - - def unlink_discord(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.unlink_discord_from_user({:system, system_id}) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, :not_linked} -> - conn - |> put_status(:bad_request) - |> json(%{error: "You are not linked to a Discord account.", code: "not_linked"}) - - {:error, :cannot_unlink} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "You cannot unlink your Discord account unless you are also logged in with another authentication method (Google or Apple).", - code: "cannot_unlink" - }) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unlink_email(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.unlink_email_from_user({:system, system_id}) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, :not_linked} -> - conn - |> put_status(:bad_request) - |> json(%{error: "You are not linked to an email account.", code: "not_linked"}) - - {:error, :cannot_unlink} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "You cannot unlink your email account unless you are also logged in with another authentication method (Discord or Apple).", - code: "cannot_unlink" - }) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def unlink_apple(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.unlink_apple_from_user({:system, system_id}) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, :not_linked} -> - conn - |> put_status(:bad_request) - |> json(%{error: "You are not linked to an Apple account.", code: "not_linked"}) - - {:error, :cannot_unlink} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: - "You cannot unlink your Apple account unless you are also logged in with another authentication method (Discord or Google).", - code: "cannot_unlink" - }) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def update_description(conn, %{"description" => description}) do - system_id = conn.private[:guardian_default_resource] - system = Accounts.get_user!({:system, system_id}) - - case Accounts.update_user(system, %{description: description}) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, %Ecto.Changeset{}} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "The description must be at most 3,000 characters.", - code: "description_invalid" - }) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def wipe_alters(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.wipe_alters({:system, system_id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def delete_account(conn, _params) do - system_id = conn.private[:guardian_default_resource] - - case Accounts.delete_user({:system, system_id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end -end diff --git a/lib/octocon_web/controllers/system/alter_controller.ex b/lib/octocon_web/controllers/system/alter_controller.ex deleted file mode 100644 index 9ac78b92..00000000 --- a/lib/octocon_web/controllers/system/alter_controller.ex +++ /dev/null @@ -1,211 +0,0 @@ -defmodule OctoconWeb.System.AlterController do - use OctoconWeb, :controller - - import OctoconWeb.Utils.Systems - - alias Octocon.Alters - alias Octocon.Utils.Alter, as: AlterUtils - - action_fallback OctoconWeb.FallbackController - - def index(conn, %{"system_id" => system_id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, system_id) do - {:noreply, conn} -> - conn - - {:self, system} -> - render(conn, :index_me, alters: Alters.get_alters_by_id({:system, system.id})) - - {:other, system} -> - render(conn, :index, - alters: Alters.get_alters_guarded({:system, system.id}, {:system, caller_id}) - ) - end - end - - def show(conn, %{"system_id" => system_id, "id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, system_id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - case Alters.get_alter_by_id({:system, caller_id}, {:id, id}) do - {:ok, alter} -> - render(conn, :show_me, alter: alter) - - _ -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - end - - {:other, system} -> - case Alters.get_alter_guarded({:system, system.id}, {:id, id}, {:system, caller_id}) do - {:ok, alter} -> - render(conn, :show, alter: alter) - - _ -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - end - end - end - - def create(conn, %{"name" => alter_name}) do - system_id = conn.private[:guardian_default_resource] - - case Alters.create_alter({:system, system_id}, %{name: alter_name}) do - {:ok, id, alter} -> - conn - |> put_status(:created) - |> put_resp_header("location", ~p"/api/systems/me/alters/#{id}") - |> render(:show_me, alter: alter) - - {:error, :database} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def delete(conn, %{"id" => id}) do - system_id = conn.private[:guardian_default_resource] - - case Alters.delete_alter({:system, system_id}, {:id, id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :no_alter_id} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :no_alter_alias} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def update(conn, %{"id" => id} = attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "name", - "description", - "avatar_url", - "color", - "pronouns", - "security_level", - "fields", - "proxy_name", - "alias", - "untracked", - "archived", - "pinned" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - if map_size(attrs) == 0 do - conn - |> put_status(:bad_request) - |> json(%{error: "No valid alter attributes provided.", code: "no_alter_attributes"}) - else - if Map.has_key?(attrs, :alias) and attrs.alias != nil and - Alters.alias_taken?({:system, system_id}, attrs.alias) do - {:ok, alter} = Alters.get_alter_by_id({:system, system_id}, {:id, id}) - - if alter != nil and alter.alias != attrs.alias do - conn - |> put_status(:bad_request) - |> json(%{error: "That alias is already taken.", code: "alias_taken"}) - else - update_alter_internal(conn, system_id, id, attrs) - end - else - update_alter_internal(conn, system_id, id, attrs) - end - end - end - - defp update_alter_internal(conn, system_id, id, attrs) do - case Alters.update_alter({:system, system_id}, {:id, id}, attrs) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter attributes.", code: "invalid_alter_attributes"}) - - {:error, :no_alter_id} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def upload_avatar(conn, %{"id" => id, "file" => %Plug.Upload{} = file}) do - system_id = conn.private[:guardian_default_resource] - - alter_id = Alters.resolve_alter({:system, system_id}, {:id, id}) - - if alter_id != nil do - case AlterUtils.upload_avatar({:system, system_id}, {:id, id}, file.path) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An error occurred while uploading the file.", code: "unknown_error"}) - end - else - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - end - end - - def delete_avatar(conn, %{"id" => id}) do - system_id = conn.private[:guardian_default_resource] - - case Alters.update_alter({:system, system_id}, {:id, id}, %{avatar_url: nil}) do - :ok -> - Octocon.Utils.nuke_existing_avatars!(system_id, id) - send_resp(conn, :no_content, "") - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter attributes.", code: "invalid_alter_attributes"}) - - {:error, :no_alter_id} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end -end diff --git a/lib/octocon_web/controllers/system/alter_json.ex b/lib/octocon_web/controllers/system/alter_json.ex deleted file mode 100644 index 1377f710..00000000 --- a/lib/octocon_web/controllers/system/alter_json.ex +++ /dev/null @@ -1,41 +0,0 @@ -defmodule OctoconWeb.System.AlterJSON do - alias Octocon.Alters.Alter - - def index_me(%{alters: alters}) do - %{data: Enum.map(alters, &data_me/1)} - end - - def index(%{alters: alters}) do - %{data: alters |> Enum.map(&data/1)} - end - - def show_me(%{alter: alter}) do - %{data: data_me(alter)} - end - - def show(%{alter: alter}) do - %{data: data(alter)} - end - - def data_me(alter) do - # Only strip internal metadata - alter - |> Map.from_struct() - |> Map.drop(Alter.dropped_fields()) - |> Map.put(:fields, (alter.fields || []) |> Enum.map(&Map.drop(&1, [:__struct__, :__meta__]))) - end - - def data(%Alter{} = alter) do - Map.take(alter, [ - :id, - :name, - :pronouns, - :description, - :fields, - :avatar_url, - :extra_images, - :color - ]) - |> Map.put(:fields, (alter.fields || []) |> Enum.map(&Map.drop(&1, [:__struct__, :__meta__]))) - end -end diff --git a/lib/octocon_web/controllers/system/fronting_controller.ex b/lib/octocon_web/controllers/system/fronting_controller.ex deleted file mode 100644 index 6b1799e4..00000000 --- a/lib/octocon_web/controllers/system/fronting_controller.ex +++ /dev/null @@ -1,267 +0,0 @@ -defmodule OctoconWeb.System.FrontingController do - use OctoconWeb, :controller - - alias Octocon.{ - Accounts, - Fronts - } - - import OctoconWeb.Utils.Systems - - action_fallback OctoconWeb.FallbackController - - def index(conn, %{"system_id" => system_id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, system_id) do - {:noreply, conn} -> - conn - - {:self, system} -> - render(conn, :index_me, fronts: Fronts.currently_fronting({:system, system.id})) - - {:other, system} -> - render(conn, :index, - fronts: Fronts.currently_fronting_guarded({:system, system.id}, {:system, caller_id}) - ) - end - end - - def show(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - try do - case Fronts.get_by_id({:system, caller_id}, {:id, id}) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Front not found.", code: "front_not_found"}) - - front -> - render(conn, :show_me, front: front) - end - rescue - _ in Ecto.Query.CastError -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid front ID.", code: "invalid_front_id"}) - end - end - - def delete(conn, %{"id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case Fronts.delete_front({:system, caller_id}, id) do - {:ok, _front} -> - send_resp(conn, :no_content, "") - - {:error, _changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid front ID.", code: "invalid_id"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def between(conn, %{"start" => time_start_unix, "end" => time_end_unix}) - when is_integer(time_start_unix) and is_integer(time_end_unix) do - system_id = conn.private[:guardian_default_resource] - - with {:ok, time_start} <- DateTime.from_unix(time_start_unix), - {:ok, time_end} <- DateTime.from_unix(time_end_unix) do - render(conn, :index_me, - fronts: Fronts.fronted_between({:system, system_id}, time_start, time_end) - ) - else - {:error, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid start or end anchor. Please pass valid Unix timestamps.", - code: "invalid_anchor" - }) - end - end - - def between(conn, %{"start" => time_start_unix, "end" => time_end_unix}) - when is_binary(time_start_unix) and is_binary(time_end_unix) do - with {time_start, _} <- Integer.parse(time_start_unix), - {time_end, _} <- Integer.parse(time_end_unix) do - between(conn, %{"start" => time_start, "end" => time_end}) - else - _ -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid start or end anchor. Please pass valid Unix timestamps.", - code: "invalid_anchor" - }) - end - end - - def month(conn, %{"end_anchor" => end_anchor}) when is_integer(end_anchor) do - system_id = conn.private[:guardian_default_resource] - - case DateTime.from_unix(end_anchor) do - {:ok, datetime} -> - render(conn, :index_me, fronts: Fronts.fronted_for_month({:system, system_id}, datetime)) - - {:error, _} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid end anchor. Please pass a valid Unix timestamp.", - code: "invalid_end_anchor" - }) - end - end - - def month(conn, %{"end_anchor" => end_anchor}) when is_binary(end_anchor) do - case Integer.parse(end_anchor) do - {new_anchor, _} -> - month(conn, %{"end_anchor" => new_anchor}) - - _ -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid end anchor. Please pass a valid Unix timestamp.", - code: "invalid_end_anchor" - }) - end - end - - def update(conn, %{"start" => start_fronts, "end" => end_fronts}) - when is_list(start_fronts) and is_list(end_fronts) do - system_id = conn.private[:guardian_default_resource] - - case Fronts.bulk_update_fronts({:system, system_id}, start_fronts, end_fronts) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def primary(conn, params) do - system_id = conn.private[:guardian_default_resource] - alter_id = params["id"] || nil - - cond do - is_nil(alter_id) -> - Accounts.set_primary_front({:system, system_id}, nil) - send_resp(conn, :no_content, "") - - is_number(alter_id) and alter_id not in 1..32_767 -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - - is_number(alter_id) -> - if Fronts.fronting?({:system, system_id}, {:id, alter_id}) do - Accounts.set_primary_front({:system, system_id}, alter_id) - send_resp(conn, :no_content, "") - else - conn - |> put_status(:bad_request) - |> json(%{error: "That alter is not currently fronting.", code: "not_fronting"}) - end - - true -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - end - end - - def update_comment(conn, %{"id" => front_id, "comment" => comment}) do - system_id = conn.private[:guardian_default_resource] - - case Fronts.update_comment({:system, system_id}, front_id, comment) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :no_front} -> - conn - |> put_status(:bad_request) - |> json(%{error: "A front does not exist with that ID.", code: "no_front"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid comment. Must be less than 50 characters.", - code: "invalid_comment" - }) - end - end - - def start(conn, %{"id" => alter_id} = params) do - system_id = conn.private[:guardian_default_resource] - - comment = params["comment"] || "" - - case Fronts.start_front({:system, system_id}, {:id, alter_id}, comment) do - {:ok, front} -> - conn - |> put_status(:created) - |> put_resp_header("location", ~p"/api/systems/me/front/#{front.id}") - |> text("") - - {:error, :already_fronting} -> - conn - |> put_status(:bad_request) - |> json(%{error: "That alter is already fronting.", code: "already_fronting"}) - - {:error, _} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def endd(conn, %{"id" => alter_id}) do - system_id = conn.private[:guardian_default_resource] - - case Fronts.end_front({:system, system_id}, {:id, alter_id}) do - :ok -> - send_resp(conn, :no_content, "") - - {:error, :not_fronting} -> - conn - |> put_status(:bad_request) - |> json(%{error: "That alter is not currently fronting.", code: "not_fronting"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def set(conn, %{"id" => id}) do - system_id = conn.private[:guardian_default_resource] - - case Fronts.set_front({:system, system_id}, {:id, id}) do - {:ok, _} -> - send_resp(conn, :no_content, "") - - {:error, :already_fronting} -> - conn - |> put_status(:bad_request) - |> json(%{error: "That alter is already fronting.", code: "already_fronting"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end -end diff --git a/lib/octocon_web/controllers/system/fronting_json.ex b/lib/octocon_web/controllers/system/fronting_json.ex deleted file mode 100644 index f21d699d..00000000 --- a/lib/octocon_web/controllers/system/fronting_json.ex +++ /dev/null @@ -1,49 +0,0 @@ -defmodule OctoconWeb.System.FrontingJSON do - @moduledoc false - - alias Octocon.Fronts.Front - - def index_me(%{fronts: fronts}) do - %{data: Enum.map(fronts, &data_me/1)} - end - - def index(%{fronts: fronts}) do - %{data: Enum.map(fronts, &data/1)} - end - - def show_me(%{front: front}) do - %{data: data_me(front)} - end - - def data_me(%Front{} = front) do - front - |> Map.from_struct() - |> Map.drop([:__meta__, :alter, :user]) - end - - def data_me(%{alter: alter, front: front, primary: primary}) do - %{ - front: - front - |> Map.from_struct() - |> Map.drop([:__meta__, :alter, :user]), - alter: - alter - |> Map.from_struct() - |> Map.take([:id, :name, :pronouns, :color, :avatar_url]), - primary: primary - } - end - - def data(%{alter: alter, front: front, primary: primary}) do - %{ - front: - front - |> Map.take([:alter_id, :comment]), - alter: - alter - |> Map.take([:id, :name, :pronouns, :color, :avatar_url]), - primary: primary - } - end -end diff --git a/lib/octocon_web/controllers/system/tag_controller.ex b/lib/octocon_web/controllers/system/tag_controller.ex deleted file mode 100644 index 07aa14b3..00000000 --- a/lib/octocon_web/controllers/system/tag_controller.ex +++ /dev/null @@ -1,302 +0,0 @@ -defmodule OctoconWeb.System.TagController do - use OctoconWeb, :controller - - import OctoconWeb.Utils.Systems - - alias Octocon.Tags - - def index(conn, %{"system_id" => system_id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, system_id) do - {:noreply, conn} -> - conn - - {:self, system} -> - render(conn, :index_me, tags: Tags.get_tags({:system, system.id})) - - {:other, system} -> - render(conn, :index, - tags: Tags.get_tags_guarded({:system, system.id}, {:system, caller_id}) - ) - end - end - - def show(conn, %{"system_id" => system_id, "id" => tag_id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, system_id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - case Tags.get_tag({:system, caller_id}, tag_id) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag not found.", code: "tag_not_found"}) - - tag -> - render(conn, :show_me, tag: tag) - end - - {:other, system} -> - case Tags.get_tag_guarded({:system, system.id}, tag_id, {:system, caller_id}) do - nil -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag not found.", code: "tag_not_found"}) - - tag -> - render(conn, :show, tag: tag) - end - end - end - - def create(conn, %{"name" => name, "parent_tag_id" => parent_tag_id}) do - system_id = conn.private[:guardian_default_resource] - - case Tags.create_tag({:system, system_id}, name, parent_tag_id) do - {:error, :not_found} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid tag attributes.", - code: "invalid_tag_attributes" - }) - - {:ok, tag} -> - conn - |> put_status(:created) - |> render(:show, tag: tag) - end - end - - def create(conn, %{"name" => name}) do - system_id = conn.private[:guardian_default_resource] - - case Tags.create_tag({:system, system_id}, name) do - {:error, :not_found} -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid tag attributes.", - code: "invalid_tag_attributes" - }) - - {:ok, tag} -> - conn - |> put_status(:created) - |> render(:show, tag: tag) - end - end - - def delete(conn, %{"id" => tag_id}) do - system_id = conn.private[:guardian_default_resource] - - case Tags.delete_tag({:system, system_id}, tag_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag not found.", code: "tag_not_found"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def update(conn, %{"id" => tag_id} = attrs) do - system_id = conn.private[:guardian_default_resource] - - attrs = - Map.take(attrs, [ - "name", - "color", - "description", - "security_level" - ]) - |> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end) - |> Map.new() - - if map_size(attrs) == 0 do - conn - |> put_status(:bad_request) - |> json(%{ - error: "No valid tag attributes provided.", - code: "no_tag_attributes" - }) - else - case Tags.update_tag({:system, system_id}, tag_id, attrs) do - {:ok, _tag} -> - send_resp(conn, :no_content, "") - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag not found.", code: "tag_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{ - error: "Invalid tag attributes.", - code: "invalid_tag_attributes" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - end - - def attach_alter(conn, %{"id" => tag_id, "alter_id" => alter_id}) - when is_integer(alter_id) do - system_id = conn.private[:guardian_default_resource] - - case Tags.attach_alter_to_tag({:system, system_id}, tag_id, {:id, alter_id}) do - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :alter_not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def attach_alter(conn, %{"id" => tag_id, "alter_id" => alter_id}) - when is_binary(alter_id) do - case parse_integer(alter_id) do - {:ok, parsed_id} -> - attach_alter(conn, %{"id" => tag_id, "alter_id" => parsed_id}) - - :error -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - end - end - - def detach_alter(conn, %{"id" => tag_id, "alter_id" => alter_id}) - when is_integer(alter_id) do - system_id = conn.private[:guardian_default_resource] - - case Tags.detach_alter_from_tag( - {:system, system_id}, - tag_id, - {:id, alter_id} - ) do - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - - {:error, :alter_not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Alter not found.", code: "alter_not_found"}) - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{ - error: "Tag not found or that alter is not attached.", - code: "tag_or_alter_not_found" - }) - - _ -> - conn - |> put_status(:internal_server_error) - |> json(%{error: "An unknown error occurred.", code: "unknown_error"}) - end - end - - def detach_alter(conn, %{"id" => tag_id, "alter_id" => alter_id}) - when is_binary(alter_id) do - case parse_integer(alter_id) do - {:ok, parsed_id} -> - detach_alter(conn, %{"id" => tag_id, "alter_id" => parsed_id}) - - :error -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid alter ID.", code: "invalid_alter_id"}) - end - end - - def set_parent(conn, %{"id" => tag_id, "parent_tag_id" => parent_tag_id}) do - system_id = conn.private[:guardian_default_resource] - - case Tags.set_parent_tag({:system, system_id}, tag_id, parent_tag_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag or parent tag not found.", code: "tag_or_parent_not_found"}) - - {:error, :tag_cycle} -> - conn - |> put_status(:bad_request) - |> json(%{error: "You cannot create a cycle with parent tags.", code: "tag_cycle"}) - - {:error, :changeset} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Invalid parent tag ID.", code: "invalid_parent_tag_id"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def remove_parent(conn, %{"id" => tag_id}) do - system_id = conn.private[:guardian_default_resource] - - case Tags.remove_parent_tag({:system, system_id}, tag_id) do - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "Tag not found.", code: "tag_not_found"}) - - :ok -> - conn - |> put_status(:no_content) - |> send_resp(:no_content, "") - end - end - - def parse_integer(value) do - case Integer.parse(value) do - :error -> :error - {id, _} when id < 0 or id > 32_767 -> :error - {id, _} -> {:ok, id} - end - end -end diff --git a/lib/octocon_web/controllers/system/tag_json.ex b/lib/octocon_web/controllers/system/tag_json.ex deleted file mode 100644 index 4e65f5f3..00000000 --- a/lib/octocon_web/controllers/system/tag_json.ex +++ /dev/null @@ -1,44 +0,0 @@ -defmodule OctoconWeb.System.TagJSON do - def index_me(%{tags: tags}) do - %{data: Enum.map(tags, &data_me/1)} - end - - def index(%{tags: tags}) do - %{data: Enum.map(tags, &data/1)} - end - - def show_me(%{tag: tag}) do - %{data: data_me(tag)} - end - - def show(%{tag: tag}) do - %{data: data(tag)} - end - - def data_me(tag) do - tag - |> Map.from_struct() - |> Map.drop([:__meta__, :user, :parent_tag]) - end - - def data(tag) when tag.alters == [] or tag.alters == nil do - tag - |> Map.from_struct() - |> Map.take([:id, :name, :alters, :description, :user_id, :color, :parent_tag_id]) - |> Map.update!(:alters, fn _ -> [] end) - end - - def data(tag) do - tag - |> Map.from_struct() - |> Map.take([:id, :name, :alters, :description, :user_id, :color, :parent_tag_id]) - |> Map.update!( - :alters, - fn alters -> - alters - |> Enum.map(&Map.from_struct/1) - |> Enum.map(&Map.take(&1, [:id, :name, :color, :avatar_url, :pronouns])) - end - ) - end -end diff --git a/lib/octocon_web/controllers/system_controller.ex b/lib/octocon_web/controllers/system_controller.ex deleted file mode 100644 index 7e491a84..00000000 --- a/lib/octocon_web/controllers/system_controller.ex +++ /dev/null @@ -1,78 +0,0 @@ -defmodule OctoconWeb.SystemController do - use OctoconWeb, :controller - - import OctoconWeb.Utils.Systems - - alias Octocon.{ - Alters, - Friendships, - Tags - } - - alias OctoconWeb.FriendJSON - - alias OctoconWeb.System.{ - AlterJSON, - TagJSON - } - - action_fallback OctoconWeb.FallbackController - - def show(conn, %{"system_id" => id}) do - case parse_system(conn, id) do - {:noreply, conn} -> conn - {:self, system} -> render(conn, :show_me, system: system) - {:other, system} -> render(conn, :show, system: system) - end - end - - def batch(conn, %{"system_id" => id}) do - caller_id = conn.private[:guardian_default_resource] - - case parse_system(conn, id) do - {:noreply, conn} -> - conn - - {:self, _system} -> - conn - |> put_status(:forbidden) - |> json(%{ - error: "You cannot view your own system through this endpoint.", - code: "invalid_endpoint" - }) - - {:other, system} -> - friendship = - Task.async(fn -> - Friendships.get_friendship_guarded({:system, caller_id}, {:system, system.id}) - end) - - alters = - Task.async(fn -> - Alters.get_alters_guarded({:system, system.id}, {:system, caller_id}) - end) - - tags = - Task.async(fn -> Tags.get_tags_guarded({:system, system.id}, {:system, caller_id}) end) - - [friendship, tags, alters] - |> Task.await_many() - |> then(fn [friendship, tags, alters] -> - [ - FriendJSON.data(friendship), - Enum.map(tags, &TagJSON.data/1), - Enum.map(alters, &AlterJSON.data/1) - ] - end) - |> then(fn [friendship, tags, alters] -> - conn - |> render( - :batch, - friendship: friendship, - tags: tags, - alters: alters - ) - end) - end - end -end diff --git a/lib/octocon_web/controllers/system_json.ex b/lib/octocon_web/controllers/system_json.ex deleted file mode 100644 index 392c9395..00000000 --- a/lib/octocon_web/controllers/system_json.ex +++ /dev/null @@ -1,89 +0,0 @@ -defmodule OctoconWeb.SystemJSON do - def show_me(%{system: system}) do - %{data: data_me(system)} - end - - def show(%{system: system}) do - %{data: data(system)} - end - - def data_me(system) do - map = - system - |> Map.from_struct() - |> Map.drop([ - :__meta__, - :alters, - :fronts, - :global_journals, - :inserted_at, - :updated_at, - :salt, - :discord_settings - ]) - # [TODO]: Remove when app is updated to not expect these - |> Map.merge(%{ - autoproxy_mode: "off", - show_system_tag: false - }) - - fields = - if map.fields != nil do - map.fields - |> Enum.map(fn field -> - if field.locked == nil do - Map.put(field, :locked, false) - else - field - end - end) - |> Enum.map(&Map.drop(&1, [:__struct__, :__meta__])) - else - [] - end - - %{map | fields: fields} - end - - def batch(%{friendship: friendship, tags: tags, alters: alters}) do - %{ - data: %{ - friendship: friendship, - tags: tags, - alters: alters - } - } - end - - defp data(nil), do: nil - - defp data(%{ - id: id, - avatar_url: avatar_url, - username: username, - description: description, - friend_status: friend_status - }) do - %{ - id: id, - avatar_url: avatar_url, - username: username, - description: description, - friend_status: friend_status - } - end - - defp data(%{ - id: id, - avatar_url: avatar_url, - username: username, - description: description - }) do - %{ - id: id, - avatar_url: avatar_url, - username: username, - description: description - } - end -end diff --git a/lib/octocon_web/dummy_conn_adapter.ex b/lib/octocon_web/dummy_conn_adapter.ex deleted file mode 100644 index 73008c40..00000000 --- a/lib/octocon_web/dummy_conn_adapter.ex +++ /dev/null @@ -1,195 +0,0 @@ -defmodule OctoconWeb.DummyConnAdapter do - @behaviour Plug.Conn.Adapter - @moduledoc false - - def conn(%Plug.Conn{} = conn, method, uri, body_or_params) do - maybe_flush() - uri = URI.parse(uri) - - method = method |> to_string() |> String.upcase() - query = uri.query || "" - owner = self() - - {params, {body, body_params}, {query, query_params}, req_headers} = - body_or_params(body_or_params, query, conn.req_headers, method) - - state = %{ - method: method, - params: params, - req_body: body, - chunks: nil, - http_protocol: get_from_adapter(conn, :get_http_protocol, :"HTTP/1.1"), - peer_data: - get_from_adapter(conn, :get_peer_data, %{ - address: {127, 0, 0, 1}, - port: 111_317, - ssl_cert: nil - }) - } - - conn_port = if conn.port != 0, do: conn.port, else: 80 - - %Plug.Conn{ - conn - | adapter: {__MODULE__, state}, - host: uri.host || conn.host || "www.octocon.app", - method: method, - owner: owner, - path_info: split_path(uri.path), - port: uri.port || conn_port, - remote_ip: conn.remote_ip || {127, 0, 0, 1}, - req_headers: req_headers, - request_path: uri.path, - query_string: query, - query_params: query_params || %Plug.Conn.Unfetched{aspect: :query_params}, - body_params: body_params || %Plug.Conn.Unfetched{aspect: :body_params}, - params: params || %Plug.Conn.Unfetched{aspect: :params}, - # credo:disable-for-next-line - scheme: (uri.scheme || "http") |> String.downcase() |> String.to_atom() - } - end - - ## Connection adapter - - def send_resp(%{method: "HEAD"} = state, _status, _headers, _body) do - {:ok, "", state} - end - - def send_resp(state, _status, _headers, body) do - {:ok, IO.iodata_to_binary(body), state} - end - - def send_file(%{method: "HEAD"} = state, _status, _headers, _path, _offset, _length) do - {:ok, "", state} - end - - def send_file(state, _status, _headers, _path, _offset, _length) do - {:ok, "", state} - end - - def send_chunked(state, _status, _headers), do: {:ok, "", %{state | chunks: ""}} - - def chunk(%{method: "HEAD"} = state, _body), do: {:ok, "", state} - - def chunk(%{chunks: chunks} = state, body) do - body = chunks <> IO.iodata_to_binary(body) - {:ok, body, %{state | chunks: body}} - end - - def read_req_body(%{req_body: body} = state, opts \\ []) do - size = min(byte_size(body), Keyword.get(opts, :length, 8_000_000)) - data = :binary.part(body, 0, size) - rest = :binary.part(body, size, byte_size(body) - size) - - tag = - case rest do - "" -> :ok - _ -> :more - end - - {tag, data, %{state | req_body: rest}} - end - - def inform(_state, _status, _headers) do - :ok - end - - def upgrade(_state, :not_supported, _opts) do - {:error, :not_supported} - end - - def upgrade(state, _protocol, _opts) do - {:ok, state} - end - - def push(_state, _path, _headers) do - :ok - end - - def get_peer_data(payload) do - Map.fetch!(payload, :peer_data) - end - - def get_http_protocol(payload) do - Map.fetch!(payload, :http_protocol) - end - - ## Private helpers - - defp get_from_adapter(conn, op, default) do - case conn.adapter do - {Plug.MissingAdapter, _} -> default - {adapter, payload} -> apply(adapter, op, [payload]) - end - end - - defp body_or_params(nil, query, headers, _method), do: {nil, {"", nil}, {query, nil}, headers} - - defp body_or_params(body, query, headers, _method) when is_binary(body) do - {nil, {body, nil}, {query, nil}, headers} - end - - defp body_or_params(params, query, headers, method) when is_list(params) do - body_or_params(Enum.into(params, %{}), query, headers, method) - end - - defp body_or_params(params, query, headers, method) - when is_map(params) and method in ["GET", "HEAD"] do - params = stringify_params(params, &to_string/1) - - from_query = Plug.Conn.Query.decode(query) - params = Map.merge(from_query, params) - - query = - params - |> Map.merge(from_query) - |> Plug.Conn.Query.encode() - - {params, {"", nil}, {query, params}, headers} - end - - defp body_or_params(params, query, headers, _method) when is_map(params) do - content_type_header = {"content-type", "multipart/mixed; boundary=plug_conn_test"} - content_type = List.keyfind(headers, "content-type", 0, content_type_header) - headers = List.keystore(headers, "content-type", 0, content_type) - - body_params = stringify_params(params, & &1) - query_params = Plug.Conn.Query.decode(query) - params = Map.merge(query_params, body_params) - - {params, {"--dummy_conn--", body_params}, {query, query_params}, headers} - end - - defp stringify_params([{_, _} | _] = params, value_fun), - do: Enum.into(params, %{}, &stringify_kv(&1, value_fun)) - - defp stringify_params([_ | _] = params, value_fun), - do: Enum.map(params, &stringify_params(&1, value_fun)) - - defp stringify_params(%{__struct__: mod} = struct, _value_fun) when is_atom(mod), do: struct - defp stringify_params(fun, _value_fun) when is_function(fun), do: fun - - defp stringify_params(%{} = params, value_fun), - do: Enum.into(params, %{}, &stringify_kv(&1, value_fun)) - - defp stringify_params(other, value_fun), do: value_fun.(other) - - defp stringify_kv({k, v}, value_fun), do: {to_string(k), stringify_params(v, value_fun)} - - defp split_path(nil), do: [] - - defp split_path(path) do - segments = :binary.split(path, "/", [:global]) - for segment <- segments, segment != "", do: segment - end - - @already_sent {:plug_conn, :sent} - - defp maybe_flush do - receive do - @already_sent -> :ok - after - 0 -> :ok - end - end -end diff --git a/lib/octocon_web/endpoint.ex b/lib/octocon_web/endpoint.ex deleted file mode 100644 index f7fcefa4..00000000 --- a/lib/octocon_web/endpoint.ex +++ /dev/null @@ -1,60 +0,0 @@ -defmodule OctoconWeb.Endpoint do - use Sentry.PlugCapture - use Phoenix.Endpoint, otp_app: :octocon - - # The session will be stored in the cookie and signed, - # this means its contents can be read but not tampered with. - # Set :encryption_salt if you would also like to encrypt it. - @session_options [ - store: :cookie, - key: "_octocon_key", - signing_salt: "L9uWUqyf", - same_site: "Lax" - ] - - # Serve at "/" the static files from "priv/static" directory. - # - # You should set gzip to true if you are running phx.digest - # when deploying your static files in production. - plug Plug.Static, - at: "/", - from: :octocon, - gzip: false, - only: OctoconWeb.static_paths() - - # Code reloading can be explicitly enabled under the - # :code_reloader configuration of your endpoint. - if code_reloading? do - plug Phoenix.CodeReloader - plug Phoenix.Ecto.CheckRepoStatus, otp_app: :octocon - end - - socket "/live", Phoenix.LiveView.Socket - - socket "/api/socket", OctoconWeb.UserSocket, - websocket: true, - longpoll: false - - plug Phoenix.LiveDashboard.RequestLogger, - param_key: "request_logger", - cookie_key: "request_logger" - - plug Plug.RequestId - plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] - - plug Plug.Parsers, - parsers: [ - :urlencoded, - {:multipart, length: 20_000_000}, - :json - ], - pass: ["*/*"], - json_decoder: Phoenix.json_library() - - plug Sentry.PlugContext - - plug Plug.MethodOverride - plug Plug.Head - plug Plug.Session, @session_options - plug OctoconWeb.Router -end diff --git a/lib/octocon_web/metrics_plug.ex b/lib/octocon_web/metrics_plug.ex deleted file mode 100644 index 6835ed62..00000000 --- a/lib/octocon_web/metrics_plug.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule OctoconWeb.MetricsPlug do - @moduledoc false - use Plug.Builder - - plug PromEx.Plug, path: "/", prom_ex_module: Octocon.PromEx - - plug :not_found - - defp not_found(conn, _opts) do - conn - |> send_resp(404, "Not found") - end -end diff --git a/lib/octocon_web/router.ex b/lib/octocon_web/router.ex deleted file mode 100644 index c04ea07d..00000000 --- a/lib/octocon_web/router.ex +++ /dev/null @@ -1,268 +0,0 @@ -defmodule OctoconWeb.Router do - use OctoconWeb, :router - - import Phoenix.LiveDashboard.Router - - alias OctoconWeb.AuthPipeline - - pipeline :browser do - plug :accepts, ["html"] - plug :fetch_session - plug :fetch_flash - plug :protect_from_forgery - plug :put_secure_browser_headers - end - - pipeline :nocsrf_browser do - plug :accepts, ["html"] - plug :fetch_session - plug :fetch_flash - plug :put_secure_browser_headers - end - - pipeline :api do - plug :accepts, ["json"] - - # plug Hammer.Plug, [ - # # Allow 10 requests per second - # rate_limit: {"api_requests", :timer.seconds(1), 50}, - # by: :ip - # ] - end - - pipeline :admins_only do - plug :admin_basic_auth - end - - pipeline :ensure_authenticated do - plug Guardian.Plug.EnsureAuthenticated - end - - # Requires authentication - scope "/api", OctoconWeb do - pipe_through [:api, AuthPipeline, :ensure_authenticated] - - scope "/systems/me" do - scope "/alters" do - post "/", System.AlterController, :create - - scope "/journals/:journal_id" do - get "/", AlterJournalController, :show - patch "/", AlterJournalController, :update - delete "/", AlterJournalController, :delete - - post "/lock", AlterJournalController, :lock - post "/unlock", AlterJournalController, :unlock - - post "/pin", AlterJournalController, :pin - post "/unpin", AlterJournalController, :unpin - end - - scope "/:id" do - patch "/", System.AlterController, :update - delete "/", System.AlterController, :delete - - put "/avatar", System.AlterController, :upload_avatar - delete "/avatar", System.AlterController, :delete_avatar - - scope "/journals" do - get "/", AlterJournalController, :index - post "/", AlterJournalController, :create - end - end - end - - scope "/tags" do - post "/", System.TagController, :create - - scope "/:id" do - patch "/", System.TagController, :update - delete "/", System.TagController, :delete - - scope "/alter" do - post "/", System.TagController, :attach_alter - delete "/", System.TagController, :detach_alter - end - - scope "/parent" do - post "/", System.TagController, :set_parent - delete "/", System.TagController, :remove_parent - end - end - end - - scope "/front" do - post "/", System.FrontingController, :update - post "/start", System.FrontingController, :start - post "/end", System.FrontingController, :endd - post "/set", System.FrontingController, :set - post "/primary", System.FrontingController, :primary - - get "/month", System.FrontingController, :month - get "/between", System.FrontingController, :between - - scope "/:id" do - get "/", System.FrontingController, :show - delete "/", System.FrontingController, :delete - post "/comment", System.FrontingController, :update_comment - end - end - end - - scope "/friends" do - get "/", FriendController, :index - - scope "/:id" do - get "/", FriendController, :show - delete "/", FriendController, :delete - - post "/trust", FriendController, :trust - post "/untrust", FriendController, :untrust - end - end - - scope "/friend-requests" do - get "/", FriendRequestController, :index - - scope "/:id" do - put "/", FriendRequestController, :send - delete "/", FriendRequestController, :cancel - - post "/accept", FriendRequestController, :accept - post "/reject", FriendRequestController, :reject - end - end - - scope "/journals" do - get "/", GlobalJournalController, :index - post "/", GlobalJournalController, :create - - scope "/:id" do - get "/", GlobalJournalController, :show - patch "/", GlobalJournalController, :update - delete "/", GlobalJournalController, :delete - - post "/lock", GlobalJournalController, :lock - post "/unlock", GlobalJournalController, :unlock - - post "/pin", GlobalJournalController, :pin - post "/unpin", GlobalJournalController, :unpin - - scope "/alter" do - post "/", GlobalJournalController, :attach_alter - delete "/", GlobalJournalController, :detach_alter - end - end - end - - scope "/polls" do - get "/", PollController, :index - post "/", PollController, :create - - scope "/:id" do - get "/", PollController, :show - patch "/", PollController, :update - delete "/", PollController, :delete - end - end - - scope "/settings" do - get "/link_token", AuthLinkTokenController, :get - - post "/setup-encryption", SettingsController, :setup_encryption - post "/recover-encryption", SettingsController, :recover_encryption - post "/reset-encryption", SettingsController, :reset_encryption - - post "/username", SettingsController, :update_username - put "/avatar", SettingsController, :upload_avatar - delete "/avatar", SettingsController, :delete_avatar - - post "/push-token", SettingsController, :add_push_token - delete "/push-token", SettingsController, :invalidate_push_token - - post "/import-pk", SettingsController, :import_pk - post "/import-sp", SettingsController, :import_sp - - post "/unlink_discord", SettingsController, :unlink_discord - post "/unlink_email", SettingsController, :unlink_email - post "/unlink_apple", SettingsController, :unlink_apple - - post "/description", SettingsController, :update_description - - post "/delete-account", SettingsController, :delete_account - post "/wipe-alters", SettingsController, :wipe_alters - - scope "/fields" do - scope "/:id" do - patch "/", SettingsController, :edit_custom_field - delete "/", SettingsController, :remove_custom_field - post "/relocate", SettingsController, :relocate_custom_field - end - - post "/", SettingsController, :create_custom_field - end - end - end - - # Does not require authentication, but still has access to the Guardian resource - scope "/api", OctoconWeb do - pipe_through [:api, AuthPipeline] - - get "/heartbeat", HeartbeatController, :index - - scope "/systems/:system_id" do - get "/", SystemController, :show - - scope "/alters" do - get "/", System.AlterController, :index - get "/:id", System.AlterController, :show - end - - scope "/tags" do - get "/", System.TagController, :index - get "/:id", System.TagController, :show - end - - get "/fronting", System.FrontingController, :index - get "/batch", SystemController, :batch - end - end - - scope "/auth", OctoconWeb do - pipe_through [:browser] - - get "/:provider", AuthController, :request - get "/:provider/callback", AuthController, :callback - end - - scope "/auth", OctoconWeb do - pipe_through [:nocsrf_browser] - - post "/:provider/callback", AuthController, :callback - end - - scope "/auth/link", OctoconWeb do - pipe_through [:browser] - - get "/:provider", AuthLinkController, :request - get "/:provider/callback", AuthLinkController, :callback - end - - scope "/auth/link", OctoconWeb do - pipe_through [:nocsrf_browser] - - post "/:provider/callback", AuthLinkController, :callback - end - - scope "/admin" do - pipe_through [:browser, :admins_only] - - live_dashboard "/dashboard", metrics: OctoconWeb.Telemetry - end - - defp admin_basic_auth(conn, _opts) do - username = System.fetch_env!("ADMIN_USERNAME") - password = System.fetch_env!("ADMIN_PASSWORD") - Plug.BasicAuth.basic_auth(conn, username: username, password: password) - end -end diff --git a/lib/octocon_web/telemetry.ex b/lib/octocon_web/telemetry.ex deleted file mode 100644 index ae65db18..00000000 --- a/lib/octocon_web/telemetry.ex +++ /dev/null @@ -1,92 +0,0 @@ -defmodule OctoconWeb.Telemetry do - use Supervisor - import Telemetry.Metrics - - def start_link(arg) do - Supervisor.start_link(__MODULE__, arg, name: __MODULE__) - end - - @impl Supervisor - def init(_arg) do - children = [ - # Telemetry poller will execute the given period measurements - # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics - {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} - # Add reporters as children of your supervision tree. - # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} - ] - - Supervisor.init(children, strategy: :one_for_one) - end - - def metrics do - [ - # Phoenix Metrics - summary("phoenix.endpoint.start.system_time", - unit: {:native, :millisecond} - ), - summary("phoenix.endpoint.stop.duration", - unit: {:native, :millisecond} - ), - summary("phoenix.router_dispatch.start.system_time", - tags: [:route], - unit: {:native, :millisecond} - ), - summary("phoenix.router_dispatch.exception.duration", - tags: [:route], - unit: {:native, :millisecond} - ), - summary("phoenix.router_dispatch.stop.duration", - tags: [:route], - unit: {:native, :millisecond} - ), - summary("phoenix.socket_connected.duration", - unit: {:native, :millisecond} - ), - summary("phoenix.channel_joined.duration", - unit: {:native, :millisecond} - ), - summary("phoenix.channel_handled_in.duration", - tags: [:event], - unit: {:native, :millisecond} - ), - - # Database Metrics - summary("octocon.repo.query.total_time", - unit: {:native, :millisecond}, - description: "The sum of the other measurements" - ), - summary("octocon.repo.query.decode_time", - unit: {:native, :millisecond}, - description: "The time spent decoding the data received from the database" - ), - summary("octocon.repo.query.query_time", - unit: {:native, :millisecond}, - description: "The time spent executing the query" - ), - summary("octocon.repo.query.queue_time", - unit: {:native, :millisecond}, - description: "The time spent waiting for a database connection" - ), - summary("octocon.repo.query.idle_time", - unit: {:native, :millisecond}, - description: - "The time the connection spent waiting before being checked out for the query" - ), - - # VM Metrics - summary("vm.memory.total", unit: {:byte, :kilobyte}), - summary("vm.total_run_queue_lengths.total"), - summary("vm.total_run_queue_lengths.cpu"), - summary("vm.total_run_queue_lengths.io") - ] - end - - defp periodic_measurements do - [ - # A module, function and arguments to be invoked periodically. - # This function must call :telemetry.execute/3 and a metric must be added above. - # {OctoconWeb, :count_users, []} - ] - end -end diff --git a/lib/octocon_web/uploaders/avatar.ex b/lib/octocon_web/uploaders/avatar.ex deleted file mode 100644 index c4aa3337..00000000 --- a/lib/octocon_web/uploaders/avatar.ex +++ /dev/null @@ -1,58 +0,0 @@ -defmodule OctoconWeb.Uploaders.Avatar do - @moduledoc false - use Waffle.Definition - - # Include ecto support (requires package waffle_ecto installed): - # use Waffle.Ecto.Definition - - @versions [:primary] - - # To add a thumbnail version: - # @versions [:original, :thumb] - - def acl(:primary, _), do: :public_read - - def validate({file, _}) do - file_extension = file.file_name |> Path.extname() |> String.downcase() - - if file_extension == "" or - Enum.member?(~w(.jpg .jpeg .png .webp .heif .heic .tiff), file_extension) do - :ok - else - {:error, "Invalid file type"} - end - end - - def transform(:primary, _) do - &process/2 - end - - def process(:primary, %Waffle.File{} = file) do - with {:ok, original_raw} <- File.read(file.path), - {:ok, original} <- Image.from_binary(original_raw), - {:ok, thumbnail} <- Image.thumbnail(original, "500x500", fit: :cover), - new_path <- Waffle.File.generate_temporary_path(".webp"), - {:ok, _} <- Image.write(thumbnail, new_path, quality: 90) do - {:ok, %Waffle.File{file | path: new_path, is_tempfile?: true, file_name: "primary.webp"}} - else - _ -> {:error, "Error creating thumbnail"} - end - end - - # Override the persisted filenames: - def filename(version, {_file, scope}) do - "#{version}-#{scope.random_id}" - end - - # Override the storage directory: - def storage_dir(_version, {_file, scope}) do - "uploads/avatars/#{scope.system_id}/#{scope.alter_id}" - end - - def s3_object_headers(_version, _data) do - [ - content_type: "image/webp", - cache_control: "public, max-age=31536000, immutable" - ] - end -end diff --git a/lib/octocon_web/uploaders/user_avatar.ex b/lib/octocon_web/uploaders/user_avatar.ex deleted file mode 100644 index 341cad09..00000000 --- a/lib/octocon_web/uploaders/user_avatar.ex +++ /dev/null @@ -1,58 +0,0 @@ -defmodule OctoconWeb.Uploaders.UserAvatar do - @moduledoc false - use Waffle.Definition - - # Include ecto support (requires package waffle_ecto installed): - # use Waffle.Ecto.Definition - - @versions [:primary] - - # To add a thumbnail version: - # @versions [:original, :thumb] - - def acl(:primary, _), do: :public_read - - def validate({file, _}) do - file_extension = file.file_name |> Path.extname() |> String.downcase() - - if file_extension == "" or - Enum.member?(~w(.jpg .jpeg .png .webp .heif .heic .tiff), file_extension) do - :ok - else - {:error, "Invalid file type"} - end - end - - def transform(:primary, _) do - &process/2 - end - - def process(:primary, %Waffle.File{} = file) do - with {:ok, original_raw} <- File.read(file.path), - {:ok, original} <- Image.from_binary(original_raw), - {:ok, thumbnail} <- Image.thumbnail(original, "500x500", fit: :cover), - new_path <- Waffle.File.generate_temporary_path(".webp"), - {:ok, _} <- Image.write(thumbnail, new_path, quality: 90) do - {:ok, %Waffle.File{file | path: new_path, is_tempfile?: true, file_name: "primary.webp"}} - else - _ -> {:error, "Error creating thumbnail"} - end - end - - # Override the persisted filenames: - def filename(version, {_file, scope}) do - "#{version}-#{scope.random_id}" - end - - # Override the storage directory: - def storage_dir(_version, {_file, scope}) do - "uploads/avatars/#{scope.system_id}/self" - end - - def s3_object_headers(_version, _data) do - [ - content_type: "image/webp", - cache_control: "public, max-age=31536000, immutable" - ] - end -end diff --git a/lib/octocon_web/user_socket.ex b/lib/octocon_web/user_socket.ex deleted file mode 100644 index 0aac06f7..00000000 --- a/lib/octocon_web/user_socket.ex +++ /dev/null @@ -1,241 +0,0 @@ -defmodule OctoconWeb.UserSocket do - use Phoenix.Socket - - channel "system:*", OctoconWeb.UserChannel - - def connect(%{"token" => token}, socket, _connect_info) do - case Octocon.Auth.Guardian.resource_from_token(token) do - {:ok, system_id, _claims} -> - { - :ok, - socket - |> assign(:system_id, system_id) - |> assign(:token, token) - } - - {:error, _reason} -> - :error - - _ -> - :error - end - end - - def id(socket), do: "system_socket:#{socket.assigns.system_id}" -end - -defmodule OctoconWeb.UserChannel do - use Phoenix.Channel - - @base_version Version.parse("1.0.0") - @batched_init_version Version.parse("2.0.0") - - require Logger - - alias Octocon.{ - Accounts, - Alters, - Fronts, - Tags - } - - alias OctoconWeb.System.{ - AlterJSON, - FrontingJSON - } - - alias OctoconWeb.System.TagJSON - alias OctoconWeb.SystemJSON - - @impl Phoenix.Channel - def join("system:" <> system_id, %{"token" => token} = params, socket) do - # Only allow 2 socket joins per second to avoid abuse (especially from misconfigured Phoenix clients) - - case Octocon.Auth.Guardian.resource_from_token(token) do - {:ok, claim_id, _claims} when claim_id == system_id -> - case Hammer.check_rate("socket:#{system_id}", :timer.seconds(1), 2) do - {:allow, _count} -> - is_reconnect = Map.get(params, "isReconnect", false) - - protocol_version = - case Map.get(params, "protocolVersion") do - nil -> {:ok, @base_version} - version_str -> Version.parse(version_str) - end - - platform = Map.get(params, "platform", "unknown") - force_batch = Map.get(params, "forceBatch", false) - - case protocol_version do - {:ok, version} -> - if is_reconnect do - {:ok, socket} - else - init_data = generate_init_data(system_id) - - # What the fuck - exceeds_ios_limit = :erlang.external_size(init_data) * 1.1 > 1_048_576 - - if force_batch || - (platform == "ios" && exceeds_ios_limit && version >= @batched_init_version) do - send(self(), {:send_batched_init, init_data}) - - response = %{ - "batched" => true, - "system" => init_data["system"], - "alters" => nil, - "fronts" => nil, - "tags" => nil - } - - {:ok, response, socket} - else - Process.send_after(socket.transport_pid, :garbage_collect, :timer.seconds(1)) - {:ok, init_data, socket} - end - end - - _ -> - {:error, %{reason: "unsupported_protocol_version"}} - end - - {:deny, _limit} -> - {:error, %{reason: "rate_limited"}} - end - - _ -> - {:error, %{reason: "unauthorized"}} - end - rescue - e -> - Logger.error("Error joining user socket:") - Logger.error(Exception.format(:error, e, __STACKTRACE__)) - {:error, %{reason: "internal_error"}} - end - - defp generate_init_data(system_id) do - identity = {:system, system_id} - - system = - Accounts.get_user!(identity) - |> SystemJSON.data_me() - - alters = - Alters.get_alters_by_id( - identity, - Alters.bare_fields() ++ [:untracked, :archived, :last_fronted] - ) - |> Enum.map(&AlterJSON.data_me/1) - - fronts = - Fronts.currently_fronting(identity) - |> Enum.map(&FrontingJSON.data_me/1) - - tags = - Tags.get_tags(identity) - |> Enum.map(&TagJSON.data_me/1) - - %{ - "system" => system, - "alters" => alters, - "fronts" => fronts, - "tags" => tags - } - end - - defp send_batched_init(socket, %{"alters" => alters, "tags" => tags, "fronts" => fronts}) do - send_batched_data( - "batched_init_alters", - "alters", - 3_000, - alters, - socket - ) - - send_batched_data( - "batched_init_tags", - "tags", - 1_000, - tags, - socket - ) - - send_batched_data( - "batched_init_fronts", - "fronts", - 50, - fronts, - socket - ) - - Process.send_after(socket.transport_pid, :garbage_collect, :timer.seconds(1)) - - push(socket, "batched_init_complete", %{}) - end - - defp send_batched_data(event_name, data_name, batch_size, data, socket) do - batched_data = Enum.chunk_every(data, batch_size) - total_batches = length(batched_data) - - Enum.with_index(batched_data) - |> Enum.each(fn {data_batch, index} -> - Process.sleep(50) - - push( - socket, - event_name, - %{ - "batch_index" => index + 1, - "total_batches" => total_batches - } - |> Map.put(data_name, data_batch) - ) - end) - end - - @impl Phoenix.Channel - def handle_in( - "endpoint", - %{ - "method" => method, - "path" => path, - "body" => body - }, - socket - ) do - response = - create_mock_conn(method, path, body, socket) - |> Plug.Conn.put_req_header("authorization", "Bearer #{socket.assigns.token}") - |> Plug.Conn.put_req_header("content-type", "application/json") - |> Plug.Conn.put_req_header("accept", "application/json") - |> OctoconWeb.Endpoint.call(nil) - |> encode_response() - - {:reply, {:ok, response}, socket} - end - - @doc false - @impl Phoenix.Channel - def handle_info({:plug_conn, :sent}, socket), do: {:noreply, socket} - - @impl Phoenix.Channel - def handle_info({:send_batched_init, init_data}, socket) do - if init_data do - send_batched_init(socket, init_data) - end - - {:noreply, socket} - end - - defp encode_response(conn) do - %{ - "status" => conn.status, - # "headers" => conn.resp_headers, - "body" => conn.resp_body - } - end - - defp create_mock_conn(method, path, body, _socket) do - OctoconWeb.DummyConnAdapter.conn(%Plug.Conn{}, method, path, body) - end -end diff --git a/lib/octocon_web/utils/alters.ex b/lib/octocon_web/utils/alters.ex deleted file mode 100644 index c9a27222..00000000 --- a/lib/octocon_web/utils/alters.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule OctoconWeb.Utils.Alters do - @moduledoc false - - # import Phoenix.Controller, only: [json: 2] - # import Plug.Conn - - # alias Octocon.Alters - # alias Octocon.Alters.Alter - # alias Octocon.Friendships - # alias Octocon.Friendships.Friendship -end diff --git a/lib/octocon_web/utils/systems.ex b/lib/octocon_web/utils/systems.ex deleted file mode 100644 index 42d156f8..00000000 --- a/lib/octocon_web/utils/systems.ex +++ /dev/null @@ -1,107 +0,0 @@ -defmodule OctoconWeb.Utils.Systems do - @moduledoc false - - import Phoenix.Controller, only: [json: 2] - import Plug.Conn - - alias Octocon.{ - Accounts, - Accounts.User, - Friendships - } - - def parse_system(conn, id) - - def parse_system(conn, "me") do - caller_id = conn.private[:guardian_default_resource] - - if caller_id == nil do - { - :noreply, - conn - |> put_status(:unauthorized) - |> json(%{error: "You are not authenticated.", code: "unauthenticated"}) - } - else - system = Accounts.get_user!({:system, caller_id}) - {:self, system} - end - end - - def parse_system(conn, "id:" <> system_id) when byte_size(system_id) == 7 do - caller_id = conn.private[:guardian_default_resource] - - case Accounts.get_user({:system, system_id}) do - nil -> - {:noreply, - conn - |> put_status(:not_found) - |> json(%{error: "No system found with ID \"#{system_id}\""})} - - system when system.id == caller_id -> - {:self, system} - - system -> - {:other, system |> put_friend_status(caller_id)} - end - end - - def parse_system(conn, "username:" <> username) - when byte_size(username) >= 3 and byte_size(username) <= 16 do - caller_id = conn.private[:guardian_default_resource] - - case Accounts.get_user_by_username(username) do - nil -> - {:noreply, - conn - |> put_status(:not_found) - |> json(%{error: "No system found with username \"#{username}\""})} - - system when system.id == caller_id -> - {:self, system} - - system -> - {:other, system |> put_friend_status(caller_id)} - end - end - - def parse_system(conn, "discord:" <> discord_id) - when byte_size(discord_id) >= 16 and byte_size(discord_id) <= 22 do - caller_id = conn.private[:guardian_default_resource] - - case Accounts.get_user({:discord, discord_id}) do - nil -> - {:noreply, - conn - |> put_status(:not_found) - |> json(%{error: "No system found with Discord ID \"#{discord_id}\""})} - - system when system.id == caller_id -> - {:self, system} - - system -> - {:other, system |> put_friend_status(caller_id)} - end - end - - def parse_system(conn, _) do - {:noreply, - conn - |> put_status(:bad_request) - |> json(%{ - error: - "Invalid format for system ID; must be one of: \"me\", \"id:\", \"username:\", \"discord:\"" - })} - end - - defp put_friend_status(system, caller_id) when caller_id == nil, do: system - defp put_friend_status(%User{} = system, caller_id) when system.id == caller_id, do: system - - defp put_friend_status(%User{} = system, caller_id) do - system - |> Map.put( - :friend_status, - Friendships.get_friendship_level({:system, caller_id}, {:system, system.id}) - ) - end -end diff --git a/lib/octocon_web/waffle_transformation.ex b/lib/octocon_web/waffle_transformation.ex deleted file mode 100644 index ce20342b..00000000 --- a/lib/octocon_web/waffle_transformation.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule OctoconWeb.WaffleTransformation do - @moduledoc false - - def apply(cmd, %Waffle.File{} = file, args, extension \\ nil) do - new_path = - if extension, - do: Waffle.File.generate_temporary_path(extension), - else: Waffle.File.generate_temporary_path(file) - - args = - ([file.path] ++ args) ++ ["#{new_path}"] - - program = to_string(cmd) - - ensure_executable_exists!(program) - - # credo:disable-for-next-line - result = System.cmd(program, args, stderr_to_stdout: true) - - case result do - {_, 0} -> - {:ok, %Waffle.File{file | path: new_path, is_tempfile?: true, file_name: "primary.webp"}} - - {error_message, _exit_code} -> - {:error, error_message} - end - end - - defp ensure_executable_exists!(program) do - unless System.find_executable(program) do - raise Waffle.MissingExecutableError, message: program - end - end -end diff --git a/mix.exs b/mix.exs deleted file mode 100644 index 6e491a46..00000000 --- a/mix.exs +++ /dev/null @@ -1,164 +0,0 @@ -defmodule Octocon.MixProject do - use Mix.Project - - def project do - [ - app: :octocon, - version: "0.1.0", - elixir: "~> 1.19", - elixirc_paths: elixirc_paths(Mix.env()), - start_permanent: Mix.env() == :prod, - aliases: aliases(), - deps: deps() - ] - end - - # Configuration for the OTP application. - # - # Type `mix help compile.app` for more information. - def application do - [ - mod: {Octocon.Application, []}, - included_applications: [:nostrum], - extra_applications: [ - :logger, - :runtime_tools, - :os_mon, - :timex, - :certifi, - :gun, - :inets, - :jason, - :mime, - :jose - ] - ] - end - - # Specifies which paths to compile per environment. - defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(_), do: ["lib"] - - # Specifies your project dependencies. - # - # Type `mix help deps` for examples and options. - defp deps do - [ - # Phoenix boilerplate - {:phoenix, "~> 1.7.21"}, - {:phoenix_ecto, "~> 4.6"}, - {:plug, "~> 1.19", override: true}, - {:ecto, "~> 3.13", override: true}, - {:ecto_sql, "~> 3.13"}, - {:ecto_psql_extras, "~> 0.8"}, - {:postgrex, "~> 0.21"}, - {:phoenix_live_dashboard, "~> 0.8"}, - - # Scylla - {:exandra, "~> 0.14"}, - - # Use Bandit instead of Cowboy - {:bandit, "~> 1.10"}, - {:websock_adapter, "~> 0.5"}, - - # Authentication - {:guardian, "~> 2.4"}, - {:guardian_db, "~> 3.0"}, - {:ueberauth, "~> 0.10"}, - {:ueberauth_google, "~> 0.10"}, - {:ueberauth_discord, "~> 0.6"}, - {:ueberauth_apple, "~> 0.6"}, - {:argon2_elixir, "~> 4.0"}, - {:jose, "~> 1.11"}, - - # Push notifications - {:pigeon, "~> 2.0"}, - - # Discord - # {:nostrum, - # github: "Kraigie/nostrum", - # ref: "e63a62fa85934a555251684cf789d81073f68adb", - # override: true, - # runtime: false}, - # {:nostrum, "~> 0.10", override: true, runtime: false}, - {:nostrum, path: "nostrum"}, - {:certifi, "~> 2.16", override: true}, - # Use zstd for gateway compression - {:ezstd, "~> 1.2"}, - # {:nosedrum, "~> 0.6"}, - # {:nosedrum, - # github: "jchristgit/nosedrum", branch: "master", override: true}, - - # Caching - {:cachex, "~> 4.1"}, - {:radix, "~> 0.5"}, - {:search, "~> 0.3"}, - # Background jobs - {:oban, "~> 2.19.4"}, - # Object storage - {:waffle, "~> 1.1"}, - {:image, "~> 0.62"}, - {:ex_aws, "~> 2.5"}, - {:ex_aws_s3, "~> 2.5"}, - - # Analytics - {:sentry, "~> 10.6.1"}, - - # Rate limiting - {:hammer, "~> 6.2"}, - {:hammer_plug, "~> 3.0"}, - - # Utils - {:timex, "~> 3.7"}, - - # Distribution - {:horde, "~> 0.9.0"}, - {:libcluster, "~> 3.3"}, - - # Time-series data - {:timescale, "~> 0.1"}, - - # Metrics - {:prom_ex, "~> 1.11"}, - {:telemetry_metrics, "~> 1.0", override: true}, - {:telemetry_poller, "~> 1.2"}, - - # Utilities - {:sweet_xml, "~> 0.7.4"}, - {:hackney, "~> 1.20"}, - {:jason, "~> 1.4"}, - {:finch, "~> 0.20"}, - {:nanoid, "~> 2.1.0"}, - - # Dev-only - {:credo, "~> 1.7", only: [:dev, :test], runtime: false} - ] - end - - # Aliases are shortcuts or tasks specific to the current project. - # For example, to install project dependencies and perform other setup tasks, run: - # - # $ mix setup - # - # See the documentation for `Mix` for more info on aliases. - defp aliases do - [ - setup: ["deps.get", "ecto.setup"], - "ecto.setup": [ - "ecto.create", - "ecto.migrate" - # "run priv/repo/seeds.exs", - # "run priv/msg_repo/seeds.exs" - ], - "ecto.reset": ["ecto.drop", "ecto.setup"], - "assets.deploy": [ - "phx.digest" - ], - lint: [ - "compile --force", - "format --check-formatted", - "credo --strict --ignore 'TagTODO'" - ] - ] - end -end diff --git a/mix.lock b/mix.lock deleted file mode 100644 index a493dbae..00000000 --- a/mix.lock +++ /dev/null @@ -1,111 +0,0 @@ -%{ - "argon2_elixir": {:hex, :argon2_elixir, "4.1.3", "4f28318286f89453364d7fbb53e03d4563fd7ed2438a60237eba5e426e97785f", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "7c295b8d8e0eaf6f43641698f962526cdf87c6feb7d14bd21e599271b510608c"}, - "bandit": {:hex, :bandit, "1.10.3", "1e5d168fa79ec8de2860d1b4d878d97d4fbbe2fdbe7b0a7d9315a4359d1d4bb9", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "99a52d909c48db65ca598e1962797659e3c0f1d06e825a50c3d75b74a5e2db18"}, - "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, - "cachex": {:hex, :cachex, "4.1.1", "574c5cd28473db313a0a76aac8c945fe44191659538ca6a1e8946ec300b1a19f", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:ex_hash_ring, "~> 6.0", [hex: :ex_hash_ring, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "d6b7449ff98d6bb92dda58bd4fc3189cae9f99e7042054d669596f56dc503cd8"}, - "castle": {:hex, :castle, "0.3.1", "e5d4f20696d878052a23c13158e1d372b24d9b30a6ea6f52fa6063c21c5ad67e", [:mix], [{:forecastle, "~> 0.1.3", [hex: :forecastle, repo: "hexpm", optional: false]}], "hexpm", "3ee9ca04b069280ab4197fe753562958729c83b3aa08125255116a989e133835"}, - "castore": {:hex, :castore, "1.0.17", "4f9770d2d45fbd91dcf6bd404cf64e7e58fed04fadda0923dc32acca0badffa2", [:mix], [], "hexpm", "12d24b9d80b910dd3953e165636d68f147a31db945d2dcb9365e441f8b5351e5"}, - "cc_precompiler": {:hex, :cc_precompiler, "0.1.11", "8c844d0b9fb98a3edea067f94f616b3f6b29b959b6b3bf25fee94ffe34364768", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3427232caf0835f94680e5bcf082408a70b48ad68a5f5c0b02a3bea9f3a075b9"}, - "certifi": {:hex, :certifi, "2.16.0", "a4edfc1d2da3424d478a3271133bf28e0ec5e6fd8c009aab5a4ae980cb165ce9", [:rebar3], [], "hexpm", "8a64f6669d85e9cc0e5086fcf29a5b13de57a13efa23d3582874b9a19303f184"}, - "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, - "comeonin": {:hex, :comeonin, "5.5.1", "5113e5f3800799787de08a6e0db307133850e635d34e9fab23c70b6501669510", [:mix], [], "hexpm", "65aac8f19938145377cee73973f192c5645873dcf550a8a6b18187d17c13ccdb"}, - "cowlib": {:hex, :cowlib, "2.16.0", "54592074ebbbb92ee4746c8a8846e5605052f29309d3a873468d76cdf932076f", [:make, :rebar3], [], "hexpm", "7f478d80d66b747344f0ea7708c187645cfcc08b11aa424632f78e25bf05db51"}, - "credo": {:hex, :credo, "1.7.16", "a9f1389d13d19c631cb123c77a813dbf16449a2aebf602f590defa08953309d4", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d0562af33756b21f248f066a9119e3890722031b6d199f22e3cf95550e4f1579"}, - "db_connection": {:hex, :db_connection, "2.9.0", "a6a97c5c958a2d7091a58a9be40caf41ab496b0701d21e1d1abff3fa27a7f371", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "17d502eacaf61829db98facf6f20808ed33da6ccf495354a41e64fe42f9c509c"}, - "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"}, - "delta_crdt": {:hex, :delta_crdt, "0.6.5", "c7bb8c2c7e60f59e46557ab4e0224f67ba22f04c02826e273738f3dcc4767adc", [:mix], [{:merkle_map, "~> 0.2.0", [hex: :merkle_map, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c6ae23a525d30f96494186dd11bf19ed9ae21d9fe2c1f1b217d492a7cc7294ae"}, - "ecto": {:hex, :ecto, "3.13.5", "9d4a69700183f33bf97208294768e561f5c7f1ecf417e0fa1006e4a91713a834", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "df9efebf70cf94142739ba357499661ef5dbb559ef902b68ea1f3c1fabce36de"}, - "ecto_psql_extras": {:hex, :ecto_psql_extras, "0.8.8", "aa02529c97f69aed5722899f5dc6360128735a92dd169f23c5d50b1f7fdede08", [:mix], [{:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "> 0.16.0", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1 or ~> 4.0", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "04c63d92b141723ad6fed2e60a4b461ca00b3594d16df47bbc48f1f4534f2c49"}, - "ecto_sql": {:hex, :ecto_sql, "3.13.4", "b6e9d07557ddba62508a9ce4a484989a5bb5e9a048ae0e695f6d93f095c25d60", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2b38cf0749ca4d1c5a8bcbff79bbe15446861ca12a61f9fba604486cb6b62a14"}, - "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, - "eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"}, - "ex_aws": {:hex, :ex_aws, "2.6.1", "194582c7b09455de8a5ab18a0182e6dd937d53df82be2e63c619d01bddaccdfa", [:mix], [{:configparser_ex, "~> 5.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "67842a08c90a1d9a09dbe4ac05754175c7ca253abe4912987c759395d4bd9d26"}, - "ex_aws_s3": {:hex, :ex_aws_s3, "2.5.9", "862b7792f2e60d7010e2920d79964e3fab289bc0fd951b0ba8457a3f7f9d1199", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "a480d2bb2da64610014021629800e1e9457ca5e4a62f6775bffd963360c2bf90"}, - "ex_hash_ring": {:hex, :ex_hash_ring, "6.0.4", "bef9d2d796afbbe25ab5b5a7ed746e06b99c76604f558113c273466d52fa6d6b", [:mix], [], "hexpm", "89adabf31f7d3dfaa36802ce598ce918e9b5b33bae8909ac1a4d052e1e567d18"}, - "exandra": {:hex, :exandra, "0.16.0", "848690b915c96a80f05c0bea782fd7b0fed7963ffb449f9022939fe6ade96e0c", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.13", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:xandra, "~> 0.19.4", [hex: :xandra, repo: "hexpm", optional: false]}], "hexpm", "4b95adfdde42bd953bb80d083347ac69cd71a520213b4c2cd83cecd9b01bd919"}, - "expo": {:hex, :expo, "1.1.1", "4202e1d2ca6e2b3b63e02f69cfe0a404f77702b041d02b58597c00992b601db5", [:mix], [], "hexpm", "5fb308b9cb359ae200b7e23d37c76978673aa1b06e2b3075d814ce12c5811640"}, - "ezstd": {:hex, :ezstd, "1.2.3", "98748f4099e6e2a067f77ace43041ebaa53c13194b08ce22370e4c93079e9e16", [:rebar3], [], "hexpm", "de32e0b41ba36a9ed46db8215da74777d2f141bb75f67bfc05dbb4b7c3386dee"}, - "file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"}, - "finch": {:hex, :finch, "0.21.0", "b1c3b2d48af02d0c66d2a9ebfb5622be5c5ecd62937cf79a88a7f98d48a8290c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "87dc6e169794cb2570f75841a19da99cfde834249568f2a5b121b809588a4377"}, - "forecastle": {:hex, :forecastle, "0.1.3", "b07d217ef10799e6d6cc7e47407858e77b1a8cb248f15185534de3403de3aa42", [:mix], [], "hexpm", "07e1ffa79c56f3e0ead59f17c0163a747dafc210ca8f244a7e65a4bfa98dc96d"}, - "gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"}, - "goth": {:hex, :goth, "1.4.5", "ee37f96e3519bdecd603f20e7f10c758287088b6d77c0147cd5ee68cf224aade", [:mix], [{:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:jose, "~> 1.11", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "0fc2dce5bd710651ed179053d0300ce3a5d36afbdde11e500d57f05f398d5ed5"}, - "guardian": {:hex, :guardian, "2.4.0", "efbbb397ecca881bb548560169922fc4433a05bc98c2eb96a7ed88ede9e17d64", [:mix], [{:jose, "~> 1.11.9", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "5c80103a9c538fbc2505bf08421a82e8f815deba9eaedb6e734c66443154c518"}, - "guardian_db": {:hex, :guardian_db, "3.0.0", "c42902e3f1af1ba1e2d0c10913b926a1421f3a7e38eb4fc382b715c17489abdb", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.1", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:guardian, "~> 1.0 or ~> 2.0", [hex: :guardian, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "9c2ec4278efa34f9f1cc6ba795e552d41fdc7ffba5319d67eeb533b89392d183"}, - "gun": {:hex, :gun, "2.2.0", "b8f6b7d417e277d4c2b0dc3c07dfdf892447b087f1cc1caff9c0f556b884e33d", [:make, :rebar3], [{:cowlib, ">= 2.15.0 and < 3.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "76022700c64287feb4df93a1795cff6741b83fb37415c40c34c38d2a4645261a"}, - "hackney": {:hex, :hackney, "1.25.0", "390e9b83f31e5b325b9f43b76e1a785cbdb69b5b6cd4e079aa67835ded046867", [:rebar3], [{:certifi, "~> 2.15.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.4", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "7209bfd75fd1f42467211ff8f59ea74d6f2a9e81cbcee95a56711ee79fd6b1d4"}, - "hammer": {:hex, :hammer, "6.2.1", "5ae9c33e3dceaeb42de0db46bf505bd9c35f259c8defb03390cd7556fea67ee2", [:mix], [{:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "b9476d0c13883d2dc0cc72e786bac6ac28911fba7cc2e04b70ce6a6d9c4b2bdc"}, - "hammer_plug": {:hex, :hammer_plug, "3.2.0", "47db6ed67d5cdf09fb6035f26b0b4b2335c3ae08a7ac061e3303bbb756fe9a09", [:mix], [{:hammer, "~> 6.0", [hex: :hammer, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "1ee7084732414c7a32f467717d13e6fba95c60b70c3f56d51f7c08a4183aadfe"}, - "horde": {:hex, :horde, "0.9.1", "547507dfe1228471d9a31cece7653be81df2cf8f210816f672ce507dc9606128", [:mix], [{:delta_crdt, "~> 0.6.2", [hex: :delta_crdt, repo: "hexpm", optional: false]}, {:libring, "~> 1.7", [hex: :libring, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 0.5.0 or ~> 1.0", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "14e1be4d6a84b066101641628a64f849d6df4f0311e93b7a9c39709e9c0bf4ba"}, - "hpack": {:hex, :hpack_erl, "0.3.0", "2461899cc4ab6a0ef8e970c1661c5fc6a52d3c25580bc6dd204f84ce94669926", [:rebar3], [], "hexpm", "d6137d7079169d8c485c6962dfe261af5b9ef60fbc557344511c1e65e3d95fb0"}, - "hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"}, - "httpoison": {:hex, :httpoison, "2.3.0", "10eef046405bc44ba77dc5b48957944df8952cc4966364b3cf6aa71dce6de587", [:mix], [{:hackney, "~> 1.21", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "d388ee70be56d31a901e333dbcdab3682d356f651f93cf492ba9f06056436a2c"}, - "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, - "image": {:hex, :image, "0.63.0", "03efcfeb1cfaac10d6ab364acce2bfa35e6b33b08947f291fdc17df796b36226", [:mix], [{:bumblebee, "~> 0.6", [hex: :bumblebee, repo: "hexpm", optional: true]}, {:evision, "~> 0.1.33 or ~> 0.2", [hex: :evision, repo: "hexpm", optional: true]}, {:exla, "~> 0.9", [hex: :exla, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:kino, "~> 0.13", [hex: :kino, repo: "hexpm", optional: true]}, {:nx, "~> 0.10", [hex: :nx, repo: "hexpm", optional: true]}, {:nx_image, "~> 0.1", [hex: :nx_image, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.1 or ~> 3.2 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: true]}, {:req, "~> 0.4", [hex: :req, repo: "hexpm", optional: true]}, {:rustler, "> 0.0.0", [hex: :rustler, repo: "hexpm", optional: true]}, {:scholar, "~> 0.3", [hex: :scholar, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: false]}, {:vix, "~> 0.33", [hex: :vix, repo: "hexpm", optional: false]}], "hexpm", "63b39a312f889bb61a04a4957977a7cd3fa23974ff56de3feebf46f69c5fa60e"}, - "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, - "joken": {:hex, :joken, "2.6.2", "5daaf82259ca603af4f0b065475099ada1b2b849ff140ccd37f4b6828ca6892a", [:mix], [{:jose, "~> 1.11.10", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "5134b5b0a6e37494e46dbf9e4dad53808e5e787904b7c73972651b51cce3d72b"}, - "jose": {:hex, :jose, "1.11.12", "06e62b467b61d3726cbc19e9b5489f7549c37993de846dfb3ee8259f9ed208b3", [:mix, :rebar3], [], "hexpm", "31e92b653e9210b696765cdd885437457de1add2a9011d92f8cf63e4641bab7b"}, - "jumper": {:hex, :jumper, "1.0.2", "68cdcd84472a00ac596b4e6459a41b3062d4427cbd4f1e8c8793c5b54f1406a7", [:mix], [], "hexpm", "9b7782409021e01ab3c08270e26f36eb62976a38c1aa64b2eaf6348422f165e1"}, - "kadabra": {:hex, :kadabra, "0.6.2", "04b4a104a7622eb422ec37635bbf0dfb51c1aa7bc803d387e0c1f98f8bb4bb08", [:mix], [{:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:hpack, "~> 0.3.0", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "2d5f0137d9ed2b885e96c3d686c72507b50b02494a2ce3e53d33a6f80e367c7b"}, - "leven": {:hex, :leven, "1.0.2", "453d8a3e729d2b9b1b191530af8f9aa5c4661e70fda2d16fae8c4c099960ecfc", [:mix], [{:benchee, "~> 1.0", [hex: :benchee, repo: "hexpm", optional: true]}], "hexpm", "d74b613b870a5132cc1146168bd27c0c0fdbd004c4460ecbc4b72ec5fc0c58de"}, - "libcluster": {:hex, :libcluster, "3.5.0", "5ee4cfde4bdf32b2fef271e33ce3241e89509f4344f6c6a8d4069937484866ba", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.3", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ebf6561fcedd765a4cd43b4b8c04b1c87f4177b5fb3cbdfe40a780499d72f743"}, - "libring": {:hex, :libring, "1.7.0", "4f245d2f1476cd7ed8f03740f6431acba815401e40299208c7f5c640e1883bda", [:mix], [], "hexpm", "070e3593cb572e04f2c8470dd0c119bc1817a7a0a7f88229f43cf0345268ec42"}, - "merkle_map": {:hex, :merkle_map, "0.2.2", "f36ff730cca1f2658e317a3c73406f50bbf5ac8aff54cf837d7ca2069a6e251c", [:mix], [], "hexpm", "383107f0503f230ac9175e0631647c424efd027e89ea65ab5ea12eeb54257aaf"}, - "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, - "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, - "mimerl": {:hex, :mimerl, "1.4.0", "3882a5ca67fbbe7117ba8947f27643557adec38fa2307490c4c4207624cb213b", [:rebar3], [], "hexpm", "13af15f9f68c65884ecca3a3891d50a7b57d82152792f3e19d88650aa126b144"}, - "mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"}, - "nanoid": {:hex, :nanoid, "2.1.0", "d192a5bf1d774258bc49762b480fca0e3128178fa6d35a464af2a738526607fd", [:mix], [], "hexpm", "ebc7a342d02d213534a7f93a091d569b9fea7f26fcd3a638dc655060fc1f76ac"}, - "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, - "nimble_ownership": {:hex, :nimble_ownership, "0.3.2", "d4fa4056ade0ae33b5a9eb64554a1b3779689282e37513260125d2d6b32e4874", [:mix], [], "hexpm", "28b9a9f4094fda1aa8ca72f732ff3223eb54aa3eda4fed9022254de2c152b138"}, - "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, - "nostrum": {:git, "https://github.com/Kraigie/nostrum.git", "e63a62fa85934a555251684cf789d81073f68adb", [ref: "e63a62fa85934a555251684cf789d81073f68adb"]}, - "oauth2": {:hex, :oauth2, "2.1.0", "beb657f393814a3a7a8a15bd5e5776ecae341fd344df425342a3b6f1904c2989", [:mix], [{:tesla, "~> 1.5", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "8ac07f85b3307dd1acfeb0ec852f64161b22f57d0ce0c15e616a1dfc8ebe2b41"}, - "oban": {:hex, :oban, "2.19.4", "045adb10db1161dceb75c254782f97cdc6596e7044af456a59decb6d06da73c1", [:mix], [{:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ecto_sqlite3, "~> 0.9", [hex: :ecto_sqlite3, repo: "hexpm", optional: true]}, {:igniter, "~> 0.5", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5fcc6219e6464525b808d97add17896e724131f498444a292071bf8991c99f97"}, - "octo_fetch": {:hex, :octo_fetch, "0.4.0", "074b5ecbc08be10b05b27e9db08bc20a3060142769436242702931c418695b19", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "cf8be6f40cd519d7000bb4e84adcf661c32e59369ca2827c4e20042eda7a7fc6"}, - "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, - "peep": {:hex, :peep, "3.5.0", "9f6ead7b0f2c684494200c8fc02e7e62e8c459afe861b29bd859e4c96f402ed8", [:mix], [{:nimble_options, "~> 1.1", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:plug, "~> 1.16", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry_metrics, "~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "5a73a99c6e60062415efeb7e536a663387146463a3d3df1417da31fd665ac210"}, - "phoenix": {:hex, :phoenix, "1.7.21", "14ca4f1071a5f65121217d6b57ac5712d1857e40a0833aff7a691b7870fc9a3b", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "336dce4f86cba56fed312a7d280bf2282c720abb6074bdb1b61ec8095bdd0bc9"}, - "phoenix_ecto": {:hex, :phoenix_ecto, "4.7.0", "75c4b9dfb3efdc42aec2bd5f8bccd978aca0651dbcbc7a3f362ea5d9d43153c6", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "1d75011e4254cb4ddf823e81823a9629559a1be93b4321a6a5f11a5306fbf4cc"}, - "phoenix_html": {:hex, :phoenix_html, "4.3.0", "d3577a5df4b6954cd7890c84d955c470b5310bb49647f0a114a6eeecc850f7ad", [:mix], [], "hexpm", "3eaa290a78bab0f075f791a46a981bbe769d94bc776869f4f3063a14f30497ad"}, - "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.8.7", "405880012cb4b706f26dd1c6349125bfc903fb9e44d1ea668adaf4e04d4884b7", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:ecto_sqlite3_extras, "~> 1.1.7 or ~> 1.2.0", [hex: :ecto_sqlite3_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.19 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "3a8625cab39ec261d48a13b7468dc619c0ede099601b084e343968309bd4d7d7"}, - "phoenix_live_view": {:hex, :phoenix_live_view, "0.20.17", "f396bbdaf4ba227b82251eb75ac0afa6b3da5e509bc0d030206374237dfc9450", [:mix], [{:floki, "~> 0.36", [hex: :floki, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a61d741ffb78c85fdbca0de084da6a48f8ceb5261a79165b5a0b59e5f65ce98b"}, - "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.2.0", "ff3a5616e1bed6804de7773b92cbccfc0b0f473faf1f63d7daf1206c7aeaaa6f", [:mix], [], "hexpm", "adc313a5bf7136039f63cfd9668fde73bba0765e0614cba80c06ac9460ff3e96"}, - "phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"}, - "pigeon": {:hex, :pigeon, "2.0.1", "b8399ed6064245c75fbfb2e92dafb43999228d8e54f0b633184b67a35d68b088", [:mix], [{:goth, "~> 1.4.3", [hex: :goth, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:joken, "~> 2.1", [hex: :joken, repo: "hexpm", optional: false]}, {:kadabra, "~> 0.6.0", [hex: :kadabra, repo: "hexpm", optional: false]}], "hexpm", "f24a8cacb4928886c25bbc95dd851e36f2317eba9db42ac13724b9e97c3ec665"}, - "plug": {:hex, :plug, "1.19.1", "09bac17ae7a001a68ae393658aa23c7e38782be5c5c00c80be82901262c394c0", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "560a0017a8f6d5d30146916862aaf9300b7280063651dd7e532b8be168511e62"}, - "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, - "poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"}, - "postgrex": {:hex, :postgrex, "0.22.0", "fb027b58b6eab1f6de5396a2abcdaaeb168f9ed4eccbb594e6ac393b02078cbd", [:mix], [{:db_connection, "~> 2.9", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "a68c4261e299597909e03e6f8ff5a13876f5caadaddd0d23af0d0a61afcc5d84"}, - "prom_ex": {:hex, :prom_ex, "1.11.0", "1f6d67f2dead92224cb4f59beb3e4d319257c5728d9638b4a5e8ceb51a4f9c7e", [:mix], [{:absinthe, ">= 1.7.0", [hex: :absinthe, repo: "hexpm", optional: true]}, {:broadway, ">= 1.1.0", [hex: :broadway, repo: "hexpm", optional: true]}, {:ecto, ">= 3.11.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:finch, "~> 0.18", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:oban, ">= 2.10.0", [hex: :oban, repo: "hexpm", optional: true]}, {:octo_fetch, "~> 0.4", [hex: :octo_fetch, repo: "hexpm", optional: false]}, {:peep, "~> 3.0", [hex: :peep, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.7.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, ">= 0.20.0", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, ">= 1.16.0", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 2.6.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, ">= 1.0.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}, {:telemetry_metrics_prometheus_core, "~> 1.2", [hex: :telemetry_metrics_prometheus_core, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 1.1", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "76b074bc3730f0802978a7eb5c7091a65473eaaf07e99ec9e933138dcc327805"}, - "radix": {:hex, :radix, "0.5.0", "b9731f67f65f82bdfb500f9b4de47426b8b5b823b3c9c74fd7048081e55f5f62", [:mix], [], "hexpm", "1286e000c70cf3189301c01f525282ef168f1ed143b4402b70679879e8e13b85"}, - "salchicha": {:hex, :salchicha, "0.5.0", "b05b404550b433494fce1abdb23314afa1f1c72aef898f2c77de6554cf58235e", [:mix], [], "hexpm", "b3e0575cd5a01672d9cefc4ec50bd56662a4d970e2ae39d8de6cf82f09012fc8"}, - "search": {:hex, :search, "0.3.0", "067f1e3178119379e669b6e1b98ab4f6f523763fb17e8121e7d49cbb99d8bc3a", [:mix], [{:leven, "~> 1.0", [hex: :leven, repo: "hexpm", optional: false]}, {:radix, "~> 0.5", [hex: :radix, repo: "hexpm", optional: false]}, {:typed_struct, "~> 0.3", [hex: :typed_struct, repo: "hexpm", optional: false]}], "hexpm", "4888310f63be84fb9e5021a4387bb373bec85ea50c718cd45e4f6a4b81c8e6ac"}, - "sentry": {:hex, :sentry, "10.6.2", "a867ab728d424e187ccb2bccc388170a740a79bc0ddccabd72d303b203acbe0e", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_ownership, "~> 0.3.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, "~> 0.20", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "31bb84247274f9262fd300df0e3eb73302e4849cc6b7a6560bb2465f03fbd446"}, - "sleeplocks": {:hex, :sleeplocks, "1.1.3", "96a86460cc33b435c7310dbd27ec82ca2c1f24ae38e34f8edde97f756503441a", [:rebar3], [], "hexpm", "d3b3958552e6eb16f463921e70ae7c767519ef8f5be46d7696cc1ed649421321"}, - "sourceror": {:hex, :sourceror, "1.11.0", "df2cdaffdc323e804009ff50b50bb31e6f2d6e116d936ccf22981f592594d624", [:mix], [], "hexpm", "6e26f572bdfc21d7ad397f596b4cfbbf31d7112126fe3e902c120947073231a8"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, - "sweet_xml": {:hex, :sweet_xml, "0.7.5", "803a563113981aaac202a1dbd39771562d0ad31004ddbfc9b5090bdcd5605277", [:mix], [], "hexpm", "193b28a9b12891cae351d81a0cead165ffe67df1b73fe5866d10629f4faefb12"}, - "table_rex": {:hex, :table_rex, "4.1.0", "fbaa8b1ce154c9772012bf445bfb86b587430fb96f3b12022d3f35ee4a68c918", [:mix], [], "hexpm", "95932701df195d43bc2d1c6531178fc8338aa8f38c80f098504d529c43bc2601"}, - "telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"}, - "telemetry_metrics": {:hex, :telemetry_metrics, "1.1.0", "5bd5f3b5637e0abea0426b947e3ce5dd304f8b3bc6617039e2b5a008adc02f8f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e7b79e8ddfde70adb6db8a6623d1778ec66401f366e9a8f5dd0955c56bc8ce67"}, - "telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.2.1", "c9755987d7b959b557084e6990990cb96a50d6482c683fb9622a63837f3cd3d8", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "5e2c599da4983c4f88a33e9571f1458bf98b0cf6ba930f1dc3a6e8cf45d5afb6"}, - "telemetry_poller": {:hex, :telemetry_poller, "1.3.0", "d5c46420126b5ac2d72bc6580fb4f537d35e851cc0f8dbd571acf6d6e10f5ec7", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "51f18bed7128544a50f75897db9974436ea9bfba560420b646af27a9a9b35211"}, - "tesla": {:hex, :tesla, "1.16.0", "de77d083aea08ebd1982600693ff5d779d68a4bb835d136a0394b08f69714660", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, ">= 1.0.0", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.21", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.2", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:mox, "~> 1.0", [hex: :mox, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "eb3bdfc0c6c8a23b4e3d86558e812e3577acff1cb4acb6cfe2da1985a1035b89"}, - "thousand_island": {:hex, :thousand_island, "1.4.3", "2158209580f633be38d43ec4e3ce0a01079592b9657afff9080d5d8ca149a3af", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6e4ce09b0fd761a58594d02814d40f77daff460c48a7354a15ab353bb998ea0b"}, - "timescale": {:hex, :timescale, "0.1.1", "619a1e3e1f632cad582c88aec7c13be40231171efc1cd6f2ed9b0c1a320602d9", [:mix], [{:ecto_sql, "~> 3.8", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "cd537db21a34dd1551a39945cb44eef1f332f0ce4c15b072347d2e8996524fa4"}, - "timex": {:hex, :timex, "3.7.13", "0688ce11950f5b65e154e42b47bf67b15d3bc0e0c3def62199991b8a8079a1e2", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.26", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "09588e0522669328e973b8b4fd8741246321b3f0d32735b589f78b136e6d4c54"}, - "typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"}, - "tzdata": {:hex, :tzdata, "1.1.3", "b1cef7bb6de1de90d4ddc25d33892b32830f907e7fc2fccd1e7e22778ab7dfbc", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "d4ca85575a064d29d4e94253ee95912edfb165938743dbf002acdf0dcecb0c28"}, - "ueberauth": {:hex, :ueberauth, "0.10.8", "ba78fbcbb27d811a6cd06ad851793aaf7d27c3b30c9e95349c2c362b344cd8f0", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f2d3172e52821375bccb8460e5fa5cb91cfd60b19b636b6e57e9759b6f8c10c1"}, - "ueberauth_apple": {:hex, :ueberauth_apple, "0.6.1", "272159a2e0e0c2f66904a06658781ecb5f980cd9d73f9e3600778154dccdbdb8", [:mix], [{:httpoison, "~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jose, "~> 1.0", [hex: :jose, repo: "hexpm", optional: false]}, {:oauth2, "~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.10", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "6e2a50d5b1e27ed6410cb087604c1053b0ea809808dc0252acbf2a17471373f9"}, - "ueberauth_discord": {:hex, :ueberauth_discord, "0.7.0", "463f6dfe1ed10a76739331ce8e1dd3600ab611f10524dd828eb3aa50e76e9d43", [:mix], [{:oauth2, "~> 1.0 or ~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.7", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "d6f98ef91abb4ddceada4b7acba470e0e68c4d2de9735ff2f24172a8e19896b4"}, - "ueberauth_google": {:hex, :ueberauth_google, "0.12.1", "90cf49743588193334f7a00da252f92d90bfd178d766c0e4291361681fafec7d", [:mix], [{:oauth2, "~> 1.0 or ~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.10.0", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "7f7deacd679b2b66e3bffb68ecc77aa1b5396a0cbac2941815f253128e458c38"}, - "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.1", "a48703a25c170eedadca83b11e88985af08d35f37c6f664d6dcfb106a97782fc", [:rebar3], [], "hexpm", "b3a917854ce3ae233619744ad1e0102e05673136776fb2fa76234f3e03b23642"}, - "unsafe": {:hex, :unsafe, "1.0.2", "23c6be12f6c1605364801f4b47007c0c159497d0446ad378b5cf05f1855c0581", [:mix], [], "hexpm", "b485231683c3ab01a9cd44cb4a79f152c6f3bb87358439c6f68791b85c2df675"}, - "vix": {:hex, :vix, "0.38.0", "77529ee4f6ced339c3d5f90a9eacf306f5b7109d3d1b5e3ef391a984ad404f75", [:make, :mix], [{:cc_precompiler, "~> 0.1.4 or ~> 0.2", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.7.3 or ~> 0.8", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: true]}], "hexpm", "dca58f654922fa678d5df8e028317483d9c0f8acb2e2714076a8468695687aa7"}, - "waffle": {:hex, :waffle, "1.1.10", "0f847ed6f95349af258a90f0f70ffea02b3d3729c4eb78f6fae7bf776e91779e", [:mix], [{:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:ex_aws_s3, "~> 2.1", [hex: :ex_aws_s3, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "859ba6377b78f0a51bc9596227b194f26241efbbd408bd217450c22b0f359cc4"}, - "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, - "websock_adapter": {:hex, :websock_adapter, "0.5.9", "43dc3ba6d89ef5dec5b1d0a39698436a1e856d000d84bf31a3149862b01a287f", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "5534d5c9adad3c18a0f58a9371220d75a803bf0b9a3d87e6fe072faaeed76a08"}, - "xandra": {:hex, :xandra, "0.19.4", "bc6e46db726ce252a21b1bb5a7a21502b61702242dccf0d1007201210064e08c", [:mix], [{:decimal, "~> 1.7 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3584949976f1146d3bef0f4f91a0fb2ce025f24be181827172ba0bc2571cb362"}, -} diff --git a/nostrum b/nostrum deleted file mode 160000 index 8b2ef42c..00000000 --- a/nostrum +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8b2ef42c2e8bcb1b681d112ce96795f8028443d8 diff --git a/nuget.config b/nuget.config new file mode 100644 index 00000000..69c7f0e8 --- /dev/null +++ b/nuget.config @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/priv/cert/selfsigned.pem b/priv/cert/selfsigned.pem deleted file mode 100644 index fb1871ea..00000000 --- a/priv/cert/selfsigned.pem +++ /dev/null @@ -1,22 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIDfDCCAmSgAwIBAgIIfIVn4Pvv8KYwDQYJKoZIhvcNAQELBQAwQzEaMBgGA1UE -CgwRUGhvZW5peCBGcmFtZXdvcmsxJTAjBgNVBAMMHFNlbGYtc2lnbmVkIHRlc3Qg -Y2VydGlmaWNhdGUwHhcNMjUwMzEyMDAwMDAwWhcNMjYwMzEyMDAwMDAwWjBDMRow -GAYDVQQKDBFQaG9lbml4IEZyYW1ld29yazElMCMGA1UEAwwcU2VsZi1zaWduZWQg -dGVzdCBjZXJ0aWZpY2F0ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB -AN9vI/E4Z2yVmK7vEzg6UdJKjGCr2Wxhl3n5qb9b4y3nx+4cpi9vtjM5xxFV3B6m -gtmhtSe+xV1FLFaMLte5ZkMuVHx0yJjcGWRFDDgVHGpeYkiIGPfqEGYmBkgqjO0S -Ek9oLc+mmvr65Bv3Skfq7D934sPVSA4Tj+lpFX1KCRqXwOA0DpClqT0ON9BaK6q5 -3LfM45+CTaJdw7rsAf0VHNKgP/MFL+/r7exXFIVS7E+SVQ/uyoKLMMvfBwk6qNBC -RjaMy+wHdrtsi6GBUOhRRV8ZgG3a1XAZvbZ9ZtgJh9w1Vji5IWVDzU2TAdKXdKis -f/8zY3jPxcvdjcTCOqSDZ6kCAwEAAaN0MHIwDAYDVR0TAQH/BAIwADAOBgNVHQ8B -Af8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB0GA1UdDgQW -BBQ4Ma3IYQT3L83haO9ieit1B1i3STAUBgNVHREEDTALgglsb2NhbGhvc3QwDQYJ -KoZIhvcNAQELBQADggEBABObL5b/v10rtF0oRLgcD52hlhG7nowTGSoc72TaNtO3 -143JwHAmNLbpjf+IUU52f40TrPSljXf8cB67qdG4DDZVhSMyXQx4Sh/xnpGWWsug -BP45z5dm1rBlCSPCXg2U7qh2f+n7FVVfV7aKzwc4raVe03P6uxrxPJat6olIkGQc -KRSh8qsppdm+MXwBeCdYmRZ/CmZBJaWtDz1G1NBlxhGQUC2lSAo1NjF4uC+7WXy5 -A62AsvtBB1ZOFdnnJQ6ld/VsDHHvRzl+ve16iwSW07MGCPB29RDpFQwlvViTlHIx -M4RlA1fX+vNn3LoN4dv63RTS5J+cnNxmbAvgtMmNlrk= ------END CERTIFICATE----- - diff --git a/priv/cert/selfsigned_key.pem b/priv/cert/selfsigned_key.pem deleted file mode 100644 index 3ae8fb16..00000000 --- a/priv/cert/selfsigned_key.pem +++ /dev/null @@ -1,28 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEA328j8ThnbJWYru8TODpR0kqMYKvZbGGXefmpv1vjLefH7hym -L2+2MznHEVXcHqaC2aG1J77FXUUsVowu17lmQy5UfHTImNwZZEUMOBUcal5iSIgY -9+oQZiYGSCqM7RIST2gtz6aa+vrkG/dKR+rsP3fiw9VIDhOP6WkVfUoJGpfA4DQO -kKWpPQ430Forqrnct8zjn4JNol3DuuwB/RUc0qA/8wUv7+vt7FcUhVLsT5JVD+7K -goswy98HCTqo0EJGNozL7Ad2u2yLoYFQ6FFFXxmAbdrVcBm9tn1m2AmH3DVWOLkh -ZUPNTZMB0pd0qKx//zNjeM/Fy92NxMI6pINnqQIDAQABAoIBACkBUSPWeaikZcKJ -DsptbZG967hcIYlx+aZBiwUa/Hzywi7xZBBlnJcXHfERBFY9sdr2AfSa3sbIyKPT -k080788ZhBjZJ+1hy2krANyUB0SAMOJl08s/0iWlCdcZtAZYIDMhh/LJfTzdaZq2 -ThQXyaa2PlA5ZsFqlqk8B10YhsYxxsLa3izVRIBNPt8WwRLsBwoBKws6QEPCVCzv -dsaDG7nFzBEjusWslcdzyba675HtBkB651hSWukgyxyOKgkhLMpWFPCnkSJ4HQox -opRSToCrW7iVLyu0Q4KdeUjxnAkGmF0y+SVJK6QGVVVW1RQ4Wh8iJgiC1iVDmb7k -cGp5MhkCgYEA8QqWK6JrXfnQ7sUNQc26NwFLQavLrVjZeTNYIApyn/NH4s77mGTW -91J0GjI9+lCLxrV3Xq8yWFivd2XTRh8BDUQ0Akl6zVJzkfn0x0Qo3WUgepWnOCV3 -l/P2VpgAYVv1K4a4dlH66LssEYayaMA3oAHYYPl3kAmkVADGUlYzr4UCgYEA7UzU -LAylAfUI4QpqGhGs/VSCA8R0lI8lrsN65pPjlN+Rc9Ig0Lc1MyIzv8qnv8c55ACZ -i0eDjwTB1YeD5O0lIOfQTiBUlNkwqoMQQxAFDmaiJEAzMahs2K4wxyve8Y2CFl95 -pyoCic91GI1yly6ycaPkCUM1NEb7EFtKv+C0RtUCgYEAyoPQg7ykZIJ9bf6VEtJc -P9aQeg5yw6PAo7nlspXUlFp77S1l6TXf/k+CSHgp5mNB7KOONTkRHXT1h2U/ysxF -v/WrlLhTuqope138sGatxsUuMpAFvICOKFbKgxqWRzZRmE5KMO7T1jFb5yW6Q5Qr -p/2k5GT0dUoEjKdR+N9/8VkCgYEAt1TL9twt/z0YBwTMxCczxuQrLk3w43VUX5iW -st0+zh2Leje1MorYhxC9ekdPIlMA3q238mrlsPCz8AwqUgsEzE9MF4/SyMyjVJi9 -T5Jo1Z9lVSxkYNEGhsDM3pN+QpBZGH/atNM38tBcXwOorDRfmHxOCPnreN/9x71A -Gs23eaECgYBp8OeVTMEqtIiM2mRuylb9IctSr5ga9cNxcRn0IIi1dJNXMjeZhxvA -kYaIXdz0ZUzzw+dd6RWRY2J7+H9WiZzwFHcqrvF+2qRuTIsWktpKXjhn4SuOZInH -iNpYr5+vKhyL9dVhWes07A37/hR+eb6edGzez+uyeVzZpJ8dcFMBIg== ------END RSA PRIVATE KEY----- - diff --git a/priv/msg_repo/migrations/.formatter.exs b/priv/msg_repo/migrations/.formatter.exs deleted file mode 100644 index 49f9151e..00000000 --- a/priv/msg_repo/migrations/.formatter.exs +++ /dev/null @@ -1,4 +0,0 @@ -[ - import_deps: [:ecto_sql], - inputs: ["*.exs"] -] diff --git a/priv/msg_repo/migrations/20240625112733_add_messages.exs b/priv/msg_repo/migrations/20240625112733_add_messages.exs deleted file mode 100644 index 83c60d98..00000000 --- a/priv/msg_repo/migrations/20240625112733_add_messages.exs +++ /dev/null @@ -1,34 +0,0 @@ -defmodule Octocon.MessageRepo.Migrations.AddMessages do - use Ecto.Migration - - import Timescale.Migration - - def up do - execute("CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE") - - create_if_not_exists table(:messages, primary_key: false) do - add :timestamp, :utc_datetime, null: false - add :message_id, :string, size: 22, null: false - add :author_id, :string, size: 22, null: false - add :system_id, :string, size: 7, null: false - add :alter_id, :int2, null: false - end - - execute( - "SELECT create_hypertable('messages', 'timestamp', chunk_time_interval => INTERVAL '1 week');" - ) - - execute( - "ALTER TABLE messages SET (timescaledb.compress, timescaledb.compress_segmentby = 'author_id');" - ) - - execute("SELECT add_compression_policy('messages', INTERVAL '1 week');") - execute("SELECT add_retention_policy('messages', INTERVAL '6 months');") - end - - def down do - drop_if_exists table(:messages) - - execute("DROP EXTENSION IF EXISTS timescaledb CASCADE") - end -end diff --git a/priv/msg_repo/seeds.exs b/priv/msg_repo/seeds.exs deleted file mode 100644 index 18227039..00000000 --- a/priv/msg_repo/seeds.exs +++ /dev/null @@ -1,11 +0,0 @@ -# Script for populating the database. You can run it as: -# -# mix run priv/repo/seeds.exs -# -# Inside the script, you can read and write to any of your -# repositories directly: -# -# Octocon.Repo.insert!(%Octocon.SomeSchema{}) -# -# We recommend using the bang functions (`insert!`, `update!` -# and so on) as they will fail if something goes wrong. diff --git a/priv/old_repo/migrations/.formatter.exs b/priv/old_repo/migrations/.formatter.exs deleted file mode 100644 index 49f9151e..00000000 --- a/priv/old_repo/migrations/.formatter.exs +++ /dev/null @@ -1,4 +0,0 @@ -[ - import_deps: [:ecto_sql], - inputs: ["*.exs"] -] diff --git a/priv/old_repo/migrations/20231004043815_create_users.exs b/priv/old_repo/migrations/20231004043815_create_users.exs deleted file mode 100644 index 0dc574dd..00000000 --- a/priv/old_repo/migrations/20231004043815_create_users.exs +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateUsers do - use Ecto.Migration - - def change do - create table(:users, primary_key: false) do - add :id, :string, primary_key: true, size: 7 - add :email, :text - add :discord_id, :string, size: 22 - add :username, :string, size: 16 - add :avatar_url, :text - - add :primary_front, :integer - add :lifetime_alter_count, :integer, default: 0 - - # Discord - add :autoproxy_mode, :integer - add :last_proxy_id, :int2 - - timestamps() - end - - create unique_index(:users, [:email]) - create unique_index(:users, [:discord_id]) - create unique_index(:users, [:username]) - end -end diff --git a/priv/old_repo/migrations/20231004195947_create_alters.exs b/priv/old_repo/migrations/20231004195947_create_alters.exs deleted file mode 100644 index b0a0c2e1..00000000 --- a/priv/old_repo/migrations/20231004195947_create_alters.exs +++ /dev/null @@ -1,28 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateAlters do - use Ecto.Migration - - def change do - create table(:alters, primary_key: false) do - add :id, :int2, primary_key: true - - add :user_id, references(:users, type: :string, on_delete: :delete_all), - primary_key: true, - size: 7 - - add :name, :text - add :pronouns, :text - add :description, :text - add :security_level, :int2 - add :avatar_url, :text - add :extra_images, {:array, :text} - add :color, :string, size: 7 - - # Discord - add :discord_proxies, {:array, :string} - - add :fields, :map - - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20231007094109_create_fronts.exs b/priv/old_repo/migrations/20231007094109_create_fronts.exs deleted file mode 100644 index 79bc1070..00000000 --- a/priv/old_repo/migrations/20231007094109_create_fronts.exs +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateFronts do - use Ecto.Migration - - def change do - create table(:fronts, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - - add :comment, :string, size: 50, default: "" - add :time_start, :utc_datetime - add :time_end, :utc_datetime - end - - create index(:fronts, [:user_id, :alter_id]) - - create index(:fronts, :user_id, - where: "time_end IS NULL", - name: :currently_fronting_by_user_id - ) - - create index(:fronts, :user_id) - end -end diff --git a/priv/old_repo/migrations/20231007153316_add_oban_jobs_table.exs b/priv/old_repo/migrations/20231007153316_add_oban_jobs_table.exs deleted file mode 100644 index a71fc88b..00000000 --- a/priv/old_repo/migrations/20231007153316_add_oban_jobs_table.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddObanJobsTable do - use Ecto.Migration - - def up do - Oban.Migration.up(version: 11) - end - - # We specify `version: 1` in `down`, ensuring that we'll roll all the way back down if - # necessary, regardless of which version we've migrated `up` to. - def down do - Oban.Migration.down(version: 1) - end -end diff --git a/priv/old_repo/migrations/20231012004017_create_friendships.exs b/priv/old_repo/migrations/20231012004017_create_friendships.exs deleted file mode 100644 index 74c60bdf..00000000 --- a/priv/old_repo/migrations/20231012004017_create_friendships.exs +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateFriendships do - use Ecto.Migration - - def change do - create table(:friendships, primary_key: false) do - add :user_id, references(:users, type: :string, on_delete: :delete_all), primary_key: true - add :friend_id, references(:users, type: :string, on_delete: :delete_all), primary_key: true - add :level, :integer, default: 0 - add :since, :utc_datetime - - timestamps() - end - - create index(:friendships, :user_id) - create index(:friendships, :friend_id) - end -end diff --git a/priv/old_repo/migrations/20231012004335_create_friend_requests.exs b/priv/old_repo/migrations/20231012004335_create_friend_requests.exs deleted file mode 100644 index 96bc2b0f..00000000 --- a/priv/old_repo/migrations/20231012004335_create_friend_requests.exs +++ /dev/null @@ -1,16 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateFriendRequests do - use Ecto.Migration - - def change do - create table(:friend_requests, primary_key: false) do - add :from_id, references(:users, type: :string, on_delete: :delete_all), primary_key: true - add :to_id, references(:users, type: :string, on_delete: :delete_all), primary_key: true - add :date_sent, :utc_datetime - - timestamps() - end - - create index(:friend_requests, :from_id) - create index(:friend_requests, :to_id) - end -end diff --git a/priv/old_repo/migrations/20231013080453_create_channel_blacklists.exs b/priv/old_repo/migrations/20231013080453_create_channel_blacklists.exs deleted file mode 100644 index e6d7ed92..00000000 --- a/priv/old_repo/migrations/20231013080453_create_channel_blacklists.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateChannelBlacklists do - use Ecto.Migration - - def change do - create table(:channel_blacklists, primary_key: false) do - add :guild_id, :string, size: 22 - add :channel_id, :string, primary_key: true, size: 22 - - timestamps() - end - - create index(:channel_blacklists, :guild_id) - end -end diff --git a/priv/old_repo/migrations/20231209191337_create_pc_items.exs b/priv/old_repo/migrations/20231209191337_create_pc_items.exs deleted file mode 100644 index 2e23c65b..00000000 --- a/priv/old_repo/migrations/20231209191337_create_pc_items.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreatePcItems do - use Ecto.Migration - - def change do - create table(:pc_items, primary_key: false) do - add :id, :int2, primary_key: true, default: 1 - add :data, :binary - timestamps() - end - - create constraint(:pc_items, :id, check: "id = 1") - end -end diff --git a/priv/old_repo/migrations/20231215063638_create_notification_tokens.exs b/priv/old_repo/migrations/20231215063638_create_notification_tokens.exs deleted file mode 100644 index 5f68d263..00000000 --- a/priv/old_repo/migrations/20231215063638_create_notification_tokens.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateNotificationTokens do - use Ecto.Migration - - def change do - create table(:notification_tokens, primary_key: false) do - add :user_id, references(:users, type: :string, on_delete: :delete_all), primary_key: true - add :token, :string, primary_key: true - - timestamps() - end - - create index(:notification_tokens, [:user_id]) - end -end diff --git a/priv/old_repo/migrations/20231220102536_add_custom_fields.exs b/priv/old_repo/migrations/20231220102536_add_custom_fields.exs deleted file mode 100644 index 85a7ed19..00000000 --- a/priv/old_repo/migrations/20231220102536_add_custom_fields.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddCustomFields do - use Ecto.Migration - - def change do - alter table(:users) do - add :fields, {:array, :map}, default: [] - end - end -end diff --git a/priv/old_repo/migrations/20231221191459_change_field_type.exs b/priv/old_repo/migrations/20231221191459_change_field_type.exs deleted file mode 100644 index 02bbbd5a..00000000 --- a/priv/old_repo/migrations/20231221191459_change_field_type.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Octocon.Repo.Migrations.ChangeFieldType do - use Ecto.Migration - - def change do - alter table(:alters) do - remove :fields - add :fields, {:array, :map}, default: [] - end - end -end diff --git a/priv/old_repo/migrations/20240106033312_create_global_journals.exs b/priv/old_repo/migrations/20240106033312_create_global_journals.exs deleted file mode 100644 index 1aa62b57..00000000 --- a/priv/old_repo/migrations/20240106033312_create_global_journals.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateGlobalJournals do - use Ecto.Migration - - def change do - create table(:global_journals) do - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20240106033340_create_alter_journals.exs b/priv/old_repo/migrations/20240106033340_create_alter_journals.exs deleted file mode 100644 index dc55047b..00000000 --- a/priv/old_repo/migrations/20240106033340_create_alter_journals.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateAlterJournals do - use Ecto.Migration - - def change do - create table(:alter_journals) do - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20240214072322_add_alter_aliases.exs b/priv/old_repo/migrations/20240214072322_add_alter_aliases.exs deleted file mode 100644 index cd990f43..00000000 --- a/priv/old_repo/migrations/20240214072322_add_alter_aliases.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddAlterAliases do - use Ecto.Migration - - def change do - alter table(:alters) do - add :alias, :text, default: nil - end - - create unique_index(:alters, [:user_id, :alias]) - end -end diff --git a/priv/old_repo/migrations/20240228024349_create_server_settings.exs b/priv/old_repo/migrations/20240228024349_create_server_settings.exs deleted file mode 100644 index 314b1579..00000000 --- a/priv/old_repo/migrations/20240228024349_create_server_settings.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateServerSettings do - use Ecto.Migration - - def change do - create table(:server_settings, primary_key: false) do - add :guild_id, :string, primary_key: true, size: 22 - add :data, :map - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20240301011012_add_system_tag.exs b/priv/old_repo/migrations/20240301011012_add_system_tag.exs deleted file mode 100644 index 50e46fea..00000000 --- a/priv/old_repo/migrations/20240301011012_add_system_tag.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddSystemTag do - use Ecto.Migration - - def change do - alter table(:users) do - add :system_tag, :string, default: nil - add :show_system_tag, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240414001800_extend_journal_entries.exs b/priv/old_repo/migrations/20240414001800_extend_journal_entries.exs deleted file mode 100644 index ebd8d38a..00000000 --- a/priv/old_repo/migrations/20240414001800_extend_journal_entries.exs +++ /dev/null @@ -1,55 +0,0 @@ -defmodule Octocon.Repo.Migrations.ExtendJournalEntries do - use Ecto.Migration - - def change do - drop_if_exists table(:global_journal_alters) - drop_if_exists table(:global_journals) - drop_if_exists table(:alter_journals) - - create table(:global_journals, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :title, :text - add :content, :text - add :color, :string, size: 7 - - timestamps() - end - - create table(:global_journal_alters, primary_key: false) do - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :global_journal_id, - references(:global_journals, type: :uuid, on_delete: :delete_all) - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - end - - create index(:global_journal_alters, [:user_id]) - create unique_index(:global_journal_alters, [:global_journal_id, :alter_id]) - - create table(:alter_journals, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - - add :title, :text - add :content, :text, default: "" - add :color, :string, size: 7 - - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20240414083021_test1.exs b/priv/old_repo/migrations/20240414083021_test1.exs deleted file mode 100644 index e5b75b58..00000000 --- a/priv/old_repo/migrations/20240414083021_test1.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Migrations.Test1 do - use Ecto.Migration - - def change do - drop_if_exists table(:global_journal_alters) - drop_if_exists table(:global_journals) - drop_if_exists table(:alter_journals) - end -end diff --git a/priv/old_repo/migrations/20240414083024_test2.exs b/priv/old_repo/migrations/20240414083024_test2.exs deleted file mode 100644 index 7c82768a..00000000 --- a/priv/old_repo/migrations/20240414083024_test2.exs +++ /dev/null @@ -1,55 +0,0 @@ -defmodule Octocon.Repo.Migrations.Test2 do - use Ecto.Migration - - def change do - drop_if_exists table(:global_journal_alters) - drop_if_exists table(:global_journals) - drop_if_exists table(:alter_journals) - - create table(:global_journals, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :title, :text - add :content, :text - add :color, :string, size: 7 - - timestamps() - end - - create table(:global_journal_alters, primary_key: false) do - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :global_journal_id, - references(:global_journals, type: :uuid, on_delete: :delete_all) - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - end - - create index(:global_journal_alters, [:user_id]) - create unique_index(:global_journal_alters, [:global_journal_id, :alter_id]) - - create table(:alter_journals, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - - add :title, :text - add :content, :text, default: "" - add :color, :string, size: 7 - - timestamps() - end - end -end diff --git a/priv/old_repo/migrations/20240605175443_create_tags.exs b/priv/old_repo/migrations/20240605175443_create_tags.exs deleted file mode 100644 index 2d6e81bd..00000000 --- a/priv/old_repo/migrations/20240605175443_create_tags.exs +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Octocon.Repo.Migrations.CreateTags do - use Ecto.Migration - - def change do - create table(:tags, primary_key: false) do - add :id, :uuid, primary_key: true - - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :name, :string - add :color, :string, size: 7 - - timestamps() - end - - create index(:tags, [:user_id]) - - create table(:alter_tags, primary_key: false) do - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7 - - add :tag_id, - references(:tags, type: :uuid, on_delete: :delete_all) - - add :alter_id, - references(:alters, type: :int2, on_delete: :delete_all, with: [user_id: :user_id]) - end - - create index(:alter_tags, [:user_id]) - create unique_index(:alter_tags, [:tag_id, :alter_id]) - - # Missed in test2 - create_if_not_exists index(:global_journals, [:user_id]) - end -end diff --git a/priv/old_repo/migrations/20240611215409_add_tag_privacy.exs b/priv/old_repo/migrations/20240611215409_add_tag_privacy.exs deleted file mode 100644 index c6e17069..00000000 --- a/priv/old_repo/migrations/20240611215409_add_tag_privacy.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddTagPrivacy do - use Ecto.Migration - - def change do - alter table(:tags) do - add :security_level, :int2, default: 3 - end - end -end diff --git a/priv/old_repo/migrations/20240615035032_add_fly_postgres_proc.exs b/priv/old_repo/migrations/20240615035032_add_fly_postgres_proc.exs deleted file mode 100644 index a4e6a6c9..00000000 --- a/priv/old_repo/migrations/20240615035032_add_fly_postgres_proc.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddFlyPostgresProc do - use Ecto.Migration - - def up do - Fly.Postgres.Migrations.V01.up() - end - - def down do - Fly.Postgres.Migrations.V01.down() - end -end diff --git a/priv/old_repo/migrations/20240616005026_refactor_proxy_cache.exs b/priv/old_repo/migrations/20240616005026_refactor_proxy_cache.exs deleted file mode 100644 index 5ef881c1..00000000 --- a/priv/old_repo/migrations/20240616005026_refactor_proxy_cache.exs +++ /dev/null @@ -1,20 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.RefactorProxyCache do - use Ecto.Migration - - def up do - drop_if_exists table(:pc_items) - - create table(:proxy_cache_items, primary_key: false) do - add :user_id, - references(:users, type: :string, on_delete: :delete_all), - size: 7, - primary_key: true - - add :data, :binary - end - end - - def down do - drop table(:proxy_cache_items) - end -end diff --git a/priv/old_repo/migrations/20240620210752_add_tag_descriptions.exs b/priv/old_repo/migrations/20240620210752_add_tag_descriptions.exs deleted file mode 100644 index bf9a9244..00000000 --- a/priv/old_repo/migrations/20240620210752_add_tag_descriptions.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddTagDescriptions do - use Ecto.Migration - - def change do - alter table(:tags) do - add :description, :string, default: nil - end - end -end diff --git a/priv/old_repo/migrations/20240625125654_add_case_insensitive_proxying.exs b/priv/old_repo/migrations/20240625125654_add_case_insensitive_proxying.exs deleted file mode 100644 index 2b802da7..00000000 --- a/priv/old_repo/migrations/20240625125654_add_case_insensitive_proxying.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddCaseInsensitiveProxying do - use Ecto.Migration - - def change do - alter table(:users) do - add :case_insensitive_proxying, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240626042819_add_show_proxy_pronouns.exs b/priv/old_repo/migrations/20240626042819_add_show_proxy_pronouns.exs deleted file mode 100644 index 05803667..00000000 --- a/priv/old_repo/migrations/20240626042819_add_show_proxy_pronouns.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddShowProxyPronouns do - use Ecto.Migration - - def change do - alter table(:users) do - add :show_proxy_pronouns, :boolean, default: true - end - end -end diff --git a/priv/old_repo/migrations/20240629191945_add_alter_proxy_name.exs b/priv/old_repo/migrations/20240629191945_add_alter_proxy_name.exs deleted file mode 100644 index 0ea15e4b..00000000 --- a/priv/old_repo/migrations/20240629191945_add_alter_proxy_name.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddAlterProxyName do - use Ecto.Migration - - def change do - alter table(:alters) do - add :proxy_name, :text, default: nil - end - - alter table(:users) do - add :ids_as_proxies, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240703003114_add_system_descriptions.exs b/priv/old_repo/migrations/20240703003114_add_system_descriptions.exs deleted file mode 100644 index ca42b355..00000000 --- a/priv/old_repo/migrations/20240703003114_add_system_descriptions.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddSystemDescriptions do - use Ecto.Migration - - def change do - alter table(:users) do - add :description, :string, default: nil - end - end -end diff --git a/priv/old_repo/migrations/20240703174705_add_latched_alter.exs b/priv/old_repo/migrations/20240703174705_add_latched_alter.exs deleted file mode 100644 index 7a8506d6..00000000 --- a/priv/old_repo/migrations/20240703174705_add_latched_alter.exs +++ /dev/null @@ -1,16 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddLatchedAlter do - use Ecto.Migration - - def change do - alter table(:users) do - add :latched_alter, - references(:alters, - type: :int2, - column: :id, - on_delete: :nilify_all, - with: [id: :user_id] - ), - default: nil - end - end -end diff --git a/priv/old_repo/migrations/20240703220938_refactor_proxy_cache_again.exs b/priv/old_repo/migrations/20240703220938_refactor_proxy_cache_again.exs deleted file mode 100644 index 47ff912a..00000000 --- a/priv/old_repo/migrations/20240703220938_refactor_proxy_cache_again.exs +++ /dev/null @@ -1,12 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.RefactorProxyCacheAgain do - use Ecto.Migration - - def change do - drop_if_exists table(:proxy_cache_items) - - create table(:proxy_cache_items, primary_key: false) do - add :discord_id, :string, primary_key: true - add :data, :binary - end - end -end diff --git a/priv/old_repo/migrations/20240717062657_add_parent_tag.exs b/priv/old_repo/migrations/20240717062657_add_parent_tag.exs deleted file mode 100644 index 3f0355ed..00000000 --- a/priv/old_repo/migrations/20240717062657_add_parent_tag.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddParentTag do - use Ecto.Migration - - def change do - alter table(:tags) do - add :parent_tag_id, references(:tags, on_delete: :nilify_all, type: :uuid), default: nil - end - end -end diff --git a/priv/old_repo/migrations/20240721083935_use_text_type.exs b/priv/old_repo/migrations/20240721083935_use_text_type.exs deleted file mode 100644 index 222aa0a0..00000000 --- a/priv/old_repo/migrations/20240721083935_use_text_type.exs +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.UseTextType do - use Ecto.Migration - - def change do - alter table(:users) do - modify :description, :text - modify :discord_id, :text - modify :username, :text - modify :system_tag, :text - end - - alter table(:tags) do - modify :description, :text - modify :name, :text - end - - alter table(:alters) do - modify :discord_proxies, {:array, :text} - end - - alter table(:fronts) do - modify :comment, :text - end - - alter table(:proxy_cache_items) do - modify :discord_id, :text - end - - alter table(:notification_tokens) do - modify :token, :text - end - - alter table(:channel_blacklists) do - modify :channel_id, :text - modify :guild_id, :text - end - end -end diff --git a/priv/old_repo/migrations/20240730010902_add_journal_entry_pins.exs b/priv/old_repo/migrations/20240730010902_add_journal_entry_pins.exs deleted file mode 100644 index 6acd789c..00000000 --- a/priv/old_repo/migrations/20240730010902_add_journal_entry_pins.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddJournalEntryPins do - use Ecto.Migration - - def change do - alter table(:global_journals) do - add :pinned, :boolean, default: false - end - - alter table(:alter_journals) do - add :pinned, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240731223317_add_field_and_journal_locks.exs b/priv/old_repo/migrations/20240731223317_add_field_and_journal_locks.exs deleted file mode 100644 index b2bc9e9c..00000000 --- a/priv/old_repo/migrations/20240731223317_add_field_and_journal_locks.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddFieldAndJournalLocks do - use Ecto.Migration - - def change do - alter table(:global_journals) do - add :locked, :boolean, default: false - end - - alter table(:alter_journals) do - add :locked, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240810084911_create_polls.exs b/priv/old_repo/migrations/20240810084911_create_polls.exs deleted file mode 100644 index 7abe9175..00000000 --- a/priv/old_repo/migrations/20240810084911_create_polls.exs +++ /dev/null @@ -1,19 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.CreatePolls do - use Ecto.Migration - - def change do - create table(:polls, primary_key: false) do - add :id, :uuid, primary_key: true - add :user_id, references(:users, type: :string, on_delete: :delete_all), size: 7 - add :title, :text - - add :type, :int2 - add :data, :map - add :time_end, :utc_datetime - - timestamps() - end - - create index(:polls, [:user_id]) - end -end diff --git a/priv/old_repo/migrations/20240825235315_add_poll_descriptions.exs b/priv/old_repo/migrations/20240825235315_add_poll_descriptions.exs deleted file mode 100644 index 0279d122..00000000 --- a/priv/old_repo/migrations/20240825235315_add_poll_descriptions.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddPollDescriptions do - use Ecto.Migration - - def change do - alter table(:polls) do - add :description, :text - end - end -end diff --git a/priv/old_repo/migrations/20240829090620_add_user_salt.exs b/priv/old_repo/migrations/20240829090620_add_user_salt.exs deleted file mode 100644 index 4121297b..00000000 --- a/priv/old_repo/migrations/20240829090620_add_user_salt.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddUserSalt do - use Ecto.Migration - - def change do - alter table(:users) do - add :salt, :text - end - end -end diff --git a/priv/old_repo/migrations/20240830010234_add_encryption_initialized_field.exs b/priv/old_repo/migrations/20240830010234_add_encryption_initialized_field.exs deleted file mode 100644 index 48244ea5..00000000 --- a/priv/old_repo/migrations/20240830010234_add_encryption_initialized_field.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddEncryptionInitializedField do - use Ecto.Migration - - def change do - alter table(:users) do - add :encryption_initialized, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20240830152916_add_encryption_key_checksum.exs b/priv/old_repo/migrations/20240830152916_add_encryption_key_checksum.exs deleted file mode 100644 index 63dfa365..00000000 --- a/priv/old_repo/migrations/20240830152916_add_encryption_key_checksum.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddEncryptionKeyChecksum do - use Ecto.Migration - - def change do - alter table(:users) do - add :encryption_key_checksum, :text - end - end -end diff --git a/priv/old_repo/migrations/20241203001127_add_untracked_and_archived_fields.exs b/priv/old_repo/migrations/20241203001127_add_untracked_and_archived_fields.exs deleted file mode 100644 index fa95c8e1..00000000 --- a/priv/old_repo/migrations/20241203001127_add_untracked_and_archived_fields.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddUntrackedAndArchivedFields do - use Ecto.Migration - - def change do - alter table(:alters) do - add :untracked, :boolean, default: false - add :archived, :boolean, default: false - end - end -end diff --git a/priv/old_repo/migrations/20250215075758_add_alter_pins_and_last_front.exs b/priv/old_repo/migrations/20250215075758_add_alter_pins_and_last_front.exs deleted file mode 100644 index d57e5d6f..00000000 --- a/priv/old_repo/migrations/20250215075758_add_alter_pins_and_last_front.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddAlterPinsAndLastFront do - use Ecto.Migration - - def change do - alter table(:alters) do - add :pinned, :boolean, default: false - add :last_fronted, :utc_datetime, default: nil - end - end -end diff --git a/priv/old_repo/migrations/20250331233214_add_auth_ids.exs b/priv/old_repo/migrations/20250331233214_add_auth_ids.exs deleted file mode 100644 index 062c3902..00000000 --- a/priv/old_repo/migrations/20250331233214_add_auth_ids.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddAuthIds do - use Ecto.Migration - - def change do - alter table(:users) do - add :apple_id, :text, default: nil - add :google_id, :text, default: nil - end - - create unique_index(:users, [:apple_id]) - create unique_index(:users, [:google_id]) - end -end diff --git a/priv/old_repo/migrations/20250406075229_consolidate_discord_settings.exs b/priv/old_repo/migrations/20250406075229_consolidate_discord_settings.exs deleted file mode 100644 index 3c142cd2..00000000 --- a/priv/old_repo/migrations/20250406075229_consolidate_discord_settings.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.ConsolidateDiscordSettings do - use Ecto.Migration - - def change do - alter table(:users) do - add :discord_settings, :map, default: %{} - end - end -end diff --git a/priv/old_repo/migrations/20250406093520_nuke_proxy_cache.exs b/priv/old_repo/migrations/20250406093520_nuke_proxy_cache.exs deleted file mode 100644 index 47dc7363..00000000 --- a/priv/old_repo/migrations/20250406093520_nuke_proxy_cache.exs +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.NukeProxyCache do - use Ecto.Migration - - def change do - drop_if_exists table(:proxy_cache_items) - end -end diff --git a/priv/old_repo/migrations/20250407030926_nuke_old_discord_fields.exs b/priv/old_repo/migrations/20250407030926_nuke_old_discord_fields.exs deleted file mode 100644 index eef8ab1a..00000000 --- a/priv/old_repo/migrations/20250407030926_nuke_old_discord_fields.exs +++ /dev/null @@ -1,16 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.NukeOldDiscordFields do - use Ecto.Migration - - def change do - alter table(:users) do - remove :system_tag - remove :show_system_tag - remove :case_insensitive_proxying - remove :show_proxy_pronouns - remove :ids_as_proxies - remove :latched_alter - remove :autoproxy_mode - remove :last_proxy_id - end - end -end diff --git a/priv/old_repo/migrations/20250407031804_readd_autoproxy_mode_temporarily.exs b/priv/old_repo/migrations/20250407031804_readd_autoproxy_mode_temporarily.exs deleted file mode 100644 index d1c741d8..00000000 --- a/priv/old_repo/migrations/20250407031804_readd_autoproxy_mode_temporarily.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.ReaddAutoproxyModeTemporarily do - use Ecto.Migration - - def change do - alter table(:users) do - add :autoproxy_mode, :integer - end - end -end diff --git a/priv/old_repo/migrations/20250407032929_remove_autoproxy_mode_again.exs b/priv/old_repo/migrations/20250407032929_remove_autoproxy_mode_again.exs deleted file mode 100644 index 438ef55a..00000000 --- a/priv/old_repo/migrations/20250407032929_remove_autoproxy_mode_again.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.RemoveAutoproxyModeAgain do - use Ecto.Migration - - def change do - alter table(:users) do - remove :autoproxy_mode - end - end -end diff --git a/priv/old_repo/migrations/20250515120053_add_unique_notification_token_index.exs b/priv/old_repo/migrations/20250515120053_add_unique_notification_token_index.exs deleted file mode 100644 index 934dffe1..00000000 --- a/priv/old_repo/migrations/20250515120053_add_unique_notification_token_index.exs +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Octocon.Repo.Local.Migrations.AddUniqueNotificationTokenIndex do - use Ecto.Migration - - def change do - create unique_index(:notification_tokens, [:token]) - end -end diff --git a/priv/old_repo/seeds.exs b/priv/old_repo/seeds.exs deleted file mode 100644 index 18227039..00000000 --- a/priv/old_repo/seeds.exs +++ /dev/null @@ -1,11 +0,0 @@ -# Script for populating the database. You can run it as: -# -# mix run priv/repo/seeds.exs -# -# Inside the script, you can read and write to any of your -# repositories directly: -# -# Octocon.Repo.insert!(%Octocon.SomeSchema{}) -# -# We recommend using the bang functions (`insert!`, `update!` -# and so on) as they will fail if something goes wrong. diff --git a/priv/repo/migrations/20250614215843_init.exs b/priv/repo/migrations/20250614215843_init.exs deleted file mode 100644 index 6971f103..00000000 --- a/priv/repo/migrations/20250614215843_init.exs +++ /dev/null @@ -1,455 +0,0 @@ -defmodule Octocon.Repo.Migrations.Init do - use Ecto.Migration - - @keyspaces [ - "nam", - "eur", - "ocn", - "eas", - "sam", - "sas", - "gdpr" - ] - - def change do - for keyspace <- @keyspaces do - create_users_table(keyspace) - create_alters_table(keyspace) - - create_tags_table(keyspace) - create_alter_tags_table(keyspace) - - create_global_journals_table(keyspace) - create_global_journal_alters_table(keyspace) - create_alter_journals_table(keyspace) - - create_polls_table(keyspace) - - create_fronts_tables(keyspace) - create_fronts_by_alter_view(keyspace) - create_fronts_by_time_views(keyspace) - - # Execute the commands on the current keyspace before moving on to the next one - flush() - end - - create_user_registry_table() - create_notification_tokens_table() - create_channel_blacklists_table() - create_server_settings_table() - - create_friendships_table() - create_friend_requests_table() - - create_metrics_table() - end - - ### REGIONAL TABLES ### - - def create_users_table(keyspace) do - create_udt(keyspace, "discord_server_settings", """ - guild_id text, - proxying_disabled boolean, - autoproxy_mode smallint, - latched_alter int - """) - - create_udt(keyspace, "discord_settings", """ - system_tag text, - show_system_tag boolean, - - case_insensitive_proxies boolean, - show_pronouns boolean, - ids_as_proxies boolean, - silent_proxying boolean, - use_proxy_delay boolean, - - global_autoproxy_mode smallint, - global_latched_alter int, - - server_settings list> - """) - - create_udt(keyspace, "field", """ - id uuid, - name text, - type smallint, - locked boolean, - security_level smallint - """) - - create_table(keyspace, "users", """ - id text, - email text, - discord_id text, - apple_id text, - google_id text, - username text, - - description text, - avatar_url text, - - lifetime_alter_count int, - primary_front int, - - last_proxy_id smallint, - discord_settings frozen<#{keyspace}.discord_settings>, - - fields list>, - - salt text, - encryption_initialized boolean, - encryption_key_checksum text, - - inserted_at timestamp, - updated_at timestamp - """, "id") - - create_index(keyspace, "users", "discord_id") - create_index(keyspace, "users", "email") - create_index(keyspace, "users", "username") - create_index(keyspace, "users", "apple_id") - create_index(keyspace, "users", "google_id") - end - - def create_alters_table(keyspace) do - create_udt(keyspace, "alter_field", """ - id uuid, - value text - """) - - table_name = "alters" - create_table(keyspace, table_name, """ - id smallint, - user_id text, - alias text, - - name text, - pronouns text, - description text, - - avatar_url text, - security_level smallint, - extra_images list, - color text, - - discord_proxies list, - proxy_name text, - - fields list>, - - untracked boolean, - archived boolean, - pinned boolean, - last_fronted timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id") - - create_raw_index(keyspace, table_name, "alters_by_alias", "(user_id), alias") - end - - def create_tags_table(keyspace) do - create_table(keyspace, "tags", """ - id uuid, - user_id text, - parent_tag_id uuid, - - name text, - description text, - color text, - security_level smallint, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id") - end - - def create_alter_tags_table(keyspace) do - create_table(keyspace, "alter_tags", """ - user_id text, - alter_id smallint, - tag_id uuid, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, tag_id, alter_id") - - create_raw_index(keyspace, "alter_tags", "alter_tags_by_alter", "(user_id), alter_id") - end - - def create_polls_table(keyspace) do - create_table(keyspace, "polls", """ - id uuid, - user_id text, - - title text, - description text, - type smallint, - data text, - - time_end timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id") - end - - def create_global_journals_table(keyspace) do - create_table(keyspace, "global_journals", """ - id uuid, - user_id text, - - title text, - content text, - color text, - - pinned boolean, - locked boolean, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id") - end - - def create_global_journal_alters_table(keyspace) do - create_table(keyspace, "global_journal_alters", """ - user_id text, - global_journal_id uuid, - alter_id smallint - """, "user_id, global_journal_id, alter_id") - end - - def create_alter_journals_table(keyspace) do - create_table(keyspace, "alter_journals", """ - id uuid, - user_id text, - alter_id smallint, - - title text, - content text, - color text, - - pinned boolean, - locked boolean, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id, alter_id") - - create_raw_index(keyspace, "alter_journals", "alter_journals_by_alter", "(user_id), alter_id") - end - - def create_alter_journals_by_alter_view(keyspace) do - execute """ - CREATE MATERIALIZED VIEW IF NOT EXISTS #{keyspace}.alter_journals_by_alter AS - SELECT * FROM #{keyspace}.alter_journals - WHERE user_id IS NOT NULL AND alter_id IS NOT NULL AND id IS NOT NULL - PRIMARY KEY (user_id, alter_id, id) - """, "DROP MATERIALIZED VIEW IF EXISTS #{keyspace}.alter_journals_by_alter" - end - - def create_fronts_tables(keyspace) do - create_table(keyspace, "fronts", """ - id uuid, - user_id text, - alter_id smallint, - - comment text, - - time_start timestamp, - time_end timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, id, time_start") - - create_table(keyspace, "current_fronts", """ - id uuid, - user_id text, - alter_id smallint, - - comment text, - - time_start timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, alter_id") - end - - def create_fronts_by_alter_view(keyspace) do - execute """ - CREATE MATERIALIZED VIEW IF NOT EXISTS #{keyspace}.fronts_by_alter AS - SELECT * FROM #{keyspace}.fronts - WHERE user_id IS NOT NULL AND alter_id IS NOT NULL AND id IS NOT NULL AND time_start IS NOT NULL - PRIMARY KEY (user_id, alter_id, id, time_start) - """, "DROP MATERIALIZED VIEW IF EXISTS #{keyspace}.fronts_by_alter" - end - - def create_fronts_by_time_views(keyspace) do - execute """ - CREATE MATERIALIZED VIEW IF NOT EXISTS #{keyspace}.fronts_by_time AS - SELECT * FROM #{keyspace}.fronts - WHERE user_id IS NOT NULL AND time_start IS NOT NULL AND time_end IS NOT NULL AND id IS NOT NULL - PRIMARY KEY (user_id, time_start, time_end, id) - """, "DROP MATERIALIZED VIEW IF EXISTS #{keyspace}.fronts_by_time" - - execute """ - CREATE MATERIALIZED VIEW IF NOT EXISTS #{keyspace}.fronts_by_end_time AS - SELECT * FROM #{keyspace}.fronts - WHERE user_id IS NOT NULL AND time_start IS NOT NULL AND time_end IS NOT NULL AND id IS NOT NULL - PRIMARY KEY (user_id, time_end, time_start, id) - """, "DROP MATERIALIZED VIEW IF EXISTS #{keyspace}.fronts_by_end_time" - end - - ### PINNED TABLES ### - - def create_user_registry_table do - keyspace = "global" - table_name = "user_registry" - - create_table(keyspace, table_name, """ - user_id text, - discord_id text, - email text, - username text, - apple_id text, - google_id text, - region text, - - inserted_at timestamp, - updated_at timestamp - """, "user_id") - - create_index(keyspace, table_name, "discord_id") - create_index(keyspace, table_name, "email") - create_index(keyspace, table_name, "username") - create_index(keyspace, table_name, "apple_id") - create_index(keyspace, table_name, "google_id") - end - - def create_notification_tokens_table do - keyspace = "global" - table = "notification_tokens" - - create_table(keyspace, table, """ - user_id text, - push_token text, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, push_token") - - create_index(keyspace, "notification_tokens", "push_token") - end - - def create_friendships_table do - keyspace = "global" - - create_table(keyspace, "friendships", """ - user_id text, - friend_id text, - level smallint, - since timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "user_id, friend_id") - end - - def create_friend_requests_table do - keyspace = "global" - table = "friend_requests" - - create_table(keyspace, table, """ - from_id text, - to_id text, - date_sent timestamp, - - inserted_at timestamp, - updated_at timestamp - """, "from_id, to_id") - - create_index(keyspace, table, "to_id") - end - - def create_channel_blacklists_table do - keyspace = "nam" - table = "channel_blacklists" - - create_table(keyspace, table, """ - channel_id text, - guild_id text, - - inserted_at timestamp, - updated_at timestamp - """, "channel_id") - - create_index(keyspace, table, "guild_id") - end - - def create_server_settings_table do - create_udt("nam", "server_settings_data", """ - log_channel text, - force_system_tags boolean, - - proxy_disabled_users list - """) - - keyspace = "nam" - table = "server_settings" - - create_table(keyspace, table, """ - guild_id text, - data frozen<#{keyspace}.server_settings_data>, - - inserted_at timestamp, - updated_at timestamp - """, "guild_id") - end - - def create_metrics_table do - keyspace = "nam_nt" - - create_table(keyspace, "metrics_counts", """ - key text, - value counter - """, "key") - end - - ### UTILS ### - - def create_udt(keyspace, name, fields) when is_binary(keyspace) and is_binary(name) and is_binary(fields) do - execute """ - CREATE TYPE IF NOT EXISTS #{keyspace}.#{name} ( - #{fields} - ) - """, "DROP TYPE IF EXISTS #{keyspace}.#{name}" - end - - def create_table(keyspace, table, fields, primary_key) when is_binary(keyspace) and is_binary(table) do - execute """ - CREATE TABLE IF NOT EXISTS #{keyspace}.#{table} ( - #{fields}, - - PRIMARY KEY (#{primary_key}) - ) - """, "DROP TABLE IF EXISTS #{keyspace}.#{table}" - end - - def create_raw_index(keyspace, table, name, on) when is_binary(keyspace) and is_binary(table) and is_binary(name) and is_binary(on) do - execute """ - CREATE INDEX IF NOT EXISTS #{name} ON #{keyspace}.#{table} (#{on}) - """, "DROP INDEX #{keyspace}.#{name}" - end - - def create_index(keyspace, table, field) when is_binary(keyspace) and is_binary(table) and is_binary(field) do - name = "#{table}_by_#{field}" - - execute """ - CREATE INDEX IF NOT EXISTS #{name} ON #{keyspace}.#{table} (#{field}) - """, "DROP INDEX #{keyspace}.#{name}" - end -end diff --git a/priv/repo/migrations/20260119043444_add_friendships_index.exs b/priv/repo/migrations/20260119043444_add_friendships_index.exs deleted file mode 100644 index a5ad0aa6..00000000 --- a/priv/repo/migrations/20260119043444_add_friendships_index.exs +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Octocon.Repo.Migrations.AddFriendshipsIndex do - use Ecto.Migration - - def change do - create_index("global", "friendships", "friend_id") - end - - def create_index(keyspace, table, field) when is_binary(keyspace) and is_binary(table) and is_binary(field) do - name = "#{table}_by_#{field}" - - execute """ - CREATE INDEX IF NOT EXISTS #{name} ON #{keyspace}.#{table} (#{field}) - """, "DROP INDEX #{keyspace}.#{name}" - end -end diff --git a/priv/static/.well-known/assetlinks.json b/priv/static/.well-known/assetlinks.json deleted file mode 100644 index 81c8262b..00000000 --- a/priv/static/.well-known/assetlinks.json +++ /dev/null @@ -1,14 +0,0 @@ -[ - { - "relation": [ - "delegate_permission/common.handle_all_urls" - ], - "target": { - "namespace": "android_app", - "package_name": "app.octocon.OctoconApp", - "sha256_cert_fingerprints": [ - "F4:F7:2E:58:8A:08:17:0C:3E:09:74:43:5D:82:AB:50:85:E6:AE:D9:4F:06:D8:19:F3:8F:5B:A4:42:5C:A5:C1" - ] - } - } -] \ No newline at end of file diff --git a/priv/static/cache_manifest.json b/priv/static/cache_manifest.json deleted file mode 100644 index 02890113..00000000 --- a/priv/static/cache_manifest.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "!comment!":"This file was auto-generated by `mix phx.digest`. Remove it and all generated artefacts with `mix phx.digest.clean --all`", - "version":1, - "latest":{"favicon.ico":"favicon-a8ca4e3a2bb8fea46a9ee9e102e7d3eb.ico","robots.txt":"robots-f71d20196d4caf35b6a670db8c70b03d.txt"}, - "digests":{"favicon-a8ca4e3a2bb8fea46a9ee9e102e7d3eb.ico":{"digest":"a8ca4e3a2bb8fea46a9ee9e102e7d3eb","logical_path":"favicon.ico","mtime":63886004309,"sha512":"vCKvNNXeSP/2RRr6IN8PVa8/Hl6ImUO7miAOIMABYwbCzlm0UTRsY30uYb1k5gcCOPIsv6nZuFlJj/h8z+InzQ==","size":1258},"robots-f71d20196d4caf35b6a670db8c70b03d.txt":{"digest":"f71d20196d4caf35b6a670db8c70b03d","logical_path":"robots.txt","mtime":63886004309,"sha512":"Y6NulQ4TS1IX4zx2PoiEDBCgfYDmBX2SuayXUI3n+x+m8Om3Ux4WllcWXqdk6JYzmcttkh/+YHhCWqr+VMBFYw==","size":26}} -} diff --git a/priv/static/favicon.ico b/priv/static/favicon.ico deleted file mode 100644 index 73de524a..00000000 Binary files a/priv/static/favicon.ico and /dev/null differ diff --git a/priv/static/robots.txt b/priv/static/robots.txt deleted file mode 100644 index 1f53798b..00000000 --- a/priv/static/robots.txt +++ /dev/null @@ -1,2 +0,0 @@ -User-agent: * -Disallow: / diff --git a/rel/env.sh.eex b/rel/env.sh.eex deleted file mode 100755 index d8bdb9d9..00000000 --- a/rel/env.sh.eex +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -ip=$(ip -j address show dev tailscale0 | jq -r '.[0].addr_info[] | select(.family=="inet") | .local') -export ERL_AFLAGS="-proto_dist inet_tcp -kernel prevent_overlapping_partitions false" -export ECTO_IPV6="true" -export RELEASE_DISTRIBUTION="name" -export RELEASE_NODE="octo@${ip}" diff --git a/rel/overlays/bin/migrate b/rel/overlays/bin/migrate deleted file mode 100755 index 1aca3ba6..00000000 --- a/rel/overlays/bin/migrate +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -cd -P -- "$(dirname -- "$0")" -exec ./octocon eval Octocon.Release.migrate diff --git a/rel/overlays/bin/migrate.bat b/rel/overlays/bin/migrate.bat deleted file mode 100755 index 1991c287..00000000 --- a/rel/overlays/bin/migrate.bat +++ /dev/null @@ -1 +0,0 @@ -call "%~dp0\octocon" eval Octocon.Release.migrate diff --git a/rel/overlays/bin/server b/rel/overlays/bin/server deleted file mode 100755 index d5707f9f..00000000 --- a/rel/overlays/bin/server +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash -cd -P -- "$(dirname -- "$0")" - -echo "Current user: $(whoami)" - -# If USE_INTEGRATED_TAILSCALE is set, init tailscale -if [ -n "${USE_INTEGRATED_TAILSCALE}" ]; then - echo "USE_INTEGRATED_TAILSCALE is set (Fly.io), initializing Tailscale" - - tailscaled --state=/var/lib/tailscale/tailscaled.state \ - --socket=/var/run/tailscale/tailscaled.sock --no-logs-no-support > /dev/null 2>&1 & - sleep 2 - - tailscale up --auth-key=${TAILSCALE_AUTHKEY} --hostname=octo-${FLY_PROCESS_GROUP}-${FLY_REGION}-${FLY_MACHINE_ID:0:4} -else - echo "USE_INTEGRATED_TAILSCALE is not set; assuming Tailscale is running in a sidecar" - - secrets_ip=$(echo "$OP_CONNECT_HOST" | sed -E 's#http://([^:/]+):.*#\1#') - echo "Ensuring Tailscale is up and secrets service is reachable at [$secrets_ip]..." - - until ping -c 1 -W 1 "$secrets_ip" >/dev/null 2>&1 && \ - ip -j address show dev tailscale0 2>/dev/null | \ - jq -e -r '.[0].addr_info[]? | select(.family=="inet") | .local' | \ - grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' - do - echo "Waiting for [$secrets_ip] to respond..." - sleep 1 - done -fi - -echo "1Password host: ${OP_CONNECT_HOST}" -export OP_CONFIG_DIR="/tmp/.op" - -echo "Switching to user: nobody" - -# PHX_SERVER=true exec ./octocon start -exec su -s /bin/sh nobody -c 'op run --no-masking --env-file="/app/etc/.env" -- ./octocon start' diff --git a/rel/overlays/bin/server-simple b/rel/overlays/bin/server-simple deleted file mode 100755 index 7e96597a..00000000 --- a/rel/overlays/bin/server-simple +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -cd -P -- "$(dirname -- "$0")" -exec ./octocon start diff --git a/rel/overlays/bin/server-simple.bat b/rel/overlays/bin/server-simple.bat deleted file mode 100755 index 3893b470..00000000 --- a/rel/overlays/bin/server-simple.bat +++ /dev/null @@ -1,2 +0,0 @@ -set PHX_SERVER=true -call "%~dp0\octocon" start diff --git a/rel/overlays/etc/.env b/rel/overlays/etc/.env deleted file mode 100644 index 23fe7152..00000000 --- a/rel/overlays/etc/.env +++ /dev/null @@ -1,32 +0,0 @@ -DATABASE_CONTACT_POINTS=op://$OP_ENV/secrets/DATABASE_CONTACT_POINTS -DATABASE_PASSWORD=op://$OP_ENV/secrets/DATABASE_PASSWORD -S3_ACCESS_HOST=op://$OP_ENV/secrets/S3_ACCESS_HOST -APPLE_CLIENT_ID=op://$OP_ENV/secrets/APPLE_CLIENT_ID -ADMIN_USERNAME=op://$OP_ENV/secrets/ADMIN_USERNAME -GRAFANA_HOST=op://$OP_ENV/secrets/GRAFANA_HOST -DISCORD_CLIENT_SECRET=op://$OP_ENV/secrets/DISCORD_CLIENT_SECRET -DISCORD_TOKEN=op://$OP_ENV/secrets/DISCORD_TOKEN -GOOGLE_CLIENT_ID=op://$OP_ENV/secrets/GOOGLE_CLIENT_ID -S3_REGION=op://$OP_ENV/secrets/S3_REGION -DISCORD_CLIENT_ID=op://$OP_ENV/secrets/DISCORD_CLIENT_ID -PORT=op://$OP_ENV/secrets/PORT -GOOGLE_CLIENT_SECRET=op://$OP_ENV/secrets/GOOGLE_CLIENT_SECRET -PRIMARY_NODE_COUNT=op://$OP_ENV/secrets/PRIMARY_NODE_COUNT -SENTRY_DSN=op://$OP_ENV/secrets/SENTRY_DSN -APPLE_PRIVATE_KEY_ID=op://$OP_ENV/secrets/APPLE_PRIVATE_KEY_ID -ADMIN_PASSWORD=op://$OP_ENV/secrets/ADMIN_PASSWORD -S3_ASSET_HOST=op://$OP_ENV/secrets/S3_ASSET_HOST -FCM_SERVICE_ACCOUNT=op://$OP_ENV/secrets/FCM_SERVICE_ACCOUNT -APPLE_PRIVATE_KEY=op://$OP_ENV/secrets/APPLE_PRIVATE_KEY -POOL_SIZE=op://$OP_ENV/secrets/POOL_SIZE -GUARDIAN_SECRET_KEY=op://$OP_ENV/secrets/GUARDIAN_SECRET_KEY -ENCRYPTION_PEPPER=op://$OP_ENV/secrets/ENCRYPTION_PEPPER -LIVE_VIEW_SIGNING_SALT=op://$OP_ENV/secrets/LIVE_VIEW_SIGNING_SALT -SECRET_KEY_BASE=op://$OP_ENV/secrets/SECRET_KEY_BASE -S3_SECRET_ACCESS_KEY=op://$OP_ENV/secrets/S3_SECRET_ACCESS_KEY -S3_HOST=op://$OP_ENV/secrets/S3_HOST -GRAFANA_TOKEN=op://$OP_ENV/secrets/GRAFANA_TOKEN -APPLE_TEAM_ID=op://$OP_ENV/secrets/APPLE_TEAM_ID -S3_ACCESS_KEY_ID=op://$OP_ENV/secrets/S3_ACCESS_KEY_ID -ENCRYPTION_PRIVATE_KEY=op://$OP_ENV/secrets/ENCRYPTION_PRIVATE_KEY -TAILSCALE_API_AUTHKEY=op://$OP_ENV/secrets/TAILSCALE_API_AUTHKEY \ No newline at end of file diff --git a/scripts/create-certs.ps1 b/scripts/create-certs.ps1 new file mode 100644 index 00000000..4a0e1101 --- /dev/null +++ b/scripts/create-certs.ps1 @@ -0,0 +1,50 @@ +# DEPRECATED: Self-hosted Interfold deployments should use the Interfold.Bootstrapper binary +# (tools/Interfold.Bootstrapper) instead. The bootstrapper's CertificatePhase ports this script's +# logic to pure C# (System.Security.Cryptography). This script is retained for one release for +# compatibility with existing developer automation; it will be removed. +# +# Migration: run `dotnet run --project tools/Interfold.Bootstrapper -- bootstrap --config interfold.bootstrap.json` +# from a Linux host. + +param( + [string]$RootName = 'Interfold SS Root CA', + [string]$Domains = 'api.octocon.dev,api.octocon.app', + [string]$OutputPath = '.\certs', + [Parameter(Mandatory=$true)][string]$Password, + [int]$Years = 8 +) + +$SubjectArray = $Domains -split "," +$NotAfter = (Get-Date).AddYears($Years) + +$Certificate = @{ + Extension =[System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($true, $true, 0, $true) + Subject = "CN=$RootName" + NotAfter = $NotAfter + KeyUsage = "CertSign" +} + +$Root = New-SelfSignedCertificate @Certificate +$Cert = New-SelfSignedCertificate -DnsName $SubjectArray -Signer $Root -NotAfter $NotAfter -KeyUsage DigitalSignature, KeyEncipherment + +Write-Host "Certificates created" + +# Move root to store +$MyCertPath = Join-Path Cert:\LocalMachine\My $Root.Thumbprint +Move-Item $MyCertPath -Destination Cert:\LocalMachine\Root + +if (-not (Test-Path $OutputPath -PathType Container)) { + New-Item -ItemType Directory -Path $OutputPath +} + +# Export root as .cer +$RootCertPath = Join-Path Cert:\LocalMachine\Root $Root.Thumbprint +$MovedRoot = Get-ChildItem -Path $RootCertPath +Export-Certificate -Cert $MovedRoot -FilePath (Join-Path $OutputPath "$RootName.cer") + +# Export leaf as .pfx +$mypwd = ConvertTo-SecureString -String $Password -Force -AsPlainText +$LeafCertPath = Join-Path Cert:\LocalMachine\My $Cert.Thumbprint +Export-PfxCertificate -Cert $LeafCertPath -FilePath (Join-Path $OutputPath "$RootName.pfx") -Password $mypwd + +Write-Host "Certificates exported." \ No newline at end of file diff --git a/scripts/create-certs.sh b/scripts/create-certs.sh new file mode 100755 index 00000000..9dab12c5 --- /dev/null +++ b/scripts/create-certs.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# +# DEPRECATED: Self-hosted Interfold deployments should use the Interfold.Bootstrapper binary +# (tools/Interfold.Bootstrapper) instead. The bootstrapper's CertificatePhase generates the +# root CA and leaf cert in pure C# via System.Security.Cryptography and installs them into the +# system trust store. This script is retained for one release for compatibility with existing +# automation; it will be removed. +# +# Migration: `sudo ./interfold-bootstrap bootstrap --config interfold.bootstrap.json` +# +set -e + +# Example usage +# ./create-certs.sh \ +# "$ROOT_NAME" \ +# "$DOMAINS" \ +# "$OUTPUT_PATH" \ +# "$PASSWORD" \ +# "$YEARS" + +# ==== CONFIGURATION ==== + +ROOT_NAME="$1" +DOMAINS="$2" +OUTPUT_PATH="$3" +PASSWORD="$4" +YEARS="$5" + +DOMAINS_ARRAY=(${DOMAINS//,/ }) + +INSTALL_TRUST_STORE=true # set false to skip + +# ==== Internal filenames ==== + +CER_OUTPUT_FILE="${ROOT_NAME}.cer" +PFX_OUTPUT_FILE="${ROOT_NAME}.pfx" +ROOT_KEY="${OUTPUT_PATH}/rootCA.key" +ROOT_CRT="${OUTPUT_PATH}/rootCA.crt" +LEAF_KEY="${OUTPUT_PATH}/leaf.key" +LEAF_CSR="${OUTPUT_PATH}/leaf.csr" +LEAF_CRT="${OUTPUT_PATH}/leaf.crt" + +echo "=== Generating Root CA ===" + +openssl req -x509 -newkey rsa:2048 -days $((YEARS * 365)) \ + -keyout "$ROOT_KEY" \ + -out "$ROOT_CRT" \ + -nodes \ + -subj "/CN=${ROOT_NAME}" \ + -addext "basicConstraints=critical,CA:TRUE,pathlen:0" \ + -addext "keyUsage=critical,keyCertSign" + +echo "Root CA created." + +# ==== Build SAN list ==== +SAN="" +for domain in "${DOMAINS_ARRAY[@]}"; do + SAN+="DNS:${domain}," +done +SAN="${SAN::-1}" + + +echo "=== Generating Leaf TLS Certificate ===" + +openssl req -newkey rsa:2048 -nodes \ + -keyout "$LEAF_KEY" \ + -out "$LEAF_CSR" \ + -subj "/CN=${DOMAINS_ARRAY[0]}" + +openssl x509 -req \ + -in "$LEAF_CSR" \ + -CA "$ROOT_CRT" \ + -CAkey "$ROOT_KEY" \ + -CAcreateserial \ + -out "$LEAF_CRT" \ + -days $((YEARS * 365)) \ + -extfile <(printf "subjectAltName=%s\nkeyUsage=digitalSignature,keyEncipherment" "$SAN") + +echo "Leaf certificate created." + + +# ==== Export ==== +cp "$ROOT_CRT" "$OUTPUT_PATH/$CER_OUTPUT_FILE" + +openssl pkcs12 -export \ + -inkey "$LEAF_KEY" \ + -in "$LEAF_CRT" \ + -certfile "$ROOT_CRT" \ + -out "$OUTPUT_PATH/$PFX_OUTPUT_FILE" \ + -password pass:"$PASSWORD" + +echo "Certificates exported:" +echo " - $CER_OUTPUT_FILE" +echo " - $PFX_OUTPUT_FILE" + + +# ==== Install into Linux system trust store ==== +if [ "$INSTALL_TRUST_STORE" = true ]; then + echo "=== Installing Root CA into System Trust Store ===" + + if [[ -d "/usr/local/share/ca-certificates" ]]; then + # Debian / Ubuntu + cp "$ROOT_CRT" /usr/local/share/ca-certificates/interfold-root-ca.crt + update-ca-certificates + echo "Installed into Debian/Ubuntu trust store." + elif [[ -d "/etc/pki/ca-trust/source/anchors" ]]; then + # RHEL / Fedora / CentOS + cp "$ROOT_CRT" /etc/pki/ca-trust/source/anchors/interfold-root-ca.crt + update-ca-trust extract + echo "Installed into RHEL/Fedora trust store." + else + echo "Unknown distro — skipping trust store install." + fi +fi + +echo "Done!" \ No newline at end of file diff --git a/scripts/docker/ensure-host-aio.sh b/scripts/docker/ensure-host-aio.sh new file mode 100755 index 00000000..cc41057d --- /dev/null +++ b/scripts/docker/ensure-host-aio.sh @@ -0,0 +1,103 @@ +#!/bin/bash +set -euo pipefail + +# Ensures the host AIO limit (fs.aio-max-nr) is sufficient for the configured workload. +# +# Scylla / Seastar refuses to start unless fs.aio-max-nr is high enough to satisfy *every* +# node's reactor reservation. The minimum and recommended values come from Seastar's own +# startup error message ("Set /proc/sys/fs/aio-max-nr to at least 66563 (minimum) or 116562 +# (recommended for networking performance)"), per Scylla node. The required total is therefore +# proportional to how many Scylla nodes will run on the host concurrently — that's why the +# previous static defaults (270667 / 320666, sized for ~4 nodes) silently broke the multi-DC +# integration test fixture (7 nodes need ~870K, not 320K). +# +# Usage: +# ensure-host-aio.sh # default: size for 4 Scylla nodes (legacy) +# ensure-host-aio.sh --scylla-nodes 7 # multi-DC test fixture path +# AIO_SCYLLA_NODES=7 ensure-host-aio.sh # same, via env var (Docker exec friendly) +# AIO_MIN_REQUIRED=600000 AIO_TARGET=900000 \ +# ensure-host-aio.sh # explicit override for advanced operators +# +# Behaviour: +# - Reads /proc/sys/fs/aio-max-nr. +# - If `current >= required`, exits 0 (never lowers the limit; the existing value might be +# deliberately high for unrelated workloads). +# - Otherwise raises it to `target`. The standard error message points at +# `sysctl -w fs.aio-max-nr=$target` if the write fails (e.g. unprivileged container). + +# --- Per-Scylla-node Seastar AIO budget (from Seastar's own startup message). Keep these in +# sync with the constants used by Interfold.Bootstrapper/Phases/PrerequisitesPhase.cs and +# tests/integration/Interfold.IntegrationTests.Shared/TestServices/HostAioPrerequisite.cs. --- +PER_NODE_MIN=66563 +PER_NODE_RECOMMENDED=116562 +HEADROOM=50000 + +# --- Argument parsing (long options only; this script is invoked from CI and from C#, never +# from a hand-typed shell, so no need for short forms). --- +SCYLLA_NODES="${AIO_SCYLLA_NODES:-4}" +while [ $# -gt 0 ]; do + case "$1" in + --scylla-nodes) + SCYLLA_NODES="$2" + shift 2 + ;; + --scylla-nodes=*) + SCYLLA_NODES="${1#--scylla-nodes=}" + shift + ;; + -h|--help) + sed -n '4,30p' "$0" + exit 0 + ;; + *) + echo "ensure-host-aio.sh: unknown argument: $1" >&2 + exit 2 + ;; + esac +done + +if ! [[ "$SCYLLA_NODES" =~ ^[0-9]+$ ]]; then + echo "ensure-host-aio.sh: --scylla-nodes must be a non-negative integer (got '$SCYLLA_NODES')." >&2 + exit 2 +fi + +# `AIO_MIN_REQUIRED` / `AIO_TARGET` overrides take precedence so an operator with an unusual +# topology (e.g. mixing Scylla with another AIO-heavy workload on the same host) can pin +# explicit values without having to model their full graph in --scylla-nodes. +COMPUTED_MIN=$(( SCYLLA_NODES * PER_NODE_MIN + HEADROOM )) +COMPUTED_TARGET=$(( SCYLLA_NODES * PER_NODE_RECOMMENDED + HEADROOM )) +MIN_REQUIRED="${AIO_MIN_REQUIRED:-$COMPUTED_MIN}" +TARGET="${AIO_TARGET:-$COMPUTED_TARGET}" + +AIO_SYSCTL_PATH="/proc/sys/fs/aio-max-nr" + +if [ ! -r "$AIO_SYSCTL_PATH" ]; then + echo "ERROR: Cannot read $AIO_SYSCTL_PATH" + exit 1 +fi + +current="$(cat "$AIO_SYSCTL_PATH")" +echo "ensure-host-aio: scylla-nodes=$SCYLLA_NODES min=$MIN_REQUIRED target=$TARGET current=$current" + +if [ "$current" -ge "$MIN_REQUIRED" ]; then + echo "Host AIO limit already satisfies minimum ($MIN_REQUIRED for $SCYLLA_NODES Scylla node(s))." + exit 0 +fi + +echo "Host AIO limit ($current) is below minimum ($MIN_REQUIRED for $SCYLLA_NODES Scylla node(s)). Raising to $TARGET..." +if echo "$TARGET" > "$AIO_SYSCTL_PATH"; then + updated="$(cat "$AIO_SYSCTL_PATH")" + echo "Updated fs.aio-max-nr: $updated" +else + echo "ERROR: Failed to update fs.aio-max-nr." + echo "Set it manually on the host and retry:" + echo " sudo sysctl -w fs.aio-max-nr=$TARGET" + exit 1 +fi + +if [ "$updated" -lt "$MIN_REQUIRED" ]; then + echo "ERROR: fs.aio-max-nr is still below minimum requirement after update." + exit 1 +fi + +echo "Host AIO tuning complete." diff --git a/scripts/generate-encryption-keypair.ps1 b/scripts/generate-encryption-keypair.ps1 new file mode 100644 index 00000000..eb7b8c1b --- /dev/null +++ b/scripts/generate-encryption-keypair.ps1 @@ -0,0 +1,62 @@ +# DEPRECATED: Self-hosted Interfold deployments should use the Interfold.Bootstrapper binary +# (tools/Interfold.Bootstrapper) instead. The bootstrapper's SecretsPhase ports this script's +# logic to pure C# (System.Security.Cryptography). This script is retained for one release for +# compatibility with existing developer automation; it will be removed. +# +# Migration: run `dotnet run --project tools/Interfold.Bootstrapper -- bootstrap` from a Linux host. + +param( + [string]$OutputDir = (Join-Path $PSScriptRoot "keys"), + [int]$KeySize = 2048, + [switch]$Force +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +if ($KeySize -lt 2048) { + throw "KeySize must be at least 2048." +} + +New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null + +$privatePemPath = Join-Path $OutputDir "encryption-private.pem" +$publicPemPath = Join-Path $OutputDir "encryption-public.pem" +$privateBase64Path = Join-Path $OutputDir "encryption-private.base64.txt" +$envSnippetPath = Join-Path $OutputDir "encryption-env.txt" + +if (-not $Force) { + $existing = @(@($privatePemPath, $publicPemPath, $privateBase64Path, $envSnippetPath) | Where-Object { Test-Path $_ }) + if ($existing.Count -gt 0) { + throw "Output file(s) already exist. Use -Force to overwrite.`n$($existing -join "`n")" + } +} + +$rsa = [System.Security.Cryptography.RSA]::Create($KeySize) +try { + $privatePem = $rsa.ExportRSAPrivateKeyPem() + $publicPem = $rsa.ExportSubjectPublicKeyInfoPem() +} +finally { + $rsa.Dispose() +} + +[System.IO.File]::WriteAllText($privatePemPath, $privatePem, [System.Text.UTF8Encoding]::new($false)) +[System.IO.File]::WriteAllText($publicPemPath, $publicPem, [System.Text.UTF8Encoding]::new($false)) + +$privateBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($privatePem)) +[System.IO.File]::WriteAllText($privateBase64Path, $privateBase64, [System.Text.UTF8Encoding]::new($false)) + +$envSnippet = @( + "# Set this in your secret store or environment" + "ENCRYPTION_PRIVATE_KEY=$privateBase64" +) +[System.IO.File]::WriteAllLines($envSnippetPath, $envSnippet, [System.Text.UTF8Encoding]::new($false)) + +Write-Host "Generated encryption keypair:" -ForegroundColor Green +Write-Host " Private PEM: $privatePemPath" +Write-Host " Public PEM: $publicPemPath" +Write-Host " Private b64: $privateBase64Path" +Write-Host " Env snippet: $envSnippetPath" +Write-Host "" +Write-Host "Use the base64 value for ENCRYPTION_PRIVATE_KEY." -ForegroundColor Yellow diff --git a/scripts/generate-encryption-keypair.sh b/scripts/generate-encryption-keypair.sh new file mode 100755 index 00000000..5b4e86ad --- /dev/null +++ b/scripts/generate-encryption-keypair.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# +# DEPRECATED: Self-hosted Interfold deployments should use the Interfold.Bootstrapper binary +# (tools/Interfold.Bootstrapper) instead. The bootstrapper's SecretsPhase generates the RSA-2048 +# keypair in pure C# via System.Security.Cryptography and persists it in deploy/secrets/secrets.json +# with mode 0600 alongside the other generated credentials. This script is retained for one release +# for compatibility with existing developer automation; it will be removed. +# +# Migration: `sudo ./interfold-bootstrap bootstrap --config interfold.bootstrap.json` +# +set -euo pipefail + +OUTPUT_DIR="${1:-"$(dirname "$0")/keys"}" +KEY_SIZE="${KEY_SIZE:-2048}" +FORCE="${FORCE:-false}" + +if ! command -v openssl >/dev/null 2>&1; then + echo "openssl is required but not found in PATH" >&2 + exit 1 +fi + +if ! [[ "$KEY_SIZE" =~ ^[0-9]+$ ]] || [ "$KEY_SIZE" -lt 2048 ]; then + echo "KEY_SIZE must be an integer >= 2048" >&2 + exit 1 +fi + +mkdir -p "$OUTPUT_DIR" + +PRIVATE_PEM="$OUTPUT_DIR/encryption-private.pem" +PUBLIC_PEM="$OUTPUT_DIR/encryption-public.pem" +PRIVATE_B64="$OUTPUT_DIR/encryption-private.base64.txt" +ENV_SNIPPET="$OUTPUT_DIR/encryption-env.txt" + +if [ "$FORCE" != "true" ]; then + for f in "$PRIVATE_PEM" "$PUBLIC_PEM" "$PRIVATE_B64" "$ENV_SNIPPET"; do + if [ -f "$f" ]; then + echo "Output file already exists: $f" >&2 + echo "Set FORCE=true to overwrite." >&2 + exit 1 + fi + done +fi + +openssl genpkey -algorithm RSA -pkeyopt "rsa_keygen_bits:${KEY_SIZE}" -out "$PRIVATE_PEM" +openssl rsa -in "$PRIVATE_PEM" -pubout -out "$PUBLIC_PEM" + +BASE64_VALUE="$(base64 < "$PRIVATE_PEM" | tr -d '\n')" +printf "%s" "$BASE64_VALUE" > "$PRIVATE_B64" + +cat > "$ENV_SNIPPET" <_admin) with a random password +# 3. Create the application database owned by admin +# 4. Grant DML-only privileges (SELECT, INSERT, UPDATE, DELETE) to app user +# 5. Create the internal.secrets table and grant app user access +# 6. Seed secrets from environment variables into internal.secrets +# 7. Scramble the cluster owner (db_init) password +# +# The container starts with a disposable init superuser 'db_init' (cluster owner). +# We can't fully lock or demote the cluster owner in PostgreSQL, so instead we +# randomize its password. The admin password is generated randomly and stored +# directly in internal.secrets — it is never exposed via output or env vars. +# +# Required environment variables: +# PGUSER - App-level username (will be non-superuser) +# PGPASSWORD - App-level password +# +# Optional environment variables (secrets to seed): +# OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET +# OCTOCON_DISCORD_OAUTH_CLIENT_SECRET +# OCTOCON_ENCRYPTION_PEPPER +# SCYLLA_USER - Scylla app username (derives admin username as _admin) +# SCYLLA_ADMIN_PASSWORD - Scylla admin password (stored in secrets for API) +# +# The container's POSTGRES_PASSWORD (used by pg_init) is passed separately via +# PG_INIT_PASSWORD so we can connect as the cluster owner during bootstrap. +# +# Usage: +# postgres-bootstrap-auth.sh + +DB_HOST="${1:?Usage: postgres-bootstrap-auth.sh }" +DB_NAME="${2:?Usage: postgres-bootstrap-auth.sh }" + +INIT_USER="db_init" +INIT_PASSWORD="${PG_INIT_PASSWORD:?PG_INIT_PASSWORD is required}" +APP_USER="${PGUSER:-postgres}" +APP_PASSWORD="${PGPASSWORD:?PGPASSWORD is required}" +ADMIN_USER="${APP_USER}_admin" + +# --- Guard: reject the well-known default password --- +if [ "$APP_PASSWORD" = "postgres" ]; then + echo "[pg-auth-bootstrap] ========================================" + echo "[pg-auth-bootstrap] ERROR: PGPASSWORD is set to the well-known default ('postgres')." + echo "[pg-auth-bootstrap] This is not allowed — all accounts must use a non-default password." + echo "[pg-auth-bootstrap]" + echo "[pg-auth-bootstrap] To fix, set a secure password in user-secrets:" + echo "[pg-auth-bootstrap] dotnet user-secrets set 'Parameters:postgres-password' ''" + echo "[pg-auth-bootstrap] (run from hosts/Interfold.AppHost)" + echo "[pg-auth-bootstrap] ========================================" + exit 1 +fi + +echo "[pg-auth-bootstrap] Waiting for PostgreSQL on ${DB_HOST}:5432..." +TIMEOUT=90 +ELAPSED=0 +until pg_isready -h "$DB_HOST" -U "$INIT_USER" -d postgres >/dev/null 2>&1; do + sleep 2 + ELAPSED=$((ELAPSED + 2)) + if [ "$ELAPSED" -ge "$TIMEOUT" ]; then + echo "[pg-auth-bootstrap] ERROR: Timed out waiting for PostgreSQL (${TIMEOUT}s)" + exit 1 + fi +done +echo "[pg-auth-bootstrap] PostgreSQL is ready." + +# Helper to run SQL as the app user (works after creation) +psql_as_app() { + PGPASSWORD="$APP_PASSWORD" psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -U "$APP_USER" -d postgres "$@" +} + +# --- Idempotency: check if bootstrap already completed --- +# If the app user can connect AND is non-superuser, bootstrap already ran. +if psql_as_app -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${APP_USER}' AND NOT rolsuper" 2>/dev/null | grep -q 1; then + echo "[pg-auth-bootstrap] App user '${APP_USER}' exists as non-superuser — bootstrap previously completed." + echo "[pg-auth-bootstrap] Bootstrap already complete (idempotent re-run)." + exit 0 +fi + +# --- First-time bootstrap --- + +# Helper to run SQL as the cluster owner (init superuser) +psql_as_init() { + PGPASSWORD="$INIT_PASSWORD" psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -U "$INIT_USER" -d postgres "$@" +} + +# Verify we can connect as the init superuser +if ! psql_as_init -c "SELECT 1" >/dev/null 2>&1; then + echo "[pg-auth-bootstrap] ERROR: Cannot authenticate as '${INIT_USER}'." + echo "[pg-auth-bootstrap] The cluster owner password may have been scrambled from a prior run." + echo "[pg-auth-bootstrap] Try deleting the 'msg_pgdata' volume for a fresh start." + exit 1 +fi + +echo "[pg-auth-bootstrap] Connected as '${INIT_USER}' (cluster owner)." + +# --- Step 1: Create the app user (non-superuser, DML-only) --- +echo "[pg-auth-bootstrap] Creating app user '${APP_USER}' (non-superuser)..." +psql_as_init -c " +DO \$\$ +BEGIN + IF EXISTS (SELECT FROM pg_roles WHERE rolname = '${APP_USER}') THEN + ALTER ROLE \"${APP_USER}\" WITH PASSWORD '${APP_PASSWORD}' NOSUPERUSER LOGIN NOCREATEDB; + RAISE NOTICE 'Role ${APP_USER} already exists, updated.'; + ELSE + CREATE ROLE \"${APP_USER}\" WITH PASSWORD '${APP_PASSWORD}' NOSUPERUSER LOGIN NOCREATEDB; + END IF; +END +\$\$; +" + +# --- Step 2: Create dedicated admin superuser with random password --- +ADMIN_PASS=$(head -c 32 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 32) + +echo "[pg-auth-bootstrap] Creating admin superuser '${ADMIN_USER}'..." +psql_as_init -c " +DO \$\$ +BEGIN + IF EXISTS (SELECT FROM pg_roles WHERE rolname = '${ADMIN_USER}') THEN + ALTER ROLE \"${ADMIN_USER}\" WITH PASSWORD '${ADMIN_PASS}' SUPERUSER LOGIN; + RAISE NOTICE 'Role ${ADMIN_USER} already exists, rotated password.'; + ELSE + CREATE ROLE \"${ADMIN_USER}\" WITH PASSWORD '${ADMIN_PASS}' SUPERUSER LOGIN; + END IF; +END +\$\$; +" + +# --- Step 3: Create the application database owned by admin --- +# The admin owns the database/schema (DDL). The app user gets DML-only. +echo "[pg-auth-bootstrap] Ensuring database '${DB_NAME}' exists..." +if ! psql_as_init -tAc "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'" | grep -q 1; then + psql_as_init -c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${ADMIN_USER}\";" + echo "[pg-auth-bootstrap] Database '${DB_NAME}' created (owner: ${ADMIN_USER})." +else + echo "[pg-auth-bootstrap] Database '${DB_NAME}' already exists." + psql_as_init -c "ALTER DATABASE \"${DB_NAME}\" OWNER TO \"${ADMIN_USER}\";" 2>/dev/null || true +fi + +# --- Step 4: Grant DML-only privileges to app user --- +echo "[pg-auth-bootstrap] Granting DML privileges to '${APP_USER}' on '${DB_NAME}'..." + +# Helper to run SQL against the application database as init +psql_as_init_db() { + PGPASSWORD="$INIT_PASSWORD" psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -U "$INIT_USER" -d "$DB_NAME" "$@" +} + +# Grant CONNECT on the database +psql_as_init -c "GRANT CONNECT ON DATABASE \"${DB_NAME}\" TO \"${APP_USER}\";" + +# Grant USAGE on public schema (no CREATE — can't make new tables) +psql_as_init_db -c "GRANT USAGE ON SCHEMA public TO \"${APP_USER}\";" + +# Grant DML on all current and future tables in public schema +psql_as_init_db -c "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO \"${APP_USER}\";" +psql_as_init_db -c "ALTER DEFAULT PRIVILEGES FOR ROLE \"${ADMIN_USER}\" IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO \"${APP_USER}\";" + +# Grant USAGE on sequences (needed for serial/identity columns if any) +psql_as_init_db -c "GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO \"${APP_USER}\";" +psql_as_init_db -c "ALTER DEFAULT PRIVILEGES FOR ROLE \"${ADMIN_USER}\" IN SCHEMA public GRANT USAGE ON SEQUENCES TO \"${APP_USER}\";" + +# --- Step 5: Create internal.secrets table --- +# Must happen here (before migrations) because PostgresMigrationService reads admin +# credentials from this table via the app user. The DDL and GRANT are also in +# Migrations/000 and 001 as idempotent insurance for non-Aspire environments. +echo "[pg-auth-bootstrap] Creating internal.secrets table..." + +# Helper to run SQL against the application database as admin +psql_as_admin_db() { + PGPASSWORD="$ADMIN_PASS" psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -U "$ADMIN_USER" -d "$DB_NAME" "$@" +} + +psql_as_admin_db -f /scripts/000_create_secrets_table.sql + +# Grant app user read-only access to internal schema and secrets table +psql_as_admin_db -c " +GRANT USAGE ON SCHEMA internal TO \"${APP_USER}\"; +GRANT SELECT ON internal.secrets TO \"${APP_USER}\"; +" + +# --- Step 6: Seed secrets from environment variables --- +echo "[pg-auth-bootstrap] Seeding secrets..." + +upsert_secret() { + local key="$1" + local value="$2" + if [ -z "$value" ]; then + return + fi + # Use psql variables to avoid SQL injection from secret values. + # Variable interpolation (:'var') only works when SQL is read from stdin, not with -c. + psql_as_admin_db \ + -v "secret_key=$key" -v "secret_value=$value" <<'UPSERT_SQL' + INSERT INTO internal.secrets (key, value, created_by, updated_at) + VALUES (:'secret_key', :'secret_value', 'bootstrap', now()) + ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now(); +UPSERT_SQL + echo "[pg-auth-bootstrap] Seeded: $key" +} + +upsert_secret "oauth:google:client_secret" "${OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET:-}" +upsert_secret "oauth:discord:client_secret" "${OCTOCON_DISCORD_OAUTH_CLIENT_SECRET:-}" +upsert_secret "encryption:pepper" "${OCTOCON_ENCRYPTION_PEPPER:-}" + +# Seed admin credentials (these are the ONLY source for migration services) +upsert_secret "postgres:admin_username" "${ADMIN_USER}" +upsert_secret "postgres:admin_password" "${ADMIN_PASS}" +SCYLLA_ADMIN_USER="${SCYLLA_USER:-cassandra}_admin" +upsert_secret "scylla:admin_username" "${SCYLLA_ADMIN_USER}" +upsert_secret "scylla:admin_password" "${SCYLLA_ADMIN_PASSWORD:-}" + +# Seed Scylla connection details (read by app services via ISecretsStore) +upsert_secret "scylla:contact_points" "${SCYLLA_CONTACT_POINTS:-127.0.0.1}" +upsert_secret "scylla:local_datacenter" "${SCYLLA_DATACENTER:-datacenter1}" +upsert_secret "scylla:username" "${SCYLLA_USER:-}" +upsert_secret "scylla:password" "${SCYLLA_PASSWORD:-}" +upsert_secret "scylla:keyspace" "${SCYLLA_KEYSPACE:-nam}" +upsert_secret "scylla:port" "${SCYLLA_PORT:-9042}" + +# --- Step 7: Scramble the cluster owner password --- +INIT_SCRAMBLED_PASS=$(head -c 32 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 32) +echo "[pg-auth-bootstrap] Scrambling cluster owner '${INIT_USER}' password..." +psql_as_init -c "ALTER ROLE \"${INIT_USER}\" WITH PASSWORD '${INIT_SCRAMBLED_PASS}';" + +echo "[pg-auth-bootstrap] ========================================" +echo "[pg-auth-bootstrap] ADMIN ACCOUNT: ${ADMIN_USER}" +echo "[pg-auth-bootstrap] Cluster owner '${INIT_USER}': password scrambled" +echo "[pg-auth-bootstrap] App user '${APP_USER}': DML-only (SELECT/INSERT/UPDATE/DELETE)" +echo "[pg-auth-bootstrap] Secrets table: internal.secrets (seeded)" +echo "[pg-auth-bootstrap] ========================================" +echo "[pg-auth-bootstrap] Bootstrap complete." diff --git a/scripts/postgres/postgres-seed-secrets.sh b/scripts/postgres/postgres-seed-secrets.sh new file mode 100755 index 00000000..54169e55 --- /dev/null +++ b/scripts/postgres/postgres-seed-secrets.sh @@ -0,0 +1,53 @@ +#!/bin/bash +set -euo pipefail + +# postgres-seed-secrets.sh — Upserts secrets from environment variables into internal.secrets. +# +# Standalone utility for re-seeding secrets without re-running full bootstrap. +# Connects using admin credentials provided via environment variables. +# +# Well-known environment variables (all optional — only non-empty values are seeded): +# OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET +# OCTOCON_DISCORD_OAUTH_CLIENT_SECRET +# OCTOCON_APPLE_OAUTH_CLIENT_SECRET +# OCTOCON_ENCRYPTION_PEPPER +# +# Required environment variables: +# PG_ADMIN_USER - Admin username (e.g. octocon_admin) +# PG_ADMIN_PASSWORD - Admin password +# +# Usage: +# postgres-seed-secrets.sh + +DB_HOST="${1:?Usage: postgres-seed-secrets.sh }" +DB_NAME="${2:?Usage: postgres-seed-secrets.sh }" + +DB_USER="${PG_ADMIN_USER:?PG_ADMIN_USER is required}" +DB_PASS="${PG_ADMIN_PASSWORD:?PG_ADMIN_PASSWORD is required}" + +export PGPASSWORD="$DB_PASS" + +echo "[pg-seed-secrets] Seeding secrets into ${DB_NAME} on ${DB_HOST}..." + +upsert_secret() { + local key="$1" + local value="$2" + if [ -z "$value" ]; then + return + fi + # Use psql variables to avoid SQL injection from secret values. + psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" \ + -v "secret_key=$key" -v "secret_value=$value" -c " + INSERT INTO internal.secrets (key, value, created_by, updated_at) + VALUES (:'secret_key', :'secret_value', 'bootstrap', now()) + ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now(); + " + echo "[pg-seed-secrets] Seeded: $key" +} + +upsert_secret "oauth:google:client_secret" "${OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET:-}" +upsert_secret "oauth:discord:client_secret" "${OCTOCON_DISCORD_OAUTH_CLIENT_SECRET:-}" +upsert_secret "oauth:apple:client_secret" "${OCTOCON_APPLE_OAUTH_CLIENT_SECRET:-}" +upsert_secret "encryption:pepper" "${OCTOCON_ENCRYPTION_PEPPER:-}" + +echo "[pg-seed-secrets] Done." diff --git a/scripts/scylla/scylla-bootstrap-auth.sh b/scripts/scylla/scylla-bootstrap-auth.sh new file mode 100755 index 00000000..72c806e0 --- /dev/null +++ b/scripts/scylla/scylla-bootstrap-auth.sh @@ -0,0 +1,138 @@ +#!/bin/bash +set -euo pipefail + +# ScyllaDB/Cassandra authentication bootstrap. +# +# This script runs ONCE to: +# 1. Create a dedicated admin account (_admin) as superuser +# 2. Create the app user (SCYLLA_USER) as non-superuser (least privilege from the start) +# 3. Lock the default 'cassandra' account +# +# The API's migration service handles DDL and permission grants at startup using admin creds. +# Admin credentials are logged to stdout (visible in container logs). +# +# Required environment variables: +# SCYLLA_USER - App-level username (non-superuser) +# SCYLLA_PASSWORD - App-level password +# +# Optional environment variables: +# SCYLLA_ADMIN_PASSWORD - Admin password (defaults to random if not set) +# +# Usage: +# scylla-bootstrap-auth.sh + +DB_HOST="${1:?Usage: scylla-bootstrap-auth.sh }" + +# Default credentials for the built-in account +DEFAULT_USER="cassandra" +DEFAULT_PASSWORD="cassandra" +ADMIN_USER="${SCYLLA_USER}_admin" + +# --- Guard: reject the well-known default password --- +if [ "$SCYLLA_PASSWORD" = "$DEFAULT_PASSWORD" ]; then + echo "[auth-bootstrap] ========================================" + echo "[auth-bootstrap] ERROR: SCYLLA_PASSWORD is set to the well-known default ('${DEFAULT_PASSWORD}')." + echo "[auth-bootstrap] This is not allowed — all accounts must use a non-default password." + echo "[auth-bootstrap]" + echo "[auth-bootstrap] To fix, set a secure password in user-secrets:" + echo "[auth-bootstrap] dotnet user-secrets set 'Parameters:scylla-password' ''" + echo "[auth-bootstrap] (run from hosts/Interfold.AppHost)" + echo "[auth-bootstrap] ========================================" + exit 1 +fi + +echo "[auth-bootstrap] Waiting for CQL on ${DB_HOST}:9042..." +TIMEOUT=90 +ELAPSED=0 + +# Try the app user first (works after previous bootstrap), then fall back to default. +CQL_READY=false +until [ "$CQL_READY" = "true" ]; do + if cqlsh "$DB_HOST" -u "$SCYLLA_USER" -p "$SCYLLA_PASSWORD" -e "DESCRIBE CLUSTER" >/dev/null 2>&1; then + CQL_READY=true + echo "[auth-bootstrap] CQL is ready (connected as '${SCYLLA_USER}')." + # If the app user can already connect, bootstrap was previously completed. + echo "[auth-bootstrap] App user '${SCYLLA_USER}' can authenticate — bootstrap previously completed." + echo "[auth-bootstrap] Bootstrap already complete (idempotent re-run)." + exit 0 + elif cqlsh "$DB_HOST" -u "$DEFAULT_USER" -p "$DEFAULT_PASSWORD" -e "DESCRIBE CLUSTER" >/dev/null 2>&1; then + CQL_READY=true + echo "[auth-bootstrap] CQL is ready (connected as '${DEFAULT_USER}')." + else + sleep 2 + ELAPSED=$((ELAPSED + 2)) + if [ "$ELAPSED" -ge "$TIMEOUT" ]; then + echo "[auth-bootstrap] ERROR: Timed out waiting for CQL (${TIMEOUT}s)" + exit 1 + fi + fi +done + +# --- Step 1: Create dedicated admin superuser --- +ADMIN_PASS="${SCYLLA_ADMIN_PASSWORD:-$(head -c 32 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 32)}" + +ADMIN_EXISTING=$(cqlsh "$DB_HOST" -u "$DEFAULT_USER" -p "$DEFAULT_PASSWORD" \ + -e "LIST ROLES OF '${ADMIN_USER}';" 2>/dev/null || true) + +if echo "$ADMIN_EXISTING" | grep -q "$ADMIN_USER"; then + echo "[auth-bootstrap] Admin user '${ADMIN_USER}' already exists. Updating password..." + cqlsh "$DB_HOST" -u "$DEFAULT_USER" -p "$DEFAULT_PASSWORD" -e \ + "ALTER ROLE '${ADMIN_USER}' WITH PASSWORD = '${ADMIN_PASS}';" +else + echo "[auth-bootstrap] Creating admin superuser '${ADMIN_USER}'..." + cqlsh "$DB_HOST" -u "$DEFAULT_USER" -p "$DEFAULT_PASSWORD" -e \ + "CREATE ROLE '${ADMIN_USER}' WITH PASSWORD = '${ADMIN_PASS}' AND SUPERUSER = true AND LOGIN = true;" +fi + +# --- Step 2: Create the app user (non-superuser from the start) --- +EXISTING=$(cqlsh "$DB_HOST" -u "$DEFAULT_USER" -p "$DEFAULT_PASSWORD" \ + -e "LIST ROLES OF '${SCYLLA_USER}';" 2>/dev/null || true) + +if echo "$EXISTING" | grep -q "$SCYLLA_USER"; then + echo "[auth-bootstrap] App user '${SCYLLA_USER}' already exists." + if [ "$SCYLLA_USER" = "$DEFAULT_USER" ]; then + # The default 'cassandra' account can't demote itself. Use the admin to do it. + echo "[auth-bootstrap] Updating '${SCYLLA_USER}' password and demoting via admin..." + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "ALTER ROLE '${SCYLLA_USER}' WITH PASSWORD = '${SCYLLA_PASSWORD}' AND SUPERUSER = false;" + fi +else + echo "[auth-bootstrap] Creating app user '${SCYLLA_USER}' (non-superuser)..." + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "CREATE ROLE IF NOT EXISTS '${SCYLLA_USER}' WITH PASSWORD = '${SCYLLA_PASSWORD}' AND SUPERUSER = false AND LOGIN = true;" +fi + +# Verify the app user can connect +echo "[auth-bootstrap] Verifying '${SCYLLA_USER}' login..." +if ! cqlsh "$DB_HOST" -u "$SCYLLA_USER" -p "$SCYLLA_PASSWORD" -e "DESCRIBE CLUSTER" >/dev/null 2>&1; then + echo "[auth-bootstrap] ERROR: Cannot authenticate as '${SCYLLA_USER}'. Aborting." + exit 1 +fi + +# --- Step 3: Lock the default 'cassandra' account (unless SCYLLA_USER IS the default) --- +if [ "$SCYLLA_USER" = "$DEFAULT_USER" ]; then + echo "[auth-bootstrap] SCYLLA_USER ('${SCYLLA_USER}') is the default account — skipping lock." + echo "[auth-bootstrap] The app will continue using the default account with the configured password." + CASSANDRA_RANDOM_PASS="(not locked - app user is default account)" +else + CASSANDRA_RANDOM_PASS=$(head -c 32 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 32) + + echo "[auth-bootstrap] Locking default '${DEFAULT_USER}' account..." + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "ALTER ROLE '${DEFAULT_USER}' WITH PASSWORD = '${CASSANDRA_RANDOM_PASS}' AND LOGIN = false;" +fi + +# --- Summary (credentials visible in container logs only) --- +echo "[auth-bootstrap] ========================================" +echo "[auth-bootstrap] ADMIN ACCOUNT: ${ADMIN_USER}" +echo "[auth-bootstrap] ========================================" +if [ "$SCYLLA_USER" = "$DEFAULT_USER" ]; then + echo "[auth-bootstrap] Default '${DEFAULT_USER}' account: ACTIVE (is app user)" +else + echo "[auth-bootstrap] Default '${DEFAULT_USER}' account: LOCKED" +fi +echo "[auth-bootstrap] App user '${SCYLLA_USER}': non-superuser (DML-only, grants managed by API)" +echo "[auth-bootstrap] ========================================" +echo "[auth-bootstrap] Bootstrap complete." +echo "[auth-bootstrap] Schema DDL: handled by API migration service using '${ADMIN_USER}'" +echo "[auth-bootstrap] App runtime: '${SCYLLA_USER}' (non-superuser, DML only)" diff --git a/scripts/scylla/scylla-finalize-auth.sh b/scripts/scylla/scylla-finalize-auth.sh new file mode 100755 index 00000000..0a4a62b0 --- /dev/null +++ b/scripts/scylla/scylla-finalize-auth.sh @@ -0,0 +1,107 @@ +#!/bin/bash +set -euo pipefail + +# ScyllaDB/Cassandra post-schema auth finalization. +# +# This script runs AFTER keyspace/schema loading to demote the app user +# from superuser to non-superuser. This ensures schema init succeeds +# (requires superuser) while the app runs with least privilege at runtime. +# +# Uses the admin account (created by bootstrap) to perform the demotion, +# since Scylla does not allow a role to demote itself. +# +# Required environment variables: +# SCYLLA_USER - App user to demote +# SCYLLA_PASSWORD - App user password +# +# Usage: +# scylla-finalize-auth.sh + +DB_HOST="${1:?Usage: scylla-finalize-auth.sh }" +RECOVERY_DIR="${RECOVERY_PATH:-/recovery}" +ADMIN_USER="${SCYLLA_USER}_admin" + +# --- Load admin credentials from recovery volume --- +ADMIN_CREDS_FILE="${RECOVERY_DIR}/admin-credentials.txt" +if [ ! -f "$ADMIN_CREDS_FILE" ]; then + echo "[auth-finalize] ERROR: Admin credentials file not found at ${ADMIN_CREDS_FILE}." + echo "[auth-finalize] Did scylla-bootstrap-auth run successfully?" + exit 1 +fi + +ADMIN_PASS=$(grep -E "^password=" "$ADMIN_CREDS_FILE" | head -1 | cut -d= -f2-) +if [ -z "${ADMIN_PASS:-}" ]; then + echo "[auth-finalize] ERROR: Could not read admin password from ${ADMIN_CREDS_FILE}." + exit 1 +fi + +echo "[auth-finalize] Connecting to ${DB_HOST} as '${ADMIN_USER}'..." +if ! cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e "DESCRIBE CLUSTER" >/dev/null 2>&1; then + echo "[auth-finalize] ERROR: Cannot authenticate as '${ADMIN_USER}'. Aborting." + exit 1 +fi + +# Check current superuser status +IS_SUPER=$(cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" \ + -e "SELECT is_superuser FROM system_auth.roles WHERE role = '${SCYLLA_USER}';" 2>/dev/null || true) + +if echo "$IS_SUPER" | grep -q "False"; then + echo "[auth-finalize] '${SCYLLA_USER}' is already non-superuser." + # Still ensure permissions are granted (idempotent — GRANT is safe to repeat) + echo "[auth-finalize] Ensuring DML permissions are granted..." + KEYSPACES=$(cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" \ + -e "DESCRIBE KEYSPACES;" 2>/dev/null \ + | tr -s ' ' '\n' \ + | grep -v "^$" \ + | grep -vE "^(system|system_auth|system_distributed|system_traces|system_schema|system_distributed_everywhere)$") + for ks in $KEYSPACES; do + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "GRANT SELECT ON KEYSPACE \"${ks}\" TO '${SCYLLA_USER}';" 2>/dev/null || true + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "GRANT MODIFY ON KEYSPACE \"${ks}\" TO '${SCYLLA_USER}';" 2>/dev/null || true + done + echo "[auth-finalize] Permissions verified. Nothing else to do." + exit 0 +fi + +echo "[auth-finalize] Demoting '${SCYLLA_USER}' to non-superuser..." +cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "ALTER ROLE '${SCYLLA_USER}' WITH SUPERUSER = false;" + +# --- Revoke DDL grants (were needed for keyspace creation, no longer needed) --- +echo "[auth-finalize] Revoking DDL permissions from '${SCYLLA_USER}'..." +cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "REVOKE CREATE ON ALL KEYSPACES FROM '${SCYLLA_USER}';" 2>/dev/null || true +cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "REVOKE ALTER ON ALL KEYSPACES FROM '${SCYLLA_USER}';" 2>/dev/null || true +cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "REVOKE DROP ON ALL KEYSPACES FROM '${SCYLLA_USER}';" 2>/dev/null || true + +# --- Grant least-privilege access to app user on all keyspaces --- +# After demotion, the app user needs explicit permissions (CassandraAuthorizer is enabled). +echo "[auth-finalize] Granting DML permissions to '${SCYLLA_USER}'..." + +# Regional keyspaces + global +KEYSPACES=$(cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" \ + -e "DESCRIBE KEYSPACES;" 2>/dev/null \ + | tr -s ' ' '\n' \ + | grep -v "^$" \ + | grep -vE "^(system|system_auth|system_distributed|system_traces|system_schema|system_distributed_everywhere)$") + +for ks in $KEYSPACES; do + echo "[auth-finalize] GRANT SELECT, MODIFY ON KEYSPACE ${ks} TO '${SCYLLA_USER}'" + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "GRANT SELECT ON KEYSPACE \"${ks}\" TO '${SCYLLA_USER}';" + cqlsh "$DB_HOST" -u "$ADMIN_USER" -p "$ADMIN_PASS" -e \ + "GRANT MODIFY ON KEYSPACE \"${ks}\" TO '${SCYLLA_USER}';" +done + +# Update recovery notes +cat >> "${ADMIN_CREDS_FILE}" 2>/dev/null <Existence-check seam so cross-feature handlers (Journals, Tags) can validate an +/// without depending on Interfold.Alters.Domain. Adapter over +/// IAlterRepository in Interfold.Alters.Domain, registered by +/// AddAltersModule. +public interface IAlterExistenceCheck +{ + Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Alters.Contracts/Abstractions/IAlterFieldDefinitions.cs b/shared/Interfold.Alters.Contracts/Abstractions/IAlterFieldDefinitions.cs new file mode 100644 index 00000000..2209e074 --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Abstractions/IAlterFieldDefinitions.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; + +namespace Interfold.Alters.Contracts.Abstractions; + +/// Viewer-visibility-filtered field-definition set consumed by the Alters read path. +/// Adapter over ISettingsFieldRepository in Interfold.Settings.Domain, registered +/// by AddSettingsModule. +public interface IAlterFieldDefinitions +{ + Task> ListVisibleAsync( + SystemId systemId, + FriendshipLevel? friendshipLevel, + CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Alters.Contracts/AlterCommandResult.cs b/shared/Interfold.Alters.Contracts/AlterCommandResult.cs new file mode 100644 index 00000000..427d58ae --- /dev/null +++ b/shared/Interfold.Alters.Contracts/AlterCommandResult.cs @@ -0,0 +1,9 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.Contracts; + +public sealed record AlterCommandResult(SystemId SystemId, AlterId AlterId, bool Replay) : ICommandResult +{ + public AlterCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Alters.Contracts/Events/AlterDomainEvents.cs b/shared/Interfold.Alters.Contracts/Events/AlterDomainEvents.cs new file mode 100644 index 00000000..c998487f --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Events/AlterDomainEvents.cs @@ -0,0 +1,10 @@ +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.Contracts.Events; + +public sealed record AlterCreatedEvent(ScopedSystemId TargetSystemId, AlterId AlterId) : ITargetedClusterEvent; + +public sealed record AlterUpdatedEvent(ScopedSystemId TargetSystemId, AlterId AlterId) : ITargetedClusterEvent; + +public sealed record AlterDeletedEvent(ScopedSystemId TargetSystemId, AlterId AlterId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Alters.Contracts/Interfold.Alters.Contracts.csproj b/shared/Interfold.Alters.Contracts/Interfold.Alters.Contracts.csproj new file mode 100644 index 00000000..36250df4 --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Interfold.Alters.Contracts.csproj @@ -0,0 +1,13 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Alters.Contracts/Models/Alter.cs b/shared/Interfold.Alters.Contracts/Models/Alter.cs new file mode 100644 index 00000000..76a594d7 --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Models/Alter.cs @@ -0,0 +1,112 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Alters.Contracts.Models; + +public class BareAlter : IAvatarBearing { + public static BareAlter CreatePlaceholder(AlterId id) => new(id, $"Alter {id}", null, null, null, null, null, Array.Empty()); + public BareAlter( + AlterId id, + string name, + AvatarUrl? avatarUrl, + AvatarSource? avatarSource, + HexColor? color, + string? pronouns, + string? description, + IReadOnlyList fields) + { + Id = id; + Name = name; + AvatarUrl = avatarUrl; + AvatarSource = avatarSource; + Color = color; + Pronouns = pronouns; + Fields = fields; + Description = description; + } + + public AlterId Id { get; set; } + public AvatarUrl? AvatarUrl { get; set; } + public AvatarSource? AvatarSource { get; set; } + public HexColor? Color { get; set; } + public string Name { get; set; } + public string? Pronouns { get; set; } + public IReadOnlyList Fields { get; set; } + public string? Description { get; set; } +} + +public sealed class AlterReadModel : BareAlter { + + public AlterReadModel( + AlterId id, + string name, + string? description, + AvatarUrl? avatarUrl, + AvatarSource? avatarSource, + HexColor? color, + string? pronouns, + VisibilityLevel securityLevel, + IReadOnlyList fields, + string? proxyName, + string? alias, + bool? untracked, + bool? archived, + bool? pinned, + IReadOnlyList? discordProxies = null, + DateTime insertedAt = default, + DateTime updatedAt = default) : base(id, name, avatarUrl, avatarSource, color, pronouns, description, fields) + { + Alias = alias; + SecurityLevel = securityLevel; + ProxyName = proxyName; + Untracked = untracked ?? false; + Archived = archived ?? false; + Pinned = pinned ?? false; + DiscordProxies = discordProxies ?? System.Array.Empty(); + InsertedAt = insertedAt; + UpdatedAt = updatedAt; + } + + // The 14-arg ctor above takes `bool?` for the flag trio so the CQL projections in + // ScyllaAlterRepository can bind `row.GetValue(...)` directly. STJ's deserialiser + // refuses that ctor because `bool? untracked` doesn't type-match the `bool Untracked` + // property (STJ requires ctor-param type ↔ property-type equality, case-insensitive name + // match alone isn't enough). That refusal breaks any call-site — production or test — that + // needs to round-trip an AlterReadModel through JSON. This second ctor accepts the flags + // as plain `bool`, marked [JsonConstructor] so STJ picks it and never sees the `bool?` + // form, and forwards to the original so the ??-false coercion stays in one place. + [JsonConstructor] + public AlterReadModel( + AlterId id, + string name, + string? description, + AvatarUrl? avatarUrl, + AvatarSource? avatarSource, + HexColor? color, + string? pronouns, + VisibilityLevel securityLevel, + IReadOnlyList fields, + string? proxyName, + string? alias, + bool untracked, + bool archived, + bool pinned, + IReadOnlyList? discordProxies, + DateTime insertedAt, + DateTime updatedAt) + : this(id, name, description, avatarUrl, avatarSource, color, pronouns, securityLevel, fields, proxyName, alias, (bool?)untracked, (bool?)archived, (bool?)pinned, discordProxies, insertedAt, updatedAt) + { + } + + public string? Alias { get; set; } + public VisibilityLevel SecurityLevel { get; set; } + public string? ProxyName { get; set; } + public bool Untracked { get; set; } + public bool Archived { get; set; } + public bool Pinned { get; set; } + public IReadOnlyList DiscordProxies { get; set; } + public DateTime InsertedAt { get; set; } + public DateTime UpdatedAt { get; set; } +} diff --git a/shared/Interfold.Alters.Contracts/Models/Commands/AlterCommands.cs b/shared/Interfold.Alters.Contracts/Models/Commands/AlterCommands.cs new file mode 100644 index 00000000..68053101 --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Models/Commands/AlterCommands.cs @@ -0,0 +1,38 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.Contracts.Models.Commands; + +public sealed record CreateAlterCommand(string Name, DateTimeOffset CreatedAt); + +// Property-initialiser record shape (rather than positional) so callers only spell out +// the fields they actually want to set — the alter-avatar handlers in AltersController +// were previously passing 13-14 nulls positionally which drowned the meaningful arguments. +// Every remaining positional field is either always required (AlterId, UpdatedAt) or has +// a natural null / false default; nothing forces callers to spell out fields they aren't +// mutating. See PR #12 review comments #7, #8, #9. +public sealed record UpdateAlterCommand +{ + public required AlterId AlterId { get; init; } + public string? Name { get; init; } + public string? Description { get; init; } + public AvatarUrl? AvatarUrl { get; init; } + public AvatarSource? AvatarSource { get; init; } + public HexColor? Color { get; init; } + public string? Pronouns { get; init; } + public VisibilityLevel? SecurityLevel { get; init; } + public IReadOnlyList? Fields { get; init; } + public string? ProxyName { get; init; } + public string? Alias { get; init; } + public bool? Untracked { get; init; } + public bool? Archived { get; init; } + public bool? Pinned { get; init; } + public required DateTimeOffset UpdatedAt { get; init; } + public bool ClearAvatar { get; init; } +} + +public sealed record DeleteAlterCommand(AlterId AlterId); + +// Id references the settings field definition (FieldId). Property name is frozen: +// it serializes as "id" in persisted command JSON and idempotency hashes. +public sealed record AlterFieldCommand(FieldId Id, string? Value); diff --git a/shared/Interfold.Alters.Contracts/Models/Read/AlterRequests.cs b/shared/Interfold.Alters.Contracts/Models/Read/AlterRequests.cs new file mode 100644 index 00000000..040bafea --- /dev/null +++ b/shared/Interfold.Alters.Contracts/Models/Read/AlterRequests.cs @@ -0,0 +1,28 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.Contracts.Models.Read; + +public sealed record CreateAlterRequest( + string Name +); + +public sealed record UpdateAlterRequest( + string? Name = null, + string? Description = null, + AvatarUrl? AvatarUrl = null, + HexColor? Color = null, + string? Pronouns = null, + VisibilityLevel? SecurityLevel = null, + string? ProxyName = null, + string? Alias = null, + bool? Untracked = null, + bool? Archived = null, + bool? Pinned = null, + IReadOnlyList? Fields = null +); + +public sealed record UpdateAlterFieldRequest( + FieldId Id, + string? Value +); diff --git a/shared/Interfold.Alters.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Alters.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..659e2b37 --- /dev/null +++ b/shared/Interfold.Alters.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,11 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Alters.Contracts; + +public sealed record AlterSocketPayload(AlterReadModel Alter) : ISocketPayload; + +public sealed record AlterDeletedSocketPayload(AlterId AlterId) : ISocketPayload; + +public sealed record AlterIdSocketPayload(AlterId? AlterId) : ISocketPayload; diff --git a/shared/Interfold.Alters.Domain/Abstractions/Repository/IAlterRepository.cs b/shared/Interfold.Alters.Domain/Abstractions/Repository/IAlterRepository.cs new file mode 100644 index 00000000..d5ca4d62 --- /dev/null +++ b/shared/Interfold.Alters.Domain/Abstractions/Repository/IAlterRepository.cs @@ -0,0 +1,40 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.Domain.Abstractions.Repository; + +public interface IAlterRepository +{ + Task CreateAsync(SystemId systemId, CreateAlterCommand command, CancellationToken cancellationToken = default); + + Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task UpdateAsync(SystemId systemId, UpdateAlterCommand command, CancellationToken cancellationToken = default); + + Task DeleteAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default + ); + + Task GetAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task GetGuardedAsync( + SystemId systemId, + AlterId alterId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default + ); + + Task AliasTakenByOtherAsync( + SystemId systemId, + AlterId alterId, + string alias, + CancellationToken cancellationToken = default + ); +} \ No newline at end of file diff --git a/shared/Interfold.Alters.Domain/AlterCommandFlow.cs b/shared/Interfold.Alters.Domain/AlterCommandFlow.cs new file mode 100644 index 00000000..8f505365 --- /dev/null +++ b/shared/Interfold.Alters.Domain/AlterCommandFlow.cs @@ -0,0 +1,74 @@ +using Interfold.Alters.Contracts; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Domain; + +internal static class AlterCommandFlow +{ + public static CommandExecutionResult Success( + ScopedSystemId systemId, + AlterId alterId) + => CommandExecutionResult.Success(new AlterCommandResult(systemId, alterId, Replay: false)); + + public static CommandExecutionResult? RejectIfInvalidAlterId( + CommandEnvelope command, + AlterId alterId, + EntityRef invalidAlterIdEntityRef) + => CommandHandler.RejectIfAlterIdOutOfRange(command.OperationId, alterId, invalidAlterIdEntityRef); + + public static async Task?> RejectIfAlterNotFoundAsync( + CommandEnvelope command, + IAlterRepository alterRepository, + AlterId alterId, + EntityRef missingAlterEntityRef, + CancellationToken cancellationToken = default) + { + var exists = await alterRepository.ExistsAsync(command.PrincipalId, alterId, cancellationToken); + return exists + ? null + : CommandHandler.RejectInvariant(command.OperationId, missingAlterEntityRef); + } + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + Func> createAsync, + EntityRef createFailedEntityRef, + Func publishAccepted, + CancellationToken cancellationToken = default) + { + var createdAlterId = await createAsync(cancellationToken); + if (createdAlterId is null) + return CommandHandler.RejectInvariant(command.OperationId, createFailedEntityRef); + + await publishAccepted(createdAlterId.Value, cancellationToken); + return Success(command.PrincipalId, createdAlterId.Value); + } + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failedEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failedEntityRef) + : null; + + public static async Task> ExecuteMutationAsync( + CommandEnvelope command, + AlterId alterId, + Func> mutateAsync, + EntityRef failedEntityRef, + Func publishAccepted, + CancellationToken cancellationToken = default) + { + var succeeded = await mutateAsync(cancellationToken); + if (RejectIfMutationFailed(command, succeeded, failedEntityRef) is { } mutationReject) + return mutationReject; + + await publishAccepted(cancellationToken); + return Success(command.PrincipalId, alterId); + } +} diff --git a/shared/Interfold.Alters.Domain/AlterExistenceCheckAdapter.cs b/shared/Interfold.Alters.Domain/AlterExistenceCheckAdapter.cs new file mode 100644 index 00000000..57322847 --- /dev/null +++ b/shared/Interfold.Alters.Domain/AlterExistenceCheckAdapter.cs @@ -0,0 +1,19 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Domain; + +// see IAlterExistenceCheck for the cycle rationale +public sealed class AlterExistenceCheckAdapter : IAlterExistenceCheck +{ + private readonly IAlterRepository _alterRepository; + + public AlterExistenceCheckAdapter(IAlterRepository alterRepository) + { + _alterRepository = alterRepository; + } + + public Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + => _alterRepository.ExistsAsync(systemId, alterId, cancellationToken); +} diff --git a/shared/Interfold.Alters.Domain/AlterFieldProjection.cs b/shared/Interfold.Alters.Domain/AlterFieldProjection.cs new file mode 100644 index 00000000..487bd0e1 --- /dev/null +++ b/shared/Interfold.Alters.Domain/AlterFieldProjection.cs @@ -0,0 +1,70 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; + +namespace Interfold.Alters.Domain; + +// Backend-agnostic helpers for guarded alter-field projection so InMemory + Scylla repos +// share the visibility rules and can't drift. +public static class AlterFieldProjection +{ + /// Guarded join: emits one per + /// definition the alter has populated. Empty-value definitions are skipped — the + /// guarded path is stricter than the unguarded one. + public static IReadOnlyList ResolveGuardedFields( + IReadOnlyDictionary? alterFieldValues, + IReadOnlyList definitions, + ILogger? logger = null) + { + if (alterFieldValues is null || alterFieldValues.Count == 0 || definitions.Count == 0) + { + return Array.Empty(); + } + + try + { + return definitions + .Where(def => alterFieldValues.ContainsKey(def.Id)) + .Select(def => new AlterPublicFieldReadModel( + def.Id, + def.Name, + def.Type, + alterFieldValues[def.Id])) + .ToArray(); + } + catch (Exception ex) + { + GuardedMetrics.ErrorsTotal.Add(1, + new KeyValuePair("entity_type", "field"), + new KeyValuePair("exception_type", ex.GetType().Name)); + logger?.LogError(ex, + "Guarded field projection failed: definitions={DefinitionCount}, values={ValueCount}", + definitions.Count, alterFieldValues.Count); + throw; + } + } + + /// Owner-view join: emits one per + /// definition, Value = null when the alter didn't populate it. Not viewer-filtered — + /// contrast with which drops unpopulated defs and expects + /// visibility-filtered definitions. + public static IReadOnlyList ResolveOwnerFields( + IReadOnlyDictionary? alterFieldValues, + IReadOnlyList definitions) + { + if (definitions.Count == 0) + { + return Array.Empty(); + } + + return definitions + .Select(def => new AlterPublicFieldReadModel( + def.Id, + def.Name, + def.Type, + alterFieldValues is not null && alterFieldValues.TryGetValue(def.Id, out var value) ? value : null)) + .ToArray(); + } +} diff --git a/shared/Interfold.Alters.Domain/CreateAlterCommandHandler.cs b/shared/Interfold.Alters.Domain/CreateAlterCommandHandler.cs new file mode 100644 index 00000000..a72256d1 --- /dev/null +++ b/shared/Interfold.Alters.Domain/CreateAlterCommandHandler.cs @@ -0,0 +1,46 @@ +using Interfold.Alters.Contracts; +using Interfold.Alters.Contracts.Events; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Domain; + +public sealed class CreateAlterCommandHandler : IdempotentCommandHandler +{ + private readonly IAlterRepository _alterRepository; + private readonly IClusterEventBus _eventBus; + + public CreateAlterCommandHandler( + IAlterRepository alterRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus + ) +:base(idempotencyStore) { + _alterRepository = alterRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.AlterCreate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (RejectIfBlank(command, command.Payload.Name, EntityRefs.AlterName) is { } blankReject) + return blankReject; + + return await AlterCommandFlow.ExecuteCreateAsync( + command, + ct => _alterRepository.CreateAsync(command.PrincipalId, command.Payload, ct), + EntityRefs.AlterCreate, + (alterId, ct) => _eventBus.PublishAsync(new AlterCreatedEvent(command.PrincipalId, alterId), ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Alters.Domain/DeleteAlterCommandHandler.cs b/shared/Interfold.Alters.Domain/DeleteAlterCommandHandler.cs new file mode 100644 index 00000000..8eae0ca5 --- /dev/null +++ b/shared/Interfold.Alters.Domain/DeleteAlterCommandHandler.cs @@ -0,0 +1,60 @@ +using Interfold.Alters.Contracts; +using Interfold.Alters.Contracts.Events; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Domain; + +public sealed class DeleteAlterCommandHandler : IdempotentCommandHandler +{ + private readonly IAlterRepository _alterRepository; + private readonly IJournalAlterCascade _journalCascade; + private readonly IClusterEventBus _eventBus; + + public DeleteAlterCommandHandler( + IAlterRepository alterRepository, + IJournalAlterCascade journalCascade, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _alterRepository = alterRepository; + _journalCascade = journalCascade; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.AlterDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (AlterCommandFlow.RejectIfInvalidAlterId(command, command.Payload.AlterId, EntityRefs.AlterId) is { } rangeReject) + return rangeReject; + + if (await AlterCommandFlow.RejectIfAlterNotFoundAsync(command, _alterRepository, command.Payload.AlterId, EntityRefs.AlterNotFound, cancellationToken) is { } notFoundReject) + return notFoundReject; + + // Cascade BEFORE the alter row itself is removed so a journal-cleanup failure + // leaves the alter intact (caller can retry); the inverse order would orphan the + // journals if the alter delete succeeded but cleanup later threw. DeleteAllForAlterAsync + // also detaches the alter from any global journals it was attached to without + // deleting the global journal itself (multiple alters can share a group journal). + await _journalCascade.DeleteAllForAlterAsync(command.PrincipalId, command.Payload.AlterId, cancellationToken); + + //TODO: Delete alter image if it exists + + return await AlterCommandFlow.ExecuteMutationAsync( + command, + command.Payload.AlterId, + ct => _alterRepository.DeleteAsync(command.PrincipalId, command.Payload.AlterId, ct), + EntityRefs.AlterDeleteFailed, + ct => _eventBus.PublishAsync(new AlterDeletedEvent(command.PrincipalId, command.Payload.AlterId), ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Alters.Domain/Interfold.Alters.Domain.csproj b/shared/Interfold.Alters.Domain/Interfold.Alters.Domain.csproj new file mode 100644 index 00000000..9e62f6d3 --- /dev/null +++ b/shared/Interfold.Alters.Domain/Interfold.Alters.Domain.csproj @@ -0,0 +1,20 @@ + + + net10.0 + enable + enable + + + + + + + + + + diff --git a/shared/Interfold.Alters.Domain/UpdateAlterCommandHandler.cs b/shared/Interfold.Alters.Domain/UpdateAlterCommandHandler.cs new file mode 100644 index 00000000..9e10a675 --- /dev/null +++ b/shared/Interfold.Alters.Domain/UpdateAlterCommandHandler.cs @@ -0,0 +1,94 @@ +using Interfold.Alters.Contracts; +using Interfold.Alters.Contracts.Events; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Alters.Domain; + +public sealed class UpdateAlterCommandHandler : IdempotentCommandHandler +{ + private readonly IAlterRepository _alterRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateAlterCommandHandler( + IAlterRepository alterRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus + ) +:base(idempotencyStore) { + _alterRepository = alterRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.AlterUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (AlterCommandFlow.RejectIfInvalidAlterId(command, command.Payload.AlterId, EntityRefs.AlterId) is { } rangeReject) + return rangeReject; + + if (!HasAnyMutableField(command.Payload)) + { + return RejectInvariant(command, EntityRefs.AlterUpdateNoFields); + } + + // avatar_url and avatar_source must move together. We accept both-set or both-null + // (the ClearAvatar branch handles "set null"); a half-set request is rejected so we + // never silently fall back to a guessed source on the repo side. + if ((command.Payload.AvatarUrl is not null) != (command.Payload.AvatarSource is not null)) + { + return RejectInvariant(command, EntityRefs.AlterAvatarSourceRequired); + } + + if (await AlterCommandFlow.RejectIfAlterNotFoundAsync(command, _alterRepository, command.Payload.AlterId, EntityRefs.AlterNotFound, cancellationToken) is { } notFoundReject) + return notFoundReject; + + if (!string.IsNullOrWhiteSpace(command.Payload.Alias)) + { + var aliasTaken = await _alterRepository.AliasTakenByOtherAsync( + command.PrincipalId, + command.Payload.AlterId, + command.Payload.Alias, + cancellationToken + ); + + if (aliasTaken) + { + return RejectInvariant(command, EntityRefs.AlterAliasTaken); + } + } + + return await AlterCommandFlow.ExecuteMutationAsync( + command, + command.Payload.AlterId, + ct => _alterRepository.UpdateAsync(command.PrincipalId, command.Payload, ct), + EntityRefs.AlterUpdateFailed, + ct => _eventBus.PublishAsync(new AlterUpdatedEvent(command.PrincipalId, command.Payload.AlterId), ct), + cancellationToken); + } + + private static bool HasAnyMutableField(UpdateAlterCommand payload) => + payload.Name is not null || + payload.Description is not null || + payload.AvatarUrl is not null || + payload.AvatarSource is not null || + payload.ClearAvatar || + payload.Color is not null || + payload.Pronouns is not null || + payload.SecurityLevel is not null || + payload.Fields is not null || + payload.ProxyName is not null || + payload.Alias is not null || + payload.Untracked is not null || + payload.Archived is not null || + payload.Pinned is not null; + +} diff --git a/shared/Interfold.Auth.Contracts/Configuration/AuthenticationConfiguration.cs b/shared/Interfold.Auth.Contracts/Configuration/AuthenticationConfiguration.cs new file mode 100644 index 00000000..26423d4a --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Configuration/AuthenticationConfiguration.cs @@ -0,0 +1,65 @@ +using System.ComponentModel.DataAnnotations; + +namespace Interfold.Auth.Contracts.Configuration; + +/// Authentication, OAuth, and JWT configuration. Env-bound (OCTOCON_/GUARDIAN_ +/// prefixes); [Required] secret fields are patched from internal.secrets by +/// AuthenticationSecretsPostConfigure before .ValidateOnStart(). +public sealed class AuthenticationConfiguration +{ + public const string SectionName = "Octocon:Authentication"; + + /// Env: OCTOCON_AUTH_CALLBACK_BASE_URL. + public string? CallbackBaseUrl { get; set; } + + /// Deep-link HMAC signing secret. Sourced from + /// internal.secrets:auth:deep_link_secret. Required. + [Required(AllowEmptyStrings = false)] + public string DeepLinkSecret { get; set; } = string.Empty; + + /// Env: OCTOCON_JWT_AUTHORITY. + public string JwtAuthority { get; set; } = ""; + + /// Env: OCTOCON_JWT_AUDIENCE. + public string JwtAudience { get; set; } = "octocon"; + + /// ES256 private key (PEM, SEC1) for token issuance. Sourced from + /// internal.secrets:auth:jwt_es256_private_pem. Required. + [Required(AllowEmptyStrings = false)] + public string JwtEs256PrivateKeyPem { get; set; } = string.Empty; + + /// ES256 verification keys derived from JwtEs256PrivateKeyPem's public half. + public string[]? JwtEs256VerificationKeyPems { get; set; } + + /// Static server-side E2E-derivation pepper. Sourced from + /// internal.secrets:encryption:pepper. Required. + [Required(AllowEmptyStrings = false)] + public string EncryptionPepper { get; set; } = string.Empty; + + /// RSA-2048 JWT public key (PEM, SPKI). Derived from Rsa256PrivateKey; exposed + /// via JWKS. Presence guaranteed transitively by the required private key. + public string Rsa256PublicKey { get; set; } = string.Empty; + + /// RSA-2048 JWT private key (PEM, PKCS#8). Sourced from + /// internal.secrets:auth:jwt_rsa256_private_pem. Required. + [Required(AllowEmptyStrings = false)] + public string Rsa256PrivateKey { get; set; } = string.Empty; + + /// Env: OCTOCON_GOOGLE_OAUTH_CLIENT_ID. + public string? GoogleOAuthClientId { get; set; } + + /// Env: OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET. + public string? GoogleOAuthClientSecret { get; set; } + + /// Env: OCTOCON_APPLE_OAUTH_CLIENT_ID. + public string? AppleOAuthClientId { get; set; } + + /// Env: OCTOCON_APPLE_OAUTH_CLIENT_SECRET. + public string? AppleOAuthClientSecret { get; set; } + + /// Env: OCTOCON_DISCORD_OAUTH_CLIENT_ID. + public string? DiscordOAuthClientId { get; set; } + + /// Env: OCTOCON_DISCORD_OAUTH_CLIENT_SECRET. + public string? DiscordOAuthClientSecret { get; set; } +} diff --git a/shared/Interfold.Auth.Contracts/Enums/OAuthProvider.cs b/shared/Interfold.Auth.Contracts/Enums/OAuthProvider.cs new file mode 100644 index 00000000..bb018d8f --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Enums/OAuthProvider.cs @@ -0,0 +1,24 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Auth.Contracts.Enums; + +/// +/// The supported external OAuth identity providers. Route segments and socket/cluster event +/// payloads carry the lowercase wire spellings ("discord" / "google" / "apple"); controllers +/// bind the route value as a raw string and feed it through +/// so unknown providers still produce the legacy "unsupported provider" response rather than +/// a model-binding 400. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum OAuthProvider +{ + [JsonStringEnumMemberName("discord")] + Discord, + + [JsonStringEnumMemberName("google")] + Google, + + [JsonStringEnumMemberName("apple")] + Apple, +} diff --git a/shared/Interfold.Auth.Contracts/Ids/Jti.cs b/shared/Interfold.Auth.Contracts/Ids/Jti.cs new file mode 100644 index 00000000..9e269f16 --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Ids/Jti.cs @@ -0,0 +1,35 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// JWT ID (jti claim) used for token revocation tracking. DB binds unwrap with +/// (Npgsql cannot bind the struct). ToString() redacts. +/// +[JsonConverter(typeof(JtiJsonConverter))] +public readonly record struct Jti +{ + public string Value { get; } + + public Jti(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator Jti(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); + + /// Total function: null/empty/whitespace input → null wrapper. + public static Jti? From(string? value) + => string.IsNullOrWhiteSpace(value) ? null : new Jti(value); + + /// Mint a fresh JWT ID (32-char lowercase hex, no dashes). + public static Jti NewJti() => new(Guid.NewGuid().ToString("N")); +} + +internal sealed class JtiJsonConverter : StringBackedJsonConverter +{ + protected override Jti Create(string value) => new(value); + protected override string GetValue(Jti value) => value.Value; +} diff --git a/shared/Interfold.Auth.Contracts/Ids/ProviderIdentity.cs b/shared/Interfold.Auth.Contracts/Ids/ProviderIdentity.cs new file mode 100644 index 00000000..73d06baf --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Ids/ProviderIdentity.cs @@ -0,0 +1,32 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Auth.Contracts.Ids; + +/// Discriminated union: exactly one of Discord/Google/Apple is populated. Construct +/// via the static factories; dispatch via property-pattern match or . +public readonly record struct ProviderIdentity +{ + public DiscordId? Discord { get; } + public Email? Google { get; } + public AppleId? Apple { get; } + + private ProviderIdentity(DiscordId? discord, Email? google, AppleId? apple) + { + Discord = discord; + Google = google; + Apple = apple; + } + + public static ProviderIdentity FromDiscord(DiscordId id) => new(id, null, null); + public static ProviderIdentity FromGoogle(Email id) => new(null, id, null); + public static ProviderIdentity FromApple(AppleId id) => new(null, null, id); + + public T MatchOrThrow(Func onDiscord, Func onGoogle, Func onApple) + => this switch + { + { Discord: { } id } => onDiscord(id), + { Google: { } email } => onGoogle(email), + { Apple: { } appleId } => onApple(appleId), + _ => throw new ArgumentOutOfRangeException(nameof(ProviderIdentity), this, "ProviderIdentity has no Discord/Google/Apple member populated.") + }; +} diff --git a/shared/Interfold.Auth.Contracts/Interfold.Auth.Contracts.csproj b/shared/Interfold.Auth.Contracts/Interfold.Auth.Contracts.csproj new file mode 100644 index 00000000..f1b786a3 --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Interfold.Auth.Contracts.csproj @@ -0,0 +1,14 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Auth.Contracts/Models/Commands/AuthCommands.cs b/shared/Interfold.Auth.Contracts/Models/Commands/AuthCommands.cs new file mode 100644 index 00000000..ad00cbe6 --- /dev/null +++ b/shared/Interfold.Auth.Contracts/Models/Commands/AuthCommands.cs @@ -0,0 +1,12 @@ +using Interfold.Auth.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Auth.Contracts.Models.Commands; + +public sealed record AuthenticateOAuthCommand(ProviderIdentity Identity); + +public sealed record LinkOAuthIdentityCommand(LinkToken LinkToken, ProviderIdentity Identity); + +public sealed record RecordAuthTokenCommand(Jti Jti, SystemId SystemId, DateTimeOffset ExpiresAt); + +public sealed record RevokeAuthTokenCommand(Jti Jti); diff --git a/shared/Interfold.Auth.Domain/Abstractions/Repository/IAuthTokenRevocationRepository.cs b/shared/Interfold.Auth.Domain/Abstractions/Repository/IAuthTokenRevocationRepository.cs new file mode 100644 index 00000000..e7c50081 --- /dev/null +++ b/shared/Interfold.Auth.Domain/Abstractions/Repository/IAuthTokenRevocationRepository.cs @@ -0,0 +1,25 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions.Repository; + +/// Per-JTI JWT revocation store. Records issued tokens and answers "still valid?" +/// on every authenticated request (must be sub-ms PK-lookup fast). Also drives logout, +/// incident response, and background expiry cleanup. +public interface IAuthTokenRevocationRepository +{ + Task RecordTokenAsync( + Jti jti, + SystemId systemId, + DateTimeOffset expiresAt, + CancellationToken cancellationToken = default); + + /// Called on every authenticated request during JWT validation. Must be fast. + Task ValidateTokenNotRevokedAsync( + Jti jti, + CancellationToken cancellationToken = default); + + /// Idempotent — revoking an already-revoked token is safe. + Task RevokeTokenAsync( + Jti jti, + CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Auth.Domain/AuthHelper.cs b/shared/Interfold.Auth.Domain/AuthHelper.cs new file mode 100644 index 00000000..615de566 --- /dev/null +++ b/shared/Interfold.Auth.Domain/AuthHelper.cs @@ -0,0 +1,57 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Auth.Domain; + +public class AuthHelper +{ + public static string CreateToken(AuthenticationConfiguration authConfig, DateTimeOffset expiresAt, DateTimeOffset now, Jti jti, SystemId systemId) + { + var headerJson = JsonSerializer.Serialize(new Dictionary + { + ["alg"] = "ES256", + ["typ"] = "JWT" + }); + + var payloadJson = JsonSerializer.Serialize(new Dictionary + { + ["iss"] = authConfig.JwtAuthority, + ["sub"] = systemId.Value, + ["iat"] = now.ToUnixTimeSeconds(), + ["nbf"] = now.ToUnixTimeSeconds(), + ["exp"] = expiresAt.ToUnixTimeSeconds(), + ["jti"] = jti.Value, + ["scope"] = "octocon:deeplink" + }); + + var encodedHeader = Base64UrlEncode(Encoding.UTF8.GetBytes(headerJson)); + var encodedPayload = Base64UrlEncode(Encoding.UTF8.GetBytes(payloadJson)); + var signingInput = $"{encodedHeader}.{encodedPayload}"; + + using var ecdsa = ECDsa.Create(); + ecdsa.ImportFromPem(NormalizePem(authConfig.JwtEs256PrivateKeyPem).AsSpan()); + var signature = ecdsa.SignData( + Encoding.UTF8.GetBytes(signingInput), + HashAlgorithmName.SHA256, + DSASignatureFormat.IeeeP1363FixedFieldConcatenation); + + var encodedSignature = Base64UrlEncode(signature); + + var token = $"{signingInput}.{encodedSignature}"; + return token; + } + + private static string Base64UrlEncode(byte[] data) + { + return Convert.ToBase64String(data) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + private static string NormalizePem(string pem) + => pem.Replace("\\n", "\n", StringComparison.Ordinal); +} diff --git a/shared/Interfold.Auth.Domain/AuthenticateOAuthCommandHandler.cs b/shared/Interfold.Auth.Domain/AuthenticateOAuthCommandHandler.cs new file mode 100644 index 00000000..9d4a1556 --- /dev/null +++ b/shared/Interfold.Auth.Domain/AuthenticateOAuthCommandHandler.cs @@ -0,0 +1,41 @@ +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Auth.Domain; + +public sealed class AuthenticateOAuthCommandHandler : ICommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IEncryptionStateRepository _encryptionRepository; + + public AuthenticateOAuthCommandHandler( + IAccountRepository accountRepository, + IEncryptionStateRepository encryptionRepository) + { + _accountRepository = accountRepository; + _encryptionRepository = encryptionRepository; + } + + public async Task> HandleAsync(CommandEnvelope command, CancellationToken cancellationToken = default) + { + var identity = command.Payload.Identity; + + var resolvedSystemId = await _accountRepository.FindOrCreateSystemIdAsync(identity, cancellationToken); + if (string.IsNullOrWhiteSpace(resolvedSystemId?.Value)) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.AuthLoginFailed); + + var systemId = resolvedSystemId.Value; + var encryptionState = await _encryptionRepository.GetAsync(systemId, cancellationToken); + + if (encryptionState?.Salt == null) + { + await _encryptionRepository.UpsertAsync(systemId, false, null, EncryptionSalt.NewRandom(), cancellationToken); + } + + return CommandExecutionResult.Success(systemId); + } +} diff --git a/shared/Interfold.Auth.Domain/Interfold.Auth.Domain.csproj b/shared/Interfold.Auth.Domain/Interfold.Auth.Domain.csproj new file mode 100644 index 00000000..6878c28d --- /dev/null +++ b/shared/Interfold.Auth.Domain/Interfold.Auth.Domain.csproj @@ -0,0 +1,23 @@ + + + net10.0 + enable + enable + + + + + + + + + + + diff --git a/shared/Interfold.Auth.Domain/LinkOAuthIdentityCommandHandler.cs b/shared/Interfold.Auth.Domain/LinkOAuthIdentityCommandHandler.cs new file mode 100644 index 00000000..e261dc36 --- /dev/null +++ b/shared/Interfold.Auth.Domain/LinkOAuthIdentityCommandHandler.cs @@ -0,0 +1,64 @@ +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Auth.Domain; + +public sealed record LinkOAuthIdentityCommandResult(AccountLinkResult Result, ScopedSystemId? SystemId); + +public sealed class LinkOAuthIdentityCommandHandler : ICommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public LinkOAuthIdentityCommandHandler( + IAccountRepository accountRepository, + IClusterEventBus eventBus) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + public async Task> HandleAsync(CommandEnvelope command, CancellationToken cancellationToken = default) + { + var linkToken = command.Payload.LinkToken; + var identity = command.Payload.Identity; + + var resolvedSystemId = await _accountRepository.ResolveSystemIdByLinkTokenAsync(linkToken, cancellationToken); + if (string.IsNullOrWhiteSpace(resolvedSystemId?.Value) || !ScopedSystemId.TryParseScoped(resolvedSystemId.Value, out var systemId)) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.AuthLinkInvalidToken); + + await _accountRepository.ClearLinkTokenAsync(systemId, cancellationToken); + + var result = await _accountRepository.LinkIdentityToUserAsync(systemId, identity, cancellationToken); + + if (result == AccountLinkResult.Success) + { + switch (identity) + { + case { Discord: { } discordId }: + await _eventBus.PublishAsync( + new SettingsDiscordAccountLinkedEvent(systemId, discordId), + cancellationToken); + break; + case { Google: { } email }: + await _eventBus.PublishAsync( + new SettingsGoogleAccountLinkedEvent(systemId, email), + cancellationToken); + break; + case { Apple: { } appleId }: + await _eventBus.PublishAsync( + new SettingsAppleAccountLinkedEvent(systemId, appleId), + cancellationToken); + break; + } + } + + return CommandExecutionResult.Success(new LinkOAuthIdentityCommandResult(result, systemId)); + } +} diff --git a/shared/Interfold.Auth.Domain/RecordAuthTokenCommandHandler.cs b/shared/Interfold.Auth.Domain/RecordAuthTokenCommandHandler.cs new file mode 100644 index 00000000..fd1485e1 --- /dev/null +++ b/shared/Interfold.Auth.Domain/RecordAuthTokenCommandHandler.cs @@ -0,0 +1,25 @@ +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Auth.Domain; + +public sealed record RecordAuthTokenCommandResult(bool Success); + +public sealed class RecordAuthTokenCommandHandler : ICommandHandler +{ + private readonly IAuthTokenRevocationRepository _tokenRevocation; + + public RecordAuthTokenCommandHandler(IAuthTokenRevocationRepository tokenRevocation) + { + _tokenRevocation = tokenRevocation; + } + + public async Task> HandleAsync(CommandEnvelope command, CancellationToken cancellationToken = default) + { + await _tokenRevocation.RecordTokenAsync(command.Payload.Jti, command.Payload.SystemId, command.Payload.ExpiresAt, cancellationToken); + return CommandExecutionResult.Success(new RecordAuthTokenCommandResult(true)); + } +} diff --git a/shared/Interfold.Auth.Domain/RevokeAuthTokenCommandHandler.cs b/shared/Interfold.Auth.Domain/RevokeAuthTokenCommandHandler.cs new file mode 100644 index 00000000..0d1b7f2f --- /dev/null +++ b/shared/Interfold.Auth.Domain/RevokeAuthTokenCommandHandler.cs @@ -0,0 +1,25 @@ +using Interfold.Auth.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Auth.Domain; + +public sealed record RevokeAuthTokenCommandResult(bool Success); + +public sealed class RevokeAuthTokenCommandHandler : ICommandHandler +{ + private readonly IAuthTokenRevocationRepository _tokenRevocation; + + public RevokeAuthTokenCommandHandler(IAuthTokenRevocationRepository tokenRevocation) + { + _tokenRevocation = tokenRevocation; + } + + public async Task> HandleAsync(CommandEnvelope command, CancellationToken cancellationToken = default) + { + await _tokenRevocation.RevokeTokenAsync(command.Payload.Jti, cancellationToken); + return CommandExecutionResult.Success(new RevokeAuthTokenCommandResult(true)); + } +} diff --git a/shared/Interfold.DatabaseBootstrap/DataStaxScyllaExecutor.cs b/shared/Interfold.DatabaseBootstrap/DataStaxScyllaExecutor.cs new file mode 100644 index 00000000..6e68009e --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/DataStaxScyllaExecutor.cs @@ -0,0 +1,76 @@ +using System.Text; +using Cassandra; +using ISession = Cassandra.ISession; + +namespace Interfold.DatabaseBootstrap; + +/// +/// implementation that talks to Scylla/Cassandra in-process +/// via the DataStax driver. Consumed by the TUnit.Aspire fixtures alongside +/// and by the dev-mode seed hosted service in +/// Interfold.AppHost. +/// +/// +/// Each call builds a single-use + because the +/// auth credentials are call-scoped (we want to validate role lockouts mid-seed). The +/// underlying socket pool is small (1 connection) and explicitly torn down on dispose so +/// nothing leaks across test cases. Tests run on a single host:port pair so the cluster cost +/// is dwarfed by the seed work itself. +/// +public sealed class DataStaxScyllaExecutor(string host, int port) : IScyllaExecutor +{ + public async Task ExecCqlAsync(string user, string password, string cql, CancellationToken ct) + { + using var cluster = BuildCluster(user, password); + using var session = await cluster.ConnectAsync().ConfigureAwait(false); + // SimpleStatement.SetIdempotence(true) is not strictly true for ALTER ROLE etc. but + // we never trigger driver-side retries in the seed flow — the orchestrator owns + // retry semantics by short-circuiting on the idempotency probes. + var stmt = new SimpleStatement(cql); + await session.ExecuteAsync(stmt).ConfigureAwait(false); + } + + public async Task TryExecCqlAsync(string user, string password, string cql, CancellationToken ct) + { + try + { + using var cluster = BuildCluster(user, password); + using var session = await cluster.ConnectAsync().ConfigureAwait(false); + var stmt = new SimpleStatement(cql); + var rs = await session.ExecuteAsync(stmt).ConfigureAwait(false); + + // Flatten the result rows into a single string so the orchestrator's `Output` + // can be substring-searched (e.g. ScyllaSeeder probes "does the admin name + // appear in LIST ROLES OF ''"). For zero-row resultsets we return empty. + var sb = new StringBuilder(); + foreach (var row in rs) + { + for (var i = 0; i < row.Length; i++) + { + if (i > 0) sb.Append(' '); + sb.Append(row.GetValue(i)?.ToString() ?? string.Empty); + } + sb.AppendLine(); + } + return new ScyllaExecResult(true, sb.ToString()); + } + catch (Exception ex) + { + // Authentication / unavailable / timeout — let the caller decide whether this + // is "not yet seeded" (idempotency probe miss) or "transient and retry". + return new ScyllaExecResult(false, ex.Message); + } + } + + private Cluster BuildCluster(string user, string password) + { + return Cluster.Builder() + .AddContactPoint(host) + .WithPort(port) + .WithCredentials(user, password) + // Localhost test runs are reachable in well under 5s even on cold-start; the + // default 5s socket timeout is fine. Connection pool kept at the driver default + // (1 / host) — seed work is sequential, more connections wouldn't help. + .Build(); + } +} diff --git a/shared/Interfold.DatabaseBootstrap/IDatabaseInitLogger.cs b/shared/Interfold.DatabaseBootstrap/IDatabaseInitLogger.cs new file mode 100644 index 00000000..aa27de42 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/IDatabaseInitLogger.cs @@ -0,0 +1,21 @@ +namespace Interfold.DatabaseBootstrap; + +/// Minimal logging surface for the database seed orchestrators. Bootstrapper +/// wraps its PhaseLogger behind this so the shared project stays framework-free. +public interface IDatabaseInitLogger +{ + void Info(string message); + void Warn(string message); + void Error(string message); +} + +/// Drops every log message. Handy in tests where seed progress is implied +/// by downstream assertions. +public sealed class NoOpDatabaseInitLogger : IDatabaseInitLogger +{ + public static readonly NoOpDatabaseInitLogger Instance = new(); + + public void Info(string message) { } + public void Warn(string message) { } + public void Error(string message) { } +} diff --git a/shared/Interfold.DatabaseBootstrap/IPostgresExecutor.cs b/shared/Interfold.DatabaseBootstrap/IPostgresExecutor.cs new file mode 100644 index 00000000..d44ecbfc --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/IPostgresExecutor.cs @@ -0,0 +1,27 @@ +namespace Interfold.DatabaseBootstrap; + +/// Transport surface for — implemented by both +/// the compose-exec (docker compose exec ... psql) and in-process Npgsql +/// transports. Every call takes user/password/database because the seed flow swaps +/// roles (init → admin) and databases (postgres → app) between steps. +public interface IPostgresExecutor +{ + /// Run a DDL/DML script; throws after the transport's own transient-retry + /// budget is exhausted. + Task ExecScriptAsync(string user, string password, string database, string sql, CancellationToken ct); + + /// Run a parameterised statement. Compose-exec binds via + /// psql -v name=value + :'name'; Npgsql binds + /// values with matching names. + Task ExecScriptWithVarsAsync( + string user, + string password, + string database, + string sql, + IReadOnlyList<(string Name, string Value)> vars, + CancellationToken ct); + + /// First column of the first row, or null. Used for idempotency + /// probes — never masks transient errors as success, so callers must guard. + Task ExecScalarAsync(string user, string password, string database, string sql, CancellationToken ct); +} diff --git a/shared/Interfold.DatabaseBootstrap/IScyllaExecutor.cs b/shared/Interfold.DatabaseBootstrap/IScyllaExecutor.cs new file mode 100644 index 00000000..6740ecab --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/IScyllaExecutor.cs @@ -0,0 +1,15 @@ +namespace Interfold.DatabaseBootstrap; + +/// Transport surface driven by . Two methods: +/// ExecCqlAsync throws on failure, TryExecCqlAsync returns success+output +/// so probes can distinguish "auth denied while bootstrapping" from "fatal". +public interface IScyllaExecutor +{ + Task ExecCqlAsync(string user, string password, string cql, CancellationToken ct); + + Task TryExecCqlAsync(string user, string password, string cql, CancellationToken ct); +} + +/// Result of . +/// is cqlsh stdout for compose-exec, a flattened row dump for the DataStax driver. +public readonly record struct ScyllaExecResult(bool Succeeded, string Output); diff --git a/shared/Interfold.DatabaseBootstrap/InProcessSeedWaits.cs b/shared/Interfold.DatabaseBootstrap/InProcessSeedWaits.cs new file mode 100644 index 00000000..31cac415 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/InProcessSeedWaits.cs @@ -0,0 +1,102 @@ +using System.Net.Sockets; +using Cassandra; +using Npgsql; + +namespace Interfold.DatabaseBootstrap; + +/// +/// Cold-start driver-side wait loops used by the in-process seed callers +/// (Interfold.AppHost's dev seed hosted service and the TUnit.Aspire fixtures). +/// The bootstrapper's own compose-exec seed uses different probes because "healthy" for a +/// container-exec differs from "healthy" for an out-of-container Npgsql/DataStax connection. +/// +public static class InProcessSeedWaits +{ + /// Built-in Scylla / Cassandra superuser present before locks it. + public const string ScyllaDefaultUser = "cassandra"; + + /// Built-in password for . + public const string ScyllaDefaultPassword = "cassandra"; + + /// + /// Waits until the caller's init role can complete SELECT 1 against Postgres. + /// Requires .RequiredConsecutiveSuccesses back-to-back + /// successes so we don't return during the socket-open-but-still-crash-recovering window + /// that fresh TimescaleDB starts hit. + /// + public static async Task WaitForPostgresAsync( + string initConnectionString, + PostgresReadinessOptions options, + CancellationToken ct) + { + var deadline = DateTime.UtcNow.Add(options.Timeout); + var attempt = 0; + var consecutiveSuccesses = 0; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + try + { + await using var conn = new NpgsqlConnection(initConnectionString); + await conn.OpenAsync(ct).ConfigureAwait(false); + await using var cmd = new NpgsqlCommand("SELECT 1", conn); + var result = await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false); + if (result is int one && one == 1) + { + consecutiveSuccesses++; + if (consecutiveSuccesses >= options.RequiredConsecutiveSuccesses) return; + } + else if (consecutiveSuccesses > 0) + { + consecutiveSuccesses = 0; + } + } + // Broad catch by driver exception type: any transient socket / auth / timeout + // failure resets the streak; the deadline is the real timeout. + catch (NpgsqlException) { consecutiveSuccesses = 0; } + catch (SocketException) { consecutiveSuccesses = 0; } + catch (TimeoutException) { consecutiveSuccesses = 0; } + await Task.Delay(TimeSpan.FromSeconds(2), ct).ConfigureAwait(false); + } + throw new TimeoutException( + $"Postgres did not become ready within {options.Timeout.TotalMinutes} minutes ({attempt} probes)."); + } + + /// + /// Waits until the built-in cassandra account answers a system.local query. + /// Fixed 5-minute budget — gossip-bootstrap is the slow phase and 5min is already generous + /// on cold-start DinD hosts. + /// + public static async Task WaitForScyllaAsync(string host, int port, CancellationToken ct) + { + var deadline = DateTime.UtcNow.AddMinutes(5); + var attempt = 0; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + try + { + using var cluster = Cluster.Builder() + .AddContactPoint(host) + .WithPort(port) + .WithCredentials(ScyllaDefaultUser, ScyllaDefaultPassword) + .WithSocketOptions(new SocketOptions().SetConnectTimeoutMillis(10000)) + .Build(); + using var session = await cluster.ConnectAsync().ConfigureAwait(false); + var rs = await session.ExecuteAsync(new SimpleStatement("SELECT cluster_name FROM system.local")) + .ConfigureAwait(false); + if (rs.GetRows().Any()) return; + } + // DataStax wraps gossip / auth / socket faults in a handful of exception types; + // any startup-phase throw is transient until the deadline. + catch (Exception) when (DateTime.UtcNow < deadline) + { + } + await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); + } + throw new TimeoutException( + $"scylla/cassandra at {host}:{port} did not become ready within 5 minutes ({attempt} probes)."); + } +} diff --git a/shared/Interfold.DatabaseBootstrap/Interfold.DatabaseBootstrap.csproj b/shared/Interfold.DatabaseBootstrap/Interfold.DatabaseBootstrap.csproj new file mode 100644 index 00000000..9a183d19 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/Interfold.DatabaseBootstrap.csproj @@ -0,0 +1,38 @@ + + + + + + net10.0 + enable + enable + Interfold.DatabaseBootstrap + + + + + $(DefineConstants);INTERFOLD_DBBOOT_LINKED + + + + + + + + + + + + + diff --git a/shared/Interfold.DatabaseBootstrap/NpgsqlPostgresExecutor.cs b/shared/Interfold.DatabaseBootstrap/NpgsqlPostgresExecutor.cs new file mode 100644 index 00000000..0b493121 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/NpgsqlPostgresExecutor.cs @@ -0,0 +1,93 @@ +using Npgsql; + +namespace Interfold.DatabaseBootstrap; + +/// +/// implementation that talks to Postgres in-process via +/// . Consumed by the TUnit.Aspire fixtures (single-node +/// Scylla, multi-node Scylla, Cassandra) and by the dev-mode seed hosted service in +/// Interfold.AppHost. The bootstrapper uses a different implementation +/// (ComposeExecPostgresExecutor) that shells out to docker compose exec. +/// +/// +/// +/// Carries a base connection string in its constructor; per-call user/password/database +/// overrides rewrite the connection string via . +/// Connections are opened + disposed per call. Npgsql's built-in pooler caches the +/// underlying TCP sockets so this stays cheap. +/// +/// +/// Parameter binding bridge: uses psql's +/// :'name' variable syntax (canonical for the compose-exec path). For Npgsql we +/// rewrite those into @name placeholders before sending so the same template body +/// works in-process. Mapping is by var-name lookup, not regex, so unrelated colons in the +/// SQL are untouched. +/// +/// +public sealed class NpgsqlPostgresExecutor(string baseConnectionString) : IPostgresExecutor +{ + public Task ExecScriptAsync(string user, string password, string database, string sql, CancellationToken ct) + => RunNonQueryAsync(user, password, database, sql, vars: null, ct); + + public Task ExecScriptWithVarsAsync( + string user, string password, string database, string sql, + IReadOnlyList<(string Name, string Value)> vars, CancellationToken ct) + => RunNonQueryAsync(user, password, database, sql, vars, ct); + + public async Task ExecScalarAsync(string user, string password, string database, string sql, CancellationToken ct) + { + await using var conn = OpenConnection(user, password, database); + await conn.OpenAsync(ct).ConfigureAwait(false); + await using var cmd = new NpgsqlCommand(sql, conn); + var result = await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false); + return result switch + { + null => null, + DBNull => null, + _ => result.ToString(), + }; + } + + private async Task RunNonQueryAsync( + string user, string password, string database, string sql, + IReadOnlyList<(string Name, string Value)>? vars, CancellationToken ct) + { + await using var conn = OpenConnection(user, password, database); + await conn.OpenAsync(ct).ConfigureAwait(false); + + var rewritten = vars is null ? sql : RewriteBindPlaceholders(sql, vars); + await using var cmd = new NpgsqlCommand(rewritten, conn); + if (vars is not null) + { + foreach (var (name, value) in vars) + { + cmd.Parameters.AddWithValue(name, value); + } + } + await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false); + } + + private NpgsqlConnection OpenConnection(string user, string password, string database) + { + var builder = new NpgsqlConnectionStringBuilder(baseConnectionString) + { + Username = user, + Password = password, + Database = database, + }; + return new NpgsqlConnection(builder.ConnectionString); + } + + private static string RewriteBindPlaceholders(string sql, IReadOnlyList<(string Name, string Value)> vars) + { + // psql uses `:'name'` for safe quoted-literal substitution; Npgsql uses `@name` as a + // typed bind parameter. We only rewrite the names we know we're about to bind so any + // other colon-prefixed token in the SQL (e.g. inside a CAST expression) is left alone. + var rewritten = sql; + foreach (var (name, _) in vars) + { + rewritten = rewritten.Replace($":'{name}'", $"@{name}", StringComparison.Ordinal); + } + return rewritten; + } +} diff --git a/shared/Interfold.DatabaseBootstrap/PostgresReadinessOptions.cs b/shared/Interfold.DatabaseBootstrap/PostgresReadinessOptions.cs new file mode 100644 index 00000000..23b00b4c --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/PostgresReadinessOptions.cs @@ -0,0 +1,7 @@ +namespace Interfold.DatabaseBootstrap; + +public record PostgresReadinessOptions( + TimeSpan Timeout, + int RequiredConsecutiveSuccesses = 3, + string? RunAsRole = null +); diff --git a/shared/Interfold.DatabaseBootstrap/PostgresSeedOptions.cs b/shared/Interfold.DatabaseBootstrap/PostgresSeedOptions.cs new file mode 100644 index 00000000..5dc0cc45 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/PostgresSeedOptions.cs @@ -0,0 +1,76 @@ +namespace Interfold.DatabaseBootstrap; + +/// Input contract for . Populated +/// from BootstrapConfig + GeneratedSecrets in production and hand-fed +/// deterministic constants by test fixtures. +/// initdb bootstrap superuser (hard-coded db_init). +/// Password for during the seed run. +/// App role — NOSUPERUSER NOCREATEDB NOCREATEROLE; the only +/// role in the compose connection string. +/// Password for . +/// Admin role (SUPERUSER); owns the app database and the +/// internal schema. +/// Password for . +/// Application database, owned by the admin role. +/// Defaults to interfold; overridable for shared clusters. +/// Seeded as oauth:google:client_secret. +/// Empty values skip the row. +/// Seeded as oauth:discord:client_secret. +/// Seeded as oauth:apple:client_secret. +/// Seeded as encryption:pepper. +/// Seeded as scylla:contact_points. +/// Seeded as scylla:local_datacenter. +/// Seeded as scylla:username; matches the app user +/// that creates on the same run. +/// Seeded as scylla:password. +/// Seeded as scylla:port. +/// Seeded as scylla:admin_username; consumed by +/// ScyllaMigrationService for keyspace-level DDL. +/// Seeded as scylla:admin_password. +/// RSA-2048 JWT signing key (PEM, PKCS#8), +/// seeded as auth:jwt_rsa256_private_pem. No on-disk fallback in the API. +/// ES256 JWT signing key (PEM, SEC1), seeded as +/// auth:jwt_es256_private_pem. +/// HMAC secret for the phase-F deep-link exchange, seeded +/// as auth:deep_link_secret. Auto-generated by SecretsPhase. +/// Password for the Kestrel leaf PFX, seeded as +/// certs:leaf_pfx_password. +/// true in production so the .env value +/// goes stale in-cluster after seeding; false in tests so idempotent retries +/// can still authenticate as db_init. +/// Optional; seeded as +/// firebase:client:android. Null/empty skips the row and the matching endpoint +/// returns 503. +/// Optional; seeded as firebase:client:ios. +/// Optional; seeded as firebase:client:web. +/// Optional Firebase FCM v1 service-account JSON, +/// seeded as fcm:service_account_json. Absent → the DI factory returns +/// NullFCMService so notifications no-op instead of erroring. +public sealed record PostgresSeedOptions( + string InitUser, + string InitPassword, + string AppUser, + string AppPassword, + string AdminUser, + string AdminPassword, + string DefaultDatabase, + string GoogleOAuthClientSecret, + string DiscordOAuthClientSecret, + string AppleOAuthClientSecret, + string EncryptionPepper, + string ScyllaContactPoints, + string ScyllaLocalDatacenter, + string ScyllaAppUser, + string ScyllaAppPassword, + int ScyllaPort, + string ScyllaAdminUser, + string ScyllaAdminPassword, + string JwtRsa256PrivateKeyPem, + string JwtEs256PrivateKeyPem, + string DeepLinkSecret, + string LeafPfxPassword, + bool ScrambleInitUserPassword, + string? FirebaseAndroidClientJson = null, + string? FirebaseIosClientJson = null, + string? FirebaseWebClientJson = null, + string? FcmServiceAccountJson = null); diff --git a/shared/Interfold.DatabaseBootstrap/PostgresSeeder.cs b/shared/Interfold.DatabaseBootstrap/PostgresSeeder.cs new file mode 100644 index 00000000..699fbc46 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/PostgresSeeder.cs @@ -0,0 +1,116 @@ +using System.Security.Cryptography; + +namespace Interfold.DatabaseBootstrap; + +/// Runs the five Postgres seed steps end-to-end. Callers are responsible for +/// waiting until the cluster accepts connections; this orchestrator does not probe +/// readiness. +public static class PostgresSeeder +{ + // Matches SecretsPhase.PasswordAlphabet so the scramble password shape is + // indistinguishable from a normal generated one. ~279 bits of entropy. + private const string ScrambleAlphabet = + "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; + + /// Roles → database → schema → secrets → (optional) init-password scramble. + /// Idempotent — a successful prior run makes this a no-op via the app-role probe. + public static async Task BootstrapAsync( + IPostgresExecutor executor, + PostgresSeedOptions options, + IDatabaseInitLogger logger, + CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(executor); + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(logger); + + if (await AppRoleAlreadyConfiguredAsync(executor, options, ct).ConfigureAwait(false)) + { + // "postgres already initialised" is grep-bait for BootstrapIdempotenceTests. + logger.Info($" postgres already initialised (state probe: app role '{options.AppUser}' is DML-only); skipping admin bootstrap"); + return; + } + + logger.Info( + $" splitting cluster owner '{options.InitUser}' into app='{options.AppUser}' (DML-only) " + + $"and admin='{options.AdminUser}' (superuser)..."); + + // 1. CREATE/ALTER admin + app roles (init user, postgres DB). + await executor.ExecScriptAsync( + options.InitUser, options.InitPassword, "postgres", + PostgresSqlTemplates.BuildRolesSql(options), ct).ConfigureAwait(false); + + // 2. App database owned by admin. CREATE DATABASE can't be in a transaction. + var dbExists = await executor.ExecScalarAsync( + options.InitUser, options.InitPassword, "postgres", + PostgresSqlTemplates.BuildDatabaseExistsProbeSql(options), ct).ConfigureAwait(false); + if (string.IsNullOrEmpty(dbExists)) + { + await executor.ExecScriptAsync( + options.InitUser, options.InitPassword, "postgres", + PostgresSqlTemplates.BuildCreateDatabaseSql(options), ct).ConfigureAwait(false); + } + else + { + await executor.ExecScriptAsync( + options.InitUser, options.InitPassword, "postgres", + PostgresSqlTemplates.BuildAlterDatabaseOwnerSql(options), ct).ConfigureAwait(false); + } + + // 3. Schema + grants on the app database, still as db_init (SUPERUSER until step 5). + await executor.ExecScriptAsync( + options.InitUser, options.InitPassword, options.DefaultDatabase, + PostgresSqlTemplates.BuildSchemaSql(options), ct).ConfigureAwait(false); + + // 4. Seed internal.secrets as the admin role (co-owns the table). + foreach (var entry in SeedKeys.All) + { + var value = entry.ValueSelector(options); + if (string.IsNullOrEmpty(value)) continue; + + await executor.ExecScriptWithVarsAsync( + options.AdminUser, options.AdminPassword, options.DefaultDatabase, + PostgresSqlTemplates.UpsertSecretSql, + [(PostgresSqlTemplates.UpsertKeyVar, entry.Key.Value), + (PostgresSqlTemplates.UpsertValueVar, value)], + ct).ConfigureAwait(false); + } + + // 5. Optional init-password scramble (production only). + if (options.ScrambleInitUserPassword) + { + var scrambled = RandomNumberGenerator.GetString(ScrambleAlphabet, 48); + await executor.ExecScriptAsync( + options.InitUser, options.InitPassword, "postgres", + PostgresSqlTemplates.BuildScrambleInitUserSql(options, scrambled), ct).ConfigureAwait(false); + + logger.Info( + $" postgres bootstrapped: app='{options.AppUser}' (DML-only), " + + $"admin='{options.AdminUser}' (superuser), {options.InitUser} scrambled"); + } + else + { + logger.Info( + $" postgres bootstrapped: app='{options.AppUser}' (DML-only), " + + $"admin='{options.AdminUser}' (superuser); {options.InitUser} password left intact " + + "(test fixture flow)"); + } + } + + private static async Task AppRoleAlreadyConfiguredAsync( + IPostgresExecutor executor, PostgresSeedOptions options, CancellationToken ct) + { + // Any exception (auth, missing DB, network) means "not yet seeded". + try + { + var result = await executor.ExecScalarAsync( + options.AppUser, options.AppPassword, options.DefaultDatabase, + PostgresSqlTemplates.BuildAppRoleConfiguredProbeSql(options), ct).ConfigureAwait(false); + return result == "1"; + } + catch + { + return false; + } + } +} diff --git a/shared/Interfold.DatabaseBootstrap/PostgresSqlTemplates.cs b/shared/Interfold.DatabaseBootstrap/PostgresSqlTemplates.cs new file mode 100644 index 00000000..302f507c --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/PostgresSqlTemplates.cs @@ -0,0 +1,94 @@ +namespace Interfold.DatabaseBootstrap; + +/// SQL bodies driven by . Every DO/IF block uses +/// + format(... %I ... %L) for defence-in-depth +/// even though the bootstrapper password alphabet excludes apostrophes. +public static class PostgresSqlTemplates +{ + /// Idempotent CREATE/ALTER ROLE for admin + app users. Run as the init user + /// against the postgres database. + public static string BuildRolesSql(PostgresSeedOptions o) => + $@" +DO $bootstrap$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '{SqlEscape.Literal(o.AdminUser)}') THEN + EXECUTE format('CREATE ROLE %I WITH SUPERUSER LOGIN PASSWORD %L', '{SqlEscape.Literal(o.AdminUser)}', '{SqlEscape.Literal(o.AdminPassword)}'); + ELSE + EXECUTE format('ALTER ROLE %I WITH SUPERUSER LOGIN PASSWORD %L', '{SqlEscape.Literal(o.AdminUser)}', '{SqlEscape.Literal(o.AdminPassword)}'); + END IF; + + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '{SqlEscape.Literal(o.AppUser)}') THEN + EXECUTE format('CREATE ROLE %I WITH NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD %L', '{SqlEscape.Literal(o.AppUser)}', '{SqlEscape.Literal(o.AppPassword)}'); + ELSE + EXECUTE format('ALTER ROLE %I WITH NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD %L', '{SqlEscape.Literal(o.AppUser)}', '{SqlEscape.Literal(o.AppPassword)}'); + END IF; +END +$bootstrap$;"; + + /// Returns 1 when exists. + public static string BuildDatabaseExistsProbeSql(PostgresSeedOptions o) => + $"SELECT 1 FROM pg_database WHERE datname = '{SqlEscape.Literal(o.DefaultDatabase)}'"; + + /// Single CREATE DATABASE — cannot be transactional. + public static string BuildCreateDatabaseSql(PostgresSeedOptions o) => + $"CREATE DATABASE \"{o.DefaultDatabase}\" OWNER \"{o.AdminUser}\";"; + + /// ALTER DATABASE … OWNER TO for the rerun-against-existing-db case. + public static string BuildAlterDatabaseOwnerSql(PostgresSeedOptions o) => + $"ALTER DATABASE \"{o.DefaultDatabase}\" OWNER TO \"{o.AdminUser}\";"; + + /// Idempotent schema + grants. Run against the app database as the init user. + public static string BuildSchemaSql(PostgresSeedOptions o) => + $@" +CREATE SCHEMA IF NOT EXISTS internal AUTHORIZATION ""{o.AdminUser}""; + +CREATE TABLE IF NOT EXISTS internal.secrets ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL, + created_by TEXT NOT NULL DEFAULT 'bootstrap', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + expires_at TIMESTAMPTZ, + rotated_from TEXT +); +ALTER TABLE internal.secrets OWNER TO ""{o.AdminUser}""; + +REVOKE ALL ON DATABASE ""{o.DefaultDatabase}"" FROM PUBLIC; +GRANT CONNECT, TEMPORARY ON DATABASE ""{o.DefaultDatabase}"" TO ""{o.AppUser}""; + +GRANT USAGE ON SCHEMA internal TO ""{o.AppUser}""; +GRANT SELECT ON internal.secrets TO ""{o.AppUser}""; + +GRANT USAGE, CREATE ON SCHEMA public TO ""{o.AdminUser}""; +GRANT USAGE ON SCHEMA public TO ""{o.AppUser}""; + +GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO ""{o.AppUser}""; +GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO ""{o.AppUser}""; + +ALTER DEFAULT PRIVILEGES FOR ROLE ""{o.AdminUser}"" IN SCHEMA public + GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ""{o.AppUser}""; +ALTER DEFAULT PRIVILEGES FOR ROLE ""{o.AdminUser}"" IN SCHEMA public + GRANT USAGE, SELECT, UPDATE ON SEQUENCES TO ""{o.AppUser}""; +"; + + /// Upsert template for every internal.secrets row. Both transport + /// adapters must bind these vars as safe quoted literals — psql via :'name', + /// Npgsql via parameter binding. + public const string UpsertSecretSql = @" +INSERT INTO internal.secrets (key, value, created_by, updated_at) +VALUES (:'secret_key', :'secret_value', 'bootstrap', now()) +ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now();"; + + public const string UpsertKeyVar = "secret_key"; + public const string UpsertValueVar = "secret_value"; + + /// Post-seed scramble of the init-user password. Production always runs; + /// tests skip via . + public static string BuildScrambleInitUserSql(PostgresSeedOptions o, string scrambled) => + $"ALTER ROLE \"{o.InitUser}\" WITH PASSWORD '{SqlEscape.Literal(scrambled)}';"; + + /// Returns 1 when the app user is already provisioned as non-superuser. + /// Connect as the app user. + public static string BuildAppRoleConfiguredProbeSql(PostgresSeedOptions o) => + $"SELECT 1 FROM pg_roles WHERE rolname = '{SqlEscape.Literal(o.AppUser)}' AND NOT rolsuper"; +} diff --git a/shared/Interfold.DatabaseBootstrap/ScyllaCqlTemplates.cs b/shared/Interfold.DatabaseBootstrap/ScyllaCqlTemplates.cs new file mode 100644 index 00000000..291fb8be --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/ScyllaCqlTemplates.cs @@ -0,0 +1,33 @@ +namespace Interfold.DatabaseBootstrap; + +/// CQL bodies driven by . +public static class ScyllaCqlTemplates +{ + /// Cassandra/Scylla built-in superuser before lockdown. + public const string DefaultUser = "cassandra"; + + /// Default password for on a fresh data volume. + public const string DefaultPassword = "cassandra"; + + /// Admin superuser create. Must run as — Cassandra + /// forbids altering your own superuser status. + public static string BuildCreateAdminRoleCql(ScyllaSeedOptions o) => + $"CREATE ROLE IF NOT EXISTS '{SqlEscape.Literal(o.AdminUser)}' " + + $"WITH PASSWORD = '{SqlEscape.Literal(o.AdminPassword)}' " + + "AND SUPERUSER = true AND LOGIN = true"; + + public static string BuildCreateAppRoleCql(ScyllaSeedOptions o) => + $"CREATE ROLE IF NOT EXISTS '{SqlEscape.Literal(o.AppUser)}' " + + $"WITH PASSWORD = '{SqlEscape.Literal(o.AppPassword)}' " + + "AND SUPERUSER = false AND LOGIN = true"; + + /// Lockdown of the default user. Deliberately omits the SUPERUSER clause — + /// cassandra is still one, and self-demotion errors. + public static string BuildLockDefaultUserCql(string scrambledPassword) => + $"ALTER ROLE '{DefaultUser}' WITH PASSWORD = '{SqlEscape.Literal(scrambledPassword)}' AND LOGIN = false"; + + /// Idempotency probe: LIST ROLES OF the admin returns the admin row iff + /// role creation succeeded on a prior run. + public static string BuildListAdminRoleCql(ScyllaSeedOptions o) => + $"LIST ROLES OF '{SqlEscape.Literal(o.AdminUser)}'"; +} diff --git a/shared/Interfold.DatabaseBootstrap/ScyllaReadinessOptions.cs b/shared/Interfold.DatabaseBootstrap/ScyllaReadinessOptions.cs new file mode 100644 index 00000000..62a0272e --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/ScyllaReadinessOptions.cs @@ -0,0 +1,13 @@ +namespace Interfold.DatabaseBootstrap; + +/// Options for ScyllaReadinessProbe.WaitAsync. Mirrors +/// , but drives CQL via +/// instead of shelling to pg_isready. +/// Absolute deadline for the probe loop (DatabaseInitPhase uses 5 min). +/// Back-to-back CQL successes required +/// before "ready". Defaults to 1; scylla's auth-ready gate already implies bootstrap +/// finished by the time DESCRIBE CLUSTER returns. +public record ScyllaReadinessOptions( + TimeSpan Timeout, + int RequiredConsecutiveSuccesses = 1 +); diff --git a/shared/Interfold.DatabaseBootstrap/ScyllaSeedOptions.cs b/shared/Interfold.DatabaseBootstrap/ScyllaSeedOptions.cs new file mode 100644 index 00000000..37ec5ecd --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/ScyllaSeedOptions.cs @@ -0,0 +1,16 @@ +namespace Interfold.DatabaseBootstrap; + +/// Input contract for . The +/// built-in cassandra / cassandra credentials are hard-coded inside the seeder. +/// Non-superuser role — SUPERUSER = false, LOGIN = true. +/// Password for . +/// Superuser role — SUPERUSER = true, LOGIN = true. Convention: <app>_admin. +/// Password for . +/// When true (and neither user equals cassandra), +/// the seeder finishes by scrambling cassandra's password and setting LOGIN = false. +public sealed record ScyllaSeedOptions( + string AppUser, + string AppPassword, + string AdminUser, + string AdminPassword, + bool LockDefaultCassandra); diff --git a/shared/Interfold.DatabaseBootstrap/ScyllaSeeder.cs b/shared/Interfold.DatabaseBootstrap/ScyllaSeeder.cs new file mode 100644 index 00000000..d5978ac5 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/ScyllaSeeder.cs @@ -0,0 +1,77 @@ +using System.Security.Cryptography; + +namespace Interfold.DatabaseBootstrap; + +/// Mints the admin + app roles then locks the default cassandra account. +/// Every statement runs as the built-in cassandra session because Cassandra +/// forbids a role from altering its own superuser status. +public static class ScyllaSeeder +{ + private const string ScrambleAlphabet = + "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; + + /// Idempotent — short-circuits when the admin role already exists. + public static async Task BootstrapAsync( + IScyllaExecutor executor, + ScyllaSeedOptions options, + IDatabaseInitLogger logger, + CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(executor); + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(logger); + + if (await AdminRoleAlreadyConfiguredAsync(executor, options, ct).ConfigureAwait(false)) + { + // "scylla already initialised" is grep-bait for BootstrapIdempotenceTests. + logger.Info($" scylla already initialised (state probe: admin role '{options.AdminUser}' present); skipping admin bootstrap"); + return; + } + + logger.Info( + $" creating scylla roles: app='{options.AppUser}' (DML-only), " + + $"admin='{options.AdminUser}' (superuser), locking default cassandra..."); + + await executor.ExecCqlAsync( + ScyllaCqlTemplates.DefaultUser, ScyllaCqlTemplates.DefaultPassword, + ScyllaCqlTemplates.BuildCreateAdminRoleCql(options), ct).ConfigureAwait(false); + + await executor.ExecCqlAsync( + ScyllaCqlTemplates.DefaultUser, ScyllaCqlTemplates.DefaultPassword, + ScyllaCqlTemplates.BuildCreateAppRoleCql(options), ct).ConfigureAwait(false); + + if (options.LockDefaultCassandra + && !string.Equals(options.AppUser, ScyllaCqlTemplates.DefaultUser, StringComparison.Ordinal) + && !string.Equals(options.AdminUser, ScyllaCqlTemplates.DefaultUser, StringComparison.Ordinal)) + { + // LOGIN=false is the real protection; the throwaway password is belt+braces. + var scrambled = RandomNumberGenerator.GetString(ScrambleAlphabet, 48); + await executor.ExecCqlAsync( + ScyllaCqlTemplates.DefaultUser, ScyllaCqlTemplates.DefaultPassword, + ScyllaCqlTemplates.BuildLockDefaultUserCql(scrambled), ct).ConfigureAwait(false); + } + + logger.Info( + $" scylla bootstrapped: app='{options.AppUser}' (non-superuser), " + + $"admin='{options.AdminUser}' (superuser), default cassandra " + + (options.LockDefaultCassandra ? "locked" : "left intact")); + } + + private static async Task AdminRoleAlreadyConfiguredAsync( + IScyllaExecutor executor, ScyllaSeedOptions options, CancellationToken ct) + { + // Default session works pre-lockdown; post-lockdown we fall back to admin creds. + var asDefault = await executor.TryExecCqlAsync( + ScyllaCqlTemplates.DefaultUser, ScyllaCqlTemplates.DefaultPassword, + ScyllaCqlTemplates.BuildListAdminRoleCql(options), ct).ConfigureAwait(false); + if (asDefault.Succeeded && asDefault.Output.Contains(options.AdminUser, StringComparison.Ordinal)) + { + return true; + } + + var asAdmin = await executor.TryExecCqlAsync( + options.AdminUser, options.AdminPassword, + ScyllaCqlTemplates.BuildListAdminRoleCql(options), ct).ConfigureAwait(false); + return asAdmin.Succeeded; + } +} diff --git a/shared/Interfold.DatabaseBootstrap/SeedKeys.cs b/shared/Interfold.DatabaseBootstrap/SeedKeys.cs new file mode 100644 index 00000000..3f83a46a --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/SeedKeys.cs @@ -0,0 +1,43 @@ +using System.Globalization; +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.DatabaseBootstrap; + +/// Well-known internal.secrets rows produced by a successful seed run. +/// Rows whose selector returns empty are skipped by , so +/// unconfigured providers (OAuth, Firebase, FCM) leave the row absent and callers 503. +internal static class SeedKeys +{ + internal readonly record struct Entry(SecretsStoreKey Key, Func ValueSelector); + + private static readonly IReadOnlyList _all = + [ + new(SecretsStoreKeys.OAuthGoogleClientSecret, o => o.GoogleOAuthClientSecret ?? string.Empty), + new(SecretsStoreKeys.OAuthDiscordClientSecret, o => o.DiscordOAuthClientSecret ?? string.Empty), + new(SecretsStoreKeys.OAuthAppleClientSecret, o => o.AppleOAuthClientSecret ?? string.Empty), + new(SecretsStoreKeys.EncryptionPepper, o => o.EncryptionPepper), + new(SecretsStoreKeys.PostgresAdminUsername, o => o.AdminUser), + new(SecretsStoreKeys.PostgresAdminPassword, o => o.AdminPassword), + new(SecretsStoreKeys.ScyllaAdminUsername, o => o.ScyllaAdminUser), + new(SecretsStoreKeys.ScyllaAdminPassword, o => o.ScyllaAdminPassword), + new(SecretsStoreKeys.ScyllaContactPoints, o => o.ScyllaContactPoints), + new(SecretsStoreKeys.ScyllaLocalDatacenter, o => o.ScyllaLocalDatacenter), + new(SecretsStoreKeys.ScyllaUsername, o => o.ScyllaAppUser), + new(SecretsStoreKeys.ScyllaPassword, o => o.ScyllaAppPassword), + // scylla:keyspace is per-node env (OCTOCON_SCYLLA_KEYSPACE), never a store row. + new(SecretsStoreKeys.ScyllaPort, o => o.ScyllaPort.ToString(CultureInfo.InvariantCulture)), + new(SecretsStoreKeys.AuthJwtRsa256PrivatePem, o => o.JwtRsa256PrivateKeyPem ?? string.Empty), + new(SecretsStoreKeys.AuthJwtEs256PrivatePem, o => o.JwtEs256PrivateKeyPem ?? string.Empty), + new(SecretsStoreKeys.AuthDeepLinkSecret, o => o.DeepLinkSecret ?? string.Empty), + // Read pre-builder by SecretsPreBuildLoader (bare NpgsqlConnection) and written into + // Kestrel:Certificates:Default:Password on the frozen IConfigurationRoot. + new(SecretsStoreKeys.CertsLeafPfxPassword, o => o.LeafPfxPassword ?? string.Empty), + new(SecretsStoreKeys.FirebaseClientAndroid, o => o.FirebaseAndroidClientJson ?? string.Empty), + new(SecretsStoreKeys.FirebaseClientIos, o => o.FirebaseIosClientJson ?? string.Empty), + new(SecretsStoreKeys.FirebaseClientWeb, o => o.FirebaseWebClientJson ?? string.Empty), + // Private FCM v1 credential — absent → NullFCMService (push flow no-ops). + new(SecretsStoreKeys.FcmServiceAccountJson, o => o.FcmServiceAccountJson ?? string.Empty), + ]; + + internal static IReadOnlyList All => _all; +} diff --git a/shared/Interfold.DatabaseBootstrap/SqlEscape.cs b/shared/Interfold.DatabaseBootstrap/SqlEscape.cs new file mode 100644 index 00000000..c87e7a36 --- /dev/null +++ b/shared/Interfold.DatabaseBootstrap/SqlEscape.cs @@ -0,0 +1,10 @@ +namespace Interfold.DatabaseBootstrap; + +/// Doubles apostrophes in SQL/CQL string literals. Both Postgres and +/// Cassandra follow the same rule. The bootstrapper's password alphabet excludes the +/// apostrophe, so this is defence-in-depth for operator-supplied values. +public static class SqlEscape +{ + public static string Literal(string value) + => value.Replace("'", "''", StringComparison.Ordinal); +} diff --git a/shared/Interfold.Friendships.Contracts/Enums/FriendshipAction.cs b/shared/Interfold.Friendships.Contracts/Enums/FriendshipAction.cs new file mode 100644 index 00000000..52443686 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Enums/FriendshipAction.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Friendships.Contracts.Enums; + +/// +/// Outcome verb carried on FriendshipCommandResult.Action. Serialized into persisted +/// idempotency outcome payloads and HTTP response bodies, so the snake-case-lower wire +/// spellings ("accepted", "sent", …) are frozen. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum FriendshipAction +{ + [JsonStringEnumMemberName("accepted")] + Accepted, + + [JsonStringEnumMemberName("sent")] + Sent, + + [JsonStringEnumMemberName("removed")] + Removed, + + [JsonStringEnumMemberName("trusted")] + Trusted, + + [JsonStringEnumMemberName("untrusted")] + Untrusted, + + [JsonStringEnumMemberName("rejected")] + Rejected, + + [JsonStringEnumMemberName("cancelled")] + Cancelled, +} diff --git a/shared/Interfold.Friendships.Contracts/Events/FriendshipDomainEvents.cs b/shared/Interfold.Friendships.Contracts/Events/FriendshipDomainEvents.cs new file mode 100644 index 00000000..85edd050 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Events/FriendshipDomainEvents.cs @@ -0,0 +1,20 @@ +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Contracts.Events; + +public sealed record FriendshipAddedEvent(ScopedSystemId TargetSystemId, SystemId SystemId) : ITargetedClusterEvent; + +public sealed record FriendshipRemovedEvent(ScopedSystemId TargetSystemId, SystemId SystemId) : ITargetedClusterEvent; + +public sealed record FriendshipTrustedEvent(ScopedSystemId TargetSystemId, SystemId SystemId) : ITargetedClusterEvent; + +public sealed record FriendshipUntrustedEvent(ScopedSystemId TargetSystemId, SystemId SystemId) : ITargetedClusterEvent; + +public sealed record FriendRequestSentEvent(ScopedSystemId TargetSystemId, SystemId ToSystemId) : ITargetedClusterEvent; + +public sealed record FriendRequestReceivedEvent(ScopedSystemId TargetSystemId, SystemId FromSystemId) : ITargetedClusterEvent; + +public sealed record FriendRequestRemovedFromEvent(ScopedSystemId TargetSystemId, SystemId FromSystemId) : ITargetedClusterEvent; + +public sealed record FriendRequestRemovedToEvent(ScopedSystemId TargetSystemId, SystemId ToSystemId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Friendships.Contracts/FriendshipCommandResult.cs b/shared/Interfold.Friendships.Contracts/FriendshipCommandResult.cs new file mode 100644 index 00000000..3ec4c6bb --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/FriendshipCommandResult.cs @@ -0,0 +1,10 @@ +using Interfold.Friendships.Contracts.Enums; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Contracts; + +public sealed record FriendshipCommandResult(SystemId SystemId, SystemId TargetSystemId, FriendshipAction Action, bool Replay) : ICommandResult +{ + public FriendshipCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Friendships.Contracts/Ids/FriendLookup.cs b/shared/Interfold.Friendships.Contracts/Ids/FriendLookup.cs new file mode 100644 index 00000000..b6f42b90 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Ids/FriendLookup.cs @@ -0,0 +1,105 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Friendships.Contracts.Ids; + +/// Which registry lane a client-supplied friend-request handle routes to. +public enum FriendLookupKind +{ + /// Bare 7-char system id or "id:"-prefixed; → user_registry.user_id. + Id, + + /// Username lookup ("username:alice"); → per-region users_by_username. + Username, +} + +/// Strictly-parsed friend-request target: either a bare/prefixed system id or a +/// "username:..."-prefixed username. Discord snowflakes, scoped region ids, unknown +/// prefixes, and blank/half inputs fail and surface as 400 at route +/// binding. JSON emits verbatim so persisted command payload +/// hashes stay stable. +[JsonConverter(typeof(FriendLookupJsonConverter))] +public readonly record struct FriendLookup : IParsable +{ + public FriendLookupKind Kind { get; } + + /// After-prefix content — the value handed to the registry column. + public string Value { get; } + + /// Raw wire form retained verbatim for JSON, logging, and hash stability. + public string OriginalValue { get; } + + private FriendLookup(FriendLookupKind kind, string value, string originalValue) + { + Kind = kind; + Value = value; + OriginalValue = originalValue; + } + + // Widens to OriginalValue so IsNullOrWhiteSpace-style checks keep raw-wire semantics. + // Hazard: default(FriendLookup).OriginalValue is null; don't widen a default slot. + public static implicit operator string(FriendLookup value) => value.OriginalValue; + + public override string ToString() => OriginalValue; + + public static FriendLookup Parse(string s, IFormatProvider? provider) + => TryParse(s, provider, out var result) + ? result + : throw new FormatException($"'{s}' is not a valid FriendLookup (expected a bare id, 'id:', or 'username:')."); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out FriendLookup result) + { + result = default; + if (string.IsNullOrWhiteSpace(s)) + { + return false; + } + + var separator = s.IndexOf(':'); + if (separator < 0) + { + result = new FriendLookup(FriendLookupKind.Id, s, s); + return true; + } + + if (separator == 0 || separator >= s.Length - 1) + { + return false; + } + + var prefix = s[..separator]; + var afterColon = s[(separator + 1)..]; + + // Wire prefixes are lowercase-canonical; other lookup shapes (Discord, region tags) + // belong to different routes and fall through to false here. + switch (prefix.ToLowerInvariant()) + { + case "id": + result = new FriendLookup(FriendLookupKind.Id, afterColon, s); + return true; + case "username": + result = new FriendLookup(FriendLookupKind.Username, afterColon, s); + return true; + default: + return false; + } + } +} + +internal sealed class FriendLookupJsonConverter : JsonConverter +{ + public override FriendLookup Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var raw = reader.GetString(); + return FriendLookup.TryParse(raw, provider: null, out var result) + ? result + : throw new JsonException($"'{raw}' is not a valid FriendLookup JSON value (expected a bare id, 'id:', or 'username:')."); + } + + public override void Write(Utf8JsonWriter writer, FriendLookup value, JsonSerializerOptions options) + => writer.WriteStringValue(value.OriginalValue); +} diff --git a/shared/Interfold.Friendships.Contracts/Ids/FriendLookupExtensions.cs b/shared/Interfold.Friendships.Contracts/Ids/FriendLookupExtensions.cs new file mode 100644 index 00000000..fd78228c --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Ids/FriendLookupExtensions.cs @@ -0,0 +1,18 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Contracts.Ids; + +public static class FriendLookupExtensions +{ + // Route-binding self-check for the friend-request send path. Id shapes delegate to + // ScopedSystemId.RepresentsSameUserAs(SystemId) so raw and same-region-scoped inputs + // both self-reject; username shapes require a registry hop and return false so the + // command handler's post-resolution guard takes over. + public static bool RepresentsSameUserAs(this FriendLookup candidate, ScopedSystemId principal) + => candidate.Kind switch + { + FriendLookupKind.Id => principal.RepresentsSameUserAs(new SystemId(candidate.Value)), + FriendLookupKind.Username => false, + _ => false, + }; +} diff --git a/shared/Interfold.Friendships.Contracts/Interfold.Friendships.Contracts.csproj b/shared/Interfold.Friendships.Contracts/Interfold.Friendships.Contracts.csproj new file mode 100644 index 00000000..b55d35b3 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Interfold.Friendships.Contracts.csproj @@ -0,0 +1,12 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Friendships.Contracts/Models/Commands/FriendshipCommands.cs b/shared/Interfold.Friendships.Contracts/Models/Commands/FriendshipCommands.cs new file mode 100644 index 00000000..71a915f4 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Models/Commands/FriendshipCommands.cs @@ -0,0 +1,19 @@ +using Interfold.Friendships.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Contracts.Models.Commands; + +public sealed record RemoveFriendshipCommand(SystemId FriendSystemId); + +public sealed record SetFriendTrustCommand(SystemId FriendSystemId, bool Trusted); + +// TargetSystemId's name is frozen (persisted payload + idempotency hashes); the type is +// FriendLookup because the route accepts either a system id (bare or 'id:'-prefixed) or a +// username ('username:'-prefixed) — everything else fails route binding with a 400. +public sealed record SendFriendRequestCommand(FriendLookup TargetSystemId); + +public sealed record AcceptFriendRequestCommand(SystemId SourceSystemId); + +public sealed record RejectFriendRequestCommand(SystemId SourceSystemId); + +public sealed record CancelFriendRequestCommand(SystemId TargetSystemId); diff --git a/shared/Interfold.Friendships.Contracts/Models/Read/FriendshipModels.cs b/shared/Interfold.Friendships.Contracts/Models/Read/FriendshipModels.cs new file mode 100644 index 00000000..7853feea --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/Models/Read/FriendshipModels.cs @@ -0,0 +1,99 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Friendships.Contracts.Models.Read; + +// DiscordId here is the friend's real linked Discord id from the users table (exposed to +// friends over the wire); the raw-string converter keeps the body shape unchanged. +public sealed record FriendProfileReadModel( + SystemId Id, + Username? Username, + AvatarUrl? AvatarUrl, + AvatarSource? AvatarSource, + string? Description, + DiscordId? DiscordId +) : IAvatarBearing; + +// Fields carries the same per-alter custom-field entries as AlterReadModel.Fields; the Kotlin +// client reads it as List (id/name/type/value). Today's only producer +// (ScyllaFriendshipRepository.GetFrontingAsync) sends an empty list. +public sealed record FriendFrontingAlterReadModel( + AlterId Id, + string? Name, + string? Pronouns, + string? Description, + IReadOnlyList Fields, + AvatarUrl? AvatarUrl, + AvatarSource? AvatarSource, + IReadOnlyList ExtraImages, + HexColor? Color +) : IAvatarBearing; + +public sealed record FriendFrontingFrontReadModel( + AlterId AlterId, + string? Comment +); + +public sealed record FriendFrontingReadModel( + FriendFrontingAlterReadModel Alter, + FriendFrontingFrontReadModel Front, + bool Primary +); + +public record FriendshipModel( + FriendshipLevel Level, + DateTimeOffset Since +); + +public sealed record FriendshipReadModel( + FriendProfileReadModel Friend, + FriendshipModel Friendship, + IReadOnlyList Fronting +); + +public sealed record FriendRequestReadModel( + FriendProfileReadModel System, + FriendshipRequestModel Request +); + +public sealed record FriendshipRequestModel( + DateTimeOffset DateSent +); + + +public sealed record FriendRequestIndexReadModel( + IReadOnlyList Incoming, + IReadOnlyList Outgoing +); + +public enum SendFriendRequestOutcome +{ + Sent, + Accepted, + AlreadyFriends, + AlreadySent, + NoUser +} + +public enum FriendRequestMutationOutcome +{ + Ok, + AlreadyFriends, + NotRequested, + NoUser +} + +public static class FriendRequestMutationOutcomeExtensions +{ + public static EntityRef? ToRejectionEntityRef(this FriendRequestMutationOutcome outcome) + { + return outcome switch + { + FriendRequestMutationOutcome.AlreadyFriends => Interfold.Shared.Contracts.Operations.EntityRefs.FriendRequestAlreadyFriends, + FriendRequestMutationOutcome.NotRequested => Interfold.Shared.Contracts.Operations.EntityRefs.FriendRequestNotRequested, + FriendRequestMutationOutcome.NoUser => Interfold.Shared.Contracts.Operations.EntityRefs.FriendRequestNoUser, + _ => null + }; + } +} diff --git a/shared/Interfold.Friendships.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Friendships.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..c7b69762 --- /dev/null +++ b/shared/Interfold.Friendships.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,9 @@ +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Friendships.Contracts; + +public sealed record FriendRequestSocketPayload(FriendshipRequestModel Request, FriendProfileReadModel System) : ISocketPayload; + +public sealed record FriendIdSocketPayload(SystemId FriendId) : ISocketPayload; diff --git a/shared/Interfold.Friendships.Domain/Abstractions/Repository/IFriendshipRepository.cs b/shared/Interfold.Friendships.Domain/Abstractions/Repository/IFriendshipRepository.cs new file mode 100644 index 00000000..1bd20c41 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/Abstractions/Repository/IFriendshipRepository.cs @@ -0,0 +1,33 @@ +using Interfold.Friendships.Contracts.Ids; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Domain.Abstractions.Repository; + +public interface IFriendshipRepository +{ + Task ResolveUserIdAsync(FriendLookup lookup, CancellationToken cancellationToken = default); + + Task GetFriendshipLevelAsync(SystemId systemId, SystemId? viewerSystemId, CancellationToken cancellationToken = default); + + Task> ListFriendshipsAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default); + + Task RemoveFriendshipAsync(SystemId systemId, SystemId friendSystemId, CancellationToken cancellationToken = default); + + Task SetTrustedAsync(SystemId systemId, SystemId friendSystemId, bool trusted, CancellationToken cancellationToken = default); + + Task GetFriendRequestsAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task SendRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default); + + Task AcceptRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default); + + Task RejectRequestAsync(SystemId systemId, SystemId sourceSystemId, CancellationToken cancellationToken = default); + + Task CancelRequestAsync(SystemId systemId, SystemId targetSystemId, CancellationToken cancellationToken = default); + + Task> DeleteAllForSystemAsync(SystemId systemId, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Friendships.Domain/AcceptFriendRequestCommandHandler.cs b/shared/Interfold.Friendships.Domain/AcceptFriendRequestCommandHandler.cs new file mode 100644 index 00000000..9ee0d2a1 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/AcceptFriendRequestCommandHandler.cs @@ -0,0 +1,63 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class AcceptFriendRequestCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public AcceptFriendRequestCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendRequestAccept; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var canonicalSourceSystemId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + command.Payload.SourceSystemId); + + var canonicalPrincipalId = FriendshipCommandNormalization.CanonicalPrincipalForPeer( + command.Payload.SourceSystemId, + command.PrincipalId); + + var outcome = await _repository.AcceptRequestAsync( + command.PrincipalId, + canonicalSourceSystemId, + cancellationToken); + + if (FriendshipCommandFlow.RejectIfMutationOutcomeFailed(command, outcome) is { } rejection) + return rejection; + + await _eventBus.PublishFriendshipAddedBothWaysAsync( + canonicalPrincipalId, + canonicalSourceSystemId, + cancellationToken); + + await _eventBus.PublishRequestRemovedFromThenToAsync( + canonicalPrincipalId, + canonicalSourceSystemId, + cancellationToken); + + return FriendshipCommandFlow.Success(command.PrincipalId, canonicalSourceSystemId, FriendshipAction.Accepted); + } + +} diff --git a/shared/Interfold.Friendships.Domain/CancelFriendRequestCommandHandler.cs b/shared/Interfold.Friendships.Domain/CancelFriendRequestCommandHandler.cs new file mode 100644 index 00000000..406b2415 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/CancelFriendRequestCommandHandler.cs @@ -0,0 +1,58 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class CancelFriendRequestCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public CancelFriendRequestCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendRequestCancel; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var canonicalTargetSystemId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + command.Payload.TargetSystemId); + + var canonicalPrincipalId = FriendshipCommandNormalization.CanonicalPrincipalForPeer( + command.Payload.TargetSystemId, + command.PrincipalId); + + var outcome = await _repository.CancelRequestAsync( + command.PrincipalId, + canonicalTargetSystemId, + cancellationToken); + + if (FriendshipCommandFlow.RejectIfMutationOutcomeFailed(command, outcome) is { } rejection) + return rejection; + + await _eventBus.PublishRequestRemovedToThenFromAsync( + canonicalPrincipalId, + canonicalTargetSystemId, + cancellationToken); + + return FriendshipCommandFlow.Success(command.PrincipalId, canonicalTargetSystemId, FriendshipAction.Cancelled); + } + +} diff --git a/shared/Interfold.Friendships.Domain/FriendshipCommandFlow.cs b/shared/Interfold.Friendships.Domain/FriendshipCommandFlow.cs new file mode 100644 index 00000000..8f18b5ae --- /dev/null +++ b/shared/Interfold.Friendships.Domain/FriendshipCommandFlow.cs @@ -0,0 +1,47 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +internal static class FriendshipCommandFlow +{ + public static CommandExecutionResult Success( + SystemId principalId, + SystemId peerSystemId, + FriendshipAction action) + => CommandExecutionResult.Success( + new FriendshipCommandResult(principalId, peerSystemId, action, Replay: false)); + + public static CommandExecutionResult? RejectIfMutationOutcomeFailed( + CommandEnvelope command, + FriendRequestMutationOutcome outcome) + { + if (outcome.ToRejectionEntityRef() is not { } rejectionEntityRef) + return null; + + return CommandHandler.RejectInvariant(command.OperationId, rejectionEntityRef); + } + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failedEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failedEntityRef) + : null; + + public static async Task?> ExecuteMutationOrRejectAsync( + CommandEnvelope command, + Func> mutateAsync, + EntityRef failedEntityRef, + CancellationToken cancellationToken = default) + { + var succeeded = await mutateAsync(cancellationToken); + return RejectIfMutationFailed(command, succeeded, failedEntityRef); + } +} diff --git a/shared/Interfold.Friendships.Domain/FriendshipCommandNormalization.cs b/shared/Interfold.Friendships.Domain/FriendshipCommandNormalization.cs new file mode 100644 index 00000000..332a3416 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/FriendshipCommandNormalization.cs @@ -0,0 +1,12 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Domain; + +internal static class FriendshipCommandNormalization +{ + public static ScopedSystemId ComposePeerId(ScopedSystemId principalId, SystemId peerSystemId) + => ScopedSystemId.Compose(principalId.Region, peerSystemId); + + public static ScopedSystemId CanonicalPrincipalForPeer(SystemId peerSystemId, ScopedSystemId principalId) + => FriendshipIdNormalization.CanonicalizeForPrincipal(peerSystemId, principalId); +} diff --git a/shared/Interfold.Friendships.Domain/FriendshipEventBusExtensions.cs b/shared/Interfold.Friendships.Domain/FriendshipEventBusExtensions.cs new file mode 100644 index 00000000..24b9130f --- /dev/null +++ b/shared/Interfold.Friendships.Domain/FriendshipEventBusExtensions.cs @@ -0,0 +1,53 @@ +using Interfold.Friendships.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +// Intent-named pair-publish helpers on IClusterEventBus for friendship events. Each +// method fans a fixed-order pair so both ends of a friendship / request always move +// together on the bus. Publish-only — orchestration lives in FriendshipCommandFlow. +internal static class FriendshipEventBusExtensions +{ + public static async ValueTask PublishFriendshipAddedBothWaysAsync( + this IClusterEventBus eventBus, + ScopedSystemId from, + ScopedSystemId to, + CancellationToken cancellationToken = default) + { + await eventBus.PublishAsync(new FriendshipAddedEvent(from, to), cancellationToken); + await eventBus.PublishAsync(new FriendshipAddedEvent(to, from), cancellationToken); + } + + public static async ValueTask PublishFriendshipRemovedBothWaysAsync( + this IClusterEventBus eventBus, + ScopedSystemId from, + ScopedSystemId to, + CancellationToken cancellationToken = default) + { + await eventBus.PublishAsync(new FriendshipRemovedEvent(from, to), cancellationToken); + await eventBus.PublishAsync(new FriendshipRemovedEvent(to, from), cancellationToken); + } + + // Sender-cancel ordering: sender's outbound first, then recipient's inbound. + public static async ValueTask PublishRequestRemovedFromThenToAsync( + this IClusterEventBus eventBus, + ScopedSystemId from, + ScopedSystemId to, + CancellationToken cancellationToken = default) + { + await eventBus.PublishAsync(new FriendRequestRemovedFromEvent(from, to), cancellationToken); + await eventBus.PublishAsync(new FriendRequestRemovedToEvent(to, from), cancellationToken); + } + + // Recipient-reject/accept ordering: recipient's inbound first, then sender's outbound. + public static async ValueTask PublishRequestRemovedToThenFromAsync( + this IClusterEventBus eventBus, + ScopedSystemId to, + ScopedSystemId from, + CancellationToken cancellationToken = default) + { + await eventBus.PublishAsync(new FriendRequestRemovedToEvent(to, from), cancellationToken); + await eventBus.PublishAsync(new FriendRequestRemovedFromEvent(from, to), cancellationToken); + } +} diff --git a/shared/Interfold.Friendships.Domain/FriendshipIdNormalization.cs b/shared/Interfold.Friendships.Domain/FriendshipIdNormalization.cs new file mode 100644 index 00000000..54594306 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/FriendshipIdNormalization.cs @@ -0,0 +1,18 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Friendships.Domain; + +// Canonicalise a friendship candidate id into the principal's region so downstream +// publishers, routers, and repos all see byte-identical scoped ids. +internal static class FriendshipIdNormalization +{ + // Compose candidate in principal's region when principal parses scoped; else pass through. + public static ScopedSystemId CanonicalizeForPrincipal(SystemId principalSystemId, ScopedSystemId candidateSystemId) + { + if (ScopedSystemId.TryParseScoped(principalSystemId, out var scopedPrincipal)) + { + return ScopedSystemId.Compose(scopedPrincipal.Region, candidateSystemId); + } + return candidateSystemId; + } +} diff --git a/shared/Interfold.Friendships.Domain/Interfold.Friendships.Domain.csproj b/shared/Interfold.Friendships.Domain/Interfold.Friendships.Domain.csproj new file mode 100644 index 00000000..ab6fbdd9 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/Interfold.Friendships.Domain.csproj @@ -0,0 +1,12 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Friendships.Domain/RejectFriendRequestCommandHandler.cs b/shared/Interfold.Friendships.Domain/RejectFriendRequestCommandHandler.cs new file mode 100644 index 00000000..f6e2ed63 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/RejectFriendRequestCommandHandler.cs @@ -0,0 +1,57 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class RejectFriendRequestCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public RejectFriendRequestCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendRequestReject; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var canonicalSourceSystemId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + command.Payload.SourceSystemId); + var canonicalPrincipalId = FriendshipCommandNormalization.CanonicalPrincipalForPeer( + canonicalSourceSystemId, + command.PrincipalId); + + var outcome = await _repository.RejectRequestAsync( + command.PrincipalId, + canonicalSourceSystemId, + cancellationToken); + + if (FriendshipCommandFlow.RejectIfMutationOutcomeFailed(command, outcome) is { } rejection) + return rejection; + + await _eventBus.PublishRequestRemovedFromThenToAsync( + canonicalPrincipalId, + canonicalSourceSystemId, + cancellationToken); + + return FriendshipCommandFlow.Success(command.PrincipalId, canonicalSourceSystemId, FriendshipAction.Rejected); + } + +} diff --git a/shared/Interfold.Friendships.Domain/RemoveFriendshipCommandHandler.cs b/shared/Interfold.Friendships.Domain/RemoveFriendshipCommandHandler.cs new file mode 100644 index 00000000..f52e6652 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/RemoveFriendshipCommandHandler.cs @@ -0,0 +1,56 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class RemoveFriendshipCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public RemoveFriendshipCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendshipRemove; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var canonicalFriendSystemId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + command.Payload.FriendSystemId); + var canonicalPrincipalId = FriendshipCommandNormalization.CanonicalPrincipalForPeer( + canonicalFriendSystemId, + command.PrincipalId); + + if (await FriendshipCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _repository.RemoveFriendshipAsync(command.PrincipalId, canonicalFriendSystemId, ct), + EntityRefs.FriendshipNotFound, + cancellationToken) is { } removalReject) + return removalReject; + + await _eventBus.PublishFriendshipRemovedBothWaysAsync( + canonicalPrincipalId, + canonicalFriendSystemId, + cancellationToken); + + return FriendshipCommandFlow.Success(command.PrincipalId, canonicalFriendSystemId, FriendshipAction.Removed); + } + +} diff --git a/shared/Interfold.Friendships.Domain/SendFriendRequestCommandHandler.cs b/shared/Interfold.Friendships.Domain/SendFriendRequestCommandHandler.cs new file mode 100644 index 00000000..d5613931 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/SendFriendRequestCommandHandler.cs @@ -0,0 +1,113 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Events; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class SendFriendRequestCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public SendFriendRequestCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendRequestSend; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var resolvedTargetSystemId = await _repository.ResolveUserIdAsync( + command.Payload.TargetSystemId, + cancellationToken); + + if (resolvedTargetSystemId is null) + { + return RejectInvariant(command, EntityRefs.FriendRequestNoUser); + } + + var targetSystemId = resolvedTargetSystemId.Value; + + // The controller's route-segment self-check catches the trivial + // "PUT /api/friend-requests/nam:principal-a" case without a repo hop; this + // resolved-id check catches the routed-through-a-username / discord-id / + // raw-bare-id shapes where the client couldn't (or didn't) know their own scoped + // id. Both guards return the same rejection so callers see one uniform error. + if (targetSystemId == command.PrincipalId.AsSystemId()) + { + return RejectInvariant(command, EntityRefs.FriendRequestNoUser); + } + + // The resolver returns a scoped-shape id today (the account repos all compose one + // before returning). Route through Compose one more time so we still hand the + // event publisher a ScopedSystemId if a repo path ever emits a bare id — the + // principal's region is the safe fallback for the same-region friendship case. + var targetScopedId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + targetSystemId); + + var outcome = await _repository.SendRequestAsync( + command.PrincipalId, + targetSystemId, + cancellationToken); + + if (outcome is SendFriendRequestOutcome.AlreadyFriends) + { + return RejectInvariant(command, EntityRefs.FriendRequestAlreadyFriends); + } + + if (outcome is SendFriendRequestOutcome.AlreadySent) + { + return RejectInvariant(command, EntityRefs.FriendRequestAlreadySent); + } + + if (outcome is SendFriendRequestOutcome.NoUser) + { + return RejectInvariant(command, EntityRefs.FriendRequestNoUser); + } + + var action = outcome is SendFriendRequestOutcome.Accepted ? FriendshipAction.Accepted : FriendshipAction.Sent; + + //TODO: Check this path sends the required events + if (outcome is SendFriendRequestOutcome.Accepted) + { + await _eventBus.PublishFriendshipAddedBothWaysAsync( + command.PrincipalId, + targetScopedId, + cancellationToken); + + await _eventBus.PublishAsync(new FriendRequestRemovedToEvent( + targetScopedId, + command.PrincipalId), cancellationToken); + } + else + { + await _eventBus.PublishAsync(new FriendRequestSentEvent( + command.PrincipalId, + targetSystemId), cancellationToken); + + await _eventBus.PublishAsync(new FriendRequestReceivedEvent( + targetScopedId, + command.PrincipalId), cancellationToken); + } + + return FriendshipCommandFlow.Success(command.PrincipalId, targetSystemId, action); + } + +} diff --git a/shared/Interfold.Friendships.Domain/SetFriendTrustCommandHandler.cs b/shared/Interfold.Friendships.Domain/SetFriendTrustCommandHandler.cs new file mode 100644 index 00000000..73578e87 --- /dev/null +++ b/shared/Interfold.Friendships.Domain/SetFriendTrustCommandHandler.cs @@ -0,0 +1,69 @@ +using Interfold.Friendships.Contracts; +using Interfold.Friendships.Contracts.Enums; +using Interfold.Friendships.Contracts.Events; +using Interfold.Friendships.Contracts.Models.Commands; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Friendships.Domain; + +public sealed class SetFriendTrustCommandHandler : IdempotentCommandHandler +{ + private readonly IFriendshipRepository _repository; + private readonly IClusterEventBus _eventBus; + + public SetFriendTrustCommandHandler( + IFriendshipRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FriendshipTrust; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var canonicalFriendSystemId = FriendshipCommandNormalization.ComposePeerId( + command.PrincipalId, + command.Payload.FriendSystemId); + + if (await FriendshipCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _repository.SetTrustedAsync( + command.PrincipalId, + canonicalFriendSystemId, + command.Payload.Trusted, + ct), + EntityRefs.FriendshipNotFound, + cancellationToken) is { } trustReject) + return trustReject; + + if (command.Payload.Trusted) + { + await _eventBus.PublishAsync( + new FriendshipTrustedEvent(command.PrincipalId, canonicalFriendSystemId), + cancellationToken); + } + else + { + await _eventBus.PublishAsync( + new FriendshipUntrustedEvent(command.PrincipalId, canonicalFriendSystemId), + cancellationToken); + } + + return FriendshipCommandFlow.Success( + command.PrincipalId, + canonicalFriendSystemId, + command.Payload.Trusted ? FriendshipAction.Trusted : FriendshipAction.Untrusted); + } + +} diff --git a/shared/Interfold.Fronting.Contracts/Events/FrontingDomainEvents.cs b/shared/Interfold.Fronting.Contracts/Events/FrontingDomainEvents.cs new file mode 100644 index 00000000..f6b71066 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Events/FrontingDomainEvents.cs @@ -0,0 +1,29 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Contracts.Events; + +/// Published to IClusterEventBus on any fronting-state change. Consumed on +/// primary nodes by FrontNotifierBackgroundService for FCM fan-out (Phase N). +public sealed record FrontingStateChangedEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record FrontDeletedEvent(ScopedSystemId TargetSystemId, FrontId FrontId) : ITargetedClusterEvent; + +/// Emits socket "fronting_started". +public sealed record FrontingStartedEvent(ScopedSystemId TargetSystemId, FrontId FrontId) : ITargetedClusterEvent; + +/// Emits socket "fronting_ended". +public sealed record FrontingEndedEvent(ScopedSystemId TargetSystemId, AlterId AlterId) : ITargetedClusterEvent; + +/// Emits socket "fronting_set". +public sealed record FrontingSetEvent(ScopedSystemId TargetSystemId, FrontId FrontId) : ITargetedClusterEvent; + +/// Emits socket "fronting_bulk". +public sealed record FrontingBulkUpdatedEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +/// Emits socket "front_updated" after a front comment change. +public sealed record FrontCommentUpdatedEvent(ScopedSystemId TargetSystemId, FrontId FrontId) : ITargetedClusterEvent; + +/// Emits socket "primary_front". +public sealed record FrontingPrimaryChangedEvent(ScopedSystemId TargetSystemId, AlterId? AlterId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Fronting.Contracts/FrontCommandResult.cs b/shared/Interfold.Fronting.Contracts/FrontCommandResult.cs new file mode 100644 index 00000000..fa816ab3 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/FrontCommandResult.cs @@ -0,0 +1,10 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Contracts; + +public sealed record FrontCommandResult(SystemId SystemId, AlterId? AlterId, FrontId? FrontId, bool Replay) : ICommandResult +{ + public FrontCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Fronting.Contracts/FrontingTaskNames.cs b/shared/Interfold.Fronting.Contracts/FrontingTaskNames.cs new file mode 100644 index 00000000..9e74fbd4 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/FrontingTaskNames.cs @@ -0,0 +1,8 @@ +namespace Interfold.Shared.Contracts; + +/// Fronting-owned singleton task names. +public static class FrontingTaskNames +{ + /// Batched push-notification flush for fronting changes. + public static readonly SingletonTaskName FrontNotifier = new("front_notifier"); +} diff --git a/shared/Interfold.Fronting.Contracts/Ids/FrontId.cs b/shared/Interfold.Fronting.Contracts/Ids/FrontId.cs new file mode 100644 index 00000000..194ad191 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Ids/FrontId.cs @@ -0,0 +1,40 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Contracts.Ids; + +/// Strongly-typed wrapper around a front id (Guid, wire form is 32-char lowercase hex). +[JsonConverter(typeof(FrontIdJsonConverter))] +public readonly record struct FrontId(Guid Value) : IParsable +{ + public static readonly FrontId Empty = new(Guid.Empty); + + public static explicit operator FrontId(Guid value) => new(value); + public static implicit operator Guid(FrontId value) => value.Value; + + public override string ToString() => Value.ToString("N"); + + public static FrontId Parse(string s, IFormatProvider? provider) + => UuidString.TryParse(s, out var g) + ? new(g) + : throw new FormatException($"Value '{s}' is not a valid FrontId."); + + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out FrontId result) + { + if (s is null || !UuidString.TryParse(s, out var g)) { result = default; return false; } + result = new FrontId(g); + return true; + } + + public const int MaxCommentLength = 50; + + public static bool IsValidComment(string? comment) => (comment?.Length ?? 0) <= MaxCommentLength; +} + +internal sealed class FrontIdJsonConverter : GuidIdJsonConverter +{ + protected override FrontId Create(Guid value) => new(value); + protected override Guid GetValue(FrontId value) => value.Value; + protected override string TypeLabel => nameof(FrontId); +} diff --git a/shared/Interfold.Fronting.Contracts/Interfold.Fronting.Contracts.csproj b/shared/Interfold.Fronting.Contracts/Interfold.Fronting.Contracts.csproj new file mode 100644 index 00000000..699770d2 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Interfold.Fronting.Contracts.csproj @@ -0,0 +1,19 @@ + + + net10.0 + enable + enable + + + + + + + + + diff --git a/shared/Interfold.Fronting.Contracts/Models/Commands/FrontingCommands.cs b/shared/Interfold.Fronting.Contracts/Models/Commands/FrontingCommands.cs new file mode 100644 index 00000000..ebfca65a --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Models/Commands/FrontingCommands.cs @@ -0,0 +1,20 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Contracts.Models.Commands; + +public sealed record StartFrontCommand(AlterId AlterId, string? Comment); + +public sealed record EndFrontCommand(AlterId AlterId); + +public sealed record SetPrimaryFrontCommand(AlterId? AlterId); + +public sealed record FrontStartItem(AlterId AlterId, string? Comment); + +public sealed record BulkUpdateFrontCommand(IReadOnlyList Start, IReadOnlyList End); + +public sealed record SetFrontCommand(AlterId AlterId, string? Comment); + +public sealed record DeleteFrontByIdCommand(FrontId FrontId); + +public sealed record UpdateFrontCommentCommand(FrontId FrontId, string Comment); diff --git a/shared/Interfold.Fronting.Contracts/Models/Read/FrontReadModels.cs b/shared/Interfold.Fronting.Contracts/Models/Read/FrontReadModels.cs new file mode 100644 index 00000000..bf8ee4fd --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Models/Read/FrontReadModels.cs @@ -0,0 +1,22 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Contracts.Models.Read; + +public sealed record FrontActiveReadModel( + BareAlter Alter, + FrontHistoryReadModel Front, + bool Primary +); + +public sealed record FrontHistoryReadModel( + FrontId Id, + AlterId AlterId, + string? Comment, + DateTimeOffset TimeStart, + DateTimeOffset? TimeEnd, + SystemId UserId +); + +// Front*Request + FrontStartEntry + FrontStartedResponse live in Interfold.Fronting.Contracts (Phase-3 migration). diff --git a/shared/Interfold.Fronting.Contracts/Models/Read/FrontRequestModels.cs b/shared/Interfold.Fronting.Contracts/Models/Read/FrontRequestModels.cs new file mode 100644 index 00000000..7b6847df --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/Models/Read/FrontRequestModels.cs @@ -0,0 +1,54 @@ +using System.Text.Json.Serialization; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Validation; + +namespace Interfold.Fronting.Contracts.Models.Read; + +public sealed record FrontBulkUpdateRequest( + IReadOnlyList Start, + IReadOnlyList End +); + +public sealed record FrontStartEntry(AlterId AlterId, string? Comment = null); + +// Front start/end/set/primary requests take the target alter as `id` — the only wire +// name the Kotlin client (and the replay fixtures) ever send. The former `alter_id` +// alias was exercised only by the server's own tests and has been removed. +// +// Attribute targets on record positional parameters: +// * JsonPropertyName has to live on the compiler-generated property so +// System.Text.Json's property-based deserialiser sees it (the `property:` target). +// * ValidAlterId has to live on the *constructor parameter*, not the property, +// because ASP.NET Core MVC's ObjectModelValidator calls +// ThrowIfRecordTypeHasValidationOnProperties() on records and hard-fails with a +// 500 the moment it finds validation metadata on a record property. Attributes +// without an explicit target on a record positional parameter default to the +// parameter target, which is exactly where MVC's parameter-binding validator +// picks them up. +public sealed record FrontStartRequest( + [property: JsonPropertyName("id")][ValidAlterId] AlterId Id = default, + string? Comment = null +); + +public sealed record FrontEndRequest( + [property: JsonPropertyName("id")][ValidAlterId] AlterId Id = default +); + +// FrontPrimaryRequest.Id stays nullable — `null` legitimately clears the primary front +// — so ValidAlterId is applied with AllowNull = true; a caller who sends `id: 0` +// still gets a 400 with the same shape as every other AlterId payload. +public sealed record FrontPrimaryRequest( + [property: JsonPropertyName("id")][ValidAlterId(AllowNull = true)] AlterId? Id = null +); + +public sealed record FrontSetRequest( + [property: JsonPropertyName("id")][ValidAlterId] AlterId Id = default, + string? Comment = null +); + +public sealed record FrontCommentRequest( + string Comment +); + +public sealed record FrontStartedResponse(FrontId FrontId); diff --git a/shared/Interfold.Fronting.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Fronting.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..22a942d2 --- /dev/null +++ b/shared/Interfold.Fronting.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,13 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Socket.Contracts; + +namespace Interfold.Fronting.Contracts; + +public sealed record SocketBatchedFrontsPayload(int BatchIndex, int TotalBatches, IReadOnlyList Fronts) : ISocketPayload; + +public sealed record FrontSocketPayload(FrontActiveReadModel Front) : ISocketPayload; + +public sealed record FrontsSocketPayload(IReadOnlyList Fronts) : ISocketPayload; + +public sealed record FrontIdSocketPayload(FrontId FrontId) : ISocketPayload; diff --git a/shared/Interfold.Fronting.Domain/Abstractions/IFCMService.cs b/shared/Interfold.Fronting.Domain/Abstractions/IFCMService.cs new file mode 100644 index 00000000..2526105b --- /dev/null +++ b/shared/Interfold.Fronting.Domain/Abstractions/IFCMService.cs @@ -0,0 +1,13 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions; + +/// Pushes FCM notifications on fronting state changes (mirrors legacy +/// Octocon.Global.FrontNotifier). +public interface IFCMService +{ + Task NotifyFrontingChangedAsync( + SystemId systemId, + IReadOnlyList currentAlterIds, + CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Fronting.Domain/Abstractions/Repository/IFrontingRepository.cs b/shared/Interfold.Fronting.Domain/Abstractions/Repository/IFrontingRepository.cs new file mode 100644 index 00000000..f9efcf6e --- /dev/null +++ b/shared/Interfold.Fronting.Domain/Abstractions/Repository/IFrontingRepository.cs @@ -0,0 +1,46 @@ +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.Domain.Abstractions.Repository; + +public interface IFrontingRepository +{ + Task IsFrontingAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task StartAsync(SystemId systemId, AlterId alterId, string? comment, DateTimeOffset startedAt, CancellationToken cancellationToken = default); + + Task EndAsync(SystemId systemId, AlterId alterId, DateTimeOffset endedAt, CancellationToken cancellationToken = default); + + Task SetPrimaryAsync(SystemId systemId, AlterId? alterId, CancellationToken cancellationToken = default); + + Task> ListActiveAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task> ListActiveGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default + ); + + Task> ListHistoryBetweenAsync( + SystemId systemId, + DateTimeOffset startInclusive, + DateTimeOffset endInclusive, + CancellationToken cancellationToken = default); + + /// Every front on record for , open + closed, ordered + /// by time_start descending. Rows with a null time_start are dropped — + /// export path only surfaces populated rows (mirrors accounts.ex:1215's filter). Bypasses + /// the fronts_by_time denormalisation because it's only populated on close. + Task> ListAllAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetActiveByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default); + + Task GetHistoryEntryByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default); + + Task EndByFrontIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default); + + Task DeleteFrontByIdAsync(SystemId systemId, FrontId frontId, CancellationToken cancellationToken = default); + + Task UpdateCommentByFrontIdAsync(SystemId systemId, FrontId frontId, string comment, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/BulkUpdateFrontCommandHandler.cs b/shared/Interfold.Fronting.Domain/BulkUpdateFrontCommandHandler.cs new file mode 100644 index 00000000..364dff14 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/BulkUpdateFrontCommandHandler.cs @@ -0,0 +1,68 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class BulkUpdateFrontCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + private readonly TimeProvider _timeProvider; + + public BulkUpdateFrontCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + TimeProvider timeProvider) : base(idempotencyStore) + { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + _timeProvider = timeProvider; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingBulkUpdate; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (FrontingCommandFlow.RejectIfAnyInvalidAlterId( + command, + command.Payload.Start.Select(x => x.AlterId).Concat(command.Payload.End)) is { } invalidAlterIdReject) + return invalidAlterIdReject; + + var invalidStartComment = command.Payload.Start.FirstOrDefault(x => !FrontId.IsValidComment(x.Comment)); + if (invalidStartComment is not null && + FrontingCommandFlow.RejectIfInvalidComment(command, invalidStartComment.Comment) is { } invalidCommentReject) + return invalidCommentReject; + + var operationTime = _timeProvider.GetUtcNow(); + await FrontingCommandFlow.EndAltersBestEffortAsync( + _frontingRepository, + command.PrincipalId, + command.Payload.End, + operationTime, + cancellationToken); + + await FrontingCommandFlow.StartAltersIfNotFrontingAsync( + _frontingRepository, + command.PrincipalId, + command.Payload.Start, + operationTime, + cancellationToken); + + // Emit granular event for socket layer to handle fronting_bulk + await _eventBus.PublishStateChangedAndBulkUpdatedAsync(command.PrincipalId, cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, alterId: null, frontId: null); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/DeleteFrontByIdCommandHandler.cs b/shared/Interfold.Fronting.Domain/DeleteFrontByIdCommandHandler.cs new file mode 100644 index 00000000..e4db6aa5 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/DeleteFrontByIdCommandHandler.cs @@ -0,0 +1,64 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class DeleteFrontByIdCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteFrontByIdCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var (resolution, rejection) = await FrontingCommandFlow.ResolveFrontByIdAfterValidationOrRejectAsync( + command, + _frontingRepository, + command.Payload.FrontId, + cancellationToken); + if (rejection is not null) + return rejection; + + if (resolution!.WasActive) + { + if (await FrontingCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _frontingRepository.EndByFrontIdAsync(command.PrincipalId, command.Payload.FrontId, ct), + EntityRefs.FrontingDeleteFailed, + cancellationToken) is { } deleteReject) + return deleteReject; + } + + if (await FrontingCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _frontingRepository.DeleteFrontByIdAsync(command.PrincipalId, command.Payload.FrontId, ct), + EntityRefs.FrontingDeleteFailed, + cancellationToken) is { } historyDeleteReject) + return historyDeleteReject; + + var alterId = resolution.AlterId; + + await _eventBus.PublishDeletedAsync(command.PrincipalId, command.Payload.FrontId, resolution.WasActive, cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, alterId, command.Payload.FrontId); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/EndFrontCommandHandler.cs b/shared/Interfold.Fronting.Domain/EndFrontCommandHandler.cs new file mode 100644 index 00000000..5565dc86 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/EndFrontCommandHandler.cs @@ -0,0 +1,62 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class EndFrontCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + private readonly TimeProvider _timeProvider; + + public EndFrontCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + TimeProvider timeProvider) : base(idempotencyStore) + { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + _timeProvider = timeProvider; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingEnd; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (await FrontingCommandFlow.RejectIfInvalidOrNotFrontingAsync(command, _frontingRepository, command.Payload.AlterId, cancellationToken) is { } notFrontingReject) + return notFrontingReject; + + var activeFronts = await _frontingRepository.ListActiveAsync(command.PrincipalId, cancellationToken); + var endedFrontWasPrimary = activeFronts.Any(front => + front.Alter.Id == command.Payload.AlterId && front.Primary); + + if (await FrontingCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _frontingRepository.EndAsync(command.PrincipalId, command.Payload.AlterId, _timeProvider.GetUtcNow(), ct), + EntityRefs.FrontingEndFailed, + cancellationToken) is { } endReject) + return endReject; + + // Emit granular event for socket layer to handle fronting_ended + await _eventBus.PublishStateChangedAndEndedAsync(command.PrincipalId, command.Payload.AlterId, cancellationToken); + + await _eventBus.PublishPrimaryClearedIfNeededAsync( + command.PrincipalId, + endedFrontWasPrimary, + cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, command.Payload.AlterId, frontId: null); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/FrontingCommandFlow.cs b/shared/Interfold.Fronting.Domain/FrontingCommandFlow.cs new file mode 100644 index 00000000..b905239e --- /dev/null +++ b/shared/Interfold.Fronting.Domain/FrontingCommandFlow.cs @@ -0,0 +1,204 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +internal static class FrontingCommandFlow +{ + public sealed record FrontByIdResolution(FrontActiveReadModel? Active, FrontHistoryReadModel? History) + { + public bool WasActive => Active is not null; + + public AlterId AlterId => Active?.Front.AlterId ?? History!.AlterId; + } + + public static CommandExecutionResult? RejectIfInvalidFrontId( + CommandEnvelope command, + FrontId frontId) + => frontId == FrontId.Empty + ? CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingInvalidFrontId) + : null; + + public static CommandExecutionResult? RejectIfInvalidComment( + CommandEnvelope command, + string? comment) + => !FrontId.IsValidComment(comment) + ? CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingInvalidComment) + : null; + + public static CommandExecutionResult? RejectIfInvalidAlterId( + CommandEnvelope command, + AlterId alterId) + => CommandHandler.RejectIfAlterIdOutOfRange( + command.OperationId, + alterId, + EntityRefs.FrontingInvalidAlterId); + + public static CommandExecutionResult? RejectIfAnyInvalidAlterId( + CommandEnvelope command, + IEnumerable alterIds) + => alterIds.Any(alterId => alterId.Value < 1) + ? CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingInvalidAlterId) + : null; + + public static CommandExecutionResult Success( + ScopedSystemId systemId, + AlterId? alterId, + FrontId? frontId) + => CommandExecutionResult.Success(new FrontCommandResult(systemId, alterId, frontId, Replay: false)); + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failedEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failedEntityRef) + : null; + + public static async Task?> ExecuteMutationOrRejectAsync( + CommandEnvelope command, + Func> mutateAsync, + EntityRef failedEntityRef, + CancellationToken cancellationToken = default) + { + var succeeded = await mutateAsync(cancellationToken); + return RejectIfMutationFailed(command, succeeded, failedEntityRef); + } + + public static (FrontId? FrontId, CommandExecutionResult? Rejection) GetStartedFrontIdOrReject( + CommandEnvelope command, + FrontId? startedFrontId) + => startedFrontId is null + ? (null, CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingStartFailed)) + : (startedFrontId, null); + + public static async Task<(FrontActiveReadModel? Active, CommandExecutionResult? Rejection)> GetActiveFrontByIdOrRejectAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + FrontId frontId, + CancellationToken cancellationToken = default) + { + var active = await frontingRepository.GetActiveByFrontIdAsync(command.PrincipalId, frontId, cancellationToken); + if (active is null) + { + return (null, CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingNoFront)); + } + + return (active, null); + } + + public static async Task<(FrontActiveReadModel? Active, CommandExecutionResult? Rejection)> GetActiveFrontByIdAfterValidationOrRejectAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + FrontId frontId, + CancellationToken cancellationToken = default) + { + if (RejectIfInvalidFrontId(command, frontId) is { } invalidFrontIdReject) + return (null, invalidFrontIdReject); + + return await GetActiveFrontByIdOrRejectAsync(command, frontingRepository, frontId, cancellationToken); + } + + public static async Task<(FrontByIdResolution? Resolution, CommandExecutionResult? Rejection)> ResolveFrontByIdOrRejectAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + FrontId frontId, + CancellationToken cancellationToken = default) + { + var active = await frontingRepository.GetActiveByFrontIdAsync(command.PrincipalId, frontId, cancellationToken); + if (active is not null) + return (new FrontByIdResolution(active, null), null); + + var history = await frontingRepository.GetHistoryEntryByFrontIdAsync(command.PrincipalId, frontId, cancellationToken); + if (history is null) + { + return (null, CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingNoFront)); + } + + return (new FrontByIdResolution(null, history), null); + } + + public static async Task<(FrontByIdResolution? Resolution, CommandExecutionResult? Rejection)> ResolveFrontByIdAfterValidationOrRejectAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + FrontId frontId, + CancellationToken cancellationToken = default) + { + if (RejectIfInvalidFrontId(command, frontId) is { } invalidFrontIdReject) + return (null, invalidFrontIdReject); + + return await ResolveFrontByIdOrRejectAsync(command, frontingRepository, frontId, cancellationToken); + } + + public static async Task?> RejectIfNotFrontingAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + AlterId alterId, + CancellationToken cancellationToken = default) + { + var fronting = await frontingRepository.IsFrontingAsync(command.PrincipalId, alterId, cancellationToken); + return fronting + ? null + : CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingNotFronting); + } + + public static async Task?> RejectIfInvalidOrNotFrontingAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + AlterId alterId, + CancellationToken cancellationToken = default) + { + if (RejectIfInvalidAlterId(command, alterId) is { } invalidAlterIdReject) + return invalidAlterIdReject; + + return await RejectIfNotFrontingAsync(command, frontingRepository, alterId, cancellationToken); + } + + public static async Task?> RejectIfAlreadyFrontingAsync( + CommandEnvelope command, + IFrontingRepository frontingRepository, + AlterId alterId, + CancellationToken cancellationToken = default) + { + var alreadyFronting = await frontingRepository.IsFrontingAsync(command.PrincipalId, alterId, cancellationToken); + return alreadyFronting + ? CommandHandler.RejectInvariant(command.OperationId, EntityRefs.FrontingAlreadyFronting) + : null; + } + + public static async Task EndAltersBestEffortAsync( + IFrontingRepository frontingRepository, + ScopedSystemId systemId, + IEnumerable alterIds, + DateTimeOffset endedAt, + CancellationToken cancellationToken = default) + { + foreach (var alterId in alterIds) + { + // EndAsync returns false if there is no active row (race/no-op); intentionally ignore. + await frontingRepository.EndAsync(systemId, alterId, endedAt, cancellationToken); + } + } + + public static async Task StartAltersIfNotFrontingAsync( + IFrontingRepository frontingRepository, + ScopedSystemId systemId, + IEnumerable startItems, + DateTimeOffset startedAt, + CancellationToken cancellationToken = default) + { + foreach (var item in startItems) + { + var alreadyFronting = await frontingRepository.IsFrontingAsync(systemId, item.AlterId, cancellationToken); + if (!alreadyFronting) + await frontingRepository.StartAsync(systemId, item.AlterId, item.Comment, startedAt, cancellationToken); + } + } +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/FrontingEventBusExtensions.cs b/shared/Interfold.Fronting.Domain/FrontingEventBusExtensions.cs new file mode 100644 index 00000000..fb36f78b --- /dev/null +++ b/shared/Interfold.Fronting.Domain/FrontingEventBusExtensions.cs @@ -0,0 +1,117 @@ +using Interfold.Fronting.Contracts.Events; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +// Intent-named publish helpers on IClusterEventBus for fronting events. Each method +// emits FrontingStateChangedEvent first (cache invalidation), then the transition event +// (specific reaction). Publish-only — orchestration lives in FrontingCommandFlow. +internal static class FrontingEventBusExtensions +{ + public static ValueTask PublishStateChangedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + CancellationToken cancellationToken = default) + => eventBus.PublishAsync(new FrontingStateChangedEvent(systemId), cancellationToken); + + public static async ValueTask PublishStateChangedAndStartedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + FrontId frontId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontingStartedEvent(systemId, frontId), cancellationToken); + } + + public static async ValueTask PublishStateChangedAndEndedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + AlterId alterId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontingEndedEvent(systemId, alterId), cancellationToken); + } + + public static async ValueTask PublishStateChangedAndSetAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + FrontId frontId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontingSetEvent(systemId, frontId), cancellationToken); + } + + public static async ValueTask PublishStateChangedAndBulkUpdatedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontingBulkUpdatedEvent(systemId), cancellationToken); + } + + public static async ValueTask PublishStateChangedAndCommentUpdatedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + FrontId frontId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontCommentUpdatedEvent(systemId, frontId), cancellationToken); + } + + public static async ValueTask PublishStateChangedAndPrimaryChangedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + AlterId? alterId, + CancellationToken cancellationToken = default) + { + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + await eventBus.PublishAsync(new FrontingPrimaryChangedEvent(systemId, alterId), cancellationToken); + } + + /// Publishes FrontDeletedEvent; prefixes with FrontingStateChangedEvent only + /// when the deleted row was the active one. + public static async ValueTask PublishDeletedAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + FrontId frontId, + bool wasActive, + CancellationToken cancellationToken = default) + { + if (wasActive) + await eventBus.PublishStateChangedAsync(systemId, cancellationToken); + + await eventBus.PublishAsync(new FrontDeletedEvent(systemId, frontId), cancellationToken); + } + + /// Fans one FrontingEndedEvent per alter id, sequentially (subscribers + /// assume in-order delivery per publisher). + public static async ValueTask PublishEndedForAltersAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + IEnumerable alterIds, + CancellationToken cancellationToken = default) + { + foreach (var alterId in alterIds) + { + await eventBus.PublishAsync(new FrontingEndedEvent(systemId, alterId), cancellationToken); + } + } + + /// "Primary cleared" event guarded by + /// so callers can invoke unconditionally. + public static ValueTask PublishPrimaryClearedIfNeededAsync( + this IClusterEventBus eventBus, + ScopedSystemId systemId, + bool wasPrimaryPresent, + CancellationToken cancellationToken = default) + => wasPrimaryPresent + ? eventBus.PublishAsync(new FrontingPrimaryChangedEvent(systemId, null), cancellationToken) + : ValueTask.CompletedTask; +} diff --git a/shared/Interfold.Fronting.Domain/Interfold.Fronting.Domain.csproj b/shared/Interfold.Fronting.Domain/Interfold.Fronting.Domain.csproj new file mode 100644 index 00000000..b930161d --- /dev/null +++ b/shared/Interfold.Fronting.Domain/Interfold.Fronting.Domain.csproj @@ -0,0 +1,17 @@ + + + net10.0 + enable + enable + + + + + + + + + diff --git a/shared/Interfold.Fronting.Domain/SetFrontCommandHandler.cs b/shared/Interfold.Fronting.Domain/SetFrontCommandHandler.cs new file mode 100644 index 00000000..da3beb10 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/SetFrontCommandHandler.cs @@ -0,0 +1,126 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class SetFrontCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + private readonly TimeProvider _timeProvider; + + public SetFrontCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + TimeProvider timeProvider) : base(idempotencyStore) + { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + _timeProvider = timeProvider; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingSet; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (FrontingCommandFlow.RejectIfInvalidAlterId(command, command.Payload.AlterId) is { } rangeReject) + return rangeReject; + + if (FrontingCommandFlow.RejectIfInvalidComment(command, command.Payload.Comment) is { } invalidCommentReject) + return invalidCommentReject; + + // "Set" semantics: after this completes, the target alter is the sole fronter. + // The previous implementation rejected with `fronting:already_fronting` whenever the + // target was already in the active set - which broke the obvious use case of "promote + // X to the only fronter when X is one of several". It also ended every other fronter + // silently (only FrontingSetEvent was published, no per-alter FrontingEndedEvent), + // so clients never received the per-alter "stopped fronting" signals they need to + // update local state. + // + // New rules: + // - End every active alter that isn't the target. Publish FrontingEndedEvent per + // ended alter so socket clients see them stop one-by-one (matches the contract + // EndFrontCommandHandler already follows for a single end). + // - If the target was already fronting, keep its front row (preserves the front_id, + // start_time and history) - reuse its existing front id in the FrontingSetEvent. + // If it wasn't, start it. + // - If anything that was the primary got ended (or the previous primary was the + // now-set target and we cleared primary), publish FrontingPrimaryChangedEvent(null). + // "set" always clears primary because there's exactly one fronter afterwards. + // - Always publish FrontingSetEvent for the target so clients pin the now-only-front. + var active = await _frontingRepository.ListActiveAsync(command.PrincipalId, cancellationToken); + var targetActive = active.FirstOrDefault(f => f.Alter.Id == command.Payload.AlterId); + var others = active.Where(f => f.Alter.Id != command.Payload.AlterId).ToArray(); + var primaryWasPresent = active.Any(f => f.Primary); + + var operationTime = _timeProvider.GetUtcNow(); + await FrontingCommandFlow.EndAltersBestEffortAsync( + _frontingRepository, + command.PrincipalId, + others.Select(other => other.Front.AlterId), + operationTime, + cancellationToken); + + FrontId frontId; + if (targetActive is not null) + { + // Target was already fronting - preserve its front row (front_id, start_time). + frontId = targetActive.Front.Id; + } + else + { + var started = await _frontingRepository.StartAsync( + command.PrincipalId, + command.Payload.AlterId, + command.Payload.Comment, + operationTime, + cancellationToken); + + var (startedFrontId, startedFrontRejection) = FrontingCommandFlow.GetStartedFrontIdOrReject(command, started); + if (startedFrontRejection is not null) + return startedFrontRejection; + + frontId = startedFrontId!.Value; + } + + // "set" semantics: after the call there's a single fronter, so any primary designation + // is moot. Clear unconditionally (no-op when there wasn't one). + await _frontingRepository.SetPrimaryAsync(command.PrincipalId, null, cancellationToken); + + // Per-alter FrontingEndedEvent for every alter that was ended by this set. Clients + // listening on the socket layer rely on this to clear those alters from their + // local "currently fronting" view. + await _eventBus.PublishEndedForAltersAsync( + command.PrincipalId, + others.Select(other => other.Front.AlterId), + cancellationToken); + + // FrontingPrimaryChangedEvent only fires when primary actually transitioned away from + // a real value - emitting it unconditionally would spam clients with no-op events + // every time `set` is called against a no-primary state. + await _eventBus.PublishPrimaryClearedIfNeededAsync( + command.PrincipalId, + primaryWasPresent, + cancellationToken); + + // Emit granular event for socket layer to handle fronting_set. Always fires - even + // when the target was already the only fronter (idempotent "set" returns success + // with the existing front id; the client may have called this to recover from a + // desync and should still receive the event). + await _eventBus.PublishStateChangedAndSetAsync(command.PrincipalId, frontId, cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, command.Payload.AlterId, frontId); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/SetPrimaryFrontCommandHandler.cs b/shared/Interfold.Fronting.Domain/SetPrimaryFrontCommandHandler.cs new file mode 100644 index 00000000..b444c6e3 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/SetPrimaryFrontCommandHandler.cs @@ -0,0 +1,57 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class SetPrimaryFrontCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + + public SetPrimaryFrontCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus + ) +:base(idempotencyStore) { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingPrimary; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (command.Payload.AlterId is { } alterId) + { + if (await FrontingCommandFlow.RejectIfInvalidOrNotFrontingAsync(command, _frontingRepository, alterId, cancellationToken) is { } notFrontingReject) + return notFrontingReject; + } + + if (await FrontingCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _frontingRepository.SetPrimaryAsync(command.PrincipalId, command.Payload.AlterId, ct), + EntityRefs.FrontingPrimaryFailed, + cancellationToken) is { } setReject) + return setReject; + + await _eventBus.PublishStateChangedAndPrimaryChangedAsync(command.PrincipalId, command.Payload.AlterId, cancellationToken); + // Inlined: SettingsEventBusExtensions.PublishProfileUpdatedAsync is internal to + // Interfold.Shared.Domain and this handler lives in Interfold.Fronting.Domain after the + // Phase-3 slice-3 move. The event itself (Interfold.Shared.Contracts.Events) is public. + await _eventBus.PublishAsync(new SettingsProfileUpdatedEvent(command.PrincipalId, EmitUsernameUpdated: false), cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, command.Payload.AlterId, frontId: null); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/StartFrontCommandHandler.cs b/shared/Interfold.Fronting.Domain/StartFrontCommandHandler.cs new file mode 100644 index 00000000..3a2c41df --- /dev/null +++ b/shared/Interfold.Fronting.Domain/StartFrontCommandHandler.cs @@ -0,0 +1,64 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class StartFrontCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + private readonly TimeProvider _timeProvider; + + public StartFrontCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + TimeProvider timeProvider) : base(idempotencyStore) + { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + _timeProvider = timeProvider; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingStart; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (FrontingCommandFlow.RejectIfInvalidAlterId(command, command.Payload.AlterId) is { } rangeReject) + return rangeReject; + + if (FrontingCommandFlow.RejectIfInvalidComment(command, command.Payload.Comment) is { } invalidCommentReject) + return invalidCommentReject; + + if (await FrontingCommandFlow.RejectIfAlreadyFrontingAsync(command, _frontingRepository, command.Payload.AlterId, cancellationToken) is { } alreadyFrontingReject) + return alreadyFrontingReject; + + var frontId = await _frontingRepository.StartAsync( + command.PrincipalId, + command.Payload.AlterId, + command.Payload.Comment, + _timeProvider.GetUtcNow(), + cancellationToken + ); + + var (startedFrontId, startedFrontRejection) = FrontingCommandFlow.GetStartedFrontIdOrReject(command, frontId); + if (startedFrontRejection is not null) + return startedFrontRejection; + + // Emit granular event for socket layer to handle fronting_started + await _eventBus.PublishStateChangedAndStartedAsync(command.PrincipalId, startedFrontId!.Value, cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, command.Payload.AlterId, startedFrontId); + } + +} \ No newline at end of file diff --git a/shared/Interfold.Fronting.Domain/UpdateFrontCommentCommandHandler.cs b/shared/Interfold.Fronting.Domain/UpdateFrontCommentCommandHandler.cs new file mode 100644 index 00000000..cfe4a462 --- /dev/null +++ b/shared/Interfold.Fronting.Domain/UpdateFrontCommentCommandHandler.cs @@ -0,0 +1,60 @@ +using Interfold.Fronting.Contracts; +using Interfold.Fronting.Contracts.Models.Commands; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Fronting.Domain; + +public sealed class UpdateFrontCommentCommandHandler : IdempotentCommandHandler +{ + private readonly IFrontingRepository _frontingRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateFrontCommentCommandHandler( + IFrontingRepository frontingRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _frontingRepository = frontingRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.FrontingUpdateComment; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (FrontingCommandFlow.RejectIfInvalidComment(command, command.Payload.Comment) is { } invalidCommentReject) + return invalidCommentReject; + + var (existing, rejection) = await FrontingCommandFlow.GetActiveFrontByIdAfterValidationOrRejectAsync( + command, + _frontingRepository, + command.Payload.FrontId, + cancellationToken); + if (rejection is not null) + return rejection; + + if (await FrontingCommandFlow.ExecuteMutationOrRejectAsync( + command, + ct => _frontingRepository.UpdateCommentByFrontIdAsync( + command.PrincipalId, + command.Payload.FrontId, + command.Payload.Comment ?? string.Empty, + ct), + EntityRefs.FrontingUpdateCommentFailed, + cancellationToken) is { } updateReject) + return updateReject; + + // Emit granular event for socket layer to handle front_updated + await _eventBus.PublishStateChangedAndCommentUpdatedAsync(command.PrincipalId, command.Payload.FrontId, cancellationToken); + + return FrontingCommandFlow.Success(command.PrincipalId, existing!.Front.AlterId, command.Payload.FrontId); + } + +} diff --git a/shared/Interfold.Journals.Contracts/Abstractions/IJournalAlterCascade.cs b/shared/Interfold.Journals.Contracts/Abstractions/IJournalAlterCascade.cs new file mode 100644 index 00000000..b2f34d1a --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Abstractions/IJournalAlterCascade.cs @@ -0,0 +1,12 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions; + +/// Cascade hook for alter deletion — removes every alter-journal entry owned by the +/// alter and detaches the alter from any global-journal rows (the global journal itself is +/// left in place; multiple alters can share it). Adapter over IJournalRepository in +/// Interfold.Journals.Domain, registered by AddJournalsModule. +public interface IJournalAlterCascade +{ + Task DeleteAllForAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Journals.Contracts/Events/JournalDomainEvents.cs b/shared/Interfold.Journals.Contracts/Events/JournalDomainEvents.cs new file mode 100644 index 00000000..fb7ce9cc --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Events/JournalDomainEvents.cs @@ -0,0 +1,17 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Contracts.Events; + +public sealed record GlobalJournalEntryCreatedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; + +public sealed record GlobalJournalEntryUpdatedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; + +public sealed record GlobalJournalEntryDeletedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; + +public sealed record AlterJournalEntryCreatedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; + +public sealed record AlterJournalEntryUpdatedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; + +public sealed record AlterJournalEntryDeletedEvent(ScopedSystemId TargetSystemId, EntryId EntryId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Journals.Contracts/Ids/EntryId.cs b/shared/Interfold.Journals.Contracts/Ids/EntryId.cs new file mode 100644 index 00000000..f101baf6 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Ids/EntryId.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Contracts.Ids; + +/// Strongly-typed wrapper around a journal entry id (Guid, wire form is 32-char lowercase hex). +[JsonConverter(typeof(EntryIdJsonConverter))] +public readonly record struct EntryId(Guid Value) : IParsable +{ + public static readonly EntryId Empty = new(Guid.Empty); + + public static explicit operator EntryId(Guid value) => new(value); + public static implicit operator Guid(EntryId value) => value.Value; + + public override string ToString() => Value.ToString("N"); + + public static EntryId Parse(string s, IFormatProvider? provider) + => UuidString.TryParse(s, out var g) + ? new(g) + : throw new FormatException($"Value '{s}' is not a valid EntryId."); + + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out EntryId result) + { + if (s is null || !UuidString.TryParse(s, out var g)) { result = default; return false; } + result = new EntryId(g); + return true; + } +} + +internal sealed class EntryIdJsonConverter : GuidIdJsonConverter +{ + protected override EntryId Create(Guid value) => new(value); + protected override Guid GetValue(EntryId value) => value.Value; + protected override string TypeLabel => nameof(EntryId); +} diff --git a/shared/Interfold.Journals.Contracts/Interfold.Journals.Contracts.csproj b/shared/Interfold.Journals.Contracts/Interfold.Journals.Contracts.csproj new file mode 100644 index 00000000..210c36cb --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Interfold.Journals.Contracts.csproj @@ -0,0 +1,13 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Journals.Contracts/JournalCommandResults.cs b/shared/Interfold.Journals.Contracts/JournalCommandResults.cs new file mode 100644 index 00000000..2fdfe799 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/JournalCommandResults.cs @@ -0,0 +1,15 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Contracts; + +public sealed record GlobalJournalCommandResult(SystemId SystemId, EntryId EntryId, bool Replay) : ICommandResult +{ + public GlobalJournalCommandResult WithReplay() => this with { Replay = true }; +} + +public sealed record AlterJournalCommandResult(SystemId SystemId, EntryId EntryId, AlterId AlterId, bool Replay) : ICommandResult +{ + public AlterJournalCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Journals.Contracts/Models/Commands/JournalCommands.cs b/shared/Interfold.Journals.Contracts/Models/Commands/JournalCommands.cs new file mode 100644 index 00000000..211b36e2 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Models/Commands/JournalCommands.cs @@ -0,0 +1,43 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Contracts.Models.Commands; + +public sealed record CreateGlobalJournalEntryCommand(string Title); + +public sealed record UpdateGlobalJournalEntryCommand( + EntryId EntryId, + string? Title, + string? Content, + HexColor? Color +); + +public sealed record DeleteGlobalJournalEntryCommand(EntryId EntryId); + +public sealed record SetGlobalJournalLockedCommand(EntryId EntryId, bool Locked); + +public sealed record SetGlobalJournalPinnedCommand(EntryId EntryId, bool Pinned); + +public sealed record AttachAlterToGlobalJournalCommand(EntryId EntryId, AlterId AlterId); + +public sealed record DetachAlterFromGlobalJournalCommand(EntryId EntryId, AlterId AlterId); + +public sealed record CreateAlterJournalEntryCommand( + AlterId AlterId, + string Title, + DateTimeOffset CreatedAt +); + +public sealed record UpdateAlterJournalEntryCommand( + EntryId EntryId, + string? Title, + string? Content, + HexColor? Color, + DateTimeOffset UpdatedAt +); + +public sealed record DeleteAlterJournalEntryCommand(EntryId EntryId); + +public sealed record SetAlterJournalLockedCommand(EntryId EntryId, bool Locked); + +public sealed record SetAlterJournalPinnedCommand(EntryId EntryId, bool Pinned); diff --git a/shared/Interfold.Journals.Contracts/Models/Read/AlterJournalReadModel.cs b/shared/Interfold.Journals.Contracts/Models/Read/AlterJournalReadModel.cs new file mode 100644 index 00000000..4e9d35d8 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Models/Read/AlterJournalReadModel.cs @@ -0,0 +1,29 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Contracts.Models.Read; + +public sealed record AlterJournalReadModel( + EntryId Id, + SystemId UserId, + AlterId AlterId, + string Title, + string? Content, + HexColor? Color, + bool Locked, + bool Pinned, + DateTime InsertedAt, + DateTime UpdatedAt +); + +public sealed record CreateAlterJournalRequest( + string Title +); + +public sealed record UpdateAlterJournalRequest( + string? Title = null, + string? Content = null, + HexColor? Color = null +); + +public sealed record AlterJournalRef(EntryId EntryId, AlterId AlterId); diff --git a/shared/Interfold.Journals.Contracts/Models/Read/JournalReadModel.cs b/shared/Interfold.Journals.Contracts/Models/Read/JournalReadModel.cs new file mode 100644 index 00000000..633caab1 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/Models/Read/JournalReadModel.cs @@ -0,0 +1,49 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Validation; + +namespace Interfold.Journals.Contracts.Models.Read; + +public sealed record JournalReadModel( + EntryId Id, + SystemId UserId, + string Title, + string? Content, + HexColor? Color, + bool Locked, + bool Pinned, + DateTime InsertedAt, + DateTime UpdatedAt, + IReadOnlyList Alters +); + + +public sealed record CreateGlobalJournalRequest( + string Title, + EntityVersion? ExpectedVersion = null +); + +public sealed record UpdateGlobalJournalRequest( + string? Title = null, + string? Content = null, + HexColor? Color = null, + EntityVersion? ExpectedVersion = null +); + +public sealed record DeleteGlobalJournalRequest( + EntityVersion? ExpectedVersion = null +); + +public sealed record JournalActionRequest( + EntityVersion? ExpectedVersion = null +); + +// ValidAlterId lives on the constructor parameter (default target), NOT the property: +// MVC's ObjectModelValidator throws InvalidOperationException on records that carry +// validation metadata on properties (see ThrowIfRecordTypeHasValidationOnProperties) +// — the parameter target is where MVC's parameter-binding validator actually reads +// the attribute from. +public sealed record JournalAlterRequest( + [ValidAlterId] AlterId AlterId, + EntityVersion? ExpectedVersion = null +); diff --git a/shared/Interfold.Journals.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Journals.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..c6219de7 --- /dev/null +++ b/shared/Interfold.Journals.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,11 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Socket.Contracts; + +namespace Interfold.Journals.Contracts; + +public sealed record GlobalJournalSocketPayload(JournalReadModel Entry) : ISocketPayload; + +public sealed record AlterJournalSocketPayload(AlterJournalReadModel Entry) : ISocketPayload; + +public sealed record EntryDeletedSocketPayload(EntryId EntryId) : ISocketPayload; diff --git a/shared/Interfold.Journals.Domain/Abstractions/Repository/IJournalRepository.cs b/shared/Interfold.Journals.Domain/Abstractions/Repository/IJournalRepository.cs new file mode 100644 index 00000000..6bfd91f5 --- /dev/null +++ b/shared/Interfold.Journals.Domain/Abstractions/Repository/IJournalRepository.cs @@ -0,0 +1,65 @@ +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Journals.Domain.Abstractions.Repository; + +public interface IJournalRepository +{ + Task CreateGlobalAsync(SystemId systemId, CreateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default); + + Task ExistsGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); + + Task UpdateGlobalAsync(SystemId systemId, UpdateGlobalJournalEntryCommand command, CancellationToken cancellationToken = default); + + Task DeleteGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); + + Task SetGlobalLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default); + + Task SetGlobalPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default); + + Task AttachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default); + + Task DetachGlobalAlterAsync(SystemId systemId, EntryId entryId, AlterId alterId, CancellationToken cancellationToken = default); + + Task CreateAlterAsync(SystemId systemId, CreateAlterJournalEntryCommand command, CancellationToken cancellationToken = default); + + Task GetAlterRefAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); + + Task UpdateAlterAsync(SystemId systemId, UpdateAlterJournalEntryCommand command, CancellationToken cancellationToken = default); + + Task DeleteAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); + + /// + /// Cascade cleanup for : + /// wipes every per-alter journal entry the alter owned (both view tables in the Scylla + /// repository) and detaches the alter from any global journals it was attached to. + /// + /// Global journal rows themselves are left intact - they may be attached to other + /// alters, and we do not want deleting one alter to silently delete a shared group + /// journal. The alter-id rows in the global_journal_alters join table are + /// removed so the deleted alter does not appear on any global journal's attached-alters + /// list after this returns. + /// + /// + /// Idempotent: returns gracefully when the alter has no journal entries and no global + /// journal attachments. Returns the number of per-alter journal entries that were + /// removed (the global-journal detach count is logged at the repository level but not + /// surfaced; the caller only needs the alter-journal count for telemetry). + /// + /// + Task DeleteAllForAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task SetAlterLockedAsync(SystemId systemId, EntryId entryId, bool locked, CancellationToken cancellationToken = default); + + Task SetAlterPinnedAsync(SystemId systemId, EntryId entryId, bool pinned, CancellationToken cancellationToken = default); + + Task> ListAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); + + Task GetAlterAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); + + Task> ListGlobalAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetGlobalAsync(SystemId systemId, EntryId entryId, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/shared/Interfold.Journals.Domain/AlterJournalCommandFlow.cs b/shared/Interfold.Journals.Domain/AlterJournalCommandFlow.cs new file mode 100644 index 00000000..b94e4862 --- /dev/null +++ b/shared/Interfold.Journals.Domain/AlterJournalCommandFlow.cs @@ -0,0 +1,91 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +internal static class AlterJournalCommandFlow +{ + public static CommandExecutionResult Success( + ScopedSystemId systemId, + EntryId entryId, + AlterId alterId) + => CommandExecutionResult.Success( + new AlterJournalCommandResult(systemId, entryId, alterId, Replay: false)); + + public static async Task?> RejectIfInvalidOrMissingAlterAsync( + CommandEnvelope command, + AlterId alterId, + IAlterExistenceCheck alterExistence, + EntityRef invalidAlterIdEntityRef, + EntityRef missingAlterEntityRef, + CancellationToken cancellationToken = default) + { + if (CommandHandler.RejectIfAlterIdOutOfRange(command.OperationId, alterId, invalidAlterIdEntityRef) is { } invalidAlterIdReject) + return invalidAlterIdReject; + + var alterExists = await alterExistence.ExistsAsync(command.PrincipalId, alterId, cancellationToken); + return alterExists + ? null + : CommandHandler.RejectInvariant(command.OperationId, missingAlterEntityRef); + } + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failureEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failureEntityRef) + : null; + + public static async Task?> ExecuteMutationOrRejectAsync( + CommandEnvelope command, + Func> mutateAsync, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + var mutated = await mutateAsync(cancellationToken); + return RejectIfMutationFailed(command, mutated, failureEntityRef); + } + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + IClusterEventBus eventBus, + Func> createAsync, + AlterId alterId, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + var entryId = await createAsync(cancellationToken); + if (entryId is null) + return CommandHandler.RejectInvariant(command.OperationId, failureEntityRef); + + await eventBus.PublishAsync(new AlterJournalEntryCreatedEvent(command.PrincipalId, entryId.Value), cancellationToken); + return Success(command.PrincipalId, entryId.Value, alterId); + } + + public static async Task> ExecuteExistingEntryMutationAsync( + CommandEnvelope command, + EntryId entryId, + IJournalRepository journalRepository, + Func> mutateAsync, + EntityRef failureEntityRef, + Func publishAsync, + CancellationToken cancellationToken = default) + { + var alterRef = await journalRepository.GetAlterRefAsync(command.PrincipalId, entryId, cancellationToken); + if (alterRef is null) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.JournalNotFound); + + if (await ExecuteMutationOrRejectAsync(command, mutateAsync, failureEntityRef, cancellationToken) is { } mutationReject) + return mutationReject; + + await publishAsync(cancellationToken); + return Success(command.PrincipalId, entryId, alterRef.AlterId); + } +} \ No newline at end of file diff --git a/shared/Interfold.Journals.Domain/AttachAlterToGlobalJournalCommandHandler.cs b/shared/Interfold.Journals.Domain/AttachAlterToGlobalJournalCommandHandler.cs new file mode 100644 index 00000000..cf0fe2fa --- /dev/null +++ b/shared/Interfold.Journals.Domain/AttachAlterToGlobalJournalCommandHandler.cs @@ -0,0 +1,54 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class AttachAlterToGlobalJournalCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IAlterExistenceCheck _alterExistence; + private readonly IClusterEventBus _eventBus; + + public AttachAlterToGlobalJournalCommandHandler( + IJournalRepository journalRepository, + IAlterExistenceCheck alterExistence, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _alterExistence = alterExistence; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalAttachAlter; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (await AlterJournalCommandFlow.RejectIfInvalidOrMissingAlterAsync( + command, + command.Payload.AlterId, + _alterExistence, + EntityRefs.AlterId, + EntityRefs.JournalAlterNotFound, + cancellationToken) is { } alterReject) + return alterReject; + + return await GlobalJournalCommandFlow.ExecuteUpdateAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.AttachGlobalAlterAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.AlterId, ct), + EntityRefs.JournalAttachFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/CreateAlterJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/CreateAlterJournalEntryCommandHandler.cs new file mode 100644 index 00000000..00eb019f --- /dev/null +++ b/shared/Interfold.Journals.Domain/CreateAlterJournalEntryCommandHandler.cs @@ -0,0 +1,59 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class CreateAlterJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IAlterExistenceCheck _alterExistence; + private readonly IClusterEventBus _eventBus; + + public CreateAlterJournalEntryCommandHandler( + IJournalRepository journalRepository, + IAlterExistenceCheck alterExistence, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _alterExistence = alterExistence; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalAlterCreate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Title, EntityRefs.JournalTitleRequired) is { } blankReject) + return blankReject; + + if (command.Payload.Title.Length > 100) + return RejectInvariant(command, EntityRefs.JournalTitleTooLong); + + if (await AlterJournalCommandFlow.RejectIfInvalidOrMissingAlterAsync( + command, + command.Payload.AlterId, + _alterExistence, + EntityRefs.AlterId, + EntityRefs.JournalAlterNotFound, + cancellationToken) is { } alterReject) + return alterReject; + + return await AlterJournalCommandFlow.ExecuteCreateAsync( + command, + _eventBus, + ct => _journalRepository.CreateAlterAsync(command.PrincipalId, command.Payload, ct), + command.Payload.AlterId, + EntityRefs.JournalCreateFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/CreateGlobalJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/CreateGlobalJournalEntryCommandHandler.cs new file mode 100644 index 00000000..ed23e7ae --- /dev/null +++ b/shared/Interfold.Journals.Domain/CreateGlobalJournalEntryCommandHandler.cs @@ -0,0 +1,47 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class CreateGlobalJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public CreateGlobalJournalEntryCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalCreate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Title, EntityRefs.JournalTitleRequired) is { } blankReject) + return blankReject; + + if (command.Payload.Title.Length > 250) + return RejectInvariant(command, EntityRefs.JournalTitleTooLong); + + return await GlobalJournalCommandFlow.ExecuteCreateAsync( + command, + _journalRepository, + _eventBus, + ct => _journalRepository.CreateGlobalAsync(command.PrincipalId, command.Payload, ct), + EntityRefs.JournalCreateFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/DeleteAlterJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/DeleteAlterJournalEntryCommandHandler.cs new file mode 100644 index 00000000..0ee45f7b --- /dev/null +++ b/shared/Interfold.Journals.Domain/DeleteAlterJournalEntryCommandHandler.cs @@ -0,0 +1,43 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class DeleteAlterJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteAlterJournalEntryCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalAlterDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await AlterJournalCommandFlow.ExecuteExistingEntryMutationAsync( + command, + command.Payload.EntryId, + _journalRepository, + ct => _journalRepository.DeleteAlterAsync(command.PrincipalId, command.Payload.EntryId, ct), + EntityRefs.JournalDeleteFailed, + ct => _eventBus.PublishAsync(new AlterJournalEntryDeletedEvent(command.PrincipalId, command.Payload.EntryId), ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/DeleteGlobalJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/DeleteGlobalJournalEntryCommandHandler.cs new file mode 100644 index 00000000..39e37309 --- /dev/null +++ b/shared/Interfold.Journals.Domain/DeleteGlobalJournalEntryCommandHandler.cs @@ -0,0 +1,42 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class DeleteGlobalJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteGlobalJournalEntryCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await GlobalJournalCommandFlow.ExecuteDeleteAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.DeleteGlobalAsync(command.PrincipalId, command.Payload.EntryId, ct), + EntityRefs.JournalDeleteFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/DetachAlterFromGlobalJournalCommandHandler.cs b/shared/Interfold.Journals.Domain/DetachAlterFromGlobalJournalCommandHandler.cs new file mode 100644 index 00000000..e863a99a --- /dev/null +++ b/shared/Interfold.Journals.Domain/DetachAlterFromGlobalJournalCommandHandler.cs @@ -0,0 +1,54 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class DetachAlterFromGlobalJournalCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IAlterExistenceCheck _alterExistence; + private readonly IClusterEventBus _eventBus; + + public DetachAlterFromGlobalJournalCommandHandler( + IJournalRepository journalRepository, + IAlterExistenceCheck alterExistence, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _alterExistence = alterExistence; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalDetachAlter; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (await AlterJournalCommandFlow.RejectIfInvalidOrMissingAlterAsync( + command, + command.Payload.AlterId, + _alterExistence, + EntityRefs.AlterId, + EntityRefs.JournalAlterNotFound, + cancellationToken) is { } alterReject) + return alterReject; + + return await GlobalJournalCommandFlow.ExecuteUpdateAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.DetachGlobalAlterAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.AlterId, ct), + EntityRefs.JournalDetachFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/GlobalJournalCommandFlow.cs b/shared/Interfold.Journals.Domain/GlobalJournalCommandFlow.cs new file mode 100644 index 00000000..e1b27ad4 --- /dev/null +++ b/shared/Interfold.Journals.Domain/GlobalJournalCommandFlow.cs @@ -0,0 +1,110 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Ids; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +internal static class GlobalJournalCommandFlow +{ + public static CommandExecutionResult Success( + ScopedSystemId systemId, + EntryId entryId) + => CommandExecutionResult.Success(new GlobalJournalCommandResult(systemId, entryId, Replay: false)); + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failureEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failureEntityRef) + : null; + + public static async Task?> ExecuteMutationOrRejectAsync( + CommandEnvelope command, + Func> mutateAsync, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + var mutated = await mutateAsync(cancellationToken); + return RejectIfMutationFailed(command, mutated, failureEntityRef); + } + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + IJournalRepository journalRepository, + IClusterEventBus eventBus, + Func> createAsync, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + var entryId = await createAsync(cancellationToken); + if (entryId is null) + return CommandHandler.RejectInvariant(command.OperationId, failureEntityRef); + + await eventBus.PublishAsync(new GlobalJournalEntryCreatedEvent(command.PrincipalId, entryId.Value), cancellationToken); + return Success(command.PrincipalId, entryId.Value); + } + + public static async Task> ExecuteUpdateAsync( + CommandEnvelope command, + EntryId entryId, + IJournalRepository journalRepository, + IClusterEventBus eventBus, + Func> applyAsync, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + return await ExecuteExistingEntryMutationAsync( + command, + entryId, + journalRepository, + applyAsync, + failureEntityRef, + ct => eventBus.PublishAsync(new GlobalJournalEntryUpdatedEvent(command.PrincipalId, entryId), ct), + cancellationToken); + } + + public static async Task> ExecuteDeleteAsync( + CommandEnvelope command, + EntryId entryId, + IJournalRepository journalRepository, + IClusterEventBus eventBus, + Func> deleteAsync, + EntityRef failureEntityRef, + CancellationToken cancellationToken = default) + { + return await ExecuteExistingEntryMutationAsync( + command, + entryId, + journalRepository, + deleteAsync, + failureEntityRef, + ct => eventBus.PublishAsync(new GlobalJournalEntryDeletedEvent(command.PrincipalId, entryId), ct), + cancellationToken); + } + + private static async Task> ExecuteExistingEntryMutationAsync( + CommandEnvelope command, + EntryId entryId, + IJournalRepository journalRepository, + Func> mutateAsync, + EntityRef failureEntityRef, + Func publishAsync, + CancellationToken cancellationToken) + { + var exists = await journalRepository.ExistsGlobalAsync(command.PrincipalId, entryId, cancellationToken); + if (!exists) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.JournalNotFound); + + if (await ExecuteMutationOrRejectAsync(command, mutateAsync, failureEntityRef, cancellationToken) is { } mutationReject) + return mutationReject; + + await publishAsync(cancellationToken); + return Success(command.PrincipalId, entryId); + } +} \ No newline at end of file diff --git a/shared/Interfold.Journals.Domain/Interfold.Journals.Domain.csproj b/shared/Interfold.Journals.Domain/Interfold.Journals.Domain.csproj new file mode 100644 index 00000000..f4d2bda3 --- /dev/null +++ b/shared/Interfold.Journals.Domain/Interfold.Journals.Domain.csproj @@ -0,0 +1,15 @@ + + + net10.0 + enable + enable + + + + + + + + + diff --git a/shared/Interfold.Journals.Domain/JournalAlterCascadeAdapter.cs b/shared/Interfold.Journals.Domain/JournalAlterCascadeAdapter.cs new file mode 100644 index 00000000..8358e5fd --- /dev/null +++ b/shared/Interfold.Journals.Domain/JournalAlterCascadeAdapter.cs @@ -0,0 +1,19 @@ +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +// see IJournalAlterCascade for the cycle rationale +public sealed class JournalAlterCascadeAdapter : IJournalAlterCascade +{ + private readonly IJournalRepository _journalRepository; + + public JournalAlterCascadeAdapter(IJournalRepository journalRepository) + { + _journalRepository = journalRepository; + } + + public Task DeleteAllForAlterAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + => _journalRepository.DeleteAllForAlterAsync(systemId, alterId, cancellationToken); +} diff --git a/shared/Interfold.Journals.Domain/SetAlterJournalStateCommandHandlers.cs b/shared/Interfold.Journals.Domain/SetAlterJournalStateCommandHandlers.cs new file mode 100644 index 00000000..30c97e54 --- /dev/null +++ b/shared/Interfold.Journals.Domain/SetAlterJournalStateCommandHandlers.cs @@ -0,0 +1,76 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class SetAlterJournalLockedCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public SetAlterJournalLockedCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) + : base(idempotencyStore) + { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalAlterSetLocked; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await AlterJournalCommandFlow.ExecuteExistingEntryMutationAsync( + command, + command.Payload.EntryId, + _journalRepository, + ct => _journalRepository.SetAlterLockedAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.Locked, ct), + EntityRefs.JournalUpdateFailed, + ct => _eventBus.PublishAsync(new AlterJournalEntryUpdatedEvent(command.PrincipalId, command.Payload.EntryId), ct), + cancellationToken); + } +} + +public sealed class SetAlterJournalPinnedCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public SetAlterJournalPinnedCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) + : base(idempotencyStore) + { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalAlterSetPinned; + + + protected override async Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await AlterJournalCommandFlow.ExecuteExistingEntryMutationAsync( + command, + command.Payload.EntryId, + _journalRepository, + ct => _journalRepository.SetAlterPinnedAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.Pinned, ct), + EntityRefs.JournalUpdateFailed, + ct => _eventBus.PublishAsync(new AlterJournalEntryUpdatedEvent(command.PrincipalId, command.Payload.EntryId), ct), + cancellationToken); + } +} diff --git a/shared/Interfold.Journals.Domain/SetGlobalJournalLockedCommandHandler.cs b/shared/Interfold.Journals.Domain/SetGlobalJournalLockedCommandHandler.cs new file mode 100644 index 00000000..9e260201 --- /dev/null +++ b/shared/Interfold.Journals.Domain/SetGlobalJournalLockedCommandHandler.cs @@ -0,0 +1,42 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class SetGlobalJournalLockedCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public SetGlobalJournalLockedCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalSetLocked; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await GlobalJournalCommandFlow.ExecuteUpdateAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.SetGlobalLockedAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.Locked, ct), + EntityRefs.JournalUpdateFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/SetGlobalJournalPinnedCommandHandler.cs b/shared/Interfold.Journals.Domain/SetGlobalJournalPinnedCommandHandler.cs new file mode 100644 index 00000000..e70f876c --- /dev/null +++ b/shared/Interfold.Journals.Domain/SetGlobalJournalPinnedCommandHandler.cs @@ -0,0 +1,42 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class SetGlobalJournalPinnedCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public SetGlobalJournalPinnedCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalSetPinned; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + return await GlobalJournalCommandFlow.ExecuteUpdateAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.SetGlobalPinnedAsync(command.PrincipalId, command.Payload.EntryId, command.Payload.Pinned, ct), + EntityRefs.JournalUpdateFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/UpdateAlterJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/UpdateAlterJournalEntryCommandHandler.cs new file mode 100644 index 00000000..148897fc --- /dev/null +++ b/shared/Interfold.Journals.Domain/UpdateAlterJournalEntryCommandHandler.cs @@ -0,0 +1,52 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Events; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class UpdateAlterJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateAlterJournalEntryCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalAlterUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (command.Payload.Title is null && command.Payload.Content is null && command.Payload.Color is null) + return RejectInvariant(command, EntityRefs.JournalNoFields); + + if (command.Payload.Title is not null && command.Payload.Title.Length > 100) + return RejectInvariant(command, EntityRefs.JournalTitleTooLong); + + if (command.Payload.Content is not null && command.Payload.Content.Length > 50_000) + return RejectInvariant(command, EntityRefs.JournalContentTooLong); + + return await AlterJournalCommandFlow.ExecuteExistingEntryMutationAsync( + command, + command.Payload.EntryId, + _journalRepository, + ct => _journalRepository.UpdateAlterAsync(command.PrincipalId, command.Payload, ct), + EntityRefs.JournalUpdateFailed, + ct => _eventBus.PublishAsync(new AlterJournalEntryUpdatedEvent(command.PrincipalId, command.Payload.EntryId), ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Journals.Domain/UpdateGlobalJournalEntryCommandHandler.cs b/shared/Interfold.Journals.Domain/UpdateGlobalJournalEntryCommandHandler.cs new file mode 100644 index 00000000..559a8e1d --- /dev/null +++ b/shared/Interfold.Journals.Domain/UpdateGlobalJournalEntryCommandHandler.cs @@ -0,0 +1,48 @@ +using Interfold.Journals.Contracts; +using Interfold.Journals.Contracts.Models.Commands; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Journals.Domain; + +public sealed class UpdateGlobalJournalEntryCommandHandler : IdempotentCommandHandler +{ + private readonly IJournalRepository _journalRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateGlobalJournalEntryCommandHandler( + IJournalRepository journalRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _journalRepository = journalRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.JournalGlobalUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (command.Payload.Title is null && command.Payload.Content is null && command.Payload.Color is null) + return RejectInvariant(command, EntityRefs.JournalNoFields); + + if (command.Payload.Title is not null && command.Payload.Title.Length > 250) + return RejectInvariant(command, EntityRefs.JournalTitleTooLong); + + return await GlobalJournalCommandFlow.ExecuteUpdateAsync( + command, + command.Payload.EntryId, + _journalRepository, + _eventBus, + ct => _journalRepository.UpdateGlobalAsync(command.PrincipalId, command.Payload, ct), + EntityRefs.JournalUpdateFailed, + cancellationToken); + } + +} diff --git a/shared/Interfold.Polls.Contracts/Enums/PollType.cs b/shared/Interfold.Polls.Contracts/Enums/PollType.cs new file mode 100644 index 00000000..f103132b --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Enums/PollType.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Poll type. Wire: lowercase name. Storage: smallint 0/1/2. Persistence read uses +/// fallback-less FromCode<PollType>() so corrupt rows throw. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum PollType : short +{ + [JsonStringEnumMemberName("vote")] + Vote = 0, + + [JsonStringEnumMemberName("choice")] + Choice = 1, + + [JsonStringEnumMemberName("approval")] + Approval = 2, +} diff --git a/shared/Interfold.Polls.Contracts/Events/SocketProjectionEvents.cs b/shared/Interfold.Polls.Contracts/Events/SocketProjectionEvents.cs new file mode 100644 index 00000000..dc450e56 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Events/SocketProjectionEvents.cs @@ -0,0 +1,11 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Contracts.Events; + +public sealed record PollCreatedEvent(ScopedSystemId TargetSystemId, PollId PollId) : ITargetedClusterEvent; + +public sealed record PollUpdatedEvent(ScopedSystemId TargetSystemId, PollId PollId) : ITargetedClusterEvent; + +public sealed record PollDeletedEvent(ScopedSystemId TargetSystemId, PollId PollId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Polls.Contracts/Ids/PollId.cs b/shared/Interfold.Polls.Contracts/Ids/PollId.cs new file mode 100644 index 00000000..6a522a7f --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Ids/PollId.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Contracts.Ids; + +/// Strongly-typed wrapper around a poll id (Guid, wire form is 32-char lowercase hex). +[JsonConverter(typeof(PollIdJsonConverter))] +public readonly record struct PollId(Guid Value) : IParsable +{ + public static readonly PollId Empty = new(Guid.Empty); + + public static explicit operator PollId(Guid value) => new(value); + public static implicit operator Guid(PollId value) => value.Value; + + public override string ToString() => Value.ToString("N"); + + public static PollId Parse(string s, IFormatProvider? provider) + => UuidString.TryParse(s, out var g) + ? new(g) + : throw new FormatException($"Value '{s}' is not a valid PollId."); + + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out PollId result) + { + if (s is null || !UuidString.TryParse(s, out var g)) { result = default; return false; } + result = new PollId(g); + return true; + } +} + +internal sealed class PollIdJsonConverter : GuidIdJsonConverter +{ + protected override PollId Create(Guid value) => new(value); + protected override Guid GetValue(PollId value) => value.Value; + protected override string TypeLabel => nameof(PollId); +} diff --git a/shared/Interfold.Polls.Contracts/Interfold.Polls.Contracts.csproj b/shared/Interfold.Polls.Contracts/Interfold.Polls.Contracts.csproj new file mode 100644 index 00000000..e12958de --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Interfold.Polls.Contracts.csproj @@ -0,0 +1,12 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Polls.Contracts/Models/Commands/PollCommands.cs b/shared/Interfold.Polls.Contracts/Models/Commands/PollCommands.cs new file mode 100644 index 00000000..a558d3f4 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Models/Commands/PollCommands.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Polls.Contracts.Models.Commands; + +/// +/// Command payload for creating a poll. is a server-side +/// timestamp supplied by the caller: the public API uses "now" (matching the envelope's +/// OccurredAt), while the Simply Plural import decodes SP's MongoDB ObjectId +/// (the first 4 bytes are a big-endian Unix-seconds creation timestamp). The importer +/// falls back to SP's lastOperationTime, then to import time, only when the SP +/// id isn't a 24-hex ObjectId - both fallbacks log a warning so operators can spot +/// historical polls whose creation date isn't trustworthy. +/// +public sealed record CreatePollCommand( + string Title, + string? Description, + PollType Type, + DateTime? TimeEnd, + DateTime InsertedAtUtc +); + +// The Id property name is frozen: it serializes as "id" in persisted command JSON +// and idempotency hashes. +public sealed record UpdatePollCommand( + PollId Id, + string? Title, + string? Description, + DateTime? TimeEnd, + bool HasTimeEnd, + JsonElement? Data +); + +public sealed record DeletePollCommand(PollId PollId); diff --git a/shared/Interfold.Polls.Contracts/Models/PollData.cs b/shared/Interfold.Polls.Contracts/Models/PollData.cs new file mode 100644 index 00000000..f5229161 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Models/PollData.cs @@ -0,0 +1,146 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Contracts.Models; + +// Server-side producer/mutator models for the poll `data` blob interior. Wire is +// two closed schemas keyed by poll type: +// vote → {"responses":[{"alter_id","vote","comment"}],"allow_veto":bool} +// choice → {"choices":[{"id","name"}],"responses":[{"alter_id","choice_id","comment"}]} +// The transport contracts (PollReadModel.Data, UpdatePollCommand.Data) stay JsonElement +// so client blobs flow verbatim into persisted command JSON + idempotency hashes. + +/// Choice-poll option id — opaque, distinct from PollId/TagId to prevent cross-wiring. +[JsonConverter(typeof(PollChoiceIdJsonConverter))] +public readonly record struct PollChoiceId +{ + public string Value { get; } + + public PollChoiceId(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public override string ToString() => Value; +} + +internal sealed class PollChoiceIdJsonConverter : StringBackedJsonConverter +{ + protected override PollChoiceId Create(string value) => new(value); + protected override string GetValue(PollChoiceId value) => value.Value; +} + +public sealed record PollDataChoice( + [property: JsonPropertyName("id")] PollChoiceId Id, + [property: JsonPropertyName("name")] string Name); + +public sealed record PollDataResponse( + [property: JsonPropertyName("alter_id")] AlterId AlterId, + [property: JsonPropertyName("vote")][property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] VoteValue? Vote, + [property: JsonPropertyName("choice_id")][property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] PollChoiceId? ChoiceId, + [property: JsonPropertyName("comment")][property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] string? Comment); + +/// The four vote spellings the client's VoteType enum accepts; producers +/// MUST drop unparseable votes or the client's poll-list deserialise throws. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum VoteValue +{ + [JsonStringEnumMemberName("yes")] + Yes, + + [JsonStringEnumMemberName("no")] + No, + + [JsonStringEnumMemberName("abstain")] + Abstain, + + [JsonStringEnumMemberName("veto")] + Veto, +} + +/// Vote-poll data. allow_veto is always written (server holds truth). +public sealed record VotePollData( + [property: JsonPropertyName("responses")] IReadOnlyList Responses, + [property: JsonPropertyName("allow_veto")] bool AllowVeto); + +/// The choice-poll data shape. +public sealed record ChoicePollData( + [property: JsonPropertyName("choices")] IReadOnlyList Choices, + [property: JsonPropertyName("responses")] IReadOnlyList Responses); + +public static class PollDataJson +{ + /// Case-insensitive parse; null for null/empty/unknown so producers can drop. + public static VoteValue? TryParseVoteValue(string? raw) + { + if (string.IsNullOrWhiteSpace(raw)) + { + return null; + } + + return raw.TryParseWire(out var vote) ? vote : null; + } + + public static JsonElement ToJsonElement(T data) + => JsonSerializer.SerializeToElement(data); + + /// Removes every top-level responses[] entry whose alter_id + /// matches, preserving all other members (including unknown ones) verbatim and in + /// order. Returns false when nothing changed. + public static bool TryRemoveAlterResponses(JsonElement data, AlterId alterId, out JsonElement result) + { + result = data; + if (data.ValueKind != JsonValueKind.Object) + { + return false; + } + + if (!data.TryGetProperty("responses", out var responses) || responses.ValueKind != JsonValueKind.Array) + { + return false; + } + + var removedAny = false; + using var stream = new MemoryStream(); + using (var writer = new Utf8JsonWriter(stream)) + { + writer.WriteStartObject(); + foreach (var property in data.EnumerateObject()) + { + if (!property.NameEquals("responses")) + { + property.WriteTo(writer); + continue; + } + + writer.WriteStartArray("responses"); + foreach (var entry in property.Value.EnumerateArray()) + { + if (entry.ValueKind == JsonValueKind.Object + && entry.TryGetProperty("alter_id", out var entryAlterId) + && entryAlterId.ValueKind == JsonValueKind.Number + && entryAlterId.TryGetInt32(out var value) + && value == alterId.Value) + { + removedAny = true; + continue; + } + + entry.WriteTo(writer); + } + writer.WriteEndArray(); + } + writer.WriteEndObject(); + } + + if (!removedAny) + { + return false; + } + + result = JsonDocument.Parse(stream.ToArray()).RootElement.Clone(); + return true; + } +} diff --git a/shared/Interfold.Polls.Contracts/Models/Read/PollReadModel.cs b/shared/Interfold.Polls.Contracts/Models/Read/PollReadModel.cs new file mode 100644 index 00000000..2afdc591 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/Models/Read/PollReadModel.cs @@ -0,0 +1,40 @@ +using System.Text.Json; +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Contracts.Models.Read; + +public sealed record PollReadModel( + PollId Id, + SystemId UserId, + string Title, + string? Description, + PollType Type, + JsonElement Data, + DateTime? TimeEnd, + DateTime InsertedAt, + DateTime UpdatedAt +); + + +public sealed record CreatePollRequest( + string Title, + string? Description = null, + PollType? Type = null, + DateTime? TimeEnd = null +); + +/// +/// Sparse-diff PATCH body: the client only includes changed keys, and time_end +/// is tri-state (absent = unchanged, null = clear the deadline, string = set it) — +/// hence rather than a plain nullable. +/// +public sealed record UpdatePollRequest( + string? Title = null, + string? Description = null, + PatchValue TimeEnd = default, + JsonElement? Data = null +); + diff --git a/shared/Interfold.Polls.Contracts/PollCommandResult.cs b/shared/Interfold.Polls.Contracts/PollCommandResult.cs new file mode 100644 index 00000000..14082262 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/PollCommandResult.cs @@ -0,0 +1,10 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Contracts; + +public sealed record PollCommandResult(SystemId SystemId, PollId PollId, bool Replay) : ICommandResult +{ + public PollCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Polls.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Polls.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..e414a617 --- /dev/null +++ b/shared/Interfold.Polls.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,9 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Socket.Contracts; + +namespace Interfold.Polls.Contracts; + +public sealed record PollSocketPayload(PollReadModel Poll) : ISocketPayload; + +public sealed record PollDeletedSocketPayload(PollId PollId) : ISocketPayload; diff --git a/shared/Interfold.Polls.Domain/Abstractions/Repository/IPollRepository.cs b/shared/Interfold.Polls.Domain/Abstractions/Repository/IPollRepository.cs new file mode 100644 index 00000000..835b5fbd --- /dev/null +++ b/shared/Interfold.Polls.Domain/Abstractions/Repository/IPollRepository.cs @@ -0,0 +1,22 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Polls.Domain.Abstractions.Repository; + +public interface IPollRepository +{ + Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default); + + Task CreateAsync(SystemId systemId, CreatePollCommand command, CancellationToken cancellationToken = default); + + Task ExistsAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default); + + Task UpdateAsync(SystemId systemId, UpdatePollCommand command, CancellationToken cancellationToken = default); + + Task DeleteAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default); + Task RemoveAlterFromPollsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Polls.Domain/CreatePollCommandHandler.cs b/shared/Interfold.Polls.Domain/CreatePollCommandHandler.cs new file mode 100644 index 00000000..38f552cf --- /dev/null +++ b/shared/Interfold.Polls.Domain/CreatePollCommandHandler.cs @@ -0,0 +1,53 @@ +using Interfold.Polls.Contracts; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Polls.Domain; + +public sealed class CreatePollCommandHandler : IdempotentCommandHandler +{ + private readonly IPollRepository _pollRepository; + private readonly IClusterEventBus _eventBus; + + public CreatePollCommandHandler( + IPollRepository pollRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _pollRepository = pollRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.PollCreate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Title, EntityRefs.PollTitleRequired) is { } blankReject) + return blankReject; + + if (PollCommandValidation.GetTitleDescriptionValidationError(command.Payload.Title, command.Payload.Description) is { } validationError) + return RejectInvariant(command, validationError); + + // Stamp the row with the envelope's OccurredAt rather than honouring whatever + // InsertedAtUtc the caller put on the payload. The caller (PollsController) sends + // `default(DateTime)` precisely so the idempotency hash stays stable across retries + // — see PollsController.Create. The SP import bypasses this handler entirely and + // sets InsertedAtUtc itself from lastOperationTime, so it isn't affected here. + // OccurredAt is nullable on the envelope; fall back to UtcNow if missing. + var insertedAtUtc = (command.OccurredAt ?? DateTimeOffset.UtcNow).UtcDateTime; + var enrichedPayload = command.Payload with { InsertedAtUtc = insertedAtUtc }; + return await PollCommandFlow.ExecuteCreateAsync( + command, + _eventBus, + ct => _pollRepository.CreateAsync(command.PrincipalId, enrichedPayload, ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Polls.Domain/DeletePollCommandHandler.cs b/shared/Interfold.Polls.Domain/DeletePollCommandHandler.cs new file mode 100644 index 00000000..4bb27550 --- /dev/null +++ b/shared/Interfold.Polls.Domain/DeletePollCommandHandler.cs @@ -0,0 +1,40 @@ +using Interfold.Polls.Contracts; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Polls.Domain; + +public sealed class DeletePollCommandHandler : IdempotentCommandHandler +{ + private readonly IPollRepository _pollRepository; + private readonly IClusterEventBus _eventBus; + + public DeletePollCommandHandler( + IPollRepository pollRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _pollRepository = pollRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.PollDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + return await PollCommandFlow.ExecuteDeleteAsync( + command, + _pollRepository, + _eventBus, + cancellationToken); + } + +} diff --git a/shared/Interfold.Polls.Domain/Interfold.Polls.Domain.csproj b/shared/Interfold.Polls.Domain/Interfold.Polls.Domain.csproj new file mode 100644 index 00000000..36c85468 --- /dev/null +++ b/shared/Interfold.Polls.Domain/Interfold.Polls.Domain.csproj @@ -0,0 +1,12 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Polls.Domain/PollCommandFlow.cs b/shared/Interfold.Polls.Domain/PollCommandFlow.cs new file mode 100644 index 00000000..d0dec321 --- /dev/null +++ b/shared/Interfold.Polls.Domain/PollCommandFlow.cs @@ -0,0 +1,87 @@ +using Interfold.Polls.Contracts; +using Interfold.Polls.Contracts.Events; +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Polls.Domain; + +internal static class PollCommandFlow +{ + public static CommandExecutionResult Success(SystemId systemId, PollId pollId) + => CommandExecutionResult.Success(new PollCommandResult(systemId, pollId, Replay: false)); + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + IClusterEventBus eventBus, + Func> create, + CancellationToken cancellationToken = default) + { + var pollId = await create(cancellationToken); + if (pollId is null) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.PollCreateFailed); + + await eventBus.PublishAsync(new PollCreatedEvent(command.PrincipalId, pollId.Value), cancellationToken); + return Success(command.PrincipalId, pollId.Value); + } + + public static Task> ExecuteUpdateAsync( + CommandEnvelope command, + IPollRepository pollRepository, + IClusterEventBus eventBus, + CancellationToken cancellationToken = default) + => ExecuteExistingPollMutationAsync( + command, + command.Payload.Id, + pollRepository, + ct => pollRepository.UpdateAsync(command.PrincipalId, command.Payload, ct), + ct => eventBus.PublishAsync(new PollUpdatedEvent(command.PrincipalId, command.Payload.Id), ct), + EntityRefs.PollUpdateFailed, + cancellationToken); + + public static Task> ExecuteDeleteAsync( + CommandEnvelope command, + IPollRepository pollRepository, + IClusterEventBus eventBus, + CancellationToken cancellationToken = default) + => ExecuteExistingPollMutationAsync( + command, + command.Payload.PollId, + pollRepository, + ct => pollRepository.DeleteAsync(command.PrincipalId, command.Payload.PollId, ct), + ct => eventBus.PublishAsync(new PollDeletedEvent(command.PrincipalId, command.Payload.PollId), ct), + EntityRefs.PollDeleteFailed, + cancellationToken); + + // NOTE: mutateAsync and publishAccepted are DEFERRED (Func) rather than hot Task/ValueTask. + // The mutate must not execute unless the poll actually exists (otherwise a stray UPDATE/DELETE + // fires against the DB), and the publish must only fire on the accepted branch (otherwise + // downstream observers see events for polls that were never mutated). Both must also be + // observed so that any exceptions surface on the request path instead of becoming + // TaskScheduler.UnobservedTaskException. Passing hot awaitables here loses all three + // guarantees — see PollCommandFlowExecutionTests for the regression guards. + private static async Task> ExecuteExistingPollMutationAsync( + CommandEnvelope command, + PollId pollId, + IPollRepository pollRepository, + Func> mutateAsync, + Func publishAccepted, + EntityRef mutationFailedEntityRef, + CancellationToken cancellationToken) + { + var exists = await pollRepository.ExistsAsync(command.PrincipalId, pollId, cancellationToken); + if (!exists) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.PollNotFound); + + var changed = await mutateAsync(cancellationToken); + if (!changed) + return CommandHandler.RejectInvariant(command.OperationId, mutationFailedEntityRef); + + await publishAccepted(cancellationToken); + return Success(command.PrincipalId, pollId); + } +} diff --git a/shared/Interfold.Polls.Domain/PollCommandValidation.cs b/shared/Interfold.Polls.Domain/PollCommandValidation.cs new file mode 100644 index 00000000..b74d7913 --- /dev/null +++ b/shared/Interfold.Polls.Domain/PollCommandValidation.cs @@ -0,0 +1,25 @@ +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; + +namespace Interfold.Polls.Domain; + +internal static class PollCommandValidation +{ + public static EntityRef? GetTitleDescriptionValidationError(string? title, string? description) + { + if (title is not null && title.Length > 100) + return EntityRefs.PollTitleTooLong; + + if (description is not null && description.Length > 2000) + return EntityRefs.PollDescriptionTooLong; + + return null; + } + + public static bool HasNoMutableFields(UpdatePollCommand payload) + => payload.Title is null + && payload.Description is null + && !payload.HasTimeEnd + && payload.Data is null; +} diff --git a/shared/Interfold.Polls.Domain/UpdatePollCommandHandler.cs b/shared/Interfold.Polls.Domain/UpdatePollCommandHandler.cs new file mode 100644 index 00000000..5deff2ad --- /dev/null +++ b/shared/Interfold.Polls.Domain/UpdatePollCommandHandler.cs @@ -0,0 +1,45 @@ +using Interfold.Polls.Contracts; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Polls.Domain; + +public sealed class UpdatePollCommandHandler : IdempotentCommandHandler +{ + private readonly IPollRepository _pollRepository; + private readonly IClusterEventBus _eventBus; + + public UpdatePollCommandHandler( + IPollRepository pollRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _pollRepository = pollRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.PollUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (PollCommandValidation.HasNoMutableFields(command.Payload)) + return RejectInvariant(command, EntityRefs.PollNoFields); + + if (PollCommandValidation.GetTitleDescriptionValidationError(command.Payload.Title, command.Payload.Description) is { } validationError) + return RejectInvariant(command, validationError); + + return await PollCommandFlow.ExecuteUpdateAsync( + command, + _pollRepository, + _eventBus, + cancellationToken); + } + +} diff --git a/shared/Interfold.Settings.Contracts/Configuration/FcmConfiguration.cs b/shared/Interfold.Settings.Contracts/Configuration/FcmConfiguration.cs new file mode 100644 index 00000000..5c4fa75b --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Configuration/FcmConfiguration.cs @@ -0,0 +1,11 @@ +namespace Interfold.Settings.Contracts.Configuration; + +/// FCM v1 service-account credential — PRIVATE, never surfaced through HTTP. +/// Populated by FcmSecretsPostConfigure from +/// internal.secrets:fcm:service_account_json. Absent → NullFCMService fallback. +public sealed class FcmConfiguration +{ + public const string SectionName = "Octocon:Fcm"; + + public string? ServiceAccountJson { get; set; } +} diff --git a/shared/Interfold.Settings.Contracts/Configuration/FirebaseClientConfiguration.cs b/shared/Interfold.Settings.Contracts/Configuration/FirebaseClientConfiguration.cs new file mode 100644 index 00000000..a7757c61 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Configuration/FirebaseClientConfiguration.cs @@ -0,0 +1,46 @@ +namespace Interfold.Settings.Contracts.Configuration; + +/// Per-platform Firebase client-init payloads served by +/// GET /api/settings/firebase-config?platform=…. Public values — safe over an +/// unauth endpoint. Populated at startup from three optional internal.secrets rows +/// (firebase:client:{android|ios|web}); a missing row leaves the property null and +/// the endpoint returns 503 for that platform. +public sealed class FirebaseClientConfiguration +{ + public const string SectionName = "Octocon:Firebase"; + + public FirebaseAndroidClientConfig? Android { get; set; } + + public FirebaseIosClientConfig? Ios { get; set; } + + public FirebaseWebClientConfig? Web { get; set; } +} + +/// Android Firebase client-init payload (mirrors FirebaseOptions.Builder). +public sealed record FirebaseAndroidClientConfig( + string ApiKey, + string ApplicationId, + string ProjectId, + string GcmSenderId, + string? StorageBucket); + +/// iOS Firebase client-init payload (mirrors FirebaseOptions). +public sealed record FirebaseIosClientConfig( + string ApiKey, + string GoogleAppId, + string GcmSenderId, + string ProjectId, + string? StorageBucket, + string BundleId, + string? ClientId); + +/// Web (wasm) Firebase client-init payload. VapidKey is the public Web Push key +/// passed to messaging.getToken({ vapidKey }). +public sealed record FirebaseWebClientConfig( + string ApiKey, + string AuthDomain, + string ProjectId, + string? StorageBucket, + string MessagingSenderId, + string AppId, + string VapidKey); diff --git a/shared/Interfold.Settings.Contracts/Enums/EncryptionAction.cs b/shared/Interfold.Settings.Contracts/Enums/EncryptionAction.cs new file mode 100644 index 00000000..431119af --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Enums/EncryptionAction.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Outcome verb carried on EncryptionCommandResult.Action. Serialized into persisted +/// idempotency outcome payloads and HTTP response bodies; wire spellings +/// ("encryption_setup" / "encryption_recovered") are frozen. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum EncryptionAction +{ + [JsonStringEnumMemberName("encryption_setup")] + EncryptionSetup, + + [JsonStringEnumMemberName("encryption_recovered")] + EncryptionRecovered, +} diff --git a/shared/Interfold.Settings.Contracts/Enums/ExportFormat.cs b/shared/Interfold.Settings.Contracts/Enums/ExportFormat.cs new file mode 100644 index 00000000..43136467 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Enums/ExportFormat.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Selects the export payload shape. Wire: lowercase name. Placed on the shared +/// spine so picks it up for filename authority without a +/// second attribute registry. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ExportFormat : short +{ + [JsonStringEnumMemberName("pk")] + Pk = 0, + + [JsonStringEnumMemberName("full")] + Full = 1, +} diff --git a/shared/Interfold.Settings.Contracts/Enums/SettingsAction.cs b/shared/Interfold.Settings.Contracts/Enums/SettingsAction.cs new file mode 100644 index 00000000..610651f0 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Enums/SettingsAction.cs @@ -0,0 +1,57 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Outcome verb carried on SettingsCommandResult.Action. Serialized into persisted +/// idempotency outcome payloads and HTTP response bodies; the snake-case-lower wire spellings +/// ("description_updated", "avatar_uploaded", …) are frozen. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum SettingsAction +{ + [JsonStringEnumMemberName("description_updated")] + DescriptionUpdated, + + [JsonStringEnumMemberName("encryption_reset")] + EncryptionReset, + + [JsonStringEnumMemberName("push_token_added")] + PushTokenAdded, + + [JsonStringEnumMemberName("push_token_removed")] + PushTokenRemoved, + + [JsonStringEnumMemberName("avatar_uploaded")] + AvatarUploaded, + + [JsonStringEnumMemberName("avatar_deleted")] + AvatarDeleted, + + [JsonStringEnumMemberName("account_deleted")] + AccountDeleted, + + [JsonStringEnumMemberName("tags_wiped")] + TagsWiped, + + [JsonStringEnumMemberName("alters_wiped")] + AltersWiped, + + [JsonStringEnumMemberName("email_unlinked")] + EmailUnlinked, + + [JsonStringEnumMemberName("apple_unlinked")] + AppleUnlinked, + + [JsonStringEnumMemberName("discord_unlinked")] + DiscordUnlinked, + + [JsonStringEnumMemberName("field_updated")] + FieldUpdated, + + [JsonStringEnumMemberName("field_relocated")] + FieldRelocated, + + [JsonStringEnumMemberName("field_deleted")] + FieldDeleted, +} diff --git a/shared/Interfold.Settings.Contracts/Enums/SettingsFieldAction.cs b/shared/Interfold.Settings.Contracts/Enums/SettingsFieldAction.cs new file mode 100644 index 00000000..958660e3 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Enums/SettingsFieldAction.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Outcome verb carried on SettingsFieldCommandResult.Action. Single-member today +/// ("field_created"), typed as an enum for consistency with the other command-result actions. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum SettingsFieldAction +{ + [JsonStringEnumMemberName("field_created")] + FieldCreated, +} diff --git a/shared/Interfold.Settings.Contracts/Events/SettingsSocketProjectionEvents.cs b/shared/Interfold.Settings.Contracts/Events/SettingsSocketProjectionEvents.cs new file mode 100644 index 00000000..6c66dcc0 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Events/SettingsSocketProjectionEvents.cs @@ -0,0 +1,38 @@ +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Events; + +public sealed record SettingsFieldsChangedEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsProfileUpdatedEvent(ScopedSystemId TargetSystemId, bool EmitUsernameUpdated) : ITargetedClusterEvent; + +public sealed record SettingsAccountDeletedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsAltersWipedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsEncryptedDataWipedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsDiscordAccountUnlinkedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsAppleAccountUnlinkedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +// One event per provider (mirrors the per-provider unlink signal events below) so the +// identity carries its real type and neither producer nor consumer switches on a provider enum. +public sealed record SettingsDiscordAccountLinkedEvent(ScopedSystemId TargetSystemId, DiscordId DiscordId) : ITargetedClusterEvent; + +public sealed record SettingsGoogleAccountLinkedEvent(ScopedSystemId TargetSystemId, Email Email) : ITargetedClusterEvent; + +public sealed record SettingsAppleAccountLinkedEvent(ScopedSystemId TargetSystemId, AppleId AppleId) : ITargetedClusterEvent; + +public sealed record SettingsGoogleAccountUnlinkedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SettingsTagsWipedSignalEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record SimplyPluralImportCompletedEvent(ScopedSystemId TargetSystemId, int AlterCount) : ITargetedClusterEvent; + +public sealed record SimplyPluralImportFailedEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; + +public sealed record PluralKitImportCompletedEvent(ScopedSystemId TargetSystemId, int AlterCount) : ITargetedClusterEvent; + +public sealed record PluralKitImportFailedEvent(ScopedSystemId TargetSystemId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Settings.Contracts/Ids/EncryptionKeyMaterial.cs b/shared/Interfold.Settings.Contracts/Ids/EncryptionKeyMaterial.cs new file mode 100644 index 00000000..2ff4bf1d --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Ids/EncryptionKeyMaterial.cs @@ -0,0 +1,30 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Ids; + +/// +/// The derived per-system encryption key returned by the setup/recover encryption commands +/// (EncryptionCommandResult.Key). Secret material — ToString() redacts; the +/// raw-string converter keeps the persisted command-result JSON and replay hashes identical. +/// +[JsonConverter(typeof(EncryptionKeyMaterialJsonConverter))] +public readonly record struct EncryptionKeyMaterial +{ + public string Value { get; } + + public EncryptionKeyMaterial(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator EncryptionKeyMaterial(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class EncryptionKeyMaterialJsonConverter : StringBackedJsonConverter +{ + protected override EncryptionKeyMaterial Create(string value) => new(value); + protected override string GetValue(EncryptionKeyMaterial value) => value.Value; +} diff --git a/shared/Interfold.Settings.Contracts/Ids/EncryptionMaterial.cs b/shared/Interfold.Settings.Contracts/Ids/EncryptionMaterial.cs new file mode 100644 index 00000000..79f8132e --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Ids/EncryptionMaterial.cs @@ -0,0 +1,50 @@ +using System.Security.Cryptography; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// Checksum of the derived encryption key persisted to encryption_state.key_checksum +/// and compared on recover/import. ToString() redacts. +/// +public readonly record struct KeyChecksum +{ + public string Value { get; } + + public KeyChecksum(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator KeyChecksum(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); + + public static KeyChecksum? FromNullable(string? value) => value is null ? null : new KeyChecksum(value); +} + +/// +/// Per-system key-derivation salt persisted to encryption_state.salt. Not secret in +/// the cryptographic sense, but redacted anyway so state dumps don't hand out derivation +/// inputs alongside checksums. +/// +public readonly record struct EncryptionSalt +{ + private const int RandomByteWidth = 32; + + public string Value { get; } + + public EncryptionSalt(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator EncryptionSalt(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); + + public static EncryptionSalt? FromNullable(string? value) => value is null ? null : new EncryptionSalt(value); + + /// Mint a fresh cryptographically-random salt (32 bytes → base64). + public static EncryptionSalt NewRandom() + => new(Convert.ToBase64String(RandomNumberGenerator.GetBytes(RandomByteWidth))); +} diff --git a/shared/Interfold.Settings.Contracts/Ids/ImportOperationId.cs b/shared/Interfold.Settings.Contracts/Ids/ImportOperationId.cs new file mode 100644 index 00000000..3061df84 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Ids/ImportOperationId.cs @@ -0,0 +1,29 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Ids; + +/// TimeUuid surrogate key of an import_operations row; also the public +/// correlation handle in HTTP + socket frames. Distinct from +/// (the command-vocabulary id string). JSON serializes as the standard "D" GUID. +[JsonConverter(typeof(ImportOperationIdJsonConverter))] +public readonly record struct ImportOperationId(Guid Value) +{ + public static readonly ImportOperationId Empty = new(Guid.Empty); + + public static explicit operator ImportOperationId(Guid value) => new(value); + + public static implicit operator Guid(ImportOperationId value) => value.Value; + + public override string ToString() => Value.ToString("D"); +} + +internal sealed class ImportOperationIdJsonConverter : JsonConverter +{ + public override ImportOperationId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.GetGuid()); + + public override void Write(Utf8JsonWriter writer, ImportOperationId value, JsonSerializerOptions options) + => writer.WriteStringValue(value.Value); +} diff --git a/shared/Interfold.Settings.Contracts/Ids/LinkToken.cs b/shared/Interfold.Settings.Contracts/Ids/LinkToken.cs new file mode 100644 index 00000000..9f66efc0 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Ids/LinkToken.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// One-time account-link token minted by GET /settings/link_token and consumed by the +/// /auth/link/{provider} flow. JSON serializes as the raw string +/// (LinkTokenReadModel.Token response body is unchanged); ToString() redacts. +/// +[JsonConverter(typeof(LinkTokenJsonConverter))] +public readonly record struct LinkToken +{ + public string Value { get; } + + public LinkToken(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator LinkToken(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); + + /// Total function: null/empty/whitespace input → null wrapper. + public static LinkToken? From(string? value) + => string.IsNullOrWhiteSpace(value) ? null : new LinkToken(value); +} + +internal sealed class LinkTokenJsonConverter : StringBackedJsonConverter +{ + protected override LinkToken Create(string value) => new(value); + protected override string GetValue(LinkToken value) => value.Value; +} diff --git a/shared/Interfold.Settings.Contracts/Ids/SettingsTokens.cs b/shared/Interfold.Settings.Contracts/Ids/SettingsTokens.cs new file mode 100644 index 00000000..11567a19 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Ids/SettingsTokens.cs @@ -0,0 +1,83 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Ids; + +/// +/// Device push-notification registration token (FCM/APNs). Carried on +/// AddPushTokenCommand/RemovePushTokenCommand whose payloads are persisted and +/// hashed by the idempotency store — the converter emits the raw string so stored hashes +/// remain valid. ToString() redacts via on spine. +/// +[JsonConverter(typeof(PushTokenJsonConverter))] +public readonly record struct PushToken +{ + public string Value { get; } + + public PushToken(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator PushToken(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class PushTokenJsonConverter : StringBackedJsonConverter +{ + protected override PushToken Create(string value) => new(value); + protected override string GetValue(PushToken value) => value.Value; +} + +/// +/// Caller-supplied third-party API token (SimplyPlural or PluralKit) used by the async import +/// pipeline. Carried on ImportSpCommand/ImportPkCommand whose persisted payload +/// hashes must not move — the converter emits the raw string. ToString() redacts. +/// +[JsonConverter(typeof(ImportTokenJsonConverter))] +public readonly record struct ImportToken +{ + public string Value { get; } + + public ImportToken(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator ImportToken(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class ImportTokenJsonConverter : StringBackedJsonConverter +{ + protected override ImportToken Create(string value) => new(value); + protected override string GetValue(ImportToken value) => value.Value; +} + +/// +/// Plaintext encryption recovery code (post-decryption). Carried on ImportSpCommand +/// (persisted payload — raw-string converter keeps hashes valid) and the import job queue. +/// ToString() redacts. +/// +[JsonConverter(typeof(RecoveryCodeJsonConverter))] +public readonly record struct RecoveryCode +{ + public string Value { get; } + + public RecoveryCode(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator RecoveryCode(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class RecoveryCodeJsonConverter : StringBackedJsonConverter +{ + protected override RecoveryCode Create(string value) => new(value); + protected override string GetValue(RecoveryCode value) => value.Value; +} diff --git a/shared/Interfold.Settings.Contracts/Interfold.Settings.Contracts.csproj b/shared/Interfold.Settings.Contracts/Interfold.Settings.Contracts.csproj new file mode 100644 index 00000000..50294f6a --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Interfold.Settings.Contracts.csproj @@ -0,0 +1,13 @@ + + + net10.0 + enable + enable + + + + + + + diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/AccountCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/AccountCommands.cs new file mode 100644 index 00000000..2d502823 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/AccountCommands.cs @@ -0,0 +1,21 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +// Persisted + idempotency-hashed payload; Username's raw-string converter keeps stored +// hashes valid. +public sealed record UpdateUsernameCommand(Username Username); + +public sealed record UnlinkDiscordCommand(); + +public sealed record UnlinkEmailCommand(); + +public sealed record UnlinkAppleCommand(); + +public sealed record DeleteAccountCommand(); + +public sealed record WipeAltersCommand(); + +public sealed record WipeTagsCommand(); + +public sealed record CreateLinkTokenCommand(); diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/AvatarCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/AvatarCommands.cs new file mode 100644 index 00000000..73ffb95f --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/AvatarCommands.cs @@ -0,0 +1,8 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +public sealed record UploadAvatarCommand(AvatarUrl AvatarUrl, AvatarSource Source); + +public sealed record DeleteAvatarCommand(); diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/EncryptionCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/EncryptionCommands.cs new file mode 100644 index 00000000..761f83a1 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/EncryptionCommands.cs @@ -0,0 +1,11 @@ +using Interfold.Settings.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +// RecoveryCode's converter emits the raw string, so persisted command JSON and +// idempotency hashes are unchanged; the property name is frozen. +public sealed record SetupEncryptionCommand(RecoveryCode RecoveryCode); + +public sealed record RecoverEncryptionCommand(RecoveryCode RecoveryCode); + +public sealed record ResetEncryptionCommand(); diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/ImportCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/ImportCommands.cs new file mode 100644 index 00000000..0457d5df --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/ImportCommands.cs @@ -0,0 +1,7 @@ +using Interfold.Settings.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +public sealed record ImportPkCommand(ImportToken Token); + +public sealed record ImportSpCommand(ImportToken Token, RecoveryCode? RecoveryCode); diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/NotificationTokenCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/NotificationTokenCommands.cs new file mode 100644 index 00000000..648c6e7b --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/NotificationTokenCommands.cs @@ -0,0 +1,9 @@ +using Interfold.Settings.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +public sealed record AddPushTokenCommand(PushToken Token); + +public sealed record RemovePushTokenCommand(PushToken Token); + +public sealed record UpdateDescriptionCommand(string Description); diff --git a/shared/Interfold.Settings.Contracts/Models/Commands/SettingsFieldCommands.cs b/shared/Interfold.Settings.Contracts/Models/Commands/SettingsFieldCommands.cs new file mode 100644 index 00000000..bd8c762f --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Commands/SettingsFieldCommands.cs @@ -0,0 +1,19 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Commands; + +/// +/// Public-API callers send InsertedAtUtc: default(DateTime). The handler stamps the +/// real value from the envelope's OccurredAt after the idempotency hash is taken, so +/// the hashed payload stays stable across retries (otherwise every replay would carry a fresh +/// DateTime.UtcNow and trip ConflictDuplicate). The Simply Plural importer +/// constructs the command directly with the decoded ObjectId timestamp. +/// +public sealed record CreateFieldCommand(string Name, FieldType Type, VisibilityLevel SecurityLevel, bool Locked, DateTime InsertedAtUtc); + +public sealed record UpdateFieldCommand(FieldId FieldId, string? Name, VisibilityLevel? SecurityLevel, bool? Locked); + +public sealed record DeleteFieldCommand(FieldId FieldId); + +public sealed record RelocateFieldCommand(FieldId FieldId, int Index); diff --git a/shared/Interfold.Settings.Contracts/Models/EncryptionState.cs b/shared/Interfold.Settings.Contracts/Models/EncryptionState.cs new file mode 100644 index 00000000..87f84b59 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/EncryptionState.cs @@ -0,0 +1,5 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Models; + +public sealed record EncryptionState(bool Initialized, KeyChecksum? KeyChecksum, EncryptionSalt? Salt); diff --git a/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportErrorCode.cs b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportErrorCode.cs new file mode 100644 index 00000000..f112c54d --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportErrorCode.cs @@ -0,0 +1,49 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Settings.Contracts.Models.ImportOperations; + +/// +/// Stable machine codes for terminal import failures, persisted to the +/// import_operations.error_code column (operators grep/alert on the exact strings — +/// the wire spellings pinned by are frozen). +/// Both paths are typed: writes emit (via the +/// ToWire() extension), reads rehydrate via the tolerant +/// (unknown legacy spellings resolve to +/// so the caller can substitute rather than throw). +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ImportErrorCode +{ + /// The Simply Plural importer returned a graceful failure. + [JsonStringEnumMemberName("sp_import_failed")] + SpImportFailed, + + /// Simply Plural rejected the supplied token. + [JsonStringEnumMemberName("sp_auth_failed")] + SpAuthFailed, + + /// Generic fallback when a runner failed without a specific code. + [JsonStringEnumMemberName("import_failed")] + ImportFailed, + + /// No IImportJobRunner is registered for the operation kind (DI misregistration). + [JsonStringEnumMemberName("no_runner")] + NoRunner, + + /// The row was stuck in Running when a fresh host booted; failed by the startup sweep. + [JsonStringEnumMemberName("host_restart")] + HostRestart, + + /// The worker was cancelled mid-job by host shutdown. + [JsonStringEnumMemberName("host_shutdown")] + HostShutdown, + + /// The runner threw an unhandled exception. + [JsonStringEnumMemberName("exception")] + Exception, + + /// The runner is a stub; the importer has not been implemented yet. + [JsonStringEnumMemberName("unimplemented")] + Unimplemented, +} diff --git a/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationDispatchStatus.cs b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationDispatchStatus.cs new file mode 100644 index 00000000..8bfbc488 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationDispatchStatus.cs @@ -0,0 +1,28 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Contracts.Models.ImportOperations; + +/// +/// Outcome an import-dispatch controller call reports back to the HTTP caller. Two values: +/// Queued (this dispatch minted a fresh operation) and Running (an import was +/// already in flight and this dispatch collapsed onto it). Wire form is the lowercase enum +/// name so the pre-enum client contract stays byte-identical. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ImportOperationDispatchStatus +{ + /// + /// This dispatch claimed the per-system slot. A background worker will pick up the + /// job; the HTTP caller can subscribe to the completion WebSocket frame. + /// + [JsonStringEnumMemberName("queued")] + Queued, + + /// + /// An import was already in flight for this system when the dispatch arrived. The + /// caller gets the in-flight operation id back and MUST NOT enqueue a second worker + /// run — the existing job's completion frame answers both callers. + /// + [JsonStringEnumMemberName("running")] + Running, +} diff --git a/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationKind.cs b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationKind.cs new file mode 100644 index 00000000..666eaed3 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationKind.cs @@ -0,0 +1,26 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Contracts.Models.ImportOperations; + +/// +/// The third-party platform an import operation is pulling data from. Persisted as its +/// lowercase enum-name string in the import_operations.kind column and emitted as +/// the same value in socket completion frames + HTTP dispatch responses. A future +/// integration adds a value here (and a matching socket-frame constant in +/// SocketEventNames.Imports). +/// +/// +/// Wire values are locked to "sp" / "pk" via +/// so the enum names can evolve without breaking the persisted / on-wire representation. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ImportOperationKind +{ + /// Simply Plural — handled by SimplyPluralImportService. Wire value: "sp". + [JsonStringEnumMemberName("sp")] + SimplyPlural, + + /// PluralKit — handler is currently a stub but inherits the same dispatch model. Wire value: "pk". + [JsonStringEnumMemberName("pk")] + PluralKit, +} diff --git a/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationSnapshot.cs b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationSnapshot.cs new file mode 100644 index 00000000..b94b9b81 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationSnapshot.cs @@ -0,0 +1,26 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.ImportOperations; + +/// Read-side snapshot of a row in import_operations. Carries every column +/// so callers can decide (publish completion, mark failed, retry) without a repository +/// round-trip. +/// Set only when Status is Succeeded / Failed. +/// Set only when Status is Succeeded. +/// Null when Status ≠ Failed OR the persisted string no longer +/// parses (tolerant read via ). +/// Audit-only. Per-system mutex on active_import_by_system +/// is the load-bearing dedupe mechanism. +public sealed record ImportOperationSnapshot( + SystemId SystemId, + ImportOperationId OperationId, + ImportOperationKind Kind, + ImportOperationStatus Status, + DateTimeOffset StartedAt, + DateTimeOffset? FinishedAt, + int? AlterCount, + ImportErrorCode? ErrorCode, + string? ErrorMessage, + IdempotencyKey IdempotencyKey); diff --git a/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationStatus.cs b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationStatus.cs new file mode 100644 index 00000000..3ecec104 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/ImportOperations/ImportOperationStatus.cs @@ -0,0 +1,51 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Settings.Contracts.Models.ImportOperations; + +/// +/// Lifecycle of an asynchronous third-party import (SP or PK). Persisted in the +/// import_operations.status column via , with the +/// wire spelling of each member pinned by +/// so a C# rename can't drift the DB format. Reads are case-insensitive to match the +/// historical tolerant Enum.TryParse behaviour. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ImportOperationStatus +{ + /// + /// The operation row exists and a worker has been signalled to start, but execution + /// has not yet begun. This is the state the row is created in by + /// IImportOperationRepository.TryClaimAsync. A row should not stay in this + /// state for long; if it does, the worker has not picked it up (host bottleneck) or + /// the worker process crashed before transitioning to . + /// + [JsonStringEnumMemberName("Queued")] + Queued, + + /// + /// The background worker has begun executing the import. Transitioned via + /// MarkRunningAsync. Stale rows in this state are recovered by the + /// background service's startup sweep (any Running row whose + /// started_at is older than the configured ceiling is rewritten to + /// with a host-restart error code). + /// + [JsonStringEnumMemberName("Running")] + Running, + + /// + /// Terminal: the importer returned Success = true. alter_count is + /// populated. The completion event has been published on the cluster bus. + /// + [JsonStringEnumMemberName("Succeeded")] + Succeeded, + + /// + /// Terminal: the importer either returned Success = false (graceful failure + /// like auth or encryption mismatch) or threw. error_code and + /// error_message are populated. The failure event has been published on the + /// cluster bus. + /// + [JsonStringEnumMemberName("Failed")] + Failed, +} diff --git a/shared/Interfold.Settings.Contracts/Models/Read/LinkTokenReadModel.cs b/shared/Interfold.Settings.Contracts/Models/Read/LinkTokenReadModel.cs new file mode 100644 index 00000000..473b527e --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Read/LinkTokenReadModel.cs @@ -0,0 +1,9 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Read; + +/// +/// Response body for GET /settings/link_token. The token JSON-serializes as the raw +/// string (see ) so the client-visible shape is unchanged. +/// +public sealed record LinkTokenReadModel(LinkToken Token); diff --git a/shared/Interfold.Settings.Contracts/Models/Read/SettingsWireRequests.cs b/shared/Interfold.Settings.Contracts/Models/Read/SettingsWireRequests.cs new file mode 100644 index 00000000..bbc15f12 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Read/SettingsWireRequests.cs @@ -0,0 +1,43 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Read; + +public sealed record SettingsUsernameRequest( + Username Username +); + +public sealed record SettingsDescriptionRequest( + string Description +); + +public sealed record SettingsPushTokenRequest( + PushToken? Token = null +); + +public sealed record SettingsEncryptionRequest( + RecoveryCode RecoveryCode +); + +public sealed record SettingsImportRequest( + ImportToken Token, + RecoveryCode? RecoveryCode = null +); + +public sealed record SettingsCreateFieldRequest( + string Name, + FieldType? Type, + VisibilityLevel? SecurityLevel, + bool? Locked +); + +public sealed record SettingsUpdateFieldRequest( + string? Name, + VisibilityLevel? SecurityLevel, + bool? Locked +); + +public sealed record SettingsRelocateFieldRequest( + int Index +); diff --git a/shared/Interfold.Settings.Contracts/Models/Wire/EncryptionKeyResponse.cs b/shared/Interfold.Settings.Contracts/Models/Wire/EncryptionKeyResponse.cs new file mode 100644 index 00000000..945b1bb7 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Wire/EncryptionKeyResponse.cs @@ -0,0 +1,3 @@ +namespace Interfold.Settings.Contracts.Models.Wire; + +public sealed record EncryptionKeyResponse(string Key); diff --git a/shared/Interfold.Settings.Contracts/Models/Wire/FieldCreatedResponse.cs b/shared/Interfold.Settings.Contracts/Models/Wire/FieldCreatedResponse.cs new file mode 100644 index 00000000..9129e25f --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Wire/FieldCreatedResponse.cs @@ -0,0 +1,5 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts.Models.Wire; + +public sealed record FieldCreatedResponse(FieldId Id); diff --git a/shared/Interfold.Settings.Contracts/Models/Wire/FirebaseClientConfigResponses.cs b/shared/Interfold.Settings.Contracts/Models/Wire/FirebaseClientConfigResponses.cs new file mode 100644 index 00000000..287f8124 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Wire/FirebaseClientConfigResponses.cs @@ -0,0 +1,51 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Settings.Contracts.Models.Wire; + +/// Response body for GET /api/settings/firebase-config?platform=…. +/// The three variants map 1:1 to the client-side Firebase init shapes; STJ emits a +/// type discriminator ("android"/"ios"/"web") matching the query parameter so +/// the client's kotlinx-serialization sealed hierarchy dispatches without a wrapper. +[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] +[JsonDerivedType(typeof(FirebaseAndroidConfigResponse), typeDiscriminator: "android")] +[JsonDerivedType(typeof(FirebaseIosConfigResponse), typeDiscriminator: "ios")] +[JsonDerivedType(typeof(FirebaseWebConfigResponse), typeDiscriminator: "web")] +public abstract record FirebaseClientConfigResponse; + +/// +/// Android response variant. Fields map into +/// com.google.firebase.FirebaseOptions.Builder on the client. +/// +public sealed record FirebaseAndroidConfigResponse( + string ApiKey, + string ApplicationId, + string ProjectId, + string GcmSenderId, + string? StorageBucket) : FirebaseClientConfigResponse; + +/// +/// iOS response variant. Fields map into FIROptions / FirebaseOptions on +/// the client. ClientId is optional because it's only meaningful for deployments +/// that use Firebase Auth's Google Sign-In (FCM-only setups can leave it null). +/// +public sealed record FirebaseIosConfigResponse( + string ApiKey, + string GoogleAppId, + string GcmSenderId, + string ProjectId, + string? StorageBucket, + string BundleId, + string? ClientId) : FirebaseClientConfigResponse; + +/// +/// Web (wasm) response variant. Includes the public VAPID key required by +/// messaging.getToken({ vapidKey }) in the Firebase JS SDK. +/// +public sealed record FirebaseWebConfigResponse( + string ApiKey, + string AuthDomain, + string ProjectId, + string? StorageBucket, + string MessagingSenderId, + string AppId, + string VapidKey) : FirebaseClientConfigResponse; diff --git a/shared/Interfold.Settings.Contracts/Models/Wire/ImportDispatchResponse.cs b/shared/Interfold.Settings.Contracts/Models/Wire/ImportDispatchResponse.cs new file mode 100644 index 00000000..f463b763 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Wire/ImportDispatchResponse.cs @@ -0,0 +1,28 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; + +namespace Interfold.Settings.Contracts.Models.Wire; + +/// +/// Body returned by the asynchronous-import endpoints (POST /api/settings/import-sp +/// and POST /api/settings/import-pk) on a successful dispatch. The HTTP status is +/// always 202 Accepted; the actual import lifecycle is then driven over the +/// WebSocket with the existing sp_import_complete / sp_import_failed +/// (or pk_*) frames. +/// +/// +/// The Compose frontend at +/// app/octocon/app/ui/compose/screens/main/settings/SettingsRootScreen.kt already +/// ignores the body on success and listens to the WebSocket for the terminal frame — so +/// this DTO is primarily for operator visibility and any future client that wants the +/// correlation handle. We still ship the values so the contract is self-documenting and +/// integration tests can assert on them. +/// +/// +/// Server-minted TimeUuid surrogate for this dispatch. Same value flows through the import_operations Cassandra row. +/// Either (this dispatch claimed a fresh slot) or (an import was already in flight for the system and this dispatch collapsed onto it). Serialises as "queued"/"running". +/// When the dispatcher observed the claim. For a collapsed dispatch this is the observation time of the *second* request, not the original — clients that care about the true import start should read it from the eventual completion frame. +public sealed record ImportDispatchResponse( + ImportOperationId OperationId, + ImportOperationDispatchStatus Status, + DateTimeOffset StartedAt); diff --git a/shared/Interfold.Settings.Contracts/Models/Wire/LinkTokenUnavailableResponse.cs b/shared/Interfold.Settings.Contracts/Models/Wire/LinkTokenUnavailableResponse.cs new file mode 100644 index 00000000..0c5f182b --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Models/Wire/LinkTokenUnavailableResponse.cs @@ -0,0 +1,8 @@ +namespace Interfold.Settings.Contracts.Models.Wire; + +/// +/// 503 body for GET /settings/link_token on non-primary nodes. Intentionally +/// non-standard (carries hint instead of code) — the Kotlin client and the +/// legacy Elixir server both use this exact shape, so it is frozen. +/// +public sealed record LinkTokenUnavailableResponse(string Error, string Hint); diff --git a/shared/Interfold.Settings.Contracts/OperationResults.cs b/shared/Interfold.Settings.Contracts/OperationResults.cs new file mode 100644 index 00000000..ccf45bc2 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/OperationResults.cs @@ -0,0 +1,41 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Contracts; + +public sealed record AccountCommandResult(SystemId SystemId, Username Username, bool Replay) : ICommandResult +{ + public AccountCommandResult WithReplay() => this with { Replay = true }; +} + +public sealed record SettingsCommandResult(SystemId SystemId, SettingsAction Action, bool Replay) : ICommandResult +{ + public SettingsCommandResult WithReplay() => this with { Replay = true }; +} + +/// Result of dispatching an async SP/PK import onto the in-process worker queue; +/// serialised as the HTTP 202 body. Status: "queued" = fresh slot claimed; +/// "running" = collapsed onto an existing operation. +public sealed record ImportDispatchCommandResult( + SystemId SystemId, + ImportOperationId OperationId, + ImportOperationKind Kind, + ImportOperationDispatchStatus Status, + DateTimeOffset StartedAt, + bool Replay) : ICommandResult +{ + public ImportDispatchCommandResult WithReplay() => this with { Replay = true }; +} + +public sealed record SettingsFieldCommandResult(SystemId SystemId, SettingsFieldAction Action, FieldId FieldId, bool Replay) : ICommandResult +{ + public SettingsFieldCommandResult WithReplay() => this with { Replay = true }; +} + +public sealed record EncryptionCommandResult(SystemId SystemId, EncryptionAction Action, EncryptionKeyMaterial Key, bool Replay) : ICommandResult +{ + public EncryptionCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Settings.Contracts/Operations/SettingsActionEntityRefExtensions.cs b/shared/Interfold.Settings.Contracts/Operations/SettingsActionEntityRefExtensions.cs new file mode 100644 index 00000000..f25b5bf4 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/Operations/SettingsActionEntityRefExtensions.cs @@ -0,0 +1,12 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Operations; + +public static class SettingsActionEntityRefExtensions +{ + /// Invariant-violation ref for a failed settings command, + /// e.g. "settings:avatar_uploaded_failed". + public static EntityRef ToFailedEntityRef(this SettingsAction action) + => new($"settings:{action.ToWire()}_failed"); +} diff --git a/shared/Interfold.Settings.Contracts/SettingsTaskNames.cs b/shared/Interfold.Settings.Contracts/SettingsTaskNames.cs new file mode 100644 index 00000000..de44f632 --- /dev/null +++ b/shared/Interfold.Settings.Contracts/SettingsTaskNames.cs @@ -0,0 +1,8 @@ +namespace Interfold.Shared.Contracts; + +/// Settings-owned singleton task names. +public static class SettingsTaskNames +{ + /// Single-writer link-token issuer. + public static readonly SingletonTaskName LinkTokenRegistry = new("link_token_registry"); +} diff --git a/shared/Interfold.Settings.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Settings.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..ee5bbb5d --- /dev/null +++ b/shared/Interfold.Settings.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,19 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Socket.Contracts; + +namespace Interfold.Settings.Contracts; + +public sealed record SettingsFieldsUpdatedPayload(IReadOnlyList Fields) : ISocketPayload; + +public sealed record SettingsUsernameUpdatedPayload(Username Username) : ISocketPayload; + +public sealed record SettingsSelfUpdatedPayload(SocketSelfReadModel Data) : ISocketPayload; + +public sealed record DiscordAccountLinkedPayload(DiscordId DiscordId) : ISocketPayload; + +public sealed record GoogleAccountLinkedPayload(Email Email) : ISocketPayload; + +public sealed record AppleAccountLinkedPayload(AppleId AppleId) : ISocketPayload; + +public sealed record ImportCompletedSocketPayload(int AlterCount) : ISocketPayload; diff --git a/shared/Interfold.Settings.Domain/Abstractions/ISimplyPluralImportService.cs b/shared/Interfold.Settings.Domain/Abstractions/ISimplyPluralImportService.cs new file mode 100644 index 00000000..c936f1ab --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/ISimplyPluralImportService.cs @@ -0,0 +1,20 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Domain.Abstractions; + +/// Full SP data import for a system. MUST NOT throw on graceful failures +/// (auth, encryption, upstream 4xx) — return Success=false with ErrorCode/ErrorMessage. +/// Throws are transport/programming errors, classified by the worker as +/// ImportErrorCode.Exception. +public interface ISimplyPluralImportService +{ + Task ImportAsync( + SystemId systemId, + ImportToken spToken, + RecoveryCode? encryptionKey, + CancellationToken cancellationToken = default); + + bool? WaitForAvatars { get; set; } +} diff --git a/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobQueue.cs b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobQueue.cs new file mode 100644 index 00000000..636d849a --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobQueue.cs @@ -0,0 +1,14 @@ +namespace Interfold.Settings.Domain.Abstractions.ImportJobs; + +/// In-process FIFO import-job queue. Handlers enqueue after LWT slot claim; +/// ImportJobBackgroundService consumes. Cross-replica dedupe lives in the +/// per-system LWT mutex, so a local channel is sufficient. +public interface IImportJobQueue +{ + /// Buffers . Bounded channel — enforces back-pressure + /// against runaway producers, not normal-use throttling. + ValueTask EnqueueAsync(ImportJobItem item, CancellationToken cancellationToken = default); + + /// FIFO consumption. One IImportJobQueue → one consuming worker. + IAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobRunner.cs b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobRunner.cs new file mode 100644 index 00000000..47a969ca --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/IImportJobRunner.cs @@ -0,0 +1,23 @@ +using Interfold.Settings.Contracts.Models.ImportOperations; + +namespace Interfold.Settings.Domain.Abstractions.ImportJobs; + +/// Per-kind importer used by the background worker. Worker matches +/// to the dequeued . Implementations +/// live wherever the downstream client lives (SP runner in Interfold.Api; PK is a stub). +public interface IImportJobRunner +{ + ImportOperationKind Kind { get; } + + /// Runs the import. MUST NOT throw on graceful failure — return + /// = false with a populated ErrorCode. Any + /// escaped exception is caught by the worker and treated as error_code = "exception". + Task RunAsync(ImportJobItem item, CancellationToken cancellationToken = default); +} + +/// Import result mirroring the worker's terminal-state vocabulary. +public sealed record ImportJobOutcome( + bool Success, + int AlterCount, + ImportErrorCode? ErrorCode = null, + string? ErrorMessage = null); diff --git a/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/ImportJobItem.cs b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/ImportJobItem.cs new file mode 100644 index 00000000..ec8c034f --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/ImportJobs/ImportJobItem.cs @@ -0,0 +1,18 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Domain.Abstractions.ImportJobs; + +/// A unit of work for the async import worker. Handler → in-process queue → +/// runner; secrets travel through this record but never persist to +/// import_operations. +/// Third-party API token. Sensitive — never log. +/// Optional SP recovery code, plaintext post-decryption. +/// Null for PK / SP-without-recovery. Sensitive — never log. +public sealed record ImportJobItem( + ImportOperationId OperationId, + ScopedSystemId SystemId, + ImportOperationKind Kind, + ImportToken Token, + RecoveryCode? RecoveryCode); diff --git a/shared/Interfold.Settings.Domain/Abstractions/Repository/IAccountRepository.cs b/shared/Interfold.Settings.Domain/Abstractions/Repository/IAccountRepository.cs new file mode 100644 index 00000000..10d1f903 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/Repository/IAccountRepository.cs @@ -0,0 +1,98 @@ +using Interfold.Auth.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Systems.Contracts.Models.Read; + +namespace Interfold.Settings.Domain.Abstractions.Repository; + +public interface IAccountRepository +{ + Task UpdateUsernameAsync(SystemId systemId, Username username, CancellationToken cancellationToken = default); + + Task UpdateDescriptionAsync(SystemId systemId, string description, CancellationToken cancellationToken = default); + + Task UpdateAvatarAsync(SystemId systemId, AvatarUrl avatarUrl, AvatarSource source, CancellationToken cancellationToken = default); + + Task ClearAvatarAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetOrCreateLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default); + + /// + /// Read-only lookup: returns the existing link token for , + /// or if none has been created yet. + /// Safe to call on non-primary nodes. + /// + Task GetLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task ResolveSystemIdByLinkTokenAsync(LinkToken linkToken, CancellationToken cancellationToken = default); + + Task ClearLinkTokenAsync(SystemId systemId, CancellationToken cancellationToken = default); + + /// + /// Read-only lookup: returns the system id linked to , or + /// if none exists. Does NOT create an account on miss — use this + /// from surfaces where an unknown Discord id should surface as "no such user" (e.g. the + /// friendship Kind.Discord resolve branch) rather than spawn a phantom account. + /// + /// + /// The Discord shape is the only identity-specific find method — the OAuth-login flow + /// always goes through which auto-provisions + /// on miss, and the friendship resolve branch only supports the Discord shape. Kept + /// distinct from (rather than adding an + /// "auto-provision?" flag) so the intent is legible at the call site and the InMemory + /// repo can dispatch to a truly read-only branch that never touches the + /// encryption-salt bootstrap path. + /// + Task TryFindSystemIdByDiscordIdAsync(DiscordId discordId, CancellationToken cancellationToken = default); + + /// + /// OAuth-login shape: returns the system id linked to the OAuth + /// , or auto-provisions a new account for it (with a + /// fresh encryption salt) if none exists. Callers that must not auto-provision on + /// miss (e.g. the friendship resolver's Discord branch) should call + /// instead. + /// + /// + /// Returns when carries no + /// populated variant. Every construction site funnels through one of the + /// / + /// / + /// factories so this branch is + /// unreachable in practice — the guard exists to make an accidental + /// default(ProviderIdentity) pass surface as "no user" rather than as a + /// dispatch panic. + /// + /// + Task FindOrCreateSystemIdAsync(ProviderIdentity identity, CancellationToken cancellationToken = default); + + /// + /// Link the OAuth to the existing user identified by + /// . Returns , + /// , or + /// per the underlying repository's + /// contract. + /// + Task LinkIdentityToUserAsync(SystemId systemId, ProviderIdentity identity, CancellationToken cancellationToken = default); + + Task UnlinkDiscordAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task UnlinkEmailAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task UnlinkAppleAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task DeleteAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task GetPublicProfileAsync(SystemId systemId, CancellationToken cancellationToken = default); + + /// + /// Narrow "public wire view" projection used by + /// . Returns only + /// the five fields the public endpoint surfaces (id, username, description, avatar_url, + /// avatar_source) so the controller doesn't have to hand-project from the full + /// — which also carries private identity + /// links (Discord / Email / Apple) that must not leak on this route. Null when the + /// account doesn't exist. + /// + Task GetPublicSystemAsync(SystemId systemId, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Settings.Domain/Abstractions/Repository/IEncryptionStateRepository.cs b/shared/Interfold.Settings.Domain/Abstractions/Repository/IEncryptionStateRepository.cs new file mode 100644 index 00000000..69e3afcb --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/Repository/IEncryptionStateRepository.cs @@ -0,0 +1,11 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Settings.Domain.Abstractions.Repository; + +public interface IEncryptionStateRepository +{ + Task GetAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task UpsertAsync(SystemId systemId, bool initialized, KeyChecksum? keyChecksum, EncryptionSalt? salt, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Settings.Domain/Abstractions/Repository/IImportOperationRepository.cs b/shared/Interfold.Settings.Domain/Abstractions/Repository/IImportOperationRepository.cs new file mode 100644 index 00000000..01292c5a --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/Repository/IImportOperationRepository.cs @@ -0,0 +1,72 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Domain.Abstractions.Repository; + +/// Async-import job persistence. Backed by import_operations (append-only +/// history) + active_import_by_system (per-system LWT mutex). Invariant: at most +/// one operation per (system, kind) may be Queued or Running at a time — prevents the +/// duplicate-SP-import bug regardless of how a duplicate dispatch arrived. +public interface IImportOperationRepository +{ + /// Atomically claims the per-system import slot via LWT on + /// active_import_by_system. If no slot exists, inserts a new Queued row and + /// returns = true; otherwise returns the + /// existing operation id with IsNew = false and the caller MUST NOT enqueue another + /// worker run. + Task TryClaimAsync( + SystemId systemId, + ImportOperationKind kind, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default); + + /// Queued → Running. No-op when the row isn't Queued (guards against + /// double-pickup on channel replay). + Task MarkRunningAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default); + + /// Terminal-success transition: sets Succeeded + finished_at + alter_count and + /// releases the active slot so the next dispatch can proceed. + Task MarkSucceededAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + int alterCount, + CancellationToken cancellationToken = default); + + /// Terminal-failure transition: sets Failed + finished_at + error and releases + /// the active slot. Wire error_code is stable for branching; message is human-only. + Task MarkFailedAsync( + SystemId systemId, + ImportOperationId operationId, + ImportOperationKind kind, + ImportErrorCode errorCode, + string? errorMessage, + CancellationToken cancellationToken = default); + + Task GetByIdAsync( + SystemId systemId, + ImportOperationId operationId, + CancellationToken cancellationToken = default); + + /// Racy soft pre-check for a deterministic "already running" reply without + /// waiting on Paxos. TryClaimAsync stays the source of truth. + Task GetActiveOperationIdAsync( + SystemId systemId, + ImportOperationKind kind, + CancellationToken cancellationToken = default); + + /// Startup-sweep query: every Running row older than + /// so crash-orphaned rows can be marked failed instead of pinning the per-system slot. + /// Unbounded partition scan — acceptable at human-driven import volume. + Task> GetStaleRunningAsync( + TimeSpan olderThan, + CancellationToken cancellationToken = default); +} + +/// Outcome of . IsNew = +/// true → caller owns dispatching this work; false → slot already taken, do not enqueue. +public readonly record struct ImportOperationClaim(ImportOperationId OperationId, bool IsNew); diff --git a/shared/Interfold.Settings.Domain/Abstractions/Repository/INotificationTokenRepository.cs b/shared/Interfold.Settings.Domain/Abstractions/Repository/INotificationTokenRepository.cs new file mode 100644 index 00000000..c9fadb84 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/Repository/INotificationTokenRepository.cs @@ -0,0 +1,20 @@ +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Settings.Domain.Abstractions.Repository; + +public interface INotificationTokenRepository +{ + Task AddAsync(SystemId systemId, PushToken token, CancellationToken cancellationToken = default); + + Task RemoveAsync(PushToken token, CancellationToken cancellationToken = default); + + /// Push tokens for every friend of , grouped by + /// friend (each friend sees a different payload due to per-recipient visibility + /// filtering). Implementations must throttle fan-out via HydrationMaxConcurrency and + /// omit friends with zero devices rather than returning an empty token list. + Task> ListTokensForFriendsOfAsync(SystemId systemId, CancellationToken cancellationToken = default); +} + +/// One friend's registered push tokens (phone + tablet + browser collapse into one list). +public sealed record FriendNotificationTokens(SystemId FriendSystemId, IReadOnlyList Tokens); diff --git a/shared/Interfold.Settings.Domain/Abstractions/Repository/ISettingsFieldRepository.cs b/shared/Interfold.Settings.Domain/Abstractions/Repository/ISettingsFieldRepository.cs new file mode 100644 index 00000000..f899c485 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Abstractions/Repository/ISettingsFieldRepository.cs @@ -0,0 +1,31 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; + +namespace Interfold.Shared.Domain.Abstractions.Repository; + +public interface ISettingsFieldRepository +{ + Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task CreateAsync( + SystemId systemId, + string name, + FieldType type, + VisibilityLevel securityLevel, + bool locked, + DateTime insertedAtUtc, + CancellationToken cancellationToken = default); + + Task UpdateAsync( + SystemId systemId, + FieldId fieldId, + string? name, + VisibilityLevel? securityLevel, + bool? locked, + CancellationToken cancellationToken = default); + + Task DeleteAsync(SystemId systemId, FieldId fieldId, CancellationToken cancellationToken = default); + + Task RelocateAsync(SystemId systemId, FieldId fieldId, int index, CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Settings.Domain/Accounts/CreateLinkTokenCommandHandler.cs b/shared/Interfold.Settings.Domain/Accounts/CreateLinkTokenCommandHandler.cs new file mode 100644 index 00000000..851b40c6 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Accounts/CreateLinkTokenCommandHandler.cs @@ -0,0 +1,26 @@ +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Accounts; + +public sealed record CreateLinkTokenCommandResult(LinkToken Token); + +public sealed class CreateLinkTokenCommandHandler : ICommandHandler +{ + private readonly IAccountRepository _accountRepository; + + public CreateLinkTokenCommandHandler(IAccountRepository accountRepository) + { + _accountRepository = accountRepository; + } + + public async Task> HandleAsync(CommandEnvelope command, CancellationToken cancellationToken = default) + { + var token = await _accountRepository.GetOrCreateLinkTokenAsync(command.PrincipalId, cancellationToken); + return CommandExecutionResult.Success(new CreateLinkTokenCommandResult(token)); + } +} diff --git a/shared/Interfold.Settings.Domain/Accounts/UpdateUsernameCommandHandler.cs b/shared/Interfold.Settings.Domain/Accounts/UpdateUsernameCommandHandler.cs new file mode 100644 index 00000000..94e3aa41 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Accounts/UpdateUsernameCommandHandler.cs @@ -0,0 +1,60 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Settings; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Accounts; + +public sealed class UpdateUsernameCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateUsernameCommandHandler( + IAccountRepository accountRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus + ) +:base(idempotencyStore) { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.AccountUsernameUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (RejectIfBlank(command, command.Payload.Username, EntityRefs.AccountUsernameInvalid) is { } blankReject) + return blankReject; + + if (command.Payload.Username.Value.Length > 64) + { + return RejectInvariant(command, EntityRefs.AccountUsernameTooLong); + } + + var persisted = await _accountRepository.UpdateUsernameAsync( + command.PrincipalId, + command.Payload.Username, + cancellationToken + ); + + if (!persisted) + { + return RejectInvariant(command, EntityRefs.AccountUsernameUpdateFailed); + } + + var result = new AccountCommandResult(command.PrincipalId, command.Payload.Username, Replay: false); + + await _eventBus.PublishProfileUpdatedAsync(command.PrincipalId, includeUsername: true, cancellationToken); + return CommandExecutionResult.Success(result); + } + +} diff --git a/shared/Interfold.Settings.Domain/AlterFieldDefinitionsAdapter.cs b/shared/Interfold.Settings.Domain/AlterFieldDefinitionsAdapter.cs new file mode 100644 index 00000000..d63f9533 --- /dev/null +++ b/shared/Interfold.Settings.Domain/AlterFieldDefinitionsAdapter.cs @@ -0,0 +1,48 @@ +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Shared.Domain.Observability; +using Microsoft.Extensions.Logging; + +namespace Interfold.Settings.Domain; + +public sealed class AlterFieldDefinitionsAdapter : IAlterFieldDefinitions +{ + private readonly ISettingsFieldRepository _settingsFields; + private readonly ILogger _logger; + + public AlterFieldDefinitionsAdapter( + ISettingsFieldRepository settingsFields, + ILogger logger) + { + _settingsFields = settingsFields; + _logger = logger; + } + + public async Task> ListVisibleAsync( + SystemId systemId, + FriendshipLevel? friendshipLevel, + CancellationToken cancellationToken = default) + { + try + { + var definitions = await _settingsFields.ListAsync(systemId, cancellationToken).ConfigureAwait(false); + return definitions + .Where(def => def.SecurityLevel.CanBeViewedBy(friendshipLevel)) + .ToArray(); + } + catch (Exception ex) + { + GuardedMetrics.ErrorsTotal.Add(1, + new KeyValuePair("entity_type", "field"), + new KeyValuePair("exception_type", ex.GetType().Name)); + _logger.LogError(ex, + "Guarded field-definition load failed: system={SystemId}, friendship_level={FriendshipLevel}", + systemId.Value, friendshipLevel); + throw; + } + } +} diff --git a/shared/Interfold.Settings.Domain/EncryptionKey.cs b/shared/Interfold.Settings.Domain/EncryptionKey.cs new file mode 100644 index 00000000..e12136f6 --- /dev/null +++ b/shared/Interfold.Settings.Domain/EncryptionKey.cs @@ -0,0 +1,49 @@ +using System.Security.Cryptography; +using System.Text; +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; +using Konscious.Security.Cryptography; + +namespace Interfold.Settings.Domain; + +public class EncryptionKey +{ + /// + /// Derive the per-system encryption key from the caller's recovery code, the system's + /// KDF salt, and the deployment-wide pepper. + /// + /// + /// Wrapper types are used end-to-end so callers never have to unwrap to raw + /// string. The pepper stays raw because no wrapper exists for cryptographic + /// deployment secrets today. + /// + /// + public static EncryptionKeyMaterial DeriveKey(string pepper, SystemId systemId, RecoveryCode recoveryCode, EncryptionSalt salt) + { + // Step 1: SHA256(pepper + user_id + recovery_code) + var hashInput = Encoding.UTF8.GetBytes(pepper + systemId.Value + recoveryCode.Value); + var sha256Hash = SHA256.HashData(hashInput); + + // Step 2: Argon2id(sha256_hash, salt, t_cost=12, m_cost=65536, parallelism=1, hash_len=32) + var saltBytes = Convert.FromBase64String(salt.Value); + using var argon2 = new Argon2id(sha256Hash); + argon2.Salt = saltBytes; + argon2.DegreeOfParallelism = 1; + argon2.MemorySize = 65536; // KB + argon2.Iterations = 12; + + var keyBytes = argon2.GetBytes(32); + return new(Convert.ToBase64String(keyBytes)); + } + + /// + /// Compute the 9-char base64 checksum of a derived encryption key. Persisted to + /// encryption_state.key_checksum so RecoverEncryption can verify a + /// recovery-code guess without ever touching the underlying key material. + /// + public static KeyChecksum DeriveChecksum(EncryptionKeyMaterial key) + { + var hash = SHA256.HashData(Encoding.UTF8.GetBytes(key.Value)); + return new(Convert.ToBase64String(hash)[..9]); + } +} diff --git a/shared/Interfold.Settings.Domain/Interfold.Settings.Domain.csproj b/shared/Interfold.Settings.Domain/Interfold.Settings.Domain.csproj new file mode 100644 index 00000000..2ab7bb00 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Interfold.Settings.Domain.csproj @@ -0,0 +1,39 @@ + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + diff --git a/shared/Interfold.Settings.Domain/Settings/AddPushTokenCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/AddPushTokenCommandHandler.cs new file mode 100644 index 00000000..e95bdcb4 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/AddPushTokenCommandHandler.cs @@ -0,0 +1,41 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class AddPushTokenCommandHandler : IdempotentCommandHandler +{ + private readonly INotificationTokenRepository _repository; + + public AddPushTokenCommandHandler( + INotificationTokenRepository repository, + IIdempotencyStore idempotencyStore) +:base(idempotencyStore) { + _repository = repository; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsPushTokenAdd; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Token.Value, EntityRefs.SettingsPushTokenInvalid) is { } blankReject) + return blankReject; + + return await SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _repository.AddAsync(command.PrincipalId, new(command.Payload.Token.Value.Trim()), ct), + EntityRefs.SettingsPushTokenAddFailed, + SettingsAction.PushTokenAdded, + cancellationToken: cancellationToken); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/CreateFieldCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/CreateFieldCommandHandler.cs new file mode 100644 index 00000000..3ffc1e34 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/CreateFieldCommandHandler.cs @@ -0,0 +1,55 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class CreateFieldCommandHandler : IdempotentCommandHandler +{ + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IClusterEventBus _eventBus; + + public CreateFieldCommandHandler(ISettingsFieldRepository fieldRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) +:base(idempotencyStore) { + _fieldRepository = fieldRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsFieldCreate; + +protected override async Task> ExecuteCoreAsync (CommandEnvelope command, CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Name, EntityRefs.SettingsFieldNameRequired) is { } blankReject) + return blankReject; + + // Stamp the row with the envelope's OccurredAt rather than honouring whatever + // InsertedAtUtc the caller put on the payload. The caller (SettingsController) sends + // `default(DateTime)` precisely so the idempotency hash stays stable across retries + // - see SettingsController.CreateField. The SP import bypasses this handler entirely + // and sets InsertedAtUtc itself from the decoded ObjectId, so it isn't affected here. + // OccurredAt is nullable on the envelope; fall back to UtcNow if missing. + var insertedAtUtc = (command.OccurredAt ?? DateTimeOffset.UtcNow).UtcDateTime; + + return await SettingsFieldCommandFlow.ExecuteCreateAsync( + command, + SettingsFieldAction.FieldCreated, + EntityRefs.SettingsFieldCreateFailed, + ct => _fieldRepository.CreateAsync( + command.PrincipalId, + command.Payload.Name, + command.Payload.Type, + command.Payload.SecurityLevel, + command.Payload.Locked, + insertedAtUtc, + ct), + ct => _eventBus.PublishAsync(new SettingsFieldsChangedEvent(command.PrincipalId), ct), + cancellationToken); + } +} \ No newline at end of file diff --git a/shared/Interfold.Settings.Domain/Settings/DeleteAccountCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/DeleteAccountCommandHandler.cs new file mode 100644 index 00000000..c3bcd5a3 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/DeleteAccountCommandHandler.cs @@ -0,0 +1,137 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Contracts.Events; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Journals.Domain.Abstractions.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class DeleteAccountCommandHandler : IdempotentCommandHandler +{ + private readonly IClusterEventBus _eventBus; + private readonly IAccountRepository _accountRepository; + private readonly IAlterRepository _alterRepository; + private readonly ITagRepository _tagRepository; + private readonly IPollRepository _pollRepository; + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IJournalRepository _journalRepository; + private readonly IFrontingRepository _frontingRepository; + private readonly IFriendshipRepository _friendshipRepository; + + public DeleteAccountCommandHandler( + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + IAccountRepository accountRepository, + IAlterRepository alterRepository, + ITagRepository tagRepository, + IPollRepository pollRepository, + ISettingsFieldRepository fieldRepository, + IJournalRepository journalRepository, + IFrontingRepository frontingRepository, + IFriendshipRepository friendshipRepository) + : base(idempotencyStore) + { + _eventBus = eventBus; + _accountRepository = accountRepository; + _alterRepository = alterRepository; + _tagRepository = tagRepository; + _pollRepository = pollRepository; + _fieldRepository = fieldRepository; + _journalRepository = journalRepository; + _frontingRepository = frontingRepository; + _friendshipRepository = friendshipRepository; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsAccountDelete; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + { + // Shared between the mutation and publish lambdas — the friendship repo returns the + // set of counterparties whose Friendship rows we tore down, and each one needs a + // FriendshipRemovedEvent on the cluster bus so their sockets refresh their + // friend-list. IdempotentCommandHandler skips ExecuteCoreAsync on replay, so this + // list is only ever populated once per idempotency key — same guard the previous + // ExecuteAndPublishAsync gave us via `Result.Replay: false`. + var unfriendedIds = new List(); + + return SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + async ct => + { + var systemId = command.PrincipalId; + + var alters = await _alterRepository.ListAsync(systemId, ct); + foreach (var alter in alters) + { + await _alterRepository.DeleteAsync(systemId, alter.Id, ct); + //TODO: Delete alter image if it exists + } + + var tags = await _tagRepository.ListAsync(systemId, ct); + foreach (var tag in tags) + { + await _tagRepository.DeleteAsync(systemId, tag.Id, ct); + } + + var polls = await _pollRepository.ListAsync(systemId, ct); + foreach (var poll in polls) + { + await _pollRepository.DeleteAsync(systemId, poll.Id, ct); + } + + var fields = await _fieldRepository.ListAsync(systemId, ct); + foreach (var field in fields) + { + await _fieldRepository.DeleteAsync(systemId, field.Id, ct); + } + + var globalEntries = await _journalRepository.ListGlobalAsync(systemId, ct); + foreach (var entry in globalEntries) + { + await _journalRepository.DeleteGlobalAsync(systemId, entry.Id, ct); + } + + // Fronting records are tied to alters so no need to delete them here + + var deletedIds = await _friendshipRepository.DeleteAllForSystemAsync(systemId, ct); + unfriendedIds.AddRange(deletedIds); + + return await _accountRepository.DeleteAsync(systemId, ct); + + //TODO: Delete account image if it exists + }, + SettingsAction.AccountDeleted.ToFailedEntityRef(), + SettingsAction.AccountDeleted, + async ct => + { + await _eventBus.PublishAsync(new SettingsAccountDeletedSignalEvent(command.PrincipalId), ct); + + foreach (var friendId in unfriendedIds) + { + // The friendship repos return bare (region-stripped) ids in their DeleteAll + // results — compose with the principal's region so the socket router filter + // sees a scoped TargetSystemId. Cross-region friendships would want the + // friend's own region here; a same-region compose is the current behaviour. + var friendTarget = ScopedSystemId.Compose( + command.PrincipalId.Region, + friendId); + await _eventBus.PublishAsync(new FriendshipRemovedEvent(friendTarget, command.PrincipalId), ct); + } + }, + cancellationToken); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/DeleteAvatarCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/DeleteAvatarCommandHandler.cs new file mode 100644 index 00000000..60d98381 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/DeleteAvatarCommandHandler.cs @@ -0,0 +1,36 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class DeleteAvatarCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteAvatarCommandHandler(IAccountRepository accountRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsAvatarDelete; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.ClearAvatarAsync(command.PrincipalId, ct), + SettingsAction.AvatarDeleted.ToFailedEntityRef(), + SettingsAction.AvatarDeleted, + ct => _eventBus.PublishProfileUpdatedAsync(command.PrincipalId, includeUsername: false, ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/DeleteFieldCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/DeleteFieldCommandHandler.cs new file mode 100644 index 00000000..73bcd70c --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/DeleteFieldCommandHandler.cs @@ -0,0 +1,37 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class DeleteFieldCommandHandler : IdempotentCommandHandler +{ + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteFieldCommandHandler(ISettingsFieldRepository fieldRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _fieldRepository = fieldRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsFieldDelete; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _fieldRepository.DeleteAsync(command.PrincipalId, command.Payload.FieldId, ct), + SettingsAction.FieldDeleted.ToFailedEntityRef(), + SettingsAction.FieldDeleted, + ct => _eventBus.PublishAsync(new SettingsFieldsChangedEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/ImportPkCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/ImportPkCommandHandler.cs new file mode 100644 index 00000000..1880f5d6 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/ImportPkCommandHandler.cs @@ -0,0 +1,63 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +/// Async dispatcher for PK imports — symmetrical to +/// . The real PK importer is still a stub, but the +/// dispatch shape mirrors SP so the real importer inherits the mutex + lifecycle for free. +public sealed class ImportPkCommandHandler : ICommandHandler +{ + private readonly IImportOperationRepository _operations; + private readonly IImportJobQueue _queue; + + public ImportPkCommandHandler(IImportOperationRepository operations, IImportJobQueue queue) + { + _operations = operations; + _queue = queue; + } + + public async Task> HandleAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (CommandHandler.RejectIfBlank(command.OperationId, command.Payload.Token.Value, EntityRefs.SettingsImportPkInvalid) is { } blankReject) + return blankReject; + + var claim = await _operations.TryClaimAsync( + command.PrincipalId, + ImportOperationKind.PluralKit, + command.IdempotencyKey, + cancellationToken).ConfigureAwait(false); + + if (claim.IsNew) + { + await _queue.EnqueueAsync( + new ImportJobItem( + claim.OperationId, + command.PrincipalId, + ImportOperationKind.PluralKit, + command.Payload.Token, + RecoveryCode: null), + cancellationToken).ConfigureAwait(false); + } + + var result = new ImportDispatchCommandResult( + command.PrincipalId, + claim.OperationId, + ImportOperationKind.PluralKit, + Status: claim.IsNew + ? ImportOperationDispatchStatus.Queued + : ImportOperationDispatchStatus.Running, + StartedAt: DateTimeOffset.UtcNow, + Replay: false); + + return CommandExecutionResult.Success(result); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/ImportSpCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/ImportSpCommandHandler.cs new file mode 100644 index 00000000..0b3d6b83 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/ImportSpCommandHandler.cs @@ -0,0 +1,66 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +/// Async dispatcher for Simply Plural imports. Claims a per-system slot via LWT +/// and enqueues the actual work for ImportJobBackgroundService; the controller +/// returns HTTP 202. Every dispatch — original or retried — collapses onto the same +/// in-flight operation via active_import_by_system. +public sealed class ImportSpCommandHandler : ICommandHandler +{ + private readonly IImportOperationRepository _operations; + private readonly IImportJobQueue _queue; + + public ImportSpCommandHandler(IImportOperationRepository operations, IImportJobQueue queue) + { + _operations = operations; + _queue = queue; + } + + public async Task> HandleAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (CommandHandler.RejectIfBlank(command.OperationId, command.Payload.Token.Value, EntityRefs.SettingsImportSpInvalid) is { } blankReject) + return blankReject; + + var claim = await _operations.TryClaimAsync( + command.PrincipalId, + ImportOperationKind.SimplyPlural, + command.IdempotencyKey, + cancellationToken).ConfigureAwait(false); + + if (claim.IsNew) + { + await _queue.EnqueueAsync( + new ImportJobItem( + claim.OperationId, + command.PrincipalId, + ImportOperationKind.SimplyPlural, + command.Payload.Token, + command.Payload.RecoveryCode), + cancellationToken).ConfigureAwait(false); + } + + // Collapsed dispatch returns Running + the existing operation_id so client logs + // stay coherent. Replay is false — the WebSocket completion frame is authoritative. + var result = new ImportDispatchCommandResult( + command.PrincipalId, + claim.OperationId, + ImportOperationKind.SimplyPlural, + Status: claim.IsNew + ? ImportOperationDispatchStatus.Queued + : ImportOperationDispatchStatus.Running, + StartedAt: DateTimeOffset.UtcNow, + Replay: false); + + return CommandExecutionResult.Success(result); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/RecoverEncryptionCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/RecoverEncryptionCommandHandler.cs new file mode 100644 index 00000000..30a5f852 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/RecoverEncryptionCommandHandler.cs @@ -0,0 +1,57 @@ +using Interfold.Auth.Contracts.Configuration; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class RecoverEncryptionCommandHandler : IdempotentCommandHandler +{ + private readonly IEncryptionStateRepository _repository; + private readonly IOptionsMonitor _authOptions; + + public RecoverEncryptionCommandHandler( + IEncryptionStateRepository repository, + IIdempotencyStore idempotencyStore, + IOptionsMonitor authOptions) +:base(idempotencyStore) { + _repository = repository; + _authOptions = authOptions; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsEncryptionRecover; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.RecoveryCode.Value, EntityRefs.SettingsRecoveryCodeInvalid) is { } blankReject) + return blankReject; + + var state = await _repository.GetAsync(command.PrincipalId, cancellationToken); + if (state is not { Initialized: true, KeyChecksum: { } keyChecksum, Salt: { } salt } + || string.IsNullOrWhiteSpace(keyChecksum.Value) || string.IsNullOrWhiteSpace(salt.Value)) + return RejectInvariant(command, EntityRefs.SettingsEncryptionNotInitialized); + + var pepper = _authOptions.CurrentValue.EncryptionPepper; + // `checksum == keyChecksum` uses the record-struct value equality (ordinal string + // equality on the underlying .Value), matching `string.Equals(..., Ordinal)` exactly. + var key = EncryptionKey.DeriveKey(pepper, command.PrincipalId, command.Payload.RecoveryCode, salt); + var checksum = EncryptionKey.DeriveChecksum(key); + + if (checksum != keyChecksum) + return RejectInvariant(command, EntityRefs.SettingsInvalidRecoveryCode); + + var result = new EncryptionCommandResult(command.PrincipalId, EncryptionAction.EncryptionRecovered, key, Replay: false); + + return CommandExecutionResult.Success(result); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/RelocateFieldCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/RelocateFieldCommandHandler.cs new file mode 100644 index 00000000..c39abfe2 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/RelocateFieldCommandHandler.cs @@ -0,0 +1,37 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class RelocateFieldCommandHandler : IdempotentCommandHandler +{ + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IClusterEventBus _eventBus; + + public RelocateFieldCommandHandler(ISettingsFieldRepository fieldRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _fieldRepository = fieldRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsFieldRelocate; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _fieldRepository.RelocateAsync(command.PrincipalId, command.Payload.FieldId, command.Payload.Index, ct), + SettingsAction.FieldRelocated.ToFailedEntityRef(), + SettingsAction.FieldRelocated, + ct => _eventBus.PublishAsync(new SettingsFieldsChangedEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/RemovePushTokenCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/RemovePushTokenCommandHandler.cs new file mode 100644 index 00000000..b40be7de --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/RemovePushTokenCommandHandler.cs @@ -0,0 +1,41 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class RemovePushTokenCommandHandler : IdempotentCommandHandler +{ + private readonly INotificationTokenRepository _repository; + + public RemovePushTokenCommandHandler( + INotificationTokenRepository repository, + IIdempotencyStore idempotencyStore) +:base(idempotencyStore) { + _repository = repository; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsPushTokenRemove; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.Token.Value, EntityRefs.SettingsPushTokenInvalid) is { } blankReject) + return blankReject; + + // Removal is treated as idempotent for retry safety. + return await SettingsIdempotentCommandFlow.ExecuteApplyAsync( + command, + ct => _repository.RemoveAsync(new(command.Payload.Token.Value.Trim()), ct), + SettingsAction.PushTokenRemoved, + cancellationToken: cancellationToken); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/ResetEncryptionCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/ResetEncryptionCommandHandler.cs new file mode 100644 index 00000000..cf765b87 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/ResetEncryptionCommandHandler.cs @@ -0,0 +1,45 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class ResetEncryptionCommandHandler : IdempotentCommandHandler +{ + private readonly IEncryptionStateRepository _repository; + private readonly IClusterEventBus _eventBus; + + public ResetEncryptionCommandHandler( + IEncryptionStateRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsEncryptionReset; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + + var persisted = await _repository.UpsertAsync(command.PrincipalId, false, null, null, cancellationToken); + if (!persisted) + return RejectInvariant(command, EntityRefs.SettingsEncryptionResetFailed); + + var result = new SettingsCommandResult(command.PrincipalId, SettingsAction.EncryptionReset, Replay: false); + + await _eventBus.PublishAsync(new SettingsEncryptedDataWipedSignalEvent(command.PrincipalId), cancellationToken); + return CommandExecutionResult.Success(result); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/SettingsEventBusExtensions.cs b/shared/Interfold.Settings.Domain/Settings/SettingsEventBusExtensions.cs new file mode 100644 index 00000000..8c8852cd --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/SettingsEventBusExtensions.cs @@ -0,0 +1,19 @@ +using Interfold.Settings.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +// Intent-named publish helpers on IClusterEventBus for settings events. Publish-only — +// orchestration lives in SettingsIdempotentCommandFlow. +internal static class SettingsEventBusExtensions +{ + /// includeUsername → EmitUsernameUpdated on the event → socket handlers fan + /// a username_updated frame. + public static ValueTask PublishProfileUpdatedAsync( + this IClusterEventBus eventBus, + ScopedSystemId principalId, + bool includeUsername, + CancellationToken cancellationToken = default) + => eventBus.PublishAsync(new SettingsProfileUpdatedEvent(principalId, includeUsername), cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/SettingsFieldCommandFlow.cs b/shared/Interfold.Settings.Domain/Settings/SettingsFieldCommandFlow.cs new file mode 100644 index 00000000..37c75c7a --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/SettingsFieldCommandFlow.cs @@ -0,0 +1,38 @@ +using Interfold.Settings.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +/// Field-creation helper for handlers returning +/// (carries the created ); +/// contrasts with which returns a plain +/// . +internal static class SettingsFieldCommandFlow +{ + public static CommandExecutionResult Success( + ScopedSystemId systemId, + SettingsFieldAction action, + FieldId fieldId) + => CommandExecutionResult.Success( + new SettingsFieldCommandResult(systemId, action, fieldId, Replay: false)); + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + SettingsFieldAction action, + EntityRef createFailedEntityRef, + Func> createAsync, + Func publishAccepted, + CancellationToken cancellationToken = default) + { + var fieldId = await createAsync(cancellationToken); + if (fieldId is null) + return CommandHandler.RejectInvariant(command.OperationId, createFailedEntityRef); + + await publishAccepted(cancellationToken); + return Success(command.PrincipalId, action, fieldId.Value); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/SettingsIdempotentCommandFlow.cs b/shared/Interfold.Settings.Domain/Settings/SettingsIdempotentCommandFlow.cs new file mode 100644 index 00000000..ecb51bca --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/SettingsIdempotentCommandFlow.cs @@ -0,0 +1,60 @@ +using Interfold.Settings.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +/// Mutate → check → publish → success helper for settings handlers that already +/// inherit . Companion to +/// (which returns a different result shape). +internal static class SettingsIdempotentCommandFlow +{ + public static CommandExecutionResult Success( + ScopedSystemId systemId, + SettingsAction action) + => CommandExecutionResult.Success(new SettingsCommandResult(systemId, action, Replay: false)); + + public static CommandExecutionResult? RejectIfMutationFailed( + CommandEnvelope command, + bool succeeded, + EntityRef failedEntityRef) + => !succeeded + ? CommandHandler.RejectInvariant(command.OperationId, failedEntityRef) + : null; + + public static async Task> ExecuteMutationAsync( + CommandEnvelope command, + Func> mutateAsync, + EntityRef failedEntityRef, + SettingsAction action, + Func? publishAccepted = null, + CancellationToken cancellationToken = default) + { + var succeeded = await mutateAsync(cancellationToken); + if (RejectIfMutationFailed(command, succeeded, failedEntityRef) is { } reject) + return reject; + + if (publishAccepted is not null) + await publishAccepted(cancellationToken); + + return Success(command.PrincipalId, action); + } + + public static async Task> ExecuteApplyAsync( + CommandEnvelope command, + Func applyAsync, + SettingsAction action, + Func? publishAccepted = null, + CancellationToken cancellationToken = default) + { + await applyAsync(cancellationToken); + + if (publishAccepted is not null) + await publishAccepted(cancellationToken); + + return Success(command.PrincipalId, action); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/SetupEncryptionCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/SetupEncryptionCommandHandler.cs new file mode 100644 index 00000000..cc1819ed --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/SetupEncryptionCommandHandler.cs @@ -0,0 +1,62 @@ +using Interfold.Auth.Contracts.Configuration; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Microsoft.Extensions.Options; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class SetupEncryptionCommandHandler : IdempotentCommandHandler +{ + private readonly IEncryptionStateRepository _repository; + private readonly IClusterEventBus _eventBus; + private readonly IOptionsMonitor _authOptions; + + public SetupEncryptionCommandHandler( + IEncryptionStateRepository repository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + IOptionsMonitor authOptions) +:base(idempotencyStore) { + _repository = repository; + _eventBus = eventBus; + _authOptions = authOptions; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsEncryptionSetup; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (RejectIfBlank(command, command.Payload.RecoveryCode.Value, EntityRefs.SettingsRecoveryCodeInvalid) is { } blankReject) + return blankReject; + + var existing = await _repository.GetAsync(command.PrincipalId, cancellationToken); + if (existing?.Salt is not { } salt || string.IsNullOrWhiteSpace(salt.Value)) + { + throw new InterfoldException("Salt must be provided.", ErrorCodes.EncryptionSaltRequired); + } + + var pepper = _authOptions.CurrentValue.EncryptionPepper; + var key = EncryptionKey.DeriveKey(pepper, command.PrincipalId, command.Payload.RecoveryCode, salt); + var checksum = EncryptionKey.DeriveChecksum(key); + + var persisted = await _repository.UpsertAsync(command.PrincipalId, true, checksum, null, cancellationToken); + if (!persisted) + return RejectInvariant(command, EntityRefs.SettingsEncryptionSetupFailed); + + var result = new EncryptionCommandResult(command.PrincipalId, EncryptionAction.EncryptionSetup, key, Replay: false); + + await _eventBus.PublishProfileUpdatedAsync(command.PrincipalId, includeUsername: false, cancellationToken); + return CommandExecutionResult.Success(result); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/UnlinkAppleCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UnlinkAppleCommandHandler.cs new file mode 100644 index 00000000..e3e4aab3 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UnlinkAppleCommandHandler.cs @@ -0,0 +1,37 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UnlinkAppleCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UnlinkAppleCommandHandler(IAccountRepository accountRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsUnlinkApple; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.UnlinkAppleAsync(command.PrincipalId, ct), + SettingsAction.AppleUnlinked.ToFailedEntityRef(), + SettingsAction.AppleUnlinked, + ct => _eventBus.PublishAsync(new SettingsAppleAccountUnlinkedSignalEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/UnlinkDiscordCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UnlinkDiscordCommandHandler.cs new file mode 100644 index 00000000..75cac0e8 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UnlinkDiscordCommandHandler.cs @@ -0,0 +1,40 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UnlinkDiscordCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UnlinkDiscordCommandHandler( + IAccountRepository accountRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) + : base(idempotencyStore) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsUnlinkDiscord; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.UnlinkDiscordAsync(command.PrincipalId, ct), + SettingsAction.DiscordUnlinked.ToFailedEntityRef(), + SettingsAction.DiscordUnlinked, + ct => _eventBus.PublishAsync(new SettingsDiscordAccountUnlinkedSignalEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/UnlinkEmailCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UnlinkEmailCommandHandler.cs new file mode 100644 index 00000000..519f4297 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UnlinkEmailCommandHandler.cs @@ -0,0 +1,43 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UnlinkEmailCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UnlinkEmailCommandHandler( + IAccountRepository accountRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) + : base(idempotencyStore) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsUnlinkEmail; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.UnlinkEmailAsync(command.PrincipalId, ct), + SettingsAction.EmailUnlinked.ToFailedEntityRef(), + SettingsAction.EmailUnlinked, + // Legacy Octocon.Accounts.unlink_email_from_user broadcast google_account_unlinked + // because the only email auth path in the old stack was the Google OAuth one; + // mirror that contract so existing clients keep receiving the same socket signal. + ct => _eventBus.PublishAsync(new SettingsGoogleAccountUnlinkedSignalEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/UpdateDescriptionCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UpdateDescriptionCommandHandler.cs new file mode 100644 index 00000000..df519139 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UpdateDescriptionCommandHandler.cs @@ -0,0 +1,48 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UpdateDescriptionCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateDescriptionCommandHandler( + IAccountRepository accountRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsDescriptionUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + if (command.Payload.Description.Length > 3000) + return RejectInvariant(command, EntityRefs.SettingsDescriptionInvalid); + + return await SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.UpdateDescriptionAsync( + command.PrincipalId, + command.Payload.Description, + ct), + EntityRefs.SettingsDescriptionUpdateFailed, + SettingsAction.DescriptionUpdated, + ct => _eventBus.PublishProfileUpdatedAsync(command.PrincipalId, includeUsername: false, ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Settings.Domain/Settings/UpdateFieldCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UpdateFieldCommandHandler.cs new file mode 100644 index 00000000..a9d4265e --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UpdateFieldCommandHandler.cs @@ -0,0 +1,37 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UpdateFieldCommandHandler : IdempotentCommandHandler +{ + private readonly ISettingsFieldRepository _fieldRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateFieldCommandHandler(ISettingsFieldRepository fieldRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _fieldRepository = fieldRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsFieldUpdate; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _fieldRepository.UpdateAsync(command.PrincipalId, command.Payload.FieldId, command.Payload.Name, command.Payload.SecurityLevel, command.Payload.Locked, ct), + SettingsAction.FieldUpdated.ToFailedEntityRef(), + SettingsAction.FieldUpdated, + ct => _eventBus.PublishAsync(new SettingsFieldsChangedEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/UploadAvatarCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/UploadAvatarCommandHandler.cs new file mode 100644 index 00000000..502a9196 --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/UploadAvatarCommandHandler.cs @@ -0,0 +1,41 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class UploadAvatarCommandHandler : IdempotentCommandHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly IClusterEventBus _eventBus; + + public UploadAvatarCommandHandler(IAccountRepository accountRepository, IIdempotencyStore idempotencyStore, IClusterEventBus eventBus) + : base(idempotencyStore) + { + _accountRepository = accountRepository; + _eventBus = eventBus; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsAvatarUpload; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + { + if (RejectIfBlank(command, command.Payload.AvatarUrl, EntityRefs.SettingsAvatarInvalid) is { } blankReject) + return Task.FromResult(blankReject); + + return SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + ct => _accountRepository.UpdateAvatarAsync(command.PrincipalId, command.Payload.AvatarUrl, command.Payload.Source, ct), + SettingsAction.AvatarUploaded.ToFailedEntityRef(), + SettingsAction.AvatarUploaded, + ct => _eventBus.PublishProfileUpdatedAsync(command.PrincipalId, includeUsername: false, ct), + cancellationToken); + } +} diff --git a/shared/Interfold.Settings.Domain/Settings/WipeAltersCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/WipeAltersCommandHandler.cs new file mode 100644 index 00000000..d86dcaac --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/WipeAltersCommandHandler.cs @@ -0,0 +1,51 @@ +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class WipeAltersCommandHandler : IdempotentCommandHandler +{ + private readonly IClusterEventBus _eventBus; + private readonly IAlterRepository _alterRepository; + + public WipeAltersCommandHandler( + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + IAlterRepository alterRepository) + : base(idempotencyStore) + { + _eventBus = eventBus; + _alterRepository = alterRepository; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsAltersWipe; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + async ct => + { + var systemId = command.PrincipalId; + var alters = await _alterRepository.ListAsync(systemId, ct); + foreach (var alter in alters) + { + await _alterRepository.DeleteAsync(systemId, alter.Id, ct); + //TODO: Delete alter image if it exists + } + + return true; + }, + SettingsAction.AltersWiped.ToFailedEntityRef(), + SettingsAction.AltersWiped, + ct => _eventBus.PublishAsync(new SettingsAltersWipedSignalEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Settings.Domain/Settings/WipeTagsCommandHandler.cs b/shared/Interfold.Settings.Domain/Settings/WipeTagsCommandHandler.cs new file mode 100644 index 00000000..87af4a3b --- /dev/null +++ b/shared/Interfold.Settings.Domain/Settings/WipeTagsCommandHandler.cs @@ -0,0 +1,53 @@ +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Settings.Domain.Settings; + +public sealed class WipeTagsCommandHandler : IdempotentCommandHandler +{ + private readonly IClusterEventBus _eventBus; + private readonly ITagRepository _tagRepository; + + public WipeTagsCommandHandler( + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus, + ITagRepository tagRepository) + : base(idempotencyStore) + { + _eventBus = eventBus; + _tagRepository = tagRepository; + } + + protected override EntityRef DuplicateEntityRef => EntityRefs.SettingsTagsWipe; + + protected override Task> ExecuteCoreAsync( + CommandEnvelope command, + CancellationToken cancellationToken) + => SettingsIdempotentCommandFlow.ExecuteMutationAsync( + command, + async ct => + { + var systemId = command.PrincipalId; + var tags = await _tagRepository.ListAsync(systemId, ct); + foreach (var tag in tags) + { + // Repository delete also removes alter_tag join rows in both backends, mirroring + // Octocon.Accounts.wipe_tags/1 in the legacy stack which truncated both Tag and + // AlterTag tables for the system. + await _tagRepository.DeleteAsync(systemId, tag.Id, ct); + } + + return true; + }, + SettingsAction.TagsWiped.ToFailedEntityRef(), + SettingsAction.TagsWiped, + ct => _eventBus.PublishAsync(new SettingsTagsWipedSignalEvent(command.PrincipalId), ct), + cancellationToken); +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/AppHostParameterKeys.cs b/shared/Interfold.Shared.Contracts/Configuration/AppHostParameterKeys.cs new file mode 100644 index 00000000..453bb3f7 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/AppHostParameterKeys.cs @@ -0,0 +1,90 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// +/// The IConfiguration keys the bootstrapper's PublishPhase injects and +/// InterfoldAppHost.Configure reads back. Centralising them keeps the writer and +/// reader from drifting (a typo used to surface as a silently-defaulted parameter at +/// compose-publish time). Key spellings are internal to the publish pipeline but must +/// match Aspire's AddParameter(name) calls, so treat them as frozen. +/// +public static class AppHostParameterKeys +{ + /// + /// The section prefix every Parameters:* key carries. Aspire's + /// AddParameter(name) takes the bare name (no prefix), so + /// strips this to derive the one from the other. + /// + public const string ParametersPrefix = "Parameters:"; + + /// + /// Derives the Aspire AddParameter name from a Parameters:* configuration + /// key, so the parameter declaration and the configuration read can share a single + /// constant instead of two spellings that can drift. + /// + public static string ToParameterName(string key) + => key.StartsWith(ParametersPrefix, StringComparison.Ordinal) + ? key[ParametersPrefix.Length..] + : throw new ArgumentException( + $"'{key}' is not a '{ParametersPrefix}' configuration key.", nameof(key)); + + // --- Toggles / topology --- + public const string IncludeApi = "Parameters:include-api"; + public const string IncludePostgres = "Parameters:include-postgres"; + public const string IncludeScylla = "Parameters:include-scylla"; + public const string IncludeCassandra = "Parameters:include-cassandra"; + public const string IncludeDashboard = "Parameters:include-dashboard"; + public const string IncludeWeb = "Parameters:include-web"; + public const string PersistentContainers = "Parameters:persistent-containers"; + public const string ScyllaTopology = "Parameters:scylla-topology"; + public const string ClusterName = "Parameters:cluster-name"; + public const string WebTls = "Parameters:web-tls"; + public const string WebServerName = "Parameters:web-server-name"; + public const string ApiImage = "Parameters:api-image"; + + // Bench-mode marker consumed by InterfoldAppHost + TestBenchCoordinator. When on, the + // AppHost forces api/web/dashboard off, pins stable container names, and emits a + // machine-readable readiness line so the coordinator's launcher can exit deterministically. + public const string TestBenchMode = "Parameters:test-bench-mode"; + + // --- Credentials / secrets --- + public const string PostgresUser = "Parameters:postgres-user"; + public const string PostgresPassword = "Parameters:postgres-password"; + public const string PostgresInitPassword = "Parameters:postgres-init-password"; + public const string PostgresDb = "Parameters:postgres-db"; + public const string ScyllaUser = "Parameters:scylla-user"; + public const string ScyllaPassword = "Parameters:scylla-password"; + public const string EncryptionPrivateKey = "Parameters:encryption-private-key"; + + // --- OAuth --- + public const string GoogleOAuthClientId = "Parameters:google-oauth-client-id"; + public const string DiscordOAuthClientId = "Parameters:discord-oauth-client-id"; + public const string AppleOAuthClientId = "Parameters:apple-oauth-client-id"; + + // --- API runtime --- + public const string ScyllaKeyspace = "Parameters:scylla-keyspace"; + public const string OAuthCallbackBaseUrl = "Parameters:oauth-callback-base-url"; + public const string JwtAuthority = "Parameters:jwt-authority"; + public const string JwtAudience = "Parameters:jwt-audience"; + public const string CorsAllowedOrigins = "Parameters:cors-allowed-origins"; + public const string NodeGroup = "Parameters:node-group"; + public const string AvatarPublicBase = "Parameters:avatar-public-base"; + public const string OtlpEndpoint = "Parameters:otlp-endpoint"; + public const string SocketBatchBytesThreshold = "Parameters:socket-batch-bytes-threshold"; + public const string DbRetryAttempts = "Parameters:db-retry-attempts"; + public const string DbRetryInitialDelayMs = "Parameters:db-retry-initial-delay-ms"; + public const string DbRetryMaxDelayMs = "Parameters:db-retry-max-delay-ms"; + public const string HydrationMaxConcurrency = "Parameters:hydration-max-concurrency"; + + // --- Host port mappings --- + public const string PortsPostgres = "Ports:postgres"; + public const string PortsScylla = "Ports:scylla"; + public const string PortsCassandra = "Ports:cassandra"; + public const string PortsApiHttp = "Ports:api-http"; + public const string PortsApiHttps = "Ports:api-https"; + public const string PortsWebHttp = "Ports:web-http"; + public const string PortsWebHttps = "Ports:web-https"; + + // --- Inside-the-container Kestrel ports (not host-published; must match the API image) --- + public const string PortsApiContainerHttp = "Ports:api-container-http"; + public const string PortsApiContainerHttps = "Ports:api-container-https"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ClusterConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/ClusterConfiguration.cs new file mode 100644 index 00000000..611100ef --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ClusterConfiguration.cs @@ -0,0 +1,21 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Shared.Contracts.Configuration; + +/// +/// Cluster and deployment configuration for node role and orchestration. +/// Binds from environment variables: +/// - FLY_PROCESS_GROUP (fly.io automatic) +/// - OCTOCON_NODE_GROUP (manual override, takes precedence) +/// +public sealed class ClusterConfiguration +{ + public const string SectionName = "Cluster"; + + /// + /// Resolved node group. Bound from FLY_PROCESS_GROUP or OCTOCON_NODE_GROUP via + /// ; wire values are "primary", + /// "auxiliary", "sidecar". + /// + public NodeGroup NodeGroup { get; set; } = NodeGroup.Auxiliary; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ComposeInfrastructureNames.cs b/shared/Interfold.Shared.Contracts/Configuration/ComposeInfrastructureNames.cs new file mode 100644 index 00000000..109559e0 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ComposeInfrastructureNames.cs @@ -0,0 +1,28 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// +/// Docker Compose network names the AppHost graph emits. Operator-frozen alongside +/// : existing deployments reference them (e.g. +/// docker network inspect runbooks), and renaming would orphan the old networks +/// on update. +/// +public static class ComposeNetworks +{ + public const string Scylla = "scylla"; + public const string Postgres = "postgres"; + public const string Api = "api"; +} + +/// +/// Docker Compose named volumes the AppHost graph emits. Operator-frozen: renaming a +/// volume silently detaches existing data on the next docker compose up. +/// +public static class ComposeVolumes +{ + public const string PostgresData = "msg_pgdata"; + public const string ScyllaData = "scylla_data"; + public const string CassandraData = "cassandra_data"; + + /// Per-region Scylla volume for multi-node topologies (scylla_{region}_data). + public static string ScyllaRegionData(string regionWireValue) => $"scylla_{regionWireValue}_data"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ComposeServices.cs b/shared/Interfold.Shared.Contracts/Configuration/ComposeServices.cs new file mode 100644 index 00000000..b89cb806 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ComposeServices.cs @@ -0,0 +1,73 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Shared.Contracts.Configuration; + +/// Docker Compose service names emitted by InterfoldAppHost. Shared with +/// the bootstrapper's ConfigPhase whitelist; any addition MUST also be added to +/// InterfoldAppHost.Configure. Regional Scylla names mirror +/// 1:1. +public static class ComposeServices +{ + /// Postgres / TimescaleDB service (application state + internal.secrets). + public const string Postgres = "msg-db"; + + /// Single-node Scylla service (used in single DatabaseMode). + public const string ScyllaSingle = "scylla"; + + /// North America seed / node for multi-region Scylla deployments. + public const string ScyllaNam = "scylla-nam"; + + /// Europe node for multi-region Scylla deployments. + public const string ScyllaEur = "scylla-eur"; + + /// South America node for multi-region Scylla deployments. + public const string ScyllaSam = "scylla-sam"; + + /// South Asia node for multi-region Scylla deployments. + public const string ScyllaSas = "scylla-sas"; + + /// East Asia node for multi-region Scylla deployments. + public const string ScyllaEas = "scylla-eas"; + + /// Oceania node for multi-region Scylla deployments. + public const string ScyllaOcn = "scylla-ocn"; + + /// GDPR (EU data residency) node for multi-region Scylla deployments. + public const string ScyllaGdpr = "scylla-gdpr"; + + /// Cassandra service (used in cassandra DatabaseMode as the Scylla fallback). + public const string Cassandra = "cassandra"; + + /// API container. Emits OCTOCON_* env, terminates TLS, hosts the WebSocket endpoint. + public const string InterfoldApi = "interfold-api"; + + /// Static/octocon web tier proxy in front of the API when web-https is enabled. + public const string OctoconWeb = "octocon-web"; + + /// Seven regional Scylla nodes in ScyllaKeyspace declaration order. + public static readonly string[] ScyllaRegionalNodes = + [ + ScyllaNam, ScyllaEur, ScyllaSam, ScyllaSas, + ScyllaEas, ScyllaOcn, ScyllaGdpr, + ]; + + /// All compose services the bootstrapper will accept as an --service filter. + public static readonly string[] AllValidUpdateServices = + [ + Postgres, + ScyllaSingle, + ScyllaNam, ScyllaEur, ScyllaSam, ScyllaSas, + ScyllaEas, ScyllaOcn, ScyllaGdpr, + Cassandra, + InterfoldApi, + OctoconWeb, + ]; + + /// Single naming rule for Scylla node services: multi-node → scylla-{region}, + /// single → scylla. Shared with the AppHost graph and bind-mount renderer. + public static string ToScyllaNodeName(string regionWireValue, bool multiNode) + => multiNode ? $"scylla-{regionWireValue}" : ScyllaSingle; + + public static string ToScyllaNodeName(Enums.ScyllaKeyspace keyspace, bool multiNode) + => ToScyllaNodeName(keyspace.ToWire(), multiNode); +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ContainerMountPaths.cs b/shared/Interfold.Shared.Contracts/Configuration/ContainerMountPaths.cs new file mode 100644 index 00000000..1378ff26 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ContainerMountPaths.cs @@ -0,0 +1,18 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// In-container mount points paired with . Changing +/// any value is a compose-wire change: existing named volumes would silently orphan. +public static class ContainerMountPaths +{ + public const string PostgresData = "/var/lib/postgresql/data"; + + /// PGDATA subdirectory — the official entrypoint isolates state one level deeper. + public const string PostgresPgData = "/var/lib/postgresql/data/pgdata"; + + public const string ScyllaData = "/var/lib/scylla"; + + public const string CassandraData = "/var/lib/cassandra"; + + /// Pre-created in-image with app:app ownership so the volume inherits the owner on first mount. + public const string InterfoldAvatars = "/app/data/avatars"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/CorsOptions.cs b/shared/Interfold.Shared.Contracts/Configuration/CorsOptions.cs new file mode 100644 index 00000000..a89a9258 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/CorsOptions.cs @@ -0,0 +1,31 @@ +namespace Interfold.Shared.Contracts.Configuration; + +using System.ComponentModel.DataAnnotations; +using Interfold.Shared.Contracts.Configuration.Validation; + +/// API CORS allow-list bound from OCTOCON_CORS_ALLOWED_ORIGINS. Trailing +/// slashes are trimmed on parse for parity with ASP.NET Core's exact-string CORS matcher. +/// Empty list falls back to allow-any (dev-only). Per-entry absolute-http validation +/// runs at ValidateOnStart. +public sealed class CorsOptions : IValidatableObject +{ + public const string SectionName = "Octocon:Cors"; + + /// Normalised, de-duplicated allow-list; pass to WithOrigins as-is. + public IReadOnlyList AllowedOrigins { get; set; } = []; + + /// + public IEnumerable Validate(ValidationContext validationContext) + { + for (var i = 0; i < AllowedOrigins.Count; i++) + { + var origin = AllowedOrigins[i]; + if (!AbsoluteHttpUriAttribute.IsAbsoluteHttpUri(origin)) + { + yield return new ValidationResult( + $"{nameof(AllowedOrigins)}[{i}]='{origin}' is not a valid absolute http(s) URL.", + [$"{nameof(AllowedOrigins)}[{i}]"]); + } + } + } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/HealthEndpoints.cs b/shared/Interfold.Shared.Contracts/Configuration/HealthEndpoints.cs new file mode 100644 index 00000000..92b20eb1 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/HealthEndpoints.cs @@ -0,0 +1,32 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// API health-probe routes shared by ServiceDefaults, AppHost, and bootstrapper. +/// External contract — changing any value here is a breaking change for operators. +public static class HealthEndpoints +{ + public const string PathPrefix = "/health"; + + /// Liveness: process is up; no dependency checks. + public const string Live = "/health/live"; + + /// Readiness: checks tagged pass. + public const string Ready = "/health/ready"; + + /// Startup: checks tagged pass. + public const string Startup = "/health/startup"; +} + +/// Tag spellings that route an IHealthCheck onto a HealthEndpoints route. +public static class HealthCheckTags +{ + public const string Live = "live"; + public const string Ready = "ready"; + public const string Startup = "startup"; +} + +/// Well-known IHealthCheck registration names. +public static class HealthCheckNames +{ + /// ServiceDefaults' built-in "always healthy" liveness check. + public const string Self = "self"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/InMemorySecretsSeedOptions.cs b/shared/Interfold.Shared.Contracts/Configuration/InMemorySecretsSeedOptions.cs new file mode 100644 index 00000000..31a1442f --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/InMemorySecretsSeedOptions.cs @@ -0,0 +1,22 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// In-memory-adapter secrets seed bound from OCTOCON_INMEMORY_SECRETS_SEED__* +/// env vars, so external test harnesses can bootstrap the published image without an +/// in-process hook. Blanks are silently skipped; SecretsBootstrapService is the +/// single source of fail-fast for mandatory rows. +public sealed class InMemorySecretsSeedOptions +{ + public const string SectionName = "Octocon:InMemorySecretsSeed"; + + /// Env: OCTOCON_INMEMORY_SECRETS_SEED__ENCRYPTION_PEPPER. + public string? EncryptionPepper { get; set; } + + /// Env: OCTOCON_INMEMORY_SECRETS_SEED__AUTH_JWT_ES256_PRIVATE_PEM. + public string? AuthJwtEs256PrivatePem { get; set; } + + /// Env: OCTOCON_INMEMORY_SECRETS_SEED__AUTH_DEEP_LINK_SECRET. + public string? AuthDeepLinkSecret { get; set; } + + /// Env: OCTOCON_INMEMORY_SECRETS_SEED__AUTH_JWT_RSA256_PRIVATE_PEM. + public string? AuthJwtRsa256PrivatePem { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ObservabilityConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/ObservabilityConfiguration.cs new file mode 100644 index 00000000..d2a21e8d --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ObservabilityConfiguration.cs @@ -0,0 +1,24 @@ +namespace Interfold.Shared.Contracts.Configuration; + +using Interfold.Shared.Contracts.Configuration.Validation; + +/// +/// OpenTelemetry and observability configuration for traces and metrics export. +/// Binds from environment variables with OCTOCON_ prefix. +/// +public sealed class ObservabilityConfiguration +{ + public const string SectionName = "Octocon:Observability"; + + /// + /// OpenTelemetry Protocol (OTLP) gRPC endpoint for trace and metrics export. + /// When set, enables export; when null/empty, metrics remain in-process only. + /// Example: 'http://localhost:4317' + /// Env: OCTOCON_OTLP_ENDPOINT + /// The mirrors the bootstrapper's + /// config.observability.otlpEndpoint rule: null/empty is fine (disables + /// export); non-empty must parse as an absolute http(s) URL. + /// + [AbsoluteHttpUri] + public string? OtlpEndpoint { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/OctoconEnvKeys.cs b/shared/Interfold.Shared.Contracts/Configuration/OctoconEnvKeys.cs new file mode 100644 index 00000000..a5e1e59c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/OctoconEnvKeys.cs @@ -0,0 +1,67 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// Well-known OCTOCON_* env-var names shared by Infrastructure binders and +/// Bootstrapper publish. External contract — changing any value here is a breaking change +/// for operators. +public static class OctoconEnvKeys +{ + public const string NodeGroup = "OCTOCON_NODE_GROUP"; + + public const string Persistence = "OCTOCON_PERSISTENCE"; + public const string ScyllaKeyspace = "OCTOCON_SCYLLA_KEYSPACE"; + public const string PostgresConnection = "OCTOCON_POSTGRES_CONNECTION"; + public const string SingleScyllaInstance = "OCTOCON_SINGLE_SCYLLA_INSTANCE"; + public const string DbRetryAttempts = "OCTOCON_DB_RETRY_ATTEMPTS"; + public const string DbRetryInitialDelayMs = "OCTOCON_DB_RETRY_INITIAL_DELAY_MS"; + public const string DbRetryMaxDelayMs = "OCTOCON_DB_RETRY_MAX_DELAY_MS"; + public const string HydrationMaxConcurrency = "OCTOCON_HYDRATION_MAX_CONCURRENCY"; + + // Test/dev-only Scylla overrides — production values live in internal.secrets. + public const string ScyllaContactPoints = "OCTOCON_SCYLLA_CONTACT_POINTS"; + public const string ScyllaPort = "OCTOCON_SCYLLA_PORT"; + + // OCTOCON_INMEMORY_SECRETS_SEED__ — `__` → `:` remap lands under + // Octocon:InMemorySecretsSeed:* at read time. + public const string InMemorySecretsSeedEncryptionPepper = + "OCTOCON_INMEMORY_SECRETS_SEED:ENCRYPTION_PEPPER"; + public const string InMemorySecretsSeedAuthJwtEs256PrivatePem = + "OCTOCON_INMEMORY_SECRETS_SEED:AUTH_JWT_ES256_PRIVATE_PEM"; + public const string InMemorySecretsSeedAuthDeepLinkSecret = + "OCTOCON_INMEMORY_SECRETS_SEED:AUTH_DEEP_LINK_SECRET"; + public const string InMemorySecretsSeedAuthJwtRsa256PrivatePem = + "OCTOCON_INMEMORY_SECRETS_SEED:AUTH_JWT_RSA256_PRIVATE_PEM"; + + public const string AuthCallbackBaseUrl = "OCTOCON_AUTH_CALLBACK_BASE_URL"; + public const string JwtAuthority = "OCTOCON_JWT_AUTHORITY"; + public const string JwtAudience = "OCTOCON_JWT_AUDIENCE"; + + public const string DiscordOAuthClientId = "OCTOCON_DISCORD_OAUTH_CLIENT_ID"; + public const string DiscordOAuthClientSecret = "OCTOCON_DISCORD_OAUTH_CLIENT_SECRET"; + public const string GoogleOAuthClientId = "OCTOCON_GOOGLE_OAUTH_CLIENT_ID"; + public const string GoogleOAuthClientSecret = "OCTOCON_GOOGLE_OAUTH_CLIENT_SECRET"; + public const string AppleOAuthClientId = "OCTOCON_APPLE_OAUTH_CLIENT_ID"; + public const string AppleOAuthClientSecret = "OCTOCON_APPLE_OAUTH_CLIENT_SECRET"; + + public const string CorsAllowedOrigins = "OCTOCON_CORS_ALLOWED_ORIGINS"; + + public const string OtlpEndpoint = "OCTOCON_OTLP_ENDPOINT"; + + public const string TrustRootCaPath = "OCTOCON_TRUST_ROOT_CA_PATH"; + public const string TrustRootCaFingerprintPath = "OCTOCON_TRUST_ROOT_CA_FINGERPRINT_PATH"; + + public const string AvatarStorageRoot = "OCTOCON_AVATAR_STORAGE_ROOT"; + public const string AvatarPublicBase = "OCTOCON_AVATAR_PUBLIC_BASE"; + + public const string SocketBatchBytesThreshold = "OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD"; + + // Interfold..IntegrationTests / TestingConfiguration only. + public const string RunApiIntegration = "OCTOCON_RUN_API_INTEGRATION"; + public const string RunLiveIntegration = "OCTOCON_RUN_LIVE_INTEGRATION"; + public const string TestScyllaContactPoints = "OCTOCON_TEST_SCYLLA_CONTACT_POINTS"; + public const string TestScyllaUsername = "OCTOCON_TEST_SCYLLA_USERNAME"; + public const string TestScyllaPassword = "OCTOCON_TEST_SCYLLA_PASSWORD"; + public const string TestRegion = "OCTOCON_TEST_REGION"; + + // Fly.io process group — read as NodeGroup fallback. + public const string FlyProcessGroup = "FLY_PROCESS_GROUP"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/PersistenceConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/PersistenceConfiguration.cs new file mode 100644 index 00000000..7814980c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/PersistenceConfiguration.cs @@ -0,0 +1,73 @@ +namespace Interfold.Shared.Contracts.Configuration; + +using System.ComponentModel.DataAnnotations; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration.Validation; + +/// Scylla/Postgres persistence + retry configuration. Bound from OCTOCON_ +/// env vars. ScyllaKeyspace unifies session default keyspace and region routing; +/// Scylla connection secrets live in internal.secrets and are read via +/// ISecretsStore. +public sealed class PersistenceConfiguration : IValidatableObject +{ + public const string SectionName = "Octocon:Persistence"; + + [EnumDataType(typeof(PersistenceMode))] + public PersistenceMode Mode { get; set; } = PersistenceMode.ScyllaPostgres; + + /// Region/keyspace identity — nam|eur|ocn|eas|sam|sas|gdpr. + /// Env: OCTOCON_SCYLLA_KEYSPACE. + public Enums.ScyllaKeyspace ScyllaKeyspace { get; set; } = Enums.ScyllaKeyspace.Nam; + + /// Env: OCTOCON_POSTGRES_CONNECTION. + [Required, MinLength(1)] + public string PostgresConnectionString { get; set; } = ""; + + /// True → migration service creates only the ScyllaKeyspace keyspace. + /// Env: OCTOCON_SINGLE_SCYLLA_INSTANCE. + public bool IsSingleScyllaInstance { get; set; } = false; + + /// Env: OCTOCON_DB_RETRY_ATTEMPTS. + [Range(ConfigurationBounds.DbRetryAttemptsMin, ConfigurationBounds.DbRetryAttemptsMax)] + public int DbRetryAttempts { get; set; } = 3; + + /// Env: OCTOCON_DB_RETRY_INITIAL_DELAY_MS (binder converts ms → TimeSpan). + public TimeSpan DbRetryInitialDelay { get; set; } = TimeSpan.FromMilliseconds(100); + + /// Env: OCTOCON_DB_RETRY_MAX_DELAY_MS. Must be ≥ DbRetryInitialDelay. + public TimeSpan DbRetryMaxDelay { get; set; } = TimeSpan.FromMilliseconds(1500); + + /// Env: OCTOCON_HYDRATION_MAX_CONCURRENCY. Caps friendship-query fan-out. + [Range(ConfigurationBounds.HydrationMaxConcurrencyMin, ConfigurationBounds.HydrationMaxConcurrencyMax)] + public int HydrationMaxConcurrency { get; set; } = 8; + + public IEnumerable Validate(ValidationContext validationContext) + { + var initialMs = DbRetryInitialDelay.TotalMilliseconds; + if (initialMs < ConfigurationBounds.DbRetryInitialDelayMsMin + || initialMs > ConfigurationBounds.DbRetryInitialDelayMsMax) + { + yield return new ValidationResult( + $"{nameof(DbRetryInitialDelay)}={initialMs}ms is outside the allowed " + + $"[{ConfigurationBounds.DbRetryInitialDelayMsMin}..{ConfigurationBounds.DbRetryInitialDelayMsMax}]ms range.", + [nameof(DbRetryInitialDelay)]); + } + + var maxMs = DbRetryMaxDelay.TotalMilliseconds; + if (maxMs < ConfigurationBounds.DbRetryMaxDelayMsMin + || maxMs > ConfigurationBounds.DbRetryMaxDelayMsMax) + { + yield return new ValidationResult( + $"{nameof(DbRetryMaxDelay)}={maxMs}ms is outside the allowed " + + $"[{ConfigurationBounds.DbRetryMaxDelayMsMin}..{ConfigurationBounds.DbRetryMaxDelayMsMax}]ms range.", + [nameof(DbRetryMaxDelay)]); + } + + if (DbRetryMaxDelay < DbRetryInitialDelay) + { + yield return new ValidationResult( + $"{nameof(DbRetryMaxDelay)} ({DbRetryMaxDelay}) must be >= {nameof(DbRetryInitialDelay)} ({DbRetryInitialDelay}).", + [nameof(DbRetryMaxDelay), nameof(DbRetryInitialDelay)]); + } + } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/PostgresRoles.cs b/shared/Interfold.Shared.Contracts/Configuration/PostgresRoles.cs new file mode 100644 index 00000000..4d88e1fe --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/PostgresRoles.cs @@ -0,0 +1,11 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// Well-known Postgres role names shared by AppHost, bootstrapper, and integration +/// tests. Wire values — all three call sites must agree or first-boot bootstrap fails. +public static class PostgresRoles +{ + /// Disposable bootstrap superuser initdb creates on msg-db's first boot. + /// Consumed once by DatabaseInitPhase / DbInitHelper to mint the real + /// application roles, then scrambled to a fresh random password. + public const string Init = "db_init"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ScyllaGlobalKeyspace.cs b/shared/Interfold.Shared.Contracts/Configuration/ScyllaGlobalKeyspace.cs new file mode 100644 index 00000000..939da60b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ScyllaGlobalKeyspace.cs @@ -0,0 +1,9 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// Singleton non-regional keyspace (users registry, friendships, requests, tokens, +/// idempotency, migration ledger). Kept off ScyllaKeyspace because that enum +/// encodes per-region identity — the global keyspace is a distinct concept. +public static class ScyllaGlobalKeyspace +{ + public const string Name = "global"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ScyllaOverrideOptions.cs b/shared/Interfold.Shared.Contracts/Configuration/ScyllaOverrideOptions.cs new file mode 100644 index 00000000..92787e34 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ScyllaOverrideOptions.cs @@ -0,0 +1,13 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// Test/dev-only overrides for Scylla contact points and port. Production values +/// live in internal.secrets; a null property here means "no override — use the +/// secrets-store row". Env: OCTOCON_SCYLLA_CONTACT_POINTS, OCTOCON_SCYLLA_PORT. +public sealed class ScyllaOverrideOptions +{ + public const string SectionName = "Octocon:Scylla"; + + public IReadOnlyList? ContactPoints { get; set; } + + public int? Port { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/ScyllaTopology.cs b/shared/Interfold.Shared.Contracts/Configuration/ScyllaTopology.cs new file mode 100644 index 00000000..b224a26b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/ScyllaTopology.cs @@ -0,0 +1,45 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// Scylla container layout for the AppHost graph +/// (Parameters:scylla-topology). Distinct from DatabaseMode — that's the +/// operator-facing single/multi/cassandra choice; this is the derived Scylla-only layout. +public enum ScyllaTopology +{ + Single, + Multi, +} + +public static class ScyllaTopologyExtensions +{ + public const string SingleWireValue = "single"; + public const string MultiWireValue = "multi"; + + public static string ToWireValue(this ScyllaTopology topology) => topology switch + { + ScyllaTopology.Single => SingleWireValue, + ScyllaTopology.Multi => MultiWireValue, + _ => throw new ArgumentOutOfRangeException(nameof(topology), topology, "Unhandled ScyllaTopology."), + }; + + /// Missing / unrecognized values resolve to , + /// matching the AppHost's historical == "multi" comparison. + public static ScyllaTopology Parse(string? raw) + => string.Equals(raw, MultiWireValue, StringComparison.OrdinalIgnoreCase) + ? ScyllaTopology.Multi + : ScyllaTopology.Single; +} + +/// Wire spellings for Aspire boolean parameter toggles. +public static class BoolWire +{ + public const string TrueValue = "true"; + public const string FalseValue = "false"; + + public static string ToWireValue(bool value) => value ? TrueValue : FalseValue; + + /// Historical toggle semantics: null/empty → fallback, else "false" (case-insensitive) → off, everything else → on. + public static bool ParseToggle(string? raw, bool fallback) + => string.IsNullOrEmpty(raw) + ? fallback + : !string.Equals(raw, FalseValue, StringComparison.OrdinalIgnoreCase); +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/SocketConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/SocketConfiguration.cs new file mode 100644 index 00000000..e4705e6e --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/SocketConfiguration.cs @@ -0,0 +1,26 @@ +namespace Interfold.Shared.Contracts.Configuration; + +using System.ComponentModel.DataAnnotations; +using Interfold.Shared.Contracts.Configuration.Validation; + +/// +/// WebSocket message batching and performance tuning. +/// Binds from environment variables with OCTOCON_ prefix. +/// +public sealed class SocketConfiguration +{ + public const string SectionName = "Octocon:Socket"; + + /// + /// Threshold in bytes for batching outbound WebSocket messages. + /// When the batched payload reaches this size, it's flushed immediately. + /// Env: OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD + /// Default: null (use built-in default) + /// Bounds mirrored from / + /// . Null means "use the + /// built-in default"; only non-null values are range-checked because [Range] + /// on a nullable int short-circuits on null. + /// + [Range(ConfigurationBounds.SocketBatchBytesThresholdMin, ConfigurationBounds.SocketBatchBytesThresholdMax)] + public int? BatchBytesThreshold { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/StorageConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/StorageConfiguration.cs new file mode 100644 index 00000000..b08a1e3b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/StorageConfiguration.cs @@ -0,0 +1,31 @@ +namespace Interfold.Shared.Contracts.Configuration; + +using Interfold.Shared.Contracts.Configuration.Validation; + +/// +/// Local file storage configuration for avatars and other static assets. +/// Binds from environment variables with OCTOCON_ prefix. +/// +public sealed class StorageConfiguration +{ + public const string SectionName = "Octocon:Storage"; + + /// + /// Local filesystem root directory for storing uploaded avatars. + /// Env: OCTOCON_AVATAR_STORAGE_ROOT + /// Must be an absolute path when set; a relative path is rejected at + /// ValidateOnStart time by . + /// + [AbsolutePath] + public string? AvatarStorageRoot { get; set; } + + /// + /// Public base URL for accessing stored avatars (e.g., 'https://cdn.example.com/avatars/'). + /// Used to construct URL responses for avatar retrieval endpoints. + /// Env: OCTOCON_AVATAR_PUBLIC_BASE + /// Must be an absolute http(s) URL when set; the same rule the bootstrapper enforces + /// on config.storage.avatarPublicBase. + /// + [AbsoluteHttpUri] + public string? AvatarPublicBase { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/TestingConfiguration.cs b/shared/Interfold.Shared.Contracts/Configuration/TestingConfiguration.cs new file mode 100644 index 00000000..c7891763 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/TestingConfiguration.cs @@ -0,0 +1,50 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// +/// Testing environment configuration for integration test gating and connections. +/// Binds from environment variables with OCTOCON_RUN_* and OCTOCON_TEST_* prefixes. +/// +public sealed class TestingConfiguration +{ + public const string SectionName = "Octocon:Testing"; + + /// + /// When true, API integration tests (in-process API harness) will run. + /// Env: OCTOCON_RUN_API_INTEGRATION + /// + public bool RunApiIntegration { get; init; } + + /// + /// When true, live integration tests (real database connections) will run. + /// Env: OCTOCON_RUN_LIVE_INTEGRATION + /// + public bool RunLiveIntegration { get; init; } + + /// + /// Scylla contact points for live testing. + /// Env: OCTOCON_TEST_SCYLLA_CONTACT_POINTS + /// Default: '127.0.0.1' + /// + public string TestScyllaContactPoints { get; init; } = "127.0.0.1"; + + /// + /// Scylla username for live testing. + /// Env: OCTOCON_TEST_SCYLLA_USERNAME + /// Default: 'cassandra' + /// + public string TestScyllaUsername { get; init; } = "cassandra"; + + /// + /// Scylla password for live testing. + /// Env: OCTOCON_TEST_SCYLLA_PASSWORD + /// Default: 'cassandra' + /// + public string TestScyllaPassword { get; init; } = "cassandra"; + + /// + /// Region for live testing. + /// Env: OCTOCON_TEST_REGION + /// Default: 'nam' + /// + public string TestRegion { get; init; } = "nam"; +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/TrustOptions.cs b/shared/Interfold.Shared.Contracts/Configuration/TrustOptions.cs new file mode 100644 index 00000000..97c08e60 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/TrustOptions.cs @@ -0,0 +1,33 @@ +namespace Interfold.Shared.Contracts.Configuration; + +/// +/// Trust-distribution configuration for the API's +/// /.well-known/interfold-root-ca.{crt,pem,sha256} endpoints (see TrustController in +/// Interfold.Api). Both paths are set by the bootstrapper-generated AppHost env block +/// (OCTOCON_TRUST_ROOT_CA_PATH + OCTOCON_TRUST_ROOT_CA_FINGERPRINT_PATH) and +/// point at the read-only /certs bind mount. In dev (no bind mount, no env), both +/// stay null and the controller returns 404 for every route. +/// +public sealed class TrustOptions +{ + public const string SectionName = "Octocon:Trust"; + + /// + /// Absolute path inside the API process to the PEM-encoded root CA certificate served at + /// /.well-known/interfold-root-ca.crt (re-encoded to DER on the wire) and + /// /.well-known/interfold-root-ca.pem. null disables the .crt / .pem routes. + /// Env: OCTOCON_TRUST_ROOT_CA_PATH. + /// + public string? RootCaPath { get; set; } + + /// + /// Absolute path inside the API process to the sidecar file containing the SHA-256 + /// fingerprint of the root CA (uppercase colon-hex, matches + /// openssl x509 -fingerprint -sha256). Served as text at + /// /.well-known/interfold-root-ca.sha256 and reused as the HTTP + /// ETag on the cert routes so a --rotate-certs invalidates downstream + /// caches automatically. null disables the .sha256 route and drops the ETag. + /// Env: OCTOCON_TRUST_ROOT_CA_FINGERPRINT_PATH. + /// + public string? RootCaFingerprintPath { get; set; } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsoluteHttpUriAttribute.cs b/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsoluteHttpUriAttribute.cs new file mode 100644 index 00000000..6dabb6f0 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsoluteHttpUriAttribute.cs @@ -0,0 +1,67 @@ +namespace Interfold.Shared.Contracts.Configuration.Validation; + +using System.ComponentModel.DataAnnotations; + +/// Requires an absolute http/https URI string. Null/empty is accepted by default; +/// combine with [Required] when mandatory. Mirrors the bootstrapper's +/// ValidateAbsoluteHttpUri helpers so both sides enforce the same contract. +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] +public sealed class AbsoluteHttpUriAttribute : ValidationAttribute +{ + /// Whitespace-only counts as empty when true (default). + public bool AllowEmpty { get; init; } = true; + + /// + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) + { + if (value is null) + { + return ValidationResult.Success; + } + + if (value is not string s) + { + return new ValidationResult( + $"{validationContext.DisplayName} must be a string absolute http(s) URL (got {value.GetType().Name}).", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + if (AllowEmpty && string.IsNullOrWhiteSpace(s)) + { + return ValidationResult.Success; + } + + if (string.IsNullOrWhiteSpace(s)) + { + return new ValidationResult( + $"{validationContext.DisplayName} must be a non-empty absolute http(s) URL.", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + if (!IsAbsoluteHttpUri(s)) + { + return new ValidationResult( + $"{validationContext.DisplayName}='{s}' is not a valid absolute http(s) URL.", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + return ValidationResult.Success; + } + + /// Pure-logic core check reused by the bootstrapper's runtime validator. + /// Whitespace/empty → false; callers that treat blank as "disabled" short-circuit first. + public static bool IsAbsoluteHttpUri(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + if (!Uri.TryCreate(value, UriKind.Absolute, out var uri)) + { + return false; + } + + return uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps; + } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsolutePathAttribute.cs b/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsolutePathAttribute.cs new file mode 100644 index 00000000..5a65aa8c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/Validation/AbsolutePathAttribute.cs @@ -0,0 +1,62 @@ +namespace Interfold.Shared.Contracts.Configuration.Validation; + +using System.ComponentModel.DataAnnotations; +using System.IO; + +/// Requires an absolute filesystem path. Null/empty is accepted; combine with +/// [Required] when mandatory. Mirrors the bootstrapper's runtime path validator. +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] +public sealed class AbsolutePathAttribute : ValidationAttribute +{ + /// Whitespace-only counts as empty when true (default). + public bool AllowEmpty { get; init; } = true; + + /// + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) + { + if (value is null) + { + return ValidationResult.Success; + } + + if (value is not string s) + { + return new ValidationResult( + $"{validationContext.DisplayName} must be a string absolute path (got {value.GetType().Name}).", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + if (AllowEmpty && string.IsNullOrWhiteSpace(s)) + { + return ValidationResult.Success; + } + + if (string.IsNullOrWhiteSpace(s)) + { + return new ValidationResult( + $"{validationContext.DisplayName} must be a non-empty absolute path.", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + if (!IsAbsolutePath(s)) + { + return new ValidationResult( + $"{validationContext.DisplayName}='{s}' must be an absolute path " + + "(e.g. '/var/lib/interfold/avatars').", + [validationContext.MemberName ?? validationContext.DisplayName]); + } + + return ValidationResult.Success; + } + + /// Pure-logic core check reused by the bootstrapper. Whitespace/empty → false. + public static bool IsAbsolutePath(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + return Path.IsPathRooted(value); + } +} diff --git a/shared/Interfold.Shared.Contracts/Configuration/Validation/ConfigurationBounds.cs b/shared/Interfold.Shared.Contracts/Configuration/Validation/ConfigurationBounds.cs new file mode 100644 index 00000000..7a915933 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Configuration/Validation/ConfigurationBounds.cs @@ -0,0 +1,22 @@ +namespace Interfold.Shared.Contracts.Configuration.Validation; + +/// Single source of truth for numeric bounds on operator-tunable OCTOCON_* env vars. +/// Referenced by both ConfigPhase.Validate() (bootstrapper) and [Range] +/// attributes on IOptions classes (ValidateOnStart) so a tune in one place lands in both. +public static class ConfigurationBounds +{ + public const int DbRetryAttemptsMin = 1; + public const int DbRetryAttemptsMax = 100; + + public const int DbRetryInitialDelayMsMin = 1; + public const int DbRetryInitialDelayMsMax = 60_000; + + public const int DbRetryMaxDelayMsMin = 1; + public const int DbRetryMaxDelayMsMax = 600_000; + + public const int HydrationMaxConcurrencyMin = 1; + public const int HydrationMaxConcurrencyMax = 1024; + + public const int SocketBatchBytesThresholdMin = 1; + public const int SocketBatchBytesThresholdMax = 16 * 1024 * 1024; +} diff --git a/shared/Interfold.Shared.Contracts/DateTimeConverters.cs b/shared/Interfold.Shared.Contracts/DateTimeConverters.cs new file mode 100644 index 00000000..9c8ba960 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/DateTimeConverters.cs @@ -0,0 +1,69 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; + +namespace Interfold.Shared.Contracts; + +// JSON converters that normalize DateTime/DateTimeOffset to UTC when writing +// and tolerate malformed incoming strings like "2026-05-15T19:32:58.8619339+01:00Z" when reading. +public sealed partial class UtcDateTimeConverter : JsonConverter +{ + internal static readonly Regex OffsetWithTrailingZ = OffsetWithTrailingZRegex(); + + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType != JsonTokenType.String) + throw new JsonException("Expected string token for DateTime"); + + var s = reader.GetString() ?? string.Empty; + // Normalize malformed offsets like "+01:00Z" -> "+01:00" + s = OffsetWithTrailingZ.Replace(s, "$1"); + + // Use DateTimeOffset to preserve offset information if present, then convert to UTC + if (DateTimeOffset.TryParse(s, out var dto)) + { + return dto.UtcDateTime; + } + + // Fallback to DateTime.Parse which will throw as appropriate + return DateTime.Parse(s).ToUniversalTime(); + } + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) + { + // Always write as UTC with the 'o' format which for UTC DateTime includes a trailing 'Z' + var utc = value.Kind == DateTimeKind.Utc ? value : value.ToUniversalTime(); + writer.WriteStringValue(utc.ToString("o")); + } + + [GeneratedRegex(@"([+-]\d{2}:\d{2})Z$", RegexOptions.Compiled)] + private static partial Regex OffsetWithTrailingZRegex(); +} + +public sealed class UtcDateTimeOffsetConverter : JsonConverter +{ + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType != JsonTokenType.String) + throw new JsonException("Expected string token for DateTimeOffset"); + + var s = reader.GetString() ?? string.Empty; + s = UtcDateTimeConverter.OffsetWithTrailingZ.Replace(s, "$1"); + + if (DateTimeOffset.TryParse(s, out var dto)) + { + return dto.ToUniversalTime(); + } + + // Last resort - allow DateTime parse and wrap into DateTimeOffset (UTC) + var dt = DateTime.Parse(s).ToUniversalTime(); + return new DateTimeOffset(dt); + } + + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) + { + // Write as UTC with 'Z' by converting to UTC DateTime and formatting with "o" + var utc = value.ToUniversalTime().UtcDateTime; + writer.WriteStringValue(utc.ToString("o")); + } +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/Enums/AvatarSource.cs b/shared/Interfold.Shared.Contracts/Enums/AvatarSource.cs new file mode 100644 index 00000000..a6e400aa --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/AvatarSource.cs @@ -0,0 +1,27 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Discriminator for the hosting source of an avatar URL. +/// Stored as smallint in Scylla, exposed on the wire as snake-case strings +/// for parity with . +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum AvatarSource : short +{ + /// + /// Avatar bytes are persisted by IAvatarStorage on this deployment; + /// the stored URL is a relative path that requires origin qualification when + /// returned to a client. + /// + [JsonStringEnumMemberName("local")] + Local = 0, + + /// + /// Avatar lives on a third-party host. The stored URL is absolute and is + /// returned to the client unchanged; cleanup paths must not delete it. + /// + [JsonStringEnumMemberName("external")] + External = 1, +} diff --git a/shared/Interfold.Shared.Contracts/Enums/ClientPlatform.cs b/shared/Interfold.Shared.Contracts/Enums/ClientPlatform.cs new file mode 100644 index 00000000..9a19074c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/ClientPlatform.cs @@ -0,0 +1,23 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// The client platform identifiers used by the firebase-config endpoint's +/// ?platform= query value and the socket join payload's platform member. +/// Wire spellings are the lowercase names ("android" / "ios" / "web"); unknown values are +/// handled by callers via so the legacy +/// invalid-platform responses are preserved. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ClientPlatform +{ + [JsonStringEnumMemberName("android")] + Android, + + [JsonStringEnumMemberName("ios")] + Ios, + + [JsonStringEnumMemberName("web")] + Web, +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/Enums/DatabaseMode.cs b/shared/Interfold.Shared.Contracts/Enums/DatabaseMode.cs new file mode 100644 index 00000000..ad69d39b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/DatabaseMode.cs @@ -0,0 +1,19 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Database stack the bootstrapper deploys. Wire values: single (one Scylla node), +/// multi (7-region Scylla cluster), cassandra (single Cassandra 5 node). Translated to +/// AppHost include-scylla/include-cassandra/scylla-topology params. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum DatabaseMode +{ + [JsonStringEnumMemberName("single")] + Single, + + [JsonStringEnumMemberName("multi")] + Multi, + + [JsonStringEnumMemberName("cassandra")] + Cassandra, +} diff --git a/shared/Interfold.Shared.Contracts/Enums/EnumCode.cs b/shared/Interfold.Shared.Contracts/Enums/EnumCode.cs new file mode 100644 index 00000000..b94fa9fc --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/EnumCode.cs @@ -0,0 +1,61 @@ +namespace Interfold.Shared.Contracts.Enums; + +/// Persistence-code () round-trip for enums backing a smallint +/// column. Outbound is a plain (short)value cast at the call site (every consumer +/// uses a : short backing). Inbound: throwing FromCode, defaulting FromCode(fallback), +/// or the bool + out TryFromCode. Prefer the extension-method form in EnumCodeExtensions. +public static class EnumCode where TEnum : struct, Enum +{ + private static readonly Dictionary ByCode; + + static EnumCode() + { + var values = Enum.GetValues(); + ByCode = new Dictionary(values.Length); + foreach (var v in values) + { + ByCode[Convert.ToInt16(v)] = v; + } + } + + /// Convert to ; null/unknown + /// returns when supplied, otherwise throws. + public static TEnum FromCode(short? code, TEnum? fallback = null) + { + if (code is { } c && ByCode.TryGetValue(c, out var value)) + { + return value; + } + + if (fallback is { } fb) + { + return fb; + } + + throw new ArgumentOutOfRangeException(nameof(code), code, + $"Unhandled {typeof(TEnum).Name} code."); + } + + /// Convert to ?; null/unknown → null. + public static TEnum? FromCodeOrNull(short? code) + { + if (code is { } c && ByCode.TryGetValue(c, out var value)) + { + return value; + } + + return null; + } + + /// Bool + out safe lookup; null/unknown → false. + public static bool TryFromCode(short? code, out TEnum value) + { + if (code is { } c && ByCode.TryGetValue(c, out value)) + { + return true; + } + + value = default; + return false; + } +} diff --git a/shared/Interfold.Shared.Contracts/Enums/EnumCodeExtensions.cs b/shared/Interfold.Shared.Contracts/Enums/EnumCodeExtensions.cs new file mode 100644 index 00000000..6b8c7625 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/EnumCodeExtensions.cs @@ -0,0 +1,30 @@ +namespace Interfold.Shared.Contracts.Enums; + +// Extension-method surface for EnumCode. Reflection tables live on the generic type; +// this file adds ergonomic call syntax so callers write `code.FromCode()`. +public static class EnumCodeExtensions +{ + public static TEnum FromCode(this short? code, TEnum fallback) where TEnum : struct, Enum + => EnumCode.FromCode(code, fallback); + + public static TEnum FromCode(this short code, TEnum fallback) where TEnum : struct, Enum + => EnumCode.FromCode(code, fallback); + + /// Fallback-less persistence-read form: unknown / null throws. Silently + /// defaulting a corrupt row masks data corruption; use fallback overloads only for + /// in-process business-default cases. + public static TEnum FromCode(this short? code) where TEnum : struct, Enum + => EnumCode.FromCode(code); + + public static TEnum FromCode(this short code) where TEnum : struct, Enum + => EnumCode.FromCode(code); + + public static TEnum? FromCodeOrNull(this short? code) where TEnum : struct, Enum + => EnumCode.FromCodeOrNull(code); + + public static TEnum? FromCodeOrNull(this short code) where TEnum : struct, Enum + => EnumCode.FromCodeOrNull(code); + + public static bool TryFromCode(this short? code, out TEnum value) where TEnum : struct, Enum + => EnumCode.TryFromCode(code, out value); +} diff --git a/shared/Interfold.Shared.Contracts/Enums/EnumWire.cs b/shared/Interfold.Shared.Contracts/Enums/EnumWire.cs new file mode 100644 index 00000000..665b5ee0 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/EnumWire.cs @@ -0,0 +1,56 @@ +using System.Reflection; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Non-JSON callers' view of the same wire vocabulary +/// exposes on the JSON boundary. Wire spelling +/// per member comes from (fallback: member +/// name), cached at type init. TryParse is case-insensitive, trims whitespace, and treats +/// null/empty/blank as unknown. +public static class EnumWire where TEnum : struct, Enum +{ + private static readonly Dictionary ToWireMap; + private static readonly Dictionary FromWireMap; + + static EnumWire() + { + var fields = typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static); + ToWireMap = new Dictionary(fields.Length); + FromWireMap = new Dictionary(fields.Length, StringComparer.OrdinalIgnoreCase); + + foreach (var field in fields) + { + var value = (TEnum)field.GetValue(null)!; + var wire = field.GetCustomAttribute()?.Name ?? field.Name; + + ToWireMap[value] = wire; + FromWireMap[wire] = value; + } + } + + /// Wire spelling for ; throws on undeclared members. + public static string ToWire(TEnum value) + { + if (ToWireMap.TryGetValue(value, out var wire)) + { + return wire; + } + + throw new ArgumentOutOfRangeException(nameof(value), value, + $"Unhandled {typeof(TEnum).Name} value."); + } + + /// Case-insensitive parse; trims whitespace. Callers add any + /// default-on-unknown semantics (see EnumWireExtensions.ParseWithDefault). + public static bool TryParse(string? raw, out TEnum value) + { + if (string.IsNullOrWhiteSpace(raw)) + { + value = default; + return false; + } + + return FromWireMap.TryGetValue(raw.Trim(), out value); + } +} diff --git a/shared/Interfold.Shared.Contracts/Enums/EnumWireExtensions.cs b/shared/Interfold.Shared.Contracts/Enums/EnumWireExtensions.cs new file mode 100644 index 00000000..c21a5b81 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/EnumWireExtensions.cs @@ -0,0 +1,38 @@ +namespace Interfold.Shared.Contracts.Enums; + +// Parse-side helpers for env-driven enums: blank → compiled-in default, unknown → throw +// (so an operator typo can't silently degrade a Primary to an Auxiliary). +// Tolerant "unknown → null" callers invoke EnumWire.TryParse directly. +public static class EnumWireExtensions +{ + public static string ToWire(this TEnum value) where TEnum : struct, Enum + => EnumWire.ToWire(value); + + public static bool TryParseWire(this string? raw, out TEnum value) where TEnum : struct, Enum + => EnumWire.TryParse(raw, out value); + + // Blank → default, unknown-non-empty → throw. Shared shape for env-parsers below. + public static T ParseWithDefault(string? raw, T defaultValue, Func errorProvider) where T : struct, Enum + { + if (string.IsNullOrWhiteSpace(raw)) + { + return defaultValue; + } + + if (raw.TryParseWire(out var value)) + { + return value; + } + + throw new InvalidOperationException(errorProvider(raw.Trim())); + } + + public static NodeGroup ParseNodeGroup(string? raw) + => ParseWithDefault(raw, NodeGroup.Auxiliary, trimmed => $"Unrecognised node group '{trimmed}'. Valid values: primary, auxiliary, sidecar."); + + public static ScyllaKeyspace ParseScyllaKeyspace(string? raw) + => ParseWithDefault(raw, ScyllaKeyspace.Nam, trimmed => $"Unrecognised scylla keyspace '{trimmed}'. Valid values: nam, eur, sam, sas, eas, ocn, gdpr."); + + public static PersistenceMode ParsePersistenceMode(string? raw) + => ParseWithDefault(raw, PersistenceMode.ScyllaPostgres, trimmed => $"Unsupported OCTOCON_PERSISTENCE value '{trimmed}'. Expected: scylla-postgres | inmemory."); +} diff --git a/shared/Interfold.Shared.Contracts/Enums/FieldType.cs b/shared/Interfold.Shared.Contracts/Enums/FieldType.cs new file mode 100644 index 00000000..fff8679b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/FieldType.cs @@ -0,0 +1,46 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Type of a user-defined settings field. The wire representation is the lowercase +/// snake_case name ("text", "month_year", "month_day", ...), +/// preserving what the Elixir server emitted so the Kotlin client and any prior +/// captured payloads deserialize unchanged. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum FieldType : short +{ + [JsonStringEnumMemberName("text")] + Text = 0, + + [JsonStringEnumMemberName("number")] + Number = 1, + + [JsonStringEnumMemberName("boolean")] + Boolean = 2, + + [JsonStringEnumMemberName("date")] + Date = 3, + + [JsonStringEnumMemberName("colour")] + Colour = 4, + + [JsonStringEnumMemberName("plaintext")] + Plaintext = 5, + + [JsonStringEnumMemberName("month")] + Month = 6, + + [JsonStringEnumMemberName("year")] + Year = 7, + + [JsonStringEnumMemberName("month_year")] + MonthYear = 8, + + [JsonStringEnumMemberName("timestamp")] + Timestamp = 9, + + [JsonStringEnumMemberName("month_day")] + MonthDay = 10, +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/Enums/FriendshipLevel.cs b/shared/Interfold.Shared.Contracts/Enums/FriendshipLevel.cs new file mode 100644 index 00000000..ff59ade9 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/FriendshipLevel.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Friendship level. Wire: "friend" / "trusted_friend". Storage: +/// smallint 0/1. Persistence read uses fallback-less FromCode<FriendshipLevel>() +/// so corrupt rows throw instead of silently downgrading a trusted relationship. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum FriendshipLevel : short +{ + [JsonStringEnumMemberName("friend")] + Friend = 0, + + [JsonStringEnumMemberName("trusted_friend")] + TrustedFriend = 1, +} diff --git a/shared/Interfold.Shared.Contracts/Enums/NodeGroup.cs b/shared/Interfold.Shared.Contracts/Enums/NodeGroup.cs new file mode 100644 index 00000000..98fd77e1 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/NodeGroup.cs @@ -0,0 +1,36 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// The role a running node plays in the cluster. +/// Maps directly to the legacy Elixir NODE_GROUP / FLY_PROCESS_GROUP values. +/// +/// +/// JSON wire values (lowercase enum name): "primary", "auxiliary", "sidecar". +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum NodeGroup +{ + /// + /// Runs the HTTP API, background jobs, and owns cluster singletons. + /// Legacy: primary. + /// + [JsonStringEnumMemberName("primary")] + Primary, + + /// + /// Runs the HTTP API only. No background jobs, no singletons. + /// Legacy: auxiliary. + /// + [JsonStringEnumMemberName("auxiliary")] + Auxiliary, + + /// + /// Handles CPU-intensive tasks (e.g. image processing, encryption offload). + /// Does not run a public HTTP server in production. + /// Legacy: sidecar. + /// + [JsonStringEnumMemberName("sidecar")] + Sidecar +} diff --git a/shared/Interfold.Shared.Contracts/Enums/ScyllaKeyspace.cs b/shared/Interfold.Shared.Contracts/Enums/ScyllaKeyspace.cs new file mode 100644 index 00000000..7a36150e --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/ScyllaKeyspace.cs @@ -0,0 +1,31 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Per-instance region / keyspace identity. Wire values (lowercase name) also +/// appear as CQL keyspace names and as the region prefix on externally-shared system IDs +/// (e.g. nam:abc1234) — the mapping must never drift. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ScyllaKeyspace +{ + [JsonStringEnumMemberName("nam")] + Nam, + + [JsonStringEnumMemberName("eur")] + Eur, + + [JsonStringEnumMemberName("sam")] + Sam, + + [JsonStringEnumMemberName("sas")] + Sas, + + [JsonStringEnumMemberName("eas")] + Eas, + + [JsonStringEnumMemberName("ocn")] + Ocn, + + [JsonStringEnumMemberName("gdpr")] + Gdpr, +} diff --git a/shared/Interfold.Shared.Contracts/Enums/TolerantWireEnumJsonConverter.cs b/shared/Interfold.Shared.Contracts/Enums/TolerantWireEnumJsonConverter.cs new file mode 100644 index 00000000..ffaf12ca --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/TolerantWireEnumJsonConverter.cs @@ -0,0 +1,37 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// Nullable-enum JSON converter: unknown wire spellings (or non-string tokens) +/// return null instead of throwing. Opt-in per-property; write emits the canonical +/// spelling. Used at boundary payloads where a stray +/// value on one field must not poison sibling fields. +public sealed class TolerantWireEnumJsonConverter : JsonConverter + where TEnum : struct, Enum +{ + public override TEnum? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + switch (reader.TokenType) + { + case JsonTokenType.Null: + return null; + case JsonTokenType.String: + return EnumWire.TryParse(reader.GetString(), out var value) ? value : null; + default: + reader.Skip(); + return null; + } + } + + public override void Write(Utf8JsonWriter writer, TEnum? value, JsonSerializerOptions options) + { + if (value is null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStringValue(EnumWire.ToWire(value.Value)); + } +} diff --git a/shared/Interfold.Shared.Contracts/Enums/VisibilityLevel.cs b/shared/Interfold.Shared.Contracts/Enums/VisibilityLevel.cs new file mode 100644 index 00000000..05bfb980 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Enums/VisibilityLevel.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Per-alter visibility tier. Values are wire-stable (JSON string form matches the +/// enum member name) and Scylla-stable (persisted as smallint with the ordinal +/// below), so re-ordering members is a breaking change for both persisted rows and +/// deployed clients. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum VisibilityLevel : short +{ + [JsonStringEnumMemberName("public")] + Public = 0, + [JsonStringEnumMemberName("friends_only")] + FriendsOnly = 1, + [JsonStringEnumMemberName("trusted_only")] + TrustedOnly = 2, + [JsonStringEnumMemberName("private")] + Private = 3 +} diff --git a/shared/Interfold.Shared.Contracts/ErrorCode.cs b/shared/Interfold.Shared.Contracts/ErrorCode.cs new file mode 100644 index 00000000..c1ebadff --- /dev/null +++ b/shared/Interfold.Shared.Contracts/ErrorCode.cs @@ -0,0 +1,32 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts; + +/// +/// A client-visible error identifier — the typed form of the frozen wire vocabulary in +/// . Same role EntityRef/EntityRefs play for conflict +/// entity references: the registry is the convention, the struct is the type distinction +/// that stops arbitrary strings from reaching ErrorResponse.Code, +/// InterfoldException.Code, or SocketReasonResponse.Reason. The constructor +/// stays public for the few dynamically-derived codes (e.g. resolution-hint conflicts). +/// JSON serializes as the raw string. +/// +[JsonConverter(typeof(ErrorCodeJsonConverter))] +public readonly record struct ErrorCode +{ + public string Value { get; } + + public ErrorCode(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public override string ToString() => Value; +} + +internal sealed class ErrorCodeJsonConverter : StringBackedJsonConverter +{ + protected override ErrorCode Create(string value) => new(value); + protected override string GetValue(ErrorCode value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/ErrorCodes.cs b/shared/Interfold.Shared.Contracts/ErrorCodes.cs new file mode 100644 index 00000000..7365ddc3 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/ErrorCodes.cs @@ -0,0 +1,96 @@ +namespace Interfold.Shared.Contracts; + +/// +/// Central registry of the client-visible error identifiers as typed +/// members, mirroring the +/// pattern: every ErrorResponse.Code, +/// SocketReasonResponse.Reason, and InterfoldException.Code literal lives here +/// so the wire vocabulary is greppable in one place. The string values are frozen — +/// the Kotlin client matches on them. +/// +public static class ErrorCodes +{ + // Generic + public static readonly ErrorCode UnknownError = new("unknown_error"); + public static readonly ErrorCode BadRequest = new("bad_request"); + + // Friendships / friend requests + public static readonly ErrorCode CannotSendSelf = new("cannot_send_self"); + public static readonly ErrorCode CannotCancelSelf = new("cannot_cancel_self"); + public static readonly ErrorCode CannotAcceptSelf = new("cannot_accept_self"); + public static readonly ErrorCode CannotRejectSelf = new("cannot_reject_self"); + public static readonly ErrorCode CannotTrustSelf = new("cannot_trust_self"); + public static readonly ErrorCode CannotUntrustSelf = new("cannot_untrust_self"); + public static readonly ErrorCode CannotViewOwnFriendship = new("cannot_view_own_friendship"); + public static readonly ErrorCode CannotDeleteOwnFriendship = new("cannot_delete_own_friendship"); + public static readonly ErrorCode FriendshipNotFound = new("friendship_not_found"); + + // Entity lookups + public static readonly ErrorCode SystemNotFound = new("system_not_found"); + public static readonly ErrorCode AlterNotFound = new("alter_not_found"); + public static readonly ErrorCode TagNotFound = new("tag_not_found"); + public static readonly ErrorCode PollNotFound = new("poll_not_found"); + public static readonly ErrorCode FrontNotFound = new("front_not_found"); + public static readonly ErrorCode JournalEntryNotFound = new("journal_entry_not_found"); + + // Validation + public static readonly ErrorCode InvalidAlterId = new("invalid_alter_id"); + public static readonly ErrorCode InvalidParentTagId = new("invalid_parent_tag_id"); + public static readonly ErrorCode InvalidPushToken = new("invalid_push_token"); + public static readonly ErrorCode InvalidEndpoint = new("invalid_endpoint"); + public static readonly ErrorCode InvalidPlatform = new("invalid_platform"); + public static readonly ErrorCode InvalidAnchor = new("invalid_anchor"); + public static readonly ErrorCode InvalidEndAnchor = new("invalid_end_anchor"); + public static readonly ErrorCode PollInvalidTimeEnd = new("poll_invalid_time_end"); + public static readonly ErrorCode AvatarFileEmpty = new("avatar_file_empty"); + public static readonly ErrorCode AvatarFileRequired = new("avatar_file_required"); + public static readonly ErrorCode AvatarUrlInvalid = new("avatar_url_invalid"); + public static readonly ErrorCode AvatarUrlTooLong = new("avatar_url_too_long"); + public static readonly ErrorCode InvalidExportFormat = new("invalid_export_format"); + + // Auth + public static readonly ErrorCode InvalidOAuthProvider = new("invalid_oauth_provider"); + public static readonly ErrorCode InvalidToken = new("invalid_token"); + public static readonly ErrorCode MissingRedirectUri = new("missing_redirect_uri"); + public static readonly ErrorCode TokenRevoked = new("token_revoked"); + + // Firebase client-config endpoint + public static readonly ErrorCode FirebaseConfigUnavailable = new("firebase_config_unavailable"); + + // Recovery-code decryption (RecoveryCodeResolver) + public static readonly ErrorCode RecoveryCodeNotProvided = new("recovery_code_not_provided"); + public static readonly ErrorCode RecoveryCodeNotJwe = new("recovery_code_not_jwe"); + public static readonly ErrorCode DecryptionError = new("decryption_error"); + + // WebSocket HTTP upgrade errors + public static readonly ErrorCode WebSocketUpgradeRequired = new("websocket_upgrade_required"); + public static readonly ErrorCode MissingSocketToken = new("missing_socket_token"); + + // Internal invariants surfaced via InterfoldException + public static readonly ErrorCode AlterCheckServerIssue = new("alter_check_server_issue"); + public static readonly ErrorCode EncryptionSaltRequired = new("encryption_salt_required"); + + // Socket endpoint relay + public static readonly ErrorCode SocketEndpointPayloadInvalid = new("socket_endpoint_payload_invalid"); + public static readonly ErrorCode SocketEndpointMethodPathRequired = new("socket_endpoint_method_path_required"); + public static readonly ErrorCode SocketEndpointPathForbidden = new("socket_endpoint_path_forbidden"); + public static readonly ErrorCode SocketEndpointProxyMisrouted = new("socket_endpoint_proxy_misrouted"); + + /// + /// Reasons carried on SocketReasonResponse frames when a socket message is refused, + /// including the token-authorization failure reasons for phx_join. + /// + public static class SocketReasons + { + public static readonly ErrorCode UnsupportedProtocolVersion = new("unsupported_protocol_version"); + public static readonly ErrorCode RateLimited = new("rate_limited"); + public static readonly ErrorCode NotJoined = new("not_joined"); + public static readonly ErrorCode EventNotImplemented = new("event_not_implemented"); + public static readonly ErrorCode MissingSocketToken = ErrorCodes.MissingSocketToken; + public static readonly ErrorCode InvalidSocketToken = new("invalid_socket_token"); + public static readonly ErrorCode InvalidSocketTokenSubject = new("invalid_socket_token_subject"); + public static readonly ErrorCode UnauthorizedTopic = new("unauthorized_topic"); + public static readonly ErrorCode TokenRevoked = new("token_revoked"); + public static readonly ErrorCode Unauthorized = new("unauthorized"); + } +} diff --git a/shared/Interfold.Shared.Contracts/Events/ITargetedClusterEvent.cs b/shared/Interfold.Shared.Contracts/Events/ITargetedClusterEvent.cs new file mode 100644 index 00000000..aaac8a1c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Events/ITargetedClusterEvent.cs @@ -0,0 +1,23 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Events; + +/// +/// Marker for cluster events whose primary recipient is a single system identified by +/// . +/// +/// IClusterEventBus uses this contract to filter delivery: a subscription created +/// with a non-null targetSystemId only receives events whose +/// matches that value. Events that do not implement this interface are broadcast to every +/// subscriber regardless of scoping, which preserves correctness for any future non-targeted signals. +/// +/// +/// is a so +/// the event bus's equality filter compares two wire-canonical strings by construction +/// rather than relying on every publisher hand-formatting a scoped prefix correctly. +/// +/// +public interface ITargetedClusterEvent +{ + ScopedSystemId TargetSystemId { get; } +} diff --git a/shared/Interfold.Shared.Contracts/ICommandResult.cs b/shared/Interfold.Shared.Contracts/ICommandResult.cs new file mode 100644 index 00000000..7a537604 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/ICommandResult.cs @@ -0,0 +1,16 @@ +namespace Interfold.Shared.Contracts; + +/// Marker for command result records carrying a replay flag. +public interface ICommandResult +{ + bool Replay { get; } +} + +/// F-bounded self-type so IdempotentCommandHandler can synthesise a +/// "same record with Replay = true" copy from a stored outcome without a per-handler +/// override. Canonical WithReplay() body: this with { Replay = true }. +public interface ICommandResult : ICommandResult + where TSelf : ICommandResult +{ + TSelf WithReplay(); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/AlterId.cs b/shared/Interfold.Shared.Contracts/Ids/AlterId.cs new file mode 100644 index 00000000..ca6b86be --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/AlterId.cs @@ -0,0 +1,51 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Strongly-typed wrapper around the per-system alter id. Backed by short +/// to match every smallint Scylla column that stores it; wire form is a JSON number, so +/// emission is byte-identical to the pre-narrowing shape. Range is enforced at the JSON +/// reader (GetInt16 throws → 400) so no downstream code sees an out-of-range value. +[JsonConverter(typeof(AlterIdJsonConverter))] +public readonly record struct AlterId : IParsable +{ + public short Value { get; } + + public AlterId(short value) + { + Value = value; + } + + public static explicit operator AlterId(short value) => new(value); + + public override string ToString() => Value.ToString(System.Globalization.CultureInfo.InvariantCulture); + + public static AlterId Parse(string s, IFormatProvider? provider) + => new(short.Parse(s, System.Globalization.NumberStyles.Integer, provider ?? System.Globalization.CultureInfo.InvariantCulture)); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out AlterId result) + { + if (short.TryParse(s, System.Globalization.NumberStyles.Integer, provider ?? System.Globalization.CultureInfo.InvariantCulture, out var value)) + { + result = new AlterId(value); + return true; + } + + result = default; + return false; + } +} + +internal sealed class AlterIdJsonConverter : JsonConverter +{ + public override AlterId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.GetInt16()); + + public override void Write(Utf8JsonWriter writer, AlterId value, JsonSerializerOptions options) + => writer.WriteNumberValue(value.Value); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/AvatarUrl.cs b/shared/Interfold.Shared.Contracts/Ids/AvatarUrl.cs new file mode 100644 index 00000000..71d0a7c1 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/AvatarUrl.cs @@ -0,0 +1,35 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// An avatar (or extra-image) URL. Carries either a server-relative path (when the paired +/// AvatarSource is Local — qualified to an absolute URL at the API boundary by +/// AvatarUrlQualifier) or an absolute external URL. Construction does not validate: +/// legacy rows and passthrough imports carry arbitrary spellings. JSON serializes as the raw +/// string, so read models, socket frames, and hashed command payloads are unchanged. +/// +[JsonConverter(typeof(AvatarUrlJsonConverter))] +public readonly record struct AvatarUrl +{ + public string Value { get; } + + public AvatarUrl(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator AvatarUrl(string value) => new(value); + + public static implicit operator string(AvatarUrl value) => value.Value; + + public override string ToString() => Value; + + public static AvatarUrl? FromNullable(string? value) => value is null ? null : new AvatarUrl(value); +} + +internal sealed class AvatarUrlJsonConverter : StringBackedJsonConverter +{ + protected override AvatarUrl Create(string value) => new(value); + protected override string GetValue(AvatarUrl value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/EntityRef.cs b/shared/Interfold.Shared.Contracts/Ids/EntityRef.cs new file mode 100644 index 00000000..7411cf1d --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/EntityRef.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// Strongly-typed wrapper around the conflict entity-reference string carried on +/// ConflictResult.EntityRef (e.g. "poll:title_too_long") and surfaced to +/// clients verbatim via ErrorResponse.EntityRef on 409/422 bodies. The closed +/// vocabulary lives in Interfold.Shared.Contracts.Operations.EntityRefs; integration tests +/// assert the exact strings, so the wire spellings are frozen. +/// +[JsonConverter(typeof(EntityRefJsonConverter))] +public readonly record struct EntityRef +{ + public string Value { get; } + + public EntityRef(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator EntityRef(string value) => new(value); + + public static implicit operator string(EntityRef value) => value.Value; + + public override string ToString() => Value; +} + +internal sealed class EntityRefJsonConverter : StringBackedJsonConverter +{ + protected override EntityRef Create(string value) => new(value); + protected override string GetValue(EntityRef value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/EntityVersion.cs b/shared/Interfold.Shared.Contracts/Ids/EntityVersion.cs new file mode 100644 index 00000000..afa7867b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/EntityVersion.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Optimistic-concurrency version stamp carried by journal mutation requests +/// (ExpectedVersion). Accepted from clients today but not enforced server-side +/// yet — the type reserves the concept for a future LWT check. JSON: raw number. +[JsonConverter(typeof(EntityVersionJsonConverter))] +public readonly record struct EntityVersion(long Value) +{ + public static explicit operator EntityVersion(long value) => new(value); + + public static implicit operator long(EntityVersion value) => value.Value; + + public override string ToString() => Value.ToString(System.Globalization.CultureInfo.InvariantCulture); +} + +internal sealed class EntityVersionJsonConverter : JsonConverter +{ + public override EntityVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.GetInt64()); + + public override void Write(Utf8JsonWriter writer, EntityVersion value, JsonSerializerOptions options) + => writer.WriteNumberValue(value.Value); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/HexColor.cs b/shared/Interfold.Shared.Contracts/Ids/HexColor.cs new file mode 100644 index 00000000..5d9b0fb7 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/HexColor.cs @@ -0,0 +1,135 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Display color for alters/tags/journals — canonically #RRGGBB, also +/// accepts #RGB and #RRGGBBAA. Fully strict: every construction path +/// validates and throws on malformed input. JSON serializes as the raw string; +/// malformed inbound surfaces as 400 via . +[JsonConverter(typeof(HexColorJsonConverter))] +public readonly record struct HexColor : IParsable +{ + public string Value { get; } + + public HexColor(string value) + { + ArgumentNullException.ThrowIfNull(value); + if (!IsWellFormedString(value)) + { + throw new FormatException( + $"'{value}' is not a well-formed HexColor (expected '#RGB', '#RRGGBB', or '#RRGGBBAA')."); + } + + Value = value; + } + + public static explicit operator HexColor(string value) => new(value); + + public static implicit operator string(HexColor value) => value.Value; + + // Tautology for values built through the public API; retained for defence-in-depth + // and test-side assertions. + public bool IsWellFormed => IsWellFormedString(Value); + + public override string ToString() => Value; + + public static HexColor Parse(string s, IFormatProvider? provider) + { + ArgumentNullException.ThrowIfNull(s); + return new HexColor(s); + } + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out HexColor result) + { + if (s is null || !IsWellFormedString(s)) + { + result = default; + return false; + } + + result = new HexColor(s); + return true; + } + + /// Scylla row rehydration: unset (null) stays null; any non-null + /// value must already be well-formed or read fails — corrupt stored colours surface + /// at list/join time. normalises legacy bare hex + /// before reads rely on this. + public static HexColor? FromNullable(string? value) => value is null ? null : new HexColor(value); + + /// Tolerant normalise-then-validate for fixup/migration tooling only — + /// unrecoverable input becomes null instead of throwing. Production row mappers use + /// . + public static HexColor? FromStorage(string? value) + { + var normalised = Normalise(value); + return normalised is null ? null : new HexColor(normalised); + } + + /// Recover a canonical spelling from noisy input (SP importer / + /// HexColorFixupService). Returns null when unrecoverable; otherwise the + /// well-formed #RRGGBB / #RGB / #RRGGBBAA form. + public static string? Normalise(string? raw) + { + if (string.IsNullOrWhiteSpace(raw)) + return null; + + var trimmed = raw.Trim(); + if (IsWellFormedString(trimmed)) + return trimmed; + + var stripped = trimmed.StartsWith('#') ? trimmed[1..] : trimmed; + if (stripped.Length != 6) + return null; + + foreach (var c in stripped) + { + if (!Uri.IsHexDigit(c)) + return null; + } + + return "#" + stripped; + } + + // Accepts #RGB, #RRGGBB, or #RRGGBBAA (case-insensitive on hex digits). + private static bool IsWellFormedString(ReadOnlySpan value) + { + if (value.Length == 0 || value[0] != '#') + return false; + + var digits = value.Length - 1; + if (digits is not (3 or 6 or 8)) + return false; + + for (var i = 1; i < value.Length; i++) + { + if (!Uri.IsHexDigit(value[i])) + return false; + } + + return true; + } +} + +internal sealed class HexColorJsonConverter : JsonConverter +{ + public override HexColor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var raw = reader.GetString(); + if (raw is null || !HexColor.TryParse(raw, provider: null, out var result)) + { + throw new JsonException( + $"'{raw}' is not a well-formed HexColor JSON value (expected '#RGB', '#RRGGBB', or '#RRGGBBAA')."); + } + + return result; + } + + public override void Write(Utf8JsonWriter writer, HexColor value, JsonSerializerOptions options) + => writer.WriteStringValue(value.Value); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/IdempotencyKey.cs b/shared/Interfold.Shared.Contracts/Ids/IdempotencyKey.cs new file mode 100644 index 00000000..22ad86bf --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/IdempotencyKey.cs @@ -0,0 +1,46 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Strongly-typed wrapper around the client-supplied (or server-minted) idempotency +/// key that scopes command deduplication. +[JsonConverter(typeof(IdempotencyKeyJsonConverter))] +public readonly record struct IdempotencyKey : IParsable +{ + public string Value { get; } + + public IdempotencyKey(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator IdempotencyKey(string value) => new(value); + + public static implicit operator string(IdempotencyKey value) => value.Value; + + public override string ToString() => Value; + + public static IdempotencyKey Parse(string s, IFormatProvider? provider) => new(s); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out IdempotencyKey result) + { + if (s is null) + { + result = default; + return false; + } + + result = new IdempotencyKey(s); + return true; + } +} + +internal sealed class IdempotencyKeyJsonConverter : StringBackedJsonConverter +{ + protected override IdempotencyKey Create(string value) => new(value); + protected override string GetValue(IdempotencyKey value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/IdentityWrappers.cs b/shared/Interfold.Shared.Contracts/Ids/IdentityWrappers.cs new file mode 100644 index 00000000..6115fae6 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/IdentityWrappers.cs @@ -0,0 +1,120 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// A system's public display/handle name. Public value — ToString() returns the raw +/// string (unlike the PII wrappers below). JSON serializes as the raw string, so read models, +/// socket payloads, and the persisted UpdateUsernameCommand payload hashes are unchanged. +/// +[JsonConverter(typeof(UsernameJsonConverter))] +public readonly record struct Username +{ + public string Value { get; } + + public Username(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator Username(string value) => new(value); + + // Safe implicit widen — Username is public data, unlike the PII wrappers below. + public static implicit operator string(Username value) => value.Value; + + public override string ToString() => Value; +} + +internal sealed class UsernameJsonConverter : JsonConverter +{ + // JSON null → empty so the command handler's username_invalid path fires as it did + // when the property was a nullable string. + public override bool HandleNull => true; + + public override Username Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.TokenType == JsonTokenType.Null ? string.Empty : reader.GetString() ?? string.Empty); + + public override void Write(Utf8JsonWriter writer, Username value, JsonSerializerOptions options) + => writer.WriteStringValue(value.Value); +} + +/// +/// A linked Discord account's snowflake id. PII — ToString() redacts (see +/// ); JSON serializes as the raw string so the +/// discord_account_linked socket payload and friend-profile bodies are unchanged. +/// +[JsonConverter(typeof(DiscordIdJsonConverter))] +public readonly record struct DiscordId +{ + public string Value { get; } + + public DiscordId(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + // No implicit widen by design: ToString() redacts, an implicit string operator + // would silently defeat that under any Foo(string) overload. Use .Value at + // persistence / DB / HTTP boundaries — explicit unwrap is greppable and audit-friendly. + public static explicit operator DiscordId(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class DiscordIdJsonConverter : StringBackedJsonConverter +{ + protected override DiscordId Create(string value) => new(value); + protected override string GetValue(DiscordId value) => value.Value; +} + +/// +/// A linked account email address (Google links resolve to email). PII — ToString() +/// redacts; JSON serializes as the raw string. +/// +[JsonConverter(typeof(EmailJsonConverter))] +public readonly record struct Email +{ + public string Value { get; } + + public Email(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator Email(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class EmailJsonConverter : StringBackedJsonConverter +{ + protected override Email Create(string value) => new(value); + protected override string GetValue(Email value) => value.Value; +} + +/// +/// A linked Apple account's stable subject identifier (the sub claim from Sign in with +/// Apple; stored in the apple_id column). PII — ToString() redacts; JSON +/// serializes as the raw string. +/// +[JsonConverter(typeof(AppleIdJsonConverter))] +public readonly record struct AppleId +{ + public string Value { get; } + + public AppleId(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator AppleId(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class AppleIdJsonConverter : StringBackedJsonConverter +{ + protected override AppleId Create(string value) => new(value); + protected override string GetValue(AppleId value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/OperationId.cs b/shared/Interfold.Shared.Contracts/Ids/OperationId.cs new file mode 100644 index 00000000..51fc6fa4 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/OperationId.cs @@ -0,0 +1,47 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Strongly-typed wrapper around the dotted command/query operation identifier +/// (e.g. "cmd.alter.create"). Carried on CommandEnvelope<T>.OperationId, +/// ConflictResult.OperationId, and idempotency store keys. +[JsonConverter(typeof(OperationIdJsonConverter))] +public readonly record struct OperationId : IParsable +{ + public string Value { get; } + + public OperationId(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator OperationId(string value) => new(value); + + public static implicit operator string(OperationId value) => value.Value; + + public override string ToString() => Value; + + public static OperationId Parse(string s, IFormatProvider? provider) => new(s); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out OperationId result) + { + if (s is null) + { + result = default; + return false; + } + + result = new OperationId(s); + return true; + } +} + +internal sealed class OperationIdJsonConverter : StringBackedJsonConverter +{ + protected override OperationId Create(string value) => new(value); + protected override string GetValue(OperationId value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/ScopedSystemId.cs b/shared/Interfold.Shared.Contracts/Ids/ScopedSystemId.cs new file mode 100644 index 00000000..18a8bcaa --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/ScopedSystemId.cs @@ -0,0 +1,186 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Shared.Contracts.Ids; + +/// A guaranteed to be in the scoped {region}:{rawId} +/// wire form. Compose is idempotent (existing prefix stripped before re-applying), so +/// "nam:nam:abcdefg" is unconstructible through this type. +[JsonConverter(typeof(ScopedSystemIdJsonConverter))] +public readonly record struct ScopedSystemId : IParsable +{ + /// Canonical wire form, e.g. "nam:abcdefg". + public string Value { get; } + + /// Bare id with region prefix stripped, e.g. "abcdefg". + public string RawId { get; } + + /// Home region — matches the prefix on . + public ScyllaKeyspace Region { get; } + + private ScopedSystemId(string value, string rawId, ScyllaKeyspace region) + { + Value = value; + RawId = rawId; + Region = region; + } + + /// Compose from a resolved region and possibly-scoped id string. Any existing + /// region prefix is stripped first (Compose is idempotent). + /// is null. + /// is blank after strip. + public static ScopedSystemId Compose(ScyllaKeyspace region, string maybeScoped) + { + ArgumentNullException.ThrowIfNull(maybeScoped); + var raw = StripRegionPrefix(maybeScoped); + if (string.IsNullOrWhiteSpace(raw)) + { + throw new ArgumentException("Cannot compose a ScopedSystemId from a blank id.", nameof(maybeScoped)); + } + + var regionWire = region.ToWire(); + return new ScopedSystemId($"{regionWire}:{raw}", raw, region); + } + + /// Compose from a resolved region and typed . + public static ScopedSystemId Compose(ScyllaKeyspace region, SystemId systemId) + => Compose(region, systemId.Value); + + /// Compose from a wire-form region tag. Unknown tags throw. + /// is not a recognised region tag. + public static ScopedSystemId Compose(string regionWire, string maybeScoped) + { + if (!TryParseRegion(regionWire, out var region)) + { + throw new ArgumentException( + $"'{regionWire}' is not a recognised region wire tag (expected one of nam, eur, sam, sas, eas, ocn, gdpr).", + nameof(regionWire)); + } + return Compose(region, maybeScoped); + } + + /// Strict parse for trust boundaries (JWT sub, incoming events). + /// Input is unscoped, blank, or the region tag is unknown. + public static ScopedSystemId ParseScoped(string wire) + { + if (!TryParseScoped(wire, out var result)) + { + throw new ArgumentException( + $"'{wire}' is not a valid scoped system id (expected ':' with region in {{nam, eur, sam, sas, eas, ocn, gdpr}}).", + nameof(wire)); + } + return result; + } + + /// Tolerant parse: returns false (never throws) for anything invalid. + /// Used by read-side callers tolerating legacy shapes. + public static bool TryParseScoped([NotNullWhen(true)] string? wire, out ScopedSystemId result) + { + result = default; + if (string.IsNullOrWhiteSpace(wire)) + { + return false; + } + + var separator = wire.IndexOf(':'); + if (separator <= 0 || separator >= wire.Length - 1) + { + return false; + } + + var regionRaw = wire[..separator]; + var raw = wire[(separator + 1)..]; + if (!TryParseRegion(regionRaw, out var region)) + { + return false; + } + + // Canonicalise casing — ScyllaKeyspace wire values are lowercase and every + // read-side caller compares against that. + result = new ScopedSystemId($"{region.ToWire()}:{raw}", raw, region); + return true; + } + + /// Return this scoped id as an unmarked . + public SystemId AsSystemId() => new(Value); + + /// Semantic "same user" test — correct primitive for controller self-request / + /// self-friendship guards. Canonicalises candidate so raw and same-region-scoped both + /// self-reject; cross-region candidates compare scoped-to-scoped byte-identical. + public bool RepresentsSameUserAs(SystemId candidate) + { + if (string.IsNullOrWhiteSpace(candidate.Value)) + { + return false; + } + + if (TryParseScoped(candidate.Value, out var scopedCandidate)) + { + return string.Equals(Value, scopedCandidate.Value, StringComparison.Ordinal); + } + + return string.Equals(RawId, candidate.Value, StringComparison.Ordinal); + } + + // Byte-identical implicit widen so persistence adapters can keep declaring SystemId + // parameters while ingress tightens to ScopedSystemId. + public static implicit operator SystemId(ScopedSystemId scoped) => scoped.AsSystemId(); + + public override string ToString() => Value; + + // IParsable — [FromRoute] binding uses strict-parse; unscoped path segments 400. + public static ScopedSystemId Parse(string s, IFormatProvider? provider) => ParseScoped(s); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out ScopedSystemId result) + => TryParseScoped(s, out result); + + public static string StripRegionPrefix(SystemId systemId) => StripRegionPrefix(systemId.Value); + + /// Strip a leading recognised {region}: prefix once; unrecognised prefixes + /// (e.g. "username:") return unchanged. Single-pass: a double-prefixed + /// "nam:nam:abcdefg" yields "nam:abcdefg". + public static string StripRegionPrefix(string maybeScoped) + { + if (string.IsNullOrWhiteSpace(maybeScoped)) + { + return maybeScoped; + } + + var separator = maybeScoped.IndexOf(':'); + if (separator <= 0) + { + return maybeScoped; + } + + if (!TryParseRegion(maybeScoped[..separator], out _)) + { + return maybeScoped; + } + + return maybeScoped[(separator + 1)..]; + } + + // Case-insensitive region-tag lookup; differs from ParseScyllaKeyspace by dropping the + // "blank defaults to Nam" fallback (blank here means "no prefix at all"). + internal static bool TryParseRegion(string raw, out ScyllaKeyspace region) + => raw.TryParseWire(out region); +} + +// Emits and reads the raw ScopedSystemId.Value string — byte-identical to +// SystemIdJsonConverter so a declared-type migration is zero wire change. +internal sealed class ScopedSystemIdJsonConverter : JsonConverter +{ + public override ScopedSystemId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var raw = reader.GetString(); + return ScopedSystemId.ParseScoped(raw ?? string.Empty); + } + + public override void Write(Utf8JsonWriter writer, ScopedSystemId value, JsonSerializerOptions options) + => writer.WriteStringValue(value.Value); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/SecretTokens.cs b/shared/Interfold.Shared.Contracts/Ids/SecretTokens.cs new file mode 100644 index 00000000..ceb30ae3 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/SecretTokens.cs @@ -0,0 +1,9 @@ +namespace Interfold.Shared.Contracts.Ids; + +// Wrapper ToString() calls this so accidental interpolation cannot leak the raw value; +// use .Value at serialization / DB / HTTP boundaries. +public static class SecretRedaction +{ + public static string Redact(string value) + => value.Length <= 4 ? "…" : $"{value[..4]}…"; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/StringBackedIds.cs b/shared/Interfold.Shared.Contracts/Ids/StringBackedIds.cs new file mode 100644 index 00000000..ca7b20fd --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/StringBackedIds.cs @@ -0,0 +1,64 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +// Guid-backed entity IDs: readonly record struct wrapping Guid, JSON emits the compact +// 32-char lowercase hex form (Guid.ToString("N")). Read accepts both "N" and hyphenated +// via UuidString.TryParse. + +// PollId + PollIdJsonConverter live in Interfold.Polls.Contracts/Ids/PollId.cs. +// EntryId + EntryIdJsonConverter live in Interfold.Journals.Contracts/Ids/EntryId.cs. +// TagId + TagIdJsonConverter live in Interfold.Tags.Contracts/Ids/TagId.cs. +// FrontId + FrontIdJsonConverter live in Interfold.Fronting.Contracts/Ids/FrontId.cs. + +/// Strongly-typed wrapper around a settings-field id (Guid, wire form is 32-char lowercase hex). +[JsonConverter(typeof(FieldIdJsonConverter))] +public readonly record struct FieldId(Guid Value) : IParsable +{ + public static readonly FieldId Empty = new(Guid.Empty); + + public static explicit operator FieldId(Guid value) => new(value); + public static implicit operator Guid(FieldId value) => value.Value; + + public override string ToString() => Value.ToString("N"); + + public static FieldId Parse(string s, IFormatProvider? provider) + => UuidString.TryParse(s, out var g) + ? new(g) + : throw new FormatException($"Value '{s}' is not a valid FieldId."); + + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out FieldId result) + { + if (s is null || !UuidString.TryParse(s, out var g)) { result = default; return false; } + result = new FieldId(g); + return true; + } +} + +// Common JSON converter shape for the Guid-backed IDs above. Writes 32-char lowercase +// hex; reads accept both "N" and hyphenated forms. Concrete stubs are named individually +// because [JsonConverter(typeof(...))] requires a concrete class name. Public so +// per-feature Contracts projects can derive their own concrete stubs. +public abstract class GuidIdJsonConverter : JsonConverter +{ + protected abstract T Create(Guid value); + protected abstract Guid GetValue(T value); + protected abstract string TypeLabel { get; } + + public sealed override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => UuidString.TryParse(reader.GetString() ?? string.Empty, out var g) + ? Create(g) + : throw new JsonException($"{TypeLabel} JSON value must be a Guid string (compact or hyphenated)."); + + public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) + => writer.WriteStringValue(GetValue(value).ToString("N")); +} + +internal sealed class FieldIdJsonConverter : GuidIdJsonConverter +{ + protected override FieldId Create(Guid value) => new(value); + protected override Guid GetValue(FieldId value) => value.Value; + protected override string TypeLabel => nameof(FieldId); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/StringBackedJsonConverter.cs b/shared/Interfold.Shared.Contracts/Ids/StringBackedJsonConverter.cs new file mode 100644 index 00000000..94d2ca4d --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/StringBackedJsonConverter.cs @@ -0,0 +1,21 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +// Common JSON converter shape for every string-backed wrapper struct — raw string in, +// raw string out (redaction lives on each struct's own ToString override). Read is +// null-tolerant (null → empty). Concrete stubs are sealed and named individually because +// [JsonConverter(typeof(...))] requires a concrete class name. Public so per-feature +// Contracts projects can derive their own concrete stubs. +public abstract class StringBackedJsonConverter : JsonConverter +{ + protected abstract T Create(string value); + protected abstract string GetValue(T value); + + public sealed override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => Create(reader.GetString() ?? string.Empty); + + public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) + => writer.WriteStringValue(GetValue(value)); +} diff --git a/shared/Interfold.Shared.Contracts/Ids/SystemId.cs b/shared/Interfold.Shared.Contracts/Ids/SystemId.cs new file mode 100644 index 00000000..d4edf98f --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/SystemId.cs @@ -0,0 +1,47 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// Strongly-typed wrapper around the 7-character alphanumeric system ID on the wire. +[JsonConverter(typeof(SystemIdJsonConverter))] +public readonly record struct SystemId : IParsable +{ + public string Value { get; } + + public SystemId(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator SystemId(string value) => new(value); + + // Safe implicit widen — SystemId is a public identifier, ToString() returns Value verbatim. + // Hazard: default(SystemId).Value is null; do not widen a default slot. + public static implicit operator string(SystemId value) => value.Value; + + public override string ToString() => Value; + + public static SystemId Parse(string s, IFormatProvider? provider) => new(s); + + public static bool TryParse( + [NotNullWhen(true)] string? s, + IFormatProvider? provider, + [MaybeNullWhen(false)] out SystemId result) + { + if (s is null) + { + result = default; + return false; + } + + result = new SystemId(s); + return true; + } +} + +internal sealed class SystemIdJsonConverter : StringBackedJsonConverter +{ + protected override SystemId Create(string value) => new(value); + protected override string GetValue(SystemId value) => value.Value; +} diff --git a/shared/Interfold.Shared.Contracts/Ids/UuidString.cs b/shared/Interfold.Shared.Contracts/Ids/UuidString.cs new file mode 100644 index 00000000..dbe7495b --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Ids/UuidString.cs @@ -0,0 +1,16 @@ +namespace Interfold.Shared.Contracts.Ids; + +/// Parse helper for compact-N-canonical wrapper ids: accepts both "N" and +/// hyphenated GUID spellings. +public static class UuidString +{ + public static bool TryParse(string value, out Guid guid) + { + if (Guid.TryParseExact(value, "N", out guid)) + { + return true; + } + + return Guid.TryParse(value, out guid); + } +} diff --git a/shared/Interfold.Shared.Contracts/Interfold.Shared.Contracts.csproj b/shared/Interfold.Shared.Contracts/Interfold.Shared.Contracts.csproj new file mode 100644 index 00000000..6c3a8871 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Interfold.Shared.Contracts.csproj @@ -0,0 +1,7 @@ + + + net10.0 + enable + enable + + diff --git a/shared/Interfold.Shared.Contracts/InterfoldContractVersions.cs b/shared/Interfold.Shared.Contracts/InterfoldContractVersions.cs new file mode 100644 index 00000000..815cd9fd --- /dev/null +++ b/shared/Interfold.Shared.Contracts/InterfoldContractVersions.cs @@ -0,0 +1,13 @@ +namespace Interfold.Shared.Contracts; + +/// +/// The API contract version stamped on every response via +/// . Wire-frozen per version — clients pin against +/// the exact spelling; bump by adding a new constant and moving . +/// +public static class InterfoldContractVersions +{ + public const string V2026_03_V1 = "2026-03-v1"; + + public const string Current = V2026_03_V1; +} diff --git a/shared/Interfold.Shared.Contracts/InterfoldException.cs b/shared/Interfold.Shared.Contracts/InterfoldException.cs new file mode 100644 index 00000000..f4a3d185 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/InterfoldException.cs @@ -0,0 +1,22 @@ +using System.Net; + +namespace Interfold.Shared.Contracts; + +public class InterfoldException : Exception +{ + public InterfoldException(string message, ErrorCode code, HttpStatusCode? httpStatusCode = null) : base(message) + { + Code = code; + HttpStatusCode = httpStatusCode; + } + + public InterfoldException(string message, ErrorCode code, Exception innerException, HttpStatusCode? httpStatusCode = null) : base(message, innerException) + { + Code = code; + HttpStatusCode = httpStatusCode; + } + + public ErrorCode Code { get; } + + public HttpStatusCode? HttpStatusCode { get; set; } +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/InterfoldHeaders.cs b/shared/Interfold.Shared.Contracts/InterfoldHeaders.cs new file mode 100644 index 00000000..e3a87f66 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/InterfoldHeaders.cs @@ -0,0 +1,46 @@ +namespace Interfold.Shared.Contracts; + +/// +/// The custom HTTP header names the API reads and writes. Spellings are frozen — the +/// Kotlin client and the socket endpoint relay depend on them. +/// +public static class InterfoldHeaders +{ + public const string OperationId = "X-Interfold-OperationId"; + public const string IdempotencyKey = "X-Interfold-Idempotency-Key"; + public const string CommandId = "X-Interfold-Command-Id"; + public const string Contract = "X-Interfold-Contract"; + public const string RequestId = "X-Interfold-Request-Id"; + + /// + /// Generic proxy-supplied correlation header ACCEPTED on inbound requests. Deliberately + /// asymmetric with : reverse proxies and load balancers stamp the + /// generic spelling, while responses echo the branded one. + /// + public const string InboundRequestId = "X-Request-Id"; + public const string Principal = "X-Interfold-Principal"; +} + +/// +/// Cookie names used by the OAuth login/link flows. Frozen — in-flight OAuth round-trips +/// span deployments, so a rename would strand users mid-flow. +/// +public static class InterfoldCookieNames +{ + public const string AuthRedirectUri = "octocon_auth_redirect_uri"; + public const string LinkToken = "octocon_link_token"; + public const string LinkRedirectUri = "octocon_link_redirect_uri"; +} + +/// JWT claim names the auth pipeline reads from issued tokens. +public static class JwtClaimNames +{ + public const string Sub = "sub"; + public const string Jti = "jti"; +} + +/// Named HttpClient registrations. +public static class HttpClientNames +{ + public const string SimplyPlural = "SimplyPlural"; +} diff --git a/shared/Interfold.Shared.Contracts/Models/AlterPublicFieldReadModel.cs b/shared/Interfold.Shared.Contracts/Models/AlterPublicFieldReadModel.cs new file mode 100644 index 00000000..48a7dda5 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/AlterPublicFieldReadModel.cs @@ -0,0 +1,13 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Models; + +// The Id here is the settings field definition's FieldId — alter field values are +// (field definition id → value) pairs joined against SettingsFieldReadModel.Id. +// Stays on the spine (rather than moving with BareAlter/AlterReadModel to +// Interfold.Alters.Contracts) because Interfold.Friendships.Contracts binds +// IReadOnlyList on FriendFrontingAlterReadModel; hoisting +// this DTO to a feature Contracts would form a Friendships.Contracts -> Alters.Contracts +// cross-feature reference. +public sealed record AlterPublicFieldReadModel(FieldId Id, string Name, FieldType Type, string? Value); diff --git a/shared/Interfold.Shared.Contracts/Models/CommandExecutionResult.cs b/shared/Interfold.Shared.Contracts/Models/CommandExecutionResult.cs new file mode 100644 index 00000000..fe1bef22 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/CommandExecutionResult.cs @@ -0,0 +1,20 @@ +using Interfold.Shared.Contracts.Operations; + +namespace Interfold.Shared.Contracts.Models; + +public sealed class CommandExecutionResult +{ + private CommandExecutionResult(TResult? result, ConflictResult? conflict, bool accepted) + { + Result = result; + Conflict = conflict; + Accepted = accepted; + } + + public TResult? Result { get; } + public ConflictResult? Conflict { get; } + public bool Accepted { get; } + + public static CommandExecutionResult Success(TResult result) => new(result, null, true); + public static CommandExecutionResult Rejected(ConflictResult conflict) => new(default, conflict, false); +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/Models/IAvatarBearing.cs b/shared/Interfold.Shared.Contracts/Models/IAvatarBearing.cs new file mode 100644 index 00000000..b6e32b6d --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/IAvatarBearing.cs @@ -0,0 +1,13 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Models; + +/// Marker for wire models carrying the persisted (avatar_url, avatar_source) +/// pair. AvatarUrlQualifier reads both off the bearing to rewrite Local URLs to +/// absolute server-origin ones before responding. +public interface IAvatarBearing +{ + AvatarUrl? AvatarUrl { get; } + AvatarSource? AvatarSource { get; } +} diff --git a/shared/Interfold.Shared.Contracts/Models/IdempotencyMatch.cs b/shared/Interfold.Shared.Contracts/Models/IdempotencyMatch.cs new file mode 100644 index 00000000..cfbec3b4 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/IdempotencyMatch.cs @@ -0,0 +1,3 @@ +namespace Interfold.Shared.Contracts.Models; + +public sealed record IdempotencyMatch(string PayloadHash, string OutcomeHash, string? OutcomePayload); \ No newline at end of file diff --git a/shared/Interfold.Shared.Contracts/Models/Read/AccountPublicProfileReadModel.cs b/shared/Interfold.Shared.Contracts/Models/Read/AccountPublicProfileReadModel.cs new file mode 100644 index 00000000..7d493f25 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/Read/AccountPublicProfileReadModel.cs @@ -0,0 +1,23 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Models.Read; + +public sealed record AccountPublicProfileReadModel( + SystemId SystemId, + Username? Username, + string? Description, + AvatarUrl? AvatarUrl, + AvatarSource? AvatarSource, + DiscordId? DiscordId, + Email? Email, + AppleId? AppleId +) : IAvatarBearing; + +public enum AccountLinkResult +{ + Success, + AlreadyLinked, + UserExists, + UserNotFound +} diff --git a/shared/Interfold.Shared.Contracts/Models/Read/SettingsFieldReadModel.cs b/shared/Interfold.Shared.Contracts/Models/Read/SettingsFieldReadModel.cs new file mode 100644 index 00000000..0a933340 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/Read/SettingsFieldReadModel.cs @@ -0,0 +1,20 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Models.Read; + +// SettingsFieldReadModel stays on spine — pinned by Interfold.Alters.Domain.AlterFieldProjection. +// AvatarUrlUploadRequest stays on spine — bound by both AltersController and SettingsController; +// hoisting to either feature Contracts would introduce a cross-feature reference. +public sealed record SettingsFieldReadModel( + FieldId Id, + string Name, + FieldType Type, + VisibilityLevel SecurityLevel, + bool Locked, + int Index, + DateTime? InsertedAt); + +public sealed record AvatarUrlUploadRequest( + AvatarUrl Url +); diff --git a/shared/Interfold.Shared.Contracts/Models/VisibilityLevelExtensions.cs b/shared/Interfold.Shared.Contracts/Models/VisibilityLevelExtensions.cs new file mode 100644 index 00000000..cfcbaed8 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Models/VisibilityLevelExtensions.cs @@ -0,0 +1,26 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Shared.Contracts.Models; + +// Viewer-gating helper for VisibilityLevel. Persistence writes (short)value; +// reads go through FromStorage so a null/unknown code cannot abort owner list/join. +// Unknown maps to Private — fail closed for non-owners. +public static class VisibilityLevelExtensions +{ + /// Scylla rehydration. Null or unknown → . + public static VisibilityLevel FromStorage(this short? code) + => code.FromCode(VisibilityLevel.Private); + + public static VisibilityLevel FromStorage(this short code) + => FromStorage((short?)code); + + /// Fail-closed viewer gate. Null friendship = not a friend. + public static bool CanBeViewedBy(this VisibilityLevel visibilityLevel, FriendshipLevel? friendshipLevel) + => visibilityLevel switch + { + VisibilityLevel.Public => true, + VisibilityLevel.FriendsOnly => friendshipLevel is FriendshipLevel.Friend or FriendshipLevel.TrustedFriend, + VisibilityLevel.TrustedOnly => friendshipLevel is FriendshipLevel.TrustedFriend, + _ => false, + }; +} diff --git a/shared/Interfold.Shared.Contracts/OperationResults.cs b/shared/Interfold.Shared.Contracts/OperationResults.cs new file mode 100644 index 00000000..f8e7f5ab --- /dev/null +++ b/shared/Interfold.Shared.Contracts/OperationResults.cs @@ -0,0 +1,11 @@ +namespace Interfold.Shared.Contracts; + +// AccountCommandResult + SettingsCommandResult + SettingsFieldCommandResult + +// EncryptionCommandResult + ImportDispatchCommandResult live in Interfold.Settings.Contracts +// (Phase-3 Settings migration; travel with the owning commands). +// AlterCommandResult lives in Interfold.Alters.Contracts (Phase-3 migration). +// FrontCommandResult lives in Interfold.Fronting.Contracts (Phase-3 migration). +// TagCommandResult lives in Interfold.Tags.Contracts (Phase-3 migration). +// PollCommandResult lives in Interfold.Polls.Contracts (Phase-3 migration). +// FriendshipCommandResult lives in Interfold.Friendships.Contracts (Phase-3 migration). +// GlobalJournalCommandResult + AlterJournalCommandResult live in Interfold.Journals.Contracts (Phase-3 migration). diff --git a/shared/Interfold.Shared.Contracts/Operations/CommandEnvelope.cs b/shared/Interfold.Shared.Contracts/Operations/CommandEnvelope.cs new file mode 100644 index 00000000..9f90550d --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Operations/CommandEnvelope.cs @@ -0,0 +1,22 @@ +using System.Net; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Operations; + +/// +/// Command envelope carried through every write path. is a +/// so command handlers, event publishers, idempotency stores, +/// and the WebSocket router see a compile-time guarantee that the principal is always in +/// wire-canonical {region}:{rawId} form. The middleware performs the one strict +/// parse of the JWT sub claim on ingress; every downstream hop uses +/// at the storage boundary and never re-parses. +/// +public sealed record CommandEnvelope( + OperationId OperationId, + Guid CommandId, + ScopedSystemId PrincipalId, + IdempotencyKey IdempotencyKey, + DateTimeOffset? OccurredAt, + TPayload Payload, + HttpStatusCode? SuccessStatusCode = null +); diff --git a/shared/Interfold.Shared.Contracts/Operations/ConflictResult.cs b/shared/Interfold.Shared.Contracts/Operations/ConflictResult.cs new file mode 100644 index 00000000..105ea83a --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Operations/ConflictResult.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Operations; + +public enum ConflictCode +{ + ConflictDuplicate, + ConflictInvariant +} + +public sealed record ConflictResult( + ConflictCode Code, + OperationId OperationId, + EntityRef EntityRef, + ResolutionHint ResolutionHint +); diff --git a/shared/Interfold.Shared.Contracts/Operations/EntityRefs.cs b/shared/Interfold.Shared.Contracts/Operations/EntityRefs.cs new file mode 100644 index 00000000..27ba97c7 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Operations/EntityRefs.cs @@ -0,0 +1,160 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Operations; + +/// +/// Central registry of every conflict entity-reference the Domain handlers emit, +/// mirroring the pattern. The string values are frozen — +/// they surface verbatim as ErrorResponse.EntityRef on 409/422 responses and +/// integration tests assert the exact spellings. +/// +public static class EntityRefs +{ + // --- auth --- + public static readonly EntityRef AuthLoginFailed = new("auth:login_failed"); + public static readonly EntityRef AuthLinkInvalidToken = new("auth:link_invalid_token"); + public static readonly EntityRef AuthLinkFailed = new("auth:link_failed"); + + // --- account --- + public static readonly EntityRef AccountUsernameInvalid = new("account:username_invalid"); + public static readonly EntityRef AccountUsernameTooLong = new("account:username_too_long"); + public static readonly EntityRef AccountUsernameUpdate = new("account:username_update"); + public static readonly EntityRef AccountUsernameUpdateFailed = new("account:username_update_failed"); + + // --- alter --- + public static readonly EntityRef AlterAliasTaken = new("alter:alias_taken"); + public static readonly EntityRef AlterAvatarSourceRequired = new("alter:avatar_source_required"); + public static readonly EntityRef AlterCreate = new("alter:create"); + public static readonly EntityRef AlterDelete = new("alter:delete"); + public static readonly EntityRef AlterDeleteFailed = new("alter:delete_failed"); + public static readonly EntityRef AlterId = new("alter:id"); + public static readonly EntityRef AlterName = new("alter:name"); + public static readonly EntityRef AlterNotFound = new("alter:not_found"); + public static readonly EntityRef AlterUpdate = new("alter:update"); + public static readonly EntityRef AlterUpdateNoFields = new("alter:update:no_fields"); + public static readonly EntityRef AlterUpdateFailed = new("alter:update_failed"); + + // --- friend_request --- + public static readonly EntityRef FriendRequestAccept = new("friend_request:accept"); + public static readonly EntityRef FriendRequestAlreadyFriends = new("friend_request:already_friends"); + public static readonly EntityRef FriendRequestAlreadySent = new("friend_request:already_sent"); + public static readonly EntityRef FriendRequestCancel = new("friend_request:cancel"); + public static readonly EntityRef FriendRequestNoUser = new("friend_request:no_user"); + public static readonly EntityRef FriendRequestNotRequested = new("friend_request:not_requested"); + public static readonly EntityRef FriendRequestReject = new("friend_request:reject"); + public static readonly EntityRef FriendRequestSend = new("friend_request:send"); + + // --- friendship --- + public static readonly EntityRef FriendshipNotFound = new("friendship:not_found"); + public static readonly EntityRef FriendshipRemove = new("friendship:remove"); + public static readonly EntityRef FriendshipTrust = new("friendship:trust"); + + // --- fronting --- + public static readonly EntityRef FrontingAlreadyFronting = new("fronting:already_fronting"); + public static readonly EntityRef FrontingBulkUpdate = new("fronting:bulk_update"); + public static readonly EntityRef FrontingDelete = new("fronting:delete"); + public static readonly EntityRef FrontingDeleteFailed = new("fronting:delete_failed"); + public static readonly EntityRef FrontingEnd = new("fronting:end"); + public static readonly EntityRef FrontingEndFailed = new("fronting:end_failed"); + public static readonly EntityRef FrontingInvalidAlterId = new("fronting:invalid_alter_id"); + public static readonly EntityRef FrontingInvalidComment = new("fronting:invalid_comment"); + public static readonly EntityRef FrontingInvalidFrontId = new("fronting:invalid_front_id"); + public static readonly EntityRef FrontingNoFront = new("fronting:no_front"); + public static readonly EntityRef FrontingNotFronting = new("fronting:not_fronting"); + public static readonly EntityRef FrontingPrimary = new("fronting:primary"); + public static readonly EntityRef FrontingPrimaryFailed = new("fronting:primary_failed"); + public static readonly EntityRef FrontingSet = new("fronting:set"); + public static readonly EntityRef FrontingStart = new("fronting:start"); + public static readonly EntityRef FrontingStartFailed = new("fronting:start_failed"); + public static readonly EntityRef FrontingUpdateComment = new("fronting:update_comment"); + public static readonly EntityRef FrontingUpdateCommentFailed = new("fronting:update_comment_failed"); + + // --- journal --- + public static readonly EntityRef JournalAlterCreate = new("journal:alter:create"); + public static readonly EntityRef JournalAlterDelete = new("journal:alter:delete"); + public static readonly EntityRef JournalAlterSetLocked = new("journal:alter:set_locked"); + public static readonly EntityRef JournalAlterSetPinned = new("journal:alter:set_pinned"); + public static readonly EntityRef JournalAlterUpdate = new("journal:alter:update"); + public static readonly EntityRef JournalAlterNotFound = new("journal:alter_not_found"); + public static readonly EntityRef JournalAttachFailed = new("journal:attach_failed"); + public static readonly EntityRef JournalContentTooLong = new("journal:content_too_long"); + public static readonly EntityRef JournalCreateFailed = new("journal:create_failed"); + public static readonly EntityRef JournalDeleteFailed = new("journal:delete_failed"); + public static readonly EntityRef JournalDetachFailed = new("journal:detach_failed"); + public static readonly EntityRef JournalGlobalAttachAlter = new("journal:global:attach_alter"); + public static readonly EntityRef JournalGlobalCreate = new("journal:global:create"); + public static readonly EntityRef JournalGlobalDelete = new("journal:global:delete"); + public static readonly EntityRef JournalGlobalDetachAlter = new("journal:global:detach_alter"); + public static readonly EntityRef JournalGlobalSetLocked = new("journal:global:set_locked"); + public static readonly EntityRef JournalGlobalSetPinned = new("journal:global:set_pinned"); + public static readonly EntityRef JournalGlobalUpdate = new("journal:global:update"); + public static readonly EntityRef JournalNoFields = new("journal:no_fields"); + public static readonly EntityRef JournalNotFound = new("journal:not_found"); + public static readonly EntityRef JournalTitleRequired = new("journal:title_required"); + public static readonly EntityRef JournalTitleTooLong = new("journal:title_too_long"); + public static readonly EntityRef JournalUpdateFailed = new("journal:update_failed"); + + // --- poll --- + public static readonly EntityRef PollCreate = new("poll:create"); + public static readonly EntityRef PollCreateFailed = new("poll:create_failed"); + public static readonly EntityRef PollDelete = new("poll:delete"); + public static readonly EntityRef PollDeleteFailed = new("poll:delete_failed"); + public static readonly EntityRef PollDescriptionTooLong = new("poll:description_too_long"); + public static readonly EntityRef PollNoFields = new("poll:no_fields"); + public static readonly EntityRef PollNotFound = new("poll:not_found"); + public static readonly EntityRef PollTitleRequired = new("poll:title_required"); + public static readonly EntityRef PollTitleTooLong = new("poll:title_too_long"); + public static readonly EntityRef PollUpdate = new("poll:update"); + public static readonly EntityRef PollUpdateFailed = new("poll:update_failed"); + + // --- settings --- + public static readonly EntityRef SettingsAccountDelete = new("settings:account:delete"); + public static readonly EntityRef SettingsAltersWipe = new("settings:alters:wipe"); + public static readonly EntityRef SettingsAvatarDelete = new("settings:avatar:delete"); + public static readonly EntityRef SettingsAvatarUpload = new("settings:avatar:upload"); + public static readonly EntityRef SettingsAvatarInvalid = new("settings:avatar_invalid"); + public static readonly EntityRef SettingsDescriptionUpdate = new("settings:description:update"); + public static readonly EntityRef SettingsDescriptionInvalid = new("settings:description_invalid"); + public static readonly EntityRef SettingsDescriptionUpdateFailed = new("settings:description_update_failed"); + public static readonly EntityRef SettingsEncryptionRecover = new("settings:encryption:recover"); + public static readonly EntityRef SettingsEncryptionReset = new("settings:encryption:reset"); + public static readonly EntityRef SettingsEncryptionSetup = new("settings:encryption:setup"); + public static readonly EntityRef SettingsEncryptionNotInitialized = new("settings:encryption_not_initialized"); + public static readonly EntityRef SettingsEncryptionResetFailed = new("settings:encryption_reset_failed"); + public static readonly EntityRef SettingsEncryptionSetupFailed = new("settings:encryption_setup_failed"); + public static readonly EntityRef SettingsFieldCreate = new("settings:field:create"); + public static readonly EntityRef SettingsFieldCreateFailed = new("settings:field:create_failed"); + public static readonly EntityRef SettingsFieldDelete = new("settings:field:delete"); + public static readonly EntityRef SettingsFieldRelocate = new("settings:field:relocate"); + public static readonly EntityRef SettingsFieldUpdate = new("settings:field:update"); + public static readonly EntityRef SettingsFieldNameRequired = new("settings:field_name_required"); + public static readonly EntityRef SettingsImportPkInvalid = new("settings:import_pk_invalid"); + public static readonly EntityRef SettingsImportSpInvalid = new("settings:import_sp_invalid"); + public static readonly EntityRef SettingsInvalidRecoveryCode = new("settings:invalid_recovery_code"); + public static readonly EntityRef SettingsPushTokenAdd = new("settings:push_token:add"); + public static readonly EntityRef SettingsPushTokenRemove = new("settings:push_token:remove"); + public static readonly EntityRef SettingsPushTokenAddFailed = new("settings:push_token_add_failed"); + public static readonly EntityRef SettingsPushTokenInvalid = new("settings:push_token_invalid"); + public static readonly EntityRef SettingsRecoveryCodeInvalid = new("settings:recovery_code_invalid"); + public static readonly EntityRef SettingsTagsWipe = new("settings:tags:wipe"); + public static readonly EntityRef SettingsUnlinkApple = new("settings:unlink:apple"); + public static readonly EntityRef SettingsUnlinkDiscord = new("settings:unlink:discord"); + public static readonly EntityRef SettingsUnlinkEmail = new("settings:unlink:email"); + + // --- tag --- + public static readonly EntityRef TagAlterNotFound = new("tag:alter_not_found"); + public static readonly EntityRef TagAttachAlter = new("tag:attach_alter"); + public static readonly EntityRef TagCreate = new("tag:create"); + public static readonly EntityRef TagCreateFailed = new("tag:create_failed"); + public static readonly EntityRef TagCycle = new("tag:cycle"); + public static readonly EntityRef TagDelete = new("tag:delete"); + public static readonly EntityRef TagDetachAlter = new("tag:detach_alter"); + public static readonly EntityRef TagNameRequired = new("tag:name_required"); + public static readonly EntityRef TagNameTooLong = new("tag:name_too_long"); + public static readonly EntityRef TagNoFields = new("tag:no_fields"); + public static readonly EntityRef TagNotFound = new("tag:not_found"); + public static readonly EntityRef TagParentNotFound = new("tag:parent_not_found"); + public static readonly EntityRef TagRemoveParent = new("tag:remove_parent"); + public static readonly EntityRef TagSetParent = new("tag:set_parent"); + public static readonly EntityRef TagUpdate = new("tag:update"); +} diff --git a/shared/Interfold.Shared.Contracts/Operations/OperationIds.cs b/shared/Interfold.Shared.Contracts/Operations/OperationIds.cs new file mode 100644 index 00000000..929b23ba --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Operations/OperationIds.cs @@ -0,0 +1,97 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Contracts.Operations; + +public static class OperationIds +{ + // Phase B canonical – used by the HTTP API + public static readonly OperationId SettingsUsernameUpdate = new("cmd.settings.username.update"); + public static readonly OperationId SettingsDescriptionUpdate = new("cmd.settings.description.update"); + public static readonly OperationId SettingsPushTokenAdd = new("cmd.settings.push_token.add"); + public static readonly OperationId SettingsPushTokenRemove = new("cmd.settings.push_token.remove"); + public static readonly OperationId SettingsEncryptionRecover = new("cmd.settings.encryption.recover"); + public static readonly OperationId SettingsEncryptionReset = new("cmd.settings.encryption.reset"); + public static readonly OperationId SettingsLinkToken = new("qry.settings.link_token"); + public static readonly OperationId SettingsAvatarUpload = new("cmd.settings.avatar.upload"); + public static readonly OperationId SettingsAvatarDelete = new("cmd.settings.avatar.delete"); + public static readonly OperationId SettingsImportPk = new("cmd.settings.import.pk"); + public static readonly OperationId SettingsImportSp = new("cmd.settings.import.sp"); + public static readonly OperationId SettingsAuthUnlinkDiscord = new("cmd.settings.auth.unlink_discord"); + public static readonly OperationId SettingsAuthUnlinkEmail = new("cmd.settings.auth.unlink_email"); + public static readonly OperationId SettingsAuthUnlinkApple = new("cmd.settings.auth.unlink_apple"); + public static readonly OperationId SettingsAccountDelete = new("cmd.settings.account.delete"); + public static readonly OperationId SettingsAltersWipe = new("cmd.settings.alters.wipe"); + public static readonly OperationId SettingsTagsWipe = new("cmd.settings.tags.wipe"); + public static readonly OperationId SettingsFieldCreate = new("cmd.settings.field.create"); + public static readonly OperationId SettingsFieldUpdate = new("cmd.settings.field.update"); + public static readonly OperationId SettingsFieldDelete = new("cmd.settings.field.delete"); + public static readonly OperationId SettingsFieldRelocate = new("cmd.settings.field.relocate"); + public static readonly OperationId SettingsExport = new("qry.settings.export"); + + public static readonly OperationId QueryFrontMonth = new("qry.front.month"); + public static readonly OperationId QueryFrontBetween = new("qry.front.between"); + public static readonly OperationId QueryFrontGet = new("qry.front.get"); + public static readonly OperationId QuerySystemHeartbeat = new("qry.system.heartbeat"); + public static readonly OperationId QuerySystemPublicGet = new("qry.system.public.get"); + public static readonly OperationId QueryAlterPublicList = new("qry.alter.public.list"); + public static readonly OperationId QueryAlterPublicGet = new("qry.alter.public.get"); + public static readonly OperationId QueryTagPublicList = new("qry.tag.public.list"); + public static readonly OperationId QueryTagPublicGet = new("qry.tag.public.get"); + public static readonly OperationId QueryFrontPublicList = new("qry.front.public.list"); + public static readonly OperationId QuerySystemPublicBatch = new("qry.system.public.batch"); + public static readonly OperationId QueryAuthOAuthRequest = new("qry.auth.oauth.request"); + public static readonly OperationId AuthOAuthCallback = new("cmd.auth.oauth.callback"); + public static readonly OperationId AuthRevokeToken = new("cmd.auth.revoke.token"); + public static readonly OperationId QueryAuthLinkRequest = new("qry.auth.link.request"); + public static readonly OperationId AuthLinkCallback = new("cmd.auth.link.callback"); + + // Legacy CLI name retained for backward compatibility with idempotency records + public static readonly OperationId AccountUsernameUpdate = new("cmd.account.username.update"); + + public static readonly OperationId AlterCreate = new("cmd.alter.create"); + public static readonly OperationId AlterUpdate = new("cmd.alter.update"); + public static readonly OperationId AlterDelete = new("cmd.alter.delete"); + public static readonly OperationId AlterAvatarUpload = new("cmd.alter.avatar.upload"); + public static readonly OperationId AlterAvatarDelete = new("cmd.alter.avatar.delete"); + public static readonly OperationId FrontStart = new("cmd.front.start"); + public static readonly OperationId FrontEnd = new("cmd.front.end"); + public static readonly OperationId FrontBulkUpdate = new("cmd.front.bulk_update"); + public static readonly OperationId FrontSet = new("cmd.front.set"); + public static readonly OperationId FrontPrimary = new("cmd.front.primary"); + public static readonly OperationId FrontDelete = new("cmd.front.delete"); + public static readonly OperationId FrontCommentUpdate = new("cmd.front.comment.update"); + public static readonly OperationId TagCreate = new("cmd.tag.create"); + public static readonly OperationId FriendDelete = new("cmd.friend.delete"); + public static readonly OperationId FriendTrust = new("cmd.friend.trust"); + public static readonly OperationId FriendUntrust = new("cmd.friend.untrust"); + public static readonly OperationId FriendRequestSend = new("cmd.friend_request.send"); + public static readonly OperationId FriendRequestCancel = new("cmd.friend_request.cancel"); + public static readonly OperationId FriendRequestAccept = new("cmd.friend_request.accept"); + public static readonly OperationId FriendRequestReject = new("cmd.friend_request.reject"); + public static readonly OperationId TagUpdate = new("cmd.tag.update"); + public static readonly OperationId TagDelete = new("cmd.tag.delete"); + public static readonly OperationId TagAttachAlter = new("cmd.tag.attach_alter"); + public static readonly OperationId TagDetachAlter = new("cmd.tag.detach_alter"); + public static readonly OperationId TagSetParent = new("cmd.tag.set_parent"); + public static readonly OperationId TagRemoveParent = new("cmd.tag.remove_parent"); + public static readonly OperationId PollCreate = new("cmd.poll.create"); + public static readonly OperationId PollUpdate = new("cmd.poll.update"); + public static readonly OperationId PollDelete = new("cmd.poll.delete"); + public static readonly OperationId JournalGlobalCreate = new("cmd.journal.global.create"); + public static readonly OperationId JournalGlobalUpdate = new("cmd.journal.global.update"); + public static readonly OperationId JournalGlobalDelete = new("cmd.journal.global.delete"); + public static readonly OperationId JournalGlobalLock = new("cmd.journal.global.lock"); + public static readonly OperationId JournalGlobalUnlock = new("cmd.journal.global.unlock"); + public static readonly OperationId JournalGlobalPin = new("cmd.journal.global.pin"); + public static readonly OperationId JournalGlobalUnpin = new("cmd.journal.global.unpin"); + public static readonly OperationId JournalGlobalAttachAlter = new("cmd.journal.global.attach_alter"); + public static readonly OperationId JournalGlobalDetachAlter = new("cmd.journal.global.detach_alter"); + public static readonly OperationId JournalAlterCreate = new("cmd.journal.alter.create"); + public static readonly OperationId JournalAlterUpdate = new("cmd.journal.alter.update"); + public static readonly OperationId JournalAlterDelete = new("cmd.journal.alter.delete"); + public static readonly OperationId JournalAlterLock = new("cmd.journal.alter.lock"); + public static readonly OperationId JournalAlterUnlock = new("cmd.journal.alter.unlock"); + public static readonly OperationId JournalAlterPin = new("cmd.journal.alter.pin"); + public static readonly OperationId JournalAlterUnpin = new("cmd.journal.alter.unpin"); + public static readonly OperationId SettingsEncryptionSetup = new("cmd.settings.encryption.setup"); +} diff --git a/shared/Interfold.Shared.Contracts/Operations/ResolutionHint.cs b/shared/Interfold.Shared.Contracts/Operations/ResolutionHint.cs new file mode 100644 index 00000000..9a30cdf1 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Operations/ResolutionHint.cs @@ -0,0 +1,21 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Operations; + +/// +/// How a rejected command should be resolved by the caller. Carried on +/// and surfaced to clients verbatim as the +/// ErrorResponse code ("no_retry" / "manual_merge_required"), so the +/// wire spellings below are frozen. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum ResolutionHint +{ + /// Duplicate command with a different payload — replaying will never succeed. + [JsonStringEnumMemberName("no_retry")] + NoRetry, + + /// An invariant was violated; the caller must reconcile state before retrying. + [JsonStringEnumMemberName("manual_merge_required")] + ManualMergeRequired, +} diff --git a/shared/Interfold.Shared.Contracts/PatchValue.cs b/shared/Interfold.Shared.Contracts/PatchValue.cs new file mode 100644 index 00000000..64a5697f --- /dev/null +++ b/shared/Interfold.Shared.Contracts/PatchValue.cs @@ -0,0 +1,107 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts; + +public enum PatchValueState +{ + /// The property was absent from the JSON payload — leave the target unchanged. + Unset = 0, + + /// The property was an explicit JSON null — clear the target. + Null = 1, + + /// The property carried a parseable value. + Value = 2, + + /// + /// The property was present but could not be parsed as T. Captured as a state + /// (instead of letting the converter throw) so callers can return their own + /// domain-specific error body rather than MVC's generic model-binding 400. + /// + Invalid = 3 +} + +/// +/// Tri-state JSON PATCH field: distinguishes "property absent" (don't touch) from +/// "property null" (clear) from "property has a value" (set). Sparse-diff PATCH bodies +/// (e.g. the Kotlin client's poll update) rely on this distinction, which a plain +/// nullable property cannot represent. +/// +[JsonConverter(typeof(PatchValueJsonConverterFactory))] +public readonly record struct PatchValue +{ + public PatchValueState State { get; } + + /// Only meaningful when is . + public T? Value { get; } + + private PatchValue(PatchValueState state, T? value) + { + State = state; + Value = value; + } + + public static PatchValue Unset => default; + public static PatchValue OfNull() => new(PatchValueState.Null, default); + public static PatchValue Of(T value) => new(PatchValueState.Value, value); + public static PatchValue Invalid() => new(PatchValueState.Invalid, default); + + /// True when the property appeared in the payload (null, value, or invalid). + public bool IsSet => State is not PatchValueState.Unset; + + public bool IsInvalid => State is PatchValueState.Invalid; +} + +public sealed class PatchValueJsonConverterFactory : JsonConverterFactory +{ + public override bool CanConvert(Type typeToConvert) + => typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(PatchValue<>); + + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + var inner = typeToConvert.GetGenericArguments()[0]; + var converterType = typeof(PatchValueJsonConverter<>).MakeGenericType(inner); + return (JsonConverter)Activator.CreateInstance(converterType)!; + } +} + +internal sealed class PatchValueJsonConverter : JsonConverter> +{ + public override PatchValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.Null) + { + return PatchValue.OfNull(); + } + + // Materialize the whole value first so a failed parse can't leave the reader + // mid-token; parse failures become the Invalid state instead of a JsonException. + var element = JsonElement.ParseValue(ref reader); + try + { + var value = element.Deserialize(options); + return value is null ? PatchValue.OfNull() : PatchValue.Of(value); + } + catch (JsonException) + { + return PatchValue.Invalid(); + } + catch (FormatException) + { + return PatchValue.Invalid(); + } + } + + public override void Write(Utf8JsonWriter writer, PatchValue value, JsonSerializerOptions options) + { + if (value.State == PatchValueState.Value) + { + JsonSerializer.Serialize(writer, value.Value, options); + } + else + { + writer.WriteNullValue(); + } + } +} diff --git a/shared/Interfold.Shared.Contracts/PersistenceMode.cs b/shared/Interfold.Shared.Contracts/PersistenceMode.cs new file mode 100644 index 00000000..8e61604a --- /dev/null +++ b/shared/Interfold.Shared.Contracts/PersistenceMode.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts; + +/// +/// Persistence backend selection sourced from the OCTOCON_PERSISTENCE env var. Wire +/// spellings live on so a rename to any enum +/// member here does NOT silently shift the env-var / deployment contract. Non-JSON callers +/// round-trip through (write side: +/// ; read side: +/// ) — the same single +/// source of truth every other Interfold enum uses. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum PersistenceMode +{ + [JsonStringEnumMemberName("inmemory")] + InMemory, + + [JsonStringEnumMemberName("scylla-postgres")] + ScyllaPostgres, +} diff --git a/shared/Interfold.Shared.Contracts/Secrets/ISecretsStore.cs b/shared/Interfold.Shared.Contracts/Secrets/ISecretsStore.cs new file mode 100644 index 00000000..e404ed29 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Secrets/ISecretsStore.cs @@ -0,0 +1,17 @@ +namespace Interfold.Shared.Contracts.Secrets; + +public sealed record SecretEntry( + SecretsStoreKey Key, + string Value, + string CreatedBy, + DateTimeOffset CreatedAt, + DateTimeOffset UpdatedAt, + DateTimeOffset? ExpiresAt, + string? RotatedFrom); + +public interface ISecretsStore +{ + Task GetAsync(SecretsStoreKey key, CancellationToken cancellationToken = default); + Task GetRequiredAsync(SecretsStoreKey key, CancellationToken cancellationToken = default); + Task> ListAsync(CancellationToken cancellationToken = default); +} diff --git a/shared/Interfold.Shared.Contracts/Secrets/SecretsStoreKeys.cs b/shared/Interfold.Shared.Contracts/Secrets/SecretsStoreKeys.cs new file mode 100644 index 00000000..81e17cd6 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Secrets/SecretsStoreKeys.cs @@ -0,0 +1,61 @@ +namespace Interfold.Shared.Contracts.Secrets; + +// Typed row key for `internal.secrets`. Struct + registry share this file because it's +// linked into DatabaseBootstrap; unwrap Value at DB parameters and dictionary keys. +#if INTERFOLD_DBBOOT_LINKED +internal +#else +public +#endif +readonly record struct SecretsStoreKey +{ + public string Value { get; } + + public SecretsStoreKey(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public override string ToString() => Value; +} + +/// Well-known row keys in internal.secrets. Persisted wire values — +/// changing any string here is a breaking schema change for existing deployments. +#if INTERFOLD_DBBOOT_LINKED +internal +#else +public +#endif +static class SecretsStoreKeys +{ + public static readonly SecretsStoreKey OAuthGoogleClientSecret = new("oauth:google:client_secret"); + public static readonly SecretsStoreKey OAuthDiscordClientSecret = new("oauth:discord:client_secret"); + public static readonly SecretsStoreKey OAuthAppleClientSecret = new("oauth:apple:client_secret"); + + public static readonly SecretsStoreKey EncryptionPepper = new("encryption:pepper"); + + public static readonly SecretsStoreKey PostgresAdminUsername = new("postgres:admin_username"); + public static readonly SecretsStoreKey PostgresAdminPassword = new("postgres:admin_password"); + + public static readonly SecretsStoreKey ScyllaAdminUsername = new("scylla:admin_username"); + public static readonly SecretsStoreKey ScyllaAdminPassword = new("scylla:admin_password"); + public static readonly SecretsStoreKey ScyllaContactPoints = new("scylla:contact_points"); + public static readonly SecretsStoreKey ScyllaLocalDatacenter = new("scylla:local_datacenter"); + public static readonly SecretsStoreKey ScyllaUsername = new("scylla:username"); + public static readonly SecretsStoreKey ScyllaPassword = new("scylla:password"); + public static readonly SecretsStoreKey ScyllaPort = new("scylla:port"); + + public static readonly SecretsStoreKey AuthJwtRsa256PrivatePem = new("auth:jwt_rsa256_private_pem"); + public static readonly SecretsStoreKey AuthJwtEs256PrivatePem = new("auth:jwt_es256_private_pem"); + public static readonly SecretsStoreKey AuthDeepLinkSecret = new("auth:deep_link_secret"); + + public static readonly SecretsStoreKey CertsLeafPfxPassword = new("certs:leaf_pfx_password"); + + public static readonly SecretsStoreKey FirebaseClientAndroid = new("firebase:client:android"); + public static readonly SecretsStoreKey FirebaseClientIos = new("firebase:client:ios"); + public static readonly SecretsStoreKey FirebaseClientWeb = new("firebase:client:web"); + + /// FCM v1 service-account credential (PRIVATE). Absent row → NullFCMService, + /// so deployments without Firebase silently no-op the push flow. + public static readonly SecretsStoreKey FcmServiceAccountJson = new("fcm:service_account_json"); +} diff --git a/shared/Interfold.Shared.Contracts/SingletonTaskNames.cs b/shared/Interfold.Shared.Contracts/SingletonTaskNames.cs new file mode 100644 index 00000000..d8a83ae1 --- /dev/null +++ b/shared/Interfold.Shared.Contracts/SingletonTaskNames.cs @@ -0,0 +1,14 @@ +namespace Interfold.Shared.Contracts; + +/// Typed name of a singleton background task gated by ISingletonTaskOwner. +public readonly record struct SingletonTaskName +{ + public string Value { get; } + + public SingletonTaskName(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public override string ToString() => Value; +} diff --git a/shared/Interfold.Shared.Contracts/Validation/ValidAlterIdAttribute.cs b/shared/Interfold.Shared.Contracts/Validation/ValidAlterIdAttribute.cs new file mode 100644 index 00000000..f4c8fe7c --- /dev/null +++ b/shared/Interfold.Shared.Contracts/Validation/ValidAlterIdAttribute.cs @@ -0,0 +1,68 @@ +namespace Interfold.Shared.Contracts.Validation; + +using System.ComponentModel.DataAnnotations; +using Interfold.Shared.Contracts.Ids; + +/// +/// Requires an request-model property to carry a positive, +/// in-range identifier. Default +/// "Invalid alter ID." so InvalidModelStateResponseFactory maps to the +/// invalid_alter_id wire code. +/// +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)] +public sealed class ValidAlterIdAttribute : ValidationAttribute +{ + // Cassandra smallint column ceiling. Now a tautology after AlterId.Value narrowed + // to short; retained for the null/negative case that the type can't express. + public const short MaxValue = short.MaxValue; + + public const short MinValue = 1; + + public ValidAlterIdAttribute() + { + ErrorMessage = "Invalid alter ID."; + } + + /// When true, a null is accepted (e.g. + /// FrontPrimaryRequest clears the primary front); non-null values still range-checked. + public bool AllowNull { get; init; } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) + { + var memberName = validationContext.MemberName ?? validationContext.DisplayName; + + if (value is null) + { + return AllowNull + ? ValidationResult.Success + : new ValidationResult(ErrorMessage, [memberName]); + } + + AlterId? alterId = value switch + { + AlterId id => id, + _ => null + }; + + if (alterId is null) + { + return new ValidationResult(ErrorMessage, [memberName]); + } + + return IsValid(alterId) + ? ValidationResult.Success + : new ValidationResult(ErrorMessage, [memberName]); + } + + // Pure-logic core reusable by tests / command handlers that want one source of + // truth for the AlterId range. Null returns false — treat unset as short-circuit. + public static bool IsValid(AlterId? value) + { + if (value is not { } id) + { + return false; + } + + return id.Value is >= MinValue and <= MaxValue; + } +} diff --git a/shared/Interfold.Shared.Domain/Abstractions/IClusterEventBus.cs b/shared/Interfold.Shared.Domain/Abstractions/IClusterEventBus.cs new file mode 100644 index 00000000..97c96f6d --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/IClusterEventBus.cs @@ -0,0 +1,31 @@ +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions; + +/// In-process (eventually cluster-wide) pub/sub for domain events (mirrors +/// legacy Phoenix.PubSub). +public interface IClusterEventBus +{ + /// Publishes to every subscriber of + /// . Subscribers scoped to a + /// targetSystemId only receive events when implements + /// and the event's target matches. + ValueTask PublishAsync(TEvent evt, CancellationToken ct = default) + where TEvent : class; + + /// Broadcast subscribe — yields every event of . + /// Stream completes on cancellation. + IAsyncEnumerable SubscribeAsync(CancellationToken ct = default) + where TEvent : class + => SubscribeAsync(targetSystemId: null, ct); + + /// Target-scoped subscribe. Non-null filters + /// events implementing by equality on TargetSystemId; + /// null delivers everything. Non-targeted event types bypass the filter and are delivered + /// to every subscriber. + IAsyncEnumerable SubscribeAsync( + ScopedSystemId? targetSystemId, + CancellationToken ct = default) + where TEvent : class; +} diff --git a/shared/Interfold.Shared.Domain/Abstractions/ICommandHandler.cs b/shared/Interfold.Shared.Domain/Abstractions/ICommandHandler.cs new file mode 100644 index 00000000..05da992c --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/ICommandHandler.cs @@ -0,0 +1,12 @@ +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; + +namespace Interfold.Shared.Domain.Abstractions; + +public interface ICommandHandler +{ + Task> HandleAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default + ); +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Domain/Abstractions/IIdempotencyStore.cs b/shared/Interfold.Shared.Domain/Abstractions/IIdempotencyStore.cs new file mode 100644 index 00000000..d2e6058d --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/IIdempotencyStore.cs @@ -0,0 +1,24 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Shared.Domain.Abstractions; + +public interface IIdempotencyStore +{ + Task FindAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default + ); + + Task SaveAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + string payloadHash, + string outcomeHash, + string? outcomePayload, + CancellationToken cancellationToken = default + ); +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Domain/Abstractions/INodeRoleContext.cs b/shared/Interfold.Shared.Domain/Abstractions/INodeRoleContext.cs new file mode 100644 index 00000000..5d9b9c28 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/INodeRoleContext.cs @@ -0,0 +1,19 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Shared.Domain.Abstractions; + +/// Current-node role, resolved once at startup from FLY_PROCESS_GROUP / +/// OCTOCON_NODE_GROUP. +public interface INodeRoleContext +{ + NodeGroup Role { get; } + + /// Primary — owns singletons, runs background jobs. + bool IsPrimary => Role == NodeGroup.Primary; + + /// Auxiliary — HTTP-only. + bool IsAuxiliary => Role == NodeGroup.Auxiliary; + + /// Sidecar — CPU-task node. + bool IsSidecar => Role == NodeGroup.Sidecar; +} diff --git a/shared/Interfold.Shared.Domain/Abstractions/IRegionContext.cs b/shared/Interfold.Shared.Domain/Abstractions/IRegionContext.cs new file mode 100644 index 00000000..fbdb49a5 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/IRegionContext.cs @@ -0,0 +1,15 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions; + +/// Region resolution for the multi-region topology. Typed as +/// ; callers unwrap to the lowercase wire name only at CQL +/// keyspace interpolation. Region-prefix stripping lives on +/// . +public interface IRegionContext +{ + ScyllaKeyspace CurrentRegion { get; } + + ScyllaKeyspace ResolveUserRegion(SystemId systemId); +} diff --git a/shared/Interfold.Shared.Domain/Abstractions/ISingletonTaskOwner.cs b/shared/Interfold.Shared.Domain/Abstractions/ISingletonTaskOwner.cs new file mode 100644 index 00000000..014f5d09 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/ISingletonTaskOwner.cs @@ -0,0 +1,16 @@ +using Interfold.Shared.Contracts; + +namespace Interfold.Shared.Domain.Abstractions; + +/// Gates named singleton background tasks so only one node (usually the primary) +/// runs each (mirrors the legacy FrontNotifier / LinkTokenRegistry Horde pattern). +public interface ISingletonTaskOwner +{ + bool OwnsTask(SingletonTaskName taskName); + + /// No-op when this node isn't the owner. + Task RunIfOwnerAsync( + SingletonTaskName taskName, + Func work, + CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Domain/Abstractions/IdempotentCommandHandler.cs b/shared/Interfold.Shared.Domain/Abstractions/IdempotentCommandHandler.cs new file mode 100644 index 00000000..9fef9f3f --- /dev/null +++ b/shared/Interfold.Shared.Domain/Abstractions/IdempotentCommandHandler.cs @@ -0,0 +1,141 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Shared.Domain.Abstractions; + +public abstract class IdempotentCommandHandler : ICommandHandler + where TResult : class, ICommandResult +{ + private readonly IIdempotencyStore _idempotencyStore; + + protected IdempotentCommandHandler(IIdempotencyStore idempotencyStore) + { + _idempotencyStore = idempotencyStore; + } + + public async Task> HandleAsync( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + var payloadJson = CommandSerialization.Serialize(command.Payload); + var payloadHash = CommandSerialization.Hash(payloadJson); + + var previous = await _idempotencyStore.FindAsync( + command.PrincipalId, + command.OperationId, + command.IdempotencyKey, + cancellationToken + ); + + if (previous is not null) + { + if (!string.Equals(previous.PayloadHash, payloadHash, StringComparison.Ordinal)) + { + return RejectDuplicate(command); + } + + var replay = CommandSerialization.Deserialize(previous.OutcomePayload); + if (replay is not null) + { + return CommandExecutionResult.Success(CreateReplayResult(replay)); + } + } + + var execution = await ExecuteCoreAsync(command, cancellationToken); + + if (execution.Accepted && execution.Result is not null) + { + var resultJson = CommandSerialization.Serialize(execution.Result); + + await _idempotencyStore.SaveAsync( + command.PrincipalId, + command.OperationId, + command.IdempotencyKey, + payloadHash, + CommandSerialization.Hash(resultJson), + resultJson, + cancellationToken + ); + } + + return execution; + } + + protected abstract Task> ExecuteCoreAsync(CommandEnvelope command, CancellationToken cancellationToken); + + /// + /// Produces the replayed-result variant when an idempotent hit is served from the store. + /// The default flips the Replay flag via ; + /// override only if a specific result record needs cross-field invariants (e.g. clearing + /// a one-shot side-effect handle) to move together with the flag. Every replay path here + /// used to be a 2-line handler override that read originalResult with { Replay = true }; + /// the F-bounded interface plus this virtual default deletes 46 near-identical overrides. + /// + protected virtual TResult CreateReplayResult(TResult originalResult) => + originalResult.WithReplay(); + + protected abstract EntityRef DuplicateEntityRef { get; } + + private CommandExecutionResult RejectDuplicate(CommandEnvelope command) => + CommandExecutionResult.Rejected( + new ConflictResult( + ConflictCode.ConflictDuplicate, + command.OperationId, + DuplicateEntityRef, + ResolutionHint.NoRetry + ) + ); + + protected static CommandExecutionResult RejectInvariant( + CommandEnvelope command, + EntityRef entityRef + ) => + CommandHandler.RejectInvariant(command.OperationId, entityRef); + + protected static CommandExecutionResult? RejectIfBlank( + CommandEnvelope command, + string? value, + EntityRef entityRef + ) => + CommandHandler.RejectIfBlank(command.OperationId, value, entityRef); + + protected static CommandExecutionResult? RejectIfAlterIdOutOfRange( + CommandEnvelope command, + AlterId alterId, + EntityRef entityRef + ) => + CommandHandler.RejectIfAlterIdOutOfRange(command.OperationId, alterId, entityRef); +} + +public static class CommandHandler +{ + public static CommandExecutionResult RejectInvariant( + OperationId operationId, + EntityRef entityRef + ) => + CommandExecutionResult.Rejected( + new ConflictResult( + ConflictCode.ConflictInvariant, + operationId, + entityRef, + ResolutionHint.ManualMergeRequired + ) + ); + + public static CommandExecutionResult? RejectIfBlank( + OperationId operationId, + string? value, + EntityRef entityRef + ) => + string.IsNullOrWhiteSpace(value) ? RejectInvariant(operationId, entityRef) : null; + + public static CommandExecutionResult? RejectIfAlterIdOutOfRange( + OperationId operationId, + AlterId alterId, + EntityRef entityRef + ) => + alterId.Value < 1 ? RejectInvariant(operationId, entityRef) : null; +} diff --git a/shared/Interfold.Shared.Domain/CommandSerialization.cs b/shared/Interfold.Shared.Domain/CommandSerialization.cs new file mode 100644 index 00000000..3d8751be --- /dev/null +++ b/shared/Interfold.Shared.Domain/CommandSerialization.cs @@ -0,0 +1,32 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +namespace Interfold.Shared.Domain; + +internal static class CommandSerialization +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + public static string Serialize(T value) => JsonSerializer.Serialize(value, JsonOptions); + + public static T? Deserialize(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return default; + } + + return JsonSerializer.Deserialize(value, JsonOptions); + } + + public static string Hash(string value) + { + var bytes = Encoding.UTF8.GetBytes(value); + var hashBytes = SHA256.HashData(bytes); + return Convert.ToHexString(hashBytes); + } +} \ No newline at end of file diff --git a/shared/Interfold.Shared.Domain/Interfold.Shared.Domain.csproj b/shared/Interfold.Shared.Domain/Interfold.Shared.Domain.csproj new file mode 100644 index 00000000..ee4151e1 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Interfold.Shared.Domain.csproj @@ -0,0 +1,19 @@ + + + net10.0 + enable + enable + + + + + + + + + + + diff --git a/shared/Interfold.Shared.Domain/Observability/GuardedInstrumentation.cs b/shared/Interfold.Shared.Domain/Observability/GuardedInstrumentation.cs new file mode 100644 index 00000000..f21b2ef5 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Observability/GuardedInstrumentation.cs @@ -0,0 +1,113 @@ +using System.Diagnostics; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Logging; + +namespace Interfold.Shared.Domain.Observability; + +// Centralised metric-emission + structured-log helpers for the guarded-visibility +// read paths. Keeps the tag shape identical across every repository (Alter, Tag, +// Fronting) and every backend (Scylla, InMemory) so downstream aggregators can +// group by entity_type without worrying about site-to-site tag drift. +public static class GuardedInstrumentation +{ + // For list-style calls: emits filtered/visible counters, latency histogram, and a + // structured log line whose level depends on whether any rows were visible. Callers + // pass their own entity_type ("alter" | "tag" | "fronting") and method name. + public static void RecordList( + ILogger logger, + string entityType, + string method, + SystemId? viewerSystemId, + string ownerSystemId, + int totalCount, + int visibleCount, + double elapsedMs) + { + var filteredCount = totalCount - visibleCount; + + if (visibleCount > 0) + { + GuardedMetrics.FilteredTotal.Add(visibleCount, new TagList + { + { "entity_type", entityType }, + { "outcome", "visible" }, + }); + } + + if (filteredCount > 0) + { + GuardedMetrics.FilteredTotal.Add(filteredCount, new TagList + { + { "entity_type", entityType }, + { "outcome", "filtered" }, + }); + } + + GuardedMetrics.LatencyMs.Record(elapsedMs, new TagList + { + { "entity_type", entityType }, + { "method", method }, + }); + + if (visibleCount == 0) + { + GuardedMetrics.NoResultsTotal.Add(1, new TagList { { "entity_type", entityType } }); + logger.LogWarning( + "Guarded {EntityType} list returned no visible rows: caller={CallerId}, system={SystemId}, total={TotalCount}, duration_ms={DurationMs}", + entityType, viewerSystemId?.Value, ownerSystemId, totalCount, elapsedMs); + } + else + { + logger.LogInformation( + "Guarded {EntityType} list: caller={CallerId}, system={SystemId}, total={TotalCount}, visible={VisibleCount}, filtered={FilteredCount}, duration_ms={DurationMs}", + entityType, viewerSystemId?.Value, ownerSystemId, totalCount, visibleCount, filteredCount, elapsedMs); + } + } + + // For single-item guarded gets: found records a visible bump, filtered records a + // filtered bump, not-found bumps the no_results counter without touching filtered. + public static void RecordGet( + ILogger logger, + string entityType, + string method, + SystemId? viewerSystemId, + string ownerSystemId, + string targetId, + bool found, + bool filtered, + double elapsedMs) + { + if (found) + { + GuardedMetrics.FilteredTotal.Add(1, new TagList + { + { "entity_type", entityType }, + { "outcome", "visible" }, + }); + } + + if (filtered) + { + GuardedMetrics.FilteredTotal.Add(1, new TagList + { + { "entity_type", entityType }, + { "outcome", "filtered" }, + }); + } + + GuardedMetrics.LatencyMs.Record(elapsedMs, new TagList + { + { "entity_type", entityType }, + { "method", method }, + }); + + if (!found) + { + GuardedMetrics.NoResultsTotal.Add(1, new TagList { { "entity_type", entityType } }); + } + + logger.LogInformation( + "Guarded {EntityType} get: caller={CallerId}, system={SystemId}, target={TargetId}, found={Found}, filtered={Filtered}, duration_ms={DurationMs}", + entityType, viewerSystemId?.Value, ownerSystemId, targetId, found, filtered, elapsedMs); + } +} diff --git a/shared/Interfold.Shared.Domain/Observability/GuardedMetrics.cs b/shared/Interfold.Shared.Domain/Observability/GuardedMetrics.cs new file mode 100644 index 00000000..1d401f95 --- /dev/null +++ b/shared/Interfold.Shared.Domain/Observability/GuardedMetrics.cs @@ -0,0 +1,31 @@ +using System.Diagnostics.Metrics; + +namespace Interfold.Shared.Domain.Observability; + +// Mirror of the guarded-family instruments also declared on the "Interfold.Api" meter by +// Interfold.Api.Helpers.InterfoldMetrics (in the Interfold.Shared.Api project). Domain and +// Infrastructure can't reference Shared.Api without forming a cycle, so the physical instrument +// declarations are duplicated here on the same meter name. OpenTelemetry aggregates by +// (meter, name, tags) at export, so a single time-series flows out regardless of which side +// bumps it. +public static class GuardedMetrics +{ + private static readonly Meter Meter = new("Interfold.Api", "1.0"); + + public static readonly Counter FilteredTotal = Meter.CreateCounter( + "octocon.guarded.filtered.total", + description: "Guarded rows tagged by entity_type and outcome (visible|filtered)."); + + public static readonly Counter NoResultsTotal = Meter.CreateCounter( + "octocon.guarded.no_results.total", + description: "Guarded reads that returned zero visible rows, tagged by entity_type."); + + public static readonly Histogram LatencyMs = Meter.CreateHistogram( + "octocon.guarded.latency_ms", + unit: "ms", + description: "Guarded read latency in milliseconds, tagged by entity_type and method."); + + public static readonly Counter ErrorsTotal = Meter.CreateCounter( + "octocon.guarded.errors.total", + description: "Guarded-path exceptions, tagged by entity_type and exception_type."); +} diff --git a/shared/Interfold.Socket.Contracts/AccountLinkFlag.cs b/shared/Interfold.Socket.Contracts/AccountLinkFlag.cs new file mode 100644 index 00000000..e12b33ed --- /dev/null +++ b/shared/Interfold.Socket.Contracts/AccountLinkFlag.cs @@ -0,0 +1,44 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts; + +/// +/// Link-presence flag for the socket self payload. The legacy wire contract (inherited from +/// the Elixir server) reuses the identity-shaped member names (discord_id, +/// apple_id, email, ...) but carries only "SET" or null — the Kotlin +/// client null-checks the fields to drive the account-linking settings UI and never reads +/// the value. This type makes that explicit instead of posing as an identity string. +/// +[JsonConverter(typeof(AccountLinkFlagJsonConverter))] +public readonly record struct AccountLinkFlag(bool IsLinked) +{ + public const string SetWireValue = "SET"; + + public static readonly AccountLinkFlag NotLinked = new(false); + + /// Linked when the underlying identity value is present; the value itself never + /// leaves the server. + public static AccountLinkFlag FromValuePresence(string? identityValue) + => new(!string.IsNullOrWhiteSpace(identityValue)); +} + +internal sealed class AccountLinkFlagJsonConverter : JsonConverter +{ + public override bool HandleNull => true; + + public override AccountLinkFlag Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.TokenType == JsonTokenType.String && !string.IsNullOrWhiteSpace(reader.GetString())); + + public override void Write(Utf8JsonWriter writer, AccountLinkFlag value, JsonSerializerOptions options) + { + if (value.IsLinked) + { + writer.WriteStringValue(AccountLinkFlag.SetWireValue); + } + else + { + writer.WriteNullValue(); + } + } +} diff --git a/shared/Interfold.Socket.Contracts/EmptyPayload.cs b/shared/Interfold.Socket.Contracts/EmptyPayload.cs new file mode 100644 index 00000000..b94326ba --- /dev/null +++ b/shared/Interfold.Socket.Contracts/EmptyPayload.cs @@ -0,0 +1,3 @@ +namespace Interfold.Socket.Contracts; + +public sealed record EmptyPayload : ISocketPayload; diff --git a/shared/Interfold.Socket.Contracts/Enums/AutoproxyMode.cs b/shared/Interfold.Socket.Contracts/Enums/AutoproxyMode.cs new file mode 100644 index 00000000..6179bdec --- /dev/null +++ b/shared/Interfold.Socket.Contracts/Enums/AutoproxyMode.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Autoproxy mode. Wire representation is the lowercase name; the Scylla schema stores +/// this as a smallint (see autoproxy_mode in the users table). Server +/// currently hard-codes on the read-model boundary — reserved for +/// future wiring of latch/front autoproxy. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum AutoproxyMode : short +{ + [JsonStringEnumMemberName("off")] + Off = 0, + + [JsonStringEnumMemberName("latch")] + Latch = 1, + + [JsonStringEnumMemberName("front")] + Front = 2, +} diff --git a/shared/Interfold.Socket.Contracts/Enums/PhoenixReplyStatus.cs b/shared/Interfold.Socket.Contracts/Enums/PhoenixReplyStatus.cs new file mode 100644 index 00000000..f03b90ac --- /dev/null +++ b/shared/Interfold.Socket.Contracts/Enums/PhoenixReplyStatus.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Enums; + +/// +/// Reply status carried by a Phoenix channel reply frame. Wire values are +/// "ok" / "error" and MUST NOT change — the Kotlin client and the +/// legacy Elixir server pinned this two-value contract. +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum PhoenixReplyStatus +{ + [JsonStringEnumMemberName("ok")] + Ok, + + [JsonStringEnumMemberName("error")] + Error, +} diff --git a/shared/Interfold.Socket.Contracts/FcmPayloadContracts.cs b/shared/Interfold.Socket.Contracts/FcmPayloadContracts.cs new file mode 100644 index 00000000..dc0cfcce --- /dev/null +++ b/shared/Interfold.Socket.Contracts/FcmPayloadContracts.cs @@ -0,0 +1,21 @@ +namespace Interfold.Shared.Contracts; + +/// +/// Data-payload keys for FCM push messages. The Kotlin app and the web service worker +/// match on these exact strings — frozen wire contract. +/// +public static class FcmPayloadKeys +{ + public const string Type = "type"; + public const string SystemId = "system_id"; + public const string AlterIds = "alter_ids"; + public const string DeepLink = "deep_link"; +} + +/// +/// Values carried in . Frozen for the same reason. +/// +public static class FcmNotificationTypes +{ + public const string FrontingChanged = "fronting_changed"; +} diff --git a/shared/Interfold.Socket.Contracts/ISocketPayload.cs b/shared/Interfold.Socket.Contracts/ISocketPayload.cs new file mode 100644 index 00000000..7792d638 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/ISocketPayload.cs @@ -0,0 +1,4 @@ +namespace Interfold.Socket.Contracts; + +/// Marker interface for socket event payloads. +public interface ISocketPayload { } diff --git a/shared/Interfold.Socket.Contracts/Ids/SocketToken.cs b/shared/Interfold.Socket.Contracts/Ids/SocketToken.cs new file mode 100644 index 00000000..5e355658 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/Ids/SocketToken.cs @@ -0,0 +1,29 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Shared.Contracts.Ids; + +/// +/// The JWT carried in the phx_join payload's token member (mirrors the +/// query-string token the socket upgrade authenticated with). JSON serializes as the raw +/// string; ToString() redacts so a logged join payload can't leak the credential. +/// +[JsonConverter(typeof(SocketTokenJsonConverter))] +public readonly record struct SocketToken +{ + public string Value { get; } + + public SocketToken(string value) + { + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public static explicit operator SocketToken(string value) => new(value); + + public override string ToString() => SecretRedaction.Redact(Value); +} + +internal sealed class SocketTokenJsonConverter : StringBackedJsonConverter +{ + protected override SocketToken Create(string value) => new(value); + protected override string GetValue(SocketToken value) => value.Value; +} diff --git a/shared/Interfold.Socket.Contracts/Interfold.Socket.Contracts.csproj b/shared/Interfold.Socket.Contracts/Interfold.Socket.Contracts.csproj new file mode 100644 index 00000000..e540cb02 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/Interfold.Socket.Contracts.csproj @@ -0,0 +1,15 @@ + + + net10.0 + enable + enable + + + + + + diff --git a/shared/Interfold.Socket.Contracts/PhoenixEventNames.cs b/shared/Interfold.Socket.Contracts/PhoenixEventNames.cs new file mode 100644 index 00000000..a1375104 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/PhoenixEventNames.cs @@ -0,0 +1,20 @@ +namespace Interfold.Socket.Contracts; + +/// +/// Phoenix-protocol transport event and topic names, mirroring the +/// pattern for application push events. These are the +/// framing vocabulary the Kotlin client's Phoenix socket implementation speaks — the +/// spellings are frozen. +/// +public static class PhoenixEventNames +{ + public const string Heartbeat = "heartbeat"; + public const string Join = "phx_join"; + public const string Reply = "phx_reply"; + + /// Interfold extension event: HTTP relay over the socket. + public const string Endpoint = "endpoint"; + + /// The default control topic (heartbeats). + public const string PhoenixTopic = "phoenix"; +} diff --git a/shared/Interfold.Socket.Contracts/PhoenixReplyPayload.cs b/shared/Interfold.Socket.Contracts/PhoenixReplyPayload.cs new file mode 100644 index 00000000..79784a1b --- /dev/null +++ b/shared/Interfold.Socket.Contracts/PhoenixReplyPayload.cs @@ -0,0 +1,5 @@ +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Socket.Contracts; + +public sealed record PhoenixReplyPayload(PhoenixReplyStatus Status, TResponse Response) : ISocketPayload; diff --git a/shared/Interfold.Socket.Contracts/PhxFrame.cs b/shared/Interfold.Socket.Contracts/PhxFrame.cs new file mode 100644 index 00000000..6835a83f --- /dev/null +++ b/shared/Interfold.Socket.Contracts/PhxFrame.cs @@ -0,0 +1,129 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Socket.Contracts; + +/// +/// A Phoenix-protocol WebSocket frame (object format). +/// +public sealed class PhxFrame +{ + [JsonPropertyName("topic")] + public string Topic { get; init; } = string.Empty; + + [JsonPropertyName("event")] + public string Event { get; init; } = string.Empty; + + [JsonPropertyName("payload")] + public TPayload? Payload { get; init; } + + [JsonPropertyName("ref")] + public string? Ref { get; init; } + + [JsonPropertyName("join_ref")] + public string? JoinRef { get; init; } + + public byte[] ToBytes() => Encoding.UTF8.GetBytes(JsonSerializer.Serialize(this, SocketJson.Options)); +} + +/// +/// Payload for phx_join frames. +/// +public sealed class PhxJoinPayload +{ + [JsonPropertyName("token")] + public Shared.Contracts.Ids.SocketToken Token { get; init; } = new(string.Empty); + + [JsonPropertyName("protocolVersion")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? ProtocolVersion { get; init; } + + // Join-side platform tolerates unknown wire spellings → null (so a stray value can't + // fail the whole join). Every other ClientPlatform site keeps the strict converter. + [JsonPropertyName("platform")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonConverter(typeof(TolerantWireEnumJsonConverter))] + public ClientPlatform? Platform { get; init; } + + [JsonPropertyName("isReconnect")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? IsReconnect { get; init; } + + [JsonPropertyName("forceBatch")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? ForceBatch { get; init; } +} + +public sealed class PhxEndpointPayload : PhxEndpointPayload; + +/// +/// Payload for endpoint frames (proxied HTTP calls over WebSocket). +/// +public class PhxEndpointPayload where T : new() +{ + [JsonPropertyName("method")] + public string Method { get; init; } = string.Empty; + + [JsonPropertyName("path")] + public string Path { get; init; } = string.Empty; + + [JsonPropertyName("body"), JsonConverter(typeof(BodyJsonConverterFactory))] + public T Body { get; init; } = new T(); +} + +public class BodyJsonConverterFactory : JsonConverterFactory +{ + public override bool CanConvert(Type typeToConvert) => true; + + public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + var converterType = typeof(BodyJson<>).MakeGenericType(typeToConvert); + return (JsonConverter?)Activator.CreateInstance(converterType); + } +} + +public class BodyJson : JsonConverter + where T : new() +{ + public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.Null) + { + return default; + } + + if (reader.TokenType != JsonTokenType.String) + { + throw new Exception("Expected string token for JSON body"); + } + + var rawContent = reader.GetString(); + var content = string.IsNullOrWhiteSpace(rawContent) + ? new T() + : JsonSerializer.Deserialize(rawContent, options); + + return content; + } + + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) + { + var format = value == null ? "" : JsonSerializer.Serialize(value, value.GetType(), options); + writer.WriteStringValue(format); + } +} + +/// +/// Helper for constructing Phoenix array-format WebSocket frames +/// (used when testing the array-protocol variant). +/// +public static class PhxArrayFrame +{ + public static byte[] CreateBytes( + string? joinRef, string? @ref, string topic, string @event, TPayload payload) + { + var array = new object?[] { joinRef, @ref, topic, @event, (object?)payload }; + return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(array, SocketJson.Options)); + } +} diff --git a/shared/Interfold.Socket.Contracts/SocketEndpointProxyRequest.cs b/shared/Interfold.Socket.Contracts/SocketEndpointProxyRequest.cs new file mode 100644 index 00000000..d86a7a61 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketEndpointProxyRequest.cs @@ -0,0 +1,9 @@ +namespace Interfold.Socket.Contracts; + +/// +/// Typed shape of the endpoint Phoenix frame payload. Body is a raw +/// JSON string (Phoenix wraps the inner API request body as a JSON-string field so +/// it can be forwarded to the loopback API without being re-serialized — see the +/// forwarding comment in WebSocketHandler.HandleEndpointProxyAsync). +/// +public sealed record SocketEndpointProxyRequest(string? Method, string? Path, string? Body); diff --git a/shared/Interfold.Socket.Contracts/SocketEndpointProxyResponse.cs b/shared/Interfold.Socket.Contracts/SocketEndpointProxyResponse.cs new file mode 100644 index 00000000..f879dc24 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketEndpointProxyResponse.cs @@ -0,0 +1,10 @@ +using System.Net; +using System.Text.Json.Serialization; + +namespace Interfold.Socket.Contracts; + +// Status is HTTP semantics; JsonNumberEnumConverter pins the historical bare-number wire +// form (SocketJson has no enum policy, but explicit is safer than relying on the default). +public sealed record SocketEndpointProxyResponse( + [property: JsonConverter(typeof(JsonNumberEnumConverter))] HttpStatusCode Status, + string Body) : ISocketPayload; diff --git a/shared/Interfold.Socket.Contracts/SocketEventNames.cs b/shared/Interfold.Socket.Contracts/SocketEventNames.cs new file mode 100644 index 00000000..3036e1f7 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketEventNames.cs @@ -0,0 +1,90 @@ +namespace Interfold.Socket.Contracts; + +public static class SocketEventNames +{ + public static class Alters + { + //alters_created has not been added yet + public const string Created = "alter_created"; + public const string Updated = "alter_updated"; + public const string Deleted = "alter_deleted"; + } + + public static class Tags + { + public const string Created = "tag_created"; + public const string Updated = "tag_updated"; + public const string Deleted = "tag_deleted"; + } + + public static class Polls + { + public const string Created = "poll_created"; + public const string Updated = "poll_updated"; + public const string Deleted = "poll_deleted"; + } + + public static class Imports + { + public const string SpComplete = "sp_import_complete"; + public const string SpFailed = "sp_import_failed"; + public const string PkComplete = "pk_import_complete"; + public const string PkFailed = "pk_import_failed"; + } + + public static class Fronting + { + public const string Started = "fronting_started"; + public const string Ended = "fronting_ended"; + public const string Set = "fronting_set"; + public const string BulkUpdated = "fronting_bulk"; + public const string CommentUpdated = "front_updated"; + public const string PrimaryChanged = "primary_front"; + public const string Deleted = "front_deleted"; + } + + public static class Journals + { + public const string GlobalCreated = "global_journal_entry_created"; + public const string GlobalUpdated = "global_journal_entry_updated"; + public const string GlobalDeleted = "global_journal_entry_deleted"; + public const string AlterCreated = "alter_journal_entry_created"; + public const string AlterUpdated = "alter_journal_entry_updated"; + public const string AlterDeleted = "alter_journal_entry_deleted"; + } + + public static class Friendships + { + public const string Added = "friend_added"; + public const string Removed = "friend_removed"; + public const string Trusted = "friend_trusted"; + public const string Untrusted = "friend_untrusted"; + public const string RequestSent = "friend_request_sent"; + public const string RequestReceived = "friend_request_received"; + public const string RequestRemoved = "friend_request_removed"; + } + + public static class BatchedInit { + public const string Alters = "batched_init_alters"; + public const string Tags = "batched_init_tags"; + public const string Fronts = "batched_init_fronts"; + public const string Complete = "batched_init_complete"; + } + + public static class Settings + { + public const string FieldsUpdated = "fields_updated"; + public const string UsernameUpdated = "username_updated"; + public const string SelfUpdated = "self_updated"; + public const string AccountDeleted = "account_deleted"; + public const string AltersWiped = "alters_wiped"; + public const string TagsWiped = "tags_wiped"; + public const string EncryptedDataWiped = "encrypted_data_wiped"; + public const string DiscordAccountUnlinked = "discord_account_unlinked"; + public const string AppleAccountUnlinked = "apple_account_unlinked"; + public const string GoogleAccountUnlinked = "google_account_unlinked"; + public const string DiscordAccountLinked = "discord_account_linked"; + public const string GoogleAccountLinked = "google_account_linked"; + public const string AppleAccountLinked = "apple_account_linked"; + } +} \ No newline at end of file diff --git a/shared/Interfold.Socket.Contracts/SocketJoinReconnectPayload.cs b/shared/Interfold.Socket.Contracts/SocketJoinReconnectPayload.cs new file mode 100644 index 00000000..31927427 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketJoinReconnectPayload.cs @@ -0,0 +1,3 @@ +namespace Interfold.Socket.Contracts; + +public sealed record SocketJoinReconnectPayload(SocketSelfReadModel System) : ISocketPayload; diff --git a/shared/Interfold.Socket.Contracts/SocketJson.cs b/shared/Interfold.Socket.Contracts/SocketJson.cs new file mode 100644 index 00000000..0c00650e --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketJson.cs @@ -0,0 +1,17 @@ +using System.Text.Json; +using Interfold.Shared.Contracts; + +namespace Interfold.Socket.Contracts; + +public static class SocketJson +{ public static JsonSerializerOptions Options { get; } = new() + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + Converters = + { + new UtcDateTimeConverter(), + new UtcDateTimeOffsetConverter() + } + }; +} + diff --git a/shared/Interfold.Socket.Contracts/SocketReasonResponse.cs b/shared/Interfold.Socket.Contracts/SocketReasonResponse.cs new file mode 100644 index 00000000..68104f2b --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketReasonResponse.cs @@ -0,0 +1,5 @@ +using Interfold.Shared.Contracts; + +namespace Interfold.Socket.Contracts; + +public sealed record SocketReasonResponse(ErrorCode Reason) : ISocketPayload; diff --git a/shared/Interfold.Socket.Contracts/SocketSelfReadModel.cs b/shared/Interfold.Socket.Contracts/SocketSelfReadModel.cs new file mode 100644 index 00000000..6fc74330 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SocketSelfReadModel.cs @@ -0,0 +1,28 @@ +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; + +namespace Interfold.Socket.Contracts; + +// The four *Linked members keep the legacy identity-shaped wire names (discord_id etc.) +// but carry only the "SET"/null link-presence flag — see AccountLinkFlag. GoogleLinked is +// historically always null (Google links surface via email) and stays for wire shape. +public sealed record SocketSelfReadModel( + SystemId Id, + Username? Username, + string? Description, + AvatarUrl? AvatarUrl, + AvatarSource? AvatarSource, + [property: JsonPropertyName("discord_id")] AccountLinkFlag DiscordLinked, + [property: JsonPropertyName("google_id")] AccountLinkFlag GoogleLinked, + [property: JsonPropertyName("apple_id")] AccountLinkFlag AppleLinked, + [property: JsonPropertyName("email")] AccountLinkFlag EmailLinked, + AutoproxyMode AutoproxyMode, + bool ShowSystemTag, + int LifetimeAlterCount, + AlterId? PrimaryFront, + IReadOnlyList Fields, + bool EncryptionInitialized) : ISocketPayload, IAvatarBearing; diff --git a/shared/Interfold.Socket.Contracts/SystemIdSocketPayload.cs b/shared/Interfold.Socket.Contracts/SystemIdSocketPayload.cs new file mode 100644 index 00000000..fc4436f5 --- /dev/null +++ b/shared/Interfold.Socket.Contracts/SystemIdSocketPayload.cs @@ -0,0 +1,5 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Socket.Contracts; + +public sealed record SystemIdSocketPayload(SystemId SystemId) : ISocketPayload; diff --git a/shared/Interfold.Systems.Contracts/Interfold.Systems.Contracts.csproj b/shared/Interfold.Systems.Contracts/Interfold.Systems.Contracts.csproj new file mode 100644 index 00000000..dc0eb2d4 --- /dev/null +++ b/shared/Interfold.Systems.Contracts/Interfold.Systems.Contracts.csproj @@ -0,0 +1,19 @@ + + + net10.0 + enable + enable + + + + + + + + + + diff --git a/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemBatchReadModel.cs b/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemBatchReadModel.cs new file mode 100644 index 00000000..d8ad609e --- /dev/null +++ b/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemBatchReadModel.cs @@ -0,0 +1,18 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Systems.Contracts.Models.Read; + +/// Read model for GET /api/systems/{systemId}/batch. Emits +/// { friendship, tags, alters }; guarded (visibility-filtered) projections +/// use and . Namespace +/// preserved as Interfold.Shared.Contracts.Models.Read for wire-compat; physically +/// migrated through spine → host/Interfold.Api/Models/ (Phase-3 Friendships slice) → +/// Interfold.Systems.Contracts (Phase-3 Systems facade slice — this file). Landed +/// here because PublicSystemsController.Batch is the sole consumer. +public sealed record PublicSystemBatchReadModel( + FriendshipReadModel? Friendship, + IReadOnlyList Tags, + IReadOnlyList Alters +); diff --git a/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemViewModels.cs b/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemViewModels.cs new file mode 100644 index 00000000..aa4ebecf --- /dev/null +++ b/shared/Interfold.Systems.Contracts/Models/Read/PublicSystemViewModels.cs @@ -0,0 +1,15 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Systems.Contracts.Models.Read; + +/// Read model for GET /api/systems/{systemId}. Emits +/// { id, avatar_url, avatar_source, username, description } under snake_case. +public sealed record PublicSystemReadModel( + SystemId Id, + AvatarUrl? AvatarUrl, + AvatarSource? AvatarSource, + Username? Username, + string? Description +) : IAvatarBearing; diff --git a/shared/Interfold.Tags.Contracts/Events/SocketProjectionEvents.cs b/shared/Interfold.Tags.Contracts/Events/SocketProjectionEvents.cs new file mode 100644 index 00000000..5b9b5d16 --- /dev/null +++ b/shared/Interfold.Tags.Contracts/Events/SocketProjectionEvents.cs @@ -0,0 +1,15 @@ +using Interfold.Shared.Contracts.Events; +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Tags.Contracts.Events; + +// Extracted from Interfold.Shared.Contracts/Events/SocketProjectionEvents.cs during Phase-3 Tags +// migration. Other feature-scoped events (Alter*, Poll*, Journal*, Friendship*, Settings*) +// migrate with their respective features. + +public sealed record TagCreatedEvent(ScopedSystemId TargetSystemId, TagId TagId) : ITargetedClusterEvent; + +public sealed record TagUpdatedEvent(ScopedSystemId TargetSystemId, TagId TagId) : ITargetedClusterEvent; + +public sealed record TagDeletedEvent(ScopedSystemId TargetSystemId, TagId TagId) : ITargetedClusterEvent; diff --git a/shared/Interfold.Tags.Contracts/Ids/TagId.cs b/shared/Interfold.Tags.Contracts/Ids/TagId.cs new file mode 100644 index 00000000..fc79d7a1 --- /dev/null +++ b/shared/Interfold.Tags.Contracts/Ids/TagId.cs @@ -0,0 +1,36 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Tags.Contracts.Ids; + +/// Strongly-typed wrapper around a tag id (Guid, wire form is 32-char lowercase hex). +[JsonConverter(typeof(TagIdJsonConverter))] +public readonly record struct TagId(Guid Value) : IParsable +{ + public static readonly TagId Empty = new(Guid.Empty); + + public static explicit operator TagId(Guid value) => new(value); + public static implicit operator Guid(TagId value) => value.Value; + + public override string ToString() => Value.ToString("N"); + + public static TagId Parse(string s, IFormatProvider? provider) + => UuidString.TryParse(s, out var g) + ? new(g) + : throw new FormatException($"Value '{s}' is not a valid TagId."); + + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TagId result) + { + if (s is null || !UuidString.TryParse(s, out var g)) { result = default; return false; } + result = new TagId(g); + return true; + } +} + +internal sealed class TagIdJsonConverter : GuidIdJsonConverter +{ + protected override TagId Create(Guid value) => new(value); + protected override Guid GetValue(TagId value) => value.Value; + protected override string TypeLabel => nameof(TagId); +} diff --git a/shared/Interfold.Tags.Contracts/Interfold.Tags.Contracts.csproj b/shared/Interfold.Tags.Contracts/Interfold.Tags.Contracts.csproj new file mode 100644 index 00000000..da5763bb --- /dev/null +++ b/shared/Interfold.Tags.Contracts/Interfold.Tags.Contracts.csproj @@ -0,0 +1,19 @@ + + + net10.0 + enable + enable + + + + + + + + + diff --git a/shared/Interfold.Tags.Contracts/Models/Commands/TagCommands.cs b/shared/Interfold.Tags.Contracts/Models/Commands/TagCommands.cs new file mode 100644 index 00000000..cb0234e3 --- /dev/null +++ b/shared/Interfold.Tags.Contracts/Models/Commands/TagCommands.cs @@ -0,0 +1,32 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Tags.Contracts.Models.Commands; + +/// +/// Public-API callers send InsertedAtUtc: default(DateTime). The handler stamps the +/// real value from the envelope's OccurredAt after the idempotency hash is taken, so +/// the hashed payload stays stable across retries (otherwise every replay would carry a fresh +/// DateTime.UtcNow and trip ConflictDuplicate). The Simply Plural importer +/// constructs the command directly with the decoded ObjectId timestamp. +/// +public sealed record CreateTagCommand(string Name, TagId? ParentTagId, DateTime InsertedAtUtc); + +public sealed record UpdateTagCommand( + TagId TagId, + string? Name, + HexColor? Color, + string? Description, + VisibilityLevel? SecurityLevel +); + +public sealed record DeleteTagCommand(TagId TagId); + +public sealed record AttachAlterToTagCommand(TagId TagId, AlterId AlterId); + +public sealed record DetachAlterFromTagCommand(TagId TagId, AlterId AlterId); + +public sealed record SetParentTagCommand(TagId TagId, TagId ParentTagId); + +public sealed record RemoveParentTagCommand(TagId TagId); diff --git a/shared/Interfold.Tags.Contracts/Models/Read/TagReadModel.cs b/shared/Interfold.Tags.Contracts/Models/Read/TagReadModel.cs new file mode 100644 index 00000000..29f0c5c4 --- /dev/null +++ b/shared/Interfold.Tags.Contracts/Models/Read/TagReadModel.cs @@ -0,0 +1,58 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Validation; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Tags.Contracts.Models.Read; + +public sealed record TagReadModel( + TagId Id, + string Name, + HexColor? Color, + string? Description, + TagId? ParentTagId, + IReadOnlyList Alters, + DateTime InsertedAt, + DateTime UpdatedAt, + VisibilityLevel SecurityLevel, + SystemId? UserId +); + +public sealed record TagPublicReadModel( + TagId Id, + string Name, + HexColor? Color, + string? Description, + TagId? ParentTagId, + IReadOnlyList Alters, + DateTime InsertedAt, + DateTime UpdatedAt, + VisibilityLevel SecurityLevel, + SystemId? UserId +); + +public sealed record CreateTagRequest( + string Name, + TagId? ParentTagId +); + +public sealed record UpdateTagRequest( + string? Name = null, + HexColor? Color = null, + string? Description = null, + VisibilityLevel? SecurityLevel = null +); + +// ValidAlterId lives on the constructor parameter, NOT the property — see the same +// pattern in JournalAlterRequest / FrontStartRequest. MVC's ObjectModelValidator +// rejects records that carry validation metadata on their auto-generated properties +// (ThrowIfRecordTypeHasValidationOnProperties) and reads parameter-level attributes +// via its parameter-binding validator instead. +public sealed record TagAlterRequest( + [ValidAlterId] AlterId AlterId +); + +public sealed record SetParentRequest( + TagId? ParentTagId +); diff --git a/shared/Interfold.Tags.Contracts/SocketPayloadContracts.cs b/shared/Interfold.Tags.Contracts/SocketPayloadContracts.cs new file mode 100644 index 00000000..ee6f26e6 --- /dev/null +++ b/shared/Interfold.Tags.Contracts/SocketPayloadContracts.cs @@ -0,0 +1,17 @@ +using Interfold.Socket.Contracts; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Tags.Contracts; + +// Extracted from Interfold.Shared.Contracts/SocketPayloadContracts.cs during Phase-3 Tags migration. +// The cross-feature aggregate join payloads (SocketJoinInitPayload, SocketJoinBatchedPayload) +// still reference IReadOnlyList and stay in Interfold.Shared.Contracts — Interfold.Shared.Contracts +// grew a spine back-ref to Interfold.Tags.Contracts to keep them compiling. Both dissolve when +// the Socket feature (Phase 3 #10) takes the aggregate join payloads. + +public sealed record SocketBatchedTagsPayload(int BatchIndex, int TotalBatches, IReadOnlyList Tags) : ISocketPayload; + +public sealed record TagSocketPayload(TagReadModel Tag) : ISocketPayload; + +public sealed record TagDeletedSocketPayload(TagId TagId) : ISocketPayload; diff --git a/shared/Interfold.Tags.Contracts/TagCommandResult.cs b/shared/Interfold.Tags.Contracts/TagCommandResult.cs new file mode 100644 index 00000000..51ac492c --- /dev/null +++ b/shared/Interfold.Tags.Contracts/TagCommandResult.cs @@ -0,0 +1,13 @@ +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Tags.Contracts; + +// Extracted from OperationResults.cs during Phase-3 Tags migration. The other per-feature +// command results (Alter, Poll, etc.) will migrate with their respective features. + +public sealed record TagCommandResult(SystemId SystemId, TagId TagId, bool Replay) : ICommandResult +{ + public TagCommandResult WithReplay() => this with { Replay = true }; +} diff --git a/shared/Interfold.Tags.Domain/Abstractions/Repository/ITagRepository.cs b/shared/Interfold.Tags.Domain/Abstractions/Repository/ITagRepository.cs new file mode 100644 index 00000000..dde3d067 --- /dev/null +++ b/shared/Interfold.Tags.Domain/Abstractions/Repository/ITagRepository.cs @@ -0,0 +1,52 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Tags.Domain.Abstractions.Repository; + +public interface ITagRepository +{ + /// Returns the new tag's ID on success, null if the parent was not found. + Task CreateAsync(SystemId systemId, CreateTagCommand command, CancellationToken cancellationToken = default); + + Task ExistsAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default); + + /// Returns true if the tag was found and updated, false if not found. + Task UpdateAsync(SystemId systemId, UpdateTagCommand command, CancellationToken cancellationToken = default); + + /// Returns true if the tag was found and deleted, false if not found. + Task DeleteAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default); + + /// Returns true if the alter was attached, false if the tag does not exist. + Task AttachAlterAsync(SystemId systemId, TagId tagId, AlterId alterId, CancellationToken cancellationToken = default); + + /// Returns true if the alter was detached, false if the tag/alter combo does not exist. + Task DetachAlterAsync(SystemId systemId, TagId tagId, AlterId alterId, CancellationToken cancellationToken = default); + + /// Returns the immediate parent tag ID, or null if none or tag does not exist. + Task GetParentIdAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default); + + /// Returns true if the parent was set, false if the tag or parent tag does not exist. + Task SetParentAsync(SystemId systemId, TagId tagId, TagId parentTagId, CancellationToken cancellationToken = default); + + /// Returns true if the parent was removed, false if the tag does not exist. + Task RemoveParentAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default); + + Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default); + + Task> ListGuardedAsync( + SystemId systemId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default + ); + + Task GetAsync(SystemId systemId, TagId tagId, CancellationToken cancellationToken = default); + + Task GetGuardedAsync( + SystemId systemId, + TagId tagId, + SystemId? viewerSystemId, + CancellationToken cancellationToken = default + ); +} diff --git a/shared/Interfold.Tags.Domain/AttachAlterToTagCommandHandler.cs b/shared/Interfold.Tags.Domain/AttachAlterToTagCommandHandler.cs new file mode 100644 index 00000000..53cf2881 --- /dev/null +++ b/shared/Interfold.Tags.Domain/AttachAlterToTagCommandHandler.cs @@ -0,0 +1,49 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class AttachAlterToTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IAlterExistenceCheck _alterExistence; + private readonly IClusterEventBus _eventBus; + + public AttachAlterToTagCommandHandler( + ITagRepository tagRepository, + IAlterExistenceCheck alterExistence, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _alterExistence = alterExistence; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagAttachAlter; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var payload = command.Payload; + + if (await TagCommandFlow.RejectIfAlterNotFoundAsync(command, payload.AlterId, _alterExistence, cancellationToken) is { } rejection) + return rejection; + + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + payload.TagId, + ct => _tagRepository.AttachAlterAsync(command.PrincipalId, payload.TagId, payload.AlterId, ct), + EntityRefs.TagNotFound, + _eventBus, + cancellationToken); + } + +} diff --git a/shared/Interfold.Tags.Domain/CreateTagCommandHandler.cs b/shared/Interfold.Tags.Domain/CreateTagCommandHandler.cs new file mode 100644 index 00000000..6c689e43 --- /dev/null +++ b/shared/Interfold.Tags.Domain/CreateTagCommandHandler.cs @@ -0,0 +1,68 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class CreateTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IClusterEventBus _eventBus; + + public CreateTagCommandHandler( + ITagRepository tagRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus + ) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagCreate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default + ) + { + if (RejectIfBlank(command, command.Payload.Name, EntityRefs.TagNameRequired) is { } blankReject) + return blankReject; + + if (TagCommandValidation.GetNameValidationError(command.Payload.Name) is { } validationError) + return RejectInvariant(command, validationError); + + if (command.Payload.ParentTagId is { } parentTagId && parentTagId != TagId.Empty) + { + var parentExists = await _tagRepository.ExistsAsync( + command.PrincipalId, + parentTagId, + cancellationToken + ); + + if (!parentExists) + return RejectInvariant(command, EntityRefs.TagParentNotFound); + } + + // See CreateTagCommand XML-doc: public-API callers send `default(DateTime)` so the + // idempotency hash stays stable across retries. We stamp the real value here from + // the envelope just before the repo call. The SP import bypasses this handler and + // sets InsertedAtUtc itself from the decoded ObjectId, so it isn't affected here. + var insertedAtUtc = (command.OccurredAt ?? DateTimeOffset.UtcNow).UtcDateTime; + return await TagCommandFlow.ExecuteCreateAsync( + command, + _eventBus, + ct => _tagRepository.CreateAsync( + command.PrincipalId, + command.Payload with { InsertedAtUtc = insertedAtUtc }, + ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Tags.Domain/DeleteTagCommandHandler.cs b/shared/Interfold.Tags.Domain/DeleteTagCommandHandler.cs new file mode 100644 index 00000000..cf5a2bd1 --- /dev/null +++ b/shared/Interfold.Tags.Domain/DeleteTagCommandHandler.cs @@ -0,0 +1,44 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Events; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class DeleteTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IClusterEventBus _eventBus; + + public DeleteTagCommandHandler( + ITagRepository tagRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagDelete; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var tagId = command.Payload.TagId; + + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + tagId, + ct => _tagRepository.DeleteAsync(command.PrincipalId, tagId, ct), + EntityRefs.TagNotFound, + ct => _eventBus.PublishAsync(new TagDeletedEvent(command.PrincipalId, tagId), ct), + cancellationToken); + } + +} diff --git a/shared/Interfold.Tags.Domain/DetachAlterFromTagCommandHandler.cs b/shared/Interfold.Tags.Domain/DetachAlterFromTagCommandHandler.cs new file mode 100644 index 00000000..0727a710 --- /dev/null +++ b/shared/Interfold.Tags.Domain/DetachAlterFromTagCommandHandler.cs @@ -0,0 +1,49 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class DetachAlterFromTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IAlterExistenceCheck _alterExistence; + private readonly IClusterEventBus _eventBus; + + public DetachAlterFromTagCommandHandler( + ITagRepository tagRepository, + IAlterExistenceCheck alterExistence, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _alterExistence = alterExistence; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagDetachAlter; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var payload = command.Payload; + + if (await TagCommandFlow.RejectIfAlterNotFoundAsync(command, payload.AlterId, _alterExistence, cancellationToken) is { } rejection) + return rejection; + + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + payload.TagId, + ct => _tagRepository.DetachAlterAsync(command.PrincipalId, payload.TagId, payload.AlterId, ct), + EntityRefs.TagNotFound, + _eventBus, + cancellationToken); + } + +} diff --git a/shared/Interfold.Tags.Domain/Interfold.Tags.Domain.csproj b/shared/Interfold.Tags.Domain/Interfold.Tags.Domain.csproj new file mode 100644 index 00000000..31e278eb --- /dev/null +++ b/shared/Interfold.Tags.Domain/Interfold.Tags.Domain.csproj @@ -0,0 +1,15 @@ + + + net10.0 + enable + enable + + + + + + + + + diff --git a/shared/Interfold.Tags.Domain/RemoveParentTagCommandHandler.cs b/shared/Interfold.Tags.Domain/RemoveParentTagCommandHandler.cs new file mode 100644 index 00000000..8ab8216e --- /dev/null +++ b/shared/Interfold.Tags.Domain/RemoveParentTagCommandHandler.cs @@ -0,0 +1,42 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class RemoveParentTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IClusterEventBus _eventBus; + + public RemoveParentTagCommandHandler( + ITagRepository tagRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagRemoveParent; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var tagId = command.Payload.TagId; + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + tagId, + ct => _tagRepository.RemoveParentAsync(command.PrincipalId, tagId, ct), + EntityRefs.TagNotFound, + _eventBus, + cancellationToken); + } + +} diff --git a/shared/Interfold.Tags.Domain/SetParentTagCommandHandler.cs b/shared/Interfold.Tags.Domain/SetParentTagCommandHandler.cs new file mode 100644 index 00000000..34804516 --- /dev/null +++ b/shared/Interfold.Tags.Domain/SetParentTagCommandHandler.cs @@ -0,0 +1,68 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class SetParentTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IClusterEventBus _eventBus; + + public SetParentTagCommandHandler( + ITagRepository tagRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagSetParent; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var payload = command.Payload; + + if (payload.TagId == payload.ParentTagId) + return RejectInvariant(command, EntityRefs.TagCycle); + + // Cycle detection: walk up from the proposed parent — if we encounter TagId, it's a cycle. + if (await WouldCreateCycleAsync(command.PrincipalId, payload.ParentTagId, payload.TagId, cancellationToken)) + return RejectInvariant(command, EntityRefs.TagCycle); + + // SetParentAsync returns false if tag or parent tag does not exist. + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + payload.TagId, + ct => _tagRepository.SetParentAsync(command.PrincipalId, payload.TagId, payload.ParentTagId, ct), + EntityRefs.TagNotFound, + _eventBus, + cancellationToken); + } + + /// + /// Walks up the ancestor chain starting at . + /// Returns true if appears, indicating a cycle. + /// + private async Task WouldCreateCycleAsync( + SystemId systemId, TagId candidateParentId, TagId childId, CancellationToken cancellationToken) + { + TagId? current = candidateParentId; + while (current is not null) + { + if (current == childId) return true; + current = await _tagRepository.GetParentIdAsync(systemId, current.Value, cancellationToken); + } + return false; + } + +} diff --git a/shared/Interfold.Tags.Domain/TagCommandFlow.cs b/shared/Interfold.Tags.Domain/TagCommandFlow.cs new file mode 100644 index 00000000..4c913321 --- /dev/null +++ b/shared/Interfold.Tags.Domain/TagCommandFlow.cs @@ -0,0 +1,73 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Events; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Commands; + +namespace Interfold.Tags.Domain; + +internal static class TagCommandFlow +{ + public static CommandExecutionResult Success(SystemId systemId, TagId tagId) + => CommandExecutionResult.Success(new TagCommandResult(systemId, tagId, Replay: false)); + + public static async Task> ExecuteCreateAsync( + CommandEnvelope command, + IClusterEventBus eventBus, + Func> createAsync, + CancellationToken cancellationToken = default) + { + var createdTagId = await createAsync(cancellationToken); + if (createdTagId is null) + return CommandHandler.RejectInvariant(command.OperationId, EntityRefs.TagCreateFailed); + + await eventBus.PublishAsync(new TagCreatedEvent(command.PrincipalId, createdTagId.Value), cancellationToken); + return Success(command.PrincipalId, createdTagId.Value); + } + + public static async Task?> RejectIfAlterNotFoundAsync( + CommandEnvelope command, + AlterId alterId, + IAlterExistenceCheck alterExistence, + CancellationToken cancellationToken = default) + { + var alterExists = await alterExistence.ExistsAsync(command.PrincipalId, alterId, cancellationToken); + return alterExists + ? null + : CommandHandler.RejectInvariant(command.OperationId, EntityRefs.TagAlterNotFound); + } + + public static async Task> ExecuteTagMutationAsync( + CommandEnvelope command, + TagId tagId, + Func> mutateAsync, + EntityRef notFoundEntityRef, + IClusterEventBus eventBus, + CancellationToken cancellationToken = default) + => await ExecuteTagMutationAsync( + command, + tagId, + mutateAsync, + notFoundEntityRef, + ct => eventBus.PublishAsync(new TagUpdatedEvent(command.PrincipalId, tagId), ct), + cancellationToken); + + public static async Task> ExecuteTagMutationAsync( + CommandEnvelope command, + TagId tagId, + Func> mutateAsync, + EntityRef notFoundEntityRef, + Func publishAccepted, + CancellationToken cancellationToken = default) + { + var mutated = await mutateAsync(cancellationToken); + if (!mutated) + return CommandHandler.RejectInvariant(command.OperationId, notFoundEntityRef); + + await publishAccepted(cancellationToken); + return Success(command.PrincipalId, tagId); + } +} \ No newline at end of file diff --git a/shared/Interfold.Tags.Domain/TagCommandValidation.cs b/shared/Interfold.Tags.Domain/TagCommandValidation.cs new file mode 100644 index 00000000..3ea2de03 --- /dev/null +++ b/shared/Interfold.Tags.Domain/TagCommandValidation.cs @@ -0,0 +1,22 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Operations; +using Interfold.Tags.Contracts.Models.Commands; + +namespace Interfold.Tags.Domain; + +internal static class TagCommandValidation +{ + public static EntityRef? GetNameValidationError(string? name) + { + if (name is not null && name.Length > 50) + return EntityRefs.TagNameTooLong; + + return null; + } + + public static bool HasNoMutableFields(UpdateTagCommand payload) + => payload.Name is null + && payload.Color is null + && payload.Description is null + && payload.SecurityLevel is null; +} diff --git a/shared/Interfold.Tags.Domain/UpdateTagCommandHandler.cs b/shared/Interfold.Tags.Domain/UpdateTagCommandHandler.cs new file mode 100644 index 00000000..e6659411 --- /dev/null +++ b/shared/Interfold.Tags.Domain/UpdateTagCommandHandler.cs @@ -0,0 +1,49 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Tags.Contracts; +using Interfold.Tags.Contracts.Models.Commands; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Tags.Domain; + +public sealed class UpdateTagCommandHandler : IdempotentCommandHandler +{ + private readonly ITagRepository _tagRepository; + private readonly IClusterEventBus _eventBus; + + public UpdateTagCommandHandler( + ITagRepository tagRepository, + IIdempotencyStore idempotencyStore, + IClusterEventBus eventBus) +:base(idempotencyStore) { + _tagRepository = tagRepository; + _eventBus = eventBus; + } + + + protected override EntityRef DuplicateEntityRef => EntityRefs.TagUpdate; + +protected override async Task> ExecuteCoreAsync ( + CommandEnvelope command, + CancellationToken cancellationToken = default) + { + var payload = command.Payload; + + if (TagCommandValidation.HasNoMutableFields(payload)) + return RejectInvariant(command, EntityRefs.TagNoFields); + + if (TagCommandValidation.GetNameValidationError(payload.Name) is { } validationError) + return RejectInvariant(command, validationError); + + return await TagCommandFlow.ExecuteTagMutationAsync( + command, + payload.TagId, + ct => _tagRepository.UpdateAsync(command.PrincipalId, payload, ct), + EntityRefs.TagNotFound, + _eventBus, + cancellationToken); + } + +} diff --git a/simple.Dockerfile b/simple.Dockerfile deleted file mode 100644 index 602f9bff..00000000 --- a/simple.Dockerfile +++ /dev/null @@ -1,97 +0,0 @@ -# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian -# instead of Alpine to avoid DNS resolution issues in production. -# -# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu -# https://hub.docker.com/_/ubuntu?tab=tags -# -# This file is based on these images: -# -# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image -# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20230612-slim - for the release image -# - https://pkgs.org/ - resource for finding needed packages -# - Ex: hexpm/elixir:1.15.6-erlang-25.3.2.6-debian-bullseye-20230612-slim -# -ARG ELIXIR_VERSION=1.19.5 -ARG OTP_VERSION=27.0 -ARG DEBIAN_VERSION=trixie-20260112-slim - -ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}" -ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}" - -FROM ${BUILDER_IMAGE} AS builder - -# install build dependencies -RUN apt-get update -y && apt-get install -y build-essential git \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# prepare build dir -WORKDIR /app - -# install hex + rebar -RUN mix local.hex --force && \ - mix local.rebar --force - -# set build ENV -ENV MIX_ENV="prod" - -# install mix dependencies -COPY mix.exs mix.lock ./ -RUN mix deps.get --only $MIX_ENV -RUN mkdir config - -# copy compile-time config files before we compile dependencies -# to ensure any relevant config change will trigger the dependencies -# to be re-compiled. -COPY config/config.exs config/${MIX_ENV}.exs config/ -RUN mix deps.compile - -COPY priv priv - -COPY lib lib - -COPY assets assets - -# Compile assets -RUN mix assets.deploy - -# Compile the release -RUN mix compile - -# Changes to config/runtime.exs don't require recompiling the code -COPY config/runtime.exs config/ - -COPY rel rel -RUN mix sentry.package_source_code -RUN mix release - -# start a new build stage so that the final image will only contain -# the compiled release and other runtime necessities -FROM ${RUNNER_IMAGE} - -RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates curl gnupg iproute2 iputils-ping jq \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* - -# Set the locale -RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen - -ENV LANG en_US.UTF-8 -ENV LANGUAGE en_US:en -ENV LC_ALL en_US.UTF-8 - -RUN mkdir /mnesia - -WORKDIR "/app" -RUN chown nobody /app -RUN chown nobody /mnesia - -# set runner ENV -ENV MIX_ENV="prod" - -# Only copy the final release from the build stage -COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/octocon ./ - -RUN chmod -R +x /app/bin - -USER nobody - -CMD ["/app/bin/server"] diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props new file mode 100644 index 00000000..f024e33e --- /dev/null +++ b/tests/Directory.Build.props @@ -0,0 +1,32 @@ + + + + false + true + + + diff --git a/tests/Directory.Build.targets b/tests/Directory.Build.targets new file mode 100644 index 00000000..704acad6 --- /dev/null +++ b/tests/Directory.Build.targets @@ -0,0 +1,15 @@ + + + + + + diff --git a/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AlterIdValidationTests.cs b/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AlterIdValidationTests.cs new file mode 100644 index 00000000..d8f645b0 --- /dev/null +++ b/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AlterIdValidationTests.cs @@ -0,0 +1,219 @@ +using System.Net; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Alters.IntegrationTests.Controllers; + +/// +/// Wire-boundary verification for the ValidAlterId refactor: every controller path +/// that previously accepted a nullable AlterId and defaulted it to new(0) so +/// invalid ids could leak past model binding must now short-circuit with a +/// 400 { code: "invalid_alter_id" } — the same wire shape callers already handle for +/// every other ErrorResponse. +/// +/// +/// The negative-path helpers below build their request bodies through +/// because the whole point is to send +/// id: 0 / alter_id: 0 — values that 's constructor +/// would happily accept in C#, but that must be rejected by ASP.NET Core model +/// binding before the controller action runs. Raw JSON keeps the wire bytes explicit and +/// avoids any typed-code path that would round-trip through ValidAlterId client-side. +/// +/// +/// +/// Deliberately isolated to only (unlike the other +/// controller test classes in this folder that fan out across Scylla + Cassandra + +/// InMemory). The behaviour under test is a framework-level model-binding short-circuit — +/// the request never reaches the persistence layer, so parameterising across backends +/// would be pure duplicated wire cost. Restricting to InMemory also lets these tests run +/// on a workstation without Docker: RequiredFixtures.Discover walks the +/// ClassDataSource attributes on the scheduled class set, so with only the InMemory +/// attribute here neither nor +/// gets discovered — and per +/// 's docstring, when neither is required that Aspire host is +/// not even instantiated (no Postgres container either). This is the same trick +/// MultiNodeScyllaTests uses to keep its docker footprint minimal. +/// +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class AlterIdValidationTests(InMemoryWebFactoryFixture fixture) : BaseEndpointTest +{ + private const string InvalidAlterIdCode = "invalid_alter_id"; + private const string InvalidAlterIdMessage = "Invalid alter ID."; + + // ---------- Fronting ---------- + + [Test] + public async Task FrontStart_ValidId_HappyPathStillWorks() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("alterid-front-ok"); + var alterId = await CreateAlterAsync(client, principal, "HappyPathAlter"); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/start", + new FrontStartRequest(alterId), + principal); + + await Assert.That(res.StatusCode) + .IsEqualTo(HttpStatusCode.Created) + .Because("valid alter ids must still succeed; the refactor only tightens the invalid branch."); + } + + [Test] + public async Task FrontStart_ZeroId_Returns400InvalidAlterId() + => await AssertInvalidAlterId(HttpMethod.Post, "/api/systems/me/front/start", "{\"id\":0}"); + + [Test] + public async Task FrontStart_MissingBody_Returns400InvalidAlterId() + // The record has `Id = default`, so an empty body deserialises to AlterId(0) and + // ValidAlterId rejects it exactly like an explicit `{ "id": 0 }`. + => await AssertInvalidAlterId(HttpMethod.Post, "/api/systems/me/front/start", "{}"); + + [Test] + public async Task FrontEnd_ZeroId_Returns400InvalidAlterId() + => await AssertInvalidAlterId(HttpMethod.Post, "/api/systems/me/front/end", "{\"id\":0}"); + + [Test] + public async Task FrontSet_ZeroId_Returns400InvalidAlterId() + => await AssertInvalidAlterId(HttpMethod.Post, "/api/systems/me/front/set", "{\"id\":0}"); + + [Test] + public async Task FrontPrimary_ZeroId_Returns400InvalidAlterId() + // FrontPrimary keeps a nullable AlterId (null clears the primary front) but a non-null + // out-of-range id must still be rejected: [ValidAlterId(AllowNull = true)] only + // skips the null check, not the range check. + => await AssertInvalidAlterId(HttpMethod.Post, "/api/systems/me/front/primary", "{\"id\":0}"); + + // ---------- Tags ---------- + + [Test] + public async Task TagAttachAlter_ZeroAlterId_Returns400InvalidAlterId() + { + // Tag id is a route token, not a body field — model binding for the body still runs + // and the ValidAlterId short-circuit fires before the tag lookup, so we can use any + // syntactically-valid GUID without setting the tag up in the store. + var tagId = Guid.NewGuid(); + await AssertInvalidAlterId(HttpMethod.Post, $"/api/systems/me/tags/{tagId}/alter", "{\"alter_id\":0}"); + } + + [Test] + public async Task TagDetachAlter_ZeroAlterId_Returns400InvalidAlterId() + { + var tagId = Guid.NewGuid(); + await AssertInvalidAlterId(HttpMethod.Delete, $"/api/systems/me/tags/{tagId}/alter", "{\"alter_id\":0}"); + } + + // ---------- Journals ---------- + + [Test] + public async Task JournalAttachAlter_ZeroAlterId_Returns400InvalidAlterId() + { + var journalId = Guid.NewGuid(); + await AssertInvalidAlterId(HttpMethod.Post, $"/api/journals/{journalId}/alter", "{\"alter_id\":0}"); + } + + [Test] + public async Task JournalDetachAlter_ZeroAlterId_Returns400InvalidAlterId() + { + var journalId = Guid.NewGuid(); + await AssertInvalidAlterId(HttpMethod.Delete, $"/api/journals/{journalId}/alter", "{\"alter_id\":0}"); + } + + // ---------- Route alterId ---------- + + [Test] + public async Task AltersShow_ZeroRouteAlterId_Returns400InvalidAlterId() + => await AssertInvalidAlterIdRoute(HttpMethod.Get, "/api/systems/me/alters/0"); + + [Test] + public async Task AltersUpdate_ZeroRouteAlterId_Returns400InvalidAlterId() + => await AssertInvalidAlterIdRoute(HttpMethod.Patch, "/api/systems/me/alters/0", "{\"name\":\"ignored\"}"); + + [Test] + public async Task AltersDelete_ZeroRouteAlterId_Returns400InvalidAlterId() + => await AssertInvalidAlterIdRoute(HttpMethod.Delete, "/api/systems/me/alters/0"); + + [Test] + public async Task AlterJournalsIndex_ZeroRouteAlterId_Returns400InvalidAlterId() + => await AssertInvalidAlterIdRoute(HttpMethod.Get, "/api/systems/me/alters/0/journals"); + + [Test] + public async Task PublicSystemsShowAlter_ZeroRouteAlterId_Returns400InvalidAlterId() + { + var systemId = TestIds.NewSystemId("public-alterid"); + await AssertInvalidAlterIdRoute(HttpMethod.Get, $"/api/systems/{systemId}/alters/0"); + } + + // ---------- helpers ---------- + + private async Task AssertInvalidAlterId(HttpMethod method, string path, string rawJsonBody) + { + using var client = TestClient.NoRedirect(fixture); + + // A fresh principal per call keeps the negative tests independent of any tag / journal + // rows created by other tests in the same session — the model-binding 400 fires + // before the controller reads persistence, but authorisation still runs, so we need + // a legitimate token bound to a real user row. + var principal = TestIds.NewSystemId("alterid-neg"); + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendRawJsonAsync(method, path, rawJsonBody, principal); + var error = await res.ReadErrorAsync(HttpStatusCode.BadRequest); + + using (Assert.Multiple()) + { + await Assert.That(error.Code.Value) + .IsEqualTo(InvalidAlterIdCode) + .Because("the ErrorResponse code must survive the DataAnnotation → ErrorResponse mapping."); + + await Assert.That(error.Error) + .IsEqualTo(InvalidAlterIdMessage) + .Because("the error message must be the sentinel used by ValidationErrorCodeRegistry."); + } + } + + private async Task AssertInvalidAlterIdRoute(HttpMethod method, string path, string? rawJsonBody = null) + { + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("alterid-route-neg"); + await EnsureUserExistsAsync(client, principal); + + HttpResponseMessage res; + if (rawJsonBody is null) + { + using var req = new HttpRequestMessage(method, path); + AttachPrincipalAuth(req, client, principal); + res = await client.SendAsync(req); + } + else + { + res = await client.SendRawJsonAsync(method, path, rawJsonBody, principal); + } + + try + { + var error = await res.ReadErrorAsync(HttpStatusCode.BadRequest); + + using (Assert.Multiple()) + { + await Assert.That(error.Code.Value) + .IsEqualTo(InvalidAlterIdCode) + .Because("the ErrorResponse code must survive route-parameter validation mapping."); + + await Assert.That(error.Error) + .IsEqualTo(InvalidAlterIdMessage) + .Because("the error message must be the sentinel used by ValidationErrorCodeRegistry."); + } + } + finally + { + res.Dispose(); + } + } +} + diff --git a/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AltersControllerTests.cs b/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AltersControllerTests.cs new file mode 100644 index 00000000..02b73d6c --- /dev/null +++ b/tests/integration/Interfold.Alters.IntegrationTests/Controllers/AltersControllerTests.cs @@ -0,0 +1,279 @@ +using System.Net; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Alters.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class AltersControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task FieldSecurityLevelByRelationship_AppliesCorrectly() + { + using var client = TestClient.NoRedirect(fixture); + await RunFieldVisibilityScenarioAsync(client, "parity-guarded-fields"); + } + + [Test] + public async Task CustomFields_FieldSecurityLevelByRelationship_AppliesCorrectly() + { + // Uses the plain (redirect-following) client rather than TestClient.NoRedirect(fixture); + // the mirror test above pins the no-redirect codepath, this one pins the default + // HttpClient shape. The shared body doesn't hit any redirect endpoints, so both clients + // should surface identical results — a divergence here would surface a redirect-behaviour + // regression in the guarded read path. + using var client = fixture.Factory.CreateClient(); + await RunFieldVisibilityScenarioAsync(client, "settings-guarded-fields"); + } + + /// + /// Shared 60-line body for the two field-visibility tests. Seeds the quartet, mints four + /// per-visibility settings fields on the owner, creates a public alter, populates the four + /// custom-field values, then asserts each viewer level (non-friend / friend / trusted) sees + /// the exact expected slice of values in the guarded alter read. + /// + /// + /// Both callers pin the *same* server behaviour with a different client (no-redirect vs + /// default). Extracted so a future assertion tweak (adding a fifth visibility level, + /// renaming the field marker strings) lands in one place instead of drifting between the + /// two entry points — which is exactly how the line-45 friend → nonFriend bug slipped + /// into the tree before this refactor. + /// + private static async Task RunFieldVisibilityScenarioAsync(HttpClient client, string prefix) + { + var (owner, nonFriend, friend, trusted) = await SeedVisibilityQuartetAsync(client, prefix); + + var fieldPublic = await CreateSettingsFieldAsync(client, owner, "FieldPublic", FieldType.Text, VisibilityLevel.Public); + var fieldFriends = await CreateSettingsFieldAsync(client, owner, "FieldFriends", FieldType.Text, VisibilityLevel.FriendsOnly); + var fieldTrusted = await CreateSettingsFieldAsync(client, owner, "FieldTrusted", FieldType.Text, VisibilityLevel.TrustedOnly); + var fieldPrivate = await CreateSettingsFieldAsync(client, owner, "FieldPrivate", FieldType.Text, VisibilityLevel.Private); + + var alterId = await CreateAlterAsync(client, owner, "GuardedFieldsAlter"); + await SetAlterSecurityLevelAsync(client, owner, alterId, VisibilityLevel.Public); + await UpdateAlterFieldsAsync(client, owner, alterId, new UpdateAlterFieldRequest[] + { + new(fieldPublic, "PublicValue"), + new(fieldFriends, "FriendsValue"), + new(fieldTrusted, "TrustedValue"), + new(fieldPrivate, "PrivateValue"), + }); + + using var nonFriendRes = await client.SendAuthedGetAsync($"/api/systems/{owner}/alters/{alterId}", nonFriend); + var nonFriendBody = await nonFriendRes.Content.ReadAsStringAsync(); + using (Assert.Multiple()) + { + await Assert.That(nonFriendRes.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(nonFriendBody).Contains("PublicValue"); + await Assert.That(nonFriendBody).DoesNotContain("FriendsValue"); + await Assert.That(nonFriendBody).DoesNotContain("TrustedValue"); + await Assert.That(nonFriendBody).DoesNotContain("PrivateValue"); + } + + await SendFriendRequestAndAcceptAsync(client, friend, owner); + + using var friendRes = await client.SendAuthedGetAsync($"/api/systems/{owner}/alters/{alterId}", friend); + var friendBody = await friendRes.Content.ReadAsStringAsync(); + using (Assert.Multiple()) + { + await Assert.That(friendRes.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(friendBody).Contains("PublicValue"); + await Assert.That(friendBody).Contains("FriendsValue"); + await Assert.That(friendBody).DoesNotContain("TrustedValue"); + await Assert.That(friendBody).DoesNotContain("PrivateValue"); + } + + await SendFriendRequestAndAcceptAsync(client, trusted, owner); + await SetFriendTrustAsync(client, owner, trusted); + + using var trustedRes = await client.SendAuthedGetAsync($"/api/systems/{owner}/alters/{alterId}", trusted); + var trustedBody = await trustedRes.Content.ReadAsStringAsync(); + using (Assert.Multiple()) + { + await Assert.That(trustedRes.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(trustedBody).Contains("PublicValue"); + await Assert.That(trustedBody).Contains("FriendsValue"); + await Assert.That(trustedBody).Contains("TrustedValue"); + await Assert.That(trustedBody).DoesNotContain("PrivateValue"); + } + } + + + + [Test] + public async Task OwnerView_ListAndGet_ReturnsAllFieldValuesIncludingPrivate() + { + // Regression: the owner-view read paths (/api/systems/me/alters and .../{id}) must return + // populated field values at every VisibilityLevel — Public through Private. Before the fix, + // InMemoryAlterRepository fed FriendshipLevel.TrustedFriend through the guarded projection, + // which stripped Private-tier field definitions and dropped unpopulated ones, so the client + // never saw updates to Private fields. Scylla + Cassandra already did the right thing. + using var client = TestClient.NoRedirect(fixture); + + var owner = TestIds.NewSystemId("owner-view-fields"); + + var fieldPublic = await CreateSettingsFieldAsync(client, owner, "OwnerFieldPublic", FieldType.Text, VisibilityLevel.Public); + var fieldFriends = await CreateSettingsFieldAsync(client, owner, "OwnerFieldFriends", FieldType.Text, VisibilityLevel.FriendsOnly); + var fieldTrusted = await CreateSettingsFieldAsync(client, owner, "OwnerFieldTrusted", FieldType.Text, VisibilityLevel.TrustedOnly); + var fieldPrivate = await CreateSettingsFieldAsync(client, owner, "OwnerFieldPrivate", FieldType.Text, VisibilityLevel.Private); + + var alterId = await CreateAlterAsync(client, owner, "OwnerViewFieldsAlter"); + await UpdateAlterFieldsAsync(client, owner, alterId, new UpdateAlterFieldRequest[] + { + new(fieldPublic, "OwnerPublicValue"), + new(fieldFriends, "OwnerFriendsValue"), + new(fieldTrusted, "OwnerTrustedValue"), + new(fieldPrivate, "OwnerPrivateValue"), + }); + + using var listRes = await client.SendAuthedGetAsync("/api/systems/me/alters", owner); + var listBody = await listRes.Content.ReadAsStringAsync(); + using (Assert.Multiple()) + { + await Assert.That(listRes.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(listBody).Contains("OwnerPublicValue"); + await Assert.That(listBody).Contains("OwnerFriendsValue"); + await Assert.That(listBody).Contains("OwnerTrustedValue"); + await Assert.That(listBody).Contains("OwnerPrivateValue"); + } + + using var getRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/{alterId}", owner); + var getBody = await getRes.Content.ReadAsStringAsync(); + using (Assert.Multiple()) + { + await Assert.That(getRes.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(getBody).Contains("OwnerPublicValue"); + await Assert.That(getBody).Contains("OwnerFriendsValue"); + await Assert.That(getBody).Contains("OwnerTrustedValue"); + await Assert.That(getBody).Contains("OwnerPrivateValue"); + } + } + + + [Test] + public async Task AlterDelete_CascadesAlterJournalsAndDetachesFromGlobalJournals() + { + // Regression: deleting an alter must wipe its alter_journals entries (both view tables + // in the Scylla repo) and detach it from any global_journal_alters rows, while leaving + // the global journal itself intact (multiple alters can share a group journal). + // Before the fix, DeleteAlterCommandHandler called only IAlterRepository.DeleteAsync and + // left every alter-journal entry orphaned in the database. + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("parity-alter-delete-cascade"); + var deletedAlterId = await CreateAlterAsync(client, principal, "DeleteMe"); + var keeperAlterId = await CreateAlterAsync(client, principal, "KeepMe"); + + // Create an alter-journal entry owned by the alter we're about to delete. After the + // delete, the per-entry GET must 404 (the cascade wiped it). + using var alterJournalRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/systems/me/alters/{deletedAlterId}/journals", + new CreateAlterJournalRequest("AlterJournalToCascade"), + principal); + var alterJournalEnv = await alterJournalRes.ReadEnvelopeAsync(HttpStatusCode.Created); + var alterJournalEntryId = alterJournalEnv.Data.Id; + + // Create a global journal and attach BOTH alters. After the cascade, the global + // journal must still exist with the keeper alter still attached. + using var globalRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/journals", + new CreateGlobalJournalRequest("GroupJournalShared"), + principal); + var globalEnv = await globalRes.ReadEnvelopeAsync(HttpStatusCode.Created); + var globalJournalId = globalEnv.Data.Id; + + foreach (var alterIdToAttach in new[] { deletedAlterId, keeperAlterId }) + { + using var attachRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/journals/{globalJournalId}/alter", + new JournalAlterRequest(alterIdToAttach), + principal); + await Assert.That(attachRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + } + + // Confirm setup: both alters attached before the delete. + using var preDeleteRes = await client.SendAuthedGetAsync($"/api/journals/{globalJournalId}", principal); + var preDeleteEnv = await preDeleteRes.ReadEnvelopeAsync(HttpStatusCode.OK); + using (Assert.Multiple()) + { + await Assert.That(preDeleteEnv.Data.Alters).Contains(deletedAlterId); + await Assert.That(preDeleteEnv.Data.Alters).Contains(keeperAlterId); + } + + using var deleteAlterRes = await client.SendAuthedDeleteAsync($"/api/systems/me/alters/{deletedAlterId}", principal); + await Assert.That(deleteAlterRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // Cascade 1: the per-alter journal entry is gone. + using var showAlterJournalRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/journals/{alterJournalEntryId}", principal); + await Assert.That(showAlterJournalRes.StatusCode) + .IsEqualTo(HttpStatusCode.NotFound) + .Because("the alter's journal entry should be cascade-deleted along with the alter."); + + // Cascade 2: the global journal still exists, but the deleted alter is no longer in + // its alter_ids list. The keeper alter must still be attached. + using var postDeleteRes = await client.SendAuthedGetAsync($"/api/journals/{globalJournalId}", principal); + var postDeleteEnv = await postDeleteRes.ReadEnvelopeAsync(HttpStatusCode.OK); + using (Assert.Multiple()) + { + await Assert.That(postDeleteEnv.Data.Alters).DoesNotContain(deletedAlterId); + await Assert.That(postDeleteEnv.Data.Alters).Contains(keeperAlterId); + } + } + + + [Test] + public async Task AlterCreate_IdempotentReplay_WorksAgainstLiveAdapters() + { + using var client = fixture.Factory.CreateClient(); + + var systemId = TestIds.NewSystemId("itest", maxLen: 14); + var idempotencyKey = Guid.NewGuid().ToString("N"); + + using var firstRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest("IntegrationSmoke"), + systemId, + idempotencyKey: idempotencyKey); + var firstEnv = await firstRes.ReadEnvelopeAsync(HttpStatusCode.Created); + await Assert.That(firstEnv.Replay.GetValueOrDefault()).IsFalse(); + + using var secondRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest("IntegrationSmoke"), + systemId, + idempotencyKey: idempotencyKey); + var secondEnv = await secondRes.ReadEnvelopeAsync(HttpStatusCode.Created); + await Assert.That(secondEnv.Replay).IsTrue(); + } + + [Test] + public async Task OperationalHealth_GuardedPaths_GetGuardedAsync_Succeeds() + { + using var client = TestClient.NoRedirect(fixture); + + // Query a non-existent alter should return 404, not 500 + var principal = "operational-health-test"; + using var res = await client.SendAuthedGetAsync("/api/systems/me/alters/9999", principal); + + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.NotFound); + } + + [Test] + public async Task Idempotency_AlterCreate_ReplayStable() + { + await RunSoakAsync(fixture.Factory, async (client, key) => + { + return await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest("SoakAlter"), + "soak-default-principal", + idempotencyKey: key); + }); + } +} + diff --git a/tests/integration/Interfold.Alters.IntegrationTests/Interfold.Alters.IntegrationTests.csproj b/tests/integration/Interfold.Alters.IntegrationTests/Interfold.Alters.IntegrationTests.csproj new file mode 100644 index 00000000..925e0845 --- /dev/null +++ b/tests/integration/Interfold.Alters.IntegrationTests/Interfold.Alters.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Alters.IntegrationTests/Services/Scylla/ScyllaAlterRepositoryUdtNullTests.cs b/tests/integration/Interfold.Alters.IntegrationTests/Services/Scylla/ScyllaAlterRepositoryUdtNullTests.cs new file mode 100644 index 00000000..75163581 --- /dev/null +++ b/tests/integration/Interfold.Alters.IntegrationTests/Services/Scylla/ScyllaAlterRepositoryUdtNullTests.cs @@ -0,0 +1,128 @@ +using Cassandra; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Infrastructure.Scylla.Repository; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Domain.Abstractions.Repository; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Alters.IntegrationTests.Services.Scylla; + +/// +/// Direct-to-repository coverage for 's handling of +/// nullable UDT / column shapes. The integration suite covers happy-path CRUD end-to-end +/// through the HTTP pipeline (AltersControllerTests / PublicSystemsControllerTests), +/// but corrupt or partially-null on-disk rows can't be produced through the public API — +/// the wire boundary rejects them before they reach Scylla. Both scenarios below insert +/// the pathological row directly via the cluster session, then drive the production +/// against it. +/// +/// +/// Companion to : same fixture, +/// same "direct-CQL synthesise, then production-repo read" template, same rationale +/// (things that can't reach the DB via the wire but must not blow up the read path). +/// +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class ScyllaAlterRepositoryUdtNullTests(ScyllaWebFactoryFixture fixture) : BaseEndpointTest +{ + /// + /// A row with security_level = NULL maps to : + /// non-friend viewers are filtered out; the owner still sees the alter. + /// + [Test] + public async Task GetGuardedAsync_NullSecurityLevelOnRow_TreatsAsPrivate() + { + var factory = fixture.Factory; + using var client = factory.CreateClient(); + + var rawSystemId = TestIds.NewSystemId("sys-null-sec"); + var systemId = new SystemId(rawSystemId); + var seededAlterId = await CreateAlterAsync(client, rawSystemId, "seed-alter"); + + var alterRepo = factory.Services.GetRequiredService(); + var (session, keyspace, normalizedSystemId) = await ScyllaDirectHarness.ResolveAsync(fixture, systemId); + + await session.ExecuteAsync(new SimpleStatement( + $"INSERT INTO {keyspace}.alters (user_id, id, name, security_level) VALUES (?, ?, ?, ?)", + normalizedSystemId, + seededAlterId.Value, + "seed-alter-nulled", + (short?)null)); + + await Assert.That(await alterRepo.GetGuardedAsync(systemId, seededAlterId, viewerSystemId: null)) + .IsNull() + .Because("Null security_level must fail closed as Private, so a non-friend viewer does not see the alter."); + + var owner = await alterRepo.GetAsync(systemId, seededAlterId); + await Assert.That(owner).IsNotNull(); + await Assert.That(owner!.SecurityLevel).IsEqualTo(VisibilityLevel.Private) + .Because("The owner list/join path must still return the row; Private is the fail-closed stand-in for a missing code."); + } + + /// + /// The UDT list on alters.fields permits null values per column + /// (alter_field (id uuid, value text)), so a partial-migration or manual write + /// can produce a UDT entry with value = NULL. The projection must surface such + /// entries with a null rather than + /// throwing or silently dropping the whole entry — the field is still projection-visible + /// (the alter has "opinions" about it), it just doesn't carry a value. + /// + [Test] + public async Task GetGuardedAsync_PartiallyNullFieldUdt_EmitsFieldWithNullValue() + { + var factory = fixture.Factory; + using var client = factory.CreateClient(); + + var rawSystemId = TestIds.NewSystemId("sys-null-udt"); + var systemId = new SystemId(rawSystemId); + var seededAlterId = await CreateAlterAsync(client, rawSystemId, "field-udt-alter"); + + var settingsFields = factory.Services.GetRequiredService(); + var alterRepo = factory.Services.GetRequiredService(); + var (session, keyspace, normalizedSystemId) = await ScyllaDirectHarness.ResolveAsync(fixture, systemId); + + // Field definition drives the projection filter — without a matching definition, + // ResolveGuardedFields skips the UDT entry entirely (that's the unrelated path). + var fieldId = (await settingsFields.CreateAsync( + systemId, + "NullableField", + FieldType.Text, + VisibilityLevel.Public, + locked: false, + DateTime.UtcNow))!.Value; + + // Ensure the alter-field UDT mapping is registered on the cluster session + // (production reads/writes register it lazily on first hit; the direct-CQL + // write below binds a strongly-typed AlterFieldUdt and would otherwise hit + // "No UDT descriptor found"). + ScyllaAlterRepository.EnsureAlterFieldUdtMapping(session, keyspace); + + var udtWithNullValue = new List + { + new() { Id = fieldId.Value, Value = null }, + }; + + await session.ExecuteAsync(new SimpleStatement( + $"UPDATE {keyspace}.alters SET fields = ? WHERE user_id = ? AND id = ?", + udtWithNullValue, + normalizedSystemId, + seededAlterId.Value)); + + var view = await alterRepo.GetGuardedAsync(systemId, seededAlterId, viewerSystemId: null); + + using (Assert.Multiple()) + { + await Assert.That(view).IsNotNull() + .Because("A public-visibility alter with a null-valued field UDT still resolves for an anonymous viewer — the null value doesn't break the row's readability."); + await Assert.That(view!.Fields.Count).IsEqualTo(1) + .Because("The projection must surface exactly the one field the alter has an opinion about — the null Value is a stored opinion, not a missing definition."); + await Assert.That(view.Fields[0].Id).IsEqualTo(fieldId); + await Assert.That(view.Fields[0].Value).IsNull() + .Because("The UDT's null Value must round-trip as a null AlterPublicFieldReadModel.Value — silently coercing to empty string would misrepresent \"no data\" as \"blank string\"."); + } + } +} diff --git a/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthControllerTests.cs b/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthControllerTests.cs new file mode 100644 index 00000000..353da0bb --- /dev/null +++ b/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthControllerTests.cs @@ -0,0 +1,163 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Read; +using Interfold.Auth.Contracts.Configuration; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Interfold.Auth.IntegrationTests.Controllers; + +/// +/// Integration tests for authentication, OAuth flows, and WebSocket upgrades. +/// Refactored to use WebApplicationFactory for in-memory testing. +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class AuthControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task Api_AuthRequest_FallsBackTo403_WhenChallengeDisabled() + { + using var client = TestClient.NoRedirect(fixture); + + var response = await client.GetAsync("/auth/google"); + var body = await response.Content.ReadAsStringAsync(); + + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.Forbidden); + } + + //TODO: DO we need this? Maybe can split it up another way... + [Test] + public async Task Api_AuthAndIdempotencyFlow_VerifiesEndToEndBehavior() + { + using var client = fixture.Factory.CreateClient(); + var principalId = "sys-api-smoke"; + + // Unauthorized check — the anonymous object with a `name` field is intentional here. + // The endpoint never sees the body (auth middleware blocks first), so a typed + // CreateAlterRequest would only obscure that this is a stand-alone shape assertion: + // that a valid-looking POST body without a principal still returns 401. + var unauthorized = await client.PostAsJsonAsync("/api/systems/me/alters", new { name = "NoPrincipal" }); + await Assert.That(unauthorized.StatusCode).IsEqualTo(HttpStatusCode.Unauthorized); + + // First creation + var idempotencyKey = Guid.NewGuid().ToString("N"); + using var firstRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest("IntegrationOne"), + principalId, + idempotencyKey: idempotencyKey); + var firstEnv = await firstRes.ReadEnvelopeAsync(HttpStatusCode.Created); + // Fresh writes never carry the replay flag on the wire — SuccessResponse omits it + // when null, so a null Replay here is the "first time through" signal. + await Assert.That(firstEnv.Replay.GetValueOrDefault()).IsFalse(); + + // Replay check — same key, same body ⇒ handler must short-circuit and stamp replay=true. + using var secondRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest("IntegrationOne"), + principalId, + idempotencyKey: idempotencyKey); + var secondEnv = await secondRes.ReadEnvelopeAsync(HttpStatusCode.Created); + await Assert.That(secondEnv.Replay).IsTrue(); + + using var listRes = await client.SendAuthedGetAsync("/api/systems/me/alters", principalId); + var listEnv = await listRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(listEnv.Data.Count).IsGreaterThan(0); + } + + + [Test] + public async Task Api_OAuthCallback_IssuesJwsCompactSerializationToken() + { + using var client = TestClient.NoRedirect(fixture); + + // The callback now strictly requires a client-supplied redirect_uri (stashed by the + // initial GET /auth/discord call into octocon_auth_redirect_uri). Since this test + // hits the callback directly without going through the begin step, inject the cookie + // by hand so the controller can construct a Location header — the JWT-shape + // assertions below are the actual unit under test. + var discordUid = $"jws-test-{Guid.NewGuid():N}"; + var clientRedirectUri = "https://test.invalid/auth/done"; + var callbackRequest = new HttpRequestMessage( + HttpMethod.Get, + $"/auth/discord/callback?uid={Uri.EscapeDataString(discordUid)}"); + callbackRequest.Headers.Add("Cookie", + $"octocon_auth_redirect_uri={Uri.EscapeDataString(clientRedirectUri)}"); + var response = await client.SendAsync(callbackRequest); + var body = await response.Content.ReadAsStringAsync(); + + await Assert.That(response.StatusCode) + .Satisfies(x => x is HttpStatusCode.Redirect or HttpStatusCode.Found + or HttpStatusCode.MovedPermanently or HttpStatusCode.TemporaryRedirect + or HttpStatusCode.PermanentRedirect) + .Because($"Expected OAuth callback redirect response, got {(int)response.StatusCode}. Body: {body}"); + + var location = response.Headers.Location + ?? throw new InvalidOperationException("Expected Location header in redirect response."); + + var query = System.Web.HttpUtility.ParseQueryString(location.Query); + var token = query["token"]; + + await Assert.That(token).IsNotNullOrWhiteSpace(); + + var segments = token!.Split('.'); + await Assert.That(segments.Length).IsEqualTo(3); + + using (Assert.Multiple()) + { + foreach (var (segment, index) in segments.Select((s, i) => (s, i))) + { + await Assert.That(segment).IsNotNullOrWhiteSpace(); + await Assert.That(!segment.Contains('+') && !segment.Contains('/') && !segment.Contains('=')).IsTrue(); + } + } + + var headerBytes = Base64UrlDecodeBytes(segments[0]); + using var headerDoc = JsonDocument.Parse(headerBytes); + var alg = headerDoc.RootElement.GetProperty("alg").GetString(); + var typ = headerDoc.RootElement.GetProperty("typ").GetString(); + using (Assert.Multiple()) + { + await Assert.That(alg).IsEqualTo("ES256"); + await Assert.That(typ).IsEqualTo("JWT"); + } + + var payloadBytes = Base64UrlDecodeBytes(segments[1]); + using var payloadDoc = JsonDocument.Parse(payloadBytes); + var root = payloadDoc.RootElement; + + var authConfig = fixture.Factory.Services.GetRequiredService>().CurrentValue; + var expectedIssuer = authConfig.JwtAuthority; + + using (Assert.Multiple()) + { + await Assert.That( + root.TryGetProperty("iss", out var iss) + && !string.IsNullOrWhiteSpace(iss.GetString()) + && (string.IsNullOrWhiteSpace(expectedIssuer) + || string.Equals(iss.GetString(), expectedIssuer, StringComparison.Ordinal))) + .IsTrue(); + await Assert.That(root.TryGetProperty("sub", out var sub) && !string.IsNullOrWhiteSpace(sub.GetString())).IsTrue(); + } + + long iatVal = 0; + long expVal = 0; + + using (Assert.Multiple()) + { + await Assert.That(root.TryGetProperty("iat", out var iat) && iat.TryGetInt64(out iatVal) && iatVal > 0).IsTrue(); + await Assert.That(root.TryGetProperty("nbf", out var nbf) && nbf.TryGetInt64(out _)).IsTrue(); + await Assert.That(root.TryGetProperty("exp", out var exp) && exp.TryGetInt64(out expVal) && expVal > iatVal).IsTrue(); + await Assert.That(root.TryGetProperty("jti", out var jti) && !string.IsNullOrWhiteSpace(jti.GetString())).IsTrue(); + await Assert.That(root.TryGetProperty("scope", out var scope) && + string.Equals(scope.GetString(), "octocon:deeplink", StringComparison.Ordinal)).IsTrue(); + } + } +} + diff --git a/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthLinkControllerTests.cs b/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthLinkControllerTests.cs new file mode 100644 index 00000000..ccf421ff --- /dev/null +++ b/tests/integration/Interfold.Auth.IntegrationTests/Controllers/AuthLinkControllerTests.cs @@ -0,0 +1,170 @@ +using System.Net; +using Interfold.Auth.Contracts.Ids; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Settings.Contracts.Models.Read; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Auth.IntegrationTests.Controllers; + +[Category("Auth")] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class AuthLinkControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + private static string UniqueId(string prefix) => TestIds.NewSystemId(prefix, maxLen: 24); + + [Test] + public async Task Begin_WithoutRedirectUri_ReturnsBadRequest() + { + using var client = TestClient.NoRedirect(fixture); + + using var res = await client.GetAsync("/auth/link/discord"); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + + var error = await res.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(error.Code).IsEqualTo(ErrorCodes.MissingRedirectUri); + } + + [Test] + public async Task Callback_FullFlow_Succeeds() + { + // Callback exercises the end-to-end LinkToken → Callback → LinkIdentityToUserAsync + // pipeline, which needs BOTH the seed (POST-equivalent to the link_token endpoint's + // Primary-only write) AND the callback to land on the same host / repository + // instance. WebApplicationFactory can hand back different IServiceProvider + // trees across `Factory.Services` vs an HTTP request scope, so a repo access via + // `Factory.Services` seeds a DIFFERENT InMemory dict than the one the callback + // resolves against. Using a private factory pinned to NodeGroup=Primary keeps the + // whole flow inside a single client / single SP, and the /api/settings/link_token + // endpoint's write branch (SingletonTaskOwner.OwnsTask(...) == true) actually mints + // the token. Sibling test below carries the same rationale. + await using var factory = CreatePrimaryNodeFactory(); + using var client = TestClient.NoRedirect(factory); + var user = UniqueId("link-flow-u"); + await EnsureUserExistsAsync(client, user); + + // 1. Get Link Token for the user (Primary-node write path — enabled by the + // factory config above so /api/settings/link_token mints instead of returning 503). + using var tokenRes = await client.SendAuthedGetAsync("/api/settings/link_token", user); + var tokenEnv = await tokenRes.ReadEnvelopeAsync(HttpStatusCode.OK); + var token = tokenEnv.Data.Token; + await Assert.That(token.Value).IsNotEmpty(); + + // 2. Perform Link Callback + var discordIdStr = $"discord-link-{Guid.NewGuid():N}"; + var clientRedirectUri = "https://test.invalid/link/done"; + + var callbackRequest = new HttpRequestMessage( + HttpMethod.Get, + $"/auth/link/discord/callback?uid={Uri.EscapeDataString(discordIdStr)}"); + callbackRequest.Headers.Add("Cookie", + $"octocon_link_token={Uri.EscapeDataString(token.Value)}; octocon_link_redirect_uri={Uri.EscapeDataString(clientRedirectUri)}"); + + using var response = await client.SendAsync(callbackRequest); + await Assert.That(response.StatusCode) + .Satisfies(x => x is HttpStatusCode.Redirect or HttpStatusCode.Found + or HttpStatusCode.MovedPermanently or HttpStatusCode.TemporaryRedirect + or HttpStatusCode.PermanentRedirect) + .Because($"Expected redirect to client redirect URI, got {(int)response.StatusCode}"); + + var location = response.Headers.Location?.ToString(); + await Assert.That(location).IsEqualTo(clientRedirectUri); + + // 3. Verify in repository that identity is linked. Repositories return the scoped + // wire form (e.g. "nam:link-flow-u-abc") — the raw region tag varies per adapter + // (InMemory hashes the id, Scylla resolves by the operator-configured keyspace), + // so compare on the region-stripped raw id, which is stable across all backends. + var accountRepo = factory.Services.GetRequiredService(); + var linkedSystemId = await accountRepo.TryFindSystemIdByDiscordIdAsync(new DiscordId(discordIdStr)); + await Assert.That(linkedSystemId).IsNotNull(); + await Assert.That(ScopedSystemId.StripRegionPrefix(linkedSystemId!.Value)).IsEqualTo(user); + } + + [Test] + public async Task Callback_WhenAlreadyLinkedToAnotherUser_Returns500InternalServerError() + { + // See Callback_FullFlow_Succeeds for the private-factory rationale — same story + // (Primary-node write path for /api/settings/link_token + single-SP seed→callback + // chain). + await using var factory = CreatePrimaryNodeFactory(); + using var client = TestClient.NoRedirect(factory); + var userA = UniqueId("link-err-a"); + var userB = UniqueId("link-err-b"); + await EnsureUserExistsAsync(client, userA); + await EnsureUserExistsAsync(client, userB); + + // Link user A to discord ID via the domain repository — the write must land on + // the same singleton graph the HTTP callback below will read from, so resolve it + // off `factory.Services` (which for a private factory is the ONLY SP tree since + // there's no cross-factory SP fragmentation to worry about — the private factory + // is instantiated seconds ago and only this test client has touched it). + var accountRepo = factory.Services.GetRequiredService(); + var discordIdStr = $"discord-err-{Guid.NewGuid():N}"; + var linkResult = await accountRepo.LinkIdentityToUserAsync( + ScopedSystemId.Compose(ScyllaKeyspace.Nam, userA).AsSystemId(), + ProviderIdentity.FromDiscord(new DiscordId(discordIdStr))); + await Assert.That(linkResult).IsEqualTo(AccountLinkResult.Success); + + // Get link token for user B via HTTP (Primary-node write path). + using var tokenRes = await client.SendAuthedGetAsync("/api/settings/link_token", userB); + var tokenEnv = await tokenRes.ReadEnvelopeAsync(HttpStatusCode.OK); + var token = tokenEnv.Data.Token; + + // Try linking discord ID to user B + var clientRedirectUri = "https://test.invalid/link/done"; + var callbackRequest = new HttpRequestMessage( + HttpMethod.Get, + $"/auth/link/discord/callback?uid={Uri.EscapeDataString(discordIdStr)}"); + callbackRequest.Headers.Add("Cookie", + $"octocon_link_token={Uri.EscapeDataString(token.Value)}; octocon_link_redirect_uri={Uri.EscapeDataString(clientRedirectUri)}"); + + using var response = await client.SendAsync(callbackRequest); + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.InternalServerError); + } + + /// + /// Build a private, Primary-node-pinned + /// that mirrors the parent fixture's backend wiring (Postgres connection string, + /// CQL endpoint port). Persistence mode is read off + /// so no fixture-type + /// switch is needed here — the enum on the shared factory is the single source of + /// truth, and inside the factory ctor + /// produces the wire spelling. + /// + private InterfoldWebApplicationFactory CreatePrimaryNodeFactory() + { + var factory = new InterfoldWebApplicationFactory(fixture.Factory.PersistenceMode) + .WithConfiguration(OctoconEnvKeys.NodeGroup, NodeGroup.Primary.ToWire()); + + // CQL-backed fixtures still need the operator-supplied connection details the shared + // fixture layered onto its own factory. Only Scylla and Cassandra carry this shape; + // InMemory has no external backend and needs no extra config beyond the mode above. + (string PostgresConnection, int CqlPort)? backend = fixture switch + { + ScyllaWebFactoryFixture s => (s.Aspire.PostgresConnectionString, s.Aspire.ScyllaPort!.Value), + CassandraWebFactoryFixture c => (c.Aspire.PostgresConnectionString, c.Aspire.CassandraPort!.Value), + _ => null, + }; + + if (backend is { } b) + { + factory + .WithConfiguration("OCTOCON_POSTGRES_CONNECTION", b.PostgresConnection) + .WithConfiguration("OCTOCON_SCYLLA_PORT", b.CqlPort.ToString()) + .WithConfiguration("OCTOCON_SINGLE_SCYLLA_INSTANCE", "true") + .WithConfiguration("OCTOCON_DB_RETRY_ATTEMPTS", "10") + .WithConfiguration("OCTOCON_DB_RETRY_INITIAL_DELAY_MS", "500") + .WithConfiguration("OCTOCON_DB_RETRY_MAX_DELAY_MS", "3000"); + } + + return factory; + } +} diff --git a/tests/integration/Interfold.Auth.IntegrationTests/Interfold.Auth.IntegrationTests.csproj b/tests/integration/Interfold.Auth.IntegrationTests/Interfold.Auth.IntegrationTests.csproj new file mode 100644 index 00000000..46088b58 --- /dev/null +++ b/tests/integration/Interfold.Auth.IntegrationTests/Interfold.Auth.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/AssemblyInfo.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/AssemblyInfo.cs new file mode 100644 index 00000000..08d80132 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +// Cap concurrency for the whole assembly. See DinDParallelLimit for the reasoning. +[assembly: ParallelLimiter] + +// Every test class in this assembly carries [Explicit]. The DinD fixtures each cost +// 5–8 GB of transient inner-Docker storage and several minutes of wall-clock (image build, +// pre-pull, publish tarball, systemd bootstrap). A solution-wide local `dotnet test` that +// also spins up the centralised test-bench (see TestBenchCoordinator) and the eleven other +// leaf integration projects previously tipped the disk past 100% and OOM-killed the +// docker daemon; the exclusion keeps the local sanity-check loop fast. +// +// Local: `dotnet test` at the root skips this whole assembly. +// Manual: `dotnet --treenode-filter '/*/*/*/*'` runs every test — the filter +// matches all tests, all are explicit, so TUnit runs them (see +// https://tunit.dev/docs/writing-tests/explicit). +// CI: the `test-int` leg for this project invokes the DLL with the same treenode filter +// so every leg still runs the full matrix. +// +// TUnit does not support an assembly-level [Explicit]; the attribute has to sit on every +// test class. New test classes in this project MUST add [Explicit] at the class level. + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Attributes/RequiresDockerAttribute.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Attributes/RequiresDockerAttribute.cs new file mode 100644 index 00000000..a393f3f2 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Attributes/RequiresDockerAttribute.cs @@ -0,0 +1,42 @@ +using System.Diagnostics; + +namespace Interfold.Bootstrapper.IntegrationTests.Attributes; + +/// +/// TUnit skip attribute applied at the class level on every DinD-driven test. Probes for a +/// reachable Docker daemon exactly once per test session and short-circuits the entire test +/// class with a "skipped" result if Docker isn't available — devs without Docker get a clean +/// report instead of a fixture-init failure storm. +/// +/// +/// Uses with Task<bool> so the actual docker info +/// process spawn happens at most once even though is +/// invoked once per test method. See . +/// +public sealed class RequiresDockerAttribute() : SkipAttribute("Docker daemon not available on the host") +{ + private static readonly Lazy> DockerAvailable = new(async () => + { + try + { + var psi = new ProcessStartInfo("docker", "info") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + using var p = Process.Start(psi)!; + await p.WaitForExitAsync(); + return p.ExitCode == 0; + } + catch + { + return false; + } + }); + + public override async Task ShouldSkip(TestRegisteredContext context) + => !await DockerAvailable.Value; +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/BackupPhaseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/BackupPhaseTests.cs new file mode 100644 index 00000000..59a80535 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/BackupPhaseTests.cs @@ -0,0 +1,149 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the backup subcommand against a real running compose stack +/// inside the shared Ubuntu DinD fixture. Each test runs a full bootstrap first +/// (bringing up postgres + scylla), then exercises the backup pipeline and asserts the +/// on-disk artifacts. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class BackupPhaseTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task BackupCreatesPostgresAndScyllaArtifacts() + { + // Full bootstrap first so we have a live compose stack to back up. The backup phase + // depends on the API container + DB containers running (it execs into them via + // `docker compose exec`). + var (scratch, _) = await dinD.BootstrapAsync(nameof(BackupCreatesPostgresAndScyllaArtifacts), TestConfigPaths.DefaultConfig); + + var backup = await dinD.RunOnScratchAsync(scratch, nameof(BackupCreatesPostgresAndScyllaArtifacts), "backup", + "--component", "all"); + await Assert.That(backup.ExitCode).IsEqualTo(0).Because($"backup failed: {backup.Stderr}"); + + // ls the per-component subdirs — must contain at least one matching archive each. + var pgList = await dinD.ExecAsync(["sh", "-c", $"ls -1 {scratch.OutputDir}/backups/postgres/*.dump 2>/dev/null | head -5"]); + await Assert.That(pgList.ExitCode).IsEqualTo(0L); + await Assert.That(pgList.Stdout.Trim().Length).IsGreaterThan(0) + .Because("backup should produce at least one .dump under backups/postgres/"); + + var scyllaList = await dinD.ExecAsync(["sh", "-c", $"ls -1 {scratch.OutputDir}/backups/scylla/*.tar.gz 2>/dev/null | head -5"]); + await Assert.That(scyllaList.ExitCode).IsEqualTo(0L); + await Assert.That(scyllaList.Stdout.Trim().Length).IsGreaterThan(0) + .Because("backup should produce at least one .tar.gz under backups/scylla/"); + + // Size sanity: an empty pg_dump custom-format file is 6 bytes (the magic header), but a + // populated one is at least a few hundred bytes. Anything below 100 strongly suggests + // pg_dump bailed and we wrote a truncated artifact. + var pgSize = await dinD.ExecAsync(["sh", "-c", $"stat -c %s {scratch.OutputDir}/backups/postgres/*.dump | head -1"]); + await Assert.That(int.Parse(pgSize.Stdout.Trim())).IsGreaterThan(100) + .Because("pg_dump output must be non-trivial in size"); + + // The scylla tar.gz includes at least the directory tree even when there's no user data; + // anything below 100 bytes is a smoking gun for a failed tar. + var scyllaSize = await dinD.ExecAsync(["sh", "-c", $"stat -c %s {scratch.OutputDir}/backups/scylla/*.tar.gz | head -1"]); + await Assert.That(int.Parse(scyllaSize.Stdout.Trim())).IsGreaterThan(100) + .Because("scylla tar.gz output must be non-trivial in size"); + } + + [Test] + public async Task BackupRetentionPrunesOldestPastRetainCount() + { + // Two-phase retention check: take three backups in a row with retain=2 and assert only + // the two newest survive each iteration. The mtime spacing isn't guaranteed across + // back-to-back invocations (they happen within the same second), so we use the + // timestamp encoded in the filename as the secondary ordering. + var (scratch, _) = await dinD.BootstrapAsync(nameof(BackupRetentionPrunesOldestPastRetainCount), TestConfigPaths.DefaultConfig); + + // First two runs: both should survive under retain=2. + for (var i = 0; i < 2; i++) + { + // sleep 1s between backups so the timestamps differ; backup filenames use second- + // resolution and we need each iteration's archive to be distinguishable on disk. + await dinD.ExecAsync(["sleep", "1"]); + var b = await dinD.RunOnScratchAsync(scratch, $"{nameof(BackupRetentionPrunesOldestPastRetainCount)}-{i}", "backup", + "--component", "all", "--retain", "2"); + await Assert.That(b.ExitCode).IsEqualTo(0).Because($"backup #{i} failed: {b.Stderr}"); + } + + var countAfterTwo = await dinD.CountFilesAsync(scratch, "backups/postgres/"); + await Assert.That(countAfterTwo).IsEqualTo(2) + .Because("two backups + retain=2 should leave exactly 2 files"); + + // Third run: pruning kicks in, exactly 2 must remain. + await dinD.ExecAsync(["sleep", "1"]); + var third = await dinD.RunOnScratchAsync(scratch, $"{nameof(BackupRetentionPrunesOldestPastRetainCount)}-third", "backup", + "--component", "all", "--retain", "2"); + await Assert.That(third.ExitCode).IsEqualTo(0).Because($"third backup failed: {third.Stderr}"); + + var pgCount = await dinD.CountFilesAsync(scratch, "backups/postgres/"); + await Assert.That(pgCount).IsEqualTo(2) + .Because("three backups + retain=2 should prune the oldest, leaving exactly 2"); + + var scyllaCount = await dinD.CountFilesAsync(scratch, "backups/scylla/"); + await Assert.That(scyllaCount).IsEqualTo(2) + .Because("scylla retention must mirror postgres retention"); + } + + [Test] + public async Task BackupComponentFlagRestrictsScope() + { + // --component postgres should write a .dump but NOT touch backups/scylla/, and vice + // versa. Pins the contract so a future refactor that ignores the flag is caught. + var (scratch, _) = await dinD.BootstrapAsync(nameof(BackupComponentFlagRestrictsScope), TestConfigPaths.DefaultConfig); + + var pgOnly = await dinD.RunOnScratchAsync(scratch, $"{nameof(BackupComponentFlagRestrictsScope)}-pg", "backup", + "--component", "postgres"); + await Assert.That(pgOnly.ExitCode).IsEqualTo(0).Because(pgOnly.Stderr); + + // Postgres subdir populated; scylla subdir either missing or empty. + var pgList = await dinD.CountFilesAsync(scratch, "backups/postgres/*.dump"); + await Assert.That(pgList).IsEqualTo(1); + + var scyllaList = await dinD.CountFilesAsync(scratch, "backups/scylla/*.tar.gz"); + await Assert.That(scyllaList).IsEqualTo(0) + .Because("--component=postgres must not touch the scylla subdir"); + } + + [Test] + public async Task BackupWithoutComposeFailsClearly() + { + // Backup against a bare scratch directory (no compose, no secrets, nothing but the + // config file) must fail with a clear, actionable error rather than dying inside + // docker compose. This is the most-likely operator misuse: running `backup` before + // `bootstrap` / `publish`. The phase runs several prerequisite checks and any of + // them is a legitimate signal — the important contract is that the error names a + // specific missing artifact and tells the operator to run `bootstrap` first. + var scratch = await dinD.CreateScratchAsync(nameof(BackupWithoutComposeFailsClearly), TestConfigPaths.DefaultConfig); + + // Write the config but skip the bootstrap. The scratch's outputDir has only the + // config file, no compose stack. + var result = await dinD.RunOnScratchAsync(scratch, nameof(BackupWithoutComposeFailsClearly), "backup", + "--component", "postgres"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("backup against a bare scratch (no compose, no secrets) must fail"); + + var combined = result.Stdout + result.Stderr; + // Either message satisfies the "clear, actionable error" contract — the phase + // checks secrets before the compose file so a bare scratch trips the secrets + // check first; a stack with secrets but no compose file trips the compose check. + // Both name a specific missing artifact and point at `bootstrap` for the fix. + await Assert.That(combined).Contains("docker-compose.yaml").Or.Contains("secrets/secrets.json") + .Because("error must name a specific missing artifact"); + await Assert.That(combined).Contains("bootstrap") + .Because("error must guide the operator to `bootstrap` as the fix"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/BootstrapIdempotenceTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/BootstrapIdempotenceTests.cs new file mode 100644 index 00000000..f8a47cc5 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/BootstrapIdempotenceTests.cs @@ -0,0 +1,107 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Confirms a second bootstrap against an already-running stack short-circuits the +/// expensive admin work via the in-cluster state probes +/// (PostgresAlreadyInitializedAsync / ScyllaAlreadyInitializedAsync) and leaves +/// the existing containers untouched. +/// +/// +/// Both tests in this file run a full bootstrap (which host-binds postgres / scylla / api +/// ports) but each one lands on its own port window inside the shared DinD via +/// 's allocator, so they can execute +/// concurrently with each other and with every other compose-up test in the assembly. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class BootstrapIdempotenceTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task SecondBootstrapShortCircuitsDbInit() + { + var scratch = await dinD.CreateScratchAsync(nameof(SecondBootstrapShortCircuitsDbInit), TestConfigPaths.DefaultConfig); + + // First bootstrap: full path - prereqs (skipped) -> config -> secrets -> certs -> publish + // -> db-init -> launch. Brings the postgres + scylla admin work all the way through. + var first = await dinD.RunOnScratchAsync(scratch, $"{nameof(SecondBootstrapShortCircuitsDbInit)}-first", "bootstrap", + "--skip-prereqs"); + await Assert.That(first.ExitCode).IsEqualTo(0) + .Because($"first bootstrap failed: {first.Stderr}"); + + // Second bootstrap against the same scratch: the orchestrator runs the same phase + // sequence, but each idempotent phase should self-skip or short-circuit. + var second = await dinD.RunOnScratchAsync(scratch, $"{nameof(SecondBootstrapShortCircuitsDbInit)}-second", "bootstrap", + "--skip-prereqs", "--print-phase-status"); + await Assert.That(second.ExitCode).IsEqualTo(0) + .Because($"second bootstrap failed: {second.Stderr}"); + + // The machine-readable phase status lines on stderr should report skips for the + // already-completed work. db-init's short-circuit doesn't emit a `phase=db-init + // status=skipped` line (it's a fine-grained in-method check), but the stdout from the + // phase logs the postgres + scylla state-probe success. + await Assert.That(second.Stderr).Contains("phase=secrets status=skipped") + .Because("secrets phase should self-skip on a second run"); + await Assert.That(second.Stderr).Contains("phase=certs status=skipped") + .Because("certs phase should self-skip on a second run"); + await Assert.That(second.Stdout) + .Contains("postgres already initialised") + .Because("postgres state probe should report short-circuit on rerun"); + await Assert.That(second.Stdout) + .Contains("scylla already initialised") + .Because("scylla state probe should report short-circuit on rerun"); + } + + [Test] + public async Task SecondBootstrapLeavesContainersHealthy() + { + var scratch = await dinD.CreateScratchAsync(nameof(SecondBootstrapLeavesContainersHealthy), TestConfigPaths.DefaultConfig); + + await dinD.RunOnScratchAsync(scratch, $"{nameof(SecondBootstrapLeavesContainersHealthy)}-first", "bootstrap", + "--skip-prereqs"); + + // Capture the container IDs after the first pass so we can compare against the second. + var composeFile = $"{scratch.OutputDir}/docker-compose.yaml"; + var firstIds = await ContainerIdsAsync(composeFile); + await Assert.That(firstIds.Count).IsGreaterThan(0) + .Because("first bootstrap must have brought at least one container up"); + + var second = await dinD.RunOnScratchAsync(scratch, $"{nameof(SecondBootstrapLeavesContainersHealthy)}-second", "bootstrap", + "--skip-prereqs"); + await Assert.That(second.ExitCode).IsEqualTo(0).Because(second.Stderr); + + var secondIds = await ContainerIdsAsync(composeFile); + // Same set of IDs - if a phase had re-created a container we'd see a different ID for + // that service. docker compose's stable container naming means an unchanged service has + // an unchanged container ID across reruns. + await Assert.That(secondIds.Count).IsEqualTo(firstIds.Count) + .Because("number of compose services must be stable across reruns"); + foreach (var id in firstIds) + { + await Assert.That(secondIds.Contains(id)).IsTrue() + .Because($"container {id} should still exist after the rerun (no service was recreated)"); + } + } + + /// + /// Returns the set of container IDs for the given compose file. Empty if compose ps fails. + /// + private async Task> ContainerIdsAsync(string composeFile) + { + var ps = await dinD.ExecAsync( + ["docker", "compose", "-f", composeFile, "ps", "-q"]); + if (ps.ExitCode != 0) return new HashSet(); + return ps.Stdout.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .ToHashSet(StringComparer.Ordinal); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitFaultRecoveryTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitFaultRecoveryTests.cs new file mode 100644 index 00000000..f4ee2100 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitFaultRecoveryTests.cs @@ -0,0 +1,95 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Confirms the bootstrapper recovers cleanly from a fault injected between the Postgres and +/// Scylla initialisation sub-steps inside DatabaseInitPhase. This exercises the +/// idempotent state probes (PostgresAlreadyInitializedAsync / +/// ScyllaAlreadyInitializedAsync) that decide whether a rerun should reapply admin +/// bootstrap or short-circuit. +/// +/// +/// The first run uses the hidden --fault-inject=after-db-postgres flag (wired into +/// ) which throws inside the phase +/// right after the Postgres roles are minted. The Orchestrator catches the throw, returns +/// non-zero, and skips Launch — leaving the postgres data volume populated with the +/// interfold_admin role but no scylla role yet. +/// +/// Runs against its own per-test host-port window inside the shared DinD (via +/// 's port allocator), so it can execute +/// alongside any other compose-up test without colliding on postgres/scylla/api ports. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class DbInitFaultRecoveryTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task PartiallyBootstrappedDbCompletesOnRerun() + { + var scratch = await dinD.CreateScratchAsync(nameof(PartiallyBootstrappedDbCompletesOnRerun), TestConfigPaths.DefaultConfig); + + // Run #1: bootstrap halts mid-DatabaseInitPhase, after Postgres admin role creation but + // before Scylla. Exit code is non-zero because the phase throws to skip Launch (we don't + // want compose up against an un-initialised scylla). + var halted = await dinD.RunOnScratchAsync(scratch, $"{nameof(PartiallyBootstrappedDbCompletesOnRerun)}-halt", "bootstrap", + "--skip-prereqs", "--fault-inject=after-db-postgres"); + await Assert.That(halted.ExitCode).IsNotEqualTo(0) + .Because("fault-inject=after-db-postgres should surface a non-zero exit so launch is skipped"); + + // The postgres compose service must be running and the admin role must exist. + var composeFile = $"{scratch.OutputDir}/docker-compose.yaml"; + var pgAdminProbe = await dinD.PsqlAsync( + composeFile, PostgresRoles.Init, "postgres", + "\"SELECT rolsuper FROM pg_roles WHERE rolname='interfold_admin'\"", + host: null); + await Assert.That(pgAdminProbe.ExitCode).IsEqualTo(0L) + .Because($"postgres should be running after a partial bootstrap: {pgAdminProbe.Stderr}"); + await Assert.That(pgAdminProbe.Stdout.Trim()).IsEqualTo("t") + .Because("interfold_admin postgres role must have been created during the halted run"); + + // The scylla side should NOT have an admin role yet — that's the bit the fault skipped. + // (We don't assert scylla container *isn't* running — the postgres init sub-phase brings + // both stateful services up via `docker compose up -d msg-db scylla`.) + // + // Using `LIST ROLES` (the full listing) rather than `LIST ROLES OF ''` because the + // latter errors with ` doesn't exist` on a missing role — the error + // text contains the role name we're checking for, producing a false-positive match. The + // full listing only mentions a role if it actually exists in the cluster. + var scyllaAdminProbe = await dinD.CqlshAsync(composeFile, "cassandra", "cassandra", "\"LIST ROLES\""); + await Assert.That(scyllaAdminProbe.Stdout.Contains("interfold_admin")).IsFalse() + .Because("the scylla admin role must NOT exist yet after a halt-after-postgres run"); + + // Run #2: rerun without the fault flag. The orchestrator hits db-init again, the + // postgres state probe short-circuits the admin bootstrap, scylla init runs fresh, and + // launch brings everything up to health. + var resumed = await dinD.RunOnScratchAsync(scratch, $"{nameof(PartiallyBootstrappedDbCompletesOnRerun)}-resume", "bootstrap", + "--skip-prereqs", "--print-phase-status"); + await Assert.That(resumed.ExitCode).IsEqualTo(0) + .Because($"resumed bootstrap should complete cleanly: {resumed.Stderr}"); + await Assert.That(resumed.Stdout) + .Contains("postgres already initialised") + .Or.Contains("postgres state probe") + .Because("the postgres state probe should surface the short-circuit on rerun"); + + // Final state: scylla admin must now exist, locking the original cassandra default. + // Resolve the password C#-side and pass it through CqlshAsync's `password` parameter — + // the same shape sibling tests in DbInitSecurityInvariantsTests use, and immune to + // the JSON-writer drift the shell `grep | sed` variant would silently break under. + var scyllaAdminPassword = await dinD.ReadSecretsFieldAsync(scratch, "scyllaAdminPassword"); + var scyllaAdminFinal = await dinD.CqlshAsync( + composeFile, "interfold_admin", scyllaAdminPassword, "\"LIST ROLES\""); + await Assert.That(scyllaAdminFinal.Stdout.Contains("interfold_admin")).IsTrue() + .Because($"after resume, interfold_admin must exist in scylla: {scyllaAdminFinal.Stdout}"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitSecurityInvariantsTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitSecurityInvariantsTests.cs new file mode 100644 index 00000000..314e7a24 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DbInitSecurityInvariantsTests.cs @@ -0,0 +1,197 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// In-cluster security invariants for the running stack after a successful bootstrap. +/// Each test pins one specific contract from the database init phase: +/// +/// Cassandra default account is locked. +/// db_init password is scrambled in-cluster. +/// Application interfold user is DML-only on Postgres. +/// Application interfold user is not a superuser on Scylla. +/// interfold_admin is a Postgres superuser. +/// internal.secrets is seeded with the expected keys. +/// +/// +/// +/// Each test runs a full bootstrap against its own scratch directory and a private host-port +/// window inside the shared DinD (see 's port +/// allocator), so they execute concurrently with each other and with the other compose-up +/// tests in the assembly. The previous ubuntu-compose-up NotInParallel serialiser is no +/// longer needed. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class DbInitSecurityInvariantsTests(UbuntuDinDFixture dinD) +{ + + /// + /// Application database name the test bootstrap config asks the seeder to create. MUST stay + /// in lockstep with the postgresDatabase field of interfold.bootstrap.test.json. + /// Deliberately distinct from both the legacy octocon name and the production default + /// interfold — every probe below selects against this name, so a regression that + /// reintroduces a hardcoded literal anywhere in DatabaseInitPhase / + /// PostgresSeedOptions / PostgresSqlTemplates would surface as a + /// connect-or-permission-denied error here. + /// + private const string TestPostgresDb = "test_pg_db"; + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task CassandraDefaultIsLockedAfterBootstrap() + { + var (_, composeFile) = await dinD.BootstrapAsync(nameof(CassandraDefaultIsLockedAfterBootstrap), TestConfigPaths.DefaultConfig); + + // After BootstrapScyllaAsync, the default cassandra/cassandra login is locked - either + // LOGIN=false or the password is scrambled. Either failure mode produces a non-zero + // cqlsh exit; we don't care which, just that the default no longer authenticates. + var auth = await dinD.CqlshAsync(composeFile, "cassandra", "cassandra", "\"DESCRIBE CLUSTER\""); + // cqlsh prints "Authentication error" or "Bad credentials"; the exact string varies by + // scylla version so we just check the cqlsh exit code via grep — a clean auth would + // print the cluster name on stdout and exit 0. + await Assert.That(auth.Stdout.Contains("cluster", StringComparison.OrdinalIgnoreCase)).IsFalse() + .Because($"cassandra/cassandra should be locked after bootstrap, but the cluster description came back: {auth.Stdout}"); + } + + [Test] + public async Task DbInitPostgresPasswordIsScrambledInCluster() + { + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(DbInitPostgresPasswordIsScrambledInCluster), TestConfigPaths.DefaultConfig); + + // The bootstrapper scrambles db_init's in-cluster password as the final step of + // BootstrapPostgresAsync. The compose .env still ships the *initial* db_init password + // (so volume-reset reruns work) - but the live cluster's password is something + // operationally unrecoverable. Authing as db_init with the .env password must fail. + var initPasswordRaw = await dinD.ExecAsync( + ["sh", "-c", + $"grep '^POSTGRES_INIT_PASSWORD=' {scratch.OutputDir}/.env | sed 's/^POSTGRES_INIT_PASSWORD=//'"]); + var initPassword = initPasswordRaw.Stdout.Trim(); + await Assert.That(initPassword).IsNotEmpty() + .Because("POSTGRES_INIT_PASSWORD must be present in the published .env"); + + var auth = await dinD.PsqlAsync( + composeFile, PostgresRoles.Init, "postgres", "'SELECT 1'", + password: initPassword, softFail: true); + // Auth failure prints "password authentication failed for user" on stderr. + await Assert.That(auth.Stdout + auth.Stderr).Contains("authentication failed") + .Or.Contains("password authentication") + .Because($"db_init password should be scrambled in-cluster post-bootstrap; got: {auth.Stdout} / {auth.Stderr}"); + } + + [Test] + public async Task InterfoldPostgresUserIsDmlOnly() + { + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(InterfoldPostgresUserIsDmlOnly), TestConfigPaths.DefaultConfig); + + // Read the app user's password from the persisted secrets file (the only place it lives + // outside the cluster after a successful bootstrap). ReadSecretsFieldAsync parses the + // file in-process via JsonDocument so it stays correct even if SecretsPhase.PersistAsync + // switches to prettified/reordered JSON — the same rationale as the sibling call at L165. + var appPass = await dinD.ReadSecretsFieldAsync(scratch, "postgresPassword"); + await Assert.That(appPass).IsNotEmpty() + .Because("interfold password should be readable from secrets.json"); + + // Try a DDL statement the DML-only role should NOT be able to execute. CREATE ROLE + // requires SUPERUSER/CREATEROLE, neither of which the app user has. + var ddl = await dinD.PsqlAsync( + composeFile, "interfold", TestPostgresDb, "\"CREATE ROLE escalation_attempt\"", + password: appPass, softFail: true); + await Assert.That(ddl.Stdout + ddl.Stderr).Contains("permission denied") + .Or.Contains("must be superuser") + .Because($"app user must not be allowed to CREATE ROLE: {ddl.Stdout} / {ddl.Stderr}"); + } + + [Test] + public async Task InterfoldScyllaUserIsNonSuperuser() + { + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(InterfoldScyllaUserIsNonSuperuser), TestConfigPaths.DefaultConfig); + + // Read scyllaAdminPassword from THIS test's secrets.json. Explicitly using scratch + // (per-test dir) — `/opt/scratch/*/secrets.json | head -1` would pick the + // alphabetically-first scratch dir (which is a sibling test's, since the class shares + // one DinD container and TearDownComposeAsync deliberately leaves scratch dirs in place + // for failure-artifact capture) and produce a "Bad credentials" auth error every time + // this test isn't the first alphabetically. + var adminPass = await dinD.ReadSecretsFieldAsync(scratch, "scyllaAdminPassword"); + await Assert.That(adminPass).IsNotEmpty() + .Because("scyllaAdminPassword must be persisted in secrets.json"); + + // LIST ROLES OF '' from the admin session prints a table that includes a 'super' + // column. The app user must show super=False there. + var adminUser = "interfold_admin"; + var roles = await dinD.CqlshAsync(composeFile, adminUser, adminPass, "\"LIST ROLES OF 'interfold'\""); + + // cqlsh prints a fixed-width table: ` role | super | login | options`. We have to + // look at the SUPER column specifically — a blanket `Contains("True")` matches the + // LOGIN column too, which is legitimately True for the app user. + await Assert.That(roles.Stdout).Contains("interfold") + .Because($"LIST ROLES OF 'interfold' should return at least the role row: {roles.Stdout}"); + + var interfoldRow = roles.Stdout + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .FirstOrDefault(line => line.StartsWith("interfold ", StringComparison.Ordinal) + || line.StartsWith("interfold|", StringComparison.Ordinal)); + await Assert.That(interfoldRow).IsNotNull() + .Because($"failed to locate the 'interfold' role row in cqlsh output: {roles.Stdout}"); + + // Columns are pipe-delimited with arbitrary whitespace padding. The second column is + // `super` (the first column being the role name itself). + var columns = interfoldRow! + .Split('|', StringSplitOptions.TrimEntries) + .Select(c => c.Trim()) + .ToArray(); + await Assert.That(columns.Length).IsGreaterThanOrEqualTo(2) + .Because($"expected at least role|super columns; got '{interfoldRow}'"); + await Assert.That(columns[1]).IsEqualTo("False") + .Because($"interfold scylla user must NOT be a superuser; super column = '{columns[1]}' in row '{interfoldRow}'"); + } + + [Test] + public async Task InterfoldAdminPostgresUserIsSuperuser() + { + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(InterfoldAdminPostgresUserIsSuperuser), TestConfigPaths.DefaultConfig); + + var adminPass = await dinD.ReadSecretsFieldAsync(scratch, "postgresAdminPassword"); + await Assert.That(adminPass).IsNotEmpty() + .Because("postgresAdminPassword must be persisted in secrets.json"); + + var probe = await dinD.PsqlAsync( + composeFile, "interfold_admin", TestPostgresDb, + "\"SELECT rolsuper FROM pg_roles WHERE rolname='interfold_admin'\"", + password: adminPass); + await Assert.That(probe.ExitCode).IsEqualTo(0L).Because(probe.Stderr); + await Assert.That(probe.Stdout.Trim()).IsEqualTo("t") + .Because("interfold_admin must have rolsuper=true"); + } + + [Test] + public async Task InternalSecretsTableIsSeeded() + { + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(InternalSecretsTableIsSeeded), TestConfigPaths.DefaultConfig); + + var appPass = await dinD.ReadSecretsFieldAsync(scratch, "postgresPassword"); + + // The app user has SELECT on internal.secrets - we use it (not admin) so we also + // implicitly confirm the grant from BootstrapPostgresAsync still applies. + var count = await dinD.PsqlAsync( + composeFile, "interfold", TestPostgresDb, "'SELECT COUNT(*) FROM internal.secrets'", + password: appPass); + await Assert.That(count.ExitCode).IsEqualTo(0L).Because(count.Stderr); + // The seed list in BootstrapPostgresAsync inserts ~12 keys (only OAuth secrets that + // are blank are skipped). Anything > 0 confirms seeding ran end-to-end; we use a soft + // lower bound so future seed-list edits don't break this test. + var rows = int.TryParse(count.Stdout.Trim(), out var n) ? n : 0; + await Assert.That(rows).IsGreaterThan(0) + .Because($"internal.secrets should contain seeded rows after bootstrap: got {count.Stdout}"); + } +} + + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/DinDHookHelpers.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DinDHookHelpers.cs new file mode 100644 index 00000000..c67d2ffc --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/DinDHookHelpers.cs @@ -0,0 +1,37 @@ +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Shared body for the one-line [After(Test)] DumpOnFailure hook that every +/// DinD integration-test class carries. +/// +/// +/// The 19 [After(Test)] public Task DumpOnFailure(...) hooks across this project +/// look ripe for a DinDIntegrationTestBase<TFixture> lift-to-base +/// refactor, but Round-1 C1 deliberately picked the per-class hook over inheritance +/// because the test classes use TUnit's primary-ctor fixture pattern +/// (UbuntuDinDFixture dinD) — the compiler-generated backing field can't be +/// re-exposed to a base class without either giving up primary-ctor syntax or +/// duplicating the fixture parameter on both ctor and base. Round-2 D10 re-evaluated +/// the shape and confirmed no drift (all 19 sites are still the byte-identical +/// single-line delegate to this helper, with 3 sites overriding teardown: false); +/// the per-class hook stays. +/// +public static class DinDHookHelpers +{ + public static async Task DumpOnFailureAsync(TFixture dinD, TestContext ctx, bool teardown = true) + where TFixture : DinDFixtureBase + { + if (ctx.Execution.Result?.State == TestState.Failed) + { + await dinD.CaptureFailureArtifactsAsync(ctx.Metadata.TestName); + } + + if (teardown) + { + await dinD.TearDownComposeAsync(ctx.Metadata.TestName); + } + } +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/FedoraBootstrapTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/FedoraBootstrapTests.cs new file mode 100644 index 00000000..2416a98d --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/FedoraBootstrapTests.cs @@ -0,0 +1,57 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// RHEL/Fedora-side smoke coverage. Scenarios overlap with the Ubuntu suite in spirit but +/// specifically exercise the dnf install path of PrerequisitesPhase and the +/// update-ca-trust extract trust-store install in CertificatePhase. +/// Each test allocates its own scratch dir so the suite can execute concurrently. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class FedoraBootstrapTests(FedoraDinDFixture dinD) +{ + + // Trust-store-install tests use a variant that enables /etc/pki/ca-trust/source/anchors/ + // writes. See Ubuntu suite for the parallel-safety rationale. + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + // Each compose-up test draws a unique host-port window from the DinD-wide port allocator + // (see DinDFixtureBase.CreateScratchAsync), so the previous fedora-compose-up serialiser + // is no longer required to avoid host-port collisions. + [Test] + public async Task StackComesUpHealthyOnRhelFamily() + { + var (scratch, _) = await dinD.BootstrapAsync(nameof(StackComesUpHealthyOnRhelFamily), TestConfigPaths.DefaultConfig); + + var ps = await dinD.ExecAsync( + ["docker", "compose", "-f", $"{scratch.OutputDir}/docker-compose.yaml", "ps", "--format", "json"]); + await Assert.That(ps.ExitCode).IsEqualTo(0L); + await Assert.That(ps.Stdout).Contains("\"State\":\"running\"").Or.Contains("\"Health\":\"healthy\"") + .Because("expected at least one healthy/running service in Fedora compose ps output"); + } + + // Trust-store install writes /etc/pki/ca-trust/source/anchors/interfold-root-ca.crt, a + // process-global path shared across tests in this DinD - serialise on a per-fixture key. + [Test] + [NotInParallel("fedora-trust-install")] + public async Task RootCaInstalledInRhelTrustStore() + { + var (scratch, _) = await dinD.PublishAsync(nameof(RootCaInstalledInRhelTrustStore), TestConfigPaths.TrustInstallConfig); + + // update-ca-trust extract writes the extracted PEM bundles to /etc/pki/ca-trust/extracted/. + var lookup = await dinD.ExecAsync( + ["sh", "-c", + "grep -l 'Interfold Test Root CA' /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem || true"]); + await Assert.That(lookup.Stdout.Trim()).IsNotEmpty() + .Because("root CA subject should appear in /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem after update-ca-trust extract"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BareDinDFixtureBase.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BareDinDFixtureBase.cs new file mode 100644 index 00000000..16c770c4 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BareDinDFixtureBase.cs @@ -0,0 +1,56 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Base class for DinD fixtures rooted on stripped distro images that ship without docker +/// or openssl. Collapses the byte-identical InitializeAsync that Ubuntu and Fedora +/// bare-prereqs fixtures used to hold verbatim, and exposes two hooks +/// (, ) so the diagnostic message +/// stays specific per subclass. +/// +/// +/// +/// Concrete subclasses stay sealed — TUnit's [ClassDataSource] needs a concrete +/// non-abstract type per test class, so an abstract base is fine but the leaf must not be. +/// +/// +/// The one-shot precondition guard sits here (rather than inside individual +/// PrereqsPhaseTests cases) because three of the four tests that share this +/// fixture run bootstrap --fault-inject=after-prereqs, which installs docker as a +/// side effect. Any per-test "docker isn't installed yet" assertion would only survive +/// for whichever test happened to run first. Lifting it to fixture-build time turns it +/// into a one-shot assertion against the fixture IMAGE, which is what the original test +/// comment described it as. +/// +/// +public abstract class BareDinDFixtureBase : DinDFixtureBase +{ + /// + /// Human-readable distro name for the "expects a bare X image" error message. + /// Example values: "Ubuntu", "Fedora". + /// + protected abstract string DistroLabel { get; } + + /// + /// The package manager name whose install path this fixture exists to exercise + /// (referenced in the guard's error message). Example values: "apt", "dnf". + /// + protected abstract string PackageManager { get; } + + // No dockerd to start, no API image to load — skip the parent's PreloadImages flow. + // Bare fixtures ship without dockerd, so `docker load` would fail with connection + // refused anyway. + protected override bool PreloadImages => false; + + public override async Task InitializeAsync() + { + await base.InitializeAsync().ConfigureAwait(false); + var prePath = await ExecAsync(["sh", "-c", "command -v docker || true"]).ConfigureAwait(false); + if (!string.IsNullOrWhiteSpace(prePath.Stdout)) + { + throw new InvalidOperationException( + $"{GetType().Name} expects a bare {DistroLabel} image without docker " + + $"pre-installed, but `command -v docker` returned '{prePath.Stdout.Trim()}'. " + + $"Inspect {DockerfileName} — the {PackageManager} install path can no longer be exercised."); + } + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BootstrapperBuild.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BootstrapperBuild.cs new file mode 100644 index 00000000..0a0dcb95 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/BootstrapperBuild.cs @@ -0,0 +1,226 @@ +using System.Diagnostics; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Session-wide caches of the expensive prep work used by every DinD fixture: the bootstrapper +/// publish output and the saved API image tarball. Both are produced once per test run. +/// +internal static class BootstrapperBuild +{ + /// + /// Publishes the bootstrapper as a self-contained linux-x64 single-file binary and returns the + /// containing directory. Support files ship embedded in the assembly; publish stages bind-mount + /// sources under the deploy output directory. + /// + public static readonly Lazy> PublishedDirectory = new(PublishBootstrapperAsync); + + /// + /// Builds the API container image via Aspire's container publish, then docker-saves it to a + /// tarball the DinD container can docker load into its inner daemon. + /// + public static readonly Lazy> ApiImageTarPath = new(BuildAndSaveApiImageAsync); + + private const string ApiImageRef = "interfold-api:test"; + + // Root of the per-session publish + api-tar staging area under $TMPDIR. Every DinD + // test-host process drops ~150 MB here (bootstrapper publish + saved API image tar) + // and never cleans it up, so aborted / repeated sessions pile up on the runner disk. + // PruneStaleSessions is called from both Lazy factories so the cleanup runs exactly + // once per test-host regardless of which artifact is requested first. + private static readonly string StagingRoot = Path.Combine( + Path.GetTempPath(), "interfold-bootstrap-test-publish"); + private static int _prunedFlag; + + private static async Task PublishBootstrapperAsync() + { + PruneStaleSessions(); + var outDir = Path.Combine(StagingRoot, + $"run-{Environment.ProcessId}-{DateTime.UtcNow:yyyyMMddHHmmss}"); + Directory.CreateDirectory(outDir); + + await RunDotnetAsync( + "publish", + "tools/Interfold.Bootstrapper/Interfold.Bootstrapper.csproj", + "/p:PublishProfile=linux-x64", + "-o", outDir).ConfigureAwait(false); + + return outDir; + } + + private static async Task BuildAndSaveApiImageAsync() + { + // Skip the `dotnet publish /t:PublishContainer` step if the local Docker daemon already + // has a tagged `interfold-api:test` image. The PublishContainer task always re-fetches + // the base layer manifest from mcr.microsoft.com, and that goes through whatever + // credential helper Docker Desktop has configured. When the helper fails (no creds in + // keychain, or MCR rate-limit + new auth-required tier) the whole test session aborts + // before any DinD work runs. As long as we still have a fresh-enough cached build the + // rebuild adds no signal — we just need *some* tarball to load into DinD. + var alreadyExists = await DockerImageExistsAsync(ApiImageRef).ConfigureAwait(false); + if (!alreadyExists) + { + await RunDotnetAsync( + "publish", + "hosts/Interfold.Api.Host/Interfold.Api.Host.csproj", + "-c", "Release", + "/t:PublishContainer", + "/p:ContainerImageName=interfold-api", + "/p:ContainerImageTag=test", + "--os", "linux", + "--arch", "x64").ConfigureAwait(false); + } + + PruneStaleSessions(); + var tarPath = Path.Combine(StagingRoot, $"api-{Environment.ProcessId}.tar"); + Directory.CreateDirectory(Path.GetDirectoryName(tarPath)!); + + await RunAsync("docker", new[] { "save", ApiImageRef, "-o", tarPath }, workingDir: RepoRoot.Path).ConfigureAwait(false); + return tarPath; + } + + // Removes `run-{pid}-{ts}/` dirs and `api-{pid}.tar` files whose PID is no longer + // alive. Runs once per test-host process (`Interlocked.Exchange` gate) so the two + // Lazy factories don't double-scan. Also schedules ProcessExit cleanup of the + // CURRENT session's staging so a clean exit no longer leaks either — aborted + // sessions still leak but the next successful run reclaims them. + private static void PruneStaleSessions() + { + if (Interlocked.Exchange(ref _prunedFlag, 1) != 0) return; + try + { + if (Directory.Exists(StagingRoot)) + { + foreach (var dir in Directory.EnumerateDirectories(StagingRoot, "run-*")) + { + if (IsForeignAlivePid(Path.GetFileName(dir), "run-")) continue; + TryDelete(() => Directory.Delete(dir, recursive: true)); + } + foreach (var tar in Directory.EnumerateFiles(StagingRoot, "api-*.tar")) + { + if (IsForeignAlivePid(Path.GetFileName(tar), "api-", ".tar")) continue; + TryDelete(() => File.Delete(tar)); + } + } + } + catch + { + // Best-effort — a scan failure must not sink the whole test run. + } + + AppDomain.CurrentDomain.ProcessExit += (_, _) => + { + var thisPid = Environment.ProcessId.ToString(System.Globalization.CultureInfo.InvariantCulture); + try + { + foreach (var dir in Directory.EnumerateDirectories(StagingRoot, $"run-{thisPid}-*")) + TryDelete(() => Directory.Delete(dir, recursive: true)); + var tar = Path.Combine(StagingRoot, $"api-{thisPid}.tar"); + if (File.Exists(tar)) TryDelete(() => File.Delete(tar)); + } + catch + { + // Best-effort — shutdown cleanup is not allowed to throw. + } + }; + } + + private static bool IsForeignAlivePid(string name, string prefix, string suffix = "") + { + if (!name.StartsWith(prefix, StringComparison.Ordinal)) return true; + var rest = name[prefix.Length..]; + if (suffix.Length > 0 && rest.EndsWith(suffix, StringComparison.Ordinal)) + rest = rest[..^suffix.Length]; + var dash = rest.IndexOf('-'); + var pidText = dash >= 0 ? rest[..dash] : rest; + if (!int.TryParse(pidText, System.Globalization.NumberStyles.Integer, + System.Globalization.CultureInfo.InvariantCulture, out var pid)) + { + return true; + } + if (pid == Environment.ProcessId) return true; + try + { + using var _ = Process.GetProcessById(pid); + return true; + } + catch (ArgumentException) + { + return false; + } + catch + { + return true; + } + } + + private static void TryDelete(Action action) + { + try { action(); } + catch { /* leftover on next run is fine; better than throwing on cleanup */ } + } + + private static async Task DockerImageExistsAsync(string imageRef) + { + // `docker image inspect ` exits 0 when the image is locally present, 1 otherwise. + // We swallow exceptions because the goal is "do we have a usable cached image" - any + // failure to interrogate the daemon just means we should fall back to the publish path. + try + { + var psi = new ProcessStartInfo + { + FileName = "docker", + WorkingDirectory = RepoRoot.Path, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + psi.ArgumentList.Add("image"); + psi.ArgumentList.Add("inspect"); + psi.ArgumentList.Add(imageRef); + + using var proc = Process.Start(psi); + if (proc is null) return false; + await proc.WaitForExitAsync().ConfigureAwait(false); + return proc.ExitCode == 0; + } + catch + { + return false; + } + } + + private static Task RunDotnetAsync(params string[] args) => + RunAsync("dotnet", args, workingDir: RepoRoot.Path); + + private static async Task RunAsync(string fileName, IEnumerable args, string workingDir) + { + var psi = new ProcessStartInfo + { + FileName = fileName, + WorkingDirectory = workingDir, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var a in args) psi.ArgumentList.Add(a); + + using var proc = Process.Start(psi) + ?? throw new InvalidOperationException($"Failed to start {fileName}."); + + var stdOutTask = proc.StandardOutput.ReadToEndAsync(); + var stdErrTask = proc.StandardError.ReadToEndAsync(); + await proc.WaitForExitAsync().ConfigureAwait(false); + + if (proc.ExitCode != 0) + { + throw new InvalidOperationException( + $"{fileName} {string.Join(' ', args)} exited {proc.ExitCode}.\n" + + $"stdout:\n{await stdOutTask.ConfigureAwait(false)}\n" + + $"stderr:\n{await stdErrTask.ConfigureAwait(false)}"); + } + } +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDFixtureBase.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDFixtureBase.cs new file mode 100644 index 00000000..2e2be512 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDFixtureBase.cs @@ -0,0 +1,448 @@ +using System.Collections.Concurrent; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using DotNet.Testcontainers.Builders; +using DotNet.Testcontainers.Configurations; +using DotNet.Testcontainers.Containers; +using TUnit.Core.Interfaces; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// DinD fixture base for bootstrapper integration tests. Subclasses pick a +/// ; base handles session-cached publish, API-image build, +/// per-fixture DinD build, privileged spawn, and image preload. +public abstract class DinDFixtureBase : IAsyncInitializer, IAsyncDisposable +{ + public const string BootstrapperMountPath = "/opt/bootstrapper"; + public const string SupportFilesMountPath = "/opt/support"; + public const string ScratchRoot = "/opt/scratch"; + + // 6 ports per test rounded to 10 for readable gaps; ~4000 blocks before ephemeral range. + private const int PortAllocationStride = 10; + + // Below Linux ephemeral range (32768-60999). + private const int PortAllocationStart = 25000; + + // Pre-decrement so the first Interlocked.Add returns exactly PortAllocationStart. + private static int _portCursor = PortAllocationStart - PortAllocationStride; + + public ConcurrentDictionary BootstrapperLogs { get; } = new(); + public ConcurrentDictionary Scratches { get; } = new(); + + private IContainer? _dinD; + private string? _publishedBootstrapperDir; + + protected abstract string DockerfileName { get; } + + /// false skips API image preload + external pulls (Alpine negative-path). + protected virtual bool PreloadImages => true; + + /// Extra images to preload beyond the baseline; cassandra fixture uses this + /// for cassandra:5. + protected virtual IReadOnlyList AdditionalPreloadImages => []; + + /// When true, inner dockerd enables the containerd image store (see + /// DIND_CONTAINERD_SNAPSHOTTER in dockerd-entrypoint.sh). Needed to + /// reproduce compose#14014 for update-images digest snapshotting. + protected virtual bool UseContainerdSnapshotter => false; + + public virtual async Task InitializeAsync() + { + _publishedBootstrapperDir = await BootstrapperBuild.PublishedDirectory.Value.ConfigureAwait(false); + var apiImageTarPath = PreloadImages + ? await BootstrapperBuild.ApiImageTarPath.Value.ConfigureAwait(false) + : null; + + // Build the DinD image once per concrete fixture type. Keyed by the Dockerfile name so the + // three subclasses each get their own cached image entry. + var dindImage = await DinDImageCache.GetOrBuildAsync(DockerfileName, FixturesDir).ConfigureAwait(false); + +#pragma warning disable CS0618 // Parameterless ctor planned for removal in TC 5.x; the new ctor signature isn't yet stable. + var builder = new ContainerBuilder() + .WithImage(dindImage) + .WithPrivileged(true) + // Mount the bootstrapper publish output and a tmpfs config dir. + .WithBindMount(_publishedBootstrapperDir, BootstrapperMountPath, AccessMode.ReadWrite) + // Mount repo support files needed at compose-up time. + .WithBindMount(SupportFilesHostPath(), SupportFilesMountPath, AccessMode.ReadOnly); + + if (UseContainerdSnapshotter) + { + builder = builder.WithEnvironment("DIND_CONTAINERD_SNAPSHOTTER", "1"); + } + + if (apiImageTarPath is not null) + { + builder = builder.WithBindMount(apiImageTarPath, "/opt/api-image.tar", AccessMode.ReadOnly); + } + + _dinD = builder.Build(); +#pragma warning restore CS0618 + await _dinD.StartAsync().ConfigureAwait(false); + + if (PreloadImages) + { + await WaitForInnerDockerAsync().ConfigureAwait(false); + await ExecAsync(["docker", "load", "-i", "/opt/api-image.tar"]).ConfigureAwait(false); + await ExecAsync(["docker", "pull", "timescale/timescaledb:latest-pg18"]).ConfigureAwait(false); + await ExecAsync(["docker", "pull", "scylladb/scylla:2026.1"]).ConfigureAwait(false); + foreach (var image in AdditionalPreloadImages) + { + await ExecAsync(["docker", "pull", image]).ConfigureAwait(false); + } + } + + await ExecAsync(["mkdir", "-p", ScratchRoot]).ConfigureAwait(false); + } + + /// Per-test scratch dir + uniquely-named 6-port window. The scratch doubles as + /// the bootstrapper output dir so each compose up gets its own project name + /// (parent-basename), and port rewrites let concurrent tests share the DinD network + /// namespace without collisions. + public async Task CreateScratchAsync(string testName, string configHostPath, CancellationToken ct = default) + { + if (_dinD is null) throw new InvalidOperationException("Fixture not initialized."); + + // Compose project names must be lowercase ASCII. + var sanitized = string.Concat(testName.ToLowerInvariant() + .Where(c => (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_')); + // Random suffix so name-reuse and [DataDrivenTest] iterations don't collide. + var suffix = Guid.NewGuid().ToString("N").Substring(0, 8); + var dir = $"{ScratchRoot}/{sanitized}-{suffix}"; + var configPath = $"{dir}/interfold.bootstrap.json"; + + await ExecAsync(["mkdir", "-p", dir], ct).ConfigureAwait(false); + var templateBytes = await File.ReadAllBytesAsync(configHostPath, ct).ConfigureAwait(false); + var ports = AllocatePorts(); + var rewritten = RewriteConfigWithPorts(templateBytes, ports); + const UnixFileModes Mode0600 = UnixFileModes.UserRead | UnixFileModes.UserWrite; + await _dinD.CopyAsync(rewritten, configPath, 0, 0, Mode0600, ct).ConfigureAwait(false); + + var scratch = new DinDScratch(dir, dir, configPath, ports); + // Stash so [After(Test)] hooks can recover by test name without instance-field state. + Scratches[testName] = scratch; + return scratch; + } + + /// Lock-free monotonic port allocation shared across concurrent fixtures. + public static DinDPortAllocation AllocatePorts() + { + var basePort = Interlocked.Add(ref _portCursor, PortAllocationStride); + return new DinDPortAllocation( + ApiHttp: basePort + 0, + ApiHttps: basePort + 1, + WebHttp: basePort + 2, + WebHttps: basePort + 3, + Postgres: basePort + 4, + Scylla: basePort + 5); + } + + /// Overwrites the top-level ports object; the rest of the config is + /// preserved verbatim via . + private static byte[] RewriteConfigWithPorts(byte[] configBytes, DinDPortAllocation ports) + { + var root = JsonNode.Parse(configBytes) + ?? throw new InvalidOperationException("test bootstrap config parsed to null"); + root["ports"] = new JsonObject + { + ["apiHttp"] = ports.ApiHttp, + ["apiHttps"] = ports.ApiHttps, + ["webHttp"] = ports.WebHttp, + ["webHttps"] = ports.WebHttps, + ["postgres"] = ports.Postgres, + ["scylla"] = ports.Scylla, + }; + var json = root.ToJsonString(new JsonSerializerOptions { WriteIndented = true }); + return Encoding.UTF8.GetBytes(json); + } + + public async ValueTask DisposeAsync() + { + if (_dinD is not null) + { + await _dinD.DisposeAsync().ConfigureAwait(false); + } + } + + public async Task ExecAsync(IList command, CancellationToken ct = default) + { + if (_dinD is null) throw new InvalidOperationException("Fixture not initialized."); + var result = await _dinD.ExecAsync(command, ct).ConfigureAwait(false); + return new ExecResult(result.ExitCode ?? -1, result.Stdout ?? string.Empty, result.Stderr ?? string.Empty); + } + + /// Copy a file out of the DinD container into a byte buffer. + public async Task CopyOutAsync(string containerPath, CancellationToken ct = default) + { + if (_dinD is null) throw new InvalidOperationException("Fixture not initialized."); + return await _dinD.ReadFileAsync(containerPath, ct).ConfigureAwait(false); + } + + /// Single fix-forward point for reading secrets.json. + public async Task ReadSecretsJsonAsync(DinDScratch scratch, CancellationToken ct = default) + { + var bytes = await CopyOutAsync(scratch.SecretsJsonPath, ct).ConfigureAwait(false); + return Encoding.UTF8.GetString(bytes); + } + + public async Task ReadSecretsFieldAsync(DinDScratch scratch, string field, CancellationToken ct = default) + { + var json = await ReadSecretsJsonAsync(scratch, ct).ConfigureAwait(false); + using var doc = JsonDocument.Parse(json); + if (doc.RootElement.TryGetProperty(field, out var prop)) + { + return prop.ValueKind == JsonValueKind.String ? (prop.GetString() ?? string.Empty) : prop.GetRawText().Trim('"'); + } + return string.Empty; + } + + /// Counts files matching a glob under the scratch's output dir. Callers pass + /// a trailing relative path (e.g. "backups/postgres/*.dump"); the scratch is + /// applied here to prevent the literal-{scratch.OutputDir} footgun. + public async Task CountFilesAsync(DinDScratch scratch, string relativePathOrGlob, CancellationToken ct = default) + { + var fullPath = $"{scratch.OutputDir}/{relativePathOrGlob.TrimStart('/')}"; + var exec = await ExecAsync(["sh", "-c", $"ls -1 {fullPath} 2>/dev/null | wc -l"], ct).ConfigureAwait(false); + return int.TryParse(exec.Stdout.Trim(), out var count) ? count : 0; + } + + public async Task CopyOutAsTextAsync(string containerPath, CancellationToken ct = default) + { + var bytes = await CopyOutAsync(containerPath, ct).ConfigureAwait(false); + return Encoding.UTF8.GetString(bytes); + } + + /// Copy a host file into the DinD container. + public async Task CopyInAsync(string hostPath, string containerPath, CancellationToken ct = default) + { + if (_dinD is null) throw new InvalidOperationException("Fixture not initialized."); + var bytes = await File.ReadAllBytesAsync(hostPath, ct).ConfigureAwait(false); + const UnixFileModes Mode0600 = UnixFileModes.UserRead | UnixFileModes.UserWrite; + await _dinD.CopyAsync(bytes, containerPath, 0, 0, Mode0600, ct).ConfigureAwait(false); + } + + /// Shared docker compose exec msg-db psql shape. + /// carries its outer shell quoting; is forwarded verbatim + /// (literals or shell expressions); appends 2>&1 || true + /// for probes that expect a non-zero exit. + public Task PsqlAsync( + string composeFile, + string user, + string database, + string quotedSql, + string? password = null, + string psqlFlags = "-tAc", + string? host = "127.0.0.1", + bool softFail = false, + CancellationToken ct = default) + { + // "PGPASSWORD" stays a literal — the csproj holds the bootstrapper reference with + // ExcludeAssets="all" (compile-link not allowed), and the env-var name is fixed by + // the Postgres client protocol, so no drift risk. + var pwPrefix = password is null ? string.Empty : $"-e PGPASSWORD={password} "; + var hostArg = host is null ? string.Empty : $"-h {host} "; + var tail = softFail ? " 2>&1 || true" : string.Empty; + var cmd = $"docker compose -f {composeFile} exec -T {pwPrefix}msg-db " + + $"psql -U {user} -d {database} {hostArg}{psqlFlags} {quotedSql}{tail}"; + return ExecAsync(["sh", "-c", cmd], ct); + } + + /// Shared docker compose exec scylla cqlsh shape. + /// defaults to true because auth probes typically expect a non-zero exit and grep the output. + public Task CqlshAsync( + string composeFile, + string user, + string password, + string quotedCqlExpression, + bool softFail = true, + CancellationToken ct = default) + { + var tail = softFail ? " 2>&1 || true" : string.Empty; + var cmd = $"docker compose -f {composeFile} exec -T scylla " + + $"cqlsh -u {user} -p {password} -e {quotedCqlExpression}{tail}"; + return ExecAsync(["sh", "-c", cmd], ct); + } + + /// Captures stdout/stderr into . + public async Task RunBootstrapperAsync(string testName, IList args, CancellationToken ct = default) + { + var cmd = new List { $"{BootstrapperMountPath}/interfold-bootstrap" }; + cmd.AddRange(args); + var result = await ExecAsync(cmd, ct).ConfigureAwait(false); + + var sb = BootstrapperLogs.GetOrAdd(testName, _ => new StringBuilder()); + sb.AppendLine($"$ {string.Join(' ', cmd)}"); + sb.AppendLine($"# exit={result.ExitCode}"); + sb.AppendLine("--- stdout ---"); + sb.AppendLine(result.Stdout); + sb.AppendLine("--- stderr ---"); + sb.AppendLine(result.Stderr); + return result; + } + + /// Runs a subcommand against an existing scratch with the three shared flags + /// pre-populated. Does not assert on exit code — callers decide (health-timeout expects + /// non-zero, re-runs expect zero, etc.). + public Task RunOnScratchAsync( + DinDScratch scratch, + string testName, + string command, + params string[] extraArgs) + { + var args = new List(4 + extraArgs.Length) + { + command, + "--config", scratch.ConfigPath, + "--output-dir", scratch.OutputDir, + "--non-interactive", + }; + args.AddRange(extraArgs); + return RunBootstrapperAsync(testName, args); + } + + /// Full bootstrap against a fresh scratch; returns the in-container compose path. + public async Task<(DinDScratch Scratch, string ComposeFile)> BootstrapAsync(string testName, string configPath, params string[] extraArgs) + { + var scratch = await CreateScratchAsync(testName, configPath); + var args = new List { "bootstrap", "--config", scratch.ConfigPath, "--output-dir", scratch.OutputDir, "--non-interactive", "--skip-prereqs" }; + args.AddRange(extraArgs); + var result = await RunBootstrapperAsync(testName, args); + await Assert.That(result.ExitCode).IsEqualTo(0) + .Because($"bootstrap must succeed before invariants can be checked: {result.Stderr}"); + return (scratch, $"{scratch.OutputDir}/docker-compose.yaml"); + } + + /// Publish against a fresh scratch; returns the in-container compose path. + public async Task<(DinDScratch Scratch, string ComposeFile)> PublishAsync(string testName, string configPath, params string[] extraArgs) + { + var scratch = await CreateScratchAsync(testName, configPath); + var args = new List { "publish", "--config", scratch.ConfigPath, "--output-dir", scratch.OutputDir, "--non-interactive" }; + args.AddRange(extraArgs); + var result = await RunBootstrapperAsync(testName, args); + await Assert.That(result.ExitCode).IsEqualTo(0) + .Because($"publish must succeed: {result.Stderr}"); + return (scratch, $"{scratch.OutputDir}/docker-compose.yaml"); + } + + /// Dumps redacted scratch + compose log tail + bootstrapper stdout/stderr to + /// {bin}/test-artifacts/{testName}/. No-op if no scratch was created. + public async Task CaptureFailureArtifactsAsync(string testName, CancellationToken ct = default) + { + if (!Scratches.TryGetValue(testName, out var scratch)) + { + return; + } + await CaptureFailureArtifactsAsync(scratch, testName, ct).ConfigureAwait(false); + } + + /// Releases the test's reserved host-port window in the shared DinD so reruns + /// don't trip over stale binds. Idempotent. + public async Task TearDownComposeAsync(string testName, CancellationToken ct = default) + { + if (_dinD is null) return; + if (!Scratches.TryGetValue(testName, out var scratch)) return; + + // -v: clean data volumes; --remove-orphans: catch renamed services. Errors swallowed + // — real issues resurface on the next compose up. + try + { + await ExecAsync( + ["sh", "-c", $"docker compose -f {scratch.OutputDir}/docker-compose.yaml down -v --remove-orphans --timeout 10 2>&1 || true"], + ct).ConfigureAwait(false); + } + catch + { + // best-effort cleanup; the next compose up will report any real issue + } + } + + private async Task CaptureFailureArtifactsAsync(DinDScratch scratch, string testName, CancellationToken ct) + { + var sanitized = string.Concat(testName.Where(c => !Path.GetInvalidFileNameChars().Contains(c))); + var dest = Path.Combine(AppContext.BaseDirectory, "test-artifacts", sanitized); + Directory.CreateDirectory(dest); + + // Snapshot the deploy directory; redact secrets before writing locally. + var listing = await ExecAsync(["sh", "-c", $"find {scratch.OutputDir} -type f 2>/dev/null || true"], ct).ConfigureAwait(false); + foreach (var path in listing.Stdout.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + try + { + var content = await CopyOutAsync(path, ct).ConfigureAwait(false); + var rel = path.StartsWith(scratch.OutputDir + "/") ? path[(scratch.OutputDir.Length + 1)..] : path.TrimStart('/'); + var target = Path.Combine(dest, rel); + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + if (rel.EndsWith("secrets.json", StringComparison.Ordinal)) + { + await File.WriteAllTextAsync(target, RedactSecrets(content), ct).ConfigureAwait(false); + } + else + { + await File.WriteAllBytesAsync(target, content, ct).ConfigureAwait(false); + } + } + catch + { + // best-effort capture; one unreadable file shouldn't sink the rest + } + } + + var logs = await ExecAsync( + ["sh", "-c", $"docker compose -f {scratch.OutputDir}/docker-compose.yaml logs --tail 200 || true"], ct).ConfigureAwait(false); + await File.WriteAllTextAsync(Path.Combine(dest, "compose-logs.txt"), logs.Stdout + "\n--- stderr ---\n" + logs.Stderr, ct).ConfigureAwait(false); + + if (BootstrapperLogs.TryGetValue(testName, out var sb)) + { + await File.WriteAllTextAsync(Path.Combine(dest, "bootstrapper.log"), sb.ToString(), ct).ConfigureAwait(false); + } + } + + private static string RedactSecrets(byte[] raw) + { + // Textual scrub; using System.Text.Json would drag in the bootstrapper source-gen context. + var text = Encoding.UTF8.GetString(raw); + return System.Text.RegularExpressions.Regex.Replace( + text, + @"""(postgresPassword|scyllaPassword|scyllaAdminPassword|encryptionPrivateKeyB64|encryptionPepper|leafPfxPassword)""\s*:\s*""[^""]*""", + "\"$1\": \"***\""); + } + + private async Task WaitForInnerDockerAsync() + { + const int maxAttempts = 60; + for (var i = 0; i < maxAttempts; i++) + { + var probe = await ExecAsync(["docker", "info"]).ConfigureAwait(false); + if (probe.ExitCode == 0) return; + await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); + } + throw new TimeoutException("Inner dockerd inside the DinD fixture failed to become ready within 60 seconds."); + } + + private static string FixturesDir => Path.Combine(AppContext.BaseDirectory, "Fixtures"); + + private static string SupportFilesHostPath() + { + // Bind the repo root so the bootstrapper's anchor sees the same layout as the release tarball. + return RepoRoot.Path; + } +} + +/// Result of a DinD ExecAsync call. +public readonly record struct ExecResult(long ExitCode, string Stdout, string Stderr); + +/// Per-test scratch workspace inside the shared DinD. +public readonly record struct DinDScratch(string Root, string OutputDir, string ConfigPath, DinDPortAllocation Ports) +{ + public string SecretsJsonPath => $"{OutputDir}/secrets/secrets.json"; +} + +/// Per-test 6-port allocation baked into the per-test config. +public readonly record struct DinDPortAllocation( + int ApiHttp, + int ApiHttps, + int WebHttp, + int WebHttps, + int Postgres, + int Scylla); diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDImageCache.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDImageCache.cs new file mode 100644 index 00000000..11cea522 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDImageCache.cs @@ -0,0 +1,36 @@ +using System.Collections.Concurrent; +using DotNet.Testcontainers.Builders; +using DotNet.Testcontainers.Images; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Caches built DinD images by Dockerfile filename so all subclasses of a given fixture share +/// the build cost. The cache is keyed by the on-disk Dockerfile path; the value is the resolved +/// image reference Testcontainers hands back from ImageFromDockerfileBuilder.Build(). +/// +internal static class DinDImageCache +{ + private static readonly ConcurrentDictionary>> Cache = new(); + + public static Task GetOrBuildAsync(string dockerfileName, string fixturesDir) + { + var entry = Cache.GetOrAdd( + dockerfileName, + key => new Lazy>(() => BuildAsync(key, fixturesDir))); + return entry.Value; + } + + private static async Task BuildAsync(string dockerfileName, string fixturesDir) + { + var image = new ImageFromDockerfileBuilder() + .WithName($"interfold-bootstrapper-test/{dockerfileName.ToLowerInvariant()}:latest") + .WithDockerfileDirectory(fixturesDir) + .WithDockerfile(dockerfileName) + .WithCleanUp(false) + .Build(); + await image.CreateAsync().ConfigureAwait(false); + return image; + } +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimit.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimit.cs new file mode 100644 index 00000000..241135b7 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimit.cs @@ -0,0 +1,92 @@ +using System.Globalization; +using TUnit.Core.Interfaces; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Caps how many bootstrapper integration tests run concurrently across the whole assembly, +/// sized against the host / docker daemon's actual CPU, memory, and disk budget. +/// +/// +/// +/// Each active test does several docker exec calls into a per-fixture DinD container's +/// inner daemon, publishes an Aspire-managed compose stack (Scylla + Postgres + API), and +/// pins a per-fixture image layer plus inner named volumes to disk. Running too many in +/// parallel produces BadGateway exec responses, docker probe timed out health +/// check failures, and — historically — No space left on device runner crashes. +/// +/// +/// Resolution order (evaluated exactly once when TUnit first reads +/// ; the resulting value is captured in a keyed +/// SemaphoreSlim(limit, limit) for the whole session): +/// +/// +/// +/// If INTERFOLD_DIND_PARALLEL_LIMIT is set to a positive integer, use it verbatim. +/// Skips the auto-probe entirely; matches CI's explicit override on ci-cd.yml. +/// +/// +/// Otherwise, probe the docker daemon via +/// (docker info for NCPU / MemTotal / DockerRootDir, with a host fallback via +/// / / +/// ). On Windows Docker Desktop the daemon path is critical — +/// the WSL2 VM's budget is much smaller than the physical host, and probing the host +/// directly would over-provision. +/// +/// +/// Feed the snapshot into , which +/// leaves the operator-requested headroom on each axis: 10% RAM free after peak +/// load, 15% disk free after peak load, and ~1.5 CPU per slot (Scylla is +/// shard-per-core, dockerd + API round up). +/// +/// +/// Clamp to [1, ]. +/// +/// +/// +/// Diagnostic lines are emitted to stdout during construction so operators can see why +/// the runner picked a particular concurrency level. Both are prefixed with +/// [DinDParallelLimit] and land in the TUnit run log and any CI job log — +/// [DinDParallelLimit] source=... from the probe and [DinDParallelLimit] limit=N ... +/// from the choice. +/// +/// +/// Tuning env vars (all optional, invariant culture): +/// +/// INTERFOLD_DIND_PARALLEL_LIMIT — hard override; skips auto-probe. +/// INTERFOLD_DIND_CPU_PER_SLOT — default 1.5 (double). +/// INTERFOLD_DIND_MEM_PER_SLOT_MB — default 2560. +/// INTERFOLD_DIND_DISK_PER_SLOT_MB — default 4096. +/// INTERFOLD_DIND_MEM_HEADROOM_PCT — default 10. +/// INTERFOLD_DIND_DISK_HEADROOM_PCT — default 15. +/// +/// +/// +public sealed class DinDParallelLimit : IParallelLimit +{ + internal const string EnvVarName = "INTERFOLD_DIND_PARALLEL_LIMIT"; + + public int Limit { get; } = ResolveLimit(); + + private static int ResolveLimit() + { + var raw = Environment.GetEnvironmentVariable(EnvVarName); + if (!string.IsNullOrWhiteSpace(raw) + && int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed) + && parsed > 0) + { + Console.WriteLine($"[DinDParallelLimit] limit={parsed} ({EnvVarName} override)"); + return parsed; + } + + var budget = DinDResourceProbe.Probe(); + var costs = SlotCosts.FromEnvironment(Environment.GetEnvironmentVariable); + var cpuSlots = DinDParallelLimitCalculator.ComputeCpuSlots(budget.Cpus, costs.CpuPerSlot); + var memSlots = DinDParallelLimitCalculator.ComputeMemorySlots(budget.TotalMemoryBytes, costs); + var diskSlots = DinDParallelLimitCalculator.ComputeDiskSlots(budget.FreeDiskBytes, budget.TotalDiskBytes, costs); + var limit = DinDParallelLimitCalculator.Compute(budget, costs); + Console.WriteLine( + $"[DinDParallelLimit] limit={limit} (cpuSlots={cpuSlots} memSlots={memSlots} diskSlots={diskSlots})"); + return limit; + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimitCalculator.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimitCalculator.cs new file mode 100644 index 00000000..2f037edc --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDParallelLimitCalculator.cs @@ -0,0 +1,178 @@ +using System.Globalization; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Pure math for turning a host / docker-daemon resource snapshot into a safe upper +/// bound on concurrent DinD test scenarios. All methods are side-effect free so the +/// numbers can be asserted directly in Interfold.Bootstrapper.UnitTests without +/// touching Docker or the file system. +/// +/// +/// The three axes (CPU, memory, disk) are combined by as +/// Min(cpuSlots, memSlots, diskSlots) and then clamped into +/// [1, ]. The floor of 1 guarantees forward progress +/// even on a tightly constrained box; the ceiling of 16 prevents pathological +/// over-scheduling on very fat hosts (nobody usefully runs more than 16 concurrent +/// DinDs — dockerd's exec throughput plateaus long before that). +/// +public static class DinDParallelLimitCalculator +{ + /// + /// Upper bound the calculator will ever return. Applied after the three-axis Min so + /// even a machine with ample RAM / disk / CPU never spins up more DinDs than + /// dockerd can usefully juggle. + /// + public const int HardCeiling = 16; + + /// + /// Combines the three axes and clamps into [1, ]. + /// + public static int Compute(ResourceBudget budget, SlotCosts costs) + { + var cpuSlots = ComputeCpuSlots(budget.Cpus, costs.CpuPerSlot); + var memSlots = ComputeMemorySlots(budget.TotalMemoryBytes, costs); + var diskSlots = ComputeDiskSlots(budget.FreeDiskBytes, budget.TotalDiskBytes, costs); + var min = Math.Min(cpuSlots, Math.Min(memSlots, diskSlots)); + return Math.Clamp(min, 1, HardCeiling); + } + + /// + /// Slots supported by the CPU budget: floor(cpus / cpuPerSlot). Returns + /// when is non-positive so + /// this axis effectively drops out of the Min. + /// + public static int ComputeCpuSlots(int cpus, double cpuPerSlot) + { + if (cpuPerSlot <= 0) return HardCeiling; + if (cpus <= 0) return 0; + return (int)Math.Floor(cpus / cpuPerSlot); + } + + /// + /// Slots supported by the memory budget after reserving the headroom fraction. + /// floor((totalMem * (1 - headroom%)) / memPerSlot). Returns + /// when is + /// non-positive. + /// + public static int ComputeMemorySlots(long totalMemoryBytes, SlotCosts costs) + { + if (costs.MemPerSlotBytes <= 0) return HardCeiling; + if (totalMemoryBytes <= 0) return 0; + var headroomPct = Math.Clamp(costs.MemHeadroomPct, 0, 100); + var usable = (long)(totalMemoryBytes * ((100 - headroomPct) / 100.0)); + if (usable <= 0) return 0; + var slots = usable / costs.MemPerSlotBytes; + return slots > int.MaxValue ? int.MaxValue : (int)slots; + } + + /// + /// Slots supported by the disk budget after reserving the headroom fraction of the + /// total drive. floor((freeDisk - totalDisk * headroom%) / diskPerSlot). + /// Returns 0 if the drive is already tighter than the headroom (operators see the + /// diagnostic line and can free space or override); the outer clamp lifts to 1 so + /// the suite still tries to run one at a time. Returns + /// when is non-positive. + /// + public static int ComputeDiskSlots(long freeDiskBytes, long totalDiskBytes, SlotCosts costs) + { + if (costs.DiskPerSlotBytes <= 0) return HardCeiling; + if (freeDiskBytes <= 0 || totalDiskBytes <= 0) return 0; + var headroomPct = Math.Clamp(costs.DiskHeadroomPct, 0, 100); + var floorBytes = (long)(totalDiskBytes * (headroomPct / 100.0)); + var usable = freeDiskBytes - floorBytes; + if (usable <= 0) return 0; + var slots = usable / costs.DiskPerSlotBytes; + return slots > int.MaxValue ? int.MaxValue : (int)slots; + } +} + +/// +/// Snapshot of what the host / docker daemon reports at probe time. +/// Deliberately dumb POCO so unit tests can construct arbitrary combinations. +/// +/// Logical CPU count (0 or negative signals "unknown"). +/// Total physical memory available to the daemon or process. +/// Free bytes on the drive holding docker's root (or the OS drive on fallback). +/// Total bytes on the same drive; used to compute the disk headroom floor. +public readonly record struct ResourceBudget( + int Cpus, + long TotalMemoryBytes, + long FreeDiskBytes, + long TotalDiskBytes); + +/// +/// Per-slot resource cost + free-space headroom expectations. Defaults are tuned +/// against the fixture inventory: outer DinD idle (~0.5 GB) plus a peak inner +/// compose stack (Scylla ~1.5 GB + Postgres ~0.2 GB + API ~0.2 GB) plus safety. +/// Each field is env-overridable via . +/// +public readonly record struct SlotCosts( + double CpuPerSlot, + long MemPerSlotBytes, + long DiskPerSlotBytes, + int MemHeadroomPct, + int DiskHeadroomPct) +{ + /// Default CPU units reserved per active DinD slot (1 shard/core for Scylla + dockerd overhead). + public const double DefaultCpuPerSlot = 1.5; + /// Default memory reserved per active DinD slot (outer + inner peak). + public const long DefaultMemPerSlotBytes = 2560L * 1024 * 1024; + /// Default disk reserved per active DinD slot (per-fixture image layers + inner volumes + logs). + public const long DefaultDiskPerSlotBytes = 4096L * 1024 * 1024; + /// Default free-memory percentage the calculator preserves after peak load. + public const int DefaultMemHeadroomPct = 10; + /// Default free-disk percentage the calculator preserves after peak load. + public const int DefaultDiskHeadroomPct = 15; + + /// Baked-in defaults, suitable for the fixture inventory as of writing. + public static SlotCosts Default => new( + DefaultCpuPerSlot, + DefaultMemPerSlotBytes, + DefaultDiskPerSlotBytes, + DefaultMemHeadroomPct, + DefaultDiskHeadroomPct); + + /// + /// Reads each per-slot cost + headroom from the environment, falling back to the + /// baked-in default on missing / non-parseable / non-positive values. The + /// indirection keeps this testable without touching + /// process environment. + /// + public static SlotCosts FromEnvironment(Func getEnv) => new( + ReadDouble(getEnv, "INTERFOLD_DIND_CPU_PER_SLOT", DefaultCpuPerSlot), + ReadMebibytes(getEnv, "INTERFOLD_DIND_MEM_PER_SLOT_MB", DefaultMemPerSlotBytes), + ReadMebibytes(getEnv, "INTERFOLD_DIND_DISK_PER_SLOT_MB", DefaultDiskPerSlotBytes), + ReadInt(getEnv, "INTERFOLD_DIND_MEM_HEADROOM_PCT", DefaultMemHeadroomPct), + ReadInt(getEnv, "INTERFOLD_DIND_DISK_HEADROOM_PCT", DefaultDiskHeadroomPct)); + + private static double ReadDouble(Func getEnv, string key, double fallback) + { + var raw = getEnv(key); + return !string.IsNullOrWhiteSpace(raw) + && double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) + && value > 0 + ? value + : fallback; + } + + private static int ReadInt(Func getEnv, string key, int fallback) + { + var raw = getEnv(key); + return !string.IsNullOrWhiteSpace(raw) + && int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) + && value >= 0 + ? value + : fallback; + } + + private static long ReadMebibytes(Func getEnv, string key, long fallbackBytes) + { + var raw = getEnv(key); + return !string.IsNullOrWhiteSpace(raw) + && long.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var mb) + && mb > 0 + ? mb * 1024L * 1024L + : fallbackBytes; + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDResourceProbe.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDResourceProbe.cs new file mode 100644 index 00000000..69ba3857 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/DinDResourceProbe.cs @@ -0,0 +1,153 @@ +using System.Diagnostics; +using System.Text.Json; + +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Best-effort snapshot of the resource budget available to concurrent DinD tests. +/// Tries the docker daemon first (via docker info) because that's the ceiling +/// that actually matters when the test scenarios are all remote-controlled containers. +/// On Windows Docker Desktop the daemon's NCPU / MemTotal reflect the +/// WSL2 VM, which is usually much smaller than the physical host — asking the host +/// directly would over-provision. If docker isn't on PATH, times out, or returns +/// malformed JSON, falls back to the host: , +/// , and against the OS drive. +/// +/// +/// Never throws — probe is best-effort by contract. Emits exactly one diagnostic line +/// per invocation to stdout so operators can see which source and which numbers drove +/// the sizing decision. The line is prefixed with [DinDParallelLimit] so it +/// grep-groups with the choice diagnostic emitted by . +/// +public static class DinDResourceProbe +{ + private static readonly TimeSpan DockerInfoTimeout = TimeSpan.FromSeconds(5); + + /// + /// Samples the daemon (preferred) or host (fallback) resources exactly once and + /// returns the numbers as a . Also writes a + /// [DinDParallelLimit] source=<docker|host> ... line to stdout. + /// + public static ResourceBudget Probe() + { + var docker = TryProbeDocker(); + if (docker is { } b) + { + EmitDiagnostic("docker", b); + return b; + } + var host = ProbeHost(); + EmitDiagnostic("host", host); + return host; + } + + private static ResourceBudget? TryProbeDocker() + { + try + { + var psi = new ProcessStartInfo("docker", "info --format {{json .}}") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + using var proc = Process.Start(psi); + if (proc is null) return null; + + var stdout = proc.StandardOutput.ReadToEndAsync(); + var stderr = proc.StandardError.ReadToEndAsync(); + + if (!proc.WaitForExit(DockerInfoTimeout)) + { + try { proc.Kill(entireProcessTree: true); } catch { /* best effort */ } + return null; + } + _ = stderr; // drained purely to keep the pipe from stalling + + if (proc.ExitCode != 0) return null; + + var json = stdout.GetAwaiter().GetResult(); + if (string.IsNullOrWhiteSpace(json)) return null; + + using var doc = JsonDocument.Parse(json); + var root = doc.RootElement; + + var ncpu = TryInt(root, "NCPU"); + var memTotal = TryLong(root, "MemTotal"); + var dockerRoot = root.TryGetProperty("DockerRootDir", out var drEl) ? drEl.GetString() : null; + + if (ncpu <= 0 || memTotal <= 0) return null; + + var (freeDisk, totalDisk) = ResolveDiskFor(dockerRoot); + if (freeDisk <= 0 || totalDisk <= 0) return null; + + return new ResourceBudget(ncpu, memTotal, freeDisk, totalDisk); + } + catch + { + return null; + } + } + + private static ResourceBudget ProbeHost() + { + var cpus = Environment.ProcessorCount; + // TotalAvailableMemoryBytes reflects the container cgroup cap (if any) or the + // physical RAM the GC can address, which is what we want as a ceiling — not + // WorkingSet or CommittedBytes, which are process-scoped. + var totalMem = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; + var (freeDisk, totalDisk) = ResolveDiskFor(preferredPath: null); + return new ResourceBudget(cpus, totalMem, freeDisk, totalDisk); + } + + /// + /// Best-effort disk stats. Tries first (docker + /// daemon's DockerRootDir when we could parse it), then the drive holding + /// the test binaries. On Windows Docker Desktop the docker root is a path inside + /// the WSL2 VM (/var/lib/docker) that doesn't exist on the host — that + /// call fails and we drop through to C:\, whose free space is a fair proxy + /// for the WSL2 VHDX-backed disk anyway (Docker Desktop stores it on the host). + /// + private static (long free, long total) ResolveDiskFor(string? preferredPath) + { + foreach (var path in new[] { preferredPath, Path.GetPathRoot(AppContext.BaseDirectory) }) + { + if (string.IsNullOrWhiteSpace(path)) continue; + try + { + var drive = new DriveInfo(path); + if (drive.IsReady && drive.TotalSize > 0) + { + return (drive.AvailableFreeSpace, drive.TotalSize); + } + } + catch + { + // try the next candidate + } + } + return (0, 0); + } + + private static int TryInt(JsonElement root, string name) + => root.TryGetProperty(name, out var el) && el.ValueKind == JsonValueKind.Number && el.TryGetInt32(out var v) + ? v + : 0; + + private static long TryLong(JsonElement root, string name) + => root.TryGetProperty(name, out var el) && el.ValueKind == JsonValueKind.Number && el.TryGetInt64(out var v) + ? v + : 0L; + + private static void EmitDiagnostic(string source, ResourceBudget budget) + { + const double gb = 1024.0 * 1024.0 * 1024.0; + var memGb = budget.TotalMemoryBytes / gb; + var freeGb = budget.FreeDiskBytes / gb; + var totalGb = budget.TotalDiskBytes / gb; + Console.WriteLine( + $"[DinDParallelLimit] source={source} cpu={budget.Cpus} mem={memGb:F1}GB disk={freeGb:F1}GB free of {totalGb:F1}GB"); + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-bare-dind b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-bare-dind new file mode 100644 index 00000000..51d3efb6 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-bare-dind @@ -0,0 +1,32 @@ +# Test fixture - NOT shipped. RedHat-family analog of Dockerfile.ubuntu-bare-dind. Drives the +# bootstrapper's PrereqsPhase against a host where the dnf-based docker install path will run. +# +# Same shape as the Ubuntu bare fixture: no docker, no openssl, no inner dockerd. +FROM fedora:40 + +# Minimal base + sudo so we can simulate non-root drop-down. Crucially NO docker, NO openssl. +# `crypto-policies` is pulled in implicitly by ca-certificates and brings *libraries* that +# look like openssl (libcrypto.so) but NOT the `openssl` CLI, which is what PrereqsPhase +# detects via `command -v openssl`. +RUN dnf install -y --setopt=install_weak_deps=False \ + ca-certificates \ + curl \ + gnupg2 \ + iproute \ + sudo \ + which \ + && dnf remove -y openssl || true \ + && dnf clean all + +# Build-time guards so we catch base-image regressions early. If Fedora ever ships docker or +# openssl by default in this image these will fail with a clear message. +RUN ! command -v docker >/dev/null 2>&1 || (echo "ERROR: docker pre-installed in bare fixture" && exit 1) +RUN ! command -v openssl >/dev/null 2>&1 || (echo "ERROR: openssl pre-installed in bare fixture" && exit 1) + +# Non-root user for RefusesWhenNotRoot. Distinct UID from the ubuntu fixture so a stray exec +# can't accidentally match the wrong user. +RUN useradd --create-home --uid 4343 testuser + +WORKDIR /opt + +ENTRYPOINT ["sleep", "infinity"] diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-dind b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-dind new file mode 100644 index 00000000..2f16da89 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.fedora-dind @@ -0,0 +1,29 @@ +# Test fixture - NOT shipped. Fedora 40 variant of the DinD harness; exercises the dnf install +# path and the update-ca-trust-extract trust-store install in CertificatePhase. +FROM fedora:40 + +RUN dnf install -y \ + ca-certificates \ + curl \ + iptables \ + iproute \ + kmod \ + openssl \ + sudo \ + xz \ + && dnf -y install dnf-plugins-core \ + # Fedora 40 ships dnf4 where `addrepo --from-repofile` (dnf5 syntax) is not recognised. + # `--add-repo URL` is the portable form across dnf4 and dnf5. + && dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo \ + && dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \ + && dnf clean all + +COPY dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh +# Same CRLF guard as the Ubuntu fixture - the script may have Windows line endings on checkout. +RUN sed -i 's/\r$//' /usr/local/bin/dockerd-entrypoint.sh \ + && chmod +x /usr/local/bin/dockerd-entrypoint.sh + +WORKDIR /opt +EXPOSE 2375 + +ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh"] diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-bare-dind b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-bare-dind new file mode 100644 index 00000000..58aafd12 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-bare-dind @@ -0,0 +1,39 @@ +# Test fixture - NOT shipped. Used by PrereqsPhaseTests to drive the bootstrapper's +# prerequisites phase against a Linux host where Docker and openssl are deliberately ABSENT. +# +# Differences vs Dockerfile.ubuntu-dind: +# * No docker-ce / containerd / compose plugin install — the bootstrapper must install them. +# * No openssl — the bootstrapper must install it. +# * Entrypoint just sleeps; there is no inner dockerd to wait for (we don't preload images). +# * We DO ship a tiny `apt-utils` set so apt-get update works inside the container; that's +# not part of "Docker tooling" so it's not what we're trying to detect installation of. +FROM ubuntu:24.04 + +ENV DEBIAN_FRONTEND=noninteractive + +# Minimal base: ca-certificates so apt over HTTPS works, curl/gnupg so the bootstrapper can +# fetch the Docker repo key, sudo so we can simulate running with sudo. Crucially NO docker +# package, NO openssl package — those are what PrereqsPhase will install at test time. +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + gnupg \ + iproute2 \ + sudo \ + && apt-get remove -y openssl \ + && rm -rf /var/lib/apt/lists/* + +# Sanity-check that our "bare" assumption actually holds at build time. If a future base image +# starts shipping docker or openssl by default these will fail loudly here. +RUN ! command -v docker >/dev/null 2>&1 || (echo "ERROR: docker pre-installed in bare fixture" && exit 1) +RUN ! command -v openssl >/dev/null 2>&1 || (echo "ERROR: openssl pre-installed in bare fixture" && exit 1) + +# Create a non-root test user used by the RefusesWhenNotRoot test. UID/GID are arbitrary. +RUN useradd --create-home --uid 4242 testuser + +WORKDIR /opt + +# No dockerd entrypoint — the bootstrapper will install docker but we never need to actually +# run any containers inside this fixture. Sleep keeps the container alive for `docker exec`. +ENTRYPOINT ["sleep", "infinity"] diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-dind b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-dind new file mode 100644 index 00000000..64744dc9 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.ubuntu-dind @@ -0,0 +1,60 @@ +# Test fixture - NOT shipped. Used by the Bootstrapper integration tests' UbuntuDinDFixture to +# host an isolated Docker daemon inside a DinD container. The bootstrapper binary and its +# support files (scripts/, migrations, db/) are bind-mounted in at runtime. +# +# Notes: +# * Uses Ubuntu 24.04 LTS so we exercise the Debian/Ubuntu code path of PrerequisitesPhase. +# * `iptables-legacy` is required because Docker-in-Docker on modern kernels still expects +# the legacy backend for its built-in bridge network. +# * `--storage-driver=vfs` keeps the inner dockerd portable across hosts whose overlay2 +# setup interacts badly with nested mounts. +FROM ubuntu:24.04 + +ENV DEBIAN_FRONTEND=noninteractive + +# jq and systemd are pre-installed here (not on-demand at test time) so parallel +# tests in SystemdInstallTests can never race on /var/lib/dpkg/lock-frontend inside +# the session-shared DinD. Two tests in that class used to call apt-get install jq +# and apt-get install systemd on-demand under >/dev/null 2>&1, and CI would flake +# with exit 100 whenever the schedule interleaved them. Baking both packages here +# also makes those tests ~10s faster and keeps PID 1 unchanged: systemd is present +# as a binary on PATH (so systemd-analyze verify works during install-service), +# but the ENTRYPOINT stays dockerd-entrypoint.sh — nothing switches to systemd as +# PID 1, which is the invariant SystemdInstallTests' class docstring pins. +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + gnupg \ + iptables \ + iproute2 \ + jq \ + kmod \ + openssl \ + sudo \ + systemd \ + xz-utils \ + && install -m 0755 -d /etc/apt/keyrings \ + && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg \ + && chmod a+r /etc/apt/keyrings/docker.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu noble stable" \ + > /etc/apt/sources.list.d/docker.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \ + && rm -rf /var/lib/apt/lists/* + +# Switch to legacy iptables so dockerd's built-in NAT rules apply cleanly inside the container. +RUN update-alternatives --set iptables /usr/sbin/iptables-legacy \ + && update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy + +COPY dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh +# Strip any CRLF line endings that snuck in from a Windows checkout; otherwise the shebang +# resolves to `/usr/bin/env bash\r` and the container exits immediately on start. +RUN sed -i 's/\r$//' /usr/local/bin/dockerd-entrypoint.sh \ + && chmod +x /usr/local/bin/dockerd-entrypoint.sh + +# Keep the bootstrapper artifacts mounted from the host fixture. +WORKDIR /opt +EXPOSE 2375 + +ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh"] diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.unsupported-distro-dind b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.unsupported-distro-dind new file mode 100644 index 00000000..203adb53 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/Dockerfile.unsupported-distro-dind @@ -0,0 +1,27 @@ +# Test fixture - NOT shipped. Negative-path harness: a glibc-compatible base whose os-release +# claims to be a distro family the bootstrapper does not support. +# +# We deliberately use debian-slim (glibc) so the self-contained bootstrapper binary - linked +# against glibc - can actually exec. The earlier Alpine variant fails before any C# code runs +# because Alpine ships musl libc and there's no glibc dynamic linker, which would only test +# "wrong libc", not the bootstrapper's distro-family rejection. +# +# To trigger the rejection we overwrite /etc/os-release with an ID the bootstrapper's +# DistroInfo resolver does not recognise (see tools/Interfold.Bootstrapper/Util/DistroInfo.cs). +FROM debian:bookworm-slim + +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Replace os-release so DistroInfo.Resolve() returns DistroFamily.Unknown. +RUN printf 'ID=interfold-test-unsupported\nID_LIKE=void\nPRETTY_NAME="Interfold Test - Unsupported Distro"\n' \ + > /etc/os-release + +# Keep the layout identical to the other DinD fixtures so test code can copy in / out using the +# same paths. There's no dockerd here - the bootstrapper should refuse before it ever tries to +# reach the daemon. +WORKDIR /opt + +# Sleep forever; tests drive everything through `docker exec`. +CMD ["sleep", "infinity"] diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraBarePrereqsDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraBarePrereqsDinDFixture.cs new file mode 100644 index 00000000..31947597 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraBarePrereqsDinDFixture.cs @@ -0,0 +1,21 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// DinD fixture rooted on a stripped Fedora 40 image with neither docker nor +/// openssl pre-installed. Used exclusively by PrereqsPhaseTests to exercise the +/// dnf install path inside PrerequisitesPhase (the RedHat-family counterpart of the +/// Ubuntu bare fixture). +/// +/// +/// Shares the fixture-level dockerd-absence guard, PreloadImages = false, and +/// InitializeAsync shape with via +/// . +/// +public sealed class FedoraBarePrereqsDinDFixture : BareDinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.fedora-bare-dind"; + + protected override string DistroLabel => "Fedora"; + + protected override string PackageManager => "dnf"; +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraDinDFixture.cs new file mode 100644 index 00000000..a0595508 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/FedoraDinDFixture.cs @@ -0,0 +1,11 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// DinD fixture rooted on fedora:40 — exercises the dnf path of PrerequisitesPhase +/// and the RHEL/Fedora trust-store install via update-ca-trust extract. +/// +public sealed class FedoraDinDFixture : DinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.fedora-dind"; +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuBarePrereqsDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuBarePrereqsDinDFixture.cs new file mode 100644 index 00000000..aa41ac52 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuBarePrereqsDinDFixture.cs @@ -0,0 +1,29 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// DinD fixture rooted on a stripped Ubuntu 24.04 image with neither docker nor +/// openssl pre-installed. Used exclusively by PrereqsPhaseTests to exercise the +/// apt install path inside PrerequisitesPhase. +/// +/// +/// +/// Shares fixture-level dockerd-absence guard, PreloadImages = false, and +/// InitializeAsync shape with via +/// . See that base class for the full rationale on why +/// the "docker isn't pre-installed" precondition lives at fixture build time rather +/// than inside individual test cases. +/// +/// +/// We share this with PrereqsPhaseTests via SharedType.PerTestSession +/// (like the other DinD fixtures) but each test inside still runs in its own scratch +/// directory so they don't interfere with each other's installed-package state. +/// +/// +public sealed class UbuntuBarePrereqsDinDFixture : BareDinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.ubuntu-bare-dind"; + + protected override string DistroLabel => "Ubuntu"; + + protected override string PackageManager => "apt"; +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuCassandraDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuCassandraDinDFixture.cs new file mode 100644 index 00000000..19a27336 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuCassandraDinDFixture.cs @@ -0,0 +1,31 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Ubuntu 24.04 DinD variant that additionally pre-pulls the cassandra:5 base image +/// (referenced by FROM cassandra:5 in db/cassandra/Dockerfile) so the +/// call +/// invoked from bootstrap / launch / update-images can build +/// interfold-cassandra:local against the local cache instead of fetching cassandra:5 +/// over the network on every test run. +/// +/// +/// A separate fixture (instead of unconditionally adding cassandra:5 to +/// ) so the vast majority of tests that only exercise the +/// Scylla path don't pay the extra image pull. The scylla-mode +/// stays byte-for-byte the same as before; cassandra-mode tests opt in by declaring this +/// fixture in their ClassDataSource attribute. +/// +/// Intentionally keeps the default vfs store (does not enable +/// ): nested DinD on GHA cannot +/// mount containerd-overlayfs during docker build of the Cassandra Dockerfile +/// (invalid argument on overlay-on-overlay). The compose#14014 dangling-image +/// surface is covered by unit coverage of the inspect-based digest path plus the +/// regression that still force-rebuilds the local tag under vfs. +/// +/// +public sealed class UbuntuCassandraDinDFixture : DinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.ubuntu-dind"; + + protected override IReadOnlyList AdditionalPreloadImages => ["cassandra:5"]; +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuDinDFixture.cs new file mode 100644 index 00000000..b9fac662 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UbuntuDinDFixture.cs @@ -0,0 +1,11 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// DinD fixture rooted on ubuntu:24.04 — exercises the apt path of PrerequisitesPhase +/// and the Debian/Ubuntu trust-store install in CertificatePhase. +/// +public sealed class UbuntuDinDFixture : DinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.ubuntu-dind"; +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UnsupportedDistroDinDFixture.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UnsupportedDistroDinDFixture.cs new file mode 100644 index 00000000..be719461 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/UnsupportedDistroDinDFixture.cs @@ -0,0 +1,16 @@ +namespace Interfold.Bootstrapper.IntegrationTests.Fixtures; + +/// +/// Negative-path fixture: a glibc-compatible Debian-slim base whose /etc/os-release +/// claims an unknown distro ID. The bootstrapper binary execs successfully, hits +/// PrerequisitesPhase, and refuses with the "Unsupported Linux distribution" message +/// before touching any package manager. See Dockerfile.unsupported-distro-dind. +/// +public sealed class UnsupportedDistroDinDFixture : DinDFixtureBase +{ + protected override string DockerfileName => "Dockerfile.unsupported-distro-dind"; + + // No Docker daemon to wait for, no API image to load, no images to pre-pull. + protected override bool PreloadImages => false; +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/dockerd-entrypoint.sh b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/dockerd-entrypoint.sh new file mode 100644 index 00000000..bd770684 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/dockerd-entrypoint.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Shared entrypoint for the Ubuntu/Fedora DinD fixtures. +# Starts dockerd with VFS storage (portable across host overlays) and a TCP socket on 2375 so the +# outer Testcontainers fixture can probe `docker info` over the network. +set -euo pipefail + +# Scylla refuses to start unless fs.aio-max-nr is at least 66563. The Docker Desktop / +# WSL2 default (often 65536) is just below the floor and the DinD container - although +# privileged - inherits the host kernel's sysctl tree. Bump it before any child +# container can fail on the floor check. Best-effort: if the sysctl is not writable +# (rootless / hardened host) we keep going; the affected tests will fail loudly later +# instead of silently here. +if [ -w /proc/sys/fs/aio-max-nr ]; then + current=$(cat /proc/sys/fs/aio-max-nr 2>/dev/null || echo 0) + if [ "${current:-0}" -lt 1048576 ]; then + echo 1048576 > /proc/sys/fs/aio-max-nr || true + fi +fi + +mkdir -p /var/lib/docker + +# Default: vfs — portable across hosts whose overlay2 setup fights nested mounts. +# Opt-in containerd image store (DIND_CONTAINERD_SNAPSHOTTER=1) reproduces the +# "compose images → No such image after local tag rebuild" failure surface that +# UpdateImagesPhase must tolerate (compose#14014). Cassandra-mode DinD enables this. +DOCKERD_EXTRA_ARGS=(--storage-driver=vfs) +if [ "${DIND_CONTAINERD_SNAPSHOTTER:-0}" = "1" ]; then + mkdir -p /etc/docker + printf '%s\n' '{"features":{"containerd-snapshotter":true}}' > /etc/docker/daemon.json + DOCKERD_EXTRA_ARGS=() +fi + +dockerd \ + --host=unix:///var/run/docker.sock \ + --host=tcp://0.0.0.0:2375 \ + "${DOCKERD_EXTRA_ARGS[@]}" \ + --iptables=true & +DOCKERD_PID=$! + +# Forward signals so `docker stop` of the outer container shuts dockerd cleanly. +trap 'kill -TERM $DOCKERD_PID; wait $DOCKERD_PID' SIGTERM SIGINT + +# Wait for dockerd's socket to exist before tests start probing it. +for i in {1..60}; do + if docker info >/dev/null 2>&1; then + break + fi + sleep 1 +done + +wait $DOCKERD_PID diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.bad-image.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.bad-image.json new file mode 100644 index 00000000..add1d034 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.bad-image.json @@ -0,0 +1,16 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["api.test.local", "alt.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:nonexistent-tag-for-health-timeout-test", + "oauth": { + "googleClientSecret": "test-google-secret", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.cassandra.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.cassandra.json new file mode 100644 index 00000000..089d665b --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.cassandra.json @@ -0,0 +1,17 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["api.test.local", "alt.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "cassandra", + "apiImage": "interfold-api:test", + "postgresDatabase": "test_pg_db", + "oauth": { + "googleClientSecret": "test-google-secret", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.json new file mode 100644 index 00000000..db401709 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.json @@ -0,0 +1,17 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["api.test.local", "alt.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:test", + "postgresDatabase": "test_pg_db", + "oauth": { + "googleClientSecret": "test-google-secret", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.mdns-gate.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.mdns-gate.json new file mode 100644 index 00000000..4a3fd166 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.mdns-gate.json @@ -0,0 +1,17 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["definitely-not-resolvable.local", "api.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:test", + "postgresDatabase": "test_pg_db", + "oauth": { + "googleClientSecret": "test-google-secret", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.trust-install.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.trust-install.json new file mode 100644 index 00000000..62679911 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.trust-install.json @@ -0,0 +1,16 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["api.test.local", "alt.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": true + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:test", + "oauth": { + "googleClientSecret": "test-google-secret", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-http-only.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-http-only.json new file mode 100644 index 00000000..2d415d26 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-http-only.json @@ -0,0 +1,18 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["web.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false, + "includeWeb": true, + "webHttps": false + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:test", + "oauth": { + "googleClientSecret": "", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-https.json b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-https.json new file mode 100644 index 00000000..5f0e8239 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Fixtures/interfold.bootstrap.test.web-https.json @@ -0,0 +1,17 @@ +{ + "deployment": { + "outputDir": "/opt/deploy", + "hosts": ["web.test.local", "127.0.0.1"], + "rootCaName": "Interfold Test Root CA", + "certYears": 2, + "trustStoreInstall": false, + "webHttps": true + }, + "ports": { "apiHttp": 5000, "apiHttps": 5001, "webHttp": 8080, "webHttps": 8081 }, + "databaseMode": "single", + "apiImage": "interfold-api:test", + "oauth": { + "googleClientSecret": "", + "discordClientSecret": "" + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/IncludeWebHttpOnlyTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/IncludeWebHttpOnlyTests.cs new file mode 100644 index 00000000..d07bb151 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/IncludeWebHttpOnlyTests.cs @@ -0,0 +1,81 @@ +using System.Text; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the opt-in deployment.includeWeb path with TLS termination +/// turned off (deployment.webHttps=false). This is the HTTP-only debug variant — the +/// bootstrapper must: +/// +/// Emit the octocon-web service in the generated compose so the wasm UI ships +/// alongside the API. +/// NOT bind-mount the leaf cert or nginx envsubst template — those are only useful for +/// the TLS-terminating variant covered by . +/// Map the operator's ports.webHttp / ports.webHttps entries onto the +/// upstream image's :8080 listener (it ONLY listens there per its +/// Dockerfile.wasm) and emit an HTTP healthcheck against that port. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class IncludeWebHttpOnlyTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task PublishWithIncludeWebHttpOnlyEmitsServiceWithoutTlsWiring() + { + // Pure publish-time contract: includeWeb=true && webHttps=false should land the + // octocon-web service in the compose YAML, but WITHOUT the TLS-only bind mounts and env + // vars. This is the "ship the wasm container for debugging behind an external proxy" + // shape — the operator wants the container, doesn't want the bootstrapper to wire TLS + // termination into it. + var (scratch, _) = await dinD.PublishAsync( + nameof(PublishWithIncludeWebHttpOnlyEmitsServiceWithoutTlsWiring), TestConfigPaths.WebHttpOnlyConfig); + + var composeBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/docker-compose.yaml"); + var compose = Encoding.UTF8.GetString(composeBytes); + + // The service must be present — that's the whole point of the flag. + await Assert.That(compose).Contains("octocon-web") + .Because("octocon-web service must be present when deployment.includeWeb=true even without webHttps"); + + // The HTTP-only healthcheck path: the AppHost emits `curl http://localhost:8080/` for + // the no-TLS variant because the upstream wasm image's nginx listens ONLY on :8080. + await Assert.That(compose).Contains("http://localhost:8080/") + .Because("HTTP-only octocon-web must healthcheck the upstream image's :8080 listener"); + + // None of the TLS-only bits should appear — bind mounts, NGINX_* env vars, or the HTTPS + // healthcheck variant. If any of these slip in the operator gets a half-configured + // stack that either fails to start (mount source missing) or trips a TLS handshake + // failure on the healthcheck. + await Assert.That(compose).DoesNotContain("/etc/nginx/templates/default.conf.template") + .Because("HTTP-only variant must NOT bind-mount the nginx envsubst template"); + await Assert.That(compose).DoesNotContain("NGINX_SERVER_NAME") + .Because("HTTP-only variant must NOT emit NGINX_* env vars"); + await Assert.That(compose).DoesNotContain("NGINX_HTTPS_PORT_SUFFIX") + .Because("HTTP-only variant has no redirect to stitch a port suffix into"); + await Assert.That(compose).DoesNotContain("https://localhost:443/") + .Because("HTTP-only variant must NOT emit the HTTPS healthcheck variant"); + + // Operator host port maps onto the upstream image's :8080 listener. + await Assert.That(compose).Contains($"\"{scratch.Ports.WebHttp}:8080\"") + .Because($"compose must publish the allocated webHttp host port ({scratch.Ports.WebHttp}) onto the upstream image's :8080"); + + // The webHttps host port must NOT appear in the compose: with no TLS termination there + // is no second listener inside the container to map it onto, and binding it anyway just + // shadows webHttp on a second port and confuses operators who configured webHttps=false + // expecting "no HTTPS at all". Match by `":` so we catch any target on the RHS. + await Assert.That(compose).DoesNotContain($"\"{scratch.Ports.WebHttps}:") + .Because($"HTTP-only variant must NOT bind webHttps host port ({scratch.Ports.WebHttps}); " + + "Ports:web-https is unused when webHttps=false"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/Interfold.Bootstrapper.IntegrationTests.csproj b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Interfold.Bootstrapper.IntegrationTests.csproj new file mode 100644 index 00000000..36d688e8 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/Interfold.Bootstrapper.IntegrationTests.csproj @@ -0,0 +1,54 @@ + + + net10.0 + false + true + enable + enable + Interfold.Bootstrapper.IntegrationTests + + $(TestingPlatformCommandLineArguments) --ignore-exit-code 8 + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/LaunchPhaseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/LaunchPhaseTests.cs new file mode 100644 index 00000000..a996c2ba --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/LaunchPhaseTests.cs @@ -0,0 +1,108 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the up subcommand and the broader +/// behaviour. up is the +/// operator's surgical re-launch command — it runs ONLY the launch phase against an +/// already-generated compose stack, skipping config/secrets/certs/publish/db-init entirely. +/// This makes it especially important to confirm the phase can: +/// +/// find the persisted compose file and bring the stack up against it, +/// react to a bad image reference by surfacing diagnostic output instead of hanging, +/// respect the operator's custom HTTP port when polling for API health. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class LaunchPhaseTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task UpCommandLaunchesPrePublishedStack() + { + // Stage the stack via `publish` first (no DB init, no launch), then run `up` standalone. + // This is the supported workflow for operators who want to inspect the compose file + // before turning the lights on. + var (scratch, _) = await dinD.PublishAsync(nameof(UpCommandLaunchesPrePublishedStack), TestConfigPaths.DefaultConfig); + + // `up` doesn't run db-init by default (the Orchestrator routes only Bootstrap/Rotate to it), + // so the API would fail health checks because the postgres admin user wouldn't exist yet. + // We pre-run db-init via a real bootstrap pass first, then teardown only the API services, + // then `up` brings them back. To keep this test simple and still exercise the `up` code + // path, we drive a full bootstrap once and then a follow-up `up` against the same scratch + // - this confirms `up` correctly finds the pre-published compose file and re-launches it. + var bootstrap = await dinD.RunOnScratchAsync(scratch, $"{nameof(UpCommandLaunchesPrePublishedStack)}-bootstrap", "bootstrap", + "--skip-prereqs"); + await Assert.That(bootstrap.ExitCode).IsEqualTo(0).Because(bootstrap.Stderr); + + // Stop the stack (compose down keeps volumes around) so we can verify `up` re-launches it. + await dinD.ExecAsync( + ["docker", "compose", "-f", $"{scratch.OutputDir}/docker-compose.yaml", "stop"]); + + // `up` deliberately omits --config (it operates on the pre-published output dir only), + // so we can't use RunOnScratchAsync here — spell out the arg list by hand. + var up = await dinD.RunBootstrapperAsync($"{nameof(UpCommandLaunchesPrePublishedStack)}-up", + ["up", "--output-dir", scratch.OutputDir, "--non-interactive"]); + await Assert.That(up.ExitCode).IsEqualTo(0) + .Because($"`up` against a stopped pre-published stack should succeed: {up.Stderr}"); + } + + [Test] + public async Task HealthTimeoutDumpsComposeLogs() + { + // We override apiImage to a tag that doesn't exist in the DinD's local image store. The + // compose-up will fail at image-pull time with a clear error message — the bootstrapper + // surfaces that via logger.Error → captured stderr. We don't drive the + // WaitForApiHealthyAsync timeout directly (it's 5 minutes and would blow the test budget), + // but this test still pins the "bad image -> non-zero exit + diagnostic output" contract. + var scratch = await dinD.CreateScratchAsync(nameof(HealthTimeoutDumpsComposeLogs), TestConfigPaths.BadImageConfig); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(HealthTimeoutDumpsComposeLogs), "bootstrap", + "--skip-prereqs"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("bootstrap with a non-pullable apiImage must exit non-zero"); + + // Either the compose pull error or the eventual launch failure must surface in the + // captured output. We accept either because docker version / compose plugin version + // affects exactly which error wins the race (some emit "manifest unknown" on pull, + // others fail at image inspect, others at container create). + var combined = result.Stdout + result.Stderr; + await Assert.That(combined.Length).IsGreaterThan(0) + .Because("a failed bootstrap must produce some diagnostic output"); + } + + [Test] + public async Task RespectsCustomApiHttpPort() + { + // Every test gets its own apiHttp port via the DinD port allocator + // (CreateScratchAsync substitutes the allocated 6-port window into the test + // config). That makes this test redundant with every other launch-style test in + // terms of "does the operator's port actually get used", but it's kept as the + // explicit canary: LaunchPhase must emit a polling line for the allocator's port + // specifically, so a future refactor that hard-codes 5000 / 4200 / 9042 in the + // launch path is caught directly. + var scratch = await dinD.CreateScratchAsync(nameof(RespectsCustomApiHttpPort), TestConfigPaths.DefaultConfig); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(RespectsCustomApiHttpPort), "bootstrap", + "--skip-prereqs"); + + // The test passes whether or not the API ultimately reached healthy state — what we're + // pinning is that the launch phase polled the allocated port. If the API doesn't come up + // (e.g. DinD pressure causes a health timeout) we still want to see the polling line. + var combined = result.Stdout + result.Stderr; + var expectedUrl = $"http://localhost:{scratch.Ports.ApiHttp}"; + await Assert.That(combined).Contains(expectedUrl) + .Because($"LaunchPhase should poll the allocated apiHttp port ({expectedUrl}): {result.Stderr}"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/MdnsGateTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/MdnsGateTests.cs new file mode 100644 index 00000000..51ea81fe --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/MdnsGateTests.cs @@ -0,0 +1,113 @@ +using System.Text; +using System.Text.Json.Nodes; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// End-to-end coverage for the post-fill mDNS gate +/// (ConfigPhase.ApplyMdnsGateAsync) running against a real Ubuntu DinD where +/// avahi-daemon / libnss-mdns is not installed. Complements the deterministic +/// unit tests in ConfigMdnsGateTests: the unit tests drive the probe seam directly, +/// this test exercises the whole gate — the shelled-out getent hosts lookup, the +/// PhaseLogger warning wording surfaced through stderr, and the strip + subsequent +/// Validate re-run — all in a container that reproduces the "operator on a fresh +/// Ubuntu box without mDNS" scenario. +/// +/// +/// The fixture ships definitely-not-resolvable.local alongside api.test.local +/// and 127.0.0.1. In a stock Ubuntu container without avahi none of the +/// .local entries can resolve, so the gate must strip both, leaving +/// 127.0.0.1 as the sole surviving host — enough for Validate to pass and +/// bootstrap to continue. If a regression starts halting on this path (or worse, silently +/// keeps the broken names in the SANs of the generated leaf cert), this test catches it. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class MdnsGateTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task NonInteractiveBootstrapStripsUnresolvableLocalHostsAndContinues() + { + // Must invoke `bootstrap` (not `publish`) — ApplyMdnsGateAsync is deliberately gated to + // BootstrapCommand.Bootstrap only (see ConfigPhase.cs's `if (options.Command == BootstrapCommand.Bootstrap)` + // guard around the gate call). The `publish` / `up` / `update-images` / etc. paths + // load the persisted JSON verbatim and never re-warn about stale .local entries — the + // operator re-runs `bootstrap` to pick up the strip. To keep the test fast we pair + // `bootstrap` with --skip-prereqs (avoids apt install) and --fault-inject=after-publish + // (halts cleanly right after PublishPhase, so certs + compose YAML are produced but + // db-init / launch / compose-up never run). + var scratch = await dinD.CreateScratchAsync( + nameof(NonInteractiveBootstrapStripsUnresolvableLocalHostsAndContinues), + TestConfigPaths.MdnsGateConfig); + + var result = await dinD.RunOnScratchAsync(scratch, + nameof(NonInteractiveBootstrapStripsUnresolvableLocalHostsAndContinues), + "bootstrap", + "--skip-prereqs", "--fault-inject=after-publish"); + + // Exit 0 pins the "bootstrap always continues" contract. A regression that (say) started + // treating the strip as a fatal error would flip this to non-zero and fail loudly. + await Assert.That(result.ExitCode).IsEqualTo(0) + .Because($"non-interactive bootstrap must continue after stripping unresolvable .local hosts: {result.Stderr}"); + + // The warning wording is part of the operator UX contract — it must make clear + // (a) which hosts were removed, (b) that the current run is continuing, and + // (c) how to restore the entries on a future run. The stdout captures both stdout + // and stderr on this code path because PhaseLogger.Warn goes through Console.WriteLine + // (stdout), not Console.Error. + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("removing unresolvable .local host(s)") + .Because("gate must warn about the strip so the operator sees why the SAN list shrank"); + await Assert.That(combined).Contains("bootstrap will continue") + .Because("gate warning must emphasise the current run is continuing — this is the load-bearing wording that pins the 'no halt' invariant"); + await Assert.That(combined).Contains("definitely-not-resolvable.local") + .Because("gate must name the specific hosts it removed so the operator can act on them"); + + // The persisted config on disk must reflect the strip — the caller re-persists after + // mutation so a subsequent `bootstrap` re-run sees the pruned list and doesn't + // re-issue the same warning for the same broken hosts. + var persistedBytes = await dinD.CopyOutAsync(scratch.ConfigPath); + var persistedJson = JsonNode.Parse(persistedBytes) + ?? throw new InvalidOperationException("persisted bootstrap config parsed to null"); + var hosts = persistedJson["deployment"]?["hosts"]?.AsArray() + ?? throw new InvalidOperationException("deployment.hosts missing from persisted config"); + var hostList = hosts.Select(h => h!.GetValue()).ToList(); + + await Assert.That(hostList).DoesNotContain("definitely-not-resolvable.local") + .Because("gate must remove the unresolvable .local entry from the persisted config, not just from the in-memory copy"); + // api.test.local also gets stripped — no avahi in the DinD means every .local fails to + // resolve, and the gate probes each entry individually. + await Assert.That(hostList).DoesNotContain("api.test.local") + .Because("every unresolvable .local entry must be stripped, not just the first failure"); + await Assert.That(hostList).Contains("127.0.0.1") + .Because("non-.local entries must survive the strip so Validate has at least one host to work with"); + + // The generated leaf cert must NOT include the stripped .local names as SANs. This is the + // "the strip actually flows through to the produced artifacts, not just the log line" + // check that guarantees a downstream consumer's TLS handshake against 127.0.0.1 doesn't + // trip a "certificate has a SAN for a name that doesn't resolve" warning in strict clients. + var leafBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/leaf.crt"); + using var leaf = System.Security.Cryptography.X509Certificates.X509Certificate2.CreateFromPem( + Encoding.UTF8.GetString(leafBytes)); + var sanExt = leaf.Extensions + .OfType() + .FirstOrDefault(); + await Assert.That(sanExt).IsNotNull().Because("leaf cert must have a SAN extension"); + var sans = sanExt!.EnumerateDnsNames().ToList(); + await Assert.That(sans).DoesNotContain("definitely-not-resolvable.local") + .Because("stripped .local entries must not leak into the leaf cert's SAN list"); + await Assert.That(sans).DoesNotContain("api.test.local") + .Because("stripped .local entries must not leak into the leaf cert's SAN list"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/PrereqsPhaseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/PrereqsPhaseTests.cs new file mode 100644 index 00000000..1c4f263d --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/PrereqsPhaseTests.cs @@ -0,0 +1,141 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Exercises against +/// "bare" Linux fixtures where neither docker nor openssl is pre-installed. +/// +/// +/// +/// We drive the phase via bootstrap --fault-inject=after-prereqs: the fault hook halts +/// the orchestrator cleanly after the prereqs phase returns, so we never enter config +/// or any later phase. That keeps the test surface tight (we only assert on what +/// PrerequisitesPhase mutated) and avoids needing a working dockerd inside the bare +/// fixture. +/// +/// +/// These tests share state inside the same DinD container (installed packages, +/// /etc/sysctl.d entries). That's OK: each test asserts on the end-state, and +/// reinstall-via-apt/dnf is a no-op once the package is already present. We serialise them +/// under ubuntu-bare-prereqs / fedora-bare-prereqs just to avoid running +/// concurrent apt/dnf operations in the same container. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class UbuntuPrereqsPhaseTests(UbuntuBarePrereqsDinDFixture dinD) +{ + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx, teardown: false); + + [Test] + [NotInParallel("ubuntu-bare-prereqs")] + public async Task InstallsDockerOnDebianWhenAbsent() + { + // The "docker isn't pre-installed" pre-condition has been lifted to + // UbuntuBarePrereqsDinDFixture.InitializeAsync where it runs exactly once at fixture + // build time. Inside the shared session-scoped fixture the apt install is idempotent — + // re-running the bootstrap path on an already-bootstrapped container should still leave + // docker on PATH, which is what this test verifies. The post-condition below therefore + // holds regardless of whether this test runs first or after a sibling test in the same + // [NotInParallel("ubuntu-bare-prereqs")] group has already triggered the install. + var result = await dinD.RunBootstrapperAsync(nameof(InstallsDockerOnDebianWhenAbsent), + ["bootstrap", "--non-interactive", "--fault-inject=after-prereqs"]); + await Assert.That(result.ExitCode).IsEqualTo(0L) + .Because($"bootstrap should halt cleanly after prereqs; got stderr: {result.Stderr}"); + + var postPath = await dinD.ExecAsync(["sh", "-c", "command -v docker"]); + await Assert.That(postPath.ExitCode).IsEqualTo(0L) + .Because($"docker must be on PATH after PrerequisitesPhase; stdout={postPath.Stdout}, stderr={postPath.Stderr}"); + } + + [Test] + [NotInParallel("ubuntu-bare-prereqs")] + public async Task InstallsOpenSslWhenAbsent() + { + // openssl may be left over from a previous test in this fixture's lifetime, so we + // only assert on the post-state. The relevant codepath in PrereqsPhase is idempotent. + var result = await dinD.RunBootstrapperAsync(nameof(InstallsOpenSslWhenAbsent), + ["bootstrap", "--non-interactive", "--fault-inject=after-prereqs"]); + await Assert.That(result.ExitCode).IsEqualTo(0L).Because(result.Stderr); + + var postPath = await dinD.ExecAsync(["sh", "-c", "command -v openssl"]); + await Assert.That(postPath.ExitCode).IsEqualTo(0L) + .Because($"openssl must be on PATH after PrerequisitesPhase; stdout={postPath.Stdout}, stderr={postPath.Stderr}"); + } + + [Test] + [NotInParallel("ubuntu-bare-prereqs")] + public async Task PersistsAioSysctlDropIn() + { + var result = await dinD.RunBootstrapperAsync(nameof(PersistsAioSysctlDropIn), + ["bootstrap", "--non-interactive", "--fault-inject=after-prereqs"]); + await Assert.That(result.ExitCode).IsEqualTo(0L).Because(result.Stderr); + + // PrereqsPhase writes /etc/sysctl.d/99-interfold.conf with a topology-sized + // `fs.aio-max-nr = `. With no config file (the --non-interactive path used here), + // the AIO sizing falls back to the single-node baseline (1 * 116562 + 50000 = 166562); + // we assert only on the key being present so the test stays robust if the per-node + // constants change. + var content = await dinD.ExecAsync(["sh", "-c", "cat /etc/sysctl.d/99-interfold.conf || true"]); + await Assert.That(content.Stdout).Contains("fs.aio-max-nr") + .Because($"sysctl drop-in should contain fs.aio-max-nr; got: {content.Stdout}"); + } + + [Test] + [NotInParallel("ubuntu-bare-prereqs")] + public async Task RefusesWhenNotRoot() + { + // The bare ubuntu Dockerfile creates `testuser` (uid 4242). Run the bootstrapper as that + // user; PrerequisitesPhase.EnsureRoot must reject the invocation with a clear message. + // We use `su -c` to switch identity. The bootstrapper prints to stderr and exits + // non-zero, so the exit propagates back through su. + var result = await dinD.ExecAsync( + ["su", "testuser", "-c", + $"{DinDFixtureBase.BootstrapperMountPath}/interfold-bootstrap bootstrap --non-interactive --fault-inject=after-prereqs"]); + + await Assert.That(result.ExitCode).IsNotEqualTo(0L) + .Because("non-root invocation must exit non-zero"); + + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("with sudo") + .Or.Contains("non-root") + .Or.Contains("require root") + .Because($"bootstrapper should explain the non-root rejection; got: {combined}"); + } +} + +/// +/// Fedora-family analog of . Exercises the dnf install +/// path inside . +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class FedoraPrereqsPhaseTests(FedoraBarePrereqsDinDFixture dinD) +{ + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx, teardown: false); + + [Test] + [NotInParallel("fedora-bare-prereqs")] + public async Task InstallsDockerOnFedoraWhenAbsent() + { + // Mirrors InstallsDockerOnDebianWhenAbsent: the "bare fixture has no docker" guard + // now lives in FedoraBarePrereqsDinDFixture.InitializeAsync and runs once at fixture + // build time. dnf install is idempotent so the post-condition holds regardless of + // sibling-test ordering inside the [NotInParallel("fedora-bare-prereqs")] group. + var result = await dinD.RunBootstrapperAsync(nameof(InstallsDockerOnFedoraWhenAbsent), + ["bootstrap", "--non-interactive", "--fault-inject=after-prereqs"]); + await Assert.That(result.ExitCode).IsEqualTo(0L).Because(result.Stderr); + + var postPath = await dinD.ExecAsync(["sh", "-c", "command -v docker"]); + await Assert.That(postPath.ExitCode).IsEqualTo(0L) + .Because($"docker must be on PATH after PrerequisitesPhase on Fedora; stdout={postPath.Stdout}, stderr={postPath.Stderr}"); + } +} + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/PublishIntegrationTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/PublishIntegrationTests.cs new file mode 100644 index 00000000..ac12a14c --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/PublishIntegrationTests.cs @@ -0,0 +1,126 @@ +using System.Text; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; +using Interfold.Bootstrapper.IntegrationTests.TestServices; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for driven against +/// a real DinD-bound bootstrapper. The pure path is unit-tested in +/// PublishEnvPostProcessingTests; these tests assert the file that actually lands on +/// disk after Aspire publish + our env post-processing matches the contract. +/// +/// +/// All tests here run the publish command only (no compose up), so they don't +/// need the multi-minute health-wait budget that the launch-style tests do — they run in well +/// under 30 seconds each, fully in parallel with every other test in the assembly. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class PublishIntegrationTests(UbuntuDinDFixture dinD) +{ + + /// + /// Documented set of keys we expect PublishPhase.BuildEnvReplacements to fill in the + /// emitted .env. Mirror of the 6 parameter keys asserted by the unit test after the + /// encryption-pepper / JWT / OAuth-secret / leaf-PFX-password migration into + /// internal.secrets — keeping the list duplicated catches drift in either direction + /// at integration time. + /// + private static readonly string[] ExpectedEnvParameterKeys = + [ + "POSTGRES_USER", + "POSTGRES_PASSWORD", + "POSTGRES_INIT_PASSWORD", + "SCYLLA_USER", + "SCYLLA_PASSWORD", + "ENCRYPTION_PRIVATE_KEY", + ]; + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task BindMountPathsResolveAbsoluteInComposeEnv() + { + var (scratch, _) = await dinD.PublishAsync(nameof(BindMountPathsResolveAbsoluteInComposeEnv), TestConfigPaths.DefaultConfig); + + var envBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/.env"); + var env = DotEnvParser.ParseEnv(envBytes); + + // The compose graph emits one bind-mount key per service+target pair; Aspire names them + // _BINDMOUNTS__. Single-mode scylla emits one rackdc mount + the api gets + // one (/certs only — /keys was removed when JWT material moved into internal.secrets). + var bindMountKeys = env + .Where(kv => kv.Key.Contains("BIND", StringComparison.Ordinal)) + .ToList(); + await Assert.That(bindMountKeys.Count).IsGreaterThan(0) + .Because("the published .env should contain at least one bind-mount entry"); + + foreach (var (key, value) in bindMountKeys) + { + await Assert.That(value).IsNotEmpty() + .Because($"bind-mount key {key} should not be blank in the .env"); + await Assert.That(value).StartsWith("/") + .Because($"bind-mount source must be an absolute path: {key}={value}"); + } + } + + [Test] + public async Task CustomApiImageAppearsInGeneratedCompose() + { + // The default test fixture config sets apiImage to `interfold-api:test`, which differs + // from the production default `ghcr.io/azyyyyyy/interfold-api:latest`. The compose YAML + // must reference the override - if not, the rest of the pipeline would silently pull + // from the public registry. + var (scratch, _) = await dinD.PublishAsync(nameof(CustomApiImageAppearsInGeneratedCompose), TestConfigPaths.DefaultConfig); + + var composeBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/docker-compose.yaml"); + var compose = Encoding.UTF8.GetString(composeBytes); + + await Assert.That(compose).Contains("interfold-api:test") + .Because("the custom apiImage from interfold.bootstrap.test.json should appear in the emitted compose"); + // The production default tag must NOT leak through when overridden. + await Assert.That(compose).DoesNotContain("ghcr.io/azyyyyyy/interfold-api:latest") + .Because("the default api image tag must not appear when apiImage is overridden"); + } + + [Test] + public async Task EnvFileContainsAllRequiredKeys() + { + var (scratch, _) = await dinD.PublishAsync(nameof(EnvFileContainsAllRequiredKeys), TestConfigPaths.DefaultConfig); + + var envBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/.env"); + var env = DotEnvParser.ParseEnv(envBytes); + + foreach (var key in ExpectedEnvParameterKeys) + { + await Assert.That(env.ContainsKey(key)).IsTrue() + .Because($"expected env key '{key}' missing from .env"); + await Assert.That(env[key]).IsNotEmpty() + .Because($"env key '{key}' must be non-empty in the post-processed .env"); + } + + // OAuth client secrets + leaf PFX password must NOT appear in the .env any more — + // they live inside internal.secrets and are loaded at API startup (via + // SecretsBootstrapService and the one-shot Kestrel Npgsql loader). + await Assert.That(env.ContainsKey("GOOGLE_OAUTH_CLIENT_SECRET")).IsFalse() + .Because("the google oauth client secret must not leak into compose .env"); + await Assert.That(env.ContainsKey("DISCORD_OAUTH_CLIENT_SECRET")).IsFalse() + .Because("the discord oauth client secret must not leak into compose .env"); + await Assert.That(env.ContainsKey("LEAF_PFX_PASSWORD")).IsFalse() + .Because("the leaf PFX password must live in internal.secrets, not the .env"); + + // Negative invariants — the admin passwords must NEVER appear in the published .env. + await Assert.That(env.ContainsKey("POSTGRES_ADMIN_PASSWORD")).IsFalse() + .Because("postgres admin credential must live in internal.secrets, not the .env"); + await Assert.That(env.ContainsKey("SCYLLA_ADMIN_PASSWORD")).IsFalse() + .Because("scylla admin credential must live in internal.secrets, not the .env"); + } + +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/RepoRoot.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/RepoRoot.cs new file mode 100644 index 00000000..94f0db5b --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/RepoRoot.cs @@ -0,0 +1,31 @@ +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Resolves the repository root by walking up from the test binary's directory looking for the +/// solution file. Avoids hardcoded relative paths that break when the test runner copies binaries +/// to a non-standard location. +/// +internal static class RepoRoot +{ + private static readonly Lazy Resolved = new(Resolve); + + public static string Path => Resolved.Value; + + public static string Combine(params string[] segments) => System.IO.Path.Combine([Path, .. segments]); + + private static string Resolve() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null) + { + if (dir.GetFiles("Interfold.slnx").Length > 0) + { + return dir.FullName; + } + dir = dir.Parent; + } + throw new InvalidOperationException( + $"Could not locate Interfold.slnx starting from {AppContext.BaseDirectory}."); + } +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/RestorePhaseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/RestorePhaseTests.cs new file mode 100644 index 00000000..ef115ad8 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/RestorePhaseTests.cs @@ -0,0 +1,186 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the restore subcommand against a real running compose stack +/// inside the shared Ubuntu DinD fixture. Each test runs a full bootstrap, inserts +/// a marker row, takes a backup, mutates or drops the marker, then restores and asserts +/// the marker is back to its pre-mutation state. +/// +/// +/// +/// The postgres marker uses a dedicated table under the test_pg_db database so we +/// don't have to guess at the API's schema. pg_restore --clean --if-exists drops and +/// recreates the table as part of the restore. +/// +/// +/// The scylla assertions focus on the stack-coordination side effects (containers stopped, +/// data volume overwritten, containers back up) rather than on CQL row-level round-trips +/// — the fixture's config uses databaseMode=single where the API isn't wired to +/// exercise a scylla-side round-trip via the schema, and the pure "restore mechanic" +/// contract (compose stop → wipe → cp → start) is what changed relative to backup. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class RestorePhaseTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task RestorePostgresFromDumpRoundTripsMarkerTable() + { + // 1. Bootstrap → live stack. 2. Create a marker table + row via psql (using the + // admin credentials from secrets.json). 3. Take a backup. 4. DROP the table. + // 5. Restore --restore-postgres --force. 6. Assert the marker table + row are back. + var (scratch, composeFile) = await dinD.BootstrapAsync(nameof(RestorePostgresFromDumpRoundTripsMarkerTable), TestConfigPaths.DefaultConfig); + + // Extract the admin password once from secrets.json — every psql probe below reuses it. + // ReadSecretsFieldAsync copies the file out of the container and parses it in-process, + // which is byte-exact against `grep {field} | sed …` on any well-formed JSON but stays + // resilient to whitespace, ordering, or escape variations the shell regex would miss. + var adminPass = await dinD.ReadSecretsFieldAsync(scratch, "postgresAdminPassword"); + await Assert.That(adminPass).IsNotEmpty() + .Because("postgresAdminPassword must be present in secrets.json after bootstrap"); + + var seed = await dinD.PsqlAsync( + composeFile, "interfold_admin", "test_pg_db", + "\"CREATE TABLE restore_marker(id int primary key); INSERT INTO restore_marker(id) VALUES (42);\"", + password: adminPass, psqlFlags: "-c", host: null); + await Assert.That(seed.ExitCode).IsEqualTo(0L) + .Because($"seeding marker table failed: {seed.Stderr}"); + + // Take the backup that we'll restore from. + var backup = await dinD.RunOnScratchAsync(scratch, $"{nameof(RestorePostgresFromDumpRoundTripsMarkerTable)}-backup", "backup", + "--component", "postgres"); + await Assert.That(backup.ExitCode).IsEqualTo(0).Because($"backup failed: {backup.Stderr}"); + + // Drop the marker table so the restore path has something concrete to bring back. + var drop = await dinD.PsqlAsync( + composeFile, "interfold_admin", "test_pg_db", + "\"DROP TABLE restore_marker;\"", + password: adminPass, psqlFlags: "-c", host: null); + await Assert.That(drop.ExitCode).IsEqualTo(0L) + .Because($"dropping the marker table failed: {drop.Stderr}"); + + // Confirm the table is really gone before the restore — otherwise we'd be + // testing a no-op path and the assertion below would pass vacuously. + var missing = await dinD.PsqlAsync( + composeFile, "interfold_admin", "test_pg_db", + "\"SELECT to_regclass('public.restore_marker') IS NULL\"", + password: adminPass, host: null); + await Assert.That(missing.Stdout.Trim()).IsEqualTo("t") + .Because("marker table must actually be absent before the restore"); + + // Restore --restore-latest picks up the .dump we just took. --force skips the + // destructive-op confirmation prompt (required in --non-interactive mode). + var restore = await dinD.RunOnScratchAsync(scratch, nameof(RestorePostgresFromDumpRoundTripsMarkerTable), "restore", + "--restore-latest", "--force"); + await Assert.That(restore.ExitCode).IsEqualTo(0).Because($"restore failed: {restore.Stderr}"); + + // The marker must be back. Value check pins that the round-trip preserved the + // row data, not just the schema. + var probe = await dinD.PsqlAsync( + composeFile, "interfold_admin", "test_pg_db", + "\"SELECT id FROM restore_marker\"", + password: adminPass, host: null); + await Assert.That(probe.ExitCode).IsEqualTo(0L) + .Because($"post-restore probe failed: {probe.Stderr}"); + await Assert.That(probe.Stdout.Trim()).IsEqualTo("42") + .Because("restore must have re-created the marker table with its original row"); + } + + [Test] + public async Task RestoreLatestPicksMostRecentArchive() + { + // Take two backups with a delay between them, then --restore-latest must resolve + // to the newer archive by mtime. The log lines from RestorePhase.ResolveArchives + // name the chosen path, giving us a deterministic assertion target without + // having to poke inside the running database. + var (scratch, _) = await dinD.BootstrapAsync(nameof(RestoreLatestPicksMostRecentArchive), TestConfigPaths.DefaultConfig); + + // First backup, then a 2s sleep so mtimes are distinguishable at second + // resolution, then a second backup. + var b1 = await dinD.RunOnScratchAsync(scratch, $"{nameof(RestoreLatestPicksMostRecentArchive)}-b1", "backup", + "--component", "postgres"); + await Assert.That(b1.ExitCode).IsEqualTo(0).Because(b1.Stderr); + + await dinD.ExecAsync(["sleep", "2"]); + + var b2 = await dinD.RunOnScratchAsync(scratch, $"{nameof(RestoreLatestPicksMostRecentArchive)}-b2", "backup", + "--component", "postgres"); + await Assert.That(b2.ExitCode).IsEqualTo(0).Because(b2.Stderr); + + // Resolve the newest archive name via a shell one-liner so we know what the + // restore log should reference. + var newestList = await dinD.ExecAsync(["sh", "-c", + $"ls -1t {scratch.OutputDir}/backups/postgres/*.dump | head -1"]); + await Assert.That(newestList.ExitCode).IsEqualTo(0L); + var newestArchive = newestList.Stdout.Trim(); + await Assert.That(newestArchive.Length).IsGreaterThan(0) + .Because("there must be at least one archive present after two backup runs"); + + // Restore with --restore-latest and no explicit --restore-postgres so the + // resolver picks by mtime. The log line names the chosen path — assert it. + var restore = await dinD.RunOnScratchAsync(scratch, nameof(RestoreLatestPicksMostRecentArchive), "restore", + "--restore-latest", "--force"); + await Assert.That(restore.ExitCode).IsEqualTo(0).Because($"restore failed: {restore.Stderr}"); + + var combined = restore.Stdout + restore.Stderr; + await Assert.That(combined).Contains(Path.GetFileName(newestArchive)) + .Because("--restore-latest must resolve to the newest archive by mtime"); + } + + [Test] + public async Task RestoreWithoutArchivesFailsClearly() + { + // Zero-archive-selector case: --restore-latest by itself against an + // empty {backups}/ directory must produce an actionable error naming the + // missing selectors, not crash somewhere deep in pg_restore. + var (scratch, _) = await dinD.BootstrapAsync(nameof(RestoreWithoutArchivesFailsClearly), TestConfigPaths.DefaultConfig); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(RestoreWithoutArchivesFailsClearly), "restore", + "--restore-latest", "--force"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("restore --restore-latest with no archives on disk must fail"); + + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("restore-postgres").Or.Contains("restore-scylla").Or.Contains("archives") + .Because("error must name the missing selectors or archives"); + } + + [Test] + public async Task RestoreInNonInteractiveModeRequiresForce() + { + // Destructive-op safety gate. Passing a real archive selector without --force + // in --non-interactive mode must refuse to proceed and print a clear error + // pointing the operator at the --force flag. + var (scratch, _) = await dinD.BootstrapAsync(nameof(RestoreInNonInteractiveModeRequiresForce), TestConfigPaths.DefaultConfig); + + // Take a backup so there's a real archive to point at. + var backup = await dinD.RunOnScratchAsync(scratch, $"{nameof(RestoreInNonInteractiveModeRequiresForce)}-backup", "backup", + "--component", "postgres"); + await Assert.That(backup.ExitCode).IsEqualTo(0).Because(backup.Stderr); + + // No --force. Must fail with a clear error mentioning --force. + var result = await dinD.RunOnScratchAsync(scratch, nameof(RestoreInNonInteractiveModeRequiresForce), "restore", + "--restore-latest"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("restore in non-interactive mode without --force must be refused"); + + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("--force") + .Because("error must name --force as the escape hatch for non-interactive mode"); + } +} + + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/SystemdInstallTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/SystemdInstallTests.cs new file mode 100644 index 00000000..2587a653 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/SystemdInstallTests.cs @@ -0,0 +1,255 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the install-service subcommand. The shared Ubuntu DinD +/// fixture deliberately keeps PID 1 as dockerd-entrypoint.sh (not systemd), +/// so we exercise the unit-file rendering + on-disk write path against an operator-supplied +/// unit dir (--systemd-unit-dir) and skip the daemon-reload / enable steps. The +/// systemd package IS pre-installed in Dockerfile.ubuntu-dind — the binary +/// (specifically systemd-analyze) works fine outside of a systemd-managed PID 1 +/// and is what and the bootstrapper's +/// install-service phase both rely on. jq is also baked into the image so +/// can rewrite the config JSON without any test +/// having to shell out to apt-get install at runtime. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class SystemdInstallTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task WritesAllThreeUnitFilesToTargetDir() + { + // We don't need a live compose stack for install-service — it only reads the bootstrap + // config + writes unit files. Skip the full bootstrap to keep the test fast. + var scratch = await dinD.CreateScratchAsync(nameof(WritesAllThreeUnitFilesToTargetDir), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(WritesAllThreeUnitFilesToTargetDir), "install-service", + "--systemd-unit-dir", unitDir); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + // Each of the four units must be present on disk. interfold-update.service is + // always rendered — the OnSuccess drop-in that enables the chain is what depends + // on update.enabled, but the target unit exists regardless so manual invocations + // (`update-images`) always have a service to point at. + foreach (var name in new[] { "interfold.service", "interfold-backup.service", "interfold-backup.timer", "interfold-update.service" }) + { + var probe = await dinD.ExecAsync(["test", "-f", $"{unitDir}/{name}"]); + await Assert.That(probe.ExitCode).IsEqualTo(0L) + .Because($"expected {unitDir}/{name} to exist after install-service"); + } + } + + [Test] + public async Task UnitFilesContainExpectedTokens() + { + // Pin the rendered contents: the boot service must use docker compose up -d (not + // `interfold-bootstrap up`), the backup service must point at the resolved config + + // outputDir, and the timer must embed the OnCalendar string from the config. + var scratch = await dinD.CreateScratchAsync(nameof(UnitFilesContainExpectedTokens), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(UnitFilesContainExpectedTokens), "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + var bootService = await dinD.CopyOutAsTextAsync($"{unitDir}/interfold.service"); + await Assert.That(bootService).Contains($"ExecStart=/usr/bin/docker compose -f {scratch.OutputDir}/docker-compose.yaml up -d"); + await Assert.That(bootService).Contains("WantedBy=multi-user.target"); + + var backupService = await dinD.CopyOutAsTextAsync($"{unitDir}/interfold-backup.service"); + await Assert.That(backupService).Contains("ExecStart=/opt/bootstrapper/interfold-bootstrap backup"); + await Assert.That(backupService).Contains($"--config {scratch.ConfigPath}"); + await Assert.That(backupService).Contains($"--output-dir {scratch.OutputDir}"); + + var timer = await dinD.CopyOutAsTextAsync($"{unitDir}/interfold-backup.timer"); + // The test fixture's config doesn't override the schedule, so it lands at the default + // "daily" from BackupSection.Schedule. + await Assert.That(timer).Contains("OnCalendar=daily"); + await Assert.That(timer).Contains("Persistent=true"); + } + + [Test] + public async Task WritesUpdateServiceWhenPresentInUnitNames() + { + // interfold-update.service is always rendered alongside the three baseline units. + // Even when config.update.enabled=false the .service file exists (manual + // `update-images` invocations use it); only the OnSuccess= drop-in is conditional. + var scratch = await dinD.CreateScratchAsync(nameof(WritesUpdateServiceWhenPresentInUnitNames), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(WritesUpdateServiceWhenPresentInUnitNames), "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + var probe = await dinD.ExecAsync(["test", "-f", $"{unitDir}/interfold-update.service"]); + await Assert.That(probe.ExitCode).IsEqualTo(0L) + .Because("interfold-update.service should always be rendered even when update.enabled=false"); + + var updateService = await dinD.CopyOutAsTextAsync($"{unitDir}/interfold-update.service"); + await Assert.That(updateService).Contains("ExecStart=/opt/bootstrapper/interfold-bootstrap update-images"); + await Assert.That(updateService).Contains($"--config {scratch.ConfigPath}"); + await Assert.That(updateService).Contains($"--output-dir {scratch.OutputDir}"); + await Assert.That(updateService).Contains("Type=oneshot"); + } + + [Test] + public async Task WhenUpdateEnabledDropInIsWritten() + { + // Flip update.enabled=true in a copy of the test config, run install-service, and + // assert the OnSuccess drop-in appears at the exact path SystemdInstallPhase + // documents (interfold-backup.service.d/50-chain-update.conf) with the correct + // OnSuccess= directive. + var scratch = await dinD.CreateScratchAsync(nameof(WhenUpdateEnabledDropInIsWritten), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + await OverlayUpdateConfigAsync(scratch.ConfigPath, updateEnabled: true); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(WhenUpdateEnabledDropInIsWritten), "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + var dropInPath = $"{unitDir}/interfold-backup.service.d/50-chain-update.conf"; + var probe = await dinD.ExecAsync(["test", "-f", dropInPath]); + await Assert.That(probe.ExitCode).IsEqualTo(0L) + .Because($"expected drop-in {dropInPath} when update.enabled=true"); + + var content = await dinD.CopyOutAsTextAsync(dropInPath); + await Assert.That(content).Contains("OnSuccess=interfold-update.service") + .Because("drop-in exists to chain the update service after a successful backup"); + await Assert.That(content).Contains("[Unit]") + .Because("drop-in must be a valid systemd unit fragment"); + } + + [Test] + public async Task WhenUpdateDisabledDropInIsAbsent() + { + // Baseline config leaves update.enabled=false — the drop-in must NOT be written, + // even though the .service template it references was rendered. Guards the + // "manual updates only" default stance so operators who never touch the update + // section don't wake up to auto-chained updates. + var scratch = await dinD.CreateScratchAsync(nameof(WhenUpdateDisabledDropInIsAbsent), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(WhenUpdateDisabledDropInIsAbsent), "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + var probe = await dinD.ExecAsync(["test", "-e", $"{unitDir}/interfold-backup.service.d/50-chain-update.conf"]); + await Assert.That(probe.ExitCode).IsNotEqualTo(0L) + .Because("drop-in must be absent when update.enabled=false (default stance)"); + } + + [Test] + public async Task DropInIsRemovedWhenUpdateFlippedOff() + { + // Two-phase run: first install with update.enabled=true (writes the drop-in), + // then re-install with update.enabled=false and assert the drop-in was removed. + // Pins the idempotency contract — flipping the config off must clean up stale + // drop-ins so a subsequent backup no longer fires the update chain. + var scratch = await dinD.CreateScratchAsync(nameof(DropInIsRemovedWhenUpdateFlippedOff), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + // Phase 1: install with update.enabled=true. + await OverlayUpdateConfigAsync(scratch.ConfigPath, updateEnabled: true); + var install1 = await dinD.RunOnScratchAsync(scratch, $"{nameof(DropInIsRemovedWhenUpdateFlippedOff)}-1", "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(install1.ExitCode).IsEqualTo(0).Because(install1.Stderr); + + var probeExists = await dinD.ExecAsync(["test", "-f", $"{unitDir}/interfold-backup.service.d/50-chain-update.conf"]); + await Assert.That(probeExists.ExitCode).IsEqualTo(0L) + .Because("phase 1 should have written the drop-in"); + + // Phase 2: flip off and re-install. + await OverlayUpdateConfigAsync(scratch.ConfigPath, updateEnabled: false); + var install2 = await dinD.RunOnScratchAsync(scratch, $"{nameof(DropInIsRemovedWhenUpdateFlippedOff)}-2", "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(install2.ExitCode).IsEqualTo(0).Because(install2.Stderr); + + var probeGone = await dinD.ExecAsync(["test", "-e", $"{unitDir}/interfold-backup.service.d/50-chain-update.conf"]); + await Assert.That(probeGone.ExitCode).IsNotEqualTo(0L) + .Because("phase 2 must have removed the stale drop-in when update.enabled flipped to false"); + } + + /// + /// Rewrites the in-DinD config file to set (or unset) update.enabled. Reads the + /// existing config, merges an update block via jq (baked into + /// Dockerfile.ubuntu-dind alongside systemd), and writes it back atomically + /// so a follow-up install-service run sees the flipped flag. + /// + /// + /// jq lives in the image on purpose — before that, this helper installed it + /// on-demand under >/dev/null 2>&1, which raced with + /// SystemdAnalyzeAcceptsRenderedUnits' equivalent apt-get install systemd + /// on /var/lib/dpkg/lock-frontend and produced silent exit 100 flakes in CI + /// whenever TUnit interleaved the two tests. Moving both installs into the image kills + /// the race and makes the helper synchronous / assertion-free. + /// + private async Task OverlayUpdateConfigAsync(string configPath, bool updateEnabled) + { + var enabledLiteral = updateEnabled ? "true" : "false"; + // Merge (+ operator) so any pre-existing update block wins on collision except for + // enabled, which the outer setter forces. Writes to a sibling tempfile then moves + // into place so partial writes don't leave the config half-parsed. + var script = + $"jq '. + {{ update: ((.update // {{}}) + {{ enabled: {enabledLiteral} }}) }}' {configPath} " + + $"> {configPath}.tmp && mv {configPath}.tmp {configPath}"; + var overlay = await dinD.ExecAsync(["sh", "-c", script]); + await Assert.That(overlay.ExitCode).IsEqualTo(0L) + .Because($"overlaying update.enabled={updateEnabled} onto {configPath} failed: {overlay.Stderr}"); + } + + [Test] + public async Task SystemdAnalyzeAcceptsRenderedUnits() + { + // Runs `systemd-analyze verify` against every rendered unit via the bootstrapper's + // install-service phase. The DinD's PID 1 is dockerd-entrypoint, not systemd — that's + // fine, systemd-analyze is a static parser that doesn't need a running systemd to run. + // The `systemd` apt package (which ships systemd-analyze) is baked into + // Dockerfile.ubuntu-dind alongside `jq` — a previous incarnation of this test installed + // it on-demand under `>/dev/null 2>&1` and raced with OverlayUpdateConfigAsync's jq + // install on `/var/lib/dpkg/lock-frontend`, producing silent exit-100 flakes in CI. + var scratch = await dinD.CreateScratchAsync(nameof(SystemdAnalyzeAcceptsRenderedUnits), TestConfigPaths.DefaultConfig); + var unitDir = $"{scratch.Root}/systemd-units"; + await dinD.ExecAsync(["mkdir", "-p", unitDir]); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(SystemdAnalyzeAcceptsRenderedUnits), "install-service", + "--systemd-unit-dir", unitDir, + "--binary-path", "/opt/bootstrapper/interfold-bootstrap"); + await Assert.That(result.ExitCode).IsEqualTo(0).Because($"install-service failed: {result.Stderr}"); + + // The phase itself runs systemd-analyze verify when the binary is on PATH; confirm via + // the captured logs that it did so and accepted every unit. + await Assert.That(result.Stdout + result.Stderr).Contains("verified interfold.service") + .Because("install-service should report a successful verification line for interfold.service"); + await Assert.That(result.Stdout + result.Stderr).Contains("verified interfold-backup.service"); + await Assert.That(result.Stdout + result.Stderr).Contains("verified interfold-backup.timer"); + await Assert.That(result.Stdout + result.Stderr).Contains("verified interfold-update.service") + .Because("install-service should verify the new update service alongside the baseline three"); + await Assert.That(result.Stdout + result.Stderr).Contains("calendar 'daily' parses OK") + .Because("install-service should report systemd-analyze acceptance for the schedule"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestConfigPaths.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestConfigPaths.cs new file mode 100644 index 00000000..03edaf15 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestConfigPaths.cs @@ -0,0 +1,13 @@ +namespace Interfold.Bootstrapper.IntegrationTests; + +public static class TestConfigPaths +{ + public static string DefaultConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.json"); + public static string TrustInstallConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.trust-install.json"); + public static string CassandraConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.cassandra.json"); + public static string BadImageConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.bad-image.json"); + public static string WebHttpOnlyConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.web-http-only.json"); + public static string MdnsGateConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.mdns-gate.json"); + public static string WebHttpsConfig => Path.Combine(AppContext.BaseDirectory, "Fixtures", "interfold.bootstrap.test.web-https.json"); +} + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestServices/DotEnvParser.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestServices/DotEnvParser.cs new file mode 100644 index 00000000..7f20d4d2 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TestServices/DotEnvParser.cs @@ -0,0 +1,28 @@ +using System.Text; + +namespace Interfold.Bootstrapper.IntegrationTests.TestServices; + +/// +/// Cheap-and-cheerful .env parser shared by publish-style integration tests. Lines of +/// the form KEY=VALUE, comments (lines beginning with #) stripped, blank lines +/// ignored. We don't bother handling quoted values because the bootstrapper's emitter doesn't +/// produce any (every value is either a known-safe alphabet password or an absolute path). +/// Previously duplicated verbatim in PublishIntegrationTests and WebHttpsTests. +/// +internal static class DotEnvParser +{ + public static IDictionary ParseEnv(byte[] bytes) + { + var text = Encoding.UTF8.GetString(bytes); + var dict = new Dictionary(StringComparer.Ordinal); + foreach (var rawLine in text.Split('\n', StringSplitOptions.RemoveEmptyEntries)) + { + var line = rawLine.TrimEnd('\r').Trim(); + if (line.Length == 0 || line.StartsWith('#')) continue; + var eq = line.IndexOf('='); + if (eq <= 0) continue; + dict[line[..eq]] = line[(eq + 1)..]; + } + return dict; + } +} diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustDownloadTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustDownloadTests.cs new file mode 100644 index 00000000..fe0bf481 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustDownloadTests.cs @@ -0,0 +1,86 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// End-to-end verification of the trust-distribution flow: bootstrap a full stack inside +/// DinD, then prove the same root CA is reachable over both filesystem (via the +/// bootstrapper-issued certs/ dir) and HTTP (via the API container's +/// /.well-known/interfold-root-ca.* routes). This closes the loop between +/// CertificatePhase (which produces the artefacts), AppHost (which env-wires the +/// container), TrustOptions (which binds the env vars) and TrustController (which serves +/// the bytes) — a regression in any of those four points would fail this single test. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class TrustDownloadTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task WellKnownEndpointServesBootstrapperCert() + { + var (scratch, _) = await dinD.BootstrapAsync(nameof(WellKnownEndpointServesBootstrapperCert), TestConfigPaths.DefaultConfig); + + // On-disk artefacts the bootstrapper just produced — our golden truth for the three + // endpoint responses. + var rootCrtOnDisk = await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/rootCA.crt"); + var fingerprintOnDisk = Encoding.UTF8.GetString( + await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/rootCA.sha256.txt")).Trim(); + + var apiPort = scratch.Ports.ApiHttp; + var baseUrl = $"http://localhost:{apiPort}/.well-known/interfold-root-ca"; + + // --- .pem route: bytes-on-the-wire equal bytes-on-disk --- + var pemResp = await dinD.ExecAsync(["curl", "-fsSL", $"{baseUrl}.pem"]); + await Assert.That(pemResp.ExitCode).IsEqualTo(0) + .Because($"curl {baseUrl}.pem failed: stdout='{pemResp.Stdout}' stderr='{pemResp.Stderr}'"); + var pemBytesFetched = Encoding.UTF8.GetBytes(pemResp.Stdout); + await Assert.That(pemBytesFetched).IsEquivalentTo(rootCrtOnDisk) + .Because("the .pem route must stream rootCA.crt verbatim"); + + // --- .sha256 route: text equals the recorded fingerprint --- + var sha256Resp = await dinD.ExecAsync(["curl", "-fsSL", $"{baseUrl}.sha256"]); + await Assert.That(sha256Resp.ExitCode).IsEqualTo(0) + .Because($"curl {baseUrl}.sha256 failed: stdout='{sha256Resp.Stdout}' stderr='{sha256Resp.Stderr}'"); + await Assert.That(sha256Resp.Stdout.Trim()).IsEqualTo(fingerprintOnDisk) + .Because("the .sha256 route must equal rootCA.sha256.txt verbatim"); + + // --- .crt route: response (DER) must round-trip through the recorded fingerprint --- + // Curl is piped through base64 on the DinD side because the body is binary and + // ExecAsync.Stdout is a string — UTF-8 decoding would mangle the DER bytes otherwise. + var derResp = await dinD.ExecAsync(["sh", "-c", $"curl -fsSL {baseUrl}.crt | base64 -w0"]); + await Assert.That(derResp.ExitCode).IsEqualTo(0) + .Because($"curl {baseUrl}.crt | base64 failed: stdout='{derResp.Stdout}' stderr='{derResp.Stderr}'"); + var derFetched = Convert.FromBase64String(derResp.Stdout.Trim()); + + using var diskCert = X509Certificate2.CreateFromPem(Encoding.UTF8.GetString(rootCrtOnDisk)); + await Assert.That(derFetched).IsEquivalentTo(diskCert.RawData) + .Because("the .crt route must serve the cert's DER (X509Certificate2.RawData) byte-for-byte"); + + // Trust-on-first-use checkpoint: a client that downloads the cert AND the fingerprint + // and runs sha256(.crt) must match the fingerprint, which is the entire point of the + // OOB-verification recipe documented in the trust-distribution section. + var liveFingerprint = string.Join(":", SHA256.HashData(derFetched).Select(b => b.ToString("X2"))); + await Assert.That(liveFingerprint).IsEqualTo(fingerprintOnDisk) + .Because("sha256(downloaded .crt) must equal the published fingerprint"); + + // --- ETag invariant: every route returns the fingerprint-derived ETag --- + var headersResp = await dinD.ExecAsync(["sh", "-c", $"curl -sSI {baseUrl}.crt | tr -d '\\r'"]); + await Assert.That(headersResp.ExitCode).IsEqualTo(0).Because(headersResp.Stderr); + await Assert.That(headersResp.Stdout).Contains($"ETag: \"{fingerprintOnDisk}\"") + .Because("the .crt route must publish the fingerprint as its ETag for cache invalidation on --rotate-certs"); + await Assert.That(headersResp.Stdout).Contains("Cache-Control: public, max-age=60, must-revalidate") + .Because("the .crt route must publish a 60s cacheable Cache-Control directive"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustStoreFalseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustStoreFalseTests.cs new file mode 100644 index 00000000..a3d5e77c --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/TrustStoreFalseTests.cs @@ -0,0 +1,76 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Confirms the trustStoreInstall=false branch of CertificatePhase really does +/// leave the system trust store untouched. The default test fixture config sets the flag to +/// false; this test runs publish with that config and asserts the Debian anchor +/// directory (/usr/local/share/ca-certificates/) has no interfold-root-ca* +/// symlinks afterwards. +/// +/// +/// The complementary positive case (trustStoreInstall=true → root CA appears under +/// /etc/ssl/certs/) is already covered by +/// . +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class TrustStoreFalseTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + // Shares the "ubuntu-trust-install" NotInParallel key with + // UbuntuBootstrapTests.RootCaInstalledInDebianTrustStore (the positive case) so we never run + // a `trustStoreInstall=true` publish concurrently with this `trustStoreInstall=false` + // publish against the same DinD. Without the key the two could race on + // /usr/local/share/ca-certificates and either fight over update-ca-certificates or + // false-flag this test when the install test landed the cert first. + [Test] + [NotInParallel("ubuntu-trust-install")] + public async Task TrustStoreInstallFalseLeavesSystemStoreClean() + { + // Snapshot the trust-store paths BEFORE running publish. The DinD fixture is shared + // (SharedType.PerTestSession) with UbuntuBootstrapTests.RootCaInstalledInDebianTrustStore, + // so a successful sibling run can leave `interfold-root-ca.crt` in the anchor dir and + // its hashed symlinks under /etc/ssl/certs. The real assertion we want here is + // "publish with trustStoreInstall=false didn't ADD anything", so we compare pre vs post + // listings instead of demanding a clean global state. + var anchorBefore = await ListInterfoldEntriesAsync("/usr/local/share/ca-certificates/"); + var sslCertsBefore = await ListInterfoldEntriesAsync("/etc/ssl/certs/"); + + var (scratch, _) = await dinD.PublishAsync(nameof(TrustStoreInstallFalseLeavesSystemStoreClean), TestConfigPaths.DefaultConfig); + + var anchorAfter = await ListInterfoldEntriesAsync("/usr/local/share/ca-certificates/"); + var sslCertsAfter = await ListInterfoldEntriesAsync("/etc/ssl/certs/"); + + var anchorAdded = anchorAfter.Except(anchorBefore).ToArray(); + await Assert.That(anchorAdded).IsEmpty() + .Because($"trustStoreInstall=false should not write new interfold-* entries to the anchor directory; pre={string.Join(',', anchorBefore)} post={string.Join(',', anchorAfter)}"); + + var sslCertsAdded = sslCertsAfter.Except(sslCertsBefore).ToArray(); + await Assert.That(sslCertsAdded).IsEmpty() + .Because($"trustStoreInstall=false should not write new interfold-* entries to /etc/ssl/certs; pre={string.Join(',', sslCertsBefore)} post={string.Join(',', sslCertsAfter)}"); + + // Sanity: the generated cert files themselves should still exist under the scratch dir — + // it's only the SYSTEM trust install that should be skipped. + var leafExists = await dinD.ExecAsync(["test", "-f", $"{scratch.OutputDir}/certs/leaf.crt"]); + await Assert.That(leafExists.ExitCode).IsEqualTo(0L) + .Because("the leaf cert artifact must still be produced even when trust-store install is off"); + } + + private async Task> ListInterfoldEntriesAsync(string directory) + { + var ls = await dinD.ExecAsync( + ["sh", "-c", $"ls {directory} 2>/dev/null | grep -i interfold || true"]); + return ls.Stdout + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .ToHashSet(StringComparer.Ordinal); + } +} + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/UbuntuBootstrapTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UbuntuBootstrapTests.cs new file mode 100644 index 00000000..dd380bf3 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UbuntuBootstrapTests.cs @@ -0,0 +1,248 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Full-coverage scenarios for the bootstrapper running on ubuntu:24.04. All tests share a single +/// instance for the lifetime of the test session via TUnit's +/// SharedType.PerTestSession, so the expensive publish / pre-pull happens once. +/// Each test allocates a private scratch directory under /opt/scratch via +/// , allowing the suite to run in parallel +/// without races on /opt/deploy. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class UbuntuBootstrapTests(UbuntuDinDFixture dinD) +{ + + // Trust-store-install tests use a variant that enables /usr/local/share/ca-certificates/ + // writes. That path is shared inside the DinD container across all parallel tests, so the + // tests that exercise it must also opt in to the NotInParallel guard below. + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task ProducesValidComposeOnFreshBox() + { + var (scratch, _) = await dinD.PublishAsync(nameof(ProducesValidComposeOnFreshBox), TestConfigPaths.DefaultConfig, "--print-phase-status"); + + var composeBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/docker-compose.yaml"); + var compose = Encoding.UTF8.GetString(composeBytes); + + await Assert.That(compose).Contains("msg-db:").Because("compose missing msg-db service"); + await Assert.That(compose).Contains("scylla:").Because("compose missing scylla service"); + await Assert.That(compose).Contains("interfold-api").Because("compose missing interfold-api service"); + await Assert.That(compose).DoesNotContain("${Parameters_").Because("unresolved parameter placeholder leaked into compose"); + + // Admin work moved into the bootstrapper's DatabaseInitPhase, so the compose graph must + // no longer reference the legacy bootstrap-auth init containers, and the only + // *connection string* it ships is the app user's. The msg-db service still gets a + // POSTGRES_USER / POSTGRES_PASSWORD pair, but those are init-only credentials for the + // transient `db_init` cluster owner that DatabaseInitPhase scrambles in-cluster as one + // of its final steps - they're not a connection string anything connects with. + await Assert.That(compose).DoesNotContain("pg-bootstrap-auth") + .Because("legacy pg-bootstrap-auth init container leaked back into compose"); + await Assert.That(compose).DoesNotContain("scylla-bootstrap-auth") + .Because("legacy scylla-bootstrap-auth init container leaked back into compose"); + await Assert.That(compose).DoesNotContain("SCYLLA_ADMIN_PASSWORD") + .Because("scylla admin password must not appear in compose - it lives in internal.secrets"); + await Assert.That(compose).DoesNotContain("POSTGRES_ADMIN_PASSWORD") + .Because("postgres admin password must not appear in compose - it lives in internal.secrets"); + await Assert.That(compose).Contains(PostgresRoles.Init) + .Because("msg-db cluster owner must be the disposable 'db_init' role, not the app user"); + await Assert.That(compose).Contains("POSTGRES_INIT_PASSWORD") + .Because("compose must reference POSTGRES_INIT_PASSWORD for the disposable cluster owner"); + } + + // Each compose-up test now binds a private host-port window inside the shared DinD via + // CreateScratchAsync's port allocator, so concurrent `compose up` calls no longer collide on + // 5000/5432/9042/etc. The previous `ubuntu-compose-up` NotInParallel serialiser is therefore + // gone — the DinD's inner dockerd throughput is the new (much higher) ceiling. + [Test] + public async Task StackComesUpHealthy() + { + var (scratch, _) = await dinD.BootstrapAsync(nameof(StackComesUpHealthy), TestConfigPaths.DefaultConfig); + + var ps = await dinD.ExecAsync( + ["docker", "compose", "-f", $"{scratch.OutputDir}/docker-compose.yaml", "ps", "--format", "json"]); + await Assert.That(ps.ExitCode).IsEqualTo(0L); + await Assert.That(ps.Stdout).Contains("\"State\":\"running\"").Or.Contains("\"Health\":\"healthy\"") + .Because("expected at least one healthy/running service in compose ps output"); + } + + [Test] + public async Task IsIdempotentOnRerun() + { + var scratch = await dinD.CreateScratchAsync(nameof(IsIdempotentOnRerun), TestConfigPaths.DefaultConfig); + + await dinD.RunOnScratchAsync(scratch, $"{nameof(IsIdempotentOnRerun)}-first", "publish", + "--print-phase-status"); + + var firstHash = await ShaOfComposeAsync(scratch); + var firstSecrets = await dinD.CopyOutAsync(scratch.SecretsJsonPath); + + var second = await dinD.RunOnScratchAsync(scratch, $"{nameof(IsIdempotentOnRerun)}-second", "publish", + "--print-phase-status"); + + await Assert.That(second.ExitCode).IsEqualTo(0).Because($"second run failed: {second.Stderr}"); + await Assert.That(second.Stderr).Contains("phase=secrets status=skipped") + .Because("secrets phase should self-skip on a second run"); + await Assert.That(second.Stderr).Contains("phase=certs status=skipped") + .Because("certs phase should self-skip on a second run"); + + var secondHash = await ShaOfComposeAsync(scratch); + await Assert.That(secondHash).IsEqualTo(firstHash) + .Because("compose output should be byte-identical across two non-rotating runs"); + + var secondSecrets = await dinD.CopyOutAsync(scratch.SecretsJsonPath); + await Assert.That(Convert.ToHexString(secondSecrets)).IsEqualTo(Convert.ToHexString(firstSecrets)) + .Because("secrets file must be untouched between bootstraps"); + } + + [Test] + public async Task GeneratedLeafCertHasCorrectSans() + { + var (scratch, _) = await dinD.PublishAsync(nameof(GeneratedLeafCertHasCorrectSans), TestConfigPaths.DefaultConfig); + + var leafBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/leaf.crt"); + var rootBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/rootCA.crt"); + + using var leaf = X509Certificate2.CreateFromPem(Encoding.UTF8.GetString(leafBytes)); + using var root = X509Certificate2.CreateFromPem(Encoding.UTF8.GetString(rootBytes)); + + var sanExt = leaf.Extensions.OfType().FirstOrDefault(); + await Assert.That(sanExt).IsNotNull().Because("leaf cert must include a SubjectAlternativeName extension"); + var sans = sanExt!.EnumerateDnsNames().ToList(); + await Assert.That(sans).Contains("api.test.local"); + await Assert.That(sans).Contains("alt.test.local"); + + await Assert.That(leaf.Issuer).IsEqualTo(root.Subject) + .Because("leaf must be signed by the generated root CA"); + } + + [Test] + public async Task SecretsFileHasRestrictedPermissions() + { + var (scratch, _) = await dinD.PublishAsync(nameof(SecretsFileHasRestrictedPermissions), TestConfigPaths.DefaultConfig); + + var stat = await dinD.ExecAsync(["stat", "-c", "%a %U", scratch.SecretsJsonPath]); + await Assert.That(stat.ExitCode).IsEqualTo(0L); + await Assert.That(stat.Stdout.Trim()).StartsWith("600 ") + .Because("secrets file must be mode 0600"); + await Assert.That(stat.Stdout.Trim()).EndsWith("root") + .Because("secrets file must be owned by root"); + } + + // Rotate-secrets runs db-init -> launch (it has to refresh the in-cluster admin password) so + // it host-binds postgres / scylla / api ports too. Per-test port allocation in + // CreateScratchAsync means each invocation lands on a private port window, so the previous + // NotInParallel("ubuntu-compose-up") key is no longer required for safety. + [Test] + public async Task RotateSecretsRegeneratesPasswordsAndPreservesCerts() + { + var pair = await RunRotateAsync(nameof(RotateSecretsRegeneratesPasswordsAndPreservesCerts), "rotate-secrets"); + + await Assert.That(pair.PostSecrets).IsNotEqualTo(pair.PreSecrets).Because("secrets must rotate"); + await Assert.That(pair.PostLeafSha).IsEqualTo(pair.PreLeafSha).Because("certs must remain unchanged on rotate-secrets"); + } + + // Rotate-certs also runs db-init (defensive against an empty DB volume) -> launch, so like + // rotate-secrets it host-binds postgres / scylla / api ports. The per-test port allocation + // makes that safe to run concurrently with sibling compose-up tests. + [Test] + public async Task RotateCertsRegeneratesCertsAndPreservesSecrets() + { + var pair = await RunRotateAsync(nameof(RotateCertsRegeneratesCertsAndPreservesSecrets), "rotate-certs"); + + await Assert.That(pair.PostSecrets).IsEqualTo(pair.PreSecrets).Because("secrets must remain unchanged on rotate-certs"); + await Assert.That(pair.PostLeafSha).IsNotEqualTo(pair.PreLeafSha).Because("leaf cert must change on rotate-certs"); + } + + /// + /// Common wiring for the two rotate scenarios: publish a scratch deployment, snapshot the + /// on-disk secrets JSON + leaf-cert SHA, run the supplied rotate command, and snapshot both + /// again. Callers pick the polarity of the assertions — which pair must change vs which must + /// be byte-identical — so a future rotate command can plug in with just an extra call site + /// without cloning the fixture-driving preamble. + /// + private async Task RunRotateAsync(string testName, string rotateCommand) + { + var scratch = await dinD.CreateScratchAsync(testName, TestConfigPaths.DefaultConfig); + await dinD.RunOnScratchAsync(scratch, $"{testName}-init", "publish"); + + var preSecrets = await dinD.ReadSecretsJsonAsync(scratch); + var preLeafSha = ShaOf(await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/leaf.crt")); + + var rotate = await dinD.RunOnScratchAsync(scratch, testName, rotateCommand); + await Assert.That(rotate.ExitCode).IsEqualTo(0).Because($"{rotateCommand} failed: {rotate.Stderr}"); + + var postSecrets = await dinD.ReadSecretsJsonAsync(scratch); + var postLeafSha = ShaOf(await dinD.CopyOutAsync($"{scratch.OutputDir}/certs/leaf.crt")); + + return new RotatePair(preSecrets, postSecrets, preLeafSha, postLeafSha); + } + + private sealed record RotatePair(string PreSecrets, string PostSecrets, string PreLeafSha, string PostLeafSha); + + [Test] + public async Task RecoversFromInterruptedPublish() + { + var scratch = await dinD.CreateScratchAsync(nameof(RecoversFromInterruptedPublish), TestConfigPaths.DefaultConfig); + + // First invocation halts immediately after the secrets phase via the hidden --fault-inject hook. + var halted = await dinD.RunOnScratchAsync(scratch, $"{nameof(RecoversFromInterruptedPublish)}-halt", "publish", + "--fault-inject=after-secrets"); + await Assert.That(halted.ExitCode).IsEqualTo(0) + .Because($"fault-inject halt should exit cleanly: {halted.Stderr}"); + + // The secrets file should already exist - confirm it. + var secretsExist = await dinD.ExecAsync(["test", "-f", scratch.SecretsJsonPath]); + await Assert.That(secretsExist.ExitCode).IsEqualTo(0L) + .Because("partial run should have persisted the secrets file"); + + // Re-run from scratch - phases that already ran should skip cleanly. + var resumed = await dinD.RunOnScratchAsync(scratch, $"{nameof(RecoversFromInterruptedPublish)}-resume", "publish", + "--print-phase-status"); + await Assert.That(resumed.ExitCode).IsEqualTo(0) + .Because($"resumed run should complete cleanly: {resumed.Stderr}"); + await Assert.That(resumed.Stderr).Contains("phase=secrets status=skipped"); + + // Compose YAML must be present after the resumed run. + var composeExists = await dinD.ExecAsync(["test", "-f", $"{scratch.OutputDir}/docker-compose.yaml"]); + await Assert.That(composeExists.ExitCode).IsEqualTo(0L); + } + + // Trust-store install writes /usr/local/share/ca-certificates/interfold-root-ca.crt, which is a + // process-global filesystem path shared across all tests in this DinD. Serialise via NotInParallel + // so the file copy + `update-ca-certificates` invocation doesn't race with itself. + [Test] + [NotInParallel("ubuntu-trust-install")] + public async Task RootCaInstalledInDebianTrustStore() + { + var (scratch, _) = await dinD.PublishAsync(nameof(RootCaInstalledInDebianTrustStore), TestConfigPaths.TrustInstallConfig); + + // update-ca-certificates places extracted PEMs under /etc/ssl/certs/ symlinked from the anchor. + var ls = await dinD.ExecAsync( + ["sh", "-c", "ls /etc/ssl/certs/ | grep -i interfold || true"]); + await Assert.That(ls.Stdout.Trim()).IsNotEmpty() + .Because("root CA should have been linked into /etc/ssl/certs after update-ca-certificates"); + } + + private async Task ShaOfComposeAsync(DinDScratch scratch) + { + var bytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/docker-compose.yaml"); + return ShaOf(bytes); + } + + private static string ShaOf(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)); +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/UnsupportedOsTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UnsupportedOsTests.cs new file mode 100644 index 00000000..3a8e6477 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UnsupportedOsTests.cs @@ -0,0 +1,46 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Negative-path coverage: when the bootstrapper runs on a distro family it does not support, +/// it must exit non-zero with a clear "unsupported distro" message and must not have made +/// partial filesystem changes under its output directory. The fixture is a glibc Debian-slim base +/// with a doctored /etc/os-release so the binary actually execs before being rejected. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class UnsupportedOsTests(UnsupportedDistroDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx, teardown: false); + + [Test] + public async Task RefusesToRunOnUnsupportedDistro() + { + var scratch = await dinD.CreateScratchAsync(nameof(RefusesToRunOnUnsupportedDistro), TestConfigPaths.DefaultConfig); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(RefusesToRunOnUnsupportedDistro), "bootstrap"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0L) + .Because("bootstrap must exit non-zero on an unsupported distro"); + + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("Unsupported Linux distribution").Or.Contains("unsupported-distro") + .Because("error message should explicitly identify the unsupported distro"); + + // Filesystem state must be untouched - secrets/certs/compose should not exist. + // wc -l returns the number of lines from `ls`, which is 0 when none of the paths + // exist. Assert on the parsed count, not the ExecResult itself. + var any = await dinD.ExecAsync( + ["sh", "-c", $"ls {scratch.OutputDir}/secrets {scratch.OutputDir}/certs {scratch.OutputDir}/docker-compose.yaml 2>/dev/null | wc -l"]); + await Assert.That(int.Parse(any.Stdout.Trim())).IsEqualTo(0) + .Because("a refused run must not leave partial artifacts under the output dir"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesCassandraModeTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesCassandraModeTests.cs new file mode 100644 index 00000000..516b2912 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesCassandraModeTests.cs @@ -0,0 +1,200 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Cassandra-mode integration tests for the update-images subcommand. Sibling of +/// (which exercises the scylla-mode compose stack). +/// These tests pin the two behaviours unique to databaseMode=cassandra: +/// +/// +/// bootstrap publish stamps pull_policy: never onto the cassandra +/// service in the emitted docker-compose.yaml, so docker compose pull +/// skips the local-only interfold-cassandra:local tag instead of failing. +/// +/// +/// update-images rebuilds interfold-cassandra:local before the pull +/// (so Dockerfile changes take effect via update-images alone), scoped to whether +/// the effective service whitelist includes cassandra. +/// +/// +/// +/// +/// +/// Uses the dedicated so the extra +/// cassandra:5 base image is pre-pulled once per session — the DinD is otherwise +/// network-isolated (the API image is loaded from a bind-mounted tar), so a first-run +/// pull of cassandra:5 inside the test would exceed the fixture's warm-up budget. +/// +/// +/// The health-check side of update-images (post-pull compose up + nodetool status +/// probe) is deliberately not asserted here — cassandra:5 takes ~90s to reach UN in a +/// DinD environment, which would dominate the CI runtime. The scylla-mode sibling suite +/// covers the health-check code path; these tests scope to the cassandra-specific +/// build + pull-skip contract by driving `update-images` with the whitelist narrowed +/// to non-cassandra services (so no cassandra recreate happens) OR by asserting on the +/// pull-phase output before recreate begins. +/// +/// +/// Nested DinD keeps vfs (not containerd overlay) so Cassandra Dockerfile builds work on +/// GHA. UpdateImagesSurvivesStaleLocalCassandraImageAfterRebuild still force-rebuilds +/// the local tag without recreate and asserts update-images completes digest snapshotting +/// without calling docker compose images — the production failure mode on Desktop. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class UpdateImagesCassandraModeTests(UbuntuCassandraDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task PublishInCassandraModeStampsPullPolicyOnComposeYaml() + { + // The narrowest test in the file: run just the `publish` subcommand (no compose up), + // then read the emitted docker-compose.yaml and assert the cassandra service block + // carries `pull_policy: never`. Fast — no health check, no image pull, no compose up. + // Locks down PublishPhase.StampCassandraPullPolicyNever's happy-path contract in a + // real end-to-end publish, not just the unit test's synthetic YAML. + var (scratch, composeFile) = await dinD.PublishAsync(nameof(PublishInCassandraModeStampsPullPolicyOnComposeYaml), TestConfigPaths.CassandraConfig, "--skip-prereqs"); + + // grep -A 1 pulls the line after each `${CASSANDRA_IMAGE}` match; we assert the + // policy shows up in that immediate neighbourhood. Portable across compose-yaml + // layout tweaks (indent, blank lines, extra volume keys under the service) as long + // as pull_policy: never lands next to the image key. + var grep = await dinD.ExecAsync(["sh", "-c", + $"grep -A 1 -F '${{CASSANDRA_IMAGE}}' {scratch.OutputDir}/docker-compose.yaml"]); + await Assert.That(grep.ExitCode).IsEqualTo(0L) + .Because($"the cassandra service must contain the ${{CASSANDRA_IMAGE}} anchor: {grep.Stderr}"); + await Assert.That(grep.Stdout).Contains("pull_policy: never") + .Because("PublishPhase.StampCassandraPullPolicyNever must land pull_policy: never right after the image key"); + } + + [Test] + public async Task UpdateInCassandraModeSkipsCassandraOnPullAndRebuildsLocalImage() + { + // End-to-end pull-side contract: after a full bootstrap in cassandra mode, an + // update-images run must (1) rebuild interfold-cassandra:local before pulling + // (log line "cassandra mode: rebuilding") and (2) let `docker compose pull` + // skip the cassandra service natively via pull_policy: never rather than + // failing on the non-registry-backed tag. + // + // We scope the update to msg-db (a registry-backed TimescaleDB image the DinD + // has cached) + cassandra via --service. The msg-db entry gives compose pull + // something registry-backed to work against so it produces observable output; + // the cassandra entry drives ShouldRebuildCassandra's "cassandra in scope" + // branch. interfold-api is deliberately excluded — interfold-api:test is a + // locally-loaded tag with no registry manifest, so including it would cause + // `docker compose pull` to exit non-zero with "pull access denied" before the + // cassandra pull-skip evidence could land. See the class remarks on + // UpdateImagesPhaseTests for the general "locally-built API image" caveat. + var (scratch, _) = await dinD.BootstrapAsync(nameof(UpdateInCassandraModeSkipsCassandraOnPullAndRebuildsLocalImage), TestConfigPaths.CassandraConfig); + + // Sanity: the local image was built by bootstrap. `docker images -q` returns + // the image ID (non-empty) if the tag exists locally. + var imgBefore = await dinD.ExecAsync(["sh", "-c", "docker images -q interfold-cassandra:local"]); + await Assert.That(imgBefore.Stdout.Trim().Length).IsGreaterThan(0) + .Because("bootstrap should have built interfold-cassandra:local via CassandraImagePhase.EnsureBuiltAsync"); + + var update = await dinD.RunOnScratchAsync(scratch, nameof(UpdateInCassandraModeSkipsCassandraOnPullAndRebuildsLocalImage), "update-images", + "--service", "msg-db", "--service", "cassandra", + "--skip-pre-update-backup"); + await Assert.That(update.ExitCode).IsEqualTo(0).Because($"update-images failed: {update.Stderr}"); + + var combined = update.Stdout + update.Stderr; + + // Rebuild log line: proves ShouldRebuildCassandra + EnsureBuiltAsync fired. + await Assert.That(combined).Contains("cassandra mode: rebuilding interfold-cassandra:local") + .Because("update-images in cassandra mode with cassandra in scope must rebuild the local image"); + + // Pull-skip evidence: docker compose reports the cassandra service as "Skipped" + // (compose behaviour when pull_policy: never — see pkg/compose/pull.go on + // docker/compose main). Absent this, the pull would fail with a manifest-not-found + // error on interfold-cassandra:local, so seeing exit 0 above is already necessary + // proof; the string match here pins the branch as well. + await Assert.That(combined).Contains("Skipped") + .Because("`docker compose pull` should skip the cassandra service due to pull_policy: never"); + + // Image is still present after the update (rebuild is idempotent at the Docker + // layer-cache level, so the ID may or may not have changed depending on cache + // hits; either way the tag must resolve). + var imgAfter = await dinD.ExecAsync(["sh", "-c", "docker images -q interfold-cassandra:local"]); + await Assert.That(imgAfter.Stdout.Trim().Length).IsGreaterThan(0) + .Because("interfold-cassandra:local must still exist after the update-images rebuild"); + } + + [Test] + public async Task UpdateInCassandraModeWithMsgDbWhitelistSkipsRebuild() + { + // The scoping guard: narrowing an update with `--service msg-db` in cassandra mode + // must NOT trigger an unrelated Cassandra rebuild. Locks down ShouldRebuildCassandra's + // "whitelist excludes cassandra" branch in a real invocation. + var (scratch, _) = await dinD.BootstrapAsync(nameof(UpdateInCassandraModeWithMsgDbWhitelistSkipsRebuild), TestConfigPaths.CassandraConfig); + + var update = await dinD.RunOnScratchAsync(scratch, nameof(UpdateInCassandraModeWithMsgDbWhitelistSkipsRebuild), "update-images", + "--service", "msg-db", + "--skip-pre-update-backup"); + await Assert.That(update.ExitCode).IsEqualTo(0).Because($"update-images failed: {update.Stderr}"); + + var combined = update.Stdout + update.Stderr; + await Assert.That(combined).DoesNotContain("cassandra mode: rebuilding") + .Because("--service msg-db must not trigger a cassandra rebuild"); + } + + [Test] + public async Task UpdateImagesSurvivesStaleLocalCassandraImageAfterRebuild() + { + // Regression for compose#14014: rebuilding interfold-cassandra:local without + // recreating the container is the Desktop failure mode where `docker compose + // images` exits 1 ("No such image"). UpdateImagesPhase must snapshot digests via + // container inspect + tag resolve instead — otherwise update-images dies at + // "snapshotting pre-pull image digests". + // + // Nested DinD stays on vfs (overlay-on-overlay cannot build Cassandra under + // containerd), so compose images may still succeed here; the hard contract is + // that update-images completes without depending on that command. + // + // Scope matches UpdateInCassandraModeSkipsCassandraOnPullAndRebuildsLocalImage: + // msg-db (registry-backed) + cassandra; exclude interfold-api (local-only tag). + const string testName = nameof(UpdateImagesSurvivesStaleLocalCassandraImageAfterRebuild); + var (scratch, composeFile) = await dinD.BootstrapAsync(testName, TestConfigPaths.CassandraConfig); + + var stamp = Guid.NewGuid().ToString("N"); + var rebuild = await dinD.ExecAsync(["sh", "-c", + $"printf 'FROM interfold-cassandra:local\\nLABEL interfold.digest_probe={stamp}\\n' | docker build -t interfold-cassandra:local -"]); + await Assert.That(rebuild.ExitCode).IsEqualTo(0L) + .Because($"forced local tag rebuild must succeed: {rebuild.Stderr}"); + + // Soft probe: when the engine drops the dangling index, document the surface. + var imagesProbe = await dinD.ExecAsync(["sh", "-c", + $"docker compose -f {composeFile} images >/tmp/compose-images.out 2>/tmp/compose-images.err; printf '%s' $?"]); + var imagesErr = (await dinD.ExecAsync(["cat", "/tmp/compose-images.err"])).Stdout; + if (imagesErr.Contains("No such image", StringComparison.Ordinal)) + { + await Assert.That(imagesProbe.Stdout.Trim()).IsEqualTo("1") + .Because("compose images must exit 1 when the dangling local image is gone"); + } + + var update = await dinD.RunOnScratchAsync(scratch, testName, "update-images", + "--service", "msg-db", "--service", "cassandra", + "--skip-pre-update-backup"); + await Assert.That(update.ExitCode).IsEqualTo(0) + .Because($"update-images must survive stale local cassandra image: {update.Stderr}"); + + var combined = update.Stdout + update.Stderr; + await Assert.That(combined).DoesNotContain("docker compose images exited") + .Because("digest snapshot must not depend on docker compose images"); + await Assert.That(combined).DoesNotContain("No such image") + .Because("digest snapshot must not surface the dangling-image compose failure"); + await Assert.That(combined).Contains("resolved digests") + .Because("pre-pull digest snapshot must complete against the running stack"); + } +} + + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesPhaseTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesPhaseTests.cs new file mode 100644 index 00000000..3cbbb57b --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/UpdateImagesPhaseTests.cs @@ -0,0 +1,163 @@ +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the update-images subcommand against a real running compose +/// stack inside the shared Ubuntu DinD fixture. Each test runs a full bootstrap first, +/// then exercises the update pipeline (pull + optional recreate + health check + retention) +/// and asserts on the on-disk backup archives, compose state, and exit codes. +/// +/// +/// +/// These tests exercise the happy path only: the DinD fixture pre-loads +/// interfold-api:test and scylladb/scylla:2026.1 as fixed image IDs, so a +/// docker compose pull in a network-isolated DinD against the registry-backed +/// services (msg-db = TimescaleDB, scylla = ScyllaDB) is a no-op at the +/// digest level. That's exactly the "no changes" branch we want to pin — the fail +/// branches (image bump, health check failure, auto-restore recovery) require registry +/// mutation and are intentionally deferred to a follow-up PR that stands up an in-DinD +/// registry container. +/// +/// +/// The "no changes" case is still worth wiring end-to-end because it exercises the full +/// argv/parse pipeline: config load, compose discovery, backup invocation, pull, digest +/// diff, and the early-exit branch — all against a real running stack, not a mock. +/// +/// +/// Every update-images invocation here scopes to --service msg-db --service +/// scylla because interfold-api:test is a locally-loaded tag with no +/// matching registry manifest; a bare docker compose pull would exit non-zero +/// with "pull access denied for interfold-api" before the phase's digest diff +/// even ran. Skipping the api service on the pull side loses no coverage of the +/// pipeline mechanics under test (the "no-op", pre-update backup, and +/// --skip-pre-update-backup branches are all orthogonal to which services get +/// pulled). The general "operator uses a locally-built API image" case is a separate +/// product concern — see the pull-skip discussion in +/// for the pattern +/// that would extend it to the api service too. +/// +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class UpdateImagesPhaseTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task UpdateWithNoChangedImagesIsNoOp() + { + // Full bootstrap first: brings up postgres + scylla + api against the fixture's + // pre-loaded images. Then run `update-images` — the pull is a no-op (nothing in the + // fixture points at a mutable registry tag), so the digest diff must be empty and + // the phase short-circuits without touching the running containers. + var (scratch, _) = await dinD.BootstrapAsync(nameof(UpdateWithNoChangedImagesIsNoOp), TestConfigPaths.DefaultConfig); + + // Capture the api container ID before the update so we can assert it wasn't + // recreated (recreate is what makes update-images observable when digests change; + // a no-op update must NOT touch the running container). + var beforeId = await dinD.ExecAsync(["sh", "-c", + $"docker compose -f {scratch.OutputDir}/docker-compose.yaml ps -q interfold-api"]); + await Assert.That(beforeId.ExitCode).IsEqualTo(0L); + var apiIdBefore = beforeId.Stdout.Trim(); + await Assert.That(apiIdBefore.Length).IsGreaterThan(0) + .Because("bootstrap should have produced a running interfold-api container"); + + var update = await dinD.RunOnScratchAsync(scratch, nameof(UpdateWithNoChangedImagesIsNoOp), "update-images", + "--service", "msg-db", "--service", "scylla"); + await Assert.That(update.ExitCode).IsEqualTo(0).Because($"update-images failed: {update.Stderr}"); + + var combined = update.Stdout + update.Stderr; + await Assert.That(combined).Contains("no-op") + .Because("with no image changes the phase must log a no-op branch and skip recreate"); + + // Container ID unchanged — proves nothing was recreated. If update-images had + // called `up -d` on a service whose digest hadn't changed, compose would still + // leave the container ID stable, but the fact that we see the log line above + // AND the ID is stable pins the branch as well as the observable outcome. + var afterId = await dinD.ExecAsync(["sh", "-c", + $"docker compose -f {scratch.OutputDir}/docker-compose.yaml ps -q interfold-api"]); + await Assert.That(afterId.Stdout.Trim()).IsEqualTo(apiIdBefore) + .Because("no-op update must not recreate the api container"); + } + + [Test] + public async Task UpdatePerformsPreUpdateBackup() + { + // The backup step is always taken unless --skip-pre-update-backup is passed. + // Even on the no-op path we should see fresh archives in {outputDir}/backups/ + // because the backup fires BEFORE the digest diff (so a pull that turns out to + // be a no-op still leaves a recovery snapshot on disk). + var (scratch, _) = await dinD.BootstrapAsync(nameof(UpdatePerformsPreUpdateBackup), TestConfigPaths.DefaultConfig); + + // Baseline: no backups exist yet. + var pgBefore = await dinD.CountFilesAsync(scratch, "backups/postgres/*.dump"); + await Assert.That(pgBefore).IsEqualTo(0) + .Because("baseline: bootstrap should not have taken any backups yet"); + + var update = await dinD.RunOnScratchAsync(scratch, nameof(UpdatePerformsPreUpdateBackup), "update-images", + "--service", "msg-db", "--service", "scylla"); + await Assert.That(update.ExitCode).IsEqualTo(0).Because($"update-images failed: {update.Stderr}"); + + // Both components must have received a fresh archive from the pre-update backup step. + var pgAfter = await dinD.CountFilesAsync(scratch, "backups/postgres/*.dump"); + await Assert.That(pgAfter).IsEqualTo(1) + .Because("update-images must take a pre-update postgres backup"); + + var scyllaAfter = await dinD.CountFilesAsync(scratch, "backups/scylla/*.tar.gz"); + await Assert.That(scyllaAfter).IsEqualTo(1) + .Because("update-images must take a pre-update scylla backup"); + } + + [Test] + public async Task UpdateWithSkipPreUpdateBackupDoesNotWriteArchives() + { + // Escape-hatch flag: --skip-pre-update-backup MUST prevent any backup being taken + // (the operator has just taken a manual one and doesn't want the duplicate). + // Pinned so a future refactor that flips the default behaviour gets caught. + var (scratch, _) = await dinD.BootstrapAsync(nameof(UpdateWithSkipPreUpdateBackupDoesNotWriteArchives), TestConfigPaths.DefaultConfig); + + var update = await dinD.RunOnScratchAsync(scratch, nameof(UpdateWithSkipPreUpdateBackupDoesNotWriteArchives), "update-images", + "--service", "msg-db", "--service", "scylla", + "--skip-pre-update-backup"); + await Assert.That(update.ExitCode).IsEqualTo(0).Because($"update-images failed: {update.Stderr}"); + + var pgAfter = await dinD.CountFilesAsync(scratch, "backups/postgres/*.dump"); + await Assert.That(pgAfter).IsEqualTo(0) + .Because("--skip-pre-update-backup must suppress the backup step"); + + var scyllaAfter = await dinD.CountFilesAsync(scratch, "backups/scylla/*.tar.gz"); + await Assert.That(scyllaAfter).IsEqualTo(0) + .Because("--skip-pre-update-backup must suppress both components' archives"); + + await Assert.That(update.Stdout + update.Stderr).Contains("skip-pre-update-backup") + .Because("the phase must warn loudly when the escape hatch is used"); + } + + [Test] + public async Task UpdateWithoutComposeFailsClearly() + { + // Running update-images against a bare scratch (no compose, no secrets, no + // running stack) must fail with an actionable error rather than dying inside + // docker compose. This is the "operator ran update-images before bootstrap" + // misuse case — the phase runs several prerequisite checks and any of them + // gives us the "guide to bootstrap" contract. + var scratch = await dinD.CreateScratchAsync(nameof(UpdateWithoutComposeFailsClearly), TestConfigPaths.DefaultConfig); + + var result = await dinD.RunOnScratchAsync(scratch, nameof(UpdateWithoutComposeFailsClearly), "update-images"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("update-images against a bare scratch must fail"); + + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("bootstrap") + .Because("error must guide the operator to run `bootstrap` first"); + } +} + + + diff --git a/tests/integration/Interfold.Bootstrapper.IntegrationTests/WebHttpsTests.cs b/tests/integration/Interfold.Bootstrapper.IntegrationTests/WebHttpsTests.cs new file mode 100644 index 00000000..d2bedc13 --- /dev/null +++ b/tests/integration/Interfold.Bootstrapper.IntegrationTests/WebHttpsTests.cs @@ -0,0 +1,232 @@ +using System.Text; +using Interfold.Bootstrapper.IntegrationTests.Attributes; +using Interfold.Bootstrapper.IntegrationTests.Fixtures; +using Interfold.Bootstrapper.IntegrationTests.TestServices; + +namespace Interfold.Bootstrapper.IntegrationTests; + +/// +/// Integration tests for the opt-in deployment.webHttps path. When set, the bootstrapper +/// must emit a compose file where octocon-web bind-mounts the issued cert directory and +/// an nginx envsubst template, exposes :443, and uses the env vars that the template references. +/// The actual TLS handshake is asserted by spinning up just the web service inside DinD and +/// curling its own healthcheck endpoint over HTTPS via docker exec. +/// +[RequiresDocker] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[Explicit] +public class WebHttpsTests(UbuntuDinDFixture dinD) +{ + + [After(Test)] + public Task DumpOnFailure(TestContext ctx) => DinDHookHelpers.DumpOnFailureAsync(dinD, ctx); + + [Test] + public async Task PublishWithWebHttpsEmitsCertAndTemplateBindMounts() + { + // Pure publish-time contract: the bootstrapper should fill in the two new bind-mount + // sources for octocon-web (cert dir + nginx envsubst template) and stamp the NGINX_* + // env values that drive the template. No `docker compose up` here, so the test runs in + // single-digit seconds against the shared DinD. + var (scratch, _) = await dinD.PublishAsync(nameof(PublishWithWebHttpsEmitsCertAndTemplateBindMounts), TestConfigPaths.WebHttpsConfig); + + var envBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/.env"); + var env = DotEnvParser.ParseEnv(envBytes); + + // Every bind-mount-source line in the post-processed .env should resolve to an absolute + // path. We don't assert the exact key names because Aspire derives them from the service + // resource graph (currently `OCTOCON_WEB_BINDMOUNT_0`, `OCTOCON_WEB_BINDMOUNT_1`, ...); + // we match the stable `OCTOCON_WEB_BINDMOUNT` prefix so a future Aspire upgrade that + // tweaks the suffix shape (e.g. back to `OCTOCON_WEB_BINDMOUNTS__0`) still passes. + var webBindMounts = env + .Where(kv => kv.Key.StartsWith("OCTOCON_WEB_BINDMOUNT", StringComparison.OrdinalIgnoreCase)) + .ToList(); + await Assert.That(webBindMounts.Count).IsGreaterThanOrEqualTo(2) + .Because("expected at least two octocon-web bind mounts (certs + nginx template) " + + $"in the .env; got keys: {string.Join(", ", env.Keys)}"); + + foreach (var (key, value) in webBindMounts) + { + await Assert.That(value).IsNotEmpty() + .Because($"{key} should not be blank — PublishPhase failed to substitute the bind-mount source"); + await Assert.That(value).StartsWith("/") + .Because($"{key}={value} must be an absolute host path"); + } + + // Spot-check the compose file itself: the cert mount target and the nginx template mount + // target must both be present, and the service must publish container port 443 for HTTPS. + var composeBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/docker-compose.yaml"); + var compose = Encoding.UTF8.GetString(composeBytes); + + await Assert.That(compose).Contains("octocon-web") + .Because("octocon-web service must be present when deployment.webHttps=true"); + await Assert.That(compose).Contains("/certs") + .Because("octocon-web must bind-mount the certs directory at /certs"); + await Assert.That(compose).Contains("/etc/nginx/templates/default.conf.template") + .Because("octocon-web must bind-mount the nginx envsubst template"); + await Assert.That(compose).Contains("NGINX_SERVER_NAME") + .Because("octocon-web must receive the NGINX_SERVER_NAME env var (consumed by the template)"); + await Assert.That(compose).Contains("NGINX_SSL_CERT_FILE") + .Because("octocon-web must receive the NGINX_SSL_CERT_FILE env var (consumed by the template)"); + await Assert.That(compose).Contains("NGINX_SSL_KEY_FILE") + .Because("octocon-web must receive the NGINX_SSL_KEY_FILE env var (consumed by the template)"); + await Assert.That(compose).Contains("NGINX_HTTPS_PORT_SUFFIX") + .Because("octocon-web must receive the NGINX_HTTPS_PORT_SUFFIX env var so the 301 " + + "redirect from :80 points at the operator's webHttps host port, not the " + + "implicit :443 default"); + await Assert.That(compose).Contains($"\"{scratch.Ports.WebHttps}:443\"") + .Because($"compose must publish the allocated webHttps host port ({scratch.Ports.WebHttps}) onto the container's :443 listener"); + + // Negative invariant: the default (HTTP-only) variant of the web service emits a curl + // healthcheck against the upstream image's :8080 listener. When webHttps=true we expect + // the HTTPS variant of the healthcheck instead — the HTTP-only sentinel + // "http://localhost:8080/" must not appear in the generated compose. + await Assert.That(compose).DoesNotContain("http://localhost:8080/") + .Because("the HTTP healthcheck variant must not be emitted when webHttps=true"); + await Assert.That(compose).Contains("https://localhost:443/") + .Because("the HTTPS healthcheck must point at the in-container 443 listener"); + } + + [Test] + public async Task WebContainerServesHttpsAfterComposeUp() + { + // End-to-end smoke: publish the compose, start ONLY the web container (no API / db + // dependencies needed because the static site is self-contained), wait for nginx's own + // healthcheck to flip green, then assert the rendered conf.d entry mentions the cert + // paths we passed in. The healthcheck itself probes https://localhost:443/ with `-kf` + // from inside the container, so a healthy state implies a successful TLS handshake + // against the bootstrapper-issued leaf cert. + var (scratch, _) = await dinD.PublishAsync(nameof(WebContainerServesHttpsAfterComposeUp), TestConfigPaths.WebHttpsConfig); + + // Bring up only the web service. --no-deps because the compose file networks reference + // the api network but the web container itself has no service-level depends_on; compose + // will create the network on demand. Pulling the upstream image happens here on first run. + var up = await dinD.ExecAsync( + [ + "sh", "-c", + $"docker compose -f {scratch.OutputDir}/docker-compose.yaml up -d --no-deps octocon-web 2>&1" + ]); + await Assert.That(up.ExitCode).IsEqualTo(0) + .Because($"`docker compose up -d octocon-web` failed: {up.Stdout}{up.Stderr}"); + + // Compose uses the parent directory name as the project name and renames containers + // to `--`. Find the actual container id rather than guessing. + var containerProbe = await dinD.ExecAsync( + [ + "sh", "-c", + $"docker compose -f {scratch.OutputDir}/docker-compose.yaml ps -q octocon-web" + ]); + var containerId = containerProbe.Stdout.Trim(); + await Assert.That(containerId).IsNotEmpty() + .Because("expected `docker compose ps -q octocon-web` to print the running container id"); + + // Poll the container's healthcheck (the compose file defines an HTTPS-aware curl probe + // that exits 0 only when nginx finishes its TLS handshake). 60s budget covers cold pulls + // of the upstream image on a fresh DinD plus the 10s start_period the healthcheck declares. + var deadline = DateTime.UtcNow + TimeSpan.FromMinutes(2); + var lastHealth = string.Empty; + while (DateTime.UtcNow < deadline) + { + var inspect = await dinD.ExecAsync( + [ + "sh", "-c", + $"docker inspect --format '{{{{.State.Health.Status}}}}' {containerId}" + ]); + lastHealth = inspect.Stdout.Trim(); + if (string.Equals(lastHealth, "healthy", StringComparison.Ordinal)) break; + if (string.Equals(lastHealth, "unhealthy", StringComparison.Ordinal)) + { + // Surface the container logs immediately so the failure message is actionable. + var logs = await dinD.ExecAsync(["docker", "logs", "--tail", "100", containerId]); + throw new InvalidOperationException( + $"octocon-web reported unhealthy:\n{logs.Stdout}\n{logs.Stderr}"); + } + await Task.Delay(TimeSpan.FromSeconds(2)); + } + await Assert.That(lastHealth).IsEqualTo("healthy") + .Because($"octocon-web never became healthy (last status: '{lastHealth}'); see compose logs in artifacts"); + + // Sanity check that envsubst actually rendered our template and substituted the + // NGINX_* variables. We're not just trusting the healthcheck because the upstream image + // could in theory ship its own /etc/nginx/conf.d/default.conf that masks the template. + var renderedConf = await dinD.ExecAsync( + ["docker", "exec", containerId, "cat", "/etc/nginx/conf.d/default.conf"]); + await Assert.That(renderedConf.ExitCode).IsEqualTo(0) + .Because($"could not read rendered nginx config: {renderedConf.Stderr}"); + await Assert.That(renderedConf.Stdout).Contains("/certs/leaf.crt") + .Because("envsubst should have substituted NGINX_SSL_CERT_FILE into ssl_certificate"); + await Assert.That(renderedConf.Stdout).Contains("/certs/leaf.key") + .Because("envsubst should have substituted NGINX_SSL_KEY_FILE into ssl_certificate_key"); + await Assert.That(renderedConf.Stdout).Contains("web.test.local") + .Because("envsubst should have substituted NGINX_SERVER_NAME from the fixture's hosts[0]"); + + // The 301 redirect must point at the operator's webHttps host port (the port the + // bootstrapper mapped onto the container's :443 listener). Without the port suffix the + // browser would resolve `https://$host/...` to port 443, which the bootstrapper does NOT + // bind unless the operator picked 443 in interfold.bootstrap.json — sending the user to + // a dead port. + var expectedRedirect = scratch.Ports.WebHttps == 443 + ? "return 301 https://$host$request_uri;" + : $"return 301 https://$host:{scratch.Ports.WebHttps}$request_uri;"; + await Assert.That(renderedConf.Stdout).Contains(expectedRedirect) + .Because($"the rendered redirect must embed the operator's webHttps host port " + + $"({scratch.Ports.WebHttps}); got conf body:\n{renderedConf.Stdout}"); + + // Live probe: hit the :80 listener and confirm nginx replies with a 301 whose Location + // header references the operator-mapped HTTPS port (not nginx's implicit :443). + var redirectProbe = await dinD.ExecAsync( + [ + "docker", "exec", containerId, + "curl", "-s", "-o", "/dev/null", "-D", "-", "http://localhost:80/" + ]); + await Assert.That(redirectProbe.ExitCode).IsEqualTo(0) + .Because($"HTTP probe against octocon-web :80 failed: stdout='{redirectProbe.Stdout}' stderr='{redirectProbe.Stderr}'"); + var expectedLocationFragment = scratch.Ports.WebHttps == 443 + ? "Location: https://localhost/" + : $"Location: https://localhost:{scratch.Ports.WebHttps}/"; + await Assert.That(redirectProbe.Stdout).Contains(expectedLocationFragment) + .Because($"the 301 Location header must reference the operator-mapped webHttps port " + + $"({scratch.Ports.WebHttps}); got response headers:\n{redirectProbe.Stdout}"); + + // Final TLS probe — `-k` because the leaf is signed by the bootstrapper's private root + // CA which the curl invocation inside the container doesn't trust by default. Asserting + // a 200 confirms the static asset path also wired up (the template's `try_files` SPA + // fallback should serve index.html for `/`). + var probe = await dinD.ExecAsync( + [ + "docker", "exec", containerId, + "curl", "-skf", "-o", "/dev/null", "-w", "%{http_code}", "https://localhost:443/" + ]); + await Assert.That(probe.ExitCode).IsEqualTo(0) + .Because($"HTTPS probe against octocon-web failed: stdout='{probe.Stdout}' stderr='{probe.Stderr}'"); + await Assert.That(probe.Stdout.Trim()).IsEqualTo("200") + .Because($"expected 200 from https://localhost:443/, got '{probe.Stdout.Trim()}'"); + } + + [Test] + public async Task PublishWithWebHttpsLeavesEnvFilledWithAbsolutePaths() + { + // Companion to PublishWithWebHttpsEmitsCertAndTemplateBindMounts but focuses on the + // operator-facing failure mode: a `KEY=` line with a blank RHS in the .env will cause + // `docker compose up` to error out with "invalid mount source". This test confirms the + // ApplyReplacementsToEnvFile pass actually populated every web-related entry. + var (scratch, _) = await dinD.PublishAsync(nameof(PublishWithWebHttpsLeavesEnvFilledWithAbsolutePaths), TestConfigPaths.WebHttpsConfig); + + var envBytes = await dinD.CopyOutAsync($"{scratch.OutputDir}/.env"); + var rawText = Encoding.UTF8.GetString(envBytes); + + // No `OCTOCON_WEB_*=` line should land in the .env with a blank RHS. This guards against + // a future Aspire upgrade that adds a new placeholder we forgot to teach + // BuildEnvReplacements about. + foreach (var line in rawText.Split('\n', StringSplitOptions.RemoveEmptyEntries)) + { + var trimmed = line.TrimEnd('\r').Trim(); + if (!trimmed.StartsWith("OCTOCON_WEB", StringComparison.OrdinalIgnoreCase)) continue; + await Assert.That(trimmed.EndsWith('=')).IsFalse() + .Because($"octocon-web env line has a blank value (compose up would fail): {trimmed}"); + } + } +} + + + diff --git a/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendRequestsControllerTests.cs b/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendRequestsControllerTests.cs new file mode 100644 index 00000000..cd14da10 --- /dev/null +++ b/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendRequestsControllerTests.cs @@ -0,0 +1,141 @@ +using System.Net; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; + +namespace Interfold.Friendships.IntegrationTests.Controllers; + +[Category("Friendships")] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class FriendRequestsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + private static string UniqueId(string prefix) => TestIds.NewSystemId(prefix, maxLen: 24); + + [Test] + public async Task FriendRequests_FullLifecycle_Succeeds() + { + using var client = fixture.Factory.CreateClient(); + var userA = UniqueId("fr-life-a"); + var userB = UniqueId("fr-life-b"); + + await EnsureUserExistsAsync(client, userA); + await EnsureUserExistsAsync(client, userB); + + // 1. Initial State: No requests + using var initRes = await client.SendAuthedGetAsync("/api/friend-requests", userA); + var initEnvelope = await initRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(initEnvelope.Data.Incoming.Count).IsEqualTo(0); + await Assert.That(initEnvelope.Data.Outgoing.Count).IsEqualTo(0); + + // 2. Send Friend Request: A -> B + using var sendRes = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{userB}", + new object(), + userA); + await Assert.That(sendRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 3. Verify outgoing on A + using var aRes = await client.SendAuthedGetAsync("/api/friend-requests", userA); + var aEnvelope = await aRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(aEnvelope.Data.Outgoing.Count).IsEqualTo(1); + await Assert.That(aEnvelope.Data.Outgoing[0].System.Id.Value).IsEqualTo(userB); + + // 4. Verify incoming on B + using var bRes = await client.SendAuthedGetAsync("/api/friend-requests", userB); + var bEnvelope = await bRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(bEnvelope.Data.Incoming.Count).IsEqualTo(1); + await Assert.That(bEnvelope.Data.Incoming[0].System.Id.Value).IsEqualTo(userA); + + // 5. Cancel Friend Request: A cancels + using var cancelRes = await client.SendAuthedDeleteAsync($"/api/friend-requests/{userB}", userA); + await Assert.That(cancelRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 6. Verify empty on A + using var aAfterCancelRes = await client.SendAuthedGetAsync("/api/friend-requests", userA); + var aAfterCancelEnvelope = await aAfterCancelRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(aAfterCancelEnvelope.Data.Outgoing.Count).IsEqualTo(0); + + // 7. Send Friend Request again: A -> B + using var sendRes2 = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{userB}", + new object(), + userA); + await Assert.That(sendRes2.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 8. Reject Friend Request: B rejects A's request + using var rejectRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friend-requests/{userA}/reject", + new object(), + userB); + await Assert.That(rejectRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 9. Verify empty on B + using var bAfterRejectRes = await client.SendAuthedGetAsync("/api/friend-requests", userB); + var bAfterRejectEnvelope = await bAfterRejectRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(bAfterRejectEnvelope.Data.Incoming.Count).IsEqualTo(0); + + // 10. Send Friend Request again: A -> B + using var sendRes3 = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{userB}", + new object(), + userA); + await Assert.That(sendRes3.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 11. Accept Friend Request: B accepts A's request + using var acceptRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friend-requests/{userA}/accept", + new object(), + userB); + await Assert.That(acceptRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 12. Verify they are friends + using var friendshipsRes = await client.SendAuthedGetAsync("/api/friends", userA); + var friendshipsEnvelope = await friendshipsRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(friendshipsEnvelope.Data.Count).IsEqualTo(1); + await Assert.That(friendshipsEnvelope.Data[0].Friend.Id.Value).IsEqualTo(userB); + } + + [Test] + public async Task SelfActions_ReturnBadRequest() + { + using var client = fixture.Factory.CreateClient(); + var user = UniqueId("fr-self"); + await EnsureUserExistsAsync(client, user); + + // Send to self + using var sendRes = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{user}", + new object(), + user); + await Assert.That(sendRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var sendError = await sendRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(sendError.Code).IsEqualTo(ErrorCodes.CannotSendSelf); + + // Cancel to self + using var cancelRes = await client.SendAuthedDeleteAsync($"/api/friend-requests/{user}", user); + await Assert.That(cancelRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var cancelError = await cancelRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(cancelError.Code).IsEqualTo(ErrorCodes.CannotCancelSelf); + + // Accept from self + using var acceptRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friend-requests/{user}/accept", + new object(), + user); + await Assert.That(acceptRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var acceptError = await acceptRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(acceptError.Code).IsEqualTo(ErrorCodes.CannotAcceptSelf); + + // Reject from self + using var rejectRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friend-requests/{user}/reject", + new object(), + user); + await Assert.That(rejectRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var rejectError = await rejectRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(rejectError.Code).IsEqualTo(ErrorCodes.CannotRejectSelf); + } +} \ No newline at end of file diff --git a/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendsControllerTests.cs b/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendsControllerTests.cs new file mode 100644 index 00000000..ada2b934 --- /dev/null +++ b/tests/integration/Interfold.Friendships.IntegrationTests/Controllers/FriendsControllerTests.cs @@ -0,0 +1,119 @@ +using System.Net; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Friendships.IntegrationTests.Controllers; + +[Category("Friendships")] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class FriendsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + private static string UniqueId(string prefix) => TestIds.NewSystemId(prefix, maxLen: 24); + + [Test] + public async Task Friendship_FullLifecycle_Succeeds() + { + using var client = fixture.Factory.CreateClient(); + var userA = UniqueId("fr-life-a"); + var userB = UniqueId("fr-life-b"); + + await EnsureUserExistsAsync(client, userA); + await EnsureUserExistsAsync(client, userB); + + // 1. Initially they are not friends + using var showInitRes = await client.SendAuthedGetAsync($"/api/friends/{userB}", userA); + await Assert.That(showInitRes.StatusCode).IsEqualTo(HttpStatusCode.NotFound); + + // 2. Establish friendship + await SendFriendRequestAndAcceptAsync(client, userA, userB); + + // 3. List friends on A + using var listRes = await client.SendAuthedGetAsync("/api/friends", userA); + var listEnvelope = await listRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(listEnvelope.Data.Count).IsEqualTo(1); + await Assert.That(listEnvelope.Data[0].Friend.Id.Value).IsEqualTo(userB); + await Assert.That(listEnvelope.Data[0].Friendship.Level).IsEqualTo(FriendshipLevel.Friend); + + // 4. Show friendship details + using var showRes = await client.SendAuthedGetAsync($"/api/friends/{userB}", userA); + var showEnvelope = await showRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(showEnvelope.Data.Friend.Id.Value).IsEqualTo(userB); + await Assert.That(showEnvelope.Data.Friendship.Level).IsEqualTo(FriendshipLevel.Friend); + + // 5. Upgrade to Trust + using var trustRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friends/{userB}/trust", + new object(), + userA); + await Assert.That(trustRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 6. Verify upgraded status + using var showTrustedRes = await client.SendAuthedGetAsync($"/api/friends/{userB}", userA); + var showTrustedEnvelope = await showTrustedRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(showTrustedEnvelope.Data.Friendship.Level).IsEqualTo(FriendshipLevel.TrustedFriend); + + // 7. Downgrade from Trust (Untrust) + using var untrustRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friends/{userB}/untrust", + new object(), + userA); + await Assert.That(untrustRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 8. Verify downgraded status + using var showUntrustedRes = await client.SendAuthedGetAsync($"/api/friends/{userB}", userA); + var showUntrustedEnvelope = await showUntrustedRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(showUntrustedEnvelope.Data.Friendship.Level).IsEqualTo(FriendshipLevel.Friend); + + // 9. Delete Friendship + using var deleteRes = await client.SendAuthedDeleteAsync($"/api/friends/{userB}", userA); + await Assert.That(deleteRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + // 10. Verify no longer friends + using var listAfterDeleteRes = await client.SendAuthedGetAsync("/api/friends", userA); + var listAfterDeleteEnvelope = await listAfterDeleteRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(listAfterDeleteEnvelope.Data.Count).IsEqualTo(0); + } + + [Test] + public async Task SelfActions_ReturnBadRequest() + { + using var client = fixture.Factory.CreateClient(); + var user = UniqueId("fr-self"); + await EnsureUserExistsAsync(client, user); + + // Show self + using var showRes = await client.SendAuthedGetAsync($"/api/friends/{user}", user); + await Assert.That(showRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var showError = await showRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(showError.Code).IsEqualTo(ErrorCodes.CannotViewOwnFriendship); + + // Delete self + using var deleteRes = await client.SendAuthedDeleteAsync($"/api/friends/{user}", user); + await Assert.That(deleteRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var deleteError = await deleteRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(deleteError.Code).IsEqualTo(ErrorCodes.CannotDeleteOwnFriendship); + + // Trust self + using var trustRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friends/{user}/trust", + new object(), + user); + await Assert.That(trustRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var trustError = await trustRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(trustError.Code).IsEqualTo(ErrorCodes.CannotTrustSelf); + + // Untrust self + using var untrustRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friends/{user}/untrust", + new object(), + user); + await Assert.That(untrustRes.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + var untrustError = await untrustRes.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(untrustError.Code).IsEqualTo(ErrorCodes.CannotUntrustSelf); + } +} \ No newline at end of file diff --git a/tests/integration/Interfold.Friendships.IntegrationTests/Friendships/SendFriendRequestPrefixTests.cs b/tests/integration/Interfold.Friendships.IntegrationTests/Friendships/SendFriendRequestPrefixTests.cs new file mode 100644 index 00000000..acbafdb4 --- /dev/null +++ b/tests/integration/Interfold.Friendships.IntegrationTests/Friendships/SendFriendRequestPrefixTests.cs @@ -0,0 +1,194 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; + +namespace Interfold.Friendships.IntegrationTests.Friendships; + +/// +/// End-to-end pins on the dispatch +/// matrix, exercised through the /api/friend-requests/{id} route so the whole +/// stack (controller → command handler → friendship repo → registry) is in play. +/// +/// +/// The tests run under all three fixtures via TUnit's [ClassDataSource] +/// parameterisation. Cases split into two families: +/// +/// +/// +/// +/// +/// Backend-uniform — dispatch matrix contracts that must hold regardless of +/// which persistence backend is behind the API. The surviving shapes are bare id, +/// id:-prefixed, and username:unknown (all go through +/// route binding). The pre-merge +/// discord: and unknown-prefix shapes now fail +/// FriendLookup.TryParse and surface as a 400 at ASP.NET route binding — +/// pinned by below. +/// +/// +/// +/// +/// Backend-specific — username:known success requires the Scylla +/// users_by_username reverse index; InMemory has no such index so the same +/// route returns NoUser. That divergence is intentional (documented in the +/// InMemory friendship-repo comment) and pinned by the two +/// branches below. +/// +/// +/// +/// +[Category("Friendships")] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class SendFriendRequestPrefixTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + private enum RecipientBackend { InMemory, Regional } + + private RecipientBackend Backend => fixture is InMemoryWebFactoryFixture + ? RecipientBackend.InMemory + : RecipientBackend.Regional; + + // ---------------- Backend-uniform: bare id + id: prefix ---------------- + + [Test] + public async Task SendFriendRequest_BareRecipientId_Returns204() + { + using var client = fixture.Factory.CreateClient(); + var sender = UniqueId("s7-send-bare-a"); + var recipient = UniqueId("s7-send-bare-b"); + await EnsureUserExistsAsync(client, sender); + await EnsureUserExistsAsync(client, recipient); + + var status = await SendFriendRequestAsync(client, sender, recipient); + await Assert.That(status).IsEqualTo(HttpStatusCode.NoContent) + .Because("Bare recipient id is the baseline shape — must resolve to the recipient and land a 204."); + } + + [Test] + public async Task SendFriendRequest_ExplicitIdPrefix_Returns204() + { + using var client = fixture.Factory.CreateClient(); + var sender = UniqueId("s7-send-id-a"); + var recipient = UniqueId("s7-send-id-b"); + await EnsureUserExistsAsync(client, sender); + await EnsureUserExistsAsync(client, recipient); + + var status = await SendFriendRequestAsync(client, sender, $"id:{recipient}"); + await Assert.That(status).IsEqualTo(HttpStatusCode.NoContent) + .Because("'id:{recipient}' must dispatch identically to the bare form — both are Kind.Id under FriendLookup and target user_registry.user_id."); + } + + // ---------------- Backend-uniform: strict rejection -------------------- + + /// + /// Every shape that + /// rejects surfaces as a 400 from ASP.NET Core's IParsable route-binding pipeline + /// before the controller action runs. Covers the pre-merge discord: and + /// unknown-prefix shapes (previously 422 friend_request:no_user via a + /// registry miss) and the region-scoped shape that used to strip-and-look-up under + /// LookupHandle.Kind.Region. Backend-uniform because the rejection happens + /// before any persistence layer is touched. + /// + [Test] + [Arguments("xxx:definitely-not-a-real-user")] + [Arguments("discord:1234567890")] + [Arguments("nam:abcdefg")] + [Arguments(":emptyprefix")] + [Arguments("username:")] + public async Task SendFriendRequest_UnparseableShape_Returns400(string recipientHandle) + { + using var client = fixture.Factory.CreateClient(); + var sender = UniqueId("s7-send-badshape-a"); + await EnsureUserExistsAsync(client, sender); + + var (status, _) = await SendFriendRequestWithEntityRefAsync(client, sender, recipientHandle); + + await Assert.That(status).IsEqualTo(HttpStatusCode.BadRequest) + .Because($"'{recipientHandle}' is not a valid FriendLookup shape and must be rejected by IParsable route binding with a 400 — never reach the friendship repo, so no NoUser (422) fallback."); + } + + [Test] + public async Task SendFriendRequest_UsernamePrefix_UnknownUsername_ReturnsNoUser() + { + using var client = fixture.Factory.CreateClient(); + var sender = UniqueId("s7-send-unamemiss-a"); + await EnsureUserExistsAsync(client, sender); + + var (status, entityRef) = await SendFriendRequestWithEntityRefAsync( + client, sender, "username:definitely-not-a-real-username-12345"); + + using (Assert.Multiple()) + { + await Assert.That(status).IsEqualTo(HttpStatusCode.UnprocessableEntity); + await Assert.That(entityRef).IsEqualTo("friend_request:no_user") + .Because("Username lookup that misses must NOT fall back to treating the literal 'username:...' as a system id."); + } + } + + // ---------------- Backend-specific: username success ------------------- + + [Test] + public async Task SendFriendRequest_UsernamePrefix_KnownUsername_DispatchMatchesBackendContract() + { + using var client = fixture.Factory.CreateClient(); + var sender = UniqueId("s7-send-uname-a"); + var recipientPrincipal = UniqueId("s7-send-uname-b"); + // Deterministic per-run username so parallel test runs don't collide on the + // users_by_username partition key. + var recipientUsername = $"unameuser{DateTime.UtcNow.Ticks % 1_000_000}"; + await EnsureUserExistsAsync(client, sender); + await EnsureUserExistsAsync(client, recipientPrincipal, recipientUsername); + + var (status, entityRef) = await SendFriendRequestWithEntityRefAsync( + client, sender, $"username:{recipientUsername}"); + + if (Backend == RecipientBackend.InMemory) + { + // InMemory has no users_by_username reverse index; the correct answer is + // "no such user" rather than fabricating a SystemId('username:...'). + using (Assert.Multiple()) + { + await Assert.That(status).IsEqualTo(HttpStatusCode.UnprocessableEntity); + await Assert.That(entityRef).IsEqualTo("friend_request:no_user") + .Because("InMemory has no username reverse index — the Kind.Username branch returns null which surfaces as NoUser. This is documented in the InMemoryFriendshipRepository dispatch comment."); + } + } + else + { + await Assert.That(status).IsEqualTo(HttpStatusCode.NoContent) + .Because("Scylla/Cassandra populate users_by_username on /api/settings/username, so the username-prefixed friend request must resolve and land 204."); + } + } + + // ---------------- Helpers ---------------------------------------------- + + private static string UniqueId(string prefix) => TestIds.NewSystemId(prefix, maxLen: 24); + + private static async Task SendFriendRequestAsync( + HttpClient client, string sender, string recipientHandle) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{recipientHandle}", + new object(), + sender); + return res.StatusCode; + } + + private static async Task<(HttpStatusCode Status, string? EntityRef)> SendFriendRequestWithEntityRefAsync( + HttpClient client, string sender, string recipientHandle) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{recipientHandle}", + new object(), + sender); + + if (res.IsSuccessStatusCode) + { + return (res.StatusCode, null); + } + + var error = await res.ReadErrorAsync(res.StatusCode); + return (res.StatusCode, error.EntityRef); + } +} diff --git a/tests/integration/Interfold.Friendships.IntegrationTests/Interfold.Friendships.IntegrationTests.csproj b/tests/integration/Interfold.Friendships.IntegrationTests/Interfold.Friendships.IntegrationTests.csproj new file mode 100644 index 00000000..ff225ac6 --- /dev/null +++ b/tests/integration/Interfold.Friendships.IntegrationTests/Interfold.Friendships.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Fronting.IntegrationTests/Controllers/FrontingControllerTests.cs b/tests/integration/Interfold.Fronting.IntegrationTests/Controllers/FrontingControllerTests.cs new file mode 100644 index 00000000..a5790056 --- /dev/null +++ b/tests/integration/Interfold.Fronting.IntegrationTests/Controllers/FrontingControllerTests.cs @@ -0,0 +1,215 @@ +using System.Net; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Fronting.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class FrontingControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task FrontStart_LegacyIdField_Returns201() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-front-legacy-id"; + var alterId = await CreateAlterAsync(client, principal, "LegacyFrontAlter"); + + // Regression: front/start body used to be shape `{ id }`. `FrontStartRequest.Id` + // carries `[JsonPropertyName("id")]` so the typed record serialises to the same wire + // shape, which is what this test guards against. + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/start", + new FrontStartRequest(alterId), + principal); + + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.Created); + } + + [Test] + public async Task Api_FrontHistoryBetween_IncludesEndedFronts() + { + using var client = fixture.Factory.CreateClient(); + + // The API's fronting handlers stamp time_start/time_end from the injected TimeProvider, + // which the test fixture replaces with a FakeTimeProvider pinned at its epoch (see + // InterfoldWebApplicationFactory). Real wall-clock anchors would fall outside that + // frame and fronts_by_time WHERE time_start >= ? AND time_start <= ? would return + // zero rows. + var now = fixture.Factory.TimeProvider.GetUtcNow(); + var startAnchor = now.AddMinutes(-1).ToUnixTimeSeconds(); + + // Per-invocation nonce so the shared PostgresIdempotencyStore under the bench + // can't hand a peer variant's cached FrontId back (see docs/test-bench-audit.md + // and the identical pattern in ReplayParityTests / SeedVisibilityQuartetAsync). + // Without it the InMemory / Scylla / Cassandra factory variants race on + // "phase3-fronting-history" and later reads see a FrontId from another + // variant's backend that the current variant's CQL never wrote. + var principal = $"phase3-fronting-history-{Guid.NewGuid().ToString("N")[..8]}"; + var alter = await CreateAlterAsync(client, principal, "test alter"); + + using var started = await SendFrontStartAsync(client, alterId: alter, comment: "phase3-history", principal); + var startedEnv = await started.ReadEnvelopeAsync(HttpStatusCode.Created); + var startedFrontId = startedEnv.Data.FrontId; + + using var ended = await SendFrontEndAsync(client, alterId: alter, principal); + await Assert.That(ended.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + var endAnchor = now.AddMinutes(1).ToUnixTimeSeconds(); + using var betweenResponse = await client.SendAuthedGetAsync($"/api/systems/me/front/between?start={startAnchor}&end={endAnchor}", principal); + var betweenEnv = await betweenResponse.ReadEnvelopeAsync>(HttpStatusCode.OK); + + await Assert.That(betweenEnv.Data.Count).IsGreaterThan(0); + + var row = betweenEnv.Data[0]; + using (Assert.Multiple()) + { + await Assert.That(row.Id).IsEqualTo(startedFrontId); + await Assert.That(row.Comment).IsEqualTo("phase3-history"); + await Assert.That(row.TimeEnd).IsNotNull(); + } + } + + // ===== Set-fronting semantics ===== + // + // The set endpoint must leave the target alter as the sole fronter, regardless of the + // starting state. The previous implementation rejected with `fronting:already_fronting` + // when the target was already in the active set (broke promote-among-many) and only + // published a single FrontingSetEvent (clients never saw the per-alter end events for + // the alters that were silently dropped from the active set). + // + // The four cases below cover every starting state. They assert the post-set active + // list because that is the observable contract the client cares about; per-alter + // FrontingEndedEvent emission is exercised by the same code path (the handler will not + // reach the post-end event publish without ending the alter first). + + [Test] + public async Task FrontSet_FromNoActiveFronters_StartsTargetAsSoleFronter() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("front-set-empty"); + var target = await CreateAlterAsync(client, principal, "TargetAlter"); + + using var setResult = await SendFrontSetAsync(client, target, principal, comment: "from-empty"); + await Assert.That(setResult.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + var activeAlters = await ListActiveFrontingAlterIdsAsync(client, principal); + await Assert.That(activeAlters).IsEquivalentTo(new[] { target }); + } + + [Test] + public async Task FrontSet_WhenTargetIsAlreadySoleFronter_PreservesFrontIdIdempotently() + { + // Regression: set(X) when X is already the only fronter must not reject with + // `fronting:already_fronting` and must not create a new front row (front_id is + // the stable client-facing handle into the active row; rewriting it on every + // idempotent set call would break clients that pinned that id). + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("front-set-target-only"); + var target = await CreateAlterAsync(client, principal, "TargetAlter"); + + using var initialStart = await SendFrontStartAsync(client, alterId: target, comment: "initial", principal); + var initialEnv = await initialStart.ReadEnvelopeAsync(HttpStatusCode.Created); + var originalFrontId = initialEnv.Data.FrontId; + + using var setResult = await SendFrontSetAsync(client, target, principal, comment: "idempotent"); + await Assert.That(setResult.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + var activeFrontIdsByAlter = await ListActiveFrontIdsByAlterAsync(client, principal); + using (Assert.Multiple()) + { + await Assert.That(activeFrontIdsByAlter.Keys).IsEquivalentTo(new[] { target }); + await Assert.That(activeFrontIdsByAlter[target]) + .IsEqualTo(originalFrontId) + .Because("set against an already-sole fronter must preserve the existing front_id."); + } + } + + [Test] + public async Task FrontSet_WhenOtherAltersAreFronting_EndsThemAndStartsTarget() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("front-set-others-only"); + var other = await CreateAlterAsync(client, principal, "OtherAlter"); + var target = await CreateAlterAsync(client, principal, "TargetAlter"); + + using var otherStart = await SendFrontStartAsync(client, alterId: other, comment: "to-be-ended", principal); + await Assert.That(otherStart.StatusCode).IsEqualTo(HttpStatusCode.Created); + + using var setResult = await SendFrontSetAsync(client, target, principal); + await Assert.That(setResult.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + var activeAlters = await ListActiveFrontingAlterIdsAsync(client, principal); + await Assert.That(activeAlters) + .IsEquivalentTo(new[] { target }) + .Because("the other alter must be ended so the target is the sole fronter."); + } + + [Test] + public async Task FrontSet_WhenTargetAndOthersAreFronting_PreservesTargetAndEndsOthers() + { + // The crucial promote-among-many case. Pre-fix this rejected with + // `fronting:already_fronting`; post-fix it must end the others and keep the + // target's existing front row intact (preserving front_id and start_time). + using var client = TestClient.NoRedirect(fixture); + + var principal = TestIds.NewSystemId("front-set-mixed"); + var target = await CreateAlterAsync(client, principal, "TargetAlter"); + var other = await CreateAlterAsync(client, principal, "OtherAlter"); + + using var targetStart = await SendFrontStartAsync(client, alterId: target, comment: "stays", principal); + var targetEnv = await targetStart.ReadEnvelopeAsync(HttpStatusCode.Created); + var targetOriginalFrontId = targetEnv.Data.FrontId; + + using var otherStart = await SendFrontStartAsync(client, alterId: other, comment: "ends", principal); + await Assert.That(otherStart.StatusCode).IsEqualTo(HttpStatusCode.Created); + + using var setResult = await SendFrontSetAsync(client, target, principal); + await Assert.That(setResult.StatusCode) + .IsEqualTo(HttpStatusCode.NoContent) + .Because("set must succeed when the target is already in a multi-fronter active set; the old `already_fronting` rejection broke this case."); + + var activeFrontIdsByAlter = await ListActiveFrontIdsByAlterAsync(client, principal); + using (Assert.Multiple()) + { + await Assert.That(activeFrontIdsByAlter.Keys) + .IsEquivalentTo(new[] { target }) + .Because("only the target should remain fronting after set."); + await Assert.That(activeFrontIdsByAlter[target]) + .IsEqualTo(targetOriginalFrontId) + .Because("the target's existing front_id must be preserved; set should not end-and-restart the target."); + } + } + + private static async Task> ListActiveFrontingAlterIdsAsync(HttpClient client, string principal) + { + var byAlter = await ListActiveFrontIdsByAlterAsync(client, principal); + return byAlter.Keys.OrderBy(id => id.Value).ToArray(); + } + + private static async Task> ListActiveFrontIdsByAlterAsync(HttpClient client, string principal) + { + // Self-view: when the viewer is the system owner the response includes every active + // front regardless of visibility - the only filter ListActiveGuardedAsync applies is + // for cross-system viewers (friends/trusted/public). We authenticate as `principal` + // and query `/api/systems/{principal}/fronting`. + using var res = await client.SendAuthedGetAsync($"/api/systems/{principal}/fronting", principal); + + var envelope = await res.ReadEnvelopeAsync>(HttpStatusCode.OK); + + // Active fronts come back shaped { alter: { id, ... }, front: { id, alter_id, ... }, primary }. + // We pull alter_id from `front` rather than `alter.Id` because the latter is the + // hydrated alter read model and this stays deliberately explicit about which id + // the fronting row records. + return envelope.Data.ToDictionary(e => e.Front.AlterId, e => e.Front.Id); + } +} + diff --git a/tests/integration/Interfold.Fronting.IntegrationTests/Interfold.Fronting.IntegrationTests.csproj b/tests/integration/Interfold.Fronting.IntegrationTests/Interfold.Fronting.IntegrationTests.csproj new file mode 100644 index 00000000..c73fb948 --- /dev/null +++ b/tests/integration/Interfold.Fronting.IntegrationTests/Interfold.Fronting.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Fronting.IntegrationTests/Services/Scylla/ScyllaFrontingRepositoryNullTimeStartTests.cs b/tests/integration/Interfold.Fronting.IntegrationTests/Services/Scylla/ScyllaFrontingRepositoryNullTimeStartTests.cs new file mode 100644 index 00000000..883affb6 --- /dev/null +++ b/tests/integration/Interfold.Fronting.IntegrationTests/Services/Scylla/ScyllaFrontingRepositoryNullTimeStartTests.cs @@ -0,0 +1,79 @@ +using Cassandra; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Fronting.IntegrationTests.Services.Scylla; + +/// +/// Locks the fix for the SP-import "today-date" bug: when +/// current_fronts.time_start is somehow stored as NULL (partial-batch failure, +/// manual fixup, schema drift), the next must +/// refuse the close rather than silently stamp today's date into +/// fronts_by_time as the front's time_start. +/// +/// Scylla-only because the corruption (a stored row with time_start = NULL) can't be +/// produced through the public API; the test inserts the row directly via the cluster session +/// and then drives the production against it. Every +/// identifier in this file is fresh per-test — no real account data lands in the repo. +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class ScyllaFrontingRepositoryNullTimeStartTests(ScyllaWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task EndAsync_NullTimeStartInCurrentFronts_ReturnsFalseAndDoesNotStampToday() + { + var factory = fixture.Factory; + using var client = factory.CreateClient(); + + // Mint a unique principal — this becomes the system id throughout the test. The + // synthetic prefix makes leaked rows easy to spot if a future regression drops + // the cleanup. CreateAlterAsync also ensures the system + users row exist. + var rawSystemId = TestIds.NewSystemId("sys-null-ts"); + var systemId = new SystemId(rawSystemId); + var typedAlterId = await CreateAlterAsync(client, rawSystemId, "synthetic-alter"); + var alterId = typedAlterId.Value; + + var frontingRepo = factory.Services.GetRequiredService(); + + // NormalizeSystemId returns the raw string so it can be bound directly into CQL — the + // DataStax driver has no serializer for the SystemId wrapper. + var (session, keyspace, normalizedSystemId) = await ScyllaDirectHarness.ResolveAsync(fixture, systemId); + + var frontGuid = Guid.NewGuid(); + var insertedAt = DateTimeOffset.UtcNow; + + // Synthesise the corrupt row directly. time_start is deliberately omitted from the + // column list so Scylla stores it as NULL — this is the exact shape ScyllaFrontingRepository + // can't produce on the happy path, but which would silently break EndAsync without + // the null-time_start guard. + await session.ExecuteAsync(new SimpleStatement( + $"INSERT INTO {keyspace}.current_fronts (user_id, alter_id, id, comment, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)", + normalizedSystemId, + alterId, + frontGuid, + "synthetic-corrupt-row", + insertedAt, + insertedAt)); + + var endedAt = DateTimeOffset.UtcNow; + var endResult = await frontingRepo.EndAsync(systemId, typedAlterId, endedAt); + + // Pull every fronts_by_time row for this synthetic user. With the fix in place + // GetCurrentFrontRowAsync returns null on the null time_start and EndAsync bails + // out before touching fronts_by_time at all, so the result set must be empty. + var historyRows = (await session.ExecuteAsync(new SimpleStatement( + $"SELECT time_start FROM {keyspace}.fronts_by_time WHERE user_id = ?", + normalizedSystemId))).ToList(); + + using (Assert.Multiple()) + { + await Assert.That(endResult).IsFalse() + .Because("EndAsync must report the close as not-applied when current_fronts.time_start is null, instead of silently stamping today."); + await Assert.That(historyRows.Count).IsEqualTo(0) + .Because("fronts_by_time must not gain a synthetic-today row when EndAsync sees a corrupt current_fronts entry."); + } + } +} diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/Interfold.Infrastructure.IntegrationTests.csproj b/tests/integration/Interfold.Infrastructure.IntegrationTests/Interfold.Infrastructure.IntegrationTests.csproj new file mode 100644 index 00000000..17c85459 --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/Interfold.Infrastructure.IntegrationTests.csproj @@ -0,0 +1,43 @@ + + + + net10.0 + false + true + enable + enable + + true + + + + + + + + + + + + + + + + + AppHost + + + + + diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/InProcessEventBusReconnectTests.cs b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/InProcessEventBusReconnectTests.cs new file mode 100644 index 00000000..ecbfab16 --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/InProcessEventBusReconnectTests.cs @@ -0,0 +1,88 @@ +using Interfold.Infrastructure.Coordination; +using Interfold.IntegrationTests.Shared; + +namespace Interfold.Infrastructure.IntegrationTests.Services; + +public sealed class InProcessEventBusReconnectTests : BaseEndpointTest +{ + [Test] + public async Task Publish_DoesNotThrow_AfterSubscriberDisconnectAndReconnect() + { + using var bus = new InProcessEventBus(); + + using var firstSubscriptionCts = new CancellationTokenSource(); + await using var firstEnumerator = bus + .SubscribeAsync(firstSubscriptionCts.Token) + .GetAsyncEnumerator(firstSubscriptionCts.Token); + + await bus.PublishAsync(new TestEvent("first")); + var firstReceived = await MoveNextWithTimeoutAsync(firstEnumerator, TimeSpan.FromSeconds(2)); + await Assert.That(firstReceived).IsTrue(); + + firstSubscriptionCts.Cancel(); + await firstEnumerator.DisposeAsync(); + + using var secondSubscriptionCts = new CancellationTokenSource(); + await using var secondEnumerator = bus + .SubscribeAsync(secondSubscriptionCts.Token) + .GetAsyncEnumerator(secondSubscriptionCts.Token); + + await bus.PublishAsync(new TestEvent("second")); + var secondReceived = await MoveNextWithTimeoutAsync(secondEnumerator, TimeSpan.FromSeconds(2)); + using (Assert.Multiple()) + { + await Assert.That(secondReceived).IsTrue(); + await Assert.That(secondEnumerator.Current.Value).IsEqualTo("second"); + } + } + + [Test] + public async Task Publish_RemainsHealthy_AcrossRepeatedReconnectChurn() + { + using var bus = new InProcessEventBus(); + + for (var i = 0; i < 25; i++) + { + using var subscriptionCts = new CancellationTokenSource(); + await using var enumerator = bus + .SubscribeAsync(subscriptionCts.Token) + .GetAsyncEnumerator(subscriptionCts.Token); + + await bus.PublishAsync(new TestEvent($"cycle-{i}")); + var received = await MoveNextWithTimeoutAsync(enumerator, TimeSpan.FromSeconds(2)); + await Assert.That(received).IsTrue(); + + subscriptionCts.Cancel(); + await enumerator.DisposeAsync(); + } + + using var finalCts = new CancellationTokenSource(); + await using var finalEnumerator = bus + .SubscribeAsync(finalCts.Token) + .GetAsyncEnumerator(finalCts.Token); + + await bus.PublishAsync(new TestEvent("final"), finalCts.Token); + var finalReceived = await MoveNextWithTimeoutAsync(finalEnumerator, TimeSpan.FromSeconds(2)); + using (Assert.Multiple()) + { + await Assert.That(finalReceived).IsTrue(); + await Assert.That(finalEnumerator.Current.Value).IsEqualTo("final"); + } + } + + private static async Task MoveNextWithTimeoutAsync(IAsyncEnumerator enumerator, TimeSpan timeout) + { + using var timeoutCts = new CancellationTokenSource(timeout); + var moveNextTask = enumerator.MoveNextAsync().AsTask(); + var completedTask = await Task.WhenAny(moveNextTask, Task.Delay(Timeout.InfiniteTimeSpan, timeoutCts.Token)); + + if (!ReferenceEquals(completedTask, moveNextTask)) + { + return false; + } + + return await moveNextTask; + } + + private sealed record TestEvent(string Value); +} \ No newline at end of file diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Migrations/MigrationLedgerTests.cs b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Migrations/MigrationLedgerTests.cs new file mode 100644 index 00000000..18afe87d --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Migrations/MigrationLedgerTests.cs @@ -0,0 +1,366 @@ +using Cassandra; +using Interfold.Infrastructure.Postgres; +using Interfold.Infrastructure.Scylla; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Npgsql; + +namespace Interfold.Infrastructure.IntegrationTests.Services.Migrations; + +/// +/// Exercises the migration ledger added to and +/// . Verifies that the baseline session-bootstrap pass +/// recorded one row per applied migration, re-running the migrator is a no-op, and that +/// post-deploy content drift fails fast on the next start. +/// +/// +/// Uses as the entry fixture so the +/// toggle flips on for the session and the shared +/// brings up both Postgres and a single-node Scylla; we read +/// connection details off rather than going +/// through the API factory. The tampering tests UPDATE the ledger row in place and restore +/// it inside a try/finally so the shared fixture's state is unchanged on exit. +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class MigrationLedgerTests(ScyllaWebFactoryFixture fixture) : BaseEndpointTest +{ + private SharedDbFixture SharedDb => fixture.Aspire; + + // --- Postgres --- + + [Test] + public async Task Postgres_Ledger_HasOneRowPerEmbeddedMigration() + { + var migrationCount = CountEmbeddedResources( + typeof(PostgresMigrationService).Assembly, + "Interfold.Infrastructure.Postgres.Migrations.", + ".sql"); + + await using var conn = await OpenAdminPostgresConnectionAsync(); + + await using var cmd = new NpgsqlCommand( + "SELECT count(*) FROM internal.schema_migrations", conn); + var count = Convert.ToInt64(await cmd.ExecuteScalarAsync()); + + await Assert.That(count).IsEqualTo((long)migrationCount) + .Because($"Expected one ledger row per embedded migration ({migrationCount})."); + } + + [Test] + [DependsOn(nameof(Postgres_Ledger_HasOneRowPerEmbeddedMigration))] + public async Task Postgres_RerunningMigrations_IsANoOp() + { + var before = await SnapshotPostgresLedgerAsync(); + + await PostgresMigrationService.MigrateAsync( + BuildPersistenceConfig(), + BuildPostgresSecretsStore(), + NullLoggerFactory.Instance.CreateLogger(), + CancellationToken.None); + + var after = await SnapshotPostgresLedgerAsync(); + + await Assert.That(after.Count).IsEqualTo(before.Count) + .Because("Re-running migrations must not insert new ledger rows."); + + foreach (var (version, ts) in before) + { + await Assert.That(after.TryGetValue(version, out var afterTs)).IsTrue() + .Because($"Migration '{version}' disappeared from the ledger after re-run."); + await Assert.That(afterTs).IsEqualTo(ts) + .Because($"Migration '{version}' applied_at changed across re-run."); + } + } + + [Test] + [DependsOn(nameof(Postgres_RerunningMigrations_IsANoOp))] + public async Task Postgres_ChecksumDriftThrows_AndPreservesLedger() + { + const string targetVersion = "000_create_secrets_table.sql"; + + await using var conn = await OpenAdminPostgresConnectionAsync(); + + var originalChecksum = await ReadChecksumAsync(conn, targetVersion); + await Assert.That(originalChecksum).IsNotNull() + .Because($"Ledger row for '{targetVersion}' was missing — baseline assertion needs to pass first."); + + const string fakeChecksum = "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF"; + await UpdateChecksumAsync(conn, targetVersion, fakeChecksum); + + InvalidOperationException? thrown = null; + try + { + await PostgresMigrationService.MigrateAsync( + BuildPersistenceConfig(), + BuildPostgresSecretsStore(), + NullLoggerFactory.Instance.CreateLogger(), + CancellationToken.None); + } + catch (InvalidOperationException ex) + { + thrown = ex; + } + finally + { + await UpdateChecksumAsync(conn, targetVersion, originalChecksum!); + } + + await Assert.That(thrown).IsNotNull() + .Because("PostgresMigrationService should fail fast when a recorded checksum no longer matches the file."); + await Assert.That(thrown!.Message).Contains("checksum"); + await Assert.That(thrown.Message).Contains(targetVersion); + } + + // --- Scylla --- + + [Test] + public async Task Scylla_Ledger_HasOneRowPerKeyspaceForTemplatedMigrations() + { + await Assert.That(SharedDb.ScyllaPort).IsNotNull() + .Because("Scylla must be provisioned by the shared fixture for ledger tests."); + + using var session = await OpenScyllaSessionAsync(); + + var rows = await session.ExecuteAsync(new SimpleStatement( + "SELECT scope, version FROM global.schema_migrations")); + + var byVersion = rows + .GroupBy(r => r.GetValue("version")) + .ToDictionary(g => g.Key, g => g.Select(r => r.GetValue("scope")).ToHashSet()); + + // SharedDbFixture provisions the single-node Scylla with IsSingleScyllaInstance=true, + // so the templated migrations are applied to exactly one regional keyspace ("nam"). + await Assert.That(byVersion.TryGetValue("001_create_interfold_keyspaces.cql", out var keyspaceScopes)).IsTrue() + .Because("Expected ledger to record the regional keyspace migration."); + await Assert.That(keyspaceScopes!).Contains("nam"); + + await Assert.That(byVersion.TryGetValue("002_create_interfold_schema.templated.cql", out var schemaScopes)).IsTrue() + .Because("Expected ledger to record the templated schema migration."); + await Assert.That(schemaScopes!).Contains("nam"); + } + + [Test] + [DependsOn(nameof(Scylla_Ledger_HasOneRowPerKeyspaceForTemplatedMigrations))] + public async Task Scylla_RerunningMigrations_IsANoOp() + { + await Assert.That(SharedDb.ScyllaPort).IsNotNull(); + + var before = await SnapshotScyllaLedgerAsync(); + + await ScyllaMigrationService.MigrateAsync( + BuildPersistenceConfig(), + BuildPostgresSecretsStore(), + BuildScyllaResolver(), + NullLoggerFactory.Instance.CreateLogger(), + CancellationToken.None); + + var after = await SnapshotScyllaLedgerAsync(); + + await Assert.That(after.Count).IsEqualTo(before.Count) + .Because("Re-running Scylla migrations must not insert new ledger rows."); + + foreach (var (key, ts) in before) + { + await Assert.That(after.TryGetValue(key, out var afterTs)).IsTrue() + .Because($"Ledger row {key} disappeared across re-run."); + await Assert.That(afterTs).IsEqualTo(ts) + .Because($"Ledger row {key} applied_at changed across re-run."); + } + } + + [Test] + [DependsOn(nameof(Scylla_RerunningMigrations_IsANoOp))] + public async Task Scylla_ChecksumDriftThrows_AndPreservesLedger() + { + await Assert.That(SharedDb.ScyllaPort).IsNotNull(); + + const string targetScope = "nam"; + const string targetVersion = "002_create_interfold_schema.templated.cql"; + + using var session = await OpenScyllaSessionAsync(); + + var originalChecksum = await ReadScyllaChecksumAsync(session, targetScope, targetVersion); + await Assert.That(originalChecksum).IsNotNull() + .Because($"Ledger row ({targetScope}, {targetVersion}) was missing — baseline assertion needs to pass first."); + + const string fakeChecksum = "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF"; + await UpdateScyllaChecksumAsync(session, targetScope, targetVersion, fakeChecksum); + + InvalidOperationException? thrown = null; + try + { + await ScyllaMigrationService.MigrateAsync( + BuildPersistenceConfig(), + BuildPostgresSecretsStore(), + BuildScyllaResolver(), + NullLoggerFactory.Instance.CreateLogger(), + CancellationToken.None); + } + catch (InvalidOperationException ex) + { + thrown = ex; + } + finally + { + await UpdateScyllaChecksumAsync(session, targetScope, targetVersion, originalChecksum!); + } + + await Assert.That(thrown).IsNotNull() + .Because("ScyllaMigrationService should fail fast when a recorded checksum no longer matches the template."); + await Assert.That(thrown!.Message).Contains("checksum"); + await Assert.That(thrown.Message).Contains(targetVersion); + await Assert.That(thrown.Message).Contains(targetScope); + } + + // --- Helpers --- + + private PersistenceConfiguration BuildPersistenceConfig() => new() + { + Mode = PersistenceMode.ScyllaPostgres, + PostgresConnectionString = SharedDb.PostgresConnectionString, + IsSingleScyllaInstance = true, + ScyllaKeyspace = ScyllaKeyspace.Nam, + }; + + private ISecretsStore BuildPostgresSecretsStore() + { + var connectionFactory = new PostgresConnectionFactory(Options.Create(BuildPersistenceConfig())); + return new PostgresSecretsStore(connectionFactory); + } + + /// + /// Builds a resolver whose contact-point / port overrides point at the shared fixture's + /// host-published Scylla endpoint. The ScyllaKeyspace comes from the shared + /// — the resolver's GetKeyspace reads it + /// straight off now instead of + /// re-parsing OCTOCON_SCYLLA_KEYSPACE from a throw-away IConfiguration. + /// + private IScyllaConfigResolver BuildScyllaResolver() + { + var overrides = new ScyllaOverrideOptions + { + ContactPoints = ["127.0.0.1"], + Port = SharedDb.ScyllaPort!.Value, + }; + return new ScyllaConfigResolver( + BuildPostgresSecretsStore(), + Options.Create(overrides), + Options.Create(BuildPersistenceConfig())); + } + + private async Task OpenScyllaSessionAsync() + { + var sts = await ScyllaTestSession.OpenAsAdminAsync("127.0.0.1", SharedDb.ScyllaPort!.Value); + return sts.Session; + } + + private async Task> SnapshotPostgresLedgerAsync() + { + await using var conn = await OpenAdminPostgresConnectionAsync(); + + await using var cmd = new NpgsqlCommand( + "SELECT version, applied_at FROM internal.schema_migrations", conn); + await using var reader = await cmd.ExecuteReaderAsync(); + + var snapshot = new Dictionary(StringComparer.Ordinal); + while (await reader.ReadAsync()) + { + snapshot[reader.GetString(0)] = reader.GetDateTime(1); + } + return snapshot; + } + + /// + /// Opens a Postgres connection using the admin credentials stored in internal.secrets + /// — the app user only has SELECT on internal.secrets, so any read/write against the + /// ledger has to escalate to the same admin role + /// uses for DDL. + /// + private async Task OpenAdminPostgresConnectionAsync() + { + var secrets = BuildPostgresSecretsStore(); + var adminUser = await secrets.GetAsync(SecretsStoreKeys.PostgresAdminUsername); + var adminPassword = await secrets.GetAsync(SecretsStoreKeys.PostgresAdminPassword); + + var builder = new NpgsqlConnectionStringBuilder(SharedDb.PostgresConnectionString) + { + Username = adminUser, + Password = adminPassword, + }; + + var conn = new NpgsqlConnection(builder.ConnectionString); + await conn.OpenAsync(); + return conn; + } + + private async Task> SnapshotScyllaLedgerAsync() + { + using var session = await OpenScyllaSessionAsync(); + var rs = await session.ExecuteAsync(new SimpleStatement( + "SELECT scope, version, applied_at FROM global.schema_migrations")); + + var snapshot = new Dictionary<(string, string), DateTimeOffset>(); + foreach (var row in rs) + { + var scope = row.GetValue("scope"); + var version = row.GetValue("version"); + if (!IsScyllaMigrationServiceLedgerRow(scope, version)) + continue; + + var appliedAt = row.GetValue("applied_at"); + snapshot[(scope, version)] = appliedAt; + } + return snapshot; + } + + // HexColorFixupService writes hex_color_fixup:* rows when peer tests spin up a web host; + // this suite asserts ScyllaMigrationService idempotency only. + private static bool IsScyllaMigrationServiceLedgerRow(string scope, string _) + => !scope.StartsWith("hex_color_fixup:", StringComparison.Ordinal); + + private static async Task ReadChecksumAsync(NpgsqlConnection conn, string version) + { + await using var cmd = new NpgsqlCommand( + "SELECT checksum FROM internal.schema_migrations WHERE version = @version", conn); + cmd.Parameters.AddWithValue("version", version); + return await cmd.ExecuteScalarAsync() as string; + } + + private static async Task UpdateChecksumAsync(NpgsqlConnection conn, string version, string checksum) + { + await using var cmd = new NpgsqlCommand( + "UPDATE internal.schema_migrations SET checksum = @checksum WHERE version = @version", conn); + cmd.Parameters.AddWithValue("checksum", checksum); + cmd.Parameters.AddWithValue("version", version); + await cmd.ExecuteNonQueryAsync(); + } + + private static async Task ReadScyllaChecksumAsync(ISession session, string scope, string version) + { + var rs = await session.ExecuteAsync(new SimpleStatement( + "SELECT checksum FROM global.schema_migrations WHERE scope = ? AND version = ?", + scope, version)); + return rs.FirstOrDefault()?.GetValue("checksum"); + } + + private static async Task UpdateScyllaChecksumAsync(ISession session, string scope, string version, string checksum) + { + await session.ExecuteAsync(new SimpleStatement( + "UPDATE global.schema_migrations SET checksum = ? WHERE scope = ? AND version = ?", + checksum, scope, version)); + } + + private static int CountEmbeddedResources(System.Reflection.Assembly assembly, string prefix, string suffix) + { + return assembly.GetManifestResourceNames() + .Count(n => n.StartsWith(prefix, StringComparison.Ordinal) + && n.EndsWith(suffix, StringComparison.Ordinal)); + } +} diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Scylla/RegionContextCachingTests.cs b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Scylla/RegionContextCachingTests.cs new file mode 100644 index 00000000..e895e993 --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/Services/Scylla/RegionContextCachingTests.cs @@ -0,0 +1,179 @@ +using Cassandra; +using Interfold.Infrastructure.Scylla; +using Interfold.IntegrationTests.Shared; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; + +namespace Interfold.Infrastructure.IntegrationTests.Services.Scylla; + +public sealed class RegionContextCachingTests : BaseEndpointTest +{ + // A stub that throws if the session is ever accessed, proving the cache is always used. + private sealed class ThrowingSessionProvider : IScyllaSessionProvider + { + public Task GetSessionAsync(CancellationToken cancellationToken = default) => + throw new InvalidOperationException("DB must not be reached when cache is warm."); + } + + private static ScyllaUserRegistryRegionContext BuildContext(string defaultRegion = "nam") + { + var persistenceOptions = Options.Create(new PersistenceConfiguration + { + ScyllaKeyspace = EnumWireExtensions.ParseScyllaKeyspace(defaultRegion), + }); + return new(new ThrowingSessionProvider(), + persistenceOptions, + NullLogger.Instance); + } + + [Test] + public async Task ResolveUserRegion_FallsBackToDefault_WhenSessionThrowsAndCacheEmpty() + { + // ThrowingSessionProvider will throw; the catch block should return CurrentRegion. + var ctx = BuildContext("eur"); + var result = ctx.ResolveUserRegion(new SystemId("user-123")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Eur); + } + + [Test] + public async Task ResolveUserRegion_UsesCachedRegion_AfterRegisterRegion() + { + var ctx = BuildContext("nam"); + ctx.RegisterRegion("user-abc", ScyllaKeyspace.Eur); + + // ThrowingSessionProvider must NOT be called; cache is warm. + var result = ctx.ResolveUserRegion(new SystemId("user-abc")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Eur); + } + + [Test] + public async Task ResolveUserRegion_StripsLegacyPrefix_BeforeCacheLookup() + { + var ctx = BuildContext("nam"); + ctx.RegisterRegion("eas", ScyllaKeyspace.Nam); // unrelated key must not interfere + + // The prefixed form should resolve via the same stripped key. + ctx.RegisterRegion("eas:user-xyz", ScyllaKeyspace.Sam); + var result = ctx.ResolveUserRegion(new SystemId("eas:user-xyz")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Sam); + + // Plain key lookup after prefix strip should also be cache-warm. + var result2 = ctx.ResolveUserRegion(new SystemId("eas:user-xyz")); + await Assert.That(result2).IsEqualTo(ScyllaKeyspace.Sam); + } + + [Test] + public async Task RegisterRegion_EmptyKey_DoesNotCorruptCache() + { + var ctx = BuildContext("nam"); + ctx.RegisterRegion("", ScyllaKeyspace.Eur); // empty key — should be no-op + + // Fallback should still apply because nothing was cached ("user-1" resolution hits + // the throwing session and falls back to the default region). + var result = ctx.ResolveUserRegion(new SystemId("user-1")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Nam); + } + + // ---------------- UserRegistryLookup-driven cache-key routing -------- + // + // Pins the (cacheKey, UserRegistryLookup?) contract inside HandleForLookup — the + // read-side helper that routes region-context cache reads by handle kind. The + // invariants being locked in: + // + // - Bare id / "id:" / ":" all cache under the same key (they all + // identify the same user_registry row). + // - "username:X" / "discord:X" keep their full prefixed form as the cache key so a + // username "abcdefg" cannot spuriously alias the bare id "abcdefg". + // - Unknown non-region prefixes ("xxx:abcdefg") don't get silently stripped — the + // whole input is the cache key AND the query value, matching the strict-rejection + // contract in UserRegistryLookup.TryParse's xml-doc. + + [Test] + public async Task ResolveUserRegion_ExplicitIdPrefix_SharesCacheKeyWithBareId() + { + var ctx = BuildContext("nam"); + ctx.RegisterRegion("abcdefg", ScyllaKeyspace.Eur); + + var fromBare = ctx.ResolveUserRegion(new SystemId("abcdefg")); + var fromIdPrefix = ctx.ResolveUserRegion(new SystemId("id:abcdefg")); + + using (Assert.Multiple()) + { + await Assert.That(fromBare).IsEqualTo(ScyllaKeyspace.Eur); + await Assert.That(fromIdPrefix).IsEqualTo(ScyllaKeyspace.Eur) + .Because("'id:abcdefg' and 'abcdefg' must resolve to the same cached entry — both are the strict 'this is a system id' assertion routing to user_registry.user_id."); + } + } + + [Test] + public async Task ResolveUserRegion_UsernamePrefix_KeepsFullFormAsCacheKey() + { + // Register two entries whose after-colon halves collide: an "abcdefg" system id + // in Eur, and a "username:abcdefg" (someone whose username happens to match a + // 7-char alphanumeric shape) in Sam. If the cache key stripped the "username:" + // prefix, the second RegisterRegion would overwrite the first and the bare + // lookup would return the wrong region. + var ctx = BuildContext("nam"); + ctx.RegisterRegion("abcdefg", ScyllaKeyspace.Eur); + ctx.RegisterRegion("username:abcdefg", ScyllaKeyspace.Sam); + + using (Assert.Multiple()) + { + await Assert.That(ctx.ResolveUserRegion(new SystemId("abcdefg"))).IsEqualTo(ScyllaKeyspace.Eur) + .Because("The bare id must remain cached under its own key even after a username handle with the same after-colon shape was registered."); + await Assert.That(ctx.ResolveUserRegion(new SystemId("username:abcdefg"))).IsEqualTo(ScyllaKeyspace.Sam) + .Because("Username handles must round-trip through their prefixed cache key so username 'abcdefg' doesn't alias the bare id 'abcdefg'."); + } + } + + [Test] + public async Task ResolveUserRegion_DiscordPrefix_KeepsFullFormAsCacheKey() + { + // Same anti-aliasing invariant as the username case: a Discord snowflake ("1234") + // cached under its prefixed shape must not collide with a bare id "1234". These + // aren't likely to overlap in production (Discord snowflakes are much longer + // than the 7-char system-id alphabet), but the invariant is worth pinning so a + // future test-fixture id shape can't silently poison the cache. + var ctx = BuildContext("nam"); + ctx.RegisterRegion("1234", ScyllaKeyspace.Eur); + ctx.RegisterRegion("discord:1234", ScyllaKeyspace.Sam); + + using (Assert.Multiple()) + { + await Assert.That(ctx.ResolveUserRegion(new SystemId("1234"))).IsEqualTo(ScyllaKeyspace.Eur); + await Assert.That(ctx.ResolveUserRegion(new SystemId("discord:1234"))).IsEqualTo(ScyllaKeyspace.Sam); + } + } + + [Test] + public async Task ResolveUserRegion_UnknownPrefix_KeepsFullFormAsCacheKey() + { + // 'xxx' is not a region tag and not a discriminator prefix — UserRegistryLookup.TryParse + // rejects it. The cache key must be the WHOLE original input so a rejected handle + // is deterministic and round-trippable, matching the fallback branch in + // LookupAsync that queries user_registry.user_id with the same whole input. + var ctx = BuildContext("nam"); + ctx.RegisterRegion("xxx:abcdefg", ScyllaKeyspace.Sam); + + var result = ctx.ResolveUserRegion(new SystemId("xxx:abcdefg")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Sam) + .Because("Unknown non-region prefix must NOT be silently stripped — the whole input is both the cache key and the eventual user_id query value."); + } + + [Test] + public async Task ResolveUserRegion_ExplicitIdPrefix_UsesCachedRegionSetViaBareRegister() + { + // The complementary direction of the "shared key" invariant: register with the + // bare form, resolve via "id:" prefix, must not miss the cache. This is the + // shape a controller sees when a client asks for /users/id:abcdefg but the + // account layer registered the user under just "abcdefg". + var ctx = BuildContext("nam"); + ctx.RegisterRegion("abcdefg", ScyllaKeyspace.Ocn); + + var result = ctx.ResolveUserRegion(new SystemId("id:abcdefg")); + await Assert.That(result).IsEqualTo(ScyllaKeyspace.Ocn); + } +} diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/TestServices/MultiNodeScyllaFixture.cs b/tests/integration/Interfold.Infrastructure.IntegrationTests/TestServices/MultiNodeScyllaFixture.cs new file mode 100644 index 00000000..01296095 --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/TestServices/MultiNodeScyllaFixture.cs @@ -0,0 +1,222 @@ +extern alias AppHost; +using Aspire.Hosting; +using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Testing; +using Cassandra; +using Interfold.DatabaseBootstrap; +using Interfold.Infrastructure.Postgres; +using Interfold.Infrastructure.Scylla; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using TUnit.Aspire; + +namespace Interfold.Infrastructure.IntegrationTests.TestServices; + +/// 7-node multi-DC ScyllaDB cluster in its own Aspire host, sharing Postgres + +/// seeded internal.secrets with via a ClassDataSource dependency +/// so multi-DC migrations see the same credentials as the single-node runs. +public sealed class MultiNodeScyllaFixture : AspireFixture +{ + /// Session-shared dependency that owns Postgres + secrets seeding. + [ClassDataSource(Shared = SharedType.PerTestSession)] + public required SharedDbFixture SharedDb { get; init; } + + /// Host port for the first Scylla node's CQL endpoint (scylla-nam). + public int ScyllaPort { get; private set; } + + protected override string[] Args => + [ + $"{AppHostParameterKeys.IncludePostgres}=false", + $"{AppHostParameterKeys.IncludeScylla}=true", + $"{AppHostParameterKeys.IncludeCassandra}=false", + $"{AppHostParameterKeys.ScyllaTopology}={ScyllaTopologyExtensions.MultiWireValue}", + $"{AppHostParameterKeys.IncludeApi}=false", + $"{AppHostParameterKeys.IncludeWeb}=false", + $"{AppHostParameterKeys.PersistentContainers}=false", + // Distinct port so Aspire test-mode allocator doesn't collide with SharedDbFixture. + $"{AppHostParameterKeys.PortsScylla}=39042", + $"{AppHostParameterKeys.ScyllaUser}={TestDbCredentials.ScyllaAppUser}", + $"{AppHostParameterKeys.ScyllaPassword}={TestDbCredentials.ScyllaAppPassword}", + $"{AppHostParameterKeys.EncryptionPrivateKey}=TEST" + ]; + + // 15 min covers slowest legitimate path: gossip bootstrap + sequential per-node CQL health + // gating (~30-60s per non-seed node × 6) + migrations. + protected override TimeSpan ResourceTimeout => TimeSpan.FromMinutes(15); + protected override bool EnableTelemetryCollection => false; + + // No Aspire health checks fire under include-api=false; readiness is driven directly + // via ResourceNotificationService in WaitForResourcesAsync. + protected override ResourceWaitBehavior WaitBehavior => ResourceWaitBehavior.None; + + public override async Task InitializeAsync() + { + // Distinct Aspire endpoints from SharedDbFixture (legacy) and the test-bench launcher + // — this host runs in-process beside SharedDb and must not rebinding 24223/23223. + AspireProcessEndpoints.ApplyToCurrentProcess( + AspireProcessEndpoints.MultiNodeResourceService, + AspireProcessEndpoints.MultiNodeOtlp, + AspireProcessEndpoints.MultiNodeAppUrls); + + // Raise fs.aio-max-nr session-wide before any Scylla node starts; without it the + // 3rd container onwards fails seastar AIO init and the cluster stalls at 2 nodes. + await HostAioPrerequisite + .EnsureAsync(HostAioPrerequisite.TotalScyllaNodesForSession()) + .ConfigureAwait(false); + await base.InitializeAsync().ConfigureAwait(false); + } + + public override async ValueTask DisposeAsync() + { + using var _ = LifecycleProbe.BeginTimed("AfterFixtureDispose:MultiNodeScyllaFixture"); + LifecycleProbe.Log("BeforeFixtureDispose:MultiNodeScyllaFixture"); + + try + { + await AspireContainerCleanup + .StopResourcesAsync(AspireContainerCleanup.MultiNodeScyllaResourceNames()) + .ConfigureAwait(false); + } + catch + { + // Best-effort pre-stop; base dispose must still run. + } + + await base.DisposeAsync().ConfigureAwait(false); + + var remaining = await AspireContainerCleanup.CountRunningAspireContainersAsync().ConfigureAwait(false); + if (remaining > 0) + LifecycleProbe.Log($"MultiNodeScyllaFixture.RemainingAspireContainers:{remaining}"); + } + + protected override async Task WaitForResourcesAsync(DistributedApplication app, CancellationToken cancellationToken) + { + var notifications = app.Services.GetRequiredService(); + + string[] regions = ["nam", "eur", "sam", "sas", "eas", "ocn", "gdpr"]; + foreach (var region in regions) + { + await notifications.WaitForResourceAsync($"scylla-{region}", KnownResourceStates.Running, cancellationToken); + } + + // Seed via default cassandra/cassandra; identical creds to SharedDbFixture so the + // migration step below authenticates with the same admin row in internal.secrets. + var scEndpoint = App.GetEndpoint("scylla-nam", "cql"); + await DbInitHelper.WaitForScyllaAsync(scEndpoint.Host, scEndpoint.Port, cancellationToken); + await DbInitHelper.SeedScyllaAsync( + scEndpoint.Host, scEndpoint.Port, BuildScyllaSeedOptions(), cancellationToken); + + // Container Running only means the CQL listener bound; gossip + range streaming + // finish asynchronously and NetworkTopologyStrategy DDL needs cross-DC quorum first. + await WaitForGossipPropagationAsync(scEndpoint, regions, cancellationToken); + + // Migrations use SharedDbFixture's internal.secrets creds but the multi-DC endpoint. + var persistenceConfig = new PersistenceConfiguration + { + Mode = Interfold.Shared.Contracts.PersistenceMode.ScyllaPostgres, + PostgresConnectionString = SharedDb.PostgresConnectionString, + IsSingleScyllaInstance = false, + ScyllaKeyspace = ScyllaKeyspace.Nam, + }; + var connectionFactory = new PostgresConnectionFactory(Microsoft.Extensions.Options.Options.Create(persistenceConfig)); + var secretsStore = new PostgresSecretsStore(connectionFactory); + + var overrides = new ScyllaOverrideOptions + { + ContactPoints = [scEndpoint.Host], + Port = scEndpoint.Port, + }; + var resolver = new ScyllaConfigResolver( + secretsStore, + Microsoft.Extensions.Options.Options.Create(overrides), + Microsoft.Extensions.Options.Options.Create(persistenceConfig)); + + await ScyllaMigrationService.MigrateAsync( + persistenceConfig, + secretsStore, + resolver, + NullLoggerFactory.Instance.CreateLogger(), + cancellationToken); + + ScyllaPort = scEndpoint.Port; + } + + private static async Task WaitForGossipPropagationAsync( + Uri scEndpoint, + string[] expectedRegions, + CancellationToken cancellationToken) + { + // 5 min covers slowest DinD gossip (30-45 s/node) within the outer ResourceTimeout. + var deadline = DateTime.UtcNow + TimeSpan.FromMinutes(5); + Exception? lastError = null; + HashSet lastObserved = new(StringComparer.OrdinalIgnoreCase); + + while (DateTime.UtcNow < deadline) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + await using var sts = await ScyllaTestSession.OpenAsAppAsync( + scEndpoint.Host, scEndpoint.Port, queryTimeoutMs: 15000); + var session = sts.Session; + + var visibleDcs = new HashSet(StringComparer.OrdinalIgnoreCase); + + var localRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.local")); + foreach (var row in localRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) + visibleDcs.Add(dc.ToLowerInvariant()); + } + + var peerRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.peers")); + foreach (var row in peerRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) + visibleDcs.Add(dc.ToLowerInvariant()); + } + + if (!visibleDcs.SetEquals(lastObserved)) + { + Console.WriteLine( + $"[multi-node-fixture {DateTime.UtcNow:HH:mm:ss}] gossip view: {string.Join(", ", visibleDcs.OrderBy(x => x))}"); + lastObserved = new HashSet(visibleDcs, StringComparer.OrdinalIgnoreCase); + } + + if (expectedRegions.All(r => visibleDcs.Contains(r))) + { + return; + } + + lastError = new InvalidOperationException( + $"Gossip not converged yet — saw {string.Join(", ", visibleDcs.OrderBy(x => x))}, " + + $"expected {string.Join(", ", expectedRegions)}."); + } + catch (Exception ex) + { + lastError = ex; + } + + await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); + } + + throw new TimeoutException( + "Multi-DC Scylla gossip did not converge across all 7 regions within the 5-minute readiness window. " + + $"Last observation: {lastError?.Message}", lastError); + } + + private static ScyllaSeedOptions BuildScyllaSeedOptions() + => new( + AppUser: TestDbCredentials.ScyllaAppUser, + AppPassword: TestDbCredentials.ScyllaAppPassword, + AdminUser: TestDbCredentials.ScyllaAdminUser, + AdminPassword: TestDbCredentials.ScyllaAdminPassword, + LockDefaultCassandra: true); +} diff --git a/tests/integration/Interfold.Infrastructure.IntegrationTests/Topology/MultiNodeScyllaTests.cs b/tests/integration/Interfold.Infrastructure.IntegrationTests/Topology/MultiNodeScyllaTests.cs new file mode 100644 index 00000000..df4b77ac --- /dev/null +++ b/tests/integration/Interfold.Infrastructure.IntegrationTests/Topology/MultiNodeScyllaTests.cs @@ -0,0 +1,81 @@ +using Cassandra; +using Interfold.Infrastructure.IntegrationTests.TestServices; +using Interfold.IntegrationTests.Shared.TestServices; + +namespace Interfold.Infrastructure.IntegrationTests.Topology; + +/// +/// Tests that a multi-node ScyllaDB cluster (7 regional DCs) correctly spins up +/// and can serve cross-DC queries. Uses a dedicated +/// managed by TUnit.Aspire that shares Postgres with . +/// +/// Marked — spinning up a 7-DC Scylla ring on a workstation +/// eats several GB of RAM + several minutes of wall-clock. Local dotnet test at the +/// solution root skips this class; CI opts in explicitly via +/// --treenode-filter '/*/*/*MultiNodeScyllaTests/*' in the Infrastructure leg. +/// +/// +[Explicit] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class MultiNodeScyllaTests(MultiNodeScyllaFixture fixture) +{ + private static readonly string[] ExpectedRegions = ["nam", "eur", "sam", "sas", "eas", "ocn", "gdpr"]; + + [Test] + public async Task MultiNodeScylla_AllNodesReachUpNormalState() + { + await using var sts = await ScyllaTestSession.OpenAsAppAsync("127.0.0.1", fixture.ScyllaPort); + var session = sts.Session; + + var visibleDcs = new HashSet(StringComparer.OrdinalIgnoreCase); + + var localRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.local")); + foreach (var row in localRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) + visibleDcs.Add(dc.ToLowerInvariant()); + } + + var peerRows = await session.ExecuteAsync(new SimpleStatement("SELECT data_center FROM system.peers")); + foreach (var row in peerRows) + { + var dc = row.GetValue("data_center"); + if (!string.IsNullOrWhiteSpace(dc)) + visibleDcs.Add(dc.ToLowerInvariant()); + } + + foreach (var region in ExpectedRegions) + { + await Assert.That(visibleDcs.Contains(region)).IsTrue() + .Because($"Expected DC '{region}' to be visible in cluster"); + } + } + + [Test] + [DependsOn(nameof(MultiNodeScylla_AllNodesReachUpNormalState))] + public async Task MultiNodeScylla_CrossDcCqlQuerySucceeds() + { + await using var sts = await ScyllaTestSession.OpenAsAppAsync("127.0.0.1", fixture.ScyllaPort); + var namSession = sts.Session; + + // Column names match the live schema in + // infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql + // (PRIMARY KEY (user_id) on the `global.user_registry` table); the previous + // `id` literal would now fail with "Unknown identifier id". + var testUserId = TestIds.NewSystemId("test", maxLen: 20); + + await namSession.ExecuteAsync(new SimpleStatement( + "INSERT INTO global.user_registry (user_id, region) VALUES (?, ?)", testUserId, "nam")); + + var result = await namSession.ExecuteAsync(new SimpleStatement( + "SELECT region FROM global.user_registry WHERE user_id = ?", testUserId)); + + var row = result.FirstOrDefault(); + await Assert.That(row).IsNotNull(); + await Assert.That(row!.GetValue("region")).IsEqualTo("nam"); + + await namSession.ExecuteAsync(new SimpleStatement( + "DELETE FROM global.user_registry WHERE user_id = ?", testUserId)); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/BaseEndpointTest.cs b/tests/integration/Interfold.IntegrationTests.Shared/BaseEndpointTest.cs new file mode 100644 index 00000000..fdc27b61 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/BaseEndpointTest.cs @@ -0,0 +1,406 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Read; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Domain; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Settings.Contracts.Models.Read; +using Interfold.Settings.Contracts.Models.Wire; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; +using Microsoft.Extensions.Options; +using TUnit.Core.Services; + +namespace Interfold.IntegrationTests.Shared; + +public class BaseEndpointTest +{ + private const int SoakRepeatCount = 5; + + /// Discovers required fixtures before any [ClassDataSource] fixture initializes + /// so sees the filtered run rather than the whole assembly. + /// Hosted here because TUnit only scans hooks on classes reachable from the test graph. + [After(HookType.TestDiscovery)] + public static void RegisterRequiredFixtures() + { + LifecycleProbe.Log("After(TestDiscovery)"); + RequiredFixtures.Discover(); + } + + /// Lifecycle-ordering probe; temporary. + [Before(HookType.TestDiscovery)] + public static void Probe_BeforeTestDiscovery() + => LifecycleProbe.Log("Before(TestDiscovery)"); + + [Before(HookType.TestSession)] + public static void Probe_BeforeTestSession() + => LifecycleProbe.Log("Before(TestSession)"); + + [After(HookType.TestSession)] + public static void Probe_AfterTestSession() + => LifecycleProbe.Log("After(TestSession)"); + + public static byte[] Base64UrlDecodeBytes(string base64Url) + { + var padded = base64Url.Replace('-', '+').Replace('_', '/'); + padded = (padded.Length % 4) switch + { + 2 => padded + "==", + 3 => padded + "=", + _ => padded + }; + return Convert.FromBase64String(padded); + } + + public static bool UrlPathStartsWith(string? url, string expectedPrefix) + { + if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(expectedPrefix)) + return false; + + if (Uri.TryCreate(url, UriKind.Absolute, out var absolute)) + return absolute.AbsolutePath.StartsWith(expectedPrefix, StringComparison.Ordinal); + + return url.StartsWith(expectedPrefix, StringComparison.Ordinal); + } + + public static HttpRequestMessage BuildMultipartUploadRequest(HttpClient client, string path, string principalId, string fileName, string contentType) + { + var request = new HttpRequestMessage(HttpMethod.Put, path); + AttachPrincipalAuth(request, client, principalId); + request.Headers.Add("X-Interfold-Idempotency-Key", Guid.NewGuid().ToString("N")); + + var data = Encoding.UTF8.GetBytes("octocon-avatar-bytes"); + var fileContent = new ByteArrayContent(data); + fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType); + + var form = new MultipartFormDataContent(); + form.Add(fileContent, "file", fileName); + + request.Content = form; + + return request; + } + + /// Parses the trailing integer from a Location header. Prefer + /// when a typed wrapper is needed. + public static int ReadTrailingIntFromLocation(HttpResponseMessage response) + { + var location = response.Headers.Location?.ToString(); + if (string.IsNullOrWhiteSpace(location)) + throw new InvalidOperationException("Expected Location header on alter-create response."); + + var segment = location.Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); + if (!int.TryParse(segment, out var id) || id <= 0) + throw new InvalidOperationException($"Could not parse alter id from Location header '{location}'."); + + return id; + } + + /// Typed wrapper form of . + public static AlterId ReadTrailingAlterIdFromLocation(HttpResponseMessage response) + => new((short)ReadTrailingIntFromLocation(response)); + + /// Seeds the four-viewer visibility quartet used by every "public read gated by + /// relationship" test. All four principals get a public profile row; the three viewers + /// also get a seed alter so ListGuardedAsync can't short-circuit before the + /// relationship gate. Returns (Owner, NonFriend, Friend, Trusted). + /// + /// The composed IDs carry an 8-hex per-invocation nonce so bench-shared idempotency + /// state (PostgresIdempotencyStore, single Postgres across every leaf project + /// under the centralised test-bench) can't replay a previous run's response and hand + /// back an AlterId the current backend never wrote — same failure mode as + /// . Callers must therefore not assume the returned + /// principals equal {prefix}-role verbatim. + /// + public static async Task<(string Owner, string NonFriend, string Friend, string Trusted)> + SeedVisibilityQuartetAsync(HttpClient client, string prefix) + { + var nonce = Guid.NewGuid().ToString("N")[..8]; + var owner = $"{prefix}-owner-{nonce}"; + var nonFriend = $"{prefix}-nonfriend-{nonce}"; + var friend = $"{prefix}-friend-{nonce}"; + var trusted = $"{prefix}-trusted-{nonce}"; + + _ = await CreateAlterAsync(client, nonFriend, "SeedNonFriend"); + _ = await CreateAlterAsync(client, friend, "SeedFriend"); + _ = await CreateAlterAsync(client, trusted, "SeedTrusted"); + await EnsureUserExistsAsync(client, owner); + await EnsureUserExistsAsync(client, nonFriend); + await EnsureUserExistsAsync(client, friend); + await EnsureUserExistsAsync(client, trusted); + + return (owner, nonFriend, friend, trusted); + } + + /// Ensures a public profile exists via a username POST; treats 204 and 409 + /// (already set) as success. + public static async Task EnsureUserExistsAsync(HttpClient client, string principal, string? username = null) + { + var body = new SettingsUsernameRequest(new Username(username ?? principal)); + using var res = await client.SendAsJsonAsync(HttpMethod.Post, "/api/settings/username", body, principal); + var responseBody = res.IsSuccessStatusCode || res.StatusCode == HttpStatusCode.Conflict + ? string.Empty + : await res.Content.ReadAsStringAsync(); + await Assert.That(res.IsSuccessStatusCode || res.StatusCode == HttpStatusCode.Conflict) + .IsTrue().Because($"EnsureUserExistsAsync failed for '{principal}': {(int)res.StatusCode} body={responseBody}"); + } + + /// Creates an alter and returns its typed . Reads the + /// response as over so + /// read-model drift surfaces at deserialisation. + public static async Task CreateAlterAsync(HttpClient client, string principal, string name, VisibilityLevel visibility = VisibilityLevel.Public) + { + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest(name), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + + if (visibility != VisibilityLevel.Private) + { + await SetAlterSecurityLevelAsync(client, principal, envelope.Data.Id, visibility); + } + + return envelope.Data.Id; + } + + public static async Task CreateAlterAsync(HttpClient client, string principal, string name, string username, VisibilityLevel visibility = VisibilityLevel.Public) + { + await EnsureUserExistsAsync(client, principal, username); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/alters", + new CreateAlterRequest(name), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + + if (visibility != VisibilityLevel.Private) + { + await SetAlterSecurityLevelAsync(client, principal, envelope.Data.Id, visibility); + } + + return envelope.Data.Id; + } + + /// Generic PATCH against /api/systems/me/alters/{id}. + public static async Task PatchAlterAsync(HttpClient client, string principal, AlterId alterId, UpdateAlterRequest request) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Patch, $"/api/systems/me/alters/{alterId}", + request, + principal); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected alter update 204, got {(int)res.StatusCode}. Body: {await res.Content.ReadAsStringAsync()}"); + } + + /// Patches the alter's custom-field values via typed + /// list. + public static Task UpdateAlterFieldsAsync(HttpClient client, string principal, AlterId alterId, IReadOnlyList fields) + => PatchAlterAsync(client, principal, alterId, new UpdateAlterRequest(Fields: fields)); + + /// Sets the alter's via PATCH. + public static Task SetAlterSecurityLevelAsync(HttpClient client, string principal, AlterId alterId, VisibilityLevel securityLevel) + => PatchAlterAsync(client, principal, alterId, new UpdateAlterRequest(SecurityLevel: securityLevel)); + + /// Creates a tag and returns its typed . + public static async Task CreateTagAsync(HttpClient client, string principal, string name) + { + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/tags", + new CreateTagRequest(name, ParentTagId: null), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + return envelope.Data.Id; + } + + /// Sets a tag's via PATCH. + public static async Task SetTagSecurityLevelAsync(HttpClient client, string principal, TagId tagId, VisibilityLevel securityLevel) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Patch, $"/api/systems/me/tags/{tagId}", + new UpdateTagRequest(SecurityLevel: securityLevel), + principal); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected tag security update 204, got {(int)res.StatusCode}. Body: {await res.Content.ReadAsStringAsync()}"); + } + + /// POSTs a typed , asserts 201, returns the new + /// . + public static async Task StartFrontAsync(HttpClient client, string principal, AlterId alterId) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/start", + new FrontStartRequest(alterId), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + return envelope.Data.FrontId; + } + + /// Sends front-start without asserting status; callers dispose and can read + /// the typed envelope themselves. + public static Task SendFrontStartAsync( + HttpClient client, + AlterId alterId, + string? comment, + string principal = "fronting-default-principal") + => client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/start", + new FrontStartRequest(alterId, comment), + principal, + idempotencyKey: Guid.NewGuid().ToString("N")); + + public static Task SendFrontEndAsync( + HttpClient client, + AlterId alterId, + string principal = "fronting-default-principal") + => client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/end", + new FrontEndRequest(alterId), + principal, + idempotencyKey: Guid.NewGuid().ToString("N")); + + public static Task SendFrontSetAsync( + HttpClient client, + AlterId alterId, + string principal, + string? comment = null) + => client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/front/set", + new FrontSetRequest(alterId, comment), + principal, + idempotencyKey: Guid.NewGuid().ToString("N")); + + public static async Task SendFriendRequestAndAcceptAsync(HttpClient client, string sender, string recipient) + { + using var sendRes = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/friend-requests/{recipient}", + new object(), + sender); + await Assert.That(sendRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected friend-request send 204, got {(int)sendRes.StatusCode}. Body: {await sendRes.Content.ReadAsStringAsync()}"); + + using var acceptRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friend-requests/{sender}/accept", + new object(), + recipient); + await Assert.That(acceptRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected friend-request accept 204, got {(int)acceptRes.StatusCode}. Body: {await acceptRes.Content.ReadAsStringAsync()}"); + } + + public static async Task SetFriendTrustAsync(HttpClient client, string principal, string friendId) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/friends/{friendId}/trust", + new object(), + principal); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected trust set 204, got {(int)res.StatusCode}. Body: {await res.Content.ReadAsStringAsync()}"); + } + + /// Creates a settings field and returns its typed . + public static async Task CreateSettingsFieldAsync(HttpClient client, string principal, string fieldName, FieldType type, VisibilityLevel securityLevel) + { + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/fields", + new SettingsCreateFieldRequest(fieldName, type, securityLevel, Locked: null), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + return envelope.Data.Id; + } + + /// Loops calls against the same idempotency key; + /// iteration 0 must see replay=false and every later iteration replay=true. + public static async Task RunSoakAsync(InterfoldWebApplicationFactory factory, + Func> requestFactory) + { + using var client = factory.CreateClient(); + var token = factory.CreateToken("soak-default-principal"); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + client.DefaultRequestHeaders.Remove("X-Interfold-Principal"); + client.DefaultRequestHeaders.Add("X-Interfold-Principal", "soak-default-principal"); + + await EnsureUserExistsAsync(client, "soak-default-principal"); + + var key = Guid.NewGuid().ToString("N"); + + for (var i = 0; i < SoakRepeatCount; i++) + { + using var response = await requestFactory(client, key); + var body = await response.Content.ReadAsStringAsync(); + + await Assert.That(response.IsSuccessStatusCode).IsTrue().Because($"Soak call #{i + 1}: expected 2xx, got {(int)response.StatusCode}. Body: {body}"); + + if (string.IsNullOrEmpty(body)) + continue; + + var envelope = JsonSerializer.Deserialize>(body, TestJson.Options) + ?? throw new InvalidOperationException($"Soak call #{i + 1}: failed to deserialise TestEnvelope. Body: {body}"); + + var replay = envelope.Replay ?? false; + + if (i == 0) + { + await Assert.That(!replay).IsTrue().Because($"Soak call #1: expected replay=false on first invocation. Body: {body}"); + } + else + { + await Assert.That(replay).IsTrue().Because($"Soak call #{i + 1}: expected replay=true after first invocation. Body: {body}"); + } + } + } + + public static async Task CreateRandomToken(InterfoldWebApplicationFactory factory, string systemId) + { + var rev = factory.Services.GetRequiredService(); + // OptionsMonitor.CurrentValue is the PostConfigure-patched instance; a fresh + // IConfiguration.Get() would bypass the JWT key patches. + var authConfig = factory.Services.GetRequiredService>().CurrentValue; + + authConfig.JwtEs256PrivateKeyPem = TestDbCredentials.JwtEs256PrivateKeyPem; + + var jti = Guid.NewGuid().ToString("N"); + + var now = DateTimeOffset.UtcNow; + var expiresAt = now.AddDays(1); + + // Middleware requires a scoped `{region}:{rawId}` sub; raw-sub 401s and the WS + // pump never sees the event. Nam matches the InMemory bootstrapper default. + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, systemId); + var scopedSystemId = scoped.AsSystemId(); + + var token = AuthHelper.CreateToken(authConfig, expiresAt, now, new Jti(jti), scopedSystemId); + // Record the scoped id so audit columns match the JWT sub byte-for-byte. + await rev.RecordTokenAsync(new Jti(jti), scopedSystemId, expiresAt, CancellationToken.None); + + // Raw systemId is fine here: EnsureUserExistsAsync → AttachPrincipalAuth → + // factory.CreateToken scopes internally. + using var client = factory.CreateClient(); + await EnsureUserExistsAsync(client, systemId); + + return token; + } + + public static void AttachPrincipalAuth(HttpRequestMessage request, HttpClient client, string principal) + { + if (!InterfoldWebApplicationFactory.TryGetFactory(client, out var factory)) + throw new InvalidOperationException("Could not resolve test factory for HttpClient. Use InterfoldWebApplicationFactory.CreateClient() to create test clients."); + + var token = factory.CreateToken(principal); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + request.Headers.Remove("X-Interfold-Principal"); + request.Headers.Add("X-Interfold-Principal", principal); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/alter-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/alter-lifecycle.trace.json new file mode 100644 index 00000000..ee3a8abf --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/alter-lifecycle.trace.json @@ -0,0 +1,61 @@ +{ + "version": 1, + "description": "Alter lifecycle: create, update, delete — verifies idempotency replay for each op.", + "steps": [ + { + "name": "create alter", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-create-001", + "body": { "name": "ReplayAlter" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "alterId": "alterId" } + }, + { + "name": "create alter (replay)", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-create-001", + "body": { "name": "ReplayAlter" }, + "expectedStatus": 201, + "expectedReplay": true + }, + { + "name": "update alter", + "method": "PATCH", + "path": "/api/systems/me/alters/{alterId}", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-update-001", + "body": { "name": "ReplayAlterRenamed" }, + "expectedStatus": 204 + }, + { + "name": "update alter (replay)", + "method": "PATCH", + "path": "/api/systems/me/alters/{alterId}", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-update-001", + "body": { "name": "ReplayAlterRenamed" }, + "expectedStatus": 204 + }, + { + "name": "delete alter", + "method": "DELETE", + "path": "/api/systems/me/alters/{alterId}", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-delete-001", + "expectedStatus": 204 + }, + { + "name": "delete alter (replay)", + "method": "DELETE", + "path": "/api/systems/me/alters/{alterId}", + "principalId": "replay-sys-alter", + "idempotencyKey": "replay-alter-delete-001", + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/friendship-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/friendship-lifecycle.trace.json new file mode 100644 index 00000000..f581acc0 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/friendship-lifecycle.trace.json @@ -0,0 +1,70 @@ +{ + "version": 1, + "description": "Friendship lifecycle: send request, accept, trust, remove — with idempotency replay for each op.", + "steps": [ + { + "name": "send friend request (A -> B)", + "method": "PUT", + "path": "/api/friend-requests/replay-sys-friend-b", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-send-001", + "expectedStatus": 204 + }, + { + "name": "send friend request (replay)", + "method": "PUT", + "path": "/api/friend-requests/replay-sys-friend-b", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-send-001", + "expectedStatus": 204 + }, + { + "name": "accept friend request (B accepts A)", + "method": "POST", + "path": "/api/friend-requests/replay-sys-friend-a/accept", + "principalId": "replay-sys-friend-b", + "idempotencyKey": "replay-friend-accept-001", + "expectedStatus": 204 + }, + { + "name": "accept friend request (replay)", + "method": "POST", + "path": "/api/friend-requests/replay-sys-friend-a/accept", + "principalId": "replay-sys-friend-b", + "idempotencyKey": "replay-friend-accept-001", + "expectedStatus": 204 + }, + { + "name": "trust friend (A trusts B)", + "method": "POST", + "path": "/api/friends/replay-sys-friend-b/trust", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-trust-001", + "expectedStatus": 204 + }, + { + "name": "trust friend (replay)", + "method": "POST", + "path": "/api/friends/replay-sys-friend-b/trust", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-trust-001", + "expectedStatus": 204 + }, + { + "name": "remove friend (A removes B)", + "method": "DELETE", + "path": "/api/friends/replay-sys-friend-b", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-remove-001", + "expectedStatus": 204 + }, + { + "name": "remove friend (replay)", + "method": "DELETE", + "path": "/api/friends/replay-sys-friend-b", + "principalId": "replay-sys-friend-a", + "idempotencyKey": "replay-friend-remove-001", + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-delete-parity.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-delete-parity.trace.json new file mode 100644 index 00000000..4f3f5d42 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-delete-parity.trace.json @@ -0,0 +1,62 @@ +{ + "version": 1, + "description": "Fronting delete-parity: deleting a historical front must not wipe a subsequently-started active front on the same alter.", + "steps": [ + { + "name": "create alter", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-front-parity", + "idempotencyKey": "replay-front-parity-alter-001", + "body": { "name": "ParityAlter" }, + "expectedStatus": 201, + "captureAs": { "alterId": "id" } + }, + { + "name": "start F1", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front-parity", + "idempotencyKey": "replay-front-parity-start-f1", + "body": { "id": "{alterId}" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "frontId1": "frontId" } + }, + { + "name": "end F1 (moves F1 to history)", + "method": "POST", + "path": "/api/systems/me/front/end", + "principalId": "replay-sys-front-parity", + "idempotencyKey": "replay-front-parity-end-f1", + "body": { "id": "{alterId}" }, + "expectedStatus": 204 + }, + { + "name": "start F2 (same alter, new active front)", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front-parity", + "idempotencyKey": "replay-front-parity-start-f2", + "body": { "id": "{alterId}" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "frontId2": "frontId" } + }, + { + "name": "delete F1 from history - must not clear F2", + "method": "DELETE", + "path": "/api/systems/me/front/{frontId1}", + "principalId": "replay-sys-front-parity", + "idempotencyKey": "replay-front-parity-delete-f1", + "expectedStatus": 204 + }, + { + "name": "F2 must still be active after deleting F1", + "method": "GET", + "path": "/api/systems/me/front/{frontId2}", + "principalId": "replay-sys-front-parity", + "expectedStatus": 200 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-lifecycle.trace.json new file mode 100644 index 00000000..2a2aefb3 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-lifecycle.trace.json @@ -0,0 +1,53 @@ +{ + "version": 1, + "description": "Fronting lifecycle: start front, end front — with idempotency replay.", + "steps": [ + { + "name": "create alter for fronting", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-front", + "idempotencyKey": "replay-front-alter-create-001", + "body": { "name": "FrontTestAlter" }, + "expectedStatus": 201, + "captureAs": { "alterId": "id" } + }, + { + "name": "start front", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front", + "idempotencyKey": "replay-front-start-001", + "body": { "id": "{alterId}" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "frontId": "frontId" } + }, + { + "name": "start front (replay)", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front", + "idempotencyKey": "replay-front-start-001", + "body": { "id": "{alterId}" }, + "expectedStatus": 201, + "expectedReplay": true + }, + { + "name": "end front", + "method": "DELETE", + "path": "/api/systems/me/front/{frontId}", + "principalId": "replay-sys-front", + "idempotencyKey": "replay-front-end-001", + "expectedStatus": 204 + }, + { + "name": "end front (replay)", + "method": "DELETE", + "path": "/api/systems/me/front/{frontId}", + "principalId": "replay-sys-front", + "idempotencyKey": "replay-front-end-001", + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-visibility.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-visibility.trace.json new file mode 100644 index 00000000..bd2ea984 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/fronting-visibility.trace.json @@ -0,0 +1,76 @@ +{ + "version": 1, + "description": "Fronting visibility: active fronts should respect alter visibility", + "steps": [ + { + "name": "create public alter", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-front-vis", + "idempotencyKey": "replay-front-vis-alter1", + "body": { + "name": "PublicAlter" + }, + "expectedStatus": 201, + "captureAs": { + "publicAlterId": "id" + } + }, + { + "name": "create private alter", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-front-vis", + "idempotencyKey": "replay-front-vis-alter2", + "body": { + "name": "PrivateAlter" + }, + "expectedStatus": 201, + "captureAs": { + "privateAlterId": "id" + } + }, + { + "name": "set public alter to public", + "method": "PATCH", + "path": "/api/systems/me/alters/{publicAlterId}", + "principalId": "replay-sys-front-vis", + "idempotencyKey": "replay-front-vis-alter1-patch", + "body": { + "security_level": "public" + }, + "expectedStatus": 204 + }, + { + "name": "start front public", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front-vis", + "idempotencyKey": "replay-front-vis-start1", + "body": { + "id": "{publicAlterId}" + }, + "expectedStatus": 201 + }, + { + "name": "start front private", + "method": "POST", + "path": "/api/systems/me/front/start", + "principalId": "replay-sys-front-vis", + "idempotencyKey": "replay-front-vis-start2", + "body": { + "id": "{privateAlterId}" + }, + "expectedStatus": 201 + }, + { + "name": "get active fronts as stranger", + "method": "GET", + "path": "/api/systems/replay-sys-front-vis/fronting", + "principalId": "replay-sys-front-stranger", + "expectedStatus": 200, + "expectedJsonContains": "PublicAlter", + "expectedJsonNotContains": "PrivateAlter" + } + ] +} \ No newline at end of file diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/journal-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/journal-lifecycle.trace.json new file mode 100644 index 00000000..87947262 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/journal-lifecycle.trace.json @@ -0,0 +1,93 @@ +{ + "version": 1, + "description": "Journal lifecycle: create, update, lock, pin, unpin, delete — with idempotency replay for each mutating op.", + "steps": [ + { + "name": "create journal", + "method": "POST", + "path": "/api/journals", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-create-001", + "body": { "title": "ReplayJournal" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "entryId": "entryId" } + }, + { + "name": "create journal (replay)", + "method": "POST", + "path": "/api/journals", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-create-001", + "body": { "title": "ReplayJournal" }, + "expectedStatus": 201, + "expectedReplay": true + }, + { + "name": "update journal", + "method": "PATCH", + "path": "/api/journals/{entryId}", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-update-001", + "body": { "title": "ReplayJournalRenamed", "content": "Updated content." }, + "expectedStatus": 204 + }, + { + "name": "update journal (replay)", + "method": "PATCH", + "path": "/api/journals/{entryId}", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-update-001", + "body": { "title": "ReplayJournalRenamed", "content": "Updated content." }, + "expectedStatus": 204 + }, + { + "name": "lock journal", + "method": "POST", + "path": "/api/journals/{entryId}/lock", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-lock-001", + "expectedStatus": 204 + }, + { + "name": "lock journal (replay)", + "method": "POST", + "path": "/api/journals/{entryId}/lock", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-lock-001", + "expectedStatus": 204 + }, + { + "name": "pin journal", + "method": "POST", + "path": "/api/journals/{entryId}/pin", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-pin-001", + "expectedStatus": 204 + }, + { + "name": "unpin journal", + "method": "POST", + "path": "/api/journals/{entryId}/unpin", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-unpin-001", + "expectedStatus": 204 + }, + { + "name": "delete journal", + "method": "DELETE", + "path": "/api/journals/{entryId}", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-delete-001", + "expectedStatus": 204 + }, + { + "name": "delete journal (replay)", + "method": "DELETE", + "path": "/api/journals/{entryId}", + "principalId": "replay-sys-journal", + "idempotencyKey": "replay-journal-delete-001", + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/poll-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/poll-lifecycle.trace.json new file mode 100644 index 00000000..3506ae7f --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/poll-lifecycle.trace.json @@ -0,0 +1,44 @@ +{ + "version": 1, + "description": "Poll lifecycle: create, update, delete — with idempotency replay.", + "steps": [ + { + "name": "create poll", + "method": "POST", + "path": "/api/polls", + "principalId": "replay-sys-poll", + "idempotencyKey": "replay-poll-create-001", + "body": { "title": "ReplayPoll", "options": ["A", "B"] }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "pollId": "pollId" } + }, + { + "name": "create poll (replay)", + "method": "POST", + "path": "/api/polls", + "principalId": "replay-sys-poll", + "idempotencyKey": "replay-poll-create-001", + "body": { "title": "ReplayPoll", "options": ["A", "B"] }, + "expectedStatus": 201, + "expectedReplay": true + }, + { + "name": "update poll", + "method": "PATCH", + "path": "/api/polls/{pollId}", + "principalId": "replay-sys-poll", + "idempotencyKey": "replay-poll-update-001", + "body": { "title": "ReplayPollRenamed" }, + "expectedStatus": 204 + }, + { + "name": "delete poll", + "method": "DELETE", + "path": "/api/polls/{pollId}", + "principalId": "replay-sys-poll", + "idempotencyKey": "replay-poll-delete-001", + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/settings-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/settings-lifecycle.trace.json new file mode 100644 index 00000000..3e29def5 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/settings-lifecycle.trace.json @@ -0,0 +1,42 @@ +{ + "version": 1, + "description": "Settings lifecycle: update username, update description — with idempotency replay.", + "steps": [ + { + "name": "update username", + "method": "POST", + "path": "/api/settings/username", + "principalId": "replay-sys-settings", + "idempotencyKey": "replay-settings-username-001", + "body": { "username": "replayuser" }, + "expectedStatus": 204 + }, + { + "name": "update username (replay)", + "method": "POST", + "path": "/api/settings/username", + "principalId": "replay-sys-settings", + "idempotencyKey": "replay-settings-username-001", + "body": { "username": "replayuser" }, + "expectedStatus": 204 + }, + { + "name": "update description", + "method": "POST", + "path": "/api/settings/description", + "principalId": "replay-sys-settings", + "idempotencyKey": "replay-settings-desc-001", + "body": { "description": "replay description" }, + "expectedStatus": 204 + }, + { + "name": "update description (replay)", + "method": "POST", + "path": "/api/settings/description", + "principalId": "replay-sys-settings", + "idempotencyKey": "replay-settings-desc-001", + "body": { "description": "replay description" }, + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/tag-lifecycle.trace.json b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/tag-lifecycle.trace.json new file mode 100644 index 00000000..16f53d20 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Fixtures/tag-lifecycle.trace.json @@ -0,0 +1,65 @@ +{ + "version": 1, + "description": "Tag lifecycle: create, attach alter, detach alter, delete — with idempotency replay.", + "steps": [ + { + "name": "create alter for tagging", + "method": "POST", + "path": "/api/systems/me/alters", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-alter-create-001", + "body": { "name": "TagTestAlter" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "alterId": "alterId" } + }, + { + "name": "create tag", + "method": "POST", + "path": "/api/systems/me/tags", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-create-001", + "body": { "name": "ReplayTag" }, + "expectedStatus": 201, + "expectedReplay": false, + "captureAs": { "tagId": "tagId" } + }, + { + "name": "create tag (replay)", + "method": "POST", + "path": "/api/systems/me/tags", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-create-001", + "body": { "name": "ReplayTag" }, + "expectedStatus": 201, + "expectedReplay": true + }, + { + "name": "attach alter to tag", + "method": "POST", + "path": "/api/systems/me/tags/{tagId}/alter", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-attach-001", + "body": { "alter_id": "{alterId}" }, + "expectedStatus": 204 + }, + { + "name": "attach alter to tag (replay)", + "method": "POST", + "path": "/api/systems/me/tags/{tagId}/alter", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-attach-001", + "body": { "alter_id": "{alterId}" }, + "expectedStatus": 204 + }, + { + "name": "delete tag", + "method": "DELETE", + "path": "/api/systems/me/tags/{tagId}", + "principalId": "replay-sys-tag", + "idempotencyKey": "replay-tag-delete-001", + "body": {}, + "expectedStatus": 204 + } + ] +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Interfold.IntegrationTests.Shared.csproj b/tests/integration/Interfold.IntegrationTests.Shared/Interfold.IntegrationTests.Shared.csproj new file mode 100644 index 00000000..01cc33eb --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Interfold.IntegrationTests.Shared.csproj @@ -0,0 +1,55 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AppHost + + + + + + + PreserveNewest + + + diff --git a/tests/integration/Interfold.IntegrationTests.Shared/Models/ReplayTrace.cs b/tests/integration/Interfold.IntegrationTests.Shared/Models/ReplayTrace.cs new file mode 100644 index 00000000..62569c5d --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/Models/ReplayTrace.cs @@ -0,0 +1,112 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.IntegrationTests.Shared.Models; + +/// +/// A replay trace is a JSON file containing a sequence of s. +/// The test runner executes each step against a live API and asserts the declared outcome. +/// Format version: 1. +/// +public sealed class ReplayTrace +{ + [JsonPropertyName("version")] + public int Version { get; init; } = 1; + + [JsonPropertyName("description")] + public string Description { get; init; } = string.Empty; + + [JsonPropertyName("steps")] + public List Steps { get; init; } = []; + + private static readonly JsonSerializerOptions JsonOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + + public static ReplayTrace Load(string path) + { + var json = File.ReadAllText(path); + return JsonSerializer.Deserialize(json, JsonOptions) + ?? throw new InvalidOperationException($"Failed to deserialize trace at {path}"); + } + + public static IEnumerable DiscoverFixtures() + { + var fixtureDir = Path.Combine( + AppContext.BaseDirectory, "Fixtures"); + + if (!Directory.Exists(fixtureDir)) + yield break; + + foreach (var file in Directory.EnumerateFiles(fixtureDir, "*.trace.json", SearchOption.TopDirectoryOnly)) + yield return file; + } +} + +/// +/// A single operation in the replay trace. +/// +public sealed class ReplayStep +{ + /// Human-readable step name used in test output. + [JsonPropertyName("name")] + public string Name { get; init; } = string.Empty; + + /// HTTP method, e.g. POST. + [JsonPropertyName("method")] + public string Method { get; init; } = "POST"; + + /// + /// Request path. Supports {stepVar} substitution from . + /// Example: /api/systems/me/alters/{alterId} + /// + [JsonPropertyName("path")] + public string Path { get; init; } = string.Empty; + + /// + /// Principal identity sent + /// + [JsonPropertyName("principalId")] + public string PrincipalId { get; init; } = string.Empty; + + /// Idempotency key. Null means generate a fresh key each run. + [JsonPropertyName("idempotencyKey")] + public string? IdempotencyKey { get; init; } + + /// JSON body to send with the request. + [JsonPropertyName("body")] + public JsonElement? Body { get; init; } + + /// Expected HTTP status code. + [JsonPropertyName("expectedStatus")] + public int ExpectedStatus { get; init; } = 200; + + /// + /// When non-null, asserts the replay field in the response body. + /// + [JsonPropertyName("expectedReplay")] + public bool? ExpectedReplay { get; init; } + + /// + /// When non-null, asserts that the response body contains this substring. + /// + [JsonPropertyName("expectedJsonContains")] + public string? ExpectedJsonContains { get; init; } + + /// + /// When non-null, asserts that the response body does not contain this substring. + /// + [JsonPropertyName("expectedJsonNotContains")] + public string? ExpectedJsonNotContains { get; init; } + + /// + /// When non-null, captures the named field from the JSON response into a step + /// variable that can be referenced via {varName} in subsequent step paths. + /// Example: { "alterId": "alterId" } + /// + [JsonPropertyName("captureAs")] + public Dictionary? CaptureAs { get; init; } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/ReplayParityTests.cs b/tests/integration/Interfold.IntegrationTests.Shared/ReplayParityTests.cs new file mode 100644 index 00000000..1bc78e36 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/ReplayParityTests.cs @@ -0,0 +1,436 @@ +using System.Text; +using System.Text.Json; +using Interfold.IntegrationTests.Shared.Models; +using Interfold.IntegrationTests.Shared.TestServices; + +namespace Interfold.IntegrationTests.Shared; + +/// +/// Deterministic replay parity tests (Phase N, Scope 1). +/// +/// Each *.trace.json fixture under Fixtures/ is executed against a live +/// in-memory API process. Every step asserts HTTP status and (where declared) the +/// replay flag in the response body. +/// +/// Gated on OCTOCON_RUN_API_INTEGRATION=true. +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class ReplayParityTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + public static IEnumerable GetReplayFiles() + { + yield return "alter-lifecycle.trace.json"; + yield return "tag-lifecycle.trace.json"; + yield return "fronting-lifecycle.trace.json"; + yield return "fronting-delete-parity.trace.json"; + yield return "poll-lifecycle.trace.json"; + yield return "settings-lifecycle.trace.json"; + yield return "journal-lifecycle.trace.json"; + yield return "friendship-lifecycle.trace.json"; + yield return "fronting-visibility.trace.json"; + } + + [Test] + [CombinedDataSources] + public async Task Replay_PassesAllSteps([MethodDataSource(typeof(ReplayParityTests), nameof(GetReplayFiles))] string fixtureFileName) + { + await RunTraceAsync(fixture.Factory, fixtureFileName); + } + + // ----------------------------------------------------------------------- + // Core runner + // ----------------------------------------------------------------------- + + private static async Task RunTraceAsync(InterfoldWebApplicationFactory factory, string fixtureFileName) + { + var fixturePath = Path.Combine(AppContext.BaseDirectory, "Fixtures", fixtureFileName); + + var trace = ReplayTrace.Load(fixturePath); + using (Assert.Multiple()) + { + await Assert.That(File.Exists(fixturePath)).IsTrue(); + await Assert.That(trace.Steps.Count > 0).IsTrue(); + } + + using var client = TestClient.NoRedirect(factory); + + // Build a per-invocation identity namespace so the same trace fixture can run + // independently across the InMemory / Scylla / Cassandra factory variants AND + // across concurrent leaf integration projects. PostgresIdempotencyStore is + // registered for every db-backed run via PostgresServiceCollectionExtensions, + // and under the centralised test-bench (see TestBenchCoordinator) that store's + // Postgres instance is shared across every project. Without a per-invocation + // discriminator, the second consumer of any given trace — variant or project — + // would replay the first's outcome and receive an AlterId/EntryId that its own + // CQL backend never wrote, making the controller's read-back-after-create + // return null and surface as `unknown_error` 500. The 8-hex nonce is bounded + // so the composed principal stays inside SettingsUsernameRequest's validator. + var principalSuffix = $"{SanitizeIdentitySuffix(factory.DisplayName)}-{Guid.NewGuid().ToString("N")[..8]}"; + var principalMap = trace.Steps + .Select(s => s.PrincipalId) + .Where(p => !string.IsNullOrWhiteSpace(p)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToDictionary(p => p, p => $"{p}-{principalSuffix}", StringComparer.OrdinalIgnoreCase); + + // Seed all principals referenced in the trace so Scylla/Cassandra + // backends have the user rows before operations execute. + foreach (var principal in principalMap.Values) + { + await EnsureUserExistsAsync(client, principal); + } + + // Mutable variable store for {varName} substitutions across steps. + var vars = new Dictionary(StringComparer.OrdinalIgnoreCase); + + foreach (var step in trace.Steps) + { + await ExecuteStepAsync(client, step, vars, principalMap, principalSuffix, fixtureFileName); + } + } + + /// + /// Lowercases the factory's DisplayName and replaces anything outside [a-z0-9-] with '-' so the + /// suffix stays acceptable as a username (POST /api/settings/username has its own validation + /// rules) and stays embeddable in URL path segments without further escaping. + /// + private static string SanitizeIdentitySuffix(string displayName) + { + var lowered = displayName.ToLowerInvariant(); + var buffer = new StringBuilder(lowered.Length); + foreach (var ch in lowered) + buffer.Append(char.IsAsciiLetterOrDigit(ch) || ch == '-' ? ch : '-'); + var result = buffer.ToString().Trim('-'); + return string.IsNullOrEmpty(result) ? "fixture" : result; + } + + private static async Task ExecuteStepAsync( + HttpClient client, + ReplayStep step, + Dictionary vars, + IReadOnlyDictionary principalMap, + string principalSuffix, + string fixtureFileName) + { + var path = ApplyPrincipalRewrites(SubstituteVars(step.Path, vars), principalMap); + + using var request = new HttpRequestMessage( + new HttpMethod(step.Method.ToUpperInvariant()), path); + + + if (!string.IsNullOrWhiteSpace(step.IdempotencyKey)) + { + // Suffix idempotency keys with the same fixture-namespace tag the principal IDs + // get. The Postgres idempotency store keys on (PrincipalId, OperationId, + // IdempotencyKey); the principal already carries the suffix here, but keying the + // idempotency value too keeps logs/diagnostics aligned and protects against any + // future store implementation that ignores PrincipalId in its uniqueness key. + request.Headers.Add("X-Interfold-Idempotency-Key", $"{step.IdempotencyKey}-{principalSuffix}"); + } + + if (step.Body.HasValue) + { + var substitutedBody = SubstituteBodyValue(step.Body.Value, vars, principalMap); + var bodyJson = JsonSerializer.Serialize(substitutedBody); + request.Content = new StringContent(bodyJson, Encoding.UTF8, "application/json"); + } + + if (!string.IsNullOrWhiteSpace(step.PrincipalId) + && principalMap.TryGetValue(step.PrincipalId, out var rewrittenPrincipal)) + { + AttachPrincipalAuth(request, client, rewrittenPrincipal); + } + + var response = await client.SendAsync(request); + var body = await response.Content.ReadAsStringAsync(); + + var requestBody = request.Content is null ? string.Empty : await request.Content.ReadAsStringAsync(); + await Assert.That((int)response.StatusCode).IsEqualTo(step.ExpectedStatus) + .Because($"[{fixtureFileName}] Step '{step.Name}' ({step.Method.ToUpperInvariant()} {path}) expected {step.ExpectedStatus}, got {(int)response.StatusCode}. Request body: {requestBody}. Response body: {body}"); + + // Assert replay flag when declared (only when the response has a body). + // Traces drive arbitrary endpoints so we still parse the raw envelope here rather + // than deserialise to any single typed contract — the trace format is + // wire-shape-first by design. + if (step.ExpectedReplay.HasValue && !string.IsNullOrEmpty(body)) + { + using var replayDoc = JsonDocument.Parse(body); + var actualReplay = + replayDoc.RootElement.ValueKind == JsonValueKind.Object + && replayDoc.RootElement.TryGetProperty("replay", out var replayProp) + && (replayProp.ValueKind == JsonValueKind.True + || replayProp.ValueKind == JsonValueKind.False) + && replayProp.GetBoolean(); + + await Assert.That(actualReplay == step.ExpectedReplay.Value).IsTrue(); + } + + if (!string.IsNullOrEmpty(step.ExpectedJsonContains)) + { + await Assert.That(body).Contains(step.ExpectedJsonContains) + .Because($"[{fixtureFileName}] Step '{step.Name}': expected body to contain '{step.ExpectedJsonContains}'"); + } + + if (!string.IsNullOrEmpty(step.ExpectedJsonNotContains)) + { + await Assert.That(body).DoesNotContain(step.ExpectedJsonNotContains) + .Because($"[{fixtureFileName}] Step '{step.Name}': expected body to NOT contain '{step.ExpectedJsonNotContains}'"); + } + + // Capture fields for subsequent steps. + if (step.CaptureAs is { Count: > 0 } captures && response.IsSuccessStatusCode) + { + using var doc = JsonDocument.Parse(body); + foreach (var (varName, jsonPath) in captures) + { + if (TryCaptureValue(doc.RootElement, jsonPath, out var capturedValue)) + { + vars[varName] = capturedValue; + } + else + { + Assert.Fail($"[{fixtureFileName}] Step '{step.Name}': could not capture '{jsonPath}' from body: {body}"); + } + } + } + } + + // ----------------------------------------------------------------------- + // JSON helpers + // ----------------------------------------------------------------------- + + private static string SubstituteVars(string template, Dictionary vars) + { + foreach (var (key, value) in vars) + template = template.Replace($"{{{key}}}", value, StringComparison.OrdinalIgnoreCase); + + return template; + } + + /// + /// Rewrites every raw principal id present in with its suffixed + /// counterpart from . Used for path segments like + /// /api/friend-requests/replay-sys-friend-b where the path identifies a *different* + /// principal than the one issuing the request — that target principal's id needs the same + /// fixture suffix that applied to the issuing principal, + /// otherwise the API would resolve a non-existent user and the trace would diverge. + /// + private static string ApplyPrincipalRewrites(string text, IReadOnlyDictionary principalMap) + { + // Rewrite longest principal ids first so that prefix-overlapping ids ("replay-sys-friend" + // vs "replay-sys-friend-b") never get partially rewritten. The trace fixtures don't + // currently rely on this, but the discipline keeps future fixtures safe. + foreach (var (raw, rewritten) in principalMap.OrderByDescending(p => p.Key.Length)) + text = text.Replace(raw, rewritten, StringComparison.Ordinal); + + return text; + } + + private static object? SubstituteBodyValue( + JsonElement element, + Dictionary vars, + IReadOnlyDictionary principalMap) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + { + var dict = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var property in element.EnumerateObject()) + { + dict[property.Name] = SubstituteBodyValue(property.Value, vars, principalMap); + } + + return dict; + } + + case JsonValueKind.Array: + { + var list = new List(); + foreach (var item in element.EnumerateArray()) + { + list.Add(SubstituteBodyValue(item, vars, principalMap)); + } + + return list; + } + + case JsonValueKind.String: + { + var value = element.GetString() ?? string.Empty; + if (TryResolvePlaceholderValue(value, vars, out var resolved)) + return resolved; + + return ApplyPrincipalRewrites(SubstituteVars(value, vars), principalMap); + } + + case JsonValueKind.Number: + if (element.TryGetInt32(out var intValue)) return intValue; + if (element.TryGetInt64(out var longValue)) return longValue; + if (element.TryGetDecimal(out var decimalValue)) return decimalValue; + return element.GetDouble(); + + case JsonValueKind.True: + return true; + + case JsonValueKind.False: + return false; + + case JsonValueKind.Null: + case JsonValueKind.Undefined: + return null; + + default: + return element.GetRawText(); + } + } + + private static bool TryResolvePlaceholderValue( + string value, + Dictionary vars, + out object? resolved) + { + resolved = null; + + if (value.Length < 3 || value[0] != '{' || value[^1] != '}') + return false; + + var variableName = value[1..^1]; + if (!vars.TryGetValue(variableName, out var variableValue)) + return false; + + if (int.TryParse(variableValue, out var intValue)) + { + resolved = intValue; + return true; + } + + if (long.TryParse(variableValue, out var longValue)) + { + resolved = longValue; + return true; + } + + if (decimal.TryParse(variableValue, out var decimalValue)) + { + resolved = decimalValue; + return true; + } + + if (bool.TryParse(variableValue, out var boolValue)) + { + resolved = boolValue; + return true; + } + + resolved = variableValue; + return true; + } + + private static bool TryCaptureValue(JsonElement root, string jsonPath, out string value) + { + foreach (var candidate in GetCaptureCandidates(jsonPath)) + { + if (TryReadStringField(root, candidate, out value)) + return true; + + if (TryReadIntField(root, candidate, out var intValue)) + { + value = intValue.ToString(); + return true; + } + } + + value = string.Empty; + return false; + } + + private static IEnumerable GetCaptureCandidates(string jsonPath) + { + yield return jsonPath; + + if (jsonPath.Equals("frontId", StringComparison.OrdinalIgnoreCase)) + { + yield return "front_id"; + } + + if (jsonPath.Equals("alterId", StringComparison.OrdinalIgnoreCase) + || jsonPath.Equals("entryId", StringComparison.OrdinalIgnoreCase) + || jsonPath.Equals("pollId", StringComparison.OrdinalIgnoreCase) + || jsonPath.Equals("tagId", StringComparison.OrdinalIgnoreCase) + || jsonPath.Equals("frontId", StringComparison.OrdinalIgnoreCase)) + { + // Backward compatibility for fixtures that still request legacy create keys. + yield return "id"; + } + } + + private static bool TryReadStringField(JsonElement root, string name, out string value) + { + foreach (var prop in root.EnumerateObject()) + { + if (prop.Name.Equals(name, StringComparison.OrdinalIgnoreCase) + && prop.Value.ValueKind == JsonValueKind.String) + { + value = prop.Value.GetString()!; + return true; + } + } + + // Also search inside a top-level "data" object (201 create responses use {data:{...}, replay:false}). + foreach (var prop in root.EnumerateObject()) + { + if (prop.Name.Equals("data", StringComparison.OrdinalIgnoreCase) + && prop.Value.ValueKind == JsonValueKind.Object) + { + foreach (var child in prop.Value.EnumerateObject()) + { + if (child.Name.Equals(name, StringComparison.OrdinalIgnoreCase) + && child.Value.ValueKind == JsonValueKind.String) + { + value = child.Value.GetString()!; + return true; + } + } + } + } + + value = string.Empty; + return false; + } + + private static bool TryReadIntField(JsonElement root, string name, out int value) + { + foreach (var prop in root.EnumerateObject()) + { + if (prop.Name.Equals(name, StringComparison.OrdinalIgnoreCase) + && prop.Value.ValueKind == JsonValueKind.Number + && prop.Value.TryGetInt32(out value)) + { + return true; + } + } + + // Also search inside a top-level "data" object. + foreach (var prop in root.EnumerateObject()) + { + if (prop.Name.Equals("data", StringComparison.OrdinalIgnoreCase) + && prop.Value.ValueKind == JsonValueKind.Object) + { + foreach (var child in prop.Value.EnumerateObject()) + { + if (child.Name.Equals(name, StringComparison.OrdinalIgnoreCase) + && child.Value.ValueKind == JsonValueKind.Number + && child.Value.TryGetInt32(out value)) + { + return true; + } + } + } + } + + value = 0; + return false; + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireContainerCleanup.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireContainerCleanup.cs new file mode 100644 index 00000000..67fb7cf3 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireContainerCleanup.cs @@ -0,0 +1,146 @@ +using System.Diagnostics; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Bounded Docker teardown for Aspire test containers. Resolves runtime container names via the +/// aspire-resource-name label (same discovery contract as +/// Interfold.AppHost.DockerExecCqlProbe) and issues docker stop -t before the +/// Aspire host's implicit dispose so slow Scylla graceful shutdown cannot stall the test process. +/// +public static class AspireContainerCleanup +{ + private static readonly TimeSpan StopTimeout = TimeSpan.FromSeconds(5); + private static readonly TimeSpan CommandTimeout = TimeSpan.FromSeconds(30); + + public static async Task StopResourcesAsync(IEnumerable resourceNames, CancellationToken cancellationToken = default) + { + foreach (var resourceName in resourceNames) + { + cancellationToken.ThrowIfCancellationRequested(); + await StopResourceAsync(resourceName, cancellationToken).ConfigureAwait(false); + } + } + + public static async Task CountRunningAspireContainersAsync(CancellationToken cancellationToken = default) + { + var (exit, stdout, _) = await RunDockerAsync( + ["ps", "--filter", "label=aspire-resource-name", "--format", "{{.Names}}"], + cancellationToken).ConfigureAwait(false); + + if (exit != 0 || string.IsNullOrWhiteSpace(stdout)) + return 0; + + return stdout + .Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries) + .Count(line => !string.IsNullOrWhiteSpace(line)); + } + + public static string[] SharedDbResourceNames() + { + var names = new List { ComposeServices.Postgres }; + if (RequiredFixtures.NeedScylla) + names.Add(ComposeServices.ScyllaSingle); + if (RequiredFixtures.NeedCassandra) + names.Add(ComposeServices.Cassandra); + return names.ToArray(); + } + + public static string[] MultiNodeScyllaResourceNames() + => ComposeServices.ScyllaRegionalNodes; + + private static async Task StopResourceAsync(string resourceName, CancellationToken cancellationToken) + { + var (containerName, _) = await ResolveContainerNameAsync(resourceName, cancellationToken).ConfigureAwait(false); + if (containerName is null) + return; + + await RunDockerAsync( + ["stop", "-t", StopTimeout.TotalSeconds.ToString("0"), containerName], + cancellationToken).ConfigureAwait(false); + } + + private static async Task<(string? ContainerName, string? Error)> ResolveContainerNameAsync( + string resourceName, + CancellationToken cancellationToken) + { + var labelMatch = await RunDockerAsync( + ["ps", "--filter", $"label=aspire-resource-name={resourceName}", "--format", "{{.Names}}"], + cancellationToken).ConfigureAwait(false); + + if (labelMatch.Exit == 0 && !string.IsNullOrWhiteSpace(labelMatch.Stdout)) + { + var first = labelMatch.Stdout.Trim() + .Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries) + .FirstOrDefault(); + if (!string.IsNullOrWhiteSpace(first)) + return (first, null); + } + + var prefixMatch = await RunDockerAsync( + ["ps", "--filter", $"name=^{resourceName}-", "--format", "{{.Names}}"], + cancellationToken).ConfigureAwait(false); + + if (prefixMatch.Exit == 0 && !string.IsNullOrWhiteSpace(prefixMatch.Stdout)) + { + var first = prefixMatch.Stdout.Trim() + .Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries) + .FirstOrDefault(); + if (!string.IsNullOrWhiteSpace(first)) + return (first, null); + } + + return (null, $"no running container matches resource '{resourceName}'"); + } + + private static async Task<(int Exit, string Stdout, string Stderr)> RunDockerAsync( + IEnumerable args, + CancellationToken cancellationToken) + { + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + foreach (var arg in args) + psi.ArgumentList.Add(arg); + + using var commandCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + commandCts.CancelAfter(CommandTimeout); + + Process? proc = null; + try + { + proc = Process.Start(psi); + if (proc is null) + return (-1, string.Empty, "failed to spawn docker"); + + var stdoutTask = proc.StandardOutput.ReadToEndAsync(commandCts.Token); + var stderrTask = proc.StandardError.ReadToEndAsync(commandCts.Token); + await proc.WaitForExitAsync(commandCts.Token).ConfigureAwait(false); + return (proc.ExitCode, await stdoutTask.ConfigureAwait(false), await stderrTask.ConfigureAwait(false)); + } + catch (OperationCanceledException) when (commandCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) + { + TryKill(proc); + return (-1, string.Empty, $"docker command timed out after {CommandTimeout.TotalSeconds:0}s"); + } + catch (Exception ex) + { + TryKill(proc); + return (-1, string.Empty, $"{ex.GetType().Name}: {ex.Message}"); + } + } + + private static void TryKill(Process? proc) + { + if (proc is null || proc.HasExited) + return; + + try { proc.Kill(entireProcessTree: true); } catch { /* best-effort */ } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireProcessEndpoints.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireProcessEndpoints.cs new file mode 100644 index 00000000..11341308 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/AspireProcessEndpoints.cs @@ -0,0 +1,47 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Fixed Aspire resource-service / OTLP / Kestrel endpoints for concurrent AppHost +/// processes. launchSettings.json pins 22223/21246 for interactive +/// aspire run; every automated host must claim a distinct triple so parallel test +/// assemblies (bench launcher + Infrastructure legacy fixture + MultiNode) never collide. +public static class AspireProcessEndpoints +{ + public const string BenchResourceService = "https://127.0.0.1:23223"; + public const string BenchOtlp = "https://127.0.0.1:23246"; + public const string BenchAppUrls = "http://127.0.0.1:23277"; + + public const string LegacySharedDbResourceService = "https://127.0.0.1:24223"; + public const string LegacySharedDbOtlp = "https://127.0.0.1:24246"; + public const string LegacySharedDbAppUrls = "http://127.0.0.1:24277"; + + public const string MultiNodeResourceService = "https://127.0.0.1:25223"; + public const string MultiNodeOtlp = "https://127.0.0.1:25246"; + public const string MultiNodeAppUrls = "http://127.0.0.1:25277"; + + /// Legacy host ports — isolated from the shared + /// test-bench defaults (14200/19042/19043) so Infrastructure's + /// opted-out Aspire host can run beside the bench without Docker bind conflicts. + public const int LegacyPostgresPort = 24200; + public const int LegacyScyllaPort = 29042; + public const int LegacyCassandraPort = 29043; + + public static void ApplyTo(System.Diagnostics.ProcessStartInfo psi, string resourceService, string otlp, string appUrls) + { + // `--no-launch-profile` skips launchSettings' https applicationUrl; Aspire refuses + // plain http unless this escape hatch is set (aka.ms/aspire/allowunsecuredtransport). + psi.Environment["ASPIRE_ALLOW_UNSECURED_TRANSPORT"] = "true"; + psi.Environment["ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL"] = resourceService; + psi.Environment["ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL"] = otlp; + psi.Environment["ASPNETCORE_URLS"] = appUrls; + psi.Environment["DOTNET_URLS"] = appUrls; + } + + public static void ApplyToCurrentProcess(string resourceService, string otlp, string appUrls) + { + Environment.SetEnvironmentVariable("ASPIRE_ALLOW_UNSECURED_TRANSPORT", "true"); + Environment.SetEnvironmentVariable("ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL", resourceService); + Environment.SetEnvironmentVariable("ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL", otlp); + Environment.SetEnvironmentVariable("ASPNETCORE_URLS", appUrls); + Environment.SetEnvironmentVariable("DOTNET_URLS", appUrls); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/CassandraWebFactoryFixture.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/CassandraWebFactoryFixture.cs new file mode 100644 index 00000000..6ab492af --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/CassandraWebFactoryFixture.cs @@ -0,0 +1,54 @@ +using Interfold.Shared.Contracts; +using TUnit.Core.Interfaces; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Fixture chain that creates an backed by the +/// shared Cassandra 5 cluster managed by . TUnit resolves the +/// dependency automatically via . +/// +/// +/// Because this fixture references , the +/// hook flips +/// on for the session, which in turn flips the +/// include-cassandra Aspire parameter on. The CassandraPort property is +/// therefore always populated when this fixture's runs. +/// +public sealed class CassandraWebFactoryFixture : IWebFactoryFixture, IAsyncInitializer +{ + [ClassDataSource(Shared = SharedType.PerTestSession)] + public required SharedDbFixture Aspire { get; init; } + + public InterfoldWebApplicationFactory Factory { get; private set; } = null!; + + public Task InitializeAsync() + { + // See ScyllaWebFactoryFixture for the rationale: SharedDbFixture captures per-backend + // failures into CassandraInitException so that a wedged Cassandra container only + // breaks Cassandra-backed tests, not the entire DB-bound test cohort. + if (Aspire.CassandraInitException is { } cassandraFailure) + { + throw new InvalidOperationException( + "CassandraWebFactoryFixture cannot start because the shared Cassandra backend " + + "failed to initialise. See the inner exception for the underlying cause.", + cassandraFailure); + } + + Factory = CreatePrivateFactory(); + return Task.CompletedTask; + } + + /// + // Same wiring as InitializeAsync so a private factory exercises the identical Cassandra + + // Postgres path. CassandraPort is guaranteed non-null by InitializeAsync's guard above — + // this method is only reached after the fixture has passed that check. + public InterfoldWebApplicationFactory CreatePrivateFactory() + => new InterfoldWebApplicationFactory(PersistenceMode.ScyllaPostgres, "cassandra") + .WithConfiguration("OCTOCON_POSTGRES_CONNECTION", Aspire.PostgresConnectionString) + .WithConfiguration("OCTOCON_SCYLLA_PORT", Aspire.CassandraPort!.Value.ToString()) + .WithConfiguration("OCTOCON_SINGLE_SCYLLA_INSTANCE", "true") + .WithConfiguration("OCTOCON_DB_RETRY_ATTEMPTS", "10") + .WithConfiguration("OCTOCON_DB_RETRY_INITIAL_DELAY_MS", "500") + .WithConfiguration("OCTOCON_DB_RETRY_MAX_DELAY_MS", "3000"); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DbInitHelper.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DbInitHelper.cs new file mode 100644 index 00000000..fc2a496f --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DbInitHelper.cs @@ -0,0 +1,56 @@ +using Interfold.DatabaseBootstrap; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Thin test-side convenience shim around plus +/// the shared seeders. Holds test-only constants (default DB name, cassandra default creds) +/// so fixtures can call one line per phase without repeating the wait+seed plumbing. +public static class DbInitHelper +{ + /// Hard-coded by msg-db's POSTGRES_USER in the AppHost. + public const string PostgresInitUser = PostgresRoles.Init; + + /// Deliberately non-default name so a regressed hardcoded literal fails a test. + public const string DefaultPostgresDb = "test_pg_db"; + + /// Scylla / Cassandra built-in superuser before lockdown. + public const string ScyllaDefaultUser = InProcessSeedWaits.ScyllaDefaultUser; + + /// Default password for . + public const string ScyllaDefaultPassword = InProcessSeedWaits.ScyllaDefaultPassword; + + /// Delegates to . + public static Task WaitForPostgresAsync( + string initConnectionString, + PostgresReadinessOptions options, + CancellationToken ct) + => InProcessSeedWaits.WaitForPostgresAsync(initConnectionString, options, ct); + + /// Seeds via ; base string only needs host/port, + /// the orchestrator rewrites user/password/database per call. + public static async Task SeedPostgresAsync( + string baseConnectionString, + PostgresSeedOptions options, + CancellationToken ct) + { + var executor = new NpgsqlPostgresExecutor(baseConnectionString); + await PostgresSeeder.BootstrapAsync(executor, options, NoOpDatabaseInitLogger.Instance, ct) + .ConfigureAwait(false); + } + + /// Delegates to . + public static Task WaitForScyllaAsync(string host, int port, CancellationToken ct) + => InProcessSeedWaits.WaitForScyllaAsync(host, port, ct); + + /// Seeds via with the in-process DataStax executor. + public static async Task SeedScyllaAsync( + string host, int port, + ScyllaSeedOptions options, + CancellationToken ct) + { + var executor = new DataStaxScyllaExecutor(host, port); + await ScyllaSeeder.BootstrapAsync(executor, options, NoOpDatabaseInitLogger.Instance, ct) + .ConfigureAwait(false); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerDaemonAvailability.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerDaemonAvailability.cs new file mode 100644 index 00000000..8723c3d4 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerDaemonAvailability.cs @@ -0,0 +1,22 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Classifies docker CLI failures caused by a missing/down daemon vs. container +/// runtime errors. Used so preflight helpers report "start Docker Desktop" instead of +/// unrelated remediation (AIO sysctl, memory knobs, etc.). +public static class DockerDaemonAvailability +{ + public static bool LooksUnavailable(string text) + { + if (string.IsNullOrWhiteSpace(text)) + return false; + + return text.Contains("failed to connect to the docker API", StringComparison.OrdinalIgnoreCase) + || text.Contains("dockerDesktopLinuxEngine", StringComparison.OrdinalIgnoreCase) + || text.Contains("Cannot connect to the Docker daemon", StringComparison.OrdinalIgnoreCase) + || text.Contains("Is the docker daemon running", StringComparison.OrdinalIgnoreCase) + || text.Contains("error during connect", StringComparison.OrdinalIgnoreCase) + || text.Contains("npipe:////./pipe/", StringComparison.OrdinalIgnoreCase) + || (text.Contains("unix:///var/run/docker.sock", StringComparison.OrdinalIgnoreCase) + && text.Contains("connect", StringComparison.OrdinalIgnoreCase)); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerMemoryPreflight.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerMemoryPreflight.cs new file mode 100644 index 00000000..f3486082 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/DockerMemoryPreflight.cs @@ -0,0 +1,86 @@ +using System.Diagnostics; +using System.Globalization; +using System.Text.RegularExpressions; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Preflight for Docker Desktop / daemon memory before spinning Scylla + Cassandra. +/// Parallel integration runs (shared test-bench + Infrastructure's opted-out Aspire host) +/// need roughly 8 GiB; below that Scylla typically enters FailedToStart and the suite hangs +/// or cascades. +public static class DockerMemoryPreflight +{ + /// Soft floor for a full parallel integration matrix (GiB). + public const double RecommendedGiB = 8.0; + + /// Hard floor — below this we refuse to launch rather than burn a 10-minute timeout. + public const double MinimumGiB = 5.5; + + private static readonly Regex TotalMemoryRegex = new( + @"Total Memory:\s*(?[\d.]+)\s*(?GiB|MiB|KiB|B)", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + /// Reads docker info total memory. Returns null when docker is unavailable + /// or the field can't be parsed (caller should proceed — absence of docker is a different failure). + public static async Task TryGetTotalMemoryGiBAsync(CancellationToken ct) + { + try + { + var psi = new ProcessStartInfo("docker") + { + ArgumentList = { "info" }, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + using var proc = Process.Start(psi); + if (proc is null) return null; + var stdout = await proc.StandardOutput.ReadToEndAsync(ct).ConfigureAwait(false); + await proc.WaitForExitAsync(ct).ConfigureAwait(false); + if (proc.ExitCode != 0) return null; + + var match = TotalMemoryRegex.Match(stdout); + if (!match.Success) return null; + if (!double.TryParse(match.Groups["value"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) + return null; + + return match.Groups["unit"].Value.ToLowerInvariant() switch + { + "gib" => value, + "mib" => value / 1024.0, + "kib" => value / (1024.0 * 1024.0), + "b" => value / (1024.0 * 1024.0 * 1024.0), + _ => null, + }; + } + catch + { + return null; + } + } + + /// Throws when Docker reports less than ; logs a warning + /// when below . + public static async Task EnsureAdequateAsync(CancellationToken ct) + { + var gib = await TryGetTotalMemoryGiBAsync(ct).ConfigureAwait(false); + if (gib is null) return; + + if (gib.Value + 0.01 < MinimumGiB) + { + throw new InvalidOperationException( + $"Docker reports only {gib.Value:0.##} GiB total memory; parallel Interfold " + + $"integration tests need at least {MinimumGiB:0.#} GiB (recommend {RecommendedGiB:0.#}+ GiB). " + + "Raise Docker Desktop → Settings → Resources → Memory, then re-run. " + + "With too little RAM Scylla enters FailedToStart while Postgres/Cassandra come up."); + } + + if (gib.Value + 0.01 < RecommendedGiB) + { + Console.WriteLine( + $"[docker-memory] warning: Docker has {gib.Value:0.##} GiB; recommend {RecommendedGiB:0.#}+ GiB " + + "when the shared test-bench and Infrastructure's Aspire host both start Scylla."); + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendRequestFlow.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendRequestFlow.cs new file mode 100644 index 00000000..d58b6f1d --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendRequestFlow.cs @@ -0,0 +1,44 @@ +using System.Net.WebSockets; +using Interfold.Socket.Contracts; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Wire-level drivers for the friend-request Phoenix endpoint. The five WebSocket integration +/// tests that exercise the send/accept/reject/cancel/remove flows all begin with the identical +/// four-line preamble — build the PUT /api/friend-requests/{recipient} endpoint frame, +/// push it on the sender socket, drain the sender ack + push, then wait for the recipient push. +/// starts the same way before it forks into the trust / +/// untrust wire drive. Consolidating the preamble here means every caller stays byte-identical +/// with production behaviour even if the frame shape changes. +/// +public static class FriendRequestFlow +{ + /// + /// Drives the "send friend request → drain sender ack + push → drain recipient push" + /// sequence from a pair of already-joined sockets. Returns the drained frames so the caller + /// keeps ownership of the scenario-specific assertions — some tests only need to confirm the + /// recipient event landed, others also inspect the sender's ack / push pair (or annotate the + /// assertion with a contextual suffix). + /// + public static async Task<(ReceivedPhxFrame? SenderAck, ReceivedPhxFrame? SenderPush, ReceivedPhxFrame? RecipientEvent)> SendAndDrainAsync( + WebSocket senderWs, + string senderSystemId, + WebSocket recipientWs, + string recipientSystemId, + string refId, + CancellationToken token, + int recipientMaxFrames = 3) + { + var frame = PhxEndpointFrame.Build( + "system:" + senderSystemId, + "PUT", + "/api/friend-requests/" + recipientSystemId, + new object(), + refId); + var (ack, push) = await senderWs.SendEndpointAndCaptureAsync(frame, SocketEventNames.Friendships.RequestSent, token); + var recipientEvent = await ReceivedPhxFrame.ReceiveEventFrameAsync( + recipientWs, token, SocketEventNames.Friendships.RequestReceived, maxFrames: recipientMaxFrames); + return (ack, push, recipientEvent); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendTrustUntrustFlow.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendTrustUntrustFlow.cs new file mode 100644 index 00000000..90e71fb7 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/FriendTrustUntrustFlow.cs @@ -0,0 +1,106 @@ +using System.Net.WebSockets; +using Interfold.Socket.Contracts; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// End-to-end wire drive for the "send friend request → accept → trust → untrust" flow used +/// by the WebSocket integration suite. The flow originally lived open-coded in +/// WebSocketTests.Api_UserSocketEndpoint_PushesFriendTrustedAndUntrusted_ToActor; +/// WebSocketThreadStarvationTests.RunFriendTrustUntrustFlowAsync then cloned the +/// entire body verbatim (identical frames, identical assertions) purely so it could run +/// the same flow with the thread-pool throttled, distinguishing the two only by an +/// "under thread starvation" suffix on every Because(...) message. +/// +/// +/// This helper unifies both paths: it takes two already-joined sockets (single-party +/// helper for either side is fine) and +/// drives the four wire messages in sequence, asserting the expected ack/push pairs after +/// each. Callers pass when they want the assertion +/// messages to name the scenario (e.g. "under thread starvation"); the default +/// empty suffix matches the base WebSocketTests phrasing. +/// +/// +/// +/// The refs ("2"–"5") are hardcoded to preserve behavioural parity with the +/// original inline code, where every ref value already advanced sequentially past the +/// join at "1". Callers that need a different ref sequence must hand-roll +/// the wire drive. +/// +/// +public static class FriendTrustUntrustFlow +{ + public static async Task RunAsync( + WebSocket senderWs, + string senderSystemId, + WebSocket recipientWs, + string recipientSystemId, + CancellationToken token, + string? contextSuffix = null) + { + var suffix = string.IsNullOrEmpty(contextSuffix) ? "." : $" {contextSuffix}."; + + var (_, _, recipientReceived) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + await Assert.That(recipientReceived).IsNotNull() + .Because($"Expected friend_request_received before accept{suffix}"); + + var acceptFrame = PhxEndpointFrame.Build( + "system:" + recipientSystemId, + "POST", + "/api/friend-requests/" + senderSystemId + "/accept", + new object(), + "3"); + + await recipientWs.SendTextFrameAsync(acceptFrame, token); + + _ = await ReceivedPhxFrame.ReceiveEventFrameAsync(recipientWs, token, SocketEventNames.Friendships.Added); + _ = await ReceivedPhxFrame.ReceiveReplyAndPushAsync(senderWs, token, SocketEventNames.Friendships.Added); + + var trustFrame = PhxEndpointFrame.Build( + "system:" + senderSystemId, + "POST", + "/api/friends/" + recipientSystemId + "/trust", + new object(), + "4"); + + var (trustAck, trustPush) = await senderWs.SendEndpointAndCaptureAsync(trustFrame, SocketEventNames.Friendships.Trusted, token); + + using (Assert.Multiple()) + { + await Assert.That(trustAck).IsNotNull() + .Because($"Expected endpoint ack on sender socket for trust{suffix}"); + await Assert.That(trustPush).IsNotNull() + .Because($"Expected friend_trusted push on sender socket after trust{suffix}"); + } + + if (trustPush is not null) + { + await Assert.That(trustPush.Event).IsEqualTo(SocketEventNames.Friendships.Trusted) + .Because($"Expected friend_trusted event on sender socket{suffix}"); + } + + var untrustFrame = PhxEndpointFrame.Build( + "system:" + senderSystemId, + "POST", + "/api/friends/" + recipientSystemId + "/untrust", + new object(), + "5"); + + var (untrustAck, untrustPush) = await senderWs.SendEndpointAndCaptureAsync(untrustFrame, SocketEventNames.Friendships.Untrusted, token); + + using (Assert.Multiple()) + { + await Assert.That(untrustAck).IsNotNull() + .Because($"Expected endpoint ack on sender socket for untrust{suffix}"); + await Assert.That(untrustPush).IsNotNull() + .Because($"Expected friend_untrusted push on sender socket after untrust{suffix}"); + } + + if (untrustPush is not null) + { + await Assert.That(untrustPush.Event).IsEqualTo(SocketEventNames.Friendships.Untrusted) + .Because($"Expected friend_untrusted event on sender socket{suffix}"); + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/HostAioPrerequisite.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/HostAioPrerequisite.cs new file mode 100644 index 00000000..f891890f --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/HostAioPrerequisite.cs @@ -0,0 +1,215 @@ +using System.Diagnostics; +using System.Globalization; +using System.Runtime.InteropServices; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// One-shot host-kernel fs.aio-max-nr tuner used before any test fixture starts a +/// Scylla container. Mirrors the dynamic formula in +/// scripts/docker/ensure-host-aio.sh and +/// Interfold.Bootstrapper/Phases/PrerequisitesPhase.cs: per-Scylla-node Seastar AIO +/// budget × node count, plus a fixed headroom for non-Scylla AIO consumers (Postgres etc.). +/// +/// +/// +/// On a Linux host /proc/sys/fs/aio-max-nr is world-readable, so the fast path is a +/// bare file read: if the kernel is already at-or-above our minimum (GitHub Actions Linux +/// runners ship at 1,048,576 which dwarfs any node count we run), the helper container is +/// skipped entirely. This keeps CI green when Docker Hub throttles the anonymous +/// alpine:3.20 pull (see the July 2026 incident that took out the whole matrix on a +/// registry-1.docker.io context-deadline). +/// +/// +/// If the fast path reports insufficient (developer laptop with a low sysctl) or the file +/// can't be read (Docker Desktop on Mac/Windows where the sysctl lives inside the VM), we +/// fall through to a one-shot privileged Alpine container that raises the limit. This is +/// also the code path exercised by DinD bootstrapper integration tests. +/// +/// +/// Idempotent and process-static: once we've raised the limit during a session we don't +/// re-spawn the helper container for subsequent fixture inits. The helper itself is also +/// idempotent (the script never lowers the limit), so even without the cache a re-run would +/// be safe — the cache just avoids the docker-spawn cost. +/// +/// +public static class HostAioPrerequisite +{ + /// Per-Scylla-node Seastar minimum (refusing-to-start threshold) per the Seastar startup error. + private const int PerNodeMin = 66_563; + + /// Per-Scylla-node Seastar recommended budget (optimal networking performance). + private const int PerNodeRecommended = 116_562; + + /// Headroom for non-Scylla AIO consumers (Postgres, libraries, etc.). + private const int Headroom = 50_000; + + /// Image used for the privileged sysctl write. Pinned so a registry outage doesn't break tests silently. + private const string SysctlImage = "alpine:3.20"; + + private static readonly SemaphoreSlim Gate = new(1, 1); + private static int _appliedFor; + + /// + /// Number of Scylla nodes spins up when active. + /// Mirrors the regions array in InterfoldAppHost.cs; if you ever change that + /// region list, update this too — there's a small build-time cross-check via the array + /// length comparison in . + /// + private const int MultiNodeNodeCount = 7; + + /// + /// Total Scylla nodes the current test session will run concurrently across all + /// fixtures, derived from . SharedDbFixture's single-node + /// Scylla counts as 1; the multi-DC fixture adds . + /// Cassandra is intentionally excluded — it doesn't use Seastar's AIO budget. + /// + public static int TotalScyllaNodesForSession() + => (RequiredFixtures.NeedScylla ? 1 : 0) + + (RequiredFixtures.NeedMultiNodeScylla ? MultiNodeNodeCount : 0); + + /// Path to the Linux kernel sysctl we're reading. Extracted as a constant so the fast-path check is easy to grep for. + private const string AioSysctlPath = "/proc/sys/fs/aio-max-nr"; + + /// + /// Raises the host's fs.aio-max-nr to satisfy the requested Scylla node count if + /// the current value is below the minimum. A no-op when + /// is zero (Cassandra-only or InMemory-only sessions). + /// + /// Total Scylla nodes the session will run concurrently across all fixtures. + /// Cancellation token honored by the helper docker invocations. + public static async Task EnsureAsync(int scyllaNodeCount, CancellationToken ct = default) + { + if (scyllaNodeCount <= 0) + { + return; + } + + await Gate.WaitAsync(ct).ConfigureAwait(false); + try + { + // Re-application is harmless (the privileged container only ever raises the limit) + // but spawning Alpine for every fixture init in a multi-fixture session adds 1-2s + // of avoidable churn. Cache by the highest count seen so far so a session that + // first runs SharedDbFixture (1 node) then MultiNodeScyllaFixture (+7 nodes) gets + // the helper invoked twice — once at 1, then re-invoked for the larger 8-node + // requirement. + if (_appliedFor >= scyllaNodeCount) + { + return; + } + + var minRequired = scyllaNodeCount * PerNodeMin + Headroom; + var target = scyllaNodeCount * PerNodeRecommended + Headroom; + + // Linux-only fast path. GHA hosted runners default to 1,048,576 which satisfies + // any node count we run, so we can short-circuit before touching Docker Hub. This + // is what stops a Docker Hub throttle (context-deadline on registry-1.docker.io) + // from taking out every integration-test job. Docker Desktop on Mac/Windows falls + // through because /proc/sys/fs/aio-max-nr doesn't exist on those hosts. + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && TryReadCurrentLimit(out var current)) + { + if (current >= minRequired) + { + Console.Error.WriteLine( + $"[host-aio] current={current} >= min={minRequired} for {scyllaNodeCount} Scylla node(s); ok (no docker)"); + _appliedFor = scyllaNodeCount; + return; + } + } + + // Inline sh script so we don't have to bind-mount ensure-host-aio.sh from the test + // project. The logic is identical: read current, compare to min, write target if + // below. Output goes through Console.Error so the operator sees what happened + // even when the test framework swallows stdout. + var inline = + $"current=$(cat /proc/sys/fs/aio-max-nr); " + + $"if [ \"$current\" -ge {minRequired} ]; then " + + $" echo \"[host-aio] current=$current >= min={minRequired} for {scyllaNodeCount} Scylla node(s); ok\" 1>&2; " + + $"else " + + $" echo \"[host-aio] raising current=$current to {target} for {scyllaNodeCount} Scylla node(s)\" 1>&2; " + + $" echo {target} > /proc/sys/fs/aio-max-nr; " + + $"fi"; + + var args = new[] + { + "run", "--rm", "--privileged", + SysctlImage, + "sh", "-c", inline, + }; + + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var arg in args) + { + psi.ArgumentList.Add(arg); + } + + using var proc = Process.Start(psi) + ?? throw new InvalidOperationException("Failed to launch `docker run` for host AIO tuning."); + + await proc.WaitForExitAsync(ct).ConfigureAwait(false); + var stdErr = await proc.StandardError.ReadToEndAsync(ct).ConfigureAwait(false); + var stdOut = await proc.StandardOutput.ReadToEndAsync(ct).ConfigureAwait(false); + + if (proc.ExitCode != 0) + { + var err = (stdErr ?? string.Empty).Trim(); + var outText = (stdOut ?? string.Empty).Trim(); + if (DockerDaemonAvailability.LooksUnavailable(err) || DockerDaemonAvailability.LooksUnavailable(outText)) + { + throw new InvalidOperationException( + "Docker daemon is not reachable — cannot tune host AIO for Scylla. " + + "Start Docker Desktop (or the docker daemon) and re-run. " + + $"Detail: {FirstNonEmpty(err, outText)}"); + } + + throw new InvalidOperationException( + $"Host AIO prerequisite failed (docker exit={proc.ExitCode}). " + + $"Required min={minRequired} for {scyllaNodeCount} Scylla node(s). " + + $"On Linux: `sudo sysctl -w fs.aio-max-nr={target}` or " + + $"`bash scripts/docker/ensure-host-aio.sh --scylla-nodes {scyllaNodeCount}`. " + + "On Docker Desktop (Windows/Mac) the privileged helper raises the Linux VM sysctl — " + + "ensure Docker is running and file sharing/privileged containers are allowed. " + + $"stderr: {err} stdout: {outText}"); + } + + _appliedFor = scyllaNodeCount; + } + finally + { + Gate.Release(); + } + } + + private static string FirstNonEmpty(string a, string b) + => !string.IsNullOrWhiteSpace(a) ? a : b; + + private static bool TryReadCurrentLimit(out int current) + { + current = 0; + try + { + if (!File.Exists(AioSysctlPath)) + { + return false; + } + + var raw = File.ReadAllText(AioSysctlPath).Trim(); + return int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out current); + } + catch (IOException) + { + return false; + } + catch (UnauthorizedAccessException) + { + return false; + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IWebFactoryFixture.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IWebFactoryFixture.cs new file mode 100644 index 00000000..98933ce9 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IWebFactoryFixture.cs @@ -0,0 +1,48 @@ +using Interfold.Api.Host.Services.Secrets; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +public interface IWebFactoryFixture +{ + /// + /// Session-shared factory. Every non-mutating test resolves via this property so the + /// underlying (and any backend containers it + /// depends on) is amortised across the whole session. + /// + InterfoldWebApplicationFactory Factory { get; } + + /// + /// Construct a fresh, private with the exact + /// same backend wiring as , owned by the caller. Use when a test needs + /// to mutate global-scope factory configuration (WithConfiguration, + /// UseSetting, hosted-service replacements) that would otherwise poison the shared + /// factory for every subsequent test in the same session. + /// + /// + /// + /// Ownership. Callers own the returned factory and must dispose it (await using + /// in an async test method). Reuse across tests is not supported — build one per test that + /// needs isolation. + /// + /// + /// Cost. Each call performs a full host build (including + /// 's pre-Build fetch and + /// the migration-strip DI rewrite for DB-backed runs). Reserve for tests that actually need + /// isolation; do not use as a default. + /// + /// + /// Regression this closes. Before this method existed, the avatar-multipart tests + /// (AvatarSourceTests.Api_SettingsAvatarMultipart_ReportsLocalSource, + /// SettingsControllerTests.Api_SettingsAvatarMultipart_PersistsAndServesAvatar, + /// SettingsControllerTests.Api_AlterAvatarMultipart_PersistsAndReflectsOnPublicAlter) + /// wrote a relative OCTOCON_AVATAR_PUBLIC_BASE value onto the session-shared factory + /// via WithConfiguration. That value fails [AbsoluteHttpUri] validation on + /// StorageConfiguration.AvatarPublicBase, so every subsequent request that resolved + /// IOptionsMonitor<StorageConfiguration>.Get() (via InterfoldPrincipalMiddleware) + /// threw OptionsValidationException and 500'd — cascading into ~50 downstream test + /// failures that were hard to attribute to the actual cause. Isolating those three tests to + /// their own factory removes the vector entirely. + /// + /// + InterfoldWebApplicationFactory CreatePrivateFactory(); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InMemoryWebFactoryFixture.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InMemoryWebFactoryFixture.cs new file mode 100644 index 00000000..f481495b --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InMemoryWebFactoryFixture.cs @@ -0,0 +1,27 @@ +using Interfold.Shared.Contracts; +using TUnit.Core.Interfaces; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Standalone fixture (no Aspire, no Docker) that creates an in-memory WebApplicationFactory. +/// Always available — used as the baseline for all integration tests. +/// +public sealed class InMemoryWebFactoryFixture : IWebFactoryFixture, IAsyncInitializer +{ + public InterfoldWebApplicationFactory Factory { get; private set; } = null!; + + public Task InitializeAsync() + { + Factory = CreatePrivateFactory(); + return Task.CompletedTask; + } + + /// + // OCTOCON_SCYLLA_KEYSPACE defaults to "nam" via PersistenceConfiguration.ScyllaKeyspace, + // so an explicit override here is redundant. Leave the factory at production defaults — + // the private-factory shape must stay byte-identical to the session-shared one so tests + // that opt into isolation exercise the same host wiring as the rest of the suite. + public InterfoldWebApplicationFactory CreatePrivateFactory() + => new(PersistenceMode.InMemory); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InterfoldWebApplicationFactory.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InterfoldWebApplicationFactory.cs new file mode 100644 index 00000000..a62b8a3a --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/InterfoldWebApplicationFactory.cs @@ -0,0 +1,260 @@ +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Domain; +using Interfold.Infrastructure.Coordination; +using Interfold.Infrastructure.Postgres; +using Interfold.Infrastructure.Scylla; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Socket.Api.Socket; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Time.Testing; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +public class InterfoldWebApplicationFactory : WebApplicationFactory +{ + // Live-reloadable override at the top of the config root. WithConfiguration writes fire + // the reload token so IOptionsMonitor consumers see updates; values snapshotted at + // WebApplication.Build() (CORS, OAuth, IOptions, JWT registration) still need a fresh + // factory. + private readonly FactoryConfigurationProvider _configProvider = new(); + private static readonly ConditionalWeakTable ClientFactories = new(); + // Pinned instance so every service-provider root the factory constructs resolves the + // SAME bus. Under TUnit's parallel WS suite, multiple roots would otherwise resolve + // distinct InProcessEventBus instances and drop subscriptions. + public InProcessEventBus EventBus { get; } = new(); + + public FakeTimeProvider TimeProvider { get; } = new(); + public string DisplayName { get; private set; } + + /// Backend this factory was constructed for. + public PersistenceMode PersistenceMode { get; } + + /// When non-null, TestHttpClientFactory chains a recording handler that captures + /// outbound URI + Host header before dispatch. Factory is PerTestSession, so tests that + /// set this MUST be [NotInParallel] under a shared key. + public ConcurrentQueue? OutboundHttpUriRecorder { get; set; } + + public InterfoldWebApplicationFactory( + PersistenceMode persistenceMode, + string? displayName = null, + bool seedInMemorySecretsFromFactoryConfig = true) + { + PersistenceMode = persistenceMode; + var persistenceType = persistenceMode.ToWire(); + DisplayName = displayName ?? persistenceType; + _configProvider.Set("OCTOCON_PERSISTENCE", persistenceType); + + // In-memory runs drive the production env-var seed path via IConfiguration. Keys use + // the `:`-form (the post-normalisation shape EnvironmentVariablesConfigurationProvider + // produces from `OCTOCON_INMEMORY_SECRETS_SEED__*`) so SeedFromConfig looks them up. + // seedInMemorySecretsFromFactoryConfig=false is for the env-var regression test that + // must exercise the real Environment.SetEnvironmentVariable path unshadowed. + if (seedInMemorySecretsFromFactoryConfig && persistenceMode == PersistenceMode.InMemory) + { + _configProvider.Set("OCTOCON_INMEMORY_SECRETS_SEED:ENCRYPTION_PEPPER", "TEST"); + _configProvider.Set("OCTOCON_INMEMORY_SECRETS_SEED:AUTH_JWT_ES256_PRIVATE_PEM", TestDbCredentials.JwtEs256PrivateKeyPem); + _configProvider.Set("OCTOCON_INMEMORY_SECRETS_SEED:AUTH_DEEP_LINK_SECRET", TestDbCredentials.DeepLinkSecret); + _configProvider.Set("OCTOCON_INMEMORY_SECRETS_SEED:AUTH_JWT_RSA256_PRIVATE_PEM", TestDbCredentials.JwtRsa256PrivateKeyPem); + } + } + + public override string ToString() => DisplayName; + + /// Writes into the top-priority configuration layer and fires the reload token. + /// IOptionsMonitor consumers see the update; startup-snapshot consumers (CORS, OAuth, + /// IOptions, JWT registration) still need a fresh factory. + public InterfoldWebApplicationFactory WithConfiguration(string key, string? value) + { + _configProvider.Set(key, value); + return this; + } + + public new HttpClient CreateClient() + { + return TrackClient(base.CreateClient()); + } + + public new HttpClient CreateClient(WebApplicationFactoryClientOptions options) + { + return TrackClient(base.CreateClient(options)); + } + + public new HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) + { + return TrackClient(base.CreateDefaultClient(handlers)); + } + + internal static bool TryGetFactory(HttpClient client, out InterfoldWebApplicationFactory factory) + { + return ClientFactories.TryGetValue(client, out factory!); + } + + internal string CreateToken(string systemId) + { + // OptionsMonitor.CurrentValue returns the cached instance patched by SecretsPreBuildLoader + // + AuthenticationSecretsPostConfigure; IConfiguration.Get() would bypass those patches. + var authConfig = Services.GetRequiredService>().CurrentValue; + + // Plug the same PEM the fixtures seeded into internal.secrets. Safe to mutate on the + // cached instance: PostConfigure has already run and every consumer reads via this monitor. + authConfig.JwtEs256PrivateKeyPem = TestDbCredentials.JwtEs256PrivateKeyPem; + + if (string.IsNullOrWhiteSpace(authConfig.JwtAuthority)) + authConfig.JwtAuthority = "test-authority"; + + var jti = Guid.NewGuid().ToString("N"); + var now = DateTimeOffset.UtcNow; + var expiresAt = now.AddDays(1); + + // Middleware requires scoped `{region}:{rawId}` sub; Compose is idempotent, and Nam + // is the ParseScyllaKeyspace default for null/empty input. + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, systemId); + + return AuthHelper.CreateToken(authConfig, expiresAt, now, new(jti), scoped.AsSystemId()); + } + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + // Push snapshot via UseSetting so Program.cs sees values at DI-registration time + // (AddInterfoldCluster, JWT registration, etc. read IConfiguration before + // ConfigureAppConfiguration callbacks fire). + foreach (var pair in _configProvider.Snapshot()) + { + builder.UseSetting(pair.Key, pair.Value); + } + + // Also wire the provider as the last config source so runtime WithConfiguration + // writes fire the reload token; IOptionsMonitor consumers pick them up in place. + builder.ConfigureAppConfiguration((_, configBuilder) => + { + configBuilder.Add(new FactoryConfigurationSource(_configProvider)); + }); + + builder.ConfigureServices(x => + { + x.AddSingleton(this); + x.Replace(ServiceDescriptor.Singleton()); + x.Replace(ServiceDescriptor.Singleton(new SocketJoinRateLimiter(TimeProvider))); + // Pinned clock keeps idempotency hashes stable across retries. + x.Replace(ServiceDescriptor.Singleton(TimeProvider)); + // See EventBus property — same instance across every service-provider root. + x.Replace(ServiceDescriptor.Singleton(EventBus)); + x.Replace(ServiceDescriptor.Singleton(EventBus)); + + // DB-backed runs bootstrap once via SharedDbFixture.WaitForResourcesAsync; strip + // the migration hosted services so heavy DDL doesn't replay on every rebuild. + // In-memory has no migrations to strip and seeds via IConfiguration directly. + if (PersistenceMode != PersistenceMode.InMemory) + { + RemoveHostedService(x); + RemoveHostedService(x); + } + }); + + base.ConfigureWebHost(builder); + } + + /// Drops every registration whose implementation + /// type is . + private static void RemoveHostedService(IServiceCollection services) + where TService : class, IHostedService + { + for (var i = services.Count - 1; i >= 0; i--) + { + var descriptor = services[i]; + if (descriptor.ServiceType == typeof(IHostedService) && + descriptor.ImplementationType == typeof(TService)) + { + services.RemoveAt(i); + } + } + } + + private HttpClient TrackClient(HttpClient client) + { + ClientFactories.Remove(client); + ClientFactories.Add(client, this); + return client; + } + + public class TestHttpClientFactory : IHttpClientFactory + { + private readonly InterfoldWebApplicationFactory _factory; + + public TestHttpClientFactory(InterfoldWebApplicationFactory factory) + { + _factory = factory; + } + + public HttpClient CreateClient(string name) + { + // Recorder is opt-in per test; unset (default) → no handler, zero overhead. + var recorder = _factory.OutboundHttpUriRecorder; + return recorder is null + ? _factory.CreateDefaultClient() + : _factory.CreateDefaultClient(new RecordingDelegatingHandler(recorder)); + } + } + + /// Captured outbound URI + Host header (TestHost's inner Request.Host isn't reliable). + public sealed record RecordedHttpCall(Uri Uri, string? HostHeader); + + /// Records outbound URI + Host header before TestServer dispatches. + private sealed class RecordingDelegatingHandler( + ConcurrentQueue sink) : DelegatingHandler + { + protected override Task SendAsync( + HttpRequestMessage request, CancellationToken cancellationToken) + { + if (request.RequestUri is not null) + { + sink.Enqueue(new RecordedHttpCall(request.RequestUri, request.Headers.Host)); + } + return base.SendAsync(request, cancellationToken); + } + } + + /// Factory-lifetime configuration source that survives host rebuilds. + private sealed class FactoryConfigurationSource(FactoryConfigurationProvider provider) + : IConfigurationSource + { + public IConfigurationProvider Build(IConfigurationBuilder builder) => provider; + } + + /// Mutable in-memory provider that fires OnReload on every Set so IOptionsMonitor + /// consumers re-read the bound value; access is lock-guarded. + private sealed class FactoryConfigurationProvider : ConfigurationProvider + { + private readonly Lock _lock = new(); + + public override void Set(string key, string? value) + { + lock (_lock) + { + Data[key] = value; + } + OnReload(); + } + + /// Stable snapshot for callers that need to forward pairs to another sink + /// (e.g. UseSetting for startup-snapshot reads). + public IReadOnlyList> Snapshot() + { + lock (_lock) + { + return Data.ToArray(); + } + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IsolatedAvatarStorage.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IsolatedAvatarStorage.cs new file mode 100644 index 00000000..e7b0caa7 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/IsolatedAvatarStorage.cs @@ -0,0 +1,54 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Test-side scaffold for avatar-storage tests that need a per-test filesystem storage root +/// and per-test OCTOCON_AVATAR_PUBLIC_BASE without leaking either onto the +/// session-shared factory. See the doc-comments on the individual avatar-multipart tests +/// for the full rationale (before this helper, three tests hand-rolled ~17 lines each of +/// runId + storage root + private-factory build + finally-cleanup, and one of them had +/// already caused a ~50-downstream-500 cascade the first time it was written against the +/// shared factory). +/// +/// +/// The body runs with a private whose +/// OCTOCON_AVATAR_STORAGE_ROOT is a fresh temp dir and whose +/// OCTOCON_AVATAR_PUBLIC_BASE matches, so URL-path assertions can pin against the +/// second callback argument (publicBasePath). Both the private factory and the temp +/// dir are torn down on return, whether the body throws or not. +/// +/// +public static class IsolatedAvatarStorage +{ + public static async Task RunAsync( + IWebFactoryFixture fixture, + Func body) + { + var runId = Guid.NewGuid().ToString("N"); + var storageRoot = Path.Combine(Path.GetTempPath(), "octocon-itest", "avatars", runId); + // AvatarPublicBase carries [AbsoluteHttpUri] validation on StorageConfiguration, + // so the configured value MUST be an absolute http(s) URL. WebApplicationFactory's + // default BaseAddress is http://localhost/, so "http://localhost" matches + // request-origin resolution downstream. publicBasePath is what callers assert + // against, because UrlPathStartsWith compares Uri.AbsolutePath (path, not full URL). + var publicBasePath = $"/avatars-itest/{runId}"; + var publicBase = $"http://localhost{publicBasePath}"; + + try + { + Directory.CreateDirectory(storageRoot); + + await using var isolatedFactory = fixture.CreatePrivateFactory() + .WithConfiguration("OCTOCON_AVATAR_STORAGE_ROOT", storageRoot) + .WithConfiguration("OCTOCON_AVATAR_PUBLIC_BASE", publicBase); + + using var client = isolatedFactory.CreateClient(); + + await body(client, publicBasePath); + } + finally + { + if (Directory.Exists(storageRoot)) + Directory.Delete(storageRoot, true); + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/LifecycleProbe.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/LifecycleProbe.cs new file mode 100644 index 00000000..8b7705ec --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/LifecycleProbe.cs @@ -0,0 +1,155 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// TEMPORARY (planned for removal): logs lifecycle ordering of TUnit hooks and +/// initialization so we can pick the right hook for the +/// scheduled-set-based rebuild. Intentionally a tiny static +/// helper so the cleanup commit is a single delete + the matching hook removals in +/// BaseEndpointTest + the call site in SharedDbFixture.BuildArgs. +/// +/// +/// Writes to artifacts/lifecycle-probe.ndjson (one line per call) instead of +/// Console.WriteLine because TUnit's stdout interception only correlates output that +/// runs inside a async flow; the static hooks we care +/// about ([Before(TestDiscovery)], [After(TestDiscovery)], +/// [Before(TestSession)]) and all run before +/// any per-test context exists, so their stdout is silently dropped. Writing to a file +/// sidesteps the interception entirely and keeps the probe diagnostic regardless of which +/// hook fires it. +/// +public static class LifecycleProbe +{ + private static readonly object FileLock = new(); + private static readonly string LogPath = ResolveLogPath(); + private static int _sequence; + + static LifecycleProbe() + { + try + { + var dir = Path.GetDirectoryName(LogPath); + if (!string.IsNullOrEmpty(dir)) + Directory.CreateDirectory(dir); + // Truncate per-process so each test run produces a clean log. + File.WriteAllText(LogPath, string.Empty); + } + catch + { + // The probe must never break a test run if the log path isn't writable; the + // diagnostic value is "best effort" by design. + } + + AppDomain.CurrentDomain.ProcessExit += (_, _) => Log("ProcessExit"); + } + + /// + /// Returns a disposable scope that logs with elapsed ms on dispose. + /// + public static TimedScope BeginTimed(string endLabel) + => new(endLabel); + + /// + /// Writes a single line to the probe log capturing hook ordering plus whether + /// TestSessionContext.Current / TestDiscoveryContext.Current already expose + /// a populated AllTests list at this point. Reads via reflection so this file + /// doesn't take a hard compile-time dependency on TUnit.Core types that move between + /// versions; null-safe on every read so a missing context shows up as a label rather than + /// throwing the probe out. + /// + public static void Log(string label, long? elapsedMs = null) + { + var seq = Interlocked.Increment(ref _sequence); + var sessionCount = ReadAllTestsCount("TUnit.Core.TestSessionContext, TUnit.Core"); + var discoveryCount = ReadAllTestsCount("TUnit.Core.TestDiscoveryContext, TUnit.Core"); + var elapsed = elapsedMs is null ? string.Empty : $",\"elapsedMs\":{elapsedMs.Value}"; + var line = $"{{\"seq\":{seq},\"timestamp\":\"{DateTime.UtcNow:O}\",\"label\":\"{label}\"" + + elapsed + + $",\"sessionCount\":\"{sessionCount}\",\"discoveryCount\":\"{discoveryCount}\"}}"; + + try + { + lock (FileLock) + { + File.AppendAllText(LogPath, line + Environment.NewLine); + } + } + catch + { + // Swallow IO errors so the probe never fails a test run. + } + } + + private static string ResolveLogPath() + { + // Walk up from AppContext.BaseDirectory to find the repo root (marked by Interfold.sln + // or Interfold.slnx). Anchoring under /artifacts lines up with the rest of + // the suite's log conventions and keeps probe output discoverable from + // `artifacts/lifecycle-probe.ndjson` regardless of which working directory the test + // runner executes from. + var current = AppContext.BaseDirectory; + for (var i = 0; i < 10 && !string.IsNullOrEmpty(current); i++) + { + if (File.Exists(Path.Combine(current, "Interfold.slnx")) || + File.Exists(Path.Combine(current, "Interfold.sln"))) + { + return Path.Combine(current, "artifacts", "lifecycle-probe.ndjson"); + } + + current = Path.GetDirectoryName(current); + } + + // Fall back to the binary's directory if we can't find the repo root - still better + // than silently dropping the probe output entirely. + return Path.Combine(AppContext.BaseDirectory, "lifecycle-probe.ndjson"); + } + + private static string ReadAllTestsCount(string assemblyQualifiedTypeName) + { + var contextType = Type.GetType(assemblyQualifiedTypeName); + if (contextType is null) + return "(type-missing)"; + + var current = contextType + .GetProperty("Current", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)? + .GetValue(null); + if (current is null) + return "null"; + + var allTests = contextType + .GetProperty("AllTests", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)? + .GetValue(current); + if (allTests is null) + return "(prop-missing)"; + + if (allTests is System.Collections.ICollection collection) + return collection.Count.ToString(); + + var counter = 0; + if (allTests is System.Collections.IEnumerable enumerable) + { + foreach (var _ in enumerable) + counter++; + return counter.ToString(); + } + + return "(non-enumerable)"; + } + + public readonly struct TimedScope : IDisposable + { + private readonly string _endLabel; + private readonly long _startedTicks; + + public TimedScope(string endLabel) + { + _endLabel = endLabel; + _startedTicks = Environment.TickCount64; + } + + public void Dispose() + { + var elapsed = Environment.TickCount64 - _startedTicks; + Log(_endLabel, elapsed); + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/PhxEndpointFrame.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/PhxEndpointFrame.cs new file mode 100644 index 00000000..8bed4bad --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/PhxEndpointFrame.cs @@ -0,0 +1,37 @@ +using Interfold.Socket.Contracts; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Test-side builder for the Phoenix endpoint frame used by the WebSocket integration +/// suite. Every proxied-HTTP-over-WebSocket call is a 6-property literal +/// (Topic, Event = "endpoint", Payload = new PhxEndpointPayload{ Method, Path, Body }, +/// Ref, JoinRef) that was open-coded verbatim ~20 times across +/// WebSocketTests.cs. Consolidating the shape here means a future +/// wire-format tweak (e.g. moving join_ref) rolls forward through one +/// call site rather than 20+. +/// +public static class PhxEndpointFrame +{ + /// + /// Build the serialised bytes of an endpoint frame targeting + /// . Defaults to JoinRef = "1" because every + /// existing call site's join was request-ref "1"; callers with a + /// different join sequence pass the correct value explicitly. + /// + public static byte[] Build( + string topic, + string method, + string path, + object body, + string @ref, + string? joinRef = "1") + => new PhxFrame + { + Topic = topic, + Event = "endpoint", + Payload = new PhxEndpointPayload { Method = method, Path = path, Body = body }, + Ref = @ref, + JoinRef = joinRef, + }.ToBytes(); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ReceivedPhxFrame.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ReceivedPhxFrame.cs new file mode 100644 index 00000000..847a2671 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ReceivedPhxFrame.cs @@ -0,0 +1,309 @@ +using System.Net.WebSockets; +using System.Text; +using System.Text.Json; +using Interfold.Socket.Contracts; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// A strongly-typed representation of a received Phoenix WebSocket frame. +/// Handles both object-format and array-format frames. +/// +public sealed record ReceivedPhxFrame( + string Topic, + string Event, + string? Ref, + string? JoinRef, + JsonElement? RawPayload) +{ + /// + /// Deserialize the raw payload into a strongly-typed payload object. + /// + public TPayload Payload() => + RawPayload is null + ? throw new InvalidOperationException("Frame has no payload.") + : JsonSerializer.Deserialize(RawPayload.Value.GetRawText(), SocketJson.Options) + ?? throw new InvalidOperationException($"Failed to deserialize payload as {typeof(TPayload).Name}."); + + /// + /// For phx_reply frames: deserialize the payload as PhoenixReplyPayload<TResponse>. + /// + public PhoenixReplyPayload Reply() + { + if (!string.Equals(Event, "phx_reply", StringComparison.Ordinal)) + throw new InvalidOperationException($"Expected phx_reply event but got '{Event}'."); + + return Payload>(); + } + + /// + /// For endpoint proxy replies: deserialize the response body (which is a JSON string) into a typed object. + /// + public TBody EndpointBody() + { + var reply = Reply(); + if (string.IsNullOrWhiteSpace(reply.Response.Body)) + throw new InvalidOperationException("Endpoint proxy response body is empty."); + + return JsonSerializer.Deserialize(reply.Response.Body, SocketJson.Options) + ?? throw new InvalidOperationException($"Failed to deserialize endpoint body as {typeof(TBody).Name}."); + } + + /// + /// Parse a raw WebSocket text message into a ReceivedPhxFrame. + /// Supports both object-format and array-format Phoenix frames. + /// + public static ReceivedPhxFrame Parse(string rawText) + { + using var doc = JsonDocument.Parse(rawText); + var root = doc.RootElement; + + if (root.ValueKind == JsonValueKind.Array && root.GetArrayLength() >= 5) + { + var joinRef = root[0].ValueKind == JsonValueKind.String ? root[0].GetString() : null; + var @ref = root[1].ValueKind == JsonValueKind.String ? root[1].GetString() : null; + var topic = root[2].GetString() ?? string.Empty; + var @event = root[3].GetString() ?? string.Empty; + var payload = root[4].Clone(); + + return new ReceivedPhxFrame(topic, @event, @ref, joinRef, payload); + } + + if (root.ValueKind == JsonValueKind.Object) + { + var topic = root.TryGetProperty("topic", out var topicProp) && topicProp.ValueKind == JsonValueKind.String + ? topicProp.GetString() ?? string.Empty + : string.Empty; + + var @event = root.TryGetProperty("event", out var eventProp) && eventProp.ValueKind == JsonValueKind.String + ? eventProp.GetString() ?? string.Empty + : string.Empty; + + var @ref = root.TryGetProperty("ref", out var refProp) && refProp.ValueKind == JsonValueKind.String + ? refProp.GetString() + : null; + + var joinRef = root.TryGetProperty("join_ref", out var joinRefProp) && joinRefProp.ValueKind == JsonValueKind.String + ? joinRefProp.GetString() + : null; + + JsonElement? payload = root.TryGetProperty("payload", out var payloadProp) + ? payloadProp.Clone() + : null; + + return new ReceivedPhxFrame(topic, @event, @ref, joinRef, payload); + } + + throw new InvalidOperationException($"Cannot parse Phoenix frame from: {rawText}"); + } + + /// + /// Receive a single frame from a WebSocket and parse it into a ReceivedPhxFrame. + /// + public static async Task ReceiveAsync(WebSocket ws, CancellationToken token, int timeoutSeconds = 30) + { + var text = await ReceiveTextAsync(ws, token, timeoutSeconds); + return Parse(text); + } + + /// + /// Receive a phx_reply frame and return the typed reply payload. + /// + public static async Task> ReceiveReplyAsync(WebSocket ws, CancellationToken token, int timeoutSeconds = 30) + { + var frame = await ReceiveAsync(ws, token, timeoutSeconds); + return frame.Reply(); + } + + /// + /// Scan up to maxFrames received frames for one matching the expected event name, and return its typed payload. + /// Returns null if not found within maxFrames. + /// + public static async Task ReceiveEventPayloadAsync( + WebSocket ws, + CancellationToken token, + string expectedEvent, + int maxFrames = 6, + int perFrameTimeoutSeconds = 30) where TPayload : class + { + for (var i = 0; i < maxFrames; i++) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(token); + cts.CancelAfter(TimeSpan.FromSeconds(perFrameTimeoutSeconds)); + + ReceivedPhxFrame frame; + try + { + frame = await ReceiveAsync(ws, cts.Token); + } + catch (OperationCanceledException) + { + return null; + } + + if (string.Equals(frame.Event, expectedEvent, StringComparison.Ordinal)) + return frame.Payload(); + } + + return null; + } + + /// + /// Scan up to maxFrames received frames for one matching the expected event name, and return the frame. + /// Returns null if not found within maxFrames. + /// + public static async Task ReceiveEventFrameAsync( + WebSocket ws, + CancellationToken token, + string expectedEvent, + int maxFrames = 6, + int perFrameTimeoutSeconds = 30) + { + for (var i = 0; i < maxFrames; i++) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(token); + cts.CancelAfter(TimeSpan.FromSeconds(perFrameTimeoutSeconds)); + + ReceivedPhxFrame frame; + try + { + frame = await ReceiveAsync(ws, cts.Token); + } + catch (OperationCanceledException) + { + return null; + } + + if (string.Equals(frame.Event, expectedEvent, StringComparison.Ordinal)) + return frame; + } + + return null; + } + + /// + /// Scan up to maxFrames for a frame matching the predicate. + /// + public static async Task ReceiveUntilAsync( + WebSocket ws, + CancellationToken token, + Func predicate, + int maxFrames = 6, + int perFrameTimeoutSeconds = 30) + { + for (var i = 0; i < maxFrames; i++) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(token); + cts.CancelAfter(TimeSpan.FromSeconds(perFrameTimeoutSeconds)); + + ReceivedPhxFrame frame; + try + { + frame = await ReceiveAsync(ws, cts.Token); + } + catch (OperationCanceledException) + { + return null; + } + + if (predicate(frame)) + return frame; + } + + return null; + } + + /// + /// Receive frames and collect both the phx_reply and a push event with the given name. + /// Returns both as a tuple. Either may be null if not received within maxFrames. + /// + public static async Task<(ReceivedPhxFrame? Reply, ReceivedPhxFrame? Push)> ReceiveReplyAndPushAsync( + WebSocket ws, + CancellationToken token, + string expectedPushEvent, + int maxFrames = 6, + int perFrameTimeoutSeconds = 30) + { + ReceivedPhxFrame? reply = null; + ReceivedPhxFrame? push = null; + + for (var i = 0; i < maxFrames && (reply is null || push is null); i++) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(token); + cts.CancelAfter(TimeSpan.FromSeconds(perFrameTimeoutSeconds)); + + ReceivedPhxFrame frame; + try + { + frame = await ReceiveAsync(ws, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + + if (frame.Event == "phx_reply") + reply = frame; + else if (string.Equals(frame.Event, expectedPushEvent, StringComparison.Ordinal)) + push = frame; + } + + return (reply, push); + } + + /// + /// Receive frames and collect the two specified events. + /// Returns both as a tuple. Either may be null if not received within maxAttempts. + /// + public static async Task<(ReceivedPhxFrame? A, ReceivedPhxFrame? B)> ReceiveTwoOfAsync( + WebSocket ws, string eventA, string eventB, + TimeSpan perAttemptTimeout, int maxAttempts, CancellationToken ct) + { + ReceivedPhxFrame? a = null; + ReceivedPhxFrame? b = null; + + for (var i = 0; i < maxAttempts && (a is null || b is null); i++) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); + cts.CancelAfter(perAttemptTimeout); + + ReceivedPhxFrame frame; + try + { + frame = await ReceiveAsync(ws, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + + if (string.Equals(frame.Event, eventA, StringComparison.Ordinal)) + a = frame; + else if (string.Equals(frame.Event, eventB, StringComparison.Ordinal)) + b = frame; + } + + return (a, b); + } + + private static async Task ReceiveTextAsync(WebSocket ws, CancellationToken token, int timeoutSeconds = 30) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(token); + cts.CancelAfter(TimeSpan.FromSeconds(timeoutSeconds)); + + var buffer = new byte[8192]; + using var ms = new MemoryStream(); + + WebSocketReceiveResult result; + do + { + result = await ws.ReceiveAsync(new ArraySegment(buffer), cts.Token); + if (result.MessageType == WebSocketMessageType.Close) + throw new InvalidOperationException($"WebSocket closed by server. {result.CloseStatusDescription}/{result.CloseStatus}"); + + ms.Write(buffer, 0, result.Count); + } while (result is { EndOfMessage: false, CloseStatus: not null }); + + return Encoding.UTF8.GetString(ms.ToArray()); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/RequiredFixtures.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/RequiredFixtures.cs new file mode 100644 index 00000000..0542cfb7 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/RequiredFixtures.cs @@ -0,0 +1,177 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Process-static record of which implementations +/// the filtered test session may request. Populated from +/// at [After(TestDiscovery)]. Assembly-walk +/// fallback fires if the context is null so filter-only runs never miss a needed fixture. +public static class RequiredFixtures +{ + private static readonly HashSet Discovered = new(); + private static readonly object DiscoveryLock = new(); + private static bool _populated; + + public static bool NeedScylla => Has(); + + public static bool NeedCassandra => Has(); + + // MultiNodeScyllaFixture lives in a sibling test assembly, so a typeof() reference from + // Shared would form a project cycle. HostAioPrerequisite still needs to know when the + // multi-node cluster is scheduled (so it can bump fs.aio-max-nr), so probe by unqualified + // type name against the same discovered set — there is exactly one in the tree. + public static bool NeedMultiNodeScylla => HasByName("MultiNodeScyllaFixture"); + + private static bool HasByName(string simpleName) + { + EnsureDiscovered(); + foreach (var t in Discovered) + { + if (string.Equals(t.Name, simpleName, StringComparison.Ordinal)) + { + return true; + } + } + return false; + } + + /// Driven by [After(TestDiscovery)] on BaseEndpointTest; + /// idempotent. + public static void Discover() + { + EnsureDiscovered(); + } + + private static bool Has() + { + EnsureDiscovered(); + return Discovered.Contains(typeof(TFixture)); + } + + private static void EnsureDiscovered() + { + if (_populated) + { + return; + } + + lock (DiscoveryLock) + { + if (_populated) + { + return; + } + + var scheduled = TryGetScheduledClassTypes(); + if (scheduled is not null) + { + foreach (var classType in scheduled) + { + foreach (var fixtureType in ExtractClassDataSourceTypes(classType, transitive: true)) + { + Discovered.Add(fixtureType); + } + } + + _populated = true; + return; + } + + // Fallback: scheduled-test contexts weren't populated (e.g. a future TUnit release + // reshuffles the discovery lifecycle). Walk the full assembly so filter-only runs + // never miss a needed fixture; the over-eagerness only manifests under filtering. + DiscoverViaAssemblyWalk(); + _populated = true; + } + } + + private static IEnumerable? TryGetScheduledClassTypes() + { + // Prefer the session context (populated alongside discovery) but fall back to the + // discovery context which is what fires the [After(TestDiscovery)] hook. Both list the + // *scheduled* tests post-filter once discovery has run. + var allTests = (IEnumerable?)TestSessionContext.Current?.AllTests + ?? TestDiscoveryContext.Current?.AllTests; + if (allTests is null) + { + return null; + } + + // Materialise once — TUnit's AllTests collection isn't documented as multi-iteration safe. + var snapshot = allTests as IReadOnlyCollection ?? allTests.ToArray(); + if (snapshot.Count == 0) + { + // Empty scheduled set still counts as "populated" — honour the filter even when + // the user explicitly excludes everything. + return Array.Empty(); + } + + var classTypes = new HashSet(); + foreach (var test in snapshot) + { + var classType = test.Metadata.TestDetails.ClassType; + if (classType is not null) + { + classTypes.Add(classType); + } + } + + return classTypes; + } + + private static void DiscoverViaAssemblyWalk() + { + var assembly = typeof(RequiredFixtures).Assembly; + foreach (var type in assembly.GetTypes()) + { + foreach (var fixtureType in ExtractClassDataSourceTypes(type, transitive: false)) + { + Discovered.Add(fixtureType); + } + } + } + + /// Yields the generic args of every ClassDataSourceAttribute<...> + /// on (or a base). Transitive walk covers indirect + /// fixtures (e.g. ScyllaWebFactoryFixture → SharedDbFixture) so filter-only runs still + /// discover them. + private static IEnumerable ExtractClassDataSourceTypes(Type testClassType, bool transitive) + { + var visited = new HashSet(); + var stack = new Stack(); + stack.Push(testClassType); + + while (stack.Count > 0) + { + var current = stack.Pop(); + if (!visited.Add(current)) + { + continue; + } + + foreach (var attr in current.GetCustomAttributes(inherit: true)) + { + var attrType = attr.GetType(); + if (!attrType.IsGenericType) + { + continue; + } + + var openGeneric = attrType.GetGenericTypeDefinition(); + if (openGeneric.FullName?.StartsWith( + "TUnit.Core.ClassDataSourceAttribute`", + StringComparison.Ordinal) != true) + { + continue; + } + + foreach (var t in attrType.GetGenericArguments()) + { + yield return t; + if (transitive) + { + stack.Push(t); + } + } + } + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaDirectHarness.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaDirectHarness.cs new file mode 100644 index 00000000..1c3633ae --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaDirectHarness.cs @@ -0,0 +1,42 @@ +using Cassandra; +using Interfold.Infrastructure.Scylla; +using Interfold.Shared.Contracts.Ids; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Shared preamble for direct-CQL Scylla integration tests — the ones that must synthesise +/// a pathological row via the cluster session (partial migration / manual fixup / schema +/// drift) before driving the production repository against it. Every such test resolves +/// the same trio out of the factory (, +/// ) and derives the same trio of per-system primitives +/// ( → normalized string, regional keyspace, live +/// ). +/// +/// +/// Previously duplicated verbatim in ScyllaAlterRepositoryUdtNullTests and +/// ScyllaFrontingRepositoryNullTimeStartTests; extract point picked out by the R2 +/// dedup plan (D11.c). +/// +/// +public static class ScyllaDirectHarness +{ + public sealed record Context( + ISession Session, + string Keyspace, + string NormalizedSystemId); + + public static async Task ResolveAsync(IWebFactoryFixture fixture, SystemId systemId) + { + var services = fixture.Factory.Services; + var keyspaceResolver = services.GetRequiredService(); + var sessionProvider = services.GetRequiredService(); + + var session = await sessionProvider.GetSessionAsync(); + var keyspace = keyspaceResolver.ResolveRegionalKeyspace(systemId); + var normalizedSystemId = keyspaceResolver.NormalizeSystemId(systemId); + + return new Context(session, keyspace, normalizedSystemId); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaRackDcMountPaths.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaRackDcMountPaths.cs new file mode 100644 index 00000000..d2d17c8f --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaRackDcMountPaths.cs @@ -0,0 +1,31 @@ +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Absolute rackdc bind-mount sources for Scylla. Path shape matches +/// Interfold.AppHost.AppHostRepoPaths.ScyllaRackDcProperties — keep in sync. +public static class ScyllaRackDcMountPaths +{ + public static string RackDcFile(string repoRoot, string regionWire) => + Path.GetFullPath(Path.Combine(repoRoot, "db", "scylla", $"cassandra-rackdc.{regionWire}.properties")); + + /// Verifies every regional rackdc file the multi-DC graph can bind exists on disk. + /// Throws with an actionable message when a mount source is missing. + public static void VerifyAllPresent(string repoRoot) + { + var missing = new List(); + foreach (var region in new[] { "nam", "eur", "sam", "sas", "eas", "ocn", "gdpr" }) + { + var path = RackDcFile(repoRoot, region); + if (!File.Exists(path)) + missing.Add(path); + } + + if (missing.Count == 0) + return; + + throw new InvalidOperationException( + "Scylla GossipingPropertyFileSnitch requires rackdc bind-mount sources under db/scylla/, " + + "but the following files are missing:\n " + + string.Join("\n ", missing) + + "\nMulti-DC topology depends on these files — do not fall back to SimpleSnitch."); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaTestSession.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaTestSession.cs new file mode 100644 index 00000000..26b6ace4 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaTestSession.cs @@ -0,0 +1,65 @@ +using Cassandra; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Thin wrapper around a DataStax + +/// pair. Centralises builder settings (credentials, DC-aware routing, +/// query timeout) that were previously duplicated across ~6 test sites and ensures the +/// cluster is shut down on disposal. +/// +public sealed class ScyllaTestSession : IAsyncDisposable +{ + public Cluster Cluster { get; } + public ISession Session { get; } + + private ScyllaTestSession(Cluster cluster, ISession session) + { + Cluster = cluster; + Session = session; + } + + /// + /// Opens a session authenticated as the application-level user + /// (). + /// + public static Task OpenAsAppAsync( + string host, int port, + string dc = "nam", int queryTimeoutMs = 30_000, + CancellationToken ct = default) + => OpenAsync(host, port, TestDbCredentials.ScyllaAppUser, TestDbCredentials.ScyllaAppPassword, dc, queryTimeoutMs, ct); + + /// + /// Opens a session authenticated as the admin user + /// (). + /// + public static Task OpenAsAdminAsync( + string host, int port, + string dc = "nam", int queryTimeoutMs = 30_000, + CancellationToken ct = default) + => OpenAsync(host, port, TestDbCredentials.ScyllaAdminUser, TestDbCredentials.ScyllaAdminPassword, dc, queryTimeoutMs, ct); + + private static async Task OpenAsync( + string host, int port, + string user, string password, + string dc, int queryTimeoutMs, + CancellationToken ct) + { + var cluster = Cluster.Builder() + .AddContactPoint(host) + .WithPort(port) + .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy(dc)) + .WithCredentials(user, password) + .WithQueryTimeout(queryTimeoutMs) + .Build(); + + var session = await cluster.ConnectAsync().ConfigureAwait(false); + return new ScyllaTestSession(cluster, session); + } + + public async ValueTask DisposeAsync() + { + Session.Dispose(); + await Cluster.ShutdownAsync().ConfigureAwait(false); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaWebFactoryFixture.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaWebFactoryFixture.cs new file mode 100644 index 00000000..6fe9f94f --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/ScyllaWebFactoryFixture.cs @@ -0,0 +1,57 @@ +using Interfold.Shared.Contracts; +using TUnit.Core.Interfaces; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Fixture chain that creates an backed by the +/// shared single-node ScyllaDB cluster managed by . TUnit +/// resolves the dependency automatically via . +/// +/// +/// Because this fixture references , the +/// hook flips +/// on for the session, which in turn flips the +/// include-scylla Aspire parameter on. The ScyllaPort property is therefore +/// always populated when this fixture's runs. +/// +public sealed class ScyllaWebFactoryFixture : IWebFactoryFixture, IAsyncInitializer +{ + [ClassDataSource(Shared = SharedType.PerTestSession)] + public required SharedDbFixture Aspire { get; init; } + + public InterfoldWebApplicationFactory Factory { get; private set; } = null!; + + public Task InitializeAsync() + { + // SharedDbFixture captures per-backend startup failures into ScyllaInitException / + // CassandraInitException rather than aborting its own InitializeAsync. Rethrowing the + // captured exception here (wrapped so the trigger is obvious in the test report) + // scopes the failure to Scylla-backed tests only — Cassandra-backed and InMemory + // tests in the same run keep passing. + if (Aspire.ScyllaInitException is { } scyllaFailure) + { + throw new InvalidOperationException( + "ScyllaWebFactoryFixture cannot start because the shared Scylla backend failed " + + "to initialise. See the inner exception for the underlying cause.", + scyllaFailure); + } + + Factory = CreatePrivateFactory(); + return Task.CompletedTask; + } + + /// + // Configuration must stay byte-identical to InitializeAsync so that tests opting into a + // private factory exercise the same Scylla + Postgres wiring as the session-shared one. + // ScyllaPort is asserted non-null by InitializeAsync's guard above — any caller reaching + // this method has already passed that check via the same shared Aspire fixture. + public InterfoldWebApplicationFactory CreatePrivateFactory() + => new InterfoldWebApplicationFactory(PersistenceMode.ScyllaPostgres, "scylla-single-node") + .WithConfiguration("OCTOCON_POSTGRES_CONNECTION", Aspire.PostgresConnectionString) + .WithConfiguration("OCTOCON_SCYLLA_PORT", Aspire.ScyllaPort!.Value.ToString()) + .WithConfiguration("OCTOCON_SINGLE_SCYLLA_INSTANCE", "true") + .WithConfiguration("OCTOCON_DB_RETRY_ATTEMPTS", "10") + .WithConfiguration("OCTOCON_DB_RETRY_INITIAL_DELAY_MS", "500") + .WithConfiguration("OCTOCON_DB_RETRY_MAX_DELAY_MS", "3000"); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/SharedDbFixture.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/SharedDbFixture.cs new file mode 100644 index 00000000..f298d6e0 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/SharedDbFixture.cs @@ -0,0 +1,548 @@ +extern alias AppHost; +using AppHostRepoPaths = AppHost::Interfold.AppHost.AppHostRepoPaths; +using AspireResourceFailureDiagnostics = AppHost::Interfold.AppHost.AspireResourceFailureDiagnostics; +using Aspire.Hosting; +using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Testing; +using Interfold.DatabaseBootstrap; +using Interfold.Infrastructure.Postgres; +using Interfold.Infrastructure.Scylla; +using Interfold.IntegrationTests.Shared.TestServices.TestBench; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Npgsql; +using TUnit.Aspire; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Session-shared Aspire host for every DB-bound integration test. Postgres always +/// runs (internal.secrets is the shared source of truth); Scylla/Cassandra spin up only when +/// reports a dependent test in the session. Migrations run once +/// here; the per-test strips the migration +/// hosted services so they don't replay on every rebuild. +public sealed class SharedDbFixture : AspireFixture +{ + // Bench-mode short-circuit — when the current test host is opted in via + // `INTERFOLD_TEST_BENCH=1`, the fixture attaches to the auto-managed cross-project + // bench (`TestBenchCoordinator`) instead of booting an in-process AppHost. Legacy CI + // legs and any local run without the env var take the original AspireFixture path. + private readonly bool _benchMode = TestBenchCoordinator.IsOptedIn(); + + /// Postgres connection string; always populated after startup. + public string PostgresConnectionString { get; private set; } = string.Empty; + + /// Host port for the ScyllaDB CQL endpoint; null when Scylla wasn't required. + public int? ScyllaPort { get; private set; } + + /// Host port for the Cassandra CQL endpoint; null when Cassandra wasn't required. + public int? CassandraPort { get; private set; } + + /// Captured Scylla init failure (container / probe / seed / migration). The + /// per-backend web-factory fixture rethrows this so only Scylla-dependent tests fail — + /// Cassandra-only and InMemory-only tests survive. + public Exception? ScyllaInitException { get; private set; } + + /// Cassandra mirror of . + public Exception? CassandraInitException { get; private set; } + + protected override string[] Args => BuildArgs(); + + private static string[] BuildArgs() + { + // BaseEndpointTest's [Before(TestDiscovery)] hook populates RequiredFixtures before + // AspireFixture.InitializeAsync (the only caller of this getter) runs. + LifecycleProbe.Log("SharedDbFixture.BuildArgs"); + var args = new List + { + $"{AppHostParameterKeys.IncludeApi}=false", + $"{AppHostParameterKeys.IncludeWeb}=false", + $"{AppHostParameterKeys.PersistentContainers}=false", + $"{AppHostParameterKeys.IncludePostgres}=true", + $"{AppHostParameterKeys.IncludeScylla}={BoolWire.ToWireValue(RequiredFixtures.NeedScylla)}", + $"{AppHostParameterKeys.IncludeCassandra}={BoolWire.ToWireValue(RequiredFixtures.NeedCassandra)}", + // Isolated from the shared test-bench defaults (14200/19042/19043) so the + // Infrastructure opt-out host can run beside the bench without Docker binds colliding. + $"{AppHostParameterKeys.PortsPostgres}={AspireProcessEndpoints.LegacyPostgresPort}", + $"{AppHostParameterKeys.PortsScylla}={AspireProcessEndpoints.LegacyScyllaPort}", + $"{AppHostParameterKeys.PortsCassandra}={AspireProcessEndpoints.LegacyCassandraPort}", + $"{AppHostParameterKeys.PostgresUser}={TestDbCredentials.PostgresAppUser}", + $"{AppHostParameterKeys.PostgresPassword}={TestDbCredentials.PostgresAppPassword}", + // Pinned so the AppHost's GenerateParameterDefault output can't drift from DbInitHelper. + $"{AppHostParameterKeys.PostgresInitPassword}={TestDbCredentials.PostgresInitPassword}", + // Route both sides through DbInitHelper.DefaultPostgresDb so a rename moves in lockstep. + $"{AppHostParameterKeys.PostgresDb}={DbInitHelper.DefaultPostgresDb}", + $"{AppHostParameterKeys.ScyllaUser}={TestDbCredentials.ScyllaAppUser}", + $"{AppHostParameterKeys.ScyllaPassword}={TestDbCredentials.ScyllaAppPassword}", + $"{AppHostParameterKeys.EncryptionPrivateKey}=TEST", + }; + return args.ToArray(); + } + + protected override TimeSpan ResourceTimeout => TimeSpan.FromMinutes(5); + protected override bool EnableTelemetryCollection => false; + + /// Per-container readiness budget so one stuck backend can't starve the other of + /// the outer . + private static readonly TimeSpan PerContainerReadyTimeout = TimeSpan.FromMinutes(2); + + // No Aspire-level healthchecks fire in test mode (include-api=false), and the default + // AllHealthy behaviour would hang on parameter resources. WaitForResourcesAsync owns + // readiness directly via ResourceNotificationService. + protected override ResourceWaitBehavior WaitBehavior => ResourceWaitBehavior.None; + + public override async Task InitializeAsync() + { + if (_benchMode) + { + await InitializeBenchModeAsync(CancellationToken.None).ConfigureAwait(false); + return; + } + + // Aspire dashboard / resource-service endpoints default to launchSettings (22223). + // Claim a legacy-only triple before the in-process AppHost starts so we don't fight + // the out-of-process test-bench launcher (23223) or MultiNode (25223). + AspireProcessEndpoints.ApplyToCurrentProcess( + AspireProcessEndpoints.LegacySharedDbResourceService, + AspireProcessEndpoints.LegacySharedDbOtlp, + AspireProcessEndpoints.LegacySharedDbAppUrls); + + await DockerMemoryPreflight.EnsureAdequateAsync(CancellationToken.None).ConfigureAwait(false); + ScyllaRackDcMountPaths.VerifyAllPresent(TestBenchCoordinator.ResolveRepoRoot()); + + // Raise fs.aio-max-nr session-wide (this fixture + optional MultiNodeScyllaFixture) + // before any Scylla node starts; MultiNode re-asserts and short-circuits via cache. + await HostAioPrerequisite + .EnsureAsync(HostAioPrerequisite.TotalScyllaNodesForSession()) + .ConfigureAwait(false); + await base.InitializeAsync().ConfigureAwait(false); + } + + /// Bench-mode alternative to base.InitializeAsync + + /// . Delegates container lifecycle to + /// , then runs the same seed + migration pipeline + /// against the already-running containers. Migrations are idempotent, so a hot attach + /// re-runs them harmlessly. + private async Task InitializeBenchModeAsync(CancellationToken cancellationToken) + { + LifecycleProbe.Log("SharedDbFixture.InitializeBenchMode"); + + // Raise the fs.aio-max-nr sysctl before Scylla starts, same as the legacy path. + await HostAioPrerequisite + .EnsureAsync(HostAioPrerequisite.TotalScyllaNodesForSession()) + .ConfigureAwait(false); + + var bench = await TestBenchCoordinator.EnsureRunningAsync(cancellationToken).ConfigureAwait(false); + + // Bench always brings up all three engines — cross-project isolation is shared-global + // (docs/test-bench-audit.md), so per-project RequiredFixtures flags no longer gate + // which containers exist. Expose both CQL ports; per-backend factories pick one. + ScyllaPort = bench.ScyllaPort; + CassandraPort = bench.CassandraPort; + PostgresConnectionString = bench.PostgresConnectionStringForApp; + + // Postgres readiness + seed + migration. Postgres is foundational (internal.secrets), + // so any failure here is a session-wide problem; propagate instead of capturing. + await DbInitHelper.WaitForPostgresAsync( + bench.PostgresConnectionStringForInit, + new PostgresReadinessOptions(TimeSpan.FromMinutes(6), 3), + cancellationToken).ConfigureAwait(false); + // SeedPostgresAsync runs `CREATE ROLE` / `CREATE DATABASE` / seed writes that + // are individually idempotent but NOT concurrency-safe: peer projects racing + // the same cold-boot bench all pass AppRoleAlreadyConfiguredAsync at once and + // then collide on `CREATE DATABASE test_pg_db` (23505 unique_violation on + // pg_database_datname_index). ApplyOnceAsync serialises the whole seed behind + // the bench's file lock so only the first arrival runs it; hot-attach observers + // skip via the "seed:postgres" marker. + await TestBenchCoordinator.ApplyOnceAsync( + "seed:postgres", + () => DbInitHelper.SeedPostgresAsync( + bench.PostgresConnectionStringForInit, + BuildPostgresSeedOptions(cqlEndpoint: null), + cancellationToken), + cancellationToken).ConfigureAwait(false); + await WaitForPostgresConnectivityAsync(PostgresConnectionString, cancellationToken).ConfigureAwait(false); + + var persistenceConfig = new PersistenceConfiguration + { + Mode = Interfold.Shared.Contracts.PersistenceMode.ScyllaPostgres, + PostgresConnectionString = PostgresConnectionString, + IsSingleScyllaInstance = true, + ScyllaKeyspace = ScyllaKeyspace.Nam, + }; + var connectionFactory = new PostgresConnectionFactory(Microsoft.Extensions.Options.Options.Create(persistenceConfig)); + var secretsStore = new PostgresSecretsStore(connectionFactory); + + // ApplyOnceAsync serialises this migration behind the bench's file lock so + // concurrent test-host inits never step on each other, and MigrationLedgerTests' + // deliberate checksum-drift can't collide with a peer project's fixture init. + // See docs/test-bench-audit.md. + // + // PostgresMigrationService.MigrateAsync silently returns when + // `secretsStore.GetAsync(PostgresAdmin{Username,Password})` yields empty (production + // dev-mode intent). Under bench mode that silent no-op is catastrophic: the + // ApplyOnceAsync marker flips to `true` on a clean return, every subsequent + // hot-attach skips migrations, and the first test hitting the shared bench 500s + // with `42P01: relation "octocon_idempotency" does not exist`. Verify the ledger + // exists post-migrate so a silent skip surfaces as a fail-fast here instead. + await TestBenchCoordinator.ApplyOnceAsync( + "postgres", + async () => + { + await PostgresMigrationService.MigrateAsync( + persistenceConfig, + secretsStore, + NullLoggerFactory.Instance.CreateLogger(), + cancellationToken).ConfigureAwait(false); + await VerifyPostgresLedgerAppliedAsync(bench.PostgresConnectionStringForInit, cancellationToken).ConfigureAwait(false); + }, + cancellationToken).ConfigureAwait(false); + + // Scylla + Cassandra: capture per-engine so a wedged CQL backend only breaks + // fixtures that consume it. Mirrors the legacy path's per-backend try/catch. + ScyllaInitException = await TryInitialiseBenchCqlAsync( + "scylla", "127.0.0.1", bench.ScyllaPort, persistenceConfig, secretsStore, cancellationToken).ConfigureAwait(false); + CassandraInitException = await TryInitialiseBenchCqlAsync( + "cassandra", "127.0.0.1", bench.CassandraPort, persistenceConfig, secretsStore, cancellationToken).ConfigureAwait(false); + } + + private async Task TryInitialiseBenchCqlAsync( + string backendLabel, + string host, + int port, + PersistenceConfiguration persistenceConfig, + ISecretsStore secretsStore, + CancellationToken cancellationToken) + { + try + { + // The wait-for-CQL, seed, and migrate steps all live INSIDE ApplyOnceAsync so + // the (default cassandra/cassandra) credentials WaitForScyllaAsync uses are only + // exercised on cold-boot — right before the seeder scrambles that account. A + // hot-attach observer sees the marker already set and skips the whole block; the + // TCP-hot probe in TestBenchCoordinator.ArePortsHotAsync gated us in, so we + // already know the CQL listener is alive. + await TestBenchCoordinator.ApplyOnceAsync( + $"cql-seed-and-migrate:{backendLabel}", + async () => + { + await DbInitHelper.WaitForScyllaAsync(host, port, cancellationToken).ConfigureAwait(false); + await DbInitHelper.SeedScyllaAsync(host, port, BuildScyllaSeedOptions(), cancellationToken).ConfigureAwait(false); + var endpoint = new Uri($"tcp://{host}:{port}"); + await RunScyllaMigrationsAsync(endpoint, persistenceConfig, secretsStore, cancellationToken).ConfigureAwait(false); + }, + cancellationToken).ConfigureAwait(false); + return null; + } + catch (Exception ex) + { + LifecycleProbe.Log($"SharedDbFixture.BenchCqlInitFailed:{backendLabel}"); + return ex; + } + } + + public override async ValueTask DisposeAsync() + { + using var _ = LifecycleProbe.BeginTimed("AfterFixtureDispose:SharedDbFixture"); + LifecycleProbe.Log("BeforeFixtureDispose:SharedDbFixture"); + + if (_benchMode) + { + // Bench containers survive this test host — teardown is the reaper's job on + // the next attach. All we do is drop our PID from the users list. + await TestBenchCoordinator.DetachAsync(CancellationToken.None).ConfigureAwait(false); + return; + } + + try + { + await AspireContainerCleanup + .StopResourcesAsync(AspireContainerCleanup.SharedDbResourceNames()) + .ConfigureAwait(false); + } + catch + { + // Best-effort pre-stop; base dispose must still run. + } + + await base.DisposeAsync().ConfigureAwait(false); + + var remaining = await AspireContainerCleanup.CountRunningAspireContainersAsync().ConfigureAwait(false); + if (remaining > 0) + LifecycleProbe.Log($"SharedDbFixture.RemainingAspireContainers:{remaining}"); + } + + protected override async Task WaitForResourcesAsync(DistributedApplication app, CancellationToken cancellationToken) + { + var notifications = app.Services.GetRequiredService(); + + // Postgres is foundational (internal.secrets); failures propagate. Wait for Running, + // not Healthy — Aspire testing randomises ports so hardcoded-port checks never pass. + await notifications.WaitForResourceAsync("msg-db", KnownResourceStates.Running, cancellationToken); + + var pgEndpoint = App.GetEndpoint("msg-db", "postgres"); + + var initConnectionString = + $"Host={pgEndpoint.Host};Port={pgEndpoint.Port};" + + $"Username={DbInitHelper.PostgresInitUser};Password={TestDbCredentials.PostgresInitPassword};" + + "Database=postgres;SSL Mode=Disable"; + await DbInitHelper.WaitForPostgresAsync( + initConnectionString, + new Interfold.DatabaseBootstrap.PostgresReadinessOptions(TimeSpan.FromMinutes(6), 3), + cancellationToken); + // Seed also writes the Scylla contact-point row; localhost placeholder is only + // observable when the run is already broken before this point. + await DbInitHelper.SeedPostgresAsync( + initConnectionString, + BuildPostgresSeedOptions(cqlEndpoint: null), + cancellationToken); + + PostgresConnectionString = + $"Host={pgEndpoint.Host};Port={pgEndpoint.Port};" + + $"Username={TestDbCredentials.PostgresAppUser};Password={TestDbCredentials.PostgresAppPassword};" + + $"Database={DbInitHelper.DefaultPostgresDb};SSL Mode=Disable;Maximum Pool Size=5"; + + // Docker Desktop on Windows can delay host port forwarding even after healthy; + // hard-fail here rather than during the first test. + await WaitForPostgresConnectivityAsync(PostgresConnectionString, cancellationToken); + + // Single session-wide Postgres migration pass; per-test factories strip the hosted service. + var persistenceConfig = new PersistenceConfiguration + { + Mode = Interfold.Shared.Contracts.PersistenceMode.ScyllaPostgres, + PostgresConnectionString = PostgresConnectionString, + IsSingleScyllaInstance = true, + ScyllaKeyspace = ScyllaKeyspace.Nam, + }; + var connectionFactory = new PostgresConnectionFactory(Microsoft.Extensions.Options.Options.Create(persistenceConfig)); + var secretsStore = new PostgresSecretsStore(connectionFactory); + + await PostgresMigrationService.MigrateAsync( + persistenceConfig, + secretsStore, + NullLoggerFactory.Instance.CreateLogger(), + cancellationToken); + + // Each backend's init is captured into *InitException so only the per-backend + // web-factory fixture surfaces it; the other backend and InMemory keep running. + if (RequiredFixtures.NeedScylla) + { + ScyllaInitException = await TryInitialiseCqlBackendAsync( + resourceName: "scylla", + endpointName: "cql", + notifications: notifications, + persistenceConfig: persistenceConfig, + secretsStore: secretsStore, + assignPort: port => ScyllaPort = port, + cancellationToken: cancellationToken); + } + + if (RequiredFixtures.NeedCassandra) + { + CassandraInitException = await TryInitialiseCqlBackendAsync( + resourceName: "cassandra", + endpointName: "cql", + notifications: notifications, + persistenceConfig: persistenceConfig, + secretsStore: secretsStore, + assignPort: port => CassandraPort = port, + cancellationToken: cancellationToken); + } + } + + /// Container-wait + probe + seed + migration for one CQL backend. Never throws; + /// returns null on success or the captured exception on failure so the outer method can + /// attribute per backend. + private async Task TryInitialiseCqlBackendAsync( + string resourceName, + string endpointName, + ResourceNotificationService notifications, + PersistenceConfiguration persistenceConfig, + ISecretsStore secretsStore, + Action assignPort, + CancellationToken cancellationToken) + { + try + { + await WaitForContainerRunningAsync(notifications, resourceName, cancellationToken).ConfigureAwait(false); + + var endpoint = App.GetEndpoint(resourceName, endpointName); + await DbInitHelper.WaitForScyllaAsync(endpoint.Host, endpoint.Port, cancellationToken).ConfigureAwait(false); + await DbInitHelper.SeedScyllaAsync( + endpoint.Host, endpoint.Port, + BuildScyllaSeedOptions(), + cancellationToken).ConfigureAwait(false); + await RunScyllaMigrationsAsync(endpoint, persistenceConfig, secretsStore, cancellationToken).ConfigureAwait(false); + assignPort(endpoint.Port); + return null; + } + catch (Exception ex) + { + // Swallow OCE too — otherwise the base AspireFixture recategorises it as a + // session-wide timeout and fails every test in the run. + return ex; + } + } + + /// Waits for Running, racing against FailedToStart and + /// . + private async Task WaitForContainerRunningAsync( + ResourceNotificationService notifications, + string resourceName, + CancellationToken outerCancellationToken) + { + using var perResourceCts = CancellationTokenSource.CreateLinkedTokenSource(outerCancellationToken); + perResourceCts.CancelAfter(PerContainerReadyTimeout); + var token = perResourceCts.Token; + + var readyTask = notifications.WaitForResourceAsync( + resourceName, KnownResourceStates.Running, token); + var failedTask = notifications.WaitForResourceAsync( + resourceName, KnownResourceStates.FailedToStart, token); + + try + { + var completed = await Task.WhenAny(readyTask, failedTask).ConfigureAwait(false); + if (completed == failedTask) + { + await failedTask.ConfigureAwait(false); + var repoRoot = TestBenchCoordinator.ResolveRepoRoot(); + var detail = await AspireResourceFailureDiagnostics + .DescribeAsync(resourceName, repoRoot, outerCancellationToken) + .ConfigureAwait(false); + throw new InvalidOperationException(detail); + } + + await readyTask.ConfigureAwait(false); + } + catch (OperationCanceledException) when (perResourceCts.IsCancellationRequested && !outerCancellationToken.IsCancellationRequested) + { + throw new TimeoutException( + $"Aspire resource '{resourceName}' did not reach Running within " + + $"{PerContainerReadyTimeout.TotalSeconds:0}s. The container is either still " + + "pulling an image, failing to build (e.g. missing 'docker buildx'), or stuck " + + "in a restart loop — inspect the AppHost container logs for details."); + } + } + + private static async Task RunScyllaMigrationsAsync( + Uri cqlEndpoint, + PersistenceConfiguration persistenceConfig, + ISecretsStore secretsStore, + CancellationToken cancellationToken) + { + // Pin the host-mapped endpoint so migrations hit the same CQL listener the API uses. + var overrides = new ScyllaOverrideOptions + { + ContactPoints = [cqlEndpoint.Host], + Port = cqlEndpoint.Port, + }; + var resolver = new ScyllaConfigResolver( + secretsStore, + Microsoft.Extensions.Options.Options.Create(overrides), + Microsoft.Extensions.Options.Options.Create(persistenceConfig)); + + await ScyllaMigrationService.MigrateAsync( + persistenceConfig, + secretsStore, + resolver, + NullLoggerFactory.Instance.CreateLogger(), + cancellationToken); + } + + private static PostgresSeedOptions BuildPostgresSeedOptions(Uri? cqlEndpoint) + => new( + InitUser: DbInitHelper.PostgresInitUser, + InitPassword: TestDbCredentials.PostgresInitPassword, + AppUser: TestDbCredentials.PostgresAppUser, + AppPassword: TestDbCredentials.PostgresAppPassword, + AdminUser: TestDbCredentials.PostgresAdminUser, + AdminPassword: TestDbCredentials.PostgresAdminPassword, + DefaultDatabase: DbInitHelper.DefaultPostgresDb, + GoogleOAuthClientSecret: "TEST", + DiscordOAuthClientSecret: "TEST", + AppleOAuthClientSecret: "TEST", + EncryptionPepper: "TEST", + // API runs on the host and reaches scylla/cassandra via the host port mapping; + // localhost placeholder keeps the row well-formed when no CQL backend is enabled. + ScyllaContactPoints: cqlEndpoint?.Host ?? "127.0.0.1", + ScyllaLocalDatacenter: "nam", + ScyllaAppUser: TestDbCredentials.ScyllaAppUser, + ScyllaAppPassword: TestDbCredentials.ScyllaAppPassword, + ScyllaPort: cqlEndpoint?.Port ?? 9042, + ScyllaAdminUser: TestDbCredentials.ScyllaAdminUser, + ScyllaAdminPassword: TestDbCredentials.ScyllaAdminPassword, + JwtRsa256PrivateKeyPem: TestDbCredentials.JwtRsa256PrivateKeyPem, + JwtEs256PrivateKeyPem: TestDbCredentials.JwtEs256PrivateKeyPem, + DeepLinkSecret: TestDbCredentials.DeepLinkSecret, + LeafPfxPassword: TestDbCredentials.LeafPfxPassword, + // Tests want db_init stable across idempotent reruns; production always scrambles. + ScrambleInitUserPassword: false); + + private static ScyllaSeedOptions BuildScyllaSeedOptions() + => new( + AppUser: TestDbCredentials.ScyllaAppUser, + AppPassword: TestDbCredentials.ScyllaAppPassword, + AdminUser: TestDbCredentials.ScyllaAdminUser, + AdminPassword: TestDbCredentials.ScyllaAdminPassword, + LockDefaultCassandra: true); + + private static async Task WaitForPostgresConnectivityAsync(string connectionString, CancellationToken ct) + { + for (var attempt = 1; attempt <= 20; attempt++) + { + try + { + await using var conn = new NpgsqlConnection(connectionString); + await conn.OpenAsync(ct); + return; + } + catch (Exception) when (attempt < 20 && !ct.IsCancellationRequested) + { + await Task.Delay(1000, ct); + } + } + } + + // Catches the PostgresMigrationService.MigrateAsync silent-skip trap: it returns + // cleanly when admin creds are absent from the secrets store, ApplyOnceAsync then + // marks migrations as applied on a return without an exception, and every subsequent + // hot-attach observer skips the migration too. The ledger table is created inside + // MigrateAsync's EnsureLedgerAsync, so its absence is a strict signal that no + // migration passed through, regardless of admin-creds or connection-string mistakes. + // Reads via db_init because SELECT on internal.schema_migrations is only granted to + // the admin role, and swaps the database to test_pg_db because the bench init string + // targets `postgres` for role/database bootstrapping. + private static async Task VerifyPostgresLedgerAppliedAsync(string initConnectionString, CancellationToken ct) + { + var verifyCs = new NpgsqlConnectionStringBuilder(initConnectionString) + { + Database = DbInitHelper.DefaultPostgresDb, + }.ConnectionString; + await using var conn = new NpgsqlConnection(verifyCs); + await conn.OpenAsync(ct).ConfigureAwait(false); + await using var existsCmd = new NpgsqlCommand( + "SELECT to_regclass('internal.schema_migrations') IS NOT NULL", conn); + var existsResult = await existsCmd.ExecuteScalarAsync(ct).ConfigureAwait(false); + var tableExists = existsResult is bool b && b; + var rowCount = 0L; + if (tableExists) + { + await using var countCmd = new NpgsqlCommand( + "SELECT count(*) FROM internal.schema_migrations", conn); + rowCount = (long)(await countCmd.ExecuteScalarAsync(ct).ConfigureAwait(false) ?? 0L); + } + if (!tableExists || rowCount == 0) + { + throw new InvalidOperationException( + $"SharedDbFixture: Postgres migrations did not populate internal.schema_migrations " + + $"(tableExists={tableExists}, rowCount={rowCount}). This usually means " + + "PostgresMigrationService.MigrateAsync short-circuited on missing admin " + + "credentials (PostgresAdmin{Username,Password} in internal.secrets); the " + + "ApplyOnceAsync marker would then strand every hot-attach on a schema-less DB."); + } + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/StubSpHandler.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/StubSpHandler.cs new file mode 100644 index 00000000..aa40e253 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/StubSpHandler.cs @@ -0,0 +1,89 @@ +using System.Collections.Concurrent; +using System.Net; +using System.Text; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// HttpMessageHandler that serves canned JSON responses for Simply Plural API URLs. +/// +/// Tests register responses by either exact path () +/// or by path prefix (), and assert against +/// / to validate request count +/// and ordering. Apparyllis is being sunset, so all SP coverage must be replay-based; +/// this handler is the single mock surface used by every SP import test. +/// +/// Unmatched paths return 404 so a forgotten stub fails loudly rather than silently. +/// Exact matches take precedence over prefix matches. +/// +public sealed class StubSpHandler : HttpMessageHandler +{ + private readonly ConcurrentDictionary _exact = new(StringComparer.Ordinal); + private readonly List<(string Prefix, string Body)> _prefixes = new(); + private readonly ConcurrentQueue _requests = new(); + private readonly object _prefixLock = new(); + + public StubSpHandler OnGet(string path, string responseBody) + { + _exact[path] = responseBody; + return this; + } + + public StubSpHandler OnGetPrefix(string pathPrefix, string responseBody) + { + lock (_prefixLock) + { + _prefixes.Add((pathPrefix, responseBody)); + } + return this; + } + + public IReadOnlyList Requests => _requests.ToArray(); + + public IReadOnlyList RequestsTo(string pathPrefix) + => _requests.Where(r => r.StartsWith(pathPrefix, StringComparison.Ordinal)).ToArray(); + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + // Strip query string for matching but keep it in the recorded URL so tests can + // inspect e.g. ?startTime=...&endTime=... ranges issued by ImportFrontsAsync. + var uri = request.RequestUri ?? throw new InvalidOperationException("StubSpHandler received a request without a URI."); + var pathAndQuery = uri.PathAndQuery; + var pathOnly = uri.AbsolutePath; + _requests.Enqueue(pathAndQuery); + + if (_exact.TryGetValue(pathOnly, out var body)) + { + return Task.FromResult(Json(body)); + } + + string? prefixBody = null; + lock (_prefixLock) + { + foreach (var (prefix, p) in _prefixes) + { + if (pathOnly.StartsWith(prefix, StringComparison.Ordinal)) + { + prefixBody = p; + break; + } + } + } + + if (prefixBody is not null) + { + return Task.FromResult(Json(prefixBody)); + } + + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound) + { + Content = new StringContent($"No StubSpHandler stub registered for {pathOnly}", Encoding.UTF8, "text/plain"), + }); + } + + private static HttpResponseMessage Json(string body) => + new(HttpStatusCode.OK) + { + Content = new StringContent(body, Encoding.UTF8, "application/json"), + }; +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/BenchConnectionInfo.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/BenchConnectionInfo.cs new file mode 100644 index 00000000..c840f9ec --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/BenchConnectionInfo.cs @@ -0,0 +1,11 @@ +namespace Interfold.IntegrationTests.Shared.TestServices.TestBench; + +/// Connection information returned by +/// so BenchSharedDb can attach to the running bench containers without going through +/// Aspire. Ports are the host-side bindings — everything binds to 127.0.0.1. +public sealed record BenchConnectionInfo( + string PostgresConnectionStringForInit, + string PostgresConnectionStringForApp, + int ScyllaPort, + int CassandraPort, + string LeaseFilePath); diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchCoordinator.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchCoordinator.cs new file mode 100644 index 00000000..1c1d488c --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchCoordinator.cs @@ -0,0 +1,609 @@ +extern alias AppHost; +using System.Diagnostics; +using System.Globalization; +using System.Net.Sockets; +using System.Reflection; // AssemblyMetadataAttribute; System.Reflection.Assembly is qualified inline (collides with TUnit.HookType.Assembly). +using AppHostRepoPaths = AppHost::Interfold.AppHost.AppHostRepoPaths; +using AspireResourceFailureDiagnostics = AppHost::Interfold.AppHost.AspireResourceFailureDiagnostics; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.IntegrationTests.Shared.TestServices.TestBench; + +/// Auto-managed centralised DB bench for integration tests. First test-host +/// attaches with a cold state → launches the AppHost as a detached child process in +/// , waits for the readiness marker, then +/// lets the AppHost exit (persistent-lifetime containers survive it). Subsequent attaches +/// see hot ports and skip the AppHost entirely. +/// +/// +/// Opt-in via — a null / empty / "0" value means the caller +/// falls back to the legacy per-project AspireFixture path. That's the CI default and +/// the escape hatch for anyone who wants the old behaviour locally. +/// +/// +/// Cross-process coordination: all state is under , +/// mutations are gated on . The reaper runs +/// under the same lock at the top of so exactly one +/// process decides teardown. +/// +/// +public static class TestBenchCoordinator +{ + /// Env-var override for the opt-in decision. Highest precedence; use "0" + /// or "false" to force the legacy path (CI leg) and "1" or "true" + /// to force bench mode regardless of the compile-time default. + public const string OptInEnvVar = "INTERFOLD_TEST_BENCH"; + + /// Assembly-metadata key the integration test csproj sets to "1" via + /// tests/Directory.Build.props. It's the only reliable channel we have because + /// supplied via a runsettings <EnvironmentVariables> + /// block never reaches the test-host under MTP + TUnit — MTP's dotnet test + /// integration doesn't include the vstest bridge that would honour that XML. Baking + /// the flag into the assembly at build time keeps dotnet test, direct DLL + /// launches, and IDE runners all in lockstep. + private const string OptInAssemblyMetadataKey = "InterfoldTestBenchOptIn"; + + /// Machine-readable marker TestBenchReadyEmitter writes to stdout once + /// every DB resource is Running. Keep in sync with the AppHost side. + private const string ReadyLinePrefix = "[test-bench] ready"; + + /// Fixed host ports — the bench uses one set forever so cross-process attach + /// can trust the numbers without discovery. + private static readonly BenchPorts DefaultPorts = new(); + + /// Brief state mutations (attach / detach / claim launcher). Must stay short — + /// long work (AppHost launch, migrations) runs outside the lock so parallel test + /// assemblies on a high-core machine don't burn the wait budget. + private static readonly TimeSpan LockTimeout = TimeSpan.FromMinutes(3); + private static readonly TimeSpan ApplyOnceLockTimeout = TimeSpan.FromMinutes(10); + private static readonly TimeSpan LauncherTimeout = TimeSpan.FromMinutes(10); + private static readonly TimeSpan PortProbeTimeout = TimeSpan.FromMilliseconds(500); + private static readonly TimeSpan PortPollInterval = TimeSpan.FromMilliseconds(500); + + /// Returns true when the current test host is opted in. Env-var override wins, + /// otherwise the compile-time attribute on the + /// entry assembly decides. Consumers use this to attach to the bench vs. legacy path. + public static bool IsOptedIn() + { + if (TryReadBoolWire(Environment.GetEnvironmentVariable(OptInEnvVar)) is bool envDecision) + return envDecision; + return TryReadBoolWire(GetAssemblyOptInValue()) ?? false; + } + + private static string? GetAssemblyOptInValue() + { + // Read the opt-in flag ONLY from the test-host's entry assembly (e.g. + // Interfold.Auth.IntegrationTests.dll). tests/Directory.Build.targets injects the + // metadata at the leaf csproj level, honouring the per-project + // override — a leaf that opts out (Infrastructure, for + // MigrationLedgerTests) simply has NO metadata baked in and IsOptedIn() therefore + // falls through to false. + // + // Do NOT fall back to typeof(TestBenchCoordinator).Assembly — Interfold.IntegrationTests.Shared + // also lives under tests/integration/ and therefore also carries the opt-in metadata, + // which would silently override every leaf's opt-out. The July 2026 concurrent run + // showed exactly that failure mode: Infrastructure ran against the shared bench, + // peer projects' migrations wrote extra ledger rows, and + // Scylla_RerunningMigrations_IsANoOp saw 14 → 16 between snapshots. + return FindMetadataValue(System.Reflection.Assembly.GetEntryAssembly()); + } + + private static string? FindMetadataValue(System.Reflection.Assembly? assembly) + { + if (assembly is null) return null; + foreach (var attr in assembly.GetCustomAttributes()) + { + if (string.Equals(attr.Key, OptInAssemblyMetadataKey, StringComparison.Ordinal)) + return attr.Value; + } + return null; + } + + private static bool? TryReadBoolWire(string? raw) + { + if (string.IsNullOrEmpty(raw)) return null; + if (string.Equals(raw, "0", StringComparison.Ordinal) + || string.Equals(raw, "false", StringComparison.OrdinalIgnoreCase)) + return false; + if (string.Equals(raw, "1", StringComparison.Ordinal) + || string.Equals(raw, "true", StringComparison.OrdinalIgnoreCase)) + return true; + return null; + } + + /// Ensures the bench containers are running and returns connection info. May + /// spawn a detached AppHost process on cold-boot. Cross-process safe. + /// + /// The advisory lock is held only for short state mutations. AppHost launch runs + /// outside the lock: a marker elects a + /// single launcher while peers poll ports (and re-elect if the launcher dies or clears + /// the marker). Holding the lock across launch was the failure mode on high-core + /// dotnet test runs (60s waiters vs multi-minute boot). + /// + public static async Task EnsureRunningAsync(CancellationToken ct) + { + var ports = DefaultPorts; + var deadline = DateTime.UtcNow + LauncherTimeout; + var launchedThisCall = false; + + while (true) + { + ct.ThrowIfCancellationRequested(); + if (DateTime.UtcNow >= deadline) + { + throw new TimeoutException( + $"Timed out after {LauncherTimeout.TotalMinutes:0} minutes waiting for the test-bench " + + $"(pg={ports.Postgres}, scylla={ports.Scylla}, cassandra={ports.Cassandra})."); + } + + bool shouldLaunch; + using (await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false)) + { + var state = TestBenchLease.ReadState(); + state.Users = TestBenchLease.PruneStale(state.Users); + if (state.Launching is { } launching && !TestBenchLease.IsAlive(launching)) + state.Launching = null; + + if (await TestBenchIdleReaper.RunIfIdleAsync(state, ct).ConfigureAwait(false)) + state = new TestBenchLeaseState(); + + ports = state.Ports ?? DefaultPorts; + + if (await ArePortsHotAsync(ports, ct).ConfigureAwait(false)) + { + state.Ports = ports; + state.Launching = null; + state.LastAttachUtc = DateTime.UtcNow; + state.Users.Add(TestBenchLease.CurrentUser()); + TestBenchLease.WriteState(state); + return BuildConnectionInfo(ports); + } + + shouldLaunch = state.Launching is null; + if (shouldLaunch) + { + state.Launching = TestBenchLease.CurrentUser("test-bench-launcher"); + state.Ports = ports; + TestBenchLease.WriteState(state); + } + else + { + TestBenchLease.WriteState(state); + } + } + + if (shouldLaunch) + { + Console.WriteLine("[test-bench] cold ports; launching AppHost to boot containers"); + try + { + await DockerMemoryPreflight.EnsureAdequateAsync(ct).ConfigureAwait(false); + ScyllaRackDcMountPaths.VerifyAllPresent(ResolveRepoRoot()); + await LaunchAppHostAsync(ports, ct).ConfigureAwait(false); + } + catch + { + await ClearLaunchingMarkerAsync(CancellationToken.None).ConfigureAwait(false); + throw; + } + + launchedThisCall = true; + break; + } + + Console.WriteLine("[test-bench] cold ports; waiting for peer AppHost launcher"); + var remaining = deadline - DateTime.UtcNow; + if (remaining <= TimeSpan.Zero) + continue; + + var peerOutcome = await WaitForPortsOrLauncherAbandonedAsync(ports, remaining, ct).ConfigureAwait(false); + if (peerOutcome == PeerWaitOutcome.PortsHot) + break; + + Console.WriteLine("[test-bench] peer launcher abandoned; retrying claim"); + } + + return await FinishAttachAsync(ports, wipeMigrations: launchedThisCall, ct).ConfigureAwait(false); + } + + private static async Task FinishAttachAsync( + BenchPorts ports, bool wipeMigrations, CancellationToken ct) + { + using var _ = await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + state.Users = TestBenchLease.PruneStale(state.Users); + + if (!await ArePortsHotAsync(ports, ct).ConfigureAwait(false)) + { + state.Launching = null; + TestBenchLease.WriteState(state); + throw new TimeoutException( + $"Test-bench ports (pg={ports.Postgres}, scylla={ports.Scylla}, cassandra={ports.Cassandra}) " + + "were not accepting connections after AppHost launch / peer wait."); + } + + // Fresh containers → recorded migration markers are stale. + if (wipeMigrations) + state.MigrationsApplied.Clear(); + + state.Ports = ports; + state.Launching = null; + state.LastAttachUtc = DateTime.UtcNow; + state.Users.Add(TestBenchLease.CurrentUser()); + TestBenchLease.WriteState(state); + return BuildConnectionInfo(ports); + } + + private enum PeerWaitOutcome + { + PortsHot, + LauncherAbandoned, + } + + /// Peer wait: ports becoming hot wins; a cleared/dead Launching marker + /// means we should loop and try to claim the launcher role ourselves. + private static async Task WaitForPortsOrLauncherAbandonedAsync( + BenchPorts ports, TimeSpan timeout, CancellationToken ct) + { + var deadline = DateTime.UtcNow + timeout; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + if (await ArePortsHotAsync(ports, ct).ConfigureAwait(false)) + return PeerWaitOutcome.PortsHot; + + var state = TestBenchLease.ReadState(); + if (state.Launching is null + || !TestBenchLease.IsAlive(state.Launching)) + { + return PeerWaitOutcome.LauncherAbandoned; + } + + await Task.Delay(PortPollInterval, ct).ConfigureAwait(false); + } + + throw new TimeoutException( + $"Timed out waiting {timeout.TotalMinutes:0.##} minutes for test-bench ports " + + $"(pg={ports.Postgres}, scylla={ports.Scylla}, cassandra={ports.Cassandra}) " + + "after a peer claimed the AppHost launcher role."); + } + + private static async Task ClearLaunchingMarkerAsync(CancellationToken ct) + { + try + { + using var _ = await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + state.Launching = null; + TestBenchLease.WriteState(state); + } + catch + { + // Best-effort — a stuck marker is cleared by IsAlive prune on the next attach. + } + } + + /// Cross-process one-shot for idempotent bench-setup work (DB migrations, + /// keyspace seeds, etc.). The delegate is invoked exactly once per bench-lifetime; + /// concurrent callers elect a single worker, then poll the completed marker. + /// + /// The file lock is held only for claim / complete mutations — never across + /// . Scylla/Cassandra seed+migrate can run for minutes; holding + /// bench.lock that long caused peer assemblies to hit + /// while waiting to attach. Exclusion for + /// non-concurrency-safe seeds (e.g. CREATE DATABASE) comes from + /// , same pattern as + /// . + /// + public static async Task ApplyOnceAsync(string key, Func work, CancellationToken ct) + { + if (!IsOptedIn()) + { + await work().ConfigureAwait(false); + return; + } + + var deadline = DateTime.UtcNow + ApplyOnceLockTimeout; + while (true) + { + ct.ThrowIfCancellationRequested(); + var remaining = deadline - DateTime.UtcNow; + if (remaining <= TimeSpan.Zero) + { + throw new TimeoutException( + $"Timed out waiting {ApplyOnceLockTimeout.TotalMinutes:0} minutes for " + + $"test-bench ApplyOnce key '{key}' (peer migration still in progress or abandoned)."); + } + + var lockWait = remaining < LockTimeout ? remaining : LockTimeout; + var claim = await TryClaimApplyOnceAsync(key, lockWait, ct).ConfigureAwait(false); + if (claim == ApplyOnceClaim.AlreadyDone) + return; + + if (claim == ApplyOnceClaim.PeerRunning) + { + await Task.Delay(PortPollInterval, ct).ConfigureAwait(false); + var snap = TestBenchLease.ReadState(); + if (snap.MigrationsApplied.TryGetValue(key, out var done) && done) + return; + continue; + } + + try + { + await work().ConfigureAwait(false); + await CompleteApplyOnceAsync(key, ct).ConfigureAwait(false); + return; + } + catch + { + await ClearApplyOnceClaimAsync(key, CancellationToken.None).ConfigureAwait(false); + throw; + } + } + } + + private enum ApplyOnceClaim + { + AlreadyDone, + PeerRunning, + Claimed, + } + + private static async Task TryClaimApplyOnceAsync( + string key, TimeSpan lockTimeout, CancellationToken ct) + { + using var _ = await TestBenchLease.AcquireAsync(lockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + if (state.MigrationsApplied.TryGetValue(key, out var applied) && applied) + return ApplyOnceClaim.AlreadyDone; + + if (state.MigrationsInProgress.TryGetValue(key, out var worker) + && TestBenchLease.IsAlive(worker)) + { + return ApplyOnceClaim.PeerRunning; + } + + state.MigrationsInProgress[key] = TestBenchLease.CurrentUser($"apply-once:{key}"); + TestBenchLease.WriteState(state); + return ApplyOnceClaim.Claimed; + } + + private static async Task CompleteApplyOnceAsync(string key, CancellationToken ct) + { + using var _ = await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + state.MigrationsApplied[key] = true; + state.MigrationsInProgress.Remove(key); + state.LastAttachUtc = DateTime.UtcNow; + TestBenchLease.WriteState(state); + } + + private static async Task ClearApplyOnceClaimAsync(string key, CancellationToken ct) + { + try + { + using var _ = await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + state.MigrationsInProgress.Remove(key); + TestBenchLease.WriteState(state); + } + catch + { + // Best-effort — a stuck claim is cleared by IsAlive prune on the next claim. + } + } + + /// Removes the current process from the lease's users list. Never throws; + /// bench teardown happens asynchronously via the reaper on the next attach. + public static async Task DetachAsync(CancellationToken ct) + { + try + { + using var _ = await TestBenchLease.AcquireAsync(LockTimeout, ct).ConfigureAwait(false); + var state = TestBenchLease.ReadState(); + var currentPid = Environment.ProcessId; + state.Users = state.Users + .Where(u => u.Pid != currentPid) + .ToList(); + state.Users = TestBenchLease.PruneStale(state.Users); + state.LastAttachUtc = DateTime.UtcNow; + TestBenchLease.WriteState(state); + } + catch + { + // Best-effort — never fail teardown because of a stuck lock or filesystem hiccup. + } + } + + /// Convenience: returns the postgres init connection string with app credentials + /// swapped in for a caller that already knows the bench is up. + public static BenchConnectionInfo BuildConnectionInfo(BenchPorts ports) + { + var initCs = + $"Host=127.0.0.1;Port={ports.Postgres};" + + $"Username={DbInitHelper.PostgresInitUser};Password={TestDbCredentials.PostgresInitPassword};" + + "Database=postgres;SSL Mode=Disable"; + var appCs = + $"Host=127.0.0.1;Port={ports.Postgres};" + + $"Username={TestDbCredentials.PostgresAppUser};Password={TestDbCredentials.PostgresAppPassword};" + + $"Database={DbInitHelper.DefaultPostgresDb};SSL Mode=Disable;Maximum Pool Size=5"; + return new BenchConnectionInfo( + PostgresConnectionStringForInit: initCs, + PostgresConnectionStringForApp: appCs, + ScyllaPort: ports.Scylla, + CassandraPort: ports.Cassandra, + LeaseFilePath: TestBenchLease.GetStateFilePath()); + } + + /// Probes all three fixed ports with a bounded timeout. All three must respond + /// or we take the cold path — a half-up bench needs the launcher to bring the missing + /// engine back up. + private static async Task ArePortsHotAsync(BenchPorts ports, CancellationToken ct) + { + return await ProbeAsync(ports.Postgres, ct).ConfigureAwait(false) + && await ProbeAsync(ports.Scylla, ct).ConfigureAwait(false) + && await ProbeAsync(ports.Cassandra, ct).ConfigureAwait(false); + } + + private static async Task ProbeAsync(int port, CancellationToken ct) + { + try + { + using var tcp = new TcpClient(); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); + cts.CancelAfter(PortProbeTimeout); + await tcp.ConnectAsync("127.0.0.1", port, cts.Token).ConfigureAwait(false); + return tcp.Connected; + } + catch + { + return false; + } + } + + private static async Task LaunchAppHostAsync(BenchPorts ports, CancellationToken ct) + { + var repoRoot = ResolveRepoRoot(); + var appHostCsproj = Path.Combine(repoRoot, "hosts", "Interfold.AppHost", "Interfold.AppHost.csproj"); + if (!File.Exists(appHostCsproj)) + throw new FileNotFoundException( + $"Test-bench launcher could not locate the AppHost csproj at '{appHostCsproj}'. " + + "The bench requires the AppHost project to be present in the repo tree.", + appHostCsproj); + + var psi = new ProcessStartInfo("dotnet") + { + WorkingDirectory = repoRoot, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + // Skip launchSettings.json — it pins ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL=22223 + // (and AppHost Kestrel URLs) that collide with Infrastructure's legacy AspireFixture + // when both run under a parallel `dotnet test`. `--no-build` avoids a rebuild race + // while peer test assemblies hold AppHost output DLLs open. + psi.ArgumentList.Add("run"); + psi.ArgumentList.Add("--no-launch-profile"); + psi.ArgumentList.Add("--no-build"); + psi.ArgumentList.Add("--project"); + psi.ArgumentList.Add(appHostCsproj); + psi.ArgumentList.Add("--"); + foreach (var arg in BuildAppHostConfigArgs(ports)) + psi.ArgumentList.Add(arg); + + AspireProcessEndpoints.ApplyTo( + psi, + AspireProcessEndpoints.BenchResourceService, + AspireProcessEndpoints.BenchOtlp, + AspireProcessEndpoints.BenchAppUrls); + + using var proc = new Process { StartInfo = psi, EnableRaisingEvents = true }; + var readyTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var stderrBuf = new System.Text.StringBuilder(); + + proc.OutputDataReceived += (_, e) => + { + if (e.Data is null) return; + // Echo the child's stdout to the parent so operators can diagnose slow boots. + Console.WriteLine(e.Data); + if (e.Data.StartsWith(ReadyLinePrefix, StringComparison.Ordinal)) + readyTcs.TrySetResult(true); + }; + proc.ErrorDataReceived += (_, e) => + { + if (e.Data is null) return; + stderrBuf.AppendLine(e.Data); + Console.Error.WriteLine(e.Data); + }; + + proc.Start(); + proc.BeginOutputReadLine(); + proc.BeginErrorReadLine(); + + try + { + using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct); + timeoutCts.CancelAfter(LauncherTimeout); + using var reg = timeoutCts.Token.Register(() => readyTcs.TrySetCanceled(timeoutCts.Token)); + var exitTask = proc.WaitForExitAsync(timeoutCts.Token); + var completed = await Task.WhenAny(readyTcs.Task, exitTask).ConfigureAwait(false); + if (completed == exitTask) + { + await exitTask.ConfigureAwait(false); + var detail = await EnrichLaunchFailureAsync(stderrBuf.ToString(), ct).ConfigureAwait(false); + throw new InvalidOperationException( + $"Test-bench AppHost exited (code={proc.ExitCode}) before emitting '{ReadyLinePrefix}'. " + + $"stderr:{Environment.NewLine}{detail}"); + } + await readyTcs.Task.ConfigureAwait(false); + + // AppHost prints ready and StopApplication'd itself in TestBenchReadyEmitter; + // wait briefly for the graceful shutdown, then kill if it drags its feet. + try + { + using var gracefulCts = CancellationTokenSource.CreateLinkedTokenSource(ct); + gracefulCts.CancelAfter(TimeSpan.FromSeconds(30)); + await proc.WaitForExitAsync(gracefulCts.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + try { proc.Kill(entireProcessTree: true); } catch { /* best-effort */ } + } + } + catch (OperationCanceledException) when (!ct.IsCancellationRequested) + { + try { proc.Kill(entireProcessTree: true); } catch { /* best-effort */ } + var detail = await EnrichLaunchFailureAsync(stderrBuf.ToString(), ct).ConfigureAwait(false); + throw new TimeoutException( + $"Test-bench AppHost did not emit '{ReadyLinePrefix}' within {LauncherTimeout.TotalMinutes:0} minutes. " + + $"stderr:{Environment.NewLine}{detail}"); + } + } + + private static async Task EnrichLaunchFailureAsync(string stderr, CancellationToken ct) + { + if (stderr.Contains("ready-emitter failed", StringComparison.OrdinalIgnoreCase) + && stderr.Contains("scylla", StringComparison.OrdinalIgnoreCase)) + { + var repoRoot = ResolveRepoRoot(); + var diagnosis = await AspireResourceFailureDiagnostics + .DescribeAsync("scylla", repoRoot, ct) + .ConfigureAwait(false); + return string.IsNullOrWhiteSpace(stderr) + ? diagnosis + : stderr.TrimEnd() + Environment.NewLine + diagnosis; + } + + return stderr; + } + + private static IEnumerable BuildAppHostConfigArgs(BenchPorts ports) + { + // Use CommandLine configuration syntax `--key=value` (Microsoft.Extensions.Configuration.CommandLine + // parses these into IConfiguration). Aspire's DistributedApplicationBuilder inherits + // the default config providers so these overrides land on the same keys the + // InterfoldAppHost reads. + yield return $"--{AppHostParameterKeys.TestBenchMode}=true"; + yield return $"--{AppHostParameterKeys.IncludeApi}=false"; + yield return $"--{AppHostParameterKeys.IncludeWeb}=false"; + yield return $"--{AppHostParameterKeys.IncludeDashboard}=false"; + yield return $"--{AppHostParameterKeys.PersistentContainers}=true"; + yield return $"--{AppHostParameterKeys.IncludePostgres}=true"; + yield return $"--{AppHostParameterKeys.IncludeScylla}=true"; + yield return $"--{AppHostParameterKeys.IncludeCassandra}=true"; + yield return $"--{AppHostParameterKeys.PortsPostgres}={ports.Postgres.ToString(CultureInfo.InvariantCulture)}"; + yield return $"--{AppHostParameterKeys.PortsScylla}={ports.Scylla.ToString(CultureInfo.InvariantCulture)}"; + yield return $"--{AppHostParameterKeys.PortsCassandra}={ports.Cassandra.ToString(CultureInfo.InvariantCulture)}"; + yield return $"--{AppHostParameterKeys.PostgresUser}={TestDbCredentials.PostgresAppUser}"; + yield return $"--{AppHostParameterKeys.PostgresPassword}={TestDbCredentials.PostgresAppPassword}"; + yield return $"--{AppHostParameterKeys.PostgresInitPassword}={TestDbCredentials.PostgresInitPassword}"; + yield return $"--{AppHostParameterKeys.PostgresDb}={DbInitHelper.DefaultPostgresDb}"; + yield return $"--{AppHostParameterKeys.ScyllaUser}={TestDbCredentials.ScyllaAppUser}"; + yield return $"--{AppHostParameterKeys.ScyllaPassword}={TestDbCredentials.ScyllaAppPassword}"; + yield return $"--{AppHostParameterKeys.EncryptionPrivateKey}=TEST"; + } + + public static string ResolveRepoRoot() => AppHostRepoPaths.ResolveRepoRoot(); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchIdleReaper.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchIdleReaper.cs new file mode 100644 index 00000000..e3686bce --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchIdleReaper.cs @@ -0,0 +1,104 @@ +using System.Diagnostics; +using System.Globalization; + +namespace Interfold.IntegrationTests.Shared.TestServices.TestBench; + +/// Opportunistically tears down the bench when it's been idle. Invoked at the top +/// of under the same cross-process +/// lock, so exactly one reaper decision runs at a time. Runs docker rm -f against +/// the three pinned container names from . No named +/// volumes need cleaning up: bench containers hold their data in their own filesystem +/// (see InterfoldAppHost.cs where testBenchMode takes the +/// WithLifetime(Persistent) branch instead of AsPersistent(volumeName, path)), +/// so docker rm -f discards everything in one call and the next cold-boot always +/// gets a fresh init with the current TestDbCredentials. +public static class TestBenchIdleReaper +{ + public static readonly TimeSpan DefaultIdleTimeout = TimeSpan.FromMinutes(15); + private const string IdleTimeoutEnv = "INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES"; + + /// Returns the idle timeout — the env var when set to a positive integer, + /// otherwise . + public static TimeSpan ResolveIdleTimeout() + { + var raw = Environment.GetEnvironmentVariable(IdleTimeoutEnv); + if (!string.IsNullOrWhiteSpace(raw) + && int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var minutes) + && minutes > 0) + { + return TimeSpan.FromMinutes(minutes); + } + return DefaultIdleTimeout; + } + + /// Runs the reaper decision. If the lease has been idle longer than the + /// timeout AND no live user PIDs remain, docker rm -f the bench containers and + /// delete the state file. Returns true when a teardown actually ran. + public static async Task RunIfIdleAsync(TestBenchLeaseState state, CancellationToken ct) + { + var idle = DateTime.UtcNow - state.LastAttachUtc; + var timeout = ResolveIdleTimeout(); + + // Empty users list is common on the very first attach — the LastAttachUtc default + // (DateTime.MinValue) drives the age check to a huge positive value which would + // spuriously trip the reaper. Skip when there is nothing yet to reap. + if (state.LastAttachUtc == DateTime.MinValue) return false; + if (state.Users.Count > 0) return false; + if (idle < timeout) return false; + + await TeardownAsync(ct).ConfigureAwait(false); + TestBenchLease.DeleteState(); + return true; + } + + /// Force-remove the bench containers unconditionally. Used by tests and by + /// external scripts that want to nuke the bench without waiting for the idle window. + public static async Task TeardownAsync(CancellationToken ct) + { + foreach (var name in TestBenchContainerNames.All) + { + // `-v` removes anonymous volumes attached to the container. The postgres, + // scylla, and cassandra images all declare a VOLUME for their data dir, so + // docker auto-creates an anonymous volume per container even though bench mode + // never explicitly mounts one. Without `-v` those anonymous volumes accumulate + // across every reap cycle and slowly fill /var/lib/docker. + await DockerAsync(["rm", "-f", "-v", name], ct).ConfigureAwait(false); + } + } + + private static async Task DockerAsync(IEnumerable args, CancellationToken ct) + { + var psi = new ProcessStartInfo("docker") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var a in args) psi.ArgumentList.Add(a); + + try + { + using var proc = Process.Start(psi); + if (proc is null) return -1; + await proc.WaitForExitAsync(ct).ConfigureAwait(false); + return proc.ExitCode; + } + catch + { + return -1; + } + } +} + +/// Fixed container names for bench mode. Values match +/// Interfold.AppHost.TestBenchContainerNames — keep in sync. +public static class TestBenchContainerNames +{ + public const string Postgres = "interfold-test-bench-pg"; + public const string Scylla = "interfold-test-bench-scylla"; + public const string Cassandra = "interfold-test-bench-cassandra"; + + public static readonly IReadOnlyList All = [Postgres, Scylla, Cassandra]; +} + diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchLease.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchLease.cs new file mode 100644 index 00000000..47e291aa --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestBench/TestBenchLease.cs @@ -0,0 +1,224 @@ +using System.Diagnostics; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Interfold.IntegrationTests.Shared.TestServices.TestBench; + +/// Advisory cross-process lock + JSON state file backing the auto-managed test +/// bench. The state file records which test-host PIDs currently attach to the bench and +/// the last-attach timestamp used by the idle reaper to decide when to docker rm -f +/// the persistent-lifetime containers. +/// +/// +/// Lock semantics: a plain opened with +/// on bench.lock. Cross-platform, best-effort — an OS-level crash leaves the file +/// but not the lock, so a stale lock file is harmless. Retry with a 100 ms backoff up to +/// the caller-supplied timeout. +/// +/// +/// The state file is written whole-file atomically (temp + rename) so a torn write can +/// never leave partial JSON behind. Readers tolerate a missing/empty file by treating it +/// as a fresh lease. +/// +/// +public static class TestBenchLease +{ + private const string LeaseDirectoryName = "interfold-test-bench"; + private const string LockFileName = "bench.lock"; + private const string StateFileName = "bench.state.json"; + + private static readonly JsonSerializerOptions StateJsonOptions = new() + { + WriteIndented = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Converters = { new JsonStringEnumConverter() }, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + }; + + /// Test-only override of the lease's base directory. The unit tests + /// link-compile TestBenchLease.cs into their own assembly so they get their + /// own writable copy of this setter; production callers never touch it. + internal static string? TestOnlyBaseDirectoryOverride { get; set; } + + /// Returns the lease directory rooted under the current user's temp path. + /// Created on demand. + public static string GetLeaseDirectory() + { + var baseDir = TestOnlyBaseDirectoryOverride ?? Path.GetTempPath(); + var dir = Path.Combine(baseDir, LeaseDirectoryName); + Directory.CreateDirectory(dir); + return dir; + } + + public static string GetLockFilePath() => Path.Combine(GetLeaseDirectory(), LockFileName); + + public static string GetStateFilePath() => Path.Combine(GetLeaseDirectory(), StateFileName); + + /// Acquires the cross-process advisory lock on . + /// Retries every 100 ms until elapses. Throws + /// on timeout so bench init fails loudly rather than + /// silently sharing the lease. + public static async Task AcquireAsync(TimeSpan timeout, CancellationToken ct) + { + var path = GetLockFilePath(); + var deadline = DateTime.UtcNow + timeout; + Exception? last = null; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + try + { + // FileShare.None gives us cross-process exclusion; the stream itself is + // deliberately zero-content — we care about the lock, not the bytes. + var stream = new FileStream( + path, + FileMode.OpenOrCreate, + FileAccess.ReadWrite, + FileShare.None, + bufferSize: 1, + FileOptions.DeleteOnClose | FileOptions.WriteThrough); + return new LockHandle(stream); + } + catch (IOException ex) + { + last = ex; + await Task.Delay(100, ct).ConfigureAwait(false); + } + } + throw new TimeoutException( + $"Failed to acquire test-bench lock at '{path}' within {timeout.TotalSeconds:0}s. " + + "Last error: " + (last?.Message ?? "unknown")); + } + + /// Loads the lease state from disk. Returns a fresh empty lease if the file + /// is missing, empty, or malformed. + public static TestBenchLeaseState ReadState() + { + var path = GetStateFilePath(); + try + { + if (!File.Exists(path)) return new TestBenchLeaseState(); + var text = File.ReadAllText(path); + if (string.IsNullOrWhiteSpace(text)) return new TestBenchLeaseState(); + return JsonSerializer.Deserialize(text, StateJsonOptions) + ?? new TestBenchLeaseState(); + } + catch (JsonException) + { + // Malformed — treat as fresh. The reaper will overwrite the next time it acts. + return new TestBenchLeaseState(); + } + } + + /// Persists the lease state to via a temp-file + /// rename so a torn write cannot corrupt the file readers observe. + public static void WriteState(TestBenchLeaseState state) + { + var path = GetStateFilePath(); + var tmp = path + ".tmp"; + var json = JsonSerializer.Serialize(state, StateJsonOptions); + File.WriteAllText(tmp, json); + // File.Move overwrite is atomic within the same filesystem on both Linux and Windows. + File.Move(tmp, path, overwrite: true); + } + + /// Deletes the state file. The lock file self-deletes when its handle closes. + public static void DeleteState() + { + var path = GetStateFilePath(); + try { File.Delete(path); } + catch (FileNotFoundException) { } + catch (DirectoryNotFoundException) { } + } + + /// Removes entries in whose PID no longer resolves to + /// a running process. Used to keep the "still-attached" list honest across crash/kill. + public static List PruneStale(IReadOnlyList users) + { + var alive = new List(users.Count); + foreach (var user in users) + { + if (IsAlive(user)) alive.Add(user); + } + return alive; + } + + /// Returns true when 's recorded PID still resolves to a + /// running process whose creation time matches . + /// The creation-time cross-check guards against PID reuse between test runs (Linux PID + /// wraparound is short in dense CI environments). + public static bool IsAlive(TestBenchUser user) + { + try + { + using var proc = Process.GetProcessById(user.Pid); + // StartTime accessor throws on some kernel-thread edge cases; wrap defensively. + var started = proc.StartTime.ToUniversalTime(); + // Allow 500ms of slop for filesystem timestamp rounding. + return Math.Abs((started - user.PidCreatedUtc).TotalSeconds) < 1.0; + } + catch + { + return false; + } + } + + /// Snapshot of this process's identity for the users list. + public static TestBenchUser CurrentUser(string? testHostName = null) + { + using var current = Process.GetCurrentProcess(); + return new TestBenchUser( + Pid: current.Id, + PidCreatedUtc: current.StartTime.ToUniversalTime(), + TestHost: testHostName ?? current.ProcessName); + } + + private sealed class LockHandle : IDisposable + { + private FileStream? _stream; + + public LockHandle(FileStream stream) { _stream = stream; } + + public void Dispose() + { + var s = Interlocked.Exchange(ref _stream, null); + if (s is null) return; + try { s.Dispose(); } + catch { /* best-effort */ } + } + } +} + +/// Serialised lease payload. See . +public sealed class TestBenchLeaseState +{ + public DateTime LastAttachUtc { get; set; } + public List Users { get; set; } = new(); + public BenchPorts Ports { get; set; } = new(); + /// Migration marker per engine (postgres / scylla / cassandra). + /// Advisory only — the migrators themselves are idempotent, but skipping them on hot attach + /// shaves seconds off every subsequent test-host boot. + public Dictionary MigrationsApplied { get; set; } = new(); + /// Process currently executing an ApplyOnceAsync key. Peers wait on the + /// completed marker (or re-elect if the worker dies) without holding bench.lock + /// for the duration of Scylla/Cassandra seed+migrate. + public Dictionary MigrationsInProgress { get; set; } = new(); + /// Process currently cold-booting the AppHost. Peers observe a live launcher + /// and wait on ports instead of spawning a second AppHost. Cleared when ports are hot + /// or the launcher dies / fails. + public TestBenchUser? Launching { get; set; } +} + +/// Fixed-port bindings for the bench containers. Ports are the host-side numbers +/// that both probes and passes to the AppHost so +/// discovery + attach stay in lockstep. +public sealed class BenchPorts +{ + public int Postgres { get; set; } = 14200; + public int Scylla { get; set; } = 19042; + public int Cassandra { get; set; } = 19043; +} + +/// A single test-host process attached to the bench. PID + StartTime doubles as +/// a poor man's process identity so a reused PID cannot masquerade as an old attach. +public sealed record TestBenchUser(int Pid, DateTime PidCreatedUtc, string TestHost); diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestClient.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestClient.cs new file mode 100644 index 00000000..e79a42a6 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestClient.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc.Testing; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Factory helpers for creating instances with common options. +/// Centralises the new WebApplicationFactoryClientOptions { AllowAutoRedirect = false } +/// boilerplate that appeared at 42 sites across the integration-test suite. +/// +public static class TestClient +{ + private static readonly WebApplicationFactoryClientOptions NoRedirectOptions = new() + { + AllowAutoRedirect = false, + }; + + /// + /// Creates an from the session-shared factory with + /// AllowAutoRedirect = false. + /// + public static HttpClient NoRedirect(IWebFactoryFixture fixture) + => fixture.Factory.CreateClient(NoRedirectOptions); + + /// + /// Creates an from a caller-owned factory with + /// AllowAutoRedirect = false. Use when the test builds its own + /// in-body. + /// + public static HttpClient NoRedirect(InterfoldWebApplicationFactory factory) + => factory.CreateClient(NoRedirectOptions); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestDbCredentials.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestDbCredentials.cs new file mode 100644 index 00000000..dea935b2 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestDbCredentials.cs @@ -0,0 +1,109 @@ +using System.Security.Cryptography; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Deterministic credentials shared by the consolidated and +/// the multi-DC topology fixture (). Hard-coded so the +/// AppHost Args, the in-process seeder, and any assertions further downstream agree +/// on the exact values without having to read the auto-generated parameter defaults back +/// out of the service provider. +/// +/// +/// These match the legacy per-fixture private const blocks one-for-one — extracted +/// here so a future credential rotation only touches one place. None of these values are +/// real secrets; they exist purely to keep the in-test cluster authenticatable. +/// +/// The JWT keypair PEMs below are DETERMINISTIC — baked in as constants rather than +/// generated at process start — because under the centralised test-bench every leaf +/// integration project shares one Postgres, and +/// short-circuits step 4 (internal.secrets upsert) once the app role is provisioned. +/// A per-process Lazy<ECDsa.Create> would leave the first cold-booter's public +/// key authoritative in the DB — every subsequent test-host would sign tokens with a +/// different key and the API would reject them with Invalid JWT signature: tested 1 +/// verification key(s) but none matched. Baking the PEM in guarantees signer / verifier +/// agreement across processes. +/// +/// Regenerate via a one-off snippet if the shape ever needs to change: +/// +/// using var rsa = RSA.Create(2048); +/// Console.WriteLine(rsa.ExportPkcs8PrivateKeyPem()); +/// using var ec = ECDsa.Create(ECCurve.NamedCurves.nistP256); +/// Console.WriteLine(ec.ExportECPrivateKeyPem()); +/// +/// +public static class TestDbCredentials +{ + public const string PostgresInitPassword = "test_init_pw_789!Bootstrap"; + public const string PostgresAppUser = "test_pg_user"; + public const string PostgresAppPassword = "test_pg_pw_789!Secure"; + public const string PostgresAdminUser = "test_pg_user_admin"; + public const string PostgresAdminPassword = "test_pg_admin_pw_456!Strong"; + public const string ScyllaAppUser = "test_app_user"; + public const string ScyllaAppPassword = "test_secure_pw_123!Safe"; + public const string ScyllaAdminUser = "test_app_user_admin"; + public const string ScyllaAdminPassword = "test_admin_pw_456!Strong"; + + public const string DeepLinkSecret = "test_deep_link_secret_0123456789abcdefghijklmnopqrstuv"; + public const string LeafPfxPassword = "test_leaf_pfx_pw_0123!Strong"; + + public const string JwtRsa256PrivateKeyPem = """ + -----BEGIN PRIVATE KEY----- + MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCi/EMiHUj5rcmb + F5QkZ5FBghIezaPBUhVVfPrXn26KqR9QXVYYXWLrZNTuxdxazNt8gM6br6yrlnW9 + W8C659Dz0G1j1mZGQVAVKxxfkfncdIOqYk9VPeD27JN+ysqTUvy//N9LaG/LD3gx + 3BL/nyS0j1WCoGmtBNRKKypQt5pJkujvaEEcTUD4ZcCNd7HLjWL+rnpjU1mXQfJH + dAUuo9qCObfsacGN1W4Y0DVrtWb6+wI/v5WzWIa9PR19UpS50n+H/gKuAdCv5v2f + gzH69wFe+CwKZF4q7Z9Axkd3CDzP+Nhuc9Vxncm+DoA7JEiKHCCe6N3F3bNeBawv + j7EVrhi7AgMBAAECggEABTx4hayOLyNTsk1gH4lKQ+IDZgTySWAzOkNhJbNEEGVd + ka3l+bNqXaioJKGrsikZthbHTH4o3HxhfPtpJjgTYPnlwcreD0zC32M6rqFYdyox + HrS42sK5qwUvueeW+YO1hg+ANwJ8cPgmwudQnvDEc56VPzSbIIlirp1TZzN22/00 + naNBpdYcsxrb4+/esIRPdYkrV9uTzXgGkZSieLSEQzBoeyoBXl7lZ9+Hhei/Rw2M + GNFFPth3awLd0Ricbt0Btstjj+uEoelwLNKRIox8Uq8tKKvazhzfWKIvGRaO8Vgg + 42M85qbloWZHwfWQBH4SZwBby1lpwqsvgzd75MnoyQKBgQDePQ4kD1ZpHYZcjXAt + LaEgtWZOF+xDZzqHNegNcYuMFIjSXUUlKI07Y65wsapSLssP9e1q9igsF95yTdo3 + krEF5LwSLEB7FyPsnz8CtWMHEUB4/l9wzljPM5g6v0T6T5Vq1jYsUnSbe2g+YSbp + RXHLUn3/X0XDET2Kr3ur9/icswKBgQC7vtSEJQG4hSWrCAW9WcYVcInnt3EZGZ3/ + ksSucQtM8cYXw/lSfHkKy2Pc/ZzHnVZJ6GK5td4wwx+1OImBBiCdWPSwVR+OsKUl + RjW4lueVfOSlBbHyiHPOl3sL5PlNF2PTTLhtfz0WJnPHoM4jgHmXAcri0DoPiaRs + WTtW+KYn2QKBgGnFFzDUV+TpV2Q/MI9f8xrVGt66BglCXRmy70FOtAK0VzX/jAQW + W7lTRNd/xzcb7CspeZh5lT7/ETDHmr7uQvAyH8xqYTn0FnXsiJmqaHoZnNy4/AV9 + P8lFngL/uz2CmPNjBh9sEvFY95EQvesx0Ona1fqvhk1DrE2QHUUIXLt/AoGAXKgZ + if4Zk09i/729121u7TXVWZ6XGqQh7fgpSU5RHXBVK3V0ntj/g+xNJMuljH6CD4e4 + 8Z5oQfiKtY1pj1vOzNkSKdRY9rsHRhYYfTk8ofI5hZgB8oiVXugzufMPdpSMl8PN + YXziUQQ5L9SU58CIQZaI4teRUAnGNBL1zj55AkECgYBNiW9yo6LPCkx1fzbIuQD4 + akE4cZcE9NWZ4sd1qc4BDp75q+hqbayFOfT66j0pGPjLR25c48E5Waaez9+msokM + jBtkjyCA8wJPwMoRW/04himva1xsH9327GKfs/+b7wSupJ7WnRy2/vZOWsGsbQmn + M3WS7LMNe0kWrpy+/njL+Q== + -----END PRIVATE KEY----- + """; + + public const string JwtEs256PrivateKeyPem = """ + -----BEGIN EC PRIVATE KEY----- + MHcCAQEEIF3B7fhYJm2Au2j1Oyiz8QTrOhFJB3KliuIhcCWpugTVoAoGCCqGSM49 + AwEHoUQDQgAEgc8fRNFLhhK+GNX2GHv97z5PMgZFqRcg8Gs+A7ogntzi8gc5+Hre + 74GuDZ8Xv3vtxQ4xEPw1CAAEEj86f4srnQ== + -----END EC PRIVATE KEY----- + """; + + private static readonly Lazy _rsaPublicPem = + new(() => DerivePublicPem(JwtRsa256PrivateKeyPem, isEc: false)); + private static readonly Lazy _esPublicPem = + new(() => DerivePublicPem(JwtEs256PrivateKeyPem, isEc: true)); + + public static string JwtRsa256PublicKeyPem => _rsaPublicPem.Value; + public static string JwtEs256PublicKeyPem => _esPublicPem.Value; + + private static string DerivePublicPem(string privatePem, bool isEc) + { + if (isEc) + { + using var ec = ECDsa.Create(); + ec.ImportFromPem(privatePem); + return ec.ExportSubjectPublicKeyInfoPem(); + } + using var rsa = RSA.Create(); + rsa.ImportFromPem(privatePem); + return rsa.ExportSubjectPublicKeyInfoPem(); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestIds.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestIds.cs new file mode 100644 index 00000000..23911b99 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestIds.cs @@ -0,0 +1,39 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Test-side helpers for minting deterministic-shape-but-unique identifiers. Consolidates +/// the scattered $"{prefix}-{Guid.NewGuid():N}"[..N] shape that was open-coded in +/// nearly every integration test — each site picked its own length cap (14, 16, 18, 20, 24, +/// 32), which made drift hard to catch. This helper keeps the length explicit at the call +/// site while sharing the prefix + GUID layout. +/// +/// +/// Length note: SystemId's on-wire cap is 32 characters (see +/// validation). The default maxLen: 32 matches that; call +/// sites that need a shorter id (e.g. avatar tests that want a stable short principal for +/// filename readability) pass their own length explicitly, so the intent stays visible +/// where it matters. +/// +/// +public static class TestIds +{ + /// + /// Build a raw system-id string of the form "{prefix}-{Guid:N}", truncated to + /// characters. The GUID guarantees uniqueness; the prefix + /// keeps leaked rows greppable back to the test that produced them. + /// + public static string NewSystemId(string prefix, int maxLen = 32) + { + var raw = $"{prefix}-{Guid.NewGuid():N}"; + return raw.Length <= maxLen ? raw : raw[..maxLen]; + } + + /// + /// Convenience overload returning a strongly-typed . Prefer this + /// at call sites that immediately wrap the raw string. + /// + public static SystemId NewTypedSystemId(string prefix, int maxLen = 32) + => new(NewSystemId(prefix, maxLen)); +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestJson.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestJson.cs new file mode 100644 index 00000000..a38a3e08 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/TestJson.cs @@ -0,0 +1,212 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Interfold.Shared.Api.Models; +using Interfold.Shared.Contracts; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Test-side mirror of ; the production ctor's +/// [JsonIgnore] StatusCode parameter blocks STJ deserialisation. +public sealed record TestEnvelope( + T Data, + [property: JsonPropertyName("replay")] bool? Replay = null +); + +/// Test-side mirror of ; same STJ constraint as +/// . +public sealed record TestErrorResponse( + string Error, + ErrorCode Code, + [property: JsonPropertyName("entity_ref")] string? EntityRef = null, + string? Detail = null +); + +/// Shared JSON plumbing. Every request/response routes through +/// so bytes match the API's SnakeCaseLower serializer — the idempotency-key hash depends on +/// canonical shape. +public static class TestJson +{ + /// Mirrors the API's SnakeCaseLower JsonSerializerOptions. + public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web) + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower, + }; + + /// Sends a JSON body under the specified principal's auth. Optionally sets + /// the idempotency-key header. + public static async Task SendAsJsonAsync( + this HttpClient client, + HttpMethod method, + string path, + TReq body, + string principal, + string? idempotencyKey = null, + CancellationToken ct = default) + { + using var request = new HttpRequestMessage(method, path) + { + Content = JsonContent.Create(body, options: Options), + }; + + BaseEndpointTest.AttachPrincipalAuth(request, client, principal); + + if (!string.IsNullOrEmpty(idempotencyKey)) + { + request.Headers.Add("X-Interfold-Idempotency-Key", idempotencyKey); + } + + return await client.SendAsync(request, ct).ConfigureAwait(false); + } + + /// Authed GET (built-in HttpClient.GetAsync has no request-configure hook). + public static async Task SendAuthedGetAsync( + this HttpClient client, + string path, + string principal, + CancellationToken ct = default) + { + using var request = new HttpRequestMessage(HttpMethod.Get, path); + BaseEndpointTest.AttachPrincipalAuth(request, client, principal); + return await client.SendAsync(request, ct).ConfigureAwait(false); + } + + /// Authed DELETE. + public static async Task SendAuthedDeleteAsync( + this HttpClient client, + string path, + string principal, + CancellationToken ct = default) + { + using var request = new HttpRequestMessage(HttpMethod.Delete, path); + BaseEndpointTest.AttachPrincipalAuth(request, client, principal); + return await client.SendAsync(request, ct).ConfigureAwait(false); + } + + /// Anonymous JSON send for 401-negative tests. + public static async Task SendAsJsonAnonymousAsync( + this HttpClient client, + HttpMethod method, + string path, + TReq body, + CancellationToken ct = default) + { + using var request = new HttpRequestMessage(method, path) + { + Content = JsonContent.Create(body, options: Options), + }; + + return await client.SendAsync(request, ct).ConfigureAwait(false); + } + + /// Asserts the status, unwraps the API's {data, replay} envelope. + public static async Task> ReadEnvelopeAsync( + this HttpResponseMessage response, + HttpStatusCode expected, + CancellationToken ct = default) + { + var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); + + await Assert.That(response.StatusCode) + .IsEqualTo(expected) + .Because($"Expected {(int)expected} {expected} from {response.RequestMessage?.Method} " + + $"{response.RequestMessage?.RequestUri}, got {(int)response.StatusCode}. Body: {body}"); + + var envelope = JsonSerializer.Deserialize>(body, Options); + if (envelope is null) + { + throw new InvalidOperationException( + $"Failed to deserialise TestEnvelope<{typeof(T).Name}> from body: {body}"); + } + + return envelope; + } + + /// Asserts the status and returns the parsed . + public static async Task ReadErrorAsync( + this HttpResponseMessage response, + HttpStatusCode expected, + CancellationToken ct = default) + { + var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); + + await Assert.That(response.StatusCode) + .IsEqualTo(expected) + .Because($"Expected {(int)expected} {expected} from {response.RequestMessage?.Method} " + + $"{response.RequestMessage?.RequestUri}, got {(int)response.StatusCode}. Body: {body}"); + + var error = JsonSerializer.Deserialize(body, Options); + if (error is null) + { + throw new InvalidOperationException( + $"Failed to deserialise TestErrorResponse from body: {body}"); + } + + return error; + } + + /// Envelope read without status assertion (caller already verified). + public static async Task> ReadEnvelopeUncheckedAsync( + this HttpResponseMessage response, + CancellationToken ct = default) + { + var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); + return JsonSerializer.Deserialize>(body, Options) + ?? throw new InvalidOperationException( + $"Failed to deserialise TestEnvelope<{typeof(T).Name}> from body: {body}"); + } + + /// Asserts status and deserialises the raw body (no envelope unwrap). + public static async Task ReadJsonAsync( + this HttpResponseMessage response, + HttpStatusCode expected, + CancellationToken ct = default) + { + var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); + + await Assert.That(response.StatusCode) + .IsEqualTo(expected) + .Because($"Expected {(int)expected} {expected} from {response.RequestMessage?.Method} " + + $"{response.RequestMessage?.RequestUri}, got {(int)response.StatusCode}. Body: {body}"); + + var value = JsonSerializer.Deserialize(body, Options); + if (value is null) + { + throw new InvalidOperationException( + $"Failed to deserialise {typeof(T).Name} from body: {body}"); + } + + return value; + } + + /// Raw-JSON escape hatch for tests that deliberately send malformed payloads. + public static StringContent RawJsonContent(string json) + { + var content = new StringContent(json, Encoding.UTF8); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + return content; + } + + /// Authed raw-JSON send (pairs with ). + public static async Task SendRawJsonAsync( + this HttpClient client, + HttpMethod method, + string path, + string json, + string principal, + CancellationToken ct = default) + { + using var request = new HttpRequestMessage(method, path) + { + Content = RawJsonContent(json), + }; + + BaseEndpointTest.AttachPrincipalAuth(request, client, principal); + + return await client.SendAsync(request, ct).ConfigureAwait(false); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketExtensions.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketExtensions.cs new file mode 100644 index 00000000..badacc6a --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketExtensions.cs @@ -0,0 +1,78 @@ +using System.Net.WebSockets; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; +using Microsoft.AspNetCore.TestHost; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// +/// Test-side conveniences for the raw API. Test code sends thousands of +/// Phoenix frames through +/// with the same WebSocketMessageType.Text, endOfMessage: true arguments every time; the +/// extension collapses that transport ceremony so tests read as intent (send frame X) rather than +/// as an implementation of the WebSocket contract. +/// +public static class WebSocketExtensions +{ + /// + /// Sends as a single-shot text WebSocket frame. Every Phoenix frame + /// in this codebase is text-mode + end-of-message; production paths route through + /// WebSocketEvents.SendPhoenixPushAsync which uses the same pair, so tests should not + /// be able to accidentally introduce a binary or multi-part frame that wouldn't match a + /// production sender. + /// + public static Task SendTextFrameAsync(this WebSocket ws, byte[] frame, CancellationToken ct) + => ws.SendAsync(frame, WebSocketMessageType.Text, endOfMessage: true, ct); + + public static Task CloseTestDoneAsync(this WebSocket ws, CancellationToken token) + => ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "test done", token); + + public static async Task CloseTestDoneAsync(WebSocket a, WebSocket b, CancellationToken token) + { + await a.CloseAsync(WebSocketCloseStatus.NormalClosure, "test done", token).ConfigureAwait(false); + await b.CloseAsync(WebSocketCloseStatus.NormalClosure, "test done", token).ConfigureAwait(false); + } + + public static Task ReceiveEventFrameAsync( + this WebSocket ws, + CancellationToken token, + string expectedEvent, + int maxFrames = 5, + int perFrameTimeoutSeconds = 5) + => ReceivedPhxFrame.ReceiveEventFrameAsync(ws, token, expectedEvent, maxFrames, perFrameTimeoutSeconds); + + public static async Task<(ReceivedPhxFrame? Reply, ReceivedPhxFrame? Push)> SendEndpointAndCaptureAsync( + this WebSocket ws, + byte[] frame, + string expectedPushEvent, + CancellationToken token) + { + await ws.SendTextFrameAsync(frame, token).ConfigureAwait(false); + return await ReceivedPhxFrame.ReceiveReplyAndPushAsync(ws, token, expectedPushEvent).ConfigureAwait(false); + } + + // Extracted from WebSocketTests during the Phase-6 test-project split so Shared's + // WebSocketHarness can drive the join handshake without a circular ref back into + // Interfold.Socket.IntegrationTests. + public static Uri WebSocketBasePath(TestServer server) + => new($"wss://{server.BaseAddress.Host}"); + + public static async Task JoinTopicAsync(WebSocket ws, string topic, string socketToken, CancellationToken timeoutToken) + { + var joinFrame = new PhxFrame + { + Topic = topic, + Event = "phx_join", + Payload = new PhxJoinPayload { Token = new SocketToken(socketToken), IsReconnect = true }, + Ref = "1", + JoinRef = "1" + }; + + await ws.SendTextFrameAsync(joinFrame.ToBytes(), timeoutToken); + var frame = await ReceivedPhxFrame.ReceiveAsync(ws, timeoutToken); + var reply = frame.Reply(); + if (reply.Status != PhoenixReplyStatus.Ok) + throw new InvalidOperationException($"JoinTopicAsync failed for topic '{topic}'. Status: {reply.Status}"); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketHarness.cs b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketHarness.cs new file mode 100644 index 00000000..8e8be643 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/TestServices/WebSocketHarness.cs @@ -0,0 +1,71 @@ +using System.Net.WebSockets; + +namespace Interfold.IntegrationTests.Shared.TestServices; + +/// Mint-token + connect + phx_join scaffolding for the Phoenix-style websocket +/// surface. Both helpers fail fast if the join step returns non-ok; tests that expect a +/// join failure must hand-roll setup instead. +public static class WebSocketHarness +{ + /// Opens two sockets and joins system:{id} on each. Returned sockets + /// are unowned — adopt with using var at the call site. + public static async Task<(WebSocket FirstWs, WebSocket SecondWs, string FirstToken, string SecondToken)> + ConnectPairAndJoinAsync( + IWebFactoryFixture fixture, + string firstSystemId, + string secondSystemId, + CancellationToken cancellationToken) + { + var wsClientFactory = fixture.Factory.Server.CreateWebSocketClient(); + + var firstToken = await BaseEndpointTest.CreateRandomToken(fixture.Factory, firstSystemId); + var secondToken = await BaseEndpointTest.CreateRandomToken(fixture.Factory, secondSystemId); + + var firstUri = new Uri(WebSocketExtensions.WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={firstToken}"); + var secondUri = new Uri(WebSocketExtensions.WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={secondToken}"); + + var firstWs = await wsClientFactory.ConnectAsync(firstUri, cancellationToken); + var secondWs = await wsClientFactory.ConnectAsync(secondUri, cancellationToken); + + try + { + await WebSocketExtensions.JoinTopicAsync(firstWs, $"system:{firstSystemId}", firstToken, cancellationToken); + await WebSocketExtensions.JoinTopicAsync(secondWs, $"system:{secondSystemId}", secondToken, cancellationToken); + } + catch + { + // Dispose to avoid leaking sockets before caller adopts ownership; swallow so + // the original join failure surfaces. + try { firstWs.Dispose(); } catch { } + try { secondWs.Dispose(); } catch { } + throw; + } + + return (firstWs, secondWs, firstToken, secondToken); + } + + /// Single-party sister of . Returned + /// socket is unowned. + public static async Task<(WebSocket Ws, string Token)> ConnectAndJoinAsync( + IWebFactoryFixture fixture, + string systemId, + CancellationToken cancellationToken) + { + var wsClientFactory = fixture.Factory.Server.CreateWebSocketClient(); + var socketToken = await BaseEndpointTest.CreateRandomToken(fixture.Factory, systemId); + var uri = new Uri(WebSocketExtensions.WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={socketToken}"); + + var ws = await wsClientFactory.ConnectAsync(uri, cancellationToken); + try + { + await WebSocketExtensions.JoinTopicAsync(ws, $"system:{systemId}", socketToken, cancellationToken); + } + catch + { + try { ws.Dispose(); } catch { } + throw; + } + + return (ws, socketToken); + } +} diff --git a/tests/integration/Interfold.IntegrationTests.Shared/tests.runsettings b/tests/integration/Interfold.IntegrationTests.Shared/tests.runsettings new file mode 100644 index 00000000..ce6b1af8 --- /dev/null +++ b/tests/integration/Interfold.IntegrationTests.Shared/tests.runsettings @@ -0,0 +1,13 @@ + + + + + diff --git a/tests/integration/Interfold.Journals.IntegrationTests/Controllers/AlterJournalsControllerTests.cs b/tests/integration/Interfold.Journals.IntegrationTests/Controllers/AlterJournalsControllerTests.cs new file mode 100644 index 00000000..546f8be4 --- /dev/null +++ b/tests/integration/Interfold.Journals.IntegrationTests/Controllers/AlterJournalsControllerTests.cs @@ -0,0 +1,95 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Journals.Contracts.Models.Read; + +namespace Interfold.Journals.IntegrationTests.Controllers; + +[Category("Alter Journals")] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class AlterJournalsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test, Category("Index")] + public async Task AlterJournal_ListWhenEmpty_ReturnsDataAsEmptyArray() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-alter-journal-empty-list"; + var alterId = await CreateAlterAsync(client, principal, "NoJournalAlter"); + + using var listRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/{alterId}/journals", principal); + + var envelope = await listRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(envelope.Data.Count).IsEqualTo(0); + } + + [Test] + public async Task AlterJournal_NestedCreate_Returns201WithDataAndReplay() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-alter-journal"; + var alterId = await CreateAlterAsync(client, principal, "JournalHolder"); + + // POST /api/systems/me/alters/:id/journals → 201 + {data, replay} + using var createRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/systems/me/alters/{alterId}/journals", + new CreateAlterJournalRequest("NestedParityJournal"), + principal); + var createEnv = await createRes.ReadEnvelopeAsync(HttpStatusCode.Created); + var entryId = createEnv.Data.Id; + + using (Assert.Multiple()) + { + await Assert.That(entryId).IsNotEqualTo(default); + await Assert.That(createEnv.Replay ?? false).IsFalse(); + } + + // GET /api/systems/me/alters/:id/journals → 200 + {data:[...]} + using var listRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/{alterId}/journals", principal); + var listEnv = await listRes.ReadEnvelopeAsync>(HttpStatusCode.OK); + await Assert.That(listEnv.Data.Count).IsGreaterThan(0); + + // GET /api/systems/me/alters/journals/:journalId → 200 + {data:{...}} + using var showRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/journals/{entryId}", principal); + var showEnv = await showRes.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(showEnv.Data.Id).IsEqualTo(entryId); + + // PATCH /api/systems/me/alters/journals/:journalId → 204 + using var patchRes = await client.SendAsJsonAsync( + HttpMethod.Patch, $"/api/systems/me/alters/journals/{entryId}", + new UpdateAlterJournalRequest(Title: "UpdatedParityJournal"), + principal); + await Assert.That(patchRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + patchRes.Dispose(); + + // DELETE /api/systems/me/alters/journals/:journalId → 204 + using var deleteRes = await client.SendAuthedDeleteAsync($"/api/systems/me/alters/journals/{entryId}", principal); + + await Assert.That(deleteRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + } + + [Test] + public async Task AlterJournal_ShowAfterDelete_Returns404() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-alter-journal-delete-404"; + var alterId = await CreateAlterAsync(client, principal, "DeleteHolder"); + + using var createRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/systems/me/alters/{alterId}/journals", + new CreateAlterJournalRequest("JournalToDelete"), + principal); + var createEnv = await createRes.ReadEnvelopeAsync(HttpStatusCode.Created); + var entryId = createEnv.Data.Id; + + (await client.SendAuthedDeleteAsync($"/api/systems/me/alters/journals/{entryId}", principal)).Dispose(); + + using var showRes = await client.SendAuthedGetAsync($"/api/systems/me/alters/journals/{entryId}", principal); + await Assert.That(showRes.StatusCode).IsEqualTo(HttpStatusCode.NotFound); + } +} + diff --git a/tests/integration/Interfold.Journals.IntegrationTests/Controllers/JournalsControllerTests.cs b/tests/integration/Interfold.Journals.IntegrationTests/Controllers/JournalsControllerTests.cs new file mode 100644 index 00000000..ef7e5af2 --- /dev/null +++ b/tests/integration/Interfold.Journals.IntegrationTests/Controllers/JournalsControllerTests.cs @@ -0,0 +1,24 @@ +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Journals.Contracts.Models.Read; + +namespace Interfold.Journals.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class JournalsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task Idempotency_GlobalJournalCreate_ReplayStable() + { + await RunSoakAsync(fixture.Factory, async (client, key) => + { + return await client.SendAsJsonAsync( + HttpMethod.Post, "/api/journals", + new CreateGlobalJournalRequest("SoakJournal"), + "soak-default-principal", + idempotencyKey: key); + }); + } +} diff --git a/tests/integration/Interfold.Journals.IntegrationTests/Interfold.Journals.IntegrationTests.csproj b/tests/integration/Interfold.Journals.IntegrationTests/Interfold.Journals.IntegrationTests.csproj new file mode 100644 index 00000000..e7b96e87 --- /dev/null +++ b/tests/integration/Interfold.Journals.IntegrationTests/Interfold.Journals.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Ops.IntegrationTests/Controllers/ClusterRoleAndOAuthRegistrationTests.cs b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/ClusterRoleAndOAuthRegistrationTests.cs new file mode 100644 index 00000000..a44926f2 --- /dev/null +++ b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/ClusterRoleAndOAuthRegistrationTests.cs @@ -0,0 +1,97 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Ops.Api.Models; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Ops.IntegrationTests.Controllers; + +/// +/// Tests covering configuration values that snapshots once +/// at startup and never re-reads. Each test instantiates its own +/// (no [ClassDataSource], no shared +/// fixture) because the relevant settings - cluster role binding via +/// and OAuth scheme registration via +/// AddInterfoldAuthChallengeSchemes - are committed during the host build pipeline, +/// not via . Since the rest of the suite consolidates onto +/// , isolating these four tests behind their own factory keeps +/// the parallel session footprint to a single unavoidable per-test InMemory host. +/// +public sealed class ClusterRoleAndOAuthRegistrationTests : BaseEndpointTest +{ + [Test] + public async Task NodeRole_ReturnsPrimary_WhenOctoconNodeGroupIsPrimary() + { + using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory) + .WithConfiguration("OCTOCON_NODE_GROUP", "primary"); + using var client = factory.CreateClient(); + + using var response = await client.GetAsync("/health/node-role"); + var body = await response.ReadJsonAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(body.Role).IsEqualTo(NodeGroup.Primary); + await Assert.That(body.OwnsSingletons).IsTrue(); + } + } + + [Test] + public async Task NodeRole_ReturnsPrimary_WhenFlyProcessGroupIsPrimary() + { + using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory) + .WithConfiguration("FLY_PROCESS_GROUP", "primary"); + using var client = factory.CreateClient(); + + using var response = await client.GetAsync("/health/node-role"); + var body = await response.ReadJsonAsync(HttpStatusCode.OK); + + await Assert.That(body.Role).IsEqualTo(NodeGroup.Primary); + } + + [Test] + public async Task NodeRole_FlyProcessGroup_TakesPrecedenceOver_OctoconNodeGroup() + { + using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory) + .WithConfiguration("OCTOCON_NODE_GROUP", "primary") + .WithConfiguration("FLY_PROCESS_GROUP", "sidecar"); + using var client = factory.CreateClient(); + + using var response = await client.GetAsync("/health/node-role"); + var body = await response.ReadJsonAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(body.Role).IsEqualTo(NodeGroup.Sidecar); + await Assert.That(body.OwnsSingletons).IsFalse(); + } + } + + [Test] + public async Task Api_AuthRequest_IssuesChallengeRedirect_WhenChallengeEnabledAndSchemeConfigured() + { + // The Google challenge scheme + endpoint are baked into the API as constants — see + // OAuthChallengeServiceCollectionExtensions. The scheme is only registered when + // OCTOCON_GOOGLE_OAUTH_CLIENT_ID is set (the operator's "I want to use Google" + // signal); the test pins it here so the scheme registers and the controller issues + // a real challenge. Because scheme registration is a one-shot startup step rather + // than IOptionsMonitor-bound, this test owns its own factory. + using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory) + .WithConfiguration("OCTOCON_GOOGLE_OAUTH_CLIENT_ID", "test-client-id"); + + using var client = TestClient.NoRedirect(factory); + + using var response = await client.GetAsync("/auth/google"); + + var locationHeader = response.Headers.Location; + var location = locationHeader!.ToString(); + using (Assert.Multiple()) + { + await Assert.That(response.StatusCode is HttpStatusCode.Redirect or HttpStatusCode.Found).IsTrue(); + await Assert.That(locationHeader).IsNotNull(); + await Assert.That(location.StartsWith("https://accounts.google.com/o/oauth2/v2/auth", StringComparison.Ordinal)).IsTrue(); + await Assert.That(location.Contains("%2Fauth%2Fgoogle%2Fcallback", StringComparison.Ordinal)).IsTrue(); + } + } +} diff --git a/tests/integration/Interfold.Ops.IntegrationTests/Controllers/InMemorySecretsSeedTests.cs b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/InMemorySecretsSeedTests.cs new file mode 100644 index 00000000..e65b5e4c --- /dev/null +++ b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/InMemorySecretsSeedTests.cs @@ -0,0 +1,132 @@ +using System.Net; +using System.Net.Http.Json; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; + +namespace Interfold.Ops.IntegrationTests.Controllers; + +/// +/// Regression coverage that documents the env-var seed contract for the in-memory +/// ISecretsStore with a self-describing test name: a future refactor that breaks +/// the OCTOCON_INMEMORY_SECRETS_SEED__* wiring fails here with a clear signal, +/// rather than a flood of red across every other in-memory integration test. +/// +/// The factory is constructed inline (no [ClassDataSource]) so the test boots a +/// fresh host whose only secrets material flows through the production env-var path +/// registered in +/// — exactly what the published image does when an external runner (e.g. the Kotlin +/// Testcontainers harness) drives it as a separate process. +/// +public class InMemorySecretsSeedTests : BaseEndpointTest +{ + // Operator-facing env-var names (the `__` form). The .NET EnvironmentVariablesConfigurationProvider + // rewrites `__` to `:` on load, so SeedFromConfig in InMemoryServiceCollectionExtensions looks up + // the `:`-normalised key — but the contract operators set in their docker run / Testcontainers + // env is the `__` form below, which is exactly what we mutate here to lock that real path. + private const string EnvKeyEncryptionPepper = "OCTOCON_INMEMORY_SECRETS_SEED__ENCRYPTION_PEPPER"; + private const string EnvKeyJwtEs256PrivatePem = "OCTOCON_INMEMORY_SECRETS_SEED__AUTH_JWT_ES256_PRIVATE_PEM"; + private const string EnvKeyDeepLinkSecret = "OCTOCON_INMEMORY_SECRETS_SEED__AUTH_DEEP_LINK_SECRET"; + private const string EnvKeyJwtRsa256PrivatePem = "OCTOCON_INMEMORY_SECRETS_SEED__AUTH_JWT_RSA256_PRIVATE_PEM"; + + [Test] + public async Task Api_InMemorySecretsSeed_PatchesAuthFromEnvVars() + { + await using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory); + using var client = factory.CreateClient(); + + var principalId = TestIds.NewSystemId("sys-secrets-seed", maxLen: 24); + + // POST /api/settings/username is a [Authorize]-gated route. Reaching 204 NoContent + // proves end-to-end that: + // (a) the host started without tripping SecretsBootstrapService's hard + // fail-fast on `encryption:pepper` (so OCTOCON_INMEMORY_SECRETS_SEED__ENCRYPTION_PEPPER + // was honoured), and + // (b) the JWT minted by CreateToken with TestDbCredentials.JwtEs256PrivateKeyPem + // verifies against AuthenticationConfiguration.JwtEs256VerificationKeyPems — + // which in turn proves OCTOCON_INMEMORY_SECRETS_SEED__AUTH_JWT_ES256_PRIVATE_PEM + // was seeded into the store and patched onto AuthenticationConfiguration by + // SecretsBootstrapService at startup. + using var request = new HttpRequestMessage(HttpMethod.Post, "/api/settings/username") + { + Content = JsonContent.Create(new { username = principalId }) + }; + AttachPrincipalAuth(request, client, principalId); + + var response = await client.SendAsync(request); + + await Assert.That(response.StatusCode) + .IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected 204 NoContent, got {(int)response.StatusCode}. Body: {await response.Content.ReadAsStringAsync()}"); + } + + /// + /// Locks the real env-var ingestion path that octocon-app's Testcontainers harness and + /// the published-image docker run both rely on. The sibling test above only exercises the + /// FactoryConfigurationProvider shortcut — that path stores keys verbatim and so happily + /// matched the literal __-form lookup even while real env vars (which the + /// EnvironmentVariablesConfigurationProvider rewrites to :) silently missed. + /// This test mutates with the + /// operator-facing __ form and builds the factory with the in-fixture :-form + /// seeds suppressed (seedInMemorySecretsFromFactoryConfig: false), so the only route + /// from env var to InMemorySecretsStore is the production + /// EnvironmentVariablesConfigurationProvider → SeedFromConfig chain. + /// + /// + /// + /// Environment.SetEnvironmentVariable is process-global, so the NotInParallel + /// key serialises this test against any other env-var-sensitive test in the suite (none today, + /// but the named key is the established pattern in AvatarSourceTests / + /// SettingsControllerTests). Prior values are captured up front and restored in the + /// finally so re-runs in the same test session don't leak state — important because + /// EnvironmentVariablesConfigurationProvider snapshots env vars at host build time, so a + /// leaked value would silently flip the behaviour of a later host build in the same process. + /// + /// + [Test, NotInParallel("inmemory-secrets-seed-envvars")] + public async Task Api_InMemorySecretsSeed_PatchesAuthFromRealEnvVars() + { + var prior = new[] + { + (Key: EnvKeyEncryptionPepper, Value: Environment.GetEnvironmentVariable(EnvKeyEncryptionPepper)), + (Key: EnvKeyJwtEs256PrivatePem, Value: Environment.GetEnvironmentVariable(EnvKeyJwtEs256PrivatePem)), + (Key: EnvKeyDeepLinkSecret, Value: Environment.GetEnvironmentVariable(EnvKeyDeepLinkSecret)), + (Key: EnvKeyJwtRsa256PrivatePem, Value: Environment.GetEnvironmentVariable(EnvKeyJwtRsa256PrivatePem)), + }; + + try + { + Environment.SetEnvironmentVariable(EnvKeyEncryptionPepper, "TEST"); + Environment.SetEnvironmentVariable(EnvKeyJwtEs256PrivatePem, TestDbCredentials.JwtEs256PrivateKeyPem); + Environment.SetEnvironmentVariable(EnvKeyDeepLinkSecret, TestDbCredentials.DeepLinkSecret); + Environment.SetEnvironmentVariable(EnvKeyJwtRsa256PrivatePem, TestDbCredentials.JwtRsa256PrivateKeyPem); + + // seedInMemorySecretsFromFactoryConfig: false suppresses the constructor's `:`-form + // FactoryConfigurationProvider writes for these four keys, so SeedFromConfig has no + // shortcut and must resolve them from the env-var-backed configuration provider — i.e. + // the same code path the published image exercises in production. + await using var factory = new InterfoldWebApplicationFactory( + PersistenceMode.InMemory, + seedInMemorySecretsFromFactoryConfig: false); + using var client = factory.CreateClient(); + + var principalId = TestIds.NewSystemId("sys-secrets-env", maxLen: 24); + using var request = new HttpRequestMessage(HttpMethod.Post, "/api/settings/username") + { + Content = JsonContent.Create(new { username = principalId }) + }; + AttachPrincipalAuth(request, client, principalId); + + var response = await client.SendAsync(request); + + await Assert.That(response.StatusCode) + .IsEqualTo(HttpStatusCode.NoContent) + .Because($"Expected 204 NoContent, got {(int)response.StatusCode}. Body: {await response.Content.ReadAsStringAsync()}"); + } + finally + { + foreach (var (key, value) in prior) + Environment.SetEnvironmentVariable(key, value); + } + } +} diff --git a/tests/integration/Interfold.Ops.IntegrationTests/Controllers/NodeRoleControllerTests.cs b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/NodeRoleControllerTests.cs new file mode 100644 index 00000000..6a0699f5 --- /dev/null +++ b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/NodeRoleControllerTests.cs @@ -0,0 +1,38 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Ops.Api.Models; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Ops.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class NodeRoleControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task NodeRole_DefaultsToAuxiliary_WhenNoEnvVarSet() + { + using var client = fixture.Factory.CreateClient(); + + using var response = await client.GetAsync("/health/node-role"); + var body = await response.ReadJsonAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(body.Role).IsEqualTo(NodeGroup.Auxiliary); + await Assert.That(body.OwnsSingletons).IsFalse(); + } + } + + [Test] + public async Task NodeRole_Endpoint_IsAnonymous() + { + using var client = fixture.Factory.CreateClient(); + + using var response = await client.GetAsync("/health/node-role"); + + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); + } +} diff --git a/tests/integration/Interfold.Ops.IntegrationTests/Controllers/TrustControllerTests.cs b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/TrustControllerTests.cs new file mode 100644 index 00000000..69fe3c02 --- /dev/null +++ b/tests/integration/Interfold.Ops.IntegrationTests/Controllers/TrustControllerTests.cs @@ -0,0 +1,261 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; + +namespace Interfold.Ops.IntegrationTests.Controllers; + +// Each test spins its own factory so the env-bound TrustOptions snapshot (read once at +// startup via IOptions) captures the path shape the case wants. +public class TrustControllerTests : BaseEndpointTest +{ + [Test] + public async Task ReturnsNotFoundWhenRootCaPathUnset() + { + // Dev-mode parity: no /certs bind mount -> no OCTOCON_TRUST_* env vars; contract is + // 404-on-every-route, not 500-because-IO-failed. + await using var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory); + using var client = factory.CreateClient(); + + foreach (var path in new[] + { + "/.well-known/interfold-root-ca.crt", + "/.well-known/interfold-root-ca.pem", + "/.well-known/interfold-root-ca.sha256", + }) + { + var response = await client.GetAsync(path); + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound) + .Because($"{path} must 404 when TrustOptions paths are unset"); + } + } + + [Test] + public async Task ReturnsCertWithCorrectContentTypeAndBytes() + { + await using var h = await NewTrustCertHarnessAsync(); + + var crt = await h.Client.GetAsync("/.well-known/interfold-root-ca.crt"); + await Assert.That(crt.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(crt.Content.Headers.ContentType?.MediaType).IsEqualTo("application/pkix-cert"); + var crtBytes = await crt.Content.ReadAsByteArrayAsync(); + await Assert.That(crtBytes).IsEquivalentTo(h.ExpectedDer) + .Because("the .crt route must serve the cert in DER form"); + + var pem = await h.Client.GetAsync("/.well-known/interfold-root-ca.pem"); + await Assert.That(pem.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(pem.Content.Headers.ContentType?.MediaType).IsEqualTo("application/x-pem-file"); + var pemBytes = await pem.Content.ReadAsByteArrayAsync(); + await Assert.That(pemBytes).IsEquivalentTo(await File.ReadAllBytesAsync(h.CertPath)) + .Because("the .pem route must stream the on-disk bytes verbatim"); + + var sha256 = await h.Client.GetAsync("/.well-known/interfold-root-ca.sha256"); + await Assert.That(sha256.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(sha256.Content.Headers.ContentType?.MediaType).IsEqualTo("text/plain"); + var sha256Text = (await sha256.Content.ReadAsStringAsync()).Trim(); + await Assert.That(sha256Text).IsEqualTo(h.ExpectedFingerprint); + + // Safari / iOS profile flow: explicit Accept: application/x-x509-ca-cert gets the + // legacy type back. + using var legacyReq = new HttpRequestMessage(HttpMethod.Get, "/.well-known/interfold-root-ca.crt"); + legacyReq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-x509-ca-cert")); + var legacy = await h.Client.SendAsync(legacyReq); + await Assert.That(legacy.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(legacy.Content.Headers.ContentType?.MediaType).IsEqualTo("application/x-x509-ca-cert"); + } + + [Test] + public async Task ReturnsEtagMatchingFingerprintFile() + { + // The ETag is the only mechanism that propagates a --rotate-certs through downstream + // caches before Cache-Control max-age expires. Locking the value to the fingerprint + // file's contents (verbatim, quoted) protects that contract. + await using var h = await NewTrustCertHarnessAsync(); + + foreach (var path in new[] + { + "/.well-known/interfold-root-ca.crt", + "/.well-known/interfold-root-ca.pem", + "/.well-known/interfold-root-ca.sha256", + }) + { + var response = await h.Client.GetAsync(path); + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); + + var etag = response.Headers.ETag?.Tag; + await Assert.That(etag).IsEqualTo($"\"{h.ExpectedFingerprint}\"") + .Because($"{path} must return ETag derived from rootCA.sha256.txt"); + + var cacheControl = response.Headers.CacheControl?.ToString(); + await Assert.That(cacheControl).IsNotNull(); + await Assert.That(cacheControl!).Contains("public"); + await Assert.That(cacheControl!).Contains("max-age=60"); + await Assert.That(cacheControl!).Contains("must-revalidate"); + } + } + + [Test] + public async Task HeadRequestsReturnHeadersWithoutBody() + { + // [HttpHead] must sit alongside [HttpGet] on each action; otherwise routing rejects + // HEAD, it falls through to the default authorize policy, and CDN / curl -I + // revalidation gets a 401 challenge instead of headers. + await using var h = await NewTrustCertHarnessAsync(); + + foreach (var path in new[] + { + "/.well-known/interfold-root-ca.crt", + "/.well-known/interfold-root-ca.pem", + "/.well-known/interfold-root-ca.sha256", + }) + { + using var req = new HttpRequestMessage(HttpMethod.Head, path); + var response = await h.Client.SendAsync(req); + + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"HEAD {path} must return 200 (not 401 / 405) so cache revalidation works"); + + var etag = response.Headers.ETag?.Tag; + await Assert.That(etag).IsEqualTo($"\"{h.ExpectedFingerprint}\"") + .Because($"HEAD {path} must publish the same ETag as GET so conditional caches stay in sync"); + + var body = await response.Content.ReadAsByteArrayAsync(); + await Assert.That(body.Length).IsEqualTo(0) + .Because($"HEAD {path} must omit the response body (RFC 9110 §9.3.2)"); + } + } + + [Test] + public async Task WellKnownRoutesBypassHttpsRedirect() + { + // Trust-bootstrap contract: devices fetching the root CA have no HTTPS trust path yet, + // so /.well-known/* must serve over plain HTTP. HTTPS_PORT=443 activates + // HttpsRedirectionMiddleware in-test (without it the middleware logs a warning and + // lets everything through); environment=Production opens the !IsDevelopment() gate in + // Program.cs that wraps HSTS + HttpsRedirection. + await using var h = await NewTrustCertHarnessAsync( + noRedirect: true, + extraConfig: + [ + ("HTTPS_PORT", "443"), + ("environment", "Production"), + ]); + + foreach (var path in new[] + { + "/.well-known/interfold-root-ca.crt", + "/.well-known/interfold-root-ca.pem", + "/.well-known/interfold-root-ca.sha256", + }) + { + var response = await h.Client.GetAsync(path); + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"{path} must serve the artefact directly over plain HTTP, not redirect to HTTPS"); + await Assert.That((int)response.StatusCode is >= 300 and < 400).IsFalse() + .Because($"{path} must NOT issue any 3xx redirect - clients fetching the root CA have no HTTPS trust path"); + } + + // Negative control: every other path must still redirect, so the bypass stays + // narrowly scoped. + var control = await h.Client.GetAsync("/api/i-do-not-exist"); + await Assert.That((int)control.StatusCode is >= 300 and < 400).IsTrue() + .Because($"non-.well-known paths must still go through HttpsRedirection (got {(int)control.StatusCode})"); + } + + // Mints a self-signed CA + fingerprint sidecar in a temp dir, wires them onto the factory + // via OCTOCON_TRUST_ROOT_CA_{PATH,FINGERPRINT_PATH}, and returns a harness that owns + // teardown. noRedirect: surfaces 3xx responses as-is. extraConfig: extra env-var pairs + // applied after the two OCTOCON_TRUST_ROOT_CA_* keys. + private static async Task NewTrustCertHarnessAsync( + bool noRedirect = false, + IReadOnlyList<(string Key, string Value)>? extraConfig = null) + { + var (certPath, fingerprintPath, expectedDer, expectedFingerprint, tmpDir) = MintTempCertFiles(); + + var factory = new InterfoldWebApplicationFactory(PersistenceMode.InMemory) + .WithConfiguration("OCTOCON_TRUST_ROOT_CA_PATH", certPath) + .WithConfiguration("OCTOCON_TRUST_ROOT_CA_FINGERPRINT_PATH", fingerprintPath); + if (extraConfig is not null) + { + foreach (var (key, value) in extraConfig) + { + factory.WithConfiguration(key, value); + } + } + + var client = noRedirect ? TestClient.NoRedirect(factory) : factory.CreateClient(); + + // Boot the factory's TestServer before the caller starts asserting. + await Task.Yield(); + + return new TrustCertHarness(client, factory, tmpDir, + certPath, expectedDer, expectedFingerprint); + } + + // Teardown order matters: client first so no in-flight response outlives the server, + // then factory (stops the in-memory host), then temp dir (best-effort; OS tmp reaper + // catches leaks). + private sealed class TrustCertHarness( + HttpClient client, + InterfoldWebApplicationFactory factory, + string tmpDir, + string certPath, + byte[] expectedDer, + string expectedFingerprint) : IAsyncDisposable + { + public HttpClient Client { get; } = client; + public string CertPath { get; } = certPath; + public byte[] ExpectedDer { get; } = expectedDer; + public string ExpectedFingerprint { get; } = expectedFingerprint; + + public async ValueTask DisposeAsync() + { + Client.Dispose(); + await factory.DisposeAsync().ConfigureAwait(false); + try { Directory.Delete(tmpDir, recursive: true); } catch { /* best effort */ } + } + } + + // Mints rootCA.crt (PEM) + rootCA.sha256.txt (uppercase colon-hex) in a temp dir; returns + // paths + expected DER so the caller can assert byte-for-byte. + private static (string certPath, string fingerprintPath, byte[] expectedDer, string expectedFingerprint, string tmpDir) MintTempCertFiles() + { + var tmpDir = Path.Combine(Path.GetTempPath(), "interfold-trust-test-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tmpDir); + + using var key = RSA.Create(2048); + var req = new CertificateRequest( + new X500DistinguishedName("CN=Test Trust Controller Root"), + key, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + req.CertificateExtensions.Add(new X509BasicConstraintsExtension(certificateAuthority: true, hasPathLengthConstraint: true, pathLengthConstraint: 0, critical: true)); + + using var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddYears(1)); + var certPath = Path.Combine(tmpDir, "rootCA.crt"); + File.WriteAllText(certPath, cert.ExportCertificatePem()); + + var hash = SHA256.HashData(cert.RawData); + var fingerprint = ToColonHexUpper(hash); + var fingerprintPath = Path.Combine(tmpDir, "rootCA.sha256.txt"); + File.WriteAllText(fingerprintPath, fingerprint + Environment.NewLine); + + var expectedDer = (byte[])cert.RawData.Clone(); + + return (certPath, fingerprintPath, expectedDer, fingerprint, tmpDir); + } + + private static string ToColonHexUpper(byte[] hash) + { + var sb = new System.Text.StringBuilder(hash.Length * 3); + for (var i = 0; i < hash.Length; i++) + { + if (i > 0) sb.Append(':'); + sb.Append(hash[i].ToString("X2")); + } + return sb.ToString(); + } +} diff --git a/tests/integration/Interfold.Ops.IntegrationTests/Interfold.Ops.IntegrationTests.csproj b/tests/integration/Interfold.Ops.IntegrationTests/Interfold.Ops.IntegrationTests.csproj new file mode 100644 index 00000000..9912a2a5 --- /dev/null +++ b/tests/integration/Interfold.Ops.IntegrationTests/Interfold.Ops.IntegrationTests.csproj @@ -0,0 +1,22 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Polls.IntegrationTests/Controllers/PollsControllerTests.cs b/tests/integration/Interfold.Polls.IntegrationTests/Controllers/PollsControllerTests.cs new file mode 100644 index 00000000..c0869bb0 --- /dev/null +++ b/tests/integration/Interfold.Polls.IntegrationTests/Controllers/PollsControllerTests.cs @@ -0,0 +1,195 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Polls.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class PollsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + public static IEnumerable PollTypes() + { + yield return PollType.Vote; + yield return PollType.Choice; + yield return PollType.Approval; + } + + [Test] + public async Task ErrorResponse_ConflictFormats_IncludeEntityRefAndCode() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-error-format"; + var tooLongTitle = new string('a', 101); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest(tooLongTitle), + principal); + var body = await res.Content.ReadAsStringAsync(); + + // Wire-shape probes: keep the raw-string assertions here — this test exists to + // pin the exact `{ "code", "entity_ref", ... }` shape of the error envelope, so + // deserialising through ErrorResponse would hide any drift in the JSON keys. + using (Assert.Multiple()) + { + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.UnprocessableEntity); + await Assert.That(body).Contains("\"code\""); + await Assert.That(body).Contains("\"entity_ref\""); + await Assert.That(body).Contains("poll:title_too_long"); + } + } + + [Test] + public async Task PollValidation_DescriptionTooLong_Returns422() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-poll-desc-validation"; + var tooLongDesc = new string('a', 2001); // Max is 2000 + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest("ValidTitle", Description: tooLongDesc), + principal); + + var error = await res.ReadErrorAsync(HttpStatusCode.UnprocessableEntity); + await Assert.That(error.EntityRef).IsEqualTo("poll:description_too_long"); + } + + [Test] + [CombinedDataSources] + public async Task PollType_AllSupportedTypes_RoundTripCorrectly( + [MethodDataSource(typeof(PollsControllerTests), nameof(PollTypes))] PollType type) + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-poll-types"; + + using var createRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest($"Poll_{type}", Type: type), + principal); + var createEnv = await createRes.ReadEnvelopeAsync(HttpStatusCode.Created); + + using var getRes = await client.SendAuthedGetAsync($"/api/polls/{createEnv.Data.Id}", principal); + var getBody = await getRes.Content.ReadAsStringAsync(); + var getEnv = await getRes.ReadEnvelopeAsync(HttpStatusCode.OK); + + // Typed round-trip: the deserialised enum must match the value we sent. + await Assert.That(getEnv.Data.Type).IsEqualTo(type); + // Wire-shape round-trip: the raw JSON must carry the exact JsonStringEnumMemberName + // spelling (`"vote"` / `"choice"` / `"approval"`), not the CLR enum name. This is the + // load-bearing bit for cross-language clients that don't have PollType's converter. + var expectedWireValue = type switch + { + PollType.Vote => "vote", + PollType.Choice => "choice", + PollType.Approval => "approval", + _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Add a wire-form entry when introducing a new PollType."), + }; + await Assert.That(getBody).Contains($"\"type\":\"{expectedWireValue}\""); + } + + [Test] + public async Task PollUpdate_TimeEndNullOnly_ClearsExistingTimeEnd() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-poll-time-end"; + var initialTimeEnd = DateTime.UtcNow.AddHours(1); + + using var createRes = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest("TimeEndEdgeCase", TimeEnd: initialTimeEnd), + principal); + var createEnv = await createRes.ReadEnvelopeAsync(HttpStatusCode.Created); + + // PatchValue.OfNull() emits `"time_end": null` which the server maps to + // the "clear" branch — a plain `TimeEnd = null` on a nullable field would be + // indistinguishable from "field absent" on the wire and hit the "unchanged" branch. + using var patchRes = await client.SendAsJsonAsync( + HttpMethod.Patch, $"/api/polls/{createEnv.Data.Id}", + new UpdatePollRequest(TimeEnd: PatchValue.OfNull()), + principal); + await Assert.That(patchRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + patchRes.Dispose(); + + using var getRes = await client.SendAuthedGetAsync($"/api/polls/{createEnv.Data.Id}", principal); + var getEnv = await getRes.ReadEnvelopeAsync(HttpStatusCode.OK); + + await Assert.That(getEnv.Data.TimeEnd).IsNull(); + } + + [Test] + public async Task PollValidation_TitleTooLong_Returns422() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-poll-validation"; + var tooLongTitle = new string('a', 101); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest(tooLongTitle), + principal); + + var error = await res.ReadErrorAsync(HttpStatusCode.UnprocessableEntity); + await Assert.That(error.EntityRef).IsEqualTo("poll:title_too_long"); + } + + [Test] + public async Task LegacyRoute_SystemsMePolls_Returns404() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-legacy-routes"; + + foreach (var (method, path) in new[] + { + (HttpMethod.Get, "/api/systems/me/polls"), + (HttpMethod.Post, "/api/systems/me/polls"), + }) + { + HttpResponseMessage res; + if (method == HttpMethod.Post) + { + res = await client.SendAsJsonAsync(method, path, new CreatePollRequest("LegacyPoll"), principal); + } + else + { + using var req = new HttpRequestMessage(method, path); + AttachPrincipalAuth(req, client, principal); + res = await client.SendAsync(req); + } + + try + { + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.NotFound); + } + finally + { + res.Dispose(); + } + } + } + + [Test] + public async Task Idempotency_PollCreate_ReplayStable() + { + await RunSoakAsync(fixture.Factory, async (client, key) => + { + return await client.SendAsJsonAsync( + HttpMethod.Post, "/api/polls", + new CreatePollRequest("SoakPoll"), + "soak-default-principal", + idempotencyKey: key); + }); + } +} + diff --git a/tests/integration/Interfold.Polls.IntegrationTests/Interfold.Polls.IntegrationTests.csproj b/tests/integration/Interfold.Polls.IntegrationTests/Interfold.Polls.IntegrationTests.csproj new file mode 100644 index 00000000..5e1c515e --- /dev/null +++ b/tests/integration/Interfold.Polls.IntegrationTests/Interfold.Polls.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Polls.IntegrationTests/Services/Scylla/ScyllaMappingRegressionTests.cs b/tests/integration/Interfold.Polls.IntegrationTests/Services/Scylla/ScyllaMappingRegressionTests.cs new file mode 100644 index 00000000..9cff0def --- /dev/null +++ b/tests/integration/Interfold.Polls.IntegrationTests/Services/Scylla/ScyllaMappingRegressionTests.cs @@ -0,0 +1,98 @@ +using Interfold.IntegrationTests.Shared; +using Interfold.Journals.Contracts.Ids; +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Polls.IntegrationTests.Services.Scylla; + +public sealed class ScyllaMappingRegressionTests : BaseEndpointTest +{ + [Test] + public async Task PollUuidParsing_AcceptsCompactAndHyphenated_RejectsInvalid() + { + var compact = Guid.NewGuid().ToString("N"); + var hyphenated = Guid.NewGuid().ToString("D"); + + var compactOk = PollId.TryParse(compact, provider: null, out var compactParsed); + var hyphenatedOk = PollId.TryParse(hyphenated, provider: null, out var hyphenatedParsed); + var invalidOk = PollId.TryParse("not-a-guid", provider: null, out _); + + using (Assert.Multiple()) + { + await Assert.That(compactOk).IsTrue(); + await Assert.That(hyphenatedOk).IsTrue(); + await Assert.That(invalidOk).IsFalse(); + await Assert.That(compactParsed.Value.ToString("N")).IsEqualTo(compact); + await Assert.That(hyphenatedParsed.Value.ToString("D")).IsEqualTo(hyphenated); + } + } + + [Test] + public async Task PollTypeMapping_KnownCodesMapExactly_UnknownCodesThrow() + { + // Pins the Option D 2026-07-17 strict-throw flip: every repo call site now uses + // the fallback-less `.FromCode()` overload, so unknown on-disk shorts + // must surface as ArgumentOutOfRangeException instead of silently coercing to Vote. + // The pre-flip lenient version of this test asserted `.IsEqualTo(PollType.Vote)` + // for MinValue/MaxValue — that behaviour is intentionally dead. + using (Assert.Multiple()) + { + await Assert.That((short)PollType.Vote).IsEqualTo((short)0); + await Assert.That((short)PollType.Choice).IsEqualTo((short)1); + await Assert.That((short)PollType.Approval).IsEqualTo((short)2); + + await Assert.That(((short)0).FromCode()).IsEqualTo(PollType.Vote); + await Assert.That(((short)1).FromCode()).IsEqualTo(PollType.Choice); + await Assert.That(((short)2).FromCode()).IsEqualTo(PollType.Approval); + + await Assert.That(() => short.MinValue.FromCode()) + .Throws() + .Because("Unknown on-disk PollType shorts must fail loud so corrupt/stale rows surface at the read site."); + await Assert.That(() => short.MaxValue.FromCode()) + .Throws() + .Because("Same strict contract as MinValue — no silent coercion to the default enum member."); + } + } + + [Test] + public async Task FriendshipLevelMapping_KnownCodesMapExactly_UnknownCodesThrow() + { + // Same strict-throw contract as PollType. Silently downgrading a corrupt + // trusted_friend row to Friend was the previous fallback — that hides a + // security-relevant miscoercion from operators, so the strict flip is the + // right default. + using (Assert.Multiple()) + { + await Assert.That(((short)0).FromCode()).IsEqualTo(FriendshipLevel.Friend); + await Assert.That(((short)1).FromCode()).IsEqualTo(FriendshipLevel.TrustedFriend); + + await Assert.That(() => short.MinValue.FromCode()) + .Throws() + .Because("Unknown on-disk FriendshipLevel shorts must fail loud rather than downgrading to Friend."); + await Assert.That(() => short.MaxValue.FromCode()) + .Throws() + .Because("Same strict contract as MinValue."); + } + } + + [Test] + public async Task UuidParsingRegression_TagJournalSettings_AcceptsCompactAndRejectsInvalid() + { + var tag = Guid.NewGuid().ToString("N"); + var journal = Guid.NewGuid().ToString("N"); + var field = Guid.NewGuid().ToString("N"); + + using (Assert.Multiple()) + { + await Assert.That(TagId.TryParse(tag, provider: null, out _)).IsTrue(); + await Assert.That(EntryId.TryParse(journal, provider: null, out _)).IsTrue(); + await Assert.That(FieldId.TryParse(field, provider: null, out _)).IsTrue(); + + await Assert.That(TagId.TryParse("bad", provider: null, out _)).IsFalse(); + await Assert.That(EntryId.TryParse("bad", provider: null, out _)).IsFalse(); + await Assert.That(FieldId.TryParse("bad", provider: null, out _)).IsFalse(); + } + } +} \ No newline at end of file diff --git a/tests/integration/Interfold.Settings.IntegrationTests/Controllers/AvatarSourceTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/AvatarSourceTests.cs new file mode 100644 index 00000000..26e781d2 --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/AvatarSourceTests.cs @@ -0,0 +1,176 @@ +using System.Net; +using Interfold.Alters.Contracts.Models; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Systems.Contracts.Models.Read; + +namespace Interfold.Settings.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class AvatarSourceTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + private const string ExternalSystemAvatar = "https://cdn.example.com/system-avatar.png"; + private const string ExternalAlterAvatar = "https://cdn.example.com/alter-avatar.png"; + + [Test] + public async Task Api_SettingsAvatarByUrl_PersistsAndExposesExternalSource() + { + using var client = TestClient.NoRedirect(fixture); + + var principalId = TestIds.NewSystemId("sys-extavatar", maxLen: 24); + await EnsureUserExistsAsync(client, principalId); + + using var uploadResponse = await client.SendAsJsonAsync( + HttpMethod.Put, "/api/settings/avatar", + new AvatarUrlUploadRequest(new AvatarUrl(ExternalSystemAvatar)), + principalId); + await Assert.That(uploadResponse.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var profileResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}", principalId); + var profile = await profileResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + // Passthrough: stored URL is returned verbatim, never qualified with the API origin. + await Assert.That(profile.Data.AvatarUrl?.Value).IsEqualTo(ExternalSystemAvatar); + await Assert.That(profile.Data.AvatarSource).IsEqualTo(AvatarSource.External); + } + } + + [Test] + public async Task Api_AlterAvatarByUrl_PersistsAndExposesExternalSource() + { + using var client = TestClient.NoRedirect(fixture); + + var principalId = TestIds.NewSystemId("sys-altext", maxLen: 20); + + var alterId = await CreateAlterAsync(client, principalId, "ExternalAvatarAlter", "external-alter", VisibilityLevel.Public); + + using var uploadResponse = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/systems/me/alters/{alterId}/avatar", + new AvatarUrlUploadRequest(new AvatarUrl(ExternalAlterAvatar)), + principalId); + await Assert.That(uploadResponse.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var alterResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}/alters/{alterId}", principalId); + var alter = await alterResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(alter.Data.AvatarUrl?.Value).IsEqualTo(ExternalAlterAvatar); + await Assert.That(alter.Data.AvatarSource).IsEqualTo(AvatarSource.External); + } + } + + /// + /// Shape-only validator covers ftp/file/relative URLs and over-length values uniformly. + /// We use one of the disallowed-scheme cases as the canonical 400 contract. + /// + [Test] + public async Task Api_SettingsAvatarByUrl_RejectsNonHttpUrl() + { + using var client = TestClient.NoRedirect(fixture); + + var principalId = TestIds.NewSystemId("sys-bad", maxLen: 16); + await EnsureUserExistsAsync(client, principalId); + + // AvatarUrl's constructor doesn't validate, so the typed record carries the + // ftp:// string through to the server unchanged. The 400 + avatar_url_invalid + // contract is the server-side rule under test. + using var uploadResponse = await client.SendAsJsonAsync( + HttpMethod.Put, "/api/settings/avatar", + new AvatarUrlUploadRequest(new AvatarUrl("ftp://example.com/avatar.png")), + principalId); + + var error = await uploadResponse.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(error.Code).IsEqualTo(ErrorCodes.AvatarUrlInvalid); + } + + [Test] + public async Task Api_AlterAvatarByUrl_RejectsRelativeUrl() + { + using var client = TestClient.NoRedirect(fixture); + + var principalId = TestIds.NewSystemId("sys-altrel", maxLen: 20); + + var alterId = await CreateAlterAsync(client, principalId, "RelativeAvatarAlter", "relative-alter"); + + // Same rationale as the sibling test — AvatarUrl accepts anything, and the + // server enforces the http(s)-scheme rule at validation time. + using var uploadResponse = await client.SendAsJsonAsync( + HttpMethod.Put, $"/api/systems/me/alters/{alterId}/avatar", + new AvatarUrlUploadRequest(new AvatarUrl("/relative/path.png")), + principalId); + + var error = await uploadResponse.ReadErrorAsync(HttpStatusCode.BadRequest); + await Assert.That(error.Code).IsEqualTo(ErrorCodes.AvatarUrlInvalid); + } + + // Isolation contract: this test builds its OWN InterfoldWebApplicationFactory via + // IWebFactoryFixture.CreatePrivateFactory() rather than mutating fixture.Factory + // (the session-shared one). The previous version wrote OCTOCON_AVATAR_PUBLIC_BASE + // and OCTOCON_AVATAR_STORAGE_ROOT via WithConfiguration on the shared factory — + // because StorageConfiguration binds via IOptionsMonitor, those writes cascaded + // live into every subsequent test's request pipeline, and any test that resolved + // IOptionsMonitor.Get() (e.g. via InterfoldPrincipalMiddleware) + // afterwards would throw OptionsValidationException on the AvatarPublicBase's + // [AbsoluteHttpUri] rule. That poisoning cascade produced ~50 downstream 500s in + // the full suite. Owning our own factory closes that vector — nothing this test + // writes can ever be seen by a test that runs on fixture.Factory. + // + // NotInParallel is retained on the shared "avatar-storage-config" bucket so the + // two SettingsControllerTests siblings (which also build private factories) don't + // run three factory builds concurrently for the same backend — pending the + // Group-B Npgsql pool sizing fix, parallel builds can exhaust the default pool of + // 5 connections during SecretsPreBuildLoader's Postgres fetch. Once Group B lands + // this attribute can be removed. + [Test, NotInParallel("avatar-storage-config")] + public async Task Api_SettingsAvatarMultipart_ReportsLocalSource() + { + await IsolatedAvatarStorage.RunAsync(fixture, async (client, publicBasePath) => + { + var principalId = TestIds.NewSystemId("sys-localavatar", maxLen: 24); + + using var uploadRequest = BuildMultipartUploadRequest(client, "/api/settings/avatar", principalId, "avatar.png", "image/png"); + using var uploadResponse = await client.SendAsync(uploadRequest); + await Assert.That(uploadResponse.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var profileResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}", principalId); + var profile = await profileResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(profile.Data.AvatarSource).IsEqualTo(AvatarSource.Local); + // Local avatars get the request origin prepended; the URL path must sit + // under our configured public base so external observers can fetch the bytes. + await Assert.That(UrlPathStartsWith(profile.Data.AvatarUrl?.Value, $"{publicBasePath}/{principalId}/self/")).IsTrue(); + } + + // End-to-end serving check (mirrors the sibling assertion in + // SettingsControllerTests.Api_SettingsAvatarMultipart_PersistsAndServesAvatar): + // the avatar URL the SPA receives MUST be servable. This test covers the + // local-avatar-source codepath specifically — the assertion sits here, not in + // the source-only happy-path tests above, because this is the only test in + // the file that exercises a real multipart upload (the others use external + // URLs and never persist bytes). + using var avatarRequest = new HttpRequestMessage(HttpMethod.Get, profile.Data.AvatarUrl?.Value); + // Anonymous on purpose — see the sibling test for the rationale (URL is the capability). + using var avatarResponse = await client.SendAsync(avatarRequest); + var avatarBytes = await avatarResponse.Content.ReadAsByteArrayAsync(); + using (Assert.Multiple()) + { + await Assert.That(avatarResponse.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"Expected the local-source avatar URL to be servable. URL was '{profile.Data.AvatarUrl}'."); + await Assert.That(avatarBytes.Length).IsGreaterThan(0) + .Because("Expected non-zero bytes back from the avatar GET."); + } + }); + } +} + diff --git a/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerImportSpTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerImportSpTests.cs new file mode 100644 index 00000000..685b4107 --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerImportSpTests.cs @@ -0,0 +1,182 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Contracts.Models.Read; +using Interfold.Settings.Contracts.Models.Wire; + +namespace Interfold.Settings.IntegrationTests.Controllers; + +/// +/// End-to-end tests for the async-import dispatch endpoint. The controller layer is the +/// one piece the unit tests cannot exercise on their own — they cover the handler, the +/// repository, the queue, and the worker in isolation, but not the HTTP plumbing +/// (envelope construction from , CommandAccepted +/// status mapping, body serialisation of ImportDispatchResponse). +/// +/// +/// We run against the in-memory fixture only. Cassandra and Scylla variants would +/// re-test the same controller code path with no additional coverage and would slow the +/// CI suite measurably. The repository-port equivalence is pinned separately by +/// InMemoryImportOperationRepositoryTests and a future +/// ScyllaImportOperationRepositoryTests integration. +/// +/// +/// +/// Side-effect note. The real SpImportJobRunner registered in +/// Program.cs resolves to SimplyPluralImportService, which would normally +/// hit https://api.apparyllis.com/v1. In the test factory all HTTP clients are +/// built from TestHttpClientFactory.CreateDefaultClient(), which dispatches by +/// path through TestServer — the absolute Apparyllis URL resolves to a missing route +/// inside our own API, the service throws / returns a graceful failure, and the worker +/// publishes . This is intentional: it lets +/// us assert the full dispatch -> worker -> bus path end-to-end without making an +/// outbound network call. +/// +/// +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class SettingsControllerImportSpTests(InMemoryWebFactoryFixture fixture) : BaseEndpointTest +{ + /// + /// POST /api/settings/import-sp must return HTTP 202 Accepted with an + /// ImportDispatchResponse body carrying a non-empty operation_id and + /// status = "queued". This is the load-bearing controller contract: the + /// Compose app ignores the body on success but a future client (or operator log) + /// uses the operation_id as the correlation handle, and the 202 itself is what + /// keeps the response off the synchronous retry path that produced the original + /// duplicate-import bug. + /// + [Test] + public async Task ImportSp_FreshDispatch_Returns202WithOperationId() + { + using var client = TestClient.NoRedirect(fixture); + var principal = TestIds.NewSystemId("sp-import-202", maxLen: 24); + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/import-sp", + new SettingsImportRequest(new ImportToken("synthetic-sp-token")), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Accepted); + + using (Assert.Multiple()) + { + await Assert.That(envelope.Data.OperationId.Value) + .IsNotEqualTo(Guid.Empty) + .Because("The response body must carry the dispatcher's operation_id; otherwise the correlation handle promised in ImportDispatchResponse never reaches the caller."); + await Assert.That(envelope.Data.Status) + .IsEqualTo(ImportOperationDispatchStatus.Queued) + .Because("A fresh dispatch against an empty per-system slot must surface as 'queued'; 'running' would indicate the controller is reporting the wrong claim outcome."); + } + } + + /// + /// The PluralKit symmetric of the SP dispatch test. Pins that the + /// /api/settings/import-pk endpoint also returns 202 Accepted with the same + /// ImportDispatchResponse shape so the contract is uniform across platforms. + /// + [Test] + public async Task ImportPk_FreshDispatch_Returns202WithOperationId() + { + using var client = TestClient.NoRedirect(fixture); + var principal = TestIds.NewSystemId("pk-import-202", maxLen: 24); + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/import-pk", + new SettingsImportRequest(new ImportToken("synthetic-pk-token")), + principal); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Accepted); + + using (Assert.Multiple()) + { + await Assert.That(envelope.Data.Status).IsEqualTo(ImportOperationDispatchStatus.Queued); + await Assert.That(envelope.Data.OperationId.Value).IsNotEqualTo(Guid.Empty); + } + } + + /// + /// The full dispatch -> worker -> bus chain: after the controller accepts the + /// dispatch, the background worker must actually pick up the job and the lifecycle + /// event must reach the cluster event bus. We assert on the failure event because + /// the real SP runner has no live API to talk to in the test environment — but the + /// shape of the assertion (Subscribe before POST, then await with timeout) is + /// identical to the success-path one a future test could write with a stub runner. + /// + [Test] + public async Task ImportSp_AfterDispatch_WorkerProcessesAndPublishesFailedEvent(CancellationToken token) + { + using var client = TestClient.NoRedirect(fixture); + var principal = TestIds.NewSystemId("sp-import-wf", maxLen: 24); + await EnsureUserExistsAsync(client, principal); + + // Subscribe BEFORE dispatching so we can't miss the publish. + using var subscribeCts = CancellationTokenSource.CreateLinkedTokenSource(token); + subscribeCts.CancelAfter(TimeSpan.FromSeconds(15)); + await using var enumerator = fixture.Factory.EventBus + .SubscribeAsync(subscribeCts.Token) + .GetAsyncEnumerator(subscribeCts.Token); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/import-sp", + new SettingsImportRequest(new ImportToken("synthetic-sp-token")), + principal, + ct: token); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.Accepted) + .Because($"Setup invariant: this test only proves the worker runs after a successful dispatch. Body: {await res.Content.ReadAsStringAsync(token)}"); + + // Drain the bus until we see a failed event for OUR principal (other parallel + // tests may share the bus). The worker either gracefully fails or throws — both + // routes publish SimplyPluralImportFailedEvent, which is the load-bearing + // invariant: the client never gets stuck on Importing after a worker pickup. + // + // SimplyPluralImportFailedEvent.TargetSystemId is a ScopedSystemId whose .Value + // is the scoped form ("nam:") produced by the JWT-derived principal path, + // while `principal` above is the bare 24-char raw id this test minted. Comparing + // .Value would ordinal-mismatch on the region prefix and drop every event on the + // floor. Compare .RawId (the prefix-stripped form) instead so the shape matches + // what `principal` holds. + SimplyPluralImportFailedEvent? observed = null; + while (await enumerator.MoveNextAsync()) + { + if (string.Equals(enumerator.Current.TargetSystemId.RawId, principal, StringComparison.Ordinal)) + { + observed = enumerator.Current; + break; + } + } + + await Assert.That(observed).IsNotNull() + .Because("The background worker must publish a SimplyPluralImportFailedEvent for our principal after the dispatch — otherwise the Compose app's Importing dialog would never resolve."); + } + + /// + /// Empty-token rejection: the handler must reject before claiming a slot, the + /// controller must surface a non-202 error response. The + /// wrapper accepts empty strings at construction (the whole point of the test is + /// to see the handler reject them), so we build the record directly with an empty + /// token rather than the raw JSON body the pre-sweep test used. + /// + [Test] + public async Task ImportSp_EmptyToken_ReturnsErrorWithoutAccepting() + { + using var client = TestClient.NoRedirect(fixture); + var principal = TestIds.NewSystemId("sp-import-bad", maxLen: 24); + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/import-sp", + new SettingsImportRequest(new ImportToken("")), + principal); + + using (Assert.Multiple()) + { + await Assert.That(res.StatusCode).IsNotEqualTo(HttpStatusCode.Accepted) + .Because($"Empty tokens must not be Accepted — otherwise the handler is silently producing valid dispatches for garbage input. Body: {await res.Content.ReadAsStringAsync()}"); + await Assert.That((int)res.StatusCode).IsGreaterThanOrEqualTo(400) + .Because($"Empty token is invalid input and must surface as a 4xx error response, got {(int)res.StatusCode}. Body: {await res.Content.ReadAsStringAsync()}"); + } + } +} diff --git a/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerTests.cs new file mode 100644 index 00000000..b6c276e2 --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsControllerTests.cs @@ -0,0 +1,189 @@ +using System.Net; +using Interfold.Alters.Contracts.Models; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Settings.Contracts.Models.Read; +using Interfold.Settings.Contracts.Models.Wire; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Systems.Contracts.Models.Read; + +namespace Interfold.Settings.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class SettingsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task SettingsField_InvalidType_ReturnsBadRequest() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-field-fallback"; + await EnsureUserExistsAsync(client, principal); + + // type = "garbage" — the Elixir server used to fold this to "text", but we deliberately + // hold every enum boundary to the same fail-fast contract EnumWireExtensions documents. + // Kept as raw JSON: the whole point is to bypass typed serialisation and prove the + // server rejects the malformed enum literal at the wire, which a typed FieldType + // value could never produce. + using var res = await client.SendRawJsonAsync( + HttpMethod.Post, "/api/settings/fields", + "{\"name\":\"FallbackField\",\"type\":\"garbage\"}", + principal); + + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + } + + [Test] + public async Task SettingsField_MissingType_FallsBackToText_ReturnsCreatedWithId() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-field-missing-type"; + await EnsureUserExistsAsync(client, principal); + + // type absent entirely — the typed record has Type as `FieldType?` and passing null + // both preserves the "absent" wire shape AND documents the intent at the C# call site. + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/fields", + new SettingsCreateFieldRequest("NoTypeField", Type: null, SecurityLevel: null, Locked: null), + principal); + + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + await Assert.That(envelope.Data.Id).IsNotEqualTo(default(FieldId)); + } + + [Test] + public async Task Idempotency_SettingsUsernameUpdate_ReplayStable() + { + await RunSoakAsync(fixture.Factory, async (client, key) => + { + return await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/username", + new SettingsUsernameRequest(new Username("soakuser")), + "soak-default-principal", + idempotencyKey: key); + }); + } + + [Test] + public async Task SettingsField_Create_ReturnsCreatedFieldId() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-field-defaults"; + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAsJsonAsync( + HttpMethod.Post, "/api/settings/fields", + new SettingsCreateFieldRequest("DefaultSecurityField", FieldType.Text, SecurityLevel: null, Locked: null), + principal); + + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.Created); + await Assert.That(envelope.Data.Id).IsNotEqualTo(default(FieldId)); + } + + // Isolation contract for both avatar multipart tests below: each builds its OWN + // InterfoldWebApplicationFactory via IWebFactoryFixture.CreatePrivateFactory() + // instead of mutating fixture.Factory. The previous version wrote + // OCTOCON_AVATAR_STORAGE_ROOT and OCTOCON_AVATAR_PUBLIC_BASE via WithConfiguration + // on the session-shared factory. StorageConfiguration binds via IOptionsMonitor, + // so those writes cascaded live into every subsequent test's request pipeline; + // any test that resolved IOptionsMonitor.Get() afterwards + // (e.g. through InterfoldPrincipalMiddleware) would throw + // OptionsValidationException on the AvatarPublicBase's [AbsoluteHttpUri] rule, + // producing ~50 downstream 500s in the full suite. Private factories close that + // vector at the design level — nothing these tests write can ever be seen by a + // test that runs on fixture.Factory. + // + // NotInParallel is retained on the shared "avatar-storage-config" bucket so the + // three avatar-multipart tests (this one, the alter sibling below, and + // AvatarSourceTests.Api_SettingsAvatarMultipart_ReportsLocalSource) don't run + // three factory builds concurrently for the same backend — pending the Group-B + // Npgsql pool sizing fix, parallel builds can exhaust the default pool of 5 + // connections during SecretsPreBuildLoader's Postgres fetch. Once Group B lands + // this attribute can be removed. + [Test, NotInParallel("avatar-storage-config")] + public async Task Api_SettingsAvatarMultipart_PersistsAndServesAvatar() + { + await IsolatedAvatarStorage.RunAsync(fixture, async (client, publicBasePath) => + { + var principalId = TestIds.NewSystemId("sys-avatar", maxLen: 18); + + using var uploadRequest = BuildMultipartUploadRequest(client, "/api/settings/avatar", principalId, "avatar-system.png", "image/png"); + using var uploadResponse = await client.SendAsync(uploadRequest); + await Assert.That(uploadResponse.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var profileResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}", principalId); + var profile = await profileResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + using (Assert.Multiple()) + { + await Assert.That(profile.Data.AvatarUrl).IsNotNull(); + await Assert.That(UrlPathStartsWith(profile.Data.AvatarUrl?.Value, $"{publicBasePath}/{principalId}/self/")).IsTrue(); + } + + // End-to-end serving check: the avatar_url that the SPA receives from the + // profile response must actually return the bytes when fetched. Before the + // Phase-2 middleware in Program.cs this would 404 — the API wrote the file + // to disk and stamped a URL, but no handler served the bytes back. The + // assertion below is the regression guard for that "URL stamped but no + // listener" gap. We do it inside the same test so any future shape change + // to LocalAvatarStorage's URL format and the middleware's path matching is + // caught in one place. + using var avatarRequest = new HttpRequestMessage(HttpMethod.Get, profile.Data.AvatarUrl?.Value); + // Avatar GETs are unauthenticated (the URL itself is the capability — this + // is the same exposure model as a CDN-fronted setup). Anonymous request is + // intentional so the assertion reflects what the SPA / external embed sees. + using var avatarResponse = await client.SendAsync(avatarRequest); + var avatarBytes = await avatarResponse.Content.ReadAsByteArrayAsync(); + using (Assert.Multiple()) + { + await Assert.That(avatarResponse.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"Expected the avatar URL returned by the profile endpoint to be servable. URL was '{profile.Data.AvatarUrl}'."); + await Assert.That(avatarBytes.Length).IsGreaterThan(0) + .Because("Expected non-zero bytes back from the avatar GET — a zero-length response would indicate the middleware matched the path but couldn't read the file off disk."); + } + }); + } + + // Isolation contract: see the sibling Api_SettingsAvatarMultipart_PersistsAndServesAvatar + // above for the full rationale — this test also builds its OWN factory via + // CreatePrivateFactory so its avatar-storage config mutations never leak into + // fixture.Factory and cannot poison other tests in the session. + [Test, NotInParallel("avatar-storage-config")] + public async Task Api_AlterAvatarMultipart_PersistsAndReflectsOnPublicAlter() + { + await IsolatedAvatarStorage.RunAsync(fixture, async (client, publicBasePath) => + { + var principalId = TestIds.NewSystemId("sys-alter-avatar", maxLen: 24); + + var alterId = await CreateAlterAsync(client, principalId, "AvatarTarget", "avatar-parity", VisibilityLevel.Public); + + using var uploadRequest = BuildMultipartUploadRequest(client, $"/api/systems/me/alters/{alterId}/avatar", principalId, "avatar-alter.png", "image/png"); + using var uploadResponse = await client.SendAsync(uploadRequest); + await Assert.That(uploadResponse.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var publicAlterResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}/alters/{alterId}", principalId); + var publicAlter = await publicAlterResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + var expectedPrefix = $"{publicBasePath}/{principalId}/{alterId}/"; + using (Assert.Multiple()) + { + await Assert.That(publicAlter.Data.AvatarUrl).IsNotNull(); + await Assert.That(UrlPathStartsWith(publicAlter.Data.AvatarUrl?.Value, expectedPrefix)).IsTrue(); + } + + using var deleteRes = await client.SendAuthedDeleteAsync($"/api/systems/me/alters/{alterId}/avatar", principalId); + await Assert.That(deleteRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + + using var afterDeleteResponse = await client.SendAuthedGetAsync($"/api/systems/{principalId}/alters/{alterId}", principalId); + var afterDelete = await afterDeleteResponse.ReadEnvelopeAsync(HttpStatusCode.OK); + + await Assert.That(afterDelete.Data.AvatarUrl).IsNull(); + }); + } +} + diff --git a/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsExportEndpointTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsExportEndpointTests.cs new file mode 100644 index 00000000..f90a789a --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/Controllers/SettingsExportEndpointTests.cs @@ -0,0 +1,118 @@ +using System.Net; +using System.Text.Json; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; + +namespace Interfold.Settings.IntegrationTests.Controllers; + +// GET /api/settings/export coverage — mirrors the Elixir bot's +// OctoconDiscord.Commands.Export flow (see octocon/lib/octocon_discord/commands/export.ex): +// enum-driven format selection, raw JSON body (no Response envelope), and +// Content-Disposition stamped with the wire spelling of the format. +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class SettingsExportEndpointTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task Export_MissingFormat_ReturnsBadRequest() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "export-missing-format"; + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAuthedGetAsync("/api/settings/export", principal); + var body = await res.Content.ReadAsStringAsync(); + + using (Assert.Multiple()) + { + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.BadRequest) + .Because($"Expected 400 for missing ?format; body: {body}"); + await Assert.That(body).Contains(ErrorCodes.InvalidExportFormat.Value) + .Because($"Expected error body to carry the invalid_export_format code; body: {body}"); + } + } + + [Test] + public async Task Export_UnknownFormat_ReturnsBadRequest() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "export-unknown-format"; + await EnsureUserExistsAsync(client, principal); + + // ASP.NET Core's enum model binder is case-insensitive but rejects unrecognised names. + // The [ApiController] filter surfaces the failure as 400 before entering the action. + using var res = await client.SendAuthedGetAsync("/api/settings/export?format=xyz", principal); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + } + + [Test] + public async Task Export_PkFormat_ReturnsAttachmentWithVersion2Body() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "export-pk-happy"; + await EnsureUserExistsAsync(client, principal); + _ = await CreateAlterAsync(client, principal, "AlphaAlter"); + + using var res = await client.SendAuthedGetAsync("/api/settings/export?format=pk", principal); + var body = await res.Content.ReadAsStringAsync(); + + using (Assert.Multiple()) + { + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"Expected 200 for ?format=pk; body: {body}"); + await Assert.That(res.Content.Headers.ContentType?.MediaType).IsEqualTo("application/json"); + await Assert.That(res.Content.Headers.ContentDisposition?.DispositionType).IsEqualTo("attachment"); + await Assert.That(res.Content.Headers.ContentDisposition?.FileName).IsEqualTo("octocon_export_pk.json"); + + using var doc = JsonDocument.Parse(body); + await Assert.That(doc.RootElement.GetProperty("version").GetInt32()).IsEqualTo(2) + .Because("PluralKit v2 importable payload MUST advertise version=2 or PK will refuse the file."); + await Assert.That(doc.RootElement.GetProperty("members").ValueKind).IsEqualTo(JsonValueKind.Array); + } + } + + [Test] + public async Task Export_FullFormat_ReturnsAttachmentWithFullBody() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "export-full-happy"; + await EnsureUserExistsAsync(client, principal); + _ = await CreateAlterAsync(client, principal, "AlphaAlter"); + + using var res = await client.SendAuthedGetAsync("/api/settings/export?format=full", principal); + var body = await res.Content.ReadAsStringAsync(); + + using (Assert.Multiple()) + { + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because($"Expected 200 for ?format=full; body: {body}"); + await Assert.That(res.Content.Headers.ContentType?.MediaType).IsEqualTo("application/json"); + await Assert.That(res.Content.Headers.ContentDisposition?.DispositionType).IsEqualTo("attachment"); + await Assert.That(res.Content.Headers.ContentDisposition?.FileName).IsEqualTo("octocon_export_full.json"); + + using var doc = JsonDocument.Parse(body); + await Assert.That(doc.RootElement.TryGetProperty("user", out _)).IsTrue(); + await Assert.That(doc.RootElement.TryGetProperty("alters", out _)).IsTrue(); + await Assert.That(doc.RootElement.TryGetProperty("fronts", out _)).IsTrue(); + await Assert.That(doc.RootElement.TryGetProperty("tags", out _)).IsTrue(); + await Assert.That(doc.RootElement.TryGetProperty("polls", out _)).IsTrue(); + } + } + + [Test] + public async Task Export_PkFormat_IsCaseInsensitive() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "export-pk-case"; + await EnsureUserExistsAsync(client, principal); + + using var res = await client.SendAuthedGetAsync("/api/settings/export?format=PK", principal); + await Assert.That(res.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because("Enum model binder is case-insensitive so ?format=PK binds to ExportFormat.Pk."); + } +} diff --git a/tests/integration/Interfold.Settings.IntegrationTests/Interfold.Settings.IntegrationTests.csproj b/tests/integration/Interfold.Settings.IntegrationTests/Interfold.Settings.IntegrationTests.csproj new file mode 100644 index 00000000..fc6baa97 --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/Interfold.Settings.IntegrationTests.csproj @@ -0,0 +1,24 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SimplyPluralImportServiceIsSpCdnUrlTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SimplyPluralImportServiceIsSpCdnUrlTests.cs new file mode 100644 index 00000000..b6c8a4b8 --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SimplyPluralImportServiceIsSpCdnUrlTests.cs @@ -0,0 +1,62 @@ +using Interfold.IntegrationTests.Shared; +using Interfold.Settings.Api.Services.SimplyPlural; + +namespace Interfold.Settings.IntegrationTests.SimplyPluralImport; + +/// +/// Unit coverage for . The discriminator +/// gates whether an arbitrary members.avatarUrl / users.avatarUrl string gets +/// queued for SP CDN rehost or stored as an external passthrough URL — the latter is a +/// one-way trip and would silently 404 after Apparyllis sunsets, so a misclassification +/// here means lost avatars. The host list was widened from `spaces.apparyllis.com` alone +/// to also cover the canonical post-v1.12 upload URL (`serve.apparyllis.com`) and the +/// legacy DigitalOcean Spaces hostname; this test pins all three. +/// +public sealed class SimplyPluralImportServiceIsSpCdnUrlTests : BaseEndpointTest +{ + [Test] + [Arguments("https://spaces.apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("https://serve.apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("https://simply-plural.sfo3.digitaloceanspaces.com/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("HTTPS://SPACES.APPARYLLIS.COM/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("HTTPS://Serve.Apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] + public async Task IsSpCdnUrl_ReturnsTrue_ForEverySpHostedAvatarVariant(string url) + { + await Assert.That(SimplyPluralImportService.IsSpCdnUrl(url)).IsTrue(); + } + + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments(" ")] + [Arguments("https://example.com/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("http://spaces.apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] // plain http, not https + [Arguments("//spaces.apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] // protocol-relative + [Arguments("ftp://spaces.apparyllis.com/avatars/uid-synth/avatar-uuid-synth")] + [Arguments("https://dist.apparyllis.com/resources/Logo_Apparyllis_Square_1024.png")] // SP-owned but + // not an avatar + // CDN — content + // CDN only + public async Task IsSpCdnUrl_ReturnsFalse_ForEverythingElse(string? url) + { + await Assert.That(SimplyPluralImportService.IsSpCdnUrl(url)).IsFalse(); + } + + /// + /// Pins the documented limitation of the current + /// matcher: it's a case-insensitive StartsWith, so attacker-controlled URLs whose host + /// has an SP-host as a literal prefix (e.g. spaces.apparyllis.com.evil.test) currently + /// pass. That predates this change — the original single-host check had the same shape — but + /// we lock the behaviour here so any future tightening (e.g. Uri.Host equality) is + /// observably a behaviour change, not a silent one. + /// + [Test] + public async Task IsSpCdnUrl_StartsWithSemantics_AcceptsSuffixedHostsAsKnownGap() + { + // Self-SSRF only: the SP token is user-supplied, so the worst case is a user mis-importing + // their own attacker-controlled URL. Tightening this requires switching to Uri.Host + // equality and is intentionally left out of the host-list widening patch. + await Assert.That(SimplyPluralImportService.IsSpCdnUrl( + "https://spaces.apparyllis.com.evil.test/avatars/uid-synth/avatar-uuid-synth")).IsTrue(); + } +} diff --git a/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpImportTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpImportTests.cs new file mode 100644 index 00000000..0085ee3f --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpImportTests.cs @@ -0,0 +1,1259 @@ +using System.Globalization; +using System.Text.Json; +using System.Web; +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.InMemory; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Api.Services.SimplyPlural; +using Interfold.Settings.Api.SimplyPlural; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Domain.Abstractions.Repository; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Interfold.Settings.IntegrationTests.SimplyPluralImport; + +/// +/// Regression coverage for the Simply Plural import "today-date" bug fix. Every test wires up the +/// real against the InMemory persistence stack and feeds +/// it canned SP responses through , so we keep coverage +/// after Apparyllis sunsets the live API. +/// +/// Data hygiene: every test mints fresh identifiers via / +/// and uses obviously-synthetic timestamps (never the real values from the production dry-run +/// captures). No real account data lives in this file. +/// +public sealed class SpImportTests : BaseEndpointTest +{ + private static SystemId Uid() => TestIds.NewTypedSystemId("sys", maxLen: 16); + private static string MemberUuid() => Guid.NewGuid().ToString("N"); + + // Synthetic timestamps (ms since Unix epoch). The whole point of this test file is to + // verify the import respects these dates *exactly* and never silently substitutes today. + private const long Date_2020_01_15 = 1579082400000; // 2020-01-15T10:00:00Z + private const long Date_2021_06_10 = 1623333600000; // 2021-06-10T14:00:00Z + private const long Date_2021_06_10_End = 1623337200000; // 2021-06-10T15:00:00Z + private const long Date_2022_03_05 = 1646474400000; // 2022-03-05T10:00:00Z + + [Test] + public async Task LiveFronter_WithRealStartTime_PreservesPastDate() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var frontId = MemberUuid(); + + // Sticky live front with a real past startTime — must be preserved verbatim. + var liveFronters = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = frontId, + content = new { member = alpha, startTime = Date_2020_01_15, live = true }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, currentFrontersJson: liveFronters); + + var (sp, frontingRepo, _, _, _, _) = await RunImportAsync(stub, systemId); + await Assert.That(sp).IsNotNull(); + + var active = await frontingRepo.ListActiveAsync(systemId); + await Assert.That(active.Count).IsEqualTo(1); + + var expected = DateTimeOffset.FromUnixTimeMilliseconds(Date_2020_01_15); + await Assert.That(active[0].Front.TimeStart).IsEqualTo(expected); + + // Defensive: an accidental DateTimeOffset.UtcNow substitution would land within + // a few seconds of "now"; the synthetic date is years in the past. + await Assert.That(active[0].Front.TimeStart) + .IsLessThan(DateTimeOffset.UtcNow.AddDays(-30)); + } + + [Test] + public async Task LiveFronter_StartTimeZero_FallsBackToObjectId() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // SP omits startTime on some sticky/primary live fronts. The importer now decodes + // the front document's ObjectId for its creation second instead of falling back to + // lastOperationTime (which drifts on every edit). Golden 24-hex id 0x671425cd -> + // 2024-10-19 21:34:05Z. We set lastOperationTime to a deliberately different date + // so this test fails loudly if any future regression starts reading it again. + const string spFrontIdHistorical = "671425cd02c63d13c59ba474"; + var expectedStart = new DateTimeOffset(2024, 10, 19, 21, 34, 5, TimeSpan.Zero); + + var liveFronters = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spFrontIdHistorical, + content = new + { + member = alpha, + startTime = 0L, + lastOperationTime = Date_2021_06_10, + live = true, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, currentFrontersJson: liveFronters); + + var (_, frontingRepo, _, _, _, _) = await RunImportAsync(stub, systemId); + + var active = await frontingRepo.ListActiveAsync(systemId); + await Assert.That(active.Count).IsEqualTo(1); + + using (Assert.Multiple()) + { + await Assert.That(active[0].Front.TimeStart).IsEqualTo(expectedStart); + // ObjectId wins over lastOperationTime: the 2021-06-10 value must NOT be used. + await Assert.That(active[0].Front.TimeStart) + .IsNotEqualTo(DateTimeOffset.FromUnixTimeMilliseconds(Date_2021_06_10)); + await Assert.That(active[0].Front.TimeStart) + .IsLessThan(DateTimeOffset.UtcNow.AddDays(-30)); + } + } + + [Test] + public async Task LiveFronter_BothZero_SkippedWithWarning() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var frontId = MemberUuid(); + + // Both startTime and lastOperationTime missing — must be skipped, never stamped today. + var liveFronters = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = frontId, + content = new + { + member = alpha, + startTime = 0L, + lastOperationTime = 0L, + live = true, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, currentFrontersJson: liveFronters); + + var (_, frontingRepo, _, _, _, logger) = await RunImportAsync(stub, systemId); + + var active = await frontingRepo.ListActiveAsync(systemId); + await Assert.That(active.Count).IsEqualTo(0); + + var warnings = logger.Records + .Where(r => r.Level == LogLevel.Warning && r.Message.Contains("Skipping live SP fronter")) + .ToArray(); + using (Assert.Multiple()) + { + await Assert.That(warnings.Length).IsEqualTo(1); + // Confirm the warning mentions the synthetic SP member uuid so the operator + // can trace which row was skipped without enabling debug logging. + await Assert.That(warnings[0].Message).Contains(alpha); + } + } + + [Test] + public async Task HistoricalFront_StartAndEnd_RetainOriginalTimes() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var historicalFrontId = MemberUuid(); + + // Closed historical front in a single past chunk. ImportFrontsAsync sends one request + // per chunk window with startTime/endTime query params; OnGetPrefix lets the same body + // serve every chunk request. Each chunk returning the same payload is fine because + // ImportFrontsAsync dedupes by SpEntity.Id. + var historyJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = historicalFrontId, + content = new + { + member = alpha, + startTime = Date_2021_06_10, + endTime = Date_2021_06_10_End, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, frontHistoryJson: historyJson, currentFrontersJson: "[]"); + + var (_, frontingRepo, _, _, _, _) = await RunImportAsync(stub, systemId); + + var history = await frontingRepo.ListHistoryBetweenAsync( + systemId, + DateTimeOffset.FromUnixTimeMilliseconds(0), + DateTimeOffset.UtcNow.AddYears(1)); + + await Assert.That(history.Count).IsEqualTo(1); + var entry = history[0]; + using (Assert.Multiple()) + { + await Assert.That(entry.TimeStart).IsEqualTo(DateTimeOffset.FromUnixTimeMilliseconds(Date_2021_06_10)); + await Assert.That(entry.TimeEnd).IsEqualTo(DateTimeOffset.FromUnixTimeMilliseconds(Date_2021_06_10_End)); + } + } + + [Test] + public async Task ChunkLoop_IssuesExactlyNumberOfChunksRequests_NoOffByOne() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + var stub = BuildSp(sysId.Value, alpha, + frontHistoryJson: "[]", + currentFrontersJson: "[]"); + + await RunImportAsync(stub, systemId); + + // ImportFrontsAsync chunks the SP epoch (2015-01-01) → now in 6-month windows. + // Pre-fix the loop ran `i <= numberOfChunks`, issuing one extra request that + // always returned `[]`. Compute the expected count with the same formula the + // production code uses so the assertion stays correct as time marches on. + const long startEpoch = 1_420_070_400_000; + const int monthInterval = 6; + var chunkSizeMs = (long)monthInterval * 30 * 24 * 60 * 60 * 1000; + var endTimeMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var expectedChunks = (int)Math.Ceiling((double)(endTimeMs - startEpoch) / chunkSizeMs); + + var historyRequests = stub.RequestsTo($"/v1/frontHistory/{sysId}"); + await Assert.That(historyRequests.Count).IsEqualTo(expectedChunks); + + // Sanity-check: no captured chunk should have startTime > endTime (the off-by-one + // bug produced exactly that on its final iteration). + foreach (var url in historyRequests) + { + var queryIndex = url.IndexOf('?'); + await Assert.That(queryIndex).IsGreaterThan(-1); + var query = HttpUtility.ParseQueryString(url[(queryIndex + 1)..]); + var startMs = long.Parse(query["startTime"]!, CultureInfo.InvariantCulture); + var endMs = long.Parse(query["endTime"]!, CultureInfo.InvariantCulture); + await Assert.That(startMs).IsLessThanOrEqualTo(endMs); + } + } + + [Test] + public async Task Poll_TitleAndDescriptionTooLong_TruncatedToHandlerLimits() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // SP has no length limits server-side; Interfold's CreatePollCommandHandler enforces + // title <= 100 and desc <= 2000. The import path bypasses the handler and previously + // truncated to 200/3000, leaving rows that the public API would reject on next edit. + var longTitle = new string('t', 250); + var longDesc = new string('d', 4000); + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = MemberUuid(), + content = new + { + name = longTitle, + desc = longDesc, + custom = false, + endTime = 0L, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, _) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + await Assert.That(polls[0].Title.Length).IsEqualTo(100); + await Assert.That(polls[0].Description).IsNotNull(); + await Assert.That(polls[0].Description!.Length).IsEqualTo(2000); + } + + [Test] + public async Task CustomPoll_WithEmptyOptionsArray_IsSkippedAndWarned() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + // SP's v1 schema marks `options` as optional. Importing a custom poll with no options + // would create a "choice" row that users can't vote on, so the importer should bail. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic choice", + desc = "", + custom = true, + endTime = 0L, + options = Array.Empty(), + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(0); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollId, StringComparison.Ordinal) && + r.Message.Contains("no options", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task CustomPoll_WithMissingOptionsKey_IsSkippedAndWarned() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + // Same as the empty-array case but `options` is omitted entirely. SpPollContent maps + // both shapes to a null `Options`, and the importer should treat them identically. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic choice no key", + desc = "", + custom = true, + endTime = 0L, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(0); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollId, StringComparison.Ordinal) && + r.Message.Contains("no options", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task Poll_VoteWithUnmappedMemberId_IsSkippedAndWarned() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + var unmappedVoter = MemberUuid(); + + // Two votes: one from the imported member (mapped → alter), one from a member uuid we + // never imported. The unmapped vote previously slipped through as a raw 24-char SP id + // in the `votes[i].id` field; we now drop it and surface a per-poll warning. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic vote", + desc = "", + custom = false, + endTime = 0L, + allowAbstain = true, + allowVeto = false, + votes = new[] + { + new { id = alpha, vote = "yes", comment = "synthetic-mapped" }, + new { id = unmappedVoter, vote = "no", comment = "synthetic-unmapped" }, + }, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var responses = polls[0].Data.GetProperty("responses"); + await Assert.That(responses.GetArrayLength()).IsEqualTo(1); + + var onlyVote = responses[0]; + // The mapped vote carries the numeric alter id, never the SP uuid. + await Assert.That(onlyVote.GetProperty("alter_id").ValueKind).IsEqualTo(JsonValueKind.Number); + await Assert.That(onlyVote.GetProperty("vote").GetString()).IsEqualTo("yes"); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollId, StringComparison.Ordinal) && + r.Message.Contains("unmappable votes", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task Poll_VoteWithEmptyOrWhitespaceId_IsSkipped() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + // Defensive coverage: SP's voteType only declares `id: string` without a minLength, + // so blank ids could theoretically appear. They should never become rows in our data. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic blank-id votes", + desc = "", + custom = false, + endTime = 0L, + votes = new[] + { + new { id = "", vote = "yes", comment = "" }, + new { id = " ", vote = "no", comment = "" }, + new { id = alpha, vote = "abstain", comment = "synthetic" }, + }, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, _) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var responses = polls[0].Data.GetProperty("responses"); + await Assert.That(responses.GetArrayLength()).IsEqualTo(1); + await Assert.That(responses[0].GetProperty("vote").GetString()).IsEqualTo("abstain"); + } + + [Test] + public async Task Poll_VoteWithNullOrEmptyVoteString_IsSkipped() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + // An empty `vote` string is meaningless — SP wouldn't render it anywhere — so we + // refuse to import it even when the voter id is otherwise valid. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic blank-vote", + desc = "", + custom = false, + endTime = 0L, + votes = new[] + { + new { id = alpha, vote = "", comment = "synthetic-blank" }, + }, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, _) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var data = polls[0].Data; + await Assert.That(data.GetProperty("responses").GetArrayLength()).IsEqualTo(0); + } + + [Test] + public async Task NormalPoll_RoundTrips() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic abstain vote", + desc = "synthetic desc", + custom = false, + endTime = Date_2021_06_10_End, + allowAbstain = true, + allowVeto = true, + votes = new[] + { + new { id = alpha, vote = "abstain", comment = "synthetic" }, + }, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, _) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var poll = polls[0]; + await Assert.That(poll.Title).IsEqualTo("synthetic abstain vote"); + await Assert.That(poll.Description).IsEqualTo("synthetic desc"); + await Assert.That(poll.Type).IsEqualTo(PollType.Vote); + await Assert.That(poll.TimeEnd).IsNotNull(); + await Assert.That(poll.TimeEnd!.Value) + .IsEqualTo(DateTimeOffset.FromUnixTimeMilliseconds(Date_2021_06_10_End).UtcDateTime); + + var responses = poll.Data.GetProperty("responses"); + await Assert.That(responses.GetArrayLength()).IsEqualTo(1); + await Assert.That(responses[0].GetProperty("vote").GetString()).IsEqualTo("abstain"); + await Assert.That(responses[0].GetProperty("comment").GetString()).IsEqualTo("synthetic"); + // alter_id is the numeric alter id, never the raw SP member uuid. + await Assert.That(responses[0].GetProperty("alter_id").ValueKind).IsEqualTo(JsonValueKind.Number); + // SP's allowVeto flag carries over into the client's allow_veto member. + await Assert.That(poll.Data.GetProperty("allow_veto").GetBoolean()).IsTrue(); + } + + [Test] + public async Task CustomPoll_RoundTrips() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic choice", + desc = "", + custom = true, + endTime = 0L, + options = new object[] + { + new { name = "One", color = "#111111" }, + new { name = "Two", color = "#222222" }, + new { name = "Three", color = "#333333" }, + }, + votes = new[] + { + new { id = alpha, vote = "Two", comment = "synthetic" }, + }, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, _) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var poll = polls[0]; + await Assert.That(poll.Title).IsEqualTo("synthetic choice"); + await Assert.That(poll.Type).IsEqualTo(PollType.Choice); + + var choices = poll.Data.GetProperty("choices"); + await Assert.That(choices.GetArrayLength()).IsEqualTo(3); + await Assert.That(choices[0].GetProperty("name").GetString()).IsEqualTo("One"); + await Assert.That(choices[1].GetProperty("name").GetString()).IsEqualTo("Two"); + await Assert.That(choices[2].GetProperty("name").GetString()).IsEqualTo("Three"); + + // The vote's option name translates to the minted choice id of the "Two" choice. + var responses = poll.Data.GetProperty("responses"); + await Assert.That(responses.GetArrayLength()).IsEqualTo(1); + await Assert.That(responses[0].GetProperty("choice_id").GetString()) + .IsEqualTo(choices[1].GetProperty("id").GetString()); + await Assert.That(responses[0].GetProperty("alter_id").ValueKind).IsEqualTo(JsonValueKind.Number); + } + + [Test] + public async Task Poll_LastOperationTime_PreservedAsInsertedAt() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + // 32-hex GUID, deliberately NOT a 24-hex ObjectId, so SpObjectId.TryDecodeTimestamp + // fails and the importer falls back to lastOperationTime. This test now covers the + // *fallback* branch of the ObjectId-primary cascade. + var spPollId = MemberUuid(); + + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic historical poll", + desc = "", + custom = false, + endTime = 0L, + lastOperationTime = Date_2020_01_15, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + var expected = DateTimeOffset.FromUnixTimeMilliseconds(Date_2020_01_15).UtcDateTime; + await Assert.That(polls[0].InsertedAt).IsEqualTo(expected); + + // Defensive: any accidental UtcNow substitution would land within seconds of "now". + await Assert.That(polls[0].InsertedAt).IsLessThan(DateTime.UtcNow.AddDays(-30)); + + // The fallback path must surface a per-row warning so operators can spot polls + // whose creation date came from lastOperationTime (drifts on edits) rather than + // from a real ObjectId. + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollId, StringComparison.Ordinal) && + r.Message.Contains("non-decodable 24-hex id", StringComparison.OrdinalIgnoreCase) && + r.Message.Contains("falling back to lastOperationTime", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task Poll_ImportedWithObjectIdTimestamp_PreservesDecodedUtc() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // Golden 24-hex ObjectId: 0x671425cd = 1,729,373,645 = 2024-10-19 21:34:05Z. We set + // a deliberately *different* lastOperationTime (Date_2020_01_15) so this test fails + // loudly if a future regression makes the importer prefer lastOperationTime over + // the decoded ObjectId. + const string spPollIdHistorical = "671425cd02c63d13c59ba474"; + var expectedInsertedAt = new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc); + + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollIdHistorical, + content = new + { + name = "synthetic objectid poll", + desc = "", + custom = false, + endTime = 0L, + lastOperationTime = Date_2020_01_15, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + + using (Assert.Multiple()) + { + await Assert.That(polls[0].InsertedAt).IsEqualTo(expectedInsertedAt); + // ObjectId wins over lastOperationTime, so we must NOT land on the 2020 date. + await Assert.That(polls[0].InsertedAt) + .IsNotEqualTo(DateTimeOffset.FromUnixTimeMilliseconds(Date_2020_01_15).UtcDateTime); + // Defensive: an accidental UtcNow stamp would land within seconds of "now". + await Assert.That(polls[0].InsertedAt).IsLessThan(DateTime.UtcNow.AddDays(-30)); + } + + // Happy path: no per-row warning should be emitted when the ObjectId decoded cleanly. + var fallbackWarned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollIdHistorical, StringComparison.Ordinal) && + (r.Message.Contains("falling back", StringComparison.OrdinalIgnoreCase) || + r.Message.Contains("import time", StringComparison.OrdinalIgnoreCase))); + await Assert.That(fallbackWarned).IsFalse(); + } + + [Test] + public async Task Poll_LastOperationTimeMissing_FallsBackToImportTimeAndWarns() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + var spPollId = MemberUuid(); + + // SP either omits `lastOperationTime` or sends 0 on very old rows. We accept those + // but log a warning so we can spot polls that lost their creation timestamp. + var pollsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spPollId, + content = new + { + name = "synthetic missing-lastop poll", + desc = "", + custom = false, + endTime = 0L, + lastOperationTime = 0L, + }, + }, + }); + + var importStarted = DateTime.UtcNow; + var stub = BuildSp(sysId.Value, alpha, pollsJson: pollsJson); + var (_, _, pollRepo, _, _, logger) = await RunImportAsync(stub, systemId); + + var polls = await pollRepo.ListAsync(systemId); + await Assert.That(polls.Count).IsEqualTo(1); + await Assert.That(polls[0].InsertedAt).IsGreaterThanOrEqualTo(importStarted.AddSeconds(-5)); + await Assert.That(polls[0].InsertedAt).IsLessThanOrEqualTo(DateTime.UtcNow.AddSeconds(5)); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spPollId, StringComparison.Ordinal) && + r.Message.Contains("no lastOperationTime", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task CustomField_ImportedWithObjectIdTimestamp_PreservesDecodedUtc() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // The first 4 bytes of a MongoDB ObjectId are a big-endian Unix-seconds timestamp. + // 0x671425cd = 1,729,373,645 = 2024-10-19 21:34:05Z. The remaining 16 hex chars are + // synthetic - SP only uses them as machine/process/counter bytes and we ignore them. + const string spFieldIdHistorical = "671425cd02c63d13c59ba474"; + var expectedInsertedAt = new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc); + + var customFieldsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spFieldIdHistorical, + content = new + { + name = "synthetic-favourite-colour", + type = 0, + supportMarkdown = false, + @private = false, + preventTrusted = false, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, customFieldsJson: customFieldsJson); + var (_, _, _, fieldRepo, _, _) = await RunImportAsync(stub, systemId); + + var fields = await fieldRepo.ListAsync(systemId); + await Assert.That(fields.Count).IsEqualTo(1); + + var field = fields[0]; + using (Assert.Multiple()) + { + await Assert.That(field.Name).IsEqualTo("synthetic-favourite-colour"); + await Assert.That(field.InsertedAt).IsEqualTo(expectedInsertedAt); + await Assert.That(field.InsertedAt).IsNotNull(); + // Defensive: an accidental UtcNow stamp would land within seconds of "now". + await Assert.That(field.InsertedAt!.Value).IsLessThan(DateTime.UtcNow.AddDays(-30)); + } + } + + [Test] + public async Task CustomField_WithNullSupportMarkdown_IsStillImported() + { + // Real-world regression: SP's update300 migration (SimplyPluralApi/src/api/v1/user/updates/update300.ts:187) + // copies legacy field schemas via `insertOne({ ..., supportMarkdown: field.supportMarkdown })` + // where the legacy `field.supportMarkdown` is undefined for pre-markdown rows. Mongo stores + // undefined as null, the SP API serialises `"supportMarkdown":null`, and the importer's + // SpCustomFieldContent.SupportMarkdown used to be a non-nullable bool. Result: the whole + // customFields response failed to deserialise, FetchAsync's catch-all swallowed the + // exception, and the import silently skipped the entire fields step (with alters then + // landing with no Field values because the SP->our-id mapping was empty). + // + // Hand-written JSON here because anonymous types can't emit a JSON null for a bool member. + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + const string spFieldIdHistorical = "671425cd02c63d13c59ba474"; + var expectedInsertedAt = new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc); + + var customFieldsJson = $$""" + [ + { + "exists": true, + "id": "{{spFieldIdHistorical}}", + "content": { + "name": "synthetic-legacy-migrated-field", + "type": 0, + "supportMarkdown": null, + "buckets": [] + } + } + ] + """; + + var stub = BuildSp(sysId.Value, alpha, customFieldsJson: customFieldsJson); + var (_, _, _, fieldRepo, _, _) = await RunImportAsync(stub, systemId); + + var fields = await fieldRepo.ListAsync(systemId); + await Assert.That(fields.Count) + .IsEqualTo(1) + .Because("legacy SP fields with supportMarkdown:null must still be imported - they're the common case on systems migrated via SP's update300 path."); + + // SP's own code defaults missing supportMarkdown to true (SimplyPluralApi/src/api/v2/user.ts:95, + // generateReport.ts:117), so a null on the wire should produce the markdown variant ("text"), + // not the plaintext variant. + await Assert.That(fields[0].Type).IsEqualTo(FieldType.Text); + await Assert.That(fields[0].InsertedAt).IsEqualTo(expectedInsertedAt); + } + + [Test] + public async Task CustomField_WithUnparseableSpId_IsSkippedAndWarned() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // SP always produces valid 24-hex ObjectIds in practice, but if the API ever returns + // anything else we must skip rather than silently stamp UtcNow (which is what the + // pre-fix code did for every field). One per-row warning lets operators trace the skip. + const string spFieldIdGarbage = "not-an-objectid"; + + var customFieldsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spFieldIdGarbage, + content = new + { + name = "synthetic-cannot-decode", + type = 0, + supportMarkdown = false, + @private = false, + preventTrusted = false, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, customFieldsJson: customFieldsJson); + var (_, _, _, fieldRepo, _, logger) = await RunImportAsync(stub, systemId); + + var fields = await fieldRepo.ListAsync(systemId); + await Assert.That(fields.Count).IsEqualTo(0); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spFieldIdGarbage, StringComparison.Ordinal) && + r.Message.Contains("24-hex ObjectId", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task Tag_ImportedWithObjectIdTimestamp_PreservesDecodedUtc() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // Same golden ObjectId we use in the custom-fields test. 0x671425cd = 1,729,373,645 + // = 2024-10-19 21:34:05Z. Tags (SP "groups") inherit the same ObjectId-decoding + // strategy as fields because SP exposes no per-row creation timestamp for groups. + const string spGroupIdHistorical = "671425cd02c63d13c59ba474"; + var expectedInsertedAt = new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc); + + var groupsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spGroupIdHistorical, + content = new + { + name = "synthetic-friends-group", + desc = "", + color = "", + parent = "root", + members = Array.Empty(), + @private = false, + preventTrusted = false, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, groupsJson: groupsJson); + var (_, _, _, _, tagRepo, _) = await RunImportAsync(stub, systemId); + + var tags = await tagRepo.ListAsync(systemId); + await Assert.That(tags.Count).IsEqualTo(1); + + var tag = tags[0]; + using (Assert.Multiple()) + { + await Assert.That(tag.Name).IsEqualTo("synthetic-friends-group"); + await Assert.That(tag.InsertedAt).IsEqualTo(expectedInsertedAt); + // Defensive: an accidental UtcNow stamp would land within seconds of "now". + await Assert.That(tag.InsertedAt).IsLessThan(DateTime.UtcNow.AddDays(-30)); + } + } + + [Test] + public async Task Tag_WithUnparseableSpId_IsSkippedAndWarned() + { + var sysId = Uid(); + var systemId = Uid(); + var alpha = MemberUuid(); + + // SP always produces valid 24-hex ObjectIds for groups in practice, but if the API + // ever returns anything else we must skip rather than silently stamp UtcNow. One + // per-row warning lets operators trace the skip. + const string spGroupIdGarbage = "not-an-objectid"; + + var groupsJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spGroupIdGarbage, + content = new + { + name = "synthetic-cannot-decode-group", + desc = "", + color = "", + parent = "root", + members = Array.Empty(), + @private = false, + preventTrusted = false, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha, groupsJson: groupsJson); + var (_, _, _, _, tagRepo, logger) = await RunImportAsync(stub, systemId); + + var tags = await tagRepo.ListAsync(systemId); + await Assert.That(tags.Count).IsEqualTo(0); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spGroupIdGarbage, StringComparison.Ordinal) && + r.Message.Contains("24-hex ObjectId", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + [Test] + public async Task Alter_NoDate_FallsBackToObjectIdAndWarns() + { + var sysId = Uid(); + var systemId = Uid(); + + // 24-hex MongoDB ObjectId: 0x671425cd = 1,729,373,645 = 2024-10-19 21:34:05Z. + // The importer used to silently stamp 1970-01-01 whenever SP returned date == 0. + // We now fall back to the ObjectId-decoded creation second and warn once per row. + // We can't assert the resulting CreatedAt directly (AlterReadModel doesn't expose + // it), but the warning fired is a sufficient witness that the new cascade took + // the ObjectId branch. + const string spMemberIdHistorical = "671425cd02c63d13c59ba474"; + + var membersJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spMemberIdHistorical, + content = new + { + name = "synthetic-no-date-alter", + date = 0L, + lastOperationTime = Date_2022_03_05, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha: spMemberIdHistorical, membersJson: membersJson); + var (_, _, _, _, _, logger) = await RunImportAsync(stub, systemId); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spMemberIdHistorical, StringComparison.Ordinal) && + r.Message.Contains("no date", StringComparison.OrdinalIgnoreCase) && + r.Message.Contains("ObjectId-decoded", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + + // The UtcNow-fallback warning must NOT also fire: ObjectId decode succeeded so we + // shouldn't have walked further down the cascade. + var utcNowWarned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spMemberIdHistorical, StringComparison.Ordinal) && + r.Message.Contains("import time as created date", StringComparison.OrdinalIgnoreCase)); + await Assert.That(utcNowWarned).IsFalse(); + } + + [Test] + public async Task Alter_NoDateAndNonDecodableId_FallsBackToUtcNowAndWarns() + { + var sysId = Uid(); + var systemId = Uid(); + // 32-hex GUID, deliberately not a 24-hex ObjectId, so SpObjectId.TryDecodeTimestamp + // fails and we walk to the terminal UtcNow + warn branch. Alters can't be skipped + // (downstream associations depend on them), so the terminal branch must still + // create the row. + var spMemberIdGarbage = MemberUuid(); + + var membersJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = spMemberIdGarbage, + content = new + { + name = "synthetic-garbage-id-alter", + date = 0L, + lastOperationTime = Date_2022_03_05, + }, + }, + }); + + var stub = BuildSp(sysId.Value, alpha: spMemberIdGarbage, membersJson: membersJson); + var (_, _, _, _, _, logger) = await RunImportAsync(stub, systemId); + + var warned = logger.Records.Any(r => + r.Level == LogLevel.Warning && + r.Message.Contains(spMemberIdGarbage, StringComparison.Ordinal) && + r.Message.Contains("no date and non-decodable id", StringComparison.OrdinalIgnoreCase) && + r.Message.Contains("import time as created date", StringComparison.OrdinalIgnoreCase)); + await Assert.That(warned).IsTrue(); + } + + /// + /// Builds the canonical "minimal valid SP backend" stub. The single SP member + /// is wired up so the import creates exactly one alter, which is the only association + /// front-related assertions care about. Override the front-related blobs per test. + /// + private static StubSpHandler BuildSp( + string sysId, + string alpha, + string? frontHistoryJson = null, + string? currentFrontersJson = null, + string? pollsJson = null, + string? customFieldsJson = null, + string? groupsJson = null, + string? membersJson = null) + { + var meJson = JsonSerializer.Serialize(new + { + exists = true, + id = sysId, + content = new { desc = "synthetic test system" }, + }); + + var defaultMembersJson = JsonSerializer.Serialize(new[] + { + new + { + exists = true, + id = alpha, + content = new + { + name = "alpha", + date = Date_2022_03_05, + lastOperationTime = Date_2022_03_05, + }, + }, + }); + + return new StubSpHandler() + .OnGet("/v1/me", meJson) + .OnGet($"/v1/customFields/{sysId}", customFieldsJson ?? "[]") + .OnGet($"/v1/members/{sysId}", membersJson ?? defaultMembersJson) + .OnGet($"/v1/customFronts/{sysId}", "[]") + .OnGet($"/v1/groups/{sysId}", groupsJson ?? "[]") + .OnGetPrefix($"/v1/frontHistory/{sysId}", frontHistoryJson ?? "[]") + .OnGet("/v1/fronters/", currentFrontersJson ?? "[]") + .OnGet($"/v1/polls/{sysId}", pollsJson ?? "[]"); + } + + private static async Task<(ImportJobOutcome Result, IFrontingRepository FrontingRepo, IPollRepository PollRepo, ISettingsFieldRepository FieldRepo, ITagRepository TagRepo, CapturingLogger Logger)> RunImportAsync( + StubSpHandler stub, + SystemId systemId) + { + var services = new ServiceCollection(); + services.AddSingleton(new ConfigurationManager()); + + var logger = new CapturingLogger(); + services.AddSingleton(new CapturingLoggerFactory(logger)); + services.AddLogging(); + + InMemoryServiceCollectionExtensions.Register(); + services.AddInterfoldPersistence(PersistenceMode.InMemory, cfg => + { + cfg.ScyllaKeyspace = ScyllaKeyspace.Nam; + }); + services.AddInterfoldDomainHandlers(); + + // InMemoryAlterRepository takes IAlterFieldDefinitions via ctor injection; the + // adapter's real home is AddSettingsModule but this fixture builds its own graph + // (see the -Core rationale below), so wire it explicitly — same pattern as + // FCMServiceFactoryFixture.CreateScope. + services.AddSingleton(); + + services.AddOptions(); + services.AddSingleton(); + // SimplyPluralImportService takes TimeProvider via ctor injection (R2-C15). + services.AddSingleton(TimeProvider.System); + + services.AddHttpClient(HttpClientNames.SimplyPlural) + .ConfigurePrimaryHttpMessageHandler(() => stub); + + // -Core overload skips the async IImportJobRunner queue consumer that the full + // AddSimplyPluralImport extension registers (Program.cs uses that). Tests drive + // ImportAsync directly so the runner would be dead weight. + services.AddSimplyPluralImportCore(); + + await using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + + var importService = scope.ServiceProvider.GetRequiredService(); + importService.WaitForAvatars = true; + + // encryptionKey is null → ImportAsync skips ValidateEncryptionKeyAsync; notes are skipped too. + // spToken is a synthetic placeholder — StubSpHandler doesn't check it. + var result = await importService.ImportAsync(systemId, new ImportToken("synthetic-token"), encryptionKey: null); + + var frontingRepo = scope.ServiceProvider.GetRequiredService(); + var pollRepo = scope.ServiceProvider.GetRequiredService(); + var fieldRepo = scope.ServiceProvider.GetRequiredService(); + var tagRepo = scope.ServiceProvider.GetRequiredService(); + return (result, frontingRepo, pollRepo, fieldRepo, tagRepo, logger); + } + + private sealed class NullAvatarStorage : IAvatarStorage + { + public Task SaveSystemAvatarAsync(SystemId systemId, Stream stream, CancellationToken cancellationToken = default) + => Task.FromResult(new AvatarUrl(string.Empty)); + + public Task SaveAlterAvatarAsync(SystemId systemId, AlterId alterId, Stream stream, CancellationToken cancellationToken = default) + => Task.FromResult(new AvatarUrl(string.Empty)); + + public Task DeleteByUrlAsync(AvatarUrl? avatarUrl, CancellationToken cancellationToken = default) + => Task.FromResult(false); + } + + /// + /// Minimal in-tree FakeLogger replacement. Avoids pulling in + /// Microsoft.Extensions.Diagnostics.Testing just for one assertion. Captures the + /// formatted message and level so tests can grep for the specific warning emitted by + /// the skip+warn branch in ImportFrontsAsync. + /// + private sealed record CapturedLog(LogLevel Level, string Message, string? Category); + + private sealed class CapturingLogger : ILogger + { + public List Records { get; } = new(); + public string? Category { get; set; } + + public IDisposable BeginScope(TState state) where TState : notnull => NullScope.Instance; + public bool IsEnabled(LogLevel logLevel) => true; + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, + Func formatter) + { + lock (Records) + { + Records.Add(new CapturedLog(logLevel, formatter(state, exception), Category)); + } + } + + private sealed class NullScope : IDisposable + { + public static readonly NullScope Instance = new(); + public void Dispose() { } + } + } + + private sealed class CapturingLoggerFactory : ILoggerFactory + { + private readonly CapturingLogger _logger; + public CapturingLoggerFactory(CapturingLogger logger) => _logger = logger; + + public void AddProvider(ILoggerProvider provider) { } + + public ILogger CreateLogger(string categoryName) + { + _logger.Category ??= categoryName; + return _logger; + } + + public void Dispose() { } + } +} diff --git a/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpObjectIdTests.cs b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpObjectIdTests.cs new file mode 100644 index 00000000..6c452c5e --- /dev/null +++ b/tests/integration/Interfold.Settings.IntegrationTests/SimplyPluralImport/SpObjectIdTests.cs @@ -0,0 +1,60 @@ +using Interfold.IntegrationTests.Shared; +using Interfold.Settings.Api.Services.SimplyPlural; + +namespace Interfold.Settings.IntegrationTests.SimplyPluralImport; + +/// +/// Unit coverage for . SP gives the importer no +/// `lastOperationTime` (or any time field) on custom fields - the only created-date signal +/// is the MongoDB ObjectId's first 4 bytes (big-endian Unix seconds). If we misdecode that we +/// either stamp the wrong date or accidentally accept garbage ids, so this nails down the +/// happy path plus the obvious failure modes. All inputs are synthetic. +/// +public sealed class SpObjectIdTests : BaseEndpointTest +{ + // 0x671425cd = 1,729,373,645 seconds since Unix epoch = 2024-10-19 21:34:05Z. + // The remaining 16 hex chars are arbitrary - they're machine/process/counter bytes + // that we don't care about for the timestamp decode. + [Test] + public async Task TryDecode_GoldenVector_ReturnsExpectedUtc() + { + var ok = SpObjectId.TryDecodeTimestamp("671425cd02c63d13c59ba474", out var utc); + + using (Assert.Multiple()) + { + await Assert.That(ok).IsTrue(); + await Assert.That(utc).IsEqualTo(new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc)); + } + } + + [Test] + public async Task TryDecode_AcceptsUppercaseHex() + { + var ok = SpObjectId.TryDecodeTimestamp("671425CD02C63D13C59BA474", out var utc); + + using (Assert.Multiple()) + { + await Assert.That(ok).IsTrue(); + await Assert.That(utc).IsEqualTo(new DateTime(2024, 10, 19, 21, 34, 5, DateTimeKind.Utc)); + } + } + + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments("not-an-objectid")] + [Arguments("671425cd02c63d13c59ba47")] // 23 chars + [Arguments("671425cd02c63d13c59ba4744")] // 25 chars + [Arguments("zz1425cd02c63d13c59ba474")] // non-hex in the timestamp prefix + [Arguments("671425cd02c63d13c59ba47z")] // non-hex in the trailing bytes + public async Task TryDecode_InvalidInputs_ReturnFalseAndDefaultUtc(string? input) + { + var ok = SpObjectId.TryDecodeTimestamp(input, out var utc); + + using (Assert.Multiple()) + { + await Assert.That(ok).IsFalse(); + await Assert.That(utc).IsEqualTo(default(DateTime)); + } + } +} diff --git a/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketTests.cs b/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketTests.cs new file mode 100644 index 00000000..382f2b4c --- /dev/null +++ b/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketTests.cs @@ -0,0 +1,768 @@ +using System.Collections.Concurrent; +using System.Net.WebSockets; +using System.Text.Json; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Settings.Contracts.Events; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Api.Models; +using Interfold.Socket.Contracts; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.TestHost; + +namespace Interfold.Socket.IntegrationTests.Endpoints; + +// 5 minute timeout since we want to ensure this does end up timing out if a connection gets stuck but we also +// need to account for Cassandra's slower performance with bootstrapping. +[Timeout(1000 * 300)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class WebSocketTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + internal static string UniqueId(string prefix) => TestIds.NewSystemId(prefix, maxLen: int.MaxValue); + + // Events carry ScopedSystemId, so tests that publish directly onto the bus compose + // one from the raw test id. NAM is the only region the test bootstrapper seeds; + // compose is idempotent, so callers that already pass "nam:..." (or another valid + // region prefix) keep the same wire bytes. + internal static ScopedSystemId AsScopedSystemId(string rawSystemId) + => ScopedSystemId.Compose(ScyllaKeyspace.Nam, rawSystemId); + + [Test] + public async Task Api_UserSocketEndpoint_AllowsWebSocketUpgrade(CancellationToken token) + { + var server = fixture.Factory.Server; + var client = server.CreateWebSocketClient(); + + var systemId = UniqueId("sys-phx-join"); + string socketToken = await CreateRandomToken(fixture.Factory, systemId); + var uri = new Uri(WebSocketBasePath(server), "/api/socket/websocket?token=" + socketToken); + + using var ws = await client.ConnectAsync(uri, token); + + await Assert.That(ws.State).IsEqualTo(WebSocketState.Open).Because($"Expected websocket to be open after connecting to /api/socket/weboscket, got {ws.State}."); + + // Raw dictionary payload (not the typed PhxJoinPayload): pins the server's tolerance + // for unknown platform spellings ("wasm") which the typed record can no longer emit. + var arrayJoinFrame = PhxArrayFrame.CreateBytes( + "51", "51", $"system:{systemId}", "phx_join", + new Dictionary + { + ["token"] = socketToken, + ["protocolVersion"] = "2.0.0", + ["platform"] = "wasm", + ["isReconnect"] = true, + }); + + await ws.SendTextFrameAsync(arrayJoinFrame, token); + + var frame = await ReceivedPhxFrame.ReceiveAsync(ws, token); + + using (Assert.Multiple()) + { + await Assert.That(frame.JoinRef).IsEqualTo("51").Because("Expected join_ref=51 in array reply."); + await Assert.That(frame.Ref).IsEqualTo("51").Because("Expected ref=51 in array reply."); + await Assert.That(frame.Topic).IsEqualTo($"system:{systemId}").Because("Expected topic to match array join topic."); + await Assert.That(frame.Event).IsEqualTo("phx_reply").Because("Expected array reply event phx_reply."); + var reply = frame.Reply(); + await Assert.That(reply.Status).IsEqualTo(PhoenixReplyStatus.Ok).Because("Expected status=ok for reconnect join."); + await Assert.That(reply.Response.System.Id).IsEqualTo(new SystemId(systemId)).Because("Expected system ID in reconnect payload."); + } + } + + [Test] + public async Task Api_UserSocketEndpoint_RejectsUnsupportedProtocolVersion(CancellationToken token) + { + var systemId = UniqueId("sys-phx-unsupported"); + var wsClient = fixture.Factory.Server.CreateWebSocketClient(); + string socketToken = await CreateRandomToken(fixture.Factory, systemId); + var uri = new Uri(WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={socketToken}"); + using var ws = await wsClient.ConnectAsync(uri, token); + + var joinFrame = new PhxFrame + { + Topic = $"system:{systemId}", + Event = "phx_join", + Payload = new PhxJoinPayload { Token = new SocketToken(socketToken), ProtocolVersion = "not-a-version" }, + Ref = "1", + JoinRef = "1" + }.ToBytes(); + + await ws.SendTextFrameAsync(joinFrame, token); + var reply = await ReceivedPhxFrame.ReceiveReplyAsync(ws, token); + + using (Assert.Multiple()) + { + await Assert.That(reply.Status).IsEqualTo(PhoenixReplyStatus.Error).Because("Expected status=error for unsupported protocol version."); + await Assert.That(reply.Response.Reason).IsEqualTo(ErrorCodes.SocketReasons.UnsupportedProtocolVersion).Because("Expected reason=unsupported_protocol_version."); + } + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_BatchesForIos_WhenThresholdExceeded(CancellationToken token) + { + var systemId = UniqueId("sys-phx-ios-batch"); + var wsClient = fixture.Factory.Server.CreateWebSocketClient(); + string socketToken = await CreateRandomToken(fixture.Factory, systemId); + var uri = new Uri(WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={socketToken}"); + using var ws = await wsClient.ConnectAsync(uri, token); + + var joinFrame = new PhxFrame + { + Topic = $"system:{systemId}", + Event = "phx_join", + Payload = new PhxJoinPayload { Token = new SocketToken(socketToken), Platform = ClientPlatform.Ios, ProtocolVersion = "2.0.0", ForceBatch = true }, + Ref = "1", + JoinRef = "1" + }.ToBytes(); + + await ws.SendTextFrameAsync(joinFrame, token); + var reply = await ReceivedPhxFrame.ReceiveReplyAsync(ws, token); + + using (Assert.Multiple()) + { + await Assert.That(reply.Status).IsEqualTo(PhoenixReplyStatus.Ok).Because("Expected status=ok for iOS batched join."); + await Assert.That(reply.Response.Batched).IsTrue().Because("Expected batched=true for iOS join above threshold."); + } + + var batchedComplete = await ws.ReceiveEventFrameAsync( token, SocketEventNames.BatchedInit.Complete, maxFrames: 6); + await Assert.That(batchedComplete).IsNotNull().Because("Expected batched_init_complete after iOS batched join."); + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_RateLimitsThirdJoinWithinOneSecond(CancellationToken token) + { + var systemId = UniqueId("sys-rate-limit"); + var wsClient = fixture.Factory.Server.CreateWebSocketClient(); + string socketToken = await CreateRandomToken(fixture.Factory, systemId); + var uri = new Uri(WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={socketToken}"); + using var ws = await wsClient.ConnectAsync(uri, token); + + var firstReply = await SendJoinAndReceiveReplyAsync(ws, socketToken, systemId, "1", token); + var secondReply = await SendJoinAndReceiveReplyAsync(ws, socketToken, systemId, "2", token); + var thirdReply = await SendJoinAndReceiveReplyAsync(ws, socketToken, systemId, "3", token); + + using (Assert.Multiple()) + { + await Assert.That(firstReply.Status).IsEqualTo(PhoenixReplyStatus.Ok).Because("Expected first join to be accepted."); + await Assert.That(secondReply.Status).IsEqualTo(PhoenixReplyStatus.Ok).Because("Expected second join to be accepted."); + await Assert.That(thirdReply.Status).IsEqualTo(PhoenixReplyStatus.Error).Because("Expected third join to be rate-limited."); + } + + var thirdResponse = JsonSerializer.Deserialize( + JsonSerializer.Serialize(thirdReply.Response, SocketJson.Options), SocketJson.Options); + await Assert.That(thirdResponse!.Reason).IsEqualTo(ErrorCodes.SocketReasons.RateLimited).Because("Expected reason=rate_limited on third join."); + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFrontingChangedEvent_AfterFrontStart(CancellationToken token) + { + var systemId = UniqueId("sys-front-push"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + var createAlterFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/alters", new { name = "FrontPushAlter" }, "2"); + + var (createAlterReply, createAlterPush) = await ws.SendEndpointAndCaptureAsync(createAlterFrame, SocketEventNames.Alters.Created, token); + + await Assert.That(createAlterReply).IsNotNull().Because("Expected endpoint ack (phx_reply) for alter create call."); + + var createdAlterId = createAlterPush is not null + ? createAlterPush.RawPayload!.Value.GetProperty("alter").GetProperty("id").GetInt32() + : ExtractAlterIdFromEndpointReply(createAlterReply!); + + var startFrontFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/front/start", new { id = createdAlterId }, "3"); + + var (endpointAck, frontingPush) = await ws.SendEndpointAndCaptureAsync(startFrontFrame, SocketEventNames.Fronting.Started, token); + + using (Assert.Multiple()) + { + await Assert.That(endpointAck).IsNotNull().Because("Expected endpoint ack (phx_reply) for front start call."); + } + + if (frontingPush is not null) + { + await Assert.That(frontingPush.RawPayload?.TryGetProperty("front", out _) ?? false) + .IsTrue().Because("Expected fronting push payload to include front object."); + } + + await ws.CloseTestDoneAsync(token); + } + + // Flaky under a 12-project cold-boot fanout: the domain-fanout push + // (alter_created) races the phx_reply on the same socket, and + // ReceiveReplyAndPushAsync's 6-frame / 30 s-per-frame window occasionally + // times out on the push half before it arrives. Two retries are enough to + // ride out the contention spike; the ack path itself is solid. + [Test] + [Retry(2)] + public async Task Api_UserSocketEndpoint_PushesAlterTagAndFieldsEvents_AfterEndpointWrites(CancellationToken token) + { + var systemId = UniqueId("sys-domain-fanout"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + var createAlterFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/alters", new { name = "DomainFanoutAlter" }, "2"); + + var (alterReply, alterPush) = await ws.SendEndpointAndCaptureAsync( createAlterFrame, SocketEventNames.Alters.Created, token); + using (Assert.Multiple()) + { + await Assert.That(alterReply).IsNotNull().Because("Expected endpoint ack for alter create."); + await Assert.That(alterPush).IsNotNull().Because("Expected alter_created push after alter create."); + } + await Assert.That(alterPush!.RawPayload?.GetProperty("alter").GetProperty("name").GetString()) + .IsEqualTo("DomainFanoutAlter").Because("Expected alter name in push payload."); + + var createTagFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/tags", new { name = "DomainFanoutTag" }, "3"); + + var (tagReply, tagPush) = await ws.SendEndpointAndCaptureAsync( createTagFrame, SocketEventNames.Tags.Created, token); + using (Assert.Multiple()) + { + await Assert.That(tagReply).IsNotNull().Because("Expected endpoint ack for tag create."); + await Assert.That(tagPush).IsNotNull().Because("Expected tag_created push after tag create."); + } + await Assert.That(tagPush!.RawPayload?.GetProperty("tag").GetProperty("name").GetString()) + .IsEqualTo("DomainFanoutTag").Because("Expected tag name in push payload."); + + var createFieldFrame = PhxEndpointFrame.Build(topic, "POST", "/api/settings/fields", new { name = "DomainFanoutField", type = "text", security_level = "private", locked = false }, "4"); + + var (fieldReply, fieldPush) = await ws.SendEndpointAndCaptureAsync( createFieldFrame, SocketEventNames.Settings.FieldsUpdated, token); + using (Assert.Multiple()) + { + await Assert.That(fieldReply).IsNotNull().Because("Expected endpoint ack for field create."); + await Assert.That(fieldPush).IsNotNull().Because("Expected fields_updated push after field create."); + } + await Assert.That(fieldPush!.RawPayload!.Value.GetProperty("fields").GetArrayLength()) + .IsGreaterThan(0).Because("Expected at least one field in fields_updated payload."); + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendRequestReceived_ToRecipientSystem(CancellationToken token) + { + var senderSystemId = UniqueId("sys-friend-sender"); + var recipientSystemId = UniqueId("sys-friend-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + var (senderAck, senderPush, recipientFrame) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + + using (Assert.Multiple()) + { + await Assert.That(senderAck).IsNotNull().Because("Expected endpoint ack on sender socket for friend request send."); + await Assert.That(senderPush).IsNotNull().Because("Expected friend_request_sent push on sender socket."); + } + + await Assert.That(recipientFrame).IsNotNull().Because("Expected recipient-side friend_request_received push."); + + await Assert.That(recipientFrame!.RawPayload?.TryGetProperty("system", out _) ?? false) + .IsTrue().Because("Expected system profile in friend_request_received payload."); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendRequestAccepted_ToActorAndRecipient(CancellationToken token) + { + var senderSystemId = UniqueId("sys-accept-sender"); + var recipientSystemId = UniqueId("sys-accept-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + var (_, _, recipientEvent) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + await Assert.That(recipientEvent).IsNotNull().Because("Expected friend_request_received"); + + var acceptFrame = PhxEndpointFrame.Build("system:" + recipientSystemId, "POST", "/api/friend-requests/" + senderSystemId + "/accept", new object(), "3"); + + var (recipientAck, recipientFriendAdded) = await recipientWs.SendEndpointAndCaptureAsync(acceptFrame, SocketEventNames.Friendships.Added, token); + + using (Assert.Multiple()) + { + await Assert.That(recipientAck).IsNotNull().Because("Expected endpoint ack on recipient socket for accept."); + await Assert.That(recipientFriendAdded).IsNotNull().Because("Expected friend_added push on recipient socket after accept."); + } + + var (senderFriendAdded, senderRequestCleared) = await ReceivedPhxFrame.ReceiveTwoOfAsync( + senderWs, SocketEventNames.Friendships.Added, SocketEventNames.Friendships.RequestRemoved, + TimeSpan.FromSeconds(5), 5, token); + + using (Assert.Multiple()) + { + await Assert.That(senderFriendAdded).IsNotNull().Because("Expected friend_added push on sender socket after accept."); + await Assert.That(senderRequestCleared).IsNotNull().Because("Expected friend_request_removed push on sender socket after accept (outgoing request cleanup)."); + } + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendRequestRejected_ToActorAndRecipient(CancellationToken token) + { + var senderSystemId = UniqueId("sys-reject-sender"); + var recipientSystemId = UniqueId("sys-reject-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + var (_, _, recipientEvent) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + await Assert.That(recipientEvent).IsNotNull().Because("Expected friend_request_received"); + + var rejectFrame = PhxEndpointFrame.Build("system:" + recipientSystemId, "DELETE", "/api/friend-requests/" + senderSystemId, new object(), "3"); + + var (recipientAck, recipientRemoved) = await recipientWs.SendEndpointAndCaptureAsync(rejectFrame, SocketEventNames.Friendships.RequestRemoved, token); + + using (Assert.Multiple()) + { + await Assert.That(recipientAck).IsNotNull().Because("Expected endpoint ack on recipient socket for reject."); + } + + var senderRemoved = await senderWs.ReceiveEventFrameAsync( token, SocketEventNames.Friendships.RequestRemoved, maxFrames: 8); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendRequestCancelled_ToActorAndRecipient(CancellationToken token) + { + var senderSystemId = UniqueId("sys-cancel-sender"); + var recipientSystemId = UniqueId("sys-cancel-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + var (_, _, recipientReceived) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + await Assert.That(recipientReceived).IsNotNull().Because("Expected friend_request_received before cancel."); + + var cancelFrame = PhxEndpointFrame.Build("system:" + senderSystemId, "DELETE", "/api/friend-requests/" + recipientSystemId, new object(), "3"); + + var (senderAck, senderRemoved) = await senderWs.SendEndpointAndCaptureAsync(cancelFrame, SocketEventNames.Friendships.RequestRemoved, token); + + using (Assert.Multiple()) + { + await Assert.That(senderAck).IsNotNull().Because("Expected endpoint ack on sender socket for cancel."); + await Assert.That(senderRemoved).IsNotNull().Because("Expected friend_request_removed push on sender socket after cancel."); + } + + var recipientRemoved = await recipientWs.ReceiveEventFrameAsync( token, SocketEventNames.Friendships.RequestRemoved, maxFrames: 3, perFrameTimeoutSeconds: 5); + await Assert.That(recipientRemoved).IsNotNull().Because("Expected recipient-side friend_request_removed push."); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendRemoved_ToActorAndRecipient(CancellationToken token) + { + var senderSystemId = UniqueId("sys-remove-sender"); + var recipientSystemId = UniqueId("sys-remove-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + // Send friend request + var (_, _, recipientReceived) = await FriendRequestFlow.SendAndDrainAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, "2", token); + await Assert.That(recipientReceived).IsNotNull().Because("Expected friend_request_received before accept."); + + // Accept friend request + var acceptFrame = PhxEndpointFrame.Build("system:" + recipientSystemId, "POST", "/api/friend-requests/" + senderSystemId + "/accept", new object(), "3"); + + var (recipientAcceptAck, recipientAdded) = await recipientWs.SendEndpointAndCaptureAsync(acceptFrame, SocketEventNames.Friendships.Added, token); + + using (Assert.Multiple()) + { + await Assert.That(recipientAcceptAck).IsNotNull().Because("Expected endpoint ack on recipient socket for accept."); + await Assert.That(recipientAdded).IsNotNull().Because("Expected friend_added push on recipient socket after accept."); + } + + // Drain sender-side accept events + _ = await ReceivedPhxFrame.ReceiveReplyAndPushAsync(senderWs, token, SocketEventNames.Friendships.Added); + + // Remove friend + var removeFrame = PhxEndpointFrame.Build("system:" + senderSystemId, "DELETE", "/api/friends/" + recipientSystemId, new object(), "4"); + + var (senderRemoveAck, senderRemoved) = await senderWs.SendEndpointAndCaptureAsync(removeFrame, SocketEventNames.Friendships.Removed, token); + + using (Assert.Multiple()) + { + await Assert.That(senderRemoveAck).IsNotNull().Because("Expected endpoint ack on sender socket for remove."); + await Assert.That(senderRemoved).IsNotNull().Because("Expected friend_removed push on sender socket after remove."); + } + + if (senderRemoved is not null) + { + await Assert.That(senderRemoved.Event).IsEqualTo(SocketEventNames.Friendships.Removed) + .Because("Expected friend_removed event on sender socket."); + } + + var recipientRemovedFrame = await recipientWs.ReceiveEventFrameAsync( token, SocketEventNames.Friendships.Removed, maxFrames: 8); + await Assert.That(recipientRemovedFrame).IsNotNull().Because("Timed out waiting for recipient-side friend_removed push."); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendTrustedAndUntrusted_ToActor(CancellationToken token) + { + var senderSystemId = UniqueId("sys-trust-sender"); + var recipientSystemId = UniqueId("sys-trust-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + await FriendTrustUntrustFlow.RunAsync(senderWs, senderSystemId, recipientWs, recipientSystemId, token); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendAdded_OnMutualFriendRequest(CancellationToken token) + { + var systemAId = UniqueId("sys-mutual-a"); + var systemBId = UniqueId("sys-mutual-b"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, systemAId, systemBId, token); + using var wsA = pair.FirstWs; + using var wsB = pair.SecondWs; + + // A sends friend request to B + var sendAFrame = PhxEndpointFrame.Build("system:" + systemAId, "PUT", "/api/friend-requests/" + systemBId, new object(), "2"); + + _ = await wsA.SendEndpointAndCaptureAsync(sendAFrame, SocketEventNames.Friendships.RequestSent, token); + _ = await wsB.ReceiveEventFrameAsync( token, SocketEventNames.Friendships.RequestReceived, maxFrames: 3); + + // B sends mutual friend request to A (should auto-accept) + var sendBFrame = PhxEndpointFrame.Build("system:" + systemBId, "PUT", "/api/friend-requests/" + systemAId, new object(), "3"); + + await wsB.SendTextFrameAsync(sendBFrame, token); + + var (bAck, bFriendAdded) = await ReceivedPhxFrame.ReceiveTwoOfAsync( + wsB, "phx_reply", SocketEventNames.Friendships.Added, + TimeSpan.FromSeconds(2), 5, token); + + using (Assert.Multiple()) + { + await Assert.That(bAck).IsNotNull().Because("Expected endpoint ack on B socket for mutual send."); + await Assert.That(bFriendAdded).IsNotNull().Because("Expected friend_added push on B socket after mutual send auto-accept."); + } + + var (aFriendAdded, aRequestCleared) = await ReceivedPhxFrame.ReceiveTwoOfAsync( + wsA, SocketEventNames.Friendships.Added, SocketEventNames.Friendships.RequestRemoved, + TimeSpan.FromSeconds(2), 5, token); + + using (Assert.Multiple()) + { + await Assert.That(aFriendAdded).IsNotNull().Because("Expected friend_added push on A socket after mutual send auto-accept."); + await Assert.That(aRequestCleared).IsNotNull().Because("Expected friend_request_removed push on A socket after mutual send auto-accept (outgoing request cleanup)."); + } + + await WebSocketExtensions.CloseTestDoneAsync(wsA, wsB, token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesTagsWiped_AfterWipeTagsEndpoint(CancellationToken token) + { + var systemId = UniqueId("sys-tags-wiped"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + // Seed a tag so the wipe path actually iterates the repository — exercises the cascade + // path Octocon.Accounts.wipe_tags/1 used to cover in the legacy worker. + var createTagFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/tags", new { name = "WipeMe" }, "2"); + + _ = await ws.SendEndpointAndCaptureAsync( createTagFrame, SocketEventNames.Tags.Created, token); + + var wipeFrame = PhxEndpointFrame.Build(topic, "POST", "/api/settings/wipe-tags", new object(), "3"); + + var (wipeAck, wipePush) = await ws.SendEndpointAndCaptureAsync( wipeFrame, SocketEventNames.Settings.TagsWiped, token); + + using (Assert.Multiple()) + { + await Assert.That(wipeAck).IsNotNull().Because("Expected endpoint ack for wipe-tags call."); + await Assert.That(wipePush).IsNotNull().Because("Expected tags_wiped push after POST /api/settings/wipe-tags."); + } + + if (wipePush is not null) + { + await Assert.That(wipePush.Event).IsEqualTo(SocketEventNames.Settings.TagsWiped) + .Because("Expected event name on wipe push to be tags_wiped."); + } + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesGoogleAccountUnlinked_AfterUnlinkEmailEndpoint(CancellationToken token) + { + var systemId = UniqueId("sys-google-unlink"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + var unlinkFrame = PhxEndpointFrame.Build(topic, "POST", "/api/settings/unlink_email", new object(), "2"); + + var (ack, push) = await ws.SendEndpointAndCaptureAsync( unlinkFrame, SocketEventNames.Settings.GoogleAccountUnlinked, token); + + using (Assert.Multiple()) + { + await Assert.That(ack).IsNotNull().Because("Expected endpoint ack for unlink_email call."); + await Assert.That(push).IsNotNull().Because("Expected google_account_unlinked push after POST /api/settings/unlink_email (legacy contract for the email auth path)."); + } + + if (push is not null) + { + await Assert.That(push.Event).IsEqualTo(SocketEventNames.Settings.GoogleAccountUnlinked) + .Because("Expected event name on unlink push to be google_account_unlinked."); + } + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesSpImportLifecycleEvents_OnEventBusPublish(CancellationToken token) + { + // The Simply Plural import service hits the live Apparyllis API, so we drive the socket + // pump from the bus instead. Both lifecycle events are wired through SocketEventPumpRunner; + // this test covers the projection contract (event name + alter_count payload) without + // depending on outbound HTTP. The matching publish-from-handler paths are exercised by + // ImportSpCommandHandler unit tests where the import service is stubbed. + var systemId = UniqueId("sys-sp-import"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + await fixture.Factory.EventBus.PublishAsync(new SimplyPluralImportCompletedEvent(AsScopedSystemId(systemId), 7), token); + + var completeFrame = await ws.ReceiveEventFrameAsync( token, SocketEventNames.Imports.SpComplete, maxFrames: 4); + await Assert.That(completeFrame).IsNotNull().Because("Expected sp_import_complete push after bus publish."); + await Assert.That(completeFrame!.RawPayload?.GetProperty("alter_count").GetInt32() ?? -1) + .IsEqualTo(7).Because("Expected alter_count=7 in sp_import_complete payload (legacy contract is snake_case)."); + + await fixture.Factory.EventBus.PublishAsync(new SimplyPluralImportFailedEvent(AsScopedSystemId(systemId)), token); + + var failedFrame = await ws.ReceiveEventFrameAsync( token, SocketEventNames.Imports.SpFailed, maxFrames: 4); + await Assert.That(failedFrame).IsNotNull().Because("Expected sp_import_failed push after bus publish."); + + await ws.CloseTestDoneAsync(token); + } + + [Test] + public async Task Api_UserSocketEndpoint_PushesPkImportLifecycleEvents_OnEventBusPublish(CancellationToken token) + { + // PluralKit import is a TODO in the new stack, so the lifecycle wiring is verified the + // same way as SP: drive the bus directly and assert the projection. The handler-side + // publish path will start firing the same events for free once the importer lands. + var systemId = UniqueId("sys-pk-import"); + var topic = $"system:{systemId}"; + var (rawWs, socketToken) = await WebSocketHarness.ConnectAndJoinAsync(fixture, systemId, token); + using var ws = rawWs; + + await fixture.Factory.EventBus.PublishAsync(new PluralKitImportCompletedEvent(AsScopedSystemId(systemId), 3), token); + + var completeFrame = await ws.ReceiveEventFrameAsync( token, SocketEventNames.Imports.PkComplete, maxFrames: 4); + await Assert.That(completeFrame).IsNotNull().Because("Expected pk_import_complete push after bus publish."); + await Assert.That(completeFrame!.RawPayload?.GetProperty("alter_count").GetInt32() ?? -1) + .IsEqualTo(3).Because("Expected alter_count=3 in pk_import_complete payload (legacy contract is snake_case)."); + + await fixture.Factory.EventBus.PublishAsync(new PluralKitImportFailedEvent(AsScopedSystemId(systemId)), token); + + var failedFrame = await ws.ReceiveEventFrameAsync( token, SocketEventNames.Imports.PkFailed, maxFrames: 4); + await Assert.That(failedFrame).IsNotNull().Because("Expected pk_import_failed push after bus publish."); + + await ws.CloseTestDoneAsync(token); + } + + /// Regression for the docker-deployment crash: the WebSocket endpoint relay + /// used to build its outbound URL from the inbound Host (operator-facing hostname + + /// host-mapped port), which the container can neither resolve nor reach. Recorder + /// asserts the proxy dials the loopback Kestrel listener, not the operator authority + /// — TestServer routes by path so only URI-recording distinguishes the two. + [Test, NotInParallel("websocket-uri-recorder")] + public async Task Api_UserSocketEndpoint_ProxiesToLocalListener_RegardlessOfInboundHostHeader(CancellationToken token) + { + var systemId = UniqueId("sys-proxy-loopback"); + var topic = $"system:{systemId}"; + var socketToken = await CreateRandomToken(fixture.Factory, systemId); + + var recorder = new ConcurrentQueue(); + fixture.Factory.OutboundHttpUriRecorder = recorder; + try + { + var wsClient = fixture.Factory.Server.CreateWebSocketClient(); + // Simulate the production topology: inbound Host is the operator-facing + // hostname + host-side port; the internal Kestrel port differs (5100/5101 by + // default) and the hostname isn't in the container's DNS. The proxy MUST + // ignore both. + // + // Port must be a valid TCP port because HandleEndpointProxyAsync assigns + // Request.Host.Value to Headers.Host, whose setter validates via + // ParserHelpers.CheckValidHost. The real crash-report values are both valid + // AND the most faithful reproduction of the production scenario. + const string operatorFacingHost = "pineapple.local"; + const int hostSideMappedPort = 5001; + wsClient.ConfigureRequest = request => + { + request.Host = new HostString(operatorFacingHost, hostSideMappedPort); + }; + + var uri = new Uri(WebSocketBasePath(fixture.Factory.Server), $"api/socket/websocket?token={socketToken}"); + using var ws = await wsClient.ConnectAsync(uri, token); + + await WebSocketExtensions.JoinTopicAsync(ws, topic, socketToken, token); + + // Known-good relay target (returns 201) keeps signal on URI composition, not + // controller wiring. The endpoint also fires alter_created which interleaves + // with phx_reply; SendEndpointAndCaptureAsync handles either order. + var endpointFrame = PhxEndpointFrame.Build(topic, "POST", "/api/systems/me/alters", new { name = "LoopbackProbeAlter" }, "2"); + + var (replyFrame, _) = await ws.SendEndpointAndCaptureAsync(endpointFrame, SocketEventNames.Alters.Created, token); + await Assert.That(replyFrame).IsNotNull() + .Because("Expected a phx_reply for the endpoint proxy call."); + var reply = replyFrame!.Reply(); + + using (Assert.Multiple()) + { + // The proxy completing successfully despite the bogus inbound Host is + // the first half of the contract — pre-fix the call would have built a + // non-loopback URL string, which under real Kestrel produces the + // "Name or service not known" SocketException seen in the production + // crash. Under TestServer the authority is cosmetic, so this assertion + // alone is not sufficient — see the URI-recorder assertions below. + await Assert.That(reply.Status).IsEqualTo(PhoenixReplyStatus.Ok) + .Because("Expected the endpoint-proxy phx_reply to be ok; the proxy must succeed regardless of the inbound Host header."); + await Assert.That(reply.Response.Status).IsEqualTo(System.Net.HttpStatusCode.Created) + .Because("Expected the relayed POST /api/systems/me/alters to return 201 Created (mirrors the contract from the existing endpoint-proxy tests above)."); + + // The outbound recorder assertions ARE the regression guard. Pre-fix the + // proxy passed `{Request.Scheme}://{Request.Host}{path}` to HttpClient, + // which the recorder would have captured as containing `pineapple.local` + // and `:5001`. Post-fix the proxy passes the IServerAddressesFeature- + // derived loopback URL (or the TestServer fallback `http://localhost`), + // so neither token appears. + var recorded = recorder.ToArray(); + await Assert.That(recorded.Length).IsGreaterThan(0) + .Because("Expected the endpoint proxy to issue at least one outbound HttpClient call (the relayed POST to /api/systems/me/alters)."); + + // Filter to the relayed path AND our operator-facing Host header. The + // recorder hook is process-wide, so concurrent websocket tests that relay + // the same endpoint through the proxy (with their own default `localhost` + // upgrade host) also land in the queue — matching on this test's unique + // Host value picks out OUR relay deterministically. This keeps the + // regression guard intact: if the proxy stopped forwarding the outer Host, + // no recorded call would carry it and this lookup would come back null. + var operatorFacingHostHeader = $"{operatorFacingHost}:{hostSideMappedPort}"; + var proxyCall = recorded.FirstOrDefault(c => + c.Uri.AbsolutePath.Equals("/api/systems/me/alters", StringComparison.Ordinal) + && string.Equals(c.HostHeader, operatorFacingHostHeader, StringComparison.OrdinalIgnoreCase)); + await Assert.That(proxyCall).IsNotNull() + .Because($"Expected to record an HttpClient call to /api/systems/me/alters carrying the forwarded outer Host header '{operatorFacingHostHeader}' — its absence means the endpoint proxy either never dialed the relay target or stopped forwarding the upgrade's Host. Recorded calls: [{string.Join(", ", recorded.Select(c => $"({c.Uri}, Host={c.HostHeader ?? ""})"))}]."); + + var proxyUri = proxyCall!.Uri; + await Assert.That(proxyUri.Host).IsNotEqualTo(operatorFacingHost) + .Because($"Regression: the proxy is dialing the inbound Host header ({operatorFacingHost}) instead of the local Kestrel listener — this is the exact crash from host/Interfold.Api/Socket/WebSocketHandler.cs:444. Recorded host was '{proxyUri.Host}'."); + await Assert.That(proxyUri.Port).IsNotEqualTo(hostSideMappedPort) + .Because($"Regression: the proxy is dialing the host-side mapped port ({hostSideMappedPort}) instead of the container-internal Kestrel port. Recorded URI was '{proxyUri}'."); + + // The proxy MUST target a loopback host. Under TestServer the helper's + // null/empty-Addresses fallback produces `http://localhost`; under real + // Kestrel the helper rewrites wildcard binds to `127.0.0.1`. Accepting + // either keeps the test stable across both the in-process harness here + // and any future test variant that runs against a real Kestrel. + var isLoopback = proxyUri.Host is "localhost" or "127.0.0.1" or "::1" or "[::1]"; + await Assert.That(isLoopback).IsTrue() + .Because($"Expected the proxy to dial a loopback host (localhost / 127.0.0.1 / ::1). Recorded URI was '{proxyUri}'."); + + // Regression guard for the loopback-origin leak in WebSocketHandler. + // HandleEndpointProxyAsync: before the fix, the proxy left + // `request.Headers.Host` unset on the inner HttpRequestMessage, so under + // real Kestrel the inner controller saw `Request.Host` = the loopback + // dial target (e.g. `127.0.0.1:5101`) and stamped unreachable URLs in + // phx_reply payloads (avatar URLs, OAuth callbacks, etc.). The fix sets + // the Host header on the inner self-call to the outer upgrade's host. + // + // We assert on the OUTBOUND `HttpRequestMessage.Headers.Host` (what the + // production code intentionally set) rather than the inner + // `HttpContext.Request.Host` because the latter depends on TestHost's + // ClientHandler propagating the Host header into the in-process + // HttpContext — that propagation is not reliable across platforms (.NET + // 10.0.9 on Linux observed to drop it). Real Kestrel always honors the + // Host header from the HTTP wire as a matter of HTTP standard, so the + // outbound assertion is sufficient for production correctness. + await Assert.That(proxyCall!.HostHeader).IsEqualTo(operatorFacingHostHeader) + .Because($"Regression: the proxy must forward the OUTER upgrade's Host header ('{operatorFacingHostHeader}') onto the inner HttpRequestMessage so the inner pipeline observes the operator-facing origin, not the loopback dial target. Recorded Host header was '{proxyCall.HostHeader ?? ""}'."); + } + + await ws.CloseTestDoneAsync(token); + } + finally + { + // Clear the recorder hook before any other test sees the factory; the + // [NotInParallel] key guarantees no overlap but resetting in `finally` + // keeps the post-condition obvious to readers. + fixture.Factory.OutboundHttpUriRecorder = null; + } + } + + // JoinTopicAsync + WebSocketBasePath moved to Interfold.IntegrationTests.TestServices.WebSocketExtensions + // during the Phase-6 test-project split so Shared plumbing can join without a circular ref. + + async Task> SendJoinAndReceiveReplyAsync(WebSocket ws, string socketToken, string systemId, string refId, CancellationToken token) + { + var joinFrame = new PhxFrame + { + Topic = $"system:{systemId}", + Event = "phx_join", + Payload = new PhxJoinPayload { Token = new SocketToken(socketToken), IsReconnect = true }, + Ref = refId, + JoinRef = "1" + }; + + await ws.SendTextFrameAsync(joinFrame.ToBytes(), token); + var frame = await ReceivedPhxFrame.ReceiveAsync(ws, token); + return frame.Reply(); + } + + static int ExtractAlterIdFromEndpointReply(ReceivedPhxFrame replyFrame) + { + var reply = replyFrame.Reply(); + if (string.IsNullOrWhiteSpace(reply.Response.Body)) + throw new InvalidOperationException("Endpoint proxy response body is empty — cannot extract alter ID."); + + using var bodyDoc = JsonDocument.Parse(reply.Response.Body); + var root = bodyDoc.RootElement; + + if (root.TryGetProperty("data", out var data) && data.TryGetProperty("id", out var idProp) && idProp.TryGetInt32(out var id)) + return id; + + throw new InvalidOperationException($"Could not parse alter id from endpoint reply body: {reply.Response.Body}"); + } + + static Uri WebSocketBasePath(TestServer server) + { + return WebSocketExtensions.WebSocketBasePath(server); + } +} diff --git a/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketThreadStarvationTests.cs b/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketThreadStarvationTests.cs new file mode 100644 index 00000000..2e41f57c --- /dev/null +++ b/tests/integration/Interfold.Socket.IntegrationTests/Endpoints/WebSocketThreadStarvationTests.cs @@ -0,0 +1,197 @@ +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Interfold.Socket.IntegrationTests.Endpoints; + +/// +/// Regression guard for the CI-only failure in +/// WebSocketTests.Api_UserSocketEndpoint_PushesFriendTrustedAndUntrusted_ToActor. +/// +/// The root cause was SocketEventPumpRunner.SubscribeAsync<TEvent>: a single +/// transient exception inside the per-event await foreach loop tore down that +/// subscription for the rest of the socket's lifetime, so the recipient silently +/// stopped receiving friend_request_received pushes. The bug only reproduced +/// under thread-pool contention (CI runners are ~2 cores; dev boxes are not), which +/// is what makes the underlying handler chain transiently throw. +/// +/// This test makes the contention deterministic so the bug stays caught even when +/// nothing else is running. It: +/// +/// +/// Runs with [NotInParallel] (no key) so it cannot accidentally rely on +/// other tests warming up the pool. +/// Drops to 2 / 2 so the +/// runtime injects additional pool threads on its slow ~500ms cadence rather +/// than the eager dev-box default. We deliberately do not touch +/// — capping max at +/// gridlocks the test on a many-core +/// box because the synthetic load claims every available thread. +/// Spins up a handful of background tasks that hammer GET /health/live +/// against the same the +/// WebSocket flow is using. Pure CPU hogs (e.g. +/// loops) weren't enough to reproduce; the CI failure only shows up when the +/// real ASP.NET request pipeline (routing, middleware, DI scope creation, HTTP +/// message handlers) is busy, and hammering a real anonymous endpoint +/// reproduces that contention shape while staying side-effect-free. +/// Runs the exact friend trust/untrust WebSocket flow that fails in CI. +/// Cancels the load tasks and restores the previous min in finally so +/// the rest of the session is unaffected. +/// +/// +/// If the resilience guard in SocketEventPumpRunner is removed (or another +/// regression introduces the same "one handler throw kills the subscription" +/// behaviour) this test fails locally and in CI without having to rely on the +/// natural thread-pool pressure of a parallel suite. +/// +/// [Retry(2)] is intentional: this is a stress test driven by synthetic load, +/// so on a quiet pool we want it to assert hard, but on a saturated host (low-core +/// CI runner with other concurrent work, busy dev box) a single timing slip past the +/// 30s per-frame ReceivedPhxFrame budget should not turn the build red. Pre-fix the +/// failure is deterministic — every attempt fails — so retries don't mask the +/// regression we're guarding against; post-fix flakiness collapses to a small +/// residual that the retries absorb. +/// +[Timeout(1000 * 300)] +[NotInParallel] +[Retry(2)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class WebSocketThreadStarvationTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + // Match the CI runner's pre-warmed worker/IO counts. SetMaxThreads cannot go below + // ProcessorCount on .NET, so we additionally saturate the pool with synthetic + // hammer tasks below to make it _behave_ like a small pool on any host. + private const int ThrottledWorkerMin = 2; + private const int ThrottledIoMin = 2; + + [Test] + public async Task Api_UserSocketEndpoint_PushesFriendTrustedAndUntrusted_ToActor_UnderThreadStarvation(CancellationToken token) + { + var logger = fixture.Factory.Services + .GetRequiredService() + .CreateLogger(); + + ThreadPool.GetMinThreads(out var previousWorkerMin, out var previousIoMin); + + // Lower MIN only — capping MAX gridlocks the test on dev boxes (the synthetic + // load claims every available thread and the WebSocket continuations have + // nowhere to run). With MIN lowered the runtime injects new threads on its + // slow ~500ms cadence while the hammer keeps the request pipeline busy. + var minApplied = ThreadPool.SetMinThreads(ThrottledWorkerMin, ThrottledIoMin); + + var processorCount = Environment.ProcessorCount; + + // Hammer count scales linearly with cores so the test produces consistent + // pipeline pressure on any host — not just the 2-core CI runner — and the + // bug reproduces deterministically without the fix on dev boxes too. + // + // - 2-core CI runner -> 4 tasks (floor) + // - 14-core dev box -> 7 tasks + // - 32-core CI worker -> 16 tasks + // - 64-core monster -> 32 tasks + // + // Floor of 4 matches the original clamp that reliably reproduces pre-fix on + // the smallest CI runner. We deliberately don't cap on the high end any more + // — the previous Math.Clamp(., 4, 4) ceiling meant a 64-core host saw the + // same 4 hammers as a 16-core, so the regression couldn't reproduce there at + // all. Combined with the 25ms per-request breather (~40 req/s per task) that + // works out to ~160 req/s on CI scaling to ~1.3k req/s on a 64-core box — + // enough pipeline contention everywhere without OOMing the InMemory fixture + // at any size. [Retry(2)] on the class absorbs any post-fix timing slips + // that the larger total throughput introduces; pre-fix the failure is still + // deterministic on every attempt. + var hammerCount = Math.Max(4, processorCount / 2); + + logger.LogInformation( + "ThreadPool throttle applied. minApplied={MinApplied} processors={Processors} hammerTasks={HammerTasks} " + + "prevMin=({PrevWorkerMin},{PrevIoMin}) newMin=({NewWorkerMin},{NewIoMin})", + minApplied, + processorCount, + hammerCount, + previousWorkerMin, + previousIoMin, + ThrottledWorkerMin, + ThrottledIoMin); + + using var stopHammer = new CancellationTokenSource(); + var hammerTasks = new List(hammerCount); + for (var i = 0; i < hammerCount; i++) + { + hammerTasks.Add(Task.Run(async () => + { + // Each hammer task uses its own HttpClient so we don't contend on a + // single client's connection pool — we want contention on the + // TestServer pipeline (routing, middleware, DI scoping, HttpClient + // continuations), not on a single client's outbound queue. + using var hammerClient = fixture.Factory.CreateClient(); + + while (!stopHammer.Token.IsCancellationRequested) + { + try + { + // ResponseHeadersRead avoids buffering the response body. + // /health/live returns only ~30 bytes, but at unthrottled + // throughput the buffering OOMed the InMemory fixture in + // under a minute on dev. + using (await hammerClient.GetAsync( + "/health/live", + HttpCompletionOption.ResponseHeadersRead, + stopHammer.Token)) + { + // No-op: response disposes when the using exits. + } + + // ~40 RPS per task. The goal is sustained pipeline pressure + // (so the runtime keeps queuing continuations while the + // friend-request flow runs), not a load test. + await Task.Delay(25, stopHammer.Token); + } + catch (OperationCanceledException) + { + return; + } + catch + { + // Transient failures are fine; the point is keeping the + // request pipeline busy, not asserting health-check success. + } + } + })); + } + + try + { + await RunFriendTrustUntrustFlowAsync(token); + } + finally + { + await stopHammer.CancelAsync(); + try { await Task.WhenAll(hammerTasks); } catch { /* cancellation throws are expected */ } + ThreadPool.SetMinThreads(previousWorkerMin, previousIoMin); + logger.LogInformation( + "ThreadPool throttle released. hammerTasks={HammerTasks} restoredMin=({RestoredWorkerMin},{RestoredIoMin})", + hammerCount, + previousWorkerMin, + previousIoMin); + } + } + + private async Task RunFriendTrustUntrustFlowAsync(CancellationToken token) + { + var senderSystemId = WebSocketTests.UniqueId("sys-starve-sender"); + var recipientSystemId = WebSocketTests.UniqueId("sys-starve-recipient"); + + var pair = await WebSocketHarness.ConnectPairAndJoinAsync(fixture, senderSystemId, recipientSystemId, token); + using var senderWs = pair.FirstWs; + using var recipientWs = pair.SecondWs; + + await FriendTrustUntrustFlow.RunAsync( + senderWs, senderSystemId, recipientWs, recipientSystemId, token, + contextSuffix: "under thread starvation"); + + await WebSocketExtensions.CloseTestDoneAsync(senderWs, recipientWs, token); + } +} diff --git a/tests/integration/Interfold.Socket.IntegrationTests/Interfold.Socket.IntegrationTests.csproj b/tests/integration/Interfold.Socket.IntegrationTests/Interfold.Socket.IntegrationTests.csproj new file mode 100644 index 00000000..2316ea8d --- /dev/null +++ b/tests/integration/Interfold.Socket.IntegrationTests/Interfold.Socket.IntegrationTests.csproj @@ -0,0 +1,22 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Systems.IntegrationTests/Controllers/PublicSystemsControllerTests.cs b/tests/integration/Interfold.Systems.IntegrationTests/Controllers/PublicSystemsControllerTests.cs new file mode 100644 index 00000000..cc10c324 --- /dev/null +++ b/tests/integration/Interfold.Systems.IntegrationTests/Controllers/PublicSystemsControllerTests.cs @@ -0,0 +1,310 @@ +using System.Net; +using Interfold.Alters.Contracts.Models; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Tags.Contracts.Ids; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Systems.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class PublicSystemsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task PublicBatch_SelfLookup_Returns403InvalidEndpoint() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-public-batch-self"; + _ = await CreateAlterAsync(client, principal, "BatchSelfSeed"); + await EnsureUserExistsAsync(client, principal, "batch-self"); + + using var res = await client.SendAuthedGetAsync($"/api/systems/{principal}/batch", principal); + var error = await res.ReadErrorAsync(HttpStatusCode.Forbidden); + await Assert.That(error.Code).IsEqualTo(ErrorCodes.InvalidEndpoint); + } + + [Test, Skip("Need to rework")] //Well all of them really... + public async Task Visibility_NonFriendFriendTrusted_AppliesToFronting() + { + using var client = TestClient.NoRedirect(fixture); + + var (owner, nonFriend, friend, trusted) = await SeedVisibilityQuartetAsync(client, "fronting-visibility"); + + var alterPublic = await CreateAlterAsync(client, owner, "VisPublic"); + var alterFriends = await CreateAlterAsync(client, owner, "VisFriends"); + var alterTrusted = await CreateAlterAsync(client, owner, "VisTrusted"); + var alterPrivate = await CreateAlterAsync(client, owner, "VisPrivate"); + + await SetAlterSecurityLevelAsync(client, owner, alterPublic, VisibilityLevel.Public); + await SetAlterSecurityLevelAsync(client, owner, alterFriends, VisibilityLevel.FriendsOnly); + await SetAlterSecurityLevelAsync(client, owner, alterTrusted, VisibilityLevel.TrustedOnly); + await SetAlterSecurityLevelAsync(client, owner, alterPrivate, VisibilityLevel.Private); + + await StartFrontAsync(client, owner, alterPublic); + await StartFrontAsync(client, owner, alterFriends); + await StartFrontAsync(client, owner, alterTrusted); + await StartFrontAsync(client, owner, alterPrivate); + + // Non-friend viewer: only the Public fronter surfaces; the other three are filtered + // by the guarded fronting list. One GET per viewer is enough — the response is + // idempotent for a fixed friendship-graph state, so batching the four visibility + // assertions against the same payload is both stricter and one-quarter the round + // trips of the pre-typed-sweep needle hunt. + await AssertFrontingVisibilityAsync( + client, owner, nonFriend, + visible: new[] { alterPublic }, + hidden: new[] { alterFriends, alterTrusted, alterPrivate }); + + // Friend viewer gains FriendsOnly. + await SendFriendRequestAndAcceptAsync(client, friend, owner); + await AssertFrontingVisibilityAsync( + client, owner, friend, + visible: new[] { alterPublic, alterFriends }, + hidden: new[] { alterTrusted, alterPrivate }); + + // Trusted viewer gains TrustedOnly; Private stays hidden. + await SendFriendRequestAndAcceptAsync(client, trusted, owner); + await SetFriendTrustAsync(client, owner, trusted); + await AssertFrontingVisibilityAsync( + client, owner, trusted, + visible: new[] { alterPublic, alterFriends, alterTrusted }, + hidden: new[] { alterPrivate }); + } + + /// + /// Asserts every id in appears in the guarded fronting list + /// for , and every id in is absent. + /// Uses .Id (the row-level typed shape produced + /// by PublicSystemsController.ListFronting) rather than string-searching the + /// envelope; this way, a serialiser drift that renames the field or drops the alter + /// nesting fails at deserialise time instead of silently matching / missing. + /// + private static async Task AssertFrontingVisibilityAsync( + HttpClient client, + string owner, + string viewer, + AlterId[] visible, + AlterId[] hidden) + { + using var res = await client.SendAuthedGetAsync($"/api/systems/{owner}/fronting", viewer); + var envelope = await res.ReadEnvelopeAsync>(HttpStatusCode.OK); + var visibleAlterIds = envelope.Data.Select(f => f.Alter.Id).ToArray(); + + using (Assert.Multiple()) + { + foreach (var expected in visible) + { + await Assert.That(visibleAlterIds).Contains(expected) + .Because($"Expected viewer '{viewer}' to see fronter alter {expected.Value} in owner '{owner}''s fronting list."); + } + foreach (var restricted in hidden) + { + await Assert.That(visibleAlterIds).DoesNotContain(restricted) + .Because($"Expected viewer '{viewer}' NOT to see fronter alter {restricted.Value} in owner '{owner}''s fronting list (visibility-restricted)."); + } + } + } + + [Test] + public async Task PublicAlter_PrivateSecurity_Returns404ForAnonymous() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-guarded-alter"; + var alterId = await CreateAlterAsync(client, principal, "GuardedAlter"); + await EnsureUserExistsAsync(client, principal, "guarded-alter"); + + await SetAlterSecurityLevelAsync(client, principal, alterId, VisibilityLevel.Private); + + using var publicRes = await client.GetAsync($"/api/systems/{principal}/alters/{alterId}"); + await Assert.That(publicRes.StatusCode).IsEqualTo(HttpStatusCode.Unauthorized); + } + + [Test] + public async Task Visibility_NonFriendFriendTrusted_AppliesAcrossPublicReads() + { + using var client = TestClient.NoRedirect(fixture); + + var (owner, nonFriend, friend, trusted) = await SeedVisibilityQuartetAsync(client, "alters-visibility"); + + var alterPublic = await CreateAlterAsync(client, owner, "VisPublic"); + var alterFriends = await CreateAlterAsync(client, owner, "VisFriends"); + var alterTrusted = await CreateAlterAsync(client, owner, "VisTrusted"); + var alterPrivate = await CreateAlterAsync(client, owner, "VisPrivate"); + + await SetAlterSecurityLevelAsync(client, owner, alterPublic, VisibilityLevel.Public); + await SetAlterSecurityLevelAsync(client, owner, alterFriends, VisibilityLevel.FriendsOnly); + await SetAlterSecurityLevelAsync(client, owner, alterTrusted, VisibilityLevel.TrustedOnly); + await SetAlterSecurityLevelAsync(client, owner, alterPrivate, VisibilityLevel.Private); + + // Non-friend viewer: sees Public only. Restricted alters surface as + // alter_not_found (the guarded read masks visibility rejects as 404s so callers + // can't distinguish "no such alter" from "not permitted"). + await AssertAlterVisibleAsync(client, owner, alterPublic, nonFriend); + await AssertAlterHiddenAsync(client, owner, alterFriends, nonFriend); + await AssertAlterHiddenAsync(client, owner, alterTrusted, nonFriend); + await AssertAlterHiddenAsync(client, owner, alterPrivate, nonFriend); + + // Friend viewer gains FriendsOnly; TrustedOnly stays hidden. + await SendFriendRequestAndAcceptAsync(client, friend, owner); + + await AssertAlterVisibleAsync(client, owner, alterFriends, friend); + await AssertAlterHiddenAsync(client, owner, alterTrusted, friend); + + // Trusted viewer gains TrustedOnly; Private stays hidden. + await SendFriendRequestAndAcceptAsync(client, trusted, owner); + await SetFriendTrustAsync(client, owner, trusted); + + await AssertAlterVisibleAsync(client, owner, alterTrusted, trusted); + await AssertAlterHiddenAsync(client, owner, alterPrivate, trusted); + } + + /// + /// Asserts can GET /api/systems/{owner}/alters/{alterId} + /// with 200 + the alter payload's matching. Thin domain-flavoured + /// wrapper over ; kept as a named site so + /// the call sites read as intent (visible alter) rather than as generics gymnastics. + /// + private static Task AssertAlterVisibleAsync(HttpClient client, string owner, AlterId alterId, string viewer) + => AssertGuardedResourceVisibleAsync( + client, owner, alterId, viewer, "alters", a => a.Id, "alter"); + + /// + /// Asserts hits 404 + + /// on the guarded alter GET. The API deliberately conflates "no such alter" and + /// "not permitted" into the same shape so callers can't fingerprint restricted alters; + /// this assertion pins that behaviour via . + /// + private static Task AssertAlterHiddenAsync(HttpClient client, string owner, AlterId alterId, string viewer) + => AssertGuardedResourceHiddenAsync( + client, owner, alterId, viewer, "alters", ErrorCodes.AlterNotFound, "alter"); + + [Test] + public async Task PublicTag_PrivateSecurity_Returns404ForAnonymous() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-guarded-tag"; + var tagId = await CreateTagAsync(client, principal, "GuardedTag"); + await EnsureUserExistsAsync(client, principal, "guarded-tag"); + + await SetTagSecurityLevelAsync(client, principal, tagId, VisibilityLevel.Private); + + using var publicRes = await client.GetAsync($"/api/systems/{principal}/tags/{tagId}"); + await Assert.That(publicRes.StatusCode).IsEqualTo(HttpStatusCode.Unauthorized); + } + + [Test] + public async Task Visibility_NonFriendFriendTrusted_AppliesToTags() + { + using var client = TestClient.NoRedirect(fixture); + + var (owner, nonFriend, friend, trusted) = await SeedVisibilityQuartetAsync(client, "tags-visibility"); + + var tagPublic = await CreateTagAsync(client, owner, "TagPublic"); + var tagFriends = await CreateTagAsync(client, owner, "TagFriends"); + var tagTrusted = await CreateTagAsync(client, owner, "TagTrusted"); + var tagPrivate = await CreateTagAsync(client, owner, "TagPrivate"); + + await SetTagSecurityLevelAsync(client, owner, tagPublic, VisibilityLevel.Public); + await SetTagSecurityLevelAsync(client, owner, tagFriends, VisibilityLevel.FriendsOnly); + await SetTagSecurityLevelAsync(client, owner, tagTrusted, VisibilityLevel.TrustedOnly); + await SetTagSecurityLevelAsync(client, owner, tagPrivate, VisibilityLevel.Private); + + // Non-friend viewer: Public only; restricted tags mask as tag_not_found for the + // same fingerprint-resistance reason the alter surface uses. + await AssertTagVisibleAsync(client, owner, tagPublic, nonFriend); + await AssertTagHiddenAsync(client, owner, tagFriends, nonFriend); + await AssertTagHiddenAsync(client, owner, tagTrusted, nonFriend); + await AssertTagHiddenAsync(client, owner, tagPrivate, nonFriend); + + // Friend viewer gains FriendsOnly. + await SendFriendRequestAndAcceptAsync(client, friend, owner); + + await AssertTagVisibleAsync(client, owner, tagFriends, friend); + await AssertTagHiddenAsync(client, owner, tagTrusted, friend); + + // Trusted viewer gains TrustedOnly; Private stays hidden. + await SendFriendRequestAndAcceptAsync(client, trusted, owner); + await SetFriendTrustAsync(client, owner, trusted); + + await AssertTagVisibleAsync(client, owner, tagTrusted, trusted); + await AssertTagHiddenAsync(client, owner, tagPrivate, trusted); + } + + /// + /// Tag counterpart of . Delegates to the shared + /// so a future change to the guarded-read + /// success contract (envelope shape, status code, id-carrying model surface) lands in + /// one place instead of drifting across the alter and tag mirrors. + /// + private static Task AssertTagVisibleAsync(HttpClient client, string owner, TagId tagId, string viewer) + => AssertGuardedResourceVisibleAsync( + client, owner, tagId, viewer, "tags", t => t.Id, "tag"); + + /// + /// Tag counterpart of . Delegates to + /// — the same "hide the reason" contract + /// the alter surface uses, so both mirrors share one authoritative failure shape. + /// + private static Task AssertTagHiddenAsync(HttpClient client, string owner, TagId tagId, string viewer) + => AssertGuardedResourceHiddenAsync( + client, owner, tagId, viewer, "tags", ErrorCodes.TagNotFound, "tag"); + + /// + /// Generic form of the "guarded public read succeeds for this viewer" assertion. Callers + /// supply the read model type, the id selector (so a typed / + /// comparison keeps its type safety end-to-end), the URL segment, + /// and a resource label for the human-readable failure message. Deserialising into the + /// real read model means a wire-shape drift trips at deserialise time rather than + /// silently matching / missing in a stringly-typed body scan. + /// + private static async Task AssertGuardedResourceVisibleAsync( + HttpClient client, + string owner, + TId id, + string viewer, + string urlSegment, + Func idSelector, + string resourceLabel) + where TReadModel : notnull + where TId : notnull + { + using var res = await client.SendAuthedGetAsync($"/api/systems/{owner}/{urlSegment}/{id}", viewer); + var envelope = await res.ReadEnvelopeAsync(HttpStatusCode.OK); + await Assert.That(idSelector(envelope.Data)).IsEqualTo(id) + .Because($"Expected viewer '{viewer}' to see {resourceLabel} {id} in owner '{owner}''s public read; got a different {resourceLabel} id back."); + } + + /// + /// Generic form of the "guarded public read masks as 404" assertion. Callers supply the + /// URL segment, the expected error code, and the resource label; the shared body pins + /// the 404 status + the code so a regression that unmasks visibility (e.g. surfacing + /// alter_forbidden instead of alter_not_found) fails the same way across + /// every guarded-resource surface. + /// + private static async Task AssertGuardedResourceHiddenAsync( + HttpClient client, + string owner, + TId id, + string viewer, + string urlSegment, + ErrorCode expectedErrorCode, + string resourceLabel) + where TId : notnull + { + using var res = await client.SendAuthedGetAsync($"/api/systems/{owner}/{urlSegment}/{id}", viewer); + var error = await res.ReadErrorAsync(HttpStatusCode.NotFound); + await Assert.That(error.Code).IsEqualTo(expectedErrorCode) + .Because($"Expected viewer '{viewer}' to receive {expectedErrorCode.Value} for owner '{owner}''s {resourceLabel} {id}; got '{error.Code.Value}' with detail '{error.Detail}'."); + } + +} + diff --git a/tests/integration/Interfold.Systems.IntegrationTests/Interfold.Systems.IntegrationTests.csproj b/tests/integration/Interfold.Systems.IntegrationTests/Interfold.Systems.IntegrationTests.csproj new file mode 100644 index 00000000..b2dee855 --- /dev/null +++ b/tests/integration/Interfold.Systems.IntegrationTests/Interfold.Systems.IntegrationTests.csproj @@ -0,0 +1,22 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/integration/Interfold.Tags.IntegrationTests/Controllers/TagsControllerTests.cs b/tests/integration/Interfold.Tags.IntegrationTests/Controllers/TagsControllerTests.cs new file mode 100644 index 00000000..1e9b82cc --- /dev/null +++ b/tests/integration/Interfold.Tags.IntegrationTests/Controllers/TagsControllerTests.cs @@ -0,0 +1,51 @@ +using System.Net; +using Interfold.IntegrationTests.Shared; +using Interfold.IntegrationTests.Shared.TestServices; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Tags.IntegrationTests.Controllers; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class TagsControllerTests(IWebFactoryFixture fixture) : BaseEndpointTest +{ + [Test] + public async Task Idempotency_TagCreate_ReplayStable() + { + await RunSoakAsync(fixture.Factory, async (client, key) => + { + return await client.SendAsJsonAsync( + HttpMethod.Post, "/api/systems/me/tags", + new CreateTagRequest("SoakTag", ParentTagId: null), + "soak-default-principal", + idempotencyKey: key); + }); + } + + [Test] + public async Task TagParent_SetAndRemove_Returns204() + { + using var client = TestClient.NoRedirect(fixture); + + var principal = "parity-tag-parent"; + var parentTagId = await CreateTagAsync(client, principal, "ParentTag"); + var childTagId = await CreateTagAsync(client, principal, "ChildTag"); + + using var setRes = await client.SendAsJsonAsync( + HttpMethod.Post, $"/api/systems/me/tags/{childTagId}/parent", + new SetParentRequest(parentTagId), + principal); + + using var removeRes = await client.SendAsJsonAsync( + HttpMethod.Delete, $"/api/systems/me/tags/{childTagId}/parent", + new SetParentRequest(ParentTagId: null), + principal); + + using (Assert.Multiple()) + { + await Assert.That(setRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + await Assert.That(removeRes.StatusCode).IsEqualTo(HttpStatusCode.NoContent); + } + } +} diff --git a/tests/integration/Interfold.Tags.IntegrationTests/Interfold.Tags.IntegrationTests.csproj b/tests/integration/Interfold.Tags.IntegrationTests/Interfold.Tags.IntegrationTests.csproj new file mode 100644 index 00000000..5648b844 --- /dev/null +++ b/tests/integration/Interfold.Tags.IntegrationTests/Interfold.Tags.IntegrationTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/unit/Interfold.Alters.UnitTests/Domain/AlterCommandFlowExecutionTests.cs b/tests/unit/Interfold.Alters.UnitTests/Domain/AlterCommandFlowExecutionTests.cs new file mode 100644 index 00000000..5b91d682 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/Domain/AlterCommandFlowExecutionTests.cs @@ -0,0 +1,200 @@ +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Alters.Domain; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Api.UnitTests.Domain; + +// Regression harness for UpdateAlterCommandHandler's reject branches. Same "no side +// effects when validation fails" contract PollCommandFlowExecutionTests pins for the +// poll flow — a future eager-argument evaluation would spuriously write + publish +// while HTTP-level tests still see the correct alter:not_found conflict. +public sealed class AlterCommandFlowExecutionTests +{ + private static readonly ScopedSystemId Principal = + ScopedSystemId.Compose(ScyllaKeyspace.Nam, "altrflw"); + + private static readonly AlterId AnyAlterId = new(5); + + [Test] + public async Task UpdateAlter_MissingAlter_RejectsWithNotFoundAndDoesNotMutateOrPublish() + { + var repo = new CountingAlterRepository { Exists = false }; + var bus = new CountingEventBus(); + var handler = new UpdateAlterCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(NewMutatingPayload("renamed")); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse() + .Because("A missing alter must be rejected, never accepted."); + await Assert.That(result.Conflict?.EntityRef).IsEqualTo(EntityRefs.AlterNotFound) + .Because("The rejection must be the not-found invariant, not a mutation-failed variant."); + await Assert.That(repo.ExistsCalls).IsEqualTo(1) + .Because("The existence check itself must run exactly once so the reject path is provable."); + await Assert.That(repo.UpdateCalls).IsEqualTo(0) + .Because("A not-found alter must never reach UpdateAsync — an eager-Task regression on ExecuteMutationAsync would raise this count."); + await Assert.That(bus.PublishCalls).IsEqualTo(0) + .Because("A not-found alter must never publish AlterUpdatedEvent — same eager-Task regression class."); + await Assert.That(repo.AliasTakenCalls).IsEqualTo(0) + .Because("The alias-collision probe is downstream of the existence check and must not run against a missing row."); + } + } + + [Test] + public async Task UpdateAlter_ExistsButMutationReturnsFalse_RejectsWithUpdateFailedAndDoesNotPublish() + { + var repo = new CountingAlterRepository { Exists = true, UpdateResult = false }; + var bus = new CountingEventBus(); + var handler = new UpdateAlterCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(NewMutatingPayload("renamed")); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse(); + await Assert.That(result.Conflict?.EntityRef).IsEqualTo(EntityRefs.AlterUpdateFailed); + await Assert.That(repo.UpdateCalls).IsEqualTo(1); + await Assert.That(bus.PublishCalls).IsEqualTo(0) + .Because("A mutation that reports no change must not surface an Updated event."); + } + } + + [Test] + public async Task UpdateAlter_HappyPath_CallsMutateOncePublishesOnceAndAccepts() + { + var repo = new CountingAlterRepository { Exists = true, UpdateResult = true }; + var bus = new CountingEventBus(); + var handler = new UpdateAlterCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(NewMutatingPayload("renamed")); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsTrue(); + await Assert.That(repo.UpdateCalls).IsEqualTo(1); + await Assert.That(bus.PublishCalls).IsEqualTo(1); + } + } + + private static UpdateAlterCommand NewMutatingPayload(string newName) => + new() + { + AlterId = AnyAlterId, + Name = newName, + UpdatedAt = DateTimeOffset.UtcNow, + }; + + private static CommandEnvelope NewEnvelope(UpdateAlterCommand payload) => + new( + OperationId: OperationIds.AlterUpdate, + CommandId: Guid.NewGuid(), + PrincipalId: Principal, + IdempotencyKey: new IdempotencyKey(Guid.NewGuid().ToString("N")), + OccurredAt: DateTimeOffset.UtcNow, + Payload: payload); + + // Only the members reachable from UpdateAlterCommandHandler are implemented; the + // rest throw so an unrelated method call fails loudly rather than returning defaults. + private sealed class CountingAlterRepository : IAlterRepository + { + public bool Exists { get; set; } + public bool UpdateResult { get; set; } + public int ExistsCalls; + public int UpdateCalls; + public int AliasTakenCalls; + + public Task ExistsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref ExistsCalls); + return Task.FromResult(Exists); + } + + public Task UpdateAsync(SystemId systemId, UpdateAlterCommand command, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref UpdateCalls); + return Task.FromResult(UpdateResult); + } + + public Task AliasTakenByOtherAsync(SystemId systemId, AlterId alterId, string alias, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref AliasTakenCalls); + return Task.FromResult(false); + } + + public Task CreateAsync(SystemId systemId, CreateAlterCommand command, CancellationToken cancellationToken = default) + => throw new NotSupportedException("CreateAsync is not reachable from the UpdateAlterCommandHandler path."); + + public Task DeleteAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + => throw new NotSupportedException("DeleteAsync is not reachable from the UpdateAlterCommandHandler path."); + + public Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + => throw new NotSupportedException("ListAsync is not reachable from the UpdateAlterCommandHandler path."); + + public Task> ListGuardedAsync(SystemId systemId, SystemId? viewerSystemId, CancellationToken cancellationToken = default) + => throw new NotSupportedException("ListGuardedAsync is not reachable from the UpdateAlterCommandHandler path."); + + public Task GetAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + => throw new NotSupportedException("GetAsync is not reachable from the UpdateAlterCommandHandler path."); + + public Task GetGuardedAsync(SystemId systemId, AlterId alterId, SystemId? viewerSystemId, CancellationToken cancellationToken = default) + => throw new NotSupportedException("GetGuardedAsync is not reachable from the UpdateAlterCommandHandler path."); + } + + private sealed class CountingEventBus : IClusterEventBus + { + public int PublishCalls; + + public ValueTask PublishAsync(TEvent evt, CancellationToken ct = default) where TEvent : class + { + Interlocked.Increment(ref PublishCalls); + return ValueTask.CompletedTask; + } + + public IAsyncEnumerable SubscribeAsync(ScopedSystemId? targetSystemId, CancellationToken ct = default) where TEvent : class + => AsyncEnumerable.Empty(); + } + + private sealed class NoopIdempotencyStore : IIdempotencyStore + { + public Task FindAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default) => Task.FromResult(null); + + public Task SaveAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + string payloadHash, + string outcomeHash, + string? outcomePayload, + CancellationToken cancellationToken = default) => Task.CompletedTask; + } + + private static class AsyncEnumerable + { + public static IAsyncEnumerable Empty() => new EmptyAsyncEnumerable(); + + private sealed class EmptyAsyncEnumerable : IAsyncEnumerable, IAsyncEnumerator + { + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) => this; + public T Current => default!; + public ValueTask MoveNextAsync() => ValueTask.FromResult(false); + public ValueTask DisposeAsync() => ValueTask.CompletedTask; + } + } +} diff --git a/tests/unit/Interfold.Alters.UnitTests/Ids/AlterIdJsonTests.cs b/tests/unit/Interfold.Alters.UnitTests/Ids/AlterIdJsonTests.cs new file mode 100644 index 00000000..ff451ab6 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/Ids/AlterIdJsonTests.cs @@ -0,0 +1,80 @@ +using System.Text.Json; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// AlterId JSON contract with a short-backed value: bare JSON number, byte-identical +// round-trips in smallint range, out-of-range surfaces as JsonException at the byte +// boundary (400 before the controller runs, not just at ValidAlterIdAttribute). +public sealed class AlterIdJsonTests +{ + [Test] + [Arguments((short)1)] + [Arguments((short)1000)] + [Arguments(short.MaxValue)] + [Arguments((short)0)] + [Arguments((short)-1)] + [Arguments(short.MinValue)] + public async Task Json_InRangeSmallint_RoundTrips(short value) + { + var original = new AlterId(value); + var json = JsonSerializer.Serialize(original); + var deserialised = JsonSerializer.Deserialize(json); + + using (Assert.Multiple()) + { + await Assert.That(json).IsEqualTo(value.ToString(System.Globalization.CultureInfo.InvariantCulture)) + .Because("Wire emission is a bare JSON number matching short.ToString — byte-identical to the pre-narrowing shape, which is a precondition for idempotent replay of stored commands."); + await Assert.That(deserialised.Value).IsEqualTo(value); + } + } + + [Test] + [Arguments("32768")] + [Arguments("-32769")] + [Arguments("2147483647")] + [Arguments("-2147483648")] + [Arguments("9999999999999")] + public async Task Json_OutOfShortRange_ThrowsJsonException(string numberLiteral) + { + await Assert.That(() => JsonSerializer.Deserialize(numberLiteral)) + .ThrowsExactly() + .Because("The JSON reader must reject anything outside short range at the byte boundary — ASP.NET Core maps this to a 400 before the model-binding validator ever runs."); + } + + [Test] + [Arguments("\"5\"")] + [Arguments("null")] + [Arguments("true")] + [Arguments("[1]")] + public async Task Json_NonNumeric_ThrowsJsonException(string malformedJson) + { + await Assert.That(() => JsonSerializer.Deserialize(malformedJson)) + .ThrowsExactly(); + } + + [Test] + public async Task IParsable_InRange_ReturnsAlterId() + { + var parsed = AlterId.TryParse("42", provider: null, out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(result.Value).IsEqualTo((short)42); + } + } + + [Test] + [Arguments("32768")] + [Arguments("-32769")] + [Arguments("2147483647")] + [Arguments("not-a-number")] + public async Task IParsable_OutOfRange_ReturnsFalse(string input) + { + var parsed = AlterId.TryParse(input, provider: null, out _); + await Assert.That(parsed).IsFalse() + .Because("Route-parameter binding uses TryParse; anything outside short range or non-numeric must yield false so ASP.NET Core surfaces a 400 rather than a runtime overflow."); + } + +} diff --git a/tests/unit/Interfold.Alters.UnitTests/InMemoryAlterRepositoryTests.cs b/tests/unit/Interfold.Alters.UnitTests/InMemoryAlterRepositoryTests.cs new file mode 100644 index 00000000..6111c8e2 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/InMemoryAlterRepositoryTests.cs @@ -0,0 +1,252 @@ +using System.Collections.Concurrent; +using Interfold.Alters.Contracts.Models.Commands; +using Interfold.Settings.Domain; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Api.UnitTests.Support; +using Interfold.Friendships.Contracts.Models.Read; +using Interfold.Infrastructure.InMemory.Repository; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Interfold.Api.UnitTests; + +// Direct-to-repository coverage for InMemoryAlterRepository — the visibility matrix +// and field-projection paths that HTTP-layer tests can mask. +public sealed class InMemoryAlterRepositoryTests +{ + private static readonly SystemId OwnerId = new("owner01"); + private static readonly SystemId FriendId = new("frnd001"); + private static readonly SystemId TrustedId = new("trstd01"); + private static readonly SystemId StrangerId = new("stngr01"); + + private sealed record TestHarness( + InMemoryAlterRepository Alters, + InMemoryFriendshipRepository Friendships, + InMemorySettingsFieldRepository Fields); + + private static TestHarness BuildHarness() + { + // Fixed region so hash-routing doesn't land principals in different keyspaces + // and break the "everything talks to the same store" precondition. + var region = new FixedRegionContext(ScyllaKeyspace.Nam); + var friendships = new InMemoryFriendshipRepository(); + // Empty provider is safe: only used for cascade-delete of field definitions, + // which these tests never trigger. + var fields = new InMemorySettingsFieldRepository(region, new ServiceCollection().BuildServiceProvider()); + var alterFieldDefinitions = new AlterFieldDefinitionsAdapter(fields, NullLogger.Instance); + var polls = new InMemoryPollRepository(region); + var alters = new InMemoryAlterRepository(region, friendships, fields, alterFieldDefinitions, polls, NullLogger.Instance); + return new TestHarness(alters, friendships, fields); + } + + // Mutual auto-accept is the only public seam that produces a linked friendship + // in the InMemory port; SetTrustedAsync tightens owner→viewer specifically. + private static async Task SeedFriendshipAsync(InMemoryFriendshipRepository friendships, SystemId a, SystemId b, bool trusted = false) + { + await friendships.SendRequestAsync(a, b); + var outcome = await friendships.SendRequestAsync(b, a); + await Assert.That(outcome).IsEqualTo(SendFriendRequestOutcome.Accepted) + .Because("Mutual-request auto-accept is the only public seam that produces a linked friendship in the InMemory port."); + + if (trusted) + { + await friendships.SetTrustedAsync(a, b, trusted: true); + } + } + + private static Task CreateAlterAsync(InMemoryAlterRepository alters, SystemId systemId, string name) + => alters.CreateAsync(systemId, new CreateAlterCommand(name, DateTimeOffset.UtcNow)) + .ContinueWith(t => t.Result ?? throw new InvalidOperationException($"CreateAsync returned null for '{name}'.")); + + private static Task SetVisibilityAsync(InMemoryAlterRepository alters, SystemId systemId, AlterId alterId, VisibilityLevel level) + => alters.UpdateAsync(systemId, new UpdateAlterCommand + { + AlterId = alterId, + SecurityLevel = level, + UpdatedAt = DateTimeOffset.UtcNow, + }); + + [Test] + public async Task ListGuardedAsync_VisibilityMatrix_FiltersByViewer() + { + var harness = BuildHarness(); + + var publicId = await CreateAlterAsync(harness.Alters, OwnerId, "Publicly Visible"); + var friendsId = await CreateAlterAsync(harness.Alters, OwnerId, "Friends Only"); + var trustedOnlyId = await CreateAlterAsync(harness.Alters, OwnerId, "Trusted Only"); + var privateId = await CreateAlterAsync(harness.Alters, OwnerId, "Private"); + + await SetVisibilityAsync(harness.Alters, OwnerId, publicId, VisibilityLevel.Public); + await SetVisibilityAsync(harness.Alters, OwnerId, friendsId, VisibilityLevel.FriendsOnly); + await SetVisibilityAsync(harness.Alters, OwnerId, trustedOnlyId, VisibilityLevel.TrustedOnly); + await SetVisibilityAsync(harness.Alters, OwnerId, privateId, VisibilityLevel.Private); + + await SeedFriendshipAsync(harness.Friendships, OwnerId, FriendId); + await SeedFriendshipAsync(harness.Friendships, OwnerId, TrustedId, trusted: true); + + var strangerView = await harness.Alters.ListGuardedAsync(OwnerId, StrangerId); + var anonymousView = await harness.Alters.ListGuardedAsync(OwnerId, viewerSystemId: null); + var friendView = await harness.Alters.ListGuardedAsync(OwnerId, FriendId); + var trustedView = await harness.Alters.ListGuardedAsync(OwnerId, TrustedId); + var selfView = await harness.Alters.ListGuardedAsync(OwnerId, OwnerId); + + using (Assert.Multiple()) + { + await Assert.That(strangerView.Select(a => a.Id).ToArray()).IsEquivalentTo(new[] { publicId }); + await Assert.That(anonymousView.Select(a => a.Id).ToArray()).IsEquivalentTo(new[] { publicId }); + await Assert.That(friendView.Select(a => a.Id).ToArray()).IsEquivalentTo(new[] { publicId, friendsId }); + await Assert.That(trustedView.Select(a => a.Id).ToArray()).IsEquivalentTo(new[] { publicId, friendsId, trustedOnlyId }); + // Self-view is TrustedFriend-equivalent per ResolveFriendshipLevelAsync's self-check. + await Assert.That(selfView.Select(a => a.Id).ToArray()).IsEquivalentTo(new[] { publicId, friendsId, trustedOnlyId }); + } + } + + [Test] + public async Task GetGuardedAsync_VisibilityMatrix_FiltersByViewer() + { + var harness = BuildHarness(); + + var friendsId = await CreateAlterAsync(harness.Alters, OwnerId, "Friends Only"); + var trustedOnlyId = await CreateAlterAsync(harness.Alters, OwnerId, "Trusted Only"); + var privateId = await CreateAlterAsync(harness.Alters, OwnerId, "Private"); + + await SetVisibilityAsync(harness.Alters, OwnerId, friendsId, VisibilityLevel.FriendsOnly); + await SetVisibilityAsync(harness.Alters, OwnerId, trustedOnlyId, VisibilityLevel.TrustedOnly); + await SetVisibilityAsync(harness.Alters, OwnerId, privateId, VisibilityLevel.Private); + + await SeedFriendshipAsync(harness.Friendships, OwnerId, FriendId); + await SeedFriendshipAsync(harness.Friendships, OwnerId, TrustedId, trusted: true); + + using (Assert.Multiple()) + { + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, friendsId, StrangerId)).IsNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, trustedOnlyId, StrangerId)).IsNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, privateId, StrangerId)).IsNull(); + + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, friendsId, FriendId)).IsNotNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, trustedOnlyId, FriendId)).IsNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, privateId, FriendId)).IsNull(); + + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, friendsId, TrustedId)).IsNotNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, trustedOnlyId, TrustedId)).IsNotNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, privateId, TrustedId)).IsNull(); + + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, friendsId, viewerSystemId: null)).IsNull(); + await Assert.That(await harness.Alters.GetGuardedAsync(OwnerId, privateId, viewerSystemId: null)).IsNull(); + } + } + + // Field with a higher security level than the viewer's friendship level is projected + // out entirely — never redacted, never present with a null value. + [Test] + public async Task GetGuardedAsync_FieldSecurityLevel_FiltersDefinitionsByViewer() + { + var harness = BuildHarness(); + + var publicField = (await harness.Fields.CreateAsync(OwnerId, "PublicField", FieldType.Text, VisibilityLevel.Public, locked: false, DateTime.UtcNow))!.Value; + var friendsField = (await harness.Fields.CreateAsync(OwnerId, "FriendsField", FieldType.Text, VisibilityLevel.FriendsOnly, locked: false, DateTime.UtcNow))!.Value; + var trustedField = (await harness.Fields.CreateAsync(OwnerId, "TrustedField", FieldType.Text, VisibilityLevel.TrustedOnly, locked: false, DateTime.UtcNow))!.Value; + + var alterId = await CreateAlterAsync(harness.Alters, OwnerId, "Alter With Fields"); + await SetVisibilityAsync(harness.Alters, OwnerId, alterId, VisibilityLevel.Public); + var updated = await harness.Alters.UpdateAsync(OwnerId, new UpdateAlterCommand + { + AlterId = alterId, + Fields = new[] + { + new AlterFieldCommand(publicField, "public-value"), + new AlterFieldCommand(friendsField, "friends-value"), + new AlterFieldCommand(trustedField, "trusted-value"), + }, + UpdatedAt = DateTimeOffset.UtcNow, + }); + await Assert.That(updated).IsTrue(); + + await SeedFriendshipAsync(harness.Friendships, OwnerId, FriendId); + await SeedFriendshipAsync(harness.Friendships, OwnerId, TrustedId, trusted: true); + + var strangerView = await harness.Alters.GetGuardedAsync(OwnerId, alterId, StrangerId); + var friendView = await harness.Alters.GetGuardedAsync(OwnerId, alterId, FriendId); + var trustedView = await harness.Alters.GetGuardedAsync(OwnerId, alterId, TrustedId); + + using (Assert.Multiple()) + { + await Assert.That(strangerView).IsNotNull(); + await Assert.That(friendView).IsNotNull(); + await Assert.That(trustedView).IsNotNull(); + + await Assert.That(strangerView!.Fields.Select(f => f.Id).ToArray()).IsEquivalentTo(new[] { publicField }); + await Assert.That(friendView!.Fields.Select(f => f.Id).ToArray()).IsEquivalentTo(new[] { publicField, friendsField }); + await Assert.That(trustedView!.Fields.Select(f => f.Id).ToArray()).IsEquivalentTo(new[] { publicField, friendsField, trustedField }); + } + } + + // Guarded projection drops definitions the alter hasn't populated; unguarded reads + // are permissive and emit one row per definition with a null value when unset. + [Test] + public async Task GetGuardedAsync_MissingFieldValues_AreOmittedFromProjection() + { + var harness = BuildHarness(); + + var populated = (await harness.Fields.CreateAsync(OwnerId, "Populated", FieldType.Text, VisibilityLevel.Public, locked: false, DateTime.UtcNow))!.Value; + var unpopulated = (await harness.Fields.CreateAsync(OwnerId, "Unpopulated", FieldType.Text, VisibilityLevel.Public, locked: false, DateTime.UtcNow))!.Value; + + var alterId = await CreateAlterAsync(harness.Alters, OwnerId, "Sparse Alter"); + await SetVisibilityAsync(harness.Alters, OwnerId, alterId, VisibilityLevel.Public); + await harness.Alters.UpdateAsync(OwnerId, new UpdateAlterCommand + { + AlterId = alterId, + Fields = new[] { new AlterFieldCommand(populated, "the-only-value") }, + UpdatedAt = DateTimeOffset.UtcNow, + }); + + var view = await harness.Alters.GetGuardedAsync(OwnerId, alterId, StrangerId); + + using (Assert.Multiple()) + { + await Assert.That(view).IsNotNull(); + await Assert.That(view!.Fields.Count).IsEqualTo(1) + .Because("The unpopulated definition must be projected out — the guarded path drops definitions the alter hasn't filled in."); + await Assert.That(view.Fields[0].Id).IsEqualTo(populated); + await Assert.That(view.Fields[0].Value).IsEqualTo("the-only-value"); + await Assert.That(view.Fields.Any(f => f.Id == unpopulated)).IsFalse(); + } + } + + [Test] + public async Task CreateAsync_UnderParallelWriters_PreservesAllAlters() + { + var harness = BuildHarness(); + const int writerCount = 200; + + var createdIds = new ConcurrentBag(); + + await Parallel.ForEachAsync( + Enumerable.Range(0, writerCount), + new ParallelOptions { MaxDegreeOfParallelism = 32 }, + async (i, ct) => + { + var id = await harness.Alters.CreateAsync( + OwnerId, + new CreateAlterCommand($"parallel-{i}", DateTimeOffset.UtcNow), + ct); + if (id is not null) + { + createdIds.Add(id.Value); + } + }); + + var listed = await harness.Alters.ListAsync(OwnerId); + + using (Assert.Multiple()) + { + await Assert.That(createdIds.Count).IsEqualTo(writerCount) + .Because("Every CreateAsync must return a non-null AlterId under concurrent writers."); + await Assert.That(createdIds.Distinct().Count()).IsEqualTo(writerCount) + .Because("The AddOrUpdate counter must issue a distinct id per writer — a collision here would prove the increment isn't atomic."); + await Assert.That(listed.Count).IsEqualTo(writerCount) + .Because("The per-system store must contain every created alter after the fan-in."); + } + } +} diff --git a/tests/unit/Interfold.Alters.UnitTests/Interfold.Alters.UnitTests.csproj b/tests/unit/Interfold.Alters.UnitTests/Interfold.Alters.UnitTests.csproj new file mode 100644 index 00000000..4709f997 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/Interfold.Alters.UnitTests.csproj @@ -0,0 +1,28 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Alters.UnitTests/Models/AlterReadModelSerializationTests.cs b/tests/unit/Interfold.Alters.UnitTests/Models/AlterReadModelSerializationTests.cs new file mode 100644 index 00000000..76d3d743 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/Models/AlterReadModelSerializationTests.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using Interfold.Alters.Contracts.Models; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Api.UnitTests.Models; + +// PluralKit round-trip prerequisite: `discord_proxies` is the ONLY source for the +// PK v2 importer's `proxy_tags`, and `inserted_at` / `updated_at` are surfaced by +// the full-export path. All three live on the Scylla `alters` row (see +// infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql:118-129) +// but are dropped by the current AlterReadModel — this test locks in the widening. +public sealed class AlterReadModelSerializationTests +{ + [Test] + public async Task RoundTrip_PreservesDiscordProxiesAndTimestamps() + { + var original = new AlterReadModel( + id: new AlterId(1), + name: "Alpha", + description: "d", + avatarUrl: null, + avatarSource: null, + color: null, + pronouns: null, + securityLevel: VisibilityLevel.Public, + fields: System.Array.Empty(), + proxyName: null, + alias: null, + untracked: false, + archived: false, + pinned: false, + discordProxies: new[] { "heart;text moon" }, + insertedAt: new System.DateTime(2024, 1, 2, 3, 4, 5, System.DateTimeKind.Utc), + updatedAt: new System.DateTime(2024, 6, 7, 8, 9, 10, System.DateTimeKind.Utc)); + + var json = JsonSerializer.Serialize(original); + var deserialised = JsonSerializer.Deserialize(json) + ?? throw new System.InvalidOperationException("AlterReadModel deserialised to null."); + + using (Assert.Multiple()) + { + await Assert.That(deserialised.DiscordProxies).IsEquivalentTo(new[] { "heart;text moon" }); + await Assert.That(deserialised.InsertedAt).IsEqualTo(original.InsertedAt); + await Assert.That(deserialised.UpdatedAt).IsEqualTo(original.UpdatedAt); + } + } +} diff --git a/tests/unit/Interfold.Alters.UnitTests/Validation/ValidAlterIdAttributeTests.cs b/tests/unit/Interfold.Alters.UnitTests/Validation/ValidAlterIdAttributeTests.cs new file mode 100644 index 00000000..57276d79 --- /dev/null +++ b/tests/unit/Interfold.Alters.UnitTests/Validation/ValidAlterIdAttributeTests.cs @@ -0,0 +1,170 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Journals.Contracts.Models.Read; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Validation; +using Interfold.Tags.Contracts.Models.Read; + +namespace Interfold.Api.UnitTests.Validation; + +// Pure-attribute behaviour for ValidAlterIdAttribute. Mirrors the AbsoluteHttpUri / +// AbsolutePath tests in Interfold.Bootstrapper.UnitTests: exercises IsValid + the +// DataAnnotation wrapper so an accidental range edit trips a fast unit test. +public sealed class ValidAlterIdAttributeTests +{ + [Test] + [Arguments((short)1)] + [Arguments((short)2)] + [Arguments((short)1000)] + [Arguments(short.MaxValue)] + public async Task IsValid_AcceptsInRangeIds(short value) + { + await Assert.That(ValidAlterIdAttribute.IsValid(new AlterId(value))).IsTrue(); + } + + [Test] + [Arguments((short)0)] + [Arguments((short)-1)] + [Arguments(short.MinValue)] + public async Task IsValid_RejectsNegativeAndZeroIds(short value) + { + // Out-of-range JSON numbers surface as a JsonException in AlterIdJsonConverter; + // only null / zero / negatives reach this runtime check. + await Assert.That(ValidAlterIdAttribute.IsValid(new AlterId(value))).IsFalse(); + } + + [Test] + public async Task IsValid_RejectsNull() + { + await Assert.That(ValidAlterIdAttribute.IsValid(null)).IsFalse(); + } + + [Test] + public async Task Attribute_DefaultAllowNullIsFalse_NullIsRejected() + { + var attr = new ValidAlterIdAttribute(); + var ctx = new ValidationContext(new object()) { DisplayName = "AlterId", MemberName = "AlterId" }; + var result = attr.GetValidationResult(null, ctx); + await Assert.That(result).IsNotNull(); + } + + [Test] + public async Task Attribute_AllowNull_PassesForNull() + { + var attr = new ValidAlterIdAttribute { AllowNull = true }; + var ctx = new ValidationContext(new object()) { DisplayName = "Id", MemberName = "Id" }; + await Assert.That(attr.GetValidationResult(null, ctx)).IsEqualTo(ValidationResult.Success); + } + + [Test] + public async Task Attribute_AllowNull_StillRangeChecksNonNull() + { + var attr = new ValidAlterIdAttribute { AllowNull = true }; + var ctx = new ValidationContext(new object()) { DisplayName = "Id", MemberName = "Id" }; + var result = attr.GetValidationResult(new AlterId(0), ctx); + await Assert.That(result).IsNotNull() + .Because("AllowNull only unlocks the null case; 0 / negative / out-of-range values must still be rejected."); + } + + [Test] + public async Task Attribute_AcceptsAlterIdValueTypeDirectly() + { + var attr = new ValidAlterIdAttribute(); + var ctx = new ValidationContext(new object()) { DisplayName = "AlterId", MemberName = "AlterId" }; + await Assert.That(attr.GetValidationResult(new AlterId(5), ctx)).IsEqualTo(ValidationResult.Success); + } + + // InvalidModelStateResponseFactory matches this exact string via + // ValidationErrorCodeRegistry to preserve the wire-visible `invalid_alter_id` code. + [Test] + public async Task Attribute_ErrorMessageIsInvalidAlterIdSentinel() + { + var attr = new ValidAlterIdAttribute(); + await Assert.That(attr.ErrorMessage).IsEqualTo("Invalid alter ID."); + } + + // MVC hard-throws on records with validation metadata on properties + // (ThrowIfRecordTypeHasValidationOnProperties); reflect the primary-constructor + // parameter here to mirror what MVC reads at model-binding time. + + [Test] + public async Task FrontStartRequest_JsonWithValidId_DeserializesAndValidatesViaParameterAttribute() + { + var opts = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; + var req = JsonSerializer.Deserialize("{\"id\":5}", opts)!; + + await Assert.That(req.Id).IsEqualTo(new AlterId(5)); + await Assert.That(ValidateRecordParameter("Id", req.Id)).IsTrue(); + } + + [Test] + public async Task FrontStartRequest_JsonWithMissingId_FailsValidationViaParameterAttribute() + { + var opts = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; + var req = JsonSerializer.Deserialize("{}", opts)!; + + await Assert.That(req.Id).IsEqualTo(new AlterId(0)); + await Assert.That(ValidateRecordParameter("Id", req.Id)).IsFalse() + .Because("missing id defaults to AlterId(0); ValidAlterId must reject it."); + } + + [Test] + public async Task FrontPrimaryRequest_JsonWithNullId_PassesValidationViaParameterAttribute() + { + var opts = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; + var req = JsonSerializer.Deserialize("{\"id\":null}", opts)!; + + await Assert.That(req.Id).IsNull(); + await Assert.That(ValidateRecordParameter("Id", req.Id)).IsTrue() + .Because("FrontPrimaryRequest uses AllowNull = true to preserve the 'clear primary' semantics."); + } + + [Test] + public async Task FrontPrimaryRequest_JsonWithZeroId_FailsValidationViaParameterAttribute() + { + var opts = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; + var req = JsonSerializer.Deserialize("{\"id\":0}", opts)!; + + await Assert.That(req.Id).IsEqualTo(new AlterId(0)); + await Assert.That(ValidateRecordParameter("Id", req.Id)).IsFalse() + .Because("AllowNull only unlocks null; explicit 0 still trips the range check."); + } + + // Fails fast if the attribute drifts back onto the property (would surface as 500 + // from ThrowIfRecordTypeHasValidationOnProperties in an integration test). + [Test] + public async Task JournalAlterRequest_ParameterCarriesValidAlterIdAttribute() + => await Assert.That(GetParameterAttribute("AlterId")).IsNotNull(); + + [Test] + public async Task TagAlterRequest_ParameterCarriesValidAlterIdAttribute() + => await Assert.That(GetParameterAttribute("AlterId")).IsNotNull(); + + // Mirrors MVC's parameter-binding validator: attribute off the constructor parameter, + // not the compiler-generated property. Missing attribute → false (loud signal). + private static bool ValidateRecordParameter(string parameterName, object? value) + { + var attr = GetParameterAttribute(parameterName); + if (attr is null) + { + return false; + } + + var ctx = new ValidationContext(new object()) + { + DisplayName = parameterName, + MemberName = parameterName + }; + return attr.GetValidationResult(value, ctx) == ValidationResult.Success; + } + + private static ValidAlterIdAttribute? GetParameterAttribute(string parameterName) + { + var ctor = typeof(T).GetConstructors().First(); + var parameter = ctor.GetParameters().First(p => p.Name == parameterName); + return (ValidAlterIdAttribute?)parameter + .GetCustomAttributes(typeof(ValidAlterIdAttribute), inherit: false) + .FirstOrDefault(); + } +} diff --git a/tests/unit/Interfold.Auth.UnitTests/Auth/JwtEs256ValidatorTests.cs b/tests/unit/Interfold.Auth.UnitTests/Auth/JwtEs256ValidatorTests.cs new file mode 100644 index 00000000..9680c5bc --- /dev/null +++ b/tests/unit/Interfold.Auth.UnitTests/Auth/JwtEs256ValidatorTests.cs @@ -0,0 +1,130 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Auth.Api.Auth; + +namespace Interfold.Api.UnitTests.Auth; + +public sealed class JwtEs256ValidatorTests +{ + private static (string Token, string PrivateKeyPem, string PublicKeyPem) GenerateToken( + string sub = "user123", + string aud = "octocon", + long? exp = null, + bool modifySignature = false, + string? alg = "ES256") + { + using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256); + var privateKeyPem = ecdsa.ExportPkcs8PrivateKeyPem(); + var publicKeyPem = ecdsa.ExportSubjectPublicKeyInfoPem(); + + var headerObj = alg != null ? new { alg } : null; + var header = headerObj != null ? JsonSerializer.Serialize(headerObj) : "{}"; + + var expValue = exp ?? DateTimeOffset.UtcNow.AddMinutes(10).ToUnixTimeSeconds(); + var payload = JsonSerializer.Serialize(new { sub, aud, exp = expValue }); + + var headerB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(header)).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + var payloadB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(payload)).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + var signingInput = headerB64 + "." + payloadB64; + + var signature = ecdsa.SignData(Encoding.UTF8.GetBytes(signingInput), HashAlgorithmName.SHA256, DSASignatureFormat.IeeeP1363FixedFieldConcatenation); + if (modifySignature) + { + signature[0] ^= 0xFF; + } + + var signatureB64 = Convert.ToBase64String(signature).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + var token = signingInput + "." + signatureB64; + + return (token, privateKeyPem, publicKeyPem); + } + + [Test] + public async Task ValidateAsync_ValidToken_ReturnsSuccess() + { + var (token, _, publicKeyPem) = GenerateToken(); + + var result = await JwtEs256Validator.ValidateAsync(token, publicKeyPem, "octocon"); + + await Assert.That(result.IsValid).IsTrue(); + await Assert.That(result.ErrorMessage).IsNull(); + await Assert.That(result.Principal).IsNotNull(); + await Assert.That(result.Principal!.Identity!.Name).IsEqualTo("user123"); + } + + [Test] + public async Task ValidateAsync_ExpiredToken_ReturnsFailure() + { + var expiredTime = DateTimeOffset.UtcNow.AddMinutes(-5).ToUnixTimeSeconds(); + var (token, _, publicKeyPem) = GenerateToken(exp: expiredTime); + + var result = await JwtEs256Validator.ValidateAsync(token, publicKeyPem, "octocon"); + + await Assert.That(result.IsValid).IsFalse(); + await Assert.That(result.ErrorMessage).Contains("expired"); + } + + [Test] + public async Task ValidateAsync_AudienceMismatch_ReturnsFailure() + { + var (token, _, publicKeyPem) = GenerateToken(aud: "wrong-audience"); + + var result = await JwtEs256Validator.ValidateAsync(token, publicKeyPem, "octocon"); + + await Assert.That(result.IsValid).IsFalse(); + await Assert.That(result.ErrorMessage).Contains("Audience mismatch"); + } + + [Test] + public async Task ValidateAsync_InvalidSignature_ReturnsFailure() + { + var (token, _, publicKeyPem) = GenerateToken(modifySignature: true); + + var result = await JwtEs256Validator.ValidateAsync(token, publicKeyPem, "octocon"); + + await Assert.That(result.IsValid).IsFalse(); + await Assert.That(result.ErrorMessage).Contains("Invalid JWT signature"); + } + + [Test] + public async Task ValidateAsync_NonEs256Algorithm_ReturnsFailure() + { + var (token, _, publicKeyPem) = GenerateToken(alg: "RS256"); + + var result = await JwtEs256Validator.ValidateAsync(token, publicKeyPem, "octocon"); + + await Assert.That(result.IsValid).IsFalse(); + await Assert.That(result.ErrorMessage).Contains("Only ES256 is accepted"); + } + + [Test] + public async Task ValidateAsync_PemNormalization_AcceptsVariousNewlines() + { + var (token, _, publicKeyPem) = GenerateToken(); + var messyPem = publicKeyPem.Replace("\n", "\\r\\n"); + + var result = await JwtEs256Validator.ValidateAsync(token, messyPem, "octocon"); + + await Assert.That(result.IsValid).IsTrue(); + } + + [Test] + public async Task ValidateSignature_ValidSignature_ReturnsToken() + { + var (token, _, publicKeyPem) = GenerateToken(); + + var tokenObj = JwtEs256Validator.ValidateSignature(token, [publicKeyPem], useJsonWebToken: true); + + await Assert.That(tokenObj).IsNotNull(); + } + + [Test] + public async Task ValidateSignature_InvalidSignature_Throws() + { + var (token, _, publicKeyPem) = GenerateToken(modifySignature: true); + + await Assert.That(() => JwtEs256Validator.ValidateSignature(token, [publicKeyPem], useJsonWebToken: true)) + .Throws(); + } +} diff --git a/tests/unit/Interfold.Auth.UnitTests/Interfold.Auth.UnitTests.csproj b/tests/unit/Interfold.Auth.UnitTests/Interfold.Auth.UnitTests.csproj new file mode 100644 index 00000000..51c5727f --- /dev/null +++ b/tests/unit/Interfold.Auth.UnitTests/Interfold.Auth.UnitTests.csproj @@ -0,0 +1,24 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Auth.UnitTests/Options/AuthenticationSecretsPostConfigureTests.cs b/tests/unit/Interfold.Auth.UnitTests/Options/AuthenticationSecretsPostConfigureTests.cs new file mode 100644 index 00000000..ad2beb9f --- /dev/null +++ b/tests/unit/Interfold.Auth.UnitTests/Options/AuthenticationSecretsPostConfigureTests.cs @@ -0,0 +1,153 @@ +using System.Security.Cryptography; +using Interfold.Api.UnitTests.Support; +using Interfold.Auth.Api.Services.Secrets; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Interfold.Api.UnitTests.Options; + +// Contract for AuthenticationSecretsPostConfigure: seven auth rows from the snapshot +// flow onto AuthenticationConfiguration; a missing mandatory row surfaces as a +// boot-time OptionsValidationException with the offending field named. Pure-C# (no +// host, no ISecretsStore, no DI hosting stack) via the SecretsSnapshotMock helper. +public sealed class AuthenticationSecretsPostConfigureTests +{ + // Deterministic PEMs generated once per run; cheap enough that we don't cache + // across cases, expensive enough that we don't want them per-assertion. + private static readonly (string PrivatePem, string PublicPem) Rsa = GenerateRsaKeypair(); + private static readonly (string PrivatePem, string PublicPem) Es = GenerateEs256Keypair(); + + [Test] + public async Task PostConfigure_SnapshotHasAllRows_PatchesAllFields() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.OAuthGoogleClientSecret, "google-secret") + .With(SecretsStoreKeys.OAuthDiscordClientSecret, "discord-secret") + .With(SecretsStoreKeys.OAuthAppleClientSecret, "apple-secret") + .With(SecretsStoreKeys.EncryptionPepper, "test-pepper") + .With(SecretsStoreKeys.AuthDeepLinkSecret, "test-deep-link") + .With(SecretsStoreKeys.AuthJwtRsa256PrivatePem, Rsa.PrivatePem) + .With(SecretsStoreKeys.AuthJwtEs256PrivatePem, Es.PrivatePem) + .ApplyPostConfigure(); + + using (Assert.Multiple()) + { + await Assert.That(options.GoogleOAuthClientSecret).IsEqualTo("google-secret") + .Because("The Google OAuth client secret row must land on the matching nullable field so /oauth/google callbacks can complete the token exchange."); + await Assert.That(options.DiscordOAuthClientSecret).IsEqualTo("discord-secret") + .Because("Discord OAuth client secret parity — every OAuth provider needs its secret patched from the same code path."); + await Assert.That(options.AppleOAuthClientSecret).IsEqualTo("apple-secret") + .Because("Apple OAuth client secret parity."); + await Assert.That(options.EncryptionPepper).IsEqualTo("test-pepper") + .Because("The encryption pepper must move from snapshot to options; downstream KDFs read it via IOptionsMonitor at request time."); + await Assert.That(options.DeepLinkSecret).IsEqualTo("test-deep-link") + .Because("Deep-link HMAC secret must survive PostConfigure so phase-F token exchange keeps working."); + await Assert.That(options.Rsa256PrivateKey).IsEqualTo(Rsa.PrivatePem) + .Because("RSA-2048 private PEM must be assigned verbatim so PatchRsa256 can derive the SPKI public half."); + await Assert.That(options.Rsa256PublicKey).IsEqualTo(Rsa.PublicPem) + .Because("PatchRsa256 derives the SPKI public half in place — the JWKS endpoint reads this property."); + await Assert.That(options.JwtEs256PrivateKeyPem).IsEqualTo(Es.PrivatePem) + .Because("ES256 signing PEM must be assigned so AuthHelper.CreateToken can mint fresh tokens."); + await Assert.That(options.JwtEs256VerificationKeyPems) + .IsNotNull() + .And + .HasSingleItem() + .Because("PatchEs256 seeds a single-element verification array from the private PEM (ECDsa.ImportFromPem extracts the public half at verify time)."); + } + } + + // Framework spec allows re-resolution on config reload; two PostConfigure passes + // must not duplicate the ES256 verification-key entry. + [Test] + public async Task PostConfigure_IdempotentAcrossResolves() + { + var snapshot = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.EncryptionPepper, "pepper") + .With(SecretsStoreKeys.AuthDeepLinkSecret, "dl") + .With(SecretsStoreKeys.AuthJwtRsa256PrivatePem, Rsa.PrivatePem) + .With(SecretsStoreKeys.AuthJwtEs256PrivatePem, Es.PrivatePem); + var patcher = new AuthenticationSecretsPostConfigure(snapshot.Object); + var options = new AuthenticationConfiguration(); + + patcher.PostConfigure(Microsoft.Extensions.Options.Options.DefaultName, options); + patcher.PostConfigure(Microsoft.Extensions.Options.Options.DefaultName, options); + + await Assert.That(options.JwtEs256VerificationKeyPems) + .IsNotNull() + .And + .HasSingleItem() + .Because("Two PostConfigure passes must leave the verification-key array with a single entry — re-applying the same PEM would grow the array unboundedly on OptionsMonitor.OnChange, breaking ES256 signature validation ordering."); + } + + [Test] + public async Task PostConfigure_MissingMandatoryRow_TripsValidation() + { + var services = new ServiceCollection(); + var snapshot = SecretsSnapshotMock.Empty(); + services.AddSingleton(snapshot.Object); + services.AddSingleton, AuthenticationSecretsPostConfigure>(); + services.AddOptions() + .Configure(_ => { }) + .ValidateDataAnnotations(); + + await using var provider = services.BuildServiceProvider(); + var monitor = provider.GetRequiredService>(); + + var ex = Assert.Throws(() => _ = monitor.CurrentValue); + await Assert.That(ex!.Failures) + .Contains(f => f.Contains("EncryptionPepper", StringComparison.Ordinal)) + .Because("The DataAnnotations validator must call out EncryptionPepper by name — operators need the field name in logs to find the missing internal.secrets row without diffing configuration."); + } + + // Separate from the pepper case because KDF vs JWT are distinct downstream systems. + [Test] + public async Task PostConfigure_MissingEs256Pem_TripsValidation() + { + var services = new ServiceCollection(); + // Populate everything except ES256 so the validator can only blame that field. + var snapshot = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.EncryptionPepper, "pepper") + .With(SecretsStoreKeys.AuthDeepLinkSecret, "dl") + .With(SecretsStoreKeys.AuthJwtRsa256PrivatePem, Rsa.PrivatePem); + services.AddSingleton(snapshot.Object); + services.AddSingleton, AuthenticationSecretsPostConfigure>(); + services.AddOptions() + .Configure(_ => { }) + .ValidateDataAnnotations(); + + await using var provider = services.BuildServiceProvider(); + var monitor = provider.GetRequiredService>(); + + var ex = Assert.Throws(() => _ = monitor.CurrentValue); + await Assert.That(ex!.Failures) + .Contains(f => f.Contains("JwtEs256PrivateKeyPem", StringComparison.Ordinal)) + .Because("A missing ES256 PEM must fail validation naming the field so the operator connects the log message to the internal.secrets:auth:jwt_es256_private_pem row."); + } + + [Test] + public async Task PostConfigure_NamedInstance_LeavesUntouched() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.EncryptionPepper, "leaked-if-named") + .ApplyPostConfigure(name: "other-name"); + + await Assert.That(options.EncryptionPepper).IsEqualTo(string.Empty) + .Because("PostConfigure guards on Options.DefaultName so secrets never bleed into an unrelated named options bucket."); + } + + private static (string PrivatePem, string PublicPem) GenerateRsaKeypair() + { + using var rsa = RSA.Create(2048); + return (rsa.ExportPkcs8PrivateKeyPem(), rsa.ExportSubjectPublicKeyInfoPem()); + } + + private static (string PrivatePem, string PublicPem) GenerateEs256Keypair() + { + using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256); + return (ecdsa.ExportECPrivateKeyPem(), ecdsa.ExportSubjectPublicKeyInfoPem()); + } + +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/BackupCommandBuildingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/BackupCommandBuildingTests.cs new file mode 100644 index 00000000..f53a118d --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/BackupCommandBuildingTests.cs @@ -0,0 +1,232 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Asserts the argv shape hands to docker compose for both the +/// pg_dump and nodetool snapshot pipelines. These tests don't exec anything — they just +/// pin the argv contract so a future refactor that swaps argument order or adds a flag +/// catches the change at unit speed rather than in the DinD integration suite. +/// +public sealed class BackupCommandBuildingTests +{ + [Test] + public async Task PostgresDumpArgsUseAdminUserAndDatabase() + { + var args = BackupPhase.BuildPostgresDumpArgs( + composeFile: "/srv/deploy/docker-compose.yaml", + adminUser: "interfold_admin", + database: "interfold"); + + // Order is load-bearing: `compose` must be the first arg so docker dispatches to the + // compose plugin; `-f` must come before the file path; `exec -T msg-db` must precede + // pg_dump and its flags. Asserting the full sequence catches any silent re-ordering. + await Assert.That(args).IsEquivalentTo(new[] + { + "compose", "-f", "/srv/deploy/docker-compose.yaml", + "exec", "-T", + "--env", Interfold.Bootstrapper.Util.DatabaseArchiveStreamer.PgPasswordEnvVar, + "msg-db", + "pg_dump", + "-U", "interfold_admin", + "-d", "interfold", + "-Fc", + }); + } + + [Test] + public async Task PostgresDumpArgsDoNotContainPlaintextPassword() + { + // The password lands on the exec'd process via the PGPASSWORD env var (set elsewhere) + // rather than the argv. Argv ends up in `ps` output and audit logs; passwords must + // never appear there. We just assert no recognisable secret-shaped string. + var args = BackupPhase.BuildPostgresDumpArgs( + composeFile: "/srv/deploy/docker-compose.yaml", + adminUser: "interfold_admin", + database: "interfold"); + + await Assert.That(args).DoesNotContain($"{Interfold.Bootstrapper.Util.DatabaseArchiveStreamer.PgPasswordEnvVar}=anything"); + // The literal "PGPASSWORD" appears as the env-var name (no `=value` suffix); that's + // safe — only the value side leaks to ps. + foreach (var arg in args) + { + await Assert.That(arg).DoesNotContain("password"); + await Assert.That(arg).DoesNotContain("secret"); + } + } + + [Test] + public async Task ScyllaSnapshotArgsTargetCorrectService() + { + var (snap, clear) = BackupPhase.BuildScyllaSnapshotArgs( + composeFile: "/srv/deploy/docker-compose.yaml", + service: "scylla", + dataPath: ContainerMountPaths.ScyllaData, + tag: "interfold-backup-20260301-120000"); + + await Assert.That(snap).IsEquivalentTo(new[] + { + "compose", "-f", "/srv/deploy/docker-compose.yaml", + "exec", "-T", "scylla", + "nodetool", "snapshot", "-t", "interfold-backup-20260301-120000", + }); + + // The middle "resolve container id" step used to be part of this tuple; it now + // lives at the call site via DockerCompose.PsAsync (see R2-C11). The archive + // itself is still streamed out of the container by the host-side `docker cp` + // step (see BuildContainerCpArgs) because the scylladb/scylla image ships no + // `tar` binary — an in-container tar exits 127. + await Assert.That(clear).IsEquivalentTo(new[] + { + "compose", "-f", "/srv/deploy/docker-compose.yaml", + "exec", "-T", "scylla", + "nodetool", "clearsnapshot", "-t", "interfold-backup-20260301-120000", + }); + } + + [Test] + public async Task BuildContainerCpArgsStreamsPathToStdout() + { + // `docker cp : -` is a daemon-level operation: it does not require any + // binary inside the container. Passing `-` as the destination emits a raw tar + // archive of the source path's contents on stdout, which BackupPhase then wraps + // in a host-side GZipStream to produce the final .tar.gz on disk. + var args = BackupPhase.BuildContainerCpArgs( + containerId: "63fe606f0e95", + dataPath: ContainerMountPaths.ScyllaData); + + await Assert.That(args).IsEquivalentTo(new[] + { + "cp", $"63fe606f0e95:{ContainerMountPaths.ScyllaData}", "-", + }); + } + + [Test] + public async Task BuildContainerCpArgsRejectsBlankInputs() + { + // Defensive: the caller resolves the container id from `docker compose ps -q` at + // runtime — an empty result would silently produce `cp : -` which docker + // would interpret as a source-side host path, not a container path. Rejecting + // early gives a clear error at the phase boundary. + Assert.Throws( + () => BackupPhase.BuildContainerCpArgs(containerId: "", dataPath: ContainerMountPaths.ScyllaData)); + Assert.Throws( + () => BackupPhase.BuildContainerCpArgs(containerId: "abc", dataPath: " ")); + await Task.CompletedTask; + } + + [Test] + public async Task ResolveScyllaSeedSingleModeUsesScyllaService() + { + // single-mode AppHost wires up the bare "scylla" service name; matches the AppHost + // resource graph in InterfoldAppHost.Configure. + var config = TestSupport.MakeConfig(DatabaseMode.Single); + var (service, dataPath) = BackupPhase.ResolveScyllaSeed(config); + + await Assert.That(service).IsEqualTo("scylla"); + await Assert.That(dataPath).IsEqualTo(ContainerMountPaths.ScyllaData); + } + + [Test] + public async Task ResolveScyllaSeedMultiModeUsesNamSeed() + { + // multi-mode publishes 7 regional services; the seed (NAM) is the only one we + // snapshot. Multi-DC operators that want all seven captured are documented as + // out-of-scope for the bootstrapper itself. + var config = TestSupport.MakeConfig(DatabaseMode.Multi); + var (service, dataPath) = BackupPhase.ResolveScyllaSeed(config); + + await Assert.That(service).IsEqualTo("scylla-nam"); + await Assert.That(dataPath).IsEqualTo(ContainerMountPaths.ScyllaData); + } + + [Test] + public async Task ResolveScyllaSeedCassandraModeUsesCassandraService() + { + // cassandra-mode replaces Scylla entirely with a single Cassandra 5 node; the data + // directory inside the official cassandra image is /var/lib/cassandra (not /var/lib/scylla). + var config = TestSupport.MakeConfig(DatabaseMode.Cassandra); + var (service, dataPath) = BackupPhase.ResolveScyllaSeed(config); + + await Assert.That(service).IsEqualTo("cassandra"); + await Assert.That(dataPath).IsEqualTo(ContainerMountPaths.CassandraData); + } + + [Test] + public async Task ResolveBackupRootPrefersCliOverride() + { + // Operator escape hatch wins over both config and the default. Path is normalised to + // an absolute one because BackupPhase later wraps it in Directory.CreateDirectory + + // EnumerateFiles, and both behave erratically with relative paths under a systemd + // unit's unpredictable CWD. + var options = TestSupport.MakeOptions( + command: BootstrapCommand.Backup, + backupDirOverride: "/srv/backups"); + + var config = TestSupport.MakeConfig(tweak: c => c.Backup.Directory = "/var/never-seen"); + var resolved = BackupPhase.ResolveBackupRoot(options, config); + + // Path.GetFullPath normalises to the platform's separator style; just check it ends + // with the expected suffix to keep this portable across Windows / Linux runners. + await Assert.That(resolved.Replace('\\', '/')) + .Contains("/srv/backups"); + } + + [Test] + public async Task ResolveBackupRootFallsBackToConfig() + { + var options = TestSupport.MakeOptions(command: BootstrapCommand.Backup); + + var config = TestSupport.MakeConfig(tweak: c => c.Backup.Directory = "/var/backups/interfold"); + var resolved = BackupPhase.ResolveBackupRoot(options, config); + + await Assert.That(resolved.Replace('\\', '/')) + .Contains("/var/backups/interfold"); + } + + [Test] + public async Task ResolveBackupRootDefaultsToOutputDirSubfolder() + { + // Neither CLI nor config supplied → fall back to {outputDir}/backups. This is the + // default path that 99% of operators will hit; the assertion confirms the join is + // exactly "backups" (no typos, no leading slash). + var outputDir = Path.GetFullPath("./deploy"); + var options = TestSupport.MakeOptions( + command: BootstrapCommand.Backup, + outputDir: outputDir); + + var config = TestSupport.MakeConfig(); + var resolved = BackupPhase.ResolveBackupRoot(options, config); + + await Assert.That(resolved).IsEqualTo(Path.Combine(outputDir, "backups")); + } + + [Test] + public async Task BuildArchiveFileNameProducesExpectedExtensions() + { + // Postgres uses pg_dump's custom binary format (.dump) restorable with pg_restore; + // Scylla uses gzipped tar of the nodetool snapshot tree. The extensions are part of + // the documented backup layout that operators rely on for ad-hoc tooling — pinning + // them here so a refactor can't quietly switch to .pgdump or .tgz. + await Assert.That(BackupPhase.BuildArchiveFileName(BackupDatabaseComponent.Postgres, "20260301-120000")) + .IsEqualTo("20260301-120000.dump"); + await Assert.That(BackupPhase.BuildArchiveFileName(BackupDatabaseComponent.Scylla, "20260301-120000")) + .IsEqualTo("20260301-120000.tar.gz"); + } + + [Test] + public async Task ComponentParsing_RejectsUnknownAndDefaultsToAll() + { + // The CLI-boundary parse is what shields BuildArchiveFileName from meaningless + // component values now that the helper takes the enum: unknown strings return + // null (the phase surfaces its legacy --component error) and absent values keep + // the historical "all" default. + await Assert.That(BackupDatabaseComponentExtensions.TryParse("redis")).IsNull(); + await Assert.That(BackupDatabaseComponentExtensions.TryParse(null)).IsEqualTo(BackupDatabaseComponent.All); + await Assert.That(BackupDatabaseComponentExtensions.TryParse("POSTGRES")).IsEqualTo(BackupDatabaseComponent.Postgres); + await Assert.That(BackupDatabaseComponentExtensions.TryParse("scylla")).IsEqualTo(BackupDatabaseComponent.Scylla); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/BackupRetentionTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/BackupRetentionTests.cs new file mode 100644 index 00000000..fa9b2f44 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/BackupRetentionTests.cs @@ -0,0 +1,144 @@ +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Pure-logic tests for . The function takes an unordered +/// set of s + a retain count and returns the files to delete (oldest by +/// mtime, oldest-first). Every test below stages files on disk in a temp directory rather +/// than mocking FileInfo, since FileInfo's mtime fields are populated from the live +/// filesystem and there's no public seam to fake them. +/// +public sealed class BackupRetentionTests +{ + /// + /// Spins up a fresh temp dir, writes placeholder files with + /// distinct, monotonic LastWriteTimeUtc stamps (oldest first), and returns them in the + /// order created. Callers can shuffle if they need to exercise the orderer. + /// + private static List StageFiles(string dir, int count, string ext = ".dump") + { + var baseTime = DateTime.UtcNow.AddDays(-30); + var files = new List(count); + for (var i = 0; i < count; i++) + { + var path = Path.Combine(dir, $"file-{i:D4}{ext}"); + File.WriteAllText(path, $"content-{i}"); + // Spread the mtimes by one hour so the ordering is unambiguous regardless of + // OS-level clock resolution (NTFS is 100ns, ext4 is ns, but APFS is 1ns — + // a 1-hour spacing wipes out any platform-specific quirks). + File.SetLastWriteTimeUtc(path, baseTime.AddHours(i)); + files.Add(new FileInfo(path)); + } + return files; + } + + [Test] + public async Task EmptyInputReturnsEmpty() + { + // No files means nothing to prune, regardless of retain count. Important for the + // first-ever backup invocation, where the component subdirectory is empty. + var result = BackupRetention.Prune([], keep: 14).ToList(); + await Assert.That(result).IsEmpty(); + } + + [Test] + public async Task FewerFilesThanRetainKeepsAll() + { + // 5 files, retain 14 → prune nothing. The "ramp-up" period for a fresh deployment. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 5); + var result = BackupRetention.Prune(files, keep: 14).ToList(); + await Assert.That(result).IsEmpty(); + } + + [Test] + public async Task ExactlyRetainKeepsAll() + { + // Boundary case: exactly retainCount files. The "==" boundary should not delete + // anything — only "more than retain" triggers deletion. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 14); + var result = BackupRetention.Prune(files, keep: 14).ToList(); + await Assert.That(result).IsEmpty(); + } + + [Test] + public async Task OneOverRetainDeletesOldest() + { + // 15 files, retain 14 → delete the single oldest. Steady-state daily backup behaviour. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 15); + var result = BackupRetention.Prune(files, keep: 14).ToList(); + await Assert.That(result.Count).IsEqualTo(1); + // file-0000 has the oldest mtime per StageFiles. + await Assert.That(result[0].Name).IsEqualTo("file-0000.dump"); + } + + [Test] + public async Task ManyOverRetainDeletesOldestN() + { + // 100 files, retain 14 → delete the 86 oldest. Caps the worst-case "operator ran + // backup hourly for a week before turning on retention" cleanup. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 100); + var result = BackupRetention.Prune(files, keep: 14).ToList(); + await Assert.That(result.Count).IsEqualTo(86); + // The first 86 (oldest-first) must be the ones the helper returns. + for (var i = 0; i < 86; i++) + { + await Assert.That(result[i].Name).IsEqualTo($"file-{i:D4}.dump"); + } + } + + [Test] + public async Task ShuffledInputStillOrdersByMtime() + { + // The helper must internally sort — the caller hands us a DirectoryInfo enumeration + // whose order is filesystem-defined (NTFS is alphabetical, ext4 is hash-ordered) and + // bears no relationship to mtime. We shuffle the staged list before passing it in to + // catch any implementation that just .Take()s without sorting. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 20); + var shuffled = files.OrderBy(_ => Guid.NewGuid()).ToList(); + var result = BackupRetention.Prune(shuffled, keep: 5).ToList(); + await Assert.That(result.Count).IsEqualTo(15); + for (var i = 0; i < 15; i++) + { + await Assert.That(result[i].Name).IsEqualTo($"file-{i:D4}.dump"); + } + } + + [Test] + public async Task KeepZeroDeletesEverything() + { + // keep=0 is the "stop retaining" semantics; pruning would wipe the directory clean. + // The validator rejects 0 from operator-supplied config (RetainCount: 1..1000), but + // the pure helper still accepts it — defence in depth lets callers reuse Prune in + // future "clean up before resnapshot" flows without re-validating. + using var scratch = TestSupport.NewScratchDir("interfold-retention"); + var files = StageFiles(scratch.Path, 3); + var result = BackupRetention.Prune(files, keep: 0).ToList(); + await Assert.That(result.Count).IsEqualTo(3); + } + + [Test] + public async Task NegativeKeepThrows() + { + // Negative retain count is a programming error; surface it as ArgumentOutOfRangeException + // rather than silently treating it as "delete everything" or "keep everything". + var ex = Assert.Throws( + () => BackupRetention.Prune([], keep: -1).ToList()); + await Assert.That(ex.ParamName).IsEqualTo("keep"); + } + + [Test] + public async Task NullFilesThrows() + { + // Explicit null guard surfaces the misuse instead of throwing NullReferenceException + // inside the enumerator. Defensive but cheap. + var ex = Assert.Throws( + () => BackupRetention.Prune(null!, keep: 1).ToList()); + await Assert.That(ex.ParamName).IsEqualTo("files"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/BinarySizeGuardrailTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/BinarySizeGuardrailTests.cs new file mode 100644 index 00000000..75cd1eff --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/BinarySizeGuardrailTests.cs @@ -0,0 +1,78 @@ +using TUnit.Core.Exceptions; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Guardrail against accidental binary-size regressions. The bootstrapper is published as a +/// self-contained single-file ELF under tools/Interfold.Bootstrapper/bin/Release/net10.0/linux-x64/publish; +/// trimming + PublishReadyToRun=false currently keep it well under 75 MiB. If a future +/// change accidentally pulls in a heavy dependency the size will balloon and this test fails +/// before the regression ships. +/// +/// Behaviour when the published artifact is absent: +/// +/// Locally (CI env var not set): the test is SKIPPED so contributors aren't forced to +/// publish before running unit tests. +/// In CI (CI=true): the test FAILS. The CI workflow is responsible for publishing +/// the bootstrapper before the test step runs — see Dockerfile.bootstrapper and +/// the "Tar.gz binaries + stage linux-x64 for binary-size guardrail" step in +/// .github/workflows/ci-cd.yml. A silent skip in CI would mask a regression in +/// either of those. +/// +/// +public sealed class BinarySizeGuardrailTests +{ + /// Hard upper bound. Current binary size sits well below this. + private const long MaxBytes = 75L * 1024 * 1024; + + [Test] + public async Task PublishedLinuxX64BinaryIsBelowThreshold() + { + var path = LocatePublishedBinary(); + if (path is null || !File.Exists(path)) + { + var message = + $"Published linux-x64 binary not found at expected path '{path ?? ""}'. " + + "Run `dotnet publish tools/Interfold.Bootstrapper -c Release -r linux-x64 " + + "/p:PublishProfile=linux-x64` (or build via Dockerfile.bootstrapper) to generate it."; + + if (TestSupport.IsRunningInCi) + { + // CI is contractually obligated to publish + stage the binary before this + // step runs (see ci-cd.yml). Skipping here would let workflow regressions + // ride into main with a green build, so we fail loudly instead. + throw new InvalidOperationException( + "CI requires the published linux-x64 bootstrapper binary to be present. " + message); + } + + throw new SkipTestException(message); + } + + var size = new FileInfo(path).Length; + await Assert.That(size).IsLessThan(MaxBytes) + .Because($"binary at {path} is {size / 1024.0 / 1024.0:F1} MiB " + + $"(limit {MaxBytes / 1024.0 / 1024.0:F0} MiB)"); + } + + /// + /// Walks up to the repo root (identified by Interfold.slnx) and returns the published + /// binary path under tools/Interfold.Bootstrapper/bin/... as specified by the linux-x64 + /// publish profile. The file name is interfold-bootstrap per the assembly name. + /// + private static string? LocatePublishedBinary() + { + var repoRoot = TestSupport.LocateRepoRoot(); + if (repoRoot is null) return null; + + return Path.Combine( + repoRoot, + "tools", + "Interfold.Bootstrapper", + "bin", + "Release", + "net10.0", + "linux-x64", + "publish", + "interfold-bootstrap"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/CassandraImagePhaseTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/CassandraImagePhaseTests.cs new file mode 100644 index 00000000..db92cc5c --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/CassandraImagePhaseTests.cs @@ -0,0 +1,24 @@ +using Interfold.Bootstrapper.Phases; +using Interfold.Bootstrapper.Util; + +namespace Interfold.Bootstrapper.UnitTests; + +public sealed class CassandraImagePhaseTests +{ + [Test] + public async Task BuildDockerBuildArgsUsesStdinDockerfileAndLocalTag() + { + var args = CassandraImagePhase.BuildDockerBuildArgs("/tmp/ctx"); + await Assert.That(args).IsEquivalentTo( + ["build", "-t", CassandraImagePhase.LocalImageTag, "-f", "-", "/tmp/ctx"]); + } + + [Test] + public async Task EmbeddedCassandraDockerfileOpensFromSupportResources() + { + using var stream = EmbeddedSupportFiles.Open(EmbeddedSupportFiles.CassandraDockerfileRelative); + using var reader = new StreamReader(stream); + var text = await reader.ReadToEndAsync(); + await Assert.That(text).Contains("FROM cassandra:5"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/CertificateGenerationTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/CertificateGenerationTests.cs new file mode 100644 index 00000000..ecf65916 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/CertificateGenerationTests.cs @@ -0,0 +1,503 @@ +using System.Formats.Asn1; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text.RegularExpressions; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// Real X.509 work (RSA root CA + leaf → PEM/PFX), fast enough to unit test. +public sealed class CertificateGenerationTests +{ + private static BootstrapOptions OptionsFor(string outputDir) => TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: outputDir, + skipPrereqs: true, + nonInteractive: true); + + // trustStoreInstall=false keeps unit tests hermetic; true would shell to + // update-ca-certificates and trip UnauthorizedAccessException on unprivileged CI. + // Trust-store install is covered by the Docker integration tests. + private static (BootstrapConfig Config, GeneratedSecrets Secrets) MakeInputs( + string? rootCaName = null, + int? certYears = null, + IList? hosts = null, + bool trustStoreInstall = false) + { + var config = new BootstrapConfig + { + Deployment = + { + RootCaName = rootCaName ?? "Interfold Root CA", + CertYears = certYears ?? 5, + Hosts = hosts is null ? ["api.example.com"] : [.. hosts], + TrustStoreInstall = trustStoreInstall, + }, + }; + return (config, SecretsPhase.Generate()); + } + + + private static X509Certificate2 LoadCert(string path) => X509CertificateLoader.LoadCertificateFromFile(path); + + /// Runs the phase against a scratch dir and returns a disposable artefact + /// bundle. Named parameters keep call sites intent-first. + private static async Task RunPhaseAsync( + string? rootCaName = null, + int? certYears = null, + IList? hosts = null, + bool trustStoreInstall = false) + { + var scratch = TestSupport.NewScratchDir("interfold-certs"); + var (config, secrets) = MakeInputs(rootCaName, certYears, hosts, trustStoreInstall); + var options = OptionsFor(scratch.Path); + var logger = new PhaseLogger(options); + await CertificatePhase.RunAsync(options, config, secrets, logger, CancellationToken.None); + return new CertPhaseArtifacts(scratch, config, secrets, options); + } + + /// + /// Disposable bundle produced by : owns the scratch directory, + /// exposes the phase inputs, and provides conveniences to avoid + /// rebuilding Path.Combine(scratch.Path, "certs", ...) at every call site. + /// + private sealed record CertPhaseArtifacts( + TestSupport.ScratchDir Scratch, + BootstrapConfig Config, + GeneratedSecrets Secrets, + BootstrapOptions Options) : IDisposable + { + public string CertsDir => Path.Combine(Scratch.Path, "certs"); + public string CertPath(string file) => Path.Combine(CertsDir, file); + public void Dispose() => Scratch.Dispose(); + } + + [Test] + public async Task CustomRootCaNameAppearsInCertSubject() + { + using var artifacts = await RunPhaseAsync(rootCaName: "Acme Trust Root"); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + await Assert.That(root.Subject).Contains("CN=Acme Trust Root"); + } + + [Test] + public async Task CustomCertYearsControlsNotAfter() + { + const int years = 12; + var before = DateTimeOffset.UtcNow; + using var artifacts = await RunPhaseAsync(certYears: years); + var after = DateTimeOffset.UtcNow; + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var notAfter = root.NotAfter.ToUniversalTime(); + + // notAfter is approximately (notBefore + years). notBefore is constructed as + // (UtcNow - 5min) at phase invocation time, so notAfter ~= before + years - small + // window. Allow a 2-minute slop on either side to keep the test stable on slow CI. + var expectedMin = before.AddYears(years).AddMinutes(-10); + var expectedMax = after.AddYears(years); + await Assert.That(notAfter).IsGreaterThanOrEqualTo(expectedMin.UtcDateTime); + await Assert.That(notAfter).IsLessThanOrEqualTo(expectedMax.UtcDateTime); + } + + [Test] + public async Task LeafIsSignedByRoot() + { + using var artifacts = await RunPhaseAsync(); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + using var leaf = LoadCert(artifacts.CertPath("leaf.crt")); + + // Build a chain anchored on the generated root and verify it. + using var chain = new X509Chain(); + chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.CustomTrustStore.Add(root); + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + // The leaf is self-signed by our private root CA; the OS trust store has no opinion on it. + chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag; + + var built = chain.Build(leaf); + // Surface the status flags in the failure message if the build fails — useful for diagnosing + // cert-extension regressions (e.g. someone removes the AKI extension). + var statuses = chain.ChainStatus.Length == 0 + ? "" + : string.Join(", ", chain.ChainStatus.Select(s => $"{s.Status}: {s.StatusInformation.Trim()}")); + await Assert.That(built).IsTrue().Because($"leaf->root chain build failed: {statuses}"); + + // Strand 2: leaf SAN is inside the root's permittedSubtrees by construction, so + // none of the NameConstraints status flags should fire. Asserting their absence + // is the cheap canary for regressions in BuildNameConstraintsExtension (wrong + // OID, missing IMPLICIT tag, off-by-one in length prefixes, etc.) — those would + // typically surface as InvalidNameConstraints / HasNotSupportedNameConstraint + // even on a within-permitted leaf. + var ncFlagFired = chain.ChainStatus.Any(s => + s.Status == X509ChainStatusFlags.HasNotPermittedNameConstraint + || s.Status == X509ChainStatusFlags.HasExcludedNameConstraint + || s.Status == X509ChainStatusFlags.InvalidNameConstraints + || s.Status == X509ChainStatusFlags.HasNotSupportedNameConstraint); + await Assert.That(ncFlagFired).IsFalse() + .Because($"leaf is within permittedSubtrees; no NameConstraints flag should fire. statuses: {statuses}"); + } + + [Test] + public async Task MultipleHostsBecomeMultipleSans() + { + using var artifacts = await RunPhaseAsync(hosts: + ["api.example.com", "admin.example.com", "www.example.com"]); + + using var leaf = LoadCert(artifacts.CertPath("leaf.crt")); + // SAN extension OID: 2.5.29.17. + var san = leaf.Extensions.FirstOrDefault(e => e.Oid?.Value == "2.5.29.17"); + await Assert.That(san).IsNotNull(); + + // Format() returns a human-readable representation that includes each DNS name. + var sanText = san!.Format(multiLine: true); + await Assert.That(sanText).Contains("api.example.com"); + await Assert.That(sanText).Contains("admin.example.com"); + await Assert.That(sanText).Contains("www.example.com"); + } + + [Test] + public async Task TrustStoreInstallFalseProducesArtifactsWithoutErroring() + { + // With trustStoreInstall=false the phase must still produce a full set of cert artifacts + // (root/leaf .crt, leaf.key, leaf.pfx) — it just skips the system trust-store install + // step. This unit test covers the branch; the integration tests verify that no anchors + // appear under /usr/local/share/ca-certificates on Linux. + using var artifacts = await RunPhaseAsync(trustStoreInstall: false); + + // The artifacts must still exist — only the trust-store install is gated on the flag. + await Assert.That(File.Exists(artifacts.CertPath("rootCA.crt"))).IsTrue(); + await Assert.That(File.Exists(artifacts.CertPath("leaf.crt"))).IsTrue(); + await Assert.That(File.Exists(artifacts.CertPath("leaf.key"))).IsTrue(); + await Assert.That(File.Exists(artifacts.CertPath("leaf.pfx"))).IsTrue(); + } + + [Test] + public async Task RootCaKeyIsModeSixHundred() + { + // Linux-only. .NET's File.WriteAllText honours umask (typically 0644), which would leave + // the CA private key world-readable. CertificatePhase explicitly chmods rootCA.key to + // 0600 after PersistAsync so a non-bootstrapper UID on the host can't read it, and the + // non-root API container UID (64198) that bind-mounts the same dir RO gets EACCES. + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return; + } + + using var artifacts = await RunPhaseAsync(); + + var rootKeyPath = artifacts.CertPath("rootCA.key"); + await Assert.That(File.Exists(rootKeyPath)).IsTrue(); + + var actualMode = File.GetUnixFileMode(rootKeyPath); + const UnixFileMode expectedMode = UnixFileMode.UserRead | UnixFileMode.UserWrite; + await Assert.That(actualMode).IsEqualTo(expectedMode) + .Because($"rootCA.key must be 0600 (User R+W only); got {actualMode}"); + } + + [Test] + public async Task RootHasCriticalNameConstraints() + { + using var artifacts = await RunPhaseAsync(hosts: ["api.example.com"]); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var nc = root.Extensions.FirstOrDefault(e => e.Oid?.Value == "2.5.29.30"); + await Assert.That(nc).IsNotNull() + .Because("Name Constraints extension (OID 2.5.29.30) must be present on the root CA"); + await Assert.That(nc!.Critical).IsTrue() + .Because("Name Constraints MUST be critical per RFC 5280 §4.2.1.10"); + } + + [Test] + public async Task PermittedSubtreesMatchHosts() + { + var hosts = new List { "api.example.com", "admin.example.com", "www.example.com" }; + using var artifacts = await RunPhaseAsync(hosts: hosts); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var nc = root.Extensions.First(e => e.Oid?.Value == "2.5.29.30"); + + var parsed = ParseDnsPermittedSubtrees(nc.RawData); + await Assert.That(parsed).IsEquivalentTo(hosts) + .Because($"permittedSubtrees must contain exactly the configured hosts; got [{string.Join(", ", parsed)}]"); + } + + [Test] + public async Task WildcardDomainsStripStarPrefix() + { + // dNSName subtree semantics (RFC 5280 §4.2.1.10) already match every host under the + // suffix, and the literal `*` character is not legal in an IA5String constraint value. + // BuildNameConstraintsExtension must therefore collapse `*.example.com` to + // `example.com` in the permittedSubtrees set. + using var artifacts = await RunPhaseAsync(hosts: ["*.example.com"]); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var nc = root.Extensions.First(e => e.Oid?.Value == "2.5.29.30"); + var parsed = ParseDnsPermittedSubtrees(nc.RawData); + + await Assert.That(parsed.Count).IsEqualTo(1); + await Assert.That(parsed[0]).IsEqualTo("example.com"); + } + + [Test] + public async Task LeafOutsideConstraintsFailsValidation() + { + // Negative test: hand-craft a leaf with a SAN outside the root's permittedSubtrees and + // confirm that chain.Build rejects it with the expected NameConstraints status flag. + // This is the load-bearing assertion that a leaked CA private key cannot mint a + // trusted cert for arbitrary hosts. + using var artifacts = await RunPhaseAsync(hosts: ["api.example.com"]); + + var rootCertPem = await File.ReadAllTextAsync(artifacts.CertPath("rootCA.crt")); + var rootKeyPem = await File.ReadAllTextAsync(artifacts.CertPath("rootCA.key")); + using var root = X509Certificate2.CreateFromPem(rootCertPem, rootKeyPem); + + using var leafKey = RSA.Create(2048); + var leafReq = new CertificateRequest( + new X500DistinguishedName("CN=evil.attacker.com"), + leafKey, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + var sanBuilder = new SubjectAlternativeNameBuilder(); + sanBuilder.AddDnsName("evil.attacker.com"); + leafReq.CertificateExtensions.Add(sanBuilder.Build()); + leafReq.CertificateExtensions.Add( + new X509BasicConstraintsExtension(false, false, 0, true)); + leafReq.CertificateExtensions.Add(new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment, true)); + leafReq.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension( + new OidCollection { new("1.3.6.1.5.5.7.3.1") }, false)); + + var serial = RandomNumberGenerator.GetBytes(16); + serial[0] &= 0x7F; + + // Clamp the leaf strictly inside the root's lifetime so a NotTimeValid status can't + // mask the NameConstraints violation we're actually testing for. + var notBefore = new DateTimeOffset(root.NotBefore.ToUniversalTime(), TimeSpan.Zero).AddMinutes(1); + var notAfter = new DateTimeOffset(root.NotAfter.ToUniversalTime(), TimeSpan.Zero).AddSeconds(-1); + using var badLeaf = leafReq.Create(root, notBefore, notAfter, serial); + + using var chain = new X509Chain(); + chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.CustomTrustStore.Add(root); + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + + var built = chain.Build(badLeaf); + var statuses = chain.ChainStatus.Length == 0 + ? "" + : string.Join(", ", chain.ChainStatus.Select(s => s.Status.ToString())); + + await Assert.That(built).IsFalse() + .Because($"chain build must reject a leaf whose SAN is outside the CA's Name Constraints; statuses: {statuses}"); + + var ncViolation = chain.ChainStatus.Any(s => + s.Status == X509ChainStatusFlags.HasNotPermittedNameConstraint + || s.Status == X509ChainStatusFlags.HasExcludedNameConstraint + || s.Status == X509ChainStatusFlags.InvalidNameConstraints + || s.Status == X509ChainStatusFlags.HasNotSupportedNameConstraint); + await Assert.That(ncViolation).IsTrue() + .Because($"chain failed but not for a Name Constraints reason; got: {statuses}"); + } + + [Test] + public async Task Sha256FingerprintFormatIsColonHexUpperCase() + { + // Pure unit test of the FormatSha256Fingerprint helper against a fixed input. SHA-256 + // of an empty byte array is the canonical empty-hash value documented in NIST FIPS + // 180-4 test vectors. The formatted output must match `openssl dgst -sha256` style + // exactly (uppercase, colon-separated) so a human comparing fingerprints sees the + // same characters in both tools. + var hash = SHA256.HashData(Array.Empty()); + var formatted = CertificatePhase.FormatSha256Fingerprint(hash); + + await Assert.That(formatted).IsEqualTo( + "E3:B0:C4:42:98:FC:1C:14:9A:FB:F4:C8:99:6F:B9:24:27:AE:41:E4:64:9B:93:4C:A4:95:99:1B:78:52:B8:55"); + } + + [Test] + public async Task FingerprintFileWrittenInColonHexUpperCase() + { + // End-to-end check on the new rootCA.sha256.txt artefact: it must exist next to + // rootCA.crt after the certs phase runs, hold the canonical colon-hex SHA-256 of the + // root cert's DER bytes, and match what `FormatSha256Fingerprint(SHA256.HashData(...))` + // would compute live. PrintTrustInfo / TrustController both depend on this format. + using var artifacts = await RunPhaseAsync(); + + var fingerprintPath = artifacts.CertPath("rootCA.sha256.txt"); + await Assert.That(File.Exists(fingerprintPath)).IsTrue() + .Because("rootCA.sha256.txt must be written alongside rootCA.crt"); + + var fingerprint = (await File.ReadAllTextAsync(fingerprintPath)).Trim(); + + // 32 hex pairs + 31 colon separators = 95 characters. + await Assert.That(fingerprint.Length).IsEqualTo(95) + .Because($"colon-hex SHA-256 must be 95 chars; got '{fingerprint}' (len={fingerprint.Length})"); + await Assert.That(Regex.IsMatch(fingerprint, "^([0-9A-F]{2}:){31}[0-9A-F]{2}$")).IsTrue() + .Because($"fingerprint must match the uppercase colon-hex pattern; got: '{fingerprint}'"); + + using var cert = LoadCert(artifacts.CertPath("rootCA.crt")); + var expected = CertificatePhase.FormatSha256Fingerprint(SHA256.HashData(cert.RawData)); + await Assert.That(fingerprint).IsEqualTo(expected) + .Because("the file contents must equal the live hash of the published root CA cert"); + } + + [Test] + public async Task LeafSanIncludesIPv4AsIpAddress() + { + // An IPv4 host on deployment.hosts must end up as an iPAddress GeneralName on the leaf's + // SAN extension. Clients hitting `https://192.168.1.42/` need that entry to validate the + // cert against the bare IP authority. + using var artifacts = await RunPhaseAsync(hosts: ["192.168.1.42"]); + + using var leaf = LoadCert(artifacts.CertPath("leaf.crt")); + var sanExt = leaf.Extensions.OfType().First(); + var ips = sanExt.EnumerateIPAddresses().Select(ip => ip.ToString()).ToList(); + await Assert.That(ips).Contains("192.168.1.42") + .Because($"leaf SAN must contain an iPAddress entry for the configured IPv4 host; got [{string.Join(", ", ips)}]"); + } + + [Test] + public async Task LeafSanIncludesIPv6AsIpAddress() + { + using var artifacts = await RunPhaseAsync(hosts: ["fe80::1234"]); + + using var leaf = LoadCert(artifacts.CertPath("leaf.crt")); + var sanExt = leaf.Extensions.OfType().First(); + var ips = sanExt.EnumerateIPAddresses().Select(ip => ip.ToString()).ToList(); + await Assert.That(ips).Contains("fe80::1234") + .Because($"leaf SAN must contain an iPAddress entry for the configured IPv6 host; got [{string.Join(", ", ips)}]"); + } + + [Test] + public async Task CidrEntrySkippedFromLeafSan() + { + // CIDR entries widen the root CA's Name Constraints scope but MUST NOT appear on the + // leaf SAN — a leaf cert can only serve a specific host. The leaf gets the DNS name; the + // CIDR only shows up in the root's permittedSubtrees (tested separately below). + using var artifacts = await RunPhaseAsync(hosts: ["api.example.com", "10.0.0.0/8"]); + + using var leaf = LoadCert(artifacts.CertPath("leaf.crt")); + var sanExt = leaf.Extensions.OfType().First(); + var dnsNames = sanExt.EnumerateDnsNames().ToList(); + var ips = sanExt.EnumerateIPAddresses().Select(ip => ip.ToString()).ToList(); + + await Assert.That(dnsNames).Contains("api.example.com"); + // The CIDR must NOT appear in either category. + await Assert.That(ips.Count).IsEqualTo(0) + .Because($"leaf SAN must not contain any iPAddress entry for a CIDR host; got [{string.Join(", ", ips)}]"); + await Assert.That(dnsNames.Any(d => d.Contains('/'))).IsFalse() + .Because($"leaf SAN must not contain any DNS entry containing '/' (CIDR shape); got [{string.Join(", ", dnsNames)}]"); + } + + [Test] + public async Task NameConstraintsEncodesIpv4Cidr() + { + // The byte layout of an iPAddress permittedSubtree is load-bearing: chain validators on + // every client check it as `address || mask`. A drift here would silently broaden or + // narrow the operator's blast-radius cap. Pin the canonical /24 form (192.168.1.0/24) + // against the expected 8 bytes: 4 addr (C0 A8 01 00) + 4 mask (FF FF FF 00). + using var artifacts = await RunPhaseAsync(hosts: ["api.example.com", "192.168.1.0/24"]); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var nc = root.Extensions.First(e => e.Oid?.Value == "2.5.29.30"); + var ipSubtrees = ParseIpPermittedSubtrees(nc.RawData); + + await Assert.That(ipSubtrees.Count).IsEqualTo(1) + .Because($"expected exactly one iPAddress permittedSubtree; got {ipSubtrees.Count}"); + await Assert.That(ipSubtrees[0]).IsEquivalentTo(new byte[] + { + 0xC0, 0xA8, 0x01, 0x00, + 0xFF, 0xFF, 0xFF, 0x00, + }); + } + + [Test] + public async Task NameConstraintsEncodesIpv6SingleHostMask() + { + // A bare IPv6 literal becomes a single-host iPAddress subtree with the all-ones /128 + // mask (16 addr bytes + 16 mask bytes). Spot-check the boundary bytes for the canonical + // form rather than asserting the full 32 bytes verbatim. + using var artifacts = await RunPhaseAsync(hosts: ["api.example.com", "fe80::1"]); + + using var root = LoadCert(artifacts.CertPath("rootCA.crt")); + var nc = root.Extensions.First(e => e.Oid?.Value == "2.5.29.30"); + var ipSubtrees = ParseIpPermittedSubtrees(nc.RawData); + + await Assert.That(ipSubtrees.Count).IsEqualTo(1); + var bytes = ipSubtrees[0]; + await Assert.That(bytes.Length).IsEqualTo(32) + .Because($"IPv6 iPAddress subtree must be 16 (addr) + 16 (mask) = 32 bytes; got {bytes.Length}"); + // fe80::1 = fe 80 00...00 01 — boundary bytes are deterministic. + await Assert.That(bytes[0]).IsEqualTo((byte)0xFE); + await Assert.That(bytes[1]).IsEqualTo((byte)0x80); + await Assert.That(bytes[15]).IsEqualTo((byte)0x01); + // All 16 mask bytes are 0xFF for a /128 single host. + for (var i = 16; i < 32; i++) + { + await Assert.That(bytes[i]).IsEqualTo((byte)0xFF) + .Because($"mask byte at index {i} must be 0xFF for a /128 subtree"); + } + } + + /// + /// Parses the DER-encoded Name Constraints extension value (OID 2.5.29.30) and returns + /// the dNSName entries inside permittedSubtrees. Skips iPAddress entries (handled by + /// ) so legacy tests that only care about DNS + /// names continue to pass on mixed-shape configs. + /// + private static List ParseDnsPermittedSubtrees(byte[] rawExtensionValue) + { + var reader = new AsnReader(rawExtensionValue, AsnEncodingRules.DER); + var nameConstraints = reader.ReadSequence(); + var permitted = nameConstraints.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); + var names = new List(); + while (permitted.HasData) + { + var subtree = permitted.ReadSequence(); + var peeked = subtree.PeekTag(); + if (peeked.TagClass == TagClass.ContextSpecific && peeked.TagValue == 2) + { + names.Add(subtree.ReadCharacterString( + UniversalTagNumber.IA5String, + new Asn1Tag(TagClass.ContextSpecific, 2))); + } + else + { + _ = subtree.ReadEncodedValue(); + } + } + return names; + } + + /// + /// Sibling of that pulls out the iPAddress entries + /// (tag [7] IMPLICIT OCTET STRING; payload is address || mask per RFC 5280 §4.2.1.10). DNS + /// entries are skipped. Used by the IP / CIDR Name Constraints tests below. + /// + private static List ParseIpPermittedSubtrees(byte[] rawExtensionValue) + { + var reader = new AsnReader(rawExtensionValue, AsnEncodingRules.DER); + var nameConstraints = reader.ReadSequence(); + var permitted = nameConstraints.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); + var entries = new List(); + while (permitted.HasData) + { + var subtree = permitted.ReadSequence(); + var peeked = subtree.PeekTag(); + if (peeked.TagClass == TagClass.ContextSpecific && peeked.TagValue == 7) + { + entries.Add(subtree.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 7))); + } + else + { + _ = subtree.ReadEncodedValue(); + } + } + return entries; + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/CliParsingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/CliParsingTests.cs new file mode 100644 index 00000000..fb463f48 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/CliParsingTests.cs @@ -0,0 +1,324 @@ +using System.Diagnostics; +using System.Text; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Drives in-process so we can assert CLI parsing behaviour +/// without forking. Stdout/stderr are temporarily swapped onto capture writers for +/// the duration of each invocation; the original streams are restored in a finally so +/// other tests in the same process aren't affected. +/// +/// +/// [NotInParallel("bootstrapper-console")] matches +/// / : +/// is process-global. The capture writers are also locked — +/// TUnit's engine can still write to from another thread, and +/// an unlocked races ToString into +/// . +/// +[NotInParallel("bootstrapper-console")] +public sealed class CliParsingTests +{ + private sealed record CapturedRun(int ExitCode, string Stdout, string Stderr); + + /// Thread-safe over a . + /// is not safe for concurrent writes. + private sealed class CaptureWriter : TextWriter + { + private readonly StringBuilder _sb = new(); + private readonly object _gate = new(); + + public override Encoding Encoding => Encoding.UTF8; + + public override void Write(char value) + { + lock (_gate) _sb.Append(value); + } + + public override void Write(char[] buffer, int index, int count) + { + lock (_gate) _sb.Append(buffer, index, count); + } + + public override void Write(ReadOnlySpan value) + { + lock (_gate) _sb.Append(value); + } + + public override void Write(string? value) + { + if (value is null) return; + lock (_gate) _sb.Append(value); + } + + public override string ToString() + { + lock (_gate) return _sb.ToString(); + } + } + + /// + /// Invokes the CLI and captures stdout/stderr. The CLI is a real System.CommandLine root, so + /// this exercises argument parsing exactly as the operator's invocation would. + /// Used for tests that drive code through to (e.g. the + /// non-interactive missing-config path) — these write through + /// and so are visible to the swapped-in writers. + /// + private static async Task InvokeInProcessAsync(params string[] args) + { + var prevOut = Console.Out; + var prevErr = Console.Error; + var prevIn = Console.In; + var stdout = new CaptureWriter(); + var stderr = new CaptureWriter(); + int exit; + try + { + // TUnit0055 warns that overwriting Console writers can mask its log output; in this + // test we deliberately want to capture what the bootstrapper writes, and we restore + // the originals in the finally block, so the warning is misleading here. +#pragma warning disable TUnit0055 + Console.SetOut(stdout); + Console.SetError(stderr); + // Force "non-interactive" semantics in ConfigPhase by closing stdin — otherwise + // `Console.IsInputRedirected` may be false under the test runner and the prompt + // path would block waiting for keystrokes that never come. + Console.SetIn(new StringReader(string.Empty)); +#pragma warning restore TUnit0055 + exit = await RootCli.RunAsync(args); + } + finally + { +#pragma warning disable TUnit0055 + Console.SetOut(prevOut); + Console.SetError(prevErr); + Console.SetIn(prevIn); +#pragma warning restore TUnit0055 + } + + // Snapshot after restore so TUnit's Console interceptor is no longer writing into + // these builders. + return new CapturedRun(exit, stdout.ToString(), stderr.ToString()); + } + + /// + /// Shells out to the compiled bootstrapper. Necessary for help / parser-error tests because + /// System.CommandLine 3.x writes those to ParseResult.Configuration.Output rather than + /// the redirectable . Skips the test (via + /// ) when the bootstrapper binary isn't on + /// disk yet — `dotnet test` from the solution root will have built it as a transitive dep. + /// + private static async Task InvokeViaProcessAsync(params string[] args) + { + var path = TestSupport.BootstrapperBinaryOrSkip(); + var psi = new ProcessStartInfo + { + FileName = "dotnet", + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + psi.ArgumentList.Add(path); + foreach (var a in args) psi.ArgumentList.Add(a); + + using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start dotnet"); + proc.StandardInput.Close(); + var stdout = await proc.StandardOutput.ReadToEndAsync(); + var stderr = await proc.StandardError.ReadToEndAsync(); + await proc.WaitForExitAsync(); + return new CapturedRun(proc.ExitCode, stdout, stderr); + } + + [Test] + public async Task HelpFlagPrintsCommandList() + { + // System.CommandLine 3.x writes help text to a configured TextWriter inside the parse + // result, NOT through Console.Out — so we shell out and read the real stdout. + var run = await InvokeViaProcessAsync("--help"); + + await Assert.That(run.ExitCode).IsEqualTo(0); + var combined = run.Stdout + run.Stderr; + await Assert.That(combined).Contains("bootstrap"); + await Assert.That(combined).Contains("publish"); + await Assert.That(combined).Contains("up"); + await Assert.That(combined).Contains("rotate-secrets"); + await Assert.That(combined).Contains("rotate-certs"); + await Assert.That(combined).Contains("show-trust"); + } + + [Test] + public async Task ShowTrustReadsExistingCertWithoutRegenerating() + { + // End-to-end behavioural check: pre-stage a certs/ directory with a known rootCA.crt + // (and matching rootCA.sha256.txt), invoke `bootstrap show-trust`, and confirm the + // command (a) succeeds, (b) does NOT rewrite the cert (mtime unchanged), and + // (c) prints the recorded fingerprint exactly. This is the load-bearing assertion + // that the Orchestrator short-circuit doesn't accidentally fall through to + // CertificatePhase.RunAsync and regenerate the CA. + using var scratch = TestSupport.NewScratchDir("interfold-cli-showtrust"); + var tmpDir = scratch.Path; + var certsDir = Path.Combine(tmpDir, "certs"); + Directory.CreateDirectory(certsDir); + + using var key = System.Security.Cryptography.RSA.Create(2048); + var req = new System.Security.Cryptography.X509Certificates.CertificateRequest( + new System.Security.Cryptography.X509Certificates.X500DistinguishedName("CN=Test Root"), + key, + System.Security.Cryptography.HashAlgorithmName.SHA256, + System.Security.Cryptography.RSASignaturePadding.Pkcs1); + using var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddYears(1)); + var rootCrtPath = Path.Combine(certsDir, "rootCA.crt"); + var rootFpPath = Path.Combine(certsDir, "rootCA.sha256.txt"); + await File.WriteAllTextAsync(rootCrtPath, cert.ExportCertificatePem()); + + var expectedFingerprint = CertificatePhase.FormatSha256Fingerprint( + System.Security.Cryptography.SHA256.HashData(cert.RawData)); + await File.WriteAllTextAsync(rootFpPath, expectedFingerprint + Environment.NewLine); + + var preCertMtime = File.GetLastWriteTimeUtc(rootCrtPath); + var preFpMtime = File.GetLastWriteTimeUtc(rootFpPath); + + // Force a measurable mtime delta so an accidental rewrite would be visible even on + // file systems whose timestamp resolution is in tens of milliseconds (ext4, APFS). + await Task.Delay(100); + + var run = await InvokeInProcessAsync("show-trust", "--output-dir", tmpDir, "--non-interactive"); + + await Assert.That(run.ExitCode).IsEqualTo(0) + .Because($"show-trust must succeed on a pre-staged certs dir. stderr: {run.Stderr}"); + await Assert.That(run.Stdout + run.Stderr).Contains(expectedFingerprint) + .Because("show-trust must print the recorded fingerprint verbatim"); + await Assert.That(File.GetLastWriteTimeUtc(rootCrtPath)).IsEqualTo(preCertMtime) + .Because("show-trust must not rewrite rootCA.crt"); + await Assert.That(File.GetLastWriteTimeUtc(rootFpPath)).IsEqualTo(preFpMtime) + .Because("show-trust must not rewrite rootCA.sha256.txt"); + } + + [Test] + public async Task ShowTrustFailsWhenRootCaMissing() + { + // Diagnostic UX check: running show-trust against a directory that has no rootCA.crt + // must fail with a non-zero exit and an actionable error message that points the + // operator at `bootstrap` / `bootstrap rotate-certs`. + using var scratch = TestSupport.NewScratchDir("interfold-cli-showtrust-missing"); + var tmpDir = scratch.Path; + var run = await InvokeInProcessAsync("show-trust", "--output-dir", tmpDir, "--non-interactive"); + + await Assert.That(run.ExitCode).IsNotEqualTo(0) + .Because("show-trust must exit non-zero when rootCA.crt is missing"); + var combined = run.Stdout + run.Stderr; + await Assert.That(combined).Contains("rootCA.crt") + .Because("the error message should name the missing file so operators know what to look for"); + } + + [Test] + public async Task HelpListsShortOptions() + { + // -c short form for --config and -o short form for --output-dir must both surface in the + // help text. If a future refactor drops the aliases this catches it immediately. + var run = await InvokeViaProcessAsync("--help"); + + var combined = run.Stdout + run.Stderr; + await Assert.That(combined).Contains("-c"); + await Assert.That(combined).Contains("-o"); + } + + [Test] + public async Task UnknownCommandExitsNonZero() + { + // System.CommandLine treats an unrecognised verb as an error. The exact wording varies + // across SDK versions so we just assert on the non-zero exit code. + var run = await InvokeViaProcessAsync("this-is-definitely-not-a-command"); + + await Assert.That(run.ExitCode).IsNotEqualTo(0); + } + + [Test] + public async Task ShortOptionsMatchLongOptions() + { + // Compare two parser invocations of the `publish` command — one with --config, one + // with -c — pointing at the same nonexistent file. Both must produce equivalent + // failure behaviour because -c is the alias of --config. We can drive this in-process + // because the failure happens inside ConfigPhase (which uses Console.WriteLine). + using var scratch = TestSupport.NewScratchDir("interfold-cli-short"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "does-not-exist.json"); + + var longRun = await InvokeInProcessAsync("publish", "--config", configPath, + "--output-dir", tmpDir, "--non-interactive"); + var shortRun = await InvokeInProcessAsync("publish", "-c", configPath, + "-o", tmpDir, "--non-interactive"); + + await Assert.That(shortRun.ExitCode).IsEqualTo(longRun.ExitCode) + .Because("-c/-o aliases must produce the same exit code as --config/--output-dir"); + } + + [Test] + public async Task NonInteractiveWithoutConfigExitsNonZero() + { + // ConfigPhase aborts when --non-interactive is set and no config file is present at the + // expected path. Drive the `publish` command so the failure happens at the config phase + // rather than at prereqs (which is Linux-only). + using var scratch = TestSupport.NewScratchDir("interfold-cli-ni"); + var tmpDir = scratch.Path; + var run = await InvokeInProcessAsync("publish", + "--config", Path.Combine(tmpDir, "missing.json"), + "--output-dir", tmpDir, + "--non-interactive"); + + await Assert.That(run.ExitCode).IsNotEqualTo(0); + } + + [Test] + public async Task BootstrapHelpListsReconfigure() + { + var run = await InvokeViaProcessAsync("bootstrap", "--help"); + await Assert.That(run.ExitCode).IsEqualTo(0); + var combined = run.Stdout + run.Stderr; + await Assert.That(combined).Contains("--reconfigure") + .Because("bootstrap (and root default) own --reconfigure for editing an existing config interactively."); + } + + [Test] + public async Task PublishHelpDoesNotListReconfigure() + { + var run = await InvokeViaProcessAsync("publish", "--help"); + await Assert.That(run.ExitCode).IsEqualTo(0); + var combined = run.Stdout + run.Stderr; + await Assert.That(combined).DoesNotContain("--reconfigure") + .Because("--reconfigure is bootstrap-only; other subcommands must not accept it."); + } + + [Test] + public async Task ReconfigureWithNonInteractiveExitsNonZero() + { + using var scratch = TestSupport.NewScratchDir("interfold-cli-reconfigure-ni"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "interfold.bootstrap.json"); + var cfg = TestSupport.MakeConfig(tweak: c => + { + c.Deployment.Hosts = ["api.example.com"]; + c.Deployment.OutputDir = tmpDir; + }); + await File.WriteAllTextAsync( + configPath, + System.Text.Json.JsonSerializer.Serialize(cfg, Interfold.Bootstrapper.Configuration.BootstrapJsonContext.Default.BootstrapConfig)); + + // Redirected stdin (InvokeInProcessAsync) + --non-interactive both block --reconfigure. + var run = await InvokeInProcessAsync( + "bootstrap", + "--config", configPath, + "--output-dir", tmpDir, + "--skip-prereqs", + "--reconfigure", + "--non-interactive"); + + await Assert.That(run.ExitCode).IsNotEqualTo(0); + await Assert.That(run.Stdout + run.Stderr).Contains("--reconfigure"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ComposeImagesParseTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ComposeImagesParseTests.cs new file mode 100644 index 00000000..0932e8e3 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ComposeImagesParseTests.cs @@ -0,0 +1,159 @@ +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for , +/// , and +/// . +/// +public sealed class ComposeImagesParseTests +{ + [Test] + public async Task ParsesPsJsonArrayShape() + { + const string payload = """ + [ + {"ID":"aaa111","Image":"sha256:img1","Service":"msg-db","Name":"deploy-msg-db-1"}, + {"ID":"bbb222","Image":"sha256:img2","Service":"scylla","Name":"deploy-scylla-1"} + ] + """; + + var parsed = UpdateImagesPhase.ParseComposePsJson(payload); + + await Assert.That(parsed.Count).IsEqualTo(2); + await Assert.That(parsed.Single(r => r.Service == "msg-db").ContainerId).IsEqualTo("aaa111"); + await Assert.That(parsed.Single(r => r.Service == "scylla").ContainerId).IsEqualTo("bbb222"); + } + + [Test] + public async Task ParsesPsJsonLinesShape() + { + const string payload = """ + {"ID":"aaa111","Image":"postgres:16","Service":"msg-db","Name":"deploy-msg-db-1"} + {"ID":"bbb222","Image":"scylla:2026.1","Service":"scylla","Name":"deploy-scylla-1"} + {"ID":"ccc333","Image":"api:latest","Service":"interfold-api","Name":"deploy-interfold-api-1"} + """; + + var parsed = UpdateImagesPhase.ParseComposePsJson(payload); + + await Assert.That(parsed.Count).IsEqualTo(3); + await Assert.That(parsed.Single(r => r.Service == "interfold-api").ContainerId).IsEqualTo("ccc333"); + } + + [Test] + public async Task EmptyPsInputReturnsEmptyList() + { + var parsed = UpdateImagesPhase.ParseComposePsJson(""); + await Assert.That(parsed.Count).IsEqualTo(0); + } + + [Test] + public async Task WhitespacePsInputReturnsEmptyList() + { + var parsed = UpdateImagesPhase.ParseComposePsJson(" \n \n "); + await Assert.That(parsed.Count).IsEqualTo(0); + } + + [Test] + public async Task MalformedPsJsonLineIsSkipped() + { + const string payload = """ + {"ID":"aaa","Image":"sha256:a","Service":"msg-db"} + not json at all + {"ID":"bbb","Image":"sha256:b","Service":"scylla"} + """; + + var parsed = UpdateImagesPhase.ParseComposePsJson(payload); + + await Assert.That(parsed.Count).IsEqualTo(2); + await Assert.That(parsed.Any(r => r.Service == "msg-db")).IsTrue(); + await Assert.That(parsed.Any(r => r.Service == "scylla")).IsTrue(); + } + + [Test] + public async Task EntryMissingServiceIsIgnored() + { + const string payload = """ + {"ID":"aaa","Image":"sha256:orphan"} + {"ID":"bbb","Image":"sha256:real","Service":"real-svc"} + """; + + var parsed = UpdateImagesPhase.ParseComposePsJson(payload); + + await Assert.That(parsed.Count).IsEqualTo(1); + await Assert.That(parsed[0].Service).IsEqualTo("real-svc"); + } + + [Test] + public async Task EntryMissingContainerIdKeepsEmptyString() + { + const string payload = """ + {"Service":"weird-svc"} + """; + + var parsed = UpdateImagesPhase.ParseComposePsJson(payload); + + await Assert.That(parsed.Count).IsEqualTo(1); + await Assert.That(parsed[0].ContainerId).IsEqualTo(string.Empty); + } + + [Test] + public async Task ParseContainerImageFieldsSplitsTabSeparatedLines() + { + const string payload = """ + sha256:run1 postgres:16 + sha256:run2 interfold-cassandra:local + """; + + var parsed = UpdateImagesPhase.ParseContainerImageFields(payload); + + await Assert.That(parsed.Count).IsEqualTo(2); + await Assert.That(parsed[0].RunningImageId).IsEqualTo("sha256:run1"); + await Assert.That(parsed[0].ConfigImage).IsEqualTo("postgres:16"); + await Assert.That(parsed[1].ConfigImage).IsEqualTo("interfold-cassandra:local"); + } + + [Test] + public async Task ParseContainerImageFieldsMissingTabKeepsWholeLineAsRunningId() + { + var parsed = UpdateImagesPhase.ParseContainerImageFields("sha256:only"); + await Assert.That(parsed.Count).IsEqualTo(1); + await Assert.That(parsed[0].RunningImageId).IsEqualTo("sha256:only"); + await Assert.That(parsed[0].ConfigImage).IsEqualTo(string.Empty); + } + + [Test] + public async Task ResolveDesiredDigestPrefersInspectedTagId() + { + // Post-pull: Config.Image tag resolved to a new id even while the container still + // reports the old running ImageID — that mismatch is what triggers recreate. + var resolved = UpdateImagesPhase.ResolveDesiredDigest( + runningImageId: "sha256:old", + inspectedTagId: "sha256:new"); + + await Assert.That(resolved).IsEqualTo("sha256:new"); + } + + [Test] + public async Task ResolveDesiredDigestFallsBackToRunningWhenTagMissing() + { + // Tag inspect failed (image record gone / local-only pruned) — keep the running + // id so the diff stays well-defined instead of inventing an empty digest. + var resolved = UpdateImagesPhase.ResolveDesiredDigest( + runningImageId: "sha256:still-running", + inspectedTagId: null); + + await Assert.That(resolved).IsEqualTo("sha256:still-running"); + } + + [Test] + public async Task ResolveDesiredDigestFallsBackWhenInspectedIdEmpty() + { + var resolved = UpdateImagesPhase.ResolveDesiredDigest( + runningImageId: "sha256:running", + inspectedTagId: ""); + + await Assert.That(resolved).IsEqualTo("sha256:running"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigInteractivePromptTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigInteractivePromptTests.cs new file mode 100644 index 00000000..2f5b79a7 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigInteractivePromptTests.cs @@ -0,0 +1,1073 @@ +using System.Net; +using System.Text.RegularExpressions; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Enums; +using Spectre.Console; +using Spectre.Console.Testing; + +namespace Interfold.Bootstrapper.UnitTests; + +/// Drives Spectre SelectionPrompt<int> for the config form: 48 field +/// rows across 11 sections + trailing "Confirm and save". Headers are inert; cursor resets +/// to field 0 after each edit, so tests use absolute Navigate distances. +/// Field order: 0..6 Deployment · 7..12 Ports · 13..16 Database · 17..21 API · +/// 22..23 Cluster/telemetry · 24..25 Storage · 26..30 Performance · 31..36 OAuth · +/// 37..41 Backup · 42..46 Updates · 47 Firebase. Navigate(48) = Confirm and save. +/// [NotInParallel("bootstrapper-console")]: Spectre TestConsole + MTP's +/// NamedPipeServer race under Linux thread pressure (dotnet/runtime#58045). ~10s serialised. +/// [Retry(2)] absorbs the residual native abort (exit 134/SIGABRT) that still slips +/// through on 2-core GHA runners when the pipe race wins — the underlying runtime bug is +/// non-deterministic, and the test logic itself is race-free, so a retry is safe. +[NotInParallel("bootstrapper-console")] +[Retry(2)] +public sealed class ConfigInteractivePromptTests +{ + private const int FieldCount = 48; + + /// Sized to fit the whole 60-row form so Spectre never paginates. + private static TestConsole NewConsole() + { + var c = new TestConsole(); + c.Interactive(); + c.Profile.Height = 120; + c.Profile.Width = 130; + return c; + } + + /// Pushes N DownArrows + Enter. Field N sits N DownArrows below field 0. + private static void Navigate(TestConsole c, int downArrows) + { + for (var i = 0; i < downArrows; i++) + c.Input.PushKey(ConsoleKey.DownArrow); + c.Input.PushKey(ConsoleKey.Enter); + } + + private static void ConfirmForm(TestConsole c) => Navigate(c, FieldCount); + + /// Opens the field's editor and pushes as lines + /// (multiple answers drive a re-prompt on invalid input). + private static void EditField(TestConsole c, int fieldIndex, params string[] answers) + { + Navigate(c, fieldIndex); + foreach (var a in answers) + c.Input.PushTextWithEnter(a); + } + + /// + /// Standard wrapper — both probes return null so neither auto-default fires in tests. A + /// CI runner with a routable NIC would otherwise pre-populate Hosts and break the empty- + /// Hosts assertions. Tests exercising the auto-default call PromptForConfig directly. + /// + private static BootstrapConfig PromptWithoutDetection(IAnsiConsole console, bool maskSecrets = false) => + ConfigPhase.PromptForConfig( + console, + maskSecrets, + localAddressProbe: () => null, + hostnameProbe: () => null); + + [Test] + public async Task ConfirmingFormImmediatelyUsesDefaults() + { + var console = NewConsole(); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + // Deployment defaults + await Assert.That(config.Deployment.OutputDir).IsEqualTo("./deploy"); + await Assert.That(config.Deployment.RootCaName).IsEqualTo("Interfold Root CA"); + await Assert.That(config.Deployment.CertYears).IsEqualTo(5); + await Assert.That(config.Deployment.TrustStoreInstall).IsTrue(); + await Assert.That(config.Deployment.IncludeWeb).IsFalse(); + await Assert.That(config.Deployment.WebHttps).IsFalse(); + // No default; the auto-fill is suppressed via PromptWithoutDetection. Validate + // fails fast on this state downstream (see DeploymentSection.Hosts). + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(0); + + // Ports defaults + await Assert.That(config.Ports.ApiHttp).IsEqualTo(5000); + await Assert.That(config.Ports.ApiHttps).IsEqualTo(5001); + await Assert.That(config.Ports.WebHttp).IsEqualTo(8080); + await Assert.That(config.Ports.WebHttps).IsEqualTo(8081); + await Assert.That(config.Ports.Postgres).IsEqualTo(4200); + await Assert.That(config.Ports.Scylla).IsEqualTo(9042); + + // Database / API defaults + await Assert.That(config.DatabaseMode).IsEqualTo(DatabaseMode.Single); + await Assert.That(config.PostgresDatabase).IsEqualTo("interfold"); + await Assert.That(config.ClusterName).IsEqualTo("InterfoldCluster"); + await Assert.That(config.ScyllaKeyspace).IsEqualTo(ScyllaKeyspace.Nam); + await Assert.That(config.ApiImage).IsEqualTo("ghcr.io/azyyyyyy/interfold-api:latest"); + + // ApiRuntime: derivation happens in RunAsync / Validate, not PromptForConfig, so + // the stored fields stay empty here (the menu's Show callbacks derive for display only). + await Assert.That(config.ApiRuntime.CallbackBaseUrl).IsEqualTo(string.Empty); + await Assert.That(config.ApiRuntime.JwtAuthority).IsEqualTo(string.Empty); + await Assert.That(config.ApiRuntime.JwtAudience).IsEqualTo("octocon"); + await Assert.That(config.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(0); + + // Blank string/null fields reproduce the pre-bootstrapper "env var unset" behaviour. + await Assert.That(config.Cluster.NodeGroup).IsEqualTo(NodeGroup.Auxiliary); + await Assert.That(config.Observability.OtlpEndpoint).IsEqualTo(string.Empty); + await Assert.That(config.Storage.AvatarStorageRoot).IsEqualTo(string.Empty); + await Assert.That(config.Storage.AvatarPublicBase).IsEqualTo(string.Empty); + await Assert.That(config.Socket.BatchBytesThreshold).IsNull(); + await Assert.That(config.Persistence.DbRetryAttempts).IsEqualTo(3); + await Assert.That(config.Persistence.DbRetryInitialDelayMs).IsEqualTo(100); + await Assert.That(config.Persistence.DbRetryMaxDelayMs).IsEqualTo(1500); + await Assert.That(config.Persistence.HydrationMaxConcurrency).IsEqualTo(8); + + // OAuth credentials default to empty (paired per provider: ID then secret). + await Assert.That(config.OAuth.GoogleClientId).IsEqualTo(string.Empty); + await Assert.That(config.OAuth.GoogleClientSecret).IsEqualTo(string.Empty); + await Assert.That(config.OAuth.DiscordClientId).IsEqualTo(string.Empty); + await Assert.That(config.OAuth.DiscordClientSecret).IsEqualTo(string.Empty); + await Assert.That(config.OAuth.AppleClientId).IsEqualTo(string.Empty); + await Assert.That(config.OAuth.AppleClientSecret).IsEqualTo(string.Empty); + + // Update section defaults to "manual only" — stock bootstrap matches pre-feature deployments. + await Assert.That(config.Update.Enabled).IsFalse(); + await Assert.That(config.Update.HealthCheckTimeoutSeconds).IsEqualTo(180); + await Assert.That(config.Update.AutoRestoreOnFailure).IsFalse(); + await Assert.That(config.Update.RecreateOnUpdate).IsTrue(); + await Assert.That(config.Update.Services.Length).IsEqualTo(0); + } + + [Test] + public async Task EditingHostsParsesCommaSeparated() + { + var console = NewConsole(); + EditField(console, fieldIndex: 1, "api.example.com,admin.example.com,www.example.com"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(3); + await Assert.That(config.Deployment.Hosts).Contains("api.example.com"); + await Assert.That(config.Deployment.Hosts).Contains("admin.example.com"); + await Assert.That(config.Deployment.Hosts).Contains("www.example.com"); + } + + [Test] + public async Task EditingHostsTrimsWhitespace() + { + var console = NewConsole(); + EditField(console, fieldIndex: 1, " foo.example.com , bar.example.com "); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts).Contains("foo.example.com"); + await Assert.That(config.Deployment.Hosts).Contains("bar.example.com"); + } + + [Test] + public async Task EditingHostsAcceptsIpAndCidr() + { + // IPv4 literal + IPv6 CIDR must reach the stored list verbatim. + var console = NewConsole(); + EditField(console, fieldIndex: 1, "192.168.1.42,fe80::/64"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("192.168.1.42"); + await Assert.That(config.Deployment.Hosts[1]).IsEqualTo("fe80::/64"); + } + + [Test] + public async Task DetectedDeviceIpPreFillsHostsOnFreshBootstrap() + { + // Deterministic probe (test doesn't depend on the runner's NICs). + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("192.168.1.42")); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task DetectedIpv6IsStoredAsBareLiteralNotBracketed() + { + // Downstream consumers (SAN encoder, URL derivation) expect the bare literal; + // storing "[::1]" would round-trip into the JSON with brackets and surprise editors. + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("2001:db8::1")); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("2001:db8::1"); + } + + [Test] + public async Task OperatorEditedHostsOverrideDetectedIpDefault() + { + // Auto-default is just a pre-fill — a typed value must win. + var console = NewConsole(); + EditField(console, fieldIndex: 1, "api.example.com"); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("192.168.1.42")); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("api.example.com"); + } + + [Test] + public async Task NullDetectorLeavesHostsEmpty() + { + // Null detector must NOT invent a fallback (would mask a future "default to 127.0.0.1" + // regression). Validate fails fast on the resulting empty Hosts downstream. + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => null); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(0); + } + + [Test] + public async Task DetectedIpAppearsInPublicHostMenuRow() + { + // Guards against a refactor that pre-fills Hosts but doesn't render the value on its row. + var console = NewConsole(); + ConfirmForm(console); + + ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("10.0.0.42")); + + await Assert.That(Regex.IsMatch(console.Output, @"Public host\(s\)\s+10\.0\.0\.42")).IsTrue(); + } + + [Test] + public async Task HostsPrefillIncludesMdnsHostnameAndIp() + { + // Order matters: hostname first so HostParser.PickPrimary latches it for leaf-cert + // and derived-URL purposes. A swap would silently point every derived URL at the IP. + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("192.168.1.42"), + hostnameProbe: () => "workstation.local"); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("workstation.local"); + await Assert.That(config.Deployment.Hosts[1]).IsEqualTo("192.168.1.42"); + // Menu row = comma-joined; pin the exact display so a "print only Hosts[0]" regression gets caught. + await Assert.That(Regex.IsMatch(console.Output, @"Public host\(s\)\s+workstation\.local,192\.168\.1\.42")).IsTrue(); + } + + [Test] + public async Task HostsPrefillOmitsHostnameWhenProbeReturnsNull() + { + // Null hostname probe (banner reported mDNS unavailable) must not become an empty + // or null entry in Hosts — only the detected IP lands. + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => IPAddress.Parse("192.168.1.42"), + hostnameProbe: () => null); + + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task EditingOAuthSecretsCapturesValues() + { + // Paired per provider (ID then secret): secrets sit at 32 / 34 / 36. + var console = NewConsole(); + EditField(console, fieldIndex: 32, "google-secret-xyz"); + EditField(console, fieldIndex: 34, "discord-secret-abc"); + EditField(console, fieldIndex: 36, "apple-secret-jwt"); + ConfirmForm(console); + + // maskSecrets:false keeps the prompt off the ReadKey path so PushTextWithEnter suffices. + var config = PromptWithoutDetection(console); + + await Assert.That(config.OAuth.GoogleClientSecret).IsEqualTo("google-secret-xyz"); + await Assert.That(config.OAuth.DiscordClientSecret).IsEqualTo("discord-secret-abc"); + await Assert.That(config.OAuth.AppleClientSecret).IsEqualTo("apple-secret-jwt"); + } + + [Test] + public async Task EditingOAuthClientIdsCapturesValues() + { + // Public IDs → plain PromptStr (no masking). ID rows sit at 31 / 33 / 35. + var console = NewConsole(); + EditField(console, fieldIndex: 31, "1234.apps.googleusercontent.com"); + EditField(console, fieldIndex: 33, "9876543210"); + EditField(console, fieldIndex: 35, "com.example.interfold.signin"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.OAuth.GoogleClientId).IsEqualTo("1234.apps.googleusercontent.com"); + await Assert.That(config.OAuth.DiscordClientId).IsEqualTo("9876543210"); + await Assert.That(config.OAuth.AppleClientId).IsEqualTo("com.example.interfold.signin"); + } + + [Test] + public async Task FormShowsEmptyMarkerNextToUnsetOAuthClientIds() + { + // Parity with secret rows: an unset ID must render , not a blank cell (which + // reads as "no row" and can lead operators to miss the second half of a provider's pair). + var console = NewConsole(); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + await Assert.That(Regex.IsMatch(output, @"Google OAuth client ID\s+")).IsTrue(); + await Assert.That(Regex.IsMatch(output, @"Discord OAuth client ID\s+")).IsTrue(); + await Assert.That(Regex.IsMatch(output, @"Apple OAuth client ID\s+")).IsTrue(); + } + + [Test] + public async Task FormShowsOAuthClientIdsVerbatimInMenuRow() + { + // IDs are public → the menu row echoes the value verbatim, not /. + const string googleId = "1234.apps.googleusercontent.com"; + var console = NewConsole(); + EditField(console, fieldIndex: 31, googleId); + ConfirmForm(console); + + PromptWithoutDetection(console); + + await Assert.That(console.Output).Contains(googleId); + } + + [Test] + public async Task EditingPortsCapturesOverrides() + { + var console = NewConsole(); + EditField(console, fieldIndex: 7, "5100"); // ApiHttp + EditField(console, fieldIndex: 8, "5101"); // ApiHttps + EditField(console, fieldIndex: 9, "8090"); // WebHttp + EditField(console, fieldIndex: 10, "8091"); // WebHttps + EditField(console, fieldIndex: 11, "4300"); // Postgres + EditField(console, fieldIndex: 12, "9043"); // Scylla + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Ports.ApiHttp).IsEqualTo(5100); + await Assert.That(config.Ports.ApiHttps).IsEqualTo(5101); + await Assert.That(config.Ports.WebHttp).IsEqualTo(8090); + await Assert.That(config.Ports.WebHttps).IsEqualTo(8091); + await Assert.That(config.Ports.Postgres).IsEqualTo(4300); + await Assert.That(config.Ports.Scylla).IsEqualTo(9043); + } + + [Test] + public async Task EditingBoolsCapturesOverrides() + { + var console = NewConsole(); + EditField(console, fieldIndex: 4, "n"); // TrustStoreInstall := false + EditField(console, fieldIndex: 6, "y"); // WebHttps := true + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.TrustStoreInstall).IsFalse(); + await Assert.That(config.Deployment.WebHttps).IsTrue(); + } + + [Test] + public async Task EditingIncludeWebCapturesOverride() + { + // IncludeWeb (row 5) is independent from WebHttps (row 6) — toggling one must not + // silently promote the other. + var console = NewConsole(); + EditField(console, fieldIndex: 5, "y"); // IncludeWeb := true + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.IncludeWeb).IsTrue(); + await Assert.That(config.Deployment.WebHttps).IsFalse(); + } + + [Test] + public async Task PortPromptRePromptsOnInvalidInt() + { + // "bad" triggers the re-prompt; the second answer (5005) is what should stick. + var console = NewConsole(); + EditField(console, fieldIndex: 7, "bad", "5005"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Ports.ApiHttp).IsEqualTo(5005); + await Assert.That(console.Output).Contains("must be an integer"); + } + + [Test] + public async Task BoolPromptRePromptsOnInvalidAnswer() + { + var console = NewConsole(); + EditField(console, fieldIndex: 4, "maybe", "n"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Deployment.TrustStoreInstall).IsFalse(); + } + + [Test] + public async Task DatabaseModePromptEnforcesChoices() + { + var console = NewConsole(); + EditField(console, fieldIndex: 13, "invalid", "multi"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.DatabaseMode).IsEqualTo(DatabaseMode.Multi); + } + + [Test] + public async Task EditingScyllaKeyspaceCapturesValue() + { + // Happy-path edit; AddChoices enforcement is covered by the rejection test below. + var console = NewConsole(); + EditField(console, fieldIndex: 16, "eur"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.ScyllaKeyspace).IsEqualTo(ScyllaKeyspace.Eur); + } + + [Test] + public async Task ScyllaKeyspacePromptEnforcesChoices() + { + // AddChoices re-prompts on non-listed values; the eventually-accepted value sticks. + var console = NewConsole(); + EditField(console, fieldIndex: 16, "ant", "gdpr"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.ScyllaKeyspace).IsEqualTo(ScyllaKeyspace.Gdpr); + } + + [Test] + public async Task EditingApiRuntimeCapturesValues() + { + // Rows 17-20: CallbackBaseUrl / JwtAuthority / JwtAudience / CORS. Verbatim round-trip. + var console = NewConsole(); + EditField(console, fieldIndex: 17, "https://api.custom.example.com"); + EditField(console, fieldIndex: 18, "https://issuer.custom.example.com"); + EditField(console, fieldIndex: 19, "custom-aud"); + EditField(console, fieldIndex: 20, "https://app.example.com,https://admin.example.com"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.custom.example.com"); + await Assert.That(config.ApiRuntime.JwtAuthority).IsEqualTo("https://issuer.custom.example.com"); + await Assert.That(config.ApiRuntime.JwtAudience).IsEqualTo("custom-aud"); + await Assert.That(config.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(2); + await Assert.That(config.ApiRuntime.CorsAllowedOrigins).Contains("https://app.example.com"); + await Assert.That(config.ApiRuntime.CorsAllowedOrigins).Contains("https://admin.example.com"); + } + + [Test] + public async Task FormShowsDerivedCallbackBaseUrlInMenuRow() + { + // ApiRuntime Show callbacks paint the derived default when the stored field is empty. + // With WebHttps=false and shipped port defaults: callback is https://host:5001 (API + // always https in self-host), CORS is http://host:8080 (web tier). + var console = NewConsole(); + EditField(console, fieldIndex: 1, "api.example.com"); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + await Assert.That(Regex.IsMatch(output, @"OAuth callback base URL\s+https://api\.example\.com:5001")).IsTrue(); + await Assert.That(Regex.IsMatch(output, @"JWT authority \(iss claim\)\s+https://api\.example\.com:5001")).IsTrue(); + await Assert.That(Regex.IsMatch(output, @"CORS allowed origins\s+http://api\.example\.com:8080")).IsTrue(); + } + + [Test] + public async Task CorsAllowedOriginsRePromptsOnInvalidUri() + { + // Bare hostnames aren't absolute http(s) URIs — re-prompt; second answer sticks. + var console = NewConsole(); + EditField(console, fieldIndex: 20, + "not-a-url,still-not-a-url", + "https://app.example.com,https://admin.example.com"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(2); + await Assert.That(config.ApiRuntime.CorsAllowedOrigins).Contains("https://app.example.com"); + await Assert.That(console.Output).Contains("not a valid http(s) origin"); + } + + [Test] + public async Task FormListsEveryFieldLabel() + { + // Confirming immediately is enough — the menu renders before the first key is consumed. + var console = NewConsole(); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + // Deployment + await Assert.That(output).Contains("Output directory"); + await Assert.That(output).Contains("Public host"); + await Assert.That(output).Contains("Root CA"); + await Assert.That(output).Contains("Leaf cert validity"); + await Assert.That(output).Contains("Install root CA"); + await Assert.That(output).Contains("octocon-web"); + // Ports + await Assert.That(output).Contains("API HTTP port"); + await Assert.That(output).Contains("API HTTPS port"); + await Assert.That(output).Contains("Web HTTP port"); + await Assert.That(output).Contains("Web HTTPS port"); + await Assert.That(output).Contains("Postgres host port"); + await Assert.That(output).Contains("Scylla"); + // Database + await Assert.That(output).Contains("Database mode"); + await Assert.That(output).Contains("Postgres application DB name"); + await Assert.That(output).Contains("Cluster name"); + await Assert.That(output).Contains("Scylla keyspace (region)"); + // API + await Assert.That(output).Contains("OAuth callback base URL"); + await Assert.That(output).Contains("JWT authority (iss claim)"); + await Assert.That(output).Contains("JWT audience (aud claim)"); + await Assert.That(output).Contains("CORS allowed origins"); + await Assert.That(output).Contains("API image"); + // Cluster & telemetry + await Assert.That(output).Contains("Cluster node group"); + await Assert.That(output).Contains("OTLP endpoint"); + // Storage + await Assert.That(output).Contains("Avatar storage root"); + await Assert.That(output).Contains("Avatar public base URL"); + // Performance tuning + await Assert.That(output).Contains("Socket batch flush threshold"); + await Assert.That(output).Contains("DB retry attempts"); + await Assert.That(output).Contains("DB retry initial delay"); + await Assert.That(output).Contains("DB retry max delay"); + await Assert.That(output).Contains("Hydration max concurrency"); + // OAuth: assert ID and secret rows separately so a dropped label is caught. + await Assert.That(output).Contains("Google OAuth client ID"); + await Assert.That(output).Contains("Google OAuth client secret"); + await Assert.That(output).Contains("Discord OAuth client ID"); + await Assert.That(output).Contains("Discord OAuth client secret"); + await Assert.That(output).Contains("Apple OAuth client ID"); + await Assert.That(output).Contains("Apple OAuth client secret"); + } + + [Test] + public async Task FormListsEverySectionHeader() + { + var console = NewConsole(); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + await Assert.That(output).Contains("Deployment"); + await Assert.That(output).Contains("Ports"); + await Assert.That(output).Contains("Database"); + // "---" framing distinguishes ambiguous tokens (e.g. "API" also appears in field labels). + await Assert.That(Regex.IsMatch(output, @"---\s+API\s+---")).IsTrue(); + await Assert.That(output).Contains("Cluster & telemetry"); + await Assert.That(Regex.IsMatch(output, @"---\s+Storage\s+---")).IsTrue(); + await Assert.That(output).Contains("Performance tuning"); + await Assert.That(output).Contains("OAuth credentials"); + await Assert.That(output).Contains("Backup & autostart"); + await Assert.That(Regex.IsMatch(output, @"---\s+Updates\s+---")).IsTrue(); + await Assert.That(output).Contains("Configure interfold.bootstrap.json"); + await Assert.That(output).Contains("Confirm and save"); + } + + [Test] + public async Task SectionHeadersSitAboveTheirFirstField() + { + // Position guard (FormListsEverySectionHeader only checks presence). The grouped + // declaration makes mechanical index drift impossible, so this now catches a field + // declared under the wrong Group(...) — same visible symptom. + // Assert: previous section's last field < header < current section's first field. + var console = NewConsole(); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + // (previous section's last field label, header text, section's first field label) + var boundaries = new (string PrevField, string Header, string FirstField)[] + { + ("Terminate HTTPS at octocon-web", "--- Ports ---", "API HTTP port"), + ("Scylla/Cassandra host port", "--- Database ---", "Database mode"), + ("Scylla keyspace (region)", "--- API ---", "OAuth callback base URL"), + ("Pre-built Interfold API image", "--- Cluster & telemetry ---", "Cluster node group"), + ("OTLP endpoint", "--- Storage ---", "Avatar storage root"), + ("Avatar public base URL", "--- Performance tuning ---", "Socket batch flush threshold"), + ("Hydration max concurrency", "--- OAuth credentials ---", "Google OAuth client ID"), + ("Apple OAuth client secret", "--- Backup & autostart ---", "Scheduled backups enabled"), + ("Autostart server on boot", "--- Updates ---", "Chain updates after backup"), + ("Update service whitelist", "--- Firebase ---", "Firebase push notifications"), + }; + foreach (var (prevField, header, firstField) in boundaries) + { + var prevIdx = output.IndexOf(prevField, StringComparison.Ordinal); + var headerIdx = output.IndexOf(header, StringComparison.Ordinal); + var firstIdx = output.IndexOf(firstField, StringComparison.Ordinal); + await Assert.That(prevIdx).IsGreaterThanOrEqualTo(0); + await Assert.That(headerIdx).IsGreaterThan(prevIdx) + .Because($"'{header}' must render below '{prevField}'"); + await Assert.That(firstIdx).IsGreaterThan(headerIdx) + .Because($"'{firstField}' must render below '{header}'"); + } + } + + [Test] + public async Task EditingBackupTogglesAndSchedule() + { + // Happy-path edit of every row in the five-row backup section (37..41). + var console = NewConsole(); + EditField(console, fieldIndex: 37, "y"); // Enabled := true + EditField(console, fieldIndex: 38, "Mon..Fri 03:30"); // Schedule + EditField(console, fieldIndex: 39, "30"); // RetainCount + EditField(console, fieldIndex: 40, "/srv/backups/interfold"); // Directory + EditField(console, fieldIndex: 41, "y"); // AutostartServer := true + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Backup.Enabled).IsTrue(); + await Assert.That(config.Backup.Schedule).IsEqualTo("Mon..Fri 03:30"); + await Assert.That(config.Backup.RetainCount).IsEqualTo(30); + await Assert.That(config.Backup.Directory).IsEqualTo("/srv/backups/interfold"); + await Assert.That(config.Backup.AutostartServer).IsTrue(); + } + + [Test] + public async Task BackupRetainPromptRejectsOutOfRangeValues() + { + // 0 is outside [1..1000]; PromptInt re-prompts and the second answer sticks. + var console = NewConsole(); + EditField(console, fieldIndex: 39, "0", "7"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Backup.RetainCount).IsEqualTo(7); + } + + [Test] + public async Task BackupSectionEmptyDirectoryDefaults() + { + // Empty Directory = the "use {outputDir}/backups" sentinel — operators who ignore + // the section entirely (the common case) still produce a valid config. + var console = NewConsole(); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Backup.Directory).IsEqualTo(string.Empty); + await Assert.That(config.Backup.Enabled).IsFalse(); + await Assert.That(config.Backup.AutostartServer).IsFalse(); + } + + [Test] + public async Task EditingNodeGroupCapturesValue() + { + // Happy-path edit; rejection covered by NodeGroupPromptEnforcesChoices below. + var console = NewConsole(); + EditField(console, fieldIndex: 22, "primary"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Cluster.NodeGroup).IsEqualTo(NodeGroup.Primary); + } + + [Test] + public async Task NodeGroupPromptEnforcesChoices() + { + // AddChoices re-prompts on non-listed values (same shape as ScyllaKeyspace). + var console = NewConsole(); + EditField(console, fieldIndex: 22, "guardian", "sidecar"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Cluster.NodeGroup).IsEqualTo(NodeGroup.Sidecar); + } + + [Test] + public async Task EditingStorageAndObservabilityCapturesValues() + { + // Non-empty round-trip; blank-state pinned by ConfirmingFormImmediatelyUsesDefaults. + var console = NewConsole(); + EditField(console, fieldIndex: 23, "http://otel-collector:4317"); + EditField(console, fieldIndex: 24, "/var/lib/interfold/avatars"); + EditField(console, fieldIndex: 25, "https://cdn.example.com/avatars/"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Observability.OtlpEndpoint).IsEqualTo("http://otel-collector:4317"); + await Assert.That(config.Storage.AvatarStorageRoot).IsEqualTo("/var/lib/interfold/avatars"); + await Assert.That(config.Storage.AvatarPublicBase).IsEqualTo("https://cdn.example.com/avatars/"); + } + + [Test] + public async Task EditingTuningIntsCapturesValues() + { + // Row 26 uses PromptNullableInt; the other four use PromptInt. All five round-trip. + var console = NewConsole(); + EditField(console, fieldIndex: 26, "131072"); + EditField(console, fieldIndex: 27, "5"); + EditField(console, fieldIndex: 28, "250"); + EditField(console, fieldIndex: 29, "3000"); + EditField(console, fieldIndex: 30, "16"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Socket.BatchBytesThreshold).IsEqualTo(131072); + await Assert.That(config.Persistence.DbRetryAttempts).IsEqualTo(5); + await Assert.That(config.Persistence.DbRetryInitialDelayMs).IsEqualTo(250); + await Assert.That(config.Persistence.DbRetryMaxDelayMs).IsEqualTo(3000); + await Assert.That(config.Persistence.HydrationMaxConcurrency).IsEqualTo(16); + } + + [Test] + public async Task BatchBytesThresholdAcceptsBlankAsNull() + { + // Blank on row 26 must clear to null (PromptNullableInt contract), not fall back to the existing value. + var console = NewConsole(); + EditField(console, fieldIndex: 26, string.Empty); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Socket.BatchBytesThreshold).IsNull(); + } + + [Test] + public async Task DbRetryAttemptsRePromptsOnOutOfRangeInt() + { + // 9999 breaches the [1..100] bound on row 27; second answer sticks. Pins that the + // tuning fields share PromptInt's validator with the port rows. + var console = NewConsole(); + EditField(console, fieldIndex: 27, "9999", "5"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Persistence.DbRetryAttempts).IsEqualTo(5); + await Assert.That(console.Output).Contains("must be an integer in"); + } + + [Test] + public async Task FormShowsEditedValueInMenuRow() + { + // Post-edit re-render must echo the freshly-typed value on the row. + var console = NewConsole(); + EditField(console, fieldIndex: 7, "5005"); + ConfirmForm(console); + + PromptWithoutDetection(console); + + await Assert.That(console.Output).Contains("5005"); + } + + [Test] + public async Task FormMasksOAuthSecretsInMenuRow() + { + // Runs unmasked (maskSecrets:false) to prove the Mask() Show() callback is what + // hides the value in the row — Spectre's per-field Secret() is covered separately by + // MaskSecretsHidesOAuthEchoInPromptOutput. The raw secret WILL appear in the + // TextPrompt echo; the guard is that it never appears NEXT TO the label. + const string secret = "google-secret-xyz"; + var console = NewConsole(); + EditField(console, fieldIndex: 32, secret); + ConfirmForm(console); + + PromptWithoutDetection(console); + + var output = console.Output; + // next to the edited row, next to the two unedited ones. + await Assert.That(output).Contains(""); + await Assert.That(output).Contains(""); + + // The actual masking guard. + var leakedMenuRow = new Regex(@"Google OAuth client secret\s+google-secret-xyz"); + await Assert.That(leakedMenuRow.IsMatch(output)).IsFalse(); + + // Sanity: without this, the guard above passes vacuously. + await Assert.That(output).Contains(secret); + } + + [Test] + public async Task EditingUpdateSectionCapturesValues() + { + // Happy-path edit of every row in the five-row update section (42..46). + var console = NewConsole(); + EditField(console, fieldIndex: 42, "y"); // Enabled := true + EditField(console, fieldIndex: 43, "300"); // HealthCheckTimeoutSeconds + EditField(console, fieldIndex: 44, "y"); // AutoRestoreOnFailure := true + EditField(console, fieldIndex: 45, "n"); // RecreateOnUpdate := false + EditField(console, fieldIndex: 46, "interfold-api,octocon-web"); // Services + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Update.Enabled).IsTrue(); + await Assert.That(config.Update.HealthCheckTimeoutSeconds).IsEqualTo(300); + await Assert.That(config.Update.AutoRestoreOnFailure).IsTrue(); + await Assert.That(config.Update.RecreateOnUpdate).IsFalse(); + await Assert.That(config.Update.Services.Length).IsEqualTo(2); + await Assert.That(config.Update.Services).Contains("interfold-api"); + await Assert.That(config.Update.Services).Contains("octocon-web"); + } + + [Test] + public async Task UpdateHealthCheckTimeoutRePromptsOnOutOfRange() + { + // 9999 breaches the [1..3600] bound; second answer sticks. + var console = NewConsole(); + EditField(console, fieldIndex: 43, "9999", "60"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Update.HealthCheckTimeoutSeconds).IsEqualTo(60); + await Assert.That(console.Output).Contains("must be an integer in"); + } + + [Test] + public async Task UpdateServicesPromptRejectsUnknownEntry() + { + // Unknown entry re-prompts against ValidUpdateServices; second answer sticks. + var console = NewConsole(); + EditField(console, fieldIndex: 46, + "msg-db,not-a-real-service", + "msg-db,scylla"); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Update.Services.Length).IsEqualTo(2); + await Assert.That(config.Update.Services).Contains("msg-db"); + await Assert.That(config.Update.Services).Contains("scylla"); + await Assert.That(console.Output).Contains("not a known compose service"); + } + + [Test] + public async Task UpdateServicesBlankClearsWhitelist() + { + // Blank = "every service" (stored as empty array) — the un-scope path in the UI. + var console = NewConsole(); + EditField(console, fieldIndex: 46, string.Empty); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Update.Services.Length).IsEqualTo(0); + } + + [Test] + public async Task UpdateSectionEmptyDefaults() + { + // Locks the "manual only" property-initialiser defaults so a flipped default is caught. + var console = NewConsole(); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Update.Enabled).IsFalse(); + await Assert.That(config.Update.HealthCheckTimeoutSeconds).IsEqualTo(180); + await Assert.That(config.Update.AutoRestoreOnFailure).IsFalse(); + await Assert.That(config.Update.RecreateOnUpdate).IsTrue(); + await Assert.That(config.Update.Services.Length).IsEqualTo(0); + } + + [Test] + public async Task ExistingSeed_ConfirmImmediately_PreservesCustomValues() + { + var existing = TestSupport.MakeConfig(tweak: c => + { + c.Deployment.Hosts = ["custom.example.com", "10.0.0.5"]; + c.Deployment.RootCaName = "Custom Root CA"; + c.Deployment.CertYears = 10; + c.Ports.ApiHttps = 5443; + c.Firebase.AndroidConfigPath = "/opt/firebase/google-services.json"; + c.Firebase.WebConfigPath = "/opt/firebase/firebase-web-config.json"; + }); + + var console = NewConsole(); + ConfirmForm(console); + + var config = ConfigPhase.PromptForConfig( + console, + maskSecrets: false, + localAddressProbe: () => null, + hostnameProbe: () => null, + existing: existing); + + await Assert.That(config.Deployment.Hosts).IsEquivalentTo(["custom.example.com", "10.0.0.5"]) + .Because("--reconfigure must open the form pre-filled; confirm-without-edits must keep Hosts."); + await Assert.That(config.Deployment.RootCaName).IsEqualTo("Custom Root CA"); + await Assert.That(config.Deployment.CertYears).IsEqualTo(10); + await Assert.That(config.Ports.ApiHttps).IsEqualTo(5443); + await Assert.That(config.Firebase.AndroidConfigPath).IsEqualTo("/opt/firebase/google-services.json"); + await Assert.That(config.Firebase.WebConfigPath).IsEqualTo("/opt/firebase/firebase-web-config.json"); + } + + [Test] + public async Task MaskSecretsHidesOAuthEchoInPromptOutput() + { + // Prod path (maskSecrets:true) → Secret('*') masks the echo. PushTextWithEnter's + // per-char + Enter sequence matches what ReadKey consumes. + const string secret = "google-secret-xyz"; + var console = NewConsole(); + EditField(console, fieldIndex: 32, secret); + ConfirmForm(console); + + var config = PromptWithoutDetection(console, maskSecrets: true); + + // Value still lands on the config; masking is display-only. + await Assert.That(config.OAuth.GoogleClientSecret).IsEqualTo(secret); + await Assert.That(console.Output).DoesNotContain(secret); + } + + /// + /// Minimal service-account fixture so the scanner's "type": "service_account" + /// sniff finds a valid candidate. FirebasePhase-level parsing is validated separately. + /// + private const string FirebaseServiceAccountFixture = /*lang=json,strict*/ """ + { + "type": "service_account", + "project_id": "octocon-test", + "private_key_id": "abc", + "private_key": "-----BEGIN PRIVATE KEY-----\nfake\n-----END PRIVATE KEY-----\n", + "client_email": "sa@octocon.iam.gserviceaccount.com", + "client_id": "1", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token" + } + """; + + + + [Test] + public async Task FirebaseAutoDetectBranchPopulatesAllFourPathsFromFolder() + { + // End-to-end drive of the scanner → wizard → config wiring on the auto-detect branch. + using var scratch = TestSupport.NewScratchDir("firebase-wizard"); + var folder = scratch.Path; + var android = Path.Combine(folder, "google-services.json"); + var ios = Path.Combine(folder, "GoogleService-Info.plist"); + var web = Path.Combine(folder, "firebase-web-config.json"); + var sa = Path.Combine(folder, "octocon-firebase-adminsdk-abc.json"); + File.WriteAllText(android, "{}"); + File.WriteAllText(ios, ""); + File.WriteAllText(web, "{}"); + File.WriteAllText(sa, FirebaseServiceAccountFixture); + + var console = NewConsole(); + Navigate(console, downArrows: 47); + // Auto-detect is the first choice — Enter without a DownArrow. + console.Input.PushKey(ConsoleKey.Enter); + console.Input.PushTextWithEnter(folder); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Firebase.AndroidConfigPath).IsEqualTo(Path.GetFullPath(android)); + await Assert.That(config.Firebase.IosConfigPath).IsEqualTo(Path.GetFullPath(ios)); + await Assert.That(config.Firebase.WebConfigPath).IsEqualTo(Path.GetFullPath(web)); + await Assert.That(config.Firebase.ServiceAccountPath).IsEqualTo(Path.GetFullPath(sa)); + } + + [Test] + public async Task FirebasePerFileBranchWritesFourExplicitPaths() + { + // Per-file branch: four sequential TextPrompts, each guarded by File.Exists. + using var scratch = TestSupport.NewScratchDir("firebase-wizard"); + var folder = scratch.Path; + var android = Path.Combine(folder, "google-services.json"); + var ios = Path.Combine(folder, "GoogleService-Info.plist"); + var web = Path.Combine(folder, "firebase-web-config.json"); + var sa = Path.Combine(folder, "service-account.json"); + File.WriteAllText(android, "{}"); + File.WriteAllText(ios, ""); + File.WriteAllText(web, "{}"); + File.WriteAllText(sa, FirebaseServiceAccountFixture); + + var console = NewConsole(); + Navigate(console, downArrows: 47); + // Per-file is the 2nd choice — one DownArrow before Enter. + console.Input.PushKey(ConsoleKey.DownArrow); + console.Input.PushKey(ConsoleKey.Enter); + console.Input.PushTextWithEnter(android); + console.Input.PushTextWithEnter(ios); + console.Input.PushTextWithEnter(web); + console.Input.PushTextWithEnter(sa); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Firebase.AndroidConfigPath).IsEqualTo(android); + await Assert.That(config.Firebase.IosConfigPath).IsEqualTo(ios); + await Assert.That(config.Firebase.WebConfigPath).IsEqualTo(web); + await Assert.That(config.Firebase.ServiceAccountPath).IsEqualTo(sa); + } + + [Test] + public async Task FirebaseCancelBranchLeavesSectionAtDefaults() + { + // Cancel is the 4th choice — three DownArrows. Every *Path must stay empty. + var console = NewConsole(); + Navigate(console, downArrows: 47); + console.Input.PushKey(ConsoleKey.DownArrow); + console.Input.PushKey(ConsoleKey.DownArrow); + console.Input.PushKey(ConsoleKey.DownArrow); + console.Input.PushKey(ConsoleKey.Enter); + ConfirmForm(console); + + var config = PromptWithoutDetection(console); + + await Assert.That(config.Firebase.AndroidConfigPath).IsEmpty(); + await Assert.That(config.Firebase.IosConfigPath).IsEmpty(); + await Assert.That(config.Firebase.WebConfigPath).IsEmpty(); + await Assert.That(config.Firebase.ServiceAccountPath).IsEmpty(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigMdnsGateTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigMdnsGateTests.cs new file mode 100644 index 00000000..e49af9f3 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigMdnsGateTests.cs @@ -0,0 +1,276 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Drives directly via its probe seam. The +/// gate is Tier 2 of the two-tier mDNS design: it runs AFTER +/// on the command +/// and catches any .local entries that survived the operator's typing (either they +/// declined the pre-prompt banner's install offer and typed one anyway, or the config was +/// loaded from JSON — which bypasses the banner entirely). +/// +/// +/// Tests focus on the return-value contract (true = mutated, false = no-op) and the +/// observable mutations to . The exact warning wording +/// (including the "bootstrap will continue" copy that pins the "we don't halt" invariant) +/// is asserted end-to-end by the DinD integration tests where a real subprocess writes to a +/// real stdout — TUnit's Source-Generated engine intercepts ahead +/// of any per-test swap, so unit tests can't reliably read +/// PhaseLogger output. +/// +/// +/// Interactive install-yes / install-no branches for the post-fill gate stay covered by +/// the integration tests (they need a real +/// confirmation prompt keystroke). Here we lock down the skip / accept / strip +/// branches deterministically via the probe seam plus the Validate re-run downstream of +/// an emptied hosts list. +/// +/// +public sealed class ConfigMdnsGateTests +{ + private static BootstrapOptions OptionsFor(bool nonInteractive) => TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: "./deploy", + skipPrereqs: true, + nonInteractive: nonInteractive); + + /// + /// Constructs a minimal with the supplied hosts. Every + /// other field stays at its property-initialiser default — the gate doesn't touch + /// anything except Deployment.Hosts, so this keeps the tests focused on the + /// hosts-mutation behaviour without unrelated field wiring. + /// + private static BootstrapConfig ConfigWithHosts(params string[] hosts) + { + var config = new BootstrapConfig(); + config.Deployment.Hosts = [.. hosts]; + return config; + } + + [Test] + public async Task GateSkipsWhenNoLocalHostInList() + { + // Hosts list contains only non-.local entries — nothing for the gate to probe. Must + // short-circuit BEFORE calling the probe (probe throws so we catch the regression + // that would silently start shelling out to `getent` for non-.local hosts and + // burn a getent per config on every bootstrap run). + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("api.example.com", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => throw new InvalidOperationException( + "probe must not be called when no .local entries are present")); + + await Assert.That(mutated).IsFalse(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts).Contains("api.example.com"); + await Assert.That(config.Deployment.Hosts).Contains("192.168.1.42"); + } + + [Test] + public async Task GateSkipsWhenProbeReturnsNull() + { + // Non-Linux short-circuit path — the probe returns null and the gate reports "no + // mutation" so callers keep the operator's original list intact. Same behaviour + // applies to any environmental failure the probe surfaces as null (getent binary + // missing, Process.Start failure). Contract: the gate NEVER strips on ambiguity. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("workstation.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(null)); + + await Assert.That(mutated).IsFalse(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("workstation.local"); + } + + [Test] + public async Task GateAcceptsWhenAllLocalHostsResolve() + { + // Every .local host resolves via the local mDNS chain — nothing to strip. The gate + // logs an "mDNS ok" confirmation info line and reports no mutation. Pins the + // "silent-when-things-work" path so we don't accidentally start warning on healthy + // configs. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("workstation.local", "printer.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(true)); + + await Assert.That(mutated).IsFalse(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(3); + } + + [Test] + public async Task GateStripsUnresolvableLocalHostInNonInteractive() + { + // The bread-and-butter unattended path: mDNS is broken (avahi missing on the host) + // and we're running from a systemd timer / scheduled job. The gate can't prompt, so + // it strips the unresolvable .local entries and returns true so the caller knows to + // re-persist + re-validate. The bootstrap run itself continues with the reduced + // list — asserted by the caller re-running Validate() successfully in + // GateStripsPreservingNonLocalHostsAllowsRevalidate below. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("workstation.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(false)); + + await Assert.That(mutated).IsTrue(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("192.168.1.42"); + await Assert.That(config.Deployment.Hosts).DoesNotContain("workstation.local"); + } + + [Test] + public async Task GateStripsAllBrokenLocalHostsIndividually() + { + // Every .local host in the list is broken — the gate must strip them all rather + // than stopping at the first failure. Documents the "probe every entry" + // per-hostname behaviour so a future refactor that (say) short-circuits on the + // first failure doesn't silently leave stale entries in the list. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("host-a.local", "host-b.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(false)); + + await Assert.That(mutated).IsTrue(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts).DoesNotContain("host-a.local"); + await Assert.That(config.Deployment.Hosts).DoesNotContain("host-b.local"); + await Assert.That(config.Deployment.Hosts).Contains("192.168.1.42"); + } + + [Test] + public async Task GateStripsOnlyBrokenLocalHostsWhenSomeResolve() + { + // Mixed working / broken .local entries in the same list — only the broken ones + // should be removed. Pins the "per-entry, not all-or-nothing" strip contract: + // partial mDNS coverage is legitimate (e.g. some devices advertise, others don't) + // and the gate must preserve the working entries so their SANs land in the leaf + // cert as expected. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("good.local", "bad.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (name, _) => Task.FromResult(name == "good.local")); + + await Assert.That(mutated).IsTrue(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(2); + await Assert.That(config.Deployment.Hosts).Contains("good.local"); + await Assert.That(config.Deployment.Hosts).DoesNotContain("bad.local"); + await Assert.That(config.Deployment.Hosts).Contains("192.168.1.42"); + } + + [Test] + public async Task GateStripsPreservingNonLocalHostsAllowsRevalidate() + { + // Downstream contract test: after the gate strips a broken .local entry, the + // caller re-runs Validate() (see ConfigPhase.RunAsync). The remaining non-.local + // host must be enough to satisfy validation — the strip is safe as long as at + // least one leaf-eligible host survives. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("workstation.local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(false)); + + await Assert.That(mutated).IsTrue(); + // Validate() must succeed on the reduced list. If a regression changed the strip + // to (say) leave the entry in as a placeholder, this would still pass; but if a + // regression removed the non-.local guard from Validate, this catches the + // downstream breakage. + ConfigPhase.Validate(config); + } + + [Test] + public async Task GateEmptiesHostListWhenAllUnresolvable() + { + // The safety net: if a config with ONLY unresolvable .local entries goes through + // the gate in non-interactive mode, the strip empties the Hosts list. Bootstrap + // deliberately does NOT salvage — the caller (ConfigPhase.RunAsync) calls + // Validate after the mutation, and Validate throws the standard "at least one + // host required" error. Better to fail loudly than silently produce a cert with + // zero SANs. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("workstation.local"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(false)); + + await Assert.That(mutated).IsTrue(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(0); + // The caller's subsequent Validate() throws with the "at least one host" invariant. + // Confirm the exact contract here so a future refactor that (say) silently retains + // the broken entry to keep validation happy gets caught. + var ex = Assert.Throws(() => ConfigPhase.Validate(config)); + await Assert.That(ex!.Message).Contains("must contain at least one host"); + } + + [Test] + public async Task GateCaseInsensitivelyMatchesLocalSuffix() + { + // Operators sometimes uppercase parts of hostnames when hand-editing JSON. The + // suffix match must be case-insensitive so "WORKSTATION.Local" is still recognised + // as a .local entry and probed / stripped identically to "workstation.local". + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("WORKSTATION.Local", "192.168.1.42"); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (_, _) => Task.FromResult(false)); + + await Assert.That(mutated).IsTrue(); + await Assert.That(config.Deployment.Hosts.Count).IsEqualTo(1); + await Assert.That(config.Deployment.Hosts[0]).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task GateProbesEveryLocalEntry() + { + // Contract: every .local entry gets its own probe call — no dedupe, no "first + // failure short-circuits the rest". Documents that the probe fires per-entry so an + // aggressive optimisation can't silently break the "per-hostname strip" behaviour + // asserted by GateStripsOnlyBrokenLocalHostsWhenSomeResolve. + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var config = ConfigWithHosts("one.local", "two.local", "three.local"); + var probed = new List(); + + var mutated = await ConfigPhase.ApplyMdnsGateAsync( + config, options, logger, CancellationToken.None, + probe: (name, _) => + { + probed.Add(name); + return Task.FromResult(true); + }); + + await Assert.That(mutated).IsFalse(); + await Assert.That(probed.Count).IsEqualTo(3); + await Assert.That(probed).Contains("one.local"); + await Assert.That(probed).Contains("two.local"); + await Assert.That(probed).Contains("three.local"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPhaseResolveDerivedDefaultsTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPhaseResolveDerivedDefaultsTests.cs new file mode 100644 index 00000000..c2f40657 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPhaseResolveDerivedDefaultsTests.cs @@ -0,0 +1,269 @@ +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for . The helper is the +/// single source of truth for "what does an empty field +/// resolve to?" — both the interactive form's Show callbacks and the non-interactive +/// path call it, so its behaviour gets pinned here in +/// isolation. +/// +/// Three rules under test: +/// +/// +/// The API-facing fields ( / +/// ) always derive with scheme https +/// because the API container's Kestrel default endpoint is bound to the bootstrapper- +/// issued leaf PFX unconditionally (see InterfoldAppHost.ConfigureApiSelfHostEnv), +/// and they carry :{Ports.ApiHttps} unless the operator picked 443. +/// never gates the API-side scheme. +/// +/// +/// The CORS allow-list () represents +/// the SPA / native-client origins. Its scheme follows +/// and its port follows +/// or , with the +/// port suffix dropped when the operator picked the scheme's default port (80 for http, +/// 443 for https). The primary host (first non-CIDR +/// entry) wins for the two single-value API fields; +/// CORS gets one entry per non-CIDR host. IPv6 literals are bracket-wrapped per +/// RFC 3986 §3.2.2. +/// +/// A non-empty stored value always wins over the derived default — derivation only +/// fills blanks, it never overwrites. +/// +/// +public sealed class ConfigPhaseResolveDerivedDefaultsTests +{ + [Test] + public async Task ApiUrlIsHttpsWithApiHttpsPortRegardlessOfWebHttpsFalse() + { + // The API container always terminates HTTPS in self-host — its Kestrel default + // endpoint binds to the bootstrapper-issued leaf PFX unconditionally, independent of + // Deployment.WebHttps (which only governs the web container). So the derived + // CallbackBaseUrl and JwtAuthority must say `https` and carry `:{Ports.ApiHttps}` + // even when the operator turned the web tier's HTTPS off. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: false, hosts: "api.example.com"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com:5001"); + // CORS still tracks the web tier — http on default Ports.WebHttp=8080 here. + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("http://api.example.com:8080"); + } + + [Test] + public async Task ApiUrlStaysHttpsWithApiHttpsPortWhenWebHttpsTrue() + { + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com:5001"); + // CORS scheme flips to https alongside WebHttps; port becomes Ports.WebHttps=8081. + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://api.example.com:8081"); + } + + [Test] + public async Task ApiHttpsPort443OmitsPortSuffix() + { + // Operator fronting the API with a 443-bound proxy (or rebinding Kestrel onto 443 + // directly) gets a clean URL without a port suffix — matches the canonical form for + // an https URI on its default port (RFC 7230 §2.7.2). + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.Ports.ApiHttps = 443; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com"); + } + + [Test] + public async Task CorsOmitsPortSuffixOnDefaultHttpsPort() + { + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.Ports.WebHttps = 443; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://api.example.com"); + } + + [Test] + public async Task CorsOmitsPortSuffixOnDefaultHttpPort() + { + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: false, hosts: "api.example.com"); + cfg.Ports.WebHttp = 80; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("http://api.example.com"); + } + + [Test] + public async Task PrimaryHostWinsForSingleValueFields() + { + // Two hosts supplied. CallbackBaseUrl and JwtAuthority must take only the FIRST + // non-CIDR one — they're single-value scalars, and the bootstrapper's contract says + // the operator's primary public host is what callbacks and JWT iss claims target. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime( + webHttps: true, + "api.example.com", "admin.example.com"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com:5001"); + // …but the second host still ends up in the CORS list (with the web port suffix). + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://api.example.com:8081"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://admin.example.com:8081"); + } + + [Test] + public async Task CorsListGetsOneEntryPerHost() + { + // CORS preserves all non-CIDR hosts: every leaf-eligible entry on the deployment side + // gets a matching entry in the allow-list, in the same order, with the scheme + port + // derived from WebHttps + the matching Ports.Web* slot. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime( + webHttps: false, + "api.example.com", "admin.example.com", "www.example.com"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(3); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins[0]).IsEqualTo("http://api.example.com:8080"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins[1]).IsEqualTo("http://admin.example.com:8080"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins[2]).IsEqualTo("http://www.example.com:8080"); + } + + [Test] + public async Task StoredCallbackBaseUrlWinsOverDerived() + { + // The "stored wins" rule applied to CallbackBaseUrl: if the operator supplied an + // explicit value (interactive or via the JSON), derivation must not overwrite it + // — even if the value differs from what derivation would produce. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.ApiRuntime.CallbackBaseUrl = "https://callback-override.example.com"; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://callback-override.example.com"); + // JwtAuthority was empty so it still derives. + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com:5001"); + } + + [Test] + public async Task StoredJwtAuthorityWinsOverDerived() + { + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.ApiRuntime.JwtAuthority = "https://issuer-override.example.com"; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://issuer-override.example.com"); + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + } + + [Test] + public async Task StoredCorsListWinsOverDerived() + { + // A non-empty CORS list (even with a single entry) must survive derivation untouched. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.ApiRuntime.CorsAllowedOrigins = ["https://custom-front.example.com"]; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(1); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins[0]).IsEqualTo("https://custom-front.example.com"); + } + + [Test] + public async Task IsIdempotent() + { + // Calling ResolveDerivedDefaults twice must produce the same end state as calling it + // once — once a value is filled, the second pass sees it as non-empty and skips it. + // The interactive form's Show callbacks fire on every redraw, so this property is + // load-bearing for menu stability. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + var firstCallback = cfg.ApiRuntime.CallbackBaseUrl; + var firstAuthority = cfg.ApiRuntime.JwtAuthority; + var firstCorsCount = cfg.ApiRuntime.CorsAllowedOrigins.Count; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo(firstCallback); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo(firstAuthority); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(firstCorsCount); + } + + [Test] + public async Task EmptyHostsLeavesEverythingUnset() + { + // ResolveDerivedDefaults has nothing to work from when Hosts is empty. Validate's + // host check will throw downstream, but ResolveDerivedDefaults itself must early-out + // gracefully rather than throwing or writing junk values. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(); + cfg.Deployment.Hosts = []; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo(string.Empty); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo(string.Empty); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(0); + } + + [Test] + public async Task DoesNotOverwriteJwtAudience() + { + // JwtAudience has a hardcoded property-initialiser default ("octocon") and is NOT a + // derivable field — derivation must leave it alone in every path. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: true, hosts: "api.example.com"); + cfg.ApiRuntime.JwtAudience = "custom-aud"; + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.JwtAudience).IsEqualTo("custom-aud"); + } + + [Test] + public async Task Ipv6PrimaryHostProducesBracketedUrl() + { + // RFC 3986 §3.2.2: a URI authority containing an IPv6 literal MUST bracket-wrap the + // literal so the colons aren't interpreted as port separators. Pins the + // HostParser.ToUrlHost behaviour as observed through the derivation surface that real + // consumers (callback URL, JWT issuer) see — the port suffix still appears after the + // closing bracket on a non-default port. + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: false, hosts: "::1"); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://[::1]:5001"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://[::1]:5001"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("http://[::1]:8080"); + } + + [Test] + public async Task CidrEntryIgnoredForUrlDerivation() + { + // CIDR entries widen the Name Constraints scope but have no URL form, so they must be + // skipped when picking the primary host and when building the CORS allow-list. + // ["192.168.1.0/24", "192.168.1.42"] → primary is 192.168.1.42; CORS has exactly one + // entry (the CIDR doesn't add a second). + var cfg = TestSupport.MakeConfigWithEmptyApiRuntime(webHttps: false, hosts: ["192.168.1.0/24", "192.168.1.42"]); + + ConfigPhase.ResolveDerivedDefaults(cfg); + + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://192.168.1.42:5001"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(1); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins[0]).IsEqualTo("http://192.168.1.42:8080"); + } +} + diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPreFillMdnsCheckTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPreFillMdnsCheckTests.cs new file mode 100644 index 00000000..b1a4724a --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigPreFillMdnsCheckTests.cs @@ -0,0 +1,242 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Drives directly via its +/// hostnameFactory + probe seams. The banner is Tier 1 of the two-tier mDNS +/// design: it runs BEFORE on the interactive +/// fresh-config path so the operator sees the mDNS status up-front and can make an informed +/// call about whether to include .local names in their hosts list. +/// +/// +/// Tests focus on the (hostname-or-null) return value and probe-call side effects — the +/// exact banner wording is asserted by the DinD integration tests which shell out to the +/// real binary and read stdout. TUnit's Source-Generated engine wraps +/// with an AsyncLocal-scoped capturer that our per-test can't +/// reliably reach, so we deliberately don't rely on it here. +/// +/// +/// The install-yes / install-no interactive branches are covered by the same integration +/// tests (both require a real keystroke). Under the unit +/// harness we drive the "no TTY" fork via the isInteractive test seam on +/// — +/// swaps the managed reader but does NOT change +/// , which is bound to the OS-level fd state. A +/// `dotnet test` run from an interactive terminal would therefore land in the +/// AnsiConsole.Prompt branch and hang / error; the seam sidesteps that entirely. +/// +/// +/// Serialisation via [NotInParallel("bootstrapper-console")] — every test +/// swaps process-global via +/// and restores it in a finally. That swap races if any sibling test also touches the +/// process-global console streams; sharing the bootstrapper-console NotInParallel +/// key with guarantees only one console-mutating +/// test in the assembly runs at a time. The same serialisation removes the +/// NamedPipeServer teardown race documented on +/// — see that class for the full rationale. +/// +/// +[NotInParallel("bootstrapper-console")] +public sealed class ConfigPreFillMdnsCheckTests +{ + private static BootstrapOptions OptionsFor(bool nonInteractive) => TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: "./deploy", + skipPrereqs: true, + nonInteractive: nonInteractive); + + /// + /// Ensures is true so the banner takes the "no + /// TTY" fork rather than blocking on a keystroke that + /// the unit harness can't drive. Restores the original stdin in a finally so sibling + /// tests aren't affected. + /// + private static async Task WithRedirectedStdinAsync(Func> body) + { + var prevIn = Console.In; + try + { +#pragma warning disable TUnit0055 + Console.SetIn(new StringReader(string.Empty)); +#pragma warning restore TUnit0055 + return await body().ConfigureAwait(false); + } + finally + { +#pragma warning disable TUnit0055 + Console.SetIn(prevIn); +#pragma warning restore TUnit0055 + } + } + + [Test] + public async Task PreFillReturnsNullWhenNonInteractive() + { + // Non-interactive callers get no banner — the post-fill gate handles any .local + // entries the operator supplied in their config file. Documents the "the banner is + // interactive-only" contract and pins that the probe is never called on this path + // (probe throws to catch a regression that would silently start shelling out to + // `getent` in non-interactive runs). + var options = OptionsFor(nonInteractive: true); + var logger = new PhaseLogger(options); + var probeCalled = false; + + var result = await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => "workstation.local", + probe: (_, _) => + { + probeCalled = true; + return Task.FromResult(true); + }); + + await Assert.That(result).IsNull(); + await Assert.That(probeCalled).IsFalse(); + } + + [Test] + public async Task PreFillReturnsNullWhenNoHostname() + { + // hostnameFactory returns null when the device has no short-name suitable for mDNS + // (empty, "localhost", already-qualified FQDN, invalid chars). In that case the + // banner logs a single info line and skips the probe entirely — asserted here via + // the "probe was never called" side effect since the info line goes through + // PhaseLogger → Console.WriteLine which TUnit intercepts before our test can see it. + var options = OptionsFor(nonInteractive: false); + var logger = new PhaseLogger(options); + var probeCalled = false; + + var result = await WithRedirectedStdinAsync(async () => + await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => null, + probe: (_, _) => + { + probeCalled = true; + return Task.FromResult(true); + })); + + await Assert.That(result).IsNull(); + await Assert.That(probeCalled).IsFalse(); + } + + [Test] + public async Task PreFillReturnsNullOnNonLinuxProbe() + { + // The probe returns null on non-Linux (getent doesn't exist in the shape the check + // needs) or on an environmental failure (getent binary missing, Process.Start + // failure). Callers treat that as "unknown — skip the pre-fill" so operators on a + // Windows / macOS dev workstation don't get an .local name silently baked into the + // hosts row. + var options = OptionsFor(nonInteractive: false); + var logger = new PhaseLogger(options); + var probedNames = new List(); + + var result = await WithRedirectedStdinAsync(async () => + await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => "workstation.local", + probe: (name, _) => + { + probedNames.Add(name); + return Task.FromResult(null); + })); + + await Assert.That(result).IsNull(); + // Exactly one probe — the null result short-circuits without attempting to install / + // re-probe. If a regression re-probes after the initial null, this fails loudly. + await Assert.That(probedNames.Count).IsEqualTo(1); + // Pin the probe input so a future refactor that (say) starts qualifying the + // hostname twice is caught immediately. + await Assert.That(probedNames[0]).IsEqualTo("workstation.local"); + } + + [Test] + public async Task PreFillReturnsHostnameWhenProbeResolves() + { + // Happy path — mDNS is working, {hostname}.local resolves via the local resolver + // chain, and the banner logs a confirmation info line before returning the hostname + // for the pre-fill. The returned hostname must match verbatim what the caller + // (PromptForConfig) will use as the pre-fill's leading hosts entry. + var options = OptionsFor(nonInteractive: false); + var logger = new PhaseLogger(options); + var probeCount = 0; + + var result = await WithRedirectedStdinAsync(async () => + await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => "workstation.local", + probe: (_, _) => + { + probeCount++; + return Task.FromResult(true); + })); + + await Assert.That(result).IsEqualTo("workstation.local"); + // Only one probe on the happy path — the "resolves" answer short-circuits the + // install + re-probe branches entirely. + await Assert.That(probeCount).IsEqualTo(1); + } + + [Test] + public async Task PreFillReturnsNullWhenProbeFailsAndNoTty() + { + // The probe returns false → mDNS is broken. In a real TTY we would then offer to + // install avahi; the `isInteractive: () => false` seam forces the "no TTY" fork + // so the install prompt is skipped. The banner still emits the "mDNS unavailable" + // warning + install hint (unit tests can't capture the warn line — see class + // summary), but the observable side effect is (a) result is null, (b) exactly + // one probe call — the install-recheck probe never fires because the + // confirmation prompt was skipped. + // + // NB: Console.SetIn in WithRedirectedStdinAsync doesn't affect + // Console.IsInputRedirected — see the class summary. The seam is the load-bearing + // mechanism here; the stdin redirect is belt-and-braces so a regression that + // dropped the seam wouldn't hang the test on an AnsiConsole.Prompt keystroke. + var options = OptionsFor(nonInteractive: false); + var logger = new PhaseLogger(options); + var probeCount = 0; + + var result = await WithRedirectedStdinAsync(async () => + await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => "workstation.local", + probe: (_, _) => + { + probeCount++; + return Task.FromResult(false); + }, + isInteractive: () => false)); + + await Assert.That(result).IsNull(); + await Assert.That(probeCount).IsEqualTo(1); + } + + [Test] + public async Task PreFillPassesQualifiedHostnameToProbe() + { + // Bakes the "the probe is called with the mDNS-qualified name, not the raw short + // hostname" contract into the test suite. HostnameDetector.QualifyForMdns is + // responsible for the .local suffixing; ApplyPreFillMdnsCheckAsync must pass the + // qualified name through verbatim so the getent lookup can traverse the + // mdns_minimal NSS module. + var options = OptionsFor(nonInteractive: false); + var logger = new PhaseLogger(options); + string? probedName = null; + + var result = await WithRedirectedStdinAsync(async () => + await ConfigPhase.ApplyPreFillMdnsCheckAsync( + options, logger, CancellationToken.None, + hostnameFactory: () => "my-server.local", + probe: (name, _) => + { + probedName = name; + return Task.FromResult(true); + })); + + await Assert.That(probedName).IsEqualTo("my-server.local"); + await Assert.That(result).IsEqualTo("my-server.local"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigReconfigureTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigReconfigureTests.cs new file mode 100644 index 00000000..2ac18505 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigReconfigureTests.cs @@ -0,0 +1,73 @@ +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// ConfigPhase --reconfigure gate: refuses NonInteractive / redirected stdin +/// when an existing JSON is present, so CI never blocks on Spectre prompts. +public sealed class ConfigReconfigureTests +{ + [Test] + public async Task ReconfigureWithNonInteractive_ThrowsBeforePrompt() + { + using var scratch = TestSupport.NewScratchDir("interfold-reconfigure-ni"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "interfold.bootstrap.json"); + var cfg = TestSupport.MakeConfig(tweak: c => + { + c.Deployment.Hosts = ["api.example.com"]; + c.Deployment.OutputDir = tmpDir; + }); + await File.WriteAllTextAsync( + configPath, + JsonSerializer.Serialize(cfg, BootstrapJsonContext.Default.BootstrapConfig)); + + var options = TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: tmpDir, + configPath: configPath, + skipPrereqs: true, + nonInteractive: true, + reconfigure: true); + var logger = new PhaseLogger(options); + + await Assert.That(async () => + await ConfigPhase.RunAsync(options, logger, CancellationToken.None)) + .Throws() + .WithMessageMatching("*--reconfigure*") + .And.WithMessageMatching("*interactive*"); + } + + [Test] + public async Task ExistingFileWithoutReconfigure_LoadsWithoutPrompt() + { + using var scratch = TestSupport.NewScratchDir("interfold-reconfigure-load"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "interfold.bootstrap.json"); + var cfg = TestSupport.MakeConfig(tweak: c => + { + c.Deployment.Hosts = ["loaded.example.com"]; + c.Deployment.OutputDir = tmpDir; + c.Deployment.RootCaName = "Loaded Root CA"; + }); + await File.WriteAllTextAsync( + configPath, + JsonSerializer.Serialize(cfg, BootstrapJsonContext.Default.BootstrapConfig)); + + var options = TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: tmpDir, + configPath: configPath, + skipPrereqs: true, + nonInteractive: true, + reconfigure: false); + var logger = new PhaseLogger(options); + + var loaded = await ConfigPhase.RunAsync(options, logger, CancellationToken.None); + + await Assert.That(loaded.Deployment.Hosts).IsEquivalentTo(["loaded.example.com"]); + await Assert.That(loaded.Deployment.RootCaName).IsEqualTo("Loaded Root CA"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigValidationTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigValidationTests.cs new file mode 100644 index 00000000..a6d4e1a6 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/ConfigValidationTests.cs @@ -0,0 +1,622 @@ +using System.Diagnostics; +using System.Text.Json; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.UnitTests; + +/// Each test stages one invariant violation and asserts the validator throws +/// with a message naming the offending field — operators see it inline on failure. +public sealed class ConfigValidationTests +{ + private static BootstrapConfig MakeValid() => new() + { + Deployment = + { + OutputDir = "./deploy", + Hosts = ["api.example.com"], + RootCaName = "Interfold Root CA", + CertYears = 5, + TrustStoreInstall = true, + }, + Ports = + { + ApiHttp = 5000, + ApiHttps = 5001, + WebHttp = 8080, + WebHttps = 8081, + }, + DatabaseMode = DatabaseMode.Single, + }; + + /// Mutates a valid config, invokes Validate, asserts every fragment appears in + /// the thrown message. Bespoke-config / non-throwing tests stay inline. + private static async Task AssertInvalidAsync( + Action mutate, + params string[] messageContains) + { + var cfg = MakeValid(); + mutate(cfg); + var ex = Assert.Throws(() => ConfigPhase.Validate(cfg)); + foreach (var frag in messageContains) + { + await Assert.That(ex.Message).Contains(frag); + } + } + + [Test] + public async Task ValidConfigPasses() + { + ConfigPhase.Validate(MakeValid()); + await Task.CompletedTask; + } + + [Test] + public Task EmptyHostsListFailsValidation() + => AssertInvalidAsync(c => c.Deployment.Hosts = [], "hosts"); + + [Test] + public Task ZeroCertYearsFailsValidation() + => AssertInvalidAsync(c => c.Deployment.CertYears = 0, "certYears"); + + [Test] + public Task NegativeCertYearsFailsValidation() + => AssertInvalidAsync(c => c.Deployment.CertYears = -1, "certYears"); + + [Test] + public Task CertYearsOverThirtyFailsValidation() + => AssertInvalidAsync(c => c.Deployment.CertYears = 31, "certYears"); + + [Test] + public Task ApiHttpEqualsApiHttpsFailsValidation() + // Same host port can't bind two listeners; the validator must surface this before publish. + => AssertInvalidAsync(c => + { + c.Ports.ApiHttp = 5000; + c.Ports.ApiHttps = 5000; + }, "apiHttp", "apiHttps"); + + [Test] + public Task DomainWithSpaceFailsValidation() + => AssertInvalidAsync(c => c.Deployment.Hosts = ["api example.com"], "whitespace"); + + [Test] + public async Task Ipv4HostPassesValidation() + { + // LAN-only self-host: IP-SAN'd leaf; derived URL is always https + :ApiHttps (5001). + var cfg = MakeValid(); + cfg.Deployment.Hosts = ["192.168.1.42"]; + + ConfigPhase.Validate(cfg); + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://192.168.1.42:5001"); + } + + [Test] + public async Task Ipv6HostPassesValidation() + { + // Derived URL must bracket-wrap the address per RFC 3986 §3.2.2. + var cfg = MakeValid(); + cfg.Deployment.Hosts = ["fe80::1"]; + + ConfigPhase.Validate(cfg); + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://[fe80::1]:5001"); + } + + [Test] + public async Task Ipv4CidrPlusDnsHostPassesValidation() + { + // DNS/IP acts as primary; CIDR widens root-CA Name Constraints scope. + var cfg = MakeValid(); + cfg.Deployment.Hosts = ["api.example.com", "10.0.0.0/8"]; + + ConfigPhase.Validate(cfg); + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + } + + [Test] + public async Task Ipv6CidrPlusIpv4HostPassesValidation() + { + var cfg = MakeValid(); + cfg.Deployment.Hosts = ["192.168.1.42", "fe80::/64"]; + + ConfigPhase.Validate(cfg); + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://192.168.1.42:5001"); + } + + [Test] + // CIDR-only has no leaf-eligible primary. + public Task AllCidrHostsFailValidation() + => AssertInvalidAsync(c => c.Deployment.Hosts = ["192.168.1.0/24", "fe80::/64"], "non-CIDR"); + + [Test] + // Surface HostParser's fix-it rather than silently normalising. + public Task CidrWithHostBitsSetFailsValidation() + => AssertInvalidAsync(c => c.Deployment.Hosts = ["192.168.1.42/24"], + "host bits", "192.168.1.0/24", "192.168.1.42/32"); + + [Test] + public async Task DefaultConstructedDeploymentHasNoHosts() + { + // Pins the "no placeholder" contract on Deployment.Hosts. + var deployment = new DeploymentSection(); + await Assert.That(deployment.Hosts.Count).IsEqualTo(0); + } + + [Test] + public async Task DefaultConstructedBootstrapConfigFailsValidation() + { + // Non-interactive callers omitting deployment.hosts must fail precisely, not silently. + var cfg = new BootstrapConfig(); + + var ex = Assert.Throws(() => ConfigPhase.Validate(cfg)); + await Assert.That(ex.Message).Contains("hosts"); + await Assert.That(ex.Message).Contains("at least one"); + } + + [Test] + public Task PortAboveMaxFailsValidation() + => AssertInvalidAsync(c => c.Ports.ApiHttp = 70000, "ApiHttp"); + + [Test] + public async Task InvalidDatabaseModeInJsonFailsDeserialization() + { + // Rejection lives in the source-generated context, not Validate. + const string badJson = """ + { + "databaseMode": "quadruple-redundant" + } + """; + var ex = Assert.Throws(() => + JsonSerializer.Deserialize(badJson, BootstrapJsonContext.Default.BootstrapConfig)); + await Assert.That(ex.Message).Contains("DatabaseMode"); + await Assert.That(ex.Message).Contains("databaseMode"); + } + + [Test] + public async Task DefaultPostgresDatabasePasses() + { + var cfg = MakeValid(); + cfg.PostgresDatabase = "interfold"; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public async Task CustomSafePostgresDatabasePasses() + { + // Exercise underscores + digits (typical env-suffixed name). + var cfg = MakeValid(); + cfg.PostgresDatabase = "acme_prod_42"; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public Task EmptyPostgresDatabaseFailsValidation() + => AssertInvalidAsync(c => c.PostgresDatabase = string.Empty, "postgresDatabase"); + + [Test] + public Task WhitespacePostgresDatabaseFailsValidation() + => AssertInvalidAsync(c => c.PostgresDatabase = " ", "postgresDatabase"); + + [Test] + // Postgres tolerates a leading digit only inside quotes; forbid up front to avoid drift. + public Task PostgresDatabaseStartingWithDigitFailsValidation() + => AssertInvalidAsync(c => c.PostgresDatabase = "1interfold", "postgresDatabase"); + + [Test] + // Dashes need quoting; forbidding them keeps the name reusable as a role/schema prefix. + public Task PostgresDatabaseWithDashFailsValidation() + => AssertInvalidAsync(c => c.PostgresDatabase = "inter-fold", "postgresDatabase"); + + [Test] + public async Task DefaultClusterNamePasses() + { + var cfg = MakeValid(); + cfg.ClusterName = "InterfoldCluster"; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public async Task CustomClusterNameWithSpacesPasses() + { + // Spaces are legitimate here (advertised in gossip / DESCRIBE CLUSTER). + var cfg = MakeValid(); + cfg.ClusterName = "Acme Prod 1.0"; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public Task EmptyClusterNameFailsValidation() + => AssertInvalidAsync(c => c.ClusterName = string.Empty, "clusterName"); + + [Test] + // A raw quote would corrupt the Cassandra entrypoint's cassandra.yaml rewrite. + public Task ClusterNameWithSingleQuoteFailsValidation() + => AssertInvalidAsync(c => c.ClusterName = "Acme'Prod", "clusterName"); + + [Test] + public Task ClusterNameWithNewlineFailsValidation() + => AssertInvalidAsync(c => c.ClusterName = "Acme\nProd", "clusterName"); + + [Test] + // 64 chars is the published Cassandra limit. + public Task OverlyLongClusterNameFailsValidation() + => AssertInvalidAsync(c => c.ClusterName = new string('A', 65), "clusterName"); + + [Test] + public async Task InvalidScyllaKeyspaceInJsonFailsDeserialization() + { + // Rejection lives in the JSON converter, not Validate. + const string badJson = """ + { + "scyllaKeyspace": "antarctica" + } + """; + var ex = Assert.Throws(() => + JsonSerializer.Deserialize(badJson, BootstrapJsonContext.Default.BootstrapConfig)); + await Assert.That(ex.Message).Contains("ScyllaKeyspace"); + await Assert.That(ex.Message).Contains("scyllaKeyspace"); + } + + [Test] + public async Task EachValidScyllaKeyspacePasses() + { + // Iterates so future additions fail here first. + foreach (var keyspace in Enum.GetValues()) + { + var cfg = MakeValid(); + cfg.ScyllaKeyspace = keyspace; + ConfigPhase.Validate(cfg); + } + await Task.CompletedTask; + } + + [Test] + public Task NonHttpCallbackBaseUrlFailsValidation() + => AssertInvalidAsync(c => c.ApiRuntime.CallbackBaseUrl = "ftp://api.example.com", "callbackBaseUrl"); + + [Test] + // Even with a property-initialiser default, `"": ""` from hand-edited JSON must reject. + public Task EmptyJwtAudienceFailsValidation() + => AssertInvalidAsync(c => c.ApiRuntime.JwtAudience = "", "jwtAudience"); + + [Test] + public Task NonHttpCorsOriginFailsValidation() + => AssertInvalidAsync(c => c.ApiRuntime.CorsAllowedOrigins = ["https://app.example.com", "not-a-url"], + "corsAllowedOrigins"); + + [Test] + public async Task ValidateFillsDerivedApiRuntimeDefaults() + { + // Validate is a mutating check — it materialises apiRuntime defaults for JSON callers. + var cfg = MakeValid(); + cfg.Deployment.Hosts = ["api.example.com", "admin.example.com"]; + cfg.Deployment.WebHttps = true; + cfg.ApiRuntime.CallbackBaseUrl = string.Empty; + cfg.ApiRuntime.JwtAuthority = string.Empty; + cfg.ApiRuntime.CorsAllowedOrigins = []; + + ConfigPhase.Validate(cfg); + + // API URL is always https + ApiHttps (5001); CORS scheme follows WebHttps + WebHttps port. + await Assert.That(cfg.ApiRuntime.CallbackBaseUrl).IsEqualTo("https://api.example.com:5001"); + await Assert.That(cfg.ApiRuntime.JwtAuthority).IsEqualTo("https://api.example.com:5001"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins.Count).IsEqualTo(2); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://api.example.com:8081"); + await Assert.That(cfg.ApiRuntime.CorsAllowedOrigins).Contains("https://admin.example.com:8081"); + } + + // --- Cluster / Storage / Observability / Socket / Persistence tuning validation --- + + [Test] + public async Task InvalidNodeGroupInJsonFailsDeserialization() + { + // Rejection lives in the JSON converter, not Validate. + const string badJson = """ + { + "cluster": { "nodeGroup": "guardian" } + } + """; + var ex = Assert.Throws(() => + JsonSerializer.Deserialize(badJson, BootstrapJsonContext.Default.BootstrapConfig)); + await Assert.That(ex.Message).Contains("NodeGroup"); + await Assert.That(ex.Message).Contains("nodeGroup"); + } + + [Test] + public async Task EachValidNodeGroupPasses() + { + // Drives future allow-list extensions to fail here first. + foreach (var nodeGroup in Enum.GetValues()) + { + var cfg = MakeValid(); + cfg.Cluster.NodeGroup = nodeGroup; + ConfigPhase.Validate(cfg); + } + await Task.CompletedTask; + } + + [Test] + public async Task EmptyOptionalStringsPass() + { + // Empty Avatar* / OtlpEndpoint = "feature disabled"; must not fail validation. + var cfg = MakeValid(); + cfg.Storage.AvatarStorageRoot = string.Empty; + cfg.Storage.AvatarPublicBase = string.Empty; + cfg.Observability.OtlpEndpoint = string.Empty; + cfg.Socket.BatchBytesThreshold = null; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public Task NonHttpAvatarPublicBaseFailsValidation() + => AssertInvalidAsync(c => c.Storage.AvatarPublicBase = "ftp://cdn.example.com/avatars/", "avatarPublicBase"); + + [Test] + // Relative paths resolve against the API container CWD → silent breakage. + public Task RelativeAvatarStorageRootFailsValidation() + => AssertInvalidAsync(c => c.Storage.AvatarStorageRoot = "avatars", "avatarStorageRoot"); + + [Test] + public Task NonHttpOtlpEndpointFailsValidation() + => AssertInvalidAsync(c => c.Observability.OtlpEndpoint = "grpc://otel-collector:4317", "otlpEndpoint"); + + [Test] + public Task ZeroDbRetryAttemptsFailsValidation() + => AssertInvalidAsync(c => c.Persistence.DbRetryAttempts = 0, "dbRetryAttempts"); + + [Test] + public Task DbRetryAttemptsAboveCapFailsValidation() + => AssertInvalidAsync(c => c.Persistence.DbRetryAttempts = 9999, "dbRetryAttempts"); + + [Test] + // Cross-check for the easy swap mistake (initial > max makes the cap below the start). + public Task DbRetryMaxBelowInitialFailsValidation() + => AssertInvalidAsync(c => + { + c.Persistence.DbRetryInitialDelayMs = 500; + c.Persistence.DbRetryMaxDelayMs = 100; + }, "dbRetryMaxDelayMs", "dbRetryInitialDelayMs"); + + [Test] + public Task HydrationConcurrencyAboveCapFailsValidation() + => AssertInvalidAsync(c => c.Persistence.HydrationMaxConcurrency = 9999, "hydrationMaxConcurrency"); + + [Test] + // Nullable → null passes, but 1..16 MiB range enforced when supplied. + public Task SocketBatchThresholdOutOfRangeFailsValidation() + => AssertInvalidAsync(c => c.Socket.BatchBytesThreshold = 0, "batchBytesThreshold"); + + [Test] + public async Task DefaultBackupSectionPasses() + { + // Shipped defaults are the "no-op" stance and must pass day one. + var cfg = MakeValid(); + await Assert.That(cfg.Backup.RetainCount).IsEqualTo(14); + await Assert.That(cfg.Backup.Schedule).IsEqualTo("daily"); + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + // 0 would delete every backup as it's written — likely a typo for `enabled=false`. + public Task ZeroBackupRetainCountFailsValidation() + => AssertInvalidAsync(c => c.Backup.RetainCount = 0, "retainCount"); + + [Test] + public Task NegativeBackupRetainCountFailsValidation() + => AssertInvalidAsync(c => c.Backup.RetainCount = -5, "retainCount"); + + [Test] + // 1000 is the documented cap; larger values are almost always a units mistake. + public Task BackupRetainCountAboveCapFailsValidation() + => AssertInvalidAsync(c => c.Backup.RetainCount = 5000, "retainCount"); + + [Test] + public Task EmptyBackupScheduleFailsValidation() + => AssertInvalidAsync(c => c.Backup.Schedule = string.Empty, "schedule"); + + [Test] + public Task WhitespaceBackupScheduleFailsValidation() + => AssertInvalidAsync(c => c.Backup.Schedule = " ", "schedule"); + + [Test] + // Belt-and-braces: string lands on OnCalendar=, but reject shell chaining upfront. + public Task BackupScheduleWithShellMetacharactersFailsValidation() + => AssertInvalidAsync(c => c.Backup.Schedule = "daily;rm -rf /", "schedule"); + + [Test] + public async Task WellKnownBackupScheduleShortcutsPass() + { + foreach (var schedule in new[] { "hourly", "daily", "weekly", "monthly", "*-*-* 03:00", "Mon..Fri 03:30", "Sat *-*-* 04,16:00:00" }) + { + var cfg = MakeValid(); + cfg.Backup.Schedule = schedule; + ConfigPhase.Validate(cfg); + } + await Task.CompletedTask; + } + + [Test] + // Relative paths resolve against systemd's unpredictable CWD. + public Task RelativeBackupDirectoryFailsValidation() + => AssertInvalidAsync(c => c.Backup.Directory = "backups/interfold", "directory"); + + [Test] + public async Task EmptyBackupDirectoryPasses() + { + // Empty = default ({outputDir}/backups). + var cfg = MakeValid(); + cfg.Backup.Directory = string.Empty; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public async Task AutostartTogglePassesIndependently() + { + // Toggles are orthogonal. + foreach (var enabled in new[] { true, false }) + { + foreach (var autostart in new[] { true, false }) + { + var cfg = MakeValid(); + cfg.Backup.Enabled = enabled; + cfg.Backup.AutostartServer = autostart; + ConfigPhase.Validate(cfg); + } + } + await Task.CompletedTask; + } + + [Test] + public Task UpdateHealthCheckTimeoutZeroFailsValidation() + => AssertInvalidAsync(c => c.Update.HealthCheckTimeoutSeconds = 0, "healthCheckTimeoutSeconds"); + + [Test] + public Task UpdateHealthCheckTimeoutAboveMaxFailsValidation() + => AssertInvalidAsync(c => c.Update.HealthCheckTimeoutSeconds = 3601, "healthCheckTimeoutSeconds"); + + [Test] + public async Task UpdateHealthCheckTimeoutInRangePasses() + { + // 1s and 3600s are the inclusive bounds. + var cfg = MakeValid(); + cfg.Update.HealthCheckTimeoutSeconds = 1; + ConfigPhase.Validate(cfg); + + cfg.Update.HealthCheckTimeoutSeconds = 3600; + ConfigPhase.Validate(cfg); + + await Task.CompletedTask; + } + + [Test] + // Fail here with a clear name instead of "no such service" from docker compose. + public Task UpdateServicesUnknownEntryFailsValidation() + => AssertInvalidAsync(c => c.Update.Services = ["msg-database"], "msg-database", "msg-db"); + + [Test] + public Task UpdateServicesBlankEntryFailsValidation() + => AssertInvalidAsync(c => c.Update.Services = ["msg-db", ""], "blank entry"); + + [Test] + public async Task UpdateServicesKnownEntriesPassValidation() + { + foreach (var svc in ConfigPhase.ValidUpdateServices) + { + var cfg = MakeValid(); + cfg.Update.Services = [svc]; + ConfigPhase.Validate(cfg); + } + await Task.CompletedTask; + } + + [Test] + public async Task UpdateServicesEmptyPasses() + { + // Empty = "every service" (the default). + var cfg = MakeValid(); + cfg.Update.Services = []; + + ConfigPhase.Validate(cfg); + await Task.CompletedTask; + } + + [Test] + public async Task UpdateTogglePassesIndependently() + { + // All three toggles are orthogonal. + foreach (var enabled in new[] { true, false }) + { + foreach (var autoRestore in new[] { true, false }) + { + foreach (var recreate in new[] { true, false }) + { + var cfg = MakeValid(); + cfg.Update.Enabled = enabled; + cfg.Update.AutoRestoreOnFailure = autoRestore; + cfg.Update.RecreateOnUpdate = recreate; + ConfigPhase.Validate(cfg); + } + } + } + await Task.CompletedTask; + } + + [Test] + public async Task MalformedJsonReturnsClearError() + { + // Drive `publish` so prereqs are skipped (Windows/macOS-friendly). + using var scratch = TestSupport.NewScratchDir("interfold-cfg-malformed"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "interfold.bootstrap.json"); + await File.WriteAllTextAsync(configPath, "{ this is not valid json"); + + var result = await RunBootstrapperAsync("publish", "--config", configPath, + "--output-dir", tmpDir, "--non-interactive"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0) + .Because("malformed JSON must abort the bootstrap"); + // JsonException message varies by SDK version but always mentions parsing. + var combined = result.Stdout + result.Stderr; + await Assert.That(combined).Contains("JSON") + .Or.Contains("json") + .Or.Contains("parse"); + } + + [Test] + public async Task MissingFileWithNonInteractiveExitsWithMessage() + { + // Drive `publish` so prereqs are skipped on non-Linux hosts. + using var scratch = TestSupport.NewScratchDir("interfold-cfg-missing-ni"); + var tmpDir = scratch.Path; + var configPath = Path.Combine(tmpDir, "interfold.bootstrap.json"); + await Assert.That(File.Exists(configPath)).IsFalse(); + + var result = await RunBootstrapperAsync("publish", "--config", configPath, + "--output-dir", tmpDir, "--non-interactive"); + + await Assert.That(result.ExitCode).IsNotEqualTo(0); + await Assert.That(result.Stdout + result.Stderr) + .Contains("Config file not found") + .Or.Contains("non-interactive"); + } + + /// Shells out to the compiled binary so tests exercise the operator dispatch + /// path; TestSupport.BootstrapperBinaryOrSkip locates the Debug build. + private static async Task<(int ExitCode, string Stdout, string Stderr)> RunBootstrapperAsync(params string[] args) + { + var path = TestSupport.BootstrapperBinaryOrSkip(); + var psi = new ProcessStartInfo + { + FileName = "dotnet", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + // Redirecting stdin eliminates the prompt path even if the host has a TTY. + RedirectStandardInput = true, + }; + psi.ArgumentList.Add(path); + foreach (var a in args) psi.ArgumentList.Add(a); + + using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start dotnet"); + proc.StandardInput.Close(); + var stdout = await proc.StandardOutput.ReadToEndAsync(); + var stderr = await proc.StandardError.ReadToEndAsync(); + await proc.WaitForExitAsync(); + return (proc.ExitCode, stdout, stderr); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/EmbeddedSupportFilesTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/EmbeddedSupportFilesTests.cs new file mode 100644 index 00000000..6c669633 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/EmbeddedSupportFilesTests.cs @@ -0,0 +1,128 @@ +using System.Runtime.InteropServices; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Enums; +using TUnit.Core.Exceptions; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Validates the embed-with-override contract: open streams from assembly resources and +/// materialize to disk only when a host path is required. +/// +public sealed class EmbeddedSupportFilesTests +{ + private const string ResourcePrefix = EmbeddedSupportFiles.ResourcePrefix; + + private static BootstrapOptions OptionsFor() => TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: Path.GetTempPath(), + skipPrereqs: true, + nonInteractive: true); + + private static IReadOnlyList EnumerateSupportResources() => + EmbeddedSupportFiles.EnumerateSupportResourceNames(); + + private static byte[] ReadResourceBytes(string resourceName) + { + using var stream = typeof(EmbeddedSupportFiles).Assembly.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException($"Manifest resource '{resourceName}' not found."); + using var ms = new MemoryStream(); + stream.CopyTo(ms); + return ms.ToArray(); + } + + [Test] + public async Task OpensEveryEmbeddedSupportResource() + { + var resources = EnumerateSupportResources(); + await Assert.That(resources.Count).IsGreaterThanOrEqualTo(10) + .Because("at least the 10 originally-embedded support files should still ship"); + + foreach (var name in resources) + { + var relative = name[ResourcePrefix.Length..]; + using var stream = EmbeddedSupportFiles.Open(relative); + await Assert.That(stream.CanRead).IsTrue() + .Because($"resource '{name}' should open for read"); + } + } + + [Test] + public async Task MaterializeWritesMissingFileWithOriginalContent() + { + using var scratch = TestSupport.NewScratchDir("interfold-embed"); + const string relative = "db/scylla/cassandra-rackdc.nam.properties"; + var target = Path.Combine(scratch.Path, "rackdc.properties"); + + var wrote = EmbeddedSupportFiles.Materialize(relative, target, new PhaseLogger(OptionsFor())); + await Assert.That(wrote).IsTrue(); + + var expected = ReadResourceBytes(ResourcePrefix + relative); + var actual = await File.ReadAllBytesAsync(target); + await Assert.That(actual.SequenceEqual(expected)).IsTrue(); + } + + [Test] + public async Task MaterializePreservesOperatorOverrideForExistingFiles() + { + using var scratch = TestSupport.NewScratchDir("interfold-embed"); + const string relative = "db/scylla/cassandra-rackdc.nam.properties"; + var target = Path.Combine(scratch.Path, "rackdc.properties"); + Directory.CreateDirectory(scratch.Path); + + var operatorContent = "# operator-customised content for unit test\n"u8.ToArray(); + await File.WriteAllBytesAsync(target, operatorContent); + + var wrote = EmbeddedSupportFiles.Materialize(relative, target, new PhaseLogger(OptionsFor())); + await Assert.That(wrote).IsFalse(); + + var afterRun = await File.ReadAllBytesAsync(target); + await Assert.That(afterRun.SequenceEqual(operatorContent)).IsTrue(); + } + + [Test] + public async Task MaterializeUnderSupportRootResolvesUnderOutputDir() + { + using var scratch = TestSupport.NewScratchDir("interfold-embed"); + var path = EmbeddedSupportFiles.MaterializeUnderSupportRoot( + scratch.Path, EmbeddedSupportFiles.NginxTemplateRelative); + + await Assert.That(path.StartsWith(EmbeddedSupportFiles.SupportRoot(scratch.Path))).IsTrue(); + await Assert.That(File.Exists(path)).IsTrue(); + } + + [Test] + public async Task SetsExecutableBitOnShellScriptsWhenMaterializedOnUnix() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + throw new SkipTestException("Executable-bit assertion is Unix-only."); + } + + using var scratch = TestSupport.NewScratchDir("interfold-embed"); + const string relative = "scripts/docker/ensure-host-aio.sh"; + var target = Path.Combine(scratch.Path, "ensure-host-aio.sh"); + EmbeddedSupportFiles.Materialize(relative, target, new PhaseLogger(OptionsFor())); + + var mode = File.GetUnixFileMode(target); + await Assert.That(mode.HasFlag(UnixFileMode.UserExecute)).IsTrue(); + } + + [Test] + public async Task StagePublishSupportFilesMaterializesRackdcUnderOutputSupport() + { + using var scratch = TestSupport.NewScratchDir("interfold-publish-support"); + var config = new BootstrapConfig { DatabaseMode = DatabaseMode.Single }; + config.Deployment.Hosts = ["api.example.com"]; + ConfigPhase.ResolveDerivedDefaults(config); + PublishPhase.StagePublishSupportFiles(config, scratch.Path, new PhaseLogger(OptionsFor())); + + var expected = EmbeddedSupportFiles.SupportFilePath( + scratch.Path, EmbeddedSupportFiles.RackDcRelative("nam")); + await Assert.That(File.Exists(expected)).IsTrue(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/FirebaseFolderScannerTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/FirebaseFolderScannerTests.cs new file mode 100644 index 00000000..37978376 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/FirebaseFolderScannerTests.cs @@ -0,0 +1,128 @@ +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +public sealed class FirebaseFolderScannerTests +{ + private const string ServiceAccountFixture = """ + { + "type": "service_account", + "project_id": "octocon-test", + "private_key_id": "abc123xyz", + "private_key": "-----BEGIN PRIVATE KEY-----\nfake\n-----END PRIVATE KEY-----\n", + "client_email": "firebase-adminsdk@octocon-test.iam.gserviceaccount.com", + "client_id": "111", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token" + } + """; + + [Test] + public async Task ScanFindsAllFourCanonicalFiles() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + File.WriteAllText(Path.Combine(folder, "google-services.json"), "{}"); + File.WriteAllText(Path.Combine(folder, "GoogleService-Info.plist"), ""); + File.WriteAllText(Path.Combine(folder, "firebase-web-config.json"), "{}"); + File.WriteAllText(Path.Combine(folder, "octocon-firebase-adminsdk-abc123.json"), ServiceAccountFixture); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.AndroidConfigPath) + .IsEqualTo(Path.GetFullPath(Path.Combine(folder, "google-services.json"))); + await Assert.That(result.Section.IosConfigPath) + .IsEqualTo(Path.GetFullPath(Path.Combine(folder, "GoogleService-Info.plist"))); + await Assert.That(result.Section.WebConfigPath) + .IsEqualTo(Path.GetFullPath(Path.Combine(folder, "firebase-web-config.json"))); + await Assert.That(result.Section.ServiceAccountPath) + .IsEqualTo(Path.GetFullPath(Path.Combine(folder, "octocon-firebase-adminsdk-abc123.json"))); + await Assert.That(result.Missing).IsEmpty(); + } + + [Test] + public async Task ScanReportsMissingPlatformsForPartialFolder() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + File.WriteAllText(Path.Combine(folder, "google-services.json"), "{}"); + File.WriteAllText(Path.Combine(folder, "myproject-firebase-adminsdk-xyz.json"), ServiceAccountFixture); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.AndroidConfigPath).IsNotEmpty(); + await Assert.That(result.Section.ServiceAccountPath).IsNotEmpty(); + await Assert.That(result.Section.IosConfigPath).IsEmpty(); + await Assert.That(result.Section.WebConfigPath).IsEmpty(); + await Assert.That(result.Missing).IsEquivalentTo(new[] { "ios", "web" }); + } + + [Test] + public async Task ScanPrefersGlobMatchForServiceAccount() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + var globPath = Path.Combine(folder, "octocon-firebase-adminsdk-abc.json"); + var sniffPath = Path.Combine(folder, "renamed-service-account.json"); + File.WriteAllText(globPath, ServiceAccountFixture); + File.WriteAllText(sniffPath, ServiceAccountFixture); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.ServiceAccountPath).IsEqualTo(Path.GetFullPath(globPath)); + } + + [Test] + public async Task ScanFallsBackToContentSniffWhenGlobMisses() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + var renamed = Path.Combine(folder, "renamed-service-account.json"); + File.WriteAllText(renamed, ServiceAccountFixture); + File.WriteAllText(Path.Combine(folder, "not-a-service-account.json"), """{"foo":"bar"}"""); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.ServiceAccountPath).IsEqualTo(Path.GetFullPath(renamed)); + } + + [Test] + public async Task ScanSkipsUnrelatedJsonFilesInSniffFallback() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + File.WriteAllText(Path.Combine(folder, "eslintrc.json"), """{"rules":{}}"""); + File.WriteAllText(Path.Combine(folder, "tsconfig.json"), """{"compilerOptions":{}}"""); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.AndroidConfigPath).IsEmpty(); + await Assert.That(result.Section.IosConfigPath).IsEmpty(); + await Assert.That(result.Section.WebConfigPath).IsEmpty(); + await Assert.That(result.Section.ServiceAccountPath).IsEmpty(); + await Assert.That(result.Missing).IsEquivalentTo(new[] { "android", "ios", "web", "service_account" }); + } + + [Test] + public async Task ScanIgnoresUnparseableJsonInSniffFallback() + { + using var scratch = TestSupport.NewScratchDir("firebase-scanner"); + var folder = scratch.Path; + File.WriteAllText(Path.Combine(folder, "broken.json"), "{ not json"); + var real = Path.Combine(folder, "real.json"); + File.WriteAllText(real, ServiceAccountFixture); + + var result = FirebaseFolderScanner.Scan(folder); + + await Assert.That(result.Section.ServiceAccountPath).IsEqualTo(Path.GetFullPath(real)); + } + + [Test] + public async Task ScanThrowsWhenFolderDoesNotExist() + { + var missing = Path.Combine(Path.GetTempPath(), "firebase-scanner-missing-" + Guid.NewGuid().ToString("N")); + + await Assert.That(() => FirebaseFolderScanner.Scan(missing)) + .Throws(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/FirebasePhaseTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/FirebasePhaseTests.cs new file mode 100644 index 00000000..d1f27c43 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/FirebasePhaseTests.cs @@ -0,0 +1,334 @@ +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Settings.Contracts.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Pins the shape contract of : which fields it extracts from +/// each Firebase input, the snake_case naming policy on the emitted seed JSON, and the +/// "empty path means skip" semantics for unconfigured platforms. Runs under +/// JsonSerializerIsReflectionEnabledByDefault=false so missing source-gen coverage +/// fails here the same way the PublishTrimmed ELF fails in production. +/// +public sealed class FirebasePhaseTests +{ + private static PhaseLogger Logger() => new(TestOptions("/tmp")); + + private static BootstrapOptions TestOptions(string outputDir) => new( + Command: BootstrapCommand.Bootstrap, + ConfigPath: null, + OutputDir: outputDir, + SkipPrereqs: true, + RotateSecrets: false, + RotateCerts: false, + NonInteractive: true, + FaultInject: null, + PrintPhaseStatus: false); + + private static string NewTempDir() + { + var path = Path.Combine(Path.GetTempPath(), "firebase-phase-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + return path; + } + + private const string AndroidFixture = /*lang=json,strict*/ """ + { + "project_info": { + "project_number": "111222333444", + "project_id": "octocon-test", + "storage_bucket": "octocon-test.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:111222333444:android:abcdef1234567890", + "android_client_info": { "package_name": "app.octocon.android" } + }, + "api_key": [ + { "current_key": "AIzaSyTest-Android-Key" } + ] + } + ] + } + """; + + private const string IosFixture = /*lang=xml*/ """ + + + + + API_KEYAIzaSyTest-Ios-Key + GOOGLE_APP_ID1:111222333444:ios:1234567890abcdef + GCM_SENDER_ID111222333444 + PROJECT_IDoctocon-test + STORAGE_BUCKEToctocon-test.appspot.com + BUNDLE_IDapp.octocon.ios + CLIENT_ID111222333444-abc.apps.googleusercontent.com + IS_ADS_ENABLED + + + """; + + private const string WebFixture = /*lang=json,strict*/ """ + { + "apiKey": "AIzaSyTest-Web-Key", + "authDomain": "octocon-test.firebaseapp.com", + "projectId": "octocon-test", + "storageBucket": "octocon-test.appspot.com", + "messagingSenderId": "111222333444", + "appId": "1:111222333444:web:1234567890abcdef", + "vapidKey": "BOx-test-vapid-key" + } + """; + + private const string ServiceAccountFixture = /*lang=json,strict*/ """ + { + "type": "service_account", + "project_id": "octocon-test", + "private_key_id": "abc123", + "private_key": "-----BEGIN PRIVATE KEY-----\nfake\n-----END PRIVATE KEY-----\n", + "client_email": "sa@octocon-test.iam.gserviceaccount.com", + "client_id": "111222333444" + } + """; + + [Test] + public async Task RunAsync_AllFourInputsPresent_ProducesFourNonNullSeedValues() + { + var dir = NewTempDir(); + try + { + var android = Path.Combine(dir, "google-services.json"); + var ios = Path.Combine(dir, "GoogleService-Info.plist"); + var web = Path.Combine(dir, "firebase-web-config.json"); + var sa = Path.Combine(dir, "sa.json"); + File.WriteAllText(android, AndroidFixture); + File.WriteAllText(ios, IosFixture); + File.WriteAllText(web, WebFixture); + File.WriteAllText(sa, ServiceAccountFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection + { + AndroidConfigPath = android, + IosConfigPath = ios, + WebConfigPath = web, + ServiceAccountPath = sa, + }, + }; + + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + await Assert.That(result.AndroidClientJson).IsNotNull(); + await Assert.That(result.IosClientJson).IsNotNull(); + await Assert.That(result.WebClientJson).IsNotNull(); + await Assert.That(result.ServiceAccountJson).IsNotNull(); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task AndroidJson_RoundTripsIntoFirebaseAndroidClientConfig() + { + var dir = NewTempDir(); + try + { + var android = Path.Combine(dir, "gs.json"); + File.WriteAllText(android, AndroidFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection { AndroidConfigPath = android }, + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + var parsed = JsonSerializer.Deserialize( + result.AndroidClientJson!, + FirebaseSnakeCaseWriteContext.Default.FirebaseAndroidClientConfig)!; + await Assert.That(parsed).IsNotNull() + .Because("The bootstrapper's emitted JSON must deserialise cleanly into the API's typed record under the same snake_case options SecretsBootstrapService uses."); + await Assert.That(parsed.ApiKey).IsEqualTo("AIzaSyTest-Android-Key"); + await Assert.That(parsed.ApplicationId).IsEqualTo("1:111222333444:android:abcdef1234567890"); + await Assert.That(parsed.ProjectId).IsEqualTo("octocon-test"); + await Assert.That(parsed.GcmSenderId).IsEqualTo("111222333444"); + await Assert.That(parsed.StorageBucket).IsEqualTo("octocon-test.appspot.com"); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task IosPlist_RoundTripsIntoFirebaseIosClientConfig() + { + var dir = NewTempDir(); + try + { + var ios = Path.Combine(dir, "gs.plist"); + File.WriteAllText(ios, IosFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection { IosConfigPath = ios }, + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + var parsed = JsonSerializer.Deserialize( + result.IosClientJson!, + FirebaseSnakeCaseWriteContext.Default.FirebaseIosClientConfig)!; + await Assert.That(parsed).IsNotNull(); + await Assert.That(parsed.ApiKey).IsEqualTo("AIzaSyTest-Ios-Key"); + await Assert.That(parsed.GoogleAppId).IsEqualTo("1:111222333444:ios:1234567890abcdef"); + await Assert.That(parsed.GcmSenderId).IsEqualTo("111222333444"); + await Assert.That(parsed.ProjectId).IsEqualTo("octocon-test"); + await Assert.That(parsed.StorageBucket).IsEqualTo("octocon-test.appspot.com"); + await Assert.That(parsed.BundleId).IsEqualTo("app.octocon.ios"); + await Assert.That(parsed.ClientId).IsEqualTo("111222333444-abc.apps.googleusercontent.com"); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task WebJson_AcceptsCamelCaseInput_AndRoundTripsIntoFirebaseWebClientConfig() + { + var dir = NewTempDir(); + try + { + var web = Path.Combine(dir, "web.json"); + File.WriteAllText(web, WebFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection { WebConfigPath = web }, + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + var parsed = JsonSerializer.Deserialize( + result.WebClientJson!, + FirebaseSnakeCaseWriteContext.Default.FirebaseWebClientConfig)!; + await Assert.That(parsed).IsNotNull() + .Because("The camelCase console output must be normalised into snake_case so the API's SnakeCaseLower deserialiser accepts the seeded row."); + await Assert.That(parsed.ApiKey).IsEqualTo("AIzaSyTest-Web-Key"); + await Assert.That(parsed.AuthDomain).IsEqualTo("octocon-test.firebaseapp.com"); + await Assert.That(parsed.ProjectId).IsEqualTo("octocon-test"); + await Assert.That(parsed.StorageBucket).IsEqualTo("octocon-test.appspot.com"); + await Assert.That(parsed.MessagingSenderId).IsEqualTo("111222333444"); + await Assert.That(parsed.AppId).IsEqualTo("1:111222333444:web:1234567890abcdef"); + await Assert.That(parsed.VapidKey).IsEqualTo("BOx-test-vapid-key"); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task ServiceAccount_IsPassedThroughVerbatim() + { + var dir = NewTempDir(); + try + { + var sa = Path.Combine(dir, "sa.json"); + File.WriteAllText(sa, ServiceAccountFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection { ServiceAccountPath = sa }, + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + await Assert.That(result.ServiceAccountJson).IsEqualTo(ServiceAccountFixture) + .Because("The FCM SDK's GoogleCredential.FromJson consumes the file verbatim; any reshaping here would invalidate the credential's signature."); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task EmptyPaths_ReturnAllNullsAndProduceNoSeedRows() + { + var dir = NewTempDir(); + try + { + var config = new BootstrapConfig + { + Firebase = new FirebaseSection(), // all four defaults are "" + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + + await Assert.That(result.AndroidClientJson).IsNull(); + await Assert.That(result.IosClientJson).IsNull(); + await Assert.That(result.WebClientJson).IsNull(); + await Assert.That(result.ServiceAccountJson).IsNull(); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task NonExistentPath_ThrowsFileNotFound() + { + var dir = NewTempDir(); + try + { + var config = new BootstrapConfig + { + Firebase = new FirebaseSection + { + AndroidConfigPath = Path.Combine(dir, "does-not-exist.json"), + }, + }; + await Assert.That(async () => + await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None)) + .Throws() + .Because("A typo in the path must fail loudly at bootstrap time rather than silently disabling push."); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task MalformedServiceAccount_MissingTypeField_Throws() + { + var dir = NewTempDir(); + try + { + var sa = Path.Combine(dir, "sa.json"); + // Well-formed JSON but not a service-account credential — the phase should reject it + // rather than seeding an invalid credential that would explode at first FCM send. + File.WriteAllText(sa, "{\"foo\":\"bar\"}"); + var config = new BootstrapConfig + { + Firebase = new FirebaseSection { ServiceAccountPath = sa }, + }; + await Assert.That(async () => + await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None)) + .Throws(); + } + finally { TryDelete(dir); } + } + + [Test] + public async Task RelativePath_ResolvesAgainstOutputDir() + { + var dir = NewTempDir(); + try + { + var subDir = Path.Combine(dir, "firebase"); + Directory.CreateDirectory(subDir); + var android = Path.Combine(subDir, "gs.json"); + File.WriteAllText(android, AndroidFixture); + + var config = new BootstrapConfig + { + Firebase = new FirebaseSection + { + // Relative path — must resolve under the phase's outputDir. + AndroidConfigPath = Path.Combine("firebase", "gs.json"), + }, + }; + var result = await FirebasePhase.RunAsync(TestOptions(dir), config, Logger(), CancellationToken.None); + await Assert.That(result.AndroidClientJson).IsNotNull(); + } + finally { TryDelete(dir); } + } + + private static void TryDelete(string dir) => TestSupport.TryDeleteDir(dir); +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/Fixtures/DinDParallelLimitCalculatorTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/Fixtures/DinDParallelLimitCalculatorTests.cs new file mode 100644 index 00000000..117045ff --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/Fixtures/DinDParallelLimitCalculatorTests.cs @@ -0,0 +1,257 @@ +using Interfold.Bootstrapper.IntegrationTests.Fixtures; + +namespace Interfold.Bootstrapper.UnitTests.Fixtures; + +/// +/// Pure-math tests for . Every case constructs +/// a + pair explicitly so there's +/// zero coupling to the host, docker, or the file system. +/// +public sealed class DinDParallelLimitCalculatorTests +{ + private const long OneMebibyte = 1024L * 1024L; + private const long OneGibibyte = 1024L * OneMebibyte; + + /// + /// GH-hosted `ubuntu-latest` profile: 4 vCPU / 16 GiB RAM / ~14 GiB free of a + /// ~30 GiB rootfs. Matches the value hardcoded on ci-cd.yml:28 today + /// (INTERFOLD_DIND_PARALLEL_LIMIT: '2') — the whole point of this change + /// is that CI's auto-computed number lands on the same 2 without the env var. + /// + [Test] + public async Task GitHubUbuntuLatestProfileClampsToTwo() + { + var budget = new ResourceBudget( + Cpus: 4, + TotalMemoryBytes: 16 * OneGibibyte, + FreeDiskBytes: 14 * OneGibibyte, + TotalDiskBytes: 30 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + // cpuSlots=2, memSlots=5 (14.4 GiB / 2.5 GiB), diskSlots=2 ((14 - 4.5) / 4) + await Assert.That(limit).IsEqualTo(2); + } + + /// + /// Windows Docker Desktop default WSL2 profile: 4 vCPU / 8 GiB / 40 GiB free of + /// 50 GiB VHDX. Reproduces the local footgun the user just hit — running with + /// the pre-change default of 6 collapsed the docker daemon. + /// + [Test] + public async Task LaptopDockerDesktopProfileClampsToTwo() + { + var budget = new ResourceBudget( + Cpus: 4, + TotalMemoryBytes: 8 * OneGibibyte, + FreeDiskBytes: 40 * OneGibibyte, + TotalDiskBytes: 50 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + // cpuSlots=2, memSlots=2 (7.2 GiB / 2.5 GiB), diskSlots=8 ((40 - 7.5) / 4) + await Assert.That(limit).IsEqualTo(2); + } + + /// + /// Fat workstation: 16 vCPU / 64 GiB / 500 GiB free of 1 TB. CPU dominates and + /// produces 10 slots; the hard ceiling of 16 doesn't yet bite here. + /// + [Test] + public async Task FatWorkstationClampsToCpuBudget() + { + var budget = new ResourceBudget( + Cpus: 16, + TotalMemoryBytes: 64 * OneGibibyte, + FreeDiskBytes: 500 * OneGibibyte, + TotalDiskBytes: 1000 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + // cpuSlots=10 (16/1.5), memSlots=23, diskSlots=87 + await Assert.That(limit).IsEqualTo(10); + } + + /// + /// The hard ceiling applies even when every axis wants to go higher. Prevents + /// pathological over-scheduling of dockerd's exec throughput. + /// + [Test] + public async Task ExtremelyLargeHostClampsToHardCeiling() + { + var budget = new ResourceBudget( + Cpus: 128, + TotalMemoryBytes: 512 * OneGibibyte, + FreeDiskBytes: 4000 * OneGibibyte, + TotalDiskBytes: 8000 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + await Assert.That(limit).IsEqualTo(DinDParallelLimitCalculator.HardCeiling); + } + + /// + /// Tiny box: 1 vCPU / 2 GiB / disk fine. Every axis wants to floor to 0; the + /// outer Clamp in lifts + /// it to 1 so tests still make forward progress (they'll just serialize). + /// + [Test] + public async Task TinyBoxFloorsToOne() + { + var budget = new ResourceBudget( + Cpus: 1, + TotalMemoryBytes: 2 * OneGibibyte, + FreeDiskBytes: 20 * OneGibibyte, + TotalDiskBytes: 30 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + // cpuSlots=0 (1/1.5), memSlots=0 (1.8 GiB / 2.5 GiB), diskSlots=3.875 -> 3 + await Assert.That(limit).IsEqualTo(1); + } + + /// + /// The disk axis reserves a percentage-of-total floor. A drive already tighter + /// than the operator-requested headroom gives 0 for that axis (post-clamp: 1) + /// so we don't try to run anything that will blow past the safety line. + /// + [Test] + public async Task DiskAlreadyBelowHeadroomForcesFloor() + { + // 100 GiB drive, only 10 GiB free. Headroom line = 15 GiB, so the calc + // computes usable = 10 - 15 = -5 GiB → 0 disk slots. Clamp lifts to 1. + var budget = new ResourceBudget( + Cpus: 8, + TotalMemoryBytes: 32 * OneGibibyte, + FreeDiskBytes: 10 * OneGibibyte, + TotalDiskBytes: 100 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + await Assert.That(limit).IsEqualTo(1); + } + + /// + /// Memory-per-slot env override lowers the memory-axis cost so more slots fit. + /// Round-trips through from a fake env. + /// + [Test] + public async Task MemoryPerSlotOverrideRaisesMemorySlots() + { + var env = new Dictionary + { + ["INTERFOLD_DIND_MEM_PER_SLOT_MB"] = "1024", // 1 GiB per slot (half the default) + }; + var costs = SlotCosts.FromEnvironment(k => env.TryGetValue(k, out var v) ? v : null); + var budget = new ResourceBudget( + Cpus: 16, // takes CPU out of the picture + TotalMemoryBytes: 8 * OneGibibyte, + FreeDiskBytes: 500 * OneGibibyte, + TotalDiskBytes: 1000 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, costs); + // memSlots = 7.2 GiB / 1 GiB = 7 (headroom 10%) + await Assert.That(limit).IsEqualTo(7); + } + + /// + /// CPU-per-slot override tightens or loosens the CPU axis. Uses invariant + /// culture (a hostile "3,0" would parse to 30 on de-DE otherwise). + /// + [Test] + public async Task CpuPerSlotOverrideChangesCpuSlots() + { + var env = new Dictionary + { + ["INTERFOLD_DIND_CPU_PER_SLOT"] = "0.5", // 0.5 cores per slot + }; + var costs = SlotCosts.FromEnvironment(k => env.TryGetValue(k, out var v) ? v : null); + var budget = new ResourceBudget( + Cpus: 4, + TotalMemoryBytes: 64 * OneGibibyte, // huge, takes memory out + FreeDiskBytes: 500 * OneGibibyte, + TotalDiskBytes: 1000 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, costs); + // cpuSlots = 4 / 0.5 = 8, everything else huge + await Assert.That(limit).IsEqualTo(8); + } + + /// + /// Headroom percentages are honoured. Bumping mem headroom to 50% halves the + /// usable RAM and roughly halves the memory-axis slots — proves the field is + /// wired end-to-end from env → cost record → math. + /// + [Test] + public async Task HeadroomPercentageOverrideIsHonoured() + { + var env = new Dictionary + { + ["INTERFOLD_DIND_MEM_HEADROOM_PCT"] = "50", + }; + var costs = SlotCosts.FromEnvironment(k => env.TryGetValue(k, out var v) ? v : null); + var budget = new ResourceBudget( + Cpus: 32, + TotalMemoryBytes: 20 * OneGibibyte, + FreeDiskBytes: 500 * OneGibibyte, + TotalDiskBytes: 1000 * OneGibibyte); + var memSlots = DinDParallelLimitCalculator.ComputeMemorySlots(budget.TotalMemoryBytes, costs); + // usable = 20 GiB * 0.5 = 10 GiB. memSlots = 10 / 2.5 = 4. + await Assert.That(memSlots).IsEqualTo(4); + var limit = DinDParallelLimitCalculator.Compute(budget, costs); + await Assert.That(limit).IsEqualTo(4); + } + + /// + /// Non-parseable / zero / negative env values fall back to the baked-in default + /// rather than blowing up the process at construction. Guards against a hostile + /// INTERFOLD_DIND_CPU_PER_SLOT=abc or INTERFOLD_DIND_MEM_PER_SLOT_MB=0. + /// + [Test] + public async Task GarbageEnvValuesFallBackToDefaults() + { + var env = new Dictionary + { + ["INTERFOLD_DIND_CPU_PER_SLOT"] = "not-a-double", + ["INTERFOLD_DIND_MEM_PER_SLOT_MB"] = "0", + ["INTERFOLD_DIND_DISK_PER_SLOT_MB"] = "-42", + ["INTERFOLD_DIND_MEM_HEADROOM_PCT"] = "abc", + ["INTERFOLD_DIND_DISK_HEADROOM_PCT"] = "", + }; + var costs = SlotCosts.FromEnvironment(k => env.TryGetValue(k, out var v) ? v : null); + await Assert.That(costs.CpuPerSlot).IsEqualTo(SlotCosts.DefaultCpuPerSlot); + await Assert.That(costs.MemPerSlotBytes).IsEqualTo(SlotCosts.DefaultMemPerSlotBytes); + await Assert.That(costs.DiskPerSlotBytes).IsEqualTo(SlotCosts.DefaultDiskPerSlotBytes); + await Assert.That(costs.MemHeadroomPct).IsEqualTo(SlotCosts.DefaultMemHeadroomPct); + await Assert.That(costs.DiskHeadroomPct).IsEqualTo(SlotCosts.DefaultDiskHeadroomPct); + } + + /// + /// Zero-cost sentinel means "this axis is disabled": returns + /// so the Min ignores it. + /// Useful for the future when someone wants CPU-only sizing on a diskless VM. + /// + [Test] + public async Task ZeroCostAxisDropsOutOfMin() + { + var budget = new ResourceBudget( + Cpus: 4, + TotalMemoryBytes: 16 * OneGibibyte, + FreeDiskBytes: 100 * OneGibibyte, + TotalDiskBytes: 200 * OneGibibyte); + var costs = new SlotCosts( + CpuPerSlot: SlotCosts.DefaultCpuPerSlot, + MemPerSlotBytes: 0, + DiskPerSlotBytes: 0, + MemHeadroomPct: 10, + DiskHeadroomPct: 15); + var memSlots = DinDParallelLimitCalculator.ComputeMemorySlots(budget.TotalMemoryBytes, costs); + var diskSlots = DinDParallelLimitCalculator.ComputeDiskSlots(budget.FreeDiskBytes, budget.TotalDiskBytes, costs); + await Assert.That(memSlots).IsEqualTo(DinDParallelLimitCalculator.HardCeiling); + await Assert.That(diskSlots).IsEqualTo(DinDParallelLimitCalculator.HardCeiling); + var limit = DinDParallelLimitCalculator.Compute(budget, costs); + await Assert.That(limit).IsEqualTo(2); // still gated by CPU axis (4 / 1.5 = 2) + } + + /// + /// Zero / unknown CPU count still yields a working suite: floor of 1 keeps tests + /// serial. Simulates a broken docker info parse that gave us NCPU=0. + /// + [Test] + public async Task ZeroCpuBudgetStillFloorsToOne() + { + var budget = new ResourceBudget( + Cpus: 0, + TotalMemoryBytes: 16 * OneGibibyte, + FreeDiskBytes: 200 * OneGibibyte, + TotalDiskBytes: 400 * OneGibibyte); + var limit = DinDParallelLimitCalculator.Compute(budget, SlotCosts.Default); + await Assert.That(limit).IsEqualTo(1); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/HostParserTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/HostParserTests.cs new file mode 100644 index 00000000..64d9614c --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/HostParserTests.cs @@ -0,0 +1,279 @@ +using System.Net; +using System.Net.Sockets; +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for . Pins the precedence rules (CIDR before IP literal +/// before DNS), the operator-actionable rejection messages, and the byte-layout the cert phase +/// downstream depends on for iPAddress permittedSubtree encoding. +/// +public sealed class HostParserTests +{ + /// + /// Invokes with the supplied bad input and asserts the resulting + /// 's message contains every supplied fragment. Collapses the + /// arrange-throws-assert triple every negative test here would otherwise re-write. + /// + private static async Task AssertHostParseFailsAsync(string input, params string[] messageFragments) + { + var ex = Assert.Throws(() => HostParser.Parse(input)); + foreach (var frag in messageFragments) + { + await Assert.That(ex.Message).Contains(frag); + } + } + + [Test] + public async Task DnsNameClassifiesAsDns() + { + var entry = HostParser.Parse("api.example.com"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Dns); + await Assert.That(entry.DnsName).IsEqualTo("api.example.com"); + await Assert.That(entry.Ip).IsNull(); + await Assert.That(entry.IsLeafEligible).IsTrue(); + await Assert.That(entry.IsCidr).IsFalse(); + } + + [Test] + public async Task WildcardDnsNameClassifiesAsDns() + { + // Wildcards are valid leaf-SAN dNSName values and produce a suffix-only Name Constraints + // subtree (handled in CertificatePhase.StripWildcardPrefix). HostParser keeps the raw form. + var entry = HostParser.Parse("*.example.com"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Dns); + await Assert.That(entry.DnsName).IsEqualTo("*.example.com"); + } + + [Test] + public async Task Ipv4LiteralClassifiesAsIpv4() + { + var entry = HostParser.Parse("192.168.1.42"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Ipv4); + await Assert.That(entry.Ip).IsNotNull(); + await Assert.That(entry.Ip!.AddressFamily).IsEqualTo(AddressFamily.InterNetwork); + await Assert.That(entry.PrefixLength).IsEqualTo(32); + await Assert.That(entry.IsLeafEligible).IsTrue(); + } + + [Test] + public async Task Ipv6LiteralClassifiesAsIpv6() + { + var entry = HostParser.Parse("fe80::1234"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Ipv6); + await Assert.That(entry.Ip!.AddressFamily).IsEqualTo(AddressFamily.InterNetworkV6); + await Assert.That(entry.PrefixLength).IsEqualTo(128); + } + + [Test] + public async Task BracketedIpv6LiteralIsAccepted() + { + // Operators pasting from a URL ("https://[::1]") shouldn't have to strip the brackets + // manually. The parser handles either form and stores the canonical unbracketed IPAddress. + var entry = HostParser.Parse("[::1]"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Ipv6); + await Assert.That(entry.Ip!.ToString()).IsEqualTo("::1"); + } + + [Test] + public async Task Ipv4CidrClassifiesAsIpv4Cidr() + { + var entry = HostParser.Parse("192.168.1.0/24"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Ipv4Cidr); + await Assert.That(entry.Ip!.AddressFamily).IsEqualTo(AddressFamily.InterNetwork); + await Assert.That(entry.PrefixLength).IsEqualTo(24); + await Assert.That(entry.IsCidr).IsTrue(); + await Assert.That(entry.IsLeafEligible).IsFalse(); + } + + [Test] + public async Task Ipv6CidrClassifiesAsIpv6Cidr() + { + var entry = HostParser.Parse("fe80::/64"); + await Assert.That(entry.Kind).IsEqualTo(HostKind.Ipv6Cidr); + await Assert.That(entry.PrefixLength).IsEqualTo(64); + } + + [Test] + public Task EmptyInputRejected() + => AssertHostParseFailsAsync(" ", "empty"); + + [Test] + public Task DnsNameWithSpaceRejected() + // Mirrors the previous Validate() RFC 1035-lite rule. Whitespace is always a typo, so we + // reject before the cert phase tries to use it as a dNSName value. + => AssertHostParseFailsAsync("api example.com", "whitespace"); + + [Test] + public Task DnsNameTooLongRejected() + // 254 chars - one over the RFC 1035 limit. Use a single-label form so we don't trip on + // any unrelated label-length check. + => AssertHostParseFailsAsync(new string('a', 254), "253"); + + [Test] + public Task Ipv4CidrWithHostBitsSetRejectedWithFixIt() + // RFC 5280 §4.2.1.10 requires host bits beyond the mask to be zero. Surface a fix-it that + // shows both the network-mask interpretation and the single-host interpretation so the + // operator can pick the one they meant. + => AssertHostParseFailsAsync("192.168.1.42/24", "host bits", "192.168.1.0/24", "192.168.1.42/32"); + + [Test] + public Task Ipv4CidrPrefixOutOfRangeRejected() + => AssertHostParseFailsAsync("10.0.0.0/33", "out of range", "32"); + + [Test] + public Task Ipv6CidrPrefixOutOfRangeRejected() + => AssertHostParseFailsAsync("fe80::/129", "out of range", "128"); + + [Test] + public Task CidrWithNonNumericPrefixRejected() + => AssertHostParseFailsAsync("10.0.0.0/abc", "non-negative integer"); + + [Test] + public async Task BuildNetmaskIpv4SlashTwentyFour() + { + // /24 = FF FF FF 00 — the canonical class-C mask, ubiquitous on LANs. If this is wrong, + // every iPAddress permittedSubtree downstream is wrong. + var mask = HostParser.BuildNetmask(byteLength: 4, prefix: 24); + await Assert.That(mask).IsEquivalentTo(new byte[] { 0xFF, 0xFF, 0xFF, 0x00 }); + } + + [Test] + public async Task BuildNetmaskIpv4FullHost() + { + // /32 = FF FF FF FF — the mask used when a single IPv4 literal becomes a Name Constraints + // subtree (no CIDR specified by the operator). + var mask = HostParser.BuildNetmask(byteLength: 4, prefix: 32); + await Assert.That(mask).IsEquivalentTo(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }); + } + + [Test] + public async Task BuildNetmaskIpv4PartialByte() + { + // /20 = FF FF F0 00 — exercises the trailing-bits branch (prefix not on a byte boundary). + var mask = HostParser.BuildNetmask(byteLength: 4, prefix: 20); + await Assert.That(mask).IsEquivalentTo(new byte[] { 0xFF, 0xFF, 0xF0, 0x00 }); + } + + [Test] + public async Task BuildNetmaskIpv6FullHost() + { + // /128 = sixteen 0xFF bytes — the mask used for a single IPv6 literal subtree. + var mask = HostParser.BuildNetmask(byteLength: 16, prefix: 128); + var expected = new byte[16]; + Array.Fill(expected, (byte)0xFF); + await Assert.That(mask).IsEquivalentTo(expected); + } + + [Test] + public async Task ToNameConstraintSubtreeBytesIpv4Cidr() + { + // RFC 5280 §4.2.1.10: payload is address || mask. 192.168.1.0/24 → C0 A8 01 00 FF FF FF 00. + var entry = HostParser.Parse("192.168.1.0/24"); + var bytes = HostParser.ToNameConstraintSubtreeBytes(entry); + await Assert.That(bytes).IsEquivalentTo(new byte[] + { + 0xC0, 0xA8, 0x01, 0x00, + 0xFF, 0xFF, 0xFF, 0x00, + }); + } + + [Test] + public async Task ToNameConstraintSubtreeBytesIpv4SingleHost() + { + // Bare IPv4 literal gets a /32 mask. 192.168.1.42 → C0 A8 01 2A FF FF FF FF. + var entry = HostParser.Parse("192.168.1.42"); + var bytes = HostParser.ToNameConstraintSubtreeBytes(entry); + await Assert.That(bytes).IsEquivalentTo(new byte[] + { + 0xC0, 0xA8, 0x01, 0x2A, + 0xFF, 0xFF, 0xFF, 0xFF, + }); + } + + [Test] + public async Task ToNameConstraintSubtreeBytesIpv6SingleHost() + { + // ::1 with /128 mask → 32 bytes total (16 addr + 16 mask). Spot-check the boundary. + var entry = HostParser.Parse("::1"); + var bytes = HostParser.ToNameConstraintSubtreeBytes(entry); + await Assert.That(bytes.Length).IsEqualTo(32); + await Assert.That(bytes[15]).IsEqualTo((byte)0x01); + await Assert.That(bytes[16]).IsEqualTo((byte)0xFF); + await Assert.That(bytes[31]).IsEqualTo((byte)0xFF); + } + + [Test] + public async Task ToUrlHostBracketsIpv6() + { + // RFC 3986 §3.2.2: a host that is an IPv6 literal MUST be enclosed in square brackets in + // a URI. Used by ResolveDerivedDefaults so `https://[::1]` is the derived callback base. + var entry = HostParser.Parse("::1"); + await Assert.That(HostParser.ToUrlHost(entry)).IsEqualTo("[::1]"); + } + + [Test] + public async Task ToUrlHostLeavesIpv4Unbracketed() + { + var entry = HostParser.Parse("192.168.1.42"); + await Assert.That(HostParser.ToUrlHost(entry)).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task ToUrlHostLeavesDnsUnbracketed() + { + var entry = HostParser.Parse("api.example.com"); + await Assert.That(HostParser.ToUrlHost(entry)).IsEqualTo("api.example.com"); + } + + [Test] + public async Task ToUrlHostThrowsForCidr() + { + var entry = HostParser.Parse("10.0.0.0/8"); + Assert.Throws(() => HostParser.ToUrlHost(entry)); + await Task.CompletedTask; + } + + [Test] + public async Task PickPrimarySkipsCidr() + { + // CIDR entries appear first; PickPrimary must walk past them to the first leaf-eligible + // entry. Matches the rule baked into ResolveDerivedDefaults / PublishPhase.PickServerName. + var entries = new[] + { + HostParser.Parse("192.168.1.0/24"), + HostParser.Parse("10.0.0.0/8"), + HostParser.Parse("api.example.com"), + }; + var primary = HostParser.PickPrimary(entries); + await Assert.That(primary).IsNotNull(); + await Assert.That(primary!.Raw).IsEqualTo("api.example.com"); + } + + [Test] + public async Task PickPrimaryReturnsNullWhenAllCidr() + { + var entries = new[] + { + HostParser.Parse("192.168.1.0/24"), + HostParser.Parse("fe80::/64"), + }; + await Assert.That(HostParser.PickPrimary(entries)).IsNull(); + } + + [Test] + public async Task IsHostBitsZeroDetectsNonZero() + { + // 192.168.1.42 = C0 A8 01 2A. With /24 the host byte must be 00; 0x2A means host bits set. + var addr = IPAddress.Parse("192.168.1.42").GetAddressBytes(); + await Assert.That(HostParser.IsHostBitsZero(addr, 24)).IsFalse(); + } + + [Test] + public async Task IsHostBitsZeroAcceptsExactNetwork() + { + var addr = IPAddress.Parse("192.168.1.0").GetAddressBytes(); + await Assert.That(HostParser.IsHostBitsZero(addr, 24)).IsTrue(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/HostnameDetectorTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/HostnameDetectorTests.cs new file mode 100644 index 00000000..92226b2e --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/HostnameDetectorTests.cs @@ -0,0 +1,142 @@ +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for . The live entry point +/// calls +/// (non-mockable), so the deterministic surface is the +/// pure static helper — same shape as +/// LocalAddressDetectorTests' Pick / IsVirtualName split. +/// +/// Coverage below pins every rejection branch of the qualifier plus the happy path so a +/// regression that (say) drops the FQDN or invalid-chars filter fails a specific test rather +/// than showing up as a mysterious pre-fill on a downstream integration suite. +/// +public sealed class HostnameDetectorTests +{ + [Test] + public async Task QualifyForMdnsRejectsNull() + { + // Guards the null-safe null-input path — HostnameDetector.TryDetectMdnsHostname wraps + // Dns.GetHostName in try/catch and returns null on platform surprise; the pure qualifier + // needs to survive that null itself without throwing. + var result = HostnameDetector.QualifyForMdns(null); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsEmpty() + { + var result = HostnameDetector.QualifyForMdns(string.Empty); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsWhitespace() + { + // " " is neither an empty string nor a valid hostname — the whitespace check has to + // catch it before the trim + regex match path would otherwise reject on regex-only. + var result = HostnameDetector.QualifyForMdns(" "); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsLocalhost() + { + // "localhost" resolves to loopback; advertising loopback via mDNS would be a lie. The + // check is case-insensitive so "Localhost" / "LOCALHOST" are also rejected. + await Assert.That(HostnameDetector.QualifyForMdns("localhost")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("Localhost")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("LOCALHOST")).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsFqdn() + { + // Already-qualified names (any dot) get rejected — appending ".local" to + // "host.corp.example.com" would produce a bogus double-suffixed name. This also + // catches the case where the operator has already put ".local" on their hostname + // manually (Dns.GetHostName() sometimes returns that on macOS). + await Assert.That(HostnameDetector.QualifyForMdns("host.corp.example.com")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("workstation.local")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("a.b")).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsInvalidCharacters() + { + // The RFC 1035 label check rejects punctuation, whitespace, and leading hyphens. A + // fabricated hostname with any of these would produce a bogus mDNS name downstream + // (the resulting {name}.local wouldn't parse via HostParser either). + await Assert.That(HostnameDetector.QualifyForMdns("host!name")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("host name")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("host_name")).IsNull(); + await Assert.That(HostnameDetector.QualifyForMdns("-startdash")).IsNull(); + } + + [Test] + public async Task QualifyForMdnsRejectsTooLongLabel() + { + // RFC 1035 caps DNS labels at 63 characters — anything longer would produce an invalid + // {name}.local that HostParser.Parse would then reject downstream. Filtering here means + // the caller (the pre-fill banner) gets a clean "no hostname" signal rather than a + // hostname that later fails validation. + var tooLong = new string('a', 64); + await Assert.That(HostnameDetector.QualifyForMdns(tooLong)).IsNull(); + } + + [Test] + public async Task QualifyForMdnsAppendsLocalSuffixToBareHostname() + { + // The happy path — a plain short hostname gets ".local" appended and returned as the + // mDNS-qualified name the pre-fill banner will offer to the operator. + var result = HostnameDetector.QualifyForMdns("workstation"); + await Assert.That(result).IsEqualTo("workstation.local"); + } + + [Test] + public async Task QualifyForMdnsAcceptsHostnameWithDigitsAndHyphens() + { + // The RFC 1035 label grammar allows digits and hyphens as long as the first character + // isn't a hyphen. Common in real-world hostnames ("dev-box-01", "node2"); rejecting + // these would drop the pre-fill for entire categories of legitimate operator setups. + await Assert.That(HostnameDetector.QualifyForMdns("dev-box-01")).IsEqualTo("dev-box-01.local"); + await Assert.That(HostnameDetector.QualifyForMdns("node2")).IsEqualTo("node2.local"); + } + + [Test] + public async Task QualifyForMdnsTrimsSurroundingWhitespace() + { + // A hostname that survived a copy/paste with a trailing newline or leading space + // should still qualify — the trim runs before the label check so the resulting + // {name}.local is clean. + await Assert.That(HostnameDetector.QualifyForMdns(" workstation ")).IsEqualTo("workstation.local"); + await Assert.That(HostnameDetector.QualifyForMdns("workstation\n")).IsEqualTo("workstation.local"); + } + + [Test] + public async Task QualifyForMdnsAccepts63CharLabel() + { + // 63 characters is the exact RFC 1035 label limit — the boundary case worth pinning + // because an off-by-one in the regex (e.g. {0,61} instead of {0,62}) would silently + // reject valid hostnames the qualifier is supposed to admit. + var maxLabel = new string('a', 63); + var result = HostnameDetector.QualifyForMdns(maxLabel); + await Assert.That(result).IsEqualTo($"{maxLabel}.local"); + } + + [Test] + public async Task LiveDetectorRunsWithoutThrowing() + { + // Smoke test for the live path: TryDetectMdnsHostname must never throw. Any platform + // surprise (Dns.GetHostName failure, permissions issue) is swallowed and reported as + // null. We don't assert WHAT it returns (depends on the runner's hostname), only that + // the call completes and produces either null or a value ending in ".local". + var result = HostnameDetector.TryDetectMdnsHostname(); + if (result is not null) + { + await Assert.That(result).EndsWith(".local"); + } + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/Interfold.Bootstrapper.UnitTests.csproj b/tests/unit/Interfold.Bootstrapper.UnitTests/Interfold.Bootstrapper.UnitTests.csproj new file mode 100644 index 00000000..0d0c550e --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/Interfold.Bootstrapper.UnitTests.csproj @@ -0,0 +1,32 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Bootstrapper.UnitTests + + false + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/LocalAddressDetectorTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/LocalAddressDetectorTests.cs new file mode 100644 index 00000000..81769a5a --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/LocalAddressDetectorTests.cs @@ -0,0 +1,285 @@ +using System.Net; +using System.Net.NetworkInformation; +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for . The picker is the only deterministic +/// surface — the live wrapper depends on +/// the runner's actual NICs and is therefore covered by a single smoke test below. Every other +/// path drives with fabricated +/// records so we exercise the filter chain +/// (status / type / virtual-name / loopback / APIPA / link-local / family preference) without +/// reaching into the OS. +/// +public sealed class LocalAddressDetectorTests +{ + private static LocalAddressDetector.NicProbe MakeProbe( + string name, + NetworkInterfaceType type = NetworkInterfaceType.Ethernet, + OperationalStatus status = OperationalStatus.Up, + string[]? addresses = null) => + new(name, type, status, (addresses ?? []).Select(IPAddress.Parse).ToArray()); + + [Test] + public async Task EmptyProbeListReturnsNull() + { + var result = LocalAddressDetector.Pick(Array.Empty()); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task LoopbackOnlyReturnsNull() + { + // Box with nothing but lo / lo0 — common in air-gapped CI containers. The picker has + // to surface "no usable address" rather than silently returning 127.0.0.1, because a + // loopback in a SAN produces a cert that only validates from the box itself. + var probes = new[] + { + MakeProbe("lo", NetworkInterfaceType.Loopback, addresses: ["127.0.0.1", "::1"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task DownInterfaceIsSkippedEvenWithUsableAddress() + { + // OperationalStatus.Down means the OS won't route through this NIC; its address is + // therefore not a useful default for a serving cert. + var probes = new[] + { + MakeProbe("eth0", status: OperationalStatus.Down, addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task SingleGoodEthernetReturnsItsIpv4() + { + var probes = new[] + { + MakeProbe("eth0", addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task LoopbackPlusEthernetPicksEthernet() + { + // The loopback NIC type is filtered out wholesale; the picker should latch onto the + // routable Ethernet's address regardless of probe order. + var probes = new[] + { + MakeProbe("lo", NetworkInterfaceType.Loopback, addresses: ["127.0.0.1"]), + MakeProbe("eth0", addresses: ["10.0.0.5"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("10.0.0.5"); + } + + [Test] + public async Task TunnelInterfaceIsSkipped() + { + // Tunnel NIC type (VPN endpoints, GRE tunnels, IPv6 transition tunnels) is operator- + // specific and usually transient. We deliberately don't bake a VPN-assigned IP into a + // cert SAN. + var probes = new[] + { + MakeProbe("vpn0", type: NetworkInterfaceType.Tunnel, addresses: ["10.8.0.2"]), + MakeProbe("eth0", addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task DockerBridgeIsSkipped() + { + // Docker / k8s / VPN-as-Ethernet bridges report NetworkInterfaceType.Ethernet on Linux + // but their name prefixes (docker, br-, veth, cni, flannel, cilium_) tag them as + // virtual. A dev laptop with both docker0 (172.17.0.1) and a real LAN NIC must pick the + // LAN — picking docker0 would mint a cert that only the host validates, since 172.17 + // is rarely reachable from outside the box. + var probes = new[] + { + MakeProbe("docker0", addresses: ["172.17.0.1"]), + MakeProbe("br-abcdef123456", addresses: ["172.18.0.1"]), + MakeProbe("eth0", addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task HyperVVirtualSwitchIsSkippedOnWindowsLikeName() + { + // Windows surfaces Hyper-V virtual switches with names like "vEthernet (Default Switch)". + // The name-prefix filter must catch them. + var probes = new[] + { + MakeProbe("vEthernet (Default Switch)", addresses: ["172.20.16.1"]), + MakeProbe("Ethernet", addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task WireGuardAndTailscaleTunnelsAreSkipped() + { + // wg* / utun* / tailscale0 are common userspace tunnel device names that on some + // platforms report as plain Ethernet (Tailscale on macOS via utunN, WireGuard on + // Linux via wgN). Catch them by name even when the type-based filter wouldn't. + var probes = new[] + { + MakeProbe("wg0", addresses: ["10.66.66.1"]), + MakeProbe("utun4", addresses: ["100.64.0.1"]), + MakeProbe("tailscale0", addresses: ["100.64.0.2"]), + MakeProbe("en0", addresses: ["192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task Ipv4ApipaIsSkipped() + { + // 169.254.0.0/16 is the IPv4 link-local fallback set when DHCP fails. It indicates + // "no real network" — never a useful SAN value. + var probes = new[] + { + MakeProbe("eth0", addresses: ["169.254.42.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task ApipaPlusRealIpv4PicksRealIp() + { + // Within a single NIC's address list APIPA should be filtered while the routable IPv4 + // wins. + var probes = new[] + { + MakeProbe("eth0", addresses: ["169.254.42.42", "10.0.0.5"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("10.0.0.5"); + } + + [Test] + public async Task Ipv6LinkLocalIsSkipped() + { + // fe80::/10 link-local is fundamentally scoped to the local link and never useful in a + // SAN. Same logic as IPv4 APIPA but for the v6 family. + var probes = new[] + { + MakeProbe("eth0", addresses: ["fe80::1234"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task DualStackPrefersIpv4() + { + // The picker's documented IPv4 bias: a NIC exposing both 192.168.1.42 and 2001:db8::1 + // returns the v4 address, matching the dominant self-host topology (LAN behind v4 NAT). + // Operators on a pure-v6 deployment still get a usable default via the next test. + var probes = new[] + { + MakeProbe("eth0", addresses: ["2001:db8::1", "192.168.1.42"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task Ipv6OnlyDeploymentReturnsIpv6() + { + // No usable v4 candidate (loopback + APIPA filtered) → the picker falls back to the + // first usable v6. Common for hosted IPv6-only VPSes. + var probes = new[] + { + MakeProbe("eth0", addresses: ["169.254.42.42", "2001:db8::1"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result!.ToString()).IsEqualTo("2001:db8::1"); + } + + [Test] + public async Task NonLoopbackNicWithLoopbackAddressIsStillSkipped() + { + // Defensive double-check: a NIC reporting type Ethernet but exposing 127.0.0.1 (rare + // mis-config, but observed on some embedded boxes) shouldn't leak the loopback into + // the picker's output. The IPAddress.IsLoopback check inside the address loop catches it. + var probes = new[] + { + MakeProbe("eth0", addresses: ["127.0.0.1"]), + }; + var result = LocalAddressDetector.Pick(probes); + await Assert.That(result).IsNull(); + } + + [Test] + public async Task LiveDetectorRunsWithoutThrowing() + { + // Smoke test for the live path: TryDetectPrimaryIp must never throw — any platform + // surprise (no NICs, permissions, NetworkInformationException from a NIC disappearing + // mid-enumeration) is swallowed and reported as null. We don't assert WHAT it returns + // (depends on the runner's NICs), only that the call completes and produces either + // null or a non-loopback address. + var result = LocalAddressDetector.TryDetectPrimaryIp(); + if (result is not null) + { + await Assert.That(IPAddress.IsLoopback(result)).IsFalse(); + } + } + + [Test] + public async Task IsVirtualNameMatchesEveryDocumentedPrefix() + { + // Pins the documented prefix list against representative real-world NIC names. If a + // future refactor accidentally drops a prefix (e.g. removes "cni"), this test fails + // before the picker silently starts returning a k8s overlay IP as the "primary" host. + await Assert.That(LocalAddressDetector.IsVirtualName("docker0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("br-abc123")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("veth1234abcd")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("tap0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("tun0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("vEthernet (Default Switch)")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("vmnet8")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("virbr0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("cni0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("flannel.1")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("cilium_host")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("kube-bridge")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("zt-abc123")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("wg0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("utun4")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("tailscale0")).IsTrue(); + + // Common real-NIC names that should NOT match — these are the operator's actual LAN + // interfaces and must reach the picker. + await Assert.That(LocalAddressDetector.IsVirtualName("eth0")).IsFalse(); + await Assert.That(LocalAddressDetector.IsVirtualName("en0")).IsFalse(); + await Assert.That(LocalAddressDetector.IsVirtualName("wlan0")).IsFalse(); + await Assert.That(LocalAddressDetector.IsVirtualName("Ethernet")).IsFalse(); + await Assert.That(LocalAddressDetector.IsVirtualName("Wi-Fi")).IsFalse(); + } + + [Test] + public async Task PrefixMatchIsCaseInsensitive() + { + // Windows often capitalises NIC names ("Ethernet", "VEthernet (..)"). The match has to + // be case-insensitive so platform-specific casing doesn't slip a virtual NIC past the + // filter. + await Assert.That(LocalAddressDetector.IsVirtualName("Docker0")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("VETHERNET (test)")).IsTrue(); + await Assert.That(LocalAddressDetector.IsVirtualName("Tailscale0")).IsTrue(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/PublishEnvPostProcessingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/PublishEnvPostProcessingTests.cs new file mode 100644 index 00000000..4658e843 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/PublishEnvPostProcessingTests.cs @@ -0,0 +1,672 @@ +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// Sub-second drift check on . Integration +/// tests cover the emitted file end-to-end; these lock the pure key set. +public sealed class PublishEnvPostProcessingTests +{ + private static (BootstrapConfig Config, GeneratedSecrets Secrets) MakeInputs( + string? apiImage = null, + DatabaseMode databaseMode = DatabaseMode.Single) + { + var config = new BootstrapConfig + { + DatabaseMode = databaseMode, + ApiImage = apiImage ?? "ghcr.io/azyyyyyy/interfold-api:latest", + }; + // Deployment.Hosts has no placeholder; without a seed ResolveDerivedDefaults has + // nothing to feed CallbackBaseUrl / JwtAuthority / CorsAllowedOrigins from. + config.Deployment.Hosts = ["api.example.com"]; + // OAuth client secrets flow into internal.secrets, not env; setting exercises the + // irrelevant path. + config.OAuth.GoogleClientSecret = "google-secret-from-config"; + config.OAuth.DiscordClientSecret = "discord-secret-from-config"; + + // Mirror ConfigPhase.Validate's post-derivation snapshot without invoking the full + // Validate (publish tests deliberately allow shapes Validate would reject). + ConfigPhase.ResolveDerivedDefaults(config); + return (config, SecretsPhase.Generate()); + } + + [Test] + public async Task BuildEnvReplacementsProducesAllRequiredParameterKeys() + { + var (config, secrets) = MakeInputs(); + // Pin all three OAuth IDs so IsNotEmpty is meaningful; empty-still-emits behaviour + // has its own test below. + config.OAuth.GoogleClientId = "google-client-id"; + config.OAuth.DiscordClientId = "discord-client-id"; + config.OAuth.AppleClientId = "apple-client-id"; + // Pin the three nullable tuning fields that still flow as Aspire parameters. + // AvatarStorageRoot is a host bind-mount path (not an env parameter); see + // BindMountPathsResolveToAbsoluteUnderOutputDir / ResolveAvatarHostRoot. + config.Storage.AvatarStorageRoot = "/var/lib/interfold/avatars"; + config.Storage.AvatarPublicBase = "https://cdn.example.com/avatars/"; + config.Observability.OtlpEndpoint = "http://localhost:4317"; + config.Socket.BatchBytesThreshold = 65536; + const string baseDir = "/var/lib/interfold"; + const string outputDir = "/srv/interfold/deploy"; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, baseDir, outputDir); + + string[] required = + [ + "POSTGRES_USER", + "POSTGRES_PASSWORD", + "POSTGRES_INIT_PASSWORD", + "POSTGRES_DB", + "SCYLLA_USER", + "SCYLLA_PASSWORD", + "ENCRYPTION_PRIVATE_KEY", + "GOOGLE_OAUTH_CLIENT_ID", + "DISCORD_OAUTH_CLIENT_ID", + "APPLE_OAUTH_CLIENT_ID", + "SCYLLA_KEYSPACE", + "OAUTH_CALLBACK_BASE_URL", + "JWT_AUTHORITY", + "JWT_AUDIENCE", + "CORS_ALLOWED_ORIGINS", + "NODE_GROUP", + "AVATAR_PUBLIC_BASE", + "OTLP_ENDPOINT", + "SOCKET_BATCH_BYTES_THRESHOLD", + "DB_RETRY_ATTEMPTS", + "DB_RETRY_INITIAL_DELAY_MS", + "DB_RETRY_MAX_DELAY_MS", + "HYDRATION_MAX_CONCURRENCY", + ]; + foreach (var key in required) + { + await Assert.That(replacements.Parameters.ContainsKey(key)).IsTrue() + .Because($"missing parameter key '{key}' in env replacements"); + await Assert.That(replacements.Parameters[key]).IsNotEmpty() + .Because($"parameter '{key}' must be non-empty"); + } + + // The encryption pepper, OAuth client secrets, JWT material, deep-link secret, and + // the leaf PFX password must NOT appear here any more — they all live inside + // internal.secrets and are loaded at startup (SecretsBootstrapService / Program.cs + // Kestrel loader). + await Assert.That(replacements.Parameters.ContainsKey("ENCRYPTION_PEPPER")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("GOOGLE_OAUTH_CLIENT_SECRET")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("DISCORD_OAUTH_CLIENT_SECRET")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("APPLE_OAUTH_CLIENT_SECRET")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("LEAF_PFX_PASSWORD")).IsFalse(); + // Admin credentials must also stay inside internal.secrets exclusively. + await Assert.That(replacements.Parameters.ContainsKey("SCYLLA_ADMIN_PASSWORD")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("POSTGRES_ADMIN_PASSWORD")).IsFalse(); + } + + [Test] + public async Task BuildEnvReplacementsCarriesOAuthClientIdsFromConfig() + { + // Contract: OAuth client IDs copy verbatim into the env dict; a rename either side + // shows up as a missing key or value mismatch here. + var (config, secrets) = MakeInputs(); + config.OAuth.GoogleClientId = "google-client-from-config.apps.googleusercontent.com"; + config.OAuth.DiscordClientId = "1234567890"; + config.OAuth.AppleClientId = "com.example.interfold.signin"; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters["GOOGLE_OAUTH_CLIENT_ID"]) + .IsEqualTo("google-client-from-config.apps.googleusercontent.com"); + await Assert.That(replacements.Parameters["DISCORD_OAUTH_CLIENT_ID"]).IsEqualTo("1234567890"); + await Assert.That(replacements.Parameters["APPLE_OAUTH_CLIENT_ID"]) + .IsEqualTo("com.example.interfold.signin"); + } + + [Test] + public async Task BuildEnvReplacementsEmitsEmptyOAuthClientIdsWhenNotConfigured() + { + // Empty client IDs mean "provider disabled"; keys MUST still emit as `KEY=` so + // ApplyReplacementsToEnvFile doesn't warn on unfilled blanks. + var (config, secrets) = MakeInputs(); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters.ContainsKey("GOOGLE_OAUTH_CLIENT_ID")).IsTrue(); + await Assert.That(replacements.Parameters.ContainsKey("DISCORD_OAUTH_CLIENT_ID")).IsTrue(); + await Assert.That(replacements.Parameters.ContainsKey("APPLE_OAUTH_CLIENT_ID")).IsTrue(); + await Assert.That(replacements.Parameters["GOOGLE_OAUTH_CLIENT_ID"]).IsEqualTo(string.Empty); + await Assert.That(replacements.Parameters["DISCORD_OAUTH_CLIENT_ID"]).IsEqualTo(string.Empty); + await Assert.That(replacements.Parameters["APPLE_OAUTH_CLIENT_ID"]).IsEqualTo(string.Empty); + } + + [Test] + public async Task BuildEnvReplacementsCarriesPostgresDatabaseNameFromConfig() + { + // POSTGRES_DB must round-trip verbatim so the API connection string lines up with + // the database DatabaseInitPhase actually creates. + var (config, secrets) = MakeInputs(); + config.PostgresDatabase = "my_custom_db"; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters.ContainsKey("POSTGRES_DB")).IsTrue(); + await Assert.That(replacements.Parameters["POSTGRES_DB"]).IsEqualTo("my_custom_db"); + } + + [Test] + public async Task BuildEnvReplacementsProducesAllTwentySixKeysInSingleMode() + { + var (config, secrets) = MakeInputs(databaseMode: DatabaseMode.Single); + const string baseDir = "/var/lib/interfold"; + const string outputDir = "/srv/interfold/deploy"; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, baseDir, outputDir); + + var total = replacements.Parameters.Count + replacements.BindMounts.Count; + // Single mode: 24 param keys + 2 bind mounts (/certs, scylla rackdc) = 26. + await Assert.That(total).IsEqualTo(26); + } + + [Test] + public async Task BuildEnvReplacementsCarriesApiRuntimeFromConfig() + { + // ScyllaKeyspace + every ApiRuntime field must round-trip verbatim; a typo either + // side surfaces here as a missing key or value mismatch. + var (config, secrets) = MakeInputs(); + config.ScyllaKeyspace = ScyllaKeyspace.Eur; + config.ApiRuntime.CallbackBaseUrl = "https://callback.example.com"; + config.ApiRuntime.JwtAuthority = "https://issuer.example.com"; + config.ApiRuntime.JwtAudience = "custom-aud"; + config.ApiRuntime.CorsAllowedOrigins = ["https://app.example.com", "https://admin.example.com"]; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters["SCYLLA_KEYSPACE"]).IsEqualTo("eur"); + await Assert.That(replacements.Parameters["OAUTH_CALLBACK_BASE_URL"]) + .IsEqualTo("https://callback.example.com"); + await Assert.That(replacements.Parameters["JWT_AUTHORITY"]).IsEqualTo("https://issuer.example.com"); + await Assert.That(replacements.Parameters["JWT_AUDIENCE"]).IsEqualTo("custom-aud"); + // Comma-separated on OCTOCON_CORS_ALLOWED_ORIGINS; API CORS startup splits on ','. + await Assert.That(replacements.Parameters["CORS_ALLOWED_ORIGINS"]) + .IsEqualTo("https://app.example.com,https://admin.example.com"); + } + + [Test] + public async Task BuildEnvReplacementsDerivesApiRuntimeFromDeploymentWhenUnset() + { + // Blank apiRuntime.* is derived from deployment.hosts + deployment.webHttps via + // ResolveDerivedDefaults; the derivation must round-trip through publish unchanged. + var config = new BootstrapConfig + { + Deployment = + { + Hosts = ["api.example.com", "admin.example.com"], + WebHttps = true, + }, + }; + // Run derivation explicitly (MakeInputs would inject non-blank defaults) to exercise + // the empty-apiRuntime path. + ConfigPhase.ResolveDerivedDefaults(config); + var secrets = SecretsPhase.Generate(); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + // API URL is always https + Ports.ApiHttps (5001) — Kestrel binds the leaf PFX + // regardless of Deployment.WebHttps, which only governs the web container. + await Assert.That(replacements.Parameters["OAUTH_CALLBACK_BASE_URL"]) + .IsEqualTo("https://api.example.com:5001"); + await Assert.That(replacements.Parameters["JWT_AUTHORITY"]) + .IsEqualTo("https://api.example.com:5001"); + // JwtAudience is a property-initialiser default, not derived. + await Assert.That(replacements.Parameters["JWT_AUDIENCE"]).IsEqualTo("octocon"); + // CORS: scheme=WebHttps, port=Ports.WebHttps (8081), one entry per non-CIDR host. + await Assert.That(replacements.Parameters["CORS_ALLOWED_ORIGINS"]) + .IsEqualTo("https://api.example.com:8081,https://admin.example.com:8081"); + await Assert.That(replacements.Parameters["SCYLLA_KEYSPACE"]).IsEqualTo("nam"); + } + + [Test] + public async Task BuildEnvReplacementsCarriesTuningFromConfig() + { + // Every operator tuning field must round-trip verbatim. + var (config, secrets) = MakeInputs(); + config.Cluster.NodeGroup = NodeGroup.Primary; + config.Storage.AvatarStorageRoot = "/srv/avatars"; + config.Storage.AvatarPublicBase = "https://cdn.example.com/a/"; + config.Observability.OtlpEndpoint = "http://otel-collector:4317"; + config.Socket.BatchBytesThreshold = 131_072; + config.Persistence.DbRetryAttempts = 5; + config.Persistence.DbRetryInitialDelayMs = 250; + config.Persistence.DbRetryMaxDelayMs = 3_000; + config.Persistence.HydrationMaxConcurrency = 16; + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters["NODE_GROUP"]).IsEqualTo("primary"); + await Assert.That(replacements.Parameters.ContainsKey("AVATAR_STORAGE_ROOT")).IsFalse() + .Because("Avatar host path is a bind-mount source, not an Aspire parameter"); + await Assert.That(replacements.BindMounts["interfold-api:/app/data/avatars"]).IsEqualTo( + Path.GetFullPath("/srv/avatars")); + await Assert.That(replacements.Parameters["AVATAR_PUBLIC_BASE"]) + .IsEqualTo("https://cdn.example.com/a/"); + await Assert.That(replacements.Parameters["OTLP_ENDPOINT"]) + .IsEqualTo("http://otel-collector:4317"); + await Assert.That(replacements.Parameters["SOCKET_BATCH_BYTES_THRESHOLD"]).IsEqualTo("131072"); + await Assert.That(replacements.Parameters["DB_RETRY_ATTEMPTS"]).IsEqualTo("5"); + await Assert.That(replacements.Parameters["DB_RETRY_INITIAL_DELAY_MS"]).IsEqualTo("250"); + await Assert.That(replacements.Parameters["DB_RETRY_MAX_DELAY_MS"]).IsEqualTo("3000"); + await Assert.That(replacements.Parameters["HYDRATION_MAX_CONCURRENCY"]).IsEqualTo("16"); + } + + [Test] + public async Task BuildEnvReplacementsDefaultsAvatarBindMountUnderOutputDir() + { + // Blank avatarStorageRoot → {outputDir}/data/avatars bind-mounted at /app/data/avatars. + var (config, secrets) = MakeInputs(); + await Assert.That(config.Storage.AvatarStorageRoot).IsEqualTo(string.Empty) + .Because("Pre-condition: this test only makes sense when the config-side default is blank."); + + var outputDir = Path.Combine(Path.GetTempPath(), "interfold-avatar-default"); + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", outputDir); + + await Assert.That(replacements.Parameters.ContainsKey("AVATAR_STORAGE_ROOT")).IsFalse() + .Because("Container path is baked into compose via WithEnvironment; host path is the bind mount."); + var expected = Path.GetFullPath(Path.Combine(outputDir, "data", "avatars")); + await Assert.That(replacements.BindMounts["interfold-api:/app/data/avatars"]).IsEqualTo(expected); + } + + [Test] + public async Task BuildEnvReplacementsEmitsEmptyTuningSlotsForNullables() + { + // "Disabled when empty" tuning fields must emit `KEY=` (not be omitted); API binders + // normalise the empty vars back to null so the not-configured branches still fire. + var (config, secrets) = MakeInputs(); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters.ContainsKey("AVATAR_STORAGE_ROOT")).IsFalse(); + await Assert.That(replacements.Parameters.ContainsKey("AVATAR_PUBLIC_BASE")).IsTrue(); + await Assert.That(replacements.Parameters.ContainsKey("OTLP_ENDPOINT")).IsTrue(); + await Assert.That(replacements.Parameters.ContainsKey("SOCKET_BATCH_BYTES_THRESHOLD")).IsTrue(); + await Assert.That(replacements.Parameters["AVATAR_PUBLIC_BASE"]).IsEqualTo(string.Empty); + await Assert.That(replacements.Parameters["OTLP_ENDPOINT"]).IsEqualTo(string.Empty); + await Assert.That(replacements.Parameters["SOCKET_BATCH_BYTES_THRESHOLD"]).IsEqualTo(string.Empty); + + // Non-nullable tuning fields fall back to their property-initialiser defaults. + await Assert.That(replacements.Parameters["NODE_GROUP"]).IsEqualTo("auxiliary"); + await Assert.That(replacements.Parameters["DB_RETRY_ATTEMPTS"]).IsEqualTo("3"); + await Assert.That(replacements.Parameters["DB_RETRY_INITIAL_DELAY_MS"]).IsEqualTo("100"); + await Assert.That(replacements.Parameters["DB_RETRY_MAX_DELAY_MS"]).IsEqualTo("1500"); + await Assert.That(replacements.Parameters["HYDRATION_MAX_CONCURRENCY"]).IsEqualTo("8"); + } + + [Test] + public async Task BindMountPathsResolveToAbsoluteUnderOutputDir() + { + var (config, secrets) = MakeInputs(); + var baseDir = Path.Combine(Path.GetTempPath(), "interfold-basedir"); + var outputDir = Path.Combine(Path.GetTempPath(), "interfold-outdir"); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, baseDir, outputDir); + + // /keys was removed — JWT signing material lives in internal.secrets now. + await Assert.That(replacements.BindMounts.ContainsKey("interfold-api:/keys")).IsFalse(); + + var apiCerts = replacements.BindMounts["interfold-api:/certs"]; + await Assert.That(Path.IsPathFullyQualified(apiCerts)).IsTrue(); + await Assert.That(apiCerts).StartsWith(outputDir); + + var apiAvatars = replacements.BindMounts["interfold-api:/app/data/avatars"]; + await Assert.That(Path.IsPathFullyQualified(apiAvatars)).IsTrue(); + await Assert.That(apiAvatars).IsEqualTo(Path.GetFullPath(Path.Combine(outputDir, "data", "avatars"))); + + // Scylla rackdc lives under {outputDir}/support (staged at publish from embeds). + var scyllaRackdc = replacements.BindMounts["scylla:/etc/scylla/cassandra-rackdc.properties"]; + await Assert.That(Path.IsPathFullyQualified(scyllaRackdc)).IsTrue(); + await Assert.That(scyllaRackdc).StartsWith(Path.GetFullPath(Path.Combine(outputDir, "support"))); + await Assert.That(scyllaRackdc).EndsWith("cassandra-rackdc.nam.properties"); + } + + [Test] + public async Task MultiModeAddsOneBindMountPerScyllaRegion() + { + var (config, secrets) = MakeInputs(databaseMode: DatabaseMode.Multi); + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, + baseDir: "/base", outputDir: "/out"); + + // Multi mode emits 7 region nodes (nam, eur, sam, sas, eas, ocn, gdpr). + string[] regions = ["nam", "eur", "sam", "sas", "eas", "ocn", "gdpr"]; + foreach (var region in regions) + { + var key = $"scylla-{region}:/etc/scylla/cassandra-rackdc.properties"; + await Assert.That(replacements.BindMounts.ContainsKey(key)).IsTrue() + .Because($"missing bind mount for region {region}"); + await Assert.That(replacements.BindMounts[key]).EndsWith($"cassandra-rackdc.{region}.properties"); + } + } + + [Test] + public async Task TranslateDatabaseModeSingleProducesScyllaSingleTopology() + { + var (includeScylla, includeCassandra, topology) = PublishPhase.TranslateDatabaseMode(DatabaseMode.Single); + + await Assert.That(includeScylla).IsTrue(); + await Assert.That(includeCassandra).IsFalse(); + await Assert.That(topology).IsEqualTo(ScyllaTopology.Single); + await Assert.That(topology.ToWireValue()).IsEqualTo("single"); + } + + [Test] + public async Task TranslateDatabaseModeMultiProducesScyllaMultiTopology() + { + var (includeScylla, includeCassandra, topology) = PublishPhase.TranslateDatabaseMode(DatabaseMode.Multi); + + await Assert.That(includeScylla).IsTrue(); + await Assert.That(includeCassandra).IsFalse(); + await Assert.That(topology).IsEqualTo(ScyllaTopology.Multi); + await Assert.That(topology.ToWireValue()).IsEqualTo("multi"); + } + + [Test] + public async Task TranslateDatabaseModeCassandraSwapsBackends() + { + // Cassandra mode disables Scylla entirely; topology "single" is filler (the + // cassandra branch in InterfoldAppHost ignores topology). + var (includeScylla, includeCassandra, topology) = PublishPhase.TranslateDatabaseMode(DatabaseMode.Cassandra); + + await Assert.That(includeScylla).IsFalse(); + await Assert.That(includeCassandra).IsTrue(); + await Assert.That(topology).IsEqualTo(ScyllaTopology.Single); + await Assert.That(topology.ToWireValue()).IsEqualTo("single"); + } + + [Test] + public async Task CassandraModeFillsCassandraImageEnvKey() + { + var (config, secrets) = MakeInputs(databaseMode: DatabaseMode.Cassandra); + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters.ContainsKey("CASSANDRA_IMAGE")).IsTrue() + .Because("Aspire emits image: \"${CASSANDRA_IMAGE}\" for the Dockerfile service"); + await Assert.That(replacements.Parameters["CASSANDRA_IMAGE"]) + .IsEqualTo(CassandraImagePhase.LocalImageTag); + } + + [Test] + public async Task NonCassandraModesOmitCassandraImageEnvKey() + { + var (config, secrets) = MakeInputs(databaseMode: DatabaseMode.Single); + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.Parameters.ContainsKey("CASSANDRA_IMAGE")).IsFalse() + .Because("Scylla-only stacks must not carry an unused CASSANDRA_IMAGE entry"); + } + + [Test] + public async Task TranslateDatabaseModeRejectsUnknownValue() + { + // Fail-fast for callers that bypass ConfigPhase.Validate. + var ex = Assert.Throws(() => PublishPhase.TranslateDatabaseMode((DatabaseMode)999)); + + await Assert.That(ex.Message).Contains("databaseMode"); + } + + [Test] + public async Task WebHttpsOffDoesNotAddOctoconWebBindMounts() + { + var (config, secrets) = MakeInputs(); + config.Deployment.IncludeWeb = false; + config.Deployment.WebHttps = false; + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.BindMounts.ContainsKey("octocon-web:/certs")).IsFalse() + .Because("octocon-web:/certs must only appear when deployment.webHttps=true"); + await Assert.That(replacements.BindMounts.ContainsKey( + "octocon-web:/etc/nginx/templates/default.conf.template")).IsFalse() + .Because("nginx template bind mount must only appear when deployment.webHttps=true"); + } + + [Test] + public async Task IncludeWebOnlyDoesNotAddOctoconWebBindMounts() + { + // HTTP-only debug variant: nginx never reads a leaf cert, so neither cert nor + // template mounts belong in .env. Include-vs-TLS-mount decoupling is intentional. + var (config, secrets) = MakeInputs(); + config.Deployment.IncludeWeb = true; + config.Deployment.WebHttps = false; + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, "/base", "/out"); + + await Assert.That(replacements.BindMounts.ContainsKey("octocon-web:/certs")).IsFalse() + .Because("HTTP-only web container does not read leaf certs — /certs bind mount must stay off"); + await Assert.That(replacements.BindMounts.ContainsKey( + "octocon-web:/etc/nginx/templates/default.conf.template")).IsFalse() + .Because("HTTP-only web container does not render the TLS template — nginx mount must stay off"); + } + + [Test] + public async Task IncludeWebAndWebHttpsBothOnAddsCertsAndNginxTemplateBindMounts() + { + // Locks the "both flags on" shape so a future OR-gated refactor can't silently drop + // the mounts in either input form. + var (config, secrets) = MakeInputs(); + config.Deployment.IncludeWeb = true; + config.Deployment.WebHttps = true; + var baseDir = Path.Combine(Path.GetTempPath(), "interfold-basedir"); + var outputDir = Path.Combine(Path.GetTempPath(), "interfold-outdir"); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, baseDir, outputDir); + + await Assert.That(replacements.BindMounts.ContainsKey("octocon-web:/certs")).IsTrue(); + await Assert.That(replacements.BindMounts.ContainsKey( + "octocon-web:/etc/nginx/templates/default.conf.template")).IsTrue(); + } + + [Test] + public async Task WebHttpsOnAddsCertsAndNginxTemplateBindMounts() + { + var (config, secrets) = MakeInputs(); + config.Deployment.WebHttps = true; + var baseDir = Path.Combine(Path.GetTempPath(), "interfold-basedir"); + var outputDir = Path.Combine(Path.GetTempPath(), "interfold-outdir"); + + var replacements = PublishPhase.BuildEnvReplacements(config, secrets, baseDir, outputDir); + + // API + web share {outputDir}/certs so Kestrel's PFX and nginx's CRT/KEY come from + // the same CertificatePhase output. + await Assert.That(replacements.BindMounts.ContainsKey("octocon-web:/certs")).IsTrue(); + var webCerts = replacements.BindMounts["octocon-web:/certs"]; + await Assert.That(Path.IsPathFullyQualified(webCerts)).IsTrue(); + await Assert.That(webCerts).StartsWith(outputDir); + await Assert.That(replacements.BindMounts["interfold-api:/certs"]).IsEqualTo(webCerts) + .Because("API and web tiers must read from the same on-disk certs directory"); + + // nginx template lives under {outputDir}/support (staged at publish from embeds). + const string nginxKey = "octocon-web:/etc/nginx/templates/default.conf.template"; + await Assert.That(replacements.BindMounts.ContainsKey(nginxKey)).IsTrue(); + var nginxTemplate = replacements.BindMounts[nginxKey]; + await Assert.That(Path.IsPathFullyQualified(nginxTemplate)).IsTrue(); + await Assert.That(nginxTemplate).StartsWith(Path.GetFullPath(Path.Combine(outputDir, "support"))); + await Assert.That(nginxTemplate).EndsWith("default.conf.template"); + } + + [Test] + public async Task ApiImageOverrideDoesNotLeakIntoEnvReplacements() + { + // ApiImage flows via Aspire Parameters:api-image into compose YAML, not .env. + var (configA, secretsA) = MakeInputs(apiImage: "ghcr.io/azyyyyyy/interfold-api:v1.2.3"); + var (configB, secretsB) = MakeInputs(apiImage: "private-registry.example.com/api:custom-tag"); + secretsB.PostgresPassword = secretsA.PostgresPassword; + secretsB.PostgresInitPassword = secretsA.PostgresInitPassword; + secretsB.PostgresAdminPassword = secretsA.PostgresAdminPassword; + secretsB.ScyllaPassword = secretsA.ScyllaPassword; + secretsB.ScyllaAdminPassword = secretsA.ScyllaAdminPassword; + secretsB.EncryptionPrivateKeyB64 = secretsA.EncryptionPrivateKeyB64; + + var a = PublishPhase.BuildEnvReplacements(configA, secretsA, "/base", "/out"); + var b = PublishPhase.BuildEnvReplacements(configB, secretsB, "/base", "/out"); + + await Assert.That(a.Parameters.Count).IsEqualTo(b.Parameters.Count); + foreach (var kv in a.Parameters) + { + await Assert.That(b.Parameters.ContainsKey(kv.Key)).IsTrue(); + await Assert.That(b.Parameters[kv.Key]).IsEqualTo(kv.Value); + } + + foreach (var key in a.Parameters.Keys) + { + await Assert.That(key.Contains("IMAGE", StringComparison.OrdinalIgnoreCase)).IsFalse() + .Because($"unexpected image-related key '{key}' leaked into env replacements"); + } + } + + [Test] + public async Task WebServerNameSkipsCidrEntries() + { + // nginx server_name accepts DNS and IPs but not CIDRs; PickServerName must skip. + var serverName = PublishPhase.PickServerName(["192.168.1.0/24", "api.example.com"]); + await Assert.That(serverName).IsEqualTo("api.example.com"); + } + + [Test] + public async Task WebServerNameFallsBackToCatchAllWhenAllCidr() + { + // Direct InterfoldAppHost.Configure callers skip Validate; `_` is nginx's catch-all. + var serverName = PublishPhase.PickServerName(["10.0.0.0/8", "fe80::/64"]); + await Assert.That(serverName).IsEqualTo("_"); + } + + [Test] + public async Task WebServerNamePicksIpLiteralAsServerName() + { + // LAN-only deployments (no DNS) get the bare IP; nginx accepts dotted-quad + v6. + var serverName = PublishPhase.PickServerName(["192.168.1.42"]); + await Assert.That(serverName).IsEqualTo("192.168.1.42"); + } + + [Test] + public async Task StampCassandraPullPolicyNeverInsertsPolicyAfterImageLine() + { + // Inserts `pull_policy: never` at the image indent so `docker compose pull` skips + // the non-registry-backed tag. End-to-end behaviour tested in UpdateImagesCassandraModeTests. + var tmp = Path.Combine(Path.GetTempPath(), $"compose-stamp-{Guid.NewGuid():N}.yaml"); + try + { + var original = string.Join("\n", new[] + { + "services:", + " cassandra:", + " image: \"${CASSANDRA_IMAGE}\"", + " volumes:", + " - cassandra-data:/var/lib/cassandra", + " interfold-api:", + " image: interfold-api:test", + "", + }); + await File.WriteAllTextAsync(tmp, original); + + PublishPhase.StampCassandraPullPolicyNever(tmp); + + var lines = await File.ReadAllLinesAsync(tmp); + var imageIdx = Array.FindIndex(lines, l => l.Contains("${CASSANDRA_IMAGE}", StringComparison.Ordinal)); + await Assert.That(imageIdx).IsGreaterThanOrEqualTo(0) + .Because("baseline: the anchor line must still be present after stamping"); + await Assert.That(lines[imageIdx + 1]).IsEqualTo(" pull_policy: never") + .Because("stamp must land on the very next line, at the same 4-space indent as the image key"); + } + finally + { + if (File.Exists(tmp)) File.Delete(tmp); + } + } + + [Test] + public async Task StampCassandraPullPolicyNeverIsIdempotent() + { + // Reruns of `bootstrap publish` must not double-stamp. + var tmp = Path.Combine(Path.GetTempPath(), $"compose-stamp-idem-{Guid.NewGuid():N}.yaml"); + try + { + var original = string.Join("\n", new[] + { + "services:", + " cassandra:", + " image: \"${CASSANDRA_IMAGE}\"", + " volumes:", + " - cassandra-data:/var/lib/cassandra", + "", + }); + await File.WriteAllTextAsync(tmp, original); + + PublishPhase.StampCassandraPullPolicyNever(tmp); + PublishPhase.StampCassandraPullPolicyNever(tmp); + + var lines = await File.ReadAllLinesAsync(tmp); + var count = lines.Count(l => string.Equals(l.Trim(), "pull_policy: never", StringComparison.Ordinal)); + await Assert.That(count).IsEqualTo(1) + .Because("second invocation must be a no-op — one policy line, not two"); + } + finally + { + if (File.Exists(tmp)) File.Delete(tmp); + } + } + + [Test] + public async Task StampCassandraPullPolicyNeverIsNoOpWhenCassandraAnchorAbsent() + { + // No ${CASSANDRA_IMAGE} anchor → leave the file alone; keeps the stamper safe + // to call from any future context. + var tmp = Path.Combine(Path.GetTempPath(), $"compose-stamp-noop-{Guid.NewGuid():N}.yaml"); + try + { + var original = string.Join("\n", new[] + { + "services:", + " scylla:", + " image: scylladb/scylla:2026.1", + " interfold-api:", + " image: interfold-api:test", + "", + }); + await File.WriteAllTextAsync(tmp, original); + + PublishPhase.StampCassandraPullPolicyNever(tmp); + + var after = await File.ReadAllTextAsync(tmp); + await Assert.That(after.Contains("pull_policy", StringComparison.Ordinal)).IsFalse() + .Because("no ${CASSANDRA_IMAGE} anchor means nothing to stamp; the file must be untouched"); + } + finally + { + if (File.Exists(tmp)) File.Delete(tmp); + } + } + + [Test] + public async Task EnumerateSharedAspireParametersConfigKeyMatchesEnvKeyKebabToUpperSnake() + { + // EnvKey must equal upper-snake(kebab parameter name); a hand-typed EnvKey would + // otherwise silently blank the corresponding OCTOCON_* env var. + var (config, secrets) = MakeInputs(); + + var seenConfigKeys = new HashSet(StringComparer.Ordinal); + var seenEnvKeys = new HashSet(StringComparer.Ordinal); + + foreach (var (configKey, envKey, _) in PublishPhase.EnumerateSharedAspireParameters(config, secrets)) + { + await Assert.That(seenConfigKeys.Add(configKey)).IsTrue() + .Because($"config-key '{configKey}' appears twice in EnumerateSharedAspireParameters"); + await Assert.That(seenEnvKeys.Add(envKey)).IsTrue() + .Because($"env-key '{envKey}' appears twice in EnumerateSharedAspireParameters"); + + // ToParameterName also enforces the "Parameters:*" namespace by throwing for + // graph-only Ports:* entries. + var bareName = AppHostParameterKeys.ToParameterName(configKey); + var expectedEnvKey = bareName.Replace('-', '_').ToUpperInvariant(); + await Assert.That(envKey).IsEqualTo(expectedEnvKey) + .Because($"env-key '{envKey}' must be the upper-snake-cased form of the kebab-cased Aspire parameter '{bareName}' (config-key '{configKey}')"); + } + + // Spec-frozen at 23 — bump this AND the enumerator together. + await Assert.That(seenConfigKeys.Count).IsEqualTo(23) + .Because("shared-parameter count is spec-frozen at 23; update BOTH the enumerator AND this assertion together"); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/RestoreCommandBuildingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/RestoreCommandBuildingTests.cs new file mode 100644 index 00000000..3de2e792 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/RestoreCommandBuildingTests.cs @@ -0,0 +1,131 @@ +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Argv-shape assertions for . Pins the pg_restore + docker cp +/// command layout so a refactor that swaps flag order or drops `--clean` catches at +/// unit speed rather than only in the DinD integration suite. +/// +public sealed class RestoreCommandBuildingTests +{ + [Test] + public async Task PgRestoreArgsIncludeCleanIfExistsAndSingleTransaction() + { + var args = RestorePhase.BuildPgRestoreArgs( + composeFile: "/srv/deploy/docker-compose.yaml", + adminUser: "interfold_admin", + database: "interfold"); + + // --clean --if-exists makes the restore idempotent (DROP ... IF EXISTS statements + // before the recreate). --single-transaction gives all-or-nothing semantics so a + // partial-failure re-run doesn't leave a half-restored soup. --no-owner strips the + // owner clauses since the destination role may not match the source dump. + await Assert.That(args).IsEquivalentTo(new[] + { + "compose", "-f", "/srv/deploy/docker-compose.yaml", + "exec", "-T", + "--env", Interfold.Bootstrapper.Util.DatabaseArchiveStreamer.PgPasswordEnvVar, + "msg-db", + "pg_restore", + "-U", "interfold_admin", + "-d", "interfold", + "--clean", "--if-exists", + "--single-transaction", + "--no-owner", + }); + } + + [Test] + public async Task PgRestoreArgsDoNotContainPlaintextPassword() + { + // Same contract as pg_dump: password reaches the process via PGPASSWORD env var, + // never argv. `ps` output must not leak the admin credential. + var args = RestorePhase.BuildPgRestoreArgs( + composeFile: "/srv/deploy/docker-compose.yaml", + adminUser: "interfold_admin", + database: "interfold"); + + foreach (var arg in args) + { + await Assert.That(arg).DoesNotContain("password"); + await Assert.That(arg).DoesNotContain("secret"); + } + } + + [Test] + public async Task BuildContainerCpWriteArgsStreamsStdinToContainer() + { + // `docker cp - :` is the inverse of the backup path — reads a raw tar + // from stdin and extracts it into the container's . Order matters: `-` + // must precede the container:path spec, not the other way around. + var args = RestorePhase.BuildContainerCpWriteArgs( + containerId: "63fe606f0e95", + dataPath: ContainerMountPaths.ScyllaData); + + await Assert.That(args).IsEquivalentTo(new[] + { + "cp", "-", $"63fe606f0e95:{ContainerMountPaths.ScyllaData}", + }); + } + + [Test] + public async Task BuildContainerCpWriteArgsRejectsBlankInputs() + { + Assert.Throws( + () => RestorePhase.BuildContainerCpWriteArgs(containerId: "", dataPath: ContainerMountPaths.ScyllaData)); + Assert.Throws( + () => RestorePhase.BuildContainerCpWriteArgs(containerId: "abc", dataPath: " ")); + await Task.CompletedTask; + } + + [Test] + public async Task ResolveLatestArchivePicksNewestByMtime() + { + // Test-only file staging: three files with distinct mtimes; ResolveLatestArchive + // must pick the one with the newest timestamp regardless of alphabetical order. + using var scratch = TestSupport.NewScratchDir("interfold-restore-latest"); + var tmpDir = scratch.Path; + var oldest = Path.Combine(tmpDir, "20260101-000000.dump"); + var middle = Path.Combine(tmpDir, "20260201-000000.dump"); + var newest = Path.Combine(tmpDir, "20260301-000000.dump"); + await File.WriteAllTextAsync(oldest, ""); + await File.WriteAllTextAsync(middle, ""); + await File.WriteAllTextAsync(newest, ""); + File.SetLastWriteTimeUtc(oldest, new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc)); + File.SetLastWriteTimeUtc(middle, new DateTime(2026, 2, 1, 0, 0, 0, DateTimeKind.Utc)); + File.SetLastWriteTimeUtc(newest, new DateTime(2026, 3, 1, 0, 0, 0, DateTimeKind.Utc)); + + var picked = BackupStoragePaths.LatestFile(tmpDir, "*.dump"); + + await Assert.That(picked).IsNotNull(); + await Assert.That(picked!.FullName).IsEqualTo(newest); + } + + [Test] + public async Task ResolveLatestArchiveReturnsNullOnMissingDirectory() + { + // Fresh installs will not have a {backupRoot}/scylla/ before the first backup. + // The resolver must return null in that case, not throw — the caller checks for + // null and surfaces "no archive found" in the operator-facing error. + var picked = BackupStoragePaths.LatestFile( + Path.Combine(Path.GetTempPath(), "does-not-exist-" + Guid.NewGuid().ToString("N")), + "*.dump"); + + await Assert.That(picked).IsNull(); + } + + [Test] + public async Task ResolveLatestArchiveReturnsNullWhenPatternDoesNotMatch() + { + // Directory exists but contains only non-matching files. + using var scratch = TestSupport.NewScratchDir("interfold-restore-nomatch"); + var tmpDir = scratch.Path; + await File.WriteAllTextAsync(Path.Combine(tmpDir, "readme.txt"), ""); + + var picked = BackupStoragePaths.LatestFile(tmpDir, "*.dump"); + + await Assert.That(picked).IsNull(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsBackfillTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsBackfillTests.cs new file mode 100644 index 00000000..611aac60 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsBackfillTests.cs @@ -0,0 +1,152 @@ +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Covers the backfill logic in . A pre-existing +/// secrets.json written by an older bootstrapper version may be missing keys that newer +/// phases require (e.g. postgresInitPassword, postgresAdminPassword, +/// scyllaAdminPassword, or the four JWT PEMs). The phase must detect the missing keys, +/// mint replacements, and persist the result while leaving other already-populated fields alone. +/// +public sealed class SecretsBackfillTests +{ + private static BootstrapOptions OptionsFor(string outputDir, bool rotateSecrets = false) => + TestSupport.MakeOptions( + command: BootstrapCommand.Bootstrap, + outputDir: outputDir, + skipPrereqs: true, + rotateSecrets: rotateSecrets, + nonInteractive: true); + + /// Writes as JSON to the canonical location under . + private static async Task StagePriorSecretsAsync(string outputDir, GeneratedSecrets secrets) + { + var secretsDir = Path.Combine(outputDir, "secrets"); + Directory.CreateDirectory(secretsDir); + var path = Path.Combine(secretsDir, "secrets.json"); + var json = JsonSerializer.Serialize(secrets, BootstrapJsonContext.Default.GeneratedSecrets); + await File.WriteAllTextAsync(path, json); + } + + private static async Task LoadPersistedAsync(string outputDir) + { + var path = Path.Combine(outputDir, "secrets", "secrets.json"); + var json = await File.ReadAllTextAsync(path); + return JsonSerializer.Deserialize(json, BootstrapJsonContext.Default.GeneratedSecrets)!; + } + + private sealed record BackfillSetup(TestSupport.ScratchDir Scratch, GeneratedSecrets Prior, GeneratedSecrets Result) : IDisposable + { + public void Dispose() => Scratch.Dispose(); + } + + private static async Task RunBackfillAsync(Action? mutate = null, bool rotateSecrets = false) + { + var scratch = TestSupport.NewScratchDir("interfold-secrets"); + try + { + var prior = SecretsPhase.Generate(); + mutate?.Invoke(prior); + await StagePriorSecretsAsync(scratch.Path, prior); + + var options = OptionsFor(scratch.Path, rotateSecrets); + var logger = new PhaseLogger(options); + var result = await SecretsPhase.RunAsync(options, new BootstrapConfig(), logger, CancellationToken.None); + return new BackfillSetup(scratch, prior, result); + } + catch + { + scratch.Dispose(); + throw; + } + } + + [Test] + public async Task BackfillsMissingPostgresInitPassword() + { + using var setup = await RunBackfillAsync(p => p.PostgresInitPassword = string.Empty); + await Assert.That(setup.Result.PostgresInitPassword).IsNotEmpty(); + var reloaded = await LoadPersistedAsync(setup.Scratch.Path); + await Assert.That(reloaded.PostgresInitPassword).IsEqualTo(setup.Result.PostgresInitPassword); + await Assert.That(reloaded.PostgresPassword).IsEqualTo(setup.Prior.PostgresPassword); + } + + [Test] + public async Task BackfillsMissingPostgresAdminPassword() + { + using var setup = await RunBackfillAsync(p => p.PostgresAdminPassword = string.Empty); + await Assert.That(setup.Result.PostgresAdminPassword).IsNotEmpty(); + var reloaded = await LoadPersistedAsync(setup.Scratch.Path); + await Assert.That(reloaded.PostgresAdminPassword).IsEqualTo(setup.Result.PostgresAdminPassword); + await Assert.That(reloaded.PostgresPassword).IsEqualTo(setup.Prior.PostgresPassword); + } + + [Test] + public async Task BackfillsMissingScyllaAdminPassword() + { + using var setup = await RunBackfillAsync(p => p.ScyllaAdminPassword = string.Empty); + await Assert.That(setup.Result.ScyllaAdminPassword).IsNotEmpty(); + var reloaded = await LoadPersistedAsync(setup.Scratch.Path); + await Assert.That(reloaded.ScyllaAdminPassword).IsEqualTo(setup.Result.ScyllaAdminPassword); + await Assert.That(reloaded.ScyllaPassword).IsEqualTo(setup.Prior.ScyllaPassword); + } + + [Test] + public async Task RotateSecretsPreservesLeafPfxPassword() + { + using var setup = await RunBackfillAsync(p => p.LeafPfxPassword = "well-known-pfx-password-do-not-rotate-on-secrets-rotate", rotateSecrets: true); + await Assert.That(setup.Result.LeafPfxPassword).IsEqualTo(setup.Prior.LeafPfxPassword); + await Assert.That(setup.Result.PostgresPassword).IsNotEqualTo(setup.Prior.PostgresPassword); + await Assert.That(setup.Result.ScyllaPassword).IsNotEqualTo(setup.Prior.ScyllaPassword); + } + + [Test] + public async Task RerunDoesNotEmitKeysDirectory() + { + using var setup = await RunBackfillAsync(); + var keysDir = Path.Combine(setup.Scratch.Path, "secrets", "keys"); + await Assert.That(Directory.Exists(keysDir)).IsFalse() + .Because("JWT PEMs live in internal.secrets exclusively; no keys/ dir should appear."); + } + + [Test] + public async Task BackfillsMissingDeepLinkSecret() + { + using var setup = await RunBackfillAsync(p => p.DeepLinkSecret = string.Empty); + await Assert.That(setup.Result.DeepLinkSecret).IsNotEmpty(); + var reloaded = await LoadPersistedAsync(setup.Scratch.Path); + await Assert.That(reloaded.DeepLinkSecret).IsEqualTo(setup.Result.DeepLinkSecret); + await Assert.That(reloaded.PostgresPassword).IsEqualTo(setup.Prior.PostgresPassword); + } + + [Test] + public async Task BackfillsMissingJwtRsaPrivatePem() + { + using var setup = await RunBackfillAsync(p => { p.JwtRsa256PrivateKeyPem = string.Empty; p.JwtRsa256PublicKeyPem = string.Empty; }); + await Assert.That(setup.Result.JwtRsa256PrivateKeyPem).Contains("-----BEGIN"); + await Assert.That(setup.Result.JwtRsa256PublicKeyPem).Contains("-----BEGIN"); + await Assert.That(setup.Result.JwtEs256PrivateKeyPem).IsEqualTo(setup.Prior.JwtEs256PrivateKeyPem); + } + + [Test] + public async Task BackfillsMissingJwtEs256PrivatePem() + { + using var setup = await RunBackfillAsync(p => { p.JwtEs256PrivateKeyPem = string.Empty; p.JwtEs256PublicKeyPem = string.Empty; }); + await Assert.That(setup.Result.JwtEs256PrivateKeyPem).Contains("-----BEGIN"); + await Assert.That(setup.Result.JwtEs256PublicKeyPem).Contains("-----BEGIN"); + await Assert.That(setup.Result.JwtRsa256PrivateKeyPem).IsEqualTo(setup.Prior.JwtRsa256PrivateKeyPem); + } + + [Test] + public async Task RotateSecretsRegeneratesJwtAndDeepLink() + { + using var setup = await RunBackfillAsync(rotateSecrets: true); + await Assert.That(setup.Result.JwtRsa256PrivateKeyPem).IsNotEqualTo(setup.Prior.JwtRsa256PrivateKeyPem); + await Assert.That(setup.Result.JwtEs256PrivateKeyPem).IsNotEqualTo(setup.Prior.JwtEs256PrivateKeyPem); + await Assert.That(setup.Result.DeepLinkSecret).IsNotEqualTo(setup.Prior.DeepLinkSecret); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsGenerationTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsGenerationTests.cs new file mode 100644 index 00000000..6ba410a1 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/SecretsGenerationTests.cs @@ -0,0 +1,145 @@ +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Properties of and +/// that the rest of the stack depends on: alphabet, length, uniqueness, and round-trip safety. +/// +public sealed class SecretsGenerationTests +{ + /// Mirror of the alphabet defined in SecretsPhase. Updating one without the other is a bug; this duplication makes the contract testable. + private const string ExpectedAlphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; + + [Test] + public async Task RandomPasswordUsesExpectedAlphabetAndLength() + { + // Sample enough passwords that we cover most of the alphabet — gives us a real check + // that the generator is drawing uniformly and not, say, biased to a single char. + var seen = new HashSet(); + for (var i = 0; i < 200; i++) + { + var pw = SecretsPhase.RandomPassword(); + await Assert.That(pw.Length).IsEqualTo(32) + .Because("password length is part of the contract — operators script around it"); + foreach (var c in pw) + { + await Assert.That(ExpectedAlphabet.Contains(c)).IsTrue() + .Because($"password contains out-of-alphabet char '{c}' (0x{(int)c:X2})"); + seen.Add(c); + } + } + // Loose bound — over 200 32-char samples we expect to see well over half the alphabet. + await Assert.That(seen.Count).IsGreaterThan(ExpectedAlphabet.Length / 2); + } + + [Test] + public async Task GenerateProducesUniqueValuesAcrossCalls() + { + var a = SecretsPhase.Generate(); + var b = SecretsPhase.Generate(); + + await Assert.That(a.PostgresPassword).IsNotEqualTo(b.PostgresPassword); + await Assert.That(a.PostgresInitPassword).IsNotEqualTo(b.PostgresInitPassword); + await Assert.That(a.PostgresAdminPassword).IsNotEqualTo(b.PostgresAdminPassword); + await Assert.That(a.ScyllaPassword).IsNotEqualTo(b.ScyllaPassword); + await Assert.That(a.ScyllaAdminPassword).IsNotEqualTo(b.ScyllaAdminPassword); + await Assert.That(a.EncryptionPepper).IsNotEqualTo(b.EncryptionPepper); + await Assert.That(a.LeafPfxPassword).IsNotEqualTo(b.LeafPfxPassword); + // RSA / ECDSA keys generate from independent random states so the PEM blobs must differ too. + await Assert.That(a.EncryptionPrivateKeyB64).IsNotEqualTo(b.EncryptionPrivateKeyB64); + await Assert.That(a.JwtRsa256PrivateKeyPem).IsNotEqualTo(b.JwtRsa256PrivateKeyPem); + await Assert.That(a.JwtEs256PrivateKeyPem).IsNotEqualTo(b.JwtEs256PrivateKeyPem); + } + + [Test] + public async Task GenerateAlwaysPopulatesEveryField() + { + // Forward-looking guard: if someone adds a new field to GeneratedSecrets but forgets to + // populate it inside Generate(), this test fails with a clear message naming the field. + var s = SecretsPhase.Generate(); + + await Assert.That(s.PostgresUser).IsNotEmpty(); + await Assert.That(s.PostgresPassword).IsNotEmpty(); + await Assert.That(s.PostgresInitPassword).IsNotEmpty(); + await Assert.That(s.PostgresAdminPassword).IsNotEmpty(); + await Assert.That(s.ScyllaUser).IsNotEmpty(); + await Assert.That(s.ScyllaPassword).IsNotEmpty(); + await Assert.That(s.ScyllaAdminPassword).IsNotEmpty(); + await Assert.That(s.EncryptionPrivateKeyB64).IsNotEmpty(); + await Assert.That(s.EncryptionPepper).IsNotEmpty(); + await Assert.That(s.LeafPfxPassword).IsNotEmpty(); + await Assert.That(s.JwtRsa256PublicKeyPem).IsNotEmpty(); + await Assert.That(s.JwtRsa256PrivateKeyPem).IsNotEmpty(); + await Assert.That(s.JwtEs256PublicKeyPem).IsNotEmpty(); + await Assert.That(s.JwtEs256PrivateKeyPem).IsNotEmpty(); + } + + /// + /// Auth-boundary drift guard: and the second-run backfill + /// path in both mint fresh JWT keypairs on their own paths + /// and MUST emit PEMs the API's RSA.ImportFromPem / ECDsa.ImportFromPem accepts. + /// Both paths route through / + /// , so we pin the PEM headers those helpers + /// emit. If someone changes the export format (PKCS#8 → PKCS#1, SEC1 → PKCS#8, etc.) this test + /// fails loudly before the mismatch reaches production. + /// + [Test] + public async Task JwtKeypairHelpersEmitExpectedPemBanners() + { + var (rsaPublic, rsaPrivate) = SecretsPhase.GenerateJwtRsaKeypair(); + await Assert.That(rsaPublic).StartsWith("-----BEGIN PUBLIC KEY-----") + .Because("API-side RSA.ImportFromPem expects SubjectPublicKeyInfo PEM ('PUBLIC KEY')."); + await Assert.That(rsaPublic.TrimEnd()).EndsWith("-----END PUBLIC KEY-----"); + await Assert.That(rsaPrivate).StartsWith("-----BEGIN PRIVATE KEY-----") + .Because("API-side RSA.ImportFromPem expects PKCS#8 PEM ('PRIVATE KEY'), not PKCS#1 ('RSA PRIVATE KEY')."); + await Assert.That(rsaPrivate.TrimEnd()).EndsWith("-----END PRIVATE KEY-----"); + + var (esPublic, esPrivate) = SecretsPhase.GenerateJwtEs256Keypair(); + await Assert.That(esPublic).StartsWith("-----BEGIN PUBLIC KEY-----") + .Because("API-side ECDsa.ImportFromPem expects SubjectPublicKeyInfo PEM for the public half."); + await Assert.That(esPublic.TrimEnd()).EndsWith("-----END PUBLIC KEY-----"); + await Assert.That(esPrivate).StartsWith("-----BEGIN EC PRIVATE KEY-----") + .Because("API-side ECDsa.ImportFromPem expects SEC1 PEM ('EC PRIVATE KEY') for the private half."); + await Assert.That(esPrivate.TrimEnd()).EndsWith("-----END EC PRIVATE KEY-----"); + + // Second-shape guard: a Generate() call must produce PEMs with the SAME banner as the helper + // in isolation. If Generate() were to drift and emit a different export format, only this + // fresh-vs-generate comparison would catch it (the standalone helper test above wouldn't). + var generated = SecretsPhase.Generate(); + await Assert.That(generated.JwtRsa256PublicKeyPem).StartsWith("-----BEGIN PUBLIC KEY-----"); + await Assert.That(generated.JwtRsa256PrivateKeyPem).StartsWith("-----BEGIN PRIVATE KEY-----"); + await Assert.That(generated.JwtEs256PublicKeyPem).StartsWith("-----BEGIN PUBLIC KEY-----"); + await Assert.That(generated.JwtEs256PrivateKeyPem).StartsWith("-----BEGIN EC PRIVATE KEY-----"); + } + + [Test] + public async Task PersistedSecretsRoundTripViaLoad() + { + // Persist → Load must preserve every field byte-for-byte. This catches accidental + // serialiser-option drift (e.g. a missing JsonPropertyName) before it surfaces as a + // "user can't auth after restart" production incident. + using var scratch = TestSupport.NewScratchDir("interfold-roundtrip"); + var tmpDir = scratch.Path; + var original = SecretsPhase.Generate(); + var path = Path.Combine(tmpDir, "secrets.json"); + await SecretsPhase.PersistAsync(original, path, CancellationToken.None); + + var loaded = await SecretsPhase.LoadAsync(path, CancellationToken.None); + + await Assert.That(loaded.PostgresUser).IsEqualTo(original.PostgresUser); + await Assert.That(loaded.PostgresPassword).IsEqualTo(original.PostgresPassword); + await Assert.That(loaded.PostgresInitPassword).IsEqualTo(original.PostgresInitPassword); + await Assert.That(loaded.PostgresAdminPassword).IsEqualTo(original.PostgresAdminPassword); + await Assert.That(loaded.ScyllaUser).IsEqualTo(original.ScyllaUser); + await Assert.That(loaded.ScyllaPassword).IsEqualTo(original.ScyllaPassword); + await Assert.That(loaded.ScyllaAdminPassword).IsEqualTo(original.ScyllaAdminPassword); + await Assert.That(loaded.EncryptionPrivateKeyB64).IsEqualTo(original.EncryptionPrivateKeyB64); + await Assert.That(loaded.EncryptionPepper).IsEqualTo(original.EncryptionPepper); + await Assert.That(loaded.LeafPfxPassword).IsEqualTo(original.LeafPfxPassword); + await Assert.That(loaded.JwtRsa256PublicKeyPem).IsEqualTo(original.JwtRsa256PublicKeyPem); + await Assert.That(loaded.JwtRsa256PrivateKeyPem).IsEqualTo(original.JwtRsa256PrivateKeyPem); + await Assert.That(loaded.JwtEs256PublicKeyPem).IsEqualTo(original.JwtEs256PublicKeyPem); + await Assert.That(loaded.JwtEs256PrivateKeyPem).IsEqualTo(original.JwtEs256PrivateKeyPem); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/SharedValidationBoundsTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/SharedValidationBoundsTests.cs new file mode 100644 index 00000000..13505c0f --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/SharedValidationBoundsTests.cs @@ -0,0 +1,284 @@ +using System.ComponentModel.DataAnnotations; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Configuration.Validation; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Unit tests for the shared validation surface ( + +/// + ). Two +/// responsibilities: +/// +/// +/// Pure attribute / bounds behaviour — the attributes and constants stand on their own. +/// Bootstrapper-vs-API parity — a value that trips the bootstrapper's +/// must also trip -style +/// DataAnnotations on the matching options class, and vice versa. Drift here means an +/// operator sees a green config gate followed by a red API boot. +/// +/// +public sealed class SharedValidationBoundsTests +{ + // --------------------------------------------------------------------------------- + // ConfigurationBounds — sanity checks that no bound has been swapped or zeroed. + // --------------------------------------------------------------------------------- + + [Test] + public async Task BoundsAreMonotonicAndPositive() + { + // Guards against the "someone accidentally set Min > Max" refactor mistake. Every + // pair the API's [Range] attributes lean on is asserted here so a bad edit trips one + // fast unit test rather than a mysterious boot-time DataAnnotations failure. The + // (min, max, name) tuple form runs the check at runtime so the TUnit analyzer sees + // dynamic operands instead of const-folding the comparison away. + (int Min, int Max, string Name)[] bounds = + [ + (ConfigurationBounds.DbRetryAttemptsMin, ConfigurationBounds.DbRetryAttemptsMax, nameof(ConfigurationBounds.DbRetryAttemptsMin)), + (ConfigurationBounds.DbRetryInitialDelayMsMin, ConfigurationBounds.DbRetryInitialDelayMsMax, nameof(ConfigurationBounds.DbRetryInitialDelayMsMin)), + (ConfigurationBounds.DbRetryMaxDelayMsMin, ConfigurationBounds.DbRetryMaxDelayMsMax, nameof(ConfigurationBounds.DbRetryMaxDelayMsMin)), + (ConfigurationBounds.HydrationMaxConcurrencyMin, ConfigurationBounds.HydrationMaxConcurrencyMax, nameof(ConfigurationBounds.HydrationMaxConcurrencyMin)), + (ConfigurationBounds.SocketBatchBytesThresholdMin, ConfigurationBounds.SocketBatchBytesThresholdMax, nameof(ConfigurationBounds.SocketBatchBytesThresholdMin)), + ]; + + foreach (var (min, max, name) in bounds) + { + await Assert.That(min < max).IsTrue().Because($"{name}: min ({min}) must be strictly less than max ({max})."); + await Assert.That(min >= 1).IsTrue().Because($"{name}: min ({min}) must be at least 1 to reject the zero-attempts / zero-delay footgun."); + } + } + + [Test] + public async Task SocketBatchBytesThresholdMaxIs16MiB() + { + // The 16 MiB ceiling is a wire contract with the WebSocket batcher — this pins it so + // a future edit that widens the bound without adjusting the batcher's back-pressure + // budget fails a test instead of only surfacing under real socket load. Materialising + // both sides into locals dodges the TUnit const-compare analyzer. + var actualMax = ConfigurationBounds.SocketBatchBytesThresholdMax; + const int expected16MiB = 16 * 1024 * 1024; + await Assert.That(actualMax == expected16MiB).IsTrue().Because($"SocketBatchBytesThresholdMax was {actualMax}, expected 16 MiB ({expected16MiB})."); + } + + // --------------------------------------------------------------------------------- + // AbsolutePathAttribute — pure attribute behaviour. + // --------------------------------------------------------------------------------- + + [Test] + public async Task AbsolutePathIsAbsolutePath_AcceptsRootedPath() + { + // Path.IsPathRooted returns true for both Unix ("/foo") and Windows ("C:\foo") shapes, + // but the bootstrapper only runs on Unix + Windows hosts — accept both to avoid + // asymmetry between CI hosts. + await Assert.That(AbsolutePathAttribute.IsAbsolutePath("/var/lib/interfold")).IsTrue(); + } + + [Test] + public async Task AbsolutePathIsAbsolutePath_RejectsRelativeAndBlank() + { + await Assert.That(AbsolutePathAttribute.IsAbsolutePath("relative/path")).IsFalse(); + await Assert.That(AbsolutePathAttribute.IsAbsolutePath(null)).IsFalse(); + await Assert.That(AbsolutePathAttribute.IsAbsolutePath("")).IsFalse(); + await Assert.That(AbsolutePathAttribute.IsAbsolutePath(" ")).IsFalse(); + } + + [Test] + public async Task AbsolutePathAttribute_AllowEmpty_PassesForBlank() + { + // AllowEmpty defaults to true so an unset OCTOCON_AVATAR_STORAGE_ROOT (which the + // env-var provider surfaces as "") isn't a boot failure — the storage-disabled branch + // in LocalAvatarStorage handles null/blank internally. + var attr = new AbsolutePathAttribute(); + var ctx = new ValidationContext(new object()) { DisplayName = "AvatarStorageRoot", MemberName = "AvatarStorageRoot" }; + await Assert.That(attr.GetValidationResult("", ctx)).IsEqualTo(ValidationResult.Success); + await Assert.That(attr.GetValidationResult(null, ctx)).IsEqualTo(ValidationResult.Success); + } + + [Test] + public async Task AbsolutePathAttribute_RejectsRelativePath() + { + var attr = new AbsolutePathAttribute(); + var ctx = new ValidationContext(new object()) { DisplayName = "AvatarStorageRoot", MemberName = "AvatarStorageRoot" }; + var result = attr.GetValidationResult("etc/passwd", ctx); + await Assert.That(result).IsNotNull(); + await Assert.That(result!.ErrorMessage).Contains("absolute path"); + } + + // --------------------------------------------------------------------------------- + // AbsoluteHttpUriAttribute — pure attribute behaviour. + // --------------------------------------------------------------------------------- + + [Test] + public async Task AbsoluteHttpUri_AcceptsHttpAndHttps() + { + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("http://otel:4317")).IsTrue(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("https://cdn.example.com/avatars/")).IsTrue(); + } + + [Test] + public async Task AbsoluteHttpUri_RejectsNonHttpSchemes() + { + // The bootstrapper accepts only http(s) URIs for OTLP + AvatarPublicBase — gRPC schemes + // and relative URIs must be rejected here so a hand-edited env doesn't sneak past the + // API's ValidateOnStart. + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("grpc://otel:4317")).IsFalse(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("file:///tmp/root.crt")).IsFalse(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("/relative/url")).IsFalse(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("not a url")).IsFalse(); + } + + [Test] + public async Task AbsoluteHttpUri_RejectsBlankOnCoreCheck() + { + // Core boolean check treats blank as "not a URL" — the attribute wrapper handles the + // AllowEmpty short-circuit for optional properties separately. + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri(null)).IsFalse(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri("")).IsFalse(); + await Assert.That(AbsoluteHttpUriAttribute.IsAbsoluteHttpUri(" ")).IsFalse(); + } + + [Test] + public async Task AbsoluteHttpUriAttribute_AllowEmpty_PassesForBlank() + { + var attr = new AbsoluteHttpUriAttribute(); + var ctx = new ValidationContext(new object()) { DisplayName = "OtlpEndpoint", MemberName = "OtlpEndpoint" }; + await Assert.That(attr.GetValidationResult("", ctx)).IsEqualTo(ValidationResult.Success); + await Assert.That(attr.GetValidationResult(null, ctx)).IsEqualTo(ValidationResult.Success); + } + + // --------------------------------------------------------------------------------- + // Bootstrapper ↔ API parity — the whole point of the shared-bounds layer. + // Every case tests one out-of-range value against both validators and asserts the + // *same* value fails both. If a future refactor loosens one side, the parity test + // fails and prevents the operator from seeing a green bootstrap gate followed by a + // red API boot. + // --------------------------------------------------------------------------------- + + [Test] + public Task Parity_DbRetryAttemptsAboveMax_FailsBothSides() + { + // Bootstrapper side — ConfigPhase.Validate throws with dbRetryAttempts in the message. + // API side — DataAnnotations validation on PersistenceConfiguration.DbRetryAttempts + // fires the same [Range] check. Same input, same rejection. + var overMax = ConfigurationBounds.DbRetryAttemptsMax + 1; + var apiOpts = MakePersistenceValid(); + apiOpts.DbRetryAttempts = overMax; + return AssertParityRejectionAsync( + bootstrapMutate: c => c.Persistence.DbRetryAttempts = overMax, + bootstrapMessageContains: "dbRetryAttempts", + apiOpts: apiOpts, + apiMemberName: nameof(PersistenceConfiguration.DbRetryAttempts)); + } + + [Test] + public Task Parity_SocketBatchBytesThresholdAboveMax_FailsBothSides() + { + var overMax = ConfigurationBounds.SocketBatchBytesThresholdMax + 1; + return AssertParityRejectionAsync( + bootstrapMutate: c => c.Socket.BatchBytesThreshold = overMax, + bootstrapMessageContains: "batchBytesThreshold", + apiOpts: new SocketConfiguration { BatchBytesThreshold = overMax }); + } + + [Test] + public Task Parity_AbsolutePathRejection_FailsBothSides() + // Bootstrapper side — the config phase rejects a relative avatarStorageRoot. + // API side — StorageConfiguration.AvatarStorageRoot carries [AbsolutePath] which + // trips on the same relative input. + => AssertParityRejectionAsync( + bootstrapMutate: c => c.Storage.AvatarStorageRoot = "relative/avatars", + bootstrapMessageContains: "avatarStorageRoot", + apiOpts: new StorageConfiguration { AvatarStorageRoot = "relative/avatars" }); + + [Test] + public Task Parity_AbsoluteHttpUriRejection_FailsBothSides() + // Bootstrapper side — non-http OtlpEndpoint blows up in ConfigPhase.Validate. + // API side — same input fails [AbsoluteHttpUri] on ObservabilityConfiguration. + => AssertParityRejectionAsync( + bootstrapMutate: c => c.Observability.OtlpEndpoint = "grpc://otel:4317", + bootstrapMessageContains: "otlpEndpoint", + apiOpts: new ObservabilityConfiguration { OtlpEndpoint = "grpc://otel:4317" }); + + /// + /// Two-sided parity assertion for the shared-bounds contract: the same out-of-range value + /// must be rejected by AND by the DataAnnotations + /// validation the API's ValidateOnStart pipeline runs. If one side drifts, an + /// operator sees a green bootstrap gate followed by a red API boot — this helper's four + /// call sites make sure both surfaces stay in lockstep on every bound. Pass + /// when the API-side test additionally pins which member + /// name the failing reports. + /// + private static async Task AssertParityRejectionAsync( + Action bootstrapMutate, + string bootstrapMessageContains, + object apiOpts, + string? apiMemberName = null) + { + var cfg = MakeBootstrapValid(); + bootstrapMutate(cfg); + var bootstrapperEx = Assert.Throws(() => ConfigPhase.Validate(cfg)); + await Assert.That(bootstrapperEx.Message).Contains(bootstrapMessageContains); + + var apiResults = ValidateOptions(apiOpts); + await Assert.That(apiResults.Count).IsGreaterThan(0); + if (apiMemberName is not null) + { + await Assert.That(string.Join(";", apiResults.Select(r => r.MemberNames.FirstOrDefault()))).Contains(apiMemberName); + } + } + + // --------------------------------------------------------------------------------- + // Fixture builders. + // --------------------------------------------------------------------------------- + + /// Default-construct a bootstrap config that already satisfies every invariant. + private static BootstrapConfig MakeBootstrapValid() => new() + { + Deployment = + { + OutputDir = "./deploy", + Hosts = ["api.example.com"], + RootCaName = "Interfold Root CA", + CertYears = 5, + TrustStoreInstall = true, + }, + Ports = + { + ApiHttp = 5000, + ApiHttps = 5001, + WebHttp = 8080, + WebHttps = 8081, + }, + DatabaseMode = DatabaseMode.Single, + }; + + /// Default-construct a that satisfies every attribute. + private static PersistenceConfiguration MakePersistenceValid() => new() + { + DbRetryAttempts = 3, + DbRetryInitialDelay = TimeSpan.FromMilliseconds(100), + DbRetryMaxDelay = TimeSpan.FromMilliseconds(1500), + HydrationMaxConcurrency = 8, + PostgresConnectionString = "Host=localhost;Port=5432;Database=interfold;Username=interfold;Password=interfold", + }; + + /// + /// Runs both the property-level DataAnnotations attributes and the + /// hook — matches the strictness the + /// Microsoft.Extensions.Options pipeline applies when + /// .ValidateDataAnnotations().ValidateOnStart() fires at container boot. + /// + private static List ValidateOptions(object opts) + { + var ctx = new ValidationContext(opts); + var results = new List(); + Validator.TryValidateObject(opts, ctx, results, validateAllProperties: true); + if (opts is IValidatableObject vo) + { + results.AddRange(vo.Validate(ctx)); + } + return results; + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/SystemdUnitRenderingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/SystemdUnitRenderingTests.cs new file mode 100644 index 00000000..3d4f3633 --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/SystemdUnitRenderingTests.cs @@ -0,0 +1,258 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Asserts the token-substitution semantics of . +/// We never shell out to systemd-analyze here — that's the integration test's job. These +/// just pin the rendering contract so a future template tweak doesn't silently drop a +/// substitution. +/// +public sealed class SystemdUnitRenderingTests +{ + /// Default render input used by every test below; tests override one field at a time. + private static SystemdInstallPhase.SystemdRenderInput MakeInput() => new( + OutputDir: "/srv/interfold/deploy", + ComposeFile: "/srv/interfold/deploy/docker-compose.yaml", + ConfigPath: "/srv/interfold/deploy/interfold.bootstrap.json", + BinaryPath: "/opt/interfold/interfold-bootstrap", + OnCalendar: "daily"); + + [Test] + public async Task InterfoldServiceContainsExpectedExecStart() + { + var rendered = SystemdInstallPhase.RenderUnit("interfold.service", MakeInput()); + + // The boot-time autostart is deliberately a direct `docker compose up -d` rather + // than `interfold-bootstrap up` — the bootstrapper's up path waits up to 5 minutes + // on /health/ready, which would block the boot critical path. + await Assert.That(rendered).Contains("ExecStart=/usr/bin/docker compose -f /srv/interfold/deploy/docker-compose.yaml up -d"); + await Assert.That(rendered).Contains("ExecStop=/usr/bin/docker compose -f /srv/interfold/deploy/docker-compose.yaml down"); + await Assert.That(rendered).Contains("WorkingDirectory=/srv/interfold/deploy"); + await Assert.That(rendered).Contains("Type=oneshot"); + await Assert.That(rendered).Contains("RemainAfterExit=yes"); + await Assert.That(rendered).Contains("After=docker.service network-online.target"); + await Assert.That(rendered).Contains("Requires=docker.service"); + await Assert.That(rendered).Contains("WantedBy=multi-user.target"); + } + + [Test] + public async Task InterfoldBackupServiceContainsExpectedExecStart() + { + var rendered = SystemdInstallPhase.RenderUnit("interfold-backup.service", MakeInput()); + + // The backup service runs the bootstrapper's `backup` subcommand and points at the + // operator's resolved config + outputDir; the timer fires it on the schedule + // operators configure via OnCalendar=. + await Assert.That(rendered).Contains( + "ExecStart=/opt/interfold/interfold-bootstrap backup " + + "--config /srv/interfold/deploy/interfold.bootstrap.json " + + "--output-dir /srv/interfold/deploy " + + "--component all"); + await Assert.That(rendered).Contains("Type=oneshot"); + // Backup runs after the server is up — operator-visible documentation says the + // dependency is "soft", so Wants= (not Requires=) is correct. + await Assert.That(rendered).Contains("Wants=interfold.service"); + await Assert.That(rendered).Contains("After=interfold.service"); + } + + [Test] + public async Task InterfoldBackupTimerEmbedsOnCalendar() + { + var input = MakeInput() with { OnCalendar = "Mon..Fri 03:30" }; + var rendered = SystemdInstallPhase.RenderUnit("interfold-backup.timer", input); + + await Assert.That(rendered).Contains("OnCalendar=Mon..Fri 03:30"); + await Assert.That(rendered).Contains("Persistent=true"); + await Assert.That(rendered).Contains("Unit=interfold-backup.service"); + await Assert.That(rendered).Contains("WantedBy=timers.target"); + } + + [Test] + public async Task NoUnsubstitutedTokensRemainInAnyUnit() + { + // The rendered unit text must never contain `{{` (the template token sentinel). + // RenderUnit itself throws when it spots a residual token, so this also serves as + // a smoke test that the renderer's own validation isn't tripping on legitimate text. + var input = MakeInput(); + foreach (var unitName in SystemdInstallPhase.UnitNames) + { + var rendered = SystemdInstallPhase.RenderUnit(unitName, input); + await Assert.That(rendered).DoesNotContain("{{"); + await Assert.That(rendered).DoesNotContain("}}"); + } + } + + [Test] + public async Task RenderUnitThrowsForUnknownUnitName() + { + // Defensive: a typo in SystemdInstallPhase.UnitNames would otherwise produce a + // confusing null-stream error from GetManifestResourceStream. The explicit + // exception message names the bad template so the maintainer sees the typo. + var ex = Assert.Throws( + () => SystemdInstallPhase.RenderUnit("does-not-exist.service", MakeInput())); + await Assert.That(ex.Message).Contains("does-not-exist.service"); + } + + [Test] + public async Task RenderUnitPreservesScheduleSpecialCharacters() + { + // systemd accepts comma-separated lists, colons, asterisks, slashes, and ranges in + // OnCalendar expressions. The renderer must not mangle any of them — the + // BackupSchedulePattern regex in ConfigPhase.Validate permits them all, so the + // rendered text must too. + var input = MakeInput() with { OnCalendar = "*-*-* 02,14:00:00" }; + var rendered = SystemdInstallPhase.RenderUnit("interfold-backup.timer", input); + + await Assert.That(rendered).Contains("OnCalendar=*-*-* 02,14:00:00"); + } + + [Test] + public async Task RenderUnitWorksForAllRegisteredUnitNames() + { + // Smoke: every name listed in UnitNames must have a matching embedded resource so + // the install loop doesn't crash partway through. Catches a future "added a unit + // to UnitNames but forgot to add the template" mistake. + var input = MakeInput(); + foreach (var unitName in SystemdInstallPhase.UnitNames) + { + var rendered = SystemdInstallPhase.RenderUnit(unitName, input); + await Assert.That(rendered.Length).IsGreaterThan(0) + .Because($"template '{unitName}' must render to a non-empty unit body"); + } + } + + [Test] + public async Task InterfoldUpdateServiceContainsExpectedExecStart() + { + // The update service delegates to `interfold-bootstrap update-images` and points + // at the operator's resolved config + outputDir. Chained to backup via the + // OnSuccess= drop-in when config.update.enabled=true; also invocable directly. + var rendered = SystemdInstallPhase.RenderUnit("interfold-update.service", MakeInput()); + + await Assert.That(rendered).Contains( + "ExecStart=/opt/interfold/interfold-bootstrap update-images " + + "--config /srv/interfold/deploy/interfold.bootstrap.json " + + "--output-dir /srv/interfold/deploy"); + await Assert.That(rendered).Contains("Type=oneshot"); + await Assert.That(rendered).Contains("WorkingDirectory=/srv/interfold/deploy"); + // Same "soft" dependency shape as the backup service — the update is a runtime + // operation, not part of the boot critical path. + await Assert.That(rendered).Contains("Wants=interfold.service"); + } + + [Test] + public async Task UpdateServiceIsInUnitNames() + { + // Pin that interfold-update.service is part of the canonical install set so + // SystemdInstallPhase writes it on every install-service invocation. Actually + // enabling / chaining it is a separate decision (config.update.enabled). + await Assert.That(SystemdInstallPhase.UnitNames).Contains("interfold-update.service"); + } + + [Test] + public async Task WhenUpdateEnabledDropInIsWritten() + { + // The drop-in template is copied verbatim (no token substitution), so this test + // exercises the write path directly against a temp unit dir. Confirms the file + // lands at the systemd-conventional path and contains the OnSuccess= directive. + using var scratch = TestSupport.NewScratchDir("interfold-dropin"); + var tmp = scratch.Path; + var logger = new PhaseLogger(TestSupport.MakeOptions( + command: BootstrapCommand.InstallService, + outputDir: tmp, + nonInteractive: true)); + + await SystemdInstallPhase.WriteBackupOnSuccessDropInAsync(tmp, logger, CancellationToken.None); + + var dropInPath = Path.Combine(tmp, + SystemdInstallPhase.BackupOnSuccessDropInDir, + SystemdInstallPhase.BackupOnSuccessDropInFile); + await Assert.That(File.Exists(dropInPath)).IsTrue() + .Because($"drop-in must be written to {dropInPath}"); + var content = await File.ReadAllTextAsync(dropInPath); + await Assert.That(content).Contains("[Unit]"); + await Assert.That(content).Contains("OnSuccess=interfold-update.service"); + } + + [Test] + public async Task RemoveDropInIsNoOpWhenAbsent() + { + // Idempotency: an install-service with update.enabled=false against a host that + // never had the drop-in must not throw. RemoveIfPresent silently no-ops when + // the file (or the directory) is missing. + using var scratch = TestSupport.NewScratchDir("interfold-dropin-abs"); + var tmp = scratch.Path; + var logger = new PhaseLogger(TestSupport.MakeOptions( + command: BootstrapCommand.InstallService, + outputDir: tmp, + nonInteractive: true)); + + SystemdInstallPhase.RemoveBackupOnSuccessDropInIfPresent(tmp, logger); + + var dropInPath = Path.Combine(tmp, + SystemdInstallPhase.BackupOnSuccessDropInDir, + SystemdInstallPhase.BackupOnSuccessDropInFile); + await Assert.That(File.Exists(dropInPath)).IsFalse(); + } + + [Test] + public async Task RemoveDropInDeletesExistingFile() + { + // The other half of the idempotency contract: a previous install wrote the + // drop-in and the operator has since flipped update.enabled=false. On the + // next install-service run we must delete the stale file so a scheduled + // backup no longer fires the update chain. + using var scratch = TestSupport.NewScratchDir("interfold-dropin-del"); + var tmp = scratch.Path; + var dropInDir = Path.Combine(tmp, SystemdInstallPhase.BackupOnSuccessDropInDir); + Directory.CreateDirectory(dropInDir); + var dropInPath = Path.Combine(dropInDir, SystemdInstallPhase.BackupOnSuccessDropInFile); + await File.WriteAllTextAsync(dropInPath, "[Unit]\nOnSuccess=interfold-update.service\n"); + + var logger = new PhaseLogger(TestSupport.MakeOptions( + command: BootstrapCommand.InstallService, + outputDir: tmp, + nonInteractive: true)); + + SystemdInstallPhase.RemoveBackupOnSuccessDropInIfPresent(tmp, logger); + + await Assert.That(File.Exists(dropInPath)).IsFalse(); + } + + [Test] + public async Task ParseSystemdMajorVersionAcceptsUbuntuFormat() + { + // Ubuntu 22.04 output: "systemd 249 (249.11-0ubuntu3.16)". + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("systemd 249 (249.11-0ubuntu3.16)\n+PAM +AUDIT ...")) + .IsEqualTo(249); + } + + [Test] + public async Task ParseSystemdMajorVersionAcceptsFedoraFormat() + { + // Fedora 40 output: "systemd 255 (255.10-1.fc40)". + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("systemd 255 (255.10-1.fc40)")) + .IsEqualTo(255); + } + + [Test] + public async Task ParseSystemdMajorVersionAcceptsDottedToken() + { + // Some builds emit "systemd 253.1"; strip past the first dot. + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("systemd 253.1 (253.1-1)")) + .IsEqualTo(253); + } + + [Test] + public async Task ParseSystemdMajorVersionRejectsMalformed() + { + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("")) + .IsNull(); + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("something completely unrelated")) + .IsNull(); + await Assert.That(SystemdInstallPhase.ParseSystemdMajorVersion("systemd")) + .IsNull(); + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/TestSupport.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/TestSupport.cs new file mode 100644 index 00000000..e006683a --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/TestSupport.cs @@ -0,0 +1,204 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Shared.Contracts.Enums; +using TUnit.Core.Exceptions; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Shared helpers for the unit-test project. +/// +internal static class TestSupport +{ + /// + /// True when the test process is running inside a CI environment. + /// + /// Uses the de-facto standard CI env var (set to true by GitHub Actions, + /// GitLab, CircleCI, Travis, Buildkite, etc). Tests that prefer SKIP over FAIL when a + /// prerequisite artifact is missing should still FAIL in CI — a silent skip there masks + /// a real workflow regression (e.g. a publish/stage step that broke without anyone + /// noticing because the test report still says "0 failed"). + /// + public static bool IsRunningInCi => + string.Equals( + Environment.GetEnvironmentVariable("CI"), + "true", + StringComparison.OrdinalIgnoreCase); + + /// + /// Returns the path to the bootstrapper assembly (so dotnet <path> can launch it + /// without needing to publish first). If the build artifact isn't on disk yet, throws a + /// so the test is reported as skipped rather than failed — + /// this happens when the unit project is run before the bootstrapper has been compiled. + /// + public static string BootstrapperBinaryOrSkip() + { + var path = LocateBootstrapperAssembly(); + if (path is null || !File.Exists(path)) + { + throw new SkipTestException( + "Bootstrapper assembly not found on disk; build Interfold.Bootstrapper first " + + "or run via `dotnet test` from the solution root."); + } + return path; + } + + /// + /// Walks up from the test assembly's location to the repo root (identified by + /// Interfold.slnx) and returns the bootstrapper artifact under + /// tools/Interfold.Bootstrapper/bin/{Debug|Release}/net10.0/. We try both because + /// contributors may have only built one configuration locally and CI builds Release. + /// + public static string? LocateBootstrapperAssembly() + { + var repoRoot = LocateRepoRoot(); + if (repoRoot is null) return null; + + string[] candidates = + [ + Path.Combine(repoRoot, "tools", "Interfold.Bootstrapper", "bin", "Debug", "net10.0", "interfold-bootstrap.dll"), + Path.Combine(repoRoot, "tools", "Interfold.Bootstrapper", "bin", "Release", "net10.0", "interfold-bootstrap.dll"), + ]; + return candidates.FirstOrDefault(File.Exists); + } + + /// + /// Walks up from the test assembly's location until it finds Interfold.slnx. Returns null + /// if the walk reaches the filesystem root without a match. + /// + public static string? LocateRepoRoot() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null) + { + if (dir.GetFiles("Interfold.slnx").Length > 0) + { + return dir.FullName; + } + dir = dir.Parent; + } + return null; + } + /// + /// Builds a for unit tests. Every parameter has a + /// unit-test-friendly default (no host mutation, no prompts, no rotation) so a caller + /// only names the dimensions their test actually cares about. Prefer this over + /// hand-rolling new BootstrapOptions(...) in test files: it stops the record's + /// positional constructor from leaking into every fixture and gives us one place to + /// add new required parameters when the record grows. + /// + /// + /// The default (null) resolves to + /// Path.GetFullPath("./deploy"). Callers that write to disk MUST pass an + /// explicit scratch directory — the shared default is a working-directory-relative + /// path shared across the test process and is not safe for file I/O. + /// + public static BootstrapOptions MakeOptions( + BootstrapCommand command = BootstrapCommand.UpdateImages, + string? outputDir = null, + bool skipPrereqs = false, + bool rotateSecrets = false, + bool rotateCerts = false, + bool nonInteractive = false, + string? faultInject = null, + bool printPhaseStatus = false, + string? backupDirOverride = null, + string[]? updateServices = null, + string? configPath = null, + bool reconfigure = false) + { + return new BootstrapOptions( + Command: command, + ConfigPath: configPath, + OutputDir: outputDir ?? System.IO.Path.GetFullPath("./deploy"), + SkipPrereqs: skipPrereqs, + RotateSecrets: rotateSecrets, + RotateCerts: rotateCerts, + NonInteractive: nonInteractive, + FaultInject: faultInject, + PrintPhaseStatus: printPhaseStatus, + BackupDirOverride: backupDirOverride, + UpdateServices: updateServices, + Reconfigure: reconfigure); + } + + /// + /// Builds a fresh at its property-initialiser defaults with + /// an optional override and an optional post-construction + /// tweak for the handful of fields a specific test cares about. Use this instead of + /// new BootstrapConfig { ... } in test files so we get one place to add scaffolding + /// (e.g. deterministic host / port seeding) when the config record grows. + /// + /// + /// Default is to match + /// 's own property initialiser; the enum + /// has no Scylla member (Single/Multi/Cassandra are the three shipped modes). + /// + public static BootstrapConfig MakeConfig( + DatabaseMode mode = DatabaseMode.Single, + Action? tweak = null) + { + var config = new BootstrapConfig { DatabaseMode = mode }; + tweak?.Invoke(config); + return config; + } + + /// + /// Builds a fresh with explicit deployment values so the + /// derivation inputs are unambiguous, default-shipped ports (5001/8080/8081), and an empty + /// so every test starts from the "needs derivation" state. + /// + public static BootstrapConfig MakeConfigWithEmptyApiRuntime( + bool webHttps = false, + params string[] hosts) + { + var cfg = new BootstrapConfig + { + Deployment = + { + Hosts = hosts.Length > 0 ? [.. hosts] : ["api.example.com"], + WebHttps = webHttps, + }, + }; + cfg.ApiRuntime.CallbackBaseUrl = string.Empty; + cfg.ApiRuntime.JwtAuthority = string.Empty; + cfg.ApiRuntime.CorsAllowedOrigins = []; + return cfg; + } + + /// + /// Represents a temporary scratch directory that is automatically deleted when disposed. + /// + public sealed class ScratchDir : IDisposable + { + public string Path { get; } + + public ScratchDir(string prefix) + { + Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), prefix + "-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(Path); + } + + public void Dispose() + { + TryDeleteDir(Path); + } + } + + public static ScratchDir NewScratchDir(string prefix) => new(prefix); + + public static void TryDeleteDir(string path) + { + try + { + if (Directory.Exists(path)) + { + Directory.Delete(path, recursive: true); + } + } + catch + { + // Best-effort cleanup + } + } +} diff --git a/tests/unit/Interfold.Bootstrapper.UnitTests/UpdateCommandBuildingTests.cs b/tests/unit/Interfold.Bootstrapper.UnitTests/UpdateCommandBuildingTests.cs new file mode 100644 index 00000000..c3d7a3fc --- /dev/null +++ b/tests/unit/Interfold.Bootstrapper.UnitTests/UpdateCommandBuildingTests.cs @@ -0,0 +1,209 @@ +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Phases; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.UnitTests; + +/// +/// Argv-shape assertions for . Mirrors the pattern in +/// — pins the docker compose command layout so +/// a future refactor that swaps flag order or drops a service filter catches the +/// change at unit speed instead of only in DinD integration tests. +/// +public sealed class UpdateCommandBuildingTests +{ + + + [Test] + public async Task ComposePsRequestsJsonFormat() + { + // The digest diff depends on `--format json` — the default text output isn't + // parseable enough to reliably extract per-service image IDs. + var args = UpdateImagesPhase.BuildComposePsJsonArgs( + composeFile: "/srv/deploy/docker-compose.yaml"); + + await Assert.That(args).IsEquivalentTo(new[] + { + "compose", "-f", "/srv/deploy/docker-compose.yaml", "ps", "-a", "--format", "json", + }); + } + + [Test] + public async Task ImageInspectIdArgsPinFormat() + { + var args = UpdateImagesPhase.BuildImageInspectIdArgs("interfold-cassandra:local"); + + await Assert.That(args).IsEquivalentTo(new[] + { + "image", "inspect", "--format", "{{.Id}}", "interfold-cassandra:local", + }); + } + + [Test] + public async Task ContainerImageFieldsArgsInspectAllIds() + { + var args = UpdateImagesPhase.BuildContainerImageFieldsArgs(["abc", "def"]); + + await Assert.That(args).IsEquivalentTo(new[] + { + "inspect", "--format", "{{.Image}}\t{{.Config.Image}}", "abc", "def", + }); + } + + [Test] + public async Task ResolveServiceWhitelistCliBeatsConfig() + { + // CLI --service wins over config.update.services. The operator's ad-hoc override + // is always the more specific intent. + var options = TestSupport.MakeOptions(updateServices: ["interfold-api"]); + var config = new BootstrapConfig { Update = { Services = ["msg-db", "scylla"] } }; + + var resolved = UpdateImagesPhase.ResolveServiceWhitelist(options, config); + + await Assert.That(resolved).IsEquivalentTo(new[] { "interfold-api" }); + } + + [Test] + public async Task ResolveServiceWhitelistFallsBackToConfig() + { + // No CLI → use the persistent config value. + var options = TestSupport.MakeOptions(updateServices: null); + var config = new BootstrapConfig { Update = { Services = ["msg-db"] } }; + + var resolved = UpdateImagesPhase.ResolveServiceWhitelist(options, config); + + await Assert.That(resolved).IsEquivalentTo(new[] { "msg-db" }); + } + + [Test] + public async Task ResolveServiceWhitelistBothEmptyMeansEveryService() + { + // The empty sentinel propagates through — UpdateImagesPhase interprets an empty + // result as "pass no service names to docker compose", which compose reads as + // "every service in the file". + var options = TestSupport.MakeOptions(updateServices: null); + var config = new BootstrapConfig(); + + var resolved = UpdateImagesPhase.ResolveServiceWhitelist(options, config); + + await Assert.That(resolved.Count).IsEqualTo(0); + } + + [Test] + public async Task DiffDigestsReturnsChangedServicesOnly() + { + // Only the services whose image ID changed appear in the diff. Unchanged + // services stay off the recreate list, which is the whole point of the diff. + var before = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["scylla"] = "sha256:bbb", + ["interfold-api"] = "sha256:ccc", + }; + var after = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["scylla"] = "sha256:bbb", + ["interfold-api"] = "sha256:ddd", + }; + + var changed = UpdateImagesPhase.DiffDigests(before, after); + await Assert.That(changed).IsEquivalentTo(new[] { "interfold-api" }); + } + + [Test] + public async Task DiffDigestsHandlesNewService() + { + var before = new Dictionary { ["msg-db"] = "sha256:aaa" }; + var after = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["new-svc"] = "sha256:new", + }; + + var changed = UpdateImagesPhase.DiffDigests(before, after); + await Assert.That(changed).Contains("new-svc"); + } + + [Test] + public async Task DiffDigestsHandlesRemovedService() + { + var before = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["dropped"] = "sha256:x", + }; + var after = new Dictionary { ["msg-db"] = "sha256:aaa" }; + + var changed = UpdateImagesPhase.DiffDigests(before, after); + await Assert.That(changed).Contains("dropped"); + } + + [Test] + public async Task DiffDigestsAllSameReturnsEmpty() + { + // The "no-op" case: every image ID matches. UpdateImagesPhase short-circuits + // here and skips the recreate + health check + downtime. + var before = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["scylla"] = "sha256:bbb", + }; + var after = new Dictionary + { + ["msg-db"] = "sha256:aaa", + ["scylla"] = "sha256:bbb", + }; + + var changed = UpdateImagesPhase.DiffDigests(before, after); + await Assert.That(changed.Count).IsEqualTo(0); + } + + [Test] + public async Task ShouldRebuildCassandraFalseInScyllaMode() + { + // databaseMode=single (Scylla) never triggers a Cassandra rebuild — the local + // interfold-cassandra:local image isn't part of a Scylla-only stack, and any + // whitelist value must be ignored here. Pins the mode gate. + var config = new BootstrapConfig { DatabaseMode = DatabaseMode.Single }; + + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, [])).IsFalse(); + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["cassandra"])).IsFalse(); + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["msg-db", "cassandra"])).IsFalse(); + } + + [Test] + public async Task ShouldRebuildCassandraTrueInCassandraModeWithEmptyWhitelist() + { + // Empty whitelist = "act on every service" (compose semantics propagated by + // ResolveServiceWhitelist), so cassandra is implicitly in scope and must rebuild. + var config = new BootstrapConfig { DatabaseMode = DatabaseMode.Cassandra }; + + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, [])).IsTrue(); + } + + [Test] + public async Task ShouldRebuildCassandraTrueWhenWhitelistNamesCassandra() + { + // Explicit ["cassandra"] whitelist means "just rebuild the DB image" — this is the + // deliberate "I patched the Dockerfile, only re-cook the Cassandra layer" path. + var config = new BootstrapConfig { DatabaseMode = DatabaseMode.Cassandra }; + + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["cassandra"])).IsTrue(); + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["cassandra", "interfold-api"])).IsTrue(); + } + + [Test] + public async Task ShouldRebuildCassandraFalseWhenWhitelistExcludesCassandra() + { + // The whole point of the scoped rebuild: an operator narrowing an update with + // `--service msg-db` must NOT get an unrelated Cassandra rebuild that would + // pay the docker-build cost (and potentially reset the running container's + // Dockerfile-baked customisations) for no reason. + var config = new BootstrapConfig { DatabaseMode = DatabaseMode.Cassandra }; + + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["msg-db"])).IsFalse(); + await Assert.That(UpdateImagesPhase.ShouldRebuildCassandra(config, ["interfold-api", "octocon-web"])).IsFalse(); + } +} + diff --git a/tests/unit/Interfold.Friendships.UnitTests/Friendships/ResolveUserIdDispatchTests.cs b/tests/unit/Interfold.Friendships.UnitTests/Friendships/ResolveUserIdDispatchTests.cs new file mode 100644 index 00000000..ade731df --- /dev/null +++ b/tests/unit/Interfold.Friendships.UnitTests/Friendships/ResolveUserIdDispatchTests.cs @@ -0,0 +1,35 @@ +using Interfold.Friendships.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.InMemory.Repository; + +namespace Interfold.Api.UnitTests.Friendships; + +// FriendLookup-driven dispatch matrix on InMemoryFriendshipRepository.ResolveUserIdAsync. +// Scylla dispatch runs through SendFriendRequestPrefixTests end-to-end. Wire universe is +// narrowed to Id + Username; other shapes fail FriendLookup.TryParse (400 at route bind). +public sealed class ResolveUserIdDispatchTests +{ + [Test] + public async Task FriendLookup_UsernamePrefix_ReturnsNull_NoRegistryHop() + { + var repo = new InMemoryFriendshipRepository(); + + var resolved = await repo.ResolveUserIdAsync(FriendLookup.Parse("username:alice", provider: null)); + + await Assert.That(resolved).IsNull() + .Because("Username lookup has no InMemory reverse index; the correct answer is 'no such user' rather than fabricating a SystemId('username:alice')."); + } + + [Test] + [Arguments("id:abcdefg", "abcdefg")] + [Arguments("abcdefg", "abcdefg")] + public async Task FriendLookup_IdShapes_NormalizeToValue(string input, string expected) + { + var repo = new InMemoryFriendshipRepository(); + + var resolved = await repo.ResolveUserIdAsync(FriendLookup.Parse(input, provider: null)); + + await Assert.That(resolved?.Value).IsEqualTo(expected) + .Because($"'{input}' must normalise to '{expected}' so the resolved id can be used as a storage key for the other InMemory repos (which write with region-stripped keys)."); + } +} diff --git a/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupRepresentsSameUserAsTests.cs b/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupRepresentsSameUserAsTests.cs new file mode 100644 index 00000000..7b8c700b --- /dev/null +++ b/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupRepresentsSameUserAsTests.cs @@ -0,0 +1,37 @@ +using Interfold.Friendships.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins the FriendLookupExtensions.RepresentsSameUserAs extension that backs the +// FriendRequestsController.Send self-request guard — moved off ScopedSystemId when +// FriendLookup relocated to Interfold.Friendships.Contracts. +public sealed class FriendLookupRepresentsSameUserAsTests +{ + private static readonly ScopedSystemId Principal = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "abcdefg"); + + // Bare and id:-prefixed both parse as FriendLookupKind.Id and delegate through the + // SystemId primitive's raw-id branch. + [Test] + [Arguments("abcdefg")] + [Arguments("id:abcdefg")] + public async Task FriendLookup_IdSelf_IsSelf(string input) + { + var candidate = FriendLookup.Parse(input, provider: null); + + await Assert.That(candidate.RepresentsSameUserAs(Principal)).IsTrue() + .Because($"'{input}' parses as FriendLookupKind.Id whose Value equals the principal's RawId; the extension must delegate to the SystemId primitive's raw-id branch and self-reject."); + } + + // Username shape can't self-reject without a registry hit; the downstream + // resolved-id self-check takes over. + [Test] + public async Task FriendLookup_UsernameShape_IsNotSelf() + { + var candidate = FriendLookup.Parse("username:alice", provider: null); + + await Assert.That(candidate.RepresentsSameUserAs(Principal)).IsFalse() + .Because("The controller cannot decide 'is alice me?' without a registry lookup; the fast-path must return false and let the downstream resolved-id self-check take over."); + } +} diff --git a/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupTests.cs b/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupTests.cs new file mode 100644 index 00000000..245f6c81 --- /dev/null +++ b/tests/unit/Interfold.Friendships.UnitTests/Ids/FriendLookupTests.cs @@ -0,0 +1,189 @@ +using Interfold.Friendships.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Routing table FriendLookup.TryParse exposes at the friend-request wire boundary. +// Two-kind (Id / Username) universe; region-scoped ids and Discord snowflakes fail +// TryParse and surface as 400 at ASP.NET route binding. +public sealed class FriendLookupTests +{ + [Test] + public async Task TryParse_UsernamePrefix_KindUsername() + { + var parsed = FriendLookup.TryParse("username:alice", provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(handle.Kind).IsEqualTo(FriendLookupKind.Username); + await Assert.That(handle.Value).IsEqualTo("alice") + .Because("Value exposes the after-colon content — the 'username:' prefix is metadata, not part of the lookup value."); + await Assert.That(handle.OriginalValue).IsEqualTo("username:alice") + .Because("OriginalValue is retained verbatim so JSON serialization and idempotency hashes stay byte-identical to the pre-merge wire form."); + } + } + + [Test] + public async Task TryParse_IdPrefix_KindId_StripsPrefix() + { + var parsed = FriendLookup.TryParse("id:abcdefg", provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(handle.Kind).IsEqualTo(FriendLookupKind.Id) + .Because("Explicit id: prefix is the strict assertion that the value is a system id — same registry column as bare-id but the caller made the intent explicit."); + await Assert.That(handle.Value).IsEqualTo("abcdefg"); + await Assert.That(handle.OriginalValue).IsEqualTo("id:abcdefg"); + } + } + + [Test] + public async Task TryParse_BareId_KindId_ValueIsWholeInput() + { + var parsed = FriendLookup.TryParse("abcdefg", provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(handle.Kind).IsEqualTo(FriendLookupKind.Id); + await Assert.That(handle.Value).IsEqualTo("abcdefg") + .Because("A bare handle has no prefix to strip — Value is the whole input."); + await Assert.That(handle.OriginalValue).IsEqualTo("abcdefg") + .Because("OriginalValue equals Value for bare shapes; the raw wire and after-prefix content are the same string."); + } + } + + [Test] + [Arguments("USERNAME:alice", FriendLookupKind.Username)] + [Arguments("ID:abcdefg", FriendLookupKind.Id)] + [Arguments("Id:abcdefg", FriendLookupKind.Id)] + public async Task TryParse_DiscriminatorPrefix_CaseInsensitive(string input, FriendLookupKind expected) + { + var parsed = FriendLookup.TryParse(input, provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue() + .Because("Discriminator prefixes are normalised case-insensitively — a client that shouts USERNAME must dispatch identically to the lowercase form."); + await Assert.That(handle.Kind).IsEqualTo(expected); + } + } + + [Test] + [Arguments((string?)null)] + [Arguments("")] + [Arguments(" ")] + public async Task TryParse_NullOrBlank_ReturnsFalse(string? input) + { + var parsed = FriendLookup.TryParse(input, provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsFalse(); + await Assert.That(handle).IsEqualTo(default(FriendLookup)); + } + } + + [Test] + [Arguments(":abcdefg")] + [Arguments("username:")] + [Arguments("id:")] + public async Task TryParse_BarePrefixOrEmptyHalf_ReturnsFalse(string input) + { + var parsed = FriendLookup.TryParse(input, provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsFalse() + .Because($"'{input}' has one half empty — never dispatch a partially-formed handle to a registry column."); + await Assert.That(handle).IsEqualTo(default(FriendLookup)); + } + } + + [Test] + [Arguments("xxx:abcdefg")] + [Arguments("phone:0123456789")] + [Arguments("bogus:whatever")] + public async Task TryParse_UnknownPrefix_ReturnsFalse(string input) + { + var parsed = FriendLookup.TryParse(input, provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsFalse() + .Because($"'{input}' has an unknown prefix that is not on the id/username allow-list — the friend-request route must reject rather than silently strip."); + await Assert.That(handle).IsEqualTo(default(FriendLookup)); + } + } + + // Region-scoped and Discord shapes fail FriendLookup.TryParse — surfaced as 400 at + // ASP.NET route binding. Region resolution still accepts them via UserRegistryLookup. + [Test] + [Arguments("nam:abcdefg")] + [Arguments("eur:abcdefg")] + [Arguments("sam:abcdefg")] + [Arguments("sas:abcdefg")] + [Arguments("eas:abcdefg")] + [Arguments("ocn:abcdefg")] + [Arguments("gdpr:abcdefg")] + [Arguments("discord:1234567890")] + public async Task TryParse_RegionOrDiscordShape_ReturnsFalse(string input) + { + var parsed = FriendLookup.TryParse(input, provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsFalse() + .Because($"'{input}' was accepted by the pre-merge four-kind LookupHandle but is out of scope for the friend-request route — it must surface as a 400 at ASP.NET route binding rather than falling through to a repository lane."); + await Assert.That(handle).IsEqualTo(default(FriendLookup)); + } + } + + [Test] + public async Task TryParse_MultipleColons_TreatsFirstColonAsPrefixSeparator() + { + var parsed = FriendLookup.TryParse("username:foo:bar", provider: null, out var handle); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(handle.Kind).IsEqualTo(FriendLookupKind.Username); + await Assert.That(handle.Value).IsEqualTo("foo:bar") + .Because("The parser splits on the first colon only so a username value can carry embedded colons (URI-like handles)."); + await Assert.That(handle.OriginalValue).IsEqualTo("username:foo:bar"); + } + } + + // Parse throws for the same inputs TryParse rejects — used by the JSON converter. + [Test] + [Arguments("")] + [Arguments(" ")] + [Arguments("nam:abcdefg")] + [Arguments("discord:1234567890")] + [Arguments("xxx:abcdefg")] + [Arguments(":abcdefg")] + [Arguments("id:")] + public async Task Parse_InvalidInput_ThrowsFormatException(string input) + { + await Assert.That(() => FriendLookup.Parse(input, provider: null)) + .Throws() + .Because($"Parse is the strict path — '{input}' must throw rather than default so persisted-payload JSON deserialization surfaces the shape mismatch as an exception rather than silently corrupting a hash."); + } + + // Implicit widen to string returns OriginalValue so historical string.IsNullOrWhiteSpace / + // interpolation callers keep their raw-wire shape after the type flip. + [Test] + [Arguments("abcdefg")] + [Arguments("id:abcdefg")] + [Arguments("username:alice")] + public async Task ImplicitWiden_ReturnsOriginalValue(string input) + { + var handle = FriendLookup.Parse(input, provider: null); + + string widened = handle; + + await Assert.That(widened).IsEqualTo(input) + .Because("Implicit widen returns OriginalValue verbatim — anything else would break historical callers that rely on the raw-wire byte-for-byte shape."); + } +} diff --git a/tests/unit/Interfold.Friendships.UnitTests/Interfold.Friendships.UnitTests.csproj b/tests/unit/Interfold.Friendships.UnitTests/Interfold.Friendships.UnitTests.csproj new file mode 100644 index 00000000..b61100d9 --- /dev/null +++ b/tests/unit/Interfold.Friendships.UnitTests/Interfold.Friendships.UnitTests.csproj @@ -0,0 +1,23 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Fronting.UnitTests/InMemoryFrontingRepositoryListAllTests.cs b/tests/unit/Interfold.Fronting.UnitTests/InMemoryFrontingRepositoryListAllTests.cs new file mode 100644 index 00000000..e2db3cb1 --- /dev/null +++ b/tests/unit/Interfold.Fronting.UnitTests/InMemoryFrontingRepositoryListAllTests.cs @@ -0,0 +1,67 @@ +using Interfold.Settings.Domain; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Api.UnitTests.Support; +using Interfold.Infrastructure.InMemory.Repository; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Interfold.Api.UnitTests; + +// Coverage for the unbounded fronts read the export path needs — reference exports +// every front that has a time_start (accounts.ex:1215). The two existing reads on +// IFrontingRepository (ListActiveAsync + ListHistoryBetweenAsync) cannot cover that +// contract: ListActiveAsync drops closed rows, ListHistoryBetweenAsync stitches +// fronts_by_time which is only populated on close (see the CQL migration at +// infrastructure/Interfold.Infrastructure.Scylla/Migrations/002_create_interfold_schema.templated.cql:306-320). +public sealed class InMemoryFrontingRepositoryListAllTests +{ + private static readonly SystemId OwnerId = new("owner01"); + private static readonly AlterId AlterOne = new(1); + private static readonly AlterId AlterTwo = new(2); + + private static InMemoryFrontingRepository BuildRepository() + { + var region = new FixedRegionContext(ScyllaKeyspace.Nam); + var friendships = new InMemoryFriendshipRepository(); + var fields = new InMemorySettingsFieldRepository(region, new ServiceCollection().BuildServiceProvider()); + var alterFieldDefinitions = new AlterFieldDefinitionsAdapter(fields, NullLogger.Instance); + var polls = new InMemoryPollRepository(region); + var alters = new InMemoryAlterRepository(region, friendships, fields, alterFieldDefinitions, polls, NullLogger.Instance); + return new InMemoryFrontingRepository(region, friendships, alters, NullLogger.Instance); + } + + [Test] + public async Task ListAllAsync_ReturnsOpenAndClosedFrontsOrderedByStartDesc() + { + var repo = BuildRepository(); + + var closed = new DateTimeOffset(2024, 4, 1, 0, 0, 0, TimeSpan.Zero); + var open = new DateTimeOffset(2024, 5, 1, 0, 0, 0, TimeSpan.Zero); + + _ = await repo.StartAsync(OwnerId, AlterOne, comment: "closed", startedAt: closed); + await repo.EndAsync(OwnerId, AlterOne, endedAt: closed.AddDays(1)); + _ = await repo.StartAsync(OwnerId, AlterTwo, comment: "open", startedAt: open); + + var all = await repo.ListAllAsync(OwnerId); + + using (Assert.Multiple()) + { + await Assert.That(all.Count).IsEqualTo(2) + .Because("ListAllAsync must surface both open and closed fronts; ListActiveAsync/ListHistoryBetweenAsync individually can't."); + await Assert.That(all[0].TimeStart).IsEqualTo(open) + .Because("Ordered by time_start descending so the export writes newest fronts first."); + await Assert.That(all[0].TimeEnd).IsNull(); + await Assert.That(all[1].TimeStart).IsEqualTo(closed); + await Assert.That(all[1].TimeEnd).IsEqualTo(closed.AddDays(1)); + } + } + + [Test] + public async Task ListAllAsync_EmptyStore_ReturnsEmptyList() + { + var repo = BuildRepository(); + var all = await repo.ListAllAsync(OwnerId); + await Assert.That(all.Count).IsEqualTo(0); + } +} diff --git a/tests/unit/Interfold.Fronting.UnitTests/Interfold.Fronting.UnitTests.csproj b/tests/unit/Interfold.Fronting.UnitTests/Interfold.Fronting.UnitTests.csproj new file mode 100644 index 00000000..ea0d12b4 --- /dev/null +++ b/tests/unit/Interfold.Fronting.UnitTests/Interfold.Fronting.UnitTests.csproj @@ -0,0 +1,31 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/Interfold.IntegrationTests.Shared.UnitTests.csproj b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/Interfold.IntegrationTests.Shared.UnitTests.csproj new file mode 100644 index 00000000..2e44023f --- /dev/null +++ b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/Interfold.IntegrationTests.Shared.UnitTests.csproj @@ -0,0 +1,40 @@ + + + + net10.0 + false + true + enable + enable + Interfold.IntegrationTests.Shared.UnitTests + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchIdleReaperTests.cs b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchIdleReaperTests.cs new file mode 100644 index 00000000..fe6580e8 --- /dev/null +++ b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchIdleReaperTests.cs @@ -0,0 +1,113 @@ +using Interfold.IntegrationTests.Shared.TestServices.TestBench; + +namespace Interfold.IntegrationTests.Shared.UnitTests.TestBench; + +/// Decision-only coverage for . +/// The real docker-rm path is deliberately not exercised here (it would require +/// live containers); we cover every combination of LastAttachUtc and +/// Users that drives the boolean return. +[NotInParallel(nameof(TestBenchIdleReaperTests))] +public sealed class TestBenchIdleReaperTests +{ + private string _scratchBase = null!; + + [Before(Test)] + public void ArrangeScratchLeaseDir() + { + _scratchBase = Path.Combine(Path.GetTempPath(), + "test-bench-reaper-tests-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_scratchBase); + TestBenchLease.TestOnlyBaseDirectoryOverride = _scratchBase; + } + + [After(Test)] + public void CleanupScratchLeaseDir() + { + TestBenchLease.TestOnlyBaseDirectoryOverride = null; + try { Directory.Delete(_scratchBase, recursive: true); } + catch { /* best-effort cleanup */ } + } + + [Test] + public async Task RunIfIdleAsync_FreshLease_DoesNothing() + { + // LastAttachUtc == DateTime.MinValue → nothing to reap yet. + var state = new TestBenchLeaseState(); + var ran = await TestBenchIdleReaper.RunIfIdleAsync(state, CancellationToken.None); + await Assert.That(ran).IsFalse(); + } + + [Test] + public async Task RunIfIdleAsync_WithLiveUsers_DoesNothing() + { + // Live user attached, well past idle timeout — reaper must NOT tear down. + var state = new TestBenchLeaseState + { + LastAttachUtc = DateTime.UtcNow - TimeSpan.FromDays(1), + Users = { TestBenchLease.CurrentUser() }, + }; + var ran = await TestBenchIdleReaper.RunIfIdleAsync(state, CancellationToken.None); + await Assert.That(ran).IsFalse(); + } + + [Test] + public async Task RunIfIdleAsync_RecentAttachEmptyUsers_DoesNothing() + { + // Nobody attached but the bench was touched a moment ago — still within idle grace. + var state = new TestBenchLeaseState + { + LastAttachUtc = DateTime.UtcNow - TimeSpan.FromSeconds(30), + Users = { }, + }; + var ran = await TestBenchIdleReaper.RunIfIdleAsync(state, CancellationToken.None); + await Assert.That(ran).IsFalse(); + } + + [Test] + public async Task ResolveIdleTimeout_DefaultsToFifteenMinutes_WithoutEnv() + { + var previous = Environment.GetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES"); + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", null); + try + { + await Assert.That(TestBenchIdleReaper.ResolveIdleTimeout()) + .IsEqualTo(TestBenchIdleReaper.DefaultIdleTimeout); + } + finally + { + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", previous); + } + } + + [Test] + public async Task ResolveIdleTimeout_PositiveEnv_Overrides() + { + var previous = Environment.GetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES"); + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", "3"); + try + { + await Assert.That(TestBenchIdleReaper.ResolveIdleTimeout()) + .IsEqualTo(TimeSpan.FromMinutes(3)); + } + finally + { + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", previous); + } + } + + [Test] + public async Task ResolveIdleTimeout_MalformedEnv_FallsBackToDefault() + { + var previous = Environment.GetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES"); + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", "not-a-number"); + try + { + await Assert.That(TestBenchIdleReaper.ResolveIdleTimeout()) + .IsEqualTo(TestBenchIdleReaper.DefaultIdleTimeout); + } + finally + { + Environment.SetEnvironmentVariable("INTERFOLD_TEST_BENCH_IDLE_TIMEOUT_MINUTES", previous); + } + } +} diff --git a/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchLeaseTests.cs b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchLeaseTests.cs new file mode 100644 index 00000000..5a6c1fe2 --- /dev/null +++ b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestBench/TestBenchLeaseTests.cs @@ -0,0 +1,171 @@ +using Interfold.IntegrationTests.Shared.TestServices.TestBench; + +namespace Interfold.IntegrationTests.Shared.UnitTests.TestBench; + +/// Filesystem-level unit coverage for . Every test +/// swaps in a per-test scratch directory via +/// and marks itself [NotInParallel] because that override is a static. +[NotInParallel(nameof(TestBenchLeaseTests))] +public sealed class TestBenchLeaseTests +{ + // Scratch base directory reset in Setup so parallel test classes cannot see each other's + // half-written state files. Kept per-instance so an assertion mid-test can still + // diagnose from the folder on failure without racing peer tests. + private string _scratchBase = null!; + + [Before(Test)] + public void ArrangeScratchLeaseDir() + { + _scratchBase = Path.Combine(Path.GetTempPath(), + "test-bench-lease-tests-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_scratchBase); + TestBenchLease.TestOnlyBaseDirectoryOverride = _scratchBase; + } + + [After(Test)] + public void CleanupScratchLeaseDir() + { + TestBenchLease.TestOnlyBaseDirectoryOverride = null; + try { Directory.Delete(_scratchBase, recursive: true); } + catch { /* best-effort cleanup */ } + } + + [Test] + public async Task AcquireAsync_HeldExclusively_UntilDisposed() + { + // A holds the lock. A second AcquireAsync with a short timeout must fail with + // TimeoutException because the file cannot be opened FileShare.None twice. + using (await TestBenchLease.AcquireAsync(TimeSpan.FromSeconds(2), CancellationToken.None)) + { + var thrown = await Assert.ThrowsAsync(async () => + await TestBenchLease.AcquireAsync(TimeSpan.FromMilliseconds(300), CancellationToken.None)); + await Assert.That(thrown!.Message).Contains("test-bench lock"); + } + + // After dispose the lock file must be gone (FileOptions.DeleteOnClose) and a new + // Acquire must succeed within a normal timeout budget. + await Assert.That(File.Exists(TestBenchLease.GetLockFilePath())).IsFalse(); + using var _ = await TestBenchLease.AcquireAsync(TimeSpan.FromSeconds(2), CancellationToken.None); + } + + [Test] + public async Task AcquireAsync_ReleasesAcrossSequentialCallers() + { + // Serialise three acquire/dispose cycles — each must succeed inside the tight + // budget because the previous handle released the lock. + for (var i = 0; i < 3; i++) + { + using var _ = await TestBenchLease.AcquireAsync(TimeSpan.FromSeconds(2), CancellationToken.None); + } + } + + [Test] + public async Task ReadState_MissingFile_ReturnsEmptyLease() + { + // No state file on disk → default lease. This is the fresh-boot case. + var state = TestBenchLease.ReadState(); + using (Assert.Multiple()) + { + await Assert.That(state).IsNotNull(); + await Assert.That(state.Users.Count).IsEqualTo(0); + await Assert.That(state.MigrationsApplied.Count).IsEqualTo(0); + await Assert.That(state.LastAttachUtc).IsEqualTo(DateTime.MinValue); + } + } + + [Test] + public async Task WriteState_ThenReadState_RoundTrips() + { + var user = TestBenchLease.CurrentUser("unit-test"); + var launcher = TestBenchLease.CurrentUser("test-bench-launcher"); + var written = new TestBenchLeaseState + { + LastAttachUtc = new DateTime(2026, 07, 27, 14, 15, 16, DateTimeKind.Utc), + Users = { user }, + MigrationsApplied = { ["postgres"] = true }, + MigrationsInProgress = { ["cql-seed-and-migrate:scylla"] = user }, + Launching = launcher, + }; + TestBenchLease.WriteState(written); + + var read = TestBenchLease.ReadState(); + using (Assert.Multiple()) + { + await Assert.That(read.LastAttachUtc).IsEqualTo(written.LastAttachUtc); + await Assert.That(read.Users.Count).IsEqualTo(1); + await Assert.That(read.Users[0].Pid).IsEqualTo(user.Pid); + await Assert.That(read.Users[0].TestHost).IsEqualTo("unit-test"); + await Assert.That(read.MigrationsApplied["postgres"]).IsTrue(); + await Assert.That(read.MigrationsInProgress["cql-seed-and-migrate:scylla"].Pid).IsEqualTo(user.Pid); + await Assert.That(read.Launching).IsNotNull(); + await Assert.That(read.Launching!.Pid).IsEqualTo(launcher.Pid); + await Assert.That(read.Launching.TestHost).IsEqualTo("test-bench-launcher"); + } + } + + [Test] + public async Task ReadState_MalformedJson_TreatsAsFreshLease() + { + // Torn-write survivors and manual edits shouldn't wedge the coordinator; a garbled + // state file must degrade to a fresh lease so the reaper / next attach can recover. + File.WriteAllText(TestBenchLease.GetStateFilePath(), "{ this-is-not-json ]"); + var state = TestBenchLease.ReadState(); + await Assert.That(state.Users.Count).IsEqualTo(0); + } + + [Test] + public async Task PruneStale_KeepsCurrentProcess_DropsDefunctPid() + { + var alive = TestBenchLease.CurrentUser(); + // A PID we're highly confident does not exist on any target host. We probe just in + // case the OS wrapped: if the recorded creation time doesn't match today's real + // process, IsAlive returns false regardless of PID identity. + var dead = new TestBenchUser(Pid: 999_999_990, + PidCreatedUtc: new DateTime(1999, 1, 1, 0, 0, 0, DateTimeKind.Utc), + TestHost: "dead"); + + var pruned = TestBenchLease.PruneStale(new[] { alive, dead }); + + using (Assert.Multiple()) + { + await Assert.That(pruned.Count).IsEqualTo(1); + await Assert.That(pruned[0].Pid).IsEqualTo(alive.Pid); + } + } + + [Test] + public async Task IsAlive_CurrentProcess_ReturnsTrue() + { + var current = TestBenchLease.CurrentUser(); + await Assert.That(TestBenchLease.IsAlive(current)).IsTrue(); + } + + [Test] + public async Task IsAlive_ImpossiblePid_ReturnsFalse() + { + var fake = new TestBenchUser(Pid: 999_999_990, + PidCreatedUtc: DateTime.UtcNow, + TestHost: "fake"); + await Assert.That(TestBenchLease.IsAlive(fake)).IsFalse(); + } + + [Test] + public async Task IsAlive_CorrectPidWrongCreationTime_ReturnsFalse() + { + // Simulates PID wraparound: right number, wrong provenance. The creation-time + // cross-check must reject this so a reused PID cannot masquerade as an old attach. + var currentPid = System.Diagnostics.Process.GetCurrentProcess().Id; + var mimic = new TestBenchUser(Pid: currentPid, + PidCreatedUtc: new DateTime(1999, 1, 1, 0, 0, 0, DateTimeKind.Utc), + TestHost: "mimic"); + await Assert.That(TestBenchLease.IsAlive(mimic)).IsFalse(); + } + + [Test] + public void DeleteState_TolerantOfMissingFile() + { + // Second delete must not throw — reaper calls this unconditionally after teardown. + TestBenchLease.DeleteState(); + TestBenchLease.DeleteState(); + } +} diff --git a/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/DockerDaemonAvailabilityTests.cs b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/DockerDaemonAvailabilityTests.cs new file mode 100644 index 00000000..01ac2b9a --- /dev/null +++ b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/DockerDaemonAvailabilityTests.cs @@ -0,0 +1,17 @@ +using Interfold.IntegrationTests.Shared.TestServices; + +namespace Interfold.IntegrationTests.Shared.UnitTests.TestServices; + +public sealed class DockerDaemonAvailabilityTests +{ + [Test] + [Arguments("failed to connect to the docker API at npipe:////./pipe/dockerDesktopLinuxEngine; check if the path is correct and if the daemon is running: open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified.", true)] + [Arguments("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?", true)] + [Arguments("error during connect: Get \"http://%2F%2F.%2Fpipe%2FdockerDesktopLinuxEngine/v1.45/info\": open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified.", true)] + [Arguments("[host-aio] raising current=65536 to 166562 for 1 Scylla node(s)\nsh: can't create /proc/sys/fs/aio-max-nr: Permission denied", false)] + [Arguments("", false)] + public async Task LooksUnavailable_ClassifiesKnownDaemonErrors(string text, bool expected) + { + await Assert.That(DockerDaemonAvailability.LooksUnavailable(text)).IsEqualTo(expected); + } +} diff --git a/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/ScyllaRackDcMountPathsTests.cs b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/ScyllaRackDcMountPathsTests.cs new file mode 100644 index 00000000..cba67c72 --- /dev/null +++ b/tests/unit/Interfold.IntegrationTests.Shared.UnitTests/TestServices/ScyllaRackDcMountPathsTests.cs @@ -0,0 +1,49 @@ +using Interfold.IntegrationTests.Shared.TestServices; + +namespace Interfold.IntegrationTests.Shared.UnitTests.TestServices; + +[NotInParallel(nameof(ScyllaRackDcMountPathsTests))] +public sealed class ScyllaRackDcMountPathsTests +{ + private string _scratch = null!; + + [Before(Test)] + public void ArrangeScratch() + { + _scratch = Path.Combine(Path.GetTempPath(), "rackdc-mount-tests-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_scratch); + Directory.CreateDirectory(Path.Combine(_scratch, "db", "scylla")); + foreach (var region in new[] { "nam", "eur", "sam", "sas", "eas", "ocn", "gdpr" }) + { + File.WriteAllText( + Path.Combine(_scratch, "db", "scylla", $"cassandra-rackdc.{region}.properties"), + $"dc={region}\nrack=rack1\n"); + } + } + + [After(Test)] + public void CleanupScratch() + { + try { Directory.Delete(_scratch, recursive: true); } + catch { /* best-effort */ } + } + + [Test] + public void VerifyAllPresent_WhenComplete_DoesNotThrow() + { + ScyllaRackDcMountPaths.VerifyAllPresent(_scratch); + } + + [Test] + public async Task VerifyAllPresent_WhenRegionMissing_ThrowsWithPath() + { + File.Delete(Path.Combine(_scratch, "db", "scylla", "cassandra-rackdc.gdpr.properties")); + + var ex = await Assert.ThrowsAsync( + () => Task.Run(() => ScyllaRackDcMountPaths.VerifyAllPresent(_scratch))); + + await Assert.That(ex!.Message).Contains("cassandra-rackdc.gdpr.properties"); + await Assert.That(ex.Message).Contains("GossipingPropertyFileSnitch"); + await Assert.That(ex.Message).Contains("do not fall back to SimpleSnitch"); + } +} diff --git a/tests/unit/Interfold.Journals.UnitTests/Interfold.Journals.UnitTests.csproj b/tests/unit/Interfold.Journals.UnitTests/Interfold.Journals.UnitTests.csproj new file mode 100644 index 00000000..19a1b950 --- /dev/null +++ b/tests/unit/Interfold.Journals.UnitTests/Interfold.Journals.UnitTests.csproj @@ -0,0 +1,28 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Ops.UnitTests/Interfold.Ops.UnitTests.csproj b/tests/unit/Interfold.Ops.UnitTests/Interfold.Ops.UnitTests.csproj new file mode 100644 index 00000000..71a287d8 --- /dev/null +++ b/tests/unit/Interfold.Ops.UnitTests/Interfold.Ops.UnitTests.csproj @@ -0,0 +1,26 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Polls.UnitTests/Domain/PollCommandFlowExecutionTests.cs b/tests/unit/Interfold.Polls.UnitTests/Domain/PollCommandFlowExecutionTests.cs new file mode 100644 index 00000000..cfb619d5 --- /dev/null +++ b/tests/unit/Interfold.Polls.UnitTests/Domain/PollCommandFlowExecutionTests.cs @@ -0,0 +1,218 @@ +using Interfold.Polls.Contracts.Ids; +using Interfold.Polls.Contracts.Models.Commands; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Api.UnitTests.Domain; + +// Regression harness for PollCommandFlow.ExecuteExistingPollMutationAsync. Guards +// against a prior eager-Task footgun where UpdateAsync/DeleteAsync and PublishAsync +// fired against a not-found row before ExistsAsync completed — invisible to HTTP-level +// tests but observable in a spurious write + published event. Helper now takes +// Func; these tests count calls to catch a re-regression at the seam. +public sealed class PollCommandFlowExecutionTests +{ + private static readonly ScopedSystemId Principal = + ScopedSystemId.Compose(ScyllaKeyspace.Nam, "poll-flow-tests"); + + private static readonly PollId AnyPollId = new(Guid.Parse("11111111-2222-3333-4444-555555555555")); + + [Test] + public async Task DeletePoll_MissingPoll_DoesNotMutateOrPublish() + { + var repo = new CountingPollRepository { Exists = false }; + var bus = new CountingEventBus(); + var handler = new DeletePollCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(OperationIds.PollDelete, new DeletePollCommand(AnyPollId)); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse() + .Because("A missing poll must be rejected with poll:not_found, not accepted."); + await Assert.That(result.Conflict?.EntityRef).IsEqualTo(EntityRefs.PollNotFound) + .Because("The rejection must be the not-found invariant, not a mutation-failed variant."); + await Assert.That(repo.DeleteCalls).IsEqualTo(0) + .Because("A not-found poll must never reach DeleteAsync — the eager-Task footgun would raise this count."); + await Assert.That(bus.PublishCalls).IsEqualTo(0) + .Because("A not-found poll must never publish PollDeletedEvent — the eager-Task footgun would raise this count."); + await Assert.That(repo.ExistsCalls).IsEqualTo(1) + .Because("The existence check itself must run exactly once so the reject path is provable."); + } + } + + [Test] + public async Task DeletePoll_ExistsButMutationReturnsFalse_RejectsWithDeleteFailedAndDoesNotPublish() + { + var repo = new CountingPollRepository { Exists = true, DeleteResult = false }; + var bus = new CountingEventBus(); + var handler = new DeletePollCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(OperationIds.PollDelete, new DeletePollCommand(AnyPollId)); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse(); + await Assert.That(result.Conflict?.EntityRef).IsEqualTo(EntityRefs.PollDeleteFailed); + await Assert.That(repo.DeleteCalls).IsEqualTo(1); + await Assert.That(bus.PublishCalls).IsEqualTo(0) + .Because("A mutation that reports no change must not surface a Deleted event."); + } + } + + [Test] + public async Task DeletePoll_HappyPath_CallsMutateOncePublishesOnceAndAccepts() + { + var repo = new CountingPollRepository { Exists = true, DeleteResult = true }; + var bus = new CountingEventBus(); + var handler = new DeletePollCommandHandler(repo, new NoopIdempotencyStore(), bus); + + var command = NewEnvelope(OperationIds.PollDelete, new DeletePollCommand(AnyPollId)); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsTrue(); + await Assert.That(repo.DeleteCalls).IsEqualTo(1); + await Assert.That(bus.PublishCalls).IsEqualTo(1); + } + } + + // Update shares the helper with Delete so it inherits the same footgun; pinned + // separately so drift in one branch cannot silently pass in the other. + [Test] + public async Task UpdatePoll_MissingPoll_DoesNotMutateOrPublish() + { + var repo = new CountingPollRepository { Exists = false }; + var bus = new CountingEventBus(); + var handler = new UpdatePollCommandHandler(repo, new NoopIdempotencyStore(), bus); + + // Populate one mutable field so the handler doesn't short-circuit at + // PollCommandValidation.HasNoMutableFields. + var payload = new UpdatePollCommand( + Id: AnyPollId, + Title: "renamed", + Description: null, + TimeEnd: null, + HasTimeEnd: false, + Data: null); + var command = NewEnvelope(OperationIds.PollUpdate, payload); + + var result = await handler.HandleAsync(command); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse(); + await Assert.That(result.Conflict?.EntityRef).IsEqualTo(EntityRefs.PollNotFound); + await Assert.That(repo.UpdateCalls).IsEqualTo(0) + .Because("A not-found poll must never reach UpdateAsync — the eager-Task footgun would raise this count."); + await Assert.That(bus.PublishCalls).IsEqualTo(0) + .Because("A not-found poll must never publish PollUpdatedEvent — the eager-Task footgun would raise this count."); + } + } + + private static CommandEnvelope NewEnvelope(OperationId opId, T payload) => + new( + OperationId: opId, + CommandId: Guid.NewGuid(), + PrincipalId: Principal, + IdempotencyKey: new IdempotencyKey(Guid.NewGuid().ToString("N")), + OccurredAt: DateTimeOffset.UtcNow, + Payload: payload); + + private sealed class CountingPollRepository : IPollRepository + { + public bool Exists { get; set; } + public bool DeleteResult { get; set; } + public bool UpdateResult { get; set; } + public int ExistsCalls; + public int DeleteCalls; + public int UpdateCalls; + + public Task> ListAsync(SystemId systemId, CancellationToken cancellationToken = default) + => Task.FromResult>(Array.Empty()); + + public Task GetAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + => Task.FromResult(null); + + public Task CreateAsync(SystemId systemId, CreatePollCommand command, CancellationToken cancellationToken = default) + => Task.FromResult(null); + + public Task ExistsAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref ExistsCalls); + return Task.FromResult(Exists); + } + + public Task UpdateAsync(SystemId systemId, UpdatePollCommand command, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref UpdateCalls); + return Task.FromResult(UpdateResult); + } + + public Task DeleteAsync(SystemId systemId, PollId pollId, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref DeleteCalls); + return Task.FromResult(DeleteResult); + } + + public Task RemoveAlterFromPollsAsync(SystemId systemId, AlterId alterId, CancellationToken cancellationToken = default) + => Task.CompletedTask; + } + + private sealed class CountingEventBus : IClusterEventBus + { + public int PublishCalls; + + public ValueTask PublishAsync(TEvent evt, CancellationToken ct = default) where TEvent : class + { + Interlocked.Increment(ref PublishCalls); + return ValueTask.CompletedTask; + } + + public IAsyncEnumerable SubscribeAsync(ScopedSystemId? targetSystemId, CancellationToken ct = default) where TEvent : class + => AsyncEnumerable.Empty(); + } + + private sealed class NoopIdempotencyStore : IIdempotencyStore + { + public Task FindAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + CancellationToken cancellationToken = default) => Task.FromResult(null); + + public Task SaveAsync( + SystemId principalId, + OperationId operationId, + IdempotencyKey idempotencyKey, + string payloadHash, + string outcomeHash, + string? outcomePayload, + CancellationToken cancellationToken = default) => Task.CompletedTask; + } + + private static class AsyncEnumerable + { + public static IAsyncEnumerable Empty() => new EmptyAsyncEnumerable(); + + private sealed class EmptyAsyncEnumerable : IAsyncEnumerable, IAsyncEnumerator + { + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) => this; + public T Current => default!; + public ValueTask MoveNextAsync() => ValueTask.FromResult(false); + public ValueTask DisposeAsync() => ValueTask.CompletedTask; + } + } +} diff --git a/tests/unit/Interfold.Polls.UnitTests/Interfold.Polls.UnitTests.csproj b/tests/unit/Interfold.Polls.UnitTests/Interfold.Polls.UnitTests.csproj new file mode 100644 index 00000000..0672537e --- /dev/null +++ b/tests/unit/Interfold.Polls.UnitTests/Interfold.Polls.UnitTests.csproj @@ -0,0 +1,24 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Settings.UnitTests/Export/ExportFixtures.cs b/tests/unit/Interfold.Settings.UnitTests/Export/ExportFixtures.cs new file mode 100644 index 00000000..4c264963 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Export/ExportFixtures.cs @@ -0,0 +1,211 @@ +using System.Text.Json; +using Interfold.Fronting.Contracts.Ids; +using Interfold.Polls.Contracts.Ids; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Tags.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Export; + +// Hand-authored fixture inputs and their pinned expected JSON, mirroring +// `Octocon.Accounts.format_pk_export` / `format_full_export` from +// octocon/lib/octocon/accounts.ex:1273-1415 against the same inputs. +// Kept close to the reference so a diff review only has to compare two JSON blobs. +internal static class ExportFixtures +{ + public static readonly SystemId OwnerId = new("owner01"); + + public static readonly string PkDescription1200 = + string.Concat(System.Linq.Enumerable.Repeat("abcdefghij", 120)); + + public static readonly string DescriptionTruncatedTo1000 = PkDescription1200[..1000]; + + public static readonly AvatarUrl AlterOneAvatar = new("https://cdn.example/1.png"); + public static readonly HexColor AlterOneColor = new("#abcdef"); + public static readonly AlterId AlterOneId = new(1); + public static readonly AlterId AlterTwoId = new(2); + public static readonly FieldId FieldOneId = new(System.Guid.Parse("11111111-1111-1111-1111-111111111111")); + public static readonly TagId TagRootId = new(System.Guid.Parse("22222222-2222-2222-2222-222222222222")); + public static readonly TagId TagChildId = new(System.Guid.Parse("33333333-3333-3333-3333-333333333333")); + public static readonly PollId PollOneId = new(System.Guid.Parse("44444444-4444-4444-4444-444444444444")); + public static readonly FrontId FrontOpenId = new(System.Guid.Parse("55555555-5555-5555-5555-555555555555")); + public static readonly FrontId FrontClosedId = new(System.Guid.Parse("66666666-6666-6666-6666-666666666666")); + + public static readonly DateTime FixedInsertedAt = new(2024, 1, 2, 3, 4, 5, DateTimeKind.Utc); + public static readonly DateTime FixedUpdatedAt = new(2024, 6, 7, 8, 9, 10, DateTimeKind.Utc); + public static readonly DateTimeOffset FrontOpenStart = new(2024, 5, 1, 0, 0, 0, TimeSpan.Zero); + public static readonly DateTimeOffset FrontClosedStart = new(2024, 4, 1, 0, 0, 0, TimeSpan.Zero); + public static readonly DateTimeOffset FrontClosedEnd = new(2024, 4, 2, 0, 0, 0, TimeSpan.Zero); + + public static AccountPublicProfileReadModel BuildProfile(string? description = null) => + new( + OwnerId, + new Username("ownername"), + description ?? PkDescription1200, + new AvatarUrl("https://cdn.example/owner.png"), + AvatarSource.External, + DiscordId: null, + Email: null, + AppleId: null); + + // pk assertion is compared via JsonNode.DeepEquals so ordering / whitespace can't + // trip us — but the *keys* and *values* still have to be identical. The literal + // below mirrors format_pk_export against the fixture above. + public static string ExpectedPkJson() + { + var desc1000 = DescriptionTruncatedTo1000; + return $$""" + { + "version": 2, + "name": "ownername", + "description": "{{desc1000}}", + "avatar_url": "https://cdn.example/owner.png", + "switches": [], + "members": [ + { + "id": "1", + "name": "Alpha", + "pronouns": "they/them", + "description": "{{desc1000}}", + "color": "abcdef", + "avatar_url": "https://cdn.example/1.png", + "proxy_tags": [{ "prefix": "prefix;", "suffix": " suffix" }], + "display_name": "AlphaProxy" + }, + { + "id": "2", + "name": "Beta", + "pronouns": "", + "description": "", + "color": null, + "avatar_url": null, + "proxy_tags": [], + "display_name": null + } + ], + "groups": [ + { + "id": "0", + "name": "RootTag", + "description": "root description", + "color": "112233", + "members": ["1", "2"] + }, + { + "id": "1", + "name": "ChildTag", + "description": null, + "color": null, + "members": ["1"] + } + ] + } + """; + } + + public static string ExpectedFullJson() + { + // Full export echoes raw values without pk-side truncation. `time_start` / + // `time_end` land as ISO-8601 with the DateTimeOffset's `+00:00`; DateTime(Kind=Utc) + // stamps as `...Z` — STJ's built-in DateTime/DateTimeOffset writers do this + // unconditionally, no custom converter needed. + return """ + { + "user": { + "username": "ownername", + "description": "%DESC%", + "id": "owner01", + "avatar_url": "https://cdn.example/owner.png", + "fields": [ + { "id": "11111111-1111-1111-1111-111111111111", "name": "Field1", "type": "text", "locked": false, "security_level": "public" } + ] + }, + "alters": [ + { + "id": 1, + "name": "Alpha", + "pronouns": "they/them", + "description": "%DESC%", + "color": "#abcdef", + "avatar_url": "https://cdn.example/1.png", + "proxy_name": "AlphaProxy", + "discord_proxies": ["prefix;text suffix"], + "fields": [ + { "id": "11111111-1111-1111-1111-111111111111", "value": "the-value" } + ] + }, + { + "id": 2, + "name": "Beta", + "pronouns": null, + "description": null, + "color": null, + "avatar_url": null, + "proxy_name": null, + "discord_proxies": [], + "fields": [] + } + ], + "fronts": [ + { + "id": "55555555-5555-5555-5555-555555555555", + "alter_id": 1, + "comment": "open front", + "time_start": "2024-05-01T00:00:00+00:00", + "time_end": null + }, + { + "id": "66666666-6666-6666-6666-666666666666", + "alter_id": 2, + "comment": "closed front", + "time_start": "2024-04-01T00:00:00+00:00", + "time_end": "2024-04-02T00:00:00+00:00" + } + ], + "tags": [ + { + "id": "22222222-2222-2222-2222-222222222222", + "name": "RootTag", + "description": "root description", + "color": "#112233", + "security_level": "public", + "parent_tag_id": null, + "inserted_at": "2024-01-02T03:04:05Z", + "updated_at": "2024-06-07T08:09:10Z", + "alters": [1, 2] + }, + { + "id": "33333333-3333-3333-3333-333333333333", + "name": "ChildTag", + "description": null, + "color": null, + "security_level": "public", + "parent_tag_id": "22222222-2222-2222-2222-222222222222", + "inserted_at": "2024-01-02T03:04:05Z", + "updated_at": "2024-06-07T08:09:10Z", + "alters": [1] + } + ], + "polls": [ + { + "id": "44444444-4444-4444-4444-444444444444", + "title": "Poll Title", + "description": "poll description", + "type": "vote", + "data": {"foo":"bar"}, + "time_end": "2024-12-31T23:59:59Z", + "inserted_at": "2024-01-02T03:04:05Z", + "updated_at": "2024-06-07T08:09:10Z" + } + ] + } + """.Replace("%DESC%", PkDescription1200); + } + + public static JsonElement ParseData(string json) + { + using var doc = JsonDocument.Parse(json); + return doc.RootElement.Clone(); + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/Export/ExportServiceTests.cs b/tests/unit/Interfold.Settings.UnitTests/Export/ExportServiceTests.cs new file mode 100644 index 00000000..af6eae89 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Export/ExportServiceTests.cs @@ -0,0 +1,346 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using Interfold.Alters.Contracts.Models; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Fronting.Contracts.Models.Read; +using Interfold.Fronting.Domain.Abstractions.Repository; +using Interfold.Polls.Contracts.Models.Read; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Api.Services.Export; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Models.Read; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Tags.Contracts.Models.Read; +using Interfold.Tags.Domain.Abstractions.Repository; + +namespace Interfold.Api.UnitTests.Export; + +// Full ExportService coverage — asserts both the pk and full JSON shapes match the +// pinned fixtures in ExportFixtures byte-for-byte (via JsonNode structural equality +// so whitespace / key ordering can't cause spurious flakes), plus the byte-truncation +// / proxy-tag split / color-strip / null-color / orphan-front-drop branches. +public sealed class ExportServiceTests +{ + // Strict mocks: any repository method the service reaches for other than the six + // configured below throws MockStrictBehaviorException, so an accidental new + // dependency surfaces immediately instead of a silently-empty read. + private static ExportService BuildService( + AccountPublicProfileReadModel? profile, + IReadOnlyList alters, + IReadOnlyList tags, + IReadOnlyList polls, + IReadOnlyList fronts, + IReadOnlyList fields) + { + var accounts = IAccountRepository.Mock(MockBehavior.Strict); + accounts.GetPublicProfileAsync(ExportFixtures.OwnerId, Any()).Returns(profile); + + var altersMock = IAlterRepository.Mock(MockBehavior.Strict); + altersMock.ListAsync(ExportFixtures.OwnerId, Any()).Returns(alters); + + var tagsMock = ITagRepository.Mock(MockBehavior.Strict); + tagsMock.ListAsync(ExportFixtures.OwnerId, Any()).Returns(tags); + + var pollsMock = IPollRepository.Mock(MockBehavior.Strict); + pollsMock.ListAsync(ExportFixtures.OwnerId, Any()).Returns(polls); + + var frontsMock = IFrontingRepository.Mock(MockBehavior.Strict); + frontsMock.ListAllAsync(ExportFixtures.OwnerId, Any()).Returns(fronts); + + var fieldsMock = ISettingsFieldRepository.Mock(MockBehavior.Strict); + fieldsMock.ListAsync(ExportFixtures.OwnerId, Any()).Returns(fields); + + return new ExportService( + accounts.Object, + altersMock.Object, + tagsMock.Object, + pollsMock.Object, + frontsMock.Object, + fieldsMock.Object); + } + + private static IReadOnlyList BuildAlters() + { + var alpha = new AlterReadModel( + ExportFixtures.AlterOneId, + "Alpha", + ExportFixtures.PkDescription1200, + ExportFixtures.AlterOneAvatar, + AvatarSource.External, + ExportFixtures.AlterOneColor, + "they/them", + VisibilityLevel.Public, + new[] { new AlterPublicFieldReadModel(ExportFixtures.FieldOneId, "Field1", FieldType.Text, "the-value") }, + "AlphaProxy", + alias: null, + untracked: false, + archived: false, + pinned: false, + discordProxies: new[] { "prefix;text suffix" }, + insertedAt: ExportFixtures.FixedInsertedAt, + updatedAt: ExportFixtures.FixedUpdatedAt); + + var beta = new AlterReadModel( + ExportFixtures.AlterTwoId, + "Beta", + description: null, + avatarUrl: null, + avatarSource: null, + color: null, + pronouns: null, + securityLevel: VisibilityLevel.Public, + fields: System.Array.Empty(), + proxyName: null, + alias: null, + untracked: false, + archived: false, + pinned: false, + discordProxies: System.Array.Empty(), + insertedAt: ExportFixtures.FixedInsertedAt, + updatedAt: ExportFixtures.FixedUpdatedAt); + + return new[] { alpha, beta }; + } + + private static IReadOnlyList BuildTags() => new[] + { + new TagReadModel( + ExportFixtures.TagRootId, + "RootTag", + new HexColor("#112233"), + "root description", + ParentTagId: null, + Alters: new[] { ExportFixtures.AlterOneId, ExportFixtures.AlterTwoId }, + InsertedAt: ExportFixtures.FixedInsertedAt, + UpdatedAt: ExportFixtures.FixedUpdatedAt, + SecurityLevel: VisibilityLevel.Public, + UserId: ExportFixtures.OwnerId), + new TagReadModel( + ExportFixtures.TagChildId, + "ChildTag", + Color: null, + Description: null, + ParentTagId: ExportFixtures.TagRootId, + Alters: new[] { ExportFixtures.AlterOneId }, + InsertedAt: ExportFixtures.FixedInsertedAt, + UpdatedAt: ExportFixtures.FixedUpdatedAt, + SecurityLevel: VisibilityLevel.Public, + UserId: ExportFixtures.OwnerId), + }; + + private static IReadOnlyList BuildFronts() => new[] + { + new FrontHistoryReadModel( + ExportFixtures.FrontOpenId, + ExportFixtures.AlterOneId, + "open front", + ExportFixtures.FrontOpenStart, + TimeEnd: null, + UserId: ExportFixtures.OwnerId), + new FrontHistoryReadModel( + ExportFixtures.FrontClosedId, + ExportFixtures.AlterTwoId, + "closed front", + ExportFixtures.FrontClosedStart, + TimeEnd: ExportFixtures.FrontClosedEnd, + UserId: ExportFixtures.OwnerId), + }; + + private static IReadOnlyList BuildPolls() + { + using var doc = JsonDocument.Parse("""{"foo":"bar"}"""); + return new[] + { + new PollReadModel( + ExportFixtures.PollOneId, + ExportFixtures.OwnerId, + "Poll Title", + "poll description", + PollType.Vote, + doc.RootElement.Clone(), + TimeEnd: new DateTime(2024, 12, 31, 23, 59, 59, DateTimeKind.Utc), + InsertedAt: ExportFixtures.FixedInsertedAt, + UpdatedAt: ExportFixtures.FixedUpdatedAt) + }; + } + + private static IReadOnlyList BuildFields() => new[] + { + new SettingsFieldReadModel( + ExportFixtures.FieldOneId, + "Field1", + FieldType.Text, + VisibilityLevel.Public, + Locked: false, + Index: 0, + InsertedAt: ExportFixtures.FixedInsertedAt), + }; + + [Test] + public async Task BuildPkAsync_MatchesReferenceShape() + { + var service = BuildService( + ExportFixtures.BuildProfile(), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildPkAsync(ExportFixtures.OwnerId, CancellationToken.None); + var actual = JsonNode.Parse(JsonSerializer.SerializeToUtf8Bytes(payload, ExportJsonOptions.Default)) + ?? throw new InvalidOperationException("pk payload serialised to null root."); + var expected = JsonNode.Parse(ExportFixtures.ExpectedPkJson()) + ?? throw new InvalidOperationException("pk fixture parsed to null root."); + + await Assert.That(JsonNode.DeepEquals(actual, expected)).IsTrue() + .Because($"pk payload did not match fixture.\n expected: {expected.ToJsonString()}\n actual: {actual.ToJsonString()}"); + } + + [Test] + public async Task BuildFullAsync_MatchesReferenceShape() + { + var service = BuildService( + ExportFixtures.BuildProfile(), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildFullAsync(ExportFixtures.OwnerId, CancellationToken.None); + var actual = JsonNode.Parse(JsonSerializer.SerializeToUtf8Bytes(payload, ExportJsonOptions.Default)) + ?? throw new InvalidOperationException("full payload serialised to null root."); + var expected = JsonNode.Parse(ExportFixtures.ExpectedFullJson()) + ?? throw new InvalidOperationException("full fixture parsed to null root."); + + await Assert.That(JsonNode.DeepEquals(actual, expected)).IsTrue() + .Because($"full payload did not match fixture.\n expected: {expected.ToJsonString()}\n actual: {actual.ToJsonString()}"); + } + + // Reference's `format_full_export` drops fronts where alter_id or time_start are missing + // (accounts.ex:1215). Our contract encodes both as non-nullable on FrontHistoryReadModel, + // but ListAllAsync's *impl* must drop rows with a null time_start (Scylla can hand back a + // corrupt row), so ExportService only sees populated ones. Documenting the shape here + // via a two-element fixture — the "drop" branch is exercised by the fronting repo tests. + [Test] + public async Task BuildFullAsync_EmitsAllFrontsReturnedByRepository() + { + var service = BuildService( + ExportFixtures.BuildProfile(description: "desc"), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildFullAsync(ExportFixtures.OwnerId, CancellationToken.None); + + await Assert.That(payload.Fronts.Count).IsEqualTo(2); + await Assert.That(payload.Fronts[0].Id).IsEqualTo(ExportFixtures.FrontOpenId); + await Assert.That(payload.Fronts[1].Id).IsEqualTo(ExportFixtures.FrontClosedId); + } + + [Test] + public async Task BuildPkAsync_TruncatesDescriptionsToByteCap() + { + var service = BuildService( + ExportFixtures.BuildProfile(), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildPkAsync(ExportFixtures.OwnerId, CancellationToken.None); + + using (Assert.Multiple()) + { + await Assert.That(Encoding.UTF8.GetByteCount(payload.Description!)).IsLessThanOrEqualTo(1000); + await Assert.That(payload.Description).IsEqualTo(ExportFixtures.DescriptionTruncatedTo1000); + await Assert.That(Encoding.UTF8.GetByteCount(payload.Members[0].Description!)).IsLessThanOrEqualTo(1000); + await Assert.That(payload.Members[0].Description).IsEqualTo(ExportFixtures.DescriptionTruncatedTo1000); + } + } + + [Test] + public async Task BuildPkAsync_SplitsDiscordProxiesOnTextSentinel() + { + var service = BuildService( + ExportFixtures.BuildProfile(description: "d"), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildPkAsync(ExportFixtures.OwnerId, CancellationToken.None); + + var alpha = payload.Members[0]; + await Assert.That(alpha.ProxyTags.Count).IsEqualTo(1); + await Assert.That(alpha.ProxyTags[0].Prefix).IsEqualTo("prefix;"); + await Assert.That(alpha.ProxyTags[0].Suffix).IsEqualTo(" suffix"); + } + + [Test] + public async Task BuildPkAsync_AssignsSequentialStringIdsToGroups() + { + var service = BuildService( + ExportFixtures.BuildProfile(description: "d"), + BuildAlters(), + BuildTags(), + BuildPolls(), + BuildFronts(), + BuildFields()); + + var payload = await service.BuildPkAsync(ExportFixtures.OwnerId, CancellationToken.None); + + await Assert.That(payload.Groups.Count).IsEqualTo(2); + await Assert.That(payload.Groups[0].Id).IsEqualTo("0"); + await Assert.That(payload.Groups[1].Id).IsEqualTo("1"); + } + + [Test] + public async Task FormatImportColor_HandlesReferenceEdgeCases() + { + using (Assert.Multiple()) + { + await Assert.That(ExportHelpers.FormatImportColor(null)).IsNull(); + await Assert.That(ExportHelpers.FormatImportColor(new HexColor("#abcdef"))).IsEqualTo("abcdef"); + await Assert.That(ExportHelpers.FormatImportColor(new HexColor("#abc"))).IsNull(); + await Assert.That(ExportHelpers.FormatImportColor(new HexColor("#abcdef12"))).IsNull(); + } + } + + [Test] + public async Task ByteTruncate_ClipsAtByteBoundaryWithoutSplittingCodepoint() + { + // 🎃 is 4 UTF-8 bytes. "a🎃" is 5 bytes. Truncating to 3 bytes must drop the 🎃 entirely + // rather than emit a lone continuation byte. + var truncated = ExportHelpers.ByteTruncateUtf8("a🎃", maxBytes: 3); + + using (Assert.Multiple()) + { + await Assert.That(truncated).IsEqualTo("a"); + await Assert.That(Encoding.UTF8.GetByteCount(truncated)).IsLessThanOrEqualTo(3); + } + } + + [Test] + public async Task ByteTruncate_ReturnsInputWhenAlreadyWithinLimit() + { + var truncated = ExportHelpers.ByteTruncateUtf8("hello", maxBytes: 100); + await Assert.That(truncated).IsEqualTo("hello"); + } + + [Test] + public async Task ByteTruncate_HandlesNullAsEmpty() + { + var truncated = ExportHelpers.ByteTruncateUtf8(null, maxBytes: 10); + await Assert.That(truncated).IsEqualTo(string.Empty); + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/Helpers/FirebaseConfigResolverTests.cs b/tests/unit/Interfold.Settings.UnitTests/Helpers/FirebaseConfigResolverTests.cs new file mode 100644 index 00000000..188e77bd --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Helpers/FirebaseConfigResolverTests.cs @@ -0,0 +1,207 @@ +using Interfold.Shared.Contracts; +using System.Net; +using System.Text.Json; +using Interfold.Settings.Api.Helpers; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Contracts.Models.Wire; +using Microsoft.AspNetCore.Http; + +namespace Interfold.Api.UnitTests.Helpers; + +// FirebaseConfigResolver: three response shapes, two error cases, cache-header stamping, +// and ETag stability — the wire contract mobile / wasm clients depend on. +public sealed class FirebaseConfigResolverTests +{ + private static FirebaseClientConfiguration FullyPopulated() => new() + { + Android = new FirebaseAndroidClientConfig( + ApiKey: "AIzaTEST-Android", + ApplicationId: "1:1234567890:android:abcdef", + ProjectId: "octocon-test", + GcmSenderId: "1234567890", + StorageBucket: "octocon-test.appspot.com"), + Ios = new FirebaseIosClientConfig( + ApiKey: "AIzaTEST-Ios", + GoogleAppId: "1:1234567890:ios:fedcba", + GcmSenderId: "1234567890", + ProjectId: "octocon-test", + StorageBucket: "octocon-test.appspot.com", + BundleId: "app.octocon.ios", + ClientId: "1234567890-abc.apps.googleusercontent.com"), + Web = new FirebaseWebClientConfig( + ApiKey: "AIzaTEST-Web", + AuthDomain: "octocon-test.firebaseapp.com", + ProjectId: "octocon-test", + StorageBucket: "octocon-test.appspot.com", + MessagingSenderId: "1234567890", + AppId: "1:1234567890:web:123abc", + VapidKey: "BOx-test-vapid-key"), + }; + + [Test] + public async Task Resolve_Android_HappyPath_ProducesAndroidResponseWithMatchingFields() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), "android"); + + await Assert.That(error).IsNull(); + await Assert.That(payload).IsTypeOf(); + var android = (FirebaseAndroidConfigResponse)payload!; + await Assert.That(android.ApiKey).IsEqualTo("AIzaTEST-Android"); + await Assert.That(android.ApplicationId).IsEqualTo("1:1234567890:android:abcdef"); + await Assert.That(android.ProjectId).IsEqualTo("octocon-test"); + await Assert.That(android.GcmSenderId).IsEqualTo("1234567890"); + await Assert.That(android.StorageBucket).IsEqualTo("octocon-test.appspot.com"); + } + + [Test] + public async Task Resolve_Ios_HappyPath_ProducesIosResponseWithMatchingFields() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), "ios"); + + await Assert.That(error).IsNull(); + await Assert.That(payload).IsTypeOf(); + var ios = (FirebaseIosConfigResponse)payload!; + await Assert.That(ios.ApiKey).IsEqualTo("AIzaTEST-Ios"); + await Assert.That(ios.GoogleAppId).IsEqualTo("1:1234567890:ios:fedcba"); + await Assert.That(ios.BundleId).IsEqualTo("app.octocon.ios"); + await Assert.That(ios.ClientId).IsEqualTo("1234567890-abc.apps.googleusercontent.com"); + } + + [Test] + public async Task Resolve_Web_HappyPath_ProducesWebResponseWithMatchingFields() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), "web"); + + await Assert.That(error).IsNull(); + await Assert.That(payload).IsTypeOf(); + var web = (FirebaseWebConfigResponse)payload!; + await Assert.That(web.VapidKey).IsEqualTo("BOx-test-vapid-key") + .Because("The VAPID key is the one field unique to the web variant — must round-trip so getToken({ vapidKey }) works on the client."); + await Assert.That(web.MessagingSenderId).IsEqualTo("1234567890"); + } + + [Test] + public async Task Resolve_PlatformCasingIsNormalised() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), " ANDROID "); + await Assert.That(error).IsNull(); + await Assert.That(payload).IsTypeOf(); + } + + [Test] + public async Task Resolve_MissingPlatformConfig_Returns503WithUnavailableCode() + { + var partial = new FirebaseClientConfiguration + { + Android = FullyPopulated().Android, + }; + var (payload, error) = FirebaseConfigResolver.Resolve(partial, "ios"); + + await Assert.That(payload).IsNull(); + await Assert.That(error).IsNotNull(); + await Assert.That(error!.Code).IsEqualTo(ErrorCodes.FirebaseConfigUnavailable); + await Assert.That(error.StatusCode).IsEqualTo(HttpStatusCode.ServiceUnavailable); + } + + [Test] + public async Task Resolve_UnknownPlatform_Returns400WithInvalidPlatformCode() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), "windows"); + + await Assert.That(payload).IsNull(); + await Assert.That(error).IsNotNull(); + await Assert.That(error!.Code).IsEqualTo(ErrorCodes.InvalidPlatform); + await Assert.That(error.StatusCode).IsEqualTo(HttpStatusCode.BadRequest); + } + + [Test] + public async Task Resolve_NullPlatform_Returns400WithInvalidPlatformCode() + { + var (payload, error) = FirebaseConfigResolver.Resolve(FullyPopulated(), null); + + await Assert.That(payload).IsNull(); + await Assert.That(error).IsNotNull(); + await Assert.That(error!.Code).IsEqualTo(ErrorCodes.InvalidPlatform); + } + + [Test] + public async Task ApplyCacheHeaders_SetsFiveMinuteMaxAge() + { + var response = BuildResponse(); + var payload = (FirebaseClientConfigResponse)new FirebaseAndroidConfigResponse( + "k", "a", "p", "s", null); + + FirebaseConfigResolver.ApplyCacheHeaders(response, payload); + + await Assert.That(response.Headers["Cache-Control"].ToString()) + .IsEqualTo("public, max-age=300, must-revalidate") + .Because("Server-side ceiling of 5 minutes bounds how quickly a Firebase rotation propagates through the wasm service worker's install-time fetch."); + } + + [Test] + public async Task ApplyCacheHeaders_EtagIsQuotedHex() + { + var response = BuildResponse(); + var payload = (FirebaseClientConfigResponse)new FirebaseAndroidConfigResponse( + "k", "a", "p", "s", null); + + FirebaseConfigResolver.ApplyCacheHeaders(response, payload); + var etag = response.Headers.ETag.ToString(); + + await Assert.That(etag).StartsWith("\""); + await Assert.That(etag).EndsWith("\""); + await Assert.That(etag.Trim('"').Length).IsEqualTo(64) + .Because("SHA-256 uppercase hex is 64 characters — matches Convert.ToHexString on a 32-byte digest."); + } + + [Test] + public async Task ApplyCacheHeaders_IdenticalPayloadsProduceIdenticalEtags() + { + var payloadA = (FirebaseClientConfigResponse)new FirebaseWebConfigResponse( + "k", "d", "p", "s", "sender", "app", "vapid"); + var payloadB = (FirebaseClientConfigResponse)new FirebaseWebConfigResponse( + "k", "d", "p", "s", "sender", "app", "vapid"); + + var responseA = BuildResponse(); + var responseB = BuildResponse(); + FirebaseConfigResolver.ApplyCacheHeaders(responseA, payloadA); + FirebaseConfigResolver.ApplyCacheHeaders(responseB, payloadB); + + await Assert.That(responseA.Headers.ETag.ToString()) + .IsEqualTo(responseB.Headers.ETag.ToString()); + } + + [Test] + public async Task ApplyCacheHeaders_DifferentPayloadsProduceDifferentEtags() + { + var payloadA = (FirebaseClientConfigResponse)new FirebaseAndroidConfigResponse( + "k1", "a", "p", "s", null); + var payloadB = (FirebaseClientConfigResponse)new FirebaseAndroidConfigResponse( + "k2", "a", "p", "s", null); + + var responseA = BuildResponse(); + var responseB = BuildResponse(); + FirebaseConfigResolver.ApplyCacheHeaders(responseA, payloadA); + FirebaseConfigResolver.ApplyCacheHeaders(responseB, payloadB); + + await Assert.That(responseA.Headers.ETag.ToString()) + .IsNotEqualTo(responseB.Headers.ETag.ToString()); + } + + // Regression guard on the discriminator + field-name policy — a flipped naming + // policy or removed [JsonPolymorphic] annotation would break the kotlinx decoder. + [Test] + public async Task WireContract_UsesSnakeCaseFieldNames() + { + var payload = (FirebaseClientConfigResponse)new FirebaseWebConfigResponse( + "aaa", "bbb.example.com", "ccc", null, "ddd", "eee", "fff"); + var json = JsonSerializer.Serialize(payload, FirebaseConfigResolver.EtagJsonOptions); + + await Assert.That(json).Contains("\"type\":\"web\""); + await Assert.That(json).Contains("\"api_key\":\"aaa\""); + await Assert.That(json).Contains("\"auth_domain\":\"bbb.example.com\""); + await Assert.That(json).Contains("\"vapid_key\":\"fff\""); + } + + private static HttpResponse BuildResponse() => new DefaultHttpContext().Response; +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/CapturingQueue.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/CapturingQueue.cs new file mode 100644 index 00000000..4f5cf7e2 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/CapturingQueue.cs @@ -0,0 +1,31 @@ +using Interfold.Infrastructure.Coordination; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// Decorator over InProcessImportJobQueue that captures every enqueued item so tests +// can assert on it without re-implementing the channel semantics. +internal sealed class CapturingQueue : IImportJobQueue, IAsyncDisposable +{ + private readonly InProcessImportJobQueue _inner; + private readonly List _enqueued = new(); + private readonly object _gate = new(); + + public CapturingQueue(InProcessImportJobQueue inner) { _inner = inner; } + + public IReadOnlyList Enqueued + { + get { lock (_gate) return _enqueued.ToArray(); } + } + + public ValueTask EnqueueAsync(ImportJobItem item, CancellationToken cancellationToken = default) + { + lock (_gate) _enqueued.Add(item); + return _inner.EnqueueAsync(item, cancellationToken); + } + + public IAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default) + => _inner.ReadAllAsync(cancellationToken); + + public ValueTask DisposeAsync() => _inner.DisposeAsync(); +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportDispatchScenario.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportDispatchScenario.cs new file mode 100644 index 00000000..2e57f82a --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportDispatchScenario.cs @@ -0,0 +1,79 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Contracts.Models; +using Interfold.Shared.Contracts.Operations; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Coordination; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Settings.Contracts; +using Interfold.Settings.Contracts.Models.Commands; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Interfold.Settings.Domain.Settings; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// Shared harness for the SP / PK ICommandHandler dispatch tests. Owns the wiring +// (in-memory repo + in-process queue + CapturingQueue) so per-kind test bodies only +// call DispatchAsync and assert. +internal sealed class ImportDispatchScenario : IAsyncDisposable + where TCommand : notnull +{ + private readonly Func> _envelopeFactory; + + public ImportDispatchScenario( + Func> handlerFactory, + Func> envelopeFactory) + { + var repo = new InMemoryImportOperationRepository(); + Queue = new InProcessImportJobQueue(); + Capture = new CapturingQueue(Queue); + Handler = handlerFactory(repo, Capture); + _envelopeFactory = envelopeFactory; + } + + public InProcessImportJobQueue Queue { get; } + public CapturingQueue Capture { get; } + public ICommandHandler Handler { get; } + + public Task> DispatchAsync( + string idempotencyKey = "idem-1", + string token = "synthetic-token") + => Handler.HandleAsync(_envelopeFactory(idempotencyKey, token)); + + public ValueTask DisposeAsync() => Queue.DisposeAsync(); +} + +internal static class ImportDispatchScenario +{ + public static readonly ScopedSystemId SpSystemId + = ScopedSystemId.ParseScoped("nam:sys-sp-dispatch-test"); + + public static readonly ScopedSystemId PkSystemId + = ScopedSystemId.ParseScoped("nam:sys-pk-dispatch-test"); + + public static ImportDispatchScenario ForSp() + => new( + (repo, queue) => new ImportSpCommandHandler(repo, queue), + (key, token) => NewSpEnvelope(SpSystemId, key, token)); + + public static ImportDispatchScenario ForPk() + => new( + (repo, queue) => new ImportPkCommandHandler(repo, queue), + (key, token) => NewPkEnvelope(PkSystemId, key, token)); + + public static CommandEnvelope NewSpEnvelope(ScopedSystemId systemId, string idempotencyKey, string token) => new( + OperationId: new("settings:import_sp"), + CommandId: Guid.NewGuid(), + PrincipalId: systemId, + IdempotencyKey: new(idempotencyKey), + OccurredAt: DateTimeOffset.UtcNow, + Payload: new ImportSpCommand(new(token), RecoveryCode: null)); + + public static CommandEnvelope NewPkEnvelope(ScopedSystemId systemId, string idempotencyKey, string token) => new( + OperationId: new("settings:import_pk"), + CommandId: Guid.NewGuid(), + PrincipalId: systemId, + IdempotencyKey: new(idempotencyKey), + OccurredAt: DateTimeOffset.UtcNow, + Payload: new ImportPkCommand(new(token))); +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportJobBackgroundServiceTests.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportJobBackgroundServiceTests.cs new file mode 100644 index 00000000..3274ef9f --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportJobBackgroundServiceTests.cs @@ -0,0 +1,239 @@ +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Coordination; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Settings.Api.Services.ImportJobs; +using Interfold.Settings.Contracts.Events; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; +using Interfold.Settings.Domain.Abstractions.Repository; +using Microsoft.Extensions.Logging.Abstractions; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// Lifecycle contract for ImportJobBackgroundService — the only path between a queued +// import job and a terminal repo row + WebSocket lifecycle frame. Wire format for those +// frames is separately pinned by WebSocketTests. +public sealed class ImportJobBackgroundServiceTests +{ + private static readonly ScopedSystemId TestSystemId = ScopedSystemId.ParseScoped("nam:sys-worker-test"); + + [Test] + public async Task RunAsync_RunnerSucceeds_MarksSucceededAndPublishesCompleteEvent() + { + await using var harness = await Harness.RunAsync(new StubRunner(succeed: true, alterCount: 7)); + + var snapshot = await harness.GetOperationAsync(); + using (Assert.Multiple()) + { + await Assert.That(snapshot!.Status).IsEqualTo(ImportOperationStatus.Succeeded) + .Because("A successful runner outcome must produce a terminal Succeeded row; otherwise the per-system slot stays locked and re-imports become impossible."); + await Assert.That(snapshot.AlterCount).IsEqualTo(7) + .Because("The alter count from the runner must be persisted verbatim — the WebSocket frame reads it from the row publish via the worker's MarkSucceeded call."); + await Assert.That(harness.EventBus.Published).Contains(e => e is SimplyPluralImportCompletedEvent c && c.AlterCount == 7) + .Because("Exactly one SimplyPluralImportCompletedEvent must be published with the reported alter count; that frame drives the client's Success transition."); + await Assert.That(harness.EventBus.Published).Contains(e => e is SettingsProfileUpdatedEvent) + .Because("SP imports also fire a SettingsProfileUpdated event so dependent client views (encryption status pill, etc.) refresh."); + } + } + + [Test] + public async Task RunAsync_RunnerReportsGracefulFailure_MarksFailedAndPublishesFailedEvent() + { + await using var harness = await Harness.RunAsync(new StubRunner(succeed: false, errorCode: ImportErrorCode.SpAuthFailed)); + + var snapshot = await harness.GetOperationAsync(); + using (Assert.Multiple()) + { + await Assert.That(snapshot!.Status).IsEqualTo(ImportOperationStatus.Failed) + .Because("A graceful (Success=false) outcome must still terminate the row in Failed so the LWT slot frees."); + await Assert.That(snapshot.ErrorCode).IsEqualTo(ImportErrorCode.SpAuthFailed) + .Because("Runner-supplied error codes must round-trip to the row so operators can grep the failure category."); + await Assert.That(harness.EventBus.Published).Contains(e => e is SimplyPluralImportFailedEvent) + .Because("A failed import must publish SimplyPluralImportFailedEvent so the client flips out of Importing."); + } + } + + // Thrown exceptions MUST be swallowed so the loop survives; treated identically to + // a graceful failure with error_code = exception. + [Test] + public async Task RunAsync_RunnerThrows_MarksFailedWithExceptionCodeAndPublishesFailedEvent() + { + await using var harness = await Harness.RunAsync(new StubRunner(throws: new InvalidOperationException("boom"))); + + var snapshot = await harness.GetOperationAsync(); + using (Assert.Multiple()) + { + await Assert.That(snapshot!.Status).IsEqualTo(ImportOperationStatus.Failed) + .Because("A thrown exception must NOT leave the row pinned at Running — that would deadlock the per-system slot until the next host restart sweep."); + await Assert.That(snapshot.ErrorCode).IsEqualTo(ImportErrorCode.Exception) + .Because("Thrown exceptions are categorised as 'exception' so the audit trail distinguishes them from runner-reported graceful failures."); + await Assert.That(harness.EventBus.Published).Contains(e => e is SimplyPluralImportFailedEvent) + .Because("Even on a thrown exception, the client must receive a failure frame — otherwise the dialog stays on Importing forever."); + } + } + + [Test] + public async Task RunAsync_TerminalTransition_ReleasesSlotForNextClaim() + { + await using var harness = await Harness.RunAsync(new StubRunner(succeed: true, alterCount: 3)); + + var second = await harness.Operations.TryClaimAsync(TestSystemId, ImportOperationKind.SimplyPlural, new("idem-2")); + await Assert.That(second.IsNew).IsTrue() + .Because("After the worker terminates an operation, a second click for the same system must claim a fresh slot — otherwise users could never re-import after a successful or failed run."); + } + + // DI-misregistration guard: an unregistered kind must not pin the slot. + [Test] + public async Task RunAsync_UnknownKind_MarksFailedWithNoRunnerCode() + { + await using var harness = await Harness.RunAsync( + runner: new StubRunner(succeed: true), + jobKindOverride: ImportOperationKind.PluralKit); + + var snapshot = await harness.GetOperationAsync(); + await Assert.That(snapshot!.Status).IsEqualTo(ImportOperationStatus.Failed) + .Because("If no runner is registered for the requested kind the worker must fail the row rather than leave it queued indefinitely."); + await Assert.That(snapshot.ErrorCode).IsEqualTo(ImportErrorCode.NoRunner) + .Because("The 'no_runner' code is the agreed signal for a DI misregistration; operators grep on it to alert on missing platform integrations."); + } + + private sealed class Harness : IAsyncDisposable + { + public required InProcessImportJobQueue Queue { get; init; } + public required IImportOperationRepository Operations { get; init; } + public required CapturingEventBus EventBus { get; init; } + public required ImportJobBackgroundService Worker { get; init; } + public required ImportOperationId OperationId { get; init; } + public required CancellationTokenSource Cts { get; init; } + + public Task GetOperationAsync() => + Operations.GetByIdAsync(TestSystemId, OperationId); + + public static async Task RunAsync(StubRunner runner, ImportOperationKind? jobKindOverride = null) + { + var queue = new InProcessImportJobQueue(); + var operations = new InMemoryImportOperationRepository(); + var bus = new CapturingEventBus(); + var jobKind = jobKindOverride ?? ImportOperationKind.SimplyPlural; + + var claim = await operations.TryClaimAsync(TestSystemId, ImportOperationKind.SimplyPlural, new("idem-1")); + var item = new ImportJobItem(claim.OperationId, TestSystemId, jobKind, Token: new("synthetic"), RecoveryCode: null); + + var cts = new CancellationTokenSource(); + var worker = new ImportJobBackgroundService( + queue, + operations, + bus, + runners: new IImportJobRunner[] { runner }, + NullLogger.Instance); + + await worker.StartAsync(cts.Token); + await queue.EnqueueAsync(item, cts.Token); + + // Unknown-kind path short-circuits without calling RunAsync, so skip the + // runner gate for those and rely on terminal polling alone. + if (jobKindOverride is null) + { + await runner.Observed.Task.WaitAsync(TimeSpan.FromSeconds(5)); + } + + await WaitForTerminalAsync(operations, claim.OperationId); + + return new Harness + { + Queue = queue, + Operations = operations, + EventBus = bus, + Worker = worker, + OperationId = claim.OperationId, + Cts = cts, + }; + } + + // Poll (rather than fixed delay) so slow-CI / saturated thread pools still get the + // terminal snapshot; throw on the hard cap so a hung worker fails visibly instead of + // letting the next claim silently observe a stuck slot (IsNew=false). + private static async Task WaitForTerminalAsync(IImportOperationRepository operations, ImportOperationId operationId) + { + var deadline = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(15); + while (DateTimeOffset.UtcNow < deadline) + { + var snapshot = await operations.GetByIdAsync(TestSystemId, operationId); + if (snapshot?.Status is ImportOperationStatus.Succeeded or ImportOperationStatus.Failed) + { + return; + } + await Task.Delay(25); + } + + throw new TimeoutException( + $"Import operation {operationId} did not reach a terminal status within 15s."); + } + + public async ValueTask DisposeAsync() + { + Cts.Cancel(); + try { await Worker.StopAsync(CancellationToken.None); } + catch (OperationCanceledException) { } + await Queue.DisposeAsync(); + Cts.Dispose(); + } + } + + private sealed class StubRunner : IImportJobRunner + { + private readonly bool _succeed; + private readonly int _alterCount; + private readonly ImportErrorCode? _errorCode; + private readonly Exception? _throws; + + public TaskCompletionSource Observed { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + + public StubRunner(bool succeed = true, int alterCount = 0, ImportErrorCode? errorCode = null, Exception? throws = null) + { + _succeed = succeed; + _alterCount = alterCount; + _errorCode = errorCode; + _throws = throws; + } + + public ImportOperationKind Kind => ImportOperationKind.SimplyPlural; + + public Task RunAsync(ImportJobItem item, CancellationToken cancellationToken = default) + { + Observed.TrySetResult(); + if (_throws is not null) throw _throws; + return Task.FromResult(_succeed + ? new ImportJobOutcome(Success: true, AlterCount: _alterCount) + : new ImportJobOutcome(Success: false, AlterCount: 0, ErrorCode: _errorCode)); + } + } + + private sealed class CapturingEventBus : IClusterEventBus + { + private readonly List _published = new(); + private readonly object _gate = new(); + + public IReadOnlyList Published + { + get { lock (_gate) return _published.ToArray(); } + } + + public ValueTask PublishAsync(TEvent evt, CancellationToken ct = default) where TEvent : class + { + lock (_gate) _published.Add(evt); + return ValueTask.CompletedTask; + } + + public IAsyncEnumerable SubscribeAsync(ScopedSystemId? targetSystemId, CancellationToken ct = default) + where TEvent : class => EmptyAsync(); + + private static async IAsyncEnumerable EmptyAsync() + { + await Task.CompletedTask; + yield break; + } + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportPkCommandHandlerDispatchTests.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportPkCommandHandlerDispatchTests.cs new file mode 100644 index 00000000..a9e90f69 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportPkCommandHandlerDispatchTests.cs @@ -0,0 +1,91 @@ +using Interfold.Infrastructure.Coordination; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Settings; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// PK dispatch is the stub-importer twin of SP; re-asserting the shape here keeps a +// regression in either handler loud. Per-kind extras (recovery-code contract, cross-kind +// concurrency) stay inline; shared skeleton is ImportDispatchScenario. +public sealed class ImportPkCommandHandlerDispatchTests +{ + [Test] + public async Task HandleAsync_FreshDispatch_ReturnsQueuedAndEnqueuesOnePkItem() + { + await using var scenario = ImportDispatchScenario.ForPk(); + + var result = await scenario.DispatchAsync(token: "synthetic-pk-token"); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsTrue(); + await Assert.That(result.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Queued); + await Assert.That(result.Result!.Kind).IsEqualTo(ImportOperationKind.PluralKit) + .Because("PK dispatches must surface the 'pk' kind so the worker routes to the PK runner — wiring it to 'sp' would silently run SP code against a PK token."); + await Assert.That(scenario.Capture.Enqueued).HasCount(1); + await Assert.That(scenario.Capture.Enqueued[0].RecoveryCode).IsNull() + .Because("PK has no recovery-code concept; carrying one through would be a contract leak from the SP path."); + } + } + + [Test] + public async Task HandleAsync_ConcurrentPkDispatch_CollapsesOntoSameOperation() + { + await using var scenario = ImportDispatchScenario.ForPk(); + + var first = await scenario.DispatchAsync("idem-1", token: "synthetic-pk-token"); + var second = await scenario.DispatchAsync("idem-2", token: "synthetic-pk-token"); + + using (Assert.Multiple()) + { + await Assert.That(second.Result!.OperationId).IsEqualTo(first.Result!.OperationId) + .Because("Repeated PK dispatches for the same system must collapse onto the in-flight op — same guarantee the SP handler gives."); + await Assert.That(second.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Running); + await Assert.That(scenario.Capture.Enqueued).HasCount(1) + .Because("Only one PK worker run per active claim — duplicate enqueues would re-introduce the bug class on the PK side once the real importer lands."); + } + } + + // LWT mutex partitions on (system, kind) — SP and PK for the same system run + // independently. Inline (not on the shared scenario) because it needs two handlers. + [Test] + public async Task HandleAsync_SpAndPk_SameSystem_BothClaimDistinctSlots() + { + var repo = new InMemoryImportOperationRepository(); + var queue = new InProcessImportJobQueue(); + var capture = new CapturingQueue(queue); + var sp = new ImportSpCommandHandler(repo, capture); + var pk = new ImportPkCommandHandler(repo, capture); + + var spResult = await sp.HandleAsync( + ImportDispatchScenario.NewSpEnvelope(ImportDispatchScenario.PkSystemId, "idem-sp", "synthetic-sp-token")); + var pkResult = await pk.HandleAsync( + ImportDispatchScenario.NewPkEnvelope(ImportDispatchScenario.PkSystemId, "idem-pk", "synthetic-pk-token")); + + using (Assert.Multiple()) + { + await Assert.That(spResult.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Queued); + await Assert.That(pkResult.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Queued) + .Because("A PK dispatch must not be blocked by an in-flight SP dispatch for the same system — the mutex is per (system, kind)."); + await Assert.That(spResult.Result!.OperationId).IsNotEqualTo(pkResult.Result!.OperationId) + .Because("Distinct kinds must get distinct operation_ids; aliasing them would corrupt the audit trail."); + await Assert.That(capture.Enqueued).HasCount(2) + .Because("Both dispatches must produce their own worker run — one per kind."); + } + + await queue.DisposeAsync(); + } + + [Test] + public async Task HandleAsync_EmptyToken_RejectsWithoutClaimingOrEnqueuing() + { + await using var scenario = ImportDispatchScenario.ForPk(); + + var result = await scenario.DispatchAsync(token: string.Empty); + + await Assert.That(result.Accepted).IsFalse() + .Because("PK dispatches with no token are invalid input — the handler must reject before claiming a slot."); + await Assert.That(scenario.Capture.Enqueued).IsEmpty(); + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportSpCommandHandlerDispatchTests.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportSpCommandHandlerDispatchTests.cs new file mode 100644 index 00000000..141f06df --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/ImportSpCommandHandlerDispatchTests.cs @@ -0,0 +1,69 @@ +using Interfold.Settings.Contracts.Models.ImportOperations; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// Async-dispatch contract for ImportSpCommandHandler. Guards against the duplicate-import +// bug class: fresh dispatch enqueues once; concurrent dispatch collapses to Running with +// the same operation_id; empty token rejects without claiming a slot. Shared skeleton is +// ImportDispatchScenario so regressions fail both the SP and PK files at once. +public sealed class ImportSpCommandHandlerDispatchTests +{ + [Test] + public async Task HandleAsync_FreshDispatch_ReturnsQueuedAndEnqueuesOneItem() + { + await using var scenario = ImportDispatchScenario.ForSp(); + + var result = await scenario.DispatchAsync(); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsTrue() + .Because("A valid token must accept; otherwise the controller would never reach the 202 branch."); + await Assert.That(result.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Queued) + .Because("A fresh claim must surface as 'queued' so the client (and operator logs) can distinguish it from a collapsed dispatch."); + await Assert.That(scenario.Capture.Enqueued).HasCount(1) + .Because("Exactly one job must reach the queue per successful claim — more than one would reintroduce the duplicate-import bug class."); + await Assert.That(scenario.Capture.Enqueued[0].Kind).IsEqualTo(ImportOperationKind.SimplyPlural); + await Assert.That(scenario.Capture.Enqueued[0].OperationId).IsEqualTo(result.Result!.OperationId) + .Because("The handler's returned operation_id must match the queued item's id, so the worker writes the row the controller advertised."); + } + } + + // Direct guard against the duplicate-SP-import bug class: repeated HTTP attempts + // (Polly retry, browser retry, double click) produce exactly one worker run. + [Test] + public async Task HandleAsync_ConcurrentDispatch_CollapsesOntoSameOperationAndDoesNotReenqueue() + { + await using var scenario = ImportDispatchScenario.ForSp(); + + var first = await scenario.DispatchAsync("idem-1"); + var second = await scenario.DispatchAsync("idem-2"); + + using (Assert.Multiple()) + { + await Assert.That(second.Accepted).IsTrue(); + await Assert.That(second.Result!.OperationId).IsEqualTo(first.Result!.OperationId) + .Because("A collapsed dispatch must return the existing operation_id; otherwise the client would subscribe to a frame that never arrives."); + await Assert.That(second.Result!.Status).IsEqualTo(ImportOperationDispatchStatus.Running) + .Because("The collapsed status is the agreed signal to the controller that it's a no-op — operator logs and audit trails depend on this string."); + await Assert.That(scenario.Capture.Enqueued).HasCount(1) + .Because("Only the original click may enqueue a job; the duplicate dispatch must collapse without producing a second worker run."); + } + } + + [Test] + public async Task HandleAsync_EmptyToken_RejectsWithoutClaimingOrEnqueuing() + { + await using var scenario = ImportDispatchScenario.ForSp(); + + var result = await scenario.DispatchAsync(token: " "); + + using (Assert.Multiple()) + { + await Assert.That(result.Accepted).IsFalse() + .Because("Whitespace-only tokens are invalid input — the handler must reject before touching the slot."); + await Assert.That(scenario.Capture.Enqueued).IsEmpty() + .Because("Rejected commands must not enqueue work; otherwise a stray validation regression would feed garbage into the worker."); + } + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InMemoryImportOperationRepositoryTests.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InMemoryImportOperationRepositoryTests.cs new file mode 100644 index 00000000..9e719be0 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InMemoryImportOperationRepositoryTests.cs @@ -0,0 +1,169 @@ +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// Per-system mutex contract for the in-memory IImportOperationRepository. The InMemory +// port must mirror the Scylla port so integration tests against InMemory are a +// meaningful proxy for the production Cassandra LWT semantics: at most one +// queued-or-running operation per (system, kind) pair. +public sealed class InMemoryImportOperationRepositoryTests +{ + [Test] + public async Task TryClaim_NoActiveOperation_ReturnsIsNewTrue() + { + var repo = new InMemoryImportOperationRepository(); + + var claim = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + + using (Assert.Multiple()) + { + await Assert.That(claim.IsNew).IsTrue() + .Because("The first claim against an empty store must always succeed; otherwise the dispatcher has no way to start an import."); + await Assert.That(claim.OperationId).IsNotEqualTo(ImportOperationId.Empty) + .Because("A successful claim must return a real operation_id — the controller uses this value as the public correlation handle."); + } + } + + // Load-bearing invariant: sequential claims collapse. Regression = duplicate-import bug. + [Test] + public async Task TryClaim_WhileActive_CollapsesOntoExistingOperation() + { + var repo = new InMemoryImportOperationRepository(); + var first = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + + var second = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-2")); + + using (Assert.Multiple()) + { + await Assert.That(second.IsNew).IsFalse() + .Because("A claim while the slot is already taken must collapse — IsNew=true here means the dispatcher would enqueue a second worker run."); + await Assert.That(second.OperationId).IsEqualTo(first.OperationId) + .Because("The collapsed claim must return the same operation_id so the caller subscribes to the same WebSocket completion frame."); + } + } + + [Test] + public async Task TryClaim_DifferentSystems_BothSucceed() + { + var repo = new InMemoryImportOperationRepository(); + var claimA = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + + var claimB = await repo.TryClaimAsync(new("nam:sys-b"), ImportOperationKind.SimplyPlural, new("idem-2")); + + using (Assert.Multiple()) + { + await Assert.That(claimA.IsNew).IsTrue(); + await Assert.That(claimB.IsNew).IsTrue() + .Because("System B's import must not be blocked by system A's — the mutex partition is per (system, kind)."); + await Assert.That(claimB.OperationId).IsNotEqualTo(claimA.OperationId) + .Because("Each fresh claim must mint its own operation_id; sharing ids across systems would break the audit trail."); + } + } + + [Test] + public async Task TryClaim_SameSystemDifferentKinds_BothSucceed() + { + var repo = new InMemoryImportOperationRepository(); + var spClaim = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + + var pkClaim = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.PluralKit, new("idem-2")); + + using (Assert.Multiple()) + { + await Assert.That(spClaim.IsNew).IsTrue(); + await Assert.That(pkClaim.IsNew).IsTrue() + .Because("SP and PK imports are independent operations; they must not share a mutex."); + } + } + + [Test] + public async Task TryClaim_AfterSucceeded_ReleasesSlotAndAllowsReclaim() + { + var repo = new InMemoryImportOperationRepository(); + var first = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + await repo.MarkSucceededAsync(new("nam:sys-a"), first.OperationId, ImportOperationKind.SimplyPlural, alterCount: 5); + + var second = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-2")); + + using (Assert.Multiple()) + { + await Assert.That(second.IsNew).IsTrue() + .Because("After the terminal transition the per-system slot must be free; otherwise users could never re-import after success."); + await Assert.That(second.OperationId).IsNotEqualTo(first.OperationId) + .Because("The new claim must mint a fresh operation_id — reusing the old one would alias two distinct imports in the audit log."); + } + } + + [Test] + public async Task TryClaim_AfterFailed_ReleasesSlotAndAllowsReclaim() + { + var repo = new InMemoryImportOperationRepository(); + var first = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + await repo.MarkFailedAsync(new("nam:sys-a"), first.OperationId, ImportOperationKind.SimplyPlural, ImportErrorCode.ImportFailed, "synthetic"); + + var second = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-2")); + + await Assert.That(second.IsNew).IsTrue() + .Because("MarkFailed must free the slot exactly like MarkSucceeded — both are terminal transitions."); + } + + // Strongest mutex test: 16 concurrent tasks must converge on one winner. + [Test] + public async Task TryClaim_ParallelDispatchers_ExactlyOneClaimsTheSlot() + { + var repo = new InMemoryImportOperationRepository(); + const int parallelism = 16; + var barrier = new TaskCompletionSource(); + + var tasks = Enumerable.Range(0, parallelism).Select(async i => + { + await barrier.Task; + return await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new($"idem-{i}")); + }).ToArray(); + + barrier.SetResult(); + var claims = await Task.WhenAll(tasks); + + var winners = claims.Count(c => c.IsNew); + var distinctOperationIds = claims.Select(c => c.OperationId).Distinct().Count(); + + using (Assert.Multiple()) + { + await Assert.That(winners).IsEqualTo(1) + .Because("Only one of the racing dispatchers may observe IsNew=true; more than one means we lost the mutex contract and would start parallel imports."); + await Assert.That(distinctOperationIds).IsEqualTo(1) + .Because("All 16 tasks must return the same operation_id (the winning one) so they all subscribe to the same WebSocket completion frame."); + } + } + + // Idempotency of pickup: a re-delivered queue item must not overwrite a terminal. + [Test] + public async Task MarkRunning_OnAlreadyTerminal_IsNoOp() + { + var repo = new InMemoryImportOperationRepository(); + var claim = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + await repo.MarkSucceededAsync(new("nam:sys-a"), claim.OperationId, ImportOperationKind.SimplyPlural, alterCount: 3); + + await repo.MarkRunningAsync(new("nam:sys-a"), claim.OperationId); + + var snapshot = await repo.GetByIdAsync(new("nam:sys-a"), claim.OperationId); + await Assert.That(snapshot!.Status).IsEqualTo(ImportOperationStatus.Succeeded) + .Because("MarkRunning must not regress a terminal Succeeded status — the only valid transition is Queued -> Running."); + } + + // Startup-sweep must never rewrite a fresh in-flight job to failed. + [Test] + public async Task GetStaleRunning_OnlyReturnsRowsOlderThanThreshold() + { + var repo = new InMemoryImportOperationRepository(); + var claim = await repo.TryClaimAsync(new("nam:sys-a"), ImportOperationKind.SimplyPlural, new("idem-1")); + await repo.MarkRunningAsync(new("nam:sys-a"), claim.OperationId); + + var stale = await repo.GetStaleRunningAsync(TimeSpan.FromMinutes(10)); + + await Assert.That(stale.Count).IsEqualTo(0) + .Because("A row that just started must never be classified as stale; otherwise the worker's startup sweep would fail in-flight imports on every restart."); + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InProcessImportJobQueueTests.cs b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InProcessImportJobQueueTests.cs new file mode 100644 index 00000000..c075ec41 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/ImportJobs/InProcessImportJobQueueTests.cs @@ -0,0 +1,132 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.Coordination; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Contracts.Models.ImportOperations; +using Interfold.Settings.Domain.Abstractions.ImportJobs; + +namespace Interfold.Api.UnitTests.ImportJobs; + +// FIFO delivery, cancellation, back-pressure for InProcessImportJobQueue. Per-(system, +// kind) dedupe lives on IImportOperationRepository, not here. +public sealed class InProcessImportJobQueueTests +{ + [Test] + public async Task ReadAll_DeliversItemsInFifoOrder() + { + await using var queue = new InProcessImportJobQueue(); + var first = NewItem("nam:sys-a"); + var second = NewItem("nam:sys-b"); + var third = NewItem("nam:sys-c"); + + await queue.EnqueueAsync(first); + await queue.EnqueueAsync(second); + await queue.EnqueueAsync(third); + await queue.DisposeAsync(); + + var observed = new List(); + await foreach (var item in queue.ReadAllAsync()) + { + observed.Add(item); + } + + using (Assert.Multiple()) + { + await Assert.That(observed).HasCount(3) + .Because("All three enqueued items must be observed by the reader; lost items would orphan an operation row in Cassandra."); + await Assert.That(observed[0]).IsEqualTo(first); + await Assert.That(observed[1]).IsEqualTo(second); + await Assert.That(observed[2]).IsEqualTo(third) + .Because("Items must be delivered in the order they were enqueued so older claims are reflected first."); + } + } + + [Test] + public async Task ReadAll_StreamsItemsPublishedAfterReaderAttached() + { + await using var queue = new InProcessImportJobQueue(); + using var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromSeconds(5)); + + var observed = new List(); + var reader = Task.Run(async () => + { + try + { + await foreach (var item in queue.ReadAllAsync(cts.Token)) + { + observed.Add(item); + if (observed.Count == 2) break; + } + } + catch (OperationCanceledException) { } + }); + + var first = NewItem("nam:sys-a"); + var second = NewItem("nam:sys-b"); + await queue.EnqueueAsync(first); + await queue.EnqueueAsync(second); + + await reader; + cts.Cancel(); + + using (Assert.Multiple()) + { + await Assert.That(observed).HasCount(2) + .Because("Both items pushed after the reader attached must be observed; otherwise the worker would silently miss late-arriving jobs."); + await Assert.That(observed[0]).IsEqualTo(first); + await Assert.That(observed[1]).IsEqualTo(second); + } + } + + [Test] + public async Task ReadAll_StopsWhenCancellationTokenFires() + { + await using var queue = new InProcessImportJobQueue(); + using var cts = new CancellationTokenSource(); + + var reader = Task.Run(async () => + { + try + { + await foreach (var _ in queue.ReadAllAsync(cts.Token)) + { + } + } + catch (OperationCanceledException) + { + } + }); + + await Task.Delay(50); + cts.Cancel(); + + var completed = await Task.WhenAny(reader, Task.Delay(TimeSpan.FromSeconds(5))); + await Assert.That(completed).IsEqualTo((Task)reader) + .Because("Cancelling the token while ReadAllAsync is parked on an empty channel must surface as a prompt exit; otherwise the worker would hang on shutdown."); + } + + [Test] + public async Task Dispose_CompletesReaderSequence() + { + var queue = new InProcessImportJobQueue(); + await queue.EnqueueAsync(NewItem("nam:sys-a")); + + await queue.DisposeAsync(); + + var observed = 0; + await foreach (var _ in queue.ReadAllAsync()) + { + observed++; + } + + await Assert.That(observed).IsEqualTo(1) + .Because("After dispose, the reader must drain the buffered item and then complete cleanly rather than hang on the closed channel."); + } + + private static ImportJobItem NewItem(string systemId) => new( + new ImportOperationId(Guid.NewGuid()), + ScopedSystemId.ParseScoped(systemId), + ImportOperationKind.SimplyPlural, + Token: new ImportToken("synthetic-token"), + RecoveryCode: null); +} diff --git a/tests/unit/Interfold.Settings.UnitTests/InMemoryAccountRepositoryLinkTokenRegressionTests.cs b/tests/unit/Interfold.Settings.UnitTests/InMemoryAccountRepositoryLinkTokenRegressionTests.cs new file mode 100644 index 00000000..168c1f6f --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/InMemoryAccountRepositoryLinkTokenRegressionTests.cs @@ -0,0 +1,95 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.InMemory.Repository; +using Microsoft.Extensions.Time.Testing; +using Interfold.Api.UnitTests.Support; + +namespace Interfold.Api.UnitTests; + +// Regression suite for InMemoryAccountRepository's link-token map. Each test pins a +// previously-latent failure mode: TTL parity with Scylla, resolve-miss forward-scrub, +// and Compose-vs-hand-concat double-prefix on the reverse-map key. +public sealed class InMemoryAccountRepositoryLinkTokenRegressionTests +{ + private static readonly SystemId RawSystemId = new("nam:abcdefg"); + private static readonly SystemId AlreadyScopedSystemId = new("nam:abcdefg"); + + + + [Test] + public async Task ResolveSystemIdByLinkTokenAsync_TokenPastTtl_ReturnsNull() + { + var start = new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.Zero); + var clock = new FakeTimeProvider(start); + var repo = new InMemoryAccountRepository(new FixedRegionContext(ScyllaKeyspace.Nam), timeProvider: clock); + + var token = await repo.GetOrCreateLinkTokenAsync(RawSystemId); + + clock.Advance(TimeSpan.FromMinutes(5) + TimeSpan.FromSeconds(1)); + + var resolved = await repo.ResolveSystemIdByLinkTokenAsync(token); + await Assert.That(resolved).IsNull() + .Because("Bug A regression: a stale token past the 5-minute TTL must not resolve — otherwise the InMemory adapter diverges from Scylla's expiry contract."); + } + + [Test] + public async Task GetLinkTokenAsync_TokenPastTtl_ReturnsNull() + { + var start = new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.Zero); + var clock = new FakeTimeProvider(start); + var repo = new InMemoryAccountRepository(new FixedRegionContext(ScyllaKeyspace.Nam), timeProvider: clock); + + await repo.GetOrCreateLinkTokenAsync(RawSystemId); + clock.Advance(TimeSpan.FromMinutes(5) + TimeSpan.FromSeconds(1)); + + var read = await repo.GetLinkTokenAsync(RawSystemId); + await Assert.That(read).IsNull() + .Because("Bug A regression: GetLinkTokenAsync must respect the same 5-minute TTL as ResolveSystemIdByLinkTokenAsync so a client-side check cannot diverge from the server-side gate."); + } + + // Deterministic-hash tokens produce the same reverse-map key for the same systemId; + // a resolve miss must scrub the forward pointer so the next GetOrCreate re-issues + // rather than adopting a dangling reverse-map pointer. + [Test] + public async Task ResolveSystemIdByLinkTokenAsync_ExpiredMiss_ScrubsForwardPointer() + { + var start = new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.Zero); + var clock = new FakeTimeProvider(start); + var repo = new InMemoryAccountRepository(new FixedRegionContext(ScyllaKeyspace.Nam), timeProvider: clock); + + var originalToken = await repo.GetOrCreateLinkTokenAsync(RawSystemId); + clock.Advance(TimeSpan.FromMinutes(5) + TimeSpan.FromSeconds(1)); + + var resolvedAfterExpiry = await repo.ResolveSystemIdByLinkTokenAsync(originalToken); + await Assert.That(resolvedAfterExpiry).IsNull() + .Because("Setup for bug B: the expired token must first be rejected so the scrub branch fires."); + + var stalePointer = await repo.GetLinkTokenAsync(RawSystemId); + await Assert.That(stalePointer).IsNull() + .Because("Bug B regression: a resolve-side miss must scrub the forward pointer so GetLinkTokenAsync doesn't hand out a token whose reverse-map entry was just refused."); + } + + // Reverse-map key must go through ScopedSystemId.Compose, not hand-concat — otherwise + // an already-scoped input becomes "nam:nam:abcdefg" and ClearLinkTokenAsync misses. + [Test] + public async Task GetOrCreateLinkTokenAsync_AlreadyScopedInput_NoDoublePrefix_AndClearRoundTrips() + { + var repo = new InMemoryAccountRepository(new FixedRegionContext(ScyllaKeyspace.Nam)); + + var issuedToken = await repo.GetOrCreateLinkTokenAsync(AlreadyScopedSystemId); + var resolved = await repo.ResolveSystemIdByLinkTokenAsync(issuedToken); + + await Assert.That(resolved).IsNotNull() + .Because("Bug C regression: a scoped-input token must resolve to the same single-prefixed id."); + await Assert.That(resolved!.Value.Value).IsEqualTo("nam:abcdefg") + .Because("The resolved id must be single-prefixed. Any double-prefix (e.g. \"nam:nam:abcdefg\") indicates the hand-concat pattern crept back in."); + + var cleared = await repo.ClearLinkTokenAsync(AlreadyScopedSystemId); + await Assert.That(cleared).IsTrue() + .Because("ClearLinkTokenAsync must succeed for the same scoped input that GetOrCreate accepted."); + + var postClear = await repo.ResolveSystemIdByLinkTokenAsync(issuedToken); + await Assert.That(postClear).IsNull() + .Because("Bug C regression: the clear path and the write path must use the same Compose call, otherwise a double-prefixed key survives ClearLinkTokenAsync because the two sides disagree on the key shape."); + } +} diff --git a/tests/unit/Interfold.Settings.UnitTests/Interfold.Settings.UnitTests.csproj b/tests/unit/Interfold.Settings.UnitTests/Interfold.Settings.UnitTests.csproj new file mode 100644 index 00000000..2fae5644 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Interfold.Settings.UnitTests.csproj @@ -0,0 +1,27 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Settings.UnitTests/Options/FcmSecretsPostConfigureTests.cs b/tests/unit/Interfold.Settings.UnitTests/Options/FcmSecretsPostConfigureTests.cs new file mode 100644 index 00000000..dc86d0fd --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Options/FcmSecretsPostConfigureTests.cs @@ -0,0 +1,47 @@ +using Interfold.Api.UnitTests.Support; +using Interfold.Settings.Api.Services.Secrets; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.Api.UnitTests.Options; + +// Contract for FcmSecretsPostConfigure: fcm:service_account_json flows verbatim onto +// FcmConfiguration.ServiceAccountJson; a missing row leaves it null (IFCMService +// falls back to NullFCMService); only the default named bucket is patched. +public sealed class FcmSecretsPostConfigureTests +{ + private const string ServiceAccountJson = """{"type":"service_account","project_id":"test"}"""; + + [Test] + public async Task PostConfigure_RowPresent_PopulatesServiceAccountJson() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.FcmServiceAccountJson, ServiceAccountJson) + .ApplyPostConfigure(); + + await Assert.That(options.ServiceAccountJson).IsEqualTo(ServiceAccountJson) + .Because("The fcm:service_account_json row must flow through verbatim — the Firebase Admin SDK, not this patcher, is responsible for parsing it."); + } + + [Test] + public async Task PostConfigure_MissingRow_LeavesNull() + { + var options = SecretsSnapshotMock.Empty() + .ApplyPostConfigure(); + + await Assert.That(options.ServiceAccountJson).IsNull() + .Because("FCM is opt-in per deployment — an absent row must leave ServiceAccountJson null so the IFCMService DI factory falls back to NullFCMService instead of tripping a boot-time validator."); + } + + [Test] + public async Task PostConfigure_NamedInstance_LeavesUntouched() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.FcmServiceAccountJson, ServiceAccountJson) + .ApplyPostConfigure(name: "other-name"); + + await Assert.That(options.ServiceAccountJson).IsNull() + .Because("PostConfigure guards on Options.DefaultName so a named bucket never receives the default's FCM credential."); + } + +} diff --git a/tests/unit/Interfold.Settings.UnitTests/Options/FirebaseClientSecretsPostConfigureTests.cs b/tests/unit/Interfold.Settings.UnitTests/Options/FirebaseClientSecretsPostConfigureTests.cs new file mode 100644 index 00000000..0983a0c3 --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/Options/FirebaseClientSecretsPostConfigureTests.cs @@ -0,0 +1,122 @@ +using Interfold.Api.UnitTests.Support; +using Interfold.Settings.Api.Services.Secrets; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Contracts.Secrets; + +namespace Interfold.Api.UnitTests.Options; + +// Contract for FirebaseClientSecretsPostConfigure: three optional JSON rows deserialise +// onto matching FirebaseClientConfiguration properties, a missing row leaves the +// platform null (endpoint returns 503), and a malformed row fails hard at boot. +public sealed class FirebaseClientSecretsPostConfigureTests +{ + private const string AndroidJson = + """ + { + "api_key": "AIzaFakeAndroidKey", + "application_id": "1:1234567890:android:abcdef", + "project_id": "test-project", + "gcm_sender_id": "1234567890", + "storage_bucket": "test-project.appspot.com" + } + """; + + private const string IosJson = + """ + { + "api_key": "AIzaFakeIosKey", + "google_app_id": "1:1234567890:ios:abcdef", + "gcm_sender_id": "1234567890", + "project_id": "test-project", + "storage_bucket": "test-project.appspot.com", + "bundle_id": "app.octocon.ios", + "client_id": "1234567890-abcdef.apps.googleusercontent.com" + } + """; + + private const string WebJson = + """ + { + "api_key": "AIzaFakeWebKey", + "auth_domain": "test-project.firebaseapp.com", + "project_id": "test-project", + "storage_bucket": "test-project.appspot.com", + "messaging_sender_id": "1234567890", + "app_id": "1:1234567890:web:abcdef", + "vapid_key": "TEST_VAPID_PUBLIC" + } + """; + + [Test] + public async Task PostConfigure_ThreeValidPlatformJsonRows_PopulatesAll() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.FirebaseClientAndroid, AndroidJson) + .With(SecretsStoreKeys.FirebaseClientIos, IosJson) + .With(SecretsStoreKeys.FirebaseClientWeb, WebJson) + .ApplyPostConfigure(); + + using (Assert.Multiple()) + { + await Assert.That(options.Android).IsNotNull() + .Because("Android JSON row must deserialise into FirebaseAndroidClientConfig; a null here means the snake_case naming policy or the ParseOrThrow helper broke."); + await Assert.That(options.Android!.ApiKey).IsEqualTo("AIzaFakeAndroidKey") + .Because("The snake_case api_key field must map to the PascalCase ApiKey property — verifies the JsonNamingPolicy.SnakeCaseLower binding on the deserializer options."); + await Assert.That(options.Ios).IsNotNull() + .Because("iOS JSON row parity with Android — same failure mode, distinct signal so a partial regression is easier to diagnose."); + await Assert.That(options.Ios!.BundleId).IsEqualTo("app.octocon.ios") + .Because("iOS-specific bundle_id must round-trip since the SDK cross-checks it against the running app's bundle at init."); + await Assert.That(options.Web).IsNotNull() + .Because("Web JSON row parity with Android/iOS."); + await Assert.That(options.Web!.VapidKey).IsEqualTo("TEST_VAPID_PUBLIC") + .Because("VAPID key is required for the JS SDK's getToken({vapidKey}) call — this field must survive PostConfigure."); + } + } + + // No [Required] on Firebase — empty snapshot passes validation; /api/settings/firebase-config + // is what surfaces the missing seed as a 503 downstream. + [Test] + public async Task PostConfigure_MissingRows_LeavesAllNull() + { + var options = SecretsSnapshotMock.Empty() + .ApplyPostConfigure(); + + using (Assert.Multiple()) + { + await Assert.That(options.Android).IsNull() + .Because("Absent Android row must leave the property null so /api/settings/firebase-config?platform=android returns 503 rather than tripping a boot-time validator — Firebase is opt-in."); + await Assert.That(options.Ios).IsNull() + .Because("Absent iOS row parity — same optional-platform contract."); + await Assert.That(options.Web).IsNull() + .Because("Absent Web row parity — same optional-platform contract."); + } + } + + [Test] + public async Task PostConfigure_MalformedJson_ThrowsWithKeyName() + { + var snapshot = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.FirebaseClientAndroid, "{ this is not JSON"); + var patcher = new FirebaseClientSecretsPostConfigure(snapshot.Object); + var options = new FirebaseClientConfiguration(); + + var ex = Assert.Throws( + () => patcher.PostConfigure(Microsoft.Extensions.Options.Options.DefaultName, options)); + + await Assert.That(ex!.Message) + .Contains(SecretsStoreKeys.FirebaseClientAndroid.Value) + .Because("The diagnostic message must name the offending internal.secrets row so an operator staring at the boot log can jump straight to the malformed seed."); + } + + [Test] + public async Task PostConfigure_NamedInstance_LeavesUntouched() + { + var options = SecretsSnapshotMock.Empty() + .With(SecretsStoreKeys.FirebaseClientAndroid, AndroidJson) + .ApplyPostConfigure(name: "other-name"); + + await Assert.That(options.Android).IsNull() + .Because("PostConfigure guards on Options.DefaultName so a named bucket never receives the default's Firebase payload."); + } + +} diff --git a/tests/unit/Interfold.Settings.UnitTests/SimplyPlural/SpJsonContextReproTests.cs b/tests/unit/Interfold.Settings.UnitTests/SimplyPlural/SpJsonContextReproTests.cs new file mode 100644 index 00000000..3059867e --- /dev/null +++ b/tests/unit/Interfold.Settings.UnitTests/SimplyPlural/SpJsonContextReproTests.cs @@ -0,0 +1,70 @@ +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using Interfold.Settings.Api.Services.SimplyPlural; +using SpJsonContext = Interfold.Settings.Api.Services.SimplyPlural.SpJsonContext; + +namespace Interfold.Api.UnitTests.SimplyPlural; + +// Pins SpCustomFieldContent.SupportMarkdown as nullable-bool. SP's update300 migration +// copies legacy field rows without backfilling supportMarkdown → wire sends null. A +// non-nullable bool would fail every legacy-migrated system's customFields deserialise +// (swallowed by FetchAsync's catch-all → silent custom-fields no-op). +public sealed class SpJsonContextReproTests +{ + [Test] + public async Task CustomFields_WithNullSupportMarkdown_ShouldDeserializeWithoutThrowing() + { + var json = """[{"exists":true,"id":"000000000000000000000001","content":{"uid":"synthetic-system","name":"SyntheticField","order":"0|aaaaaa:","type":0,"supportMarkdown":null,"buckets":["000000000000000000000002","000000000000000000000003"],"oid":"synthetic-oid-placeholder"}}]"""; + + var typeInfo = (JsonTypeInfo>>) + SpJsonContext.Default.GetTypeInfo(typeof(List>))!; + + var result = JsonSerializer.Deserialize(json, typeInfo); + + await Assert.That(result).IsNotNull(); + await Assert.That(result!.Count).IsEqualTo(1); + await Assert.That(result[0].Content.Name).IsEqualTo("SyntheticField"); + await Assert.That(result[0].Content.SupportMarkdown).IsNull(); + } + + [Test] + public async Task CustomFields_WithExplicitTrueSupportMarkdown_StillDeserializes() + { + var json = """[{"exists":true,"id":"000000000000000000000001","content":{"name":"x","type":0,"supportMarkdown":true}}]"""; + + var typeInfo = (JsonTypeInfo>>) + SpJsonContext.Default.GetTypeInfo(typeof(List>))!; + + var result = JsonSerializer.Deserialize(json, typeInfo); + + await Assert.That(result![0].Content.SupportMarkdown).IsTrue(); + } + + [Test] + public async Task CustomFields_WithExplicitFalseSupportMarkdown_StillDeserializes() + { + var json = """[{"exists":true,"id":"000000000000000000000001","content":{"name":"x","type":0,"supportMarkdown":false}}]"""; + + var typeInfo = (JsonTypeInfo>>) + SpJsonContext.Default.GetTypeInfo(typeof(List>))!; + + var result = JsonSerializer.Deserialize(json, typeInfo); + + await Assert.That(result![0].Content.SupportMarkdown).IsFalse(); + } + + // Notes carry the same null shape from pre-supportMarkdown SP; not read today but a + // hard-throw here would silently empty the per-alter notes page for any affected system. + [Test] + public async Task Notes_WithNullSupportMarkdown_ShouldDeserializeWithoutThrowing() + { + var json = """[{"exists":true,"id":"000000000000000000000004","content":{"title":"t","note":"n","color":"#000","member":"m","date":0,"supportMarkdown":null,"lastOperationTime":0}}]"""; + + var typeInfo = (JsonTypeInfo>>) + SpJsonContext.Default.GetTypeInfo(typeof(List>))!; + + var result = JsonSerializer.Deserialize(json, typeInfo); + + await Assert.That(result![0].Content.SupportMarkdown).IsNull(); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryFixture.cs b/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryFixture.cs new file mode 100644 index 00000000..be45fc3d --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryFixture.cs @@ -0,0 +1,63 @@ +using Interfold.Alters.Contracts.Abstractions; +using Interfold.Alters.Domain.Abstractions.Repository; +using Interfold.Friendships.Domain.Abstractions.Repository; +using Interfold.Settings.Domain; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Shared.Domain.Abstractions.Repository; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.InMemory; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Polls.Domain.Abstractions.Repository; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Settings.Domain.Abstractions.Repository; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using TUnit.Core.Interfaces; + +namespace Interfold.Api.UnitTests.Coordination; + +// TUnit fixture for FCMServiceFactoryTests. Each CreateScope yields a fresh SP because +// the IFCMService factory snapshots secrets at singleton-construction time — sharing +// containers across scenarios would cross-contaminate routing decisions. +public sealed class FCMServiceFactoryFixture : IAsyncInitializer +{ + public Task InitializeAsync() => Task.CompletedTask; + + public FCMFactoryScope CreateScope(NodeGroup role, bool seedServiceAccount) + { + var services = new ServiceCollection(); + services.AddSingleton(NullLoggerFactory.Instance); + services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>)); + + // FirebaseFCMService's concrete type is DI-resolvable even in "not selected" + // scenarios; wire its transitive InMemory-repo chain so construction never + // throws unexpectedly. + services.AddSingleton(_ => new InMemoryRegionContext()); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.Configure(o => + o.ServiceAccountJson = seedServiceAccount + ? "{\"type\":\"service_account\",\"project_id\":\"test\"}" + : null); + + services.AddInterfoldCluster(role); + return new FCMFactoryScope(services.BuildServiceProvider()); + } +} + +// Owns a single scenario's ServiceProvider; async disposal routes IDisposable and +// IAsyncDisposable singletons through the same walker. +public sealed class FCMFactoryScope(ServiceProvider provider) : IAsyncDisposable +{ + public IServiceProvider Services => provider; + + public ValueTask DisposeAsync() => provider.DisposeAsync(); +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryTests.cs b/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryTests.cs new file mode 100644 index 00000000..7b7f7acb --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Coordination/FCMServiceFactoryTests.cs @@ -0,0 +1,61 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Domain.Abstractions; +using Interfold.Infrastructure.Coordination; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Api.UnitTests.Coordination; + +// AddInterfoldCluster's IFCMService factory encodes three routing rules: non-primary +// nodes always get NullFCMService; primary nodes without fcm:service_account_json get +// NullFCMService; primary nodes with the credential get FirebaseFCMService. A regression +// silently drops notifications or crashes at the first fronting change. +[ClassDataSource(Shared = SharedType.PerTestSession)] +public sealed class FCMServiceFactoryTests(FCMServiceFactoryFixture fixture) +{ + [Test] + [Arguments(NodeGroup.Auxiliary)] + [Arguments(NodeGroup.Sidecar)] + public async Task NonPrimaryNode_AlwaysResolvesNullFCMService(NodeGroup role) + { + await using var scope = fixture.CreateScope(role, seedServiceAccount: true); + + var resolved = scope.Services.GetRequiredService(); + + await Assert.That(resolved).IsTypeOf() + .Because("Only primary nodes are meant to hold the send responsibility; auxiliaries would double-send if promoted."); + } + + [Test] + public async Task PrimaryNode_WithBlankServiceAccount_ResolvesNullFCMService() + { + await using var scope = fixture.CreateScope(NodeGroup.Primary, seedServiceAccount: false); + + var resolved = scope.Services.GetRequiredService(); + + await Assert.That(resolved).IsTypeOf() + .Because("The fcm:service_account_json row is absent — factory must fall back to the no-op rather than trying to construct FirebaseFCMService."); + } + + [Test] + public async Task PrimaryNode_WithSeededServiceAccount_ResolvesFirebaseFCMService() + { + await using var scope = fixture.CreateScope(NodeGroup.Primary, seedServiceAccount: true); + + var resolved = scope.Services.GetRequiredService(); + + await Assert.That(resolved).IsTypeOf() + .Because("The credential is present and the node owns the send responsibility — the factory must promote to the real sender."); + } + + [Test] + public async Task ResolutionIsStable_AcrossRepeatedRequests() + { + await using var scope = fixture.CreateScope(NodeGroup.Primary, seedServiceAccount: true); + + var first = scope.Services.GetRequiredService(); + var second = scope.Services.GetRequiredService(); + + await Assert.That(ReferenceEquals(first, second)).IsTrue() + .Because("IFCMService is a singleton — a fresh instance per request would mean the factory's blocking secrets read runs on every notification flush."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Coordination/InMemoryNotificationTokenRepositoryTests.cs b/tests/unit/Interfold.Shared.UnitTests/Coordination/InMemoryNotificationTokenRepositoryTests.cs new file mode 100644 index 00000000..51105670 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Coordination/InMemoryNotificationTokenRepositoryTests.cs @@ -0,0 +1,92 @@ +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Coordination; + +// Contract for InMemoryNotificationTokenRepository.ListTokensForFriendsOfAsync — the +// InMemory backend must match Scylla (grouped by friend, friends without tokens dropped) +// or integration tests diverge silently between backends. +public sealed class InMemoryNotificationTokenRepositoryTests +{ + [Test] + public async Task ListTokensForFriendsOf_GroupsByFriend_ExcludingCallerOwn() + { + var friendships = new InMemoryFriendshipRepository(); + var tokens = new InMemoryNotificationTokenRepository(friendships); + + await tokens.AddAsync(new("alice"), new("alice-token"), CancellationToken.None); + await tokens.AddAsync(new("bob"), new("bob-token"), CancellationToken.None); + await tokens.AddAsync(new("carol"), new("carol-token"), CancellationToken.None); + + await friendships.SendRequestAsync(new("alice"), new("bob")); + await friendships.AcceptRequestAsync(new("bob"), new("alice")); + await friendships.SendRequestAsync(new("alice"), new("carol")); + await friendships.AcceptRequestAsync(new("carol"), new("alice")); + + var result = await tokens.ListTokensForFriendsOfAsync(new("alice"), CancellationToken.None); + + await Assert.That(result.Count).IsEqualTo(2); + await Assert.That(result.Any(g => g.FriendSystemId == new SystemId("bob") && g.Tokens.Contains(new PushToken("bob-token")))).IsTrue(); + await Assert.That(result.Any(g => g.FriendSystemId == new SystemId("carol") && g.Tokens.Contains(new PushToken("carol-token")))).IsTrue(); + await Assert.That(result.Any(g => g.FriendSystemId == new SystemId("alice"))).IsFalse() + .Because("The fronting-changed push targets friends of the system, not the system itself."); + } + + [Test] + public async Task ListTokensForFriendsOf_ReturnsEmptyForSystemWithNoFriends() + { + var friendships = new InMemoryFriendshipRepository(); + var tokens = new InMemoryNotificationTokenRepository(friendships); + await tokens.AddAsync(new("lonely"), new("lonely-token"), CancellationToken.None); + + var result = await tokens.ListTokensForFriendsOfAsync(new("lonely"), CancellationToken.None); + await Assert.That(result.Count).IsEqualTo(0); + } + + // Friends without tokens are omitted entirely (not returned as empty groups) so + // callers don't have to guard on Tokens.Count > 0. + [Test] + public async Task ListTokensForFriendsOf_OmitsFriendsWithoutTokens() + { + var friendships = new InMemoryFriendshipRepository(); + var tokens = new InMemoryNotificationTokenRepository(friendships); + + await friendships.SendRequestAsync(new("alice"), new("bob")); + await friendships.AcceptRequestAsync(new("bob"), new("alice")); + + var result = await tokens.ListTokensForFriendsOfAsync(new("alice"), CancellationToken.None); + await Assert.That(result.Count).IsEqualTo(0); + } + + [Test] + public async Task ListTokensForFriendsOf_MergesMultipleTokensPerFriend() + { + var friendships = new InMemoryFriendshipRepository(); + var tokens = new InMemoryNotificationTokenRepository(friendships); + await tokens.AddAsync(new("bob"), new("bob-phone"), CancellationToken.None); + await tokens.AddAsync(new("bob"), new("bob-tablet"), CancellationToken.None); + + await friendships.SendRequestAsync(new("alice"), new("bob")); + await friendships.AcceptRequestAsync(new("bob"), new("alice")); + + var result = await tokens.ListTokensForFriendsOfAsync(new("alice"), CancellationToken.None); + await Assert.That(result.Count).IsEqualTo(1); + + var bobGroup = result.Single(); + await Assert.That(bobGroup.FriendSystemId).IsEqualTo(new SystemId("bob")); + await Assert.That(bobGroup.Tokens.Count).IsEqualTo(2); + await Assert.That(bobGroup.Tokens.Contains(new PushToken("bob-phone"))).IsTrue(); + await Assert.That(bobGroup.Tokens.Contains(new PushToken("bob-tablet"))).IsTrue(); + } + + [Test] + public async Task ListTokensForFriendsOf_HandlesBlankSystemIdGracefully() + { + var friendships = new InMemoryFriendshipRepository(); + var tokens = new InMemoryNotificationTokenRepository(friendships); + + var result = await tokens.ListTokensForFriendsOfAsync(new(""), CancellationToken.None); + await Assert.That(result.Count).IsEqualTo(0); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Helpers/AvatarServingPolicyTests.cs b/tests/unit/Interfold.Shared.UnitTests/Helpers/AvatarServingPolicyTests.cs new file mode 100644 index 00000000..32c4a847 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Helpers/AvatarServingPolicyTests.cs @@ -0,0 +1,280 @@ +using Interfold.Shared.Api.Helpers; + +namespace Interfold.Api.UnitTests.Helpers; + +// AvatarServingPolicy.Resolve — Program.cs uses this to decide whether to mount the +// avatar static-file middleware. One test per branch of the AvatarStorageRoot × +// AvatarPublicBase matrix; per-test temp dirs avoid races under TUnit's parallel runner. +public sealed class AvatarServingPolicyTests +{ + [Test] + public async Task BlankStorageRoot_DoesNotServe() + { + var (shouldServe, root, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: null, + avatarPublicBase: null); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsFalse() + .Because("With no storage root configured, the API has no directory to serve files from."); + await Assert.That(root).IsEqualTo(string.Empty) + .Because("The PhysicalRoot is meaningless when ShouldServe is false; the helper normalises it to empty."); + await Assert.That(requestPath).IsEqualTo(string.Empty) + .Because("The RequestPath is meaningless when ShouldServe is false; the helper normalises it to empty."); + } + } + + [Test] + public async Task WhitespaceStorageRoot_DoesNotServe() + { + var (shouldServe, _, _) = AvatarServingPolicy.Resolve( + avatarStorageRoot: " ", + avatarPublicBase: null); + + await Assert.That(shouldServe).IsFalse() + .Because("Whitespace-only roots should be treated identically to blank — same operator intent."); + } + + [Test] + public async Task MissingDirectoryOnDisk_DoesNotServe() + { + var nonexistent = Path.Combine(Path.GetTempPath(), $"avatar-policy-missing-{Guid.NewGuid():N}"); + + var (shouldServe, _, _) = AvatarServingPolicy.Resolve( + avatarStorageRoot: nonexistent, + avatarPublicBase: null); + + await Assert.That(shouldServe).IsFalse() + .Because("Configured-but-not-mounted storage root should refuse to serve so the operator's missing-volume mistake fails loudly instead of stamping 404s."); + } + + [Test] + public async Task AbsoluteHttpsBase_DoesNotServe() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, _, _) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "https://cdn.example.com/avatars"); + + await Assert.That(shouldServe).IsFalse() + .Because("An absolute https:// base means a CDN fronts the bytes; the API must not duplicate the serving surface."); + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task AbsoluteHttpBase_DoesNotServe() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, _, _) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "http://cdn.example.com/avatars"); + + await Assert.That(shouldServe).IsFalse() + .Because("An absolute http:// base means a CDN/reverse-proxy fronts the bytes; the helper treats it the same as https://."); + } + finally + { + Directory.Delete(tempDir); + } + } + + // Only http(s) is treated as CDN delegation; exotic schemes fall through to path-only. + [Test] + public async Task NonHttpAbsoluteBase_IsTreatedAsPathOnly() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, _, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "ftp://files.example.com/avatars"); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsTrue() + .Because("Only http(s) absolute bases disable serving; non-web schemes aren't a CDN delegation contract."); + await Assert.That(requestPath.StartsWith('/')).IsTrue() + .Because("The fall-through path still gets a leading-slash anchor — never silently swallowed."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task BlankPublicBase_UsesDefaultPublicBase() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, root, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: null); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsTrue() + .Because("Storage on disk + no CDN base = API is the canonical origin → serve."); + await Assert.That(root).IsEqualTo(tempDir) + .Because("PhysicalRoot must echo the configured storage root verbatim so PhysicalFileProvider sees the exact path."); + await Assert.That(requestPath).IsEqualTo(AvatarServingPolicy.DefaultPublicBase) + .Because("Blank base falls back to /avatars — the same prefix LocalAvatarStorage stamps into avatar_url for newly-stored files."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task PathOnlyBase_PreservesPath() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, root, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "/static/avatars"); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsTrue() + .Because("Path-only base means the API is still the origin; operator just wants a custom URL prefix."); + await Assert.That(root).IsEqualTo(tempDir); + await Assert.That(requestPath).IsEqualTo("/static/avatars") + .Because("Caller-supplied path-only base must round-trip verbatim — that's the contract LocalAvatarStorage's URL stamping relies on."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task PathOnlyBaseMissingLeadingSlash_GetsLeadingSlash() + { + var tempDir = CreateTempDirectory(); + try + { + var (_, _, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "static/avatars"); + + await Assert.That(requestPath).IsEqualTo("/static/avatars") + .Because("StaticFileOptions.RequestPath requires a leading slash; the helper normalises operator input."); + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task PathOnlyBaseWithTrailingSlash_TrimsTrailingSlash() + { + var tempDir = CreateTempDirectory(); + try + { + var (_, _, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "/avatars/"); + + await Assert.That(requestPath).IsEqualTo("/avatars") + .Because("StaticFileOptions.RequestPath compares trailing-slash-sensitively; trim once at the policy edge so callers don't have to."); + } + finally + { + Directory.Delete(tempDir); + } + } + + // A literal "/" would collapse to empty after trimming, causing StaticFileOptions to + // short-circuit every request (stealing /api/* and every SPA route). + [Test] + public async Task RootSlashBase_FallsBackToDefault() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, _, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "/"); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsTrue(); + await Assert.That(requestPath).IsEqualTo(AvatarServingPolicy.DefaultPublicBase) + .Because("A degenerate '/' base must not collapse to an empty prefix — the helper re-anchors to /avatars."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task AbsoluteHttpLocalhostBase_ServesWithExtractedPath() + { + var tempDir = CreateTempDirectory(); + try + { + var (shouldServe, root, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: "http://localhost/avatars-itest/abc123"); + + using (Assert.Multiple()) + { + await Assert.That(shouldServe).IsTrue() + .Because("localhost absolute bases are same-origin — the API still serves bytes in dev/integration."); + await Assert.That(root).IsEqualTo(tempDir); + await Assert.That(requestPath).IsEqualTo("/avatars-itest/abc123") + .Because("RequestPath must be the URI path so Program.cs middleware matches avatar_url GETs."); + } + } + finally + { + Directory.Delete(tempDir); + } + } + + [Test] + public async Task CustomDefaultPublicBase_IsHonoured() + { + var tempDir = CreateTempDirectory(); + try + { + var (_, _, requestPath) = AvatarServingPolicy.Resolve( + avatarStorageRoot: tempDir, + avatarPublicBase: null, + defaultPublicBase: "/cdn/avatars"); + + await Assert.That(requestPath).IsEqualTo("/cdn/avatars") + .Because("Callers (e.g. unit tests, future feature flags) can override the fallback; the helper honours it instead of hardcoding /avatars."); + } + finally + { + Directory.Delete(tempDir); + } + } + + private static string CreateTempDirectory() + { + var path = Path.Combine(Path.GetTempPath(), $"avatar-policy-{Guid.NewGuid():N}"); + Directory.CreateDirectory(path); + return path; + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Ids/HexColorTests.cs b/tests/unit/Interfold.Shared.UnitTests/Ids/HexColorTests.cs new file mode 100644 index 00000000..35476245 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Ids/HexColorTests.cs @@ -0,0 +1,216 @@ +using System.Text.Json; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins the strict contract every HexColor construction path honours: ctor, Parse, +// TryParse, JSON reader, and FromNullable all reject non-well-formed input. Normalise +// (shared with the SP importer + HexColorFixupService) is exercised here too, since a +// bare six-char slip through would silently write malformed rows again. +public sealed class HexColorTests +{ + [Test] + [Arguments("#F0A")] + [Arguments("#FF00AA")] + [Arguments("#ff00aa")] + [Arguments("#FfAa00")] + [Arguments("#FF00AAFF")] + public async Task Constructor_WellFormed_Accepts(string input) + { + var color = new HexColor(input); + + using (Assert.Multiple()) + { + await Assert.That(color.Value).IsEqualTo(input) + .Because("The constructor preserves the input verbatim once validation passes; case and length are all callers' concern."); + await Assert.That(color.IsWellFormed).IsTrue() + .Because("A value that survived the constructor's validation must satisfy IsWellFormed — tautology for defence-in-depth checks."); + } + } + + [Test] + [Arguments("")] + [Arguments("red")] + [Arguments("FF00AA")] + [Arguments("#GGGGGG")] + [Arguments("#FF00")] + [Arguments("#FF00AAF")] + public async Task Constructor_Malformed_Throws(string input) + { + await Assert.That(() => new HexColor(input)).ThrowsExactly() + .Because("Every wire-side construction routes through the constructor; malformed input must fail-fast rather than persisting a bad value."); + } + + [Test] + public async Task Constructor_Null_Throws() + { + await Assert.That(() => new HexColor(null!)).ThrowsExactly() + .Because("null is a caller bug, not malformed data — ArgumentNullException surfaces the parameter name."); + } + + [Test] + public async Task Parse_WellFormed_Roundtrips() + { + var color = HexColor.Parse("#FF00AA", provider: null); + await Assert.That(color.Value).IsEqualTo("#FF00AA"); + } + + [Test] + public async Task Parse_Malformed_Throws() + { + await Assert.That(() => HexColor.Parse("red", provider: null)).ThrowsExactly(); + } + + [Test] + public async Task TryParse_WellFormed_ReturnsTrue() + { + var parsed = HexColor.TryParse("#FF00AA", provider: null, out var color); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(color.Value).IsEqualTo("#FF00AA"); + } + } + + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments("red")] + [Arguments("FF00AA")] + public async Task TryParse_MalformedOrNull_ReturnsFalse(string? input) + { + var parsed = HexColor.TryParse(input, provider: null, out _); + + await Assert.That(parsed).IsFalse() + .Because("TryParse must never throw and must return false for every shape that would fail Parse — matches int.TryParse's contract."); + } + + [Test] + public async Task FromNullable_Null_ReturnsNull() + { + var color = HexColor.FromNullable(null); + await Assert.That(color).IsNull() + .Because("null input is 'no color stored' and must round-trip as null without validation."); + } + + [Test] + public async Task FromNullable_WellFormed_ReturnsHexColor() + { + var color = HexColor.FromNullable("#FF00AA"); + + using (Assert.Multiple()) + { + await Assert.That(color).IsNotNull(); + await Assert.That(color!.Value.Value).IsEqualTo("#FF00AA"); + } + } + + [Test] + [Arguments("")] + [Arguments("red")] + [Arguments("FF00AA")] + [Arguments("#GG0000")] + public async Task FromNullable_Malformed_Throws(string input) + { + await Assert.That(() => HexColor.FromNullable(input)).ThrowsExactly() + .Because("FromNullable is the Scylla read path: null when unset, throw when corrupt."); + } + + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments("red")] + [Arguments("#GG0000")] + public async Task FromStorage_Unrecoverable_ReturnsNull(string? input) + { + await Assert.That(HexColor.FromStorage(input)).IsNull(); + } + + [Test] + [Arguments("#FF00AA")] + [Arguments("FF00AA", "#FF00AA")] + [Arguments(" #abc ", "#abc")] + public async Task FromStorage_Recoverable_ReturnsHexColor(string input, string? expected = null) + { + expected ??= input; + var color = HexColor.FromStorage(input); + await Assert.That(color).IsNotNull(); + await Assert.That(color!.Value.Value).IsEqualTo(expected); + } + + [Test] + public async Task Json_WellFormed_Roundtrips() + { + var original = new HexColor("#FF00AA"); + var json = JsonSerializer.Serialize(original); + + using (Assert.Multiple()) + { + await Assert.That(json).IsEqualTo("\"#FF00AA\"") + .Because("The JSON emission is byte-identical to the raw string — persistence-adapter reads and idempotency-hashed replays depend on this."); + + var deserialised = JsonSerializer.Deserialize(json); + await Assert.That(deserialised.Value).IsEqualTo(original.Value); + } + } + + [Test] + [Arguments("\"\"")] + [Arguments("\"red\"")] + [Arguments("\"FF00AA\"")] + [Arguments("\"#GG0000\"")] + public async Task Json_Malformed_ThrowsJsonException(string malformedJson) + { + await Assert.That(() => JsonSerializer.Deserialize(malformedJson)) + .ThrowsExactly() + .Because("Malformed JSON must surface as JsonException so ASP.NET Core model binding maps to a 400, matching the StringBackedIds house style."); + } + + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments(" ")] + public async Task Normalise_NullOrBlank_ReturnsNull(string? input) + { + var result = HexColor.Normalise(input); + await Assert.That(result).IsNull(); + } + + [Test] + [Arguments("#FF00AA")] + [Arguments("#F0A")] + [Arguments("#FF00AAFF")] + [Arguments("#ff00aa")] + public async Task Normalise_AlreadyWellFormed_ReturnsInputVerbatim(string input) + { + var result = HexColor.Normalise(input); + await Assert.That(result).IsEqualTo(input) + .Because("An already-well-formed value must pass through unchanged so the fixup service treats it as a no-op (skip the UPDATE)."); + } + + [Test] + [Arguments("FF00AA", "#FF00AA")] + [Arguments("ff00aa", "#ff00aa")] + [Arguments(" #FF00AA ", "#FF00AA")] + [Arguments(" FF00AA ", "#FF00AA")] + public async Task Normalise_RecoverableBareHex_ReturnsCanonicalForm(string input, string expected) + { + var result = HexColor.Normalise(input); + await Assert.That(result).IsEqualTo(expected) + .Because("Bare six-char hex is the SP importer's historical shape and the pre-strict client contract — Normalise prepends the # and trims whitespace so downstream construction succeeds."); + } + + [Test] + [Arguments("red")] + [Arguments("FF00")] + [Arguments("FF00AAFF")] + [Arguments("#GGGGGG")] + [Arguments("GGGGGG")] + public async Task Normalise_Unrecoverable_ReturnsNull(string input) + { + var result = HexColor.Normalise(input); + await Assert.That(result).IsNull() + .Because("Anything not recoverable as exactly six hex digits (with optional #) is unrecoverable; the fixup service nulls the DB column in that case."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Ids/PiiWrapperImplicitWidenAbsenceTests.cs b/tests/unit/Interfold.Shared.UnitTests/Ids/PiiWrapperImplicitWidenAbsenceTests.cs new file mode 100644 index 00000000..150263a3 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Ids/PiiWrapperImplicitWidenAbsenceTests.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using Interfold.Settings.Contracts.Ids; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins that no PII-redacting wrapper (Category B) in Interfold.Shared.Contracts.Ids ships an +// `implicit operator string`. Adding one silently defeats SecretRedaction under any +// Foo(string) overload; interim guardrail until a Roslyn analyzer catches it at PR time. +public sealed class PiiWrapperImplicitWidenAbsenceTests +{ + [Test] + [Arguments(typeof(DiscordId))] + [Arguments(typeof(Email))] + [Arguments(typeof(AppleId))] + [Arguments(typeof(LinkToken))] + [Arguments(typeof(PushToken))] + [Arguments(typeof(ImportToken))] + [Arguments(typeof(RecoveryCode))] + [Arguments(typeof(Jti))] + [Arguments(typeof(SocketToken))] + [Arguments(typeof(EncryptionKeyMaterial))] + [Arguments(typeof(KeyChecksum))] + [Arguments(typeof(EncryptionSalt))] + public async Task CategoryB_HasNoImplicitOperatorToString(Type wrapperType) + { + var implicitToString = wrapperType + .GetMethods(BindingFlags.Public | BindingFlags.Static) + .Where(m => m.Name == "op_Implicit" && m.ReturnType == typeof(string)) + .ToArray(); + + await Assert.That(implicitToString.Length).IsEqualTo(0) + .Because($"{wrapperType.Name} is a PII-redacting wrapper and must NOT ship an implicit widen to string — that would silently defeat SecretRedaction under any Foo(string) overload. If you need the raw value at a persistence / DB / HTTP boundary, unwrap via .Value explicitly. See DiscordId's operator xml-doc for the full rationale."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdRepresentsSameUserAsTests.cs b/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdRepresentsSameUserAsTests.cs new file mode 100644 index 00000000..22794576 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdRepresentsSameUserAsTests.cs @@ -0,0 +1,64 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins the ScopedSystemId.RepresentsSameUserAs(SystemId) primitive that backs every +// SystemId-typed controller self-request guard. A bare byte compare would miss the +// raw-id shape (/api/friends/{rawId}); these tests catch that regression. The +// FriendLookup overload's tests live in Interfold.Friendships.UnitTests alongside the +// FriendLookupExtensions.RepresentsSameUserAs extension. +public sealed class ScopedSystemIdRepresentsSameUserAsTests +{ + private static readonly ScopedSystemId Principal = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "abcdefg"); + + [Test] + public async Task SystemId_SameRegionScoped_IsSelf() + { + SystemId candidate = new("nam:abcdefg"); + + await Assert.That(Principal.RepresentsSameUserAs(candidate)).IsTrue() + .Because("A scoped candidate in the principal's region and with the principal's raw id is the same user."); + } + + // A raw byte compare would see "nam:abcdefg" == "abcdefg" → false; the semantic + // check catches it up front. + [Test] + public async Task SystemId_RawId_MatchingPrincipalRawId_IsSelf() + { + SystemId candidate = new("abcdefg"); + + await Assert.That(Principal.RepresentsSameUserAs(candidate)).IsTrue() + .Because("A raw candidate whose value equals the principal's RawId represents the same user in the principal's region."); + } + + // Cross-region scoped is a different user per the "scoped composite is identity" + // contract; must not coerce to the principal's region. + [Test] + public async Task SystemId_CrossRegionScopedWithSameRawId_IsNotSelf() + { + SystemId candidate = new("eur:abcdefg"); + + await Assert.That(Principal.RepresentsSameUserAs(candidate)).IsFalse() + .Because("A cross-region scoped id with the same raw id is a different user per the 'scoped composite is identity' contract — the self-guard must not coerce it into the principal's region."); + } + + [Test] + public async Task SystemId_SameRegionDifferentRawId_IsNotSelf() + { + SystemId candidate = new("nam:xyzzyxq"); + + await Assert.That(Principal.RepresentsSameUserAs(candidate)).IsFalse() + .Because("Same-region scoped candidates with a different raw id are different users."); + } + + // Blank inputs must not blow up defensive callers (test fixtures, migration scripts). + [Test] + public async Task SystemId_BlankValue_ReturnsFalse() + { + SystemId candidate = new(string.Empty); + + await Assert.That(Principal.RepresentsSameUserAs(candidate)).IsFalse() + .Because("Blank candidates cannot represent any user — the primitive must return false rather than throw so the guard is safe on defensive inputs."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdTests.cs b/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdTests.cs new file mode 100644 index 00000000..f5e7a7cc --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Ids/ScopedSystemIdTests.cs @@ -0,0 +1,270 @@ +using System.Text.Json; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins the invariants that make ScopedSystemId a safe stand-in for hand-formatted +// $"{region}:{id}" concatenations: idempotency across scoped/raw inputs, byte-exact +// JSON wire compatibility with SystemId, and strict parsing at trust boundaries. +public sealed class ScopedSystemIdTests +{ + [Test] + public async Task Compose_RawId_ProducesScopedValue() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "abcdefg"); + + using (Assert.Multiple()) + { + await Assert.That(scoped.Value).IsEqualTo("nam:abcdefg") + .Because("Compose from a bare id must emit the canonical scoped form so persistence adapters see a single wire shape."); + await Assert.That(scoped.RawId).IsEqualTo("abcdefg") + .Because("RawId exposes the bare id for consumers (Scylla PKs) that need the region-stripped shape."); + await Assert.That(scoped.Region).IsEqualTo(ScyllaKeyspace.Nam) + .Because("Region reflects the composed-with argument, not a lazy parse of Value."); + } + } + + // Load-bearing anti-double-prefix invariant: without idempotency an already-prefixed + // input becomes "nam:nam:abcdefg". + [Test] + public async Task Compose_AlreadyScopedInput_IsIdempotent() + { + var fromRaw = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "abcdefg"); + var fromScoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "nam:abcdefg"); + + await Assert.That(fromScoped.Value).IsEqualTo(fromRaw.Value) + .Because("Compose is the anti-double-prefix guardrail — a scoped input must produce the same Value as the equivalent raw input."); + } + + // Explicit-region-wins matches FriendshipIdNormalization.CanonicalizeForPrincipal + // and every persistence adapter's semantics. + [Test] + public async Task Compose_DifferentRegionPrefix_ArgumentRegionWins() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Eur, "nam:abcdefg"); + + using (Assert.Multiple()) + { + await Assert.That(scoped.Value).IsEqualTo("eur:abcdefg") + .Because("Compose treats the region argument as authoritative — matches FriendshipIdNormalization.CanonicalizeForPrincipal."); + await Assert.That(scoped.Region).IsEqualTo(ScyllaKeyspace.Eur); + } + } + + [Test] + public async Task Compose_FromSystemId_MatchesStringOverload() + { + var scopedFromString = ScopedSystemId.Compose(ScyllaKeyspace.Sam, "sam:xyz1234"); + var scopedFromSystemId = ScopedSystemId.Compose(ScyllaKeyspace.Sam, new("sam:xyz1234")); + + await Assert.That(scopedFromSystemId.Value).IsEqualTo(scopedFromString.Value) + .Because("The SystemId overload is a strict alias for the string overload — same idempotency, same output."); + } + + // Non-region prefixes (username:, discord:) are opaque so ScyllaUserRegistryRegionContext's + // discriminator-column routing keeps working. + [Test] + public async Task Compose_UnknownPrefixNotStripped() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "username:alice"); + + await Assert.That(scoped.Value).IsEqualTo("nam:username:alice") + .Because("Non-region prefixes are opaque to Compose — stripping them would silently corrupt the discriminator-column routing."); + } + + [Test] + public async Task Compose_BlankId_Throws() + { + await Assert.That(() => ScopedSystemId.Compose(ScyllaKeyspace.Nam, " ")) + .Throws() + .Because("A blank id is never a valid input — surface the caller bug at construction, not on the first byte written to the wire."); + } + + [Test] + public async Task Compose_OnlyPrefixNoRaw_Throws() + { + await Assert.That(() => ScopedSystemId.Compose(ScyllaKeyspace.Nam, "nam:")) + .Throws() + .Because("A bare region prefix with no raw id is a caller bug — never produce a scoped id whose raw component is empty."); + } + + [Test] + public async Task TryParseScoped_ValidScoped_ReturnsTrue() + { + var parsed = ScopedSystemId.TryParseScoped("eur:acct-42", out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(result.Value).IsEqualTo("eur:acct-42"); + await Assert.That(result.Region).IsEqualTo(ScyllaKeyspace.Eur); + await Assert.That(result.RawId).IsEqualTo("acct-42"); + } + } + + // Canonical wire form is lowercase (matches ScyllaKeyspace's JsonStringEnumMemberName). + [Test] + public async Task TryParseScoped_MixedCaseRegion_Normalises() + { + var parsed = ScopedSystemId.TryParseScoped("EUR:acct-42", out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(result.Value).IsEqualTo("eur:acct-42") + .Because("The wire form must be lowercase-canonical so downstream string comparisons in SystemTopic and ScyllaKeyspaceResolver don't spuriously miss."); + } + } + + [Test] + [Arguments((string?)null)] + [Arguments("")] + [Arguments(" ")] + [Arguments("abcdefg")] + [Arguments(":abcdefg")] + [Arguments("nam:")] + [Arguments("xxx:abcdefg")] + [Arguments("username:alice")] + public async Task TryParseScoped_InvalidInputs_ReturnsFalse(string? input) + { + var parsed = ScopedSystemId.TryParseScoped(input, out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsFalse() + .Because($"'{input ?? ""}' is not a valid scoped system id and TryParse must never throw or silently succeed for it."); + await Assert.That(result).IsEqualTo(default(ScopedSystemId)); + } + } + + [Test] + public async Task ParseScoped_InvalidInput_Throws() + { + await Assert.That(() => ScopedSystemId.ParseScoped("not-scoped")) + .Throws() + .Because("ParseScoped is the strict variant used at trust boundaries — unscoped input surfaces as a fail-fast rather than a silent default."); + } + + [Test] + public async Task IParsable_TryParse_DelegatesToTryParseScoped() + { + var parsed = ScopedSystemId.TryParse("nam:principal-a", provider: null, out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(result.Value).IsEqualTo("nam:principal-a"); + } + } + + [Test] + public async Task IParsable_Parse_ThrowsOnUnscoped() + { + await Assert.That(() => ScopedSystemId.Parse("no-scope", provider: null)) + .Throws(); + } + + // Byte-identical JSON with SystemId is the precondition for retyping + // CommandEnvelope.PrincipalId / ITargetedClusterEvent.TargetSystemId without migration. + [Test] + public async Task Json_RoundTripsThroughValue() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "principal-a"); + + var scopedJson = JsonSerializer.Serialize(scoped); + var systemIdJson = JsonSerializer.Serialize(new SystemId("nam:principal-a")); + + using (Assert.Multiple()) + { + await Assert.That(scopedJson).IsEqualTo("\"nam:principal-a\"") + .Because("The scoped Value must be emitted verbatim — anything else changes the on-wire shape for consumers."); + await Assert.That(scopedJson).IsEqualTo(systemIdJson) + .Because("The migration story depends on ScopedSystemId serialising byte-identically to SystemId — otherwise retyping breaks wire compatibility."); + + var reserialised = JsonSerializer.Deserialize(scopedJson); + await Assert.That(reserialised.Value).IsEqualTo(scoped.Value) + .Because("Deserialise-then-serialise must be a fixed point for anything we route through Kafka or a snapshot store."); + } + } + + [Test] + public async Task Json_DeserialiseUnscoped_Throws() + { + await Assert.That(() => JsonSerializer.Deserialize("\"unscoped\"")) + .Throws(); + } + + // Record-struct equality is what InProcessEventBus's TargetSystemId filter relies on. + [Test] + public async Task Equality_SameValue_Equal() + { + var a = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "abcdefg"); + var b = ScopedSystemId.Compose(ScyllaKeyspace.Nam, "nam:abcdefg"); + + using (Assert.Multiple()) + { + await Assert.That(a).IsEqualTo(b); + await Assert.That(a.GetHashCode()).IsEqualTo(b.GetHashCode()); + } + } + + [Test] + public async Task AsSystemId_PreservesValueByteExact() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Eur, "acct-42"); + var systemId = scoped.AsSystemId(); + + await Assert.That(systemId.Value).IsEqualTo(scoped.Value) + .Because("AsSystemId is the wire-boundary escape hatch — it must produce the identical byte string, not a re-parse."); + } + + // TryParseRegion delegates to EnumWire.TryParse; these tests catch a + // hand-rolled switch drifting from the JsonStringEnumMemberName attributes on the enum. + [Test] + public async Task TryParseScoped_EveryWireRegion_Parses() + { + foreach (var region in Enum.GetValues()) + { + var wire = EnumWire.ToWire(region); + var parsed = ScopedSystemId.TryParseScoped($"{wire}:abcdefg", out var result); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue() + .Because($"'{wire}:abcdefg' must parse — the wire tag came directly from EnumWire.ToWire so any TryParse mismatch indicates a hand-rolled table drifted from the enum."); + await Assert.That(result.Region).IsEqualTo(region); + } + } + } + + [Test] + [Arguments("NAM")] + [Arguments("Eur")] + [Arguments("gDpR")] + public async Task TryParseScoped_CaseInsensitiveRegion_Normalises(string mixedCase) + { + var parsed = ScopedSystemId.TryParseScoped($"{mixedCase}:abcdefg", out var result); + var expected = EnumWire.ToWire( + Enum.Parse(char.ToUpperInvariant(mixedCase[0]) + mixedCase[1..].ToLowerInvariant())); + + using (Assert.Multiple()) + { + await Assert.That(parsed).IsTrue(); + await Assert.That(result.Value).IsEqualTo($"{expected}:abcdefg") + .Because("Wire values must be lowercase-canonical regardless of client casing — this is what six downstream comparison sites assume."); + } + } + + // No blank-defaults-to-Nam fallback: TryParseRegion drops the tolerant behaviour + // that EnumWireExtensions.ParseScyllaKeyspace carries. + [Test] + [Arguments("xxx:abcdefg")] + [Arguments("dev:abcdefg")] + [Arguments("northamerica:abcdefg")] + public async Task TryParseScoped_UnknownRegion_ReturnsFalse(string wire) + { + var parsed = ScopedSystemId.TryParseScoped(wire, out _); + await Assert.That(parsed).IsFalse(); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Ids/SecretTokensFromFactoryTests.cs b/tests/unit/Interfold.Shared.UnitTests/Ids/SecretTokensFromFactoryTests.cs new file mode 100644 index 00000000..1b30fde0 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Ids/SecretTokensFromFactoryTests.cs @@ -0,0 +1,145 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Ids; + +// Pins Jti.From, LinkToken.From, and Jti.NewJti. The From pair folds "null-or-blank +// → null, otherwise wrap" into a total function so raw credentials only exist inside +// the factory frame. NewJti is the same story on the write side. +public sealed class SecretTokensFromFactoryTests +{ + [Test] + [Arguments((string?)null)] + [Arguments("")] + [Arguments(" ")] + [Arguments("\t")] + [Arguments(" ")] + public async Task JtiFrom_NullOrBlankInput_ReturnsNull(string? input) + { + var result = Jti.From(input); + + await Assert.That(result).IsNull() + .Because($"'{input ?? ""}' must not produce a wrapped Jti — the AuthController null-check depends on From returning null for missing / blank JWT claims so the BadRequest 'Token is missing JTI claim' path runs."); + } + + [Test] + public async Task JtiFrom_ValidInput_ReturnsWrappedValue() + { + const string valid = "1a2b3c4d-5e6f-7890-abcd-ef0123456789"; + + var result = Jti.From(valid); + + using (Assert.Multiple()) + { + await Assert.That(result).IsNotNull() + .Because("A non-blank JTI is a valid input and must wrap."); + await Assert.That(result!.Value.Value).IsEqualTo(valid) + .Because("From must not transform the input — the JTI is used verbatim as the revocation store key and any normalization would silently break logout invalidation across sessions."); + } + } + + [Test] + [Arguments((string?)null)] + [Arguments("")] + [Arguments(" ")] + [Arguments("\t")] + [Arguments(" ")] + public async Task LinkTokenFrom_NullOrBlankInput_ReturnsNull(string? input) + { + var result = LinkToken.From(input); + + await Assert.That(result).IsNull() + .Because($"'{input ?? ""}' must not produce a wrapped LinkToken — the AuthLinkController null-check depends on From returning null for the missing-query-AND-missing-cookie case so the 403 'invalid or expired' path runs instead of a phantom lookup."); + } + + [Test] + public async Task LinkTokenFrom_ValidInput_ReturnsWrappedValue() + { + const string valid = "97e6d3ab-cbfe-4a30-8f7e-6b2c8a3d9e1f"; + + var result = LinkToken.From(valid); + + using (Assert.Multiple()) + { + await Assert.That(result).IsNotNull() + .Because("A non-blank link token is a valid input and must wrap."); + await Assert.That(result!.Value.Value).IsEqualTo(valid) + .Because("From must not transform the input — the link token is used verbatim as the Scylla lookup key and any normalization would silently break the one-time-link handshake."); + } + } + + // Belt-and-braces: incidental $"{jti}" between From and RevokeTokenAsync must go + // through the redacting ToString. + [Test] + public async Task JtiFrom_WrappedValueRoundTripsThroughRedactedToString() + { + const string valid = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature"; + + var result = Jti.From(valid); + var interpolated = $"jti = {result?.ToString()}"; + + using (Assert.Multiple()) + { + await Assert.That(interpolated).DoesNotContain(valid) + .Because("The wrapped Jti must redact under interpolation — the whole point of routing through the From factory is to ensure downstream string uses of the wrapped credential are safe by default."); + await Assert.That(interpolated).StartsWith("jti = eyJh") + .Because("Redaction is the first-4-chars + ellipsis form defined by SecretRedaction.Redact — pins the exact contract so a future 'safer' variant that returns '' does not silently break log-parsing tools that grep for the prefix."); + } + } + + [Test] + public async Task LinkTokenFrom_WrappedValueRoundTripsThroughRedactedToString() + { + const string valid = "0123456789abcdef0123456789abcdef"; + + var result = LinkToken.From(valid); + var interpolated = $"link_token = {result?.ToString()}"; + + using (Assert.Multiple()) + { + await Assert.That(interpolated).DoesNotContain(valid) + .Because("The wrapped LinkToken must redact under interpolation — the AuthLinkController's cookie / redirect logs live in the same span as the From call and must not leak the token verbatim."); + await Assert.That(interpolated).StartsWith("link_token = 0123") + .Because("Pins the first-4-chars + ellipsis contract for LinkToken symmetrically with the Jti case."); + } + } + + [Test] + public async Task JtiNewJti_ProducesUniqueValueEachCall() + { + var a = Jti.NewJti(); + var b = Jti.NewJti(); + + await Assert.That(a.Value).IsNotEqualTo(b.Value) + .Because("Two NewJti() calls must produce distinct values — a collision would silently merge the revocation entries for two independently-issued tokens, so revoking one would revoke both without any warning."); + } + + // Wire format is Guid.NewGuid().ToString("N") — any drift orphans the revocation store. + [Test] + public async Task JtiNewJti_ProducesThirtyTwoCharLowercaseHex() + { + var jti = Jti.NewJti(); + + using (Assert.Multiple()) + { + await Assert.That(jti.Value.Length).IsEqualTo(32) + .Because("Guid.NewGuid().ToString(\"N\") yields exactly 32 characters — a change here would break byte-identity with every issued JTI and orphan the revocation store on the first issue after deploy."); + await Assert.That(jti.Value).Matches(@"^[0-9a-f]{32}$") + .Because("Only lowercase hex; no dashes, no uppercase. Any drift would break byte-identity with the historical shape."); + } + } + + [Test] + public async Task JtiNewJti_ValueRedactsUnderInterpolation() + { + var jti = Jti.NewJti(); + var interpolated = $"jti = {jti}"; + + using (Assert.Multiple()) + { + await Assert.That(interpolated).DoesNotContain(jti.Value) + .Because("The freshly-minted Jti must redact under interpolation — the AuthController mint site logs several operation-id + redirect messages between NewJti and the RecordTokenAsync call, none of which should ever leak the JTI verbatim."); + await Assert.That(interpolated).StartsWith($"jti = {jti.Value[..4]}") + .Because("Redaction preserves the first 4 chars (per SecretRedaction.Redact) so operators can still correlate log lines across the mint / verify / revoke lifecycle without the full JTI being exposed."); + } + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/InMemoryAccountRepositoryIdentityRegressionTests.cs b/tests/unit/Interfold.Shared.UnitTests/InMemoryAccountRepositoryIdentityRegressionTests.cs new file mode 100644 index 00000000..2c180093 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/InMemoryAccountRepositoryIdentityRegressionTests.cs @@ -0,0 +1,170 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.InMemory.Repository; +using Interfold.Api.UnitTests.Support; +using Interfold.Auth.Contracts.Ids; + +namespace Interfold.Api.UnitTests; + +// Regression suite for the OAuth-identity paths on InMemoryAccountRepository. The +// three per-provider FindOrCreate/Unlink bodies were collapsed onto a generic helper +// keyed by identity wrapper + extractor lambda; the isolation tests below pin the +// dict-pair-per-branch invariant so a wrong-dict swap goes noisy immediately. +public sealed class InMemoryAccountRepositoryIdentityRegressionTests +{ + private const string DiscordValue = "discord-user-123"; + private const string EmailValue = "user@example.com"; + private const string EmailValueUpper = "USER@EXAMPLE.COM"; + private const string AppleValue = "001234.deadbeef.5678"; + + + + private static InMemoryAccountRepository NewRepo() => + new(new FixedRegionContext(ScyllaKeyspace.Nam)); + + // LinkIdentityToUserAsync requires the user to have username/description/avatar/linkToken + // present before it accepts a link; seed a username first (mirrors the real onboarding order). + private static async Task ProvisionAllThreeIdentitiesAsync(InMemoryAccountRepository repo) + { + var systemId = await repo.FindOrCreateSystemIdAsync(ProviderIdentity.FromDiscord(new(DiscordValue))); + await Assert.That(systemId).IsNotNull() + .Because("Setup precondition: initial FindOrCreate via Discord must mint a SystemId."); + + await repo.UpdateUsernameAsync(systemId!.Value, new("test-user")); + await repo.LinkIdentityToUserAsync(systemId.Value, ProviderIdentity.FromGoogle(new(EmailValue))); + await repo.LinkIdentityToUserAsync(systemId.Value, ProviderIdentity.FromApple(new(AppleValue))); + + return systemId.Value; + } + + [Test] + public async Task FindOrCreateSystemIdAsync_DiscordMiss_AutoProvisionsAndSecondCallReturnsSameId() + { + var repo = NewRepo(); + var identity = ProviderIdentity.FromDiscord(new(DiscordValue)); + + var first = await repo.FindOrCreateSystemIdAsync(identity); + var second = await repo.FindOrCreateSystemIdAsync(identity); + + await Assert.That(first).IsNotNull() + .Because("A miss on a valid Discord id must auto-provision — this is the OAuth-login shape's core contract."); + await Assert.That(second).IsEqualTo(first) + .Because("A second FindOrCreate for the same Discord id must return the SAME SystemId — otherwise every login would mint a fresh phantom account and orphan the previous one."); + } + + [Test] + public async Task FindOrCreateSystemIdAsync_EmailMiss_AutoProvisionsAndSecondCallReturnsSameId() + { + var repo = NewRepo(); + var identity = ProviderIdentity.FromGoogle(new(EmailValue)); + + var first = await repo.FindOrCreateSystemIdAsync(identity); + var second = await repo.FindOrCreateSystemIdAsync(identity); + + await Assert.That(first).IsNotNull() + .Because("A miss on a valid Google email must auto-provision — Google-OAuth login relies on this branch."); + await Assert.That(second).IsEqualTo(first) + .Because("A second FindOrCreate for the same email must return the SAME SystemId — otherwise Google-OAuth users would double-provision on every login."); + } + + [Test] + public async Task FindOrCreateSystemIdAsync_AppleMiss_AutoProvisionsAndSecondCallReturnsSameId() + { + var repo = NewRepo(); + var identity = ProviderIdentity.FromApple(new(AppleValue)); + + var first = await repo.FindOrCreateSystemIdAsync(identity); + var second = await repo.FindOrCreateSystemIdAsync(identity); + + await Assert.That(first).IsNotNull() + .Because("A miss on a valid Apple id must auto-provision — Sign in with Apple relies on this branch."); + await Assert.That(second).IsEqualTo(first) + .Because("A second FindOrCreate for the same Apple id must return the SAME SystemId — otherwise Apple-OAuth users would double-provision on every login."); + } + + [Test] + public async Task FindOrCreateSystemIdAsync_EmailCasedDifferently_ReturnsSameSystemId() + { + var repo = NewRepo(); + var lower = await repo.FindOrCreateSystemIdAsync(ProviderIdentity.FromGoogle(new(EmailValue))); + var upper = await repo.FindOrCreateSystemIdAsync(ProviderIdentity.FromGoogle(new(EmailValueUpper))); + + await Assert.That(upper).IsEqualTo(lower) + .Because("The email reverse-map keys on StringComparer.OrdinalIgnoreCase — a caller submitting the same address in a different case must land on the same account. Generic-helper refactor risk: if the generic ever swapped the reverse-map for a case-sensitive dict, the two calls above would provision two separate accounts."); + } + + [Test] + public async Task UnlinkDiscordAsync_AfterAutoProvision_SubsequentTryFindReturnsNull() + { + var repo = NewRepo(); + var identity = ProviderIdentity.FromDiscord(new(DiscordValue)); + var systemId = await repo.FindOrCreateSystemIdAsync(identity); + await Assert.That(systemId).IsNotNull() + .Because("Setup: FindOrCreate must succeed so the subsequent unlink has something to remove."); + + var unlinked = await repo.UnlinkDiscordAsync(systemId!.Value); + await Assert.That(unlinked).IsTrue() + .Because("UnlinkDiscordAsync returns true on success — the return-shape contract holds regardless of whether the identifier was set."); + + var afterUnlink = await repo.TryFindSystemIdByDiscordIdAsync(new(DiscordValue)); + await Assert.That(afterUnlink).IsNull() + .Because("After Unlink, the reverse (raw discord id → SystemId) lookup must return null — otherwise the account is discoverable by a discord id it no longer claims."); + } + + // Unlink on a user with nothing linked must return true (idempotent) — parity with + // the Scylla adapter's contract that drives the settings-command flow's Accepted/Replay. + [Test] + public async Task UnlinkDiscordAsync_UserWithNothingLinked_ReturnsTrue() + { + var repo = NewRepo(); + var result = await repo.UnlinkDiscordAsync(new("nam:never-linked")); + + await Assert.That(result).IsTrue() + .Because("Unlink on a user with no linked Discord id must return true — parity with the Scylla adapter, which returns true for the same shape and drives the settings-command flow's Accepted/Replay envelope."); + } + + [Test] + public async Task UnlinkDiscordAsync_DoesNotAffectEmailOrAppleReverseMaps() + { + var repo = NewRepo(); + var systemId = await ProvisionAllThreeIdentitiesAsync(repo); + + var linked = await repo.UnlinkDiscordAsync(systemId); + await Assert.That(linked).IsTrue() + .Because("Setup: the unlink itself must succeed so the isolation assertion below is meaningful."); + + var profile = await repo.GetPublicProfileAsync(systemId); + await Assert.That(profile).IsNotNull() + .Because("The user still has a username, email, and apple identity — their public profile must survive an UnlinkDiscord that only targets one of three dict pairs."); + await Assert.That(profile!.DiscordId).IsNull() + .Because("UnlinkDiscord must scrub the discord reverse map."); + await Assert.That(profile.Email).IsNotNull() + .Because("UnlinkDiscord must NOT touch the email dict pair — this is the refactor risk (generic helper receiving the wrong dict pair) the test is pinned to catch."); + await Assert.That(profile.AppleId).IsNotNull() + .Because("UnlinkDiscord must NOT touch the apple dict pair — see the email pin above for the refactor-risk rationale."); + } + + [Test] + public async Task DeleteAsync_UserWithAllThreeIdentities_ScrubsAllThreeReverseMaps() + { + var repo = NewRepo(); + var systemId = await ProvisionAllThreeIdentitiesAsync(repo); + + var deleted = await repo.DeleteAsync(systemId); + await Assert.That(deleted).IsTrue() + .Because("DeleteAsync returns true on success — matches the Scylla adapter's contract."); + + // Discord has TryFind; email/apple only surface via a fresh FindOrCreate — same + // invariant: post-delete the account must be undiscoverable from every side. + await Assert.That(await repo.TryFindSystemIdByDiscordIdAsync(new(DiscordValue))).IsNull() + .Because("Delete must scrub the discord reverse-map — a fresh FindOrCreate by the same discord id must not resurrect the deleted account."); + + var freshFromEmail = await repo.FindOrCreateSystemIdAsync(ProviderIdentity.FromGoogle(new(EmailValue))); + await Assert.That(freshFromEmail).IsNotEqualTo(systemId) + .Because("Delete must scrub the email reverse-map. Otherwise a follow-up OAuth login with the same email would silently re-adopt the just-deleted SystemId instead of provisioning a fresh account."); + + var freshFromApple = await repo.FindOrCreateSystemIdAsync(ProviderIdentity.FromApple(new(AppleValue))); + await Assert.That(freshFromApple).IsNotEqualTo(systemId) + .Because("Delete must scrub the apple reverse-map. Symmetric to the email pin above — a follow-up Sign in with Apple would otherwise adopt the deleted SystemId."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Infrastructure/InMemoryRegionContextTests.cs b/tests/unit/Interfold.Shared.UnitTests/Infrastructure/InMemoryRegionContextTests.cs new file mode 100644 index 00000000..2515b027 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Infrastructure/InMemoryRegionContextTests.cs @@ -0,0 +1,90 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Infrastructure.InMemory; + +namespace Interfold.Api.UnitTests.Infrastructure; + +// InMemoryRegionContext strip-before-hash invariant: scoped {region}:{rawId} and raw +// {rawId} shapes of the same principal must resolve to the same region, otherwise +// InMemory reads through a raw URL segment 404 against writes seeded through a JWT- +// derived scoped principal. Persistent-backend equivalent lives in RegionContextCachingTests. +public sealed class InMemoryRegionContextTests +{ + private const string RawId = "sys-region-context-test"; + + [Test] + public async Task ScopedAndRawFormsOfSamePrincipal_ResolveToSameRegion() + { + var ctx = new InMemoryRegionContext(); + + var scoped = ctx.ResolveUserRegion(new($"nam:{RawId}")); + var raw = ctx.ResolveUserRegion(new(RawId)); + + await Assert.That(scoped).IsEqualTo(raw) + .Because("A scoped nam:{rawId} JWT-derived principal and a raw {rawId} route-bound principal must land in the same region — otherwise InMemoryStorageKeys.ForSystem yields two different partition keys for the same user and public reads 404 with system_not_found."); + } + + [Test] + public async Task CrossRegionSameRawId_ResolveToSameRegion() + { + var ctx = new InMemoryRegionContext(); + + var namScoped = ctx.ResolveUserRegion(new($"nam:{RawId}")); + var eurScoped = ctx.ResolveUserRegion(new($"eur:{RawId}")); + + await Assert.That(namScoped).IsEqualTo(eurScoped) + .Because("The region prefix is stripped, not gated on — a caller that happens to hold the id under a different region wire tag (rare but possible across the seven canonical regions) must not partition into a different physical bucket."); + } + + // Large sample so per-process hash randomisation can't produce an all-one-region run + // (P ≈ 4e-42 across 7 regions × 50 samples). + [Test] + public async Task DifferentPrincipals_ResolveAcrossMultipleRegions() + { + var ctx = new InMemoryRegionContext(); + + var observed = new HashSet(); + for (var i = 0; i < 50; i++) + { + observed.Add(ctx.ResolveUserRegion(new($"sys-discriminator-{i:D4}"))); + } + + await Assert.That(observed.Count).IsGreaterThan(1) + .Because("The resolver must still discriminate between principals — if it degenerates to a single region for every id (e.g. by hashing a constant, or by falling through to CurrentRegion for non-empty inputs), the InMemory backend loses its multi-region routing and every test sharing a WebApplicationFactory sees a single-partition collision surface."); + } + + // Blank inputs must short-circuit to CurrentRegion — ScyllaKeyspaceResolver relies on + // this default rather than throwing. All three IsNullOrWhiteSpace shapes are pinned so + // a refactor to a single guard clause can't drop one. + [Test] + public async Task DefaultSystemId_FallsBackToCurrentRegion() + { + var ctx = new InMemoryRegionContext(currentRegion: ScyllaKeyspace.Eur); + + var region = ctx.ResolveUserRegion(default); + + await Assert.That(region).IsEqualTo(ScyllaKeyspace.Eur) + .Because("default(SystemId).Value is null — this branch must fall through to the constructor-supplied CurrentRegion. The ScyllaKeyspaceResolver default-keyspace path depends on it and would NRE otherwise."); + } + + [Test] + public async Task EmptyInput_FallsBackToCurrentRegion() + { + var ctx = new InMemoryRegionContext(currentRegion: ScyllaKeyspace.Sam); + + var region = ctx.ResolveUserRegion(new(string.Empty)); + + await Assert.That(region).IsEqualTo(ScyllaKeyspace.Sam) + .Because("Empty id is the second IsNullOrWhiteSpace shape — pin all three (null-via-default, empty, whitespace) so a refactor to a single guard clause can't silently drop one branch."); + } + + [Test] + public async Task WhitespaceInput_FallsBackToCurrentRegion() + { + var ctx = new InMemoryRegionContext(currentRegion: ScyllaKeyspace.Sas); + + var region = ctx.ResolveUserRegion(new(" ")); + + await Assert.That(region).IsEqualTo(ScyllaKeyspace.Sas) + .Because("Whitespace-only id completes the IsNullOrWhiteSpace matrix."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Interfold.Shared.UnitTests.csproj b/tests/unit/Interfold.Shared.UnitTests/Interfold.Shared.UnitTests.csproj new file mode 100644 index 00000000..e4d52590 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Interfold.Shared.UnitTests.csproj @@ -0,0 +1,27 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Shared.UnitTests/ModelBinding/UnixSecondsModelBinderTests.cs b/tests/unit/Interfold.Shared.UnitTests/ModelBinding/UnixSecondsModelBinderTests.cs new file mode 100644 index 00000000..0b19f3fc --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/ModelBinding/UnixSecondsModelBinderTests.cs @@ -0,0 +1,191 @@ +using System.Globalization; +using System.Reflection; +using Interfold.Shared.Api.ModelBinding; +using Interfold.Shared.Api.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Routing; + +namespace Interfold.Api.UnitTests.ModelBinding; + +// UnixSecondsModelBinder wire behaviour: on success → ModelBindingResult.Success; on +// failure → exact attribute ErrorMessage + attribute ErrorCode stashed on HttpContext.Items +// for InvalidModelStateResponseFactory to lift into ErrorResponse.Code. Regressions +// silently downgrade Fronting month/between endpoints to generic `bad_request`. +public sealed class UnixSecondsModelBinderTests +{ + private const string EndAnchorField = "end_anchor"; + private const string EndAnchorMessage = "Invalid end anchor. Please pass a valid Unix timestamp."; + private const string EndAnchorCode = "invalid_end_anchor"; + private const string StartField = "start"; + private const string StartMessage = "Invalid start anchor. Please pass a valid Unix timestamp."; + private const string BetweenCode = "invalid_anchor"; + + [Test] + public async Task Bind_ValidUnixSeconds_ProducesSuccessResultWithParsedValue() + { + var ctx = BuildContext( + fieldName: EndAnchorField, + rawValue: "1700000000", + parameter: ResolveParameter(nameof(WithEndAnchor), EndAnchorField)); + + await new UnixSecondsModelBinder().BindModelAsync(ctx); + + await Assert.That(ctx.Result.IsModelSet).IsTrue(); + await Assert.That(ctx.Result.Model).IsEqualTo(new UnixSeconds(1_700_000_000L)); + await Assert.That(ctx.ModelState.ErrorCount).IsEqualTo(0); + await Assert.That(ctx.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(EndAnchorField)]).IsNull(); + } + + [Test] + public async Task Bind_UnparseableValue_EmitsAttributeMessageAndStashesErrorCode() + { + var ctx = BuildContext( + fieldName: EndAnchorField, + rawValue: "not-a-number", + parameter: ResolveParameter(nameof(WithEndAnchor), EndAnchorField)); + + await new UnixSecondsModelBinder().BindModelAsync(ctx); + + await Assert.That(ctx.Result.IsModelSet).IsFalse(); + await Assert.That(ctx.ModelState.ContainsKey(EndAnchorField)).IsTrue(); + + var error = ctx.ModelState[EndAnchorField]!.Errors[0].ErrorMessage; + await Assert.That(error).IsEqualTo(EndAnchorMessage); + + var stashed = ctx.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(EndAnchorField)] as string; + await Assert.That(stashed).IsEqualTo(EndAnchorCode); + } + + // Between wires two params to invalid_anchor with per-param messages; pin the start- + // side wiring in isolation from the end-side. + [Test] + public async Task Bind_BetweenStartField_UsesInvalidAnchorCodeFromAttribute() + { + var ctx = BuildContext( + fieldName: StartField, + rawValue: "abc", + parameter: ResolveParameter(nameof(WithBetweenAnchors), StartField)); + + await new UnixSecondsModelBinder().BindModelAsync(ctx); + + await Assert.That(ctx.Result.IsModelSet).IsFalse(); + await Assert.That(ctx.ModelState[StartField]!.Errors[0].ErrorMessage).IsEqualTo(StartMessage); + + var stashed = ctx.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(StartField)] as string; + await Assert.That(stashed).IsEqualTo(BetweenCode); + } + + // Fronting endpoints require these params; an absent query string must still surface + // as invalid_end_anchor / invalid_anchor rather than MVC's generic "value is required". + [Test] + public async Task Bind_MissingValue_EmitsAttributeMessageAndStashesErrorCodeToo() + { + var ctx = BuildContext( + fieldName: EndAnchorField, + rawValue: null, + parameter: ResolveParameter(nameof(WithEndAnchor), EndAnchorField)); + + await new UnixSecondsModelBinder().BindModelAsync(ctx); + + await Assert.That(ctx.Result.IsModelSet).IsFalse(); + await Assert.That(ctx.ModelState[EndAnchorField]!.Errors[0].ErrorMessage).IsEqualTo(EndAnchorMessage); + + var stashed = ctx.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey(EndAnchorField)] as string; + await Assert.That(stashed).IsEqualTo(EndAnchorCode); + } + + // Defensive fallback for a parameter without [UnixSecondsBinding] — still fails + // cleanly (registry routes response to `bad_request`). + [Test] + public async Task Bind_UnparseableValue_WithoutAttribute_StashesNothing() + { + var ctx = BuildContext( + fieldName: "raw", + rawValue: "nope", + parameter: ResolveParameter(nameof(WithoutAttribute), "raw")); + + await new UnixSecondsModelBinder().BindModelAsync(ctx); + + await Assert.That(ctx.Result.IsModelSet).IsFalse(); + await Assert.That(ctx.ModelState["raw"]!.Errors[0].ErrorMessage) + .IsEqualTo("The value 'nope' is not a valid Unix timestamp."); + + var stashed = ctx.HttpContext.Items[UnixSecondsBindingAttribute.ItemsKey("raw")]; + await Assert.That(stashed).IsNull(); + } + + // Builds the DefaultModelBindingContext MVC would present at request time. + private static DefaultModelBindingContext BuildContext( + string fieldName, + string? rawValue, + ParameterInfo parameter) + { + var metadata = new EmptyModelMetadataProvider().GetMetadataForParameter(parameter); + var httpContext = new DefaultHttpContext(); + + var actionDescriptor = new ActionDescriptor + { + Parameters = new List + { + new ControllerParameterDescriptor + { + Name = parameter.Name!, + ParameterInfo = parameter, + ParameterType = parameter.ParameterType, + } + } + }; + var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); + + var routeValues = new RouteValueDictionary(); + if (rawValue is not null) + { + routeValues[fieldName] = rawValue; + } + + return new DefaultModelBindingContext + { + ActionContext = actionContext, + ModelName = fieldName, + FieldName = fieldName, + ModelMetadata = metadata, + ModelState = new ModelStateDictionary(), + ValueProvider = new RouteValueProvider(BindingSource.Query, routeValues, CultureInfo.InvariantCulture), + }; + } + + private static ParameterInfo ResolveParameter(string methodName, string queryName) + { + var method = typeof(UnixSecondsModelBinderTests) + .GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)!; + return method.GetParameters().First(p => + { + var fromQuery = p.GetCustomAttribute(); + return fromQuery?.Name == queryName || p.Name == queryName; + }); + } + + // Mirrors FrontingController.Month's parameter shape. + private static void WithEndAnchor( + [FromQuery(Name = EndAnchorField)] + [UnixSecondsBinding(ErrorCode = EndAnchorCode, ErrorMessage = EndAnchorMessage)] + UnixSeconds endAnchor) + { _ = endAnchor; } + + // Mirrors FrontingController.Between's parameter shape. + private static void WithBetweenAnchors( + [FromQuery(Name = StartField)] + [UnixSecondsBinding(ErrorCode = BetweenCode, ErrorMessage = StartMessage)] + UnixSeconds startAnchor, + [FromQuery(Name = "end")] + [UnixSecondsBinding(ErrorCode = BetweenCode, ErrorMessage = "Invalid end anchor. Please pass a valid Unix timestamp.")] + UnixSeconds endAnchor) + { _ = startAnchor; _ = endAnchor; } + + private static void WithoutAttribute(UnixSeconds raw) + { _ = raw; } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Models/VisibilityLevelTests.cs b/tests/unit/Interfold.Shared.UnitTests/Models/VisibilityLevelTests.cs new file mode 100644 index 00000000..29dbb2cc --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Models/VisibilityLevelTests.cs @@ -0,0 +1,26 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Models; + +namespace Interfold.Api.UnitTests.Models; + +public sealed class VisibilityLevelTests +{ + [Test] + [Arguments(null)] + [Arguments((short)4)] + [Arguments((short)-1)] + public async Task FromStorage_NullOrUnknown_ReturnsPrivate(short? code) + { + await Assert.That(code.FromStorage()).IsEqualTo(VisibilityLevel.Private); + } + + [Test] + [Arguments((short)0, VisibilityLevel.Public)] + [Arguments((short)1, VisibilityLevel.FriendsOnly)] + [Arguments((short)2, VisibilityLevel.TrustedOnly)] + [Arguments((short)3, VisibilityLevel.Private)] + public async Task FromStorage_KnownCode_ReturnsMatchingLevel(short code, VisibilityLevel expected) + { + await Assert.That(code.FromStorage()).IsEqualTo(expected); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/ServiceDefaults/ResilienceHandlerMethodGateTests.cs b/tests/unit/Interfold.Shared.UnitTests/ServiceDefaults/ResilienceHandlerMethodGateTests.cs new file mode 100644 index 00000000..f413324d --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/ServiceDefaults/ResilienceHandlerMethodGateTests.cs @@ -0,0 +1,134 @@ +using System.Net; +using Interfold.ServiceDefaults; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Http.Resilience; + +namespace Interfold.Api.UnitTests.ServiceDefaults; + +// Pins Extensions.ConfigureResilienceForGetOnly — the GET-only gate around the standard +// Microsoft.Extensions.Http.Resilience pipeline. Guards against the duplicate-SP-import +// class of bug where Polly's default POST retries multiplied state-changing work. +public sealed class ResilienceHandlerMethodGateTests +{ + [Test] + public async Task GetReceiving503ThenOk_IsRetriedAndSucceeds() + { + var counter = new RequestCountingHandler( + (request, attempt) => attempt == 1 + ? new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) + : new HttpResponseMessage(HttpStatusCode.OK)); + + using var client = BuildHttpClient(counter); + + using var response = await client.GetAsync("https://example.test/probe", CancellationToken.None); + + using (Assert.Multiple()) + { + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK) + .Because("A GET with a transient 503 followed by a 200 should bubble up the eventual 200 — that's the point of retaining the standard pipeline for GETs."); + await Assert.That(counter.AttemptCount).IsGreaterThanOrEqualTo(2) + .Because("Polly must have made at least one retry attempt after the 503; otherwise the GET path has lost its safety net."); + } + } + + [Test] + public async Task PostReceiving503_IsNotRetried() + { + var counter = new RequestCountingHandler( + (request, attempt) => new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)); + + using var client = BuildHttpClient(counter); + + using var response = await client.PostAsync( + "https://example.test/mutation", + new StringContent("{}"), + CancellationToken.None); + + using (Assert.Multiple()) + { + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.ServiceUnavailable) + .Because("The first 503 must surface to the caller unchanged — Polly retries on POST were the proximate cause of the duplicate-SP-import bug."); + await Assert.That(counter.AttemptCount).IsEqualTo(1) + .Because("A POST must be sent exactly once. Any retry here means the GET-only gate has regressed and we're back to the duplicate-state risk."); + } + } + + [Test] + [Arguments("PUT")] + [Arguments("PATCH")] + [Arguments("DELETE")] + public async Task NonGetMutationVerbReceiving503_IsNotRetried(string methodName) + { + var counter = new RequestCountingHandler( + (request, attempt) => new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)); + + using var client = BuildHttpClient(counter); + + using var request = new HttpRequestMessage(new HttpMethod(methodName), "https://example.test/mutation"); + + using var response = await client.SendAsync(request, CancellationToken.None); + + using (Assert.Multiple()) + { + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.ServiceUnavailable) + .Because($"{methodName} responses must not be retried — the GET-only gate covers every non-idempotent verb, not just POST."); + await Assert.That(counter.AttemptCount).IsEqualTo(1) + .Because($"{methodName} must be sent exactly once. Retries on this verb create the same duplicate-state risk POST does."); + } + } + + // SamplingDuration must be >= 2 * AttemptTimeout or the host fails to start. + [Test] + public async Task TimeoutOptions_AreConfiguredToTheDocumentedCeilings() + { + var options = new HttpStandardResilienceOptions(); + Extensions.ConfigureResilienceForGetOnly(options); + + using (Assert.Multiple()) + { + await Assert.That(options.AttemptTimeout.Timeout).IsEqualTo(TimeSpan.FromSeconds(30)) + .Because("AttemptTimeout pins the per-attempt ceiling. Bumping it from the package default of 10 s is what made the SP import safe even without the async-job refactor."); + await Assert.That(options.TotalRequestTimeout.Timeout).IsEqualTo(TimeSpan.FromMinutes(2)) + .Because("TotalRequestTimeout caps the whole pipeline including any GET retries. 2 min is the operational ceiling we agreed on."); + await Assert.That(options.CircuitBreaker.SamplingDuration).IsGreaterThanOrEqualTo(TimeSpan.FromSeconds(60)) + .Because("Microsoft.Extensions.Http.Resilience validates SamplingDuration >= 2 * AttemptTimeout — with AttemptTimeout = 30 s this must be >= 60 s or the host fails to start."); + } + } + + private static HttpClient BuildHttpClient(HttpMessageHandler primaryHandler) + { + var services = new ServiceCollection(); + services.AddHttpClient("test") + .AddStandardResilienceHandler().Configure(Extensions.ConfigureResilienceForGetOnly); + + // ConfigurePrimaryHttpMessageHandler is the factory hook for the innermost handler + // — replacing the primary handler after the resilience handler wraps the test transport. + services.AddHttpClient("test") + .ConfigurePrimaryHttpMessageHandler(() => primaryHandler); + + var provider = services.BuildServiceProvider(); + var factory = provider.GetRequiredService(); + return factory.CreateClient("test"); + } + + private sealed class RequestCountingHandler : HttpMessageHandler + { + private readonly Func _responseFactory; + private int _attempts; + + public RequestCountingHandler(Func responseFactory) + { + _responseFactory = responseFactory; + } + + public int AttemptCount => Volatile.Read(ref _attempts); + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var attempt = Interlocked.Increment(ref _attempts); + var response = _responseFactory(request, attempt); + response.RequestMessage = request; + return Task.FromResult(response); + } + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Services/SecretsPreBuildLoaderPoolingTests.cs b/tests/unit/Interfold.Shared.UnitTests/Services/SecretsPreBuildLoaderPoolingTests.cs new file mode 100644 index 00000000..fc115a0f --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Services/SecretsPreBuildLoaderPoolingTests.cs @@ -0,0 +1,125 @@ +using Interfold.Api.Host.Services.Secrets; +using Npgsql; + +namespace Interfold.Api.UnitTests.Services; + +// Pins the pool-isolation contract for SecretsPreBuildLoader.WithDedicatedPoolIdentity. +// Regressions here re-expose either the "pool exhausted" (loader joined the app's 5-slot +// pool, or MaxPoolSize too small with no gate) or "too many clients already" (loader pool +// unbounded) integration-suite failures. +public sealed class SecretsPreBuildLoaderPoolingTests +{ + [Test] + public async Task WithDedicatedPoolIdentity_StampsLoaderApplicationName() + { + const string input = "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.ApplicationName).IsEqualTo(SecretsPreBuildLoader.LoaderApplicationName); + } + + [Test] + public async Task WithDedicatedPoolIdentity_OverridesPreExistingApplicationName() + { + const string input = + "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon;Application Name=some-other-app"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.ApplicationName).IsEqualTo(SecretsPreBuildLoader.LoaderApplicationName); + } + + [Test] + public async Task WithDedicatedPoolIdentity_StampsBoundedMaxPoolSize() + { + const string input = "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.MaxPoolSize).IsEqualTo(SecretsPreBuildLoader.LoaderMaxPoolSize); + } + + [Test] + public async Task WithDedicatedPoolIdentity_OverridesFixtureMaxPoolSize() + { + const string input = + "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon;Maximum Pool Size=5"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.MaxPoolSize).IsEqualTo(SecretsPreBuildLoader.LoaderMaxPoolSize); + } + + [Test] + public async Task WithDedicatedPoolIdentity_KeepsPoolingEnabled() + { + const string input = + "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon;Pooling=false"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.Pooling).IsTrue(); + } + + [Test] + public async Task WithDedicatedPoolIdentity_StampsLoaderTimeout() + { + const string input = + "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon;Timeout=15"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + await Assert.That(parsed.Timeout).IsEqualTo(SecretsPreBuildLoader.LoaderTimeoutSeconds); + } + + [Test] + public async Task WithDedicatedPoolIdentity_PreservesAllOtherKeywords() + { + const string input = + "Host=db.internal;Port=6543;Username=app;Password=super-secret;Database=octocon;" + + "SSL Mode=Require;Command Timeout=60"; + + var output = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + + var parsed = new NpgsqlConnectionStringBuilder(output); + using (Assert.Multiple()) + { + await Assert.That(parsed.Host).IsEqualTo("db.internal"); + await Assert.That(parsed.Port).IsEqualTo(6543); + await Assert.That(parsed.Username).IsEqualTo("app"); + await Assert.That(parsed.Password).IsEqualTo("super-secret"); + await Assert.That(parsed.Database).IsEqualTo("octocon"); + await Assert.That(parsed.SslMode).IsEqualTo(SslMode.Require); + await Assert.That(parsed.CommandTimeout).IsEqualTo(60); + await Assert.That(parsed.ApplicationName).IsEqualTo(SecretsPreBuildLoader.LoaderApplicationName); + await Assert.That(parsed.MaxPoolSize).IsEqualTo(SecretsPreBuildLoader.LoaderMaxPoolSize); + await Assert.That(parsed.Timeout).IsEqualTo(SecretsPreBuildLoader.LoaderTimeoutSeconds); + await Assert.That(parsed.Pooling).IsTrue(); + } + } + + [Test] + public async Task WithDedicatedPoolIdentity_IsIdempotent() + { + const string input = "Host=localhost;Port=5432;Username=app;Password=pw;Database=octocon"; + + var once = SecretsPreBuildLoader.WithDedicatedPoolIdentity(input); + var twice = SecretsPreBuildLoader.WithDedicatedPoolIdentity(once); + + var parsed = new NpgsqlConnectionStringBuilder(twice); + using (Assert.Multiple()) + { + await Assert.That(parsed.ApplicationName).IsEqualTo(SecretsPreBuildLoader.LoaderApplicationName); + await Assert.That(parsed.MaxPoolSize).IsEqualTo(SecretsPreBuildLoader.LoaderMaxPoolSize); + await Assert.That(parsed.Timeout).IsEqualTo(SecretsPreBuildLoader.LoaderTimeoutSeconds); + await Assert.That(parsed.Pooling).IsTrue(); + } + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Services/StartupProbeConfigurationProxyTests.cs b/tests/unit/Interfold.Shared.UnitTests/Services/StartupProbeConfigurationProxyTests.cs new file mode 100644 index 00000000..2a507931 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Services/StartupProbeConfigurationProxyTests.cs @@ -0,0 +1,105 @@ +using Interfold.Shared.Api.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Interfold.Api.UnitTests.Services; + +// Pins the disposal-safety contract StartupProbeConfigurationProxy enforces. If any +// break, Program.cs's startup probe silently resurrects the "probe SP disposes the +// shared ConfigurationManager" bug and controller integration tests fail with +// ObjectDisposedException at WebApplicationFactory.CreateClient(). +public sealed class StartupProbeConfigurationProxyTests +{ + // Core invariant: disposing an SP built off SwapInto leaves the underlying + // ConfigurationManager usable — WebApplicationFactory.ConfigureWebHost still needs + // to Add sources during builder.Build(). + [Test] + public async Task ProbeDispose_DoesNotDisposeUnderlyingConfigurationManager() + { + var configManager = new ConfigurationManager(); + configManager.AddInMemoryCollection(new Dictionary + { + ["probe:sentinel"] = "before-dispose", + }); + + var services = new ServiceCollection(); + services.AddSingleton(_ => configManager); + + var probeServices = StartupProbeConfigurationProxy.SwapInto(services, configManager); + using (var probe = probeServices.BuildServiceProvider(validateScopes: false)) + { + _ = probe.GetRequiredService(); + } + + ((IConfigurationBuilder)configManager).Add(new Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource + { + InitialData = new Dictionary { ["probe:sentinel"] = "after-dispose" }.ToList(), + }); + + await Assert.That(configManager["probe:sentinel"]).IsEqualTo("after-dispose") + .Because("The proxy MUST prevent probe SP disposal from tearing down the shared ConfigurationManager — otherwise this Add() would throw ObjectDisposedException, which is exactly the fault every integration test hit before this fix."); + } + + // Counter-example showing why the proxy is necessary: without swap, DI's factory + // registration captures the resolved IConfiguration and disposes it on tear-down. + // If this test ever starts passing, revisit whether the proxy is still needed. + [Test] + public async Task ProbeDispose_WithoutProxy_DoesDisposeUnderlyingConfigurationManager() + { + var configManager = new ConfigurationManager(); + var services = new ServiceCollection(); + services.AddSingleton(_ => configManager); + + using (var probe = services.BuildServiceProvider(validateScopes: false)) + { + _ = probe.GetRequiredService(); + } + + Exception? thrown = null; + try + { + ((IConfigurationBuilder)configManager).Add(new Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource()); + } + catch (ObjectDisposedException ex) + { + thrown = ex; + } + + await Assert.That(thrown).IsNotNull() + .Because("Framework-registered IConfiguration is factory-shape and captured for disposal — this is the exact ownership chain the proxy breaks. If .NET ever changes this, revisit whether the proxy is still needed."); + } + + // Reads through the proxy must route to the live provider stack — otherwise + // Program.cs's startup snapshots drift from what the real host binds moments later. + [Test] + public async Task Proxy_ForwardsReadsToUnderlyingConfiguration() + { + var configManager = new ConfigurationManager(); + configManager.AddInMemoryCollection(new Dictionary + { + ["probe:key"] = "live-value", + ["probe:nested:child"] = "42", + }); + + var proxy = new StartupProbeConfigurationProxy(configManager); + + using IDisposable _ = Assert.Multiple(); + await Assert.That(proxy["probe:key"]).IsEqualTo("live-value") + .Because("The proxy is a thin forwarder — every indexer read must see the same value the wrapped ConfigurationManager would return."); + await Assert.That(proxy.GetSection("probe:nested")["child"]).IsEqualTo("42") + .Because("Sub-section reads must also forward, otherwise options binders that walk .GetSection(...) chains would miss configured values in the probe SP."); + } + + // The proxy MUST NOT implement IDisposable — that's the mechanism that keeps DI's + // disposal-capture walker from taking ownership of the wrapped configuration. + [Test] + public async Task Proxy_DoesNotImplementIDisposable() + { + // Reflect over the type so this catches a refactor that removes `sealed` or + // pulls the type through a disposable base class. + var implements = typeof(IDisposable).IsAssignableFrom(typeof(StartupProbeConfigurationProxy)); + + await Assert.That(implements).IsFalse() + .Because("Adding IDisposable would put the proxy back in DI's disposal set — the entire point of the wrapper is to be invisible to that walker so the shared ConfigurationManager isn't taken down when the probe SP is torn down."); + } +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Support/FixedRegionContext.cs b/tests/unit/Interfold.Shared.UnitTests/Support/FixedRegionContext.cs new file mode 100644 index 00000000..17a48bc7 --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Support/FixedRegionContext.cs @@ -0,0 +1,11 @@ +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Shared.Domain.Abstractions; + +namespace Interfold.Api.UnitTests.Support; + +public sealed class FixedRegionContext(ScyllaKeyspace region) : IRegionContext +{ + public ScyllaKeyspace CurrentRegion { get; } = region; + public ScyllaKeyspace ResolveUserRegion(SystemId systemId) => CurrentRegion; +} diff --git a/tests/unit/Interfold.Shared.UnitTests/Support/SecretsSnapshotMock.cs b/tests/unit/Interfold.Shared.UnitTests/Support/SecretsSnapshotMock.cs new file mode 100644 index 00000000..dc11bf9f --- /dev/null +++ b/tests/unit/Interfold.Shared.UnitTests/Support/SecretsSnapshotMock.cs @@ -0,0 +1,41 @@ +using Interfold.Shared.Api.Services.Secrets; +using Interfold.Shared.Contracts.Secrets; +using Microsoft.Extensions.Options; + +namespace Interfold.Api.UnitTests.Support; + +// Shared ISecretsSnapshot mock builder for the PostConfigure test files under this +// folder. Loose-mode default of null on unconfigured Get(...) preserves the +// "missing row returns null" contract every PostConfigure test relies on. +public static class SecretsSnapshotMock +{ + // IsPopulated preset to true mirrors the "loader has run" invariant every + // PostConfigure test assumes. + public static Mock Empty() + { + var mock = ISecretsSnapshot.Mock(); + mock.IsPopulated.Returns(true); + return mock; + } + + public static Mock With(this Mock mock, SecretsStoreKey key, string value) + { + mock.Get(key).Returns(value); + return mock; + } + + // Not applicable to tests that call PostConfigure twice (idempotency guards), + // tests that expect it to throw (malformed-seed guards), or tests that resolve + // options via a full DI + IOptionsMonitor pipeline (missing-mandatory-row). + public static TOptions ApplyPostConfigure( + this Mock mock, + string? name = null) + where TPost : IPostConfigureOptions + where TOptions : class, new() + { + var patcher = (TPost)Activator.CreateInstance(typeof(TPost), mock.Object)!; + var options = new TOptions(); + patcher.PostConfigure(name ?? Microsoft.Extensions.Options.Options.DefaultName, options); + return options; + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/Helpers/LoopbackHttpClientTests.cs b/tests/unit/Interfold.Socket.UnitTests/Helpers/LoopbackHttpClientTests.cs new file mode 100644 index 00000000..0ba30ad6 --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Helpers/LoopbackHttpClientTests.cs @@ -0,0 +1,64 @@ +using Interfold.Socket.Api.Helpers; + +namespace Interfold.Api.UnitTests.Helpers; + +// LoopbackHttpClient.IsLoopbackHost — defence-in-depth gate stopping the WebSocket +// endpoint relay from silently downgrading TLS validation on a non-loopback dial-out. +public sealed class LoopbackHttpClientTests +{ + [Test] + public async Task IPv4Loopback_IsLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("https://127.0.0.1:5101/api/foo")); + await Assert.That(result).IsTrue() + .Because("127.0.0.1 is the canonical IPv4 loopback literal used by ResolveLoopbackBaseUri's wildcard rewrite."); + } + + [Test] + public async Task IPv4LoopbackEntireRange_IsLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("https://127.42.0.99:5101/api/foo")); + await Assert.That(result).IsTrue() + .Because("RFC 3493 / IPAddress.IsLoopback treats the full 127.0.0.0/8 block as loopback."); + } + + [Test] + public async Task IPv6Loopback_IsLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("https://[::1]:5101/api/foo")); + await Assert.That(result).IsTrue() + .Because("::1 is the IPv6 loopback literal — relevant when the helper resolves a dual-stack binding."); + } + + [Test] + public async Task LocalhostHostname_IsLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("http://localhost/api/foo")); + await Assert.That(result).IsTrue() + .Because("`localhost` is treated as loopback without DNS resolution so the TestServer fallback works."); + } + + [Test] + public async Task OperatorFacingHostname_IsNotLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("https://pineapple.local:5001/api/foo")); + await Assert.That(result).IsFalse() + .Because("Operator-facing hostnames are NOT loopback — the permissive validator must never run against them."); + } + + [Test] + public async Task LanIp_IsNotLoopback() + { + var result = LoopbackHttpClient.IsLoopbackHost(new Uri("http://192.168.1.5:5100/api/foo")); + await Assert.That(result).IsFalse() + .Because("Specific NIC binds are reachable but cross-host — TLS validation must remain strict for them."); + } + + [Test] + public async Task NullUri_Throws() + { + await Assert.That(static () => LoopbackHttpClient.IsLoopbackHost(uri: null!)) + .Throws() + .Because("The call site passes a parsed Uri; a null here would be a programming error, not a runtime input."); + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/Interfold.Socket.UnitTests.csproj b/tests/unit/Interfold.Socket.UnitTests/Interfold.Socket.UnitTests.csproj new file mode 100644 index 00000000..3527235d --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Interfold.Socket.UnitTests.csproj @@ -0,0 +1,25 @@ + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Socket.UnitTests/Socket/IsTokenSubjectAuthorizedForTopicTests.cs b/tests/unit/Interfold.Socket.UnitTests/Socket/IsTokenSubjectAuthorizedForTopicTests.cs new file mode 100644 index 00000000..3e953475 --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Socket/IsTokenSubjectAuthorizedForTopicTests.cs @@ -0,0 +1,99 @@ +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Api.Socket; + +namespace Interfold.Api.UnitTests.Socket; + +// WebSocketHandler.IsTokenSubjectAuthorizedForTopic — region-prefix-tolerant equality +// gating a phx_join against the token's sub claim. JWTs carry scoped {region}:{rawId} +// subs but socket topics stay raw system:{rawId}; without tolerance the gate would +// 401 valid joins the middleware / SystemTopic.IdMatches accept. Sub-side rejection +// matrix lives in ScopedSystemIdTests; this file pins the comparison contract itself. +public sealed class IsTokenSubjectAuthorizedForTopicTests +{ + private const string RawId = "sys-abcdef0123456789"; + + [Test] + public async Task ScopedSub_RawTopic_SameRawId_IsAuthorized() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + SystemId topic = new(RawId); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(scopedSub, topic); + + await Assert.That(authorized).IsTrue() + .Because("JWTs carry a scoped nam:sys-... sub while the socket topic stays raw — this is the exact shape that would 401 the loopback endpoint proxy path without strip-tolerant equality."); + } + + [Test] + public async Task ScopedSub_ScopedTopic_SameRawId_IsAuthorized() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + SystemId topic = new($"nam:{RawId}"); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(scopedSub, topic); + + await Assert.That(authorized).IsTrue() + .Because("Scoped-on-both-sides is the wire shape once every client sends fully-qualified topics — the topic-side StripRegionPrefix collapses onto RawId and equality holds."); + } + + [Test] + public async Task ScopedSub_DifferentRegionOnTopic_SameRawId_IsAuthorized() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + SystemId topic = new($"eur:{RawId}"); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(scopedSub, topic); + + await Assert.That(authorized).IsTrue() + .Because("Region is stripped from the topic side (sub side already carries an authoritative RawId). Enforcing region equality is not this helper's job — that's what the region-context lookup / persistence layer decides."); + } + + [Test] + public async Task ScopedSub_RawTopic_DifferentRawIds_IsRejected() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + SystemId topic = new("sys-someone-else"); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(scopedSub, topic); + + await Assert.That(authorized).IsFalse() + .Because("Sub's RawId and topic's stripped RawId must actually match — the strip on either side is a canonicalisation, not a wildcard."); + } + + [Test] + public async Task ScopedSub_ScopedTopic_DifferentRawIds_IsRejected() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + SystemId topic = new("nam:sys-someone-else"); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(scopedSub, topic); + + await Assert.That(authorized).IsFalse() + .Because("Same region prefix on the topic side must not mask a raw-id mismatch."); + } + + // Defence-in-depth null guards; upstream caller already returns + // InvalidSocketTokenSubject / UnauthorizedTopic before entering this helper. + + [Test] + public async Task NullScopedSub_IsRejected() + { + SystemId topic = new(RawId); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(tokenSubject: null, requestedSystemId: topic); + + await Assert.That(authorized).IsFalse() + .Because("Null scoped-sub can never authorise — the upstream gate treats a failed TryParseScoped as InvalidSocketTokenSubject and returns before we're called, and the defence-in-depth null guard here is what backs that upstream contract for any future caller."); + } + + [Test] + public async Task NullRequestedSystemId_IsRejected() + { + var scopedSub = ScopedSystemId.ParseScoped($"nam:{RawId}"); + + var authorized = WebSocketHandler.IsTokenSubjectAuthorizedForTopic(tokenSubject: scopedSub, requestedSystemId: null); + + await Assert.That(authorized).IsFalse() + .Because("A null requested id (non-system topic, or SystemTopic.TryParse returning false) must not authorise — mirror the sub-side null guard."); + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/Socket/PhxJoinPayloadPlatformToleranceTests.cs b/tests/unit/Interfold.Socket.UnitTests/Socket/PhxJoinPayloadPlatformToleranceTests.cs new file mode 100644 index 00000000..1ed5f132 --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Socket/PhxJoinPayloadPlatformToleranceTests.cs @@ -0,0 +1,113 @@ +using System.Text.Json; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Socket.Contracts; + +namespace Interfold.Api.UnitTests.Socket; + +// Join-side tolerance for PhxJoinPayload.Platform: an unknown wire spelling round-trips +// to null (never throws JsonException) and every sibling member survives. Regression +// guard for Api_UserSocketEndpoint_AllowsWebSocketUpgrade — pre-fix, platform="wasm" +// threw and the handler's outer catch substituted a defaulted payload, wiping the token +// and answering the join with Unauthorized. +public sealed class PhxJoinPayloadPlatformToleranceTests +{ + [Test] + [Arguments("wasm")] + [Arguments("WASM")] + [Arguments("desktop")] + [Arguments("")] + [Arguments(" ")] + public async Task Deserialize_WithUnknownPlatformString_ReturnsNullPlatform_AndPreservesSiblings(string platform) + { + var json = $$""" + { + "token": "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature", + "protocolVersion": "2.0.0", + "platform": "{{platform}}", + "isReconnect": true, + "forceBatch": false + } + """; + + var payload = JsonSerializer.Deserialize(json, SocketJson.Options); + + using (Assert.Multiple()) + { + await Assert.That(payload).IsNotNull().Because("Expected the deserialize to succeed even with an unknown platform value."); + await Assert.That(payload!.Platform).IsNull().Because($"Expected platform='{platform}' to round-trip to null (tolerant contract), not throw or map to a wrong-vocabulary member."); + await Assert.That(payload.Token).IsEqualTo(new SocketToken("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature")) + .Because("Regression: an unknown platform value must NOT wipe the sibling token — that was the exact regression this test guards against (Api_UserSocketEndpoint_AllowsWebSocketUpgrade)."); + await Assert.That(payload.ProtocolVersion).IsEqualTo("2.0.0").Because("Expected the sibling protocolVersion to survive the unknown-platform deserialize."); + await Assert.That(payload.IsReconnect).IsTrue().Because("Expected the sibling isReconnect to survive the unknown-platform deserialize."); + await Assert.That(payload.ForceBatch).IsFalse().Because("Expected the sibling forceBatch to survive the unknown-platform deserialize."); + } + } + + [Test] + [Arguments("android", ClientPlatform.Android)] + [Arguments("ANDROID", ClientPlatform.Android)] + [Arguments("ios", ClientPlatform.Ios)] + [Arguments("Ios", ClientPlatform.Ios)] + [Arguments("web", ClientPlatform.Web)] + public async Task Deserialize_WithKnownPlatformSpelling_MapsToEnumMember(string platform, ClientPlatform expected) + { + var json = $$""" + { + "token": "t", + "platform": "{{platform}}" + } + """; + + var payload = JsonSerializer.Deserialize(json, SocketJson.Options); + + await Assert.That(payload!.Platform).IsEqualTo(expected) + .Because($"Expected platform='{platform}' to map to {expected} — tolerant read must not weaken known wire spellings."); + } + + [Test] + public async Task Deserialize_WithMissingPlatform_LeavesPlatformNull() + { + var json = """ + { + "token": "t", + "isReconnect": false + } + """; + + var payload = JsonSerializer.Deserialize(json, SocketJson.Options); + + await Assert.That(payload!.Platform).IsNull() + .Because("Expected a missing platform member to stay null so the join branch treats it as 'not iOS' (legacy semantic)."); + } + + [Test] + public async Task Deserialize_WithNumericPlatform_ReturnsNull_WithoutThrowing() + { + var json = """ + { + "token": "t", + "platform": 42 + } + """; + + var payload = JsonSerializer.Deserialize(json, SocketJson.Options); + + await Assert.That(payload!.Platform).IsNull() + .Because("Expected a non-string platform token to be tolerated as null (belt-and-braces beyond the wasm case)."); + } + + [Test] + [Arguments(ClientPlatform.Android, "android")] + [Arguments(ClientPlatform.Ios, "ios")] + [Arguments(ClientPlatform.Web, "web")] + public async Task Serialize_KnownPlatform_EmitsCanonicalLowercaseWireSpelling(ClientPlatform value, string expectedWire) + { + var payload = new PhxJoinPayload { Token = new SocketToken("t"), Platform = value }; + + var json = JsonSerializer.Serialize(payload, SocketJson.Options); + + await Assert.That(json).Contains($"\"platform\":\"{expectedWire}\"") + .Because($"Expected known platform {value} to serialize as the canonical wire spelling '{expectedWire}'."); + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/Socket/ResolveLoopbackBaseUriTests.cs b/tests/unit/Interfold.Socket.UnitTests/Socket/ResolveLoopbackBaseUriTests.cs new file mode 100644 index 00000000..49125f4c --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Socket/ResolveLoopbackBaseUriTests.cs @@ -0,0 +1,156 @@ +using Interfold.Socket.Api.Socket; + +namespace Interfold.Api.UnitTests.Socket; + +// Guards against the pre-fix behaviour where the proxy dialed its self-call using the +// inbound request Scheme+Host (operator-facing hostname:port), which crashed in +// container topologies because the container couldn't reach that address. The helper +// resolves from Kestrel's actual bindings instead. +public sealed class ResolveLoopbackBaseUriTests +{ + // Fallback used under TestServer's no-op IServerAddressesFeature. Kestrel always + // reports its bindings in production so this branch is unreachable there. + private const string TestServerFallback = "http://localhost"; + + [Test] + public async Task NullAddresses_FallsBackToLocalhost() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(addresses: null); + await Assert.That(result).IsEqualTo(TestServerFallback) + .Because("Null Addresses should not crash the proxy — under TestServer's no-op feature the in-memory HttpClient routes by pipeline so the authority is cosmetic."); + } + + [Test] + public async Task EmptyAddresses_FallsBackToLocalhost() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(addresses: new List()); + await Assert.That(result).IsEqualTo(TestServerFallback) + .Because("An empty Addresses collection (TestServer's documented behaviour) should hit the same fallback as null."); + } + + [Test] + public async Task SingleHttpListener_LeftAsIs_WhenAlreadySpecific() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://127.0.0.1:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("A specific loopback binding has no wildcard to rewrite — the URL is forwarded unchanged."); + } + + [Test] + public async Task SingleHttpListener_TrimsTrailingSlash() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://127.0.0.1:5100/"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("Trailing slash on the base URL would produce '/api//systems' when concatenated with a leading-slash path."); + } + + // 0.0.0.0 is Kestrel's shape when bound via ASPNETCORE_HTTP_PORTS; the outbound + // request cannot dial 0.0.0.0 from inside the same process — rewrite to loopback. + [Test] + public async Task WildcardZeroes_AreRewrittenToLoopback() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://0.0.0.0:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("0.0.0.0 is a wildcard bind address — calling 0.0.0.0 from inside the same process is not portable; loopback is."); + } + + // [::] is the IPv6 wildcard Kestrel reports on dual-stack Linux; loopback rewrites + // to 127.0.0.1 (not [::1]) because the http transport prefers v4 by default. + [Test] + public async Task IPv6Wildcard_IsRewrittenToLoopback() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://[::]:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("[::] is the IPv6 wildcard — the loopback equivalent for self-calls is 127.0.0.1 (we don't try [::1] because the http transport prefers v4 by default)."); + } + + // Legacy HTTP.sys wildcard shapes; accepted defensively in case an operator's + // UseUrls("http://+:5100") reintroduces the original bug. + [Test] + public async Task PlusWildcard_IsRewrittenToLoopback() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://+:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("`+` is the strong-wildcard shape from the HTTP.sys era; covered defensively because UseUrls accepts it."); + } + + [Test] + public async Task StarWildcard_IsRewrittenToLoopback() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://*:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("`*` is the weak-wildcard shape from the HTTP.sys era; covered defensively because UseUrls accepts it."); + } + + // Prefer HTTPS to avoid the UseHttpsRedirection 308 round-trip; TLS validation + // against the local leaf cert is handled by LoopbackHttpClient's permissive validator + // (safe: dial target is loopback, cannot be MITM'd from outside the process). + [Test] + public async Task PrefersHttpsListener_OverHttp_WhenBothPresent() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri([ + "http://[::]:5100", + "https://[::]:5101", + ]); + await Assert.That(result).IsEqualTo("https://127.0.0.1:5101") + .Because("Dialing HTTPS directly avoids the 308 round-trip via UseHttpsRedirection; the loopback HttpClient's permissive validator handles TLS validation."); + } + + [Test] + public async Task PrefersHttpsListener_RegardlessOfOrdering() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri([ + "https://[::]:5101", + "http://[::]:5100", + ]); + await Assert.That(result).IsEqualTo("https://127.0.0.1:5101") + .Because("Preference must be based on scheme, not on the order Kestrel emits its bindings."); + } + + [Test] + public async Task FallsBackToHttp_WhenOnlyHttpAvailable() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://[::]:5100"]); + await Assert.That(result).IsEqualTo("http://127.0.0.1:5100") + .Because("HTTP-only topology is supported — the helper returns a loopback HTTP URL when no HTTPS listener is bound."); + } + + [Test] + public async Task SpecificBoundIp_IsLeftUntouched() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://192.168.1.5:5100"]); + await Assert.That(result).IsEqualTo("http://192.168.1.5:5100") + .Because("A specific NIC bind address is already reachable as-is — only wildcards (0.0.0.0 / [::] / + / *) get rewritten."); + } + + [Test] + public async Task LocalhostBinding_IsLeftUntouched() + { + var result = WebSocketHandler.ResolveLoopbackBaseUri(["http://localhost:5100"]); + await Assert.That(result).IsEqualTo("http://localhost:5100") + .Because("localhost is already loopback — no wildcard substitution needed."); + } + + // Original bug: proxy dialed the inbound operator-facing hostname:port. Pin that + // the helper NEVER returns that shape regardless of input. + [Test] + public async Task RegressionGuard_DoesNotReturnOperatorFacingHostname() + { + var deploymentTopology = new[] + { + "http://0.0.0.0:5100", + "https://0.0.0.0:5101", + }; + var result = WebSocketHandler.ResolveLoopbackBaseUri(deploymentTopology); + + using (Assert.Multiple()) + { + await Assert.That(result).DoesNotContain("pineapple.local") + .Because("Regression guard: the proxy must never return the inbound request's operator-facing hostname."); + await Assert.That(result).DoesNotContain(":5001") + .Because("Regression guard: the proxy must never return the host-side mapped port."); + await Assert.That(result).IsEqualTo("https://127.0.0.1:5101") + .Because("Expected the rewritten loopback HTTPS URL targeting the container-internal Kestrel TLS port — preferred over HTTP to avoid the HttpsRedirection 308."); + } + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/Socket/SocketTokenRedactionTests.cs b/tests/unit/Interfold.Socket.UnitTests/Socket/SocketTokenRedactionTests.cs new file mode 100644 index 00000000..c606c79d --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/Socket/SocketTokenRedactionTests.cs @@ -0,0 +1,68 @@ +using Interfold.Shared.Contracts.Ids; + +namespace Interfold.Api.UnitTests.Socket; + +// Pins SocketToken's redaction contract. ToString() returns "abcd…" so an accidental +// $"{token}" in any WebSocketHandler log statement never leaks the JWT; raw bytes only +// reachable via explicit .Value. +public sealed class SocketTokenRedactionTests +{ + private const string SampleToken = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature"; + + [Test] + public async Task ToString_OnPopulatedToken_RedactsToFirstFourCharsPlusEllipsis() + { + SocketToken token = new(SampleToken); + + await Assert.That(token.ToString()).IsEqualTo("eyJh…") + .Because("SocketToken.ToString() must redact to the first-4-chars + ellipsis form so log-shaped uses of the wrapper cannot leak the JWT. WebSocketHandler.cs threads the wrapper through every hop specifically to lean on this contract."); + } + + [Test] + public async Task InterpolatedInString_UsesRedactedForm_NotRawValue() + { + SocketToken token = new(SampleToken); + + var interpolated = $"socket token = {token}"; + + await Assert.That(interpolated).IsEqualTo("socket token = eyJh…") + .Because("String interpolation always calls ToString() — this asserts the redacted-by-default behaviour end-to-end so a caller can never accidentally leak the raw JWT via $\"{token}\"."); + await Assert.That(interpolated).DoesNotContain(SampleToken) + .Because("Belt-and-braces: the raw token bytes must not appear anywhere in an interpolated string. Guards against a future ToString() that returns 'redacted + value' or similar 'helpful' variants."); + } + + [Test] + public async Task Value_ReturnsRawToken_Verbatim() + { + SocketToken token = new(SampleToken); + + await Assert.That(token.Value).IsEqualTo(SampleToken) + .Because(".Value is the deliberate escape hatch — it must return the raw JWT unmodified so JwtSecurityTokenHandler can validate the signature and so the loopback endpoint proxy emits a valid Authorization: Bearer header."); + } + + [Test] + public async Task ToString_OnShortToken_CollapsesToSingleEllipsis() + { + // Values of length ≤ 4 have no safe prefix to expose; SecretRedaction returns "…". + SocketToken token = new("abc"); + + await Assert.That(token.ToString()).IsEqualTo("…") + .Because("Sub-5-char values collapse to a lone ellipsis so the redaction never accidentally reveals more than 4 characters of any secret."); + } + + [Test] + public async Task RecordStructEquality_ComparesRawValuesOrdinally() + { + SocketToken a = new(SampleToken); + SocketToken b = new(SampleToken); + SocketToken different = new(SampleToken + "-tampered"); + SocketToken caseVariant = new(SampleToken.ToUpperInvariant()); + + await Assert.That(a == b).IsTrue() + .Because("Two SocketToken wrappers over the same raw string must compare equal — this is the equality WebSocketHandler.cs uses to gate `payload.token == query.token` on phx_join."); + await Assert.That(a == different).IsFalse() + .Because("Any byte difference in the raw value must produce inequality — the join gate must not admit a payload whose token differs from the one that authenticated the upgrade."); + await Assert.That(a == caseVariant).IsFalse() + .Because("JWTs are case-sensitive by construction; the record-struct equality must be ordinal (not ordinal-ignore-case) so an uppercased/lowercased copy is treated as a different credential."); + } +} diff --git a/tests/unit/Interfold.Socket.UnitTests/WireByteFreezeTests.cs b/tests/unit/Interfold.Socket.UnitTests/WireByteFreezeTests.cs new file mode 100644 index 00000000..3b060336 --- /dev/null +++ b/tests/unit/Interfold.Socket.UnitTests/WireByteFreezeTests.cs @@ -0,0 +1,160 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Interfold.Alters.Contracts.Events; +using Interfold.Auth.Contracts.Configuration; +using Interfold.Auth.Domain; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.Infrastructure.Coordination; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain; +using Interfold.Socket.Api.Socket; + +namespace Interfold.Api.UnitTests; + +// Golden-byte guardrail on the scoped-id wire boundaries. Each fact below pins one +// wire boundary a stray ScopedSystemId.RawId swap would silently corrupt. If any +// fail, treat as a real wire-format break, do not "adjust the expected value". +public sealed class WireByteFreezeTests +{ + private const string CanonicalScoped = "nam:abcdefg"; + private const string CanonicalRaw = "abcdefg"; + + [Test] + public async Task DeriveKey_IsDeterministic_AndScopeSensitive() + { + const string pepper = "test-pepper"; + RecoveryCode recoveryCode = new("test-code"); + EncryptionSalt salt = new(Convert.ToBase64String(Encoding.UTF8.GetBytes("known-salt-16b!!"))); + + var first = EncryptionKey.DeriveKey(pepper, new(CanonicalScoped), recoveryCode, salt); + var second = EncryptionKey.DeriveKey(pepper, new(CanonicalScoped), recoveryCode, salt); + var rawOnly = EncryptionKey.DeriveKey(pepper, new(CanonicalRaw), recoveryCode, salt); + + await Assert.That(first).IsEqualTo(second) + .Because("DeriveKey must be deterministic — a diff between two calls with identical inputs means Argon2 params or salt handling drifted."); + await Assert.That(first).IsNotEqualTo(rawOnly) + .Because("Dropping the region prefix from the systemId argument must change the derived key — otherwise a caller could silently swap scoped for raw and orphan the recovery flow."); + await Assert.That(Convert.FromBase64String(first.Value).Length).IsEqualTo(32) + .Because("Argon2id hash_len is pinned at 32 in EncryptionKey.DeriveKey."); + } + + [Test] + public async Task CreateToken_EmitsScopedSubClaim() + { + var (privatePem, _) = GenerateEs256Pem(); + var authConfig = new AuthenticationConfiguration + { + JwtAuthority = "https://test.interfold.local", + JwtEs256PrivateKeyPem = privatePem, + }; + var now = DateTimeOffset.FromUnixTimeSeconds(1_700_000_000); + var expiresAt = now.AddDays(1); + Jti jti = new("golden-jti"); + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, CanonicalRaw); + + var token = AuthHelper.CreateToken(authConfig, expiresAt, now, jti, scoped.AsSystemId()); + + var payload = DecodeJwtPayload(token); + await Assert.That(payload.GetProperty("sub").GetString()).IsEqualTo(CanonicalScoped) + .Because("The JWT sub claim is a wire boundary; the middleware ParseScoped requires the region prefix and would 401 an unscoped emission."); + } + + [Test] + public async Task SystemTopic_ToWireString_EmitsScopedComposite() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, CanonicalRaw); + var topic = new SystemTopic(scoped.AsSystemId()); + + await Assert.That(topic.ToWireString()).IsEqualTo($"system:{CanonicalScoped}") + .Because("SystemTopic emits the scoped composite verbatim; any change to that prefix breaks Phoenix topic matching for existing sockets."); + } + + [Test] + public async Task InProcessEventBus_ScopedToScopedMatchDelivers() + { + using var bus = new InProcessEventBus(); + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); + + var scopedTarget = ScopedSystemId.Compose(ScyllaKeyspace.Nam, CanonicalRaw); + var subscriber = bus.SubscribeAsync( + targetSystemId: scopedTarget, + ct: cts.Token); + + var enumerator = subscriber.GetAsyncEnumerator(cts.Token); + + await bus.PublishAsync(new AlterCreatedEvent(scopedTarget, new(42)), cts.Token); + + var moved = await enumerator.MoveNextAsync(); + await Assert.That(moved).IsTrue() + .Because("A scoped-to-scoped compare must deliver — this is the single-region happy path every socket push takes, so a false-negative here would silently break every WebSocket push in the codebase."); + await Assert.That(enumerator.Current.TargetSystemId.Value).IsEqualTo(CanonicalScoped) + .Because("The delivered event must carry the scoped composite verbatim — the filter is match-only, not lossy on payload."); + + await enumerator.DisposeAsync(); + } + + [Test] + public async Task InProcessEventBus_CrossRegionScopedTargetsDoNotBleedAcross() + { + using var bus = new InProcessEventBus(); + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + + var namScoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, CanonicalRaw); + var eurScoped = ScopedSystemId.Compose(ScyllaKeyspace.Eur, CanonicalRaw); + + var subscriber = bus.SubscribeAsync( + targetSystemId: namScoped, + ct: cts.Token); + + var enumerator = subscriber.GetAsyncEnumerator(cts.Token); + + await bus.PublishAsync(new AlterCreatedEvent(eurScoped, new(42)), cts.Token); + + var moved = await enumerator.MoveNextAsync(); + await Assert.That(moved).IsFalse() + .Because("A NAM-scoped subscriber must not receive an EUR-scoped publish even when the raw ids match — a strip-then-compare shape would deliver this cross-region false positive."); + + await enumerator.DisposeAsync(); + } + + [Test] + public async Task ScopedSystemId_JsonRoundTrip_IsByteExact() + { + var scoped = ScopedSystemId.Compose(ScyllaKeyspace.Nam, CanonicalRaw); + + var json = JsonSerializer.Serialize(scoped); + var roundTripped = JsonSerializer.Deserialize(json); + + await Assert.That(json).IsEqualTo($"\"{CanonicalScoped}\"") + .Because("The converter must emit Value verbatim so the wire bytes match the plain SystemId serialisation."); + await Assert.That(roundTripped.Value).IsEqualTo(CanonicalScoped) + .Because("A round-trip must preserve Value exactly; a divergence here means the converter reserialised through RawId or an object shape."); + } + + private static (string PrivatePem, string PublicPem) GenerateEs256Pem() + { + using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256); + return (ecdsa.ExportECPrivateKeyPem(), ecdsa.ExportSubjectPublicKeyInfoPem()); + } + + private static JsonElement DecodeJwtPayload(string token) + { + var parts = token.Split('.'); + var payloadJson = Encoding.UTF8.GetString(Base64UrlDecode(parts[1])); + using var doc = JsonDocument.Parse(payloadJson); + return doc.RootElement.Clone(); + } + + private static byte[] Base64UrlDecode(string s) + { + var padded = s.Replace('-', '+').Replace('_', '/'); + switch (padded.Length % 4) + { + case 2: padded += "=="; break; + case 3: padded += "="; break; + } + return Convert.FromBase64String(padded); + } +} diff --git a/tests/unit/Interfold.Systems.UnitTests/Interfold.Systems.UnitTests.csproj b/tests/unit/Interfold.Systems.UnitTests/Interfold.Systems.UnitTests.csproj new file mode 100644 index 00000000..eea2344d --- /dev/null +++ b/tests/unit/Interfold.Systems.UnitTests/Interfold.Systems.UnitTests.csproj @@ -0,0 +1,27 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + diff --git a/tests/unit/Interfold.Tags.UnitTests/Interfold.Tags.UnitTests.csproj b/tests/unit/Interfold.Tags.UnitTests/Interfold.Tags.UnitTests.csproj new file mode 100644 index 00000000..1aaa0f6f --- /dev/null +++ b/tests/unit/Interfold.Tags.UnitTests/Interfold.Tags.UnitTests.csproj @@ -0,0 +1,28 @@ + + + + net10.0 + false + true + enable + enable + Interfold.Api.UnitTests + + + + + + + + + + + + + + + + + + diff --git a/tools/Interfold.Bootstrapper/Cli/BootstrapCommandExtensions.cs b/tools/Interfold.Bootstrapper/Cli/BootstrapCommandExtensions.cs new file mode 100644 index 00000000..20fef41e --- /dev/null +++ b/tools/Interfold.Bootstrapper/Cli/BootstrapCommandExtensions.cs @@ -0,0 +1,19 @@ +namespace Interfold.Bootstrapper.Cli; + +/// +/// Log-name mapping for the standalone CLI commands (the ones that are not orchestrated +/// bootstrap phases and therefore deliberately absent from BootstrapPhase). The +/// strings are frozen — they prefix every structured log line the command emits. +/// +internal static class BootstrapCommandExtensions +{ + public static string ToPhaseLogName(this BootstrapCommand command) => command switch + { + BootstrapCommand.Backup => "backup", + BootstrapCommand.Restore => "restore", + BootstrapCommand.UpdateImages => "update-images", + BootstrapCommand.InstallService => "install-service", + _ => throw new ArgumentOutOfRangeException( + nameof(command), command, "Only the standalone CLI commands carry a phase log name."), + }; +} diff --git a/tools/Interfold.Bootstrapper/Cli/BootstrapOptions.cs b/tools/Interfold.Bootstrapper/Cli/BootstrapOptions.cs new file mode 100644 index 00000000..4d5076a6 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Cli/BootstrapOptions.cs @@ -0,0 +1,119 @@ +namespace Interfold.Bootstrapper.Cli; + +/// +/// Process-wide invocation options resolved from the CLI. Phases receive this record and +/// dispatch based on the flags rather than reading global state. +/// +/// Which subcommand was invoked. +/// Path to interfold.bootstrap.json. If null, the bootstrapper prompts interactively (in tty mode). +/// Root directory for emitted artifacts (compose, certs, secrets). +/// If true, the prerequisites phase is bypassed. Useful for re-runs after Docker is installed. +/// If true, the secrets phase regenerates everything even if a secrets file already exists. +/// If true, the certificate phase regenerates the root CA and leaf cert. +/// If true, missing config values are an error rather than a prompt. +/// Hidden testability hook: exits 1 immediately after the named phase (e.g. after-secrets). +/// Hidden testability hook: emit phase=name status=skipped reason=... lines on stderr. +/// For : which DB to snapshot. One of postgres, scylla, all. Defaults to all. +/// For : optional CLI override for config.backup.retainCount. Null means "use the config value". +/// For : optional CLI override for config.backup.directory. Null means "use the config value (or default)". +/// For : when true, the installer runs systemctl enable --now interfold.service after writing the unit. Defaults to on the config. +/// For : when true, the installer runs systemctl enable --now interfold-backup.timer. Defaults to on the config. +/// For : override the systemd unit installation directory. Defaults to /etc/systemd/system. Tests point this at a temp dir to keep the host's units untouched. +/// For : override the installed bootstrapper binary path baked into interfold-backup.service. Defaults to the running binary's location. +/// For : when true, a failing health check triggers an inline against the pre-update backup archives instead of just printing the manual recipe. Defaults to on the config. +/// For : dangerous escape hatch that skips the pre-update backup. Off by default — a backup ALWAYS happens unless the operator explicitly asks otherwise (e.g. re-running an update after a fresh manual snapshot). +/// For : CLI override for . Empty means "use the config value (which defaults to every service)". +/// For : CLI override for . Null means "use the config value". +/// For : path to a specific pg_dump archive to restore. Mutually exclusive with for the postgres component. +/// For : path to a specific scylla .tar.gz archive to restore. Mutually exclusive with for the scylla component. +/// For : pick the newest archive by mtime under {backupRoot}/{component}/ for every component that wasn't explicitly named on the CLI. Only meaningful when at least one archive exists. +/// For : skip the interactive "this will wipe your data volumes" confirmation. Required in non-interactive mode; equivalent to typing "y" at the confirmation prompt in interactive mode. +/// If true (bootstrap only), re-open the interactive config editor seeded +/// from the existing interfold.bootstrap.json, persist on confirm, then continue the pipeline. +/// Incompatible with / redirected stdin. +public sealed record BootstrapOptions( + BootstrapCommand Command, + string? ConfigPath, + string OutputDir, + bool SkipPrereqs, + bool RotateSecrets, + bool RotateCerts, + bool NonInteractive, + string? FaultInject, + bool PrintPhaseStatus, + string BackupComponent = "all", + int? BackupRetainOverride = null, + string? BackupDirOverride = null, + bool EnableAutostart = false, + bool EnableBackupTimer = false, + string? SystemdUnitDir = null, + string? BinaryPathOverride = null, + bool AutoRestore = false, + bool SkipPreUpdateBackup = false, + string[]? UpdateServices = null, + int? HealthCheckTimeoutOverride = null, + string? RestorePostgresArchive = null, + string? RestoreScyllaArchive = null, + bool RestoreLatest = false, + bool RestoreForce = false, + bool Reconfigure = false); + +public enum BootstrapCommand +{ + Bootstrap, + Publish, + Up, + RotateSecrets, + RotateCerts, + /// + /// Read-only command: prints the root CA's path, SHA-256 fingerprint, expiry, and the + /// distribute/verify recipe to stdout. Does NOT regenerate the CA or touch any other + /// phase — Orchestrator short-circuits before prereqs/config/secrets. Operators run this + /// to fetch the fingerprint they need to broadcast out-of-band so end-user devices can + /// verify the cert they download from /.well-known/interfold-root-ca.crt. + /// + ShowTrust, + + /// + /// Snapshots the live Postgres + Scylla/Cassandra state to {outputDir}/backups/ + /// (or the operator-supplied directory). Idempotent and short-circuits before any + /// host-mutating phase (prereqs/config/secrets/certs/publish): only needs an + /// already-published compose stack + a populated secrets/secrets.json. Driven + /// either manually by the operator or unattended by the systemd timer installed via + /// . + /// + Backup, + + /// + /// Installs (and optionally enables) the systemd units that own the boot-up autostart + /// + scheduled-backup wiring. Writes interfold.service, + /// interfold-backup.service, and interfold-backup.timer to + /// /etc/systemd/system/, runs systemd-analyze verify on each, then + /// runs systemctl daemon-reload. The --enable-autostart and + /// --enable-backup-timer flags additionally systemctl enable --now + /// the relevant units. + /// + InstallService, + + /// + /// Runs a pre-update backup (unless ), + /// docker compose pulls new images, up -ds the stack, health-checks + /// Postgres + Scylla + the API, and either prints the manual restore recipe on + /// failure or (with ) invokes + /// inline against the archives captured in the same run. + /// Idempotent: on a no-image-change run it prunes old backups and exits without + /// recreating any container. + /// + UpdateImages, + + /// + /// Restores the database state from backup archives on disk. Postgres restores + /// via pg_restore --clean --if-exists against the live compose-exec + /// endpoint; Scylla restores by stopping the API/web tier, stopping the seed + /// container, streaming the tar.gz archive back in via docker cp -, and + /// starting the stack. Destructive — requires explicit + /// (or an interactive "y" prompt) to + /// proceed. + /// + Restore, +} diff --git a/tools/Interfold.Bootstrapper/Cli/PhaseFailureReasons.cs b/tools/Interfold.Bootstrapper/Cli/PhaseFailureReasons.cs new file mode 100644 index 00000000..61dc6618 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Cli/PhaseFailureReasons.cs @@ -0,0 +1,59 @@ +namespace Interfold.Bootstrapper.Cli; + +/// +/// The machine-readable reason tokens passed to / +/// . Operator-frozen: they land verbatim in the +/// structured phase log lines that operators grep/alert on, mirroring the role +/// ErrorCodes plays for the HTTP surface. +/// +internal static class PhaseFailureReasons +{ + // Environment / prerequisites + public const string NonLinuxHost = "non-linux-host"; + public const string NonRoot = "non-root"; + public const string UnsupportedDistro = "unsupported-distro"; + + // Config resolution + public const string MissingConfig = "missing-config"; + public const string MissingConfigNonInteractive = "missing-config-non-interactive"; + public const string MissingConfigNoTty = "missing-config-no-tty"; + public const string ReconfigureRequiresInteractive = "reconfigure-requires-interactive"; + + // Publish / launch + public const string ComposeNotEmitted = "compose-not-emitted"; + public const string NoComposeFile = "no-compose-file"; + public const string HealthTimeout = "health-timeout"; + + // Systemd install + public const string SystemdAnalyzeVerify = "systemd-analyze-verify"; + public const string InvalidCalendar = "invalid-calendar"; + public const string Systemctl = "systemctl"; + + // Backup / restore + public const string UnknownComponent = "unknown-component"; + public const string MissingSecrets = "missing-secrets"; + public const string InvalidRetain = "invalid-retain"; + public const string MissingAdminPassword = "missing-admin-password"; + public const string EmptyPostgresDump = "empty-postgres-dump"; + public const string NodetoolSnapshot = "nodetool-snapshot"; + public const string ResolveScyllaContainer = "resolve-scylla-container"; + public const string EmptyScyllaArchive = "empty-scylla-archive"; + public const string NoArchives = "no-archives"; + public const string ConfirmationRequired = "confirmation-required"; + public const string StopScylla = "stop-scylla"; + public const string CreateScyllaContainer = "create-scylla-container"; + public const string StartScylla = "start-scylla"; + + // Update-images + public const string PreUpdateBackupFailed = "pre-update-backup-failed"; + public const string PullFailed = "pull-failed"; + public const string UpFailed = "up-failed"; + public const string HealthCheckFailed = "health-check-failed"; + + /// Skip reasons (PhaseSkip) — informational, not failures. + public static class Skip + { + public const string AlreadyPresent = "already-present"; + public const string OperatorAborted = "operator-aborted"; + } +} diff --git a/tools/Interfold.Bootstrapper/Cli/PhaseLogger.cs b/tools/Interfold.Bootstrapper/Cli/PhaseLogger.cs new file mode 100644 index 00000000..b648a198 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Cli/PhaseLogger.cs @@ -0,0 +1,55 @@ +namespace Interfold.Bootstrapper.Cli; + +/// +/// Two-channel logger used by phases. +/// * Human prose goes to stdout. +/// * Machine-readable phase status lines go to stderr when is set. +/// Tests in Interfold.Bootstrapper.IntegrationTests read the stderr channel to assert phase +/// skip/run behavior without scraping prose logs. +/// +public sealed class PhaseLogger(BootstrapOptions options) +{ + public void Info(string message) => Console.WriteLine(message); + + public void Warn(string message) => Console.WriteLine($"[warn] {message}"); + + public void Error(string message) => Console.Error.WriteLine($"[error] {message}"); + + public void PhaseStart(string phase) + { + Console.WriteLine($"==> {phase}"); + EmitMachineStatus(phase, "start", reason: null); + } + + public void PhaseSkip(string phase, string reason) + { + Console.WriteLine($" {phase}: skipped ({reason})"); + EmitMachineStatus(phase, "skipped", reason); + } + + public void PhaseDone(string phase) + { + Console.WriteLine($" {phase}: done"); + EmitMachineStatus(phase, "done", reason: null); + } + + public void PhaseFail(string phase, string reason) + { + Console.WriteLine($" {phase}: FAILED — {reason}"); + EmitMachineStatus(phase, "failed", reason); + } + + private void EmitMachineStatus(string phase, string status, string? reason) + { + if (!options.PrintPhaseStatus) + { + return; + } + + // Single-line, space-separated, no quoting needed because reasons are short identifiers. + var line = reason is null + ? $"phase={phase} status={status}" + : $"phase={phase} status={status} reason={reason}"; + Console.Error.WriteLine(line); + } +} diff --git a/tools/Interfold.Bootstrapper/Cli/RootCli.cs b/tools/Interfold.Bootstrapper/Cli/RootCli.cs new file mode 100644 index 00000000..6e53e22c --- /dev/null +++ b/tools/Interfold.Bootstrapper/Cli/RootCli.cs @@ -0,0 +1,365 @@ +using System.CommandLine; +using Interfold.Bootstrapper.Phases; + +namespace Interfold.Bootstrapper.Cli; + +/// +/// Entry point and command tree for the bootstrapper CLI. +/// Commands: bootstrap (default), publish, up, rotate-secrets, +/// rotate-certs, show-trust, backup, install-service, +/// update-images, restore. +/// +public static class RootCli +{ + public static async Task RunAsync(string[] args) + { + var root = BuildRoot(); + var parseResult = root.Parse(args); + return await parseResult.InvokeAsync().ConfigureAwait(false); + } + + private static RootCommand BuildRoot() + { + var configOpt = new Option("--config", "-c") + { + Description = "Path to interfold.bootstrap.json. If omitted, the bootstrapper prompts interactively (tty only)." + }; + var outputDirOpt = new Option("--output-dir", "-o") + { + Description = "Root directory for emitted artifacts (compose, certs, secrets).", + DefaultValueFactory = _ => "./deploy" + }; + var skipPrereqsOpt = new Option("--skip-prereqs") + { + Description = "Skip the prerequisites phase (Docker/openssl install, AIO sysctl). Use on re-runs." + }; + var nonInteractiveOpt = new Option("--non-interactive") + { + Description = "Fail rather than prompt when config values are missing." + }; + // Bootstrap-only: not on AddSharedOptions so publish/up/rotate-* reject unknown flags. + var reconfigureOpt = new Option("--reconfigure") + { + Description = "Re-open the interactive config editor seeded from the existing interfold.bootstrap.json, then continue bootstrap." + }; + // Hidden testability flags - not surfaced in help but accepted by the parser. + var faultInjectOpt = new Option("--fault-inject") { Hidden = true }; + var printPhaseStatusOpt = new Option("--print-phase-status") { Hidden = true }; + + // --- backup-specific options --- + // Component selector: postgres = pg_dump only; scylla = nodetool snapshot only; + // all = both, in that order. Invalid values are caught by BackupPhase, not here, + // so the error reads "unknown component 'foo'" with the same shape every consumer + // sees rather than System.CommandLine's generic parse failure. + var backupComponentOpt = new Option("--component") + { + Description = "Which database to back up: 'postgres', 'scylla', or 'all'. Defaults to 'all'.", + DefaultValueFactory = _ => "all" + }; + var backupRetainOpt = new Option("--retain") + { + Description = "Override config.backup.retainCount for this run. Must be in [1..1000]." + }; + var backupDirOpt = new Option("--backup-dir") + { + Description = "Override config.backup.directory for this run. Must be an absolute path." + }; + + // --- install-service-specific options --- + // When the flag is omitted on the CLI the default falls back to the matching config + // value (BackupSection.AutostartServer / BackupSection.Enabled); we accept a tri-state + // via the System.CommandLine bool option's "specified" check on the parse result. + var enableAutostartOpt = new Option("--enable-autostart") + { + Description = "After writing units, run `systemctl enable --now interfold.service`. Overrides config.backup.autostartServer." + }; + var enableBackupTimerOpt = new Option("--enable-backup-timer") + { + Description = "After writing units, run `systemctl enable --now interfold-backup.timer`. Overrides config.backup.enabled." + }; + var systemdUnitDirOpt = new Option("--systemd-unit-dir") + { + Description = "Override the systemd unit installation directory (default: /etc/systemd/system). Used by integration tests." + }; + var binaryPathOpt = new Option("--binary-path") + { + Description = "Override the bootstrapper binary path baked into interfold-backup.service (default: AppContext.BaseDirectory/interfold-bootstrap)." + }; + + // --- update-images-specific options --- + // Rollback default is manual: --auto-restore opts into the destructive path. + // --skip-pre-update-backup is an escape hatch for operators who took a fresh + // manual snapshot immediately before running update-images and don't want the + // duplicate archive. + var autoRestoreOpt = new Option("--auto-restore") + { + Description = "On health-check failure, invoke `restore --force` against the pre-update backup instead of just printing the recipe. Overrides config.update.autoRestoreOnFailure." + }; + var skipPreUpdateBackupOpt = new Option("--skip-pre-update-backup") + { + Description = "DANGEROUS: skip the pre-update database backup. Use only when a fresh backup already exists on disk. Off by default." + }; + var updateServicesOpt = new Option("--service") + { + Description = "Compose services to pull + recreate. Repeatable; e.g. --service interfold-api --service octocon-web. Empty (the default) means every service.", + AllowMultipleArgumentsPerToken = true, + }; + var healthCheckTimeoutOpt = new Option("--health-check-timeout") + { + Description = "Override config.update.healthCheckTimeoutSeconds for this run. Bounded [1..3600]." + }; + + // --- restore-specific options --- + // Two path selectors + a mtime resolver + a --force to skip the confirmation + // prompt. Restoring nothing (all three archive selectors omitted with --restore-latest + // false) is caught by RestorePhase, not here. + var restorePostgresOpt = new Option("--restore-postgres") + { + Description = "Path to a specific pg_dump archive (custom format) to restore. Mutually exclusive with --restore-latest for the postgres component." + }; + var restoreScyllaOpt = new Option("--restore-scylla") + { + Description = "Path to a specific scylla .tar.gz archive to restore. Mutually exclusive with --restore-latest for the scylla component." + }; + var restoreLatestOpt = new Option("--restore-latest") + { + Description = "Pick the newest archive by mtime under {backupRoot}/{component}/ for every component that wasn't explicitly named on the CLI." + }; + var restoreForceOpt = new Option("--force") + { + Description = "Skip the interactive 'this will wipe your data volumes' confirmation. Required in non-interactive mode." + }; + + var root = new RootCommand( + "Interfold self-hosting bootstrapper. Brings a fresh Linux box from 'git clone' to a running stack."); + + // ---------- bootstrap (default) ---------- + var bootstrapCmd = new Command("bootstrap", + "Run all phases: prereqs -> config -> secrets -> certs -> publish -> launch."); + AddSharedOptions(bootstrapCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + bootstrapCmd.Options.Add(reconfigureOpt); + bootstrapCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Bootstrap, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + reconfigureOpt: reconfigureOpt)); + root.Subcommands.Add(bootstrapCmd); + + // ---------- publish (compose-only, no docker compose up) ---------- + var publishCmd = new Command("publish", + "Run config + secrets + certs + compose-publish without launching the stack."); + AddSharedOptions(publishCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + publishCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Publish, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct)); + root.Subcommands.Add(publishCmd); + + // ---------- up (launch only against existing compose) ---------- + var upCmd = new Command("up", + "Launch an already-generated compose stack: docker compose up -d + health wait."); + AddSharedOptions(upCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + upCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Up, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct)); + root.Subcommands.Add(upCmd); + + // ---------- rotate-secrets ---------- + var rotateSecretsCmd = new Command("rotate-secrets", + "Regenerate DB/admin passwords, encryption keypair, and pepper. Re-emits compose + restarts API."); + AddSharedOptions(rotateSecretsCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + rotateSecretsCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.RotateSecrets, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: true, rotateCerts: false, ct)); + root.Subcommands.Add(rotateSecretsCmd); + + // ---------- rotate-certs ---------- + var rotateCertsCmd = new Command("rotate-certs", + "Regenerate root CA + leaf cert. Re-installs into the system trust store."); + AddSharedOptions(rotateCertsCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + rotateCertsCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.RotateCerts, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: true, ct)); + root.Subcommands.Add(rotateCertsCmd); + + // ---------- show-trust ---------- + var showTrustCmd = new Command("show-trust", + "Print the root CA path, SHA-256 fingerprint, expiry, and fetch/verify snippet. Read-only."); + AddSharedOptions(showTrustCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + showTrustCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.ShowTrust, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct)); + root.Subcommands.Add(showTrustCmd); + + // ---------- backup ---------- + var backupCmd = new Command("backup", + "Snapshot Postgres + Scylla/Cassandra to {outputDir}/backups/ and prune older copies. Idempotent."); + AddSharedOptions(backupCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + backupCmd.Options.Add(backupComponentOpt); + backupCmd.Options.Add(backupRetainOpt); + backupCmd.Options.Add(backupDirOpt); + backupCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Backup, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + backupComponentOpt: backupComponentOpt, + backupRetainOpt: backupRetainOpt, + backupDirOpt: backupDirOpt)); + root.Subcommands.Add(backupCmd); + + // ---------- install-service ---------- + var installServiceCmd = new Command("install-service", + "Install systemd units for boot-up autostart + scheduled backups. Writes to /etc/systemd/system/."); + AddSharedOptions(installServiceCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + installServiceCmd.Options.Add(enableAutostartOpt); + installServiceCmd.Options.Add(enableBackupTimerOpt); + installServiceCmd.Options.Add(systemdUnitDirOpt); + installServiceCmd.Options.Add(binaryPathOpt); + installServiceCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.InstallService, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + enableAutostartOpt: enableAutostartOpt, + enableBackupTimerOpt: enableBackupTimerOpt, + systemdUnitDirOpt: systemdUnitDirOpt, + binaryPathOpt: binaryPathOpt)); + root.Subcommands.Add(installServiceCmd); + + // ---------- update-images ---------- + var updateImagesCmd = new Command("update-images", + "Backup DB, `docker compose pull`, `up -d`, health-check, then either succeed, print restore recipe on failure, or `--auto-restore`."); + AddSharedOptions(updateImagesCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + updateImagesCmd.Options.Add(autoRestoreOpt); + updateImagesCmd.Options.Add(skipPreUpdateBackupOpt); + updateImagesCmd.Options.Add(updateServicesOpt); + updateImagesCmd.Options.Add(healthCheckTimeoutOpt); + updateImagesCmd.Options.Add(backupDirOpt); + updateImagesCmd.Options.Add(backupRetainOpt); + updateImagesCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.UpdateImages, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + backupRetainOpt: backupRetainOpt, + backupDirOpt: backupDirOpt, + autoRestoreOpt: autoRestoreOpt, + skipPreUpdateBackupOpt: skipPreUpdateBackupOpt, + updateServicesOpt: updateServicesOpt, + healthCheckTimeoutOpt: healthCheckTimeoutOpt)); + root.Subcommands.Add(updateImagesCmd); + + // ---------- restore ---------- + var restoreCmd = new Command("restore", + "Restore DB state from backup archives. Destructive — requires --force in non-interactive mode."); + AddSharedOptions(restoreCmd, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + restoreCmd.Options.Add(restorePostgresOpt); + restoreCmd.Options.Add(restoreScyllaOpt); + restoreCmd.Options.Add(restoreLatestOpt); + restoreCmd.Options.Add(restoreForceOpt); + restoreCmd.Options.Add(backupDirOpt); + restoreCmd.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Restore, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + backupDirOpt: backupDirOpt, + restorePostgresOpt: restorePostgresOpt, + restoreScyllaOpt: restoreScyllaOpt, + restoreLatestOpt: restoreLatestOpt, + restoreForceOpt: restoreForceOpt)); + root.Subcommands.Add(restoreCmd); + + // No subcommand -> default to `bootstrap`. + AddSharedOptions(root, configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt); + root.Options.Add(reconfigureOpt); + root.SetAction((parse, ct) => InvokeAsync(BootstrapCommand.Bootstrap, parse, + configOpt, outputDirOpt, skipPrereqsOpt, nonInteractiveOpt, faultInjectOpt, printPhaseStatusOpt, + rotateSecrets: false, rotateCerts: false, ct, + reconfigureOpt: reconfigureOpt)); + + return root; + } + + private static void AddSharedOptions( + Command target, + Option configOpt, + Option outputDirOpt, + Option skipPrereqsOpt, + Option nonInteractiveOpt, + Option faultInjectOpt, + Option printPhaseStatusOpt) + { + target.Options.Add(configOpt); + target.Options.Add(outputDirOpt); + target.Options.Add(skipPrereqsOpt); + target.Options.Add(nonInteractiveOpt); + target.Options.Add(faultInjectOpt); + target.Options.Add(printPhaseStatusOpt); + } + + private static async Task InvokeAsync( + BootstrapCommand command, + ParseResult parse, + Option configOpt, + Option outputDirOpt, + Option skipPrereqsOpt, + Option nonInteractiveOpt, + Option faultInjectOpt, + Option printPhaseStatusOpt, + bool rotateSecrets, + bool rotateCerts, + CancellationToken ct, + Option? backupComponentOpt = null, + Option? backupRetainOpt = null, + Option? backupDirOpt = null, + Option? enableAutostartOpt = null, + Option? enableBackupTimerOpt = null, + Option? systemdUnitDirOpt = null, + Option? binaryPathOpt = null, + Option? autoRestoreOpt = null, + Option? skipPreUpdateBackupOpt = null, + Option? updateServicesOpt = null, + Option? healthCheckTimeoutOpt = null, + Option? restorePostgresOpt = null, + Option? restoreScyllaOpt = null, + Option? restoreLatestOpt = null, + Option? restoreForceOpt = null, + Option? reconfigureOpt = null) + { + var options = new BootstrapOptions( + Command: command, + ConfigPath: parse.GetValue(configOpt), + OutputDir: Path.GetFullPath(parse.GetValue(outputDirOpt) ?? "./deploy"), + SkipPrereqs: parse.GetValue(skipPrereqsOpt), + RotateSecrets: rotateSecrets, + RotateCerts: rotateCerts, + NonInteractive: parse.GetValue(nonInteractiveOpt), + FaultInject: parse.GetValue(faultInjectOpt), + PrintPhaseStatus: parse.GetValue(printPhaseStatusOpt), + BackupComponent: backupComponentOpt is null ? "all" : (parse.GetValue(backupComponentOpt) ?? "all"), + BackupRetainOverride: backupRetainOpt is null ? null : parse.GetValue(backupRetainOpt), + BackupDirOverride: backupDirOpt is null ? null : parse.GetValue(backupDirOpt), + EnableAutostart: enableAutostartOpt is not null && parse.GetValue(enableAutostartOpt), + EnableBackupTimer: enableBackupTimerOpt is not null && parse.GetValue(enableBackupTimerOpt), + SystemdUnitDir: systemdUnitDirOpt is null ? null : parse.GetValue(systemdUnitDirOpt), + BinaryPathOverride: binaryPathOpt is null ? null : parse.GetValue(binaryPathOpt), + AutoRestore: autoRestoreOpt is not null && parse.GetValue(autoRestoreOpt), + SkipPreUpdateBackup: skipPreUpdateBackupOpt is not null && parse.GetValue(skipPreUpdateBackupOpt), + UpdateServices: updateServicesOpt is null ? null : parse.GetValue(updateServicesOpt), + HealthCheckTimeoutOverride: healthCheckTimeoutOpt is null ? null : parse.GetValue(healthCheckTimeoutOpt), + RestorePostgresArchive: restorePostgresOpt is null ? null : parse.GetValue(restorePostgresOpt), + RestoreScyllaArchive: restoreScyllaOpt is null ? null : parse.GetValue(restoreScyllaOpt), + RestoreLatest: restoreLatestOpt is not null && parse.GetValue(restoreLatestOpt), + RestoreForce: restoreForceOpt is not null && parse.GetValue(restoreForceOpt), + Reconfigure: reconfigureOpt is not null && parse.GetValue(reconfigureOpt)); + + var logger = new PhaseLogger(options); + + try + { + return await Orchestrator.RunAsync(options, logger, ct).ConfigureAwait(false); + } + catch (OperationCanceledException) when (ct.IsCancellationRequested) + { + logger.Error("Cancelled."); + return 130; + } + catch (Exception ex) + { + logger.Error(ex.Message); + return 1; + } + } +} diff --git a/tools/Interfold.Bootstrapper/Configuration/BootstrapConfig.cs b/tools/Interfold.Bootstrapper/Configuration/BootstrapConfig.cs new file mode 100644 index 00000000..7b8e789a --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/BootstrapConfig.cs @@ -0,0 +1,358 @@ +using System.Text.Json.Serialization; +using Interfold.Settings.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.Configuration; + +/// Operator-supplied configuration for interfold.bootstrap.json. Holds +/// everything that cannot be generated automatically (hosts, OAuth secrets, ports, etc.). +public sealed class BootstrapConfig +{ + [JsonPropertyName("deployment")] + public DeploymentSection Deployment { get; set; } = new(); + + [JsonPropertyName("ports")] + public PortsSection Ports { get; set; } = new(); + + /// Database stack: single (one Scylla node), multi (7-region + /// cluster), or cassandra. Translated by PublishPhase into the orthogonal + /// AppHost include-scylla / include-cassandra / scylla-topology. + [JsonPropertyName("databaseMode")] + public DatabaseMode DatabaseMode { get; set; } = DatabaseMode.Single; + + /// Pre-built API image reference. The bootstrapper does not build from source. + [JsonPropertyName("apiImage")] + public string ApiImage { get; set; } = "ghcr.io/azyyyyyy/interfold-api:latest"; + + /// Postgres application database name. Must satisfy Postgres' 63-byte + /// NAMEDATALEN budget and match [A-Za-z_][A-Za-z0-9_]*. + [JsonPropertyName("postgresDatabase")] + public string PostgresDatabase { get; set; } = "interfold"; + + /// Cluster identity advertised by both CQL backends. Pure metadata — no + /// keyspace/table contract attached. Validation rejects characters that would break + /// Scylla's argv or Cassandra's cassandra.yaml rewrite (quotes, newlines, control chars). + [JsonPropertyName("clusterName")] + public string ClusterName { get; set; } = "InterfoldCluster"; + + /// Per-instance region / keyspace identity. Also drives new-account routing. + /// In multi mode operators pick which of the seven regional keyspaces this stack serves. + [JsonPropertyName("scyllaKeyspace")] + public ScyllaKeyspace ScyllaKeyspace { get; set; } = ScyllaKeyspace.Nam; + + [JsonPropertyName("apiRuntime")] + public ApiRuntimeSection ApiRuntime { get; set; } = new(); + + /// DB retry + fan-out tuning. Defaults match the API's compile-time + /// fallbacks so a fresh bootstrap ships identical behaviour to unset env vars. + [JsonPropertyName("persistence")] + public PersistenceTuningSection Persistence { get; set; } = new(); + + /// Node role / process-group identity. Single-field section today; kept + /// separate so future cluster-shaped knobs have a home. + [JsonPropertyName("cluster")] + public ClusterSection Cluster { get; set; } = new(); + + /// Avatar storage. Blank → + /// {outputDir}/data/avatars bind-mounted at /app/data/avatars. + [JsonPropertyName("storage")] + public StorageSection Storage { get; set; } = new(); + + /// OTLP exporter configuration. Empty → API skips exporter registration. + [JsonPropertyName("observability")] + public ObservabilitySection Observability { get; set; } = new(); + + /// WebSocket batching. Null → API's compile-time default; JSON persists + /// literal null so a re-bootstrap doesn't set the threshold to 0. + [JsonPropertyName("socket")] + public SocketSection Socket { get; set; } = new(); + + [JsonPropertyName("oauth")] + public OAuthSection OAuth { get; set; } = new(); + + /// Backup + autostart preferences for the backup / install-service + /// subcommands. Defaults are "do nothing automatically". + [JsonPropertyName("backup")] + public BackupSection Backup { get; set; } = new(); + + /// Image-update preferences for the update-images subcommand. Defaults + /// are "manual only" — the CLI subcommand always works, but the backup → update systemd + /// chain requires + re-running install-service. + [JsonPropertyName("update")] + public UpdateSection Update { get; set; } = new(); + + /// Firebase / FCM inputs. Empty path = skip that platform (matching + /// internal.secrets row stays absent → API 503s the platform's config endpoint + /// or falls back to NullFCMService for send). + [JsonPropertyName("firebase")] + public FirebaseSection Firebase { get; set; } = new(); +} + +/// Non-secret API runtime config. Every field maps 1:1 to an OCTOCON_* +/// env var; three of the four are derivable from + +/// — +/// fills empties, operator-supplied values win. +public sealed class ApiRuntimeSection +{ + /// OAuth callback base URL (OCTOCON_AUTH_CALLBACK_BASE_URL). Derived + /// as https://{primary host}[:{Ports.apiHttps}] when empty — always https + /// because the API's Kestrel binds the leaf PFX unconditionally. + [JsonPropertyName("callbackBaseUrl")] + public string CallbackBaseUrl { get; set; } = string.Empty; + + /// JWT iss claim (OCTOCON_JWT_AUTHORITY). Derived the same + /// as — the API is usually its own issuer. + [JsonPropertyName("jwtAuthority")] + public string JwtAuthority { get; set; } = string.Empty; + + /// JWT aud claim (OCTOCON_JWT_AUDIENCE). Defaults to octocon + /// to match the API's compile-time fallback. + [JsonPropertyName("jwtAudience")] + public string JwtAudience { get; set; } = "octocon"; + + /// CORS allow-list (OCTOCON_CORS_ALLOWED_ORIGINS, comma-joined). + /// Empty falls back to "allow any origin" in the API — production foot-gun; derived + /// per non-CIDR host with the appropriate {scheme}://{host}[:port] when unset. + [JsonPropertyName("corsAllowedOrigins")] + public List CorsAllowedOrigins { get; set; } = []; +} + +public sealed class DeploymentSection +{ + [JsonPropertyName("outputDir")] + public string OutputDir { get; set; } = "./deploy"; + + /// Hosts the deployed API will be reachable at. Each entry is a DNS name + /// (wildcards allowed, collapse to their suffix in the root CA name constraints), + /// an IPv4/IPv6 literal, or a CIDR block (CIDR restricts permittedSubtrees but is + /// not eligible as the primary host and does not land in the leaf SAN). Default is + /// intentionally empty — non-interactive bootstrap fails fast rather than issue a + /// cert for a placeholder. Interactive mode pre-fills via + /// . + [JsonPropertyName("hosts")] + public List Hosts { get; set; } = []; + + [JsonPropertyName("rootCaName")] + public string RootCaName { get; set; } = "Interfold Root CA"; + + [JsonPropertyName("certYears")] + public int CertYears { get; set; } = 5; + + [JsonPropertyName("trustStoreInstall")] + public bool TrustStoreInstall { get; set; } = true; + + /// Ship the octocon-web wasm UI container. Independent of + /// — HTTP-only is valid (external TLS terminator). Setting + /// forces the container in regardless. + [JsonPropertyName("includeWeb")] + public bool IncludeWeb { get; set; } = false; + + /// Terminate HTTPS at octocon-web (nginx envsubst template + leaf + /// PFX bind-mount). Setting true implies — TLS termination + /// requires the container that performs it. + [JsonPropertyName("webHttps")] + public bool WebHttps { get; set; } = false; +} + +public sealed class PortsSection +{ + [JsonPropertyName("apiHttp")] + public int ApiHttp { get; set; } = 5000; + + [JsonPropertyName("apiHttps")] + public int ApiHttps { get; set; } = 5001; + + [JsonPropertyName("webHttp")] + public int WebHttp { get; set; } = 8080; + + [JsonPropertyName("webHttps")] + public int WebHttps { get; set; } = 8081; + + /// Host port for Postgres. AppHost maps this to Ports:postgres and + /// the derived API connection string. + [JsonPropertyName("postgres")] + public int Postgres { get; set; } = 4200; + + /// Host port for the CQL backend (Scylla or lone Cassandra). + [JsonPropertyName("scylla")] + public int Scylla { get; set; } = 9042; +} + +/// Per-provider OAuth credentials. Blank client ID disables the provider even +/// if a secret is set (the scheme is only registered when the ID is non-empty). +public sealed class OAuthSection +{ + [JsonPropertyName("googleClientId")] + public string GoogleClientId { get; set; } = string.Empty; + + [JsonPropertyName("googleClientSecret")] + public string GoogleClientSecret { get; set; } = string.Empty; + + [JsonPropertyName("discordClientId")] + public string DiscordClientId { get; set; } = string.Empty; + + [JsonPropertyName("discordClientSecret")] + public string DiscordClientSecret { get; set; } = string.Empty; + + [JsonPropertyName("appleClientId")] + public string AppleClientId { get; set; } = string.Empty; + + /// Apple's client secret is a short-lived JWT derived from the team key — + /// operators pre-minting one outside the bootstrapper paste it here. + [JsonPropertyName("appleClientSecret")] + public string AppleClientSecret { get; set; } = string.Empty; +} + +/// DB retry + fan-out tuning. Bounds enforced by +/// . +public sealed class PersistenceTuningSection +{ + /// OCTOCON_DB_RETRY_ATTEMPTS. Bounded 1..100 (0 would silently disable retries). + [JsonPropertyName("dbRetryAttempts")] + public int DbRetryAttempts { get; set; } = 3; + + /// OCTOCON_DB_RETRY_INITIAL_DELAY_MS. Bounded 1..60000. + [JsonPropertyName("dbRetryInitialDelayMs")] + public int DbRetryInitialDelayMs { get; set; } = 100; + + /// OCTOCON_DB_RETRY_MAX_DELAY_MS. Bounded 1..600000 and must be + /// >= (Validate rejects the swap). + [JsonPropertyName("dbRetryMaxDelayMs")] + public int DbRetryMaxDelayMs { get; set; } = 1500; + + /// OCTOCON_HYDRATION_MAX_CONCURRENCY. Bounded 1..1024. + [JsonPropertyName("hydrationMaxConcurrency")] + public int HydrationMaxConcurrency { get; set; } = 8; +} + +public sealed class ClusterSection +{ + /// OCTOCON_NODE_GROUP: primary / auxiliary / sidecar. + /// Fly.io overrides via FLY_PROCESS_GROUP at runtime. + [JsonPropertyName("nodeGroup")] + public NodeGroup NodeGroup { get; set; } = NodeGroup.Auxiliary; +} + +/// Local avatar storage. Host path is bind-mounted into the API container. +public sealed class StorageSection +{ + /// Host-side absolute directory bind-mounted at /app/data/avatars. + /// Empty → {outputDir}/data/avatars. The container always sees + /// OCTOCON_AVATAR_STORAGE_ROOT=/app/data/avatars. + [JsonPropertyName("avatarStorageRoot")] + public string AvatarStorageRoot { get; set; } = string.Empty; + + /// Public URL prefix (OCTOCON_AVATAR_PUBLIC_BASE). Non-empty must + /// parse as an absolute http(s) URL. Blank → API serves /avatars/* directly. + [JsonPropertyName("avatarPublicBase")] + public string AvatarPublicBase { get; set; } = string.Empty; +} + +public sealed class ObservabilitySection +{ + /// OTLP gRPC endpoint (OCTOCON_OTLP_ENDPOINT). Non-empty must parse + /// as absolute http(s); other schemes are rejected. + [JsonPropertyName("otlpEndpoint")] + public string OtlpEndpoint { get; set; } = string.Empty; +} + +public sealed class SocketSection +{ + /// Bytes threshold that flushes batched WS payloads + /// (OCTOCON_SOCKET_BATCH_BYTES_THRESHOLD). Bounded 1..16777216 when set; + /// null → API compile-time default. + [JsonPropertyName("batchBytesThreshold")] + public int? BatchBytesThreshold { get; set; } +} + +/// Backup + autostart preferences. All fields default to "do nothing +/// automatically"; opt in explicitly then re-run install-service. +public sealed class BackupSection +{ + /// Master toggle for the scheduled-backup systemd timer. The one-shot + /// backup subcommand always works regardless. + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = false; + + /// systemd OnCalendar= value. Validated shallowly here; + /// systemd-analyze calendar catches syntax errors at install time. + [JsonPropertyName("schedule")] + public string Schedule { get; set; } = "daily"; + + /// Archives to keep per component. Bounded 1..1000; 0 would delete each + /// backup immediately. + [JsonPropertyName("retainCount")] + public int RetainCount { get; set; } = 14; + + /// Blank → {outputDir}/backups. Non-empty values must be absolute — + /// systemd-timer CWDs are unpredictable. + [JsonPropertyName("directory")] + public string Directory { get; set; } = string.Empty; + + /// Install & enable interfold.service so docker compose up -d + /// runs on boot. Independent of . + [JsonPropertyName("autostartServer")] + public bool AutostartServer { get; set; } = false; +} + +/// Image-update preferences. Defaults to "manual only" — flipping +/// and re-running install-service materialises the systemd chain +/// that fires update-images after each successful scheduled backup. +public sealed class UpdateSection +{ + /// Master toggle for the backup → update systemd chain. The one-shot + /// update-images subcommand always works regardless. + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = false; + + /// Bounded 1..3600. Ceiling on the post-up -d health-check wait + /// before either printing the manual restore recipe or (with ) + /// invoking . + [JsonPropertyName("healthCheckTimeoutSeconds")] + public int HealthCheckTimeoutSeconds { get; set; } = 180; + + /// Config-side equivalent of the CLI --auto-restore. Default false + /// because rollback is destructive. + [JsonPropertyName("autoRestoreOnFailure")] + public bool AutoRestoreOnFailure { get; set; } = false; + + /// False → docker compose restart instead of up -d. Escape + /// hatch for two-step manual recreate; note pull-only won't run the new image until + /// a subsequent up -d. + [JsonPropertyName("recreateOnUpdate")] + public bool RecreateOnUpdate { get; set; } = true; + + /// Compose services to pull + recreate. Empty = every service. Entries + /// must match the AppHost's canonical service names; validator rejects typos. + [JsonPropertyName("services")] + public string[] Services { get; set; } = []; +} + +/// Firebase inputs. Empty path = skip that platform. Paths (not inline blobs) so +/// stock deploy dirs don't accumulate multi-line secrets in interfold.bootstrap.json — +/// operators point at 0600-mode files under secrets/firebase/ or wherever their +/// key-management story lives. +public sealed class FirebaseSection +{ + /// Firebase Android google-services.json. Reshaped into + /// and + /// seeded as firebase:client:android. + [JsonPropertyName("androidConfigPath")] + public string AndroidConfigPath { get; set; } = string.Empty; + + /// iOS GoogleService-Info.plist. Parsed as XML and reshaped into + /// for + /// firebase:client:ios. + [JsonPropertyName("iosConfigPath")] + public string IosConfigPath { get; set; } = string.Empty; + + /// Web-config JSON from the Firebase console (must already include the + /// vapidKey from Cloud Messaging → Web Push certificates). Seeded verbatim. + [JsonPropertyName("webConfigPath")] + public string WebConfigPath { get; set; } = string.Empty; + + /// FCM v1 service-account credential JSON. Seeded verbatim as + /// fcm:service_account_json. Protect on disk (0600). Empty → NullFCMService. + [JsonPropertyName("serviceAccountPath")] + public string ServiceAccountPath { get; set; } = string.Empty; +} diff --git a/tools/Interfold.Bootstrapper/Configuration/BootstrapJsonContext.cs b/tools/Interfold.Bootstrapper/Configuration/BootstrapJsonContext.cs new file mode 100644 index 00000000..960f01b9 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/BootstrapJsonContext.cs @@ -0,0 +1,19 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Bootstrapper.Configuration; + +/// +/// Source-generated for the bootstrapper's +/// JSON shapes. Keeps the bootstrapper trim-friendly: PublishTrimmed (and +/// JsonSerializerIsReflectionEnabledByDefault=false) disable reflection STJ, so every +/// serialize/deserialize path needs a context. Firebase shapes live in FirebaseJsonContext. +/// +[JsonSourceGenerationOptions( + WriteIndented = true, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.Never)] +[JsonSerializable(typeof(BootstrapConfig))] +[JsonSerializable(typeof(GeneratedSecrets))] +public sealed partial class BootstrapJsonContext : JsonSerializerContext +{ +} diff --git a/tools/Interfold.Bootstrapper/Configuration/FirebaseFolderScanner.cs b/tools/Interfold.Bootstrapper/Configuration/FirebaseFolderScanner.cs new file mode 100644 index 00000000..9f730505 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/FirebaseFolderScanner.cs @@ -0,0 +1,161 @@ +using System.Text.Json; + +namespace Interfold.Bootstrapper.Configuration; + +/// +/// Pure static scanner powering the "auto-detect from folder" branch of the interactive +/// Firebase wizard in . Given a folder full of +/// operator-downloaded Firebase artefacts, resolves each artefact to its matching +/// path so the operator doesn't have to type four paths +/// by hand. +/// +/// +/// Recognition rules per platform: +/// +/// +/// Android — file literally named google-services.json. +/// iOS — file literally named GoogleService-Info.plist. +/// Web — file literally named firebase-web-config.json. +/// Service account — glob *-firebase-adminsdk-*.json first (matches +/// Google's downloaded default {project-id}-firebase-adminsdk-{hash}.json); +/// if no glob hit, iterate every remaining *.json and pick the first one +/// whose root has "type": "service_account". Non-parseable / non-matching +/// files are skipped silently. +/// +/// +/// +/// Every resolved path is stamped as absolute so the persisted +/// interfold.bootstrap.json is stable regardless of how Deployment.OutputDir +/// later moves. Kept free of any Spectre / IAnsiConsole dependency so it drives +/// straight from unit tests with a temp directory. +/// +/// +public static class FirebaseFolderScanner +{ + private const string AndroidFileName = "google-services.json"; + private const string IosFileName = "GoogleService-Info.plist"; + private const string WebFileName = "firebase-web-config.json"; + private const string ServiceAccountGlob = "*-firebase-adminsdk-*.json"; + + /// + /// Platform label used in the list + /// for the Android input. + /// + public const string AndroidPlatform = "android"; + + /// + /// Platform label used in the list + /// for the iOS input. + /// + public const string IosPlatform = "ios"; + + /// + /// Platform label used in the list + /// for the web input. + /// + public const string WebPlatform = "web"; + + /// + /// Platform label used in the list + /// for the FCM v1 service-account credential. + /// + public const string ServiceAccountPlatform = "service_account"; + + /// + /// Scans for the four Firebase artefacts and returns + /// resolved absolute paths for every one it finds, plus a list of platform labels + /// (in the fixed order android, ios, web, service_account) for the ones it didn't. + /// The returned can be written verbatim onto + /// . + /// + /// Thrown when + /// does not resolve to an existing directory. The interactive wizard validates + /// existence in its prompt so operators normally never see this — it's a fallback + /// for programmatic callers that skip the prompt validation. + public static FirebaseFolderScanResult Scan(string folder) + { + if (!Directory.Exists(folder)) + { + throw new DirectoryNotFoundException( + $"Firebase folder '{folder}' does not exist. Provide the directory containing " + + "google-services.json / GoogleService-Info.plist / firebase-web-config.json / " + + "the FCM service-account JSON."); + } + + var section = new FirebaseSection(); + var missing = new List(4); + + var androidPath = Path.Combine(folder, AndroidFileName); + if (File.Exists(androidPath)) section.AndroidConfigPath = Path.GetFullPath(androidPath); + else missing.Add(AndroidPlatform); + + var iosPath = Path.Combine(folder, IosFileName); + if (File.Exists(iosPath)) section.IosConfigPath = Path.GetFullPath(iosPath); + else missing.Add(IosPlatform); + + var webPath = Path.Combine(folder, WebFileName); + if (File.Exists(webPath)) section.WebConfigPath = Path.GetFullPath(webPath); + else missing.Add(WebPlatform); + + var serviceAccountPath = FindServiceAccount(folder); + if (serviceAccountPath is not null) section.ServiceAccountPath = serviceAccountPath; + else missing.Add(ServiceAccountPlatform); + + return new FirebaseFolderScanResult(section, missing); + } + + /// + /// Resolves the FCM v1 service-account credential. Glob first (cheap; matches + /// Google's default download name), then content-sniff every remaining *.json + /// (robust for operators who renamed the file). Returns null when no + /// candidate parses as a service-account credential. + /// + private static string? FindServiceAccount(string folder) + { + var globHits = Directory.EnumerateFiles(folder, ServiceAccountGlob, SearchOption.TopDirectoryOnly) + .OrderBy(p => p, StringComparer.Ordinal) + .ToList(); + if (globHits.Count > 0) return Path.GetFullPath(globHits[0]); + + foreach (var candidate in Directory.EnumerateFiles(folder, "*.json", SearchOption.TopDirectoryOnly) + .OrderBy(p => p, StringComparer.Ordinal)) + { + if (LooksLikeServiceAccount(candidate)) return Path.GetFullPath(candidate); + } + return null; + } + + /// + /// True when the JSON file at parses cleanly and its root + /// object carries "type": "service_account". Any parse failure / missing key + /// / non-string value is treated as "not a service account" (silent skip) — the + /// scanner is a discovery helper, not a validator, and a well-formed but unrelated + /// JSON file (e.g. an .eslintrc.json that happened to land in the folder) + /// shouldn't abort the whole scan. + /// + private static bool LooksLikeServiceAccount(string path) + { + try + { + using var doc = JsonDocument.Parse(File.ReadAllText(path)); + return doc.RootElement.ValueKind == JsonValueKind.Object + && doc.RootElement.TryGetProperty("type", out var type) + && type.ValueKind == JsonValueKind.String + && type.GetString() == "service_account"; + } + catch + { + return false; + } + } +} + +/// +/// Outcome of a call. +/// carries absolute paths for every recognised artefact (empty string for anything +/// missing); names the platform labels for what wasn't found so +/// the wizard's summary table can render a "configure these per-file" hint. +/// +public sealed record FirebaseFolderScanResult( + FirebaseSection Section, + IReadOnlyList Missing); diff --git a/tools/Interfold.Bootstrapper/Configuration/FirebaseJsonContext.cs b/tools/Interfold.Bootstrapper/Configuration/FirebaseJsonContext.cs new file mode 100644 index 00000000..ac43a962 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/FirebaseJsonContext.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Serialization; +using Interfold.Bootstrapper.Phases; +using Interfold.Settings.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Configuration; + +/// Source-gen contexts for JSON I/O. +/// Naming policies differ per input (Google snake_case vs console camelCase) and the +/// seeded output is always snake_case — three contexts keep each policy compile-time. + +[JsonSourceGenerationOptions( + PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, + PropertyNameCaseInsensitive = true, + RespectRequiredConstructorParameters = true)] +[JsonSerializable(typeof(FirebasePhase.GoogleServicesFile))] +[JsonSerializable(typeof(FirebasePhase.ServiceAccountStub))] +internal sealed partial class FirebaseSnakeCaseReadContext : JsonSerializerContext; + +[JsonSourceGenerationOptions( + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, + RespectRequiredConstructorParameters = true)] +[JsonSerializable(typeof(FirebaseWebClientConfig))] +internal sealed partial class FirebaseCamelCaseReadContext : JsonSerializerContext; + +[JsonSourceGenerationOptions( + PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, + WriteIndented = false, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] +[JsonSerializable(typeof(FirebaseAndroidClientConfig))] +[JsonSerializable(typeof(FirebaseIosClientConfig))] +[JsonSerializable(typeof(FirebaseWebClientConfig))] +internal sealed partial class FirebaseSnakeCaseWriteContext : JsonSerializerContext; diff --git a/tools/Interfold.Bootstrapper/Configuration/GeneratedSecrets.cs b/tools/Interfold.Bootstrapper/Configuration/GeneratedSecrets.cs new file mode 100644 index 00000000..a1599213 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/GeneratedSecrets.cs @@ -0,0 +1,87 @@ +using System.Text.Json.Serialization; + +namespace Interfold.Bootstrapper.Configuration; + +/// +/// Auto-generated secret material. Persisted to deploy/secrets/secrets.json with mode 0600. +/// Treated as the single source of truth — never overwritten unless --rotate-secrets is passed. +/// +public sealed class GeneratedSecrets +{ + [JsonPropertyName("postgresUser")] + public string PostgresUser { get; set; } = "interfold"; + + [JsonPropertyName("postgresPassword")] + public string PostgresPassword { get; set; } = string.Empty; + + /// + /// Transient init/cluster-owner password for postgres. The compose file passes this to + /// msg-db via POSTGRES_PASSWORD so initdb can create the db_init bootstrap + /// superuser on the first container start. uses it + /// once to mint the app + admin roles, then scrambles db_init's password in-cluster - + /// so by the time the API starts, the value sitting in .env is already stale. + /// + [JsonPropertyName("postgresInitPassword")] + public string PostgresInitPassword { get; set; } = string.Empty; + + /// + /// Random password for the postgres `<user>_admin` role. Created by DatabaseInitPhase and + /// persisted here so a subsequent rerun can rotate the role's password to match. It is never + /// exposed via the compose .env or via AppHost parameters - the value of record lives inside + /// the `internal.secrets` table where the API reads it from. + /// + [JsonPropertyName("postgresAdminPassword")] + public string PostgresAdminPassword { get; set; } = string.Empty; + + [JsonPropertyName("scyllaUser")] + public string ScyllaUser { get; set; } = "interfold"; + + [JsonPropertyName("scyllaPassword")] + public string ScyllaPassword { get; set; } = string.Empty; + + /// + /// Random password for the scylla `<user>_admin` superuser. Same handling as + /// - kept in secrets.json on disk for idempotent reruns, + /// surfaced inside the cluster only via internal.secrets. + /// + [JsonPropertyName("scyllaAdminPassword")] + public string ScyllaAdminPassword { get; set; } = string.Empty; + + [JsonPropertyName("encryptionPrivateKeyB64")] + public string EncryptionPrivateKeyB64 { get; set; } = string.Empty; + + [JsonPropertyName("encryptionPepper")] + public string EncryptionPepper { get; set; } = string.Empty; + + /// + /// HMAC signing secret for the phase-F deep-link token exchange. Seeded once and read + /// by the API exclusively via auth:deep_link_secret in internal.secrets — + /// never surfaced as an env var or compose parameter. + /// + [JsonPropertyName("deepLinkSecret")] + public string DeepLinkSecret { get; set; } = string.Empty; + + [JsonPropertyName("leafPfxPassword")] + public string LeafPfxPassword { get; set; } = string.Empty; + + /// + /// RSA-2048 JWT signing keypair (public half). The API verifies inbound bearer tokens with + /// this and rejects startup if it's absent (see ApplyAuthentication in + /// ConfigurationServiceCollectionExtensions). Stored as PEM (SPKI) so it can be written + /// straight to disk and bind-mounted into the container. + /// + [JsonPropertyName("jwtRsa256PublicKeyPem")] + public string JwtRsa256PublicKeyPem { get; set; } = string.Empty; + + /// RSA-2048 JWT signing keypair (private half) - PEM PKCS#8. + [JsonPropertyName("jwtRsa256PrivateKeyPem")] + public string JwtRsa256PrivateKeyPem { get; set; } = string.Empty; + + /// ES256 JWT signing keypair (public half) - PEM SPKI. + [JsonPropertyName("jwtEs256PublicKeyPem")] + public string JwtEs256PublicKeyPem { get; set; } = string.Empty; + + /// ES256 JWT signing keypair (private half) - PEM SEC1. + [JsonPropertyName("jwtEs256PrivateKeyPem")] + public string JwtEs256PrivateKeyPem { get; set; } = string.Empty; +} diff --git a/tools/Interfold.Bootstrapper/Configuration/HostEntry.cs b/tools/Interfold.Bootstrapper/Configuration/HostEntry.cs new file mode 100644 index 00000000..02313b47 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/HostEntry.cs @@ -0,0 +1,221 @@ +using System.Globalization; +using System.Net; +using System.Net.Sockets; + +namespace Interfold.Bootstrapper.Configuration; + +/// Shape of one entry. Dns → dNSName SAN + +/// Name Constraints subtree. Ipv4/Ipv6 → iPAddress SAN + subtree with all-ones mask. CIDRs +/// → subtree only; excluded from the leaf SAN (leaf serves a single host) and from primary-host +/// derivation (no canonical URL form for a network range). +internal enum HostKind +{ + Dns, + Ipv4, + Ipv6, + Ipv4Cidr, + Ipv6Cidr, +} + +/// Parsed host entry. preserves operator input for error +/// messages. is 32/128 for single-IP kinds; the operator prefix +/// for CIDR. +internal sealed record HostEntry( + string Raw, + HostKind Kind, + string? DnsName, + IPAddress? Ip, + int PrefixLength) +{ + public bool IsCidr => HostParser.IsCidr(Kind); + public bool IsLeafEligible => HostParser.IsLeafEligible(Kind); +} + +/// Authoritative host classifier. Callers pattern-match on +/// instead of re-parsing the raw string. +internal static class HostParser +{ + public const int Ipv4Bits = 32; + public const int Ipv6Bits = 128; + + public static bool IsCidr(HostKind k) => k is HostKind.Ipv4Cidr or HostKind.Ipv6Cidr; + + /// Non-CIDR kinds contribute a leaf-cert SAN and are eligible as the primary host. + public static bool IsLeafEligible(HostKind k) => k is HostKind.Dns or HostKind.Ipv4 or HostKind.Ipv6; + + /// Precedence: slash → CIDR; else IP literal; else DNS (RFC 1035-ish check). + /// Bracketed IPv6 literals ([::1]) accepted; CIDR must use unbracketed + /// (fe80::/64). Throws with an operator-actionable + /// message on malformed input. + public static HostEntry Parse(string raw) + { + if (string.IsNullOrWhiteSpace(raw)) + { + throw new FormatException("host entry must not be empty or whitespace"); + } + var trimmed = raw.Trim(); + + var slashIndex = trimmed.IndexOf('/'); + if (slashIndex >= 0) + { + return ParseCidr(trimmed, slashIndex); + } + + var addrCandidate = trimmed.Length >= 2 && trimmed[0] == '[' && trimmed[^1] == ']' + ? trimmed[1..^1] + : trimmed; + if (IPAddress.TryParse(addrCandidate, out var ip)) + { + return ip.AddressFamily switch + { + AddressFamily.InterNetwork => new HostEntry(trimmed, HostKind.Ipv4, null, ip, Ipv4Bits), + AddressFamily.InterNetworkV6 => new HostEntry(trimmed, HostKind.Ipv6, null, ip, Ipv6Bits), + _ => throw new FormatException($"'{trimmed}' parsed as an IP address of unsupported family '{ip.AddressFamily}'"), + }; + } + + ValidateDnsName(trimmed); + return new HostEntry(trimmed, HostKind.Dns, trimmed, null, 0); + } + + private static HostEntry ParseCidr(string trimmed, int slashIndex) + { + var addrText = trimmed[..slashIndex]; + var lenText = trimmed[(slashIndex + 1)..]; + + if (!IPAddress.TryParse(addrText, out var ip)) + { + throw new FormatException($"CIDR '{trimmed}': '{addrText}' is not a valid IP literal"); + } + if (!int.TryParse(lenText, NumberStyles.None, CultureInfo.InvariantCulture, out var prefix)) + { + throw new FormatException($"CIDR '{trimmed}': prefix '{lenText}' is not a non-negative integer"); + } + + var maxPrefix = ip.AddressFamily switch + { + AddressFamily.InterNetwork => Ipv4Bits, + AddressFamily.InterNetworkV6 => Ipv6Bits, + _ => throw new FormatException($"CIDR '{trimmed}': unsupported IP family '{ip.AddressFamily}'"), + }; + if (prefix < 0 || prefix > maxPrefix) + { + throw new FormatException($"CIDR '{trimmed}': prefix /{prefix} out of range, must be between 0 and {maxPrefix}"); + } + + var addr = ip.GetAddressBytes(); + if (!IsHostBitsZero(addr, prefix)) + { + // RFC 5280 §4.2.1.10: host bits beyond the mask MUST be zero. Fix-it rather than + // silently snap — "192.168.1.42/24" usually means /32, not the whole /24. + var canonical = new IPAddress(MaskAddress(addr, prefix)).ToString(); + var singleHost = $"{ip}/{maxPrefix}"; + throw new FormatException( + $"CIDR '{trimmed}' has host bits set beyond the /{prefix} mask. " + + $"Use '{canonical}/{prefix}' to pin the network, or '{singleHost}' to pin a single host."); + } + + var kind = ip.AddressFamily == AddressFamily.InterNetwork ? HostKind.Ipv4Cidr : HostKind.Ipv6Cidr; + return new HostEntry(trimmed, kind, null, ip, prefix); + } + + private static void ValidateDnsName(string name) + { + // Enough to reject typos (spaces, over-long); IP-literal parsing already ran upstream. + var probe = name.StartsWith("*.", StringComparison.Ordinal) ? name[2..] : name; + if (probe.Length == 0) + { + throw new FormatException($"'{name}' is not a valid host (DNS name, IP literal, or CIDR)"); + } + if (name.Length > 253) + { + throw new FormatException($"DNS name '{name}' exceeds 253 characters"); + } + if (name.AsSpan().ContainsAny(' ', '\t')) + { + throw new FormatException($"DNS name '{name}' contains whitespace"); + } + } + + /// Network-byte-order netmask. is the address-byte + /// count (4 for IPv4, 16 for IPv6); the bit count to set. + public static byte[] BuildNetmask(int byteLength, int prefix) + { + if (prefix < 0 || prefix > byteLength * 8) + { + throw new ArgumentOutOfRangeException(nameof(prefix)); + } + var result = new byte[byteLength]; + var fullBytes = prefix / 8; + for (var i = 0; i < fullBytes; i++) + { + result[i] = 0xFF; + } + var trailingBits = prefix - (fullBytes * 8); + if (trailingBits > 0 && fullBytes < byteLength) + { + result[fullBytes] = (byte)(0xFF << (8 - trailingBits)); + } + return result; + } + + /// RFC 5280 §4.2.1.10 "host bits MUST be zero" check for iPAddress subtrees. + public static bool IsHostBitsZero(byte[] addr, int prefix) + { + var mask = BuildNetmask(addr.Length, prefix); + for (var i = 0; i < addr.Length; i++) + { + if ((addr[i] & (byte)~mask[i]) != 0) + { + return false; + } + } + return true; + } + + /// Address ANDed with the netmask — used to render the canonical network + /// address in a host-bits-set fix-it. + public static byte[] MaskAddress(byte[] addr, int prefix) + { + var mask = BuildNetmask(addr.Length, prefix); + var result = new byte[addr.Length]; + for (var i = 0; i < addr.Length; i++) + { + result[i] = (byte)(addr[i] & mask[i]); + } + return result; + } + + /// iPAddress GeneralName payload: address || mask per RFC 5280 §4.2.1.10 + /// (8 octets IPv4, 32 IPv6). Throws on non-IP kinds. + public static byte[] ToNameConstraintSubtreeBytes(HostEntry entry) + { + if (entry.Ip is null) + { + throw new InvalidOperationException( + $"ToNameConstraintSubtreeBytes requires an IP-backed HostEntry, got Kind={entry.Kind}"); + } + var addr = entry.Ip.GetAddressBytes(); + var mask = BuildNetmask(addr.Length, entry.PrefixLength); + var combined = new byte[addr.Length + mask.Length]; + Buffer.BlockCopy(addr, 0, combined, 0, addr.Length); + Buffer.BlockCopy(mask, 0, combined, addr.Length, mask.Length); + return combined; + } + + /// URI authority host portion. IPv6 bracket-wrapped per RFC 3986 §3.2.2; + /// throws on CIDR (no canonical URL form). + public static string ToUrlHost(HostEntry entry) => entry.Kind switch + { + HostKind.Dns => entry.DnsName!, + HostKind.Ipv4 => entry.Ip!.ToString(), + HostKind.Ipv6 => $"[{entry.Ip}]", + _ => throw new InvalidOperationException( + $"HostEntry '{entry.Raw}' ({entry.Kind}) has no URL host representation"), + }; + + /// First non-CIDR entry — leaf CN, nginx server_name, and URL derivation + /// primary. Null when the list is all-CIDR or empty. + public static HostEntry? PickPrimary(IEnumerable entries) => + entries.FirstOrDefault(e => e.IsLeafEligible); +} diff --git a/tools/Interfold.Bootstrapper/Configuration/HostnameDetector.cs b/tools/Interfold.Bootstrapper/Configuration/HostnameDetector.cs new file mode 100644 index 00000000..e250e156 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/HostnameDetector.cs @@ -0,0 +1,88 @@ +using System.Text.RegularExpressions; + +namespace Interfold.Bootstrapper.Configuration; + +/// +/// Thin sibling of that resolves the device's short hostname +/// into the mDNS-qualified {hostname}.local form used by the interactive +/// "Public host(s)" pre-fill and by the pre-prompt mDNS banner. +/// +/// +/// The live entry point () shells out to +/// and defers the actual filtering rules to +/// — a pure static helper unit tests can drive directly without +/// touching the OS. Rejection cases the qualifier catches: +/// +/// Null / empty / whitespace input — no hostname at all. +/// localhost (any casing) — a mDNS advertisement here would be a lie: the name +/// resolves to loopback, not the LAN address the bootstrapper is issuing certs for. +/// Already-qualified FQDNs (contain .) — appending .local to +/// host.corp.example.com produces a bogus double-suffixed name. Only bare short +/// labels get the .local suffix. +/// Invalid DNS label characters or lengths outside RFC 1035's 1..63 byte label limit — +/// the resulting {name}.local wouldn't parse as a DNS name downstream anyway. +/// +/// +/// +internal static class HostnameDetector +{ + /// + /// RFC 1035-shaped label check: 1..63 characters, ASCII letters/digits/hyphens, must start + /// with a letter or digit (no leading hyphen). Deliberately permissive on trailing hyphens + /// — the DNS grammar technically forbids them too, but real-world hostnames occasionally + /// have them and rejecting here would drop the hostname from the pre-fill for no + /// operator-facing benefit. + /// + private static readonly Regex ShortHostnamePattern = + new("^[A-Za-z0-9][A-Za-z0-9-]{0,62}$", RegexOptions.CultureInvariant | RegexOptions.Compiled); + + /// + /// Live device-hostname probe. Reads (which on + /// every supported OS returns the kernel's short hostname) and defers the filtering rules + /// to . Any exception is swallowed and reported as null so a + /// platform surprise degrades to "no hostname pre-fill" rather than crashing the bootstrap. + /// + public static string? TryDetectMdnsHostname() + { + try + { + var raw = System.Net.Dns.GetHostName(); + return QualifyForMdns(raw); + } + catch + { + return null; + } + } + + /// + /// Pure filter used by and directly exercised by unit + /// tests. Applies the rejection rules documented on the class summary and returns + /// {name}.local when the input passes. Trims surrounding whitespace before matching + /// so a hostname that survived a copy/paste with an accidental trailing newline still + /// qualifies. + /// + public static string? QualifyForMdns(string? raw) + { + if (string.IsNullOrWhiteSpace(raw)) + { + return null; + } + var name = raw.Trim(); + if (string.Equals(name, "localhost", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + // Reject already-qualified names — appending .local to `host.corp.example.com` would + // produce a bogus mDNS name. Only bare short hostnames get the .local suffix. + if (name.Contains('.', StringComparison.Ordinal)) + { + return null; + } + if (!ShortHostnamePattern.IsMatch(name)) + { + return null; + } + return $"{name}.local"; + } +} diff --git a/tools/Interfold.Bootstrapper/Configuration/LocalAddressDetector.cs b/tools/Interfold.Bootstrapper/Configuration/LocalAddressDetector.cs new file mode 100644 index 00000000..4897df3c --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/LocalAddressDetector.cs @@ -0,0 +1,129 @@ +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; + +namespace Interfold.Bootstrapper.Configuration; + +/// Best-effort primary unicast IP used to pre-fill the interactive "Public host(s)" +/// row. Operator override wins; non-interactive flows skip this so a JSON file that omits +/// hosts still fails fast instead of minting a cert for whatever address happened to +/// be on the box. Enumeration faults degrade to null (no default) rather than crashing. +internal static class LocalAddressDetector +{ + // Prefix guard for NICs that report a normal type but are actually virtual: Docker/KVM + // bridges, k8s overlays, Hyper-V switches, VPN tunnels. Without this the picker latches + // onto Docker bridge 172.17.0.1 on dev boxes, which is never what the operator wants. + private static readonly string[] VirtualNamePrefixes = + [ + "docker", // docker0, docker_gwbridge + "br-", // docker user-defined bridge networks + "veth", // container veth pairs + "tap", "tun", // OpenVPN, WireGuard userspace, qemu + "vEthernet (", // Windows Hyper-V virtual switches + "vmnet", // macOS VMware Fusion / Parallels + "virbr", // libvirt-managed bridges + "cni", // container networking interface plugins + "flannel", "cilium_", "kube-", // k8s overlays + "zt", // ZeroTier + "wg", // WireGuard + "utun", // macOS userspace TUN (Tailscale, Cloudflare WARP) + "tailscale", + ]; + + /// IPv4-biased; enumeration faults return null so the operator types a host. + public static IPAddress? TryDetectPrimaryIp() + { + try + { + var nics = NetworkInterface.GetAllNetworkInterfaces(); + var probes = new List(nics.Length); + foreach (var nic in nics) + { + // Per-NIC try/catch: NICs can disappear mid-enumeration (USB unplug, VPN teardown). + IReadOnlyList addresses; + try + { + addresses = nic.GetIPProperties().UnicastAddresses + .Select(u => u.Address) + .ToArray(); + } + catch (NetworkInformationException) + { + addresses = []; + } + probes.Add(new NicProbe( + nic.Name, + nic.NetworkInterfaceType, + nic.OperationalStatus, + addresses)); + } + return Pick(probes); + } + catch (NetworkInformationException) + { + return null; + } + } + + /// Pure picker over fabricated NIC probes. Rejects: NICs that aren't Up, Loopback + /// or Tunnel types, any name matching , loopback addresses, + /// APIPA 169.254.0.0/16, and IPv6 link-local/site-local/multicast. First IPv4 wins; IPv6 + /// only if no usable IPv4 exists. + public static IPAddress? Pick(IReadOnlyList probes) + { + IPAddress? firstV4 = null; + IPAddress? firstV6 = null; + + foreach (var nic in probes) + { + if (nic.Status != OperationalStatus.Up) continue; + if (nic.Type is NetworkInterfaceType.Loopback or NetworkInterfaceType.Tunnel) continue; + if (IsVirtualName(nic.Name)) continue; + + foreach (var addr in nic.Addresses) + { + if (IPAddress.IsLoopback(addr)) continue; + + switch (addr.AddressFamily) + { + case AddressFamily.InterNetwork: + var v4Bytes = addr.GetAddressBytes(); + // APIPA 169.254.0.0/16 == DHCP failure. + if (v4Bytes[0] == 169 && v4Bytes[1] == 254) continue; + firstV4 ??= addr; + break; + + case AddressFamily.InterNetworkV6: + if (addr.IsIPv6LinkLocal) continue; + if (addr.IsIPv6SiteLocal) continue; + if (addr.IsIPv6Multicast) continue; + firstV6 ??= addr; + break; + } + } + } + + return firstV4 ?? firstV6; + } + + /// Case-insensitive prefix match against . + internal static bool IsVirtualName(string name) + { + foreach (var prefix in VirtualNamePrefixes) + { + if (name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + return false; + } + + /// Test-friendly snapshot of one — sealed and + /// tightly coupled to the live OS query, so tests fabricate NicProbe instead. + public readonly record struct NicProbe( + string Name, + NetworkInterfaceType Type, + OperationalStatus Status, + IReadOnlyList Addresses); +} diff --git a/tools/Interfold.Bootstrapper/Configuration/MdnsAvailability.cs b/tools/Interfold.Bootstrapper/Configuration/MdnsAvailability.cs new file mode 100644 index 00000000..199f6071 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Configuration/MdnsAvailability.cs @@ -0,0 +1,124 @@ +using System.Runtime.InteropServices; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Phases; +using Interfold.Bootstrapper.Util; + +namespace Interfold.Bootstrapper.Configuration; + +/// +/// Detects whether the current device can actually resolve mDNS-shaped (*.local) +/// hostnames on the LAN, plus package-recipe helpers for the interactive install prompt +/// used by 's pre-prompt banner and post-fill safety gate. +/// +/// +/// Lives in (not ) because it's a pure +/// detection / side-effect utility invoked from — mirrors +/// the placement of , which is the same shape. +/// +/// +internal static class MdnsAvailability +{ + /// + /// Shells out to getent hosts <name> — exits 0 iff the whole + /// nsswitch → mdns_minimal → avahi chain successfully resolves . + /// The exit-code + stdout check together beat probing avahi's systemd unit status because + /// a running daemon with a broken nsswitch.conf would still fail resolution. + /// + /// + /// Returns null on non-Linux platforms (getent doesn't exist on Windows / macOS in + /// the shape the bootstrapper needs), which the callers treat as "unknown — skip the + /// gate". The bootstrap phase itself is Linux-only anyway; this branch just lets the + /// ConfigPhase logic run cleanly on a dev workstation for JSON validation. + /// + /// + public static async Task IsHostnameResolvableAsync(string hostname, CancellationToken ct) + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return null; + } + + try + { + var run = await ProcessRunner.RunAsync("getent", ["hosts", hostname], ct: ct).ConfigureAwait(false); + return run.ExitCode == 0 && !string.IsNullOrWhiteSpace(run.StdOut); + } + catch + { + // getent binary missing or Process.Start failure — treat as "cannot probe" so the + // gate short-circuits into skip rather than mutating the operator's hosts list on + // an environmental issue we can't diagnose. + return null; + } + } + + /// + /// Distro-family-specific package list to satisfy the mDNS resolution chain: + /// avahi-daemon (the daemon itself) plus the platform-appropriate NSS module + /// (libnss-mdns on Debian/Ubuntu, nss-mdns on Fedora/RHEL). Empty list for + /// unknown / unsupported families — the caller falls back to the manual install hint + /// instead of attempting an install. + /// + public static IReadOnlyList InstallPackages(DistroFamily family) => family switch + { + DistroFamily.Debian => ["avahi-daemon", "libnss-mdns"], + DistroFamily.RedHat => ["avahi", "nss-mdns"], + _ => [], + }; + + /// + /// Copy-pasteable one-liner that gets an operator to a working mDNS stack from a clean + /// distro. Included in the "mDNS unavailable" warning both banners emit so the operator + /// can act on it without leaving the terminal. The systemctl enable --now tail is + /// present in the shell recipe because the packages don't self-enable the daemon on every + /// distro (Debian starts it via a maintainer script; RHEL leaves it inactive). + /// + public static string ManualInstallHint(DistroFamily family) => family switch + { + DistroFamily.Debian => + "sudo apt-get install -y avahi-daemon libnss-mdns && sudo systemctl enable --now avahi-daemon", + DistroFamily.RedHat => + "sudo dnf install -y avahi nss-mdns && sudo systemctl enable --now avahi-daemon", + _ => + "(install avahi-daemon and its nss module for your distro, then enable the service)", + }; + + /// + /// Attempts to install the avahi packages for and enable the + /// daemon via systemctl. Returns true when the install succeeded (the + /// caller should re-probe to confirm resolution works end-to-end); false when + /// the distro is unsupported or the install failed (details are logged). + /// + public static async Task TryInstallAvahiAsync( + DistroInfo distro, PhaseLogger logger, CancellationToken ct) + { + var packages = InstallPackages(distro.Family); + if (packages.Count == 0) + { + logger.Warn($" unsupported distro family {distro.Family}; skipping avahi install"); + return false; + } + + logger.Info($" installing avahi ({string.Join(" ", packages)}) ..."); + try + { + await PrerequisitesPhase.RunInstallAsync(distro, packages, logger, ct).ConfigureAwait(false); + try + { + await ProcessRunner.RunAsync( + "systemctl", ["enable", "--now", "avahi-daemon"], ct: ct).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.Warn($" could not `systemctl enable --now avahi-daemon` ({ex.GetType().Name}: {ex.Message}); " + + "start it manually if the re-probe below still fails."); + } + return true; + } + catch (Exception ex) + { + logger.Warn($" avahi install failed ({ex.GetType().Name}: {ex.Message})"); + return false; + } + } +} diff --git a/tools/Interfold.Bootstrapper/Interfold.Bootstrapper.csproj b/tools/Interfold.Bootstrapper/Interfold.Bootstrapper.csproj new file mode 100644 index 00000000..c9bc4206 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Interfold.Bootstrapper.csproj @@ -0,0 +1,85 @@ + + + + + + Exe + net10.0 + enable + enable + Interfold.Bootstrapper + interfold-bootstrap + c0a8f3d2-1e8c-4d7a-9b9e-2f5d8c4e3a91 + true + + true + + false + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/Interfold.Bootstrapper/Phases/BackupDatabaseComponent.cs b/tools/Interfold.Bootstrapper/Phases/BackupDatabaseComponent.cs new file mode 100644 index 00000000..698558e6 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/BackupDatabaseComponent.cs @@ -0,0 +1,68 @@ +namespace Interfold.Bootstrapper.Phases; + +/// +/// The database component targeted by backup/restore. CLI wire values +/// (postgres / scylla / all) are frozen — operators and the systemd +/// backup timer pass them verbatim. +/// +internal enum BackupDatabaseComponent +{ + Postgres, + Scylla, + All, +} + +internal static class BackupDatabaseComponentExtensions +{ + public static string ToWireValue(this BackupDatabaseComponent component) => component switch + { + BackupDatabaseComponent.Postgres => "postgres", + BackupDatabaseComponent.Scylla => "scylla", + BackupDatabaseComponent.All => "all", + _ => throw new ArgumentOutOfRangeException(nameof(component), component, "Unhandled BackupDatabaseComponent."), + }; + + /// Case-insensitive parse; null/empty resolves to + /// (the CLI default), unknown values return null so the caller can emit its legacy error. + public static BackupDatabaseComponent? TryParse(string? raw) + { + if (string.IsNullOrWhiteSpace(raw)) + { + return BackupDatabaseComponent.All; + } + + return raw.Trim().ToLowerInvariant() switch + { + "postgres" => BackupDatabaseComponent.Postgres, + "scylla" => BackupDatabaseComponent.Scylla, + "all" => BackupDatabaseComponent.All, + _ => null, + }; + } +} + +/// +/// On-disk layout constants for the backup root. The subdirectory names and archive +/// extensions are frozen — restore/update flows and existing operator backups rely on them. +/// +internal static class BackupStoragePaths +{ + public const string PostgresDir = "postgres"; + public const string ScyllaDir = "scylla"; + public const string PostgresArchivePattern = "*.dump"; + public const string ScyllaArchivePattern = "*.tar.gz"; + + /// + /// Returns the newest file matching under + /// by last-write time, or null if the directory + /// doesn't exist or contains no matching files. + /// + public static FileInfo? LatestFile(string dir, string pattern) + { + if (!Directory.Exists(dir)) return null; + return new DirectoryInfo(dir) + .EnumerateFiles(pattern, SearchOption.TopDirectoryOnly) + .OrderByDescending(f => f.LastWriteTimeUtc) + .FirstOrDefault(); + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/BackupPhase.cs b/tools/Interfold.Bootstrapper/Phases/BackupPhase.cs new file mode 100644 index 00000000..3e2a90ae --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/BackupPhase.cs @@ -0,0 +1,308 @@ +using System.Globalization; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.Phases; + +/// Logical backup of live Postgres + Scylla/Cassandra state; idempotent across +/// reruns. Driven manually (interfold-bootstrap backup) or by the systemd timer from +/// . Layout: {backupDir}/{postgres|scylla}/{timestamp}.{ext}. +/// Postgres → custom-format pg_dump as the {user}_admin role, PGPASSWORD via +/// env (never argv). Scylla → nodetool snapshot + host-side docker cp piped through +/// (the scylladb/scylla image ships no tar); +/// seed node only, multi-DC wrappers layer over this. Post-write prune keeps exactly +/// RetainCount archives per component. +internal static class BackupPhase +{ + private static readonly string Phase = BootstrapCommand.Backup.ToPhaseLogName(); + + internal static readonly string[] ValidComponents = + Enum.GetValues().Select(BackupDatabaseComponentExtensions.ToWireValue).ToArray(); + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + logger.PhaseStart(Phase); + + if (BackupDatabaseComponentExtensions.TryParse(options.BackupComponent) is not { } component) + { + logger.PhaseFail(Phase, PhaseFailureReasons.UnknownComponent); + throw new InvalidOperationException( + $"--component='{options.BackupComponent}' is invalid. Expected one of: {string.Join(", ", ValidComponents)}."); + } + + var config = await PhaseArtifactLoader + .LoadRequiredConfigAsync(options, logger, Phase, "Backup", ct) + .ConfigureAwait(false); + + var secrets = PhaseArtifactLoader.LoadRequiredSecretsOrFail(options, logger, Phase, "Backup"); + + var composeFile = PhaseArtifactLoader.RequireComposeFileOrFail(options, logger, Phase); + + var backupRoot = ResolveBackupRoot(options, config); + var retainCount = options.BackupRetainOverride ?? config.Backup.RetainCount; + if (retainCount < 1) + { + logger.PhaseFail(Phase, PhaseFailureReasons.InvalidRetain); + throw new InvalidOperationException( + $"--retain={retainCount} is below the minimum of 1."); + } + logger.Info($" backup root: {backupRoot} (retain {retainCount} per component)"); + + // Shared timestamp so a postgres+scylla pair correlates by filename alone. + var timestamp = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture); + + if (component is BackupDatabaseComponent.Postgres or BackupDatabaseComponent.All) + { + await BackupPostgresAsync(composeFile, backupRoot, timestamp, config, secrets, retainCount, logger, ct) + .ConfigureAwait(false); + } + + if (component is BackupDatabaseComponent.Scylla or BackupDatabaseComponent.All) + { + await BackupScyllaAsync(composeFile, backupRoot, timestamp, config, retainCount, logger, ct) + .ConfigureAwait(false); + } + + logger.PhaseDone(Phase); + return 0; + } + + /// Precedence: --backup-dir CLI → config.backup.directory → + /// {outputDir}/backups. + internal static string ResolveBackupRoot(BootstrapOptions options, BootstrapConfig config) + { + if (!string.IsNullOrWhiteSpace(options.BackupDirOverride)) + { + return Path.GetFullPath(options.BackupDirOverride); + } + if (!string.IsNullOrWhiteSpace(config.Backup.Directory)) + { + return Path.GetFullPath(config.Backup.Directory); + } + return Path.Combine(options.OutputDir, "backups"); + } + + /// Scylla/Cassandra seed service + container data path. Mirrors the resource + /// naming in InterfoldAppHost.Configure so exec lands on the same container. + internal static (string Service, string DataPath) ResolveScyllaSeed(BootstrapConfig config) + { + return config.DatabaseMode switch + { + DatabaseMode.Cassandra => (ComposeServices.Cassandra, ContainerMountPaths.CassandraData), + DatabaseMode.Multi => (ComposeServices.ScyllaNam, ContainerMountPaths.ScyllaData), + _ => (ComposeServices.ScyllaSingle, ContainerMountPaths.ScyllaData), + }; + } + + /// Canonical archive filename (relative to the component subdirectory). + internal static string BuildArchiveFileName(BackupDatabaseComponent component, string timestamp) + { + return component switch + { + BackupDatabaseComponent.Postgres => $"{timestamp}.dump", + BackupDatabaseComponent.Scylla => $"{timestamp}.tar.gz", + _ => throw new InvalidOperationException($"Unknown component '{component}' (expected: postgres | scylla)."), + }; + } + + /// pg_dump docker-compose exec argv. PGPASSWORD flows via env, not argv, + /// so it stays invisible to ps. + internal static IReadOnlyList BuildPostgresDumpArgs( + string composeFile, string adminUser, string database) + => DockerCompose.BuildPostgresExecArgs( + composeFile, ComposeServices.Postgres, "pg_dump", adminUser, database, + "-Fc"); + + /// Returns (snapshot, clearsnapshot) argv pair. The intervening + /// docker cp (see ) produces the archive + /// host-side because scylladb/scylla ships no tar. + internal static (IReadOnlyList Snapshot, IReadOnlyList Clear) + BuildScyllaSnapshotArgs(string composeFile, string service, string dataPath, string tag) + { + _ = dataPath; // Consumed downstream by BuildContainerCpArgs; kept for signature symmetry. + var snapshot = new[] + { + "compose", "-f", composeFile, + "exec", "-T", service, + "nodetool", "snapshot", "-t", tag, + }; + var clear = new[] + { + "compose", "-f", composeFile, + "exec", "-T", service, + "nodetool", "clearsnapshot", "-t", tag, + }; + return (snapshot, clear); + } + + /// docker cp argv streaming the container path as a raw tar on stdout. + /// Container id (not service name) because docker cp is a daemon-level API; + /// - destination emits to stdout instead of a host file. + internal static IReadOnlyList BuildContainerCpArgs(string containerId, string dataPath) + => DockerCompose.BuildContainerCpFromContainer(containerId, dataPath); + + private static async Task BackupPostgresAsync( + string composeFile, string backupRoot, string timestamp, + BootstrapConfig config, GeneratedSecrets secrets, int retainCount, + PhaseLogger logger, CancellationToken ct) + { + var componentDir = Path.Combine(backupRoot, BackupStoragePaths.PostgresDir); + Directory.CreateDirectory(componentDir); + var dumpPath = Path.Combine(componentDir, BuildArchiveFileName(BackupDatabaseComponent.Postgres, timestamp)); + + // pg_dump needs the admin role — the app role's per-object grants aren't broad enough + // to capture ownership metadata. Provisioned by DatabaseInitPhase. + var (adminUser, adminPassword) = PhaseArtifactLoader.RequireAdminPassword(secrets, logger, Phase); + + logger.Info($" postgres: pg_dump -> {dumpPath}"); + var argv = BuildPostgresDumpArgs(composeFile, adminUser, config.PostgresDatabase); + + // Stream to disk so the dump never buffers in memory; password via env, never argv. + await DatabaseArchiveStreamer.StreamProcessStdoutToFileAsync( + "docker", argv, DatabaseArchiveStreamer.PgPasswordEnv(adminPassword), dumpPath, ct) + .ConfigureAwait(false); + + var size = new FileInfo(dumpPath).Length; + if (size == 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.EmptyPostgresDump); + File.Delete(dumpPath); + throw new InvalidOperationException( + $"pg_dump produced an empty file at {dumpPath}. Inspect docker logs for {ComposeServices.Postgres}."); + } + logger.Info($" postgres: wrote {FormatBytes(size)}"); + + PruneComponent(componentDir, BackupDatabaseComponent.Postgres, retainCount, logger); + } + + private static async Task BackupScyllaAsync( + string composeFile, string backupRoot, string timestamp, + BootstrapConfig config, int retainCount, + PhaseLogger logger, CancellationToken ct) + { + var (service, dataPath) = ResolveScyllaSeed(config); + var componentDir = Path.Combine(backupRoot, BackupStoragePaths.ScyllaDir); + Directory.CreateDirectory(componentDir); + var archivePath = Path.Combine(componentDir, BuildArchiveFileName(BackupDatabaseComponent.Scylla, timestamp)); + + // Tag pinned to the timestamp so a failed clear leaves an obvious orphan matching this run. + var tag = $"interfold-backup-{timestamp}"; + + var (snapshotArgs, clearArgs) = + BuildScyllaSnapshotArgs(composeFile, service, dataPath, tag); + + logger.Info($" scylla: nodetool snapshot -t {tag} (service={service})"); + await PhaseRunner.RunOrPhaseFailAsync( + () => ProcessRunner.RunAsync("docker", snapshotArgs, ct: ct), + logger, Phase, PhaseFailureReasons.NodetoolSnapshot, + $"nodetool snapshot", ct).ConfigureAwait(false); + + try + { + // docker cp needs a container id; the AppHost emits exactly one seed container. + var containerId = await DockerCompose.ResolveContainerIdAsync(composeFile, service, ct: ct).ConfigureAwait(false); + if (string.IsNullOrEmpty(containerId)) + { + logger.PhaseFail(Phase, PhaseFailureReasons.ResolveScyllaContainer); + throw new InvalidOperationException( + $"Failed to resolve container id for compose service '{service}'. " + + "Is the stack running? Try `docker compose ps` under the output directory."); + } + + logger.Info($" scylla: docker cp {service}({containerId[..Math.Min(12, containerId.Length)]}):{dataPath} -> {archivePath}"); + + // In-process GZip wrap avoids sh/pipefail semantics and needs only docker on the host. + var cpArgs = BuildContainerCpArgs(containerId, dataPath); + await DatabaseArchiveStreamer.StreamProcessStdoutToFileAsync( + "docker", cpArgs, environment: null, archivePath, ct, compress: true) + .ConfigureAwait(false); + } + finally + { + // Best-effort clearsnapshot: leftover snapshots eat container disk otherwise. + // Failure is logged, not fatal — the archive on disk is the dispositive verdict. + var clear = await ProcessRunner.RunAsync("docker", clearArgs, ct: ct).ConfigureAwait(false); + if (clear.ExitCode != 0) + { + logger.Warn($"nodetool clearsnapshot -t {tag} exited {clear.ExitCode}: {clear.StdErr.Trim()}"); + } + } + + var size = new FileInfo(archivePath).Length; + if (size == 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.EmptyScyllaArchive); + File.Delete(archivePath); + throw new InvalidOperationException( + $"Scylla tar produced an empty file at {archivePath}. Inspect docker logs for {service}."); + } + logger.Info($" scylla: wrote {FormatBytes(size)}"); + + PruneComponent(componentDir, BackupDatabaseComponent.Scylla, retainCount, logger); + } + + internal static void PruneComponent(string componentDir, BackupDatabaseComponent component, int retainCount, PhaseLogger logger) + { + var pattern = component switch + { + BackupDatabaseComponent.Postgres => BackupStoragePaths.PostgresArchivePattern, + BackupDatabaseComponent.Scylla => BackupStoragePaths.ScyllaArchivePattern, + _ => throw new InvalidOperationException($"Unknown component '{component}'."), + }; + var files = new DirectoryInfo(componentDir) + .EnumerateFiles(pattern, SearchOption.TopDirectoryOnly) + .ToList(); + + foreach (var stale in BackupRetention.Prune(files, retainCount)) + { + try + { + stale.Delete(); + logger.Info($" {component}: pruned {stale.Name}"); + } + catch (Exception ex) + { + logger.Warn($"failed to delete {stale.FullName}: {ex.Message}"); + } + } + } + + private static string FormatBytes(long bytes) + { + const long Kib = 1024L; + const long Mib = Kib * 1024L; + const long Gib = Mib * 1024L; + if (bytes >= Gib) return $"{bytes / (double)Gib:F2} GiB"; + if (bytes >= Mib) return $"{bytes / (double)Mib:F2} MiB"; + if (bytes >= Kib) return $"{bytes / (double)Kib:F2} KiB"; + return $"{bytes} B"; + } +} + +/// Pure pruning helper — returns files to delete (oldest by last-write time) so the +/// survivor set is exactly entries. Extracted from +/// for exhaustive unit testing without on-disk fixtures. +internal static class BackupRetention +{ + /// Returns files to delete, oldest-first. Empty when input ≤ keep; + /// keep == 0 deletes everything. + public static IEnumerable Prune(IEnumerable files, int keep) + { + ArgumentNullException.ThrowIfNull(files); + if (keep < 0) + { + throw new ArgumentOutOfRangeException(nameof(keep), keep, "retain count must be >= 0."); + } + + // Materialise once — DirectoryInfo enumerations don't survive a second pass. + var ordered = files.OrderBy(f => f.LastWriteTimeUtc).ToList(); + var toDeleteCount = ordered.Count - keep; + return toDeleteCount <= 0 + ? [] + : ordered.Take(toDeleteCount); + } +} + diff --git a/tools/Interfold.Bootstrapper/Phases/BootstrapArtifactPaths.cs b/tools/Interfold.Bootstrapper/Phases/BootstrapArtifactPaths.cs new file mode 100644 index 00000000..1a4809db --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/BootstrapArtifactPaths.cs @@ -0,0 +1,41 @@ +namespace Interfold.Bootstrapper.Phases; + +/// +/// The on-disk artifact conventions every phase shares, replacing per-phase copies of the +/// compose-file discovery helper and the config-path fallback. The file names are frozen — +/// operators, systemd units, and existing deployments reference them. +/// +internal static class BootstrapArtifactPaths +{ + public const string ComposeFileName = "docker-compose.yaml"; + public const string BootstrapConfigFileName = "interfold.bootstrap.json"; + public const string SecretsRelativePath = "secrets/secrets.json"; + + /// + /// Finds the emitted compose file under : the conventional + /// top-level path first, then a recursive search (Aspire >= 13 sometimes emits into a + /// subdirectory keyed by the environment name). Null when absent. + /// + public static string? FindComposeFile(string outputDir) + { + var direct = Path.Combine(outputDir, ComposeFileName); + if (File.Exists(direct)) return direct; + return Directory.EnumerateFiles(outputDir, ComposeFileName, SearchOption.AllDirectories).FirstOrDefault(); + } + + /// + /// Like but never null: falls back to the conventional + /// full path even when the file doesn't exist yet (install-service pre-populates unit + /// files for image-baking workflows; compose-up surfaces the missing file later). + /// + public static string ResolveComposeFileOrConventional(string outputDir) + { + var found = FindComposeFile(outputDir); + return Path.GetFullPath(found ?? Path.Combine(outputDir, ComposeFileName)); + } + + /// The bootstrap config path: explicit --config when supplied, else + /// {outputDir}/interfold.bootstrap.json. + public static string ResolveConfigPath(Cli.BootstrapOptions options) + => options.ConfigPath ?? Path.Combine(options.OutputDir, BootstrapConfigFileName); +} diff --git a/tools/Interfold.Bootstrapper/Phases/BootstrapPhase.cs b/tools/Interfold.Bootstrapper/Phases/BootstrapPhase.cs new file mode 100644 index 00000000..d7d1eeee --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/BootstrapPhase.cs @@ -0,0 +1,41 @@ +namespace Interfold.Bootstrapper.Phases; + +/// +/// The orchestrated bootstrap phases, in execution order, as used by the hidden +/// --fault-inject=after-<phase> testability hook. The wire names are frozen — +/// integration tests and operator muscle memory pass the exact tokens. +/// +internal enum BootstrapPhase +{ + Prereqs, + Config, + Secrets, + Certs, + Publish, + Firebase, + DbInit, + Launch, + + /// Sub-phase hook inside db-init: halts after Postgres init, before Scylla init. + DbPostgres, +} + +internal static class BootstrapPhaseExtensions +{ + public static string ToWireName(this BootstrapPhase phase) => phase switch + { + BootstrapPhase.Prereqs => "prereqs", + BootstrapPhase.Config => "config", + BootstrapPhase.Secrets => "secrets", + BootstrapPhase.Certs => "certs", + BootstrapPhase.Publish => "publish", + BootstrapPhase.Firebase => "firebase", + BootstrapPhase.DbInit => "db-init", + BootstrapPhase.Launch => "launch", + BootstrapPhase.DbPostgres => "db-postgres", + _ => throw new ArgumentOutOfRangeException(nameof(phase), phase, "Unhandled BootstrapPhase."), + }; + + /// The --fault-inject token that halts execution after this phase. + public static string ToFaultInjectToken(this BootstrapPhase phase) => $"after-{phase.ToWireName()}"; +} diff --git a/tools/Interfold.Bootstrapper/Phases/CassandraImagePhase.cs b/tools/Interfold.Bootstrapper/Phases/CassandraImagePhase.cs new file mode 100644 index 00000000..1310b673 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/CassandraImagePhase.cs @@ -0,0 +1,63 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// Builds the custom Cassandra 5 image the AppHost declares via +/// AddDockerfile("cassandra", "../../db/cassandra"). Aspire's compose publisher +/// replaces Dockerfile services with an ${CASSANDRA_IMAGE} placeholder in publish +/// mode — the bootstrapper must build the image locally and fill that .env entry before +/// docker compose up can start the stack. +/// +internal static class CassandraImagePhase +{ + /// + /// Local-only tag written to CASSANDRA_IMAGE in the emitted .env. + /// + internal const string LocalImageTag = "interfold-cassandra:local"; + + internal static bool IsCassandraDeployment(BootstrapConfig config) => + config.DatabaseMode == DatabaseMode.Cassandra; + + /// Argv for docker build -t … -f - <contextDir> with Dockerfile on stdin. + internal static IReadOnlyList BuildDockerBuildArgs(string contextDir) => + ["build", "-t", LocalImageTag, "-f", "-", contextDir]; + + /// + /// Builds from the embedded Dockerfile. Idempotent — Docker's + /// layer cache makes repeat calls cheap when nothing changed. + /// + internal static async Task EnsureBuiltAsync(PhaseLogger logger, CancellationToken ct) + { + var dockerfile = EmbeddedSupportFiles.ReadAllText(EmbeddedSupportFiles.CassandraDockerfileRelative); + var contextDir = Path.Combine(Path.GetTempPath(), $"interfold-cassandra-ctx-{Guid.NewGuid():N}"); + Directory.CreateDirectory(contextDir); + + try + { + logger.Info($" docker build -t {LocalImageTag} -f - {contextDir}"); + var run = await ProcessRunner.RunAsync( + "docker", + BuildDockerBuildArgs(contextDir), + stdin: dockerfile, + ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + logger.Error(run.StdErr.Trim()); + throw new InvalidOperationException( + $"docker build for Cassandra failed (exit {run.ExitCode})."); + } + + if (!string.IsNullOrWhiteSpace(run.StdOut)) + logger.Info(run.StdOut.Trim()); + } + finally + { + try { Directory.Delete(contextDir, recursive: true); } + catch { /* best-effort cleanup */ } + } + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/CertificatePhase.cs b/tools/Interfold.Bootstrapper/Phases/CertificatePhase.cs new file mode 100644 index 00000000..e9b005ff --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/CertificatePhase.cs @@ -0,0 +1,392 @@ +using System.Formats.Asn1; +using System.Globalization; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; + +namespace Interfold.Bootstrapper.Phases; + +/// Phase 4 — generates a root CA + leaf TLS cert (with SANs) for the configured +/// hosts, emits .crt/.key/.pfx, and installs the root CA into the OS +/// trust store. DNS names, IPv4/6 literals, and CIDR blocks are all accepted; CIDRs restrict +/// the root CA's Name Constraints but never appear on a leaf SAN. All-C# port of +/// scripts/create-certs.sh. +internal static partial class CertificatePhase +{ + private const string CertsRelativeDir = "certs"; + private const string DebianAnchorsDir = "/usr/local/share/ca-certificates"; + private const string RedHatAnchorsDir = "/etc/pki/ca-trust/source/anchors"; + private const string TrustAnchorFileName = "interfold-root-ca.crt"; + + public static async Task RunAsync( + BootstrapOptions options, + BootstrapConfig config, + GeneratedSecrets secrets, + PhaseLogger logger, + CancellationToken ct) + { + const string Phase = "certs"; + logger.PhaseStart(Phase); + + var certsDir = Path.Combine(options.OutputDir, CertsRelativeDir); + var rootCrtPath = Path.Combine(certsDir, "rootCA.crt"); + var rootKeyPath = Path.Combine(certsDir, "rootCA.key"); + var rootFingerprintPath = Path.Combine(certsDir, "rootCA.sha256.txt"); + var leafCrtPath = Path.Combine(certsDir, "leaf.crt"); + var leafKeyPath = Path.Combine(certsDir, "leaf.key"); + var leafPfxPath = Path.Combine(certsDir, "leaf.pfx"); + + // Skip-regen is gated on the original four files only; rootCA.sha256.txt is derived + // metadata that EnsureUpgradeArtefacts backfills so older installs upgrade cleanly. + var allPresentBeforeRun = File.Exists(rootCrtPath) && File.Exists(leafCrtPath) + && File.Exists(leafKeyPath) && File.Exists(leafPfxPath); + if (allPresentBeforeRun && !options.RotateCerts) + { + EnsureUpgradeArtefacts(rootCrtPath, rootKeyPath, rootFingerprintPath, logger); + PrintTrustInfo(rootCrtPath, rootFingerprintPath, logger); + logger.PhaseSkip(Phase, PhaseFailureReasons.Skip.AlreadyPresent); + return; + } + + if (allPresentBeforeRun) + { + logger.Info(" --rotate-certs set: regenerating root CA + leaf"); + } + + Directory.CreateDirectory(certsDir); + + // ConfigPhase.Validate already parsed these; re-parse here to hand helpers the typed shape. + var hosts = config.Deployment.Hosts.Select(HostParser.Parse).ToList(); + var (rootCert, rootKey) = GenerateRootCa(config.Deployment.RootCaName, config.Deployment.CertYears, hosts); + var (leafCert, leafKey) = GenerateLeaf(rootCert, rootKey, hosts, config.Deployment.CertYears); + + await PersistAsync(rootCert, rootKey, leafCert, leafKey, secrets.LeafPfxPassword, + rootCrtPath, rootKeyPath, leafCrtPath, leafKeyPath, leafPfxPath, ct).ConfigureAwait(false); + + WriteFingerprintFile(rootFingerprintPath, rootCert); + + // Container process (UID 64198) needs 0644 on bind-mounted files or startup EACCES. + // PFX is password-protected; rootCA.key never leaves the host → 0600 defence-in-depth. + UnixFilePermissions.SetWorldReadable(leafPfxPath, logger, "cert file"); + UnixFilePermissions.SetWorldReadable(leafCrtPath, logger, "cert file"); + UnixFilePermissions.SetWorldReadable(rootCrtPath, logger, "cert file"); + UnixFilePermissions.SetWorldReadable(rootFingerprintPath, logger, "cert file"); + UnixFilePermissions.SetOwnerOnly(rootKeyPath, logger, "key file"); + + rootCert.Dispose(); + leafCert.Dispose(); + rootKey.Dispose(); + leafKey.Dispose(); + + if (config.Deployment.TrustStoreInstall && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + await InstallToTrustStoreAsync(rootCrtPath, logger, ct).ConfigureAwait(false); + } + else + { + logger.Info(" skipping trust-store install (trustStoreInstall=false or non-Linux host)"); + } + + PrintTrustInfo(rootCrtPath, rootFingerprintPath, logger); + + // Rotating an already-distributed CA invalidates trust on every client device. + if (allPresentBeforeRun && options.RotateCerts) + { + logger.Warn("All previously-trusted client devices must re-install the new root CA."); + logger.Warn("Distribute the SHA-256 above out-of-band so users can verify their download."); + } + + logger.PhaseDone(Phase); + } + + private static (X509Certificate2 Cert, RSA Key) GenerateRootCa(string rootCaName, int years, IReadOnlyList hosts) + { + var key = RSA.Create(2048); + var subject = new X500DistinguishedName($"CN={rootCaName}"); + + var req = new CertificateRequest(subject, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + + // pathlen:0 — signs leaves but not intermediates. + req.CertificateExtensions.Add(new X509BasicConstraintsExtension(certificateAuthority: true, hasPathLengthConstraint: true, pathLengthConstraint: 0, critical: true)); + // Name Constraints (RFC 5280 §4.2.1.10, must be critical) cap the blast radius even on + // a full rootCA.key leak — trusted devices reject out-of-permitted names. Compatibility + // trade-off: fails on Android <7, Java <8u101, and a few embedded TLS stacks. + req.CertificateExtensions.Add(BuildNameConstraintsExtension(hosts)); + req.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign, critical: true)); + req.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(req.PublicKey, critical: false)); + + var notBefore = DateTimeOffset.UtcNow.AddMinutes(-5); + var notAfter = notBefore.AddYears(years); + var cert = req.CreateSelfSigned(notBefore, notAfter); + return (cert, key); + } + + private static (X509Certificate2 Cert, RSA Key) GenerateLeaf( + X509Certificate2 issuer, + RSA issuerKey, + IReadOnlyList hosts, + int years) + { + var key = RSA.Create(2048); + // CN := first leaf-eligible host. ConfigPhase.Validate rejects an all-CIDR list first. + var primary = HostParser.PickPrimary(hosts) + ?? throw new InvalidOperationException( + "GenerateLeaf requires at least one non-CIDR host - ConfigPhase.Validate " + + "should have rejected an all-CIDR list before this point."); + var primaryCn = primary.Kind switch + { + HostKind.Dns => primary.DnsName!, + // Unbracketed IPv6 avoids tools that diff against the raw RFC 4514 string. + _ => primary.Ip!.ToString(), + }; + var req = new CertificateRequest(new X500DistinguishedName($"CN={primaryCn}"), key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + + req.CertificateExtensions.Add(new X509BasicConstraintsExtension(certificateAuthority: false, hasPathLengthConstraint: false, pathLengthConstraint: 0, critical: true)); + req.CertificateExtensions.Add(new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment, critical: true)); + req.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension( + new OidCollection + { + new("1.3.6.1.5.5.7.3.1"), // serverAuth + }, + critical: false)); + + var sanBuilder = new SubjectAlternativeNameBuilder(); + foreach (var host in hosts) + { + switch (host.Kind) + { + case HostKind.Dns: + sanBuilder.AddDnsName(host.DnsName!); + break; + case HostKind.Ipv4: + case HostKind.Ipv6: + sanBuilder.AddIpAddress(host.Ip!); + break; + // CIDRs belong only on the root-CA Name Constraints subtree, not on a leaf SAN. + case HostKind.Ipv4Cidr: + case HostKind.Ipv6Cidr: + continue; + } + } + req.CertificateExtensions.Add(sanBuilder.Build()); + + req.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(req.PublicKey, critical: false)); + req.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromCertificate(issuer, includeKeyIdentifier: true, includeIssuerAndSerial: false)); + + var notBefore = DateTimeOffset.UtcNow.AddMinutes(-5); + var requestedNotAfter = notBefore.AddYears(years); + // Clamp leaf NotAfter to (issuer.NotAfter - 1s) so ordering survives PEM truncation. + var issuerCap = new DateTimeOffset(issuer.NotAfter.ToUniversalTime()).AddSeconds(-1); + var notAfter = requestedNotAfter > issuerCap ? issuerCap : requestedNotAfter; + + var serial = RandomNumberGenerator.GetBytes(16); + // High bit clear keeps the unsigned serial's DER encoding positive. + serial[0] &= 0x7F; + + var signed = req.Create(issuer, notBefore, notAfter, serial); + var withKey = signed.CopyWithPrivateKey(key); + signed.Dispose(); + return (withKey, key); + } + + private static async Task PersistAsync( + X509Certificate2 rootCert, + RSA rootKey, + X509Certificate2 leafCert, + RSA leafKey, + string pfxPassword, + string rootCrtPath, + string rootKeyPath, + string leafCrtPath, + string leafKeyPath, + string leafPfxPath, + CancellationToken ct) + { + await File.WriteAllTextAsync(rootCrtPath, rootCert.ExportCertificatePem(), ct).ConfigureAwait(false); + await File.WriteAllTextAsync(rootKeyPath, rootKey.ExportPkcs8PrivateKeyPem(), ct).ConfigureAwait(false); + await File.WriteAllTextAsync(leafCrtPath, leafCert.ExportCertificatePem(), ct).ConfigureAwait(false); + await File.WriteAllTextAsync(leafKeyPath, leafKey.ExportPkcs8PrivateKeyPem(), ct).ConfigureAwait(false); + + // Leaf + key only; the root ships OOB via TrustController against its SHA-256 fingerprint. + var pfxBytes = leafCert.Export(X509ContentType.Pfx, pfxPassword); + await File.WriteAllBytesAsync(leafPfxPath, pfxBytes, ct).ConfigureAwait(false); + } + + + /// Emits X.509 v3 Name Constraints (OID 2.5.29.30, critical) with one + /// permittedSubtrees entry per host. DNS → dNSName [2] IA5String (wildcards + /// collapsed to suffix — * is not a valid IA5String constraint). IP/CIDR → + /// iPAddress [7] OCTET STRING carrying address||mask; single-IP hosts use the + /// all-ones mask. + internal static X509Extension BuildNameConstraintsExtension(IReadOnlyList hosts) + { + if (hosts is null || hosts.Count == 0) + { + throw new ArgumentException( + "Name Constraints require at least one permitted host. ConfigPhase.Validate " + + "rejects an empty deployment.hosts list before this method runs.", + nameof(hosts)); + } + + var writer = new AsnWriter(AsnEncodingRules.DER); + + using (writer.PushSequence()) + { + // permittedSubtrees [0] IMPLICIT GeneralSubtrees + using (writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0))) + { + foreach (var host in hosts) + { + using (writer.PushSequence()) + { + switch (host.Kind) + { + case HostKind.Dns: + // dNSName [2] IMPLICIT IA5String + writer.WriteCharacterString( + UniversalTagNumber.IA5String, + StripWildcardPrefix(host.DnsName!), + new Asn1Tag(TagClass.ContextSpecific, 2)); + break; + case HostKind.Ipv4: + case HostKind.Ipv6: + case HostKind.Ipv4Cidr: + case HostKind.Ipv6Cidr: + // iPAddress [7] IMPLICIT OCTET STRING: payload is address || mask. + writer.WriteOctetString( + HostParser.ToNameConstraintSubtreeBytes(host), + new Asn1Tag(TagClass.ContextSpecific, 7)); + break; + } + } + } + } + } + + return new X509Extension(new Oid("2.5.29.30"), writer.Encode(), critical: true); + } + + /// Strips a leading *. so the result is a valid dNSName constraint; + /// the suffix subtree already matches every host beneath it. + internal static string StripWildcardPrefix(string domain) => + domain.StartsWith("*.", StringComparison.Ordinal) ? domain[2..] : domain; + + /// Writes SHA-256(rootCert.RawData) as colon-separated uppercase hex. Backs the + /// API's /.well-known/interfold-root-ca.sha256 and the cert routes' ETag. + internal static void WriteFingerprintFile(string path, X509Certificate2 cert) + { + var hash = SHA256.HashData(cert.RawData); + var formatted = FormatSha256Fingerprint(hash); + File.WriteAllText(path, formatted + Environment.NewLine); + } + + /// Canonical colon-separated uppercase hex (AA:BB:CC:...) matching + /// openssl x509 -fingerprint -sha256 and browser cert dialogs. + internal static string FormatSha256Fingerprint(byte[] hash) + { + var sb = new StringBuilder(hash.Length * 3); + for (var i = 0; i < hash.Length; i++) + { + if (i > 0) sb.Append(':'); + sb.Append(hash[i].ToString("X2", CultureInfo.InvariantCulture)); + } + return sb.ToString(); + } + + /// Operator-facing trust block: CA name, path, SHA-256, expiry, distribute/verify + /// recipe. Also invoked by show-trust. + internal static void PrintTrustInfo(string rootCrtPath, string rootFingerprintPath, PhaseLogger logger) + { + if (!File.Exists(rootCrtPath)) + { + logger.Warn($"trust info: rootCA.crt missing at {rootCrtPath}"); + return; + } + + using var cert = X509CertificateLoader.LoadCertificateFromFile(rootCrtPath); + + var fingerprint = File.Exists(rootFingerprintPath) + ? File.ReadAllText(rootFingerprintPath).Trim() + : FormatSha256Fingerprint(SHA256.HashData(cert.RawData)); + + // Render just the CN for friendlier output. + var caName = cert.Subject; + if (caName.StartsWith("CN=", StringComparison.Ordinal)) + { + caName = caName[3..]; + var comma = caName.IndexOf(','); + if (comma > 0) caName = caName[..comma]; + } + + logger.Info(""); + logger.Info($" Root CA: {caName}"); + logger.Info($" Path: {rootCrtPath}"); + logger.Info($" SHA-256: {fingerprint}"); + logger.Info($" Not after: {cert.NotAfter.ToUniversalTime():yyyy-MM-dd HH:mm:ss} UTC"); + logger.Info($" Distribute: curl -fSL http://:5000/.well-known/interfold-root-ca.crt -o rootCA.crt"); + logger.Info($" Verify: openssl x509 -in rootCA.crt -noout -fingerprint -sha256"); + logger.Info($" (compare the printed SHA256 Fingerprint to the value above)"); + logger.Info(""); + } + + /// Upgrades an older install in-place: backfills the SHA-256 fingerprint file and + /// re-tightens rootCA.key to 0600. Both idempotent. + internal static void EnsureUpgradeArtefacts(string rootCrtPath, string rootKeyPath, string rootFingerprintPath, PhaseLogger logger) + { + if (!File.Exists(rootFingerprintPath) && File.Exists(rootCrtPath)) + { + using var cert = X509CertificateLoader.LoadCertificateFromFile(rootCrtPath); + WriteFingerprintFile(rootFingerprintPath, cert); + UnixFilePermissions.SetWorldReadable(rootFingerprintPath, logger, "cert file"); + logger.Info($" fingerprint backfilled at {rootFingerprintPath}"); + } + + if (File.Exists(rootKeyPath)) + { + UnixFilePermissions.SetOwnerOnly(rootKeyPath, logger, "key file"); + } + } + + private static async Task InstallToTrustStoreAsync(string rootCrtPath, PhaseLogger logger, CancellationToken ct) + { + if (Directory.Exists(DebianAnchorsDir)) + { + var dst = Path.Combine(DebianAnchorsDir, TrustAnchorFileName); + File.Copy(rootCrtPath, dst, overwrite: true); + var run = await ProcessRunner.RunAsync("update-ca-certificates", [], ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + logger.Warn($"update-ca-certificates exited {run.ExitCode}: {run.StdErr.Trim()}"); + } + else + { + logger.Info(" root CA installed via update-ca-certificates"); + } + } + else if (Directory.Exists(RedHatAnchorsDir)) + { + var dst = Path.Combine(RedHatAnchorsDir, TrustAnchorFileName); + File.Copy(rootCrtPath, dst, overwrite: true); + var run = await ProcessRunner.RunAsync("update-ca-trust", ["extract"], ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + logger.Warn($"update-ca-trust extract exited {run.ExitCode}: {run.StdErr.Trim()}"); + } + else + { + logger.Info(" root CA installed via update-ca-trust extract"); + } + } + else + { + logger.Warn( + "no known trust-store path found (looked for /usr/local/share/ca-certificates and " + + "/etc/pki/ca-trust/source/anchors). Install the generated rootCA.crt manually."); + } + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/ComposeExecPostgresExecutor.cs b/tools/Interfold.Bootstrapper/Phases/ComposeExecPostgresExecutor.cs new file mode 100644 index 00000000..4f1b27fa --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/ComposeExecPostgresExecutor.cs @@ -0,0 +1,143 @@ +using Interfold.Bootstrapper.Util; +using Interfold.DatabaseBootstrap; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// implementation that shells out to +/// docker compose -f <file> exec -T <service> psql .... Used by +/// in the self-hosted deployment path. +/// +/// +/// +/// Holds the retry budget and transient-error classifier here (not in +/// ) so the shared orchestrator only ever sees a clean +/// "success or fatal" outcome — the messy "is shutting down" / "is starting up" window the +/// postgres entrypoint goes through on first boot is absorbed locally. +/// +/// +/// With 20 attempts * 5s = 100s of patience we cover the worst observed checkpoint+sync +/// window (~45s on contended DinD-on-WSL2 storage) with margin to spare, without papering +/// over genuine SQL errors (those return non-transient on the next attempt). +/// +/// +internal sealed class ComposeExecPostgresExecutor(string composeFile, string postgresService, IDatabaseInitLogger logger) + : IPostgresExecutor +{ + private const int PsqlRetryAttempts = 20; + private static readonly TimeSpan PsqlRetryDelay = TimeSpan.FromSeconds(5); + + public Task ExecScriptAsync(string user, string password, string database, string sql, CancellationToken ct) + => RunPsqlWithRetryAsync(user, password, database, sql, vars: null, ct); + + public Task ExecScriptWithVarsAsync( + string user, string password, string database, string sql, + IReadOnlyList<(string Name, string Value)> vars, CancellationToken ct) + => RunPsqlWithRetryAsync(user, password, database, sql, vars, ct); + + public async Task ExecScalarAsync(string user, string password, string database, string sql, CancellationToken ct) + { + // Scalar probes do NOT retry on transient errors — the caller (PostgresSeeder) wraps + // the call site in try/catch and treats any failure as "not yet initialised". Adding + // a retry budget here would make idempotency probes wait 100s on a fresh cluster + // before falling back to "treat as uninitialised", which is the opposite of what we + // want. + var result = await RunPsqlOnceAsync(user, password, database, sql, vars: null, ct).ConfigureAwait(false); + if (result.ExitCode != 0) + { + // Return null instead of throwing so probe callers behave consistently with the + // "empty result" case. Genuine misuse (auth failure with the wrong password) is + // surfaced upstream as "not yet initialised" — same outcome as the original + // bootstrap script's behaviour. + return null; + } + var line = result.StdOut.Trim(); + return string.IsNullOrEmpty(line) ? null : line; + } + + private async Task RunPsqlWithRetryAsync( + string user, string password, string database, string sql, + IReadOnlyList<(string Name, string Value)>? vars, CancellationToken ct) + { + Exception? lastError = null; + for (var attempt = 0; attempt < PsqlRetryAttempts; attempt++) + { + var result = await RunPsqlOnceAsync(user, password, database, sql, vars, ct).ConfigureAwait(false); + if (result.ExitCode == 0) return; + + var err = result.StdErr.Trim(); + if (IsTransientPostgresError(err)) + { + logger.Info( + $" transient psql error (attempt {attempt + 1}/{PsqlRetryAttempts}): {FirstLine(err)}"); + lastError = new InvalidOperationException(err); + await Task.Delay(PsqlRetryDelay, ct).ConfigureAwait(false); + continue; + } + + logger.Error($"psql {database} as {user} failed: {err}"); + throw new InvalidOperationException( + $"psql script failed inside {postgresService} (exit {result.ExitCode})."); + } + + logger.Error($"psql {database} as {user} kept hitting transient errors: {lastError?.Message}"); + throw new InvalidOperationException( + $"psql script failed after {PsqlRetryAttempts} retries inside {postgresService}.", lastError); + } + + private async Task RunPsqlOnceAsync( + string user, string password, string database, string sql, + IReadOnlyList<(string Name, string Value)>? vars, CancellationToken ct) + { + // -e PGPASSWORD avoids putting the password on the visible command line. + // -v ON_ERROR_STOP=1 turns the first SQL error into a non-zero exit (otherwise psql silently continues past + // errors which would mask half-broken bootstraps). -t -A trim leading whitespace + // and the column-alignment padding for scalar probes. + + var env = new Dictionary { { DatabaseArchiveStreamer.PgPasswordEnvVar, password } }; + + var args = new List + { + "-U", user, "-d", database, + "-v", "ON_ERROR_STOP=1", + }; + + if (vars is not null) + { + foreach (var (name, value) in vars) + { + args.Add("-v"); + args.Add($"{name}={value}"); + } + } + args.Add("-t"); + args.Add("-A"); + + return await Util.DockerComposeExec.RunAsync( + composeFile, postgresService, "psql", args, env, stdin: sql, ct: ct).ConfigureAwait(false); + } + + /// + /// Transition states postgres moves through while the entrypoint is bouncing it, plus + /// the classic CREATE DATABASE race against template1. Auth errors, syntax errors etc. + /// are NOT transient and surface as immediate failures. + /// + private static bool IsTransientPostgresError(string stderr) + { + return stderr.Contains("the database system is shutting down", StringComparison.OrdinalIgnoreCase) + || stderr.Contains("the database system is starting up", StringComparison.OrdinalIgnoreCase) + || stderr.Contains("Connection refused", StringComparison.OrdinalIgnoreCase) + || stderr.Contains("could not connect to server", StringComparison.OrdinalIgnoreCase) + || stderr.Contains("is being accessed by other users", StringComparison.OrdinalIgnoreCase) + // Sent to all live connections when postgres receives SIGTERM (fast-shutdown). + // ERRCODE_ADMIN_SHUTDOWN (57P01); a follow-up connect will see "shutting down" + // first, then "starting up", then succeed once normal mode comes up. + || stderr.Contains("terminating connection due to administrator command", StringComparison.OrdinalIgnoreCase); + } + + private static string FirstLine(string s) + { + var idx = s.IndexOfAny(['\r', '\n']); + return idx < 0 ? s : s[..idx]; + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/ComposeExecScyllaExecutor.cs b/tools/Interfold.Bootstrapper/Phases/ComposeExecScyllaExecutor.cs new file mode 100644 index 00000000..afae7083 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/ComposeExecScyllaExecutor.cs @@ -0,0 +1,46 @@ +using Interfold.Bootstrapper.Util; +using Interfold.DatabaseBootstrap; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// implementation that shells out to +/// docker compose -f <file> exec -T <service> cqlsh .... Used by +/// in the self-hosted deployment path. +/// +internal sealed class ComposeExecScyllaExecutor(string composeFile, string scyllaService, IDatabaseInitLogger logger) + : IScyllaExecutor +{ + public async Task ExecCqlAsync(string user, string password, string cql, CancellationToken ct) + { + var result = await RunCqlshAsync(user, password, cql, ct).ConfigureAwait(false); + if (result.ExitCode != 0) + { + logger.Error($"cqlsh on {scyllaService} as {user} failed: {result.StdErr.Trim()}"); + throw new InvalidOperationException( + $"cqlsh command failed inside {scyllaService} (exit {result.ExitCode})."); + } + } + + public async Task TryExecCqlAsync(string user, string password, string cql, CancellationToken ct) + { + var result = await RunCqlshAsync(user, password, cql, ct).ConfigureAwait(false); + // We treat any non-zero exit as "didn't work" without classifying further — the + // seed orchestrator only consumes the boolean + stdout. The bootstrapper integration + // tests verify the cassandra-default lockdown by re-trying lockdown across reboots, + // which exercises both the success and failure branches of this method. + return new ScyllaExecResult(result.ExitCode == 0, result.StdOut); + } + + private async Task RunCqlshAsync(string user, string password, string cql, CancellationToken ct) + { + // -T keeps stdin/stdout unbuffered so we capture auth-failure messages on a failed + // connection attempt. -e takes inline CQL. + var args = new[] + { + "-u", user, "-p", password, + "-e", cql, + }; + return await Util.DockerComposeExec.RunAsync(composeFile, scyllaService, "cqlsh", args, ct: ct).ConfigureAwait(false); + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/ConfigPhase.cs b/tools/Interfold.Bootstrapper/Phases/ConfigPhase.cs new file mode 100644 index 00000000..dd3bde54 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/ConfigPhase.cs @@ -0,0 +1,1276 @@ +using System.Net; +using System.Text.Json; +using System.Text.RegularExpressions; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Configuration; +using Interfold.Shared.Contracts.Configuration.Validation; +using Interfold.Shared.Contracts.Enums; +using Spectre.Console; + +namespace Interfold.Bootstrapper.Phases; + +/// Phase 2 — loads interfold.bootstrap.json or builds it via a Spectre.Console +/// walkthrough (sectioned prompts → review table → edit loop), validates, and persists. +internal static class ConfigPhase +{ + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + const string Phase = "config"; + logger.PhaseStart(Phase); + + var configPath = BootstrapArtifactPaths.ResolveConfigPath(options); + BootstrapConfig config; + if (File.Exists(configPath)) + { + logger.Info($" loading config from {configPath}"); + var json = await File.ReadAllTextAsync(configPath, ct).ConfigureAwait(false); + config = JsonSerializer.Deserialize(json, BootstrapJsonContext.Default.BootstrapConfig) + ?? throw new InvalidOperationException($"Failed to parse {configPath} (returned null)."); + + if (options.Reconfigure) + { + if (options.NonInteractive || Console.IsInputRedirected) + { + logger.PhaseFail(Phase, PhaseFailureReasons.ReconfigureRequiresInteractive); + throw new InvalidOperationException( + "--reconfigure requires an interactive TTY. " + + "Omit --non-interactive and run from a terminal (stdin must not be redirected)."); + } + + logger.Info(" --reconfigure: opening interactive editor seeded from existing config"); + var mdnsHostname = await ApplyPreFillMdnsCheckAsync(options, logger, ct).ConfigureAwait(false); + config = PromptForConfig( + AnsiConsole.Console, + maskSecrets: true, + hostnameProbe: () => mdnsHostname, + existing: config); + await PersistAsync(config, configPath, ct).ConfigureAwait(false); + } + } + else if (options.NonInteractive) + { + logger.PhaseFail(Phase, PhaseFailureReasons.MissingConfigNonInteractive); + throw new InvalidOperationException( + $"Config file not found at {configPath} and --non-interactive was set. " + + "Provide --config or rerun interactively."); + } + else if (!Console.IsInputRedirected) + { + logger.Info($" no config at {configPath}; entering interactive setup"); + // mdnsHostname is null when .local won't resolve, so we don't pre-fill a broken name. + var mdnsHostname = await ApplyPreFillMdnsCheckAsync(options, logger, ct).ConfigureAwait(false); + + // Tests pass maskSecrets: false so Spectre's ReadKey secret path doesn't + // fight the TestConsole input queue. + config = PromptForConfig( + AnsiConsole.Console, + maskSecrets: true, + hostnameProbe: () => mdnsHostname); + await PersistAsync(config, configPath, ct).ConfigureAwait(false); + } + else + { + logger.PhaseFail(Phase, PhaseFailureReasons.MissingConfigNoTty); + throw new InvalidOperationException( + $"Config file not found at {configPath} and no TTY is available. " + + "Run with --config pointing at a populated interfold.bootstrap.json."); + } + + Validate(config); + + // Post-fill mDNS gate is bootstrap-only — other subcommands load JSON verbatim. + if (options.Command == BootstrapCommand.Bootstrap) + { + var mutated = await ApplyMdnsGateAsync(config, options, logger, ct).ConfigureAwait(false); + if (mutated) + { + // Re-validate — a strip could have emptied the list, and issuing a cert with + // zero SANs later would be a harder failure to diagnose. + Validate(config); + logger.Info($" updated {configPath} to reflect the mDNS strip"); + await PersistAsync(config, configPath, ct).ConfigureAwait(false); + } + } + + if (!string.Equals(Path.GetFullPath(config.Deployment.OutputDir), options.OutputDir, StringComparison.Ordinal)) + { + logger.Info($" overriding config.outputDir with --output-dir={options.OutputDir}"); + config.Deployment.OutputDir = options.OutputDir; + } + + logger.PhaseDone(Phase); + return config; + } + + /// Spectre.Console navigable form for . Each field + /// is a menu row; arrows navigate, Enter edits, trailing Confirm and save returns. + /// Test seams: and + /// default to real detection but are stubbed out in unit tests for determinism. + /// 's value MUST land first in the seed list so + /// latches the mDNS name for the leaf cert. + /// When is supplied (e.g. --reconfigure), the form + /// opens pre-filled; hostname/IP auto-seed only applies when Hosts is still empty. + internal static BootstrapConfig PromptForConfig( + IAnsiConsole console, + bool maskSecrets = false, + Func? localAddressProbe = null, + Func? hostnameProbe = null, + BootstrapConfig? existing = null) + { + var c = existing ?? new BootstrapConfig(); + + // Hostname before IP so HostParser.PickPrimary latches the mDNS name. + if (c.Deployment.Hosts.Count == 0) + { + var seed = new List(); + var hostname = (hostnameProbe ?? (() => null))(); + if (hostname is not null) + { + seed.Add(hostname); + } + var detected = (localAddressProbe ?? LocalAddressDetector.TryDetectPrimaryIp)(); + if (detected is not null) + { + // Bare literal; HostParser.ToUrlHost brackets IPv6 later. + seed.Add(detected.ToString()); + } + if (seed.Count > 0) + { + c.Deployment.Hosts = seed; + } + } + + // Local helpers close over `console` and `maskSecrets` to keep each Edit lambda short. + string PromptStr(string label, string fallback) => console.Prompt( + new TextPrompt($"{label}:") + .DefaultValue(fallback) + .AllowEmpty()); + + int PromptInt(string label, int fallback, int min, int max) => console.Prompt( + new TextPrompt($"{label}:") + .DefaultValue(fallback) + .ValidationErrorMessage($"[red]must be an integer in [[{min}..{max}]][/]") + .Validate(n => n >= min && n <= max)); + + bool PromptBool(string label, bool fallback) => console.Prompt( + new ConfirmationPrompt($"{label}?") { DefaultValue = fallback }); + + string PromptOAuth(string label, string fallback) + { + var p = new TextPrompt($"{label} (blank to skip):") + .DefaultValue(fallback) + .AllowEmpty(); + if (maskSecrets) p.Secret('*'); + return console.Prompt(p); + } + + // Blank → null (use API default). Manual TextPrompt because Spectre lacks TextPrompt. + int? PromptNullableInt(string label, int? fallback, int min, int max) + { + var fallbackText = fallback?.ToString() ?? string.Empty; + var raw = console.Prompt( + new TextPrompt($"{label} (blank for default):") + .DefaultValue(fallbackText) + .AllowEmpty() + .Validate(s => + { + if (string.IsNullOrWhiteSpace(s)) return ValidationResult.Success(); + if (!int.TryParse(s, out var parsed)) + { + return ValidationResult.Error("[red]must be a whole number or blank[/]"); + } + return parsed >= min && parsed <= max + ? ValidationResult.Success() + : ValidationResult.Error($"[red]must be in [[{min}..{max}]] or blank[/]"); + })); + return string.IsNullOrWhiteSpace(raw) ? null : int.Parse(raw); + } + + // Grouped fields — the flat `fields` list and `sections` array are DERIVED from + // this so headers can't drift. Tuple is (Label, Show, Edit). + (string Header, (string Label, Func Show, Action Edit)[] Fields) Group( + string header, + params (string Label, Func Show, Action Edit)[] fs) => (header, fs); + + var groups = new[] + { + Group("Deployment", + ("Output directory", () => c.Deployment.OutputDir, + () => c.Deployment.OutputDir = PromptStr("Output directory", c.Deployment.OutputDir)), + ("Public host(s)", () => string.Join(",", c.Deployment.Hosts), + () => c.Deployment.Hosts = PromptHosts(console, c.Deployment.Hosts)), + ("Root CA subject", () => c.Deployment.RootCaName, + () => c.Deployment.RootCaName = PromptStr("Root CA subject", c.Deployment.RootCaName)), + ("Leaf cert validity (years)", () => c.Deployment.CertYears.ToString(), + () => c.Deployment.CertYears = PromptInt("Leaf cert validity (years)", c.Deployment.CertYears, 1, 30)), + ("Install root CA in trust store", () => c.Deployment.TrustStoreInstall.ToString(), + () => c.Deployment.TrustStoreInstall = PromptBool("Install root CA into system trust store", c.Deployment.TrustStoreInstall)), + // Publish wiring auto-promotes IncludeWeb when WebHttps is true. + ("Include octocon-web container", () => c.Deployment.IncludeWeb.ToString(), + () => c.Deployment.IncludeWeb = PromptBool("Include the octocon-web (Kotlin/Wasm UI) container", c.Deployment.IncludeWeb)), + ("Terminate HTTPS at octocon-web", () => c.Deployment.WebHttps.ToString(), + () => c.Deployment.WebHttps = PromptBool("Terminate HTTPS at octocon-web", c.Deployment.WebHttps))), + + Group("Ports", + ("API HTTP port", () => c.Ports.ApiHttp.ToString(), + () => c.Ports.ApiHttp = PromptInt("API HTTP port", c.Ports.ApiHttp, 1, 65535)), + ("API HTTPS port", () => c.Ports.ApiHttps.ToString(), + () => c.Ports.ApiHttps = PromptInt("API HTTPS port", c.Ports.ApiHttps, 1, 65535)), + ("Web HTTP port", () => c.Ports.WebHttp.ToString(), + () => c.Ports.WebHttp = PromptInt("Web HTTP port", c.Ports.WebHttp, 1, 65535)), + ("Web HTTPS port", () => c.Ports.WebHttps.ToString(), + () => c.Ports.WebHttps = PromptInt("Web HTTPS port", c.Ports.WebHttps, 1, 65535)), + ("Postgres host port", () => c.Ports.Postgres.ToString(), + () => c.Ports.Postgres = PromptInt("Postgres host port", c.Ports.Postgres, 1, 65535)), + ("Scylla/Cassandra host port", () => c.Ports.Scylla.ToString(), + () => c.Ports.Scylla = PromptInt("Scylla/Cassandra host port", c.Ports.Scylla, 1, 65535))), + + Group("Database", + ("Database mode", () => c.DatabaseMode.ToWire(), + () => c.DatabaseMode = console.Prompt( + new TextPrompt("Database mode:") + .DefaultValue(c.DatabaseMode.ToWire()) + .AddChoices(ValidDatabaseModes)).TryParseWire(out var mode) ? mode : c.DatabaseMode), + ("Postgres application DB name", () => c.PostgresDatabase, + () => c.PostgresDatabase = PromptStr("Postgres application DB name", c.PostgresDatabase)), + ("Cluster name", () => c.ClusterName, + () => c.ClusterName = PromptStr("Cluster name (Scylla/Cassandra)", c.ClusterName)), + // AddChoices enforces the seven valid keyspaces (Validate mirrors this non-interactively). + ("Scylla keyspace (region)", () => c.ScyllaKeyspace.ToWire(), + () => c.ScyllaKeyspace = EnumWireExtensions.ParseScyllaKeyspace(console.Prompt( + new TextPrompt("Scylla keyspace (region):") + .DefaultValue(c.ScyllaKeyspace.ToWire()) + .AddChoices(ValidScyllaKeyspaces))))), + + // Derivable rows snapshot into ResolveDerivedDefaults so the menu paints the + // computed default before Enter. + Group("API", + ("OAuth callback base URL", () => DerivedShow(c, ar => ar.CallbackBaseUrl), + () => c.ApiRuntime.CallbackBaseUrl = PromptStr( + "OAuth callback base URL", + DerivedShow(c, ar => ar.CallbackBaseUrl))), + ("JWT authority (iss claim)", () => DerivedShow(c, ar => ar.JwtAuthority), + () => c.ApiRuntime.JwtAuthority = PromptStr( + "JWT authority (iss claim)", + DerivedShow(c, ar => ar.JwtAuthority))), + ("JWT audience (aud claim)", () => c.ApiRuntime.JwtAudience, + () => c.ApiRuntime.JwtAudience = PromptStr( + "JWT audience (aud claim)", c.ApiRuntime.JwtAudience)), + ("CORS allowed origins", () => string.Join(",", DerivedCorsShow(c)), + () => c.ApiRuntime.CorsAllowedOrigins = PromptCorsAllowedOrigins( + console, DerivedCorsShow(c))), + ("Pre-built Interfold API image", () => c.ApiImage, + () => c.ApiImage = PromptStr("Pre-built Interfold API image reference", c.ApiImage))), + + Group("Cluster & telemetry", + ("Cluster node group", () => c.Cluster.NodeGroup.ToWire(), + () => c.Cluster.NodeGroup = EnumWireExtensions.ParseNodeGroup(console.Prompt( + new TextPrompt("Cluster node group:") + .DefaultValue(c.Cluster.NodeGroup.ToWire()) + .AddChoices(ValidNodeGroups)))), + ("OTLP endpoint", () => ShowOrEmpty(c.Observability.OtlpEndpoint), + () => c.Observability.OtlpEndpoint = PromptStr( + "OTLP endpoint (blank to disable)", + c.Observability.OtlpEndpoint))), + + // Storage: blank AvatarStorageRoot → {outputDir}/data/avatars bind-mounted at + // /app/data/avatars. Blank AvatarPublicBase → API serves /avatars/* directly. + Group("Storage", + ("Avatar storage root (host path)", () => ShowOrEmpty(c.Storage.AvatarStorageRoot), + () => c.Storage.AvatarStorageRoot = PromptStr( + "Avatar storage root (blank = {outputDir}/data/avatars, bind-mounted into the API; non-blank = absolute host path)", + c.Storage.AvatarStorageRoot)), + ("Avatar public base URL", () => ShowOrEmpty(c.Storage.AvatarPublicBase), + () => c.Storage.AvatarPublicBase = PromptStr( + "Avatar public base URL (blank = API serves /avatars/* directly; set https URL to delegate to CDN)", + c.Storage.AvatarPublicBase))), + + Group("Performance tuning", + // Blank → null (API compile-time default); Show renders "" for null. + ("Socket batch flush threshold (bytes)", + () => c.Socket.BatchBytesThreshold?.ToString() ?? "", + () => c.Socket.BatchBytesThreshold = PromptNullableInt( + "Socket batch flush threshold (bytes)", + c.Socket.BatchBytesThreshold, 1, 16 * 1024 * 1024)), + ("DB retry attempts", () => c.Persistence.DbRetryAttempts.ToString(), + () => c.Persistence.DbRetryAttempts = PromptInt( + "DB retry attempts", c.Persistence.DbRetryAttempts, 1, 100)), + ("DB retry initial delay (ms)", () => c.Persistence.DbRetryInitialDelayMs.ToString(), + () => c.Persistence.DbRetryInitialDelayMs = PromptInt( + "DB retry initial delay (ms)", + c.Persistence.DbRetryInitialDelayMs, 1, 60_000)), + ("DB retry max delay (ms)", () => c.Persistence.DbRetryMaxDelayMs.ToString(), + () => c.Persistence.DbRetryMaxDelayMs = PromptInt( + "DB retry max delay (ms)", + c.Persistence.DbRetryMaxDelayMs, 1, 600_000)), + ("Hydration max concurrency", () => c.Persistence.HydrationMaxConcurrency.ToString(), + () => c.Persistence.HydrationMaxConcurrency = PromptInt( + "Hydration max concurrency", + c.Persistence.HydrationMaxConcurrency, 1, 1024))), + + // ID (public → ShowOrEmpty) then secret (masked → /) per provider. + Group("OAuth credentials", + ("Google OAuth client ID", () => ShowOrEmpty(c.OAuth.GoogleClientId), + () => c.OAuth.GoogleClientId = PromptStr("Google OAuth client ID", c.OAuth.GoogleClientId)), + ("Google OAuth client secret", () => Mask(c.OAuth.GoogleClientSecret), + () => c.OAuth.GoogleClientSecret = PromptOAuth("Google OAuth client secret", c.OAuth.GoogleClientSecret)), + ("Discord OAuth client ID", () => ShowOrEmpty(c.OAuth.DiscordClientId), + () => c.OAuth.DiscordClientId = PromptStr("Discord OAuth client ID", c.OAuth.DiscordClientId)), + ("Discord OAuth client secret", () => Mask(c.OAuth.DiscordClientSecret), + () => c.OAuth.DiscordClientSecret = PromptOAuth("Discord OAuth client secret", c.OAuth.DiscordClientSecret)), + ("Apple OAuth client ID", () => ShowOrEmpty(c.OAuth.AppleClientId), + () => c.OAuth.AppleClientId = PromptStr("Apple OAuth client ID", c.OAuth.AppleClientId)), + ("Apple OAuth client secret", () => Mask(c.OAuth.AppleClientSecret), + () => c.OAuth.AppleClientSecret = PromptOAuth("Apple OAuth client secret", c.OAuth.AppleClientSecret))), + + Group("Backup & autostart", + ("Scheduled backups enabled", () => c.Backup.Enabled.ToString(), + () => c.Backup.Enabled = PromptBool("Enable scheduled backups (systemd timer)", c.Backup.Enabled)), + ("Backup schedule (OnCalendar)", () => c.Backup.Schedule, + () => c.Backup.Schedule = PromptStr("Backup schedule (systemd OnCalendar, e.g. 'daily', 'weekly', 'Mon..Fri 03:30')", c.Backup.Schedule)), + ("Backup retention (count per component)", () => c.Backup.RetainCount.ToString(), + () => c.Backup.RetainCount = PromptInt("Backup retention (number of archives to keep per component)", c.Backup.RetainCount, 1, 1000)), + // Blank = {outputDir}/backups; non-blank must be absolute (systemd CWD unpredictable). + ("Backup directory (absolute, blank=default)", () => ShowOrEmpty(c.Backup.Directory), + () => c.Backup.Directory = PromptStr("Backup directory (absolute path; leave blank to default to {outputDir}/backups)", c.Backup.Directory)), + ("Autostart server on boot", () => c.Backup.AutostartServer.ToString(), + () => c.Backup.AutostartServer = PromptBool("Autostart the server on host boot (installs interfold.service)", c.Backup.AutostartServer))), + + // Chains via systemd OnSuccess= drop-in. Off by default; manual works regardless. + Group("Updates", + ("Chain updates after backup", () => c.Update.Enabled.ToString(), + () => c.Update.Enabled = PromptBool("Chain interfold-update.service after each successful backup", c.Update.Enabled)), + ("Health-check timeout (seconds)", () => c.Update.HealthCheckTimeoutSeconds.ToString(), + () => c.Update.HealthCheckTimeoutSeconds = PromptInt("Health-check timeout after pull+recreate (seconds)", c.Update.HealthCheckTimeoutSeconds, 1, 3600)), + ("Auto-restore on failure", () => c.Update.AutoRestoreOnFailure.ToString(), + () => c.Update.AutoRestoreOnFailure = PromptBool("Auto-restore the pre-update backup on health-check failure (destructive)", c.Update.AutoRestoreOnFailure)), + ("Recreate containers on update", () => c.Update.RecreateOnUpdate.ToString(), + () => c.Update.RecreateOnUpdate = PromptBool("Recreate containers on update (uses 'up -d'; disable only for staged pulls)", c.Update.RecreateOnUpdate)), + ("Update service whitelist (blank=all)", () => c.Update.Services.Length == 0 ? "" : string.Join(",", c.Update.Services), + () => c.Update.Services = PromptUpdateServices(console, c.Update.Services))), + + // One row → three-way wizard (auto-detect / per-file / clear). See PromptFirebase. + Group("Firebase", + ("Firebase push notifications", () => ShowFirebaseState(c.Firebase), + () => PromptFirebase(console, c.Firebase))), + }; + + // Flatten groups; record each group's starting offset for AddChoiceGroup and header sentinels. + var fields = new List<(string Label, Func Show, Action Edit)>(); + var sectionsList = new List<(int FirstFieldIndex, string Header)>(groups.Length); + foreach (var g in groups) + { + sectionsList.Add((fields.Count, g.Header)); + fields.AddRange(g.Fields); + } + var sections = sectionsList.ToArray(); + + // Sentinels: -1 Confirm, 0..N-1 field indices, -(2+sectionIdx) inert header (must + // be unique per section for AddChoiceGroup's key contract). + const int ConfirmSentinel = -1; + int SectionHeaderSentinel(int sectionIdx) => -(2 + sectionIdx); + int SectionLength(int sectionIdx) => + (sectionIdx + 1 < sections.Length ? sections[sectionIdx + 1].FirstFieldIndex : fields.Count) + - sections[sectionIdx].FirstFieldIndex; + + while (true) + { + var prompt = new SelectionPrompt() + .Title( + "[bold]Configure interfold.bootstrap.json[/]\n" + + "[grey]Use arrow keys to navigate, Enter to edit, choose [green]Confirm and save[/] when done.[/]") + // ~60 rows (48 fields + 11 headers + 1 confirm) — oversize so large TTYs + // don't scroll; smaller TTYs paginate via MoreChoicesText. + .PageSize(60) + .MoreChoicesText("[grey](move up/down to reveal more)[/]") + // Escape values — a `[` in operator input would otherwise crash Spectre's parser. + .UseConverter(i => + { + if (i == ConfirmSentinel) return "[green]Confirm and save[/]"; + if (i < 0) + { + var sectionIdx = -(i + 2); + return $"[bold]--- {sections[sectionIdx].Header} ---[/]"; + } + var label = fields[i].Label.PadRight(40); + var value = Markup.Escape(fields[i].Show()); + return $"{label} [grey]{value}[/]"; + }); + + for (var s = 0; s < sections.Length; s++) + { + var firstIdx = sections[s].FirstFieldIndex; + var sectionFieldIdx = Enumerable.Range(firstIdx, SectionLength(s)).ToArray(); + prompt.AddChoiceGroup(SectionHeaderSentinel(s), sectionFieldIdx); + } + prompt.AddChoices(ConfirmSentinel); + + var selected = console.Prompt(prompt); + if (selected == ConfirmSentinel) break; + fields[selected].Edit(); + } + + return c; + } + + /// Comma-separated hosts prompt (DNS / IP / CIDR). + private static List PromptHosts(IAnsiConsole console, List fallback) + { + var fallbackText = string.Join(",", fallback); + var raw = console.Prompt( + new TextPrompt("Public host(s) (domain, IP, or CIDR), comma separated:") + .DefaultValue(fallbackText) + .AllowEmpty() + .Validate(s => + { + var trimmed = s?.Trim() ?? string.Empty; + if (string.IsNullOrEmpty(trimmed)) + { + // Empty only OK when the fallback is non-empty; fresh bootstrap + // must force input so we never issue a cert with no SANs. + return fallback.Count == 0 + ? ValidationResult.Error("[red]at least one host required (no default to fall back on)[/]") + : ValidationResult.Success(); + } + var parts = trimmed.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length == 0) + { + return ValidationResult.Error("[red]at least one host required[/]"); + } + foreach (var part in parts) + { + try + { + HostParser.Parse(part); + } + catch (FormatException ex) + { + return ValidationResult.Error($"[red]{ex.Message}[/]"); + } + } + return ValidationResult.Success(); + })); + + if (string.IsNullOrWhiteSpace(raw)) return fallback; + return raw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + } + + /// CORS allow-list prompt; each entry must be an absolute http(s) URI (matches + /// ). + private static List PromptCorsAllowedOrigins(IAnsiConsole console, List fallback) + { + var fallbackText = string.Join(",", fallback); + var raw = console.Prompt( + new TextPrompt("CORS allowed origins, comma separated:") + .DefaultValue(fallbackText) + .AllowEmpty() + .Validate(s => + { + if (string.IsNullOrWhiteSpace(s)) return ValidationResult.Success(); + var parts = s.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length == 0) + { + return ValidationResult.Error("[red]at least one origin required[/]"); + } + foreach (var part in parts) + { + if (!Uri.TryCreate(part, UriKind.Absolute, out var uri) + || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) + { + return ValidationResult.Error($"[red]'{part}' is not a valid http(s) origin[/]"); + } + } + return ValidationResult.Success(); + })); + + if (string.IsNullOrWhiteSpace(raw)) return fallback; + return raw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + } + + /// Blank = every service; non-empty is validated against + /// so typos don't surface later as "no such service". + private static string[] PromptUpdateServices(IAnsiConsole console, string[] fallback) + { + var fallbackText = string.Join(",", fallback); + var raw = console.Prompt( + new TextPrompt( + "Update service whitelist (comma-separated; blank = every service):") + .DefaultValue(fallbackText) + .AllowEmpty() + .Validate(s => + { + if (string.IsNullOrWhiteSpace(s)) return ValidationResult.Success(); + var parts = s.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + foreach (var part in parts) + { + if (!ValidUpdateServices.Contains(part, StringComparer.Ordinal)) + { + return ValidationResult.Error( + $"[red]'{part}' is not a known compose service. Expected one of: {string.Join(", ", ValidUpdateServices)}[/]"); + } + } + return ValidationResult.Success(); + })); + + if (string.IsNullOrWhiteSpace(raw)) return []; + return raw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToArray(); + } + + /// Menu-row summary: off or N/4 configured (...). + private static string ShowFirebaseState(FirebaseSection section) + { + var platforms = new List(4); + if (!string.IsNullOrEmpty(section.AndroidConfigPath)) platforms.Add("android"); + if (!string.IsNullOrEmpty(section.IosConfigPath)) platforms.Add("ios"); + if (!string.IsNullOrEmpty(section.WebConfigPath)) platforms.Add("web"); + if (!string.IsNullOrEmpty(section.ServiceAccountPath)) platforms.Add("service_account"); + return platforms.Count == 0 + ? "off" + : $"{platforms.Count}/4 configured ({string.Join(", ", platforms)})"; + } + + // Referenced by exact string in tests — drift breaks the interactive seam silently. + private const string FirebaseChoiceAutoDetect = "Auto-detect from folder"; + private const string FirebaseChoicePerFile = "Configure per-file"; + private const string FirebaseChoiceClear = "Clear all"; + private const string FirebaseChoiceCancel = "Cancel"; + + /// Auto-detect / per-file / clear / cancel wizard. Mutates + /// in place so the menu redraws with the new summary. + private static void PromptFirebase(IAnsiConsole console, FirebaseSection section) + { + var choice = console.Prompt( + new SelectionPrompt() + .Title("[bold]Firebase push notifications[/]") + .AddChoices(FirebaseChoiceAutoDetect, FirebaseChoicePerFile, FirebaseChoiceClear, FirebaseChoiceCancel)); + + switch (choice) + { + case FirebaseChoiceAutoDetect: + PromptFirebaseFolder(console, section); + break; + + case FirebaseChoicePerFile: + section.AndroidConfigPath = PromptFirebasePath(console, + "Path to google-services.json", section.AndroidConfigPath); + section.IosConfigPath = PromptFirebasePath(console, + "Path to GoogleService-Info.plist", section.IosConfigPath); + section.WebConfigPath = PromptFirebasePath(console, + "Path to firebase-web-config.json", section.WebConfigPath); + section.ServiceAccountPath = PromptFirebasePath(console, + "Path to FCM v1 service-account JSON", section.ServiceAccountPath); + break; + + case FirebaseChoiceClear: + section.AndroidConfigPath = string.Empty; + section.IosConfigPath = string.Empty; + section.WebConfigPath = string.Empty; + section.ServiceAccountPath = string.Empty; + break; + } + } + + /// Folder branch: scans, writes resolved paths, prints a found/missing summary. + /// Blank aborts silently. + private static void PromptFirebaseFolder(IAnsiConsole console, FirebaseSection section) + { + var folder = console.Prompt( + new TextPrompt("Folder containing Firebase files (blank to cancel):") + .AllowEmpty() + .Validate(s => + { + if (string.IsNullOrWhiteSpace(s)) return ValidationResult.Success(); + return Directory.Exists(s) + ? ValidationResult.Success() + : ValidationResult.Error($"[red]'{s}' is not an existing directory[/]"); + })); + if (string.IsNullOrWhiteSpace(folder)) return; + + var result = FirebaseFolderScanner.Scan(folder); + section.AndroidConfigPath = result.Section.AndroidConfigPath; + section.IosConfigPath = result.Section.IosConfigPath; + section.WebConfigPath = result.Section.WebConfigPath; + section.ServiceAccountPath = result.Section.ServiceAccountPath; + + RenderFirebaseScanSummary(console, result); + } + + /// Blank leaves the platform unwired; non-blank must resolve to an existing + /// file so a typo doesn't silently disable push. + private static string PromptFirebasePath(IAnsiConsole console, string label, string fallback) => + console.Prompt( + new TextPrompt($"{label} (blank to skip):") + .DefaultValue(fallback) + .AllowEmpty() + .Validate(s => + { + if (string.IsNullOrWhiteSpace(s)) return ValidationResult.Success(); + return File.Exists(s) + ? ValidationResult.Success() + : ValidationResult.Error($"[red]'{s}' does not exist[/]"); + })); + + /// Two-column table showing which platforms auto-detect resolved. + private static void RenderFirebaseScanSummary(IAnsiConsole console, FirebaseFolderScanResult result) + { + var table = new Table() + .AddColumn("Platform") + .AddColumn("Resolved path"); + + AddScanRow(table, "android", result.Section.AndroidConfigPath); + AddScanRow(table, "ios", result.Section.IosConfigPath); + AddScanRow(table, "web", result.Section.WebConfigPath); + AddScanRow(table, "service_account", result.Section.ServiceAccountPath); + + console.Write(table); + if (result.Missing.Count > 0) + { + console.MarkupLine( + $"[yellow]Missing: {string.Join(", ", result.Missing)}. " + + "Re-open the wizard and choose 'Configure per-file' to point at them individually.[/]"); + } + } + + private static void AddScanRow(Table table, string platform, string path) + { + if (string.IsNullOrEmpty(path)) + { + table.AddRow($"[grey]{platform}[/]", "[grey]-[/]"); + } + else + { + table.AddRow(platform, Markup.Escape(path)); + } + } + + /// Renders derived default when the field is empty; operator value wins otherwise. + /// One helper for CallbackBaseUrl + JwtAuthority via the selector. + private static string DerivedShow(BootstrapConfig c, Func selector) + { + var snapshot = CloneForDerivation(c); + ResolveDerivedDefaults(snapshot); + return selector(snapshot.ApiRuntime); + } + + /// List-shaped sibling of for the CORS field. + private static List DerivedCorsShow(BootstrapConfig c) + { + var snapshot = CloneForDerivation(c); + ResolveDerivedDefaults(snapshot); + return snapshot.ApiRuntime.CorsAllowedOrigins; + } + + /// Throwaway snapshot for Show callbacks so derivation doesn't mutate live config. + private static BootstrapConfig CloneForDerivation(BootstrapConfig c) + { + return new BootstrapConfig + { + Deployment = new DeploymentSection + { + Hosts = c.Deployment.Hosts, + WebHttps = c.Deployment.WebHttps, + }, + ApiRuntime = new ApiRuntimeSection + { + CallbackBaseUrl = c.ApiRuntime.CallbackBaseUrl, + JwtAuthority = c.ApiRuntime.JwtAuthority, + JwtAudience = c.ApiRuntime.JwtAudience, + CorsAllowedOrigins = [.. c.ApiRuntime.CorsAllowedOrigins], + }, + }; + } + + /// Never echoes the raw secret — supplements the field-level Secret('*') mask. + private static string Mask(string secret) => + string.IsNullOrEmpty(secret) ? "" : ""; + + /// Renders empty as <empty> so operators can tell unset from missing. + private static string ShowOrEmpty(string value) => + string.IsNullOrEmpty(value) ? "" : value; + + /// Wire spellings of every . Must stay aligned with + /// InterfoldAppHost.Configure's region list and . + internal static readonly string[] ValidScyllaKeyspaces = Enum + .GetValues() + .Select(k => k.ToWire()) + .ToArray(); + + internal static readonly string[] ValidNodeGroups = Enum + .GetValues() + .Select(g => g.ToWire()) + .ToArray(); + + internal static readonly string[] ValidDatabaseModes = Enum + .GetValues() + .Select(m => m.ToWire()) + .ToArray(); + + /// Must match builder.AddContainer(...) names in + /// InterfoldAppHost.Configure; the regional entries mirror . + internal static readonly string[] ValidUpdateServices = ComposeServices.AllValidUpdateServices; + + private const int DefaultHttpPort = 80; + private const int DefaultHttpsPort = 443; + + /// Fills empty fields from + /// + . Non-empty values win; + /// idempotent; mutates in place. Callback/JWT-authority always https (the API's Kestrel + /// terminates TLS unconditionally). CORS uses the web tier's scheme + port. Called + /// from so JSON-load configs derive identically to prompted ones. + internal static void ResolveDerivedDefaults(BootstrapConfig config) + { + if (config.Deployment.Hosts.Count == 0) + { + return; + } + + // Silent on parse failure — this runs on every menu redraw (mid-edit is common); + // Validate is the hard-fail path. + var parsed = new List(config.Deployment.Hosts.Count); + foreach (var raw in config.Deployment.Hosts) + { + if (string.IsNullOrWhiteSpace(raw)) continue; + try + { + parsed.Add(HostParser.Parse(raw)); + } + catch (FormatException) + { + } + } + + var primary = HostParser.PickPrimary(parsed); + if (primary is null) + { + // All CIDR / empty after filtering — Validate rejects later. + return; + } + + // Port suffix omitted at 443 so proxy-fronted stacks get clean URLs. + var apiPortSuffix = config.Ports.ApiHttps == DefaultHttpsPort + ? string.Empty + : $":{config.Ports.ApiHttps}"; + var apiDerivedBaseUrl = $"https://{HostParser.ToUrlHost(primary)}{apiPortSuffix}"; + + if (string.IsNullOrWhiteSpace(config.ApiRuntime.CallbackBaseUrl)) + { + config.ApiRuntime.CallbackBaseUrl = apiDerivedBaseUrl; + } + + if (string.IsNullOrWhiteSpace(config.ApiRuntime.JwtAuthority)) + { + config.ApiRuntime.JwtAuthority = apiDerivedBaseUrl; + } + + if (config.ApiRuntime.CorsAllowedOrigins.Count == 0) + { + // CORS follows the web tier's scheme + port; suffix omitted at 80/443. + var webHttps = config.Deployment.WebHttps; + var webScheme = webHttps ? "https" : "http"; + var webPort = webHttps ? config.Ports.WebHttps : config.Ports.WebHttp; + var webDefaultPort = webHttps ? DefaultHttpsPort : DefaultHttpPort; + var webPortSuffix = webPort == webDefaultPort ? string.Empty : $":{webPort}"; + + config.ApiRuntime.CorsAllowedOrigins = parsed + .Where(h => h.IsLeafEligible) + .Select(h => $"{webScheme}://{HostParser.ToUrlHost(h)}{webPortSuffix}") + .ToList(); + } + } + + /// Throws on the first broken invariant with an operator-readable message. + /// Internal for direct test-driven failure paths. + internal static void Validate(BootstrapConfig config) + { + if (config.Deployment.Hosts.Count == 0) + { + throw new InvalidOperationException( + "config.deployment.hosts must contain at least one host (DNS name, IP literal, or CIDR)."); + } + + var parsedHosts = new List(config.Deployment.Hosts.Count); + foreach (var raw in config.Deployment.Hosts) + { + try + { + parsedHosts.Add(HostParser.Parse(raw)); + } + catch (FormatException ex) + { + throw new InvalidOperationException( + $"config.deployment.hosts: {ex.Message}", ex); + } + } + if (!parsedHosts.Any(h => h.IsLeafEligible)) + { + throw new InvalidOperationException( + "config.deployment.hosts must contain at least one non-CIDR entry to serve as the " + + "primary host (leaf cert CN, nginx server_name, and derived URL defaults). " + + "CIDR blocks restrict the root CA's Name Constraints but cannot stand alone."); + } + + if (config.Deployment.CertYears is < 1 or > 30) + { + throw new InvalidOperationException( + $"config.deployment.certYears={config.Deployment.CertYears} is outside the allowed 1..30 range."); + } + + ValidatePort(config.Ports.ApiHttp, nameof(config.Ports.ApiHttp)); + ValidatePort(config.Ports.ApiHttps, nameof(config.Ports.ApiHttps)); + ValidatePort(config.Ports.WebHttp, nameof(config.Ports.WebHttp)); + ValidatePort(config.Ports.WebHttps, nameof(config.Ports.WebHttps)); + ValidatePort(config.Ports.Postgres, nameof(config.Ports.Postgres)); + ValidatePort(config.Ports.Scylla, nameof(config.Ports.Scylla)); + + // Compose binds each host port once; catch collisions here, not as + // "port already allocated" mid-launch. + var portFields = new (string Name, int Port)[] + { + (nameof(config.Ports.ApiHttp), config.Ports.ApiHttp), + (nameof(config.Ports.ApiHttps), config.Ports.ApiHttps), + (nameof(config.Ports.WebHttp), config.Ports.WebHttp), + (nameof(config.Ports.WebHttps), config.Ports.WebHttps), + (nameof(config.Ports.Postgres), config.Ports.Postgres), + (nameof(config.Ports.Scylla), config.Ports.Scylla), + }; + var seen = new Dictionary(portFields.Length); + foreach (var (name, port) in portFields) + { + if (seen.TryGetValue(port, out var other)) + { + throw new InvalidOperationException( + $"config.ports.{char.ToLowerInvariant(name[0])}{name[1..]} ({port}) collides with " + + $"config.ports.{char.ToLowerInvariant(other[0])}{other[1..]}; every bound host port must be unique."); + } + seen[port] = name; + } + + // Postgres-safe identifier: quoting-free at both bind sites (connection string + + // CREATE DATABASE) within the 63-byte NAMEDATALEN budget. + if (string.IsNullOrWhiteSpace(config.PostgresDatabase)) + { + throw new InvalidOperationException( + "config.postgresDatabase must be a non-empty Postgres identifier (default: 'interfold')."); + } + if (!PostgresIdentifierPattern.IsMatch(config.PostgresDatabase)) + { + throw new InvalidOperationException( + $"config.postgresDatabase='{config.PostgresDatabase}' is not a safe Postgres identifier. " + + "Allowed: 1..63 chars matching [A-Za-z_][A-Za-z0-9_]*."); + } + + // Intersection of what Cassandra's cassandra.yaml rewrite and Scylla's argv accept + // without quoting gymnastics; 1..64 matches Cassandra's documented limit. + if (string.IsNullOrWhiteSpace(config.ClusterName)) + { + throw new InvalidOperationException( + "config.clusterName must be a non-empty cluster identifier (default: 'InterfoldCluster')."); + } + if (!ClusterNamePattern.IsMatch(config.ClusterName)) + { + throw new InvalidOperationException( + $"config.clusterName='{config.ClusterName}' contains characters that would break " + + "Cassandra's cassandra.yaml rewrite or Scylla's CLI argument parsing. " + + "Allowed: 1..64 chars matching [A-Za-z0-9 ._-]."); + } + + // Derive first so JSON-load callers see the same post-derivation values as the form. + ResolveDerivedDefaults(config); + + ValidateAbsoluteHttpUri(config.ApiRuntime.CallbackBaseUrl, "config.apiRuntime.callbackBaseUrl"); + ValidateAbsoluteHttpUri(config.ApiRuntime.JwtAuthority, "config.apiRuntime.jwtAuthority"); + + if (string.IsNullOrWhiteSpace(config.ApiRuntime.JwtAudience)) + { + throw new InvalidOperationException( + "config.apiRuntime.jwtAudience must be a non-empty token-audience identifier (default: 'octocon')."); + } + + // WithOrigins() does exact string matching, so anything that doesn't round-trip + // through Uri.TryCreate(http/https) can never match — reject upfront. + if (config.ApiRuntime.CorsAllowedOrigins.Count == 0) + { + throw new InvalidOperationException( + "config.apiRuntime.corsAllowedOrigins must contain at least one origin after derivation. " + + "Add at least one non-CIDR entry to deployment.hosts so derivation can produce a default, " + + "or populate corsAllowedOrigins explicitly."); + } + foreach (var origin in config.ApiRuntime.CorsAllowedOrigins) + { + ValidateAbsoluteHttpUri(origin, "config.apiRuntime.corsAllowedOrigins entry"); + } + + // AvatarStorageRoot is a host path (bind-mount source); blank → {outputDir}/data/avatars. + ValidateOptionalAbsoluteHttpUri(config.Storage.AvatarPublicBase, "config.storage.avatarPublicBase"); + ValidateOptionalAbsolutePath( + config.Storage.AvatarStorageRoot, + "config.storage.avatarStorageRoot", + "must be an absolute host path (e.g. '/var/lib/interfold/avatars'). " + + "Leave blank to default to '{outputDir}/data/avatars'."); + + // http:// is legal for OTLP (SDK accepts gRPC-over-HTTP/2). + ValidateOptionalAbsoluteHttpUri(config.Observability.OtlpEndpoint, "config.observability.otlpEndpoint"); + + // Numeric bounds come from ConfigurationBounds so the bootstrapper prompts and the + // API's [Range] attributes on the matching options stay lockstep. + if (config.Socket.BatchBytesThreshold is { } socketThreshold) + { + ValidateIntRange(socketThreshold, + ConfigurationBounds.SocketBatchBytesThresholdMin, + ConfigurationBounds.SocketBatchBytesThresholdMax, + "config.socket.batchBytesThreshold"); + } + + // Persistence tuning: max-vs-initial cross-check catches the easy inverted-values mistake. + ValidateIntRange(config.Persistence.DbRetryAttempts, + ConfigurationBounds.DbRetryAttemptsMin, + ConfigurationBounds.DbRetryAttemptsMax, + "config.persistence.dbRetryAttempts"); + ValidateIntRange(config.Persistence.DbRetryInitialDelayMs, + ConfigurationBounds.DbRetryInitialDelayMsMin, + ConfigurationBounds.DbRetryInitialDelayMsMax, + "config.persistence.dbRetryInitialDelayMs"); + ValidateIntRange(config.Persistence.DbRetryMaxDelayMs, + ConfigurationBounds.DbRetryMaxDelayMsMin, + ConfigurationBounds.DbRetryMaxDelayMsMax, + "config.persistence.dbRetryMaxDelayMs"); + // Cross-check catches the easy inverted-values mistake. + if (config.Persistence.DbRetryMaxDelayMs < config.Persistence.DbRetryInitialDelayMs) + { + throw new InvalidOperationException( + $"config.persistence.dbRetryMaxDelayMs ({config.Persistence.DbRetryMaxDelayMs}) " + + $"must be >= dbRetryInitialDelayMs ({config.Persistence.DbRetryInitialDelayMs})."); + } + ValidateIntRange(config.Persistence.HydrationMaxConcurrency, + ConfigurationBounds.HydrationMaxConcurrencyMin, + ConfigurationBounds.HydrationMaxConcurrencyMax, + "config.persistence.hydrationMaxConcurrency"); + + // Real schedule grammar validation is `systemd-analyze calendar` at install time. + ValidateIntRange(config.Backup.RetainCount, 1, 1000, "config.backup.retainCount"); + if (string.IsNullOrWhiteSpace(config.Backup.Schedule)) + { + throw new InvalidOperationException( + "config.backup.schedule must be a non-empty systemd OnCalendar expression " + + "(e.g. 'daily', 'weekly', or 'Mon..Fri 03:30')."); + } + if (!BackupSchedulePattern.IsMatch(config.Backup.Schedule)) + { + throw new InvalidOperationException( + $"config.backup.schedule='{config.Backup.Schedule}' contains characters that are not " + + "valid in a systemd OnCalendar expression. Allowed: letters, digits, spaces, and " + + "the punctuation '.-:,*/'."); + } + ValidateOptionalAbsolutePath( + config.Backup.Directory, + "config.backup.directory", + "must be an absolute path (systemd-driven backup invocations have an unpredictable CWD; " + + "relative paths would not resolve consistently). Leave blank to default to '{outputDir}/backups'."); + + // Range covers realistic cold-starts (Postgres+Scylla+API ~60-120s on modest hardware); + // 3600s cap matches UpdateImagesPhase's "give up eventually" contract. + ValidateIntRange( + config.Update.HealthCheckTimeoutSeconds, 1, 3600, + "config.update.healthCheckTimeoutSeconds"); + + // Empty = every service (valid default); only non-empty entries are checked. + foreach (var svc in config.Update.Services) + { + if (string.IsNullOrWhiteSpace(svc)) + { + throw new InvalidOperationException( + "config.update.services contains a blank entry. Remove it or " + + $"replace with one of: {string.Join(", ", ValidUpdateServices)}."); + } + if (!ValidUpdateServices.Contains(svc, StringComparer.Ordinal)) + { + throw new InvalidOperationException( + $"config.update.services entry '{svc}' is not a known compose service. " + + $"Expected one of: {string.Join(", ", ValidUpdateServices)}."); + } + } + } + + /// + /// Accepts absolute http(s) URIs; rejects with the field name for a clear operator error. + /// Delegates the actual scheme check to + /// so the API's [AbsoluteHttpUri] validation and this bootstrapper check share the + /// same "is this an absolute http(s) URL" rule. + /// + private static void ValidateAbsoluteHttpUri(string value, string fieldLabel) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new InvalidOperationException($"{fieldLabel} must be a non-empty http(s) URL."); + } + if (!AbsoluteHttpUriAttribute.IsAbsoluteHttpUri(value)) + { + throw new InvalidOperationException( + $"{fieldLabel}='{value}' is not a valid absolute http(s) URL."); + } + } + + /// Blank-legal sibling of . + private static void ValidateOptionalAbsoluteHttpUri(string value, string fieldLabel) + { + if (string.IsNullOrWhiteSpace(value)) + { + return; + } + ValidateAbsoluteHttpUri(value, fieldLabel); + } + + /// + /// Blank-legal absolute-path check. Delegates to + /// so the API's [AbsolutePath] + /// validation and this bootstrapper check share the same rule. + /// + private static void ValidateOptionalAbsolutePath(string? value, string fieldLabel, string hint) + { + if (string.IsNullOrEmpty(value)) + { + return; + } + if (!AbsolutePathAttribute.IsAbsolutePath(value)) + { + throw new InvalidOperationException($"{fieldLabel}='{value}' {hint}"); + } + } + + /// Shared int range check; error reports both field label and observed value. + private static void ValidateIntRange(int value, int min, int max, string fieldLabel) + { + if (value < min || value > max) + { + throw new InvalidOperationException( + $"{fieldLabel}={value} is outside the allowed [{min}..{max}] range."); + } + } + + private static readonly Regex PostgresIdentifierPattern = + new("^[A-Za-z_][A-Za-z0-9_]{0,62}$", RegexOptions.CultureInvariant | RegexOptions.Compiled); + + private static readonly Regex ClusterNamePattern = + new("^[A-Za-z0-9 ._-]{1,64}$", RegexOptions.CultureInvariant | RegexOptions.Compiled); + + /// + /// Character allow-list for OnCalendar=. Grammar validation is deferred to + /// systemd-analyze calendar at install time; this only rejects obviously bogus + /// input (control chars, shell metacharacters). + /// + private static readonly Regex BackupSchedulePattern = + new(@"^[A-Za-z0-9 .,:\-*/]{1,256}$", RegexOptions.CultureInvariant | RegexOptions.Compiled); + + private static void ValidatePort(int port, string field) + { + if (port is < 1 or > 65535) + { + throw new InvalidOperationException($"{field}={port} is outside the valid 1..65535 range."); + } + } + + private static async Task PersistAsync(BootstrapConfig config, string path, CancellationToken ct) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + var json = JsonSerializer.Serialize(config, BootstrapJsonContext.Default.BootstrapConfig); + await File.WriteAllTextAsync(path, json, ct).ConfigureAwait(false); + } + + /// + /// Tier 1 of the two-tier mDNS design: pre-prompt banner. Detects the device hostname, + /// probes {hostname}.local, prints a banner, and (interactive only) optionally + /// installs avahi. Returns the hostname to seed "Public host(s)" with, or null when we + /// should skip the pre-fill. Tier 2 is . + /// + /// Non-interactive short-circuits (banner is TTY-only). + /// Phase logger. + /// Cancellation for probe + install. + /// Test seam; defaults to . + /// Test seam; defaults to . + /// Test seam; defaults to !Console.IsInputRedirected. + /// Provided because in unit tests doesn't change + /// (the property is bound to the OS-level fd + /// state, not the managed Console.In reader). Without this seam the unit test's + /// StringReader stdin swap is invisible to the check and the install prompt fires + /// whenever `dotnet test` runs from an interactive terminal. + internal static async Task ApplyPreFillMdnsCheckAsync( + BootstrapOptions options, + PhaseLogger logger, + CancellationToken ct, + Func? hostnameFactory = null, + Func>? probe = null, + Func? isInteractive = null) + { + // Non-interactive skips the banner; the post-fill gate handles .local entries later. + if (options.NonInteractive) + { + return null; + } + + var hostname = (hostnameFactory ?? HostnameDetector.TryDetectMdnsHostname)(); + if (hostname is null) + { + logger.Info(" mDNS pre-check: no suitable short hostname detected; skipping .local pre-fill"); + return null; + } + + var probeFn = probe ?? MdnsAvailability.IsHostnameResolvableAsync; + var initial = await probeFn(hostname, ct).ConfigureAwait(false); + if (initial is null) + { + logger.Info($" mDNS pre-check: platform doesn't support probing; skipping {hostname} pre-fill"); + return null; + } + if (initial == true) + { + logger.Info($" mDNS pre-check: {hostname} resolves; will pre-fill it in the hosts prompt"); + return hostname; + } + + // mDNS unavailable — warn up-front, then (if we have a TTY) offer install. + var distro = DistroInfo.Read(); + logger.Warn($"mDNS is unavailable on this device — {hostname} will NOT resolve on the LAN."); + logger.Warn($" to enable it: {MdnsAvailability.ManualInstallHint(distro.Family)}"); + + // No TTY (test path lands here too) → skip install offer, operator gets the manual hint. + var isInteractiveFn = isInteractive ?? (static () => !Console.IsInputRedirected); + if (!isInteractiveFn()) + { + logger.Warn($" no TTY for install prompt; {hostname} will be omitted from the pre-fill"); + return null; + } + + var install = AnsiConsole.Prompt(new ConfirmationPrompt( + $"Install avahi-daemon + nss-mdns now so {hostname} can be included in the hosts list?") + { DefaultValue = true }); + if (!install) + { + logger.Warn($" operator declined install — {hostname} will be omitted from the pre-fill"); + return null; + } + + var packages = MdnsAvailability.InstallPackages(distro.Family); + if (packages.Count == 0) + { + logger.Warn($" unsupported distro family {distro.Family}; skipping install and .local pre-fill"); + return null; + } + + if (!await MdnsAvailability.TryInstallAvahiAsync(distro, logger, ct).ConfigureAwait(false)) + { + logger.Warn($" {hostname} will be omitted from the pre-fill"); + return null; + } + + var recheck = await probeFn(hostname, ct).ConfigureAwait(false); + if (recheck == true) + { + logger.Info($" mDNS now resolvable; pre-filling {hostname}"); + return hostname; + } + logger.Warn($" avahi installed but {hostname} still doesn't resolve; omitting from pre-fill"); + return null; + } + + /// + /// Tier 2 of the two-tier mDNS design: post-validation gate. Runs on the + /// command only. Probes every .local host; + /// on failure offers install (interactive), then strips unresolvable entries with a + /// warning. Bootstrap always continues — re-running to restore stripped entries is + /// optional recovery. + /// + /// Partial resolution is treated as broken: leaf-cert SAN chains are trust-all-or-nothing, + /// so a smaller consistent set beats a broken half. + /// + /// + /// True when the hosts list was mutated (caller re-validates + re-persists). + /// Bootstrap config; mutated in place when entries are stripped. + /// Drives the interactive-vs-strip fork. + /// Phase logger. + /// Cancellation for probe + install. + /// Test seam; defaults to . + internal static async Task ApplyMdnsGateAsync( + BootstrapConfig config, + BootstrapOptions options, + PhaseLogger logger, + CancellationToken ct, + Func>? probe = null) + { + var localHosts = config.Deployment.Hosts + .Where(h => h.EndsWith(".local", StringComparison.OrdinalIgnoreCase)) + .ToList(); + if (localHosts.Count == 0) + { + return false; + } + + var probeFn = probe ?? MdnsAvailability.IsHostnameResolvableAsync; + + // Probe every .local individually (null result = can't probe on this platform). + // Per-entry avoids masking an earlier broken host with a later resolvable one. + var broken = new List(); + foreach (var host in localHosts) + { + var result = await probeFn(host, ct).ConfigureAwait(false); + if (result is null) + { + return false; + } + if (result == false) + { + broken.Add(host); + } + } + if (broken.Count == 0) + { + logger.Info($" mDNS ok: {string.Join(", ", localHosts)} all resolvable"); + return false; + } + + // Interactive install offer — catches the "changed my mind" case after the banner + // AND JSON-load runs that never saw the banner at all. Non-interactive skips it. + if (!options.NonInteractive && !Console.IsInputRedirected) + { + var distro = DistroInfo.Read(); + var install = AnsiConsole.Prompt(new ConfirmationPrompt( + $"mDNS is unavailable but your hosts list contains {string.Join(", ", broken)}. " + + "Install avahi-daemon + nss-mdns now?") + { DefaultValue = true }); + if (install) + { + if (await MdnsAvailability.TryInstallAvahiAsync(distro, logger, ct).ConfigureAwait(false)) + { + // Re-probe; any residual failure falls through to strip so the run still succeeds. + var stillBroken = false; + foreach (var host in broken) + { + if (await probeFn(host, ct).ConfigureAwait(false) != true) + { + stillBroken = true; + break; + } + } + if (!stillBroken) + { + logger.Info(" mDNS now resolvable for all .local hosts"); + return false; + } + logger.Warn($"avahi installed but {string.Join(", ", broken)} still doesn't resolve; removing from hosts."); + } + else + { + logger.Warn("removing unresolvable .local host(s) after failed avahi install."); + } + } + } + + // Strip + warn + continue. Bootstrap doesn't halt; the install hint is recovery advice. + var installHint = MdnsAvailability.ManualInstallHint(DistroInfo.Read().Family); + logger.Warn($"removing unresolvable .local host(s) from config.deployment.hosts: {string.Join(", ", broken)}"); + logger.Warn($" bootstrap will continue with the remaining hosts. To restore these entries on a future run, set up mDNS first: {installHint}"); + config.Deployment.Hosts = [.. config.Deployment.Hosts.Except(broken, StringComparer.OrdinalIgnoreCase)]; + return true; + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/DatabaseInitPhase.cs b/tools/Interfold.Bootstrapper/Phases/DatabaseInitPhase.cs new file mode 100644 index 00000000..719f9e7a --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/DatabaseInitPhase.cs @@ -0,0 +1,178 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Shared.Contracts.Configuration; +using Interfold.DatabaseBootstrap; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// Phase 5.5 — owns the database admin work that previously lived in the +/// pg-bootstrap-auth / scylla-bootstrap-auth init containers. Brings up the +/// stateful services in isolation, then hands off seed orchestration to +/// / via the compose-exec executors +/// in this folder. After this phase returns the cluster is ready for +/// to up -d the rest of the graph. +/// +/// +/// +/// All admin/seed operations are driven via docker compose exec against the running +/// containers — no admin password ever appears in docker-compose.yaml, .env, +/// or AppHost parameters. State is detected from inside the cluster (the <user>_admin +/// role's existence, plus the app user's rolsuper flag), so a rerun against a +/// populated data volume short-circuits cleanly. +/// +/// +/// The actual SQL/CQL bodies live in Interfold.DatabaseBootstrap so the same logic +/// runs against the in-process test fixtures via DbInitHelper + the Npgsql / DataStax +/// driver adapters. This file is intentionally lean: it owns only the docker-compose +/// orchestration, the cold-start wait loops, and the executor wire-up. +/// +/// +internal static class DatabaseInitPhase +{ + private static readonly string Phase = BootstrapPhase.DbInit.ToWireName(); + private const string PostgresService = ComposeServices.Postgres; + + public static async Task RunAsync( + BootstrapOptions options, + BootstrapConfig config, + GeneratedSecrets secrets, + FirebaseSeedInputs firebase, + PhaseLogger logger, + CancellationToken ct) + { + logger.PhaseStart(Phase); + + var composeFile = PhaseArtifactLoader.RequireComposeFileOrFail(options, logger, Phase); + + // Scylla service name depends on launch profile. The 'cassandra' fallback and + // multi-region scylla deployments use a different container alias - we drive the + // first node only because all admin operations propagate via gossip. + var scyllaService = ResolveScyllaServiceName(config); + var scyllaPort = ResolveScyllaPort(); + + if (CassandraImagePhase.IsCassandraDeployment(config)) + { + await CassandraImagePhase.EnsureBuiltAsync(logger, ct).ConfigureAwait(false); + } + + // Bring up only the stateful services first. We deliberately don't start the API or + // any other services so the API doesn't race against an unconfigured Postgres. + await Util.DockerCompose.UpCheckedAsync(composeFile, [PostgresService, scyllaService], logger, ct).ConfigureAwait(false); + + var seederLogger = new PhaseLoggerAdapter(logger); + var pgExecutor = new ComposeExecPostgresExecutor(composeFile, PostgresService, seederLogger); + var scExecutor = new ComposeExecScyllaExecutor(composeFile, scyllaService, seederLogger); + var pgOptions = BuildPostgresSeedOptions(config, secrets, firebase, scyllaService, scyllaPort); + var scOptions = new ScyllaSeedOptions( + AppUser: secrets.ScyllaUser, + AppPassword: secrets.ScyllaPassword, + AdminUser: $"{secrets.ScyllaUser}_admin", + AdminPassword: secrets.ScyllaAdminPassword, + LockDefaultCassandra: true); + + await WaitForPostgresAsync(composeFile, logger, ct).ConfigureAwait(false); + await PostgresSeeder.BootstrapAsync(pgExecutor, pgOptions, seederLogger, ct).ConfigureAwait(false); + + // Hidden testability hook: halt the phase between Postgres and Scylla so + // DbInitFaultRecoveryTests can confirm a rerun resumes cleanly. We throw rather than + // return so the Orchestrator's try/catch surfaces a non-zero exit and skips the + // Launch phase (which would otherwise try to `compose up` an un-initialised stack). + if (string.Equals(options.FaultInject, BootstrapPhase.DbPostgres.ToFaultInjectToken(), StringComparison.OrdinalIgnoreCase)) + { + logger.Warn("--fault-inject=after-db-postgres triggered; halting before scylla init."); + throw new InvalidOperationException("fault-inject:after-db-postgres"); + } + + await WaitForScyllaAsync(composeFile, scyllaService, scExecutor, scOptions, logger, ct).ConfigureAwait(false); + await ScyllaSeeder.BootstrapAsync(scExecutor, scOptions, seederLogger, ct).ConfigureAwait(false); + + logger.PhaseDone(Phase); + } + + private static PostgresSeedOptions BuildPostgresSeedOptions( + BootstrapConfig config, + GeneratedSecrets secrets, + FirebaseSeedInputs firebase, + string scyllaService, + int scyllaPort) + { + return new PostgresSeedOptions( + InitUser: PostgresRoles.Init, + InitPassword: secrets.PostgresInitPassword, + AppUser: secrets.PostgresUser, + AppPassword: secrets.PostgresPassword, + AdminUser: $"{secrets.PostgresUser}_admin", + AdminPassword: secrets.PostgresAdminPassword, + DefaultDatabase: config.PostgresDatabase, + GoogleOAuthClientSecret: config.OAuth.GoogleClientSecret ?? string.Empty, + DiscordOAuthClientSecret: config.OAuth.DiscordClientSecret ?? string.Empty, + AppleOAuthClientSecret: config.OAuth.AppleClientSecret ?? string.Empty, + EncryptionPepper: secrets.EncryptionPepper, + // Self-hosted deployments reach scylla via the docker network using its container + // alias. The matching DataStax driver inside the API resolves it inside the same + // network so the alias is sufficient. + ScyllaContactPoints: scyllaService, + ScyllaLocalDatacenter: "nam", + ScyllaAppUser: secrets.ScyllaUser, + ScyllaAppPassword: secrets.ScyllaPassword, + ScyllaPort: scyllaPort, + ScyllaAdminUser: $"{secrets.ScyllaUser}_admin", + ScyllaAdminPassword: secrets.ScyllaAdminPassword, + JwtRsa256PrivateKeyPem: secrets.JwtRsa256PrivateKeyPem, + JwtEs256PrivateKeyPem: secrets.JwtEs256PrivateKeyPem, + DeepLinkSecret: secrets.DeepLinkSecret, + LeafPfxPassword: secrets.LeafPfxPassword, + // Production callers always finish by scrambling the init credential in-cluster + // so the .env value sitting in the operator's deployment dir is intentionally + // stale by the time the API starts. + ScrambleInitUserPassword: true, + FirebaseAndroidClientJson: firebase.AndroidClientJson, + FirebaseIosClientJson: firebase.IosClientJson, + FirebaseWebClientJson: firebase.WebClientJson, + FcmServiceAccountJson: firebase.ServiceAccountJson); + } + + private static string ResolveScyllaServiceName(BootstrapConfig config) + => BackupPhase.ResolveScyllaSeed(config).Service; + + private static int ResolveScyllaPort() + { + // CQL port the API uses to reach Scylla *over the compose docker network* (resolving the + // scylla service name via docker DNS). That target is always the container's listening + // port — 9042 — irrespective of whatever host port the operator (or test fixture) chose + // for external access via `config.ports.scylla`. The host-port choice flows into the + // compose YAML via PublishPhase; here we deliberately stay on the in-network port. + return 9042; + } + + // -------- Bring-up -------- + + // -------- Wait loops (transport-specific) -------- + + private static Task WaitForPostgresAsync(string composeFile, PhaseLogger logger, CancellationToken ct) + { + return Util.PostgresReadinessProbe.WaitAsync( + composeFile, + PostgresService, + new Interfold.DatabaseBootstrap.PostgresReadinessOptions(TimeSpan.FromMinutes(10), 3, PostgresRoles.Init), + logger, + ct); + } + + private static Task WaitForScyllaAsync( + string composeFile, string scyllaService, + IScyllaExecutor executor, ScyllaSeedOptions options, + PhaseLogger logger, CancellationToken ct) + { + _ = composeFile; + _ = scyllaService; + return Util.ScyllaReadinessProbe.WaitAsync( + executor, + options, + new Interfold.DatabaseBootstrap.ScyllaReadinessOptions(TimeSpan.FromMinutes(5)), + logger, + ct); + } +} + diff --git a/tools/Interfold.Bootstrapper/Phases/FirebasePhase.cs b/tools/Interfold.Bootstrapper/Phases/FirebasePhase.cs new file mode 100644 index 00000000..94ef4bac --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/FirebasePhase.cs @@ -0,0 +1,271 @@ +using System.Text.Json; +using System.Xml.Linq; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Settings.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// Ingests the four operator-supplied Firebase inputs (google-services.json, +/// GoogleService-Info.plist, firebase-web-config.json, FCM v1 service-account JSON), +/// reshapes each into the wire JSON SecretsBootstrapService deserialises at API +/// startup, and returns them for to fold into +/// . Runs before +/// so the internal.secrets rows land in the same pass as the OAuth secrets. Every input +/// is optional (blank path → skip; the API falls back to NullFCMService and +/// /api/settings/firebase-config returns 503 for that platform). Non-blank paths +/// that don't resolve OR fail to parse ARE errors — surface at bootstrap, not first +/// request. +internal static class FirebasePhase +{ + private static readonly string Phase = BootstrapPhase.Firebase.ToWireName(); + + public static Task RunAsync( + BootstrapOptions options, + BootstrapConfig config, + PhaseLogger logger, + CancellationToken ct) + { + logger.PhaseStart(Phase); + + var section = config.Firebase; + var result = new FirebaseSeedInputs( + AndroidClientJson: ParseAndroid(section.AndroidConfigPath, options.OutputDir, logger), + IosClientJson: ParseIos(section.IosConfigPath, options.OutputDir, logger), + WebClientJson: ReadWebPassthrough(section.WebConfigPath, options.OutputDir, logger), + ServiceAccountJson: ReadServiceAccount(section.ServiceAccountPath, options.OutputDir, logger)); + + var configured = new[] + { + (result.AndroidClientJson, "android client-init"), + (result.IosClientJson, "ios client-init"), + (result.WebClientJson, "web client-init"), + (result.ServiceAccountJson, "fcm service-account"), + }.Count(x => !string.IsNullOrWhiteSpace(x.Item1)); + + if (configured == 0) + logger.Info(" no firebase inputs configured — API will disable push and return 503 on /api/settings/firebase-config."); + else + logger.Info($" ingested {configured}/4 firebase input(s)."); + + logger.PhaseDone(Phase); + return Task.FromResult(result); + } + + /// Reshapes google-services.json into . + /// Uses client[0] — multi-app projects should point at the file their Android + /// build actually consumes. Array-non-empty checks stay explicit (STJ can't express + /// "at least one element" via required). + private static string? ParseAndroid(string path, string outputDir, PhaseLogger logger) + { + var resolved = ResolveOptional(path, outputDir, "androidConfigPath", logger); + if (resolved is null) return null; + + try + { + var input = JsonSerializer.Deserialize( + File.ReadAllText(resolved), FirebaseSnakeCaseReadContext.Default.GoogleServicesFile) + ?? throw new InvalidDataException($"{resolved}: file is empty or JSON null"); + + if (input.Client.Length == 0) + throw new InvalidDataException($"{resolved}: no client entries found"); + var client = input.Client[0]; + if (client.ApiKey.Length == 0) + throw new InvalidDataException($"{resolved}: no api_key entries found"); + + var normalised = new FirebaseAndroidClientConfig( + ApiKey: client.ApiKey[0].CurrentKey, + ApplicationId: client.ClientInfo.MobilesdkAppId, + ProjectId: input.ProjectInfo.ProjectId, + GcmSenderId: input.ProjectInfo.ProjectNumber, + StorageBucket: input.ProjectInfo.StorageBucket); + return JsonSerializer.Serialize( + normalised, FirebaseSnakeCaseWriteContext.Default.FirebaseAndroidClientConfig); + } + catch (Exception ex) when (ex is not InvalidDataException) + { + throw new InvalidDataException($"Failed to parse {resolved} as google-services.json: {ex.Message}", ex); + } + } + + /// Parses GoogleService-Info.plist into . + /// STORAGE_BUCKET / CLIENT_ID are optional; missing entries serialise as JSON null. + private static string? ParseIos(string path, string outputDir, PhaseLogger logger) + { + var resolved = ResolveOptional(path, outputDir, "iosConfigPath", logger); + if (resolved is null) return null; + + try + { + var doc = XDocument.Load(resolved); + var dict = doc.Descendants("dict").FirstOrDefault() + ?? throw new InvalidDataException($"{resolved}: no element found"); + var map = ReadPlistDictionary(dict); + + string GetRequired(string key) => map.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) + ? v + : throw new InvalidDataException($"{resolved}: required key '{key}' is missing"); + string? GetOptional(string key) => map.TryGetValue(key, out var v) && !string.IsNullOrEmpty(v) ? v : null; + + var normalised = new FirebaseIosClientConfig( + ApiKey: GetRequired("API_KEY"), + GoogleAppId: GetRequired("GOOGLE_APP_ID"), + GcmSenderId: GetRequired("GCM_SENDER_ID"), + ProjectId: GetRequired("PROJECT_ID"), + StorageBucket: GetOptional("STORAGE_BUCKET"), + BundleId: GetRequired("BUNDLE_ID"), + ClientId: GetOptional("CLIENT_ID")); + return JsonSerializer.Serialize( + normalised, FirebaseSnakeCaseWriteContext.Default.FirebaseIosClientConfig); + } + catch (Exception ex) when (ex is not InvalidDataException) + { + throw new InvalidDataException($"Failed to parse {resolved} as GoogleService-Info.plist: {ex.Message}", ex); + } + } + + /// Camel→snake re-serialise of firebase-web-config.json into + /// . Every positional param (including nullable + /// StorageBucket) is required — a truncated console paste fails at bootstrap. + private static string? ReadWebPassthrough(string path, string outputDir, PhaseLogger logger) + { + var resolved = ResolveOptional(path, outputDir, "webConfigPath", logger); + if (resolved is null) return null; + + try + { + var web = JsonSerializer.Deserialize( + File.ReadAllText(resolved), FirebaseCamelCaseReadContext.Default.FirebaseWebClientConfig) + ?? throw new InvalidDataException($"{resolved}: file is empty or JSON null"); + return JsonSerializer.Serialize( + web, FirebaseSnakeCaseWriteContext.Default.FirebaseWebClientConfig); + } + catch (Exception ex) when (ex is not InvalidDataException) + { + throw new InvalidDataException($"Failed to parse {resolved} as firebase-web-config.json: {ex.Message}", ex); + } + } + + /// FCM v1 service-account JSON passthrough. Validates the two fields + /// FirebaseAdmin's GoogleCredential.FromJson requires; the raw bytes are + /// returned verbatim (any reshape breaks the SDK's signature verification). + private static string? ReadServiceAccount(string path, string outputDir, PhaseLogger logger) + { + var resolved = ResolveOptional(path, outputDir, "serviceAccountPath", logger); + if (resolved is null) return null; + + var raw = File.ReadAllText(resolved); + try + { + var stub = JsonSerializer.Deserialize( + raw, FirebaseSnakeCaseReadContext.Default.ServiceAccountStub) + ?? throw new InvalidDataException($"{resolved}: file is empty or JSON null"); + if (stub.Type != "service_account") + throw new InvalidDataException($"{resolved}: FCM service-account JSON must have \"type\": \"service_account\""); + } + catch (JsonException ex) + { + throw new InvalidDataException($"Failed to parse {resolved} as JSON: {ex.Message}", ex); + } + return raw; + } + + /// Flattens a <dict>'s alternating key/value children into a string map. + /// Only strings and booleans are captured; arrays and nested dicts are ignored so an + /// augmented Firebase download doesn't fail the phase. + private static Dictionary ReadPlistDictionary(XElement dict) + { + var map = new Dictionary(StringComparer.Ordinal); + var elements = dict.Elements().ToList(); + for (var i = 0; i < elements.Count - 1; i++) + { + var keyEl = elements[i]; + if (keyEl.Name.LocalName != "key") continue; + var key = keyEl.Value; + var valueEl = elements[i + 1]; + + if (valueEl.Name.LocalName == "string") + map[key] = valueEl.Value; + else if (valueEl.Name.LocalName is "true" or "false") + map[key] = valueEl.Name.LocalName; + } + return map; + } + + /// Blank → skip; non-blank must resolve to a file (a typo shouldn't silently + /// disable push). + private static string? ResolveOptional(string path, string outputDir, string fieldName, PhaseLogger logger) + { + if (string.IsNullOrWhiteSpace(path)) + { + logger.Info($" firebase.{fieldName} not set — skipping."); + return null; + } + + var resolved = Path.IsPathRooted(path) ? path : Path.Combine(outputDir, path); + if (!File.Exists(resolved)) + { + throw new FileNotFoundException( + $"firebase.{fieldName} points at '{resolved}' but no file exists there. " + + "Either provide the file or clear the path to skip this input.", resolved); + } + return resolved; + } + + // STJ deserialisation targets for Google's file shapes. `required` + + // RespectRequiredConstructorParameters (on FirebaseSnakeCaseReadContext) replaces + // hand-written null-guards. Internal (not private) so FirebaseJsonContext can + // [JsonSerializable] them. The Web input deserialises directly into + // FirebaseWebClientConfig because its camelCase matches 1:1. + + /// Only the fields the phase forwards into + /// are modelled; analytics/oauth/etc. are ignored. + internal sealed class GoogleServicesFile + { + public required GoogleServicesProjectInfo ProjectInfo { get; init; } + public required GoogleServicesClient[] Client { get; init; } + } + + internal sealed class GoogleServicesProjectInfo + { + public required string ProjectNumber { get; init; } + public required string ProjectId { get; init; } + public string? StorageBucket { get; init; } + } + + internal sealed class GoogleServicesClient + { + public required GoogleServicesClientInfo ClientInfo { get; init; } + public required GoogleServicesApiKey[] ApiKey { get; init; } + } + + internal sealed class GoogleServicesClientInfo + { + public required string MobilesdkAppId { get; init; } + } + + internal sealed class GoogleServicesApiKey + { + public required string CurrentKey { get; init; } + } + + /// Minimal projection: two required fields whose absence triggers a + /// . The full credential is trusted to FirebaseAdmin at API startup. + internal sealed class ServiceAccountStub + { + public required string Type { get; init; } + public required string ProjectId { get; init; } + } +} + +/// Result bag threaded into . +/// Null → operator didn't configure this input; +/// skips the matching row. +internal sealed record FirebaseSeedInputs( + string? AndroidClientJson, + string? IosClientJson, + string? WebClientJson, + string? ServiceAccountJson) +{ + public static FirebaseSeedInputs Empty { get; } = new(null, null, null, null); +} diff --git a/tools/Interfold.Bootstrapper/Phases/LaunchPhase.cs b/tools/Interfold.Bootstrapper/Phases/LaunchPhase.cs new file mode 100644 index 00000000..3a54426b --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/LaunchPhase.cs @@ -0,0 +1,66 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Configuration; +using static Interfold.Bootstrapper.Phases.CassandraImagePhase; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// Phase 6 — runs docker compose up -d against the emitted compose file and waits for the +/// API's /health/ready endpoint to return 200. Failure here is the most common operator-visible +/// failure mode, so the implementation logs verbosely and surfaces compose logs on timeout. +/// +internal static class LaunchPhase +{ + private static readonly TimeSpan HealthTimeout = TimeSpan.FromMinutes(5); + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + const string Phase = "launch"; + logger.PhaseStart(Phase); + + var composeFile = PhaseArtifactLoader.RequireComposeFileOrFail(options, logger, Phase); + + var config = await PhaseArtifactLoader.TryLoadConfigAsync(options, ct).ConfigureAwait(false); + var apiHttpPort = config?.Ports.ApiHttp ?? 5000; + + if (config is not null && IsCassandraDeployment(config)) + { + await EnsureBuiltAsync(logger, ct).ConfigureAwait(false); + } + + await Util.DockerCompose.UpCheckedAsync(composeFile, services: null, logger, ct).ConfigureAwait(false); + + try + { + await WaitForApiHealthyAsync(apiHttpPort, logger, ct).ConfigureAwait(false); + logger.PhaseDone(Phase); + } + catch (TimeoutException) + { + await DumpComposeLogsAsync(composeFile, logger, ct).ConfigureAwait(false); + logger.PhaseFail(Phase, PhaseFailureReasons.HealthTimeout); + throw; + } + } + + private static async Task WaitForApiHealthyAsync(int apiHttpPort, PhaseLogger logger, CancellationToken ct) + { + logger.Info($" polling http://localhost:{apiHttpPort}{HealthEndpoints.Ready} (up to {HealthTimeout.TotalMinutes:F0}m)"); + + var deadline = DateTime.UtcNow + HealthTimeout; + var err = await ApiReadinessProbe.TryWaitUntilAsync(apiHttpPort, deadline, logger, ct).ConfigureAwait(false); + if (err is not null) + { + throw new TimeoutException( + $"interfold-api did not become healthy at http://localhost:{apiHttpPort}{HealthEndpoints.Ready} within {HealthTimeout.TotalMinutes:F0} minutes."); + } + } + + private static async Task DumpComposeLogsAsync(string composeFile, PhaseLogger logger, CancellationToken ct) + { + logger.Warn("dumping compose logs for diagnosis..."); + await ComposeLogDumper.DumpAsync(composeFile, services: null, tailLines: 200, logger, ct).ConfigureAwait(false); + } +} + diff --git a/tools/Interfold.Bootstrapper/Phases/Orchestrator.cs b/tools/Interfold.Bootstrapper/Phases/Orchestrator.cs new file mode 100644 index 00000000..78c9fe22 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/Orchestrator.cs @@ -0,0 +1,138 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// Dispatches the per-command phase sequence. Phases are designed to be idempotent — second runs +/// short-circuit unless an explicit rotate flag is passed. +/// +internal static class Orchestrator +{ + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + // show-trust: pure read, skips prereqs/config/secrets/certs/publish so it's safe on a + // sealed deploy tree without interfold.bootstrap.json. + if (options.Command == BootstrapCommand.ShowTrust) + { + var certsDir = Path.Combine(options.OutputDir, "certs"); + var rootCrtPath = Path.Combine(certsDir, "rootCA.crt"); + var rootFingerprintPath = Path.Combine(certsDir, "rootCA.sha256.txt"); + + if (!File.Exists(rootCrtPath)) + { + logger.Error($"rootCA.crt not found at {rootCrtPath}. Run `bootstrap` (or `bootstrap rotate-certs`) first."); + return 1; + } + + CertificatePhase.PrintTrustInfo(rootCrtPath, rootFingerprintPath, logger); + return 0; + } + + // Runtime short-circuits (backup, install-service, update-images, restore): skip the + // heavyweight host-mutating phases; they only read config + secrets from disk. + if (options.Command == BootstrapCommand.Backup) + { + return await BackupPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + } + if (options.Command == BootstrapCommand.InstallService) + { + return await SystemdInstallPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + } + if (options.Command == BootstrapCommand.UpdateImages) + { + return await UpdateImagesPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + } + if (options.Command == BootstrapCommand.Restore) + { + return await RestorePhase.RunAsync(options, logger, ct).ConfigureAwait(false); + } + + // Phase sequence per command: + // bootstrap prereqs -> config -> secrets -> certs -> publish -> db-init -> launch + // publish config -> secrets -> certs -> publish + // up -> launch + // rotate-secrets config -> secrets (force) -> publish -> db-init -> launch + // rotate-certs config -> certs (force) -> publish -> db-init -> launch + // `publish` is artifact-only so tests can run it as a smoke check and operators can + // inspect the compose/.env before committing. db-init is idempotent (short-circuits via + // PostgresAlreadyInitializedAsync / ScyllaAlreadyInitializedAsync) and runs on + // rotate-certs too — clean-rebuild edge case where the DB volume is empty. + + if (options.Command == BootstrapCommand.Bootstrap && !options.SkipPrereqs) + { + await PrerequisitesPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Prereqs, logger)) return 0; + } + + BootstrapConfig? config = null; + if (options.Command != BootstrapCommand.Up) + { + config = await ConfigPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Config, logger)) return 0; + } + + GeneratedSecrets? secrets = null; + if (options.Command != BootstrapCommand.Up && options.Command != BootstrapCommand.RotateCerts) + { + secrets = await SecretsPhase.RunAsync(options, config!, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Secrets, logger)) return 0; + } + else if (options.Command == BootstrapCommand.RotateCerts) + { + // rotate-certs still needs the leaf PFX password — read-only. + secrets = SecretsPhase.LoadExisting(options); + } + + if (options.Command != BootstrapCommand.Up && options.Command != BootstrapCommand.RotateSecrets) + { + await CertificatePhase.RunAsync(options, config!, secrets!, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Certs, logger)) return 0; + } + + if (options.Command != BootstrapCommand.Up) + { + await PublishPhase.RunAsync(options, config!, secrets!, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Publish, logger)) return 0; + } + + // Firebase runs on db-init-bound commands so its parsed inputs land alongside the + // OAuth/JWT rows PostgresSeeder inserts. Empty paths produce Empty inputs (no push). + FirebaseSeedInputs firebase = FirebaseSeedInputs.Empty; + if (options.Command is BootstrapCommand.Bootstrap + or BootstrapCommand.RotateSecrets + or BootstrapCommand.RotateCerts) + { + firebase = await FirebasePhase.RunAsync(options, config!, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Firebase, logger)) return 0; + } + + if (options.Command is BootstrapCommand.Bootstrap + or BootstrapCommand.RotateSecrets + or BootstrapCommand.RotateCerts) + { + await DatabaseInitPhase.RunAsync(options, config!, secrets!, firebase, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.DbInit, logger)) return 0; + } + + if (options.Command is BootstrapCommand.Bootstrap or BootstrapCommand.Up + or BootstrapCommand.RotateSecrets or BootstrapCommand.RotateCerts) + { + await LaunchPhase.RunAsync(options, logger, ct).ConfigureAwait(false); + if (HaltAfter(options, BootstrapPhase.Launch, logger)) return 0; + } + + return 0; + } + + private static bool HaltAfter(BootstrapOptions options, BootstrapPhase phase, PhaseLogger logger) + { + if (!string.Equals(options.FaultInject, phase.ToFaultInjectToken(), StringComparison.OrdinalIgnoreCase)) + { + return false; + } + logger.Warn($"--fault-inject={phase.ToFaultInjectToken()} triggered; exiting before next phase."); + // Return 0 — tests do SIGKILL elsewhere; the orchestrator caller decides the exit code. + return true; + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/PhaseArtifactLoader.cs b/tools/Interfold.Bootstrapper/Phases/PhaseArtifactLoader.cs new file mode 100644 index 00000000..086bd5c3 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/PhaseArtifactLoader.cs @@ -0,0 +1,164 @@ +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// The two "look up an artifact on disk, log its resolved path, or fail-fast with the +/// canonical phase-failure reason" helpers every phase used to repeat verbatim. Consolidates: +/// +/// — resolve the config path, error out with +/// the MissingConfig reason if it's absent, else stream-deserialize it. +/// — locate the emitted compose file, error +/// out with the NoComposeFile reason if it's absent, else log its resolved path +/// and return it. +/// +/// +/// +/// +/// Previously every phase that needed either artifact reimplemented the same 8-15 line block, +/// with per-site drift in error wording ("Backup requires..." vs "restore requires..." vs +/// "update-images requires...") that we deliberately preserve here through the +/// commandVerb parameter so the operator-visible messages don't change. +/// +/// +/// Both helpers deliberately emit through before throwing +/// so failure telemetry and structured logs stay consistent. +/// used to skip the PhaseFail call on missing compose; migrating it here also folds it +/// into the same failure-emission contract as everyone else (benign observability upgrade). +/// +/// +internal static class PhaseArtifactLoader +{ + /// + /// Resolves the bootstrap config path (via ), + /// requires it to exist, and stream-deserializes it. On the missing-file path emits + /// and throws with an operator-friendly + /// message that includes the resolved path and the "Run `bootstrap` first" recovery hint. + /// + /// + /// The verb used at the start of the error message ("Backup", "restore", "update-images", + /// "install-service"). Preserves the exact per-command wording each phase historically used. + /// + public static async Task LoadRequiredConfigAsync( + BootstrapOptions options, + PhaseLogger logger, + string phase, + string commandVerb, + CancellationToken ct) + { + var configPath = BootstrapArtifactPaths.ResolveConfigPath(options); + if (!File.Exists(configPath)) + { + logger.PhaseFail(phase, PhaseFailureReasons.MissingConfig); + throw new InvalidOperationException( + $"{commandVerb} requires a populated bootstrap config at {configPath}. " + + "Run `bootstrap` first."); + } + + await using var stream = File.OpenRead(configPath); + return await JsonSerializer.DeserializeAsync( + stream, BootstrapJsonContext.Default.BootstrapConfig, ct).ConfigureAwait(false) + ?? throw new InvalidOperationException($"Failed to parse {configPath}."); + } + + /// + /// Best-effort config load: resolves the config path, returns null when the file + /// is missing or cannot be parsed. Unlike this + /// method never throws or emits a phase-failure log — callers that treat a missing config + /// as a soft default (e.g. falling back to port 5000) use this + /// overload. + /// + public static async Task TryLoadConfigAsync( + BootstrapOptions options, CancellationToken ct) + { + var configPath = BootstrapArtifactPaths.ResolveConfigPath(options); + if (!File.Exists(configPath)) + return null; + + try + { + await using var stream = File.OpenRead(configPath); + return await JsonSerializer.DeserializeAsync( + stream, BootstrapJsonContext.Default.BootstrapConfig, ct).ConfigureAwait(false); + } + catch + { + return null; + } + } + + /// + /// Locates the emitted compose file + /// ). Emits + /// and throws with the canonical + /// operator message when it's absent, otherwise logs the resolved path and returns it. + /// + public static string RequireComposeFileOrFail( + BootstrapOptions options, + PhaseLogger logger, + string phase) + { + var composeFile = BootstrapArtifactPaths.FindComposeFile(options.OutputDir); + if (composeFile is null) + { + logger.PhaseFail(phase, PhaseFailureReasons.NoComposeFile); + throw new InvalidOperationException( + $"docker-compose.yaml not found under {options.OutputDir}. Run `bootstrap publish` first."); + } + logger.Info($" using compose file {composeFile}"); + return composeFile; + } + + /// + /// Loads the existing secrets file via , emitting + /// and rethrowing with an operator-friendly + /// message when the file is absent. Mirrors the shape of . + /// + /// + /// The verb used at the start of the error message ("Backup", "restore", etc.). + /// Preserves the exact per-command wording each phase historically used. + /// + public static GeneratedSecrets LoadRequiredSecretsOrFail( + BootstrapOptions options, + PhaseLogger logger, + string phase, + string commandVerb) + { + try + { + return SecretsPhase.LoadExisting(options); + } + catch (InvalidOperationException ex) + { + logger.PhaseFail(phase, PhaseFailureReasons.MissingSecrets); + throw new InvalidOperationException( + $"{commandVerb} requires the admin credentials in secrets/secrets.json under {options.OutputDir}. " + + "Run `bootstrap` first to generate them.", ex); + } + } + + /// + /// Validates that is non-empty and + /// returns the (adminUser, adminPassword) pair. Emits + /// and throws when the password is + /// missing or empty. + /// + public static (string AdminUser, string AdminPassword) RequireAdminPassword( + GeneratedSecrets secrets, + PhaseLogger logger, + string phase) + { + var adminUser = $"{secrets.PostgresUser}_admin"; + var adminPassword = secrets.PostgresAdminPassword; + if (string.IsNullOrEmpty(adminPassword)) + { + logger.PhaseFail(phase, PhaseFailureReasons.MissingAdminPassword); + throw new InvalidOperationException( + "secrets/secrets.json does not contain a PostgresAdminPassword. " + + "Either it predates DatabaseInitPhase or it was hand-edited; re-run `bootstrap`."); + } + return (adminUser, adminPassword); + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/PhaseLoggerAdapter.cs b/tools/Interfold.Bootstrapper/Phases/PhaseLoggerAdapter.cs new file mode 100644 index 00000000..df803b9c --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/PhaseLoggerAdapter.cs @@ -0,0 +1,17 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.DatabaseBootstrap; + +namespace Interfold.Bootstrapper.Phases; + +/// +/// Tiny adapter from (bootstrapper-specific) to +/// (the framework-agnostic surface the shared seed +/// orchestrator consumes). Lives in the bootstrapper so Interfold.DatabaseBootstrap +/// can stay free of any concrete logging dependency. +/// +internal sealed class PhaseLoggerAdapter(PhaseLogger inner) : IDatabaseInitLogger +{ + public void Info(string message) => inner.Info(message); + public void Warn(string message) => inner.Warn(message); + public void Error(string message) => inner.Error(message); +} diff --git a/tools/Interfold.Bootstrapper/Phases/PrerequisitesPhase.cs b/tools/Interfold.Bootstrapper/Phases/PrerequisitesPhase.cs new file mode 100644 index 00000000..01ca9021 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/PrerequisitesPhase.cs @@ -0,0 +1,391 @@ +using System.Runtime.InteropServices; +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Enums; + +namespace Interfold.Bootstrapper.Phases; + +/// Phase 1 — verifies platform support and installs Docker Engine + Compose, +/// openssl, and a persistent fs.aio-max-nr setting (Scylla/Seastar startup requirement). +internal static partial class PrerequisitesPhase +{ + // Seastar's own startup error text; keep aligned with scripts/docker/ensure-host-aio.sh + // and tests/integration/Interfold.IntegrationTests.Shared/TestServices/HostAioPrerequisite.cs. + private const int AioPerNodeMin = 66_563; + private const int AioPerNodeRecommended = 116_562; + private const int AioHeadroom = 50_000; + private const string AioSysctlPath = "/proc/sys/fs/aio-max-nr"; + private const string SysctlDropIn = "/etc/sysctl.d/99-interfold.conf"; + + /// Raw-string overload for the pre-validation peek in + /// . + internal static int ResolveScyllaNodeCount(string? databaseMode) + => ResolveScyllaNodeCount( + databaseMode.TryParseWire(out var mode) ? mode : DatabaseMode.Single); + + internal static int ResolveScyllaNodeCount(DatabaseMode databaseMode) => databaseMode switch + { + DatabaseMode.Multi => 7, + DatabaseMode.Cassandra => 0, + _ => 1, + }; + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + string Phase = BootstrapPhase.Prereqs.ToWireName(); + logger.PhaseStart(Phase); + + EnsureLinux(logger); + EnsureRoot(logger); + + var distro = DistroInfo.Read(); + logger.Info($" detected distro: {distro.PrettyName ?? distro.Id} (family={distro.Family})"); + + if (distro.Family == DistroFamily.Unknown) + { + logger.PhaseFail(Phase, PhaseFailureReasons.UnsupportedDistro); + throw new InvalidOperationException( + $"Unsupported Linux distribution '{distro.Id}'. Supported families: Debian/Ubuntu, RHEL/Fedora. " + + "See docs/SELF_HOSTING.md for tested distros."); + } + + await EnsureDockerAsync(distro, logger, ct).ConfigureAwait(false); + await EnsureOpenSslAsync(distro, logger, ct).ConfigureAwait(false); + + // Tolerant peek — defaults to single-node baseline on missing/malformed/unrecognised. + // ConfigPhase still owns full schema validation. + var scyllaNodes = await PeekScyllaNodeCountAsync(options, logger, ct).ConfigureAwait(false); + await EnsureAioLimitAsync(scyllaNodes, logger, ct).ConfigureAwait(false); + + logger.PhaseDone(Phase); + } + + private static async Task PeekScyllaNodeCountAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + if (string.IsNullOrEmpty(options.ConfigPath) || !File.Exists(options.ConfigPath)) + { + return ResolveScyllaNodeCount(null); + } + + try + { + await using var stream = File.OpenRead(options.ConfigPath); + using var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct).ConfigureAwait(false); + if (doc.RootElement.TryGetProperty("databaseMode", out var modeElement) && + modeElement.ValueKind == JsonValueKind.String) + { + return ResolveScyllaNodeCount(modeElement.GetString()); + } + } + catch (Exception ex) + { + // ConfigPhase will surface a useful error against the same file next. + logger.Warn($"could not pre-read databaseMode from {options.ConfigPath} for AIO sizing ({ex.GetType().Name}); defaulting to single-node baseline."); + } + + return ResolveScyllaNodeCount(null); + } + + private static void EnsureLinux(PhaseLogger logger) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return; + } + logger.PhaseFail(BootstrapPhase.Prereqs.ToWireName(), PhaseFailureReasons.NonLinuxHost); + throw new InvalidOperationException( + "The bootstrapper is Linux-only. For local development use `aspire run` from " + + "hosts/Interfold.AppHost instead."); + } + + private static void EnsureRoot(PhaseLogger logger) + { + if (NativeMethods.geteuid() == 0) return; + + logger.PhaseFail(BootstrapPhase.Prereqs.ToWireName(), PhaseFailureReasons.NonRoot); + throw new InvalidOperationException( + "Run the bootstrapper with sudo. Installing Docker, writing to /etc/sysctl.d, " + + "and editing the system trust store all require root."); + } + + private static partial class NativeMethods + { + [LibraryImport("libc")] + internal static partial uint geteuid(); + } + + private static async Task EnsureDockerAsync(DistroInfo distro, PhaseLogger logger, CancellationToken ct) + { + if (await ProcessRunner.ExistsOnPathAsync("docker", ct).ConfigureAwait(false)) + { + var compose = await ProcessRunner.RunAsync("docker", ["compose", "version"], ct: ct).ConfigureAwait(false); + if (compose.ExitCode == 0) + { + logger.Info(" docker + compose plugin already present"); + return; + } + logger.Warn("docker found but `docker compose` is missing; installing compose plugin."); + } + + logger.Info(" installing docker engine + compose plugin from docker.com..."); + // Stock distro repos ship no docker-compose-plugin; installing from Docker's official + // repo per https://docs.docker.com/engine/install/ is the only path. + switch (distro.Family) + { + case DistroFamily.Debian: + await ConfigureDockerAptRepoAsync(distro, logger, ct).ConfigureAwait(false); + await RunAptInstallAsync( + ["docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", + "docker-compose-plugin", "ca-certificates"], + logger, ct).ConfigureAwait(false); + break; + case DistroFamily.RedHat: + await ConfigureDockerDnfRepoAsync(distro, logger, ct).ConfigureAwait(false); + await RunDnfInstallAsync( + ["docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", + "docker-compose-plugin", "ca-certificates"], + logger, ct).ConfigureAwait(false); + break; + default: + throw new InvalidOperationException($"Cannot install Docker on distro family {distro.Family}."); + } + + // Wrap in try/catch because Process.Start throws Win32Exception on missing binary + // (Alpine/minimal containers ship without systemd). + try + { + await ProcessRunner.RunAsync("systemctl", ["enable", "--now", "docker"], ct: ct).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.Warn($"could not run `systemctl enable --now docker` ({ex.GetType().Name}: {ex.Message}); " + + "start dockerd manually if it isn't already running."); + } + } + + private static async Task ConfigureDockerAptRepoAsync(DistroInfo distro, PhaseLogger logger, CancellationToken ct) + { + // Docker ships apt repos for ubuntu + debian only; downstream debian-likes fall back. + var dockerDistro = ResolveDebianFamilyDockerDistro(distro); + var codename = distro.VersionCodename ?? throw new InvalidOperationException( + $"Could not determine VERSION_CODENAME for {distro.PrettyName ?? distro.Id}. " + + "The bootstrapper needs this to choose the correct Docker apt suite."); + + var arch = await ResolveDpkgArchAsync(ct).ConfigureAwait(false); + + logger.Info($" configuring apt repo: download.docker.com/linux/{dockerDistro} suite={codename} arch={arch}"); + + // Standard third-party keyring location on Debian 12+ / Ubuntu 22.04+. + var mkKeyring = await ProcessRunner.RunAsync( + "install", ["-m", "0755", "-d", "/etc/apt/keyrings"], ct: ct).ConfigureAwait(false); + if (mkKeyring.ExitCode != 0) + { + throw new InvalidOperationException( + $"Could not prepare /etc/apt/keyrings (exit {mkKeyring.ExitCode}): {mkKeyring.StdErr.Trim()}"); + } + + // In-process download so the phase doesn't depend on curl being installed. + const string KeyringPath = "/etc/apt/keyrings/docker.asc"; + var keyUrl = $"https://download.docker.com/linux/{dockerDistro}/gpg"; + await DownloadFileAsync(keyUrl, KeyringPath, ct).ConfigureAwait(false); + var chmod = await ProcessRunner.RunAsync("chmod", ["a+r", KeyringPath], ct: ct).ConfigureAwait(false); + if (chmod.ExitCode != 0) + { + // apt-get update surfaces a clearer message if the file really isn't readable. + logger.Warn($"chmod a+r {KeyringPath} exited {chmod.ExitCode}: {chmod.StdErr.Trim()}"); + } + + var sourcesLine = + $"deb [arch={arch} signed-by={KeyringPath}] https://download.docker.com/linux/{dockerDistro} {codename} stable\n"; + await File.WriteAllTextAsync("/etc/apt/sources.list.d/docker.list", sourcesLine, ct).ConfigureAwait(false); + } + + private static async Task ConfigureDockerDnfRepoAsync(DistroInfo distro, PhaseLogger logger, CancellationToken ct) + { + // .repo file is self-contained (GPG key URL + signature settings) → no keyring step. + var dockerDistro = ResolveRedHatFamilyDockerDistro(distro); + var repoUrl = $"https://download.docker.com/linux/{dockerDistro}/docker-ce.repo"; + const string RepoPath = "/etc/yum.repos.d/docker-ce.repo"; + + logger.Info($" configuring dnf repo: {repoUrl}"); + await DownloadFileAsync(repoUrl, RepoPath, ct).ConfigureAwait(false); + } + + private static string ResolveDebianFamilyDockerDistro(DistroInfo distro) + { + var id = distro.Id.ToLowerInvariant(); + if (id is "ubuntu" or "debian") return id; + var likes = (distro.IdLike ?? string.Empty).ToLowerInvariant(); + if (likes.Contains("ubuntu")) return "ubuntu"; + if (likes.Contains("debian")) return "debian"; + // Most modern debian-likes are ubuntu-based. + return "ubuntu"; + } + + private static string ResolveRedHatFamilyDockerDistro(DistroInfo distro) + { + var id = distro.Id.ToLowerInvariant(); + if (id is "fedora" or "rhel" or "centos") return id; + var likes = (distro.IdLike ?? string.Empty).ToLowerInvariant(); + if (likes.Contains("fedora")) return "fedora"; + if (likes.Contains("rhel") || likes.Contains("centos") || likes.Contains("rocky") || likes.Contains("almalinux")) + { + return "rhel"; + } + return "rhel"; + } + + private static async Task ResolveDpkgArchAsync(CancellationToken ct) + { + try + { + var result = await ProcessRunner.RunAsync("dpkg", ["--print-architecture"], ct: ct).ConfigureAwait(false); + if (result.ExitCode == 0 && !string.IsNullOrWhiteSpace(result.StdOut)) + { + return result.StdOut.Trim(); + } + } + catch + { + // dpkg is base on debian/ubuntu; fall through and default to amd64. + } + // arm64 operators can hand-drop their own /etc/apt/sources.list.d/docker.list and re-run. + return "amd64"; + } + + private static async Task DownloadFileAsync(string url, string destinationPath, CancellationToken ct) + { + using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; + http.DefaultRequestHeaders.UserAgent.ParseAdd("interfold-bootstrap/1.0"); + + using var response = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false); + if (!response.IsSuccessStatusCode) + { + throw new InvalidOperationException( + $"failed to download {url}: HTTP {(int)response.StatusCode} {response.ReasonPhrase}"); + } + + Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + await using var fs = File.Create(destinationPath); + await response.Content.CopyToAsync(fs, ct).ConfigureAwait(false); + } + + private static async Task EnsureOpenSslAsync(DistroInfo distro, PhaseLogger logger, CancellationToken ct) + { + if (await ProcessRunner.ExistsOnPathAsync("openssl", ct).ConfigureAwait(false)) + { + return; + } + + logger.Info(" installing openssl..."); + switch (distro.Family) + { + case DistroFamily.Debian: + await RunAptInstallAsync(["openssl"], logger, ct).ConfigureAwait(false); + break; + case DistroFamily.RedHat: + await RunDnfInstallAsync(["openssl"], logger, ct).ConfigureAwait(false); + break; + } + } + + /// Shared install seam for the mDNS prompts; both callers get identical + /// DEBIAN_FRONTEND/-y shape and error surface. + internal static async Task RunInstallAsync( + DistroInfo distro, + IEnumerable packages, + PhaseLogger logger, + CancellationToken ct) + { + switch (distro.Family) + { + case DistroFamily.Debian: + await RunAptInstallAsync(packages, logger, ct).ConfigureAwait(false); + break; + case DistroFamily.RedHat: + await RunDnfInstallAsync(packages, logger, ct).ConfigureAwait(false); + break; + default: + throw new InvalidOperationException( + $"Cannot install packages on unsupported distro family {distro.Family}. " + + $"Manual install required. Distro: {distro.PrettyName ?? distro.Id}."); + } + } + + private static async Task RunAptInstallAsync(IEnumerable packages, PhaseLogger logger, CancellationToken ct) + { + var env = new Dictionary { ["DEBIAN_FRONTEND"] = "noninteractive" }; + var update = await ProcessRunner.RunAsync("apt-get", ["update"], environment: env, ct: ct).ConfigureAwait(false); + if (update.ExitCode != 0) + { + logger.Warn($"apt-get update exited {update.ExitCode}: {update.StdErr.Trim()}"); + } + var install = await ProcessRunner.RunAsync("apt-get", + ["install", "-y", .. packages], environment: env, ct: ct).ConfigureAwait(false); + if (install.ExitCode != 0) + { + throw new InvalidOperationException( + $"apt-get install failed (exit {install.ExitCode}):\n{install.StdErr.Trim()}"); + } + } + + private static async Task RunDnfInstallAsync(IEnumerable packages, PhaseLogger logger, CancellationToken ct) + { + var install = await ProcessRunner.RunAsync("dnf", ["install", "-y", .. packages], ct: ct).ConfigureAwait(false); + if (install.ExitCode != 0) + { + throw new InvalidOperationException( + $"dnf install failed (exit {install.ExitCode}):\n{install.StdErr.Trim()}"); + } + } + + private static async Task EnsureAioLimitAsync(int scyllaNodes, PhaseLogger logger, CancellationToken ct) + { + if (!File.Exists(AioSysctlPath)) + { + logger.Warn($"{AioSysctlPath} not present; skipping AIO tuning (likely running in a constrained container)."); + return; + } + + // Cassandra-only (scyllaNodes==0) or an operator override still gets persisted so + // reboots can't silently regress. + var minRequired = scyllaNodes * AioPerNodeMin + AioHeadroom; + var target = scyllaNodes * AioPerNodeRecommended + AioHeadroom; + + var current = int.Parse((await File.ReadAllTextAsync(AioSysctlPath, ct).ConfigureAwait(false)).Trim()); + if (current >= minRequired) + { + logger.Info($" fs.aio-max-nr={current} (>= {minRequired} for {scyllaNodes} Scylla node(s)); ok"); + await PersistSysctlAsync(Math.Max(current, target), logger, ct).ConfigureAwait(false); + return; + } + + logger.Info($" fs.aio-max-nr={current} (< {minRequired} for {scyllaNodes} Scylla node(s)); raising to {target}"); + try + { + await File.WriteAllTextAsync(AioSysctlPath, target.ToString(), ct).ConfigureAwait(false); + } + catch (Exception ex) + { + throw new InvalidOperationException( + $"Failed to raise fs.aio-max-nr to {target}: {ex.Message}. " + + $"Set it manually with `sudo sysctl -w fs.aio-max-nr={target}` and re-run.", ex); + } + await PersistSysctlAsync(target, logger, ct).ConfigureAwait(false); + } + + private static async Task PersistSysctlAsync(int value, PhaseLogger logger, CancellationToken ct) + { + var content = $"# Interfold bootstrapper - required by Scylla/Seastar.\nfs.aio-max-nr = {value}\n"; + try + { + await File.WriteAllTextAsync(SysctlDropIn, content, ct).ConfigureAwait(false); + logger.Info($" persisted to {SysctlDropIn}"); + } + catch (Exception ex) + { + logger.Warn($"could not write {SysctlDropIn}: {ex.Message} (setting is in effect for this boot)"); + } + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/PublishPhase.cs b/tools/Interfold.Bootstrapper/Phases/PublishPhase.cs new file mode 100644 index 00000000..0b9de8dc --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/PublishPhase.cs @@ -0,0 +1,498 @@ +using Aspire.Hosting; +using Interfold.AppHost; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Enums; +using Microsoft.Extensions.Configuration; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// Phase 5 — runs the embedded in publish mode +/// to emit docker-compose.yaml + .env from InterfoldAppHost.Configure. +/// Generated secrets flow via so they land in .env, not the +/// compose YAML. +internal static class PublishPhase +{ + // Anchors relative bind-mount paths from InterfoldAppHost.Configure. The graph uses + // "../../scripts/..." that resolves against CWD; setting CWD to {appDir}/_aspire_anchor/inner + // makes those match the dev layout (hosts/Interfold.AppHost/../../scripts). + private static readonly string[] AnchorSegments = ["_aspire_anchor", "inner"]; + + public static async Task RunAsync( + BootstrapOptions options, + BootstrapConfig config, + GeneratedSecrets secrets, + PhaseLogger logger, + CancellationToken ct) + { + const string Phase = "publish"; + logger.PhaseStart(Phase); + + Directory.CreateDirectory(options.OutputDir); + + var anchor = SetupAnchor(); + var previousCwd = Directory.GetCurrentDirectory(); + + try + { + Directory.SetCurrentDirectory(anchor); + + await PublishInProcessAsync(options, config, secrets, logger, ct).ConfigureAwait(false); + } + finally + { + Directory.SetCurrentDirectory(previousCwd); + } + + var composePath = Path.Combine(options.OutputDir, BootstrapArtifactPaths.ComposeFileName); + if (!File.Exists(composePath)) + { + // Aspire >=13 sometimes emits into a subdirectory keyed by the environment name. + var nested = BootstrapArtifactPaths.FindComposeFile(options.OutputDir); + if (nested is null) + { + logger.PhaseFail(Phase, PhaseFailureReasons.ComposeNotEmitted); + throw new InvalidOperationException( + $"Aspire publish completed but no {BootstrapArtifactPaths.ComposeFileName} was produced under {options.OutputDir}."); + } + logger.Info($" compose emitted at {nested}"); + composePath = nested; + } + + // Aspire 13.x emits .env with blank RHS for every parameter and bind-mount source, + // expecting the operator to fill them. The bootstrapper IS the operator. + var envPath = Path.Combine(Path.GetDirectoryName(composePath)!, ".env"); + FillEnvFile(envPath, AppContext.BaseDirectory, options.OutputDir, config, secrets, logger); + EnsureAvatarHostDirectory(config, options.OutputDir, logger); + + // Cassandra mode: stamp `pull_policy: never` on the cassandra service. This is NOT the + // update mechanism (CassandraImagePhase.EnsureBuiltAsync runs `docker build` before any + // compose step); it prevents `docker compose pull` in update-images from erroring on + // `interfold-cassandra:local` (no registry entry → "manifest not found" → non-zero exit + // before the digest diff runs). With the stamp compose reports cassandra as Skipped on + // pull (docker/compose#9376) and every other lifecycle step still targets the local build. + if (CassandraImagePhase.IsCassandraDeployment(config)) + { + StampCassandraPullPolicyNever(composePath, logger); + } + + logger.PhaseDone(Phase); + } + + /// Values to splice into the Aspire-emitted .env. + /// Parameters: keyed by upper-snake env name (e.g. POSTGRES_USER). + /// BindMounts: keyed by the service:container-target identifier from the + /// # Bind mount source for ... comment above each placeholder. Separated from + /// so unit tests can assert the key set without staging a real .env. + internal sealed record EnvReplacements( + IReadOnlyDictionary Parameters, + IReadOnlyDictionary BindMounts); + + /// Translates the operator-facing enum + /// into the orthogonal AppHost toggles InterfoldAppHost reads. The throw guards + /// internal callers (unit tests) that bypass . + internal static (bool IncludeScylla, bool IncludeCassandra, ScyllaTopology ScyllaTopology) TranslateDatabaseMode( + DatabaseMode databaseMode) => databaseMode switch + { + DatabaseMode.Single => (true, false, ScyllaTopology.Single), + DatabaseMode.Multi => (true, false, ScyllaTopology.Multi), + DatabaseMode.Cassandra => (false, true, ScyllaTopology.Single), + _ => throw new InvalidOperationException( + $"Unhandled databaseMode '{databaseMode}'. Expected: single | multi | cassandra."), + }; + + /// Single source of truth for every operator-tunable Aspire parameter that must + /// appear in BOTH the .env replacement dictionary () + /// AND the Parameters:* injection dictionary (PublishInProcessAsync). + /// ConfigKey is the IConfiguration key. + /// EnvKey is the upper-snake spelling Aspire emits into .env; explicit (not derived) so + /// an Aspire rename is a one-line edit — round-trip asserted in + /// PublishSharedAspireParametersTests. Nullable inputs collapse to empty to reproduce + /// the API's "unset env var" branch. Absent by design: graph-only knobs + /// (cluster/topology/image/dashboard/web/ports) that don't round-trip through .env, plus + /// CASSANDRA_IMAGE which flows in via . + internal static IEnumerable<(string ConfigKey, string EnvKey, string Value)> + EnumerateSharedAspireParameters(BootstrapConfig config, GeneratedSecrets secrets) + { + // POSTGRES_INIT_PASSWORD carries the *initial* db_init password so operators who nuke + // pgdata can rerun the bootstrap; DatabaseInitPhase scrambles it again in-cluster. + yield return (AppHostParameterKeys.PostgresUser, "POSTGRES_USER", secrets.PostgresUser); + yield return (AppHostParameterKeys.PostgresPassword, "POSTGRES_PASSWORD", secrets.PostgresPassword); + yield return (AppHostParameterKeys.PostgresDb, "POSTGRES_DB", config.PostgresDatabase); + yield return (AppHostParameterKeys.PostgresInitPassword, "POSTGRES_INIT_PASSWORD", secrets.PostgresInitPassword); + + // Scylla admin password is intentionally absent — that role is created by + // DatabaseInitPhase and its password only lives in internal.secrets. + yield return (AppHostParameterKeys.ScyllaUser, "SCYLLA_USER", secrets.ScyllaUser); + yield return (AppHostParameterKeys.ScyllaPassword, "SCYLLA_PASSWORD", secrets.ScyllaPassword); + + // Bootstrap encryption key only. All other secrets (OAuth client secrets, JWT signing keys, + // deep-link HMAC, leaf PFX password, encryption pepper) live in internal.secrets, seeded + // by DatabaseInitPhase and read via SecretsBootstrapService at startup. + yield return (AppHostParameterKeys.EncryptionPrivateKey, "ENCRYPTION_PRIVATE_KEY", secrets.EncryptionPrivateKeyB64); + + // OAuth client IDs are public identifiers (not secrets); empty is a valid + // "provider disabled" signal — the API's scheme registrar skips empty IDs. + yield return (AppHostParameterKeys.GoogleOAuthClientId, "GOOGLE_OAUTH_CLIENT_ID", config.OAuth.GoogleClientId ?? string.Empty); + yield return (AppHostParameterKeys.DiscordOAuthClientId, "DISCORD_OAUTH_CLIENT_ID", config.OAuth.DiscordClientId ?? string.Empty); + yield return (AppHostParameterKeys.AppleOAuthClientId, "APPLE_OAUTH_CLIENT_ID", config.OAuth.AppleClientId ?? string.Empty); + + // API runtime config → OCTOCON_* env vars. ConfigPhase.ResolveDerivedDefaults fills + // empties before Validate; CORS list joined with commas to match the wire format. + yield return (AppHostParameterKeys.ScyllaKeyspace, "SCYLLA_KEYSPACE", config.ScyllaKeyspace.ToWire()); + yield return (AppHostParameterKeys.OAuthCallbackBaseUrl, "OAUTH_CALLBACK_BASE_URL", config.ApiRuntime.CallbackBaseUrl); + yield return (AppHostParameterKeys.JwtAuthority, "JWT_AUTHORITY", config.ApiRuntime.JwtAuthority); + yield return (AppHostParameterKeys.JwtAudience, "JWT_AUDIENCE", config.ApiRuntime.JwtAudience); + yield return (AppHostParameterKeys.CorsAllowedOrigins, "CORS_ALLOWED_ORIGINS", string.Join(",", config.ApiRuntime.CorsAllowedOrigins)); + + // Non-secret operator tuning knobs. Nullable/disabled-when-empty fields serialise as + // "" — the API's binders normalise empty → null, matching the "env var unset" branch. + // AvatarStorageRoot is intentionally absent: the container always sees + // /app/data/avatars (baked via WithEnvironment); the host path is a bind-mount + // source filled by BuildEnvReplacements, not an Aspire parameter. + yield return (AppHostParameterKeys.NodeGroup, "NODE_GROUP", config.Cluster.NodeGroup.ToWire()); + yield return (AppHostParameterKeys.AvatarPublicBase, "AVATAR_PUBLIC_BASE", config.Storage.AvatarPublicBase ?? string.Empty); + yield return (AppHostParameterKeys.OtlpEndpoint, "OTLP_ENDPOINT", config.Observability.OtlpEndpoint ?? string.Empty); + yield return (AppHostParameterKeys.SocketBatchBytesThreshold, "SOCKET_BATCH_BYTES_THRESHOLD", config.Socket.BatchBytesThreshold?.ToString() ?? string.Empty); + yield return (AppHostParameterKeys.DbRetryAttempts, "DB_RETRY_ATTEMPTS", config.Persistence.DbRetryAttempts.ToString()); + yield return (AppHostParameterKeys.DbRetryInitialDelayMs, "DB_RETRY_INITIAL_DELAY_MS", config.Persistence.DbRetryInitialDelayMs.ToString()); + yield return (AppHostParameterKeys.DbRetryMaxDelayMs, "DB_RETRY_MAX_DELAY_MS", config.Persistence.DbRetryMaxDelayMs.ToString()); + yield return (AppHostParameterKeys.HydrationMaxConcurrency, "HYDRATION_MAX_CONCURRENCY", config.Persistence.HydrationMaxConcurrency.ToString()); + } + + internal static EnvReplacements BuildEnvReplacements( + BootstrapConfig config, + GeneratedSecrets secrets, + string baseDir, + string outputDir) + { + // Seeded from the shared enumerator so every operator-tunable value is emitted here AND + // injected as an AppHost Parameter from one source of truth. + var parameters = EnumerateSharedAspireParameters(config, secrets) + .ToDictionary(p => p.EnvKey, p => p.Value, StringComparer.Ordinal); + + if (CassandraImagePhase.IsCassandraDeployment(config)) + { + // Aspire emits `image: "${CASSANDRA_IMAGE}"` for AddDockerfile resources; + // without this entry compose starts the service with an empty image ref. + parameters["CASSANDRA_IMAGE"] = CassandraImagePhase.LocalImageTag; + } + + // Bind-mount lookup. Aspire emits `# Bind mount source for :` above each + // placeholder; we key on "service:target" to resolve the host path. A rename or reorder + // in a future Aspire release surfaces via the unit tests. + var bindMountLookup = new Dictionary(StringComparer.Ordinal) + { + // TLS material lives under {outputDir}/certs/; Kestrel reads leaf.pfx via + // ConfigureApiSelfHostEnv. JWT signing PEMs are in internal.secrets, no mount needed. + [$"{ComposeServices.InterfoldApi}:/certs"] = Path.Combine(outputDir, "certs"), + // Host path from BootstrapConfig.storage.avatarStorageRoot (blank → {outputDir}/data/avatars). + // Container target is always /app/data/avatars (see AvatarsPaths / ContainerMountPaths). + [$"{ComposeServices.InterfoldApi}:{ContainerMountPaths.InterfoldAvatars}"] = + ResolveAvatarHostRoot(config, outputDir), + }; + + // Web-TLS opt-in mounts: cert dir (reused from the API) + envsubst template staged + // under {outputDir}/support by StagePublishSupportFiles. + if (config.Deployment.WebHttps) + { + bindMountLookup[$"{ComposeServices.OctoconWeb}:/certs"] = Path.Combine(outputDir, "certs"); + bindMountLookup[$"{ComposeServices.OctoconWeb}:/etc/nginx/templates/default.conf.template"] = + EmbeddedSupportFiles.SupportFilePath(outputDir, EmbeddedSupportFiles.NginxTemplateRelative); + } + + // Region-keyed rackdc mount; single mode → one "scylla" node in "nam", multi mode → one + // node per region. Derived from ScyllaKeyspace so the list can't drift. + if (config.DatabaseMode != DatabaseMode.Cassandra) + { + foreach (var region in ResolveScyllaRegions(config)) + { + var nodeName = ComposeServices.ToScyllaNodeName(region, multiNode: config.DatabaseMode == DatabaseMode.Multi); + bindMountLookup[$"{nodeName}:/etc/scylla/cassandra-rackdc.properties"] = + EmbeddedSupportFiles.SupportFilePath(outputDir, EmbeddedSupportFiles.RackDcRelative(region)); + } + } + + return new EnvReplacements(parameters, bindMountLookup); + } + + internal static string[] ResolveScyllaRegions(BootstrapConfig config) => + config.DatabaseMode == DatabaseMode.Multi + ? Enum.GetValues().Select(k => k.ToWire()).ToArray() + : [ScyllaKeyspace.Nam.ToWire()]; + + /// Materializes embedded bind-mount sources under {outputDir}/support. + internal static void StagePublishSupportFiles(BootstrapConfig config, string outputDir, PhaseLogger logger) + { + var materialized = 0; + + if (config.DatabaseMode != DatabaseMode.Cassandra) + { + foreach (var region in ResolveScyllaRegions(config)) + { + var relative = EmbeddedSupportFiles.RackDcRelative(region); + var target = EmbeddedSupportFiles.SupportFilePath(outputDir, relative); + if (EmbeddedSupportFiles.Materialize(relative, target, logger)) + materialized++; + } + } + + if (config.Deployment.WebHttps) + { + var relative = EmbeddedSupportFiles.NginxTemplateRelative; + var target = EmbeddedSupportFiles.SupportFilePath(outputDir, relative); + if (EmbeddedSupportFiles.Materialize(relative, target, logger)) + materialized++; + } + + if (materialized > 0) + { + logger.Info( + $" staged {materialized} support file(s) under {EmbeddedSupportFiles.SupportRoot(outputDir)} (existing files preserved)"); + } + } + + /// Host directory bind-mounted at . + /// Blank config → {outputDir}/data/avatars (same layout as certs under outputDir). + internal static string ResolveAvatarHostRoot(BootstrapConfig config, string outputDir) + { + if (!string.IsNullOrWhiteSpace(config.Storage.AvatarStorageRoot)) + return Path.GetFullPath(config.Storage.AvatarStorageRoot); + + return Path.GetFullPath(Path.Combine(outputDir, "data", "avatars")); + } + + /// Creates the host avatar directory and relaxes permissions so the non-root + /// API container can write uploads. + internal static void EnsureAvatarHostDirectory(BootstrapConfig config, string outputDir, PhaseLogger logger) + { + var hostRoot = ResolveAvatarHostRoot(config, outputDir); + Directory.CreateDirectory(hostRoot); + UnixFilePermissions.SetWorldWritable(hostRoot, logger, "avatar storage"); + logger.Info($" avatar host storage: {hostRoot}"); + } + + /// Rewrites Aspire's blank .env RHSes with concrete secret/parameter/bind-mount + /// values. Unknown keys are left untouched so future + /// additions degrade gracefully (blank key surfaces to the operator). + private static void FillEnvFile( + string envPath, + string baseDir, + string outputDir, + BootstrapConfig config, + GeneratedSecrets secrets, + PhaseLogger logger) + { + if (!File.Exists(envPath)) + { + logger.Warn($".env not found at {envPath}; skipping value rewrite"); + return; + } + + StagePublishSupportFiles(config, outputDir, logger); + var replacements = BuildEnvReplacements(config, secrets, baseDir, outputDir); + var (rewritten, skipped) = ApplyReplacementsToEnvFile(envPath, replacements); + + logger.Info($" .env: filled {rewritten} value(s)"); + if (skipped.Count > 0) + { + logger.Warn($".env: {skipped.Count} key(s) left blank: {string.Join(", ", skipped)}"); + } + } + + /// Reads , applies in + /// place, and returns (rewrittenCount, unmatchedBlankKeys). Internal so unit tests can drive + /// the comment-pair logic directly against an in-memory .env. + internal static (int Rewritten, IReadOnlyList Skipped) ApplyReplacementsToEnvFile( + string envPath, EnvReplacements replacements) + { + var lines = File.ReadAllLines(envPath); + string? pendingBindMountKey = null; + var rewritten = 0; + var skipped = new List(); + for (var i = 0; i < lines.Length; i++) + { + var line = lines[i]; + + // Comment ↔ following KEY= line pair. + if (line.StartsWith("# Bind mount source for ", StringComparison.Ordinal)) + { + pendingBindMountKey = line["# Bind mount source for ".Length..].Trim(); + continue; + } + + var eq = line.IndexOf('='); + if (eq <= 0) + { + pendingBindMountKey = null; + continue; + } + + var key = line[..eq]; + string? value = null; + + if (pendingBindMountKey is not null && replacements.BindMounts.TryGetValue(pendingBindMountKey, out var src)) + { + value = src; + } + else if (replacements.Parameters.TryGetValue(key, out var paramVal)) + { + value = paramVal; + } + + if (value is not null) + { + lines[i] = $"{key}={value}"; + rewritten++; + } + else if (line.EndsWith('=')) + { + // Unrecognised blank RHS; operator may need to fix it before `docker compose up`. + skipped.Add(key); + } + + pendingBindMountKey = null; + } + + File.WriteAllLines(envPath, lines); + return (rewritten, skipped); + } + + /// Inserts pull_policy: never after image: "${CASSANDRA_IMAGE}" on + /// the cassandra service; idempotent. Uses the placeholder token as the anchor (unique to + /// the cassandra service in Aspire's AddDockerfile("cassandra", ...) emission) so we + /// never stamp another service. Targeted find+insert avoids a full YAML round-trip that + /// could reorder Aspire's other keys or reflow compose anchors. + internal static void StampCassandraPullPolicyNever(string composePath, PhaseLogger? logger = null) + { + var lines = File.ReadAllLines(composePath).ToList(); + for (var i = 0; i < lines.Count; i++) + { + var line = lines[i]; + if (!line.Contains("${CASSANDRA_IMAGE}", StringComparison.Ordinal)) continue; + + // Idempotency guard so re-runs of `bootstrap publish` don't double-stamp. + var next = lines.Skip(i + 1).FirstOrDefault(l => !string.IsNullOrWhiteSpace(l)); + if (string.Equals(next?.Trim(), "pull_policy: never", StringComparison.Ordinal)) + { + logger?.Info(" compose: cassandra service already has pull_policy: never"); + return; + } + + var indent = line[..(line.Length - line.TrimStart().Length)]; + lines.Insert(i + 1, $"{indent}pull_policy: never"); + File.WriteAllLines(composePath, lines); + logger?.Info(" compose: stamped pull_policy: never on cassandra service"); + return; + } + // No cassandra service in the compose file — older cassandra-mode setups that skip + // AddDockerfile don't need the stamp. No-op rather than throw. + logger?.Warn("compose: no ${CASSANDRA_IMAGE} anchor found; pull_policy stamp skipped"); + } + + private static string SetupAnchor() + { + // Aspire relative bind-mount placeholders resolve against CWD; anchor mimics dev layout. + var baseDir = AppContext.BaseDirectory; + var anchor = Path.Combine(baseDir, Path.Combine(AnchorSegments)); + Directory.CreateDirectory(anchor); + return anchor; + } + + private static async Task PublishInProcessAsync( + BootstrapOptions options, + BootstrapConfig config, + GeneratedSecrets secrets, + PhaseLogger logger, + CancellationToken ct) + { + // Aspire 13.x publish requires `--operation publish --step publish`. Without --step + // the AppHost emits only aspire-manifest.json then blocks on the CLI backchannel + // (we're not under the CLI). The docker-compose publisher is selected by the + // AddDockerComposeEnvironment registration in InterfoldAppHost.Configure, not by --publisher. + var publishArgs = new[] + { + "--operation", "publish", + "--step", "publish", + "--output-path", options.OutputDir, + }; + + var builder = DistributedApplication.CreateBuilder(new DistributedApplicationOptions + { + Args = publishArgs, + DisableDashboard = true, + }); + + var (includeScylla, includeCassandra, scyllaTopology) = TranslateDatabaseMode(config.DatabaseMode); + + // Parameters:* injected via IConfiguration; Aspire writes secret parameter values into + // the .env file rather than the compose YAML at publish time. Seeded from the shared + // enumerator; extras below are graph-only knobs (topology, image, ports, cluster name) + // that never round-trip through .env. + var injected = EnumerateSharedAspireParameters(config, secrets) + .ToDictionary(p => p.ConfigKey, p => (string?)p.Value, StringComparer.Ordinal); + + // ClusterName is IConfiguration-read (like include-scylla / scylla-topology) rather than + // an Aspire Parameter — it also feeds Scylla's WithArgs list and that overload takes + // plain strings. Baked into compose as both CASSANDRA_CLUSTER_NAME and --cluster-name. + injected[AppHostParameterKeys.ClusterName] = config.ClusterName; + injected[AppHostParameterKeys.IncludeScylla] = BoolWire.ToWireValue(includeScylla); + injected[AppHostParameterKeys.IncludeCassandra] = BoolWire.ToWireValue(includeCassandra); + injected[AppHostParameterKeys.ScyllaTopology] = scyllaTopology.ToWireValue(); + // Bootstrapper always uses a pre-built API image; this switches off the AddProject<> path. + injected[AppHostParameterKeys.ApiImage] = config.ApiImage; + // Aspire dev dashboard would pull an MCR-nightly image at compose-up — unwanted in prod. + injected[AppHostParameterKeys.IncludeDashboard] = BoolWire.FalseValue; + // WebHttps implies IncludeWeb (cert wiring is useless without the container). WebTls stays + // driven by webHttps alone so includeWeb-only stacks get HTTP-only for external-proxy setups. + injected[AppHostParameterKeys.IncludeWeb] = BoolWire.ToWireValue(config.Deployment.IncludeWeb || config.Deployment.WebHttps); + injected[AppHostParameterKeys.WebTls] = BoolWire.ToWireValue(config.Deployment.WebHttps); + // nginx server_name accepts DNS/IP literals but not CIDR → same "primary host" rule as + // ConfigPhase.ResolveDerivedDefaults. The `_` fallback in PickServerName covers only the + // bypass-validation dev path that skips ConfigPhase entirely. + injected[AppHostParameterKeys.WebServerName] = PickServerName(config.Deployment.Hosts); + injected[AppHostParameterKeys.PortsPostgres] = config.Ports.Postgres.ToString(); + injected[AppHostParameterKeys.PortsScylla] = config.Ports.Scylla.ToString(); + injected[AppHostParameterKeys.PortsApiHttp] = config.Ports.ApiHttp.ToString(); + injected[AppHostParameterKeys.PortsApiHttps] = config.Ports.ApiHttps.ToString(); + injected[AppHostParameterKeys.PortsWebHttp] = config.Ports.WebHttp.ToString(); + injected[AppHostParameterKeys.PortsWebHttps] = config.Ports.WebHttps.ToString(); + + builder.Configuration.AddInMemoryCollection(injected); + + InterfoldAppHost.Configure(builder); + + logger.Info(" invoking Aspire publish..."); + await using var app = builder.Build(); + await app.RunAsync(ct).ConfigureAwait(false); + } + + /// First parseable, non-CIDR entry wins; falls back to nginx's _ catch-all + /// for the bypass-validation dev path ( otherwise rejects + /// an all-CIDR/empty list before this runs). + internal static string PickServerName(IReadOnlyList hosts) + { + foreach (var raw in hosts) + { + if (string.IsNullOrWhiteSpace(raw)) continue; + HostEntry entry; + try + { + entry = HostParser.Parse(raw); + } + catch (FormatException) + { + continue; + } + if (!entry.IsLeafEligible) continue; + return entry.Kind switch + { + HostKind.Dns => entry.DnsName!, + _ => entry.Ip!.ToString(), + }; + } + return "_"; + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/RestorePhase.cs b/tools/Interfold.Bootstrapper/Phases/RestorePhase.cs new file mode 100644 index 00000000..9a0170b6 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/RestorePhase.cs @@ -0,0 +1,254 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// Inverse of . Postgres restores in place via +/// pg_restore --clean --if-exists. Scylla stops the API/web tier, stops the seed, +/// wipes and re-hydrates /var/lib/scylla via docker cp -, and restarts. +/// Destructive; gated by interactive confirmation or +/// . +internal static class RestorePhase +{ + private static readonly string Phase = BootstrapCommand.Restore.ToPhaseLogName(); + + /// Client services stopped before the scylla restore and restarted after. + internal static readonly string[] ScyllaRestoreClientServices = [ComposeServices.InterfoldApi, ComposeServices.OctoconWeb]; + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + logger.PhaseStart(Phase); + + var config = await PhaseArtifactLoader + .LoadRequiredConfigAsync(options, logger, Phase, "restore", ct) + .ConfigureAwait(false); + + var secrets = PhaseArtifactLoader.LoadRequiredSecretsOrFail(options, logger, Phase, "Restore"); + + var composeFile = PhaseArtifactLoader.RequireComposeFileOrFail(options, logger, Phase); + + var backupRoot = BackupPhase.ResolveBackupRoot(options, config); + var (postgresArchive, scyllaArchive) = ResolveArchives(options, backupRoot, logger); + if (postgresArchive is null && scyllaArchive is null) + { + logger.PhaseFail(Phase, PhaseFailureReasons.NoArchives); + throw new InvalidOperationException( + "restore requires at least one of --restore-postgres, --restore-scylla, or --restore-latest " + + $"(with archives under {backupRoot}/)."); + } + + // Non-interactive callers MUST pass --force so a stray systemd unit or CI job + // can't wipe a data volume. + if (!options.RestoreForce) + { + if (options.NonInteractive || Console.IsInputRedirected) + { + logger.PhaseFail(Phase, PhaseFailureReasons.ConfirmationRequired); + throw new InvalidOperationException( + "restore is destructive and requires --force in non-interactive mode. " + + "Re-run interactively without --non-interactive to type 'y' at the confirmation prompt, " + + "or pass --force to skip the prompt."); + } + Console.WriteLine(); + Console.WriteLine("*** DESTRUCTIVE OPERATION ***"); + Console.WriteLine("This will overwrite the current database contents with the archive on disk."); + if (postgresArchive is not null) Console.WriteLine($" postgres <- {postgresArchive}"); + if (scyllaArchive is not null) Console.WriteLine($" scylla <- {scyllaArchive}"); + Console.Write("Type 'y' to proceed, anything else to abort: "); + var answer = Console.ReadLine(); + if (!string.Equals(answer?.Trim(), "y", StringComparison.OrdinalIgnoreCase)) + { + logger.PhaseSkip(Phase, PhaseFailureReasons.Skip.OperatorAborted); + return 0; + } + } + + if (postgresArchive is not null) + { + await RestorePostgresAsync(composeFile, postgresArchive, config, secrets, logger, ct).ConfigureAwait(false); + } + + if (scyllaArchive is not null) + { + await RestoreScyllaAsync(composeFile, scyllaArchive, config, logger, ct).ConfigureAwait(false); + } + + logger.PhaseDone(Phase); + return 0; + } + + /// Explicit paths beat --restore-latest; latest only fills components + /// left unnamed. Returns (null, null) when neither component was chosen. + internal static (string? PostgresArchive, string? ScyllaArchive) ResolveArchives( + BootstrapOptions options, string backupRoot, PhaseLogger logger) + { + string? postgres = options.RestorePostgresArchive; + string? scylla = options.RestoreScyllaArchive; + + if (options.RestoreLatest) + { + if (postgres is null) + { + postgres = BackupStoragePaths.LatestFile(Path.Combine(backupRoot, BackupStoragePaths.PostgresDir), BackupStoragePaths.PostgresArchivePattern)?.FullName; + if (postgres is not null) logger.Info($" resolved --restore-latest postgres: {postgres}"); + } + if (scylla is null) + { + scylla = BackupStoragePaths.LatestFile(Path.Combine(backupRoot, BackupStoragePaths.ScyllaDir), BackupStoragePaths.ScyllaArchivePattern)?.FullName; + if (scylla is not null) logger.Info($" resolved --restore-latest scylla: {scylla}"); + } + } + + if (postgres is not null && !File.Exists(postgres)) + { + throw new InvalidOperationException($"Postgres archive not found: {postgres}"); + } + if (scylla is not null && !File.Exists(scylla)) + { + throw new InvalidOperationException($"Scylla archive not found: {scylla}"); + } + return (postgres, scylla); + } + + /// pg_restore argv. --clean --if-exists keeps re-runs idempotent; + /// --single-transaction keeps a partial failure from leaving a half-restored DB. + internal static IReadOnlyList BuildPgRestoreArgs( + string composeFile, string adminUser, string database) + => DockerCompose.BuildPostgresExecArgs( + composeFile, ComposeServices.Postgres, "pg_restore", adminUser, database, + "--clean", "--if-exists", + "--single-transaction", + "--no-owner"); + + /// docker cp - <id>:<path> argv: - reads a tar from + /// stdin into the container. Mirror of . + internal static IReadOnlyList BuildContainerCpWriteArgs(string containerId, string dataPath) + => DockerCompose.BuildContainerCpIntoContainer(containerId, dataPath); + + private static async Task RestorePostgresAsync( + string composeFile, string archivePath, BootstrapConfig config, GeneratedSecrets secrets, + PhaseLogger logger, CancellationToken ct) + { + var (adminUser, adminPassword) = PhaseArtifactLoader.RequireAdminPassword(secrets, logger, Phase); + + // Idempotent — update-images rollback may have left the stack stopped. + await DockerCompose.UpCheckedAsync(composeFile, [ComposeServices.Postgres], logger, ct).ConfigureAwait(false); + await WaitForPostgresAsync(composeFile, logger, ct).ConfigureAwait(false); + + logger.Info($" postgres: pg_restore --clean --if-exists <- {archivePath}"); + var argv = BuildPgRestoreArgs(composeFile, adminUser, config.PostgresDatabase); + await DatabaseArchiveStreamer.StreamFileToProcessStdinAsync( + "docker", argv, + environment: DatabaseArchiveStreamer.PgPasswordEnv(adminPassword), + sourcePath: archivePath, + decompress: false, + ct: ct).ConfigureAwait(false); + logger.Info(" postgres: restore complete"); + } + + private static async Task RestoreScyllaAsync( + string composeFile, string archivePath, BootstrapConfig config, + PhaseLogger logger, CancellationToken ct) + { + var (service, dataPath) = BackupPhase.ResolveScyllaSeed(config); + + // Stop clients so hydration queries don't race the scylla stop. Best-effort: + // missing services (cassandra-mode without web) get a warning, not a failure. + foreach (var client in ScyllaRestoreClientServices) + { + var stop = await DockerCompose.StopAsync(composeFile, [client], ct: ct).ConfigureAwait(false); + if (stop.ExitCode != 0) + { + logger.Warn($"docker compose stop {client} exited {stop.ExitCode} (missing service?): {stop.StdErr.Trim()}"); + } + } + + // Release file locks before overwriting the data volume. + logger.Info($" scylla: stopping {service}"); + await PhaseRunner.RunOrPhaseFailAsync( + () => DockerCompose.StopAsync(composeFile, [service], ct: ct), + logger, Phase, PhaseFailureReasons.StopScylla, + $"docker compose stop {service}", ct).ConfigureAwait(false); + + // docker cp needs a container (stopped is fine); fresh boxes need one created first. + var containerId = await DockerCompose.ResolveContainerIdAsync(composeFile, service, includeStopped: true, ct: ct).ConfigureAwait(false); + if (string.IsNullOrEmpty(containerId)) + { + // `compose up --no-start` creates without running on every compose v2 version. + await PhaseRunner.RunOrPhaseFailAsync( + () => ProcessRunner.RunAsync("docker", + ["compose", "-f", composeFile, "up", "--no-start", service], ct: ct), + logger, Phase, PhaseFailureReasons.CreateScyllaContainer, + $"docker compose up --no-start {service}", ct).ConfigureAwait(false); + containerId = await DockerCompose.ResolveContainerIdAsync(composeFile, service, includeStopped: true, ct: ct).ConfigureAwait(false); + } + if (string.IsNullOrEmpty(containerId)) + { + logger.PhaseFail(Phase, PhaseFailureReasons.ResolveScyllaContainer); + throw new InvalidOperationException( + $"Failed to resolve container id for compose service '{service}' even after create."); + } + + // `docker cp -` overlays; leftover SSTables from a failed prior run would confuse + // startup. `docker exec` needs a running container, so we wipe via a short-lived + // alpine helper mounted with `--volumes-from` (the portable way to mutate a stopped + // container's volume without knowing the host mountpoint). + logger.Info($" scylla: wiping {dataPath} inside container"); + var wipe = await ProcessRunner.RunAsync("docker", + ["run", "--rm", "--volumes-from", containerId, "alpine:3.20", "sh", "-c", + $"rm -rf {dataPath}/* {dataPath}/.[!.]* 2>/dev/null || true"], + ct: ct).ConfigureAwait(false); + if (wipe.ExitCode != 0) + { + // Some rootless-docker setups unmount oddly; docker cp still overlays. + logger.Warn($"scylla wipe helper exited {wipe.ExitCode}: {wipe.StdErr.Trim()}"); + } + + logger.Info($" scylla: docker cp - {service}({containerId[..Math.Min(12, containerId.Length)]}):{dataPath}"); + var cpArgs = BuildContainerCpWriteArgs(containerId, dataPath); + await DatabaseArchiveStreamer.StreamFileToProcessStdinAsync( + "docker", cpArgs, + environment: null, + sourcePath: archivePath, + decompress: true, + ct: ct).ConfigureAwait(false); + + // Startup re-hydrates from the restored SSTables — no explicit reload needed. + logger.Info($" scylla: starting {service}"); + await PhaseRunner.RunOrPhaseFailAsync( + () => DockerCompose.StartAsync(composeFile, [service], ct: ct), + logger, Phase, PhaseFailureReasons.StartScylla, + $"docker compose start {service}", ct).ConfigureAwait(false); + + // Order matters: API first so the web tier's health probe finds a live upstream. + foreach (var client in ScyllaRestoreClientServices) + { + var start = await DockerCompose.StartAsync(composeFile, [client], ct: ct).ConfigureAwait(false); + if (start.ExitCode != 0) + { + logger.Warn($"docker compose start {client} exited {start.ExitCode} (missing service?): {start.StdErr.Trim()}"); + } + } + + logger.Info(" scylla: restore complete"); + } + + private static Task WaitForPostgresAsync( + string composeFile, PhaseLogger logger, CancellationToken ct) + { + // Cluster is already warm; the DatabaseInitPhase 3-in-a-row check would cost ~4s + // for no real safety gain, so the first pg_isready is the handoff signal. + return Util.PostgresReadinessProbe.WaitAsync( + composeFile, + ComposeServices.Postgres, + TimeSpan.FromMinutes(10), + logger, + ct, + runAsRole: Interfold.Shared.Contracts.Configuration.PostgresRoles.Init); + } + +} + + diff --git a/tools/Interfold.Bootstrapper/Phases/SecretsPhase.cs b/tools/Interfold.Bootstrapper/Phases/SecretsPhase.cs new file mode 100644 index 00000000..7724f600 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SecretsPhase.cs @@ -0,0 +1,200 @@ +using System.Security.Cryptography; +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; + +namespace Interfold.Bootstrapper.Phases; + +/// Phase 3 — generates DB/admin passwords, an RSA-2048 encryption keypair, encryption +/// pepper, JWT signing keypairs (RSA-2048 + ES256), and the deep-link HMAC secret. Persists to +/// deploy/secrets/secrets.json with mode 0600. Idempotent: reruns reuse the existing +/// file unless --rotate-secrets is passed. JWT keys + deep-link secret live only in +/// secrets.json until seeds them into +/// internal.secrets; they are never written as standalone PEMs or bind-mounted. +internal static partial class SecretsPhase +{ + // Drops 0/O, 1/l/I and shell-tricky punctuation; 32 chars ≈ 190 bits of entropy. + private const string PasswordAlphabet = + "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; + + private const int PasswordLength = 32; + + // 60 chars ≈ 357 bits — comfortably above the 256-bit HMAC-SHA256 working budget. + private const int DeepLinkSecretLength = 60; + + private const int RsaKeyBits = 2048; + + public static async Task RunAsync( + BootstrapOptions options, + BootstrapConfig config, + PhaseLogger logger, + CancellationToken ct) + { + const string Phase = "secrets"; + logger.PhaseStart(Phase); + + var secretsPath = Path.Combine(options.OutputDir, BootstrapArtifactPaths.SecretsRelativePath); + if (File.Exists(secretsPath) && !options.RotateSecrets) + { + logger.PhaseSkip(Phase, PhaseFailureReasons.Skip.AlreadyPresent); + var existing = await LoadAsync(secretsPath, ct).ConfigureAwait(false); + // Backfill for secrets.json files that predate DatabaseInitPhase; without this a + // rerun would set an empty admin password (psql ALTER ROLE accepts ''). + var mutated = false; + if (string.IsNullOrEmpty(existing.PostgresInitPassword)) + { + existing.PostgresInitPassword = RandomPassword(); + mutated = true; + } + if (string.IsNullOrEmpty(existing.PostgresAdminPassword)) + { + existing.PostgresAdminPassword = RandomPassword(); + mutated = true; + } + if (string.IsNullOrEmpty(existing.ScyllaAdminPassword)) + { + existing.ScyllaAdminPassword = RandomPassword(); + mutated = true; + } + // Post-store-migration additions; backfill on rerun so pre-migration deployments + // keep working without forcing `--rotate-secrets`. + if (string.IsNullOrEmpty(existing.DeepLinkSecret)) + { + existing.DeepLinkSecret = RandomDeepLinkSecret(); + mutated = true; + } + if (string.IsNullOrEmpty(existing.JwtRsa256PrivateKeyPem)) + { + (existing.JwtRsa256PublicKeyPem, existing.JwtRsa256PrivateKeyPem) = GenerateJwtRsaKeypair(); + mutated = true; + } + if (string.IsNullOrEmpty(existing.JwtEs256PrivateKeyPem)) + { + (existing.JwtEs256PublicKeyPem, existing.JwtEs256PrivateKeyPem) = GenerateJwtEs256Keypair(); + mutated = true; + } + if (mutated) + { + logger.Info(" backfilled missing fields into existing secrets.json"); + await PersistAsync(existing, secretsPath, ct).ConfigureAwait(false); + UnixFilePermissions.SetOwnerOnly(secretsPath, logger); + } + return existing; + } + + // Preserve LeafPfxPassword on rotate-secrets: it wraps leaf.crt/leaf.key, and rotate-secrets + // doesn't touch those bytes. Rotating just the PFX password would break Kestrel's load + // without any security benefit. Use rotate-certs to rotate the wrapped material together. + string? preservedLeafPfxPassword = null; + if (File.Exists(secretsPath)) + { + logger.Info($" --rotate-secrets set: regenerating {secretsPath}"); + try + { + var prior = await LoadAsync(secretsPath, ct).ConfigureAwait(false); + preservedLeafPfxPassword = prior.LeafPfxPassword; + } + catch (Exception ex) + { + logger.Warn($" couldn't load existing secrets to preserve PFX password ({ex.GetType().Name}); regenerating"); + } + } + + var secrets = Generate(); + if (!string.IsNullOrEmpty(preservedLeafPfxPassword)) + { + secrets.LeafPfxPassword = preservedLeafPfxPassword; + } + await PersistAsync(secrets, secretsPath, ct).ConfigureAwait(false); + UnixFilePermissions.SetOwnerOnly(secretsPath, logger); + + logger.PhaseDone(Phase); + return secrets; + } + + /// Reads secrets without regenerating. Used by rotate-certs, which needs + /// the leaf PFX password but must not touch DB credentials. + public static GeneratedSecrets LoadExisting(BootstrapOptions options) + { + var secretsPath = Path.Combine(options.OutputDir, BootstrapArtifactPaths.SecretsRelativePath); + if (!File.Exists(secretsPath)) + { + throw new InvalidOperationException( + $"Cannot rotate certs without an existing secrets file at {secretsPath}. " + + "Run `bootstrap` first."); + } + var json = File.ReadAllText(secretsPath); + return JsonSerializer.Deserialize(json, BootstrapJsonContext.Default.GeneratedSecrets) + ?? throw new InvalidOperationException($"Failed to parse {secretsPath}."); + } + + /// Pure factory for a fresh . + internal static GeneratedSecrets Generate() + { + // Two independent RSA keys: EncryptionPrivateKeyB64 for at-rest data, JwtRsa256* for + // bearer signing. Independent so rotating one doesn't invalidate the other. + using var dataRsa = RSA.Create(RsaKeyBits); + var dataPrivatePem = dataRsa.ExportRSAPrivateKeyPem(); + var dataPrivateB64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(dataPrivatePem)); + + var (jwtRsaPublicPem, jwtRsaPrivatePem) = GenerateJwtRsaKeypair(); + var (jwtEsPublicPem, jwtEsPrivatePem) = GenerateJwtEs256Keypair(); + + return new GeneratedSecrets + { + PostgresUser = "interfold", + PostgresPassword = RandomPassword(), + // Transient — scrambled in-cluster after first-boot bootstrap; .env goes stale by design. + PostgresInitPassword = RandomPassword(), + // Lives in internal.secrets; never injected into compose or AppHost parameters. + PostgresAdminPassword = RandomPassword(), + ScyllaUser = "interfold", + ScyllaPassword = RandomPassword(), + ScyllaAdminPassword = RandomPassword(), + EncryptionPrivateKeyB64 = dataPrivateB64, + EncryptionPepper = RandomPassword(), + DeepLinkSecret = RandomDeepLinkSecret(), + LeafPfxPassword = RandomPassword(), + JwtRsa256PublicKeyPem = jwtRsaPublicPem, + JwtRsa256PrivateKeyPem = jwtRsaPrivatePem, + JwtEs256PublicKeyPem = jwtEsPublicPem, + JwtEs256PrivateKeyPem = jwtEsPrivatePem, + }; + } + + internal static string RandomPassword() + => RandomNumberGenerator.GetString(PasswordAlphabet, PasswordLength); + + internal static string RandomDeepLinkSecret() + => RandomNumberGenerator.GetString(PasswordAlphabet, DeepLinkSecretLength); + + internal static async Task LoadAsync(string path, CancellationToken ct) + { + var json = await File.ReadAllTextAsync(path, ct).ConfigureAwait(false); + return JsonSerializer.Deserialize(json, BootstrapJsonContext.Default.GeneratedSecrets) + ?? throw new InvalidOperationException($"Failed to parse {path}."); + } + + internal static async Task PersistAsync(GeneratedSecrets secrets, string path, CancellationToken ct) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + var json = JsonSerializer.Serialize(secrets, BootstrapJsonContext.Default.GeneratedSecrets); + await File.WriteAllTextAsync(path, json, ct).ConfigureAwait(false); + } + + /// PKCS#8 private + SPKI public PEM — the shapes RSA.ImportFromPem accepts. + internal static (string PublicPem, string PrivatePem) GenerateJwtRsaKeypair() + { + using var rsa = RSA.Create(RsaKeyBits); + return (rsa.ExportSubjectPublicKeyInfoPem(), rsa.ExportPkcs8PrivateKeyPem()); + } + + /// SEC1 private + SPKI public PEM (NIST P-256) — the shapes ECDsa.ImportFromPem + /// accepts. + internal static (string PublicPem, string PrivatePem) GenerateJwtEs256Keypair() + { + using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256); + return (ecdsa.ExportSubjectPublicKeyInfoPem(), ecdsa.ExportECPrivateKeyPem()); + } +} diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdInstallPhase.cs b/tools/Interfold.Bootstrapper/Phases/SystemdInstallPhase.cs new file mode 100644 index 00000000..67503270 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdInstallPhase.cs @@ -0,0 +1,336 @@ +using System.Text; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; + +namespace Interfold.Bootstrapper.Phases; + +/// Installs (and optionally enables) the systemd units for autostart, scheduled +/// backup, and image update: interfold.service (compose up -d), +/// interfold-backup.service/.timer, and interfold-update.service. When +/// is set, also writes an +/// OnSuccess=interfold-update.service drop-in that chains update onto backup — +/// requires systemd >= 249 (preflight refuses older hosts). Templates ship as +/// systemd/* manifest resources so unrendered strings never land on disk. +internal static class SystemdInstallPhase +{ + private static readonly string Phase = BootstrapCommand.InstallService.ToPhaseLogName(); + + private const string DefaultUnitDir = "/etc/systemd/system"; + private const string DefaultBinaryName = "interfold-bootstrap"; + + /// Minimum systemd for OnSuccess= (Ubuntu 22.04+, Debian 12+, Fedora 35+). + internal const int MinSystemdVersionForOnSuccess = 249; + + internal static readonly string[] UnitNames = + [ + SystemdUnitNames.Interfold, + SystemdUnitNames.Backup, + SystemdUnitNames.BackupTimer, + SystemdUnitNames.Update, + ]; + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + logger.PhaseStart(Phase); + + var config = await PhaseArtifactLoader + .LoadRequiredConfigAsync(options, logger, Phase, "install-service", ct) + .ConfigureAwait(false); + var configPath = BootstrapArtifactPaths.ResolveConfigPath(options); + + var unitDir = options.SystemdUnitDir ?? DefaultUnitDir; + var binaryPath = ResolveBinaryPath(options); + var composeFile = ResolveComposeFile(options); + + var renderInput = new SystemdRenderInput( + OutputDir: options.OutputDir, + ComposeFile: composeFile, + ConfigPath: Path.GetFullPath(configPath), + BinaryPath: binaryPath, + OnCalendar: config.Backup.Schedule); + + logger.Info($" rendering units to {unitDir}"); + Directory.CreateDirectory(unitDir); + + foreach (var unitName in UnitNames) + { + var rendered = RenderUnit(unitName, renderInput); + var destination = Path.Combine(unitDir, unitName); + await File.WriteAllTextAsync(destination, rendered, ct).ConfigureAwait(false); + logger.Info($" wrote {destination}"); + } + + if (config.Update.Enabled) + { + await EnsureSystemdSupportsOnSuccessAsync(logger, ct).ConfigureAwait(false); + await WriteBackupOnSuccessDropInAsync(unitDir, logger, ct).ConfigureAwait(false); + } + else + { + // Idempotency for operators toggling update.enabled back off. + RemoveBackupOnSuccessDropInIfPresent(unitDir, logger); + } + + // Skip silently on hosts without systemd-analyze (Windows CI) to keep the + // renderer covered; production hosts always ship it with systemd. + if (await ProcessRunner.ExistsOnPathAsync("systemd-analyze", ct).ConfigureAwait(false)) + { + await VerifyAllUnitsAsync(unitDir, logger, ct).ConfigureAwait(false); + await VerifyCalendarAsync(config.Backup.Schedule, logger, ct).ConfigureAwait(false); + } + else + { + logger.Warn("systemd-analyze not on PATH; skipping unit verification " + + "(install the systemd package on the target host)."); + } + + // --enable-* CLI flags beat the matching config toggles. + var enableAutostart = options.EnableAutostart || config.Backup.AutostartServer; + var enableBackupTimer = options.EnableBackupTimer || config.Backup.Enabled; + + if (await ProcessRunner.ExistsOnPathAsync("systemctl", ct).ConfigureAwait(false) + && options.SystemdUnitDir is null) + { + // Only touch the real /etc/systemd/system; test path (--systemd-unit-dir) stays inert. + await SystemctlAsync(["daemon-reload"], logger, ct).ConfigureAwait(false); + + if (enableAutostart) + { + logger.Info(" enabling interfold.service"); + await SystemctlAsync(["enable", "--now", SystemdUnitNames.Interfold], logger, ct).ConfigureAwait(false); + } + if (enableBackupTimer) + { + logger.Info(" enabling interfold-backup.timer"); + await SystemctlAsync(["enable", "--now", SystemdUnitNames.BackupTimer], logger, ct).ConfigureAwait(false); + } + } + else if (options.SystemdUnitDir is not null) + { + logger.Info(" --systemd-unit-dir set; skipping daemon-reload/enable (test mode)"); + } + else + { + logger.Warn("systemctl not on PATH; units written but not enabled. " + + "Run `systemctl daemon-reload` then `systemctl enable --now ` manually."); + } + + logger.PhaseDone(Phase); + return 0; + } + + /// Token-substitution input for , driven directly by + /// unit tests without a live . + internal sealed record SystemdRenderInput( + string OutputDir, + string ComposeFile, + string ConfigPath, + string BinaryPath, + string OnCalendar); + + /// Reads the embedded template, substitutes every {{TOKEN}}, and returns + /// the rendered text. Internal so tests can drive the renderer without touching disk. + internal static string RenderUnit(string unitName, SystemdRenderInput input) + { + ArgumentException.ThrowIfNullOrWhiteSpace(unitName); + ArgumentNullException.ThrowIfNull(input); + + var resourceName = $"systemd/{unitName}"; + var asm = typeof(SystemdInstallPhase).Assembly; + using var stream = asm.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException( + $"Embedded systemd template '{resourceName}' missing. " + + "Check Interfold.Bootstrapper.csproj's entries."); + using var reader = new StreamReader(stream, Encoding.UTF8); + var template = reader.ReadToEnd(); + + var sb = new StringBuilder(template); + // Every token in the shipped templates must have a matching replacement here; the + // "{{" residue check below fails fast on a new template token without a mapping. + sb.Replace("{{OUTPUT_DIR}}", input.OutputDir); + sb.Replace("{{COMPOSE_FILE}}", input.ComposeFile); + sb.Replace("{{CONFIG_PATH}}", input.ConfigPath); + sb.Replace("{{BINARY_PATH}}", input.BinaryPath); + sb.Replace("{{ON_CALENDAR}}", input.OnCalendar); + var rendered = sb.ToString(); + + if (rendered.Contains("{{", StringComparison.Ordinal)) + { + // Trim the residual token for a tighter error message. + var openIdx = rendered.IndexOf("{{", StringComparison.Ordinal); + var closeIdx = rendered.IndexOf("}}", openIdx, StringComparison.Ordinal); + var snippet = closeIdx > openIdx + ? rendered.Substring(openIdx, closeIdx - openIdx + 2) + : rendered[openIdx..Math.Min(rendered.Length, openIdx + 40)]; + throw new InvalidOperationException( + $"systemd template '{unitName}' contains an unsubstituted token '{snippet}'. " + + "Add the matching replacement to SystemdInstallPhase.RenderUnit."); + } + + return rendered; + } + + private static string ResolveBinaryPath(BootstrapOptions options) + { + if (!string.IsNullOrWhiteSpace(options.BinaryPathOverride)) + { + return Path.GetFullPath(options.BinaryPathOverride); + } + // Canonical install layout the README documents. + return Path.Combine(AppContext.BaseDirectory, DefaultBinaryName); + } + + private static string ResolveComposeFile(BootstrapOptions options) + => BootstrapArtifactPaths.ResolveComposeFileOrConventional(options.OutputDir); + + private static async Task VerifyAllUnitsAsync(string unitDir, PhaseLogger logger, CancellationToken ct) + { + foreach (var unitName in UnitNames) + { + var unitPath = Path.Combine(unitDir, unitName); + var verify = await ProcessRunner.RunAsync( + "systemd-analyze", ["verify", unitPath], ct: ct).ConfigureAwait(false); + if (verify.ExitCode != 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.SystemdAnalyzeVerify); + throw new InvalidOperationException( + $"systemd-analyze verify failed for {unitPath} (exit {verify.ExitCode}):\n" + + $"{verify.StdErr.Trim()}\n{verify.StdOut.Trim()}"); + } + logger.Info($" verified {unitName}"); + } + } + + private static async Task VerifyCalendarAsync(string schedule, PhaseLogger logger, CancellationToken ct) + { + var verify = await ProcessRunner.RunAsync( + "systemd-analyze", ["calendar", schedule], ct: ct).ConfigureAwait(false); + if (verify.ExitCode != 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.InvalidCalendar); + throw new InvalidOperationException( + $"systemd-analyze calendar '{schedule}' rejected the schedule (exit {verify.ExitCode}):\n" + + $"{verify.StdErr.Trim()}\n{verify.StdOut.Trim()}"); + } + logger.Info($" calendar '{schedule}' parses OK"); + } + + private static async Task SystemctlAsync(IReadOnlyList args, PhaseLogger logger, CancellationToken ct) + { + var run = await ProcessRunner.RunAsync("systemctl", args, ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.Systemctl); + throw new InvalidOperationException( + $"systemctl {string.Join(' ', args)} exited {run.ExitCode}: {run.StdErr.Trim()}"); + } + if (!string.IsNullOrWhiteSpace(run.StdOut)) logger.Info(run.StdOut.Trim()); + } + + /// 50- prefix follows systemd convention so operator overrides + /// (e.g. 90-local.conf) still win. + internal const string BackupOnSuccessDropInDir = "interfold-backup.service.d"; + + internal const string BackupOnSuccessDropInFile = "50-chain-update.conf"; + + /// Writes the OnSuccess=interfold-update.service chain drop-in verbatim. + /// Idempotent: re-runs overwrite the file. + internal static async Task WriteBackupOnSuccessDropInAsync( + string unitDir, PhaseLogger logger, CancellationToken ct) + { + var dropInDir = Path.Combine(unitDir, BackupOnSuccessDropInDir); + Directory.CreateDirectory(dropInDir); + var content = ReadEmbeddedTemplate("interfold-backup-onsuccess.conf"); + var destination = Path.Combine(dropInDir, BackupOnSuccessDropInFile); + await File.WriteAllTextAsync(destination, content, ct).ConfigureAwait(false); + logger.Info($" wrote {destination} (chains interfold-update.service after successful backup)"); + } + + /// Removes a previously-installed chain drop-in when the operator opts out. + /// No-op when the file (or directory) is absent. + internal static void RemoveBackupOnSuccessDropInIfPresent(string unitDir, PhaseLogger logger) + { + var destination = Path.Combine(unitDir, BackupOnSuccessDropInDir, BackupOnSuccessDropInFile); + if (!File.Exists(destination)) return; + try + { + File.Delete(destination); + logger.Info($" removed stale {destination} (config.update.enabled=false)"); + } + catch (Exception ex) + { + logger.Warn($"failed to delete stale drop-in {destination}: {ex.Message}"); + } + } + + private static string ReadEmbeddedTemplate(string basename) + { + var resourceName = $"systemd/{basename}"; + var asm = typeof(SystemdInstallPhase).Assembly; + using var stream = asm.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException( + $"Embedded template '{resourceName}' missing. " + + "Check Interfold.Bootstrapper.csproj's entries."); + using var reader = new StreamReader(stream, Encoding.UTF8); + return reader.ReadToEnd(); + } + + /// Fails if the running systemd is older than + /// . Skipped silently when systemctl is + /// missing (test path against a tmp unit dir). + internal static async Task EnsureSystemdSupportsOnSuccessAsync(PhaseLogger logger, CancellationToken ct) + { + if (!await ProcessRunner.ExistsOnPathAsync("systemctl", ct).ConfigureAwait(false)) + { + logger.Warn("systemctl not on PATH; skipping systemd version preflight " + + "for OnSuccess= chaining. If the target systemd is older than " + + $"{MinSystemdVersionForOnSuccess}, the drop-in will fail at daemon-reload."); + return; + } + + var run = await ProcessRunner.RunAsync("systemctl", ["--version"], ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + throw new InvalidOperationException( + $"systemctl --version exited {run.ExitCode} while checking OnSuccess= support: {run.StdErr.Trim()}"); + } + + var version = ParseSystemdMajorVersion(run.StdOut); + if (version is null) + { + logger.Warn( + "could not parse systemd version from `systemctl --version` output; " + + $"proceeding with OnSuccess= drop-in install anyway. Requires systemd >= {MinSystemdVersionForOnSuccess}."); + return; + } + if (version.Value < MinSystemdVersionForOnSuccess) + { + throw new InvalidOperationException( + $"config.update.enabled=true requires systemd >= {MinSystemdVersionForOnSuccess} " + + $"(the target host is running systemd {version.Value}). " + + "Upgrade to Ubuntu 22.04+, Debian 12+, or Fedora 35+, or leave config.update.enabled=false " + + "and invoke `update-images` manually."); + } + logger.Info($" systemd {version.Value} supports OnSuccess= chaining"); + } + + /// Parses systemd <N> from the first line of + /// systemctl --version; null on unrecognised output so callers degrade + /// gracefully on forks. + internal static int? ParseSystemdMajorVersion(string versionOutput) + { + if (string.IsNullOrWhiteSpace(versionOutput)) return null; + var firstLine = versionOutput.Split('\n', 2)[0].Trim(); + var parts = firstLine.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) return null; + if (!parts[0].Equals("systemd", StringComparison.Ordinal)) return null; + // Some distros embed a "249.11" style; strip past the first dot. + var token = parts[1]; + var dot = token.IndexOf('.'); + if (dot > 0) token = token[..dot]; + return int.TryParse(token, System.Globalization.NumberStyles.Integer, + System.Globalization.CultureInfo.InvariantCulture, out var v) ? v : null; + } + +} diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup-onsuccess.conf b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup-onsuccess.conf new file mode 100644 index 00000000..caf68502 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup-onsuccess.conf @@ -0,0 +1,9 @@ +[Unit] +# Chain the update service onto a successful backup. systemd runs OnSuccess= units +# only if the backup exited 0, so a failed backup skips the update — the operator +# still has a chance to intervene before an unattended pull recreates containers +# without a valid rollback snapshot on disk. +# +# Requires systemd >= 249 (Ubuntu 22.04+, Debian 12+, Fedora 35+). The bootstrapper +# refuses to install this drop-in on older systemd; see SystemdInstallPhase. +OnSuccess=interfold-update.service diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.service b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.service new file mode 100644 index 00000000..c93df353 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.service @@ -0,0 +1,13 @@ +[Unit] +Description=Interfold database backup (Postgres + Scylla/Cassandra) +Documentation=https://github.com/Azyyyyyy/octocon +After=interfold.service +Wants=interfold.service + +[Service] +Type=oneshot +WorkingDirectory={{OUTPUT_DIR}} +ExecStart={{BINARY_PATH}} backup --config {{CONFIG_PATH}} --output-dir {{OUTPUT_DIR}} --component all +# Operator-overridable: drop in /etc/systemd/system/interfold-backup.service.d/*.conf +# with [Service] Environment= entries to tweak per-host. The bootstrapper never edits +# user drop-ins on rerun. diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.timer b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.timer new file mode 100644 index 00000000..c25100e5 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-backup.timer @@ -0,0 +1,14 @@ +[Unit] +Description=Interfold scheduled backup timer +Documentation=https://github.com/Azyyyyyy/octocon + +[Timer] +OnCalendar={{ON_CALENDAR}} +# Persistent=true ensures a missed run (host powered off at the scheduled time) fires on +# next boot rather than silently waiting for the next slot. Mirrors the standard +# systemd-tmpfiles-clean.timer pattern. +Persistent=true +Unit=interfold-backup.service + +[Install] +WantedBy=timers.target diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-update.service b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-update.service new file mode 100644 index 00000000..8f3f47a6 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold-update.service @@ -0,0 +1,14 @@ +[Unit] +Description=Interfold image update (docker compose pull + up -d, backed by pre-update backup) +Documentation=https://github.com/Azyyyyyy/octocon +After=interfold.service interfold-backup.service +Wants=interfold.service + +[Service] +Type=oneshot +WorkingDirectory={{OUTPUT_DIR}} +ExecStart={{BINARY_PATH}} update-images --config {{CONFIG_PATH}} --output-dir {{OUTPUT_DIR}} +# Deliberately no TimeoutStartSec override: the update workflow can take several minutes +# on a slow network (image pull) or a large database (pre-update backup) — pin to systemd's +# default (900s) rather than baking a bespoke ceiling that would need to grow over time. +# Operator-overridable via /etc/systemd/system/interfold-update.service.d/*.conf. diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold.service b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold.service new file mode 100644 index 00000000..30c069e9 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdTemplates/interfold.service @@ -0,0 +1,18 @@ +[Unit] +Description=Interfold self-hosted backend (docker-compose) +Documentation=https://github.com/Azyyyyyy/octocon +After=docker.service network-online.target +Requires=docker.service +Wants=network-online.target + +[Service] +Type=oneshot +RemainAfterExit=yes +WorkingDirectory={{OUTPUT_DIR}} +ExecStart=/usr/bin/docker compose -f {{COMPOSE_FILE}} up -d +ExecStop=/usr/bin/docker compose -f {{COMPOSE_FILE}} down +TimeoutStartSec=300 +TimeoutStopSec=120 + +[Install] +WantedBy=multi-user.target diff --git a/tools/Interfold.Bootstrapper/Phases/SystemdUnitNames.cs b/tools/Interfold.Bootstrapper/Phases/SystemdUnitNames.cs new file mode 100644 index 00000000..6693d9f9 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/SystemdUnitNames.cs @@ -0,0 +1,15 @@ +namespace Interfold.Bootstrapper.Phases; + +/// +/// The systemd unit names the bootstrapper installs. Frozen operator contract — +/// `systemctl status interfold` muscle memory, existing installs, and docs all reference +/// them. The template files under Phases/SystemdTemplates/ carry the same names +/// manually (embedded resources can't reference constants); keep them in sync when renaming. +/// +internal static class SystemdUnitNames +{ + public const string Interfold = "interfold.service"; + public const string Backup = "interfold-backup.service"; + public const string BackupTimer = "interfold-backup.timer"; + public const string Update = "interfold-update.service"; +} diff --git a/tools/Interfold.Bootstrapper/Phases/UpdateImagesPhase.cs b/tools/Interfold.Bootstrapper/Phases/UpdateImagesPhase.cs new file mode 100644 index 00000000..818c1402 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Phases/UpdateImagesPhase.cs @@ -0,0 +1,529 @@ +using System.Text.Json; +using Interfold.Bootstrapper.Cli; +using Interfold.Bootstrapper.Configuration; +using Interfold.Bootstrapper.Util; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Phases; + +/// Docker image update workflow: backup → pull → digest-diff → recreate → +/// health-check → prune. Auto-restores or prints the manual rollback recipe on failure. +/// Pre-update backup is always taken unless +/// is set (major schema bumps need the snapshot to exist). Idempotent on "no digest change"; +/// old backups are still pruned so scheduled runs can't accumulate archives forever. +internal static class UpdateImagesPhase +{ + private static readonly string Phase = BootstrapCommand.UpdateImages.ToPhaseLogName(); + + public static async Task RunAsync(BootstrapOptions options, PhaseLogger logger, CancellationToken ct) + { + logger.PhaseStart(Phase); + + var config = await PhaseArtifactLoader + .LoadRequiredConfigAsync(options, logger, Phase, "update-images", ct) + .ConfigureAwait(false); + + var composeFile = PhaseArtifactLoader.RequireComposeFileOrFail(options, logger, Phase); + + // CLI --service > config.update.services > every service. Empty array → pass no names + // to `docker compose`, which interprets that as "act on every service". + var services = ResolveServiceWhitelist(options, config); + if (services.Count > 0) + { + logger.Info($" scoping to services: {string.Join(", ", services)}"); + } + else + { + logger.Info(" scoping to: every compose service"); + } + + var healthTimeout = TimeSpan.FromSeconds( + options.HealthCheckTimeoutOverride ?? config.Update.HealthCheckTimeoutSeconds); + var autoRestore = options.AutoRestore || config.Update.AutoRestoreOnFailure; + var recreate = config.Update.RecreateOnUpdate; + + // Pre = container .Image ids; post = Config.Image tag resolution. + // Avoids `docker compose images`, which exits 1 under the containerd image store + // after a local tag rebuild without recreate (compose#14014 / #14028). + logger.Info(" snapshotting pre-pull image digests"); + var preDigests = await SnapshotImageDigestsAsync( + composeFile, desired: false, logger, ct).ConfigureAwait(false); + + // `all` is the only sensible choice — a partial snapshot couldn't feed an auto-restore. + (string PostgresArchive, string ScyllaArchive)? backupArtifacts = null; + if (!options.SkipPreUpdateBackup) + { + logger.Info(" pre-update backup: forcing component=all"); + var backupOptions = options with + { + Command = BootstrapCommand.Backup, + BackupComponent = BackupDatabaseComponent.All.ToWireValue(), + }; + var backupExit = await BackupPhase.RunAsync(backupOptions, logger, ct).ConfigureAwait(false); + if (backupExit != 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.PreUpdateBackupFailed); + return backupExit; + } + backupArtifacts = ResolveLatestBackupArtifacts(backupOptions, config); + if (backupArtifacts is { } bs) + { + logger.Info($" backup complete: postgres={Path.GetFileName(bs.PostgresArchive)} scylla={Path.GetFileName(bs.ScyllaArchive)}"); + } + } + else + { + logger.Warn("--skip-pre-update-backup set; NO pre-update backup will be taken"); + } + + // Rebuild interfold-cassandra:local pre-pull so Dockerfile edits land via update-images + // without a `bootstrap publish` re-run. Docker's layer cache makes this cheap on no-op. + // Pull side is handled by PublishPhase.StampCassandraPullPolicyNever. + if (ShouldRebuildCassandra(config, services)) + { + logger.Info(" cassandra mode: rebuilding interfold-cassandra:local before pull"); + await CassandraImagePhase.EnsureBuiltAsync(logger, ct).ConfigureAwait(false); + } + + // Non-zero here means we never left the pre-pull state; safe to leave the stack alone. + logger.Info(" docker compose pull ..."); + var pull = await Util.DockerCompose.PullAsync(composeFile, services, ct: ct).ConfigureAwait(false); + if (pull.ExitCode != 0) + { + logger.PhaseFail(Phase, PhaseFailureReasons.PullFailed); + throw new InvalidOperationException($"docker compose pull exited {pull.ExitCode}: {pull.StdErr.Trim()}"); + } + // Forward both streams — docker compose writes per-service progress ("Pulling", + // "Skipped", "Pulled") to stderr; integration tests assert on "Skipped" to prove + // pull_policy: never fired for interfold-cassandra:local. + if (!string.IsNullOrWhiteSpace(pull.StdOut)) logger.Info(pull.StdOut.Trim()); + if (!string.IsNullOrWhiteSpace(pull.StdErr)) logger.Info(pull.StdErr.Trim()); + + // Skip recreate + health check + downtime when nothing actually changed. + logger.Info(" snapshotting post-pull image digests"); + var postDigests = await SnapshotImageDigestsAsync( + composeFile, desired: true, logger, ct).ConfigureAwait(false); + var changedServices = DiffDigests(preDigests, postDigests); + if (changedServices.Count == 0) + { + logger.Info(" no-op: every image already up to date; skipping recreate"); + logger.PhaseDone(Phase); + return 0; + } + logger.Info($" {changedServices.Count} service(s) will be recreated: {string.Join(", ", changedServices)}"); + + // `up -d` recreates only containers whose image ID changed, matching our diff. + if (recreate) + { + await Util.DockerCompose.UpCheckedAsync( + composeFile, services, logger, ct, + phase: Phase, phaseFailReason: PhaseFailureReasons.UpFailed).ConfigureAwait(false); + } + else + { + logger.Warn("config.update.recreateOnUpdate=false; skipping `up -d`. " + + "The new image will NOT take effect until a manual `up -d` is issued."); + } + + // Failure never leaves the stack half-updated without operator-facing recovery. + var healthErr = await CheckStackHealthAsync(composeFile, config, healthTimeout, logger, ct).ConfigureAwait(false); + if (healthErr is not null) + { + logger.PhaseFail(Phase, PhaseFailureReasons.HealthCheckFailed); + await OnHealthCheckFailedAsync( + options, config, composeFile, healthErr, backupArtifacts, autoRestore, logger, ct) + .ConfigureAwait(false); + return 1; + } + + // Skip pruning on --skip-pre-update-backup: no new archive justifies deleting old ones. + if (backupArtifacts is not null) + { + var backupRoot = BackupPhase.ResolveBackupRoot(options, config); + var retainCount = options.BackupRetainOverride ?? config.Backup.RetainCount; + BackupPhase.PruneComponent( + Path.Combine(backupRoot, BackupStoragePaths.PostgresDir), + BackupDatabaseComponent.Postgres, retainCount, logger); + BackupPhase.PruneComponent( + Path.Combine(backupRoot, BackupStoragePaths.ScyllaDir), + BackupDatabaseComponent.Scylla, retainCount, logger); + } + + logger.PhaseDone(Phase); + return 0; + } + + /// CLI --service beats ; both empty → + /// every service. CLI values bypass ConfigPhase, so they're re-checked here against + /// . + internal static IReadOnlyList ResolveServiceWhitelist(BootstrapOptions options, BootstrapConfig config) + { + if (options.UpdateServices is { Length: > 0 }) + { + var unknown = options.UpdateServices + .Where(svc => !ComposeServices.AllValidUpdateServices.Contains(svc, StringComparer.Ordinal)) + .ToArray(); + if (unknown.Length > 0) + { + throw new InvalidOperationException( + $"Unknown --service value(s): {string.Join(", ", unknown)}. " + + $"Expected one of: {string.Join(", ", ComposeServices.AllValidUpdateServices)}."); + } + + return options.UpdateServices; + } + return config.Update.Services; + } + + /// Only cassandra mode, and only when cassandra is in scope; otherwise + /// --service msg-db would trigger an unrelated rebuild. Pull side is handled by + /// . + internal static bool ShouldRebuildCassandra(BootstrapConfig config, IReadOnlyList effectiveServices) + { + if (!CassandraImagePhase.IsCassandraDeployment(config)) return false; + if (effectiveServices.Count == 0) return true; + return effectiveServices.Contains(ComposeServices.Cassandra, StringComparer.Ordinal); + } + + /// Argv for docker compose ps -a --format json (running-digest source). + internal static IReadOnlyList BuildComposePsJsonArgs(string composeFile) + { + return ["compose", "-f", composeFile, "ps", "-a", "--format", "json"]; + } + + /// Argv for docker image inspect returning the image id only. + internal static IReadOnlyList BuildImageInspectIdArgs(string imageRef) + { + return ["image", "inspect", "--format", "{{.Id}}", imageRef]; + } + + /// Argv for docker inspect returning running ImageID and Config.Image + /// (tab-separated) for each container. + internal static IReadOnlyList BuildContainerImageFieldsArgs(IReadOnlyList containerIds) + { + // .Image is the content id the container was created from — compose ps often prints + // the tag name instead, which would never match a post-pull image-inspect digest. + var args = new List(3 + containerIds.Count) + { + "inspect", "--format", "{{.Image}}\t{{.Config.Image}}", + }; + args.AddRange(containerIds); + return args; + } + + /// One compose-ps JSON row: service name and container id. + internal readonly record struct ComposePsRow(string Service, string ContainerId); + + /// Parses docker compose ps -a --format json into per-service rows. + /// Accepts both the JSON-array and JSON-Lines shapes (compose plugin version dependent). + /// Duplicate service rows collapse to last-seen; replicas always recreate in lockstep. + internal static IReadOnlyList ParseComposePsJson(string json) + { + var byService = new Dictionary(StringComparer.Ordinal); + if (string.IsNullOrWhiteSpace(json)) return []; + + var trimmed = json.TrimStart(); + if (trimmed.StartsWith('[')) + { + using var doc = JsonDocument.Parse(json); + if (doc.RootElement.ValueKind == JsonValueKind.Array) + { + foreach (var entry in doc.RootElement.EnumerateArray()) + { + TryAdd(entry, byService); + } + return byService.Values.ToArray(); + } + } + + foreach (var line in json.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (!line.StartsWith('{')) continue; + try + { + using var doc = JsonDocument.Parse(line); + TryAdd(doc.RootElement, byService); + } + catch (JsonException) + { + // Older compose versions occasionally leak stderr into stdout; don't fail the diff. + } + } + return byService.Values.ToArray(); + + static void TryAdd(JsonElement obj, Dictionary into) + { + if (obj.ValueKind != JsonValueKind.Object) return; + if (!obj.TryGetProperty("Service", out var svc) || svc.ValueKind != JsonValueKind.String) return; + var service = svc.GetString(); + if (string.IsNullOrEmpty(service)) return; + var containerId = obj.TryGetProperty("ID", out var idProp) && idProp.ValueKind == JsonValueKind.String + ? idProp.GetString() ?? string.Empty + : string.Empty; + into[service] = new ComposePsRow(service, containerId); + } + } + + /// One docker inspect line: running content id + create-time image ref. + internal readonly record struct ContainerImageFields(string RunningImageId, string ConfigImage); + + /// Parses tab-separated {{.Image}}\t{{.Config.Image}} inspect lines. + internal static IReadOnlyList ParseContainerImageFields(string inspectStdout) + { + if (string.IsNullOrWhiteSpace(inspectStdout)) return []; + var lines = inspectStdout.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + var result = new List(lines.Length); + foreach (var line in lines) + { + var tab = line.IndexOf('\t'); + if (tab < 0) + { + result.Add(new ContainerImageFields(line, string.Empty)); + continue; + } + result.Add(new ContainerImageFields(line[..tab], line[(tab + 1)..])); + } + return result; + } + + /// Prefer the tag's current image id; fall back to the running container image id + /// when the tag is missing (local-only image pruned, etc.). + internal static string ResolveDesiredDigest(string runningImageId, string? inspectedTagId) + { + if (!string.IsNullOrEmpty(inspectedTagId)) return inspectedTagId; + return runningImageId; + } + + /// Services whose image ID changed, appeared, or disappeared. Alphabetical output. + internal static IReadOnlyList DiffDigests( + IDictionary before, IDictionary after) + { + var changed = new List(); + foreach (var kvp in after) + { + if (!before.TryGetValue(kvp.Key, out var oldId) || !string.Equals(oldId, kvp.Value, StringComparison.Ordinal)) + { + changed.Add(kvp.Key); + } + } + foreach (var key in before.Keys) + { + if (!after.ContainsKey(key)) changed.Add(key); + } + changed.Sort(StringComparer.Ordinal); + return changed; + } + + /// Pre-pull: container .Image ids. Post-pull: Config.Image tag ids. + private static async Task> SnapshotImageDigestsAsync( + string composeFile, bool desired, PhaseLogger logger, CancellationToken ct) + { + var ps = await ProcessRunner.RunAsync("docker", BuildComposePsJsonArgs(composeFile), ct: ct) + .ConfigureAwait(false); + if (ps.ExitCode != 0) + { + throw new InvalidOperationException( + $"docker compose ps exited {ps.ExitCode}: {ps.StdErr.Trim()}. " + + "Is the stack up? Try `docker compose ps` under the output directory."); + } + + var rows = ParseComposePsJson(ps.StdOut); + if (rows.Count == 0) + { + throw new InvalidOperationException( + "No compose containers found. Is the stack up? Try `docker compose ps` under the output directory."); + } + + var containerIds = rows + .Select(r => r.ContainerId) + .Where(id => !string.IsNullOrWhiteSpace(id)) + .ToArray(); + if (containerIds.Length != rows.Count) + { + throw new InvalidOperationException( + "compose ps returned a service without a container ID; cannot resolve image digests."); + } + + var fields = await InspectContainerImageFieldsAsync(containerIds, ct).ConfigureAwait(false); + if (fields.Count != rows.Count) + { + throw new InvalidOperationException( + $"docker inspect returned {fields.Count} line(s) for {rows.Count} container(s)."); + } + + var result = new Dictionary(StringComparer.Ordinal); + for (var i = 0; i < rows.Count; i++) + { + var row = rows[i]; + var field = fields[i]; + if (!desired) + { + result[row.Service] = field.RunningImageId; + continue; + } + + string? tagId = null; + if (!string.IsNullOrWhiteSpace(field.ConfigImage)) + { + var inspect = await ProcessRunner + .RunAsync("docker", BuildImageInspectIdArgs(field.ConfigImage), ct: ct) + .ConfigureAwait(false); + if (inspect.ExitCode == 0) + { + tagId = inspect.StdOut.Trim(); + } + } + + result[row.Service] = ResolveDesiredDigest(field.RunningImageId, tagId); + } + + logger.Info($" resolved digests for {result.Count} service(s)"); + return result; + } + + private static async Task> InspectContainerImageFieldsAsync( + IReadOnlyList containerIds, CancellationToken ct) + { + var run = await ProcessRunner + .RunAsync("docker", BuildContainerImageFieldsArgs(containerIds), ct: ct) + .ConfigureAwait(false); + if (run.ExitCode != 0) + { + throw new InvalidOperationException( + $"docker inspect exited {run.ExitCode}: {run.StdErr.Trim()}"); + } + + return ParseContainerImageFields(run.StdOut); + } + + /// Bounded probe across Postgres (pg_isready), Scylla/Cassandra (nodetool status), + /// and the API (GET /health/ready). Returns null on success, else an operator-facing + /// message. Shared deadline so no single tier can starve the others. + private static async Task CheckStackHealthAsync( + string composeFile, BootstrapConfig config, TimeSpan totalTimeout, + PhaseLogger logger, CancellationToken ct) + { + var deadline = DateTime.UtcNow + totalTimeout; + logger.Info($" health-check: bounded to {totalTimeout.TotalSeconds:F0}s across postgres+scylla+api"); + + // pg_isready TCP probe — succeeds only after the listener (not just the init Unix socket) is up. + var pgErr = await WaitForPostgresReadyAsync(composeFile, deadline, logger, ct).ConfigureAwait(false); + if (pgErr is not null) return pgErr; + + // nodetool status is the leanest probe that doesn't need the app password; passes + // once the node reports UN. + var (scyllaService, _) = BackupPhase.ResolveScyllaSeed(config); + var scErr = await WaitForScyllaReadyAsync(composeFile, scyllaService, deadline, logger, ct).ConfigureAwait(false); + if (scErr is not null) return scErr; + + var apiErr = await WaitForApiReadyAsync(config.Ports.ApiHttp, deadline, logger, ct).ConfigureAwait(false); + if (apiErr is not null) return apiErr; + + logger.Info(" health-check: all three tiers ready"); + return null; + } + + private static Task WaitForPostgresReadyAsync( + string composeFile, DateTime deadline, PhaseLogger logger, CancellationToken ct) + => PostgresReadinessProbe.TryWaitUntilAsync(composeFile, ComposeServices.Postgres, deadline, logger, ct); + + private static async Task WaitForScyllaReadyAsync( + string composeFile, string service, DateTime deadline, PhaseLogger logger, CancellationToken ct) + { + var attempt = 0; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + // nodetool exits 0 once gossip settles and the node is UN. + var probe = await ProcessRunner.RunAsync("docker", + ["compose", "-f", composeFile, "exec", "-T", service, "nodetool", "status"], + ct: ct).ConfigureAwait(false); + if (probe.ExitCode == 0 && probe.StdOut.Contains("UN", StringComparison.Ordinal)) + { + logger.Info($" scylla ({service}) ready after {attempt} probe(s)"); + return null; + } + try { await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); } + catch (OperationCanceledException) { throw; } + } + return $"scylla ({service}) did not report UN within the health-check budget"; + } + + private static Task WaitForApiReadyAsync( + int apiHttpPort, DateTime deadline, PhaseLogger logger, CancellationToken ct) + => ApiReadinessProbe.TryWaitUntilAsync(apiHttpPort, deadline, logger, ct); + + private static async Task OnHealthCheckFailedAsync( + BootstrapOptions options, BootstrapConfig config, string composeFile, + string healthErr, (string PostgresArchive, string ScyllaArchive)? backupArtifacts, + bool autoRestore, PhaseLogger logger, CancellationToken ct) + { + logger.Error($"health check failed: {healthErr}"); + + // Best-effort log dump so operators don't have to shell in for a diagnosis. + var suspects = new[] { ComposeServices.Postgres, ComposeServices.ScyllaSingle, ComposeServices.ScyllaNam, ComposeServices.Cassandra, ComposeServices.InterfoldApi, ComposeServices.OctoconWeb }; + await ComposeLogDumper.DumpAsync(composeFile, suspects, tailLines: 200, logger, ct).ConfigureAwait(false); + + // Clean stopped state so a restore/retry doesn't fight half-recreated containers. + logger.Info(" docker compose down ..."); + var down = await DockerCompose.DownAsync(composeFile, ct: ct).ConfigureAwait(false); + if (down.ExitCode != 0) + { + logger.Warn($"docker compose down exited {down.ExitCode}: {down.StdErr.Trim()}"); + } + + if (autoRestore) + { + if (backupArtifacts is not { } archives) + { + logger.Error("--auto-restore was requested but no pre-update backup exists (--skip-pre-update-backup used?)."); + return; + } + logger.Info(" --auto-restore: invoking RestorePhase against pre-update archives"); + var restoreOptions = options with + { + Command = BootstrapCommand.Restore, + RestorePostgresArchive = archives.PostgresArchive, + RestoreScyllaArchive = archives.ScyllaArchive, + RestoreForce = true, + }; + var restoreExit = await RestorePhase.RunAsync(restoreOptions, logger, ct).ConfigureAwait(false); + if (restoreExit != 0) + { + logger.Error($"auto-restore failed with exit code {restoreExit}. Manual intervention required."); + } + return; + } + + // Copy-pasteable manual rollback command. + if (backupArtifacts is { } bs) + { + logger.Warn("update failed; the pre-update backup is on disk. To roll back manually:"); + var cmd = $"interfold-bootstrap restore --config \"{Path.GetFullPath(BootstrapArtifactPaths.ResolveConfigPath(options))}\" --output-dir \"{options.OutputDir}\" --restore-postgres \"{bs.PostgresArchive}\" --restore-scylla \"{bs.ScyllaArchive}\" --force"; + Console.Error.WriteLine(); + Console.Error.WriteLine(cmd); + Console.Error.WriteLine(); + } + else + { + logger.Warn("update failed and --skip-pre-update-backup was used; no automatic recovery path available."); + } + } + + /// Newest postgres + scylla archive by mtime — called immediately after the + /// pre-update backup, so "newest" is this run's output. Null when either is missing + /// (auto-restore downgrades to a warning). + internal static (string PostgresArchive, string ScyllaArchive)? ResolveLatestBackupArtifacts( + BootstrapOptions options, BootstrapConfig config) + { + var backupRoot = BackupPhase.ResolveBackupRoot(options, config); + var pg = BackupStoragePaths.LatestFile(Path.Combine(backupRoot, BackupStoragePaths.PostgresDir), BackupStoragePaths.PostgresArchivePattern); + var sc = BackupStoragePaths.LatestFile(Path.Combine(backupRoot, BackupStoragePaths.ScyllaDir), BackupStoragePaths.ScyllaArchivePattern); + if (pg is null || sc is null) return null; + return (pg.FullName, sc.FullName); + } + + } + + diff --git a/tools/Interfold.Bootstrapper/Program.cs b/tools/Interfold.Bootstrapper/Program.cs new file mode 100644 index 00000000..0acca462 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Program.cs @@ -0,0 +1,10 @@ +namespace Interfold.Bootstrapper; + +internal static class Program +{ + public static Task Main(string[] args) + { + // CLI surface implemented in Cli/RootCli.cs. + return Cli.RootCli.RunAsync(args); + } +} diff --git a/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm.pubxml b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm.pubxml new file mode 100644 index 00000000..8fd14cb8 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm.pubxml @@ -0,0 +1,27 @@ + + + + + FileSystem + Release + Any CPU + net10.0 + linux-arm + true + true + false + true + partial + true + embedded + + diff --git a/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm64.pubxml b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm64.pubxml new file mode 100644 index 00000000..0859598b --- /dev/null +++ b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-arm64.pubxml @@ -0,0 +1,27 @@ + + + + + FileSystem + Release + Any CPU + net10.0 + linux-arm64 + true + true + false + true + partial + true + embedded + + diff --git a/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-x64.pubxml b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-x64.pubxml new file mode 100644 index 00000000..8c4a4226 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Properties/PublishProfiles/linux-x64.pubxml @@ -0,0 +1,39 @@ + + + + + FileSystem + Release + Any CPU + net10.0 + linux-x64 + true + true + false + true + partial + true + embedded + + diff --git a/tools/Interfold.Bootstrapper/Util/ApiReadinessProbe.cs b/tools/Interfold.Bootstrapper/Util/ApiReadinessProbe.cs new file mode 100644 index 00000000..654aa42e --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/ApiReadinessProbe.cs @@ -0,0 +1,47 @@ +using System.Net; +using Interfold.Bootstrapper.Cli; +using Interfold.Shared.Contracts.Configuration; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Shared HTTP readiness probe for the API's /health/ready endpoint. Used by +/// (post-launch health gate) and +/// (post-recreate health check) — both previously +/// carried near-identical polling loops. +/// +internal static class ApiReadinessProbe +{ + /// + /// Polls http://localhost:{apiHttpPort}/health/ready until it returns 200 or + /// is reached. Returns null on success, or a short + /// operator-facing description on timeout. + /// + public static async Task TryWaitUntilAsync( + int apiHttpPort, DateTime deadline, PhaseLogger logger, CancellationToken ct) + { + using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }; + var url = $"http://localhost:{apiHttpPort}{HealthEndpoints.Ready}"; + var attempt = 0; + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + try + { + var resp = await http.GetAsync(url, ct).ConfigureAwait(false); + if (resp.StatusCode == HttpStatusCode.OK) + { + logger.Info($" api ready at {url} after {attempt} attempt(s)"); + return null; + } + } + catch (HttpRequestException) { /* container may not be listening yet */ } + catch (TaskCanceledException) when (!ct.IsCancellationRequested) { /* per-request timeout */ } + + try { await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); } + catch (OperationCanceledException) { throw; } + } + return $"api did not return 200 at {url} within the health-check budget"; + } +} diff --git a/tools/Interfold.Bootstrapper/Util/ComposeLogDumper.cs b/tools/Interfold.Bootstrapper/Util/ComposeLogDumper.cs new file mode 100644 index 00000000..3704e7eb --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/ComposeLogDumper.cs @@ -0,0 +1,57 @@ +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Best-effort compose log dumper used after health-check failures to surface container +/// output for diagnosis. Previously duplicated between +/// (all-service dump) and (per-service dump). +/// +internal static class ComposeLogDumper +{ + /// + /// Dumps docker compose logs to stderr for operator diagnosis. + /// + /// When is null or empty, runs a single + /// docker compose logs --tail {tailLines} covering every service. + /// When services are specified, iterates per-service and prefixes each block + /// with a header so the operator can distinguish backends in the output. + /// + /// + public static async Task DumpAsync( + string composeFile, + IEnumerable? services, + int tailLines, + PhaseLogger logger, + CancellationToken ct) + { + var serviceList = services as IReadOnlyList + ?? services?.ToArray() + ?? []; + + if (serviceList.Count == 0) + { + // All-service dump in a single compose call. + var logs = await ProcessRunner.RunAsync("docker", + ["compose", "-f", composeFile, "logs", "--tail", tailLines.ToString(System.Globalization.CultureInfo.InvariantCulture)], + ct: ct).ConfigureAwait(false); + if (!string.IsNullOrWhiteSpace(logs.StdOut)) Console.Error.WriteLine(logs.StdOut); + if (!string.IsNullOrWhiteSpace(logs.StdErr)) Console.Error.WriteLine(logs.StdErr); + } + else + { + // Per-service dump with a header so the operator can distinguish backends. + foreach (var svc in serviceList) + { + var logs = await ProcessRunner.RunAsync("docker", + ["compose", "-f", composeFile, "logs", "--tail", tailLines.ToString(System.Globalization.CultureInfo.InvariantCulture), svc], + ct: ct).ConfigureAwait(false); + if (logs.ExitCode == 0 && !string.IsNullOrWhiteSpace(logs.StdOut)) + { + logger.Warn($"--- {svc} logs (last {tailLines} lines) ---"); + Console.Error.WriteLine(logs.StdOut); + } + } + } + } +} diff --git a/tools/Interfold.Bootstrapper/Util/DatabaseArchiveStreamer.cs b/tools/Interfold.Bootstrapper/Util/DatabaseArchiveStreamer.cs new file mode 100644 index 00000000..e732d2fe --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/DatabaseArchiveStreamer.cs @@ -0,0 +1,119 @@ +using System.IO.Compression; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Trust-boundary helpers for backup and restore archive plumbing. Both phases +/// (BackupPhase and RestorePhase) previously hand-rolled a wrapper around +/// that stitched together three concerns: +/// +/// Streaming a running process's stdout to a file, optionally through a gzip encoder. +/// Streaming a source file into a process's stdin, optionally through a gzip decoder. +/// Injecting PGPASSWORD onto the process environment (never on the argv, so it +/// can't surface in ps output on a shared host). +/// +/// Centralising here means the two symmetrical paths (dump-to-disk, restore-from-disk) +/// share one authoritative shape for how the compressed transfer buffer is built and how +/// the admin password is smuggled onto the exec — a future change to either invariant +/// (e.g. bumping gzip level, moving off the process env for password propagation) moves +/// in lockstep instead of drifting between the two phases. +/// +internal static class DatabaseArchiveStreamer +{ + /// + /// The canonical env-var name that Postgres client tools (pg_dump, + /// pg_restore, psql) consult for the password. Kept as a constant so any + /// site that needs to build a per-invocation env dict never re-types the string. + /// + public const string PgPasswordEnvVar = "PGPASSWORD"; + + /// + /// Canonical env dictionary carrying . The two phases + /// used to build this dict inline; centralising forces the trust boundary to move + /// through one factory so a future addition (e.g. PGSSLROOTCERT) can be added + /// once instead of in both call sites. + /// + public static IDictionary PgPasswordEnv(string password) + => new Dictionary { [PgPasswordEnvVar] = password }; + + /// + /// Runs with and streams its + /// stdout directly into . Throws if the process exits + /// non-zero; partial output is preserved on disk for diagnosis but the caller deletes + /// it before propagating the exception when it's known to be empty/corrupt. + /// + /// + /// When true, the destination file is wrapped in a so the + /// process's stdout is written as gzip-compressed bytes on disk. Used by the Scylla + /// backup path where docker cp emits an uncompressed tar but the target + /// filename is .tar.gz. The Postgres backup path leaves this false — + /// pg_dump -Fc already emits a compressed custom-format archive. + /// + public static Task StreamProcessStdoutToFileAsync( + string fileName, + IReadOnlyList arguments, + IDictionary? environment, + string destinationPath, + CancellationToken ct, + bool compress = false) + => ProcessStreamRunner.RunAsync( + fileName, arguments, environment, + redirectStandardInput: false, + forwardStdout: false, + async proc => + { + await using var fs = File.Create(destinationPath); + if (compress) + { + // CompressionLevel.Fastest keeps CPU well below the docker-cp / disk + // throughput ceiling on typical backup data (SSTables compress poorly + // anyway because they're already LZ4-compressed internally). Higher + // levels would burn CPU for a marginal size win on already-compressed + // payload. + await using var gz = new GZipStream(fs, CompressionLevel.Fastest, leaveOpen: false); + await proc.StandardOutput.BaseStream.CopyToAsync(gz, ct).ConfigureAwait(false); + } + else + { + await proc.StandardOutput.BaseStream.CopyToAsync(fs, ct).ConfigureAwait(false); + } + }, + ct); + + /// + /// Streams the contents of into + /// 's stdin, optionally decompressing with + /// on the way (used for the Scylla restore path, whose + /// on-disk archive is .tar.gz but the target docker cp expects a raw + /// tar on stdin). Throws on non-zero exit; stderr is included in the exception + /// message. Deliberately does NOT redirect stdout to disk — restore commands emit + /// diagnostic output that we want on the operator's terminal. + /// + public static Task StreamFileToProcessStdinAsync( + string fileName, + IReadOnlyList arguments, + IDictionary? environment, + string sourcePath, + bool decompress, + CancellationToken ct) + => ProcessStreamRunner.RunAsync( + fileName, arguments, environment, + redirectStandardInput: true, + forwardStdout: true, + async proc => + { + await using var src = File.OpenRead(sourcePath); + if (decompress) + { + await using var gz = new GZipStream(src, CompressionMode.Decompress, leaveOpen: false); + await gz.CopyToAsync(proc.StandardInput.BaseStream, ct).ConfigureAwait(false); + } + else + { + await src.CopyToAsync(proc.StandardInput.BaseStream, ct).ConfigureAwait(false); + } + await proc.StandardInput.BaseStream.FlushAsync(ct).ConfigureAwait(false); + proc.StandardInput.Close(); + }, + ct); +} diff --git a/tools/Interfold.Bootstrapper/Util/DistroInfo.cs b/tools/Interfold.Bootstrapper/Util/DistroInfo.cs new file mode 100644 index 00000000..cf752d8d --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/DistroInfo.cs @@ -0,0 +1,88 @@ +namespace Interfold.Bootstrapper.Util; + +/// +/// Identifies the Linux distribution family. Drives apt-vs-dnf selection and the trust-store path. +/// +public enum DistroFamily +{ + Unknown, + Debian, + RedHat, +} + +/// +/// Subset of /etc/os-release we care about. See +/// os-release(5). +/// +/// The lower-case OS identifier, e.g. ubuntu, debian, fedora. +/// Optional space-separated downstream chain (e.g. Linux Mint sets this to ubuntu debian). +/// Human-readable name, e.g. Ubuntu 24.04 LTS. +/// Numeric version, e.g. 24.04 for Ubuntu, 40 for Fedora. +/// Debian/Ubuntu codename (noble, bookworm); null on non-Debian distros. +/// Resolved family classification used to pick apt vs dnf. +public sealed record DistroInfo( + string Id, + string? IdLike, + string? PrettyName, + string? VersionId, + string? VersionCodename, + DistroFamily Family) +{ + private const string OsReleasePath = "/etc/os-release"; + + public static DistroInfo Read() + { + if (!File.Exists(OsReleasePath)) + { + return new DistroInfo( + Id: "unknown", + IdLike: null, + PrettyName: null, + VersionId: null, + VersionCodename: null, + Family: DistroFamily.Unknown); + } + + var values = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var raw in File.ReadAllLines(OsReleasePath)) + { + var line = raw.Trim(); + if (line.Length == 0 || line.StartsWith('#')) continue; + var eq = line.IndexOf('='); + if (eq <= 0) continue; + var key = line[..eq].Trim(); + var value = line[(eq + 1)..].Trim().Trim('"'); + values[key] = value; + } + + values.TryGetValue("ID", out var id); + values.TryGetValue("ID_LIKE", out var idLike); + values.TryGetValue("PRETTY_NAME", out var prettyName); + values.TryGetValue("VERSION_ID", out var versionId); + // VERSION_CODENAME is the canonical codename; UBUNTU_CODENAME is the ubuntu-base used by + // downstream debian-likes (Mint, Pop!_OS) when their VERSION_CODENAME is their own name. + if (!values.TryGetValue("VERSION_CODENAME", out var versionCodename)) + { + values.TryGetValue("UBUNTU_CODENAME", out versionCodename); + } + + var family = Resolve(id, idLike); + return new DistroInfo(id ?? "unknown", idLike, prettyName, versionId, versionCodename, family); + } + + private static DistroFamily Resolve(string? id, string? idLike) + { + // ID alone is enough for the canonical distros; ID_LIKE catches downstreams (Mint -> ubuntu, Rocky -> rhel). + var haystack = string.Join(' ', id ?? string.Empty, idLike ?? string.Empty).ToLowerInvariant(); + + if (haystack.Contains("debian") || haystack.Contains("ubuntu")) + { + return DistroFamily.Debian; + } + if (haystack.Contains("rhel") || haystack.Contains("fedora") || haystack.Contains("centos") || haystack.Contains("rocky") || haystack.Contains("almalinux")) + { + return DistroFamily.RedHat; + } + return DistroFamily.Unknown; + } +} diff --git a/tools/Interfold.Bootstrapper/Util/DockerCompose.cs b/tools/Interfold.Bootstrapper/Util/DockerCompose.cs new file mode 100644 index 00000000..09f7d2a0 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/DockerCompose.cs @@ -0,0 +1,199 @@ +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +internal static class DockerCompose +{ + public static async Task UpAsync( + string composeFile, + IReadOnlyList? services = null, + bool detach = true, + bool build = false, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "up" }; + if (detach) args.Add("-d"); + if (build) args.Add("--build"); + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + public static async Task PullAsync( + string composeFile, + IReadOnlyList? services = null, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "pull" }; + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + public static async Task StopAsync( + string composeFile, + IReadOnlyList? services = null, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "stop" }; + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + /// Sister of . Does NOT create containers — pair with + /// when the container may not exist yet. + public static async Task StartAsync( + string composeFile, + IReadOnlyList? services = null, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "start" }; + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + /// adds -v (destructive; operator opt-in). + public static async Task DownAsync( + string composeFile, + IReadOnlyList? services = null, + bool removeVolumes = false, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "down" }; + if (removeVolumes) args.Add("-v"); + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + /// Defaults to -q (container id only). + /// adds -a so stopped-but-created containers appear (restore-path requirement). + public static async Task PsAsync( + string composeFile, + string? service = null, + bool quietIds = true, + bool includeStopped = false, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "ps" }; + if (includeStopped) args.Add("-a"); + if (quietIds) args.Add("-q"); + if (service is not null) args.Add(service); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + public static async Task LogsAsync( + string composeFile, + IReadOnlyList? services = null, + bool follow = false, + int? tail = null, + CancellationToken ct = default) + { + var args = new List { "compose", "-f", composeFile, "logs" }; + if (follow) args.Add("-f"); + if (tail is not null) args.Add($"--tail={tail}"); + if (services is not null) args.AddRange(services); + return await ProcessRunner.RunAsync("docker", args, ct: ct).ConfigureAwait(false); + } + + /// One-liner for the "log intent → up → exit-check → log stdout" quad each phase + /// re-implemented. Emits before throwing when both + /// and are supplied (Launch/Restore + /// bring-ups skip PhaseFail here, so both stay optional). + public static async Task UpCheckedAsync( + string composeFile, + IReadOnlyList? services, + PhaseLogger logger, + CancellationToken ct, + string? phase = null, + string? phaseFailReason = null) + { + var svcLabel = services is { Count: > 0 } ? string.Join(' ', services) : "..."; + logger.Info($" docker compose up -d {svcLabel}"); + var run = await UpAsync(composeFile, services, detach: true, build: false, ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0) + { + if (!string.IsNullOrWhiteSpace(run.StdErr)) logger.Error(run.StdErr.Trim()); + if (phase is not null && phaseFailReason is not null) + { + logger.PhaseFail(phase, phaseFailReason); + } + var svcSuffix = services is { Count: > 0 } ? $" for [{string.Join(", ", services)}]" : string.Empty; + throw new InvalidOperationException( + $"docker compose up -d{svcSuffix} exited with code {run.ExitCode}: {run.StdErr.Trim()}"); + } + if (!string.IsNullOrWhiteSpace(run.StdOut)) logger.Info(run.StdOut.Trim()); + } + + public static Task ExecAsync( + string composeFile, + string service, + string command, + IReadOnlyList? args = null, + IReadOnlyDictionary? env = null, + string? stdin = null, + CancellationToken ct = default) + { + return DockerComposeExec.RunAsync(composeFile, service, command, args, env, stdin, ct); + } + + /// Container id for a compose service via ps -q. Empty when no container + /// exists. matches stopped containers (restore path). + public static async Task ResolveContainerIdAsync( + string composeFile, string service, bool includeStopped = false, CancellationToken ct = default) + { + var run = await PsAsync(composeFile, service, includeStopped: includeStopped, ct: ct).ConfigureAwait(false); + if (run.ExitCode != 0 || string.IsNullOrWhiteSpace(run.StdOut)) return string.Empty; + return run.StdOut + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .FirstOrDefault() ?? string.Empty; + } + + /// Shared non-empty guard for the docker cp argv builders. + public static void ValidateContainerCpParams(string containerId, string dataPath) + { + if (string.IsNullOrWhiteSpace(containerId)) + { + throw new ArgumentException("containerId must be non-empty.", nameof(containerId)); + } + if (string.IsNullOrWhiteSpace(dataPath)) + { + throw new ArgumentException("dataPath must be non-empty.", nameof(dataPath)); + } + } + + /// Shared preamble for admin-role exec into the postgres container. Password flows + /// via PGPASSWORD env, never argv, so it can't leak to ps/audit. is appended verbatim in the order the underlying binary expects. + internal static IReadOnlyList BuildPostgresExecArgs( + string composeFile, string service, string tool, string adminUser, string database, params string[] toolTrailer) + { + var argv = new List(11 + (toolTrailer?.Length ?? 0)) + { + "compose", "-f", composeFile, + "exec", "-T", + "--env", DatabaseArchiveStreamer.PgPasswordEnvVar, + service, + tool, + "-U", adminUser, + "-d", database, + }; + if (toolTrailer is not null && toolTrailer.Length > 0) + { + argv.AddRange(toolTrailer); + } + return argv; + } + + /// docker cp <id>:<path> -: streams a container path to host + /// stdout as raw tar. Backup path. + internal static IReadOnlyList BuildContainerCpFromContainer(string containerId, string containerPath) + { + ValidateContainerCpParams(containerId, containerPath); + return ["cp", $"{containerId}:{containerPath}", "-"]; + } + + /// Mirror of — restore path. + internal static IReadOnlyList BuildContainerCpIntoContainer(string containerId, string containerPath) + { + ValidateContainerCpParams(containerId, containerPath); + return ["cp", "-", $"{containerId}:{containerPath}"]; + } +} diff --git a/tools/Interfold.Bootstrapper/Util/DockerComposeExec.cs b/tools/Interfold.Bootstrapper/Util/DockerComposeExec.cs new file mode 100644 index 00000000..795b9393 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/DockerComposeExec.cs @@ -0,0 +1,35 @@ +namespace Interfold.Bootstrapper.Util; + +internal static class DockerComposeExec +{ + public static async Task RunAsync( + string composeFile, + string service, + string command, + IReadOnlyList? args = null, + IReadOnlyDictionary? env = null, + string? stdin = null, + CancellationToken ct = default) + { + var execArgs = new List { "compose", "-f", composeFile, "exec", "-T" }; + + if (env is not null) + { + foreach (var (key, value) in env) + { + execArgs.Add("-e"); + execArgs.Add($"{key}={value}"); + } + } + + execArgs.Add(service); + execArgs.Add(command); + + if (args is not null) + { + execArgs.AddRange(args); + } + + return await ProcessRunner.RunAsync("docker", execArgs, stdin: stdin, ct: ct).ConfigureAwait(false); + } +} diff --git a/tools/Interfold.Bootstrapper/Util/EmbeddedSupportFiles.cs b/tools/Interfold.Bootstrapper/Util/EmbeddedSupportFiles.cs new file mode 100644 index 00000000..8b08fba7 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/EmbeddedSupportFiles.cs @@ -0,0 +1,155 @@ +using System.Reflection; +using System.Text; +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Embedded support/ resources (Scylla rackdc, nginx template, Cassandra Dockerfile, +/// ensure-host-aio.sh). Read via ; materialize to disk only when Docker +/// needs a host bind-mount path. +/// +internal static class EmbeddedSupportFiles +{ + internal const string ResourcePrefix = "support/"; + + private static Assembly BootstrapperAssembly => typeof(EmbeddedSupportFiles).Assembly; + + internal const string NginxTemplateRelative = "web/nginx/default.conf.template"; + internal const string CassandraDockerfileRelative = "db/cassandra/Dockerfile"; + + internal static string RackDcRelative(string regionWire) => + $"db/scylla/cassandra-rackdc.{regionWire}.properties"; + + internal static string SupportRoot(string outputDir) => + Path.GetFullPath(Path.Combine(outputDir, "support")); + + internal static string SupportFilePath(string outputDir, string relativeSupportPath) + { + var supportRoot = SupportRoot(outputDir); + var targetPath = Path.GetFullPath(Path.Combine( + supportRoot, + relativeSupportPath.Replace('/', Path.DirectorySeparatorChar))); + + if (!IsUnderBase(targetPath, supportRoot)) + { + throw new InvalidOperationException( + $"Support path '{relativeSupportPath}' resolves to '{targetPath}', " + + $"which is outside support root '{supportRoot}'."); + } + + return targetPath; + } + + /// Opens an embedded support resource as a read stream. + internal static Stream Open(string relativeSupportPath) + { + ArgumentException.ThrowIfNullOrWhiteSpace(relativeSupportPath); + var resourceName = ToResourceName(relativeSupportPath); + return BootstrapperAssembly.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException( + $"Embedded support resource '{resourceName}' was not found."); + } + + /// + /// Writes to when + /// missing. Returns true when a new file was written; false when skipped + /// (already exists or lost a concurrent create race). + /// + internal static bool Materialize(string relativeSupportPath, string targetPath, PhaseLogger? logger = null) + { + ArgumentException.ThrowIfNullOrWhiteSpace(relativeSupportPath); + ArgumentException.ThrowIfNullOrWhiteSpace(targetPath); + + if (File.Exists(targetPath)) + return false; + + var resourceName = ToResourceName(relativeSupportPath); + try + { + WriteResourceToPath(resourceName, targetPath); + return true; + } + catch (IOException) + { + // Lost a concurrent Materialize race between File.Exists and rename — equivalent bytes. + return false; + } + } + + /// Materializes under ; returns the absolute host path. + internal static string MaterializeUnderSupportRoot( + string outputDir, string relativeSupportPath, PhaseLogger? logger = null) + { + var targetPath = SupportFilePath(outputDir, relativeSupportPath); + Materialize(relativeSupportPath, targetPath, logger); + return targetPath; + } + + internal static IReadOnlyList EnumerateSupportResourceNames() => + BootstrapperAssembly + .GetManifestResourceNames() + .Where(n => n.StartsWith(ResourcePrefix, StringComparison.Ordinal)) + .ToList(); + + internal static string ReadAllText(string relativeSupportPath) + { + using var stream = Open(relativeSupportPath); + using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true); + return reader.ReadToEnd(); + } + + private static string ToResourceName(string relativeSupportPath) => + ResourcePrefix + relativeSupportPath.Replace('\\', '/').TrimStart('/'); + + private static bool IsUnderBase(string candidate, string canonicalBase) + { + var withSep = canonicalBase.EndsWith(Path.DirectorySeparatorChar) + ? canonicalBase + : canonicalBase + Path.DirectorySeparatorChar; + return candidate.StartsWith(withSep, StringComparison.Ordinal) || candidate == canonicalBase; + } + + private static void WriteResourceToPath(string resourceName, string targetPath) + { + using var stream = BootstrapperAssembly.GetManifestResourceStream(resourceName) + ?? throw new InvalidOperationException( + $"Manifest resource '{resourceName}' enumerated but GetManifestResourceStream returned null."); + + Directory.CreateDirectory(Path.GetDirectoryName(targetPath)!); + + var tempPath = $"{targetPath}.{Environment.ProcessId}.tmp"; + try + { + using (var fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + stream.CopyTo(fs); + } + + File.Move(tempPath, targetPath, overwrite: false); + } + catch + { + try { File.Delete(tempPath); } catch { /* best-effort cleanup */ } + throw; + } + + MaybeSetExecutableBit(targetPath); + } + + private static void MaybeSetExecutableBit(string targetPath) + { + if (!OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS()) + return; + + if (!targetPath.EndsWith(".sh", StringComparison.Ordinal)) + return; + + var current = File.GetUnixFileMode(targetPath); + File.SetUnixFileMode(targetPath, + current + | UnixFileMode.UserExecute + | UnixFileMode.GroupExecute + | UnixFileMode.OtherExecute); + } +} diff --git a/tools/Interfold.Bootstrapper/Util/PhaseRunner.cs b/tools/Interfold.Bootstrapper/Util/PhaseRunner.cs new file mode 100644 index 00000000..7ede1e6f --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/PhaseRunner.cs @@ -0,0 +1,24 @@ +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +internal static class PhaseRunner +{ + public static async Task RunOrPhaseFailAsync( + Func> op, + PhaseLogger logger, + string phase, + string reason, + string failurePrefix, + CancellationToken ct) + { + var result = await op(); + if (result.ExitCode != 0) + { + logger.PhaseFail(phase, reason); + throw new InvalidOperationException( + $"{failurePrefix} exited {result.ExitCode}: {result.StdErr.Trim()}"); + } + return result; + } +} diff --git a/tools/Interfold.Bootstrapper/Util/PostgresReadinessProbe.cs b/tools/Interfold.Bootstrapper/Util/PostgresReadinessProbe.cs new file mode 100644 index 00000000..0a9e6d6c --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/PostgresReadinessProbe.cs @@ -0,0 +1,100 @@ +using Interfold.DatabaseBootstrap; +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +internal static class PostgresReadinessProbe +{ + /// + /// Convenience overload for callers whose readiness policy is "first successful probe + /// = ready" (no 3-in-a-row confirmation). Used by RestorePhase, whose + /// original 5-minute deadline predates the consecutive-success check that landed in + /// DatabaseInitPhase: restore already owns a live cluster, so a single + /// pg_isready success is a sufficient handoff signal and staying with 3 would + /// add ~4s to every restore. + /// + public static Task WaitAsync( + string composeFile, + string service, + TimeSpan timeout, + PhaseLogger logger, + CancellationToken ct, + string? runAsRole = null) => + WaitAsync( + composeFile, + service, + new PostgresReadinessOptions(timeout, RequiredConsecutiveSuccesses: 1, RunAsRole: runAsRole), + logger, + ct); + + public static async Task WaitAsync( + string composeFile, + string service, + PostgresReadinessOptions options, + PhaseLogger logger, + CancellationToken ct) + { + var deadline = DateTime.UtcNow.Add(options.Timeout); + var attempt = 0; + var consecutiveSuccesses = 0; + + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + + var args = new List { "compose", "-f", composeFile, "exec", "-T", service, "pg_isready", "-h", "127.0.0.1", "-p", "5432" }; + if (!string.IsNullOrWhiteSpace(options.RunAsRole)) + { + args.Add("-U"); + args.Add(options.RunAsRole); + } + + var probe = await ProcessRunner.RunAsync("docker", args.ToArray(), ct: ct).ConfigureAwait(false); + + if (probe.ExitCode == 0) + { + consecutiveSuccesses++; + if (consecutiveSuccesses >= options.RequiredConsecutiveSuccesses) + { + logger.Info( + $" postgres ready after {attempt} probe(s) ({options.RequiredConsecutiveSuccesses} consecutive TCP pg_isready, normal mode confirmed)"); + return; + } + } + else if (consecutiveSuccesses > 0) + { + consecutiveSuccesses = 0; + } + await Task.Delay(TimeSpan.FromSeconds(2), ct).ConfigureAwait(false); + } + throw new TimeoutException($"{service} did not become ready within {options.Timeout.TotalMinutes} minutes."); + } + + /// + /// Deadline-based readiness probe that returns null on success or an error + /// message on timeout — suitable for callers that aggregate multiple tier-health + /// results rather than letting a timeout propagate as an exception. + /// + public static async Task TryWaitUntilAsync( + string composeFile, + string service, + DateTime deadline, + PhaseLogger logger, + CancellationToken ct) + { + var remaining = deadline - DateTime.UtcNow; + if (remaining <= TimeSpan.Zero) + return $"postgres ({service}) did not report ready within the health-check budget"; + + try + { + await WaitAsync(composeFile, service, remaining, logger, ct).ConfigureAwait(false); + return null; + } + catch (TimeoutException) + { + return $"postgres ({service}) did not report ready within the health-check budget"; + } + } +} diff --git a/tools/Interfold.Bootstrapper/Util/ProcessRunner.cs b/tools/Interfold.Bootstrapper/Util/ProcessRunner.cs new file mode 100644 index 00000000..1951fb05 --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/ProcessRunner.cs @@ -0,0 +1,91 @@ +using System.Diagnostics; +using System.Text; + +namespace Interfold.Bootstrapper.Util; + +internal sealed record ProcessRunResult(int ExitCode, string StdOut, string StdErr); + +/// +/// Lightweight wrapper over with stdout/stderr capture, timeout, and an +/// optional stdin payload. Used by phases that shell out to apt, dnf, +/// docker, update-ca-certificates, etc. +/// +internal static class ProcessRunner +{ + public static async Task RunAsync( + string fileName, + IEnumerable arguments, + string? workingDirectory = null, + IDictionary? environment = null, + string? stdin = null, + CancellationToken ct = default) + { + var psi = new ProcessStartInfo + { + FileName = fileName, + WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory, + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = stdin is not null, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var arg in arguments) + { + psi.ArgumentList.Add(arg); + } + if (environment is not null) + { + foreach (var kvp in environment) + { + psi.Environment[kvp.Key] = kvp.Value; + } + } + + using var process = new Process { StartInfo = psi }; + var stdout = new StringBuilder(); + var stderr = new StringBuilder(); + process.OutputDataReceived += (_, e) => { if (e.Data is not null) stdout.AppendLine(e.Data); }; + process.ErrorDataReceived += (_, e) => { if (e.Data is not null) stderr.AppendLine(e.Data); }; + + if (!process.Start()) + { + throw new InvalidOperationException($"Failed to start process '{fileName}'."); + } + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + if (stdin is not null) + { + await process.StandardInput.WriteAsync(stdin).ConfigureAwait(false); + await process.StandardInput.FlushAsync(ct).ConfigureAwait(false); + process.StandardInput.Close(); + } + + try + { + await process.WaitForExitAsync(ct).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + try { process.Kill(entireProcessTree: true); } catch { /* best effort */ } + throw; + } + + return new ProcessRunResult(process.ExitCode, stdout.ToString(), stderr.ToString()); + } + + public static async Task ExistsOnPathAsync(string command, CancellationToken ct = default) + { + try + { + var which = await RunAsync("/bin/sh", ["-c", $"command -v {command}"], ct: ct).ConfigureAwait(false); + return which.ExitCode == 0; + } + catch + { + return false; + } + } +} diff --git a/tools/Interfold.Bootstrapper/Util/ProcessStreamRunner.cs b/tools/Interfold.Bootstrapper/Util/ProcessStreamRunner.cs new file mode 100644 index 00000000..5f38839b --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/ProcessStreamRunner.cs @@ -0,0 +1,91 @@ +using System.Diagnostics; +using System.Text; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Thin lifecycle wrapper for processes whose stdout/stdin is piped as a raw stream (e.g. +/// database dump/restore). The caller supplies a delegate that wires +/// the directional I/O; this helper owns startup, stderr capture, kill-on-cancel, and +/// exit-code validation. +/// +internal static class ProcessStreamRunner +{ + /// Executable to run. + /// Arguments for the process. + /// Optional environment variable overrides. + /// Whether to redirect stdin (required for file→stdin pipes). + /// + /// When true, stdout is read via and + /// forwarded to . Use for stdin-direction pipes where the + /// process may emit progress on stdout. + /// + /// + /// Delegate that performs the directional I/O (e.g. stdout→file or file→stdin). + /// Receives the started so it can access + /// or . + /// + /// Cancellation token; cancellation kills the entire process tree. + public static async Task RunAsync( + string fileName, + IReadOnlyList arguments, + IDictionary? environment, + bool redirectStandardInput, + bool forwardStdout, + Func pipe, + CancellationToken ct) + { + var psi = new ProcessStartInfo + { + FileName = fileName, + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = redirectStandardInput, + UseShellExecute = false, + CreateNoWindow = true, + }; + foreach (var a in arguments) psi.ArgumentList.Add(a); + if (environment is not null) + { + foreach (var kvp in environment) + { + psi.Environment[kvp.Key] = kvp.Value; + } + } + + using var proc = new Process { StartInfo = psi }; + var stderr = new StringBuilder(); + proc.ErrorDataReceived += (_, e) => { if (e.Data is not null) stderr.AppendLine(e.Data); }; + if (forwardStdout) + { + proc.OutputDataReceived += (_, e) => { if (e.Data is not null) Console.WriteLine(e.Data); }; + } + + if (!proc.Start()) + { + throw new InvalidOperationException($"Failed to start process '{fileName}'."); + } + proc.BeginErrorReadLine(); + if (forwardStdout) + { + proc.BeginOutputReadLine(); + } + + try + { + await pipe(proc).ConfigureAwait(false); + await proc.WaitForExitAsync(ct).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + try { proc.Kill(entireProcessTree: true); } catch { /* best effort */ } + throw; + } + + if (proc.ExitCode != 0) + { + throw new InvalidOperationException( + $"{fileName} {string.Join(' ', arguments)} exited {proc.ExitCode}: {stderr.ToString().Trim()}"); + } + } +} diff --git a/tools/Interfold.Bootstrapper/Util/ScyllaReadinessProbe.cs b/tools/Interfold.Bootstrapper/Util/ScyllaReadinessProbe.cs new file mode 100644 index 00000000..e47d8ffb --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/ScyllaReadinessProbe.cs @@ -0,0 +1,81 @@ +using Interfold.Bootstrapper.Cli; +using Interfold.DatabaseBootstrap; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Scylla-side sister to . Mirrors the same +/// options-record + probe-loop shape so both readiness surfaces stay legible side by side. +/// +/// +/// Where the postgres probe polls pg_isready through docker compose exec, +/// this one drives a CQL round-trip via — +/// DESCRIBE CLUSTER is cheap, requires an authenticated session, and by definition +/// only succeeds once the node has finished gossip-bootstrap and is accepting auth. The +/// probe tries the app credentials first, then falls back to the built-in +/// cassandra/cassandra pair; either successful response is a green. +/// +/// +/// +/// Previously open-coded as WaitForScyllaAsync at +/// DatabaseInitPhase.cs:176. Extraction lets any future Scylla-mode caller +/// (restore, integration harness) reuse the same policy without duplicating the +/// two-credential fallback + attempt-count logging. +/// +/// +internal static class ScyllaReadinessProbe +{ + public static async Task WaitAsync( + IScyllaExecutor executor, + ScyllaSeedOptions credentials, + ScyllaReadinessOptions options, + PhaseLogger logger, + CancellationToken ct) + { + var deadline = DateTime.UtcNow.Add(options.Timeout); + var attempt = 0; + var consecutiveSuccesses = 0; + + while (DateTime.UtcNow < deadline) + { + ct.ThrowIfCancellationRequested(); + attempt++; + + var asApp = await executor.TryExecCqlAsync( + credentials.AppUser, credentials.AppPassword, "DESCRIBE CLUSTER", ct).ConfigureAwait(false); + if (asApp.Succeeded) + { + consecutiveSuccesses++; + if (consecutiveSuccesses >= options.RequiredConsecutiveSuccesses) + { + logger.Info($" scylla ready (as app user) after {attempt} attempt(s)"); + return; + } + await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); + continue; + } + + var asDefault = await executor.TryExecCqlAsync( + ScyllaCqlTemplates.DefaultUser, ScyllaCqlTemplates.DefaultPassword, + "DESCRIBE CLUSTER", ct).ConfigureAwait(false); + if (asDefault.Succeeded) + { + consecutiveSuccesses++; + if (consecutiveSuccesses >= options.RequiredConsecutiveSuccesses) + { + logger.Info($" scylla ready (as cassandra default) after {attempt} attempt(s)"); + return; + } + await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); + continue; + } + + if (consecutiveSuccesses > 0) + consecutiveSuccesses = 0; + + await Task.Delay(TimeSpan.FromSeconds(3), ct).ConfigureAwait(false); + } + + throw new TimeoutException($"scylla did not become ready within {options.Timeout.TotalMinutes} minutes."); + } +} diff --git a/tools/Interfold.Bootstrapper/Util/UnixFilePermissions.cs b/tools/Interfold.Bootstrapper/Util/UnixFilePermissions.cs new file mode 100644 index 00000000..632dcbbf --- /dev/null +++ b/tools/Interfold.Bootstrapper/Util/UnixFilePermissions.cs @@ -0,0 +1,95 @@ +using System.Runtime.InteropServices; +using Interfold.Bootstrapper.Cli; + +namespace Interfold.Bootstrapper.Util; + +/// +/// Cross-platform wrapper around libc chmod(2) for the "restrict / relax file +/// permissions after writing" pattern the certificate + secrets phases both need. +/// No-op on Windows (where ACLs don't translate cleanly to a Unix mode) — we expect +/// Linux for production self-hosting; the Windows path is exercised only by developer +/// workstations and CI runners for round-tripping test fixtures. +/// +/// +/// +/// Shapes are grep-verified from the phases that used to hold verbatim copies of this +/// P/Invoke: +/// +/// +/// — 0600, owner read/write only. Used for signing +/// material (rootCA.key) and the secrets store (secrets.json) where a non-owner +/// shell user should not be able to read the contents even if they've traversed +/// into the output directory. +/// — 0644, readable by any UID, writable only by +/// the owner. Used for artifacts bind-mounted into the API container which runs as +/// UID 64198 (non-root) and cannot otherwise read the bootstrapper's default 0600 +/// files. The API container never mutates these files so read-only is sufficient. +/// — 0777 on a directory. Used for the avatar +/// host bind-mount so the non-root API process can write uploads without a host-side +/// chown to the container UID. +/// +/// +/// Failures are non-fatal — logged as warnings rather than thrown — because a wrong- +/// permissions file is still functional (just less secure or wrongly-scoped for the +/// downstream mount). The phase completing lets the operator inspect and fix manually. +/// The alternative (fail-fast) would leave the deployment in a "half-generated" state +/// that's harder to recover from than the current permissive-but-warned state. +/// +/// +internal static partial class UnixFilePermissions +{ + private const int S_600 = 0x180; // 0o600 - owner read/write only + private const int S_644 = 0x1A4; // 0o644 - owner read/write, group/other read + private const int S_777 = 0x1FF; // 0o777 - world read/write/execute (bind-mount dirs) + + /// + /// Applies 0600 (owner read/write only). Windows: no-op. Failures logged + /// as a warning; the caller continues. + /// + /// The file path to chmod. + /// Where the warning goes when the chmod call returns non-zero. + /// Human-readable label for the file kind used in the warning + /// message (e.g. "key file", "file"). Defaults to "file". + public static void SetOwnerOnly(string path, PhaseLogger logger, string artifactLabel = "file") + => Apply(path, S_600, "0600", logger, artifactLabel); + + /// + /// Applies 0644 (owner read/write, group + other read). Windows: no-op. + /// Failures logged as a warning; the caller continues. + /// + /// The file path to chmod. + /// Where the warning goes when the chmod call returns non-zero. + /// Human-readable label for the file kind used in the warning + /// message (e.g. "cert file", "file"). Defaults to "file". + public static void SetWorldReadable(string path, PhaseLogger logger, string artifactLabel = "file") + => Apply(path, S_644, "0644", logger, artifactLabel); + + /// + /// Applies 0777 (world-writable). Windows: no-op. Used for the avatar host + /// directory so the non-root API container can create upload files. + /// + public static void SetWorldWritable(string path, PhaseLogger logger, string artifactLabel = "directory") + => Apply(path, S_777, "0777", logger, artifactLabel); + + private static void Apply(string path, int mode, string modeDisplay, PhaseLogger logger, string artifactLabel) + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && + !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return; + } + + var rc = NativeMethods.chmod(path, mode); + if (rc != 0) + { + var err = Marshal.GetLastPInvokeError(); + logger.Warn($"chmod({path}, {modeDisplay}) failed: errno={err} ({artifactLabel} written but permissions not adjusted)"); + } + } + + private static partial class NativeMethods + { + [LibraryImport("libc", StringMarshalling = StringMarshalling.Utf8, SetLastError = true)] + internal static partial int chmod(string path, int mode); + } +} diff --git a/tools/Interfold.SPDump/Interfold.SPDump.csproj b/tools/Interfold.SPDump/Interfold.SPDump.csproj new file mode 100644 index 00000000..78e35d47 --- /dev/null +++ b/tools/Interfold.SPDump/Interfold.SPDump.csproj @@ -0,0 +1,24 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + + + + diff --git a/tools/Interfold.SPDump/Program.cs b/tools/Interfold.SPDump/Program.cs new file mode 100644 index 00000000..49d04b27 --- /dev/null +++ b/tools/Interfold.SPDump/Program.cs @@ -0,0 +1,133 @@ +using System.Security.Cryptography; +using Interfold.Infrastructure.DependencyInjection; +using Interfold.Infrastructure.InMemory; +using Interfold.Settings.Api.Services.Http; +using Interfold.Settings.Api.SimplyPlural; +using Interfold.Settings.Contracts.Ids; +using Interfold.Settings.Domain.Abstractions; +using Interfold.Shared.Api.Services; +using Interfold.Shared.Contracts; +using Interfold.Shared.Contracts.Enums; +using Interfold.Shared.Contracts.Ids; +using Interfold.SPDump; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +const int ExitSuccess = 0; +const int ExitImportFailed = 2; +const int ExitUnhandled = 3; + +var options = SpDumpOptions.FromArgs(args); + +var services = new ServiceCollection(); +services.AddLogging(builder => builder.AddConsole()); +services.AddSingleton(new ConfigurationManager()); +services.AddSingleton(TimeProvider.System); + +InMemoryServiceCollectionExtensions.Register(); +services.AddInterfoldPersistence(PersistenceMode.InMemory, cfg => +{ + cfg.ScyllaKeyspace = ScyllaKeyspace.Nam; +}); + +services.AddInterfoldDomainHandlers(); +services.AddSingleton(); + +services.AddTransient(); +services.AddHttpClient(HttpClientNames.SimplyPlural).AddHttpMessageHandler(); + +// -Core overload skips the async IImportJobRunner queue consumer; this utility drives +// ImportAsync directly. +services.AddSimplyPluralImportCore(); + +var provider = services.BuildServiceProvider(); + +using var scope = provider.CreateScope(); +var logger = scope.ServiceProvider.GetRequiredService().CreateLogger("SPDump"); + +RecoveryCode recoveryKey; +if (options.RecoveryKey is { } providedKey) +{ + recoveryKey = providedKey; +} +else +{ + var key = new byte[32]; + RandomNumberGenerator.Fill(key); + var encryptionKeyBase64 = Convert.ToBase64String(key); + recoveryKey = new(encryptionKeyBase64); + logger.LogInformation("Using generated random encryption key (base64): {KeyPreview}", encryptionKeyBase64[..8] + "..."); +} + +try +{ + var importService = scope.ServiceProvider.GetRequiredService(); + importService.WaitForAvatars = true; + + var result = await importService.ImportAsync(SpDumpOptions.DumpSystemId, options.Token, recoveryKey); + if (result.Success) + { + logger.LogInformation("Import succeeded. Alters imported: {Count}", result.AlterCount); + return ExitSuccess; + } + + logger.LogError("Import failed: {Code} {Reason}", result.ErrorCode?.ToWire(), result.ErrorMessage); + return ExitImportFailed; +} +catch (Exception ex) +{ + logger.LogError(ex, "Unhandled exception during SP import"); + return ExitUnhandled; +} + +namespace Interfold.SPDump +{ + /// Parsed CLI: SPDump <sp-token> [recovery-key-base64]. Both values are + /// wrapped in Contracts ID types at the boundary. + internal readonly record struct SpDumpOptions(ImportToken Token, RecoveryCode? RecoveryKey) + { + /// Placeholder system id — the InMemory backend keys stores by whatever id it + /// is handed, so an empty id keeps the utility free of real-system coupling. + public static readonly SystemId DumpSystemId = new(""); + + public static SpDumpOptions FromArgs(string[] args) + { + string spToken = ""; + if (args.Length >= 1) + { + spToken = args[0]; + } + else + { + while (string.IsNullOrWhiteSpace(spToken)) + { + Console.Write("Please put in your SP token:"); + spToken = Console.ReadLine()!; + } + } + + // Blank second arg == "no key provided" (a random one is generated). + RecoveryCode? recoveryKey = args.Length >= 2 && !string.IsNullOrWhiteSpace(args[1]) + ? new RecoveryCode(args[1]) + : null; + return new(new(spToken), recoveryKey); + } + } + + internal sealed class TempAvatarStorage : IAvatarStorage + { + public Task SaveSystemAvatarAsync(SystemId systemId, Stream stream, CancellationToken cancellationToken = default) + { + return Task.FromResult(new AvatarUrl("")); + } + + public Task SaveAlterAvatarAsync(SystemId systemId, AlterId alterId, Stream stream, CancellationToken cancellationToken = default) + { + return Task.FromResult(new AvatarUrl("")); + } + + public Task DeleteByUrlAsync(AvatarUrl? avatarUrl, CancellationToken cancellationToken = default) + => Task.FromResult(false); + } +} \ No newline at end of file diff --git a/web/nginx/default.conf.template b/web/nginx/default.conf.template new file mode 100644 index 00000000..2d84120e --- /dev/null +++ b/web/nginx/default.conf.template @@ -0,0 +1,109 @@ +# Processed by the nginx image's /docker-entrypoint.d/20-envsubst-on-templates.sh +# at container startup. `NGINX_ENVSUBST_FILTER=^NGINX_` (set by the AppHost) limits +# substitution to NGINX_* vars so nginx's own `$host` / `$uri` / `$request_uri` stay +# literal. Every NGINX_* var referenced below MUST be defined on the container +# (empty string is fine, UNSET makes nginx abort with `unknown "" variable` +# because the placeholder is left literal in the rendered conf). +# +# Vars (all set by InterfoldAppHost.Configure when Parameters:web-tls=true): +# NGINX_SERVER_NAME public DNS / IP the leaf cert was issued for +# NGINX_SSL_CERT_FILE leaf cert path inside the container +# NGINX_SSL_KEY_FILE leaf key path inside the container +# NGINX_HTTPS_PORT_SUFFIX ":" the operator mapped to :443, OR empty to +# disable the HTTP->HTTPS redirect on :80 (operator +# chose 443, dropped the HTTPS port post-hoc, or +# webHttps=false). Empty -> :80 serves plaintext. + +server { + listen 80; + server_name ${NGINX_SERVER_NAME}; + + # Conditional 301 -> HTTPS. The `set` captures the envsubsted suffix into an + # nginx variable so the `if` can test it; the `return` uses ${NGINX_*} directly + # so WebHttpsTests can grep the rendered conf for the literal port. Empty + # suffix falls through to the static-serving block below — avoids 301-ing to + # a dead port when the HTTPS endpoint isn't actually reachable. + set $https_port_suffix "${NGINX_HTTPS_PORT_SUFFIX}"; + if ($https_port_suffix != "") { + return 301 https://$host${NGINX_HTTPS_PORT_SUFFIX}$request_uri; + } + + # Plaintext fallback. KEEP IN SYNC with the :443 block below (intentional + # duplication to avoid a second bind-mount). + root /usr/share/nginx/html; + index index.html; + include /etc/nginx/mime.types; + types { + application/wasm wasm; + text/javascript mjs; + } + default_type application/octet-stream; + + location ~* \.(?:js|mjs|wasm|css|woff2?)$ { + try_files $uri =404; + add_header Cache-Control "public, max-age=31536000, immutable"; + } + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + } +} + +server { + listen 443 ssl; + http2 on; + server_name ${NGINX_SERVER_NAME}; + + ssl_certificate ${NGINX_SSL_CERT_FILE}; + ssl_certificate_key ${NGINX_SSL_KEY_FILE}; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_prefer_server_ciphers on; + ssl_session_cache shared:SSL:10m; + ssl_session_timeout 1d; + + root /usr/share/nginx/html; + index index.html; + + # `include` MUST come before the supplemental `types` block. A bare + # `types { ... }` at server scope REPLACES the inherited http-level map (from + # the upstream image's nginx.conf), it doesn't merge — without the include + # only `.wasm` would be recognised and `index.html` would be served as + # `application/octet-stream`, which browsers treat as a file download. + include /etc/nginx/mime.types; + # Supplemental types (a second `types` block at the SAME scope merges). + # `.wasm` is in mime.types since 1.21.4, restated for older base images. + # `.mjs` is NOT in mime.types as of 1.30.x; needed for unbundled K/Wasm + # builds (the current octocon-wasm image is Webpack-bundled and ships no + # .mjs, so this is defensive). + types { + application/wasm wasm; + text/javascript mjs; + } + default_type application/octet-stream; + + gzip on; + gzip_proxied any; + gzip_min_length 256; + gzip_types + text/plain + text/css + text/javascript + application/javascript + application/json + application/wasm + application/octet-stream + image/svg+xml; + + # Kotlin/Wasm Gradle pipeline emits content-hashed JS/MJS/WASM/CSS — safe + # to cache for a year. + location ~* \.(?:js|mjs|wasm|css|woff2?)$ { + try_files $uri =404; + add_header Cache-Control "public, max-age=31536000, immutable"; + } + + # SPA fallback — unmatched paths serve the app shell for client-side routing. + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + } +}