From 0ac8d742da123c9b34d08c331b5498692ab6c449 Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 14:05:16 -0300 Subject: [PATCH 1/7] feat(core,ef): contratos JSON da API, testes de rascunho pela fila e chaves do painel (spec 007) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Abstractions: IDs fortes, enums, FieldDefinition e BindingSource serializados no formato das specs (sem alterar opções JSON do host); FieldDefinitionJson e FieldCompatibility públicos. - ICatalogAdmin: EnqueueStageDraftTestAsync/EnqueuePipelineDraftTestAsync (D-114); filtro por status nas listas; auditoria filtrável por ação e chave; ações LoginSucceeded/LoggedOut/UserLocked. - Agendamentos: códigos INVALID_CRON e INVALID_TIME_ZONE (spec 007) e SchedulePreview público. - Migration DashboardKeys: tabela psr.dashboard_keys para as chaves do cookie do login fixo (D-132). Co-Authored-By: Claude Opus 5.5 --- .../Fields/FieldCompatibility.cs} | 15 +- .../Fields/FieldDefinition.cs | 2 + .../Fields}/FieldDefinitionJson.cs | 14 +- src/PythonScriptRunner.Abstractions/Ids.cs | 137 +- .../Management/Audit.cs | 9 + .../Management/CatalogAdmin.cs | 23 +- .../Management/DomainErrors.cs | 8 +- .../Messaging/Commands.cs | 16 - .../Pipelines/PipelineDefinition.cs | 2 + .../Running/Statuses.cs | 13 + .../Serialization/JsonConverters.cs | 83 + .../Catalog/CatalogManifests.cs | 1 - .../Execution/ProcessPythonExecutor.cs | 1 - .../Pipelines/PipelineJson.cs | 1 - .../Pipelines/PipelineRules.cs | 2 +- .../Catalog/EfCatalogAdmin.Pipelines.cs | 36 +- .../Catalog/EfCatalogAdmin.cs | 32 + .../Entities/InfrastructureEntities.cs | 13 + .../Infrastructure/AuditLog.cs | 10 + .../20261020170325_DashboardKeys.Designer.cs | 1380 +++++++++++++++++ .../20261020170325_DashboardKeys.cs | 40 + .../Migrations/PsrDbContextModelSnapshot.cs | 29 + .../PsrDbContext.cs | 9 + .../Scheduling/CronSchedule.cs | 26 +- .../Scheduling/SchedulePreview.cs | 28 + .../Unit/PipelineRulesTests.cs | 6 +- .../SchemaTests.cs | 2 +- .../SchedulerTests.cs | 4 +- 28 files changed, 1889 insertions(+), 53 deletions(-) rename src/{PythonScriptRunner.Core/Pipelines/TypeCompatibility.cs => PythonScriptRunner.Abstractions/Fields/FieldCompatibility.cs} (78%) rename src/{PythonScriptRunner.Core/Catalog => PythonScriptRunner.Abstractions/Fields}/FieldDefinitionJson.cs (82%) create mode 100644 src/PythonScriptRunner.Abstractions/Serialization/JsonConverters.cs create mode 100644 src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.Designer.cs create mode 100644 src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.cs create mode 100644 src/PythonScriptRunner.EntityFrameworkCore/Scheduling/SchedulePreview.cs diff --git a/src/PythonScriptRunner.Core/Pipelines/TypeCompatibility.cs b/src/PythonScriptRunner.Abstractions/Fields/FieldCompatibility.cs similarity index 78% rename from src/PythonScriptRunner.Core/Pipelines/TypeCompatibility.cs rename to src/PythonScriptRunner.Abstractions/Fields/FieldCompatibility.cs index bd8f471..64107b6 100644 --- a/src/PythonScriptRunner.Core/Pipelines/TypeCompatibility.cs +++ b/src/PythonScriptRunner.Abstractions/Fields/FieldCompatibility.cs @@ -1,10 +1,17 @@ -namespace PythonScriptRunner.Core.Pipelines; +namespace PythonScriptRunner; /// Which source types can feed which target types in a binding (spec 003, safe widenings only). -internal static class TypeCompatibility +public static class FieldCompatibility { - public static bool IsAssignable(FieldDefinition source, FieldDefinition target) => - IsAssignable(source.Type, target.Type, source, target); + /// Whether a value of can be bound to . + /// Field that produces the value. + /// Field that receives it. + public static bool IsAssignable(FieldDefinition source, FieldDefinition target) + { + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(target); + return IsAssignable(source.Type, target.Type, source, target); + } private static bool IsAssignable(FieldType source, FieldType target, FieldDefinition sourceField, FieldDefinition targetField) { diff --git a/src/PythonScriptRunner.Abstractions/Fields/FieldDefinition.cs b/src/PythonScriptRunner.Abstractions/Fields/FieldDefinition.cs index 97491fc..9b0f5a4 100644 --- a/src/PythonScriptRunner.Abstractions/Fields/FieldDefinition.cs +++ b/src/PythonScriptRunner.Abstractions/Fields/FieldDefinition.cs @@ -1,10 +1,12 @@ using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace PythonScriptRunner; /// Declaration of one input or output of a stage. /// Field name (^[a-z_][a-z0-9_]{0,63}$). /// Field type. +[JsonConverter(typeof(FieldDefinitionJsonConverter))] public sealed record FieldDefinition(string Name, FieldType Type) { /// Whether a value must be provided (inputs) or produced (outputs). Default: true. diff --git a/src/PythonScriptRunner.Core/Catalog/FieldDefinitionJson.cs b/src/PythonScriptRunner.Abstractions/Fields/FieldDefinitionJson.cs similarity index 82% rename from src/PythonScriptRunner.Core/Catalog/FieldDefinitionJson.cs rename to src/PythonScriptRunner.Abstractions/Fields/FieldDefinitionJson.cs index 0522732..a3cce86 100644 --- a/src/PythonScriptRunner.Core/Catalog/FieldDefinitionJson.cs +++ b/src/PythonScriptRunner.Abstractions/Fields/FieldDefinitionJson.cs @@ -1,12 +1,15 @@ using System.Text.Json.Nodes; -namespace PythonScriptRunner.Core.Catalog; +namespace PythonScriptRunner; -/// Maps to and from the manifest/protocol JSON (camelCase). -internal static class FieldDefinitionJson +/// Maps to and from the manifest/protocol JSON (camelCase; data mapping §2.1). +public static class FieldDefinitionJson { + /// Writes a field declaration. + /// The field. public static JsonObject ToJson(FieldDefinition field) { + ArgumentNullException.ThrowIfNull(field); var type = field.Type; var json = new JsonObject { @@ -47,11 +50,16 @@ public static JsonObject ToJson(FieldDefinition field) return json; } + /// Writes field declarations. + /// The fields. public static JsonArray ToJson(IEnumerable fields) => new([.. fields.Select(f => (JsonNode)ToJson(f))]); + /// Reads a field declaration. + /// The JSON object. /// The JSON is not a valid field declaration. public static FieldDefinition FromJson(JsonObject json) { + ArgumentNullException.ThrowIfNull(json); var name = json["name"]?.GetValue() ?? throw new ArgumentException("Field without 'name'."); var type = FieldType.Parse( json["type"]?.GetValue() ?? throw new ArgumentException($"Field '{name}' without 'type'."), diff --git a/src/PythonScriptRunner.Abstractions/Ids.cs b/src/PythonScriptRunner.Abstractions/Ids.cs index 8c53214..70b5386 100644 --- a/src/PythonScriptRunner.Abstractions/Ids.cs +++ b/src/PythonScriptRunner.Abstractions/Ids.cs @@ -4,8 +4,8 @@ namespace PythonScriptRunner; /// Identifier of a run (time-ordered UUID v7). Its timestamp is the creation time of the run. /// The underlying GUID. -[JsonConverter(typeof(RunIdJsonConverter))] -public readonly record struct RunId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct RunId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static RunId New() => new(Guid.CreateVersion7()); @@ -21,36 +21,74 @@ public readonly record struct RunId(Guid Value) /// Creation time embedded in the identifier (UTC, millisecond precision). public DateTimeOffset CreatedAt => UuidV7.Timestamp(Value); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out RunId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new RunId(value); + return parsed; + } + + static RunId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of one attempt of a step (time-ordered UUID v7). /// The underlying GUID. -public readonly record struct AttemptId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct AttemptId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static AttemptId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out AttemptId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new AttemptId(value); + return parsed; + } + + static AttemptId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a step within a run (time-ordered UUID v7). /// The underlying GUID. -[JsonConverter(typeof(StepRunIdJsonConverter))] -public readonly record struct StepRunId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct StepRunId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static StepRunId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out StepRunId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new StepRunId(value); + return parsed; + } + + static StepRunId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a stored file (time-ordered UUID v7). /// The underlying GUID. -public readonly record struct ArtifactId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct ArtifactId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static ArtifactId New() => new(Guid.CreateVersion7()); @@ -59,61 +97,138 @@ public readonly record struct ArtifactId(Guid Value) /// GUID text. public static ArtifactId Parse(string text) => new(Guid.Parse(text)); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out ArtifactId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new ArtifactId(value); + return parsed; + } + + static ArtifactId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a stage (UUID v7). /// The underlying GUID. -public readonly record struct StageId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct StageId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static StageId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out StageId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new StageId(value); + return parsed; + } + + static StageId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a stage version (UUID v7). /// The underlying GUID. -public readonly record struct StageVersionId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct StageVersionId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static StageVersionId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out StageVersionId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new StageVersionId(value); + return parsed; + } + + static StageVersionId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a pipeline (UUID v7). /// The underlying GUID. -public readonly record struct PipelineId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct PipelineId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static PipelineId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out PipelineId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new PipelineId(value); + return parsed; + } + + static PipelineId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a pipeline version (UUID v7). /// The underlying GUID. -public readonly record struct PipelineVersionId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct PipelineVersionId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static PipelineVersionId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out PipelineVersionId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new PipelineVersionId(value); + return parsed; + } + + static PipelineVersionId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } /// Identifier of a schedule (UUID v7). /// The underlying GUID. -public readonly record struct ScheduleId(Guid Value) +[JsonConverter(typeof(GuidIdJsonConverter))] +public readonly record struct ScheduleId(Guid Value) : IGuidId { /// Creates a new time-ordered identifier. public static ScheduleId New() => new(Guid.CreateVersion7()); + /// Parses the GUID text (route and query binding). + /// GUID text. + /// The identifier. + public static bool TryParse(string? text, out ScheduleId id) + { + var parsed = Guid.TryParse(text, out var value); + id = new ScheduleId(value); + return parsed; + } + + static ScheduleId IGuidId.From(Guid value) => new(value); + /// public override string ToString() => Value.ToString(); } diff --git a/src/PythonScriptRunner.Abstractions/Management/Audit.cs b/src/PythonScriptRunner.Abstractions/Management/Audit.cs index cc93a6d..b018870 100644 --- a/src/PythonScriptRunner.Abstractions/Management/Audit.cs +++ b/src/PythonScriptRunner.Abstractions/Management/Audit.cs @@ -57,6 +57,12 @@ public sealed record AuditQuery /// Only events of this entity type. public string? EntityType { get; init; } + /// Only events of this action (see ). + public string? Action { get; init; } + + /// Only events of this entity key (e.g. html-para-pdf@3). + public string? EntityKey { get; init; } + /// Cursor returned by the previous page. public string? Cursor { get; init; } @@ -96,5 +102,8 @@ public static class AuditActions public const string RunCancelled = nameof(RunCancelled); public const string RunReprocessed = nameof(RunReprocessed); public const string CatalogImported = nameof(CatalogImported); + public const string LoginSucceeded = nameof(LoginSucceeded); + public const string LoggedOut = nameof(LoggedOut); + public const string UserLocked = nameof(UserLocked); #pragma warning restore CS1591 } diff --git a/src/PythonScriptRunner.Abstractions/Management/CatalogAdmin.cs b/src/PythonScriptRunner.Abstractions/Management/CatalogAdmin.cs index a2a1839..43709e4 100644 --- a/src/PythonScriptRunner.Abstractions/Management/CatalogAdmin.cs +++ b/src/PythonScriptRunner.Abstractions/Management/CatalogAdmin.cs @@ -216,6 +216,26 @@ Task UpdatePipelineSettingsAsync( Task UpdatePipelineRetentionAsync( string key, PipelineRetention retention, uint expectedRowVersion, Actor actor, CancellationToken cancellationToken = default); + /// + /// Validates the current draft of a stage and queues it as a test () for the workers (requires the + /// RabbitMQ package; D-114). STAGE_INVALID when invalid; for invalid inputs. + /// + /// Stage key. + /// Stage inputs. + /// Author. + /// Cancellation token. + Task EnqueueStageDraftTestAsync(string key, object? inputs, Actor actor, CancellationToken cancellationToken = default); + + /// + /// Validates the current draft of a pipeline and queues it as a test (; D-97, D-114). + /// PIPELINE_INVALID when invalid; for invalid inputs. + /// + /// Pipeline key. + /// Pipeline inputs. + /// Author. + /// Cancellation token. + Task EnqueuePipelineDraftTestAsync(string key, object? inputs, Actor actor, CancellationToken cancellationToken = default); + /// Validates and runs the current draft as a test (); PIPELINE_INVALID when invalid. /// Pipeline key. /// Pipeline inputs. @@ -290,7 +310,8 @@ Task SetScheduleEnabledAsync( /// Text contained in the key or the name. /// 1-based page number. /// Items per page (1-100). -public sealed record CatalogQuery(string? Search = null, int PageNumber = 1, int PageSize = 25); +/// Only items that have a version with this status. +public sealed record CatalogQuery(string? Search = null, int PageNumber = 1, int PageSize = 25, VersionStatus? Status = null); /// A new stage or pipeline. /// Slug (immutable). diff --git a/src/PythonScriptRunner.Abstractions/Management/DomainErrors.cs b/src/PythonScriptRunner.Abstractions/Management/DomainErrors.cs index 2f1baa9..531084b 100644 --- a/src/PythonScriptRunner.Abstractions/Management/DomainErrors.cs +++ b/src/PythonScriptRunner.Abstractions/Management/DomainErrors.cs @@ -60,9 +60,15 @@ public static class PsrErrorCodes /// The schedule does not exist. public const string ScheduleNotFound = "SCHEDULE_NOT_FOUND"; - /// The schedule has an invalid cron expression, time zone or version. + /// The schedule targets a version that cannot run (missing, draft or archived). public const string ScheduleInvalid = "SCHEDULE_INVALID"; + /// The cron expression is invalid. + public const string InvalidCron = "INVALID_CRON"; + + /// The IANA time zone is unknown. + public const string InvalidTimeZone = "INVALID_TIME_ZONE"; + /// Pipelines with file inputs cannot be scheduled nor started by the public queue command (D-89). public const string ScheduleFileInput = "SCHEDULE_FILE_INPUT"; diff --git a/src/PythonScriptRunner.Abstractions/Messaging/Commands.cs b/src/PythonScriptRunner.Abstractions/Messaging/Commands.cs index d9961cf..5251e04 100644 --- a/src/PythonScriptRunner.Abstractions/Messaging/Commands.cs +++ b/src/PythonScriptRunner.Abstractions/Messaging/Commands.cs @@ -84,19 +84,3 @@ private static JsonSerializerOptions CreateOptions() return options; } } - -/// Writes as its GUID text. -internal sealed class RunIdJsonConverter : JsonConverter -{ - public override RunId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetGuid()); - - public override void Write(Utf8JsonWriter writer, RunId value, JsonSerializerOptions options) => writer.WriteStringValue(value.Value); -} - -/// Writes as its GUID text. -internal sealed class StepRunIdJsonConverter : JsonConverter -{ - public override StepRunId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetGuid()); - - public override void Write(Utf8JsonWriter writer, StepRunId value, JsonSerializerOptions options) => writer.WriteStringValue(value.Value); -} diff --git a/src/PythonScriptRunner.Abstractions/Pipelines/PipelineDefinition.cs b/src/PythonScriptRunner.Abstractions/Pipelines/PipelineDefinition.cs index 15489ff..208062e 100644 --- a/src/PythonScriptRunner.Abstractions/Pipelines/PipelineDefinition.cs +++ b/src/PythonScriptRunner.Abstractions/Pipelines/PipelineDefinition.cs @@ -1,4 +1,5 @@ using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace PythonScriptRunner; @@ -55,6 +56,7 @@ public sealed record PipelineOutputDefinition(string Name, StepOutputSource Sour } /// Origin of a bound value. +[JsonConverter(typeof(BindingSourceJsonConverter))] public abstract record BindingSource { private protected BindingSource() diff --git a/src/PythonScriptRunner.Abstractions/Running/Statuses.cs b/src/PythonScriptRunner.Abstractions/Running/Statuses.cs index 7aab167..1b80221 100644 --- a/src/PythonScriptRunner.Abstractions/Running/Statuses.cs +++ b/src/PythonScriptRunner.Abstractions/Running/Statuses.cs @@ -1,6 +1,9 @@ +using System.Text.Json.Serialization; + namespace PythonScriptRunner; /// What a run executes. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum RunKind { /// A published pipeline version. @@ -14,6 +17,7 @@ public enum RunKind } /// Status of a run. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum RunStatus { /// Waiting for a concurrency slot of the pipeline. @@ -33,6 +37,7 @@ public enum RunStatus } /// Status of a step within a run. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum StepStatus { /// Not started yet. @@ -54,6 +59,7 @@ public enum StepStatus } /// Final status of one attempt. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum AttemptStatus { /// Executing. @@ -71,6 +77,7 @@ public enum AttemptStatus } /// Why an attempt was started. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum AttemptCause { /// First attempt of the step. @@ -84,6 +91,7 @@ public enum AttemptCause } /// Who or what started a run. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum RunTrigger { /// Code calling the library. @@ -97,6 +105,7 @@ public enum RunTrigger } /// How a run is executed. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum ExecutionMode { /// In the calling process, waiting for the result. @@ -106,6 +115,7 @@ public enum ExecutionMode } /// Life cycle of a stage or pipeline version. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum VersionStatus { /// Editable; can be tested but not used by published pipelines. @@ -117,6 +127,7 @@ public enum VersionStatus } /// Whether a stored file was given to a run or produced by it. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum ArtifactDirection { /// Input of the run. @@ -126,6 +137,7 @@ public enum ArtifactDirection } /// Output stream of a Python process. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum LogStream { /// Standard output. @@ -135,6 +147,7 @@ public enum LogStream } /// Classification of a failure. +[JsonConverter(typeof(JsonStringEnumConverter))] public enum ErrorKind { /// Inputs do not match the contract. diff --git a/src/PythonScriptRunner.Abstractions/Serialization/JsonConverters.cs b/src/PythonScriptRunner.Abstractions/Serialization/JsonConverters.cs new file mode 100644 index 0000000..229bd8e --- /dev/null +++ b/src/PythonScriptRunner.Abstractions/Serialization/JsonConverters.cs @@ -0,0 +1,83 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace PythonScriptRunner; + +/// A strongly typed identifier backed by a GUID. +/// The identifier type. +internal interface IGuidId + where TSelf : struct, IGuidId +{ + Guid Value { get; } + + static abstract TSelf From(Guid value); +} + +/// Writes a strongly typed identifier as its GUID text. +/// The identifier type. +internal sealed class GuidIdJsonConverter : JsonConverter + where T : struct, IGuidId +{ + public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => T.From(reader.GetGuid()); + + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => writer.WriteStringValue(value.Value); + + public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + T.From(Guid.Parse(reader.GetString()!)); + + public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => + writer.WritePropertyName(value.Value.ToString()); +} + +/// Writes a in the manifest format (data mapping §2.1). +internal sealed class FieldDefinitionJsonConverter : JsonConverter +{ + public override FieldDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var node = JsonNode.Parse(ref reader) as JsonObject ?? throw new JsonException("A field declaration must be an object."); + try + { + return FieldDefinitionJson.FromJson(node); + } + catch (ArgumentException ex) + { + throw new JsonException(ex.Message, ex); + } + } + + public override void Write(Utf8JsonWriter writer, FieldDefinition value, JsonSerializerOptions options) => + FieldDefinitionJson.ToJson(value).WriteTo(writer); +} + +/// +/// Writes a as {"kind": "pipeline", "field"}, {"kind": "step", "step", "field"} or +/// {"kind": "constant", "value"} (data mapping §4.5). +/// +internal sealed class BindingSourceJsonConverter : JsonConverter +{ + public override BindingSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var node = JsonNode.Parse(ref reader) as JsonObject ?? throw new JsonException("A binding source must be an object."); + string Text(string name) => node[name]?.GetValue() ?? throw new JsonException($"The binding source needs '{name}'."); + return Text("kind") switch + { + "pipeline" => new PipelineInputSource(Text("field")), + "step" => new StepOutputSource(Text("step"), Text("field")), + "constant" => new ConstantSource(node["value"]?.DeepClone()), + var kind => throw new JsonException($"Unknown binding kind '{kind}'."), + }; + } + + public override void Write(Utf8JsonWriter writer, BindingSource value, JsonSerializerOptions options) + { + JsonObject node = value switch + { + PipelineInputSource input => new() { ["kind"] = "pipeline", ["field"] = input.Field }, + StepOutputSource output => new() { ["kind"] = "step", ["step"] = output.Step, ["field"] = output.Field }, + ConstantSource constant => new() { ["kind"] = "constant", ["value"] = constant.Value?.DeepClone() }, + _ => throw new JsonException($"Unknown binding source {value.GetType().Name}."), + }; + node.WriteTo(writer); + } +} diff --git a/src/PythonScriptRunner.Core/Catalog/CatalogManifests.cs b/src/PythonScriptRunner.Core/Catalog/CatalogManifests.cs index 73ac004..f653d03 100644 --- a/src/PythonScriptRunner.Core/Catalog/CatalogManifests.cs +++ b/src/PythonScriptRunner.Core/Catalog/CatalogManifests.cs @@ -2,7 +2,6 @@ using System.Text.Json; using System.Text.Json.Nodes; using Json.Schema; -using PythonScriptRunner.Core.Catalog; using PythonScriptRunner.Core.Pipelines; namespace PythonScriptRunner; diff --git a/src/PythonScriptRunner.Core/Execution/ProcessPythonExecutor.cs b/src/PythonScriptRunner.Core/Execution/ProcessPythonExecutor.cs index 4aec3be..de48e1c 100644 --- a/src/PythonScriptRunner.Core/Execution/ProcessPythonExecutor.cs +++ b/src/PythonScriptRunner.Core/Execution/ProcessPythonExecutor.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.Globalization; using System.Text.Json.Nodes; -using PythonScriptRunner.Core.Catalog; using PythonScriptRunner.Core.Values; namespace PythonScriptRunner.Core.Execution; diff --git a/src/PythonScriptRunner.Core/Pipelines/PipelineJson.cs b/src/PythonScriptRunner.Core/Pipelines/PipelineJson.cs index 06431c3..4c92485 100644 --- a/src/PythonScriptRunner.Core/Pipelines/PipelineJson.cs +++ b/src/PythonScriptRunner.Core/Pipelines/PipelineJson.cs @@ -1,5 +1,4 @@ using System.Text.Json.Nodes; -using PythonScriptRunner.Core.Catalog; namespace PythonScriptRunner.Core.Pipelines; diff --git a/src/PythonScriptRunner.Core/Pipelines/PipelineRules.cs b/src/PythonScriptRunner.Core/Pipelines/PipelineRules.cs index c5d1bfd..5b05731 100644 --- a/src/PythonScriptRunner.Core/Pipelines/PipelineRules.cs +++ b/src/PythonScriptRunner.Core/Pipelines/PipelineRules.cs @@ -239,7 +239,7 @@ private static void ValidateStep( private static ValidationIssue? CheckTypes(FieldDefinition source, FieldDefinition target, string description, bool sourceMayBeAbsent) { - if (!TypeCompatibility.IsAssignable(source, target)) + if (!FieldCompatibility.IsAssignable(source, target)) { return new("PIPE_TYPE_MISMATCH", $"{description} is {source.Type} and cannot feed {target.Type}"); } diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.Pipelines.cs b/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.Pipelines.cs index e14ea6c..713ee41 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.Pipelines.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.Pipelines.cs @@ -20,6 +20,11 @@ public async Task> ListPipelinesAsync(CatalogQuery query, pipelines = pipelines.Where(p => EF.Functions.ILike(p.Key, pattern) || EF.Functions.ILike(p.Name, pattern)); } + if (query.Status is { } status) + { + pipelines = pipelines.Where(p => p.Versions.Any(v => v.Status == status)); + } + var total = await pipelines.CountAsync(cancellationToken).ConfigureAwait(false); var items = await pipelines.OrderBy(p => p.Key) .Skip((pageNumber - 1) * pageSize).Take(pageSize) @@ -370,6 +375,33 @@ public async Task UpdatePipelineRetentionAsync( public async Task TestPipelineDraftAsync(string key, object? inputs, Actor actor, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(actor); + var pipeline = await PreparePipelineTestAsync(key, actor, cancellationToken).ConfigureAwait(false); + return await engine.RunPipelineAsync(pipeline, RunKind.PipelineTest, inputs, RunOptionsFor(actor), cancellationToken).ConfigureAwait(false); + } + + public async Task EnqueuePipelineDraftTestAsync(string key, object? inputs, Actor actor, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(actor); + var pipeline = await LoadValidPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false); + var bound = engine.BindInputs(pipeline.Definition.Inputs, inputs); + await WriteAuditAsync(new AuditEntry(actor, AuditActions.PipelineTested, "PipelineVersion", pipeline.VersionId, $"{key}@{pipeline.Definition.Version}"), + cancellationToken).ConfigureAwait(false); + return await lifecycle.CreateAsync(QueuedTest(RunKind.PipelineTest, actor, bound) with { Pipeline = pipeline.Definition }, cancellationToken) + .ConfigureAwait(false); + } + + /// Loads and validates the draft, then audits the test. + private async Task PreparePipelineTestAsync(string key, Actor actor, CancellationToken cancellationToken) + { + var pipeline = await LoadValidPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false); + await WriteAuditAsync(new AuditEntry(actor, AuditActions.PipelineTested, "PipelineVersion", pipeline.VersionId, $"{key}@{pipeline.Definition.Version}"), + cancellationToken).ConfigureAwait(false); + return pipeline.Definition; + } + + /// The current draft of a pipeline; PIPELINE_INVALID when it does not validate. + private async Task<(PipelineDefinition Definition, Guid VersionId)> LoadValidPipelineDraftAsync(string key, CancellationToken cancellationToken) + { PipelineDefinition pipeline; Guid versionId; var db = await factory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); @@ -390,9 +422,7 @@ public async Task TestPipelineDraftAsync(string key, object? inputs, throw new DefinitionInvalidException(PsrErrorCodes.PipelineInvalid, report); } - await WriteAuditAsync(new AuditEntry(actor, AuditActions.PipelineTested, "PipelineVersion", versionId, $"{key}@{pipeline.Version}"), cancellationToken) - .ConfigureAwait(false); - return await engine.RunPipelineAsync(pipeline, RunKind.PipelineTest, inputs, RunOptionsFor(actor), cancellationToken).ConfigureAwait(false); + return (pipeline, versionId); } private async Task ChangePipelineAsync( diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.cs b/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.cs index d6b83c2..4d82324 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Catalog/EfCatalogAdmin.cs @@ -16,6 +16,7 @@ internal sealed partial class EfCatalogAdmin( IPipelineValidator pipelineValidator, IEnvironmentManager environments, IRunEngine engine, + AsyncRunLifecycle lifecycle, IOptions runnerOptions, PsrEntityFrameworkOptions options, TimeProvider time) : ICatalogAdmin @@ -47,6 +48,11 @@ public async Task> ListStagesAsync(CatalogQuery query, Cancel stages = stages.Where(s => EF.Functions.ILike(s.Key, pattern) || EF.Functions.ILike(s.Name, pattern)); } + if (query.Status is { } status) + { + stages = stages.Where(s => s.Versions.Any(v => v.Status == status)); + } + var total = await stages.CountAsync(cancellationToken).ConfigureAwait(false); var items = await stages.OrderBy(s => s.Key) .Skip((pageNumber - 1) * pageSize).Take(pageSize) @@ -409,6 +415,32 @@ await WriteAuditAsync(new AuditEntry(actor, AuditActions.StageTested, "StageVers return await engine.RunStageAsync(stage, RunKind.StageTest, inputs, RunOptionsFor(actor), cancellationToken).ConfigureAwait(false); } + public async Task EnqueueStageDraftTestAsync(string key, object? inputs, Actor actor, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(actor); + var (stage, versionId) = await LoadStageDraftDefinitionAsync(key, cancellationToken).ConfigureAwait(false); + var report = stageValidator.Validate(stage); + if (!report.IsValid) + { + throw new DefinitionInvalidException(PsrErrorCodes.StageInvalid, report); + } + + var bound = engine.BindInputs(stage.Inputs, inputs); + await WriteAuditAsync(new AuditEntry(actor, AuditActions.StageTested, "StageVersion", versionId, $"{key}@{stage.Version}"), cancellationToken) + .ConfigureAwait(false); + return await lifecycle.CreateAsync(QueuedTest(RunKind.StageTest, actor, bound) with { Stage = stage }, cancellationToken).ConfigureAwait(false); + } + + /// A draft test queued for the workers (D-114). + private static NewAsyncRun QueuedTest(RunKind kind, Actor actor, BoundRunInputs inputs) => new() + { + Kind = kind, + Inputs = inputs, + Trigger = actor.IsHuman ? RunTrigger.Dashboard : RunTrigger.Library, + TriggeredBy = actor.Subject, + TraceParent = System.Diagnostics.Activity.Current?.Id, + }; + private async Task<(StageDefinition Stage, Guid VersionId)> LoadStageDraftDefinitionAsync(string key, CancellationToken cancellationToken) { var db = await factory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Entities/InfrastructureEntities.cs b/src/PythonScriptRunner.EntityFrameworkCore/Entities/InfrastructureEntities.cs index 54cfcf9..d411b50 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Entities/InfrastructureEntities.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Entities/InfrastructureEntities.cs @@ -65,3 +65,16 @@ public sealed class AuditEventEntity public string? TraceId { get; set; } } + +/// Row of psr.dashboard_keys: Data Protection keys of the dashboard session cookie (D-132). +public sealed class DashboardKeyEntity +{ + public int Id { get; set; } + + public string? FriendlyName { get; set; } + + /// Key element as XML. + public required string Xml { get; set; } + + public DateTimeOffset CreatedAt { get; set; } +} diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Infrastructure/AuditLog.cs b/src/PythonScriptRunner.EntityFrameworkCore/Infrastructure/AuditLog.cs index 63c0472..3c6f239 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Infrastructure/AuditLog.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Infrastructure/AuditLog.cs @@ -73,6 +73,16 @@ public async Task> ListAsync(AuditQuery query, Cancellati events = events.Where(e => e.EntityType == entityType); } + if (query.Action is { } action) + { + events = events.Where(e => e.Action == action); + } + + if (query.EntityKey is { } entityKey) + { + events = events.Where(e => e.EntityKey == entityKey); + } + if (KeysetCursor.Decode(query.Cursor) is { } cursor) { events = events.Where(e => e.OccurredAt < cursor.At || (e.OccurredAt == cursor.At && e.Id < cursor.Id)); diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.Designer.cs b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.Designer.cs new file mode 100644 index 0000000..a798655 --- /dev/null +++ b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.Designer.cs @@ -0,0 +1,1380 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PythonScriptRunner.EntityFrameworkCore; + +#nullable disable + +namespace PythonScriptRunner.EntityFrameworkCore.Migrations +{ + [DbContext(typeof(PsrDbContext))] + [Migration("20261020170325_DashboardKeys")] + partial class DashboardKeys + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("psr") + .HasAnnotation("ProductVersion", "10.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.ArtifactContentEntity", b => + { + b.Property("ArtifactId") + .HasColumnType("uuid") + .HasColumnName("artifact_id"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("Content") + .IsRequired() + .HasColumnType("bytea") + .HasColumnName("content"); + + b.HasKey("ArtifactId", "CreatedAt") + .HasName("pk_artifact_contents"); + + b.ToTable("artifact_contents", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.ArtifactEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("RunCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("run_created_at"); + + b.Property("ContentCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("content_created_at"); + + b.Property("ContentDeletedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("content_deleted_at"); + + b.Property("ContentExpiresAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("content_expires_at"); + + b.Property("ContentType") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("character varying(255)") + .HasColumnName("content_type"); + + b.Property("Direction") + .IsRequired() + .HasMaxLength(8) + .HasColumnType("character varying(8)") + .HasColumnName("direction"); + + b.Property("FieldName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("field_name"); + + b.Property("FileName") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("character varying(255)") + .HasColumnName("file_name"); + + b.Property("ListIndex") + .HasColumnType("integer") + .HasColumnName("list_index"); + + b.Property("RunId") + .HasColumnType("uuid") + .HasColumnName("run_id"); + + b.Property("Sha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character(64)") + .HasColumnName("sha256") + .IsFixedLength(); + + b.Property("SizeBytes") + .HasColumnType("bigint") + .HasColumnName("size_bytes"); + + b.Property("StepRunId") + .HasColumnType("uuid") + .HasColumnName("step_run_id"); + + b.HasKey("Id", "RunCreatedAt") + .HasName("pk_artifacts"); + + b.HasIndex("ContentExpiresAt") + .HasDatabaseName("ix_artifacts_content_expires_at") + .HasFilter("content_deleted_at IS NULL"); + + b.HasIndex("RunId", "RunCreatedAt") + .HasDatabaseName("ix_artifacts_run_id_run_created_at"); + + b.HasIndex("StepRunId", "RunCreatedAt") + .HasDatabaseName("ix_artifacts_step_run_id_run_created_at"); + + b.ToTable("artifacts", "psr", t => + { + t.HasCheckConstraint("ck_artifacts_direction", "direction IN ('Input', 'Output')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.AttemptLogEntity", b => + { + b.Property("AttemptId") + .HasColumnType("uuid") + .HasColumnName("attempt_id"); + + b.Property("RunCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("run_created_at"); + + b.Property("Stream") + .HasMaxLength(8) + .HasColumnType("character varying(8)") + .HasColumnName("stream"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text") + .HasColumnName("content"); + + b.Property("OriginalBytes") + .HasColumnType("bigint") + .HasColumnName("original_bytes"); + + b.Property("Truncated") + .HasColumnType("boolean") + .HasColumnName("truncated"); + + b.HasKey("AttemptId", "RunCreatedAt", "Stream") + .HasName("pk_attempt_logs"); + + b.ToTable("attempt_logs", "psr", t => + { + t.HasCheckConstraint("ck_attempt_logs_stream", "stream IN ('stdout', 'stderr')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.AuditEventEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("OccurredAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("occurred_at"); + + b.Property("Action") + .IsRequired() + .HasMaxLength(60) + .HasColumnType("character varying(60)") + .HasColumnName("action"); + + b.Property("Actor") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("actor"); + + b.Property("Details") + .HasColumnType("jsonb") + .HasColumnName("details"); + + b.Property("EntityId") + .HasColumnType("uuid") + .HasColumnName("entity_id"); + + b.Property("EntityKey") + .HasMaxLength(100) + .HasColumnType("character varying(100)") + .HasColumnName("entity_key"); + + b.Property("EntityType") + .IsRequired() + .HasMaxLength(30) + .HasColumnType("character varying(30)") + .HasColumnName("entity_type"); + + b.Property("TraceId") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnName("trace_id"); + + b.HasKey("Id", "OccurredAt") + .HasName("pk_audit_events"); + + b.HasIndex("OccurredAt", "Id") + .IsDescending() + .HasDatabaseName("ix_audit_events_occurred_at_id"); + + b.ToTable("audit_events", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.DashboardKeyEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityAlwaysColumn(b.Property("Id")); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("FriendlyName") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("friendly_name"); + + b.Property("Xml") + .IsRequired() + .HasColumnType("text") + .HasColumnName("xml"); + + b.HasKey("Id") + .HasName("pk_dashboard_keys"); + + b.ToTable("dashboard_keys", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.IdempotencyKeyEntity", b => + { + b.Property("Scope") + .HasMaxLength(80) + .HasColumnType("character varying(80)") + .HasColumnName("scope"); + + b.Property("Key") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("key"); + + b.Property("ExpiresAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("expires_at"); + + b.Property("RunCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("run_created_at"); + + b.Property("RunId") + .HasColumnType("uuid") + .HasColumnName("run_id"); + + b.HasKey("Scope", "Key") + .HasName("pk_idempotency_keys"); + + b.HasIndex("ExpiresAt") + .HasDatabaseName("ix_idempotency_keys_expires_at"); + + b.ToTable("idempotency_keys", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.OutboxMessageEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("Attempts") + .HasColumnType("integer") + .HasColumnName("attempts"); + + b.Property("AvailableAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("available_at"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("Exchange") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("exchange"); + + b.Property("Headers") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("headers"); + + b.Property("LastError") + .HasColumnType("text") + .HasColumnName("last_error"); + + b.Property("MessageType") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)") + .HasColumnName("message_type"); + + b.Property("Payload") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("payload"); + + b.Property("PublishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("published_at"); + + b.Property("RoutingKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("routing_key"); + + b.HasKey("Id") + .HasName("pk_outbox_messages"); + + b.HasIndex("AvailableAt") + .HasDatabaseName("ix_outbox_messages_available_at") + .HasFilter("published_at IS NULL"); + + b.ToTable("outbox_messages", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("ArtifactRetentionDays") + .HasColumnType("integer") + .HasColumnName("artifact_retention_days"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("created_by"); + + b.Property("Description") + .HasColumnType("text") + .HasColumnName("description"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("key"); + + b.Property("LatestPublishedVersion") + .HasColumnType("integer") + .HasColumnName("latest_published_version"); + + b.Property("MaxConcurrentRuns") + .HasColumnType("integer") + .HasColumnName("max_concurrent_runs"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("name"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("xid") + .HasColumnName("xmin"); + + b.Property("RunRetentionDays") + .HasColumnType("integer") + .HasColumnName("run_retention_days"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("UpdatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("updated_by"); + + b.HasKey("Id") + .HasName("pk_pipelines"); + + b.HasIndex("Key") + .IsUnique() + .HasDatabaseName("ux_pipelines_key"); + + b.ToTable("pipelines", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineStepEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("Bindings") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("bindings"); + + b.Property("DisplayName") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("display_name"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("key"); + + b.Property("Order") + .HasColumnType("integer") + .HasColumnName("order"); + + b.Property("PipelineVersionId") + .HasColumnType("uuid") + .HasColumnName("pipeline_version_id"); + + b.Property("StageVersionId") + .HasColumnType("uuid") + .HasColumnName("stage_version_id"); + + b.HasKey("Id") + .HasName("pk_pipeline_steps"); + + b.HasIndex("StageVersionId") + .HasDatabaseName("ix_pipeline_steps_stage_version_id"); + + b.HasIndex("PipelineVersionId", "Key") + .IsUnique() + .HasDatabaseName("ux_pipeline_steps_pipeline_version_id_key"); + + b.HasIndex("PipelineVersionId", "Order") + .IsUnique() + .HasDatabaseName("ux_pipeline_steps_pipeline_version_id_order"); + + b.ToTable("pipeline_steps", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineVersionEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("ArchivedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("archived_at"); + + b.Property("ArchivedBy") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("archived_by"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("created_by"); + + b.Property("InputFields") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("input_fields"); + + b.Property("Layout") + .HasColumnType("jsonb") + .HasColumnName("layout"); + + b.Property("Outputs") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("outputs"); + + b.Property("PipelineId") + .HasColumnType("uuid") + .HasColumnName("pipeline_id"); + + b.Property("PublishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("published_at"); + + b.Property("PublishedBy") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("published_by"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("xid") + .HasColumnName("xmin"); + + b.Property("Status") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasDefaultValue("Draft") + .HasColumnName("status"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("UpdatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("updated_by"); + + b.Property("Version") + .HasColumnType("integer") + .HasColumnName("version"); + + b.HasKey("Id") + .HasName("pk_pipeline_versions"); + + b.HasIndex("PipelineId") + .IsUnique() + .HasDatabaseName("ux_pipeline_versions_draft") + .HasFilter("status = 'Draft'"); + + b.HasIndex("PipelineId", "Version") + .IsUnique() + .HasDatabaseName("ux_pipeline_versions_pipeline_id_version"); + + b.ToTable("pipeline_versions", "psr", t => + { + t.HasCheckConstraint("ck_pipeline_versions_status", "status IN ('Draft', 'Published', 'Archived')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.RunEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CancelRequestedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("cancel_requested_at"); + + b.Property("CancelRequestedBy") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("cancel_requested_by"); + + b.Property("CorrelationId") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("correlation_id"); + + b.Property("Error") + .HasColumnType("jsonb") + .HasColumnName("error"); + + b.Property("ExpiresAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("expires_at"); + + b.Property("FinishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("finished_at"); + + b.Property("IdempotencyKey") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("idempotency_key"); + + b.Property("Inputs") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("inputs"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("kind"); + + b.Property("Mode") + .IsRequired() + .HasMaxLength(8) + .HasColumnType("character varying(8)") + .HasColumnName("mode"); + + b.Property("Outputs") + .HasColumnType("jsonb") + .HasColumnName("outputs"); + + b.Property("PipelineId") + .HasColumnType("uuid") + .HasColumnName("pipeline_id"); + + b.Property("PipelineKey") + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("pipeline_key"); + + b.Property("PipelineVersion") + .HasColumnType("integer") + .HasColumnName("pipeline_version"); + + b.Property("PipelineVersionId") + .HasColumnType("uuid") + .HasColumnName("pipeline_version_id"); + + b.Property("QueuedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("queued_at"); + + b.Property("ScheduleId") + .HasColumnType("uuid") + .HasColumnName("schedule_id"); + + b.Property("StageKey") + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("stage_key"); + + b.Property("StageVersion") + .HasColumnType("integer") + .HasColumnName("stage_version"); + + b.Property("StageVersionId") + .HasColumnType("uuid") + .HasColumnName("stage_version_id"); + + b.Property("StartedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("started_at"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("status"); + + b.Property("TraceParent") + .HasMaxLength(55) + .HasColumnType("character varying(55)") + .HasColumnName("trace_parent"); + + b.Property("Trigger") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("trigger"); + + b.Property("TriggerDetails") + .HasColumnType("jsonb") + .HasColumnName("trigger_details"); + + b.Property("TriggeredBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("triggered_by"); + + b.HasKey("Id", "CreatedAt") + .HasName("pk_runs"); + + b.HasIndex("ExpiresAt") + .HasDatabaseName("ix_runs_expires_at") + .HasFilter("expires_at IS NOT NULL"); + + b.HasIndex("CreatedAt", "Id") + .IsDescending() + .HasDatabaseName("ix_runs_created_at_id"); + + b.HasIndex("Status", "CreatedAt") + .HasDatabaseName("ix_runs_status_created_at") + .HasFilter("status IN ('Pending', 'Queued', 'Running', 'Cancelling')"); + + b.HasIndex("PipelineId", "CreatedAt", "Id") + .IsDescending(false, true, true) + .HasDatabaseName("ix_runs_pipeline_id_created_at_id"); + + b.HasIndex("StageKey", "CreatedAt", "Id") + .IsDescending(false, true, true) + .HasDatabaseName("ix_runs_stage_key_created_at_id") + .HasFilter("stage_key IS NOT NULL"); + + b.ToTable("runs", "psr", t => + { + t.HasCheckConstraint("ck_runs_kind", "kind IN ('Pipeline', 'PipelineTest', 'Stage', 'StageTest')"); + + t.HasCheckConstraint("ck_runs_mode", "mode IN ('Sync', 'Async')"); + + t.HasCheckConstraint("ck_runs_status", "status IN ('Pending', 'Queued', 'Running', 'Succeeded', 'Failed', 'Cancelling', 'Cancelled')"); + + t.HasCheckConstraint("ck_runs_trigger", "trigger IN ('Library', 'Dashboard', 'Schedule', 'QueueCommand')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.ScheduleEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("created_by"); + + b.Property("Cron") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)") + .HasColumnName("cron"); + + b.Property("Inputs") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("jsonb") + .HasColumnName("inputs") + .HasDefaultValueSql("'{}'::jsonb"); + + b.Property("IsEnabled") + .ValueGeneratedOnAdd() + .HasColumnType("boolean") + .HasDefaultValue(true) + .HasColumnName("is_enabled"); + + b.Property("LastError") + .HasColumnType("text") + .HasColumnName("last_error"); + + b.Property("LastFiredAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("last_fired_at"); + + b.Property("LastRunId") + .HasColumnType("uuid") + .HasColumnName("last_run_id"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("name"); + + b.Property("NextRunAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("next_run_at"); + + b.Property("PinnedVersion") + .HasColumnType("integer") + .HasColumnName("pinned_version"); + + b.Property("PipelineId") + .HasColumnType("uuid") + .HasColumnName("pipeline_id"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("xid") + .HasColumnName("xmin"); + + b.Property("TimeZone") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasDefaultValue("America/Sao_Paulo") + .HasColumnName("time_zone"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("UpdatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("updated_by"); + + b.HasKey("Id") + .HasName("pk_schedules"); + + b.HasIndex("NextRunAt") + .HasDatabaseName("ix_schedules_next_run_at") + .HasFilter("is_enabled"); + + b.HasIndex("PipelineId") + .HasDatabaseName("ix_schedules_pipeline_id"); + + b.ToTable("schedules", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StageEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("created_by"); + + b.Property("Description") + .HasColumnType("text") + .HasColumnName("description"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("key"); + + b.Property("LatestPublishedVersion") + .HasColumnType("integer") + .HasColumnName("latest_published_version"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("name"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("xid") + .HasColumnName("xmin"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("UpdatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("updated_by"); + + b.HasKey("Id") + .HasName("pk_stages"); + + b.HasIndex("Key") + .IsUnique() + .HasDatabaseName("ux_stages_key"); + + b.ToTable("stages", "psr"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StageVersionEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("ArchivedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("archived_at"); + + b.Property("ArchivedBy") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("archived_by"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("created_by"); + + b.Property("EnvironmentHash") + .HasMaxLength(64) + .HasColumnType("character(64)") + .HasColumnName("environment_hash") + .IsFixedLength(); + + b.Property("Idempotent") + .HasColumnType("boolean") + .HasColumnName("idempotent"); + + b.Property("InputFields") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("input_fields"); + + b.Property("LockedRequirements") + .HasColumnType("text") + .HasColumnName("locked_requirements"); + + b.Property("MemoryLimitMb") + .HasColumnType("integer") + .HasColumnName("memory_limit_mb"); + + b.Property("OutputFields") + .IsRequired() + .HasColumnType("jsonb") + .HasColumnName("output_fields"); + + b.Property("PublishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("published_at"); + + b.Property("PublishedBy") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("published_by"); + + b.Property("PythonVersion") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("character varying(10)") + .HasColumnName("python_version"); + + b.PrimitiveCollection("RequiredSecrets") + .IsRequired() + .HasColumnType("text[]") + .HasColumnName("required_secrets"); + + b.Property("Requirements") + .HasColumnType("text") + .HasColumnName("requirements"); + + b.Property("RetryBackoffMultiplier") + .HasPrecision(4, 2) + .HasColumnType("numeric(4,2)") + .HasColumnName("retry_backoff_multiplier"); + + b.Property("RetryInitialDelaySeconds") + .HasColumnType("integer") + .HasColumnName("retry_initial_delay_seconds"); + + b.Property("RetryMaxAttempts") + .HasColumnType("integer") + .HasColumnName("retry_max_attempts"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("xid") + .HasColumnName("xmin"); + + b.Property("Script") + .IsRequired() + .HasColumnType("text") + .HasColumnName("script"); + + b.Property("ScriptSha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character(64)") + .HasColumnName("script_sha256") + .IsFixedLength(); + + b.Property("StageId") + .HasColumnType("uuid") + .HasColumnName("stage_id"); + + b.Property("Status") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasDefaultValue("Draft") + .HasColumnName("status"); + + b.Property("TimeoutSeconds") + .HasColumnType("integer") + .HasColumnName("timeout_seconds"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("UpdatedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("updated_by"); + + b.Property("Version") + .HasColumnType("integer") + .HasColumnName("version"); + + b.HasKey("Id") + .HasName("pk_stage_versions"); + + b.HasIndex("StageId") + .IsUnique() + .HasDatabaseName("ux_stage_versions_draft") + .HasFilter("status = 'Draft'"); + + b.HasIndex("StageId", "Version") + .IsUnique() + .HasDatabaseName("ux_stage_versions_stage_id_version"); + + b.ToTable("stage_versions", "psr", t => + { + t.HasCheckConstraint("ck_stage_versions_status", "status IN ('Draft', 'Published', 'Archived')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StepAttemptEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("RunCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("run_created_at"); + + b.Property("Attempt") + .HasColumnType("integer") + .HasColumnName("attempt"); + + b.Property("Cause") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("cause"); + + b.Property("DurationMs") + .HasColumnType("integer") + .HasColumnName("duration_ms"); + + b.Property("EnvironmentHash") + .HasMaxLength(64) + .HasColumnType("character(64)") + .HasColumnName("environment_hash") + .IsFixedLength(); + + b.Property("Error") + .HasColumnType("jsonb") + .HasColumnName("error"); + + b.Property("ErrorKind") + .HasMaxLength(40) + .HasColumnType("character varying(40)") + .HasColumnName("error_kind"); + + b.Property("ExitCode") + .HasColumnType("integer") + .HasColumnName("exit_code"); + + b.Property("FinishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("finished_at"); + + b.Property("LeaseExpiresAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("lease_expires_at"); + + b.Property("OverheadMs") + .HasColumnType("integer") + .HasColumnName("overhead_ms"); + + b.Property("RunId") + .HasColumnType("uuid") + .HasColumnName("run_id"); + + b.Property("StartedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("started_at"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("status"); + + b.Property("StepRunId") + .HasColumnType("uuid") + .HasColumnName("step_run_id"); + + b.Property("TriggeredBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("triggered_by"); + + b.Property("WorkerId") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("worker_id"); + + b.HasKey("Id", "RunCreatedAt") + .HasName("pk_step_attempts"); + + b.HasIndex("LeaseExpiresAt") + .HasDatabaseName("ix_step_attempts_lease_expires_at") + .HasFilter("status = 'Running'"); + + b.HasIndex("StepRunId", "RunCreatedAt") + .HasDatabaseName("ix_step_attempts_step_run_id_run_created_at"); + + b.ToTable("step_attempts", "psr", t => + { + t.HasCheckConstraint("ck_step_attempts_cause", "cause IN ('Initial', 'AutoRetry', 'Reprocess', 'LeaseRecovery')"); + + t.HasCheckConstraint("ck_step_attempts_status", "status IN ('Running', 'Succeeded', 'Failed', 'TimedOut', 'Cancelled', 'WorkerLost')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StepRunEntity", b => + { + b.Property("Id") + .HasColumnType("uuid") + .HasColumnName("id"); + + b.Property("RunCreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("run_created_at"); + + b.Property("CurrentAttempt") + .HasColumnType("integer") + .HasColumnName("current_attempt"); + + b.Property("Error") + .HasColumnType("jsonb") + .HasColumnName("error"); + + b.Property("FinishedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("finished_at"); + + b.Property("Inputs") + .HasColumnType("jsonb") + .HasColumnName("inputs"); + + b.Property("Order") + .HasColumnType("integer") + .HasColumnName("order"); + + b.Property("Outputs") + .HasColumnType("jsonb") + .HasColumnName("outputs"); + + b.Property("PipelineStepId") + .HasColumnType("uuid") + .HasColumnName("pipeline_step_id"); + + b.Property("RunId") + .HasColumnType("uuid") + .HasColumnName("run_id"); + + b.Property("StageKey") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("stage_key"); + + b.Property("StageVersion") + .HasColumnType("integer") + .HasColumnName("stage_version"); + + b.Property("StageVersionId") + .HasColumnType("uuid") + .HasColumnName("stage_version_id"); + + b.Property("StartedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("started_at"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnName("status"); + + b.Property("StepKey") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnName("step_key"); + + b.HasKey("Id", "RunCreatedAt") + .HasName("pk_step_runs"); + + b.HasIndex("RunId", "RunCreatedAt", "Order") + .IsUnique() + .HasDatabaseName("ux_step_runs_run_id_run_created_at_order"); + + b.ToTable("step_runs", "psr", t => + { + t.HasCheckConstraint("ck_step_runs_status", "status IN ('Pending', 'Queued', 'Running', 'Succeeded', 'Failed', 'TimedOut', 'Cancelled', 'Skipped')"); + }); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.ArtifactEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.RunEntity", null) + .WithMany() + .HasForeignKey("RunId", "RunCreatedAt") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_artifacts_runs_run_id_run_created_at"); + + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.StepRunEntity", null) + .WithMany() + .HasForeignKey("StepRunId", "RunCreatedAt") + .OnDelete(DeleteBehavior.Cascade) + .HasConstraintName("fk_artifacts_step_runs_step_run_id_run_created_at"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.AttemptLogEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.StepAttemptEntity", null) + .WithMany() + .HasForeignKey("AttemptId", "RunCreatedAt") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_attempt_logs_step_attempts_attempt_id_run_created_at"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineStepEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineVersionEntity", null) + .WithMany("Steps") + .HasForeignKey("PipelineVersionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_pipeline_steps_pipeline_versions_pipeline_version_id"); + + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.StageVersionEntity", "StageVersion") + .WithMany() + .HasForeignKey("StageVersionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired() + .HasConstraintName("fk_pipeline_steps_stage_versions_stage_version_id"); + + b.Navigation("StageVersion"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineVersionEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineEntity", "Pipeline") + .WithMany("Versions") + .HasForeignKey("PipelineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_pipeline_versions_pipelines_pipeline_id"); + + b.Navigation("Pipeline"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.ScheduleEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineEntity", null) + .WithMany() + .HasForeignKey("PipelineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_schedules_pipelines_pipeline_id"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StageVersionEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.StageEntity", "Stage") + .WithMany("Versions") + .HasForeignKey("StageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_stage_versions_stages_stage_id"); + + b.Navigation("Stage"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StepAttemptEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.StepRunEntity", null) + .WithMany("Attempts") + .HasForeignKey("StepRunId", "RunCreatedAt") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_step_attempts_step_runs_step_run_id_run_created_at"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StepRunEntity", b => + { + b.HasOne("PythonScriptRunner.EntityFrameworkCore.Entities.RunEntity", null) + .WithMany("Steps") + .HasForeignKey("RunId", "RunCreatedAt") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_step_runs_runs_run_id_run_created_at"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineEntity", b => + { + b.Navigation("Versions"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.PipelineVersionEntity", b => + { + b.Navigation("Steps"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.RunEntity", b => + { + b.Navigation("Steps"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StageEntity", b => + { + b.Navigation("Versions"); + }); + + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.StepRunEntity", b => + { + b.Navigation("Attempts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.cs b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.cs new file mode 100644 index 0000000..d51703c --- /dev/null +++ b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/20261020170325_DashboardKeys.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace PythonScriptRunner.EntityFrameworkCore.Migrations +{ + /// + public partial class DashboardKeys : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "dashboard_keys", + schema: "psr", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn), + friendly_name = table.Column(type: "character varying(200)", maxLength: 200, nullable: true), + xml = table.Column(type: "text", nullable: false), + created_at = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("pk_dashboard_keys", x => x.id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "dashboard_keys", + schema: "psr"); + } + } +} diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Migrations/PsrDbContextModelSnapshot.cs b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/PsrDbContextModelSnapshot.cs index d03adf4..8dd5ff2 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Migrations/PsrDbContextModelSnapshot.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Migrations/PsrDbContextModelSnapshot.cs @@ -225,6 +225,35 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("audit_events", "psr"); }); + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.DashboardKeyEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityAlwaysColumn(b.Property("Id")); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("FriendlyName") + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("friendly_name"); + + b.Property("Xml") + .IsRequired() + .HasColumnType("text") + .HasColumnName("xml"); + + b.HasKey("Id") + .HasName("pk_dashboard_keys"); + + b.ToTable("dashboard_keys", "psr"); + }); + modelBuilder.Entity("PythonScriptRunner.EntityFrameworkCore.Entities.IdempotencyKeyEntity", b => { b.Property("Scope") diff --git a/src/PythonScriptRunner.EntityFrameworkCore/PsrDbContext.cs b/src/PythonScriptRunner.EntityFrameworkCore/PsrDbContext.cs index 3611cd2..ee2e9b1 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/PsrDbContext.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/PsrDbContext.cs @@ -49,6 +49,8 @@ public sealed class PsrDbContext(DbContextOptions options) : DbCon public DbSet IdempotencyKeys => Set(); public DbSet AuditEvents => Set(); + + public DbSet DashboardKeys => Set(); #pragma warning restore CS1591 /// @@ -274,6 +276,13 @@ private static void ConfigureInfrastructure(ModelBuilder model) e.HasIndex(x => x.ExpiresAt); }); + model.Entity(e => + { + e.ToTable("dashboard_keys"); + e.Property(x => x.Id).UseIdentityAlwaysColumn(); + e.Property(x => x.FriendlyName).HasMaxLength(200); + }); + model.Entity(e => { e.ToTable("audit_events"); diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/CronSchedule.cs b/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/CronSchedule.cs index 45c15b0..bc48252 100644 --- a/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/CronSchedule.cs +++ b/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/CronSchedule.cs @@ -21,7 +21,7 @@ public static CronSchedule Parse(string cron, string timeZone) { if (string.IsNullOrWhiteSpace(cron) || cron.Length > 100) { - throw Invalid("The cron expression is required (up to 100 chars)."); + throw new PsrDomainException(PsrErrorCodes.InvalidCron, "The cron expression is required (up to 100 chars)."); } var fields = cron.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length; @@ -32,7 +32,7 @@ public static CronSchedule Parse(string cron, string timeZone) } catch (CronFormatException ex) { - throw Invalid($"Invalid cron expression '{cron}': {ex.Message}"); + throw new PsrDomainException(PsrErrorCodes.InvalidCron, $"Invalid cron expression '{cron}': {ex.Message}", ex); } try @@ -41,7 +41,7 @@ public static CronSchedule Parse(string cron, string timeZone) } catch (Exception ex) when (ex is TimeZoneNotFoundException or InvalidTimeZoneException or ArgumentException) { - throw Invalid($"Unknown IANA time zone '{timeZone}'."); + throw new PsrDomainException(PsrErrorCodes.InvalidTimeZone, $"Unknown IANA time zone '{timeZone}'.", ex); } } @@ -50,6 +50,25 @@ public static CronSchedule Parse(string cron, string timeZone) public DateTimeOffset? Next(DateTimeOffset after) => _expression.GetNextOccurrence(after.ToUniversalTime(), _zone) is { } next ? next.ToUniversalTime() : null; + /// The next occurrences after (UTC). + /// Reference instant. + /// How many. + public IReadOnlyList Next(DateTimeOffset after, int count) + { + var occurrences = new List(count); + var from = after; + while (occurrences.Count < count && Next(from) is { } next) + { + occurrences.Add(next); + from = next; + } + + return occurrences; + } + + /// The time zone of the expression. + public TimeZoneInfo Zone => _zone; + /// How many occurrences fall in [from, to], counting up to . /// First instant (inclusive). /// Last instant (inclusive). @@ -57,5 +76,4 @@ public static CronSchedule Parse(string cron, string timeZone) public int CountOccurrences(DateTimeOffset from, DateTimeOffset to, int max = 100_000) => to < from ? 0 : _expression.GetOccurrences(from.ToUniversalTime(), to.ToUniversalTime(), _zone, fromInclusive: true, toInclusive: true).Take(max).Count(); - private static PsrDomainException Invalid(string message) => new(PsrErrorCodes.ScheduleInvalid, message); } diff --git a/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/SchedulePreview.cs b/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/SchedulePreview.cs new file mode 100644 index 0000000..3d7bea8 --- /dev/null +++ b/src/PythonScriptRunner.EntityFrameworkCore/Scheduling/SchedulePreview.cs @@ -0,0 +1,28 @@ +using PythonScriptRunner.EntityFrameworkCore.Scheduling; + +namespace PythonScriptRunner.EntityFrameworkCore; + +/// Next occurrences of a cron expression in an IANA time zone (the schedule form preview). +public static class SchedulePreview +{ + /// Largest preview. + public const int MaxCount = 20; + + /// The next occurrences after . INVALID_CRON or INVALID_TIME_ZONE when invalid. + /// Cron expression (5 or 6 fields). + /// IANA time zone. + /// Reference instant. + /// How many (1-20). + public static IReadOnlyList Next(string cron, string timeZone, DateTimeOffset after, int count) + { + ArgumentOutOfRangeException.ThrowIfLessThan(count, 1); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, MaxCount); + var schedule = CronSchedule.Parse(cron, timeZone); + return [.. schedule.Next(after, count).Select(utc => new ScheduleOccurrence(utc, TimeZoneInfo.ConvertTime(utc, schedule.Zone)))]; + } +} + +/// One occurrence of a schedule. +/// Instant in UTC. +/// Same instant in the schedule time zone (with its offset). +public sealed record ScheduleOccurrence(DateTimeOffset Utc, DateTimeOffset Local); diff --git a/tests/PythonScriptRunner.Core.Tests/Unit/PipelineRulesTests.cs b/tests/PythonScriptRunner.Core.Tests/Unit/PipelineRulesTests.cs index 3c72833..d34de8d 100644 --- a/tests/PythonScriptRunner.Core.Tests/Unit/PipelineRulesTests.cs +++ b/tests/PythonScriptRunner.Core.Tests/Unit/PipelineRulesTests.cs @@ -67,7 +67,7 @@ public void Spec003_AC05_WarningsDoNotBlock() [MemberData(nameof(Compatibility))] public void Spec003_AC06_TypeCompatibility(FieldType source, FieldType target, bool accepted) { - TypeCompatibility.IsAssignable(new FieldDefinition("s", source), new FieldDefinition("t", target)).ShouldBe(accepted); + FieldCompatibility.IsAssignable(new FieldDefinition("s", source), new FieldDefinition("t", target)).ShouldBe(accepted); } public static TheoryData Compatibility() => new() @@ -92,8 +92,8 @@ public void TableColumns_MustExistInTheSource() var target = new FieldDefinition("t", FieldType.Table) { Columns = [new TableColumn("x", FieldType.Number)] }; var missing = new FieldDefinition("t", FieldType.Table) { Columns = [new TableColumn("y", FieldType.Number)] }; - TypeCompatibility.IsAssignable(source, target).ShouldBeTrue(); - TypeCompatibility.IsAssignable(source, missing).ShouldBeFalse(); + FieldCompatibility.IsAssignable(source, target).ShouldBeTrue(); + FieldCompatibility.IsAssignable(source, missing).ShouldBeFalse(); } private static ValidationReport Validate(PipelineDefinition pipeline) => diff --git a/tests/PythonScriptRunner.EntityFrameworkCore.Tests/SchemaTests.cs b/tests/PythonScriptRunner.EntityFrameworkCore.Tests/SchemaTests.cs index 8201395..64f910e 100644 --- a/tests/PythonScriptRunner.EntityFrameworkCore.Tests/SchemaTests.cs +++ b/tests/PythonScriptRunner.EntityFrameworkCore.Tests/SchemaTests.cs @@ -21,7 +21,7 @@ UNION ALL private static readonly string[] Tables = [ "stages", "stage_versions", "pipelines", "pipeline_versions", "pipeline_steps", "schedules", "runs", "step_runs", - "step_attempts", "attempt_logs", "artifacts", "artifact_contents", "outbox_messages", "idempotency_keys", "audit_events", + "step_attempts", "attempt_logs", "artifacts", "artifact_contents", "outbox_messages", "idempotency_keys", "audit_events", "dashboard_keys", ]; [Fact] diff --git a/tests/PythonScriptRunner.Worker.Tests/SchedulerTests.cs b/tests/PythonScriptRunner.Worker.Tests/SchedulerTests.cs index dee75ee..3cf4f96 100644 --- a/tests/PythonScriptRunner.Worker.Tests/SchedulerTests.cs +++ b/tests/PythonScriptRunner.Worker.Tests/SchedulerTests.cs @@ -113,10 +113,10 @@ public async Task ScheduleEditor_ValidatesAuditsAndRecomputesTheNextRun() await PublishEchoPipelineAsync(app); (await Should.ThrowAsync(() => app.Admin.CreateScheduleAsync("eco-agendado", EchoSchedule("61 * * * *"), PsrEnvironment.Author, Ct))) - .Code.ShouldBe(PsrErrorCodes.ScheduleInvalid); + .Code.ShouldBe(PsrErrorCodes.InvalidCron); (await Should.ThrowAsync(() => app.Admin.CreateScheduleAsync("eco-agendado", EchoSchedule("* * * * *") with { TimeZone = "Marte/Olimpo" }, PsrEnvironment.Author, Ct))) - .Code.ShouldBe(PsrErrorCodes.ScheduleInvalid); + .Code.ShouldBe(PsrErrorCodes.InvalidTimeZone); await Should.ThrowAsync(() => app.Admin.CreateScheduleAsync("eco-agendado", EchoSchedule("* * * * *") with { Inputs = new JsonObject { ["outro"] = 1 } }, PsrEnvironment.Author, Ct)); await Should.ThrowAsync(() => app.Admin.CreateScheduleAsync("nao-existe", EchoSchedule("* * * * *"), PsrEnvironment.Author, Ct)); From f57c79f99e63213d31330e409f19bf5772645a8f Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:07:00 -0300 Subject: [PATCH 2/7] =?UTF-8?q?feat(dashboard):=20painel=20embut=C3=ADvel?= =?UTF-8?q?=20com=20API=20interna,=20tempo=20real=20e=20acesso=20configur?= =?UTF-8?q?=C3=A1vel=20(spec=20007)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AddDashboard + MapPythonScriptRunnerDashboard(prefixo): API em {prefixo}/api, hub SignalR em {prefixo}/hubs/runs e página provisória com e CSP. - Modos de acesso (ADR-0021): login fixo (hash PBKDF2, cookie psr.session 8 h/24 h, bloqueio 5 falhas/15 min, 10 tentativas/min por IP, recarga sem reinício, chaves em psr.dashboard_keys), filtro próprio, autenticação do host e somente local (Development). - Papéis cumulativos por política, 401/403 em JSON só nos endpoints do painel, CSRF por X-PSR-Request, ETag/If-Match, ProblemDetails com code. - Disparos e testes por multipart (50 MB por arquivo, 200 MB por requisição, 413) ou JSON; auditoria de disparos e logins; visão geral; OpenAPI interno só em Development. Co-Authored-By: Claude Opus 5.5 --- Directory.Packages.props | 5 + PythonScriptRunner.slnx | 4 + .../Access/ApiProblems.cs | 140 ++++++++ .../Access/DashboardAccess.cs | 171 ++++++++++ .../DashboardAuthorizationResultHandler.cs | 30 ++ .../Access/FixedLoginUsers.cs | 141 ++++++++ .../Access/LoginThrottle.cs | 86 +++++ .../Access/SessionCookie.cs | 195 +++++++++++ .../Api/AdminEndpoints.cs | 129 ++++++++ .../Api/ApiModels.cs | 139 ++++++++ .../Api/AuthEndpoints.cs | 131 ++++++++ .../Api/DashboardOpenApi.cs | 123 +++++++ .../Api/PipelineEndpoints.cs | 310 ++++++++++++++++++ .../Api/RunEndpoints.cs | 158 +++++++++ .../Api/RunInputs.cs | 219 +++++++++++++ .../Api/ScheduleEndpoints.cs | 91 +++++ .../Api/StageEndpoints.cs | 246 ++++++++++++++ ...DashboardEndpointRouteBuilderExtensions.cs | 89 +++++ .../DashboardOptions.cs | 226 +++++++++++++ .../DashboardServiceCollectionExtensions.cs | 214 ++++++++++++ .../PythonScriptRunner.Dashboard.csproj | 31 ++ .../Realtime/RunsHub.cs | 81 +++++ .../Spa/DashboardSpa.cs | 71 ++++ .../wwwroot/index.html | 22 ++ 24 files changed, 3052 insertions(+) create mode 100644 src/PythonScriptRunner.Dashboard/Access/ApiProblems.cs create mode 100644 src/PythonScriptRunner.Dashboard/Access/DashboardAccess.cs create mode 100644 src/PythonScriptRunner.Dashboard/Access/DashboardAuthorizationResultHandler.cs create mode 100644 src/PythonScriptRunner.Dashboard/Access/FixedLoginUsers.cs create mode 100644 src/PythonScriptRunner.Dashboard/Access/LoginThrottle.cs create mode 100644 src/PythonScriptRunner.Dashboard/Access/SessionCookie.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/AdminEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/ApiModels.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/AuthEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/PipelineEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/RunEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/RunInputs.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/ScheduleEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/Api/StageEndpoints.cs create mode 100644 src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs create mode 100644 src/PythonScriptRunner.Dashboard/DashboardOptions.cs create mode 100644 src/PythonScriptRunner.Dashboard/DashboardServiceCollectionExtensions.cs create mode 100644 src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj create mode 100644 src/PythonScriptRunner.Dashboard/Realtime/RunsHub.cs create mode 100644 src/PythonScriptRunner.Dashboard/Spa/DashboardSpa.cs create mode 100644 src/PythonScriptRunner.Dashboard/wwwroot/index.html diff --git a/Directory.Packages.props b/Directory.Packages.props index d1093a0..4bd02bd 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,7 +5,10 @@ + + + @@ -15,8 +18,10 @@ + + diff --git a/PythonScriptRunner.slnx b/PythonScriptRunner.slnx index 0d7529d..07a760f 100644 --- a/PythonScriptRunner.slnx +++ b/PythonScriptRunner.slnx @@ -1,17 +1,21 @@ + + + + diff --git a/src/PythonScriptRunner.Dashboard/Access/ApiProblems.cs b/src/PythonScriptRunner.Dashboard/Access/ApiProblems.cs new file mode 100644 index 0000000..02e3ed5 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/ApiProblems.cs @@ -0,0 +1,140 @@ +using System.Diagnostics; +using System.Text.Json; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace PythonScriptRunner.Dashboard; + +/// Errors of the API as ProblemDetails (RFC 9457) with the extensions code, errors, current and traceId. +internal static class ApiProblems +{ + public static ProblemDetails Create(HttpContext context, int status, string code, string title, string? detail = null) + { + var problem = new ProblemDetails + { + Status = status, + Title = title, + Detail = detail, + Type = $"https://httpstatuses.io/{status}", + }; + problem.Extensions["code"] = code; + problem.Extensions["traceId"] = Activity.Current?.TraceId.ToHexString() ?? context.TraceIdentifier; + return problem; + } + + public static IResult Result(HttpContext context, int status, string code, string title, string? detail = null, Action? extend = null) + { + var problem = Create(context, status, code, title, detail); + extend?.Invoke(problem); + return TypedResults.Problem(problem); + } + + public static IResult NotFound(HttpContext context, string detail) => + Result(context, StatusCodes.Status404NotFound, "NOT_FOUND", "The resource was not found.", detail); + + public static Task WriteAsync(HttpContext context, int status, string code, string title, string? detail = null) + { + context.Response.StatusCode = status; + return context.Response.WriteAsJsonAsync(Create(context, status, code, title, detail), options: null, contentType: "application/problem+json"); + } + + /// Maps a domain exception to its status (spec 007); null for unexpected exceptions. + public static IResult? FromException(HttpContext context, Exception exception) => exception switch + { + InputValidationException invalid => Result(context, StatusCodes.Status400BadRequest, "INPUT_INVALID", "The inputs do not match the contract.", + invalid.Message, p => p.Extensions["errors"] = invalid.Errors + .GroupBy(e => e.Field) + .ToDictionary(g => g.Key, g => g.Select(e => new { e.Code, e.Message }).ToArray())), + ConcurrencyConflictException conflict => Result(context, StatusCodes.Status412PreconditionFailed, conflict.Code, "The resource was changed by someone else.", + conflict.Message, p => p.Extensions["current"] = conflict.Current), + ScriptSyntaxException syntax => Result(context, StatusCodes.Status422UnprocessableEntity, syntax.Code, "The script does not compile.", + syntax.Message, p => + { + p.Extensions["line"] = syntax.Line; + p.Extensions["column"] = syntax.Column; + }), + DefinitionInvalidException definition => Result(context, + definition.Code == PsrErrorCodes.ImportInvalid ? StatusCodes.Status400BadRequest : StatusCodes.Status422UnprocessableEntity, + definition.Code, "The definition is not valid.", definition.Message, p => p.Extensions["report"] = definition.Report), + VersionReferencedException referenced => Result(context, StatusCodes.Status409Conflict, referenced.Code, "The version is in use.", + referenced.Message, p => p.Extensions["pipelines"] = referenced.Pipelines), + PsrDomainException domain => Result(context, StatusOf(domain.Code), domain.Code, Title(domain.Code), domain.Message), + StageNotFoundException or PipelineNotFoundException or VersionNotFoundException or RunNotFoundException or ArtifactNotFoundException => + Result(context, StatusCodes.Status404NotFound, "NOT_FOUND", "The resource was not found.", exception.Message), + VersionNotRunnableException => Result(context, StatusCodes.Status409Conflict, "VERSION_NOT_RUNNABLE", "The version cannot run.", exception.Message), + PayloadTooLargeException or BadHttpRequestException { StatusCode: StatusCodes.Status413PayloadTooLarge } => + Result(context, StatusCodes.Status413PayloadTooLarge, "PAYLOAD_TOO_LARGE", "The upload is larger than allowed.", exception.Message), + BadHttpRequestException bad => Result(context, bad.StatusCode, "BAD_REQUEST", "The request is not valid.", exception.Message), + ArgumentException or JsonException or InvalidDataException => + Result(context, StatusCodes.Status400BadRequest, "INVALID_ARGUMENT", "The request is not valid.", exception.Message), + _ => null, + }; + + private static int StatusOf(string code) => code switch + { + PsrErrorCodes.ConcurrencyConflict => StatusCodes.Status412PreconditionFailed, + PsrErrorCodes.ArtifactExpired => StatusCodes.Status410Gone, + PsrErrorCodes.ScheduleNotFound => StatusCodes.Status404NotFound, + PsrErrorCodes.ScriptSyntaxError or PsrErrorCodes.DependencyResolutionFailed or PsrErrorCodes.StageInvalid or PsrErrorCodes.PipelineInvalid + => StatusCodes.Status422UnprocessableEntity, + PsrErrorCodes.RetentionExceedsGlobal or PsrErrorCodes.ScheduleFileInput or PsrErrorCodes.InvalidCron or PsrErrorCodes.InvalidTimeZone + or PsrErrorCodes.ScheduleInvalid or PsrErrorCodes.ImportInvalid or PsrErrorCodes.FileInputNotAllowed => StatusCodes.Status400BadRequest, + _ => StatusCodes.Status409Conflict, + }; + + private static string Title(string code) => code switch + { + PsrErrorCodes.ArtifactExpired => "The file is beyond the retention.", + PsrErrorCodes.ScheduleNotFound => "The schedule was not found.", + _ => "The operation is not allowed in the current state.", + }; +} + +/// +/// Endpoint filter of the API: requires X-PSR-Request: 1 on mutating requests (CSRF; QA-007-3) and turns domain +/// exceptions into ProblemDetails. +/// +internal sealed class ApiFilter : IEndpointFilter +{ + public const string CsrfHeader = "X-PSR-Request"; + + public async ValueTask InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) + { + ArgumentNullException.ThrowIfNull(context); + ArgumentNullException.ThrowIfNull(next); + var http = context.HttpContext; + if (!HttpMethods.IsGet(http.Request.Method) && !HttpMethods.IsHead(http.Request.Method) && !HttpMethods.IsOptions(http.Request.Method) + && http.Request.Headers[CsrfHeader] != "1") + { + return ApiProblems.Result(http, StatusCodes.Status400BadRequest, "CSRF_HEADER_MISSING", + $"Mutating requests must send the header {CsrfHeader}: 1."); + } + + try + { + return await next(context).ConfigureAwait(false); + } + catch (Exception ex) when (ApiProblems.FromException(http, ex) is { } problem) + { + return problem; + } + } +} + +/// An uploaded file or the whole upload exceeds the limits (413 before any run is created). +internal sealed class PayloadTooLargeException(string message) : Exception(message); + +/// Optimistic concurrency with ETag = row version (xmin) and If-Match. +internal static class ETags +{ + public static string Of(uint rowVersion) => $"\"{rowVersion}\""; + + public static void Set(HttpContext context, uint rowVersion) => context.Response.Headers.ETag = Of(rowVersion); + + /// The row version sent in If-Match; null when missing or malformed. + public static uint? IfMatch(HttpContext context) => + context.Request.Headers.IfMatch.ToString().Trim().Trim('"') is { Length: > 0 } text && uint.TryParse(text, out var version) ? version : null; + + public static IResult Required(HttpContext context) => + ApiProblems.Result(context, StatusCodes.Status428PreconditionRequired, "IF_MATCH_REQUIRED", "Send the ETag read before in If-Match."); +} diff --git a/src/PythonScriptRunner.Dashboard/Access/DashboardAccess.cs b/src/PythonScriptRunner.Dashboard/Access/DashboardAccess.cs new file mode 100644 index 0000000..c02c84d --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/DashboardAccess.cs @@ -0,0 +1,171 @@ +using System.Net; +using System.Security.Claims; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace PythonScriptRunner.Dashboard.Access; + +/// The user of a dashboard request. +/// Recorded in the audit and on the runs: login:<name>, filter:<name>, local or the host sub. +/// Display name. +/// Role, or null when authenticated by the host without any dashboard role (403). +/// How the user was identified. +internal sealed record DashboardPrincipal(string Subject, string Name, DashboardRole? Role, DashboardAccessMode Mode) +{ + /// The user as the author of changes (the subject fits the 200 chars of the audit and authorship columns). + public Actor Actor => Actor.User(Subject.Length <= 200 ? Subject : Subject[..200]); + + /// The roles the user has (cumulative). + public IReadOnlyList Roles => Role is { } role ? [.. Enum.GetValues().Where(r => r <= role)] : []; +} + +/// Resolved access settings (validated by AddDashboard). +/// Main mode, or null when only local requests are allowed. +/// Local requests enter as Admin. +/// Filter of the filter mode. +/// Policies of the roles in the host mode. +/// Schemes that read the host user. +/// Host login page. +/// Host logout page. +internal sealed record DashboardAccessSettings( + DashboardAccessMode? Mode, + bool AllowLocalRequests, + Type? FilterType, + IReadOnlyDictionary RolePolicies, + IReadOnlyList HostSchemes, + string? LoginUrl, + string? LogoutUrl) +{ + /// The mode reported to the panel. + public DashboardAccessMode EffectiveMode => Mode ?? DashboardAccessMode.Localhost; +} + +/// Identifies the user of a request in the configured mode (ADR-0021). The result is cached for the request. +internal sealed class DashboardAccess(DashboardAccessSettings settings) +{ + public const string SessionScheme = "PythonScriptRunner.Dashboard.Session"; + public const string RoleClaim = "psr:role"; + public const string StampClaim = "psr:stamp"; + + private static readonly object CacheKey = new(); + + public async ValueTask ResolveAsync(HttpContext http) + { + ArgumentNullException.ThrowIfNull(http); + if (http.Items.TryGetValue(CacheKey, out var cached)) + { + return (DashboardPrincipal?)cached; + } + + var principal = await ResolveCoreAsync(http).ConfigureAwait(false); + http.Items[CacheKey] = principal; + return principal; + } + + /// Whether the request comes from this machine (the real address, after the host ForwardedHeaders). + public static bool IsLocal(HttpContext http) + { + var remote = http.Connection.RemoteIpAddress; + return remote is not null && (IPAddress.IsLoopback(remote) || remote.Equals(http.Connection.LocalIpAddress)); + } + + private async ValueTask ResolveCoreAsync(HttpContext http) + { + if (settings.AllowLocalRequests && IsLocal(http)) + { + return new DashboardPrincipal("local", "local", DashboardRole.Admin, DashboardAccessMode.Localhost); + } + + switch (settings.Mode) + { + case DashboardAccessMode.FixedLogin: + { + var result = await http.AuthenticateAsync(SessionScheme).ConfigureAwait(false); + if (!result.Succeeded || result.Principal.Identity?.Name is not { } name + || !Enum.TryParse(result.Principal.FindFirstValue(RoleClaim), out var role)) + { + return null; + } + + return new DashboardPrincipal($"login:{name}", name, role, DashboardAccessMode.FixedLogin); + } + + case DashboardAccessMode.Filter: + { + var filter = (IDashboardAuthorizationFilter)http.RequestServices.GetRequiredService(settings.FilterType!); + var user = await filter.AuthorizeAsync(http).ConfigureAwait(false); + return user is null || string.IsNullOrWhiteSpace(user.Name) + ? null + : new DashboardPrincipal($"filter:{user.Name}", user.Name, user.Role, DashboardAccessMode.Filter); + } + + case DashboardAccessMode.Host: + return await ResolveHostAsync(http).ConfigureAwait(false); + + default: + return null; + } + } + + private async ValueTask ResolveHostAsync(HttpContext http) + { + var user = http.User; + foreach (var scheme in settings.HostSchemes) + { + var result = await http.AuthenticateAsync(scheme).ConfigureAwait(false); + if (result.Succeeded) + { + user = result.Principal; + break; + } + } + + if (user.Identity?.IsAuthenticated != true) + { + return null; + } + + var subject = user.FindFirstValue("sub") ?? user.FindFirstValue(ClaimTypes.NameIdentifier) ?? user.Identity.Name ?? "unknown"; + var name = user.Identity.Name ?? user.FindFirstValue("name") ?? subject; + // Resolved per request: host handlers may be scoped. + var authorization = http.RequestServices.GetRequiredService(); + foreach (var role in (DashboardRole[])[DashboardRole.Admin, DashboardRole.Author, DashboardRole.Executor, DashboardRole.Reader]) + { + if ((await authorization.AuthorizeAsync(user, http, settings.RolePolicies[role]).ConfigureAwait(false)).Succeeded) + { + return new DashboardPrincipal(subject, name, role, DashboardAccessMode.Host); + } + } + + return new DashboardPrincipal(subject, name, null, DashboardAccessMode.Host); + } +} + +/// Requires at least (roles are cumulative; D-28), or only an identified user when null. +/// Lowest role allowed. +internal sealed record DashboardRoleRequirement(DashboardRole? Minimum) : IAuthorizationRequirement +{ + /// Policy of any identified user, even without a role (/me, logout). + public const string UserPolicy = "psr:user"; + + /// Name of the policy of a role (psr:reader, psr:executor, psr:author, psr:admin). + public static string PolicyName(DashboardRole role) => $"psr:{role.ToString().ToLowerInvariant()}"; +} + +/// Evaluates with the dashboard user of the request. +internal sealed class DashboardRoleHandler(DashboardAccess access) : AuthorizationHandler +{ + protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, DashboardRoleRequirement requirement) + { + if (context.Resource is HttpContext http && await access.ResolveAsync(http).ConfigureAwait(false) is { } user + && (requirement.Minimum is not { } minimum || user.Role >= minimum)) + { + context.Succeed(requirement); + } + } +} + +/// Marks the endpoints of the dashboard (API and hub): their failures become JSON 401/403 instead of host challenges. +internal sealed class DashboardEndpointMetadata; diff --git a/src/PythonScriptRunner.Dashboard/Access/DashboardAuthorizationResultHandler.cs b/src/PythonScriptRunner.Dashboard/Access/DashboardAuthorizationResultHandler.cs new file mode 100644 index 0000000..0cf41f4 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/DashboardAuthorizationResultHandler.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization.Policy; +using Microsoft.AspNetCore.Http; + +namespace PythonScriptRunner.Dashboard.Access; + +/// +/// For dashboard endpoints, an authorization failure is answered with JSON — 401 when nobody is identified, 403 when the role is +/// not enough — instead of the host's challenge (which could redirect to an identity provider). Other endpoints keep the +/// handler the host had. +/// +internal sealed class DashboardAuthorizationResultHandler(IAuthorizationMiddlewareResultHandler inner, DashboardAccess access) : IAuthorizationMiddlewareResultHandler +{ + public async Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult) + { + ArgumentNullException.ThrowIfNull(context); + ArgumentNullException.ThrowIfNull(authorizeResult); + if (authorizeResult.Succeeded || context.GetEndpoint()?.Metadata.GetMetadata() is null) + { + await inner.HandleAsync(next, context, policy, authorizeResult).ConfigureAwait(false); + return; + } + + var user = await access.ResolveAsync(context).ConfigureAwait(false); + var (status, code, title) = user is null + ? (StatusCodes.Status401Unauthorized, "UNAUTHENTICATED", "Authentication is required.") + : (StatusCodes.Status403Forbidden, "FORBIDDEN", "The role of the user does not allow this action."); + await ApiProblems.WriteAsync(context, status, code, title).ConfigureAwait(false); + } +} diff --git a/src/PythonScriptRunner.Dashboard/Access/FixedLoginUsers.cs b/src/PythonScriptRunner.Dashboard/Access/FixedLoginUsers.cs new file mode 100644 index 0000000..39c2f22 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/FixedLoginUsers.cs @@ -0,0 +1,141 @@ +using System.Security.Cryptography; +using System.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace PythonScriptRunner.Dashboard.Access; + +/// Fixed login settings as bound from configuration (reloaded when it changes). +internal sealed class FixedLoginSettings +{ + public List Users { get; set; } = []; + + public TimeSpan SlidingExpiration { get; set; } = TimeSpan.FromHours(8); + + public TimeSpan AbsoluteExpiration { get; set; } = TimeSpan.FromHours(24); + + public int MaxFailedAttempts { get; set; } = 5; + + public TimeSpan LockoutDuration { get; set; } = TimeSpan.FromMinutes(15); + + public int MaxAttemptsPerMinutePerIp { get; set; } = 10; +} + +/// A validated user of the fixed login. +/// User name as configured. +/// Role. +/// Password hash. +/// Fingerprint of the hash kept in the session: a changed password ends the sessions. +internal sealed record LoginAccount(string Name, DashboardRole Role, string PasswordHash, string Stamp); + +/// +/// The current users of the fixed login (D-119, D-128). A reloaded configuration replaces them only when valid; an invalid one +/// is refused and logged, and the previous users stay. +/// +internal sealed partial class FixedLoginUsers : IDisposable +{ + private readonly ILogger _logger; + private readonly IDisposable? _subscription; + private volatile Snapshot _current; + + public FixedLoginUsers(IOptionsMonitor settings, ILogger logger) + { + _logger = logger; + _current = Validate(settings.CurrentValue) is { } first + ? first + : throw new PsrConfigurationException( + $"Invalid fixed login configuration of the dashboard: {string.Join("; ", Errors(settings.CurrentValue))}. Configure the users in code or " + + $"in the section {FixedLoginOptions.DefaultSection} (e.g. PythonScriptRunner__Dashboard__Login__Users__0__Name, __Role and " + + "__PasswordHash, with the hash generated by 'psr hash-password')."); + _subscription = settings.OnChange(changed => + { + if (Validate(changed) is { } next) + { + _current = next; + } + else + { + LogReloadRefused(_logger); + } + }); + } + + /// Current settings (session and lockout). + public FixedLoginSettings Settings => _current.Settings; + + /// Finds a user by name (ignoring case). + public LoginAccount? Find(string? name) => name is not null && _current.Accounts.TryGetValue(name, out var account) ? account : null; + + public void Dispose() => _subscription?.Dispose(); + + /// Problems of a configuration (empty when it is valid). + public static IReadOnlyList Errors(FixedLoginSettings settings) + { + var errors = new List(); + if (settings.Users.Count == 0) + { + errors.Add("no users are configured"); + } + + foreach (var user in settings.Users) + { + var name = string.IsNullOrWhiteSpace(user.Name) ? "(empty name)" : user.Name; + if (string.IsNullOrWhiteSpace(user.Name) || user.Name.Length > 100) + { + errors.Add($"user '{name}': the name must have 1-100 chars"); + } + + if (!Enum.IsDefined(user.Role)) + { + errors.Add($"user '{name}': unknown role"); + } + + if (!DashboardPasswords.IsHash(user.PasswordHash)) + { + errors.Add($"user '{name}': PasswordHash is not a hash generated by 'psr hash-password' (plain passwords are not accepted)"); + } + } + + errors.AddRange(settings.Users.Where(u => !string.IsNullOrWhiteSpace(u.Name)) + .GroupBy(u => u.Name, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1) + .Select(g => $"user '{g.Key}' is configured more than once")); + if (settings.SlidingExpiration <= TimeSpan.Zero || settings.AbsoluteExpiration < settings.SlidingExpiration) + { + errors.Add("SlidingExpiration must be positive and not longer than AbsoluteExpiration"); + } + + if (settings.MaxFailedAttempts < 1 || settings.LockoutDuration <= TimeSpan.Zero || settings.MaxAttemptsPerMinutePerIp < 1) + { + errors.Add("MaxFailedAttempts, LockoutDuration and MaxAttemptsPerMinutePerIp must be positive"); + } + + return errors; + } + + /// Stamp of a hash (SHA-256, first 16 hex chars). + public static string StampOf(string passwordHash) => + Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(passwordHash)))[..16]; + + private Snapshot? Validate(FixedLoginSettings settings) + { + var errors = Errors(settings); + if (errors.Count > 0) + { + LogInvalid(_logger, string.Join("; ", errors)); + return null; + } + + return new Snapshot(settings, settings.Users.ToDictionary( + u => u.Name, + u => new LoginAccount(u.Name, u.Role, u.PasswordHash, StampOf(u.PasswordHash)), + StringComparer.OrdinalIgnoreCase)); + } + + [LoggerMessage(300, LogLevel.Error, "Invalid fixed login configuration of the dashboard: {Errors}")] + private static partial void LogInvalid(ILogger logger, string errors); + + [LoggerMessage(301, LogLevel.Error, "The changed fixed login configuration was refused; the previous users stay valid")] + private static partial void LogReloadRefused(ILogger logger); + + private sealed record Snapshot(FixedLoginSettings Settings, IReadOnlyDictionary Accounts); +} diff --git a/src/PythonScriptRunner.Dashboard/Access/LoginThrottle.cs b/src/PythonScriptRunner.Dashboard/Access/LoginThrottle.cs new file mode 100644 index 0000000..763be3c --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/LoginThrottle.cs @@ -0,0 +1,86 @@ +using System.Collections.Concurrent; + +namespace PythonScriptRunner.Dashboard.Access; + +/// +/// Brute force protection of the fixed login (D-126, D-135): consecutive failures lock a user for a while, and attempts per +/// IP are limited per minute. Counters live in memory, per instance. +/// +internal sealed class LoginThrottle(TimeProvider time) +{ + private static readonly TimeSpan Window = TimeSpan.FromMinutes(1); + + private readonly ConcurrentDictionary _users = new(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _ips = new(StringComparer.Ordinal); + + /// Counts an attempt of an IP; returns how long to wait when over the limit. + public TimeSpan? CountAttempt(string ip, int maxPerMinute) + { + var now = time.GetUtcNow(); + if (_ips.Count > 10_000) + { + // Keeps memory bounded when many addresses try. + foreach (var (address, old) in _ips) + { + if (old.Attempts.Count == 0 || now - old.Attempts.Last() >= Window) + { + _ips.TryRemove(address, out _); + } + } + } + + var state = _ips.GetOrAdd(ip, _ => new IpState()); + lock (state) + { + while (state.Attempts.Count > 0 && now - state.Attempts.Peek() >= Window) + { + state.Attempts.Dequeue(); + } + + if (state.Attempts.Count >= maxPerMinute) + { + return state.Attempts.Peek() + Window - now; + } + + state.Attempts.Enqueue(now); + return null; + } + } + + /// Whether the user is locked now. + public bool IsLocked(string user) => + _users.TryGetValue(user, out var state) && state.LockedUntil is { } until && until > time.GetUtcNow(); + + /// Records a failure; returns true when it locks the user. + public bool Failed(string user, int maxFailures, TimeSpan lockout) + { + var state = _users.GetOrAdd(user, _ => new UserState()); + lock (state) + { + state.Failures++; + if (state.Failures < maxFailures) + { + return false; + } + + state.Failures = 0; + state.LockedUntil = time.GetUtcNow() + lockout; + return true; + } + } + + /// A successful login clears the failures of the user. + public void Succeeded(string user) => _users.TryRemove(user, out _); + + private sealed class UserState + { + public int Failures { get; set; } + + public DateTimeOffset? LockedUntil { get; set; } + } + + private sealed class IpState + { + public Queue Attempts { get; } = new(); + } +} diff --git a/src/PythonScriptRunner.Dashboard/Access/SessionCookie.cs b/src/PythonScriptRunner.Dashboard/Access/SessionCookie.cs new file mode 100644 index 0000000..e714567 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Access/SessionCookie.cs @@ -0,0 +1,195 @@ +using System.Globalization; +using System.Security.Claims; +using System.Xml.Linq; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.AspNetCore.DataProtection.Repositories; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using PythonScriptRunner.EntityFrameworkCore; +using PythonScriptRunner.EntityFrameworkCore.Entities; + +namespace PythonScriptRunner.Dashboard.Access; + +/// Where the dashboard is mapped (set by MapPythonScriptRunnerDashboard; cookie options are built later, on first use). +internal sealed class DashboardRoutes +{ + public string Prefix { get; set; } = "/psr"; + + public bool Mapped { get; set; } +} + +/// +/// Session cookie of the fixed login (D-120, D-127, D-128): psr.session, HttpOnly, SameSite=Lax, Secure on +/// HTTPS, path = prefix; 8 h sliding and 24 h absolute. Each request checks that the user still exists with the same password +/// and takes its current role. +/// +internal sealed class SessionCookieOptions( + DashboardRoutes routes, FixedLoginUsers users, TimeProvider time, IServiceProvider services, bool persistKeysInDatabase) + : IConfigureNamedOptions +{ + public const string CookieName = "psr.session"; + + /// Login time, kept in the ticket across renewals (absolute expiration). + public const string LoginAtItem = "psr.loginAt"; + + private static readonly TimeSpan RenewAfter = TimeSpan.FromMinutes(1); + + public void Configure(CookieAuthenticationOptions options) => Configure(Options.DefaultName, options); + + public void Configure(string? name, CookieAuthenticationOptions options) + { + ArgumentNullException.ThrowIfNull(options); + if (name != DashboardAccess.SessionScheme) + { + return; + } + + options.Cookie = new PrefixCookieBuilder(routes) + { + Name = CookieName, + HttpOnly = true, + SameSite = SameSiteMode.Lax, + SecurePolicy = CookieSecurePolicy.SameAsRequest, + IsEssential = true, + }; + options.ExpireTimeSpan = users.Settings.SlidingExpiration; + options.SlidingExpiration = false; + options.TimeProvider = time; + if (persistKeysInDatabase) + { + options.DataProtectionProvider = services.GetRequiredService().Provider; + } + + options.Events = new CookieAuthenticationEvents + { + OnRedirectToLogin = context => + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + return Task.CompletedTask; + }, + OnRedirectToAccessDenied = context => + { + context.Response.StatusCode = StatusCodes.Status403Forbidden; + return Task.CompletedTask; + }, + OnValidatePrincipal = Validate, + }; + } + + private async Task Validate(CookieValidatePrincipalContext context) + { + var principal = context.Principal; + var account = users.Find(principal?.Identity?.Name); + var now = time.GetUtcNow(); + if (account is null || principal!.FindFirstValue(DashboardAccess.StampClaim) != account.Stamp + || LoginAt(context.Properties) is not { } loginAt || now - loginAt >= users.Settings.AbsoluteExpiration) + { + // Removed user, changed password or past the absolute expiration (D-127, D-128). + context.RejectPrincipal(); + await context.HttpContext.SignOutAsync(DashboardAccess.SessionScheme).ConfigureAwait(false); + return; + } + + if (principal!.FindFirstValue(DashboardAccess.RoleClaim) != account.Role.ToString()) + { + // A changed role applies at once (D-128). + context.ReplacePrincipal(CreatePrincipal(account)); + context.ShouldRenew = true; + } + + // Exact sliding expiration: the idle time counts from the last request (renewed at most once a minute). + if (context.Properties.IssuedUtc is { } issued && now - issued >= RenewAfter) + { + context.ShouldRenew = true; + } + } + + public static ClaimsPrincipal CreatePrincipal(LoginAccount account) => new(new ClaimsIdentity( + [ + new Claim(ClaimTypes.Name, account.Name), + new Claim(DashboardAccess.RoleClaim, account.Role.ToString()), + new Claim(DashboardAccess.StampClaim, account.Stamp), + ], + DashboardAccess.SessionScheme)); + + public static AuthenticationProperties CreateProperties(DateTimeOffset now) => new(new Dictionary + { + [LoginAtItem] = now.UtcTicks.ToString(CultureInfo.InvariantCulture), + }) + { + IsPersistent = false, + }; + + private static DateTimeOffset? LoginAt(AuthenticationProperties properties) => + properties.Items.TryGetValue(LoginAtItem, out var text) && long.TryParse(text, CultureInfo.InvariantCulture, out var ticks) + ? new DateTimeOffset(ticks, TimeSpan.Zero) + : null; + + /// Cookie path = path base + prefix, so a host mounted under a path base still scopes the cookie to the dashboard (AC-14). + private sealed class PrefixCookieBuilder(DashboardRoutes routes) : CookieBuilder + { + public override CookieOptions Build(HttpContext context, DateTimeOffset expiresFrom) + { + var options = base.Build(context, expiresFrom); + options.Path = context.Request.PathBase.Add(routes.Prefix).Value; + return options; + } + } +} + +/// +/// Data Protection dedicated to the session cookie, with its keys in psr.dashboard_keys (D-132): every instance and +/// restart reads the same keys. The Data Protection of the host is not changed. +/// +internal sealed class DashboardDataProtection(DashboardKeys keys, ILoggerFactory loggers) : IDisposable +{ + public const string ApplicationName = "PythonScriptRunner.Dashboard"; + + private readonly Lazy _services = new(() => + { + var collection = new ServiceCollection(); + collection.AddSingleton(loggers); + collection.AddDataProtection() + .SetApplicationName(ApplicationName) + .AddKeyManagementOptions(o => o.XmlRepository = keys); + return collection.BuildServiceProvider(); + }); + + public IDataProtectionProvider Provider => _services.Value.GetRequiredService(); + + public void Dispose() + { + if (_services.IsValueCreated) + { + _services.Value.Dispose(); + } + } +} + +/// Key ring of stored in psr.dashboard_keys. +internal sealed class DashboardKeys(IDbContextFactory factory, TimeProvider time) : IXmlRepository +{ + public IReadOnlyCollection GetAllElements() + { + using var db = factory.CreateDbContext(); + return [.. db.DashboardKeys.AsNoTracking().OrderBy(k => k.Id).Select(k => k.Xml).ToList().Select(XElement.Parse)]; + } + + public void StoreElement(XElement element, string friendlyName) + { + ArgumentNullException.ThrowIfNull(element); + using var db = factory.CreateDbContext(); + db.DashboardKeys.Add(new DashboardKeyEntity + { + FriendlyName = friendlyName, + Xml = element.ToString(SaveOptions.DisableFormatting), + CreatedAt = time.GetUtcNow(), + }); + db.SaveChanges(); + } +} diff --git a/src/PythonScriptRunner.Dashboard/Api/AdminEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/AdminEndpoints.cs new file mode 100644 index 0000000..3ef99a6 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/AdminEndpoints.cs @@ -0,0 +1,129 @@ +using System.Reflection; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using PythonScriptRunner.Dashboard.Access; +using PythonScriptRunner.EntityFrameworkCore; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Audit, secret status, settings and field types (spec 007). +internal static class AdminEndpoints +{ + private static readonly FieldTypesResponse FieldTypes = BuildFieldTypes(); + + public static void Map(RouteGroupBuilder api) + { + api.MapGet("/audit", AuditAsync).RequireAuthorization(Policies.Admin).WithTags("Administration").WithName("ListAudit") + .Produces>().ProducesProblem(StatusCodes.Status400BadRequest); + api.MapGet("/admin/secrets", SecretsAsync).RequireAuthorization(Policies.Admin).WithTags("Administration").WithName("ListSecrets") + .Produces>(); + api.MapGet("/admin/settings", Settings).RequireAuthorization(Policies.Admin).WithTags("Administration").WithName("GetSettings") + .Produces(); + api.MapGet("/meta/field-types", () => FieldTypes).RequireAuthorization(Policies.Reader).WithTags("Meta").WithName("GetFieldTypes") + .Produces(); + } + + private static Task> AuditAsync([AsParameters] AuditListQuery query, IAuditReader audit, CancellationToken cancellationToken) => + audit.ListAsync(new AuditQuery + { + Actor = query.Actor, + Action = query.Action, + EntityType = query.EntityType, + EntityKey = query.EntityKey, + From = query.From, + To = query.To, + Cursor = query.Cursor, + Limit = query.Limit ?? 50, + }, cancellationToken); + + /// Secrets declared by stage versions that are not archived, and whether they resolve — never their values (AC-16). + private static async Task> SecretsAsync( + IDbContextFactory factory, ISecretProvider secrets, CancellationToken cancellationToken) + { + var db = await factory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); + List<(string Secret, string DeclaredBy)> declarations; + await using (db.ConfigureAwait(false)) + { + var rows = await db.StageVersions.AsNoTracking() + .Where(v => v.Status != VersionStatus.Archived && v.RequiredSecrets.Length > 0) + .Select(v => new { v.Stage.Key, v.Version, v.RequiredSecrets }) + .ToListAsync(cancellationToken).ConfigureAwait(false); + declarations = [.. rows.SelectMany(r => r.RequiredSecrets.Select(s => (s, $"{r.Key}@{r.Version}")))]; + } + + var result = new List(); + foreach (var group in declarations.GroupBy(d => d.Secret, StringComparer.Ordinal).OrderBy(g => g.Key, StringComparer.Ordinal)) + { + var resolved = await secrets.GetSecretAsync(group.Key, cancellationToken).ConfigureAwait(false) is not null; + result.Add(new SecretStatus(group.Key, [.. group.Select(d => d.DeclaredBy).Distinct().Order(StringComparer.Ordinal)], resolved)); + } + + return result; + } + + private static SettingsResponse Settings( + HttpContext http, PsrEntityFrameworkOptions persistence, IOptions runner, DashboardAccessSettings access, DashboardRoutes routes) + { + var options = runner.Value; + return new SettingsResponse( + RetentionSettings.From(persistence.Retention), + new LimitSettings(options.MaxFileBytes, options.MaxRunFilesBytes, options.MaxLogBytes, options.DefaultTimeout, options.DefaultMemoryLimitMb, + options.MaxConcurrentProcesses, options.DefaultPythonVersion), + AuthEndpoints.Info(http, access, routes), + new Dictionary + { + ["pythonScriptRunner"] = VersionOf(typeof(AdminEndpoints).Assembly), + ["dotnet"] = Environment.Version.ToString(), + ["psrunner"] = options.PsrunnerRequirement, + }); + } + + private static string VersionOf(Assembly assembly) + { + var version = assembly.GetCustomAttribute()?.InformationalVersion ?? assembly.GetName().Version?.ToString() ?? "0.0.0"; + var metadata = version.IndexOf('+', StringComparison.Ordinal); + return metadata < 0 ? version : version[..metadata]; + } + + /// The 12 types with the widgets of spec 008 and the binding compatibility (spec 003). + private static FieldTypesResponse BuildFieldTypes() + { + (FieldType Type, string Input, string Output)[] types = + [ + (FieldType.Number, "number", "number"), + (FieldType.Integer, "number", "number"), + (FieldType.Decimal, "decimal-text", "number"), + (FieldType.String, "text", "text-copy"), + (FieldType.Bool, "switch", "chip"), + (FieldType.Date, "date-picker", "date"), + (FieldType.DateTime, "datetime-picker", "datetime"), + (FieldType.Enum("a", "b"), "select", "chip"), + (FieldType.Json, "json-editor", "json-tree"), + (FieldType.Table, "json-editor-csv", "data-grid"), + (FieldType.File, "file-upload", "file-card"), + (FieldType.ListOf(FieldType.String), "repeatable", "list"), + ]; + var infos = types.Select(t => new FieldTypeInfo(t.Type.WireName, t.Type.IsScalar, t.Input, t.Output, + t.Type.Kind is not (FieldKind.File or FieldKind.Table) && !t.Type.IsFile)).ToList(); + var compatibility = types.ToDictionary( + source => source.Type.WireName, + source => (IReadOnlyList)[.. types + .Where(target => FieldCompatibility.IsAssignable(new FieldDefinition("source", source.Type), new FieldDefinition("target", target.Type))) + .Select(target => target.Type.WireName)]); + return new FieldTypesResponse(infos, compatibility, + [ + "integer feeds number and decimal; enum feeds string; any non-file type feeds json.", + "enum feeds enum when every source value is a target value.", + "table feeds table when every declared target column exists in the source with a compatible type.", + "file feeds file when the declared content types intersect (no declaration accepts any).", + "list feeds list when T feeds U.", + ]); + } +} + +/// Filters of GET /audit. +internal sealed record AuditListQuery( + string? Actor, string? Action, string? EntityType, string? EntityKey, DateTimeOffset? From, DateTimeOffset? To, string? Cursor, int? Limit); diff --git a/src/PythonScriptRunner.Dashboard/Api/ApiModels.cs b/src/PythonScriptRunner.Dashboard/Api/ApiModels.cs new file mode 100644 index 0000000..ce21120 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/ApiModels.cs @@ -0,0 +1,139 @@ +using System.Text.Json.Nodes; +using PythonScriptRunner.EntityFrameworkCore; + +namespace PythonScriptRunner.Dashboard.Api; + +/// One page of a catalog list. +/// Item type. +/// Items of the page. +/// 1-based page number. +/// Items per page. +/// Number of items in every page. +internal sealed record ApiPage(IReadOnlyList Items, int Page, int PageSize, int Total) +{ + public static ApiPage From(Page page) => new(page.Items, page.PageNumber, page.PageSize, page.Total); +} + +/// How the panel signs in (GET /auth); never lists users. +/// Access mode. +/// Host login page (host authentication). +/// Host logout page (host authentication). +/// Whether local requests enter without login. +internal sealed record AuthInfo(DashboardAccessMode Mode, string? LoginUrl, string? LogoutUrl, bool LocalRequests); + +/// Fixed login credentials. +/// User name. +/// Password. +internal sealed record LoginRequest(string? Name, string? Password); + +/// The current user (GET /me). +/// Recorded in the audit. +/// Display name. +/// Role (null: authenticated by the host without a dashboard role). +/// Every role the user has (cumulative). +/// How the user was identified. +internal sealed record MeResponse(string Subject, string Name, DashboardRole? Role, IReadOnlyList Roles, DashboardAccessMode Mode); + +/// Copies a version into a new draft. +/// Version to copy. +internal sealed record NewDraftRequest(int FromVersion); + +/// Next occurrences of a cron expression. +/// Cron expression (5 or 6 fields). +/// IANA time zone (default America/Sao_Paulo). +/// How many (1-20; default 5). +internal sealed record SchedulePreviewRequest(string Cron, string? TimeZone, int? Count); + +/// Run counters and lists of GET /overview (D-115). +/// When it was computed. +/// Runs created in the last 24 hours, by status. +/// Runs created in the 24 hours before, by status. +/// Runs pending, queued, running or cancelling (newest first, up to 50). +/// The 10 latest failed runs (last 7 days). +/// The 5 next schedule firings. +internal sealed record OverviewResponse( + DateTimeOffset GeneratedAt, + IReadOnlyDictionary Last24Hours, + IReadOnlyDictionary Previous24Hours, + IReadOnlyList InProgress, + IReadOnlyList RecentFailures, + IReadOnlyList UpcomingSchedules); + +/// A next schedule firing. +/// Schedule identifier. +/// Schedule name. +/// Scheduled pipeline. +/// Next firing (UTC). +internal sealed record UpcomingSchedule(ScheduleId Id, string Name, string PipelineKey, DateTimeOffset NextRunAt); + +/// Status of a secret declared by stages (never its value). +/// Secret name. +/// Stage versions that declare it (key@version). +/// Whether the secret provider returns a value. +internal sealed record SecretStatus(string Name, IReadOnlyList DeclaredBy, bool Resolved); + +/// Read-only settings (GET /admin/settings). +/// Global retention. +/// Limits of runs and uploads. +/// Access mode of the dashboard. +/// Versions of the components. +internal sealed record SettingsResponse(RetentionSettings Retention, LimitSettings Limits, AuthInfo Access, IReadOnlyDictionary Versions); + +/// Global retention in days. +/// File contents. +/// Runs. +/// Audit events. +internal sealed record RetentionSettings(double ArtifactDays, double RunDays, double AuditDays) +{ + public static RetentionSettings From(PsrRetentionOptions retention) => + new(retention.Artifacts.TotalDays, retention.Runs.TotalDays, retention.Audit.TotalDays); +} + +/// Limits of runs and uploads. +/// Largest input or output file. +/// Largest total of the files of a run. +/// Log tail kept per stream. +/// Default timeout of an attempt. +/// Default memory limit. +/// Python processes per instance. +/// Default Python version. +internal sealed record LimitSettings( + long MaxFileBytes, long MaxRunFilesBytes, int MaxLogBytes, TimeSpan DefaultTimeout, int DefaultMemoryLimitMb, int MaxConcurrentProcesses, string DefaultPythonVersion); + +/// Field types for the editor (GET /meta/field-types). +/// The 12 types and their widgets. +/// Which source types can feed which target types (representative types; see ). +/// Rules that depend on the declaration (enum values, table columns, file content types, list items). +internal sealed record FieldTypesResponse(IReadOnlyList Types, IReadOnlyDictionary> Compatibility, IReadOnlyList Rules); + +/// One field type. +/// Wire name (number, list...). +/// Whether it is a scalar. +/// Widget of the input form. +/// How outputs are shown. +/// Whether a default value can be declared. +internal sealed record FieldTypeInfo(string Name, bool Scalar, string InputWidget, string OutputWidget, bool AllowsDefault); + +/// Body of a run without files (JSON alternative to multipart). +/// Inputs. +/// Version to run (default: latest published). +/// Deduplicates repeated requests for 24 hours. +internal sealed record RunRequest(JsonObject? Inputs, int? Version, string? IdempotencyKey); + +/// Real-time update of a run (RunUpdated). +/// The run. +/// Event that changed it. +/// Step running or last finished. +internal sealed record RunUpdate(RunSummary Run, string EventType, string? CurrentStep); + +/// Real-time update of a step (StepUpdated). +/// Run. +/// Step key. +/// 1-based position. +/// Status of the step. +/// Attempt number. +/// Start time. +/// End time. +/// Error kind of a failed step. +internal sealed record StepUpdate( + RunId RunId, string StepKey, int Order, StepStatus Status, int Attempt, DateTimeOffset? StartedAt, DateTimeOffset? FinishedAt, ErrorKind? ErrorKind); diff --git a/src/PythonScriptRunner.Dashboard/Api/AuthEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/AuthEndpoints.cs new file mode 100644 index 0000000..7fb7f7f --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/AuthEndpoints.cs @@ -0,0 +1,131 @@ +using System.Globalization; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using PythonScriptRunner.Dashboard.Access; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Access endpoints (D-131): mode of the panel, fixed login and logout, current user. +internal static partial class AuthEndpoints +{ + /// Entity type of the login events in the audit. + public const string AuditEntityType = "User"; + + // Unknown users verify against this hash, so they take as long as wrong passwords (D-135). + private static readonly Lazy DummyHash = new(() => DashboardPasswords.Hash(Guid.NewGuid().ToString("N"))); + + public static void Map(RouteGroupBuilder api) + { + var auth = api.MapGroup("/auth").WithTags("Access"); + auth.MapGet("/", GetAuth).AllowAnonymous().WithName("GetAuth").Produces(); + auth.MapPost("/login", LoginAsync).AllowAnonymous().WithName("Login") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status401Unauthorized) + .ProducesProblem(StatusCodes.Status404NotFound).ProducesProblem(StatusCodes.Status429TooManyRequests); + auth.MapPost("/logout", LogoutAsync).RequireAuthorization(Policies.User).WithName("Logout") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status404NotFound); + api.MapGet("/me", GetMeAsync).RequireAuthorization(Policies.User).WithTags("Access").WithName("GetMe").Produces(); + } + + /// The access info of a request (also shown in /admin/settings). + public static AuthInfo Info(HttpContext http, DashboardAccessSettings settings, DashboardRoutes routes) + { + var returnUrl = Uri.EscapeDataString(http.Request.PathBase.Add(routes.Prefix).Value + "/"); + return new AuthInfo(settings.EffectiveMode, settings.LoginUrl?.Replace("{returnUrl}", returnUrl, StringComparison.Ordinal), settings.LogoutUrl, + settings.AllowLocalRequests); + } + + private static AuthInfo GetAuth(HttpContext http, DashboardAccessSettings settings, DashboardRoutes routes) => Info(http, settings, routes); + + private static async Task LoginAsync(LoginRequest? request, HttpContext http, DashboardAccessSettings settings, IAuditWriter audit, + TimeProvider time, ILoggerFactory loggers, CancellationToken cancellationToken) + { + if (settings.Mode != DashboardAccessMode.FixedLogin) + { + return ApiProblems.NotFound(http, "The dashboard does not use the fixed login."); + } + + var users = http.RequestServices.GetRequiredService(); + var throttle = http.RequestServices.GetRequiredService(); + var logger = loggers.CreateLogger(typeof(AuthEndpoints).FullName!); + var config = users.Settings; + var ip = http.Connection.RemoteIpAddress?.ToString() ?? "unknown"; + if (throttle.CountAttempt(ip, config.MaxAttemptsPerMinutePerIp) is { } wait) + { + http.Response.Headers.RetryAfter = Math.Max(1, (int)Math.Ceiling(wait.TotalSeconds)).ToString(CultureInfo.InvariantCulture); + LogTooManyAttempts(logger, ip); + return ApiProblems.Result(http, StatusCodes.Status429TooManyRequests, "TOO_MANY_ATTEMPTS", "Too many login attempts; try again later."); + } + + var name = request?.Name?.Trim() ?? string.Empty; + var account = users.Find(name); + var matches = DashboardPasswords.Verify(account?.PasswordHash ?? DummyHash.Value, request?.Password ?? string.Empty); + var locked = account is not null && throttle.IsLocked(account.Name); + if (account is not null && matches && !locked) + { + throttle.Succeeded(account.Name); + await http.SignInAsync(DashboardAccess.SessionScheme, SessionCookieOptions.CreatePrincipal(account), + SessionCookieOptions.CreateProperties(time.GetUtcNow())).ConfigureAwait(false); + await audit.WriteAsync(new AuditEntry(Actor.User($"login:{account.Name}"), AuditActions.LoginSucceeded, AuditEntityType, null, account.Name), + cancellationToken).ConfigureAwait(false); + return TypedResults.NoContent(); + } + + // Failures go to the log only (D-136); the answer never says whether the user exists or is locked (D-135). + LogLoginFailed(logger, name.Length <= 100 ? name : name[..100], ip, locked); + if (account is not null && !locked && throttle.Failed(account.Name, config.MaxFailedAttempts, config.LockoutDuration)) + { + LogUserLocked(logger, account.Name, config.LockoutDuration); + await audit.WriteAsync(new AuditEntry(Actor.User($"login:{account.Name}"), AuditActions.UserLocked, AuditEntityType, null, account.Name), + cancellationToken).ConfigureAwait(false); + } + + return ApiProblems.Result(http, StatusCodes.Status401Unauthorized, "LOGIN_FAILED", "Invalid user name or password."); + } + + private static async Task LogoutAsync(HttpContext http, DashboardAccessSettings settings, DashboardAccess access, IAuditWriter audit, + CancellationToken cancellationToken) + { + if (settings.Mode != DashboardAccessMode.FixedLogin) + { + return ApiProblems.NotFound(http, "The dashboard does not use the fixed login."); + } + + var user = await access.ResolveAsync(http).ConfigureAwait(false); + await http.SignOutAsync(DashboardAccess.SessionScheme).ConfigureAwait(false); + if (user is { Mode: DashboardAccessMode.FixedLogin }) + { + await audit.WriteAsync(new AuditEntry(user.Actor, AuditActions.LoggedOut, AuditEntityType, null, user.Name), cancellationToken).ConfigureAwait(false); + } + + return TypedResults.NoContent(); + } + + private static async Task GetMeAsync(HttpContext http, DashboardAccess access) + { + var user = (await access.ResolveAsync(http).ConfigureAwait(false))!; + return new MeResponse(user.Subject, user.Name, user.Role, user.Roles, user.Mode); + } + + [LoggerMessage(310, LogLevel.Warning, "Dashboard login failed for '{Name}' from {Ip} (locked: {Locked})")] + private static partial void LogLoginFailed(ILogger logger, string name, string ip, bool locked); + + [LoggerMessage(311, LogLevel.Warning, "Dashboard user '{Name}' locked for {Duration} after consecutive login failures")] + private static partial void LogUserLocked(ILogger logger, string name, TimeSpan duration); + + [LoggerMessage(312, LogLevel.Warning, "Too many dashboard login attempts from {Ip}")] + private static partial void LogTooManyAttempts(ILogger logger, string ip); +} + +/// Authorization policies of the dashboard (roles are cumulative; D-28). +internal static class Policies +{ + public const string User = DashboardRoleRequirement.UserPolicy; + public const string Reader = "psr:reader"; + public const string Executor = "psr:executor"; + public const string Author = "psr:author"; + public const string Admin = "psr:admin"; +} diff --git a/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs b/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs new file mode 100644 index 0000000..a34b592 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs @@ -0,0 +1,123 @@ +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.OpenApi; +using Microsoft.OpenApi; + +namespace PythonScriptRunner.Dashboard.Api; + +/// +/// The internal OpenAPI document of the dashboard (D-26): only its endpoints, and the JSON shapes written by custom converters +/// (identifiers, field declarations, binding sources) described, so the panel types generated from the snapshot are exact. +/// +internal static class DashboardOpenApi +{ + /// Document name (kept out of the host documents). + public const string Document = "psr-dashboard"; + + private static readonly string[] FieldTypes = ["number", "integer", "decimal", "string", "bool", "date", "datetime", "enum", "json", "table", "file", "list"]; + + public static void Configure(OpenApiOptions options) + { + options.ShouldInclude = description => description.GroupName == Document; + options.AddDocumentTransformer((document, _, _) => + { + document.Info.Title = "PythonScriptRunner Dashboard (internal API)"; + document.Info.Version = "v1"; + return Task.CompletedTask; + }); + options.AddSchemaTransformer((schema, context, _) => + { + var type = context.JsonTypeInfo.Type; + if (IsIdentifier(type)) + { + schema.Type = JsonSchemaType.String; + schema.Format = "uuid"; + } + else if (type == typeof(FieldDefinition)) + { + DescribeField(schema); + } + else if (ElementType(type) == typeof(FieldDefinition)) + { + // Lists of declarations (inputs, outputs) get no item schema from the generator: the converter hides the shape. + var item = new OpenApiSchema(); + DescribeField(item); + schema.Items = item; + } + else if (type == typeof(BindingSource)) + { + DescribeBindingSource(schema); + } + + return Task.CompletedTask; + }); + } + + /// Strongly typed identifiers (RunId, ArtifactId...) are written as their GUID text. + private static bool IsIdentifier(Type type) => + type is { IsValueType: true, Namespace: "PythonScriptRunner" } && type.Name.EndsWith("Id", StringComparison.Ordinal) + && type.GetProperty("Value")?.PropertyType == typeof(Guid); + + private static Type? ElementType(Type type) => + type.IsArray ? type.GetElementType() + : type.IsGenericType && type.GetGenericArguments() is [var item] && typeof(IEnumerable<>).MakeGenericType(item).IsAssignableFrom(type) ? item + : null; + + /// Field declaration in the manifest format (data mapping §2.1). + private static void DescribeField(OpenApiSchema schema) + { + schema.Type = JsonSchemaType.Object; + schema.Required = new HashSet { "name", "type" }; + schema.Properties = new Dictionary + { + ["name"] = new OpenApiSchema { Type = JsonSchemaType.String, Pattern = "^[a-z_][a-z0-9_]{0,63}$" }, + ["type"] = Names(FieldTypes), + ["itemType"] = Names(FieldTypes[..^1]), + ["required"] = new OpenApiSchema { Type = JsonSchemaType.Boolean, Default = JsonValue.Create(true) }, + ["default"] = new OpenApiSchema { Description = "Default value in wire format (not for files and tables)." }, + ["description"] = new OpenApiSchema { Type = JsonSchemaType.String, MaxLength = 500 }, + ["enumValues"] = Strings(), + ["contentTypes"] = Strings(), + ["columns"] = new OpenApiSchema + { + Type = JsonSchemaType.Array, + Items = new OpenApiSchema + { + Type = JsonSchemaType.Object, + Required = new HashSet { "name", "type" }, + Properties = new Dictionary + { + ["name"] = new OpenApiSchema { Type = JsonSchemaType.String }, + ["type"] = Names(FieldTypes[..7]), + }, + }, + }, + }; + } + + /// {"kind": "pipeline", "field"}, {"kind": "step", "step", "field"} or {"kind": "constant", "value"}. + private static void DescribeBindingSource(OpenApiSchema schema) + { + static OpenApiSchema Kind(string kind, params string[] fields) + { + var properties = new Dictionary { ["kind"] = Names([kind]) }; + foreach (var field in fields) + { + properties[field] = field == "value" ? new OpenApiSchema() : new OpenApiSchema { Type = JsonSchemaType.String }; + } + + return new OpenApiSchema + { + Type = JsonSchemaType.Object, + Required = new HashSet(["kind", .. fields.Where(f => f != "value")]), + Properties = properties, + }; + } + + schema.OneOf = [Kind("pipeline", "field"), Kind("step", "step", "field"), Kind("constant", "value")]; + } + + private static OpenApiSchema Names(IEnumerable names) => + new() { Type = JsonSchemaType.String, Enum = [.. names.Select(n => (JsonNode)JsonValue.Create(n))] }; + + private static OpenApiSchema Strings() => new() { Type = JsonSchemaType.Array, Items = new OpenApiSchema { Type = JsonSchemaType.String } }; +} diff --git a/src/PythonScriptRunner.Dashboard/Api/PipelineEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/PipelineEndpoints.cs new file mode 100644 index 0000000..4f94fd9 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/PipelineEndpoints.cs @@ -0,0 +1,310 @@ +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Microsoft.Net.Http.Headers; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Pipelines: catalog, drafts, validation, tests, publication, settings, export, import, runs and schedules (spec 007). +internal static class PipelineEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + var pipelines = api.MapGroup("/pipelines").WithTags("Pipelines"); + pipelines.MapGet("/", ListAsync).RequireAuthorization(Policies.Reader).WithName("ListPipelines").Produces>(); + pipelines.MapPost("/", CreateAsync).RequireAuthorization(Policies.Author).WithName("CreatePipeline") + .Produces(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status409Conflict); + pipelines.MapPost("/validate", ValidateAsync).RequireAuthorization(Policies.Author).WithName("ValidatePipeline").Produces(); + pipelines.MapGet("/{key}", GetAsync).RequireAuthorization(Policies.Reader).WithName("GetPipeline") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + pipelines.MapGet("/{key}/versions/{version:int}", GetVersionAsync).RequireAuthorization(Policies.Reader).WithName("GetPipelineVersion") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + pipelines.MapGet("/{key}/draft", GetDraftAsync).RequireAuthorization(Policies.Author).WithName("GetPipelineDraft") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + pipelines.MapPost("/{key}/draft", CreateDraftAsync).RequireAuthorization(Policies.Author).WithName("CreatePipelineDraft") + .Produces(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status409Conflict); + pipelines.MapPut("/{key}/draft", SaveDraftAsync).RequireAuthorization(Policies.Author).WithName("SavePipelineDraft") + .Produces().ProducesProblem(StatusCodes.Status412PreconditionFailed).ProducesProblem(StatusCodes.Status422UnprocessableEntity); + pipelines.MapDelete("/{key}/draft", DiscardDraftAsync).RequireAuthorization(Policies.Author).WithName("DiscardPipelineDraft") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status412PreconditionFailed); + pipelines.MapPost("/{key}/draft/test", TestDraftAsync).RequireAuthorization(Policies.Author).WithName("TestPipelineDraft") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status413PayloadTooLarge).ProducesProblem(StatusCodes.Status422UnprocessableEntity); + pipelines.MapPost("/{key}/draft/publish", PublishAsync).RequireAuthorization(Policies.Author).WithName("PublishPipeline") + .Produces().ProducesProblem(StatusCodes.Status412PreconditionFailed).ProducesProblem(StatusCodes.Status422UnprocessableEntity); + pipelines.MapPost("/{key}/versions/{version:int}/archive", ArchiveAsync).RequireAuthorization(Policies.Author).WithName("ArchivePipelineVersion") + .Produces().ProducesProblem(StatusCodes.Status409Conflict); + pipelines.MapDelete("/{key}", DeleteAsync).RequireAuthorization(Policies.Admin).WithName("DeletePipeline") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status409Conflict); + pipelines.MapPatch("/{key}/settings", UpdateSettingsAsync).RequireAuthorization(Policies.Author).WithName("UpdatePipelineSettings") + .Produces().ProducesProblem(StatusCodes.Status412PreconditionFailed); + pipelines.MapPatch("/{key}/retention", UpdateRetentionAsync).RequireAuthorization(Policies.Admin).WithName("UpdatePipelineRetention") + .Produces().ProducesProblem(StatusCodes.Status400BadRequest).ProducesProblem(StatusCodes.Status412PreconditionFailed); + pipelines.MapGet("/{key}/versions/{version:int}/export", ExportAsync).RequireAuthorization(Policies.Reader).WithName("ExportPipelineVersion") + .Produces(StatusCodes.Status200OK, contentType: "application/zip").ProducesProblem(StatusCodes.Status404NotFound); + pipelines.MapPost("/{key}/runs", RunAsync).RequireAuthorization(Policies.Executor).WithName("RunPipeline") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status409Conflict).ProducesProblem(StatusCodes.Status413PayloadTooLarge); + pipelines.MapPost("/{key}/schedules", CreateScheduleAsync).RequireAuthorization(Policies.Author).WithTags("Schedules").WithName("CreateSchedule") + .Produces(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status400BadRequest); + + api.MapPost("/import", ImportAsync).RequireAuthorization(Policies.Author).WithTags("Pipelines").WithName("Import") + .Produces().ProducesProblem(StatusCodes.Status400BadRequest).ProducesProblem(StatusCodes.Status413PayloadTooLarge); + } + + private static async Task> ListAsync( + string? search, VersionStatus? status, int? page, int? pageSize, ICatalogAdmin admin, CancellationToken cancellationToken) => + ApiPage.From(await admin.ListPipelinesAsync( + new CatalogQuery(search, page ?? 1, Math.Clamp(pageSize ?? 25, 1, 100), status), cancellationToken).ConfigureAwait(false)); + + private static async Task CreateAsync(NewCatalogItem pipeline, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + await admin.CreatePipelineAsync(pipeline, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + var detail = (await admin.GetPipelineAsync(pipeline.Key, cancellationToken).ConfigureAwait(false))!; + ETags.Set(http, detail.RowVersion); + return TypedResults.Created($"{http.Request.PathBase}{http.Request.Path}/{detail.Key}", detail); + } + + /// Validates a pipeline that is not saved (the visual editor checks while editing). + private static ValueTask ValidateAsync(PipelineDraftData data, IPipelineValidator validator, CancellationToken cancellationToken) => + validator.ValidateAsync(new PipelineDefinition + { + Key = "draft", + Version = 1, + Name = "draft", + Inputs = data.Inputs, + Steps = data.Steps, + Outputs = data.Outputs, + }, cancellationToken); + + private static async Task GetAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetPipelineAsync(key, cancellationToken).ConfigureAwait(false) is not { } pipeline) + { + return ApiProblems.NotFound(http, $"Pipeline '{key}' was not found."); + } + + ETags.Set(http, pipeline.RowVersion); + return TypedResults.Ok(pipeline); + } + + private static async Task GetVersionAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + await admin.GetPipelineVersionAsync(key, version, cancellationToken).ConfigureAwait(false) is { } detail + ? TypedResults.Ok(detail) + : ApiProblems.NotFound(http, $"Version {version} of pipeline '{key}' was not found."); + + private static async Task GetDraftAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } draft) + { + return ApiProblems.NotFound(http, $"Pipeline '{key}' has no draft."); + } + + ETags.Set(http, draft.RowVersion); + return TypedResults.Ok(draft); + } + + private static async Task CreateDraftAsync(string key, NewDraftRequest request, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + var draft = await admin.CreatePipelineDraftFromAsync(key, request.FromVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, draft.RowVersion); + return TypedResults.Created($"{http.Request.PathBase}{http.Request.Path}", draft); + } + + private static async Task SaveDraftAsync(string key, PipelineDraftData data, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Pipeline '{key}' has no draft."); + } + + var draft = await admin.SavePipelineDraftAsync(key, current.Version, data, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, draft.RowVersion); + return TypedResults.Ok(draft); + } + + private static async Task DiscardDraftAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Pipeline '{key}' has no draft."); + } + + await admin.DiscardPipelineDraftAsync(key, current.Version, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + return TypedResults.NoContent(); + } + + private static async Task TestDraftAsync( + string key, HttpContext http, ICatalogAdmin admin, IOptions options, CancellationToken cancellationToken) + { + var inputs = await RunInputs.ReadAsync(http, options.Value, cancellationToken).ConfigureAwait(false); + await using (inputs.ConfigureAwait(false)) + { + var draft = await admin.GetPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false); + var handle = await admin.EnqueuePipelineDraftTestAsync(key, inputs.Values(draft?.Data.Inputs), await http.ActorAsync().ConfigureAwait(false), + cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(handle.RunId), handle); + } + } + + private static async Task PublishAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetPipelineDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Pipeline '{key}' has no draft."); + } + + return TypedResults.Ok(await admin.PublishPipelineAsync(key, current.Version, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false)); + } + + private static async Task ArchiveAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + await admin.ArchivePipelineVersionAsync(key, version, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + + private static async Task DeleteAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + await admin.DeletePipelineAsync(key, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + return TypedResults.NoContent(); + } + + private static async Task UpdateSettingsAsync(string key, PipelineSettings settings, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + var pipeline = await admin.UpdatePipelineSettingsAsync(key, settings, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, pipeline.RowVersion); + return TypedResults.Ok(pipeline); + } + + private static async Task UpdateRetentionAsync( + string key, PipelineRetention retention, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + var pipeline = await admin.UpdatePipelineRetentionAsync(key, retention, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, pipeline.RowVersion); + return TypedResults.Ok(pipeline); + } + + private static async Task ExportAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetPipelineVersionAsync(key, version, cancellationToken).ConfigureAwait(false) is null) + { + return ApiProblems.NotFound(http, $"Version {version} of pipeline '{key}' was not found."); + } + + return await Endpoints.ZipAsync(zip => admin.ExportPipelineVersionAsync(key, version, zip, cancellationToken), $"{key}@{version}.zip") + .ConfigureAwait(false); + } + + private static async Task RunAsync( + string key, HttpContext http, IRunQueue queue, ICatalog catalog, IAuditWriter audit, IOptions options, + CancellationToken cancellationToken) + { + var inputs = await RunInputs.ReadAsync(http, options.Value, cancellationToken).ConfigureAwait(false); + await using (inputs.ConfigureAwait(false)) + { + var actor = await http.ActorAsync().ConfigureAwait(false); + PipelineDefinition? pipeline; + try + { + pipeline = await catalog.FindPipelineAsync(key, inputs.Version, cancellationToken).ConfigureAwait(false); + } + catch (VersionNotRunnableException) + { + pipeline = null; + } + + var handle = await queue.EnqueuePipelineAsync(key, inputs.Values(pipeline?.Inputs), new RunOptions + { + Version = inputs.Version, + IdempotencyKey = inputs.IdempotencyKey, + TriggeredBy = actor.Subject, + Trigger = RunTrigger.Dashboard, + }, cancellationToken).ConfigureAwait(false); + await audit.WriteAsync(new AuditEntry(actor, AuditActions.RunTriggered, "Run", handle.RunId.Value, key, new JsonObject + { + ["kind"] = nameof(RunKind.Pipeline), + ["version"] = inputs.Version, + ["deduplicated"] = handle.Deduplicated, + }), cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(handle.RunId), handle); + } + } + + private static async Task CreateScheduleAsync(string key, ScheduleData schedule, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + var created = await admin.CreateScheduleAsync(key, schedule, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + ETags.Set(http, created.RowVersion); + return TypedResults.Created($"{http.Request.PathBase}{http.RequestServices.GetRequiredService().Prefix}/api/schedules/{created.Id}", + created); + } + + /// Imports a zip in the folder catalog format: the raw body, or the first file of a multipart request. + private static async Task ImportAsync(HttpContext http, ICatalogAdmin admin, IOptions options, CancellationToken cancellationToken) + { + var limit = options.Value.MaxFileBytes; + var buffer = new MemoryStream(); + await using (buffer.ConfigureAwait(false)) + { + var source = http.Request.Body; + if (http.Request.HasFormContentType && MediaTypeHeaderValue.TryParse(http.Request.ContentType, out var mediaType) + && HeaderUtilities.RemoveQuotes(mediaType.Boundary).Value is { Length: > 0 } boundary) + { + var reader = new MultipartReader(boundary, http.Request.Body); + MultipartSection? section; + while ((section = await reader.ReadNextSectionAsync(cancellationToken).ConfigureAwait(false)) is not null + && !(ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var disposition) && disposition.IsFileDisposition())) + { + } + + source = section?.Body ?? throw new ArgumentException("The multipart request has no zip file."); + } + + var chunk = new byte[81920]; + int read; + while ((read = await source.ReadAsync(chunk, cancellationToken).ConfigureAwait(false)) > 0) + { + if (buffer.Length + read > limit) + { + throw new PayloadTooLargeException($"The archive exceeds {limit / (1024 * 1024)} MB."); + } + + await buffer.WriteAsync(chunk.AsMemory(0, read), cancellationToken).ConfigureAwait(false); + } + + buffer.Position = 0; + return TypedResults.Ok(await admin.ImportAsync(buffer, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false)); + } + } +} diff --git a/src/PythonScriptRunner.Dashboard/Api/RunEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/RunEndpoints.cs new file mode 100644 index 0000000..4938720 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/RunEndpoints.cs @@ -0,0 +1,158 @@ +using System.Globalization; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.EntityFrameworkCore; +using PythonScriptRunner.EntityFrameworkCore; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Runs, logs, files and the overview (spec 007). +internal static class RunEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + api.MapGet("/overview", OverviewAsync).RequireAuthorization(Policies.Reader).WithTags("Runs").WithName("GetOverview").Produces(); + + var runs = api.MapGroup("/runs").WithTags("Runs"); + runs.MapGet("/", ListAsync).RequireAuthorization(Policies.Reader).WithName("ListRuns") + .Produces>().ProducesProblem(StatusCodes.Status400BadRequest); + runs.MapGet("/{id}", GetAsync).RequireAuthorization(Policies.Reader).WithName("GetRun") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + runs.MapGet("/{id}/attempts/{attemptId}/logs/{stream}", LogAsync).RequireAuthorization(Policies.Reader).WithName("GetAttemptLog") + .Produces(StatusCodes.Status200OK, "text/plain").ProducesProblem(StatusCodes.Status404NotFound); + runs.MapPost("/{id}/cancel", CancelAsync).RequireAuthorization(Policies.Executor).WithName("CancelRun") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status409Conflict); + runs.MapPost("/{id}/reprocess", ReprocessAsync).RequireAuthorization(Policies.Executor).WithName("ReprocessRun") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status409Conflict); + + api.MapGet("/artifacts/{id}", DownloadAsync).RequireAuthorization(Policies.Reader).WithTags("Runs").WithName("DownloadArtifact") + .Produces(StatusCodes.Status200OK, contentType: "application/octet-stream") + .ProducesProblem(StatusCodes.Status404NotFound).ProducesProblem(StatusCodes.Status410Gone); + } + + private static Task OverviewAsync(OverviewQuery overview, CancellationToken cancellationToken) => overview.GetAsync(cancellationToken); + + private static Task> ListAsync([AsParameters] RunListQuery query, IRunManager runs, CancellationToken cancellationToken) => + runs.ListAsync(new RunQuery + { + PipelineKey = query.PipelineKey, + StageKey = query.StageKey, + Kind = query.Kind, + Status = query.Status, + Trigger = query.Trigger, + From = query.From, + To = query.To, + Cursor = query.Cursor, + Limit = query.Limit ?? 50, + }, cancellationToken); + + private static async Task GetAsync(RunId id, HttpContext http, IRunManager runs, CancellationToken cancellationToken) => + await runs.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } run + ? TypedResults.Ok(run) + : ApiProblems.NotFound(http, $"Run {id} was not found."); + + private static async Task LogAsync( + RunId id, AttemptId attemptId, string stream, HttpContext http, IRunManager runs, CancellationToken cancellationToken) + { + if (!Enum.TryParse(stream, ignoreCase: true, out var which) || !Enum.IsDefined(which) || int.TryParse(stream, out _) + || await runs.GetAttemptLogAsync(id, attemptId, which, cancellationToken).ConfigureAwait(false) is not { } log) + { + return ApiProblems.NotFound(http, $"No {stream} log for attempt {attemptId} of run {id}."); + } + + http.Response.Headers["X-PSR-Log-Truncated"] = log.Truncated ? "true" : "false"; + http.Response.Headers["X-PSR-Log-Original-Bytes"] = log.OriginalBytes.ToString(CultureInfo.InvariantCulture); + return TypedResults.Text(log.Content, "text/plain; charset=utf-8"); + } + + private static async Task CancelAsync(RunId id, HttpContext http, IRunManager runs, CancellationToken cancellationToken) + { + await runs.CancelAsync(id, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(id)); + } + + private static async Task ReprocessAsync(RunId id, HttpContext http, IRunManager runs, CancellationToken cancellationToken) + { + var run = await runs.ReprocessAsync(id, await http.ActorAsync().ConfigureAwait(false), ExecutionMode.Async, cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(id), new RunHandle(run.Id, run.Status, Deduplicated: false)); + } + + /// Streams a file as an attachment; the content is never rendered by the browser (sandboxed, no sniffing). + private static async Task DownloadAsync(ArtifactId id, HttpContext http, IArtifactStore store, CancellationToken cancellationToken) + { + if (await store.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } artifact) + { + return ApiProblems.NotFound(http, $"File {id} was not found."); + } + + var content = await store.OpenReadAsync(id, cancellationToken).ConfigureAwait(false); + http.Response.Headers.XContentTypeOptions = "nosniff"; + http.Response.Headers.ContentSecurityPolicy = "sandbox"; + return TypedResults.File(content, artifact.ContentType, artifact.FileName); + } +} + +/// Filters of GET /runs. +internal sealed record RunListQuery( + string? PipelineKey, string? StageKey, RunKind? Kind, RunStatus? Status, RunTrigger? Trigger, DateTimeOffset? From, DateTimeOffset? To, string? Cursor, int? Limit); + +/// Computes GET /overview (D-115). +internal sealed class OverviewQuery(IDbContextFactory factory, IRunManager runs, TimeProvider time) +{ + private static readonly RunStatus[] InProgressStatuses = [RunStatus.Pending, RunStatus.Queued, RunStatus.Running, RunStatus.Cancelling]; + + public async Task GetAsync(CancellationToken cancellationToken) + { + var now = time.GetUtcNow(); + var dayAgo = now.AddHours(-24); + var twoDaysAgo = now.AddHours(-48); + var db = await factory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); + await using (db.ConfigureAwait(false)) + { + var counts = await db.Runs.AsNoTracking() + .Where(r => r.CreatedAt >= twoDaysAgo && r.CreatedAt < now) + .GroupBy(r => new { Recent = r.CreatedAt >= dayAgo, r.Status }) + .Select(g => new { g.Key.Recent, g.Key.Status, Count = g.Count() }) + .ToListAsync(cancellationToken).ConfigureAwait(false); + var upcoming = await db.Schedules.AsNoTracking() + .Where(s => s.IsEnabled && s.NextRunAt != null) + .OrderBy(s => s.NextRunAt) + .Take(5) + .Join(db.Pipelines, s => s.PipelineId, p => p.Id, (s, p) => new { s.Id, s.Name, p.Key, NextRunAt = s.NextRunAt!.Value }) + .ToListAsync(cancellationToken).ConfigureAwait(false); + + // Runs created in this very instant are included; the window stays within the 90 days the history keeps. + var until = now.AddMinutes(1); + var inProgress = new List(); + foreach (var status in InProgressStatuses) + { + var page = await runs.ListAsync(new RunQuery { Status = status, From = until.AddDays(-90), To = until, Limit = 50 }, cancellationToken) + .ConfigureAwait(false); + inProgress.AddRange(page.Items); + } + + var failures = await runs.ListAsync(new RunQuery { Status = RunStatus.Failed, From = now.AddDays(-7), To = until, Limit = 10 }, cancellationToken) + .ConfigureAwait(false); + return new OverviewResponse( + now, + ByStatus(counts.Where(c => c.Recent).Select(c => (c.Status, c.Count))), + ByStatus(counts.Where(c => !c.Recent).Select(c => (c.Status, c.Count))), + [.. inProgress.OrderByDescending(r => r.CreatedAt).Take(50)], + failures.Items, + [.. upcoming.OrderBy(s => s.NextRunAt).Select(s => new UpcomingSchedule(new ScheduleId(s.Id), s.Name, s.Key, s.NextRunAt))]); + } + } + + /// Every status, zero when absent (the panel draws all counters). + private static Dictionary ByStatus(IEnumerable<(RunStatus Status, int Count)> counts) + { + var result = Enum.GetValues().ToDictionary(s => s, _ => 0); + foreach (var (status, count) in counts) + { + result[status] += count; + } + + return result; + } +} diff --git a/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs b/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs new file mode 100644 index 0000000..8a54a14 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs @@ -0,0 +1,219 @@ +using System.Globalization; +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Net.Http.Headers; + +namespace PythonScriptRunner.Dashboard.Api; + +/// +/// Inputs of a run or test sent by the panel: multipart/form-data with an inputs JSON part, one part per file +/// (named after its field) and optional version / idempotencyKey parts — or a JSON when +/// there are no files. Files are streamed to temporary files (≤ 50 MB each, ≤ 200 MB per request; 413 otherwise) and deleted +/// when this is disposed, after the run stored them. +/// +internal sealed class RunInputs : IAsyncDisposable +{ + private const int MaxTextPartBytes = 10 * 1024 * 1024; + + private readonly List _temporaryFiles = []; + private readonly Dictionary> _files = new(StringComparer.Ordinal); + private JsonObject _values = []; + + private RunInputs() + { + } + + /// Version to run, when given. + public int? Version { get; private set; } + + /// Idempotency key, when given. + public string? IdempotencyKey { get; private set; } + + /// Reads the request. Throws above the limits and for malformed parts. + public static async Task ReadAsync(HttpContext http, PythonScriptRunnerOptions limits, CancellationToken cancellationToken) + { + var inputs = new RunInputs(); + try + { + var request = http.Request; + if (request.HasFormContentType && MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaType) + && HeaderUtilities.RemoveQuotes(mediaType.Boundary).Value is { Length: > 0 } boundary) + { + if (http.Features.Get() is { IsReadOnly: false } bodySize) + { + // Room for the files plus the text parts and multipart headers. + bodySize.MaxRequestBodySize = limits.MaxRunFilesBytes + MaxTextPartBytes; + } + + await inputs.ReadMultipartAsync(new MultipartReader(boundary, request.Body), limits, Path.Combine(limits.WorkRoot, "uploads"), cancellationToken) + .ConfigureAwait(false); + } + else if (request.ContentLength is > 0 || request.Headers.TransferEncoding.Count > 0) + { + var body = await request.ReadFromJsonAsync(cancellationToken).ConfigureAwait(false); + inputs._values = body?.Inputs ?? []; + inputs.Version = body?.Version; + inputs.IdempotencyKey = body?.IdempotencyKey; + } + + return inputs; + } + catch + { + await inputs.DisposeAsync().ConfigureAwait(false); + throw; + } + } + + /// The inputs for IRunQueue / ICatalogAdmin: JSON values plus the files of each file field. + /// Declared inputs (to tell a single file from a list); null when unknown. + public Dictionary Values(IReadOnlyList? fields) + { + var values = _values.ToDictionary(p => p.Key, p => (object?)p.Value?.DeepClone(), StringComparer.Ordinal); + foreach (var (name, files) in _files) + { + var isList = fields?.FirstOrDefault(f => f.Name == name)?.Type.Kind == FieldKind.List; + values[name] = isList || files.Count > 1 ? files : files[0]; + } + + return values; + } + + public ValueTask DisposeAsync() + { + foreach (var file in _temporaryFiles) + { + try + { + File.Delete(file); + } + catch (IOException) + { + // Best effort: the work root janitor removes leftovers. + } + catch (UnauthorizedAccessException) + { + // Best effort. + } + } + + return ValueTask.CompletedTask; + } + + private async Task ReadMultipartAsync(MultipartReader reader, PythonScriptRunnerOptions limits, string folder, CancellationToken cancellationToken) + { + long total = 0; + while (await reader.ReadNextSectionAsync(cancellationToken).ConfigureAwait(false) is { } section) + { + if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var disposition) + || HeaderUtilities.RemoveQuotes(disposition.Name).Value is not { Length: > 0 } name) + { + throw new ArgumentException("Every multipart part needs a Content-Disposition with a name."); + } + + var fileName = HeaderUtilities.RemoveQuotes(disposition.FileNameStar.HasValue ? disposition.FileNameStar : disposition.FileName).Value; + if (fileName is null) + { + await ReadTextPartAsync(name, section, cancellationToken).ConfigureAwait(false); + continue; + } + + Directory.CreateDirectory(folder); + var path = Path.Combine(folder, Guid.NewGuid().ToString("N")); + _temporaryFiles.Add(path); + long size = 0; + var target = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 81920, useAsync: true); + await using (target.ConfigureAwait(false)) + { + var buffer = new byte[81920]; + int read; + while ((read = await section.Body.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0) + { + size += read; + total += read; + if (size > limits.MaxFileBytes) + { + throw new PayloadTooLargeException($"The file '{Path.GetFileName(fileName)}' of '{name}' exceeds {limits.MaxFileBytes / (1024 * 1024)} MB."); + } + + if (total > limits.MaxRunFilesBytes) + { + throw new PayloadTooLargeException($"The files of the request exceed {limits.MaxRunFilesBytes / (1024 * 1024)} MB."); + } + + await target.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); + } + } + + var safeName = Path.GetFileName(fileName.Replace('\\', '/')); + var contentType = section.ContentType is { Length: > 0 } type ? type : PsrFile.GuessContentType(safeName); + if (!_files.TryGetValue(name, out var list)) + { + _files[name] = list = []; + } + + list.Add(PsrFile.FromPath(path, contentType, safeName.Length > 0 ? safeName : "file")); + } + } + + private async Task ReadTextPartAsync(string name, MultipartSection section, CancellationToken cancellationToken) + { + using var buffer = new MemoryStream(); + var chunk = new byte[16384]; + int read; + while ((read = await section.Body.ReadAsync(chunk, cancellationToken).ConfigureAwait(false)) > 0) + { + if (buffer.Length + read > MaxTextPartBytes) + { + throw new PayloadTooLargeException($"The part '{name}' exceeds {MaxTextPartBytes / (1024 * 1024)} MB."); + } + + buffer.Write(chunk, 0, read); + } + + var text = System.Text.Encoding.UTF8.GetString(buffer.GetBuffer(), 0, (int)buffer.Length); + switch (name) + { + case "inputs": + _values = text.Length == 0 + ? [] + : JsonNode.Parse(text) as JsonObject ?? throw new ArgumentException("The 'inputs' part must be a JSON object."); + break; + case "version": + Version = text.Length == 0 ? null + : int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out var version) ? version + : throw new ArgumentException("The 'version' part must be an integer."); + break; + case "idempotencyKey": + IdempotencyKey = text.Length == 0 ? null : text; + break; + default: + throw new ArgumentException($"Unexpected text part '{name}': inputs go in the 'inputs' JSON part and files in parts named after their field."); + } + } +} + +/// Helpers shared by the endpoints. +internal static class Endpoints +{ + /// The user of an authorized request, as the author of changes. + public static async ValueTask ActorAsync(this HttpContext http) => + (await http.RequestServices.GetRequiredService().ResolveAsync(http).ConfigureAwait(false))?.Actor + ?? throw new InvalidOperationException("The dashboard user is resolved by the authorization of the endpoint."); + + /// Location of a run in the API. + public static string RunLocation(this HttpContext http, RunId runId) => + $"{http.Request.PathBase}{http.RequestServices.GetRequiredService().Prefix}/api/runs/{runId}"; + + /// Writes a zip produced by an export. + public static async Task ZipAsync(Func export, string fileName) + { + var zip = new MemoryStream(); + await export(zip).ConfigureAwait(false); + zip.Position = 0; + return TypedResults.File(zip, "application/zip", fileName); + } +} diff --git a/src/PythonScriptRunner.Dashboard/Api/ScheduleEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/ScheduleEndpoints.cs new file mode 100644 index 0000000..64cbb7f --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/ScheduleEndpoints.cs @@ -0,0 +1,91 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using PythonScriptRunner.EntityFrameworkCore; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Cron schedules of pipelines (spec 007; creation is under /pipelines/{key}/schedules). +internal static class ScheduleEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + var schedules = api.MapGroup("/schedules").WithTags("Schedules"); + schedules.MapGet("/", ListAsync).RequireAuthorization(Policies.Reader).WithName("ListSchedules").Produces>(); + schedules.MapPut("/{id}", UpdateAsync).RequireAuthorization(Policies.Author).WithName("UpdateSchedule") + .Produces().ProducesProblem(StatusCodes.Status400BadRequest).ProducesProblem(StatusCodes.Status412PreconditionFailed); + schedules.MapDelete("/{id}", DeleteAsync).RequireAuthorization(Policies.Author).WithName("DeleteSchedule") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status412PreconditionFailed); + schedules.MapPost("/{id}/enable", (ScheduleId id, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + SetEnabledAsync(id, true, http, admin, cancellationToken)) + .RequireAuthorization(Policies.Author).WithName("EnableSchedule").Produces(); + schedules.MapPost("/{id}/disable", (ScheduleId id, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + SetEnabledAsync(id, false, http, admin, cancellationToken)) + .RequireAuthorization(Policies.Author).WithName("DisableSchedule").Produces(); + schedules.MapPost("/preview", Preview).RequireAuthorization(Policies.Reader).WithName("PreviewSchedule") + .Produces>().ProducesProblem(StatusCodes.Status400BadRequest); + } + + private static async Task> ListAsync( + string? pipelineKey, int? page, int? pageSize, ICatalogAdmin admin, CancellationToken cancellationToken) => + ApiPage.From(await admin.ListSchedulesAsync( + new ScheduleQuery(pipelineKey, page ?? 1, Math.Clamp(pageSize ?? 25, 1, 100)), cancellationToken).ConfigureAwait(false)); + + private static async Task UpdateAsync(ScheduleId id, ScheduleData schedule, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + var updated = await admin.UpdateScheduleAsync(id, schedule, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, updated.RowVersion); + return TypedResults.Ok(updated); + } + + private static async Task DeleteAsync(ScheduleId id, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + var current = await admin.GetScheduleAsync(id, cancellationToken).ConfigureAwait(false); + if (current is null) + { + return ApiProblems.NotFound(http, $"Schedule {id} was not found."); + } + + if (current.RowVersion != rowVersion) + { + throw new ConcurrencyConflictException($"Schedule '{current.Data.Name}' was changed by someone else.", current); + } + + await admin.DeleteScheduleAsync(id, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + return TypedResults.NoContent(); + } + + /// Enables or disables; If-Match is optional here (a switch, not an edit). + private static async Task SetEnabledAsync(ScheduleId id, bool enabled, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + var rowVersion = ETags.IfMatch(http); + if (rowVersion is null) + { + if (await admin.GetScheduleAsync(id, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Schedule {id} was not found."); + } + + rowVersion = current.RowVersion; + } + + var schedule = await admin.SetScheduleEnabledAsync(id, enabled, rowVersion.Value, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, schedule.RowVersion); + return TypedResults.Ok(schedule); + } + + private static IReadOnlyList Preview(SchedulePreviewRequest request, TimeProvider time) => + SchedulePreview.Next(request.Cron, request.TimeZone ?? ScheduleData.DefaultTimeZone, time.GetUtcNow(), request.Count ?? 5); +} diff --git a/src/PythonScriptRunner.Dashboard/Api/StageEndpoints.cs b/src/PythonScriptRunner.Dashboard/Api/StageEndpoints.cs new file mode 100644 index 0000000..c8bdd6d --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Api/StageEndpoints.cs @@ -0,0 +1,246 @@ +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Options; + +namespace PythonScriptRunner.Dashboard.Api; + +/// Stages: catalog, drafts, tests, publication, archiving, export and runs (spec 007). +internal static class StageEndpoints +{ + /// Longest publication (uv lock) before the request gives up; the draft stays intact. + public static readonly TimeSpan PublishTimeout = TimeSpan.FromMinutes(2); + + public static void Map(RouteGroupBuilder api) + { + var stages = api.MapGroup("/stages").WithTags("Stages"); + stages.MapGet("/", ListAsync).RequireAuthorization(Policies.Reader).WithName("ListStages").Produces>(); + stages.MapPost("/", CreateAsync).RequireAuthorization(Policies.Author).WithName("CreateStage") + .Produces(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status409Conflict); + stages.MapGet("/{key}", GetAsync).RequireAuthorization(Policies.Reader).WithName("GetStage") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + stages.MapPatch("/{key}", UpdateAsync).RequireAuthorization(Policies.Author).WithName("UpdateStage") + .Produces().ProducesProblem(StatusCodes.Status412PreconditionFailed).ProducesProblem(StatusCodes.Status428PreconditionRequired); + stages.MapGet("/{key}/versions/{version:int}", GetVersionAsync).RequireAuthorization(Policies.Reader).WithName("GetStageVersion") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + stages.MapGet("/{key}/draft", GetDraftAsync).RequireAuthorization(Policies.Author).WithName("GetStageDraft") + .Produces().ProducesProblem(StatusCodes.Status404NotFound); + stages.MapPost("/{key}/draft", CreateDraftAsync).RequireAuthorization(Policies.Author).WithName("CreateStageDraft") + .Produces(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status409Conflict); + stages.MapPut("/{key}/draft", SaveDraftAsync).RequireAuthorization(Policies.Author).WithName("SaveStageDraft") + .Produces().ProducesProblem(StatusCodes.Status400BadRequest).ProducesProblem(StatusCodes.Status412PreconditionFailed); + stages.MapDelete("/{key}/draft", DiscardDraftAsync).RequireAuthorization(Policies.Author).WithName("DiscardStageDraft") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status412PreconditionFailed); + stages.MapPost("/{key}/draft/validate", ValidateDraftAsync).RequireAuthorization(Policies.Author).WithName("ValidateStageDraft") + .Produces(); + stages.MapPost("/{key}/draft/test", TestDraftAsync).RequireAuthorization(Policies.Author).WithName("TestStageDraft") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status413PayloadTooLarge).ProducesProblem(StatusCodes.Status422UnprocessableEntity); + stages.MapPost("/{key}/draft/publish", PublishAsync).RequireAuthorization(Policies.Author).WithName("PublishStage") + .Produces().ProducesProblem(StatusCodes.Status412PreconditionFailed) + .ProducesProblem(StatusCodes.Status422UnprocessableEntity).ProducesProblem(StatusCodes.Status504GatewayTimeout); + stages.MapPost("/{key}/versions/{version:int}/archive", ArchiveAsync).RequireAuthorization(Policies.Author).WithName("ArchiveStageVersion") + .Produces().ProducesProblem(StatusCodes.Status409Conflict); + stages.MapDelete("/{key}", DeleteAsync).RequireAuthorization(Policies.Admin).WithName("DeleteStage") + .Produces(StatusCodes.Status204NoContent).ProducesProblem(StatusCodes.Status409Conflict); + stages.MapGet("/{key}/versions/{version:int}/export", ExportAsync).RequireAuthorization(Policies.Reader).WithName("ExportStageVersion") + .Produces(StatusCodes.Status200OK, contentType: "application/zip").ProducesProblem(StatusCodes.Status404NotFound); + stages.MapPost("/{key}/runs", RunAsync).RequireAuthorization(Policies.Executor).WithName("RunStage") + .Produces(StatusCodes.Status202Accepted).ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status409Conflict).ProducesProblem(StatusCodes.Status413PayloadTooLarge); + } + + private static async Task> ListAsync( + string? search, VersionStatus? status, int? page, int? pageSize, ICatalogAdmin admin, CancellationToken cancellationToken) => + ApiPage.From(await admin.ListStagesAsync( + new CatalogQuery(search, page ?? 1, Math.Clamp(pageSize ?? 25, 1, 100), status), cancellationToken).ConfigureAwait(false)); + + private static async Task CreateAsync(NewCatalogItem stage, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + await admin.CreateStageAsync(stage, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + var detail = (await admin.GetStageAsync(stage.Key, cancellationToken).ConfigureAwait(false))!; + ETags.Set(http, detail.RowVersion); + return TypedResults.Created($"{http.Request.PathBase}{http.Request.Path}/{detail.Key}", detail); + } + + private static async Task GetAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetStageAsync(key, cancellationToken).ConfigureAwait(false) is not { } stage) + { + return ApiProblems.NotFound(http, $"Stage '{key}' was not found."); + } + + ETags.Set(http, stage.RowVersion); + return TypedResults.Ok(stage); + } + + private static async Task UpdateAsync(string key, CatalogItemChanges changes, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + var stage = await admin.UpdateStageAsync(key, changes, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + ETags.Set(http, stage.RowVersion); + return TypedResults.Ok(stage); + } + + private static async Task GetVersionAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + await admin.GetStageVersionAsync(key, version, cancellationToken).ConfigureAwait(false) is { } detail + ? TypedResults.Ok(detail) + : ApiProblems.NotFound(http, $"Version {version} of stage '{key}' was not found."); + + private static async Task GetDraftAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetStageDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } draft) + { + return ApiProblems.NotFound(http, $"Stage '{key}' has no draft."); + } + + ETags.Set(http, draft.RowVersion); + return TypedResults.Ok(draft); + } + + private static async Task CreateDraftAsync(string key, NewDraftRequest request, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + var draft = await admin.CreateStageDraftFromAsync(key, request.FromVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, draft.RowVersion); + return TypedResults.Created($"{http.Request.PathBase}{http.Request.Path}", draft); + } + + private static async Task SaveDraftAsync(string key, StageDraftData data, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetStageDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Stage '{key}' has no draft."); + } + + var draft = await admin.SaveStageDraftAsync(key, current.Version, data, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken) + .ConfigureAwait(false); + ETags.Set(http, draft.RowVersion); + return TypedResults.Ok(draft); + } + + private static async Task DiscardDraftAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetStageDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Stage '{key}' has no draft."); + } + + await admin.DiscardStageDraftAsync(key, current.Version, rowVersion, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + return TypedResults.NoContent(); + } + + private static Task ValidateDraftAsync(string key, ICatalogAdmin admin, CancellationToken cancellationToken) => + admin.ValidateStageDraftAsync(key, cancellationToken); + + private static async Task TestDraftAsync( + string key, HttpContext http, ICatalogAdmin admin, IOptions options, CancellationToken cancellationToken) + { + var inputs = await RunInputs.ReadAsync(http, options.Value, cancellationToken).ConfigureAwait(false); + await using (inputs.ConfigureAwait(false)) + { + var draft = await admin.GetStageDraftAsync(key, cancellationToken).ConfigureAwait(false); + var handle = await admin.EnqueueStageDraftTestAsync(key, inputs.Values(draft?.Data.Inputs), await http.ActorAsync().ConfigureAwait(false), + cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(handle.RunId), handle); + } + } + + private static async Task PublishAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (ETags.IfMatch(http) is not { } rowVersion) + { + return ETags.Required(http); + } + + if (await admin.GetStageDraftAsync(key, cancellationToken).ConfigureAwait(false) is not { } current) + { + return ApiProblems.NotFound(http, $"Stage '{key}' has no draft."); + } + + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(PublishTimeout); + try + { + return TypedResults.Ok(await admin.PublishStageAsync(key, current.Version, rowVersion, await http.ActorAsync().ConfigureAwait(false), timeout.Token) + .ConfigureAwait(false)); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + return ApiProblems.Result(http, StatusCodes.Status504GatewayTimeout, "PUBLISH_TIMEOUT", "The publication took too long.", + $"Resolving the dependencies did not finish in {PublishTimeout.TotalMinutes:0} minutes; the draft was kept."); + } + } + + private static async Task ArchiveAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) => + await admin.ArchiveStageVersionAsync(key, version, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + + private static async Task DeleteAsync(string key, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + await admin.DeleteStageAsync(key, await http.ActorAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false); + return TypedResults.NoContent(); + } + + private static async Task ExportAsync(string key, int version, HttpContext http, ICatalogAdmin admin, CancellationToken cancellationToken) + { + if (await admin.GetStageVersionAsync(key, version, cancellationToken).ConfigureAwait(false) is null) + { + return ApiProblems.NotFound(http, $"Version {version} of stage '{key}' was not found."); + } + + return await Endpoints.ZipAsync(zip => admin.ExportStageVersionAsync(key, version, zip, cancellationToken), $"{key}@{version}.zip").ConfigureAwait(false); + } + + private static async Task RunAsync( + string key, HttpContext http, IRunQueue queue, ICatalog catalog, IAuditWriter audit, IOptions options, + CancellationToken cancellationToken) + { + var inputs = await RunInputs.ReadAsync(http, options.Value, cancellationToken).ConfigureAwait(false); + await using (inputs.ConfigureAwait(false)) + { + var actor = await http.ActorAsync().ConfigureAwait(false); + var stage = await FindAsync(catalog, key, inputs.Version, cancellationToken).ConfigureAwait(false); + var handle = await queue.EnqueueStageAsync(key, inputs.Values(stage?.Inputs), new RunOptions + { + Version = inputs.Version, + IdempotencyKey = inputs.IdempotencyKey, + TriggeredBy = actor.Subject, + Trigger = RunTrigger.Dashboard, + }, cancellationToken).ConfigureAwait(false); + await audit.WriteAsync(new AuditEntry(actor, AuditActions.RunTriggered, "Run", handle.RunId.Value, key, new JsonObject + { + ["kind"] = nameof(RunKind.Stage), + ["version"] = inputs.Version, + ["deduplicated"] = handle.Deduplicated, + }), cancellationToken).ConfigureAwait(false); + return TypedResults.Accepted(http.RunLocation(handle.RunId), handle); + } + } + + private static async Task FindAsync(ICatalog catalog, string key, int? version, CancellationToken cancellationToken) + { + try + { + return await catalog.FindStageAsync(key, version, cancellationToken).ConfigureAwait(false); + } + catch (VersionNotRunnableException) + { + return null; + } + } +} diff --git a/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs b/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs new file mode 100644 index 0000000..b997e90 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs @@ -0,0 +1,89 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.OpenApi; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi; +using PythonScriptRunner; +using PythonScriptRunner.Dashboard; +using PythonScriptRunner.Dashboard.Access; +using PythonScriptRunner.Dashboard.Api; +using PythonScriptRunner.Dashboard.Realtime; +using PythonScriptRunner.Dashboard.Spa; + +namespace Microsoft.AspNetCore.Builder; + +/// Maps the embeddable dashboard of PythonScriptRunner. +public static class DashboardEndpointRouteBuilderExtensions +{ + /// + /// Maps the dashboard under : API in {prefix}/api, real-time updates in + /// {prefix}/hubs/runs and the panel in {prefix}/ (spec 007). Without this call there is no dashboard. + /// + /// The application. + /// Path of the dashboard (e.g. /psr or /admin/python). + /// A builder to add conventions to every dashboard endpoint (e.g. RequireHost). + /// AddDashboard was not called, or the dashboard is already mapped. + public static IEndpointConventionBuilder MapPythonScriptRunnerDashboard(this IEndpointRouteBuilder endpoints, string prefix = "/psr") + { + ArgumentNullException.ThrowIfNull(endpoints); + ArgumentException.ThrowIfNullOrWhiteSpace(prefix); + var normalized = "/" + prefix.Trim().Trim('/'); + if (normalized == "/" || normalized.Contains("//", StringComparison.Ordinal) || normalized.Contains('{', StringComparison.Ordinal)) + { + throw new ArgumentException("The prefix must be a path below the root, such as /psr.", nameof(prefix)); + } + + var services = endpoints.ServiceProvider; + if (services.GetService() is null) + { + throw new PsrConfigurationException("Call AddDashboard(...) on the PythonScriptRunner builder before MapPythonScriptRunnerDashboard."); + } + + var routes = services.GetRequiredService(); + if (routes.Mapped) + { + throw new PsrConfigurationException("The dashboard is already mapped; call MapPythonScriptRunnerDashboard once."); + } + + routes.Prefix = normalized; + routes.Mapped = true; + + var root = endpoints.MapGroup(normalized); + var api = root.MapGroup("/api") + .WithMetadata(new DashboardEndpointMetadata()) + .AddEndpointFilter() + .WithGroupName(DashboardOpenApi.Document); + AuthEndpoints.Map(api); + StageEndpoints.Map(api); + PipelineEndpoints.Map(api); + RunEndpoints.Map(api); + ScheduleEndpoints.Map(api); + AdminEndpoints.Map(api); + + if (services.GetRequiredService().IsDevelopment()) + { + // Internal document, only in Development (D-26); its snapshot feeds the types of the panel. + root.MapGet("/api/openapi/v1.json", async (HttpContext http, CancellationToken cancellationToken) => + { + var provider = http.RequestServices.GetRequiredKeyedService(DashboardOpenApi.Document); + var document = await provider.GetOpenApiDocumentAsync(cancellationToken).ConfigureAwait(false); + return TypedResults.Text(await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, cancellationToken).ConfigureAwait(false), + "application/json; charset=utf-8"); + }) + .AllowAnonymous() + .ExcludeFromDescription(); + } + + // Unknown API paths are 404 problems, not the panel page. + root.MapMethods("/api/{**path}", ["GET", "POST", "PUT", "PATCH", "DELETE"], (HttpContext http) => ApiProblems.NotFound(http, "Unknown dashboard API path.")) + .AllowAnonymous() + .ExcludeFromDescription(); + + root.MapHub("/hubs/runs") + .RequireAuthorization(Policies.Reader) + .WithMetadata(new DashboardEndpointMetadata()); + DashboardSpa.Map(root); + return root; + } +} diff --git a/src/PythonScriptRunner.Dashboard/DashboardOptions.cs b/src/PythonScriptRunner.Dashboard/DashboardOptions.cs new file mode 100644 index 0000000..7758a9b --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/DashboardOptions.cs @@ -0,0 +1,226 @@ +using System.Text.Json.Serialization; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; + +namespace PythonScriptRunner.Dashboard; + +/// Cumulative roles of the dashboard (D-28): each one can do everything the previous ones can. +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum DashboardRole +{ + /// Sees the catalog, runs, logs, files and schedules. + Reader, + + /// + starts, cancels and reprocesses runs. + Executor, + + /// + edits, tests, publishes and archives stages and pipelines, imports, schedules. + Author, + + /// + deletes, shortens retention, sees secret status and the audit. + Admin, +} + +/// How the dashboard identifies its users (ADR-0021). +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum DashboardAccessMode +{ + /// Users configured for the dashboard, with its own login and session cookie. + FixedLogin, + + /// An decides each request. + Filter, + + /// The host authenticates; its policies give the roles. + Host, + + /// Only local requests (Development), as Admin. + Localhost, +} + +/// A user allowed by a custom filter. +/// Name shown in the dashboard and recorded in the audit. +/// Role of the user. +public sealed record DashboardUser(string Name, DashboardRole Role); + +/// Decides who may use the dashboard, like Hangfire's filters (D-123). +public interface IDashboardAuthorizationFilter +{ + /// Identifies the user of a request. + /// The request. + /// The user and role, or null to deny (401). + ValueTask AuthorizeAsync(HttpContext context); +} + +/// Settings of the dashboard (spec 007). Choose exactly one main access mode, and optionally local requests in Development. +public sealed class DashboardOptions +{ + internal DashboardAccessMode? Mode { get; private set; } + + internal FixedLoginOptions FixedLogin { get; } = new(); + + internal HostAuthenticationOptions HostAuthentication { get; } = new(); + + internal Type? FilterType { get; private set; } + + internal int MainModes { get; private set; } + + /// + /// Lets local requests in as Admin without login (D-134). Allowed only in Development: elsewhere startup fails. Alone, it is + /// the access mode; with a main mode, local requests skip it. + /// + public bool AllowLocalRequests { get; set; } + + /// Fixed login: users and roles from configuration or code, login screen of the panel, session cookie (D-119..D-121). + /// Users, session and lockout settings. + public DashboardOptions UseFixedLogin(Action? configure = null) + { + SetMode(DashboardAccessMode.FixedLogin); + configure?.Invoke(FixedLogin); + return this; + } + + /// Custom filter (resolved from DI, registered as scoped when missing) decides each request (D-123). + /// Filter type. + public DashboardOptions UseAuthorizationFilter() + where TFilter : class, IDashboardAuthorizationFilter + { + SetMode(DashboardAccessMode.Filter); + FilterType = typeof(TFilter); + return this; + } + + /// The host authenticates (cookie, OIDC, JWT...) and its policies give the four roles. + /// Policies of the roles and the host login/logout URLs. + public DashboardOptions UseHostAuthentication(Action configure) + { + ArgumentNullException.ThrowIfNull(configure); + SetMode(DashboardAccessMode.Host); + configure(HostAuthentication); + return this; + } + + private void SetMode(DashboardAccessMode mode) + { + Mode = mode; + MainModes++; + } +} + +/// Fixed login settings. +public sealed class FixedLoginOptions +{ + /// Default configuration section of the users. + public const string DefaultSection = "PythonScriptRunner:Dashboard:Login"; + + internal string? ConfigurationSection { get; private set; } + + /// Users defined in code (ignored when is used). + public IList Users { get; } = []; + + /// Session ends after this time without use. Default: 8 hours (D-127). + public TimeSpan SlidingExpiration { get; set; } = TimeSpan.FromHours(8); + + /// Session ends this long after the login, even when used. Default: 24 hours (D-127). + public TimeSpan AbsoluteExpiration { get; set; } = TimeSpan.FromHours(24); + + /// Consecutive failures that lock a user. Default: 5 (D-126). + public int MaxFailedAttempts { get; set; } = 5; + + /// How long a user stays locked. Default: 15 minutes (D-126). + public TimeSpan LockoutDuration { get; set; } = TimeSpan.FromMinutes(15); + + /// Login attempts per minute from one IP before 429. Default: 10 (D-126). + public int MaxAttemptsPerMinutePerIp { get; set; } = 10; + + /// Keeps the keys that protect the session cookie in psr.dashboard_keys (D-132). false uses the host's Data Protection. + public bool PersistKeysInDatabase { get; set; } = true; + + /// Reads the users (and these settings) from a configuration section, reloaded when it changes (D-128). + /// Section path. Default: . + public FixedLoginOptions BindConfiguration(string section = DefaultSection) + { + ArgumentException.ThrowIfNullOrWhiteSpace(section); + ConfigurationSection = section; + return this; + } +} + +/// A user of the fixed login. +public sealed class FixedLoginUser +{ + /// User name (1-100 chars, compared ignoring case). + public string Name { get; set; } = string.Empty; + + /// Role. + public DashboardRole Role { get; set; } + + /// Password hash generated by psr hash-password or ; never the password. + public string PasswordHash { get; set; } = string.Empty; +} + +/// Host authentication settings: one policy per role and the host login/logout pages. +public sealed class HostAuthenticationOptions +{ + /// Policy of the Reader role (e.g. p => p.RequireRole("psr-leitor")). + public Action? Reader { get; set; } + + /// Policy of the Executor role. + public Action? Executor { get; set; } + + /// Policy of the Author role. + public Action? Author { get; set; } + + /// Policy of the Admin role. + public Action? Admin { get; set; } + + /// Login page of the host; {returnUrl} is replaced by the panel URL. + public string? LoginUrl { get; set; } + + /// Logout page of the host. + public string? LogoutUrl { get; set; } + + /// Authentication schemes used to read the user (default: the host's default scheme). + public IList AuthenticationSchemes { get; } = []; +} + +/// Password hashes of the fixed login (PBKDF2 in the format of ASP.NET Identity v3; D-121). +public static class DashboardPasswords +{ + private static readonly PasswordHasher Hasher = new(); + + /// Hashes a password for . + /// The password. + public static string Hash(string password) + { + ArgumentException.ThrowIfNullOrEmpty(password); + return Hasher.HashPassword(null!, password); + } + + /// Whether matches . + /// Stored hash. + /// Typed password. + public static bool Verify(string hash, string password) => + Hasher.VerifyHashedPassword(null!, hash, password) != PasswordVerificationResult.Failed; + + /// Whether is a hash of this format (Identity v3), not a plain password. + /// The value. + public static bool IsHash(string hash) + { + if (string.IsNullOrWhiteSpace(hash)) + { + return false; + } + + try + { + var bytes = Convert.FromBase64String(hash); + return bytes.Length > 13 && bytes[0] == 0x01; + } + catch (FormatException) + { + return false; + } + } +} diff --git a/src/PythonScriptRunner.Dashboard/DashboardServiceCollectionExtensions.cs b/src/PythonScriptRunner.Dashboard/DashboardServiceCollectionExtensions.cs new file mode 100644 index 0000000..e0db1bc --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/DashboardServiceCollectionExtensions.cs @@ -0,0 +1,214 @@ +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization.Policy; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using PythonScriptRunner; +using PythonScriptRunner.Dashboard; +using PythonScriptRunner.Dashboard.Access; +using PythonScriptRunner.Dashboard.Api; +using PythonScriptRunner.Dashboard.Realtime; + +namespace Microsoft.Extensions.DependencyInjection; + +/// Registers the embeddable dashboard of PythonScriptRunner. +public static class DashboardServiceCollectionExtensions +{ + /// + /// Adds the dashboard (spec 007; requires AddEntityFrameworkCore and AddRabbitMq): internal API, real-time + /// updates and the panel, protected by exactly one access mode — fixed login, custom filter or host authentication — and, + /// in Development only, local requests (ADR-0021). Map it with app.MapPythonScriptRunnerDashboard("/psr"). + /// + /// Builder returned by AddPythonScriptRunner. + /// Access mode. + /// No access mode, more than one, or an incomplete one. + public static IPythonScriptRunnerBuilder AddDashboard(this IPythonScriptRunnerBuilder builder, Action configure) + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configure); + var services = builder.Services; + if (!services.Any(d => d.ServiceType == typeof(IRunQueue))) + { + throw new PsrConfigurationException( + "AddDashboard requires the queue: call AddEntityFrameworkCore(...) and AddRabbitMq(...) first (tests and runs go to the workers)."); + } + + if (services.Any(d => d.ServiceType == typeof(DashboardAccessSettings))) + { + throw new PsrConfigurationException("AddDashboard was called more than once."); + } + + var options = new DashboardOptions(); + configure(options); + var settings = BuildSettings(options); + + services.AddSingleton(settings); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); + var authorization = services.AddAuthorizationBuilder() + .AddPolicy(Policies.User, p => p.AddRequirements(new DashboardRoleRequirement(null))); + foreach (var role in Enum.GetValues()) + { + authorization.AddPolicy(DashboardRoleRequirement.PolicyName(role), p => p.AddRequirements(new DashboardRoleRequirement(role))); + } + + DecorateAuthorizationResults(services); + switch (settings.Mode) + { + case DashboardAccessMode.FixedLogin: + AddFixedLogin(services, options.FixedLogin); + break; + case DashboardAccessMode.Filter: + services.TryAddScoped(settings.FilterType!); + break; + } + + services.AddSignalR(); + services.AddOpenApi(DashboardOpenApi.Document, DashboardOpenApi.Configure); + builder.AddRunEventHandler(s => s.PerInstance("dashboard")); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); + return builder; + } + + private static DashboardAccessSettings BuildSettings(DashboardOptions options) + { + var errors = new List(); + if (options.MainModes > 1) + { + errors.Add("choose only one access mode (UseFixedLogin, UseAuthorizationFilter or UseHostAuthentication)"); + } + + if (options.Mode is null && !options.AllowLocalRequests) + { + errors.Add("no access mode: call UseFixedLogin, UseAuthorizationFilter or UseHostAuthentication (or allow local requests in Development)"); + } + + var policies = new Dictionary(); + var host = options.HostAuthentication; + if (options.Mode == DashboardAccessMode.Host) + { + foreach (var role in Enum.GetValues()) + { + var configure = role switch + { + DashboardRole.Reader => host.Reader, + DashboardRole.Executor => host.Executor, + DashboardRole.Author => host.Author, + _ => host.Admin, + }; + if (configure is null) + { + errors.Add($"host authentication needs the policy of every role; {role} is missing"); + continue; + } + + try + { + var policy = new AuthorizationPolicyBuilder(); + configure(policy); + policies[role] = policy.Build(); + } + catch (InvalidOperationException ex) + { + errors.Add($"the policy of {role} is invalid: {ex.Message}"); + } + } + } + + if (options.Mode == DashboardAccessMode.FixedLogin && options.FixedLogin.ConfigurationSection is null) + { + // Users in code are checked now; users from configuration are checked at startup. + var problems = FixedLoginUsers.Errors(FixedLoginSettingsFrom(options.FixedLogin)); + errors.AddRange(problems.Select(p => $"fixed login: {p}")); + } + + return errors.Count > 0 + ? throw new PsrConfigurationException($"Invalid dashboard configuration: {string.Join("; ", errors)}.") + : new DashboardAccessSettings(options.Mode, options.AllowLocalRequests, options.FilterType, policies, [.. host.AuthenticationSchemes], + host.LoginUrl, host.LogoutUrl); + } + + private static void AddFixedLogin(IServiceCollection services, FixedLoginOptions login) + { + var settings = services.AddOptions().Configure(s => Copy(login, s, users: login.ConfigurationSection is null)); + if (login.ConfigurationSection is { } section) + { + settings.BindConfiguration(section); + } + + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.AddAuthentication().AddCookie(DashboardAccess.SessionScheme); + services.AddSingleton>(provider => new SessionCookieOptions( + provider.GetRequiredService(), provider.GetRequiredService(), provider.GetRequiredService(), + provider, login.PersistKeysInDatabase)); + } + + private static FixedLoginSettings FixedLoginSettingsFrom(FixedLoginOptions login) + { + var settings = new FixedLoginSettings(); + Copy(login, settings, users: true); + return settings; + } + + private static void Copy(FixedLoginOptions login, FixedLoginSettings settings, bool users) + { + settings.SlidingExpiration = login.SlidingExpiration; + settings.AbsoluteExpiration = login.AbsoluteExpiration; + settings.MaxFailedAttempts = login.MaxFailedAttempts; + settings.LockoutDuration = login.LockoutDuration; + settings.MaxAttemptsPerMinutePerIp = login.MaxAttemptsPerMinutePerIp; + if (users) + { + settings.Users = [.. login.Users.Select(u => new FixedLoginUser { Name = u.Name, Role = u.Role, PasswordHash = u.PasswordHash })]; + } + } + + /// + /// Wraps the authorization result handler of the host: dashboard endpoints answer failures with JSON 401/403, other endpoints + /// keep the host behaviour. + /// + private static void DecorateAuthorizationResults(IServiceCollection services) + { + services.AddAuthorization(); + var previous = services.LastOrDefault(d => d.ServiceType == typeof(IAuthorizationMiddlewareResultHandler) && !d.IsKeyedService); + services.RemoveAll(); + services.AddTransient(provider => new DashboardAuthorizationResultHandler( + previous switch + { + { ImplementationInstance: IAuthorizationMiddlewareResultHandler instance } => instance, + { ImplementationFactory: { } factory } => (IAuthorizationMiddlewareResultHandler)factory(provider), + { ImplementationType: { } type } => (IAuthorizationMiddlewareResultHandler)ActivatorUtilities.CreateInstance(provider, type), + _ => new AuthorizationMiddlewareResultHandler(), + }, + provider.GetRequiredService())); + } +} + +/// Checks at startup what depends on the environment or the configuration (D-122, D-134). +internal sealed class DashboardStartupCheck(IServiceProvider services, IHostEnvironment environment, DashboardAccessSettings settings) : IHostedService +{ + public Task StartAsync(CancellationToken cancellationToken) + { + if (settings.AllowLocalRequests && !environment.IsDevelopment()) + { + throw new PsrConfigurationException( + $"The dashboard allows local requests only in Development (environment: {environment.EnvironmentName}). Use a main access mode instead."); + } + + if (settings.Mode == DashboardAccessMode.FixedLogin) + { + // Validates the users (from configuration) now instead of on the first login. + _ = services.GetRequiredService(); + } + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj b/src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj new file mode 100644 index 0000000..6394314 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj @@ -0,0 +1,31 @@ + + + + PythonScriptRunner.Dashboard + Embeddable dashboard for PythonScriptRunner: internal HTTP API, SignalR updates, Vue panel and configurable access (fixed login, custom filter, host authentication or localhost). + true + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/PythonScriptRunner.Dashboard/Realtime/RunsHub.cs b/src/PythonScriptRunner.Dashboard/Realtime/RunsHub.cs new file mode 100644 index 0000000..4a69578 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Realtime/RunsHub.cs @@ -0,0 +1,81 @@ +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.SignalR; +using PythonScriptRunner.Dashboard.Api; + +namespace PythonScriptRunner.Dashboard.Realtime; + +/// +/// Real-time run updates ({prefix}/hubs/runs, Reader): clients subscribe to a run, a pipeline, a stage or everything and +/// receive RunUpdated and StepUpdated. A connection in several groups may receive an update more than once; every +/// message carries the full state, so repeats are harmless. +/// +internal sealed partial class RunsHub : Hub +{ + public const string AllGroup = "all"; + public const string RunUpdated = nameof(RunUpdated); + public const string StepUpdated = nameof(StepUpdated); + + [GeneratedRegex("^[a-z0-9]+(-[a-z0-9]+)*$")] + private static partial Regex KeyPattern(); + + public static string RunGroup(RunId runId) => $"run:{runId}"; + + public static string PipelineGroup(string key) => $"pipeline:{key}"; + + public static string StageGroup(string key) => $"stage:{key}"; + + public Task SubscribeRun(string runId) => Groups.AddToGroupAsync(Context.ConnectionId, RunGroup(ParseRun(runId))); + + public Task UnsubscribeRun(string runId) => Groups.RemoveFromGroupAsync(Context.ConnectionId, RunGroup(ParseRun(runId))); + + public Task SubscribePipeline(string key) => Groups.AddToGroupAsync(Context.ConnectionId, PipelineGroup(ParseKey(key))); + + public Task UnsubscribePipeline(string key) => Groups.RemoveFromGroupAsync(Context.ConnectionId, PipelineGroup(ParseKey(key))); + + public Task SubscribeStage(string key) => Groups.AddToGroupAsync(Context.ConnectionId, StageGroup(ParseKey(key))); + + public Task UnsubscribeStage(string key) => Groups.RemoveFromGroupAsync(Context.ConnectionId, StageGroup(ParseKey(key))); + + public Task SubscribeAll() => Groups.AddToGroupAsync(Context.ConnectionId, AllGroup); + + public Task UnsubscribeAll() => Groups.RemoveFromGroupAsync(Context.ConnectionId, AllGroup); + + private static RunId ParseRun(string runId) => RunId.TryParse(runId, out var id) ? id : throw new HubException("Invalid run id."); + + private static string ParseKey(string key) => + key is { Length: > 0 and <= 64 } && KeyPattern().IsMatch(key) ? key : throw new HubException("Invalid key."); +} + +/// +/// Pushes run events to the browsers of this instance (D-35, D-110): a per-instance subscription to psr.events, so every +/// instance receives every event and no backplane is needed. +/// +internal sealed class RunEventBridge(IHubContext hub, IRunManager runs) : IRunEventHandler +{ + public async Task HandleAsync(RunEvent runEvent, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(runEvent); + if (runEvent.RunId is not { } runId || await runs.GetAsync(runId, cancellationToken).ConfigureAwait(false) is not { } run) + { + return; + } + + string[] groups = + [ + RunsHub.RunGroup(runId), + RunsHub.AllGroup, + run.Kind is RunKind.Pipeline or RunKind.PipelineTest ? RunsHub.PipelineGroup(run.TargetKey) : RunsHub.StageGroup(run.TargetKey), + ]; + var clients = hub.Clients.Groups(groups); + var current = run.Steps.LastOrDefault(s => s.Status is not (StepStatus.Pending or StepStatus.Skipped)) ?? run.Steps.FirstOrDefault(); + var summary = new RunSummary(run.Id, run.Kind, run.Mode, run.Status, run.TargetKey, run.TargetVersion, run.Trigger, run.TriggeredBy, + run.CreatedAt, run.StartedAt, run.FinishedAt, run.Error); + if (runEvent.Step is { } eventStep && run.Steps.FirstOrDefault(s => s.Order == eventStep.Order) is { } step) + { + await clients.SendAsync(RunsHub.StepUpdated, new StepUpdate(runId, step.StepKey, step.Order, step.Status, eventStep.Attempt, step.StartedAt, + step.FinishedAt, step.Error?.Kind), cancellationToken).ConfigureAwait(false); + } + + await clients.SendAsync(RunsHub.RunUpdated, new RunUpdate(summary, runEvent.Type, current?.StepKey), cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/PythonScriptRunner.Dashboard/Spa/DashboardSpa.cs b/src/PythonScriptRunner.Dashboard/Spa/DashboardSpa.cs new file mode 100644 index 0000000..a218e1a --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/Spa/DashboardSpa.cs @@ -0,0 +1,71 @@ +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.StaticFiles; +using Microsoft.Extensions.FileProviders; + +namespace PythonScriptRunner.Dashboard.Spa; + +/// +/// Serves the panel embedded in the package (spec 008; a placeholder page until phase 6): static files as they are, every other +/// path gets index.html with <base href> pointing at the prefix (AC-14) and the security headers of spec 007. +/// +internal static partial class DashboardSpa +{ + public const string ContentSecurityPolicy = + "default-src 'self'; script-src 'self'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'"; + + private static readonly IFileProvider Files = new ManifestEmbeddedFileProvider(typeof(DashboardSpa).Assembly, "wwwroot"); + private static readonly FileExtensionContentTypeProvider ContentTypes = new(); + private static readonly Lazy Index = new(() => + { + using var reader = new StreamReader(Files.GetFileInfo("index.html").CreateReadStream(), Encoding.UTF8); + return reader.ReadToEnd(); + }); + + [GeneratedRegex("]*>", RegexOptions.IgnoreCase)] + private static partial Regex BaseTag(); + + public static void Map(RouteGroupBuilder root) + { + root.MapGet("/{**path}", Serve).AllowAnonymous().ExcludeFromDescription(); + } + + private static IResult Serve(string? path, HttpContext http) + { + SetSecurityHeaders(http.Response); + if (path is { Length: > 0 } && !path.Equals("index.html", StringComparison.OrdinalIgnoreCase) + && Files.GetFileInfo(path) is { Exists: true, IsDirectory: false } file) + { + if (!ContentTypes.TryGetContentType(path, out var contentType)) + { + contentType = "application/octet-stream"; + } + + // Vite names the built assets by hash: they never change. + http.Response.Headers.CacheControl = path.StartsWith("assets/", StringComparison.Ordinal) ? "public, max-age=31536000, immutable" : "no-cache"; + return TypedResults.Stream(file.CreateReadStream(), contentType); + } + + http.Response.Headers.CacheControl = "no-cache"; + var root = $"{http.Request.PathBase}{RootOf(http)}/"; + var tag = $""; + var html = BaseTag().IsMatch(Index.Value) + ? BaseTag().Replace(Index.Value, tag, 1) + : Index.Value.Replace("", "" + tag, StringComparison.OrdinalIgnoreCase); + return TypedResults.Content(html, "text/html; charset=utf-8"); + } + + private static void SetSecurityHeaders(HttpResponse response) + { + response.Headers.ContentSecurityPolicy = ContentSecurityPolicy; + response.Headers.XContentTypeOptions = "nosniff"; + response.Headers["Referrer-Policy"] = "same-origin"; + } + + private static string RootOf(HttpContext http) => + http.RequestServices.GetService(typeof(Access.DashboardRoutes)) is Access.DashboardRoutes routes ? routes.Prefix : "/psr"; +} diff --git a/src/PythonScriptRunner.Dashboard/wwwroot/index.html b/src/PythonScriptRunner.Dashboard/wwwroot/index.html new file mode 100644 index 0000000..92a6556 --- /dev/null +++ b/src/PythonScriptRunner.Dashboard/wwwroot/index.html @@ -0,0 +1,22 @@ + + + + + + + PythonScriptRunner + + + +
+

PythonScriptRunner

+

Página provisória do painel. A interface (spec 008) é publicada neste mesmo endereço em uma versão futura do pacote.

+

A API interna do painel responde em api/ e as atualizações em tempo real em hubs/runs.

+
+ + From 14e25fe01b665486cb8976a250f6aef3829497c6 Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:07:12 -0300 Subject: [PATCH 3/7] feat(hosts,tool): DashboardHost com login fixo ou OIDC e psr hash-password (spec 009) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DashboardHost: login fixo por padrão com usuários por variáveis de ambiente; OIDC opcional (Oidc__Authority/ClientId/ClientSecret, cookie + PKCE, papéis por Dashboard__RoleClaim e Dashboard__Roles__*); a SPA é desafiada e a API responde 401. Sem usuários e sem OIDC a inicialização falha com mensagem clara. Dashboard__PathPrefix, health checks de banco e broker, PSR_AUTO_MIGRATE. - PythonScriptRunner.Tool (7º pacote): dotnet tool `psr`; `psr hash-password` lê a senha sem eco (duas vezes) ou de --stdin e imprime o hash PBKDF2 do ASP.NET Identity v3. Co-Authored-By: Claude Opus 5.5 --- .../Program.cs | 134 ++++++++++++++++++ .../PythonScriptRunner.DashboardHost.csproj | 16 +++ src/PythonScriptRunner.Tool/PsrTool.cs | 133 +++++++++++++++++ .../PythonScriptRunner.Tool.csproj | 20 +++ 4 files changed, 303 insertions(+) create mode 100644 hosts/PythonScriptRunner.DashboardHost/Program.cs create mode 100644 hosts/PythonScriptRunner.DashboardHost/PythonScriptRunner.DashboardHost.csproj create mode 100644 src/PythonScriptRunner.Tool/PsrTool.cs create mode 100644 src/PythonScriptRunner.Tool/PythonScriptRunner.Tool.csproj diff --git a/hosts/PythonScriptRunner.DashboardHost/Program.cs b/hosts/PythonScriptRunner.DashboardHost/Program.cs new file mode 100644 index 0000000..f05c237 --- /dev/null +++ b/hosts/PythonScriptRunner.DashboardHost/Program.cs @@ -0,0 +1,134 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; + +// Standalone dashboard of PythonScriptRunner (spec 009): API, real-time updates and panel of spec 007, plus the outbox relay. +// Fixed login by default, with users from environment variables (PythonScriptRunner__Dashboard__Login__Users__0__Name, __Role, +// __PasswordHash); OIDC when Oidc__Authority is set (D-125). Also: ConnectionStrings__psr, ConnectionStrings__rabbitmq, +// Dashboard__PathPrefix (default /psr), PSR_AUTO_MIGRATE and ASPNETCORE_FORWARDEDHEADERS_ENABLED behind a proxy. +var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults("psr-dashboard"); + +var configuration = builder.Configuration; +var prefix = configuration["Dashboard:PathPrefix"] is { Length: > 0 } path ? "/" + path.Trim('/') : "/psr"; +var oidc = configuration.GetSection("Oidc"); +var useOidc = !string.IsNullOrWhiteSpace(oidc["Authority"]); +if (!useOidc && !configuration.GetSection("PythonScriptRunner:Dashboard:Login:Users").GetChildren().Any()) +{ + throw new InvalidOperationException( + "The dashboard has no way to sign in: set the fixed login users (PythonScriptRunner__Dashboard__Login__Users__0__Name, __Role and " + + "__PasswordHash, with the hash from 'psr hash-password') or enable OIDC (Oidc__Authority, Oidc__ClientId and Oidc__ClientSecret)."); +} + +var roleClaim = configuration["Dashboard:RoleClaim"] ?? "roles"; +if (useOidc) +{ + // Host authentication (BFF): the cookie keeps the session, OpenID Connect signs in; tokens never reach the browser. + builder.Services.AddAuthentication(o => + { + o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; + o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookie(o => + { + o.Cookie.Name = "psr.host"; + o.Cookie.HttpOnly = true; + o.Cookie.SameSite = SameSiteMode.Lax; + o.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; + o.ExpireTimeSpan = TimeSpan.FromHours(8); + o.SlidingExpiration = true; + }) + .AddOpenIdConnect(o => + { + o.Authority = oidc["Authority"]; + o.ClientId = oidc["ClientId"]; + o.ClientSecret = oidc["ClientSecret"]; + o.RequireHttpsMetadata = oidc.GetValue("RequireHttpsMetadata", true); + o.ResponseType = OpenIdConnectResponseType.Code; + o.UsePkce = true; + o.SaveTokens = false; + o.GetClaimsFromUserInfoEndpoint = true; + o.MapInboundClaims = false; + o.Scope.Add("profile"); + o.TokenValidationParameters.NameClaimType = "name"; + o.TokenValidationParameters.RoleClaimType = roleClaim; + }); +} + +builder.Services + .AddPythonScriptRunner(o => configuration.GetSection("PythonScriptRunner").Bind(o)) + .AddEntityFrameworkCore(o => + { + configuration.GetSection("PythonScriptRunner:EntityFrameworkCore").Bind(o); + o.UseNpgsql(configuration.GetConnectionString("psr") + ?? throw new InvalidOperationException("Set the PostgreSQL connection string ConnectionStrings__psr.")); + }) + .AddRabbitMq(o => + { + configuration.GetSection("PythonScriptRunner:RabbitMQ").Bind(o); + o.ConnectionString = configuration.GetConnectionString("rabbitmq") + ?? throw new InvalidOperationException("Set the RabbitMQ connection string ConnectionStrings__rabbitmq."); + }) + .AddDashboard(o => + { + if (!useOidc) + { + o.UseFixedLogin(login => login.BindConfiguration()); + return; + } + + // Each role needs its own claim value (Dashboard__Roles__Reader...); the highest one the user has wins. + string Role(string name, string fallback) => configuration[$"Dashboard:Roles:{name}"] ?? fallback; + o.UseHostAuthentication(roles => + { + roles.Reader = p => p.RequireAuthenticatedUser().RequireClaim(roleClaim, Role("Reader", "psr-leitor")); + roles.Executor = p => p.RequireAuthenticatedUser().RequireClaim(roleClaim, Role("Executor", "psr-executor")); + roles.Author = p => p.RequireAuthenticatedUser().RequireClaim(roleClaim, Role("Author", "psr-autor")); + roles.Admin = p => p.RequireAuthenticatedUser().RequireClaim(roleClaim, Role("Admin", "psr-admin")); + roles.LoginUrl = "/account/login?returnUrl={returnUrl}"; + roles.LogoutUrl = "/account/logout"; + }); + }); +string[] ready = [ServiceDefaultsExtensions.ReadyTag]; +builder.Services.AddHealthChecks().AddPythonScriptRunnerDatabase(tags: ready).AddPythonScriptRunnerBroker(tags: ready); + +var app = builder.Build(); +if (configuration.GetValue("PSR_AUTO_MIGRATE")) +{ + await app.Services.MigratePythonScriptRunnerAsync(); +} + +app.UseAuthentication(); +app.UseAuthorization(); +if (useOidc) +{ + // The panel itself is challenged (redirect to the provider); the API answers 401 JSON and the hub refuses (spec 009, AC-10). + app.Use(async (context, next) => + { + if (context.Request.Path.StartsWithSegments(prefix, out var rest) + && !rest.StartsWithSegments("/api") && !rest.StartsWithSegments("/hubs") + && context.User.Identity?.IsAuthenticated != true) + { + await context.ChallengeAsync(new AuthenticationProperties { RedirectUri = context.Request.PathBase + context.Request.Path + context.Request.QueryString }); + return; + } + + await next(context); + }); + app.MapGet("/account/login", (string? returnUrl) => + TypedResults.Challenge(new AuthenticationProperties { RedirectUri = IsLocal(returnUrl) ? returnUrl : prefix + "/" })); + app.MapGet("/account/logout", () => TypedResults.SignOut(new AuthenticationProperties { RedirectUri = prefix + "/" }, + [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); +} + +app.MapDefaultEndpoints(); +app.MapPythonScriptRunnerDashboard(prefix); +await app.RunAsync(); + +// Only paths of this site are accepted as return addresses (no open redirect). +static bool IsLocal(string? url) => + url is { Length: > 0 } && url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\')); + +/// Entry point (public for the integration tests). +public partial class Program; diff --git a/hosts/PythonScriptRunner.DashboardHost/PythonScriptRunner.DashboardHost.csproj b/hosts/PythonScriptRunner.DashboardHost/PythonScriptRunner.DashboardHost.csproj new file mode 100644 index 0000000..4a4eb8e --- /dev/null +++ b/hosts/PythonScriptRunner.DashboardHost/PythonScriptRunner.DashboardHost.csproj @@ -0,0 +1,16 @@ + + + + PythonScriptRunner.DashboardHost + + + + + + + + + + + + diff --git a/src/PythonScriptRunner.Tool/PsrTool.cs b/src/PythonScriptRunner.Tool/PsrTool.cs new file mode 100644 index 0000000..c956775 --- /dev/null +++ b/src/PythonScriptRunner.Tool/PsrTool.cs @@ -0,0 +1,133 @@ +using System.Reflection; +using System.Text; +using Microsoft.AspNetCore.Identity; + +namespace PythonScriptRunner.Tool; + +/// The psr command (spec 009; D-124, D-130). Today: psr hash-password. +public static class PsrTool +{ + /// Entry point. + /// Command line. + /// 0 on success. + public static int Main(string[] args) => Run(args, ToolConsole.System); + + internal static int Run(IReadOnlyList args, ToolConsole console) + { + ArgumentNullException.ThrowIfNull(args); + ArgumentNullException.ThrowIfNull(console); + switch (args.Count == 0 ? null : args[0]) + { + case "hash-password": + return HashPassword([.. args.Skip(1)], console); + case "-h" or "--help" or "help": + Usage(console.Out); + return 0; + case "--version": + console.Out.WriteLine(typeof(PsrTool).Assembly.GetCustomAttribute()?.InformationalVersion); + return 0; + case null: + Usage(console.Error); + return 1; + default: + console.Error.WriteLine($"Unknown command '{args[0]}'."); + Usage(console.Error); + return 1; + } + } + + /// + /// Prints the PBKDF2 hash (ASP.NET Identity v3 format) of a password for PasswordHash of a fixed login user (D-121). + /// The password is typed twice without echo, or read from the first line of the standard input with --stdin; it is + /// never printed. + /// + private static int HashPassword(IReadOnlyList options, ToolConsole console) + { + var unknown = options.Where(o => o != "--stdin").ToList(); + if (unknown.Count > 0) + { + console.Error.WriteLine($"Unknown option '{unknown[0]}'. The password is never an argument: type it or pipe it with --stdin."); + return 1; + } + + string? password; + if (options.Contains("--stdin")) + { + password = console.In.ReadLine(); + } + else if (console.IsInputRedirected) + { + console.Error.WriteLine("The input is redirected: use 'psr hash-password --stdin' to read the password from it."); + return 1; + } + else + { + console.Error.Write("Password: "); + password = console.ReadSecret(); + console.Error.Write("Repeat the password: "); + if (console.ReadSecret() != password) + { + console.Error.WriteLine("The passwords do not match."); + return 1; + } + } + + if (string.IsNullOrEmpty(password)) + { + console.Error.WriteLine("The password is empty."); + return 1; + } + + console.Out.WriteLine(new PasswordHasher().HashPassword(new object(), password)); + return 0; + } + + private static void Usage(TextWriter writer) + { + writer.WriteLine("Usage: psr "); + writer.WriteLine(); + writer.WriteLine("Commands:"); + writer.WriteLine(" hash-password [--stdin] Prints the hash of a password for PythonScriptRunner__Dashboard__Login__Users__N__PasswordHash."); + writer.WriteLine(" The password is typed twice without echo, or read from the first line of the input with --stdin."); + } +} + +/// Console of the tool (replaced in tests). +/// Standard input. +/// Standard output (only results). +/// Prompts and errors. +/// Whether the input is not a terminal. +/// Reads a line without echo. +internal sealed record ToolConsole(TextReader In, TextWriter Out, TextWriter Error, bool IsInputRedirected, Func ReadSecret) +{ + public static ToolConsole System { get; } = new(Console.In, Console.Out, Console.Error, Console.IsInputRedirected, ReadWithoutEcho); + + private static string ReadWithoutEcho() + { + var text = new StringBuilder(); + while (true) + { + var key = Console.ReadKey(intercept: true); + switch (key.Key) + { + case ConsoleKey.Enter: + Console.Error.WriteLine(); + return text.ToString(); + case ConsoleKey.Backspace: + if (text.Length > 0) + { + text.Length--; + } + + break; + default: + if (!char.IsControl(key.KeyChar)) + { + text.Append(key.KeyChar); + } + + break; + } + } + } +} diff --git a/src/PythonScriptRunner.Tool/PythonScriptRunner.Tool.csproj b/src/PythonScriptRunner.Tool/PythonScriptRunner.Tool.csproj new file mode 100644 index 0000000..9b7e658 --- /dev/null +++ b/src/PythonScriptRunner.Tool/PythonScriptRunner.Tool.csproj @@ -0,0 +1,20 @@ + + + + Exe + PythonScriptRunner.Tool + Command line utilities of PythonScriptRunner (dotnet tool 'psr'): psr hash-password generates the password hash of a dashboard fixed login user. + true + psr + PythonScriptRunner.Tool.PsrTool + + + + + + + + + + + From 06169299e5861ff48c7c889d0188618684f40eb3 Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:07:13 -0300 Subject: [PATCH 4/7] =?UTF-8?q?test(dashboard):=20crit=C3=A9rios=20de=20ac?= =?UTF-8?q?eite=20das=20specs=20007=20e=20009?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Projeto PythonScriptRunner.Dashboard.Tests (TestServer/WebApplicationFactory + Testcontainers), reaproveitando por link a infraestrutura dos testes do worker. - Spec 007 AC-1..AC-25: configuração inválida, matriz endpoint × papel (login fixo e host), CSRF, 412/428, sintaxe 422, upload/download e 413, keyset, 410, tempo real em duas instâncias (≤ 1 s), auditoria, desempenho com ~200 mil execuções (opt-in completo por PSR_PERF_RUNS_PER_DAY), prefixo customizado, snapshot do OpenAPI, segredos, login fixo (sessão, bloqueio, 429, recarga, chaves compartilhadas), filtro, localhost e visão geral. - Spec 009 AC-9..AC-11: DashboardHost com login fixo e com OIDC; psr hash-password. Co-Authored-By: Claude Opus 5.5 --- .../PythonScriptRunner.Core.csproj | 1 + .../PythonScriptRunner.RabbitMQ.csproj | 1 + .../AccessTests.cs | 374 ++ .../AuthorizationMatrixTests.cs | 160 + .../CatalogApiTests.cs | 111 + .../HostAndToolTests.cs | 165 + .../Infrastructure/DashboardApp.cs | 378 ++ .../Infrastructure/Snapshot.cs | 41 + .../PerformanceTests.cs | 94 + .../PrefixAndOpenApiTests.cs | 80 + .../PythonScriptRunner.Dashboard.Tests.csproj | 41 + .../RealtimeTests.cs | 147 + .../RunApiTests.cs | 268 + .../Snapshots/openapi-v1.verified.json | 5684 +++++++++++++++++ 14 files changed, 7545 insertions(+) create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/AccessTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/AuthorizationMatrixTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/CatalogApiTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/HostAndToolTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/DashboardApp.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/PerformanceTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/PrefixAndOpenApiTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/RealtimeTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/RunApiTests.cs create mode 100644 tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json diff --git a/src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj b/src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj index 7395d79..75a2dd0 100644 --- a/src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj +++ b/src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj @@ -20,6 +20,7 @@ + diff --git a/src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj b/src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj index d4f1e52..86a0b72 100644 --- a/src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj +++ b/src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj @@ -12,6 +12,7 @@ + diff --git a/tests/PythonScriptRunner.Dashboard.Tests/AccessTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/AccessTests.cs new file mode 100644 index 0000000..cff9777 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/AccessTests.cs @@ -0,0 +1,374 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.Extensions.Time.Testing; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007: access modes, configuration and the fixed login (ADR-0021; D-118..D-136). +public sealed class AccessTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC01_WithoutMap_ThereIsNoDashboard() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => s.Map = false); + using var client = app.CreateClient(); + + foreach (var path in (string[])["/psr", "/psr/", "/psr/api/auth", "/psr/api/stages", "/psr/hubs/runs/negotiate"]) + { + (await client.GetAsync(path, Ct)).StatusCode.ShouldBe(HttpStatusCode.NotFound, path); + } + } + + public static TheoryData InvalidConfigurations() => + ["no mode", "two main modes", "fixed login without users", "host without a role", "invalid hash in configuration", "local requests outside Development"]; + + [Theory] + [MemberData(nameof(InvalidConfigurations))] + public async Task Spec007_AC02_InvalidConfiguration_FailsTheStartup(string configuration) + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + Action configure = configuration switch + { + "no mode" => s => s.Dashboard = _ => { } + , + "two main modes" => s => s.Dashboard = o => o.UseFixedLogin(l => l.BindConfiguration()).UseAuthorizationFilter(), + "fixed login without users" => s => s.Dashboard = o => o.UseFixedLogin(), + "host without a role" => s => s.Dashboard = o => o.UseHostAuthentication(r => + { + r.Reader = p => p.RequireClaim("role", "r"); + r.Executor = p => p.RequireClaim("role", "e"); + r.Author = p => p.RequireClaim("role", "a"); + }), + "invalid hash in configuration" => s => + { + s.Configuration.Clear(); + foreach (var (key, value) in TestUsers.Configuration(("ana", DashboardRole.Admin, "senha-em-texto"))) + { + s.Configuration[key] = value; + } + } + , + _ => s => + { + s.Environment = "Production"; + s.Dashboard = o => + { + o.UseFixedLogin(l => l.BindConfiguration()); + o.AllowLocalRequests = true; + }; + } + , + }; + + var error = await Should.ThrowAsync(async () => + { + await using var app = await DashboardApp.StartAsync(env, configure); + }); + + error.Message.ShouldNotBeNullOrWhiteSpace(); + if (configuration == "invalid hash in configuration") + { + error.Message.ShouldContain("psr hash-password"); + } + } + + [Fact] + public async Task Spec007_AC03_Unauthenticated_Gets401JsonAndTheMode() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using (var app = await DashboardApp.StartAsync(env)) + { + using var client = app.CreateClient(); + var denied = await client.GetAsync("/psr/api/stages", Ct); + denied.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + denied.Content.Headers.ContentType!.MediaType.ShouldBe("application/problem+json"); + (await denied.JsonAsync())["code"]!.GetValue().ShouldBe("UNAUTHENTICATED"); + + var auth = await (await client.GetAsync("/psr/api/auth", Ct)).JsonAsync(); + auth["mode"]!.GetValue().ShouldBe("FixedLogin"); + auth["localRequests"]!.GetValue().ShouldBeFalse(); + auth.ToJsonString().ShouldNotContain("leitor"); + } + + await using (var host = await DashboardApp.StartAsync(env, s => HeaderAuthenticationHandler.Register(s))) + { + using var client = host.CreateClient(); + var denied = await client.GetAsync("/psr/api/stages", Ct); + denied.StatusCode.ShouldBe(HttpStatusCode.Unauthorized, "the API never follows the host challenge (redirect)"); + (await denied.JsonAsync())["code"]!.GetValue().ShouldBe("UNAUTHENTICATED"); + + var auth = await (await client.GetAsync("/psr/api/auth", Ct)).JsonAsync(); + auth["mode"]!.GetValue().ShouldBe("Host"); + auth["loginUrl"]!.GetValue().ShouldBe("/host/login?volta=%2Fpsr%2F"); + auth["logoutUrl"]!.GetValue().ShouldBe("/host/logout"); + } + } + + [Fact] + public async Task Spec007_AC05_MutatingRequestWithoutTheHeader_Gets400() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + using var client = app.CreateClient(csrf: false); + + var login = await client.PostAsJsonAsync("/psr/api/auth/login", new { name = "autor", password = TestUsers.Password }, Ct); + login.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + (await login.JsonAsync())["code"]!.GetValue().ShouldBe("CSRF_HEADER_MISSING"); + + using var request = new HttpRequestMessage(HttpMethod.Post, "/psr/api/auth/login") + { + Content = JsonContent.Create(new { name = "autor", password = TestUsers.Password }), + }; + request.Headers.Add("X-PSR-Request", "1"); + (await client.SendAsync(request, Ct)).StatusCode.ShouldBe(HttpStatusCode.NoContent); + + var create = await client.PostAsJsonAsync("/psr/api/stages", new { key = "sem-cabecalho", name = "x" }, Ct); + create.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + (await create.JsonAsync())["code"]!.GetValue().ShouldBe("CSRF_HEADER_MISSING"); + (await app.Admin.GetStageAsync("sem-cabecalho", Ct)).ShouldBeNull(); + } + + [Fact] + public async Task Spec007_AC17_FixedLogin_SetsTheSessionCookieAndLogoutEndsIt() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + using var client = app.CreateClient(); + + var login = await client.PostAsJsonAsync("/psr/api/auth/login", new { name = "AUTOR", password = TestUsers.Password }, Ct); + + login.StatusCode.ShouldBe(HttpStatusCode.NoContent); + var cookie = login.Headers.GetValues("Set-Cookie").Single(c => c.StartsWith("psr.session=", StringComparison.Ordinal)).ToLowerInvariant(); + cookie.ShouldContain("httponly"); + cookie.ShouldContain("samesite=lax"); + cookie.ShouldContain("path=/psr"); + var me = await (await client.GetAsync("/psr/api/me", Ct)).JsonAsync(); + me["name"]!.GetValue().ShouldBe("autor"); + me["subject"]!.GetValue().ShouldBe("login:autor"); + me["role"]!.GetValue().ShouldBe("Author"); + me["roles"]!.AsArray().Select(r => r!.GetValue()).ShouldBe(["Reader", "Executor", "Author"]); + me["mode"]!.GetValue().ShouldBe("FixedLogin"); + + (await client.PostAsync("/psr/api/auth/logout", null, Ct)).StatusCode.ShouldBe(HttpStatusCode.NoContent); + (await client.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + } + + [Fact] + public async Task Spec007_AC18_AC23_FailuresLockTheUser_IpsAreLimited_AndLoginsAreAudited() + { + var time = new FakeTimeProvider(DateTimeOffset.UtcNow); + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time); + await using var app = await DashboardApp.StartAsync(env); + + async Task LoginAsync(string name, string password, string ip) + { + using var client = app.CreateClient(ip: ip); + return await client.PostAsJsonAsync("/psr/api/auth/login", new { name, password }, Ct); + } + + // A successful login and a logout are audited. + using (var session = await app.LoginAsync("leitor")) + { + (await session.PostAsync("/psr/api/auth/logout", null, Ct)).StatusCode.ShouldBe(HttpStatusCode.NoContent); + } + + // 5 consecutive failures lock the user (each from another IP: the IP limit is not reached). + var generic = await LoginAsync("ninguem", "x", "198.51.100.1"); + generic.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + var genericBody = (await generic.JsonAsync()).AsObject(); + for (var i = 0; i < 5; i++) + { + (await LoginAsync("executor", "errada", $"198.51.100.{10 + i}")).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + } + + var locked = await LoginAsync("executor", TestUsers.Password, "198.51.100.30"); + locked.StatusCode.ShouldBe(HttpStatusCode.Unauthorized, "the right password gets the same answer while locked"); + var lockedBody = (await locked.JsonAsync()).AsObject(); + lockedBody["code"]!.GetValue().ShouldBe(genericBody["code"]!.GetValue()); + lockedBody["detail"]?.ToString().ShouldBe(genericBody["detail"]?.ToString()); + + time.Advance(TimeSpan.FromMinutes(14)); + (await LoginAsync("executor", TestUsers.Password, "198.51.100.31")).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + time.Advance(TimeSpan.FromMinutes(1)); + (await LoginAsync("executor", TestUsers.Password, "198.51.100.32")).StatusCode.ShouldBe(HttpStatusCode.NoContent); + + // The 11th attempt in the same minute from one IP gets 429 with Retry-After. + for (var i = 0; i < 10; i++) + { + (await LoginAsync("ninguem", "x", "192.0.2.77")).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + } + + var limited = await LoginAsync("leitor", TestUsers.Password, "192.0.2.77"); + limited.StatusCode.ShouldBe(HttpStatusCode.TooManyRequests); + limited.Headers.RetryAfter!.Delta!.Value.ShouldBeGreaterThan(TimeSpan.Zero); + time.Advance(TimeSpan.FromMinutes(1)); + (await LoginAsync("leitor", TestUsers.Password, "192.0.2.77")).StatusCode.ShouldBe(HttpStatusCode.NoContent); + + var actions = await env.ColumnAsync("SELECT action || ' ' || actor || ' ' || entity_key FROM psr.audit_events ORDER BY occurred_at, id"); + actions.ShouldBe( + [ + "LoginSucceeded login:leitor leitor", + "LoggedOut login:leitor leitor", + "UserLocked login:executor executor", + "LoginSucceeded login:executor executor", + "LoginSucceeded login:leitor leitor", + ], ignoreOrder: true); + } + + [Fact] + public async Task Spec007_AC19_Session_ExpiresAfterIdleTimeOrTheAbsoluteLimit() + { + var time = new FakeTimeProvider(DateTimeOffset.UtcNow); + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time); + await using var app = await DashboardApp.StartAsync(env); + + using (var idle = await app.LoginAsync("leitor")) + { + time.Advance(TimeSpan.FromHours(7.9)); + (await idle.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK, "used before 8 h: the session slides"); + time.Advance(TimeSpan.FromHours(7.9)); + (await idle.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + time.Advance(TimeSpan.FromHours(8.1)); + (await idle.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized, "8 h without use"); + } + + using var busy = await app.LoginAsync("leitor"); + for (var hour = 0; hour < 23; hour++) + { + time.Advance(TimeSpan.FromHours(1)); + (await busy.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + } + + time.Advance(TimeSpan.FromHours(1)); + (await busy.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized, "24 h since the login, even when used"); + } + + [Fact] + public async Task Spec007_AC20_ChangedConfiguration_AppliesWithoutRestart() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + var reloadable = new ReloadableConfigurationSource(); + await using var app = await DashboardApp.StartAsync(env, s => + { + s.Configuration.Clear(); + s.Reloadable = reloadable; + reloadable.Provider.Replace(TestUsers.Configuration()); + }); + using var removed = await app.LoginAsync("leitor"); + using var changedPassword = await app.LoginAsync("executor"); + using var promoted = await app.LoginAsync("autor"); + var newHash = DashboardPasswords.Hash("nova-senha"); + + reloadable.Provider.Replace(TestUsers.Configuration( + ("executor", DashboardRole.Executor, newHash), ("autor", DashboardRole.Admin, null), ("admin", DashboardRole.Admin, null))); + + await Eventually.WaitAsync(async () => (await removed.GetAsync("/psr/api/me", Ct)).StatusCode == HttpStatusCode.Unauthorized, TimeSpan.FromSeconds(5)); + (await changedPassword.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + (await (await promoted.GetAsync("/psr/api/me", Ct)).JsonAsync())["role"]!.GetValue().ShouldBe("Admin"); + (await promoted.GetAsync("/psr/api/audit", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + using var relogged = await app.LoginAsync("executor", "nova-senha"); + + // An invalid configuration is refused: the previous users stay. + reloadable.Provider.Replace(TestUsers.Configuration(("admin", DashboardRole.Admin, "texto-puro"))); + await Task.Delay(200, Ct); + (await relogged.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + using var stillValid = await app.LoginAsync("executor", "nova-senha"); + } + + [Fact] + public async Task Spec007_AC21_CustomFilter_GivesNameAndRole() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => s.Dashboard = o => o.UseAuthorizationFilter()); + await app.PublishStageAsync("eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + using var anonymous = app.CreateClient(); + using var ana = app.CreateClient(); + ana.DefaultRequestHeaders.Add(HeaderFilter.Header, "ana|Executor"); + + (await anonymous.GetAsync("/psr/api/stages", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + (await (await anonymous.GetAsync("/psr/api/auth", Ct)).JsonAsync())["mode"]!.GetValue().ShouldBe("Filter"); + var me = await (await ana.GetAsync("/psr/api/me", Ct)).JsonAsync(); + me["subject"]!.GetValue().ShouldBe("filter:ana"); + me["role"]!.GetValue().ShouldBe("Executor"); + (await ana.PostAsJsonAsync("/psr/api/stages", new { key = "nova", name = "nova" }, Ct)).StatusCode.ShouldBe(HttpStatusCode.Forbidden); + var run = await ana.PostAsJsonAsync("/psr/api/stages/eco/runs", new { inputs = new { texto = "oi" } }, Ct); + run.StatusCode.ShouldBe(HttpStatusCode.Accepted); + var runId = (await run.JsonAsync())["runId"]!.GetValue(); + + (await env.ScalarAsync($"SELECT triggered_by FROM psr.runs WHERE id = '{runId}'")).ShouldBe("filter:ana"); + (await env.ScalarAsync($"SELECT actor FROM psr.audit_events WHERE action = 'RunTriggered' AND entity_id = '{runId}'")).ShouldBe("filter:ana"); + } + + [Fact] + public async Task Spec007_AC22_LocalRequests_EnterAsAdminInDevelopment() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using (var app = await DashboardApp.StartAsync(env, s => + { + s.Environment = "Development"; + s.Dashboard = o => + { + o.UseFixedLogin(l => l.BindConfiguration()); + o.AllowLocalRequests = true; + }; + })) + { + using var local = app.CreateClient(ip: "127.0.0.1"); + var me = await (await local.GetAsync("/psr/api/me", Ct)).JsonAsync(); + me["subject"]!.GetValue().ShouldBe("local"); + me["role"]!.GetValue().ShouldBe("Admin"); + me["mode"]!.GetValue().ShouldBe("Localhost"); + (await local.GetAsync("/psr/api/audit", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + + using var remote = app.CreateClient(ip: "203.0.113.50"); + (await remote.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + var auth = await (await remote.GetAsync("/psr/api/auth", Ct)).JsonAsync(); + auth["mode"]!.GetValue().ShouldBe("FixedLogin"); + auth["localRequests"]!.GetValue().ShouldBeTrue(); + using var reader = await app.LoginAsync("leitor"); + (await (await reader.GetAsync("/psr/api/me", Ct)).JsonAsync())["role"]!.GetValue().ShouldBe("Reader"); + } + + await using var localOnly = await DashboardApp.StartAsync(env, s => + { + s.Environment = "Development"; + s.Dashboard = o => o.AllowLocalRequests = true; + }); + using var outsider = localOnly.CreateClient(ip: "203.0.113.51"); + (await outsider.GetAsync("/psr/api/stages", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + (await (await outsider.GetAsync("/psr/api/auth", Ct)).JsonAsync())["mode"]!.GetValue().ShouldBe("Localhost"); + using var ipv6 = localOnly.CreateClient(ip: "::1"); + (await ipv6.GetAsync("/psr/api/stages", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + } + + [Fact] + public async Task Spec007_AC24_SessionIsValidOnOtherInstancesAndAfterRestart() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + string cookie; + await using (var first = await DashboardApp.StartAsync(env)) + { + using var session = await first.LoginAsync("autor"); + cookie = session.CookieHeader("/psr/api/me"); + cookie.ShouldStartWith("psr.session="); + } + + await using var second = await DashboardApp.StartAsync(env); + await using var third = await DashboardApp.StartAsync(env); + + foreach (var app in (DashboardApp[])[second, third]) + { + using var client = app.CreateClient(); + client.DefaultRequestHeaders.Add("Cookie", cookie); + var me = await client.GetAsync("/psr/api/me", Ct); + me.StatusCode.ShouldBe(HttpStatusCode.OK); + (await me.JsonAsync())["name"]!.GetValue().ShouldBe("autor"); + } + + (await env.ScalarAsync("SELECT count(*) FROM psr.dashboard_keys")).ShouldBe(1); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/AuthorizationMatrixTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/AuthorizationMatrixTests.cs new file mode 100644 index 0000000..8e6633c --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/AuthorizationMatrixTests.cs @@ -0,0 +1,160 @@ +using System.Net; +using System.Text; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007, AC-4: every endpoint × role gives 2xx (or a business answer) or 403, in the fixed login and host modes. +public sealed class AuthorizationMatrixTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static readonly string Unknown = Guid.CreateVersion7().ToString(); + + /// Method, path, body, lowest role (null: any identified user) and whether an allowed call must be 2xx. + private static readonly (string Method, string Path, string? Body, DashboardRole? Minimum, bool Ok)[] Matrix = + [ + ("GET", "/me", null, null, true), + ("GET", "/stages", null, DashboardRole.Reader, true), + ("GET", "/stages/eco-{role}", null, DashboardRole.Reader, true), + ("GET", "/stages/eco-{role}/versions/1", null, DashboardRole.Reader, true), + ("GET", "/stages/eco-{role}/versions/1/export", null, DashboardRole.Reader, true), + ("GET", "/pipelines", null, DashboardRole.Reader, true), + ("GET", "/pipelines/trilha-{role}", null, DashboardRole.Reader, true), + ("GET", "/pipelines/trilha-{role}/versions/1", null, DashboardRole.Reader, true), + ("GET", "/pipelines/trilha-{role}/versions/1/export", null, DashboardRole.Reader, true), + ("GET", "/overview", null, DashboardRole.Reader, true), + ("GET", "/runs", null, DashboardRole.Reader, true), + ($"GET", $"/runs/{Unknown}", null, DashboardRole.Reader, false), + ("GET", $"/runs/{Unknown}/attempts/{Unknown}/logs/stdout", null, DashboardRole.Reader, false), + ("GET", $"/artifacts/{Unknown}", null, DashboardRole.Reader, false), + ("GET", "/schedules", null, DashboardRole.Reader, true), + ("POST", "/schedules/preview", """{"cron":"0 8 * * *","timeZone":"America/Sao_Paulo","count":3}""", DashboardRole.Reader, true), + ("GET", "/meta/field-types", null, DashboardRole.Reader, true), + ("POST", "/stages/eco-{role}/runs", """{"inputs":{"texto":"oi"}}""", DashboardRole.Executor, true), + ("POST", "/pipelines/trilha-{role}/runs", """{"inputs":{"texto":"oi"}}""", DashboardRole.Executor, true), + ("POST", $"/runs/{Unknown}/cancel", null, DashboardRole.Executor, false), + ("POST", $"/runs/{Unknown}/reprocess", null, DashboardRole.Executor, false), + ("POST", "/stages", """{"key":"nova-{role}","name":"Nova"}""", DashboardRole.Author, true), + ("PATCH", "/stages/eco-{role}", """{"name":"Eco"}""", DashboardRole.Author, false), + ("GET", "/stages/eco-{role}/draft", null, DashboardRole.Author, false), + ("POST", "/stages/eco-{role}/draft", """{"fromVersion":1}""", DashboardRole.Author, false), + ("PUT", "/stages/eco-{role}/draft", """{"script":"x","pythonVersion":"3.12"}""", DashboardRole.Author, false), + ("POST", "/stages/eco-{role}/draft/validate", null, DashboardRole.Author, false), + ("POST", "/stages/eco-{role}/draft/test", """{"inputs":{"texto":"oi"}}""", DashboardRole.Author, false), + ("POST", "/stages/eco-{role}/draft/publish", null, DashboardRole.Author, false), + ("DELETE", "/stages/eco-{role}/draft", null, DashboardRole.Author, false), + ("POST", "/pipelines", """{"key":"nova-{role}","name":"Nova"}""", DashboardRole.Author, true), + ("POST", "/pipelines/validate", """{"steps":[]}""", DashboardRole.Author, true), + ("GET", "/pipelines/trilha-{role}/draft", null, DashboardRole.Author, false), + ("POST", "/pipelines/trilha-{role}/draft", """{"fromVersion":1}""", DashboardRole.Author, false), + ("PUT", "/pipelines/trilha-{role}/draft", """{"steps":[]}""", DashboardRole.Author, false), + ("POST", "/pipelines/trilha-{role}/draft/test", """{"inputs":{"texto":"oi"}}""", DashboardRole.Author, false), + ("POST", "/pipelines/trilha-{role}/draft/publish", null, DashboardRole.Author, false), + ("DELETE", "/pipelines/trilha-{role}/draft", null, DashboardRole.Author, false), + ("PATCH", "/pipelines/trilha-{role}/settings", """{"maxConcurrentRuns":2}""", DashboardRole.Author, false), + ("POST", "/pipelines/trilha-{role}/schedules", """{"name":"diário {role}","cron":"0 8 * * *","inputs":{"texto":"oi"}}""", DashboardRole.Author, true), + ("PUT", $"/schedules/{Unknown}", """{"name":"x","cron":"0 8 * * *"}""", DashboardRole.Author, false), + ("POST", $"/schedules/{Unknown}/enable", null, DashboardRole.Author, false), + ("POST", $"/schedules/{Unknown}/disable", null, DashboardRole.Author, false), + ("DELETE", $"/schedules/{Unknown}", null, DashboardRole.Author, false), + ("POST", "/import", null, DashboardRole.Author, false), + ("GET", "/audit", null, DashboardRole.Admin, true), + ("GET", "/admin/secrets", null, DashboardRole.Admin, true), + ("GET", "/admin/settings", null, DashboardRole.Admin, true), + ("PATCH", "/pipelines/trilha-{role}/retention", """{"artifactRetentionDays":3,"runRetentionDays":30}""", DashboardRole.Admin, false), + ("POST", "/stages/eco-{role}/versions/1/archive", null, DashboardRole.Author, false), + ("POST", "/pipelines/trilha-{role}/versions/1/archive", null, DashboardRole.Author, false), + ("DELETE", "/pipelines/trilha-{role}", null, DashboardRole.Admin, false), + ("DELETE", "/stages/eco-{role}", null, DashboardRole.Admin, false), + ]; + + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC04_RoleMatrix_FixedLogin() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + await SeedAsync(app); + foreach (var (name, role) in TestUsers.All) + { + using var client = await app.LoginAsync(name); + await CheckAsync(client, role); + } + } + + [Fact] + public async Task Spec007_AC04_RoleMatrix_HostAuthentication() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => HeaderAuthenticationHandler.Register(s)); + await SeedAsync(app); + + // Authenticated by the host without any dashboard role: identified, but every action is forbidden. + using (var nobody = app.CreateClient()) + { + nobody.DefaultRequestHeaders.Add(HeaderAuthenticationHandler.Header, "sub-0|Sem Papel|outra-coisa"); + var me = await (await nobody.GetAsync("/psr/api/me", Ct)).JsonAsync(); + me["subject"]!.GetValue().ShouldBe("sub-0"); + me["role"].ShouldBeNull(); + (await nobody.GetAsync("/psr/api/stages", Ct)).StatusCode.ShouldBe(HttpStatusCode.Forbidden); + } + + foreach (var (_, role) in TestUsers.All) + { + using var client = app.CreateClient(); + client.DefaultRequestHeaders.Add(HeaderAuthenticationHandler.Header, $"sub-{role}|Pessoa {role}|{HeaderAuthenticationHandler.RoleClaim(role)}"); + await CheckAsync(client, role); + } + } + + /// + /// Per role (the destructive calls of one role must not change what the next one sees): a published stage eco-<role> + /// and pipeline trilha-<role>, each with a draft of version 2. + /// + private static async Task SeedAsync(DashboardApp app) + { + foreach (var (_, role) in TestUsers.All) + { + var suffix = role.ToString().ToLowerInvariant(); + await app.PublishStageAsync($"eco-{suffix}", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + var pipeline = await app.Admin.CreatePipelineAsync(new NewCatalogItem($"trilha-{suffix}", "Trilha"), PsrEnvironment.Author, Ct); + var saved = await app.Admin.SavePipelineDraftAsync($"trilha-{suffix}", pipeline.Version, + Scripts.SingleStep($"eco-{suffix}", 1, [Scripts.Text("texto")]), pipeline.RowVersion, PsrEnvironment.Author, Ct); + await app.Admin.PublishPipelineAsync($"trilha-{suffix}", saved.Version, saved.RowVersion, PsrEnvironment.Author, Ct); + await app.Admin.CreateStageDraftFromAsync($"eco-{suffix}", 1, PsrEnvironment.Author, Ct); + await app.Admin.CreatePipelineDraftFromAsync($"trilha-{suffix}", 1, PsrEnvironment.Author, Ct); + } + } + + private static async Task CheckAsync(HttpClient client, DashboardRole role) + { + var failures = new List(); + foreach (var (method, path, body, minimum, ok) in Matrix) + { + var suffix = role.ToString().ToLowerInvariant(); + using var request = new HttpRequestMessage(new HttpMethod(method), "/psr/api" + path.Replace("{role}", suffix, StringComparison.Ordinal)); + if (body is not null) + { + request.Content = new StringContent(body.Replace("{role}", suffix, StringComparison.Ordinal), Encoding.UTF8, + "application/json"); + } + + if (method is "PUT" or "PATCH" or "DELETE" || path.EndsWith("/publish", StringComparison.Ordinal)) + { + request.Headers.IfMatch.Add(new System.Net.Http.Headers.EntityTagHeaderValue("\"1\"")); + } + + using var response = await client.SendAsync(request, Ct); + var status = (int)response.StatusCode; + var allowed = minimum is null || role >= minimum; + var expectation = allowed ? (ok ? "2xx" : "not 401/403") : "403"; + var met = allowed ? (ok ? status is >= 200 and < 300 : status is not (401 or 403)) : status == 403; + if (!met) + { + failures.Add($"{role} {method} {path}: {status} (expected {expectation}) {await response.Content.ReadAsStringAsync(Ct)}"); + } + } + + failures.ShouldBeEmpty(); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/CatalogApiTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/CatalogApiTests.cs new file mode 100644 index 0000000..e3c2c3c --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/CatalogApiTests.cs @@ -0,0 +1,111 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json.Nodes; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007: catalog editing through the API (drafts, concurrency, publication, secrets). +public sealed class CatalogApiTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC06_TwoSavesWithTheSameIfMatch_TheSecondGets412WithTheCurrentDraft() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + using var author = await app.LoginAsync("autor"); + (await author.PostAsJsonAsync("/psr/api/stages", new { key = "conflito", name = "Conflito" }, Ct)).StatusCode.ShouldBe(HttpStatusCode.Created); + var read = await author.GetAsync("/psr/api/stages/conflito/draft", Ct); + var etag = read.ETag(); + var data = (await read.JsonAsync())["data"]!.AsObject(); + + data["script"] = Scripts.Echo; + var first = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data, etag); + data["script"] = Scripts.Sleep; + var second = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data, etag); + var withoutIfMatch = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data); + + first.StatusCode.ShouldBe(HttpStatusCode.OK); + first.ETag().ShouldNotBe(etag); + second.StatusCode.ShouldBe(HttpStatusCode.PreconditionFailed); + var problem = await second.JsonAsync(); + problem["code"]!.GetValue().ShouldBe("CONCURRENCY_CONFLICT"); + problem["current"]!["data"]!["script"]!.GetValue().ShouldBe(Scripts.Echo); + withoutIfMatch.StatusCode.ShouldBe((HttpStatusCode)428); + (await withoutIfMatch.JsonAsync())["code"]!.GetValue().ShouldBe("IF_MATCH_REQUIRED"); + } + + [Fact] + public async Task Spec007_AC07_AC12_PublishWithSyntaxError_Gets422WithLineAndColumn_PublicationIsAudited() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + using var author = await app.LoginAsync("autor"); + await author.PostAsJsonAsync("/psr/api/stages", new { key = "quebrado", name = "Quebrado" }, Ct); + var draft = await author.GetAsync("/psr/api/stages/quebrado/draft", Ct); + var data = (await draft.JsonAsync())["data"]!.AsObject(); + data["script"] = "from psrunner import stage\n\n\n@stage\ndef run(inputs, ctx):\n return {\"x\": (1 +}\n"; + var saved = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/quebrado/draft", data, draft.ETag()); + + var publish = await author.SendJsonAsync(HttpMethod.Post, "/psr/api/stages/quebrado/draft/publish", ifMatch: saved.ETag()); + + publish.StatusCode.ShouldBe(HttpStatusCode.UnprocessableEntity); + var problem = await publish.JsonAsync(); + problem["code"]!.GetValue().ShouldBe("SCRIPT_SYNTAX_ERROR"); + problem["line"]!.GetValue().ShouldBe(6); + problem["column"]!.GetValue().ShouldBeGreaterThan(0); + (await app.Admin.GetStageDraftAsync("quebrado", Ct)).ShouldNotBeNull("the draft is kept"); + + data["script"] = Scripts.Echo; + data["inputs"] = new JsonArray(new JsonObject { ["name"] = "texto", ["type"] = "string" }); + data["outputs"] = new JsonArray(new JsonObject { ["name"] = "texto", ["type"] = "string" }); + var fixedDraft = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/quebrado/draft", data, saved.ETag()); + fixedDraft.StatusCode.ShouldBe(HttpStatusCode.OK); + var published = await author.SendJsonAsync(HttpMethod.Post, "/psr/api/stages/quebrado/draft/publish", ifMatch: fixedDraft.ETag()); + published.StatusCode.ShouldBe(HttpStatusCode.OK); + (await published.JsonAsync())["status"]!.GetValue().ShouldBe("Published"); + + (await env.ScalarAsync("SELECT actor FROM psr.audit_events WHERE action = 'StagePublished' AND entity_key = 'quebrado@1'")) + .ShouldBe("login:autor"); + } + + [Fact] + public async Task Spec007_AC16_Secrets_ShowStatusButNeverValues() + { + const string value = "valor-super-secreto-9f3a"; + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => s.Configuration["PythonScriptRunner:Secrets:SMTP_SENHA"] = value); + await app.PublishStageAsync("envia", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")], ["SMTP_SENHA", "API_TOKEN"]); + await app.PublishStageAsync("outra", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")], ["SMTP_SENHA"]); + using var admin = await app.LoginAsync("admin"); + + var response = await admin.GetAsync("/psr/api/admin/secrets", Ct); + var settings = await admin.GetStringAsync("/psr/api/admin/settings", Ct); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + var text = await response.Content.ReadAsStringAsync(Ct); + text.ShouldNotContain(value); + settings.ShouldNotContain(value); + settings.ShouldNotContain(TestUsers.Hash); + var secrets = JsonNode.Parse(text)!.AsArray(); + secrets.Select(s => (s!["name"]!.GetValue(), s["resolved"]!.GetValue())) + .ShouldBe([("API_TOKEN", false), ("SMTP_SENHA", true)]); + secrets[1]!["declaredBy"]!.AsArray().Select(d => d!.GetValue()).ShouldBe(["envia@1", "outra@1"]); + } + + [Fact] + public async Task FieldTypes_DescribeTheTwelveTypesAndTheCompatibility() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + using var reader = await app.LoginAsync("leitor"); + + var types = await (await reader.GetAsync("/psr/api/meta/field-types", Ct)).JsonAsync(); + + types["types"]!.AsArray().Count.ShouldBe(12); + types["compatibility"]!["integer"]!.AsArray().Select(t => t!.GetValue()).ShouldBe(["number", "integer", "decimal", "json"], ignoreOrder: true); + types["compatibility"]!["file"]!.AsArray().Select(t => t!.GetValue()).ShouldBe(["file"]); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/HostAndToolTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/HostAndToolTests.cs new file mode 100644 index 0000000..905c7d9 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/HostAndToolTests.cs @@ -0,0 +1,165 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; +using PythonScriptRunner.Tool; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 009: the standalone DashboardHost (AC-9, AC-10; D-125) and psr hash-password (AC-11; D-124). +public sealed class HostAndToolTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec009_AC09_DashboardHost_FixedLoginUsersFromEnvironment() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var factory = Host(env, web => web + .UseSetting("PythonScriptRunner:Dashboard:Login:Users:0:Name", "ana") + .UseSetting("PythonScriptRunner:Dashboard:Login:Users:0:Role", "Author") + .UseSetting("PythonScriptRunner:Dashboard:Login:Users:0:PasswordHash", TestUsers.Hash)); + using var client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false, HandleCookies = true }); + client.DefaultRequestHeaders.Add("X-PSR-Request", "1"); + + (await client.GetAsync("/psr/api/me", Ct)).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + (await client.PostAsJsonAsync("/psr/api/auth/login", new { name = "ana", password = TestUsers.Password }, Ct)).StatusCode + .ShouldBe(HttpStatusCode.NoContent); + var me = await (await client.GetAsync("/psr/api/me", Ct)).JsonAsync(); + me["role"]!.GetValue().ShouldBe("Author"); + me["subject"]!.GetValue().ShouldBe("login:ana"); + (await client.GetAsync("/health/live", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + (await (await client.GetAsync("/psr/", Ct)).Content.ReadAsStringAsync(Ct)).ShouldContain(""); + } + + [Fact] + public async Task Spec009_AC09_DashboardHost_WithoutUsersAndWithoutOidc_FailsWithAClearMessage() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var factory = Host(env, _ => { }); + + var error = Should.Throw(() => factory.CreateClient()); + + var messages = string.Join(" | ", Unwrap(error).Select(e => e.Message)); + messages.ShouldContain("PythonScriptRunner__Dashboard__Login__Users__0__Name"); + messages.ShouldContain("Oidc__Authority"); + } + + [Fact] + public async Task Spec009_AC10_DashboardHost_WithOidc_ChallengesThePanelAndTheApiAnswers401() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var factory = Host(env, web => web + .UseSetting("Oidc:Authority", "https://idp.exemplo.com") + .UseSetting("Oidc:ClientId", "psr-dashboard") + .UseSetting("Oidc:ClientSecret", "segredo") + .ConfigureTestServices(services => services.Configure(OpenIdConnectDefaults.AuthenticationScheme, o => + o.Configuration = new OpenIdConnectConfiguration + { + Issuer = "https://idp.exemplo.com", + AuthorizationEndpoint = "https://idp.exemplo.com/authorize", + EndSessionEndpoint = "https://idp.exemplo.com/logout", + }))); + using var client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); + + var panel = await client.GetAsync("/psr/execucoes", Ct); + var api = await client.GetAsync("/psr/api/stages", Ct); + var auth = await (await client.GetAsync("/psr/api/auth", Ct)).JsonAsync(); + var login = await client.GetAsync("/account/login?returnUrl=%2Fpsr%2F", Ct); + + panel.StatusCode.ShouldBe(HttpStatusCode.Redirect); + panel.Headers.Location!.ToString().ShouldStartWith("https://idp.exemplo.com/authorize?"); + panel.Headers.Location.Query.ShouldContain("client_id=psr-dashboard"); + api.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + (await api.JsonAsync())["code"]!.GetValue().ShouldBe("UNAUTHENTICATED"); + auth["mode"]!.GetValue().ShouldBe("Host"); + auth["loginUrl"]!.GetValue().ShouldBe("/account/login?returnUrl=%2Fpsr%2F"); + login.StatusCode.ShouldBe(HttpStatusCode.Redirect); + login.Headers.Location!.ToString().ShouldStartWith("https://idp.exemplo.com/authorize?"); + } + + [Fact] + public async Task Spec009_AC11_HashPassword_FromStdin_IsAcceptedByTheFixedLoginAndNeverPrinted() + { + const string password = "Uma senha longa 123!"; + var (code, output, error) = RunTool(["hash-password", "--stdin"], password + Environment.NewLine); + + code.ShouldBe(0, error); + var hash = output.Trim(); + DashboardPasswords.IsHash(hash).ShouldBeTrue(); + output.ShouldNotContain(password); + error.ShouldNotContain(password); + + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => + { + s.Configuration.Clear(); + foreach (var (key, value) in TestUsers.Configuration(("carla", DashboardRole.Admin, hash))) + { + s.Configuration[key] = value; + } + }); + using var client = await app.LoginAsync("carla", password); + (await (await client.GetAsync("/psr/api/me", Ct)).JsonAsync())["role"]!.GetValue().ShouldBe("Admin"); + } + + [Fact] + public void Spec009_AC11_HashPassword_TypedTwiceWithoutEcho() + { + var typed = new Queue(["segredo-digitado", "segredo-digitado"]); + var (code, output, error) = RunTool(["hash-password"], string.Empty, typed.Dequeue, redirected: false); + + code.ShouldBe(0, error); + DashboardPasswords.Verify(output.Trim(), "segredo-digitado").ShouldBeTrue(); + (output + error).ShouldNotContain("segredo-digitado"); + error.ShouldContain("Password:"); + + var mismatch = new Queue(["um", "outro"]); + RunTool(["hash-password"], string.Empty, mismatch.Dequeue, redirected: false).Code.ShouldBe(1); + RunTool(["hash-password"], "senha\n", redirected: true).Code.ShouldBe(1, "a redirected input needs --stdin"); + RunTool(["hash-password", "minha-senha"], string.Empty).Code.ShouldBe(1, "the password is never an argument"); + RunTool(["hash-password", "--stdin"], "\n").Code.ShouldBe(1, "empty password"); + RunTool([], string.Empty).Code.ShouldBe(1); + } + + private static (int Code, string Output, string Error) RunTool(string[] args, string input, Func? readSecret = null, bool redirected = true) + { + using var output = new StringWriter(); + using var error = new StringWriter(); + var code = PsrTool.Run(args, new ToolConsole(new StringReader(input), output, error, redirected, + readSecret ?? (() => throw new InvalidOperationException("no terminal")))); + return (code, output.ToString(), error.ToString()); + } + + private WebApplicationFactory Host(PsrEnvironment env, Action configure) => + new WebApplicationFactory().WithWebHostBuilder(web => + { + web.UseSetting("ConnectionStrings:psr", env.Database) + .UseSetting("ConnectionStrings:rabbitmq", env.Broker) + .UseSetting("PythonScriptRunner:WorkRoot", Path.Combine(env.WorkRoot, "host")) + .UseSetting("PythonScriptRunner:UvPath", python.UvPath) + .UseSetting("PythonScriptRunner:EnvironmentCacheRoot", python.EnvironmentCacheRoot) + .UseSetting("PythonScriptRunner:PsrunnerRequirement", python.WheelPath) + .UseSetting("Serilog:MinimumLevel:Default", "Warning"); + configure(web); + }); + + private static IEnumerable Unwrap(Exception error) + { + for (Exception? current = error; current is not null; current = current.InnerException) + { + yield return current; + if (current is AggregateException aggregate) + { + foreach (var inner in aggregate.InnerExceptions.SelectMany(Unwrap)) + { + yield return inner; + } + } + } + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/DashboardApp.cs b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/DashboardApp.cs new file mode 100644 index 0000000..86fd7ce --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/DashboardApp.cs @@ -0,0 +1,378 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Security.Claims; +using System.Text.Encodings.Web; +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Testing.Handlers; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace PythonScriptRunner.Dashboard.Tests.Infrastructure; + +/// What a dashboard test application runs. +public sealed class DashboardAppSettings +{ + public string Prefix { get; set; } = "/psr"; + + public string Environment { get; set; } = "Production"; + + public string InstanceId { get; set; } = $"dash-{Guid.NewGuid().ToString("N")[..8]}"; + + /// Access mode; default: fixed login with from configuration. + public Action Dashboard { get; set; } = o => o.UseFixedLogin(l => l.BindConfiguration()); + + public Dictionary Configuration { get; } = new(TestUsers.Configuration()); + + /// Configuration that a test can change while the application runs (D-128). + public ReloadableConfigurationSource? Reloadable { get; set; } + + public bool Map { get; set; } = true; + + public Action? Services { get; set; } + + /// Extra PythonScriptRunner registrations (e.g. event handlers). + public Action? Builder { get; set; } + + /// Middleware added before the dashboard is mapped (e.g. host authentication). + public Action? Pipeline { get; set; } +} + +/// A web application with the dashboard on a test server, connected to a . +public sealed class DashboardApp : IAsyncDisposable +{ + private DashboardApp(WebApplication app, DashboardAppSettings settings) + { + App = app; + Settings = settings; + } + + public WebApplication App { get; } + + public DashboardAppSettings Settings { get; } + + public IServiceProvider Services => App.Services; + + public TestServer Server => App.GetTestServer(); + + public string Api => $"{Settings.Prefix}/api"; + + public ICatalogAdmin Admin => Services.GetRequiredService(); + + public IRunManager Runs => Services.GetRequiredService(); + + /// Builds and starts the application. + public static async Task StartAsync(PsrEnvironment env, Action? configure = null) + { + var app = Build(env, configure, out var settings); + try + { + await app.StartAsync(PsrEnvironment.Ct); + } + catch + { + // Stops what started before the failure (consumers, relay). + await app.StopAsync(CancellationToken.None); + await app.DisposeAsync(); + throw; + } + + return new DashboardApp(app, settings); + } + + /// Builds the application without starting it (startup failures are thrown by StartAsync). + public static WebApplication Build(PsrEnvironment env, Action? configure, out DashboardAppSettings settings) + { + settings = new DashboardAppSettings(); + configure?.Invoke(settings); + var builder = WebApplication.CreateBuilder(new WebApplicationOptions + { + EnvironmentName = settings.Environment, + ApplicationName = typeof(DashboardApp).Assembly.GetName().Name, + }); + builder.WebHost.UseTestServer(); + builder.Configuration.AddInMemoryCollection(settings.Configuration); + if (settings.Reloadable is not null) + { + ((IConfigurationBuilder)builder.Configuration).Add(settings.Reloadable); + } + + builder.Logging.ClearProviders(); + if (System.Environment.GetEnvironmentVariable("PSR_TEST_LOG") is { Length: > 0 } logFile) + { + builder.Logging.AddFilter(level => level >= LogLevel.Debug); + builder.Logging.AddProvider(new TestFileLoggerProvider(logFile, settings.InstanceId)); + } + + if (env.Time is not null) + { + builder.Services.AddSingleton(env.Time); + } + + builder.Services.AddSingleton(); + var instance = settings.InstanceId; + var dashboard = settings.Dashboard; + var psr = builder.Services + .AddPythonScriptRunner(o => + { + o.WorkRoot = Path.Combine(env.WorkRoot, instance); + o.EnvironmentCacheRoot = env.Python.EnvironmentCacheRoot; + o.UvPath = env.Python.UvPath; + o.PsrunnerRequirement = env.Python.WheelPath; + }) + .AddEntityFrameworkCore(o => + { + o.UseNpgsql(env.Database); + o.Maintenance.Enabled = false; + o.LatestVersionCacheDuration = TimeSpan.Zero; + o.UseDatabaseClock = env.Time is null; + }) + .AddRabbitMq(o => + { + o.ConnectionString = env.Broker; + o.InstanceId = instance; + o.RelayInterval = TimeSpan.FromMilliseconds(100); + o.StartupConnectTimeout = TimeSpan.FromSeconds(3); + }) + .AddDashboard(dashboard); + settings.Builder?.Invoke(psr); + settings.Services?.Invoke(builder.Services); + var app = builder.Build(); + settings.Pipeline?.Invoke(app); + if (settings.Map) + { + app.MapPythonScriptRunnerDashboard(settings.Prefix); + } + + return app; + } + + /// A client with its own cookies; mutating requests carry X-PSR-Request: 1 unless is false. + public DashboardClient CreateClient(string? ip = null, bool csrf = true) + { + var cookies = new CookieContainer(); + var client = new DashboardClient(new CookieContainerHandler(cookies) { InnerHandler = Server.CreateHandler() }, cookies) + { + BaseAddress = Server.BaseAddress, + }; + if (csrf) + { + client.DefaultRequestHeaders.Add("X-PSR-Request", "1"); + } + + if (ip is not null) + { + client.DefaultRequestHeaders.Add(RemoteIpStartupFilter.Header, ip); + } + + return client; + } + + /// A client signed in with the fixed login. + public async Task LoginAsync(string name, string password = TestUsers.Password) + { + var client = CreateClient(); + var response = await client.PostAsJsonAsync($"{Api}/auth/login", new { name, password }, PsrEnvironment.Ct); + response.StatusCode.ShouldBe(HttpStatusCode.NoContent, await response.Content.ReadAsStringAsync(PsrEnvironment.Ct)); + return client; + } + + /// Creates, saves and publishes a stage through the library. + public async Task PublishStageAsync( + string key, string script, IReadOnlyList inputs, IReadOnlyList outputs, IReadOnlyList? secrets = null) + { + var existing = await Admin.GetStageAsync(key, PsrEnvironment.Ct); + var draft = existing is null + ? await Admin.CreateStageAsync(new NewCatalogItem(key, key), PsrEnvironment.Author, PsrEnvironment.Ct) + : await Admin.CreateStageDraftFromAsync(key, existing.Versions[0].Version, PsrEnvironment.Author, PsrEnvironment.Ct); + var saved = await Admin.SaveStageDraftAsync(key, draft.Version, draft.Data with + { + Script = script, + Inputs = inputs, + Outputs = outputs, + RequiredSecrets = secrets ?? [], + }, draft.RowVersion, PsrEnvironment.Author, PsrEnvironment.Ct); + return await Admin.PublishStageAsync(key, saved.Version, saved.RowVersion, PsrEnvironment.Author, PsrEnvironment.Ct); + } + + public async ValueTask DisposeAsync() + { + try + { + await App.StopAsync(CancellationToken.None); + } + catch (ObjectDisposedException) + { + // Already stopped. + } + + await App.DisposeAsync(); + } +} + +/// A client of the test server with its cookies. +public sealed class DashboardClient(HttpMessageHandler handler, CookieContainer cookies) : HttpClient(handler) +{ + public CookieContainer Cookies { get; } = cookies; + + /// The cookies sent to , as a Cookie header. + public string CookieHeader(string path) => Cookies.GetCookieHeader(new Uri(BaseAddress!, path)); +} + +/// Fixed login users of the tests (one per role) and their configuration. +public static class TestUsers +{ + public const string Password = "senha-de-teste"; + + private static readonly Lazy SharedHash = new(() => DashboardPasswords.Hash(Password)); + + public static string Hash => SharedHash.Value; + + public static IReadOnlyList<(string Name, DashboardRole Role)> All { get; } = + [("leitor", DashboardRole.Reader), ("executor", DashboardRole.Executor), ("autor", DashboardRole.Author), ("admin", DashboardRole.Admin)]; + + public static string NameOf(DashboardRole role) => All.Single(u => u.Role == role).Name; + + /// Configuration keys of the users in PythonScriptRunner:Dashboard:Login. + public static Dictionary Configuration(params (string Name, DashboardRole Role, string? Hash)[] users) + { + var list = users.Length > 0 ? users : [.. All.Select(u => (u.Name, u.Role, (string?)null))]; + var configuration = new Dictionary(); + for (var i = 0; i < list.Length; i++) + { + configuration[$"PythonScriptRunner:Dashboard:Login:Users:{i}:Name"] = list[i].Name; + configuration[$"PythonScriptRunner:Dashboard:Login:Users:{i}:Role"] = list[i].Role.ToString(); + configuration[$"PythonScriptRunner:Dashboard:Login:Users:{i}:PasswordHash"] = list[i].Hash ?? Hash; + } + + return configuration; + } +} + +/// Sets the client address of each request (TestServer has none): X-Test-Ip, default 203.0.113.10. +public sealed class RemoteIpStartupFilter : IStartupFilter +{ + public const string Header = "X-Test-Ip"; + + public static readonly IPAddress DefaultAddress = IPAddress.Parse("203.0.113.10"); + + public Action Configure(Action next) => app => + { + app.Use((context, nextMiddleware) => + { + context.Connection.RemoteIpAddress = context.Request.Headers.TryGetValue(Header, out var ip) ? IPAddress.Parse(ip.ToString()) : DefaultAddress; + return nextMiddleware(context); + }); + next(app); + }; +} + +/// Host authentication of the tests: X-Test-User: sub|name|role1,role2; the challenge redirects like a real host. +public sealed class HeaderAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder) + : AuthenticationHandler(options, logger, encoder) +{ + public const string SchemeName = "TestHost"; + public const string Header = "X-Test-User"; + public const string ChallengeLocation = "/host/login"; + + public static void Register(DashboardAppSettings settings, string? loginUrl = "/host/login?volta={returnUrl}") + { + settings.Services += services => services.AddAuthentication(SchemeName).AddScheme(SchemeName, null); + settings.Dashboard = o => o.UseHostAuthentication(r => + { + r.Reader = p => p.RequireClaim("role", "psr-leitor"); + r.Executor = p => p.RequireClaim("role", "psr-executor"); + r.Author = p => p.RequireClaim("role", "psr-autor"); + r.Admin = p => p.RequireClaim("role", "psr-admin"); + r.LoginUrl = loginUrl; + r.LogoutUrl = "/host/logout"; + }); + } + + public static string RoleClaim(DashboardRole role) => role switch + { + DashboardRole.Reader => "psr-leitor", + DashboardRole.Executor => "psr-executor", + DashboardRole.Author => "psr-autor", + _ => "psr-admin", + }; + + protected override Task HandleAuthenticateAsync() + { + if (!Request.Headers.TryGetValue(Header, out var value) || value.ToString().Split('|') is not [var sub, var name, var roles]) + { + return Task.FromResult(AuthenticateResult.NoResult()); + } + + List claims = [new("sub", sub), new(ClaimTypes.Name, name), .. roles.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(r => new Claim("role", r))]; + return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(claims, SchemeName)), SchemeName))); + } + + protected override Task HandleChallengeAsync(AuthenticationProperties properties) + { + Response.Redirect(ChallengeLocation); + return Task.CompletedTask; + } +} + +/// Custom filter of the tests: X-Test-Filter: name|Role; no header denies. +public sealed class HeaderFilter : IDashboardAuthorizationFilter +{ + public const string Header = "X-Test-Filter"; + + public ValueTask AuthorizeAsync(HttpContext context) => + ValueTask.FromResult(context.Request.Headers.TryGetValue(Header, out var value) && value.ToString().Split('|') is [var name, var role] + ? new DashboardUser(name, Enum.Parse(role)) + : null); +} + +/// Configuration replaced by a test while the application runs. +public sealed class ReloadableConfigurationSource : IConfigurationSource +{ + public ReloadableConfigurationProvider Provider { get; } = new(); + + public IConfigurationProvider Build(IConfigurationBuilder builder) => Provider; +} + +public sealed class ReloadableConfigurationProvider : ConfigurationProvider +{ + public void Replace(IDictionary values) + { + Data = new Dictionary(values, StringComparer.OrdinalIgnoreCase); + OnReload(); + } +} + +/// Request helpers. +public static class Api +{ + public static async Task JsonAsync(this HttpResponseMessage response) + { + var text = await response.Content.ReadAsStringAsync(PsrEnvironment.Ct); + return JsonNode.Parse(text) ?? throw new InvalidOperationException("Empty body."); + } + + public static async Task SendJsonAsync(this HttpClient client, HttpMethod method, string url, object? body = null, string? ifMatch = null) + { + using var request = new HttpRequestMessage(method, url); + if (body is not null) + { + request.Content = JsonContent.Create(body); + } + + if (ifMatch is not null) + { + request.Headers.IfMatch.Add(new EntityTagHeaderValue(ifMatch)); + } + + return await client.SendAsync(request, PsrEnvironment.Ct); + } + + public static string ETag(this HttpResponseMessage response) => response.Headers.ETag?.Tag ?? throw new InvalidOperationException("No ETag."); +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs new file mode 100644 index 0000000..8f2b8bb --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs @@ -0,0 +1,41 @@ +using System.Runtime.CompilerServices; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace PythonScriptRunner.Dashboard.Tests.Infrastructure; + +/// +/// Versioned JSON snapshots next to the tests (Snapshots/<name>.verified.json). A difference writes +/// <name>.received.json and fails; review it and replace the verified file, or run with PSR_ACCEPT_SNAPSHOTS=1. +/// +public static class Snapshot +{ + private static readonly JsonSerializerOptions Indented = new() { WriteIndented = true, NewLine = "\n", Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; + + public static async Task MatchJsonAsync(string name, string json, [CallerFilePath] string testFile = "") + { + var folder = Path.Combine(Path.GetDirectoryName(testFile)!, "Snapshots"); + var verified = Path.Combine(folder, $"{name}.verified.json"); + var received = Path.Combine(folder, $"{name}.received.json"); + var actual = (JsonNode.Parse(json) ?? throw new ArgumentException("Empty JSON.", nameof(json))).ToJsonString(Indented) + "\n"; + var expected = File.Exists(verified) ? (await File.ReadAllTextAsync(verified)).ReplaceLineEndings("\n") : null; + if (actual == expected) + { + File.Delete(received); + return; + } + + Directory.CreateDirectory(folder); + if (Environment.GetEnvironmentVariable("PSR_ACCEPT_SNAPSHOTS") == "1") + { + await File.WriteAllTextAsync(verified, actual); + return; + } + + await File.WriteAllTextAsync(received, actual); + Assert.Fail(expected is null + ? $"No snapshot {verified}; review {received} and rename it to .verified.json." + : $"The snapshot changed: compare {received} with {verified} and replace the verified file when the change is intended."); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/PerformanceTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/PerformanceTests.cs new file mode 100644 index 0000000..360e654 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/PerformanceTests.cs @@ -0,0 +1,94 @@ +using System.Diagnostics; +using System.Globalization; +using System.Net; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// +/// Spec 007, AC-13 (D-116): with 90 days of synthetic runs, the main reads answer with p95 ≤ 500 ms. The CI uses ~200 thousand +/// runs; PSR_PERF_RUNS_PER_DAY=50000 runs the full medium volume (D-53) outside the CI. +/// +public sealed class PerformanceTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private const int Samples = 30; + + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC13_MainReadsWith90DaysOfRuns_AnswerWithP95Under500Ms() + { + var perDay = int.TryParse(Environment.GetEnvironmentVariable("PSR_PERF_RUNS_PER_DAY"), CultureInfo.InvariantCulture, out var configured) + ? configured + : 2_250; + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await env.ScalarAsync("SELECT psr.ensure_partitions(now() - interval '91 days', 14, 0, 0)"); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("perf", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + for (var i = 0; i < 40; i++) + { + await app.Admin.CreateStageAsync(new NewCatalogItem($"stage-{i:00}", $"Stage {i}"), PsrEnvironment.Author, Ct); + } + + // Synthetic history: UUID v7 ids carrying the creation time (the partition of each run), statuses as in production. + for (var day = 0; day < 90; day++) + { + await env.ScalarAsync($$""" + WITH rows AS ( + SELECT date_trunc('milliseconds', now() - interval '{{day}} days' - random() * interval '1 day') AS ts, + (ARRAY['Succeeded', 'Succeeded', 'Succeeded', 'Succeeded', 'Failed', 'Cancelled'])[1 + floor(random() * 6)::int] AS status + FROM generate_series(1, {{perDay}})), + inserted AS ( + INSERT INTO psr.runs (id, created_at, kind, mode, stage_key, stage_version, trigger, triggered_by, status, inputs, + queued_at, started_at, finished_at, error) + SELECT (lpad(to_hex(floor(extract(epoch FROM ts) * 1000)::bigint), 12, '0') || '7' || substr(md5(random()::text), 1, 3) + || '8' || substr(md5(random()::text), 1, 15))::uuid, + ts, 'Stage', 'Async', 'perf', 1, 'Library', 'system:library', status, '{"texto": "x"}'::jsonb, + ts, ts + interval '1 second', ts + interval '4 seconds', + CASE WHEN status = 'Failed' THEN '{"kind": "ScriptError", "message": "falhou"}'::jsonb END + FROM rows + RETURNING 1) + SELECT count(*)::int FROM inserted + """); + } + + await env.ScalarAsync("SELECT 1; ANALYZE psr.runs"); + var ids = await env.ColumnAsync($"SELECT id FROM psr.runs ORDER BY random() LIMIT {Samples}"); + using var reader = await app.LoginAsync("leitor"); + + var results = new Dictionary + { + ["/runs"] = await P95Async(reader, _ => "/psr/api/runs?limit=50"), + ["/runs?status=Failed&from=-7d"] = await P95Async(reader, _ => + $"/psr/api/runs?status=Failed&limit=50&from={Uri.EscapeDataString(DateTimeOffset.UtcNow.AddDays(-7).ToString("O"))}"), + ["/runs/{id}"] = await P95Async(reader, i => $"/psr/api/runs/{ids[i]}"), + ["/stages"] = await P95Async(reader, _ => "/psr/api/stages?pageSize=25"), + ["/overview"] = await P95Async(reader, _ => "/psr/api/overview"), + }; + + TestContext.Current.TestOutputHelper?.WriteLine( + $"{perDay * 90} runs: " + string.Join(", ", results.Select(r => $"{r.Key} p95 {r.Value.TotalMilliseconds:0} ms"))); + results.Where(r => r.Value > TimeSpan.FromMilliseconds(500)).Select(r => $"{r.Key}: {r.Value.TotalMilliseconds:0} ms").ShouldBeEmpty(); + } + + private static async Task P95Async(HttpClient client, Func path) + { + for (var i = 0; i < 3; i++) + { + (await client.GetAsync(path(i), Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + } + + var times = new List(); + for (var i = 0; i < Samples; i++) + { + var start = Stopwatch.GetTimestamp(); + using var response = await client.GetAsync(path(i), Ct); + await response.Content.ReadAsByteArrayAsync(Ct); + times.Add(Stopwatch.GetElapsedTime(start)); + response.StatusCode.ShouldBe(HttpStatusCode.OK, path(i)); + } + + times.Sort(); + return times[(int)Math.Ceiling(0.95 * times.Count) - 1]; + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/PrefixAndOpenApiTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/PrefixAndOpenApiTests.cs new file mode 100644 index 0000000..50dbd01 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/PrefixAndOpenApiTests.cs @@ -0,0 +1,80 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.SignalR.Client; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007: custom prefix (AC-14), the panel page and the internal OpenAPI document (AC-15). +public sealed class PrefixAndOpenApiTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC14_CustomPrefix_ApiHubCookieAndPanelUseIt() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env, s => s.Prefix = "/admin/python"); + using var client = app.CreateClient(); + + var login = await client.PostAsJsonAsync("/admin/python/api/auth/login", new { name = "leitor", password = TestUsers.Password }, Ct); + login.StatusCode.ShouldBe(HttpStatusCode.NoContent); + login.Headers.GetValues("Set-Cookie").Single(c => c.StartsWith("psr.session=", StringComparison.Ordinal)).ToLowerInvariant() + .ShouldContain("path=/admin/python"); + (await (await client.GetAsync("/admin/python/api/me", Ct)).JsonAsync())["role"]!.GetValue().ShouldBe("Reader"); + (await client.GetAsync("/psr/api/auth", Ct)).StatusCode.ShouldBe(HttpStatusCode.NotFound); + + var page = await client.GetAsync("/admin/python/execucoes/123", Ct); + page.StatusCode.ShouldBe(HttpStatusCode.OK); + page.Content.Headers.ContentType!.MediaType.ShouldBe("text/html"); + (await page.Content.ReadAsStringAsync(Ct)).ShouldContain(""); + page.Headers.GetValues("Content-Security-Policy").Single().ShouldContain("frame-ancestors 'none'"); + page.Headers.GetValues("X-Content-Type-Options").Single().ShouldBe("nosniff"); + page.Headers.GetValues("Referrer-Policy").Single().ShouldBe("same-origin"); + + var unknown = await client.GetAsync("/admin/python/api/nao-existe", Ct); + unknown.StatusCode.ShouldBe(HttpStatusCode.NotFound); + unknown.Content.Headers.ContentType!.MediaType.ShouldBe("application/problem+json"); + + var cookie = client.CookieHeader("/admin/python/hubs/runs"); + cookie.ShouldStartWith("psr.session="); + await using var hub = new HubConnectionBuilder() + .WithUrl(new Uri(app.Server.BaseAddress, "/admin/python/hubs/runs"), o => + { + o.HttpMessageHandlerFactory = _ => app.Server.CreateHandler(); + o.Transports = HttpTransportType.LongPolling; + o.Headers["Cookie"] = cookie; + }) + .Build(); + await hub.StartAsync(Ct); + await hub.InvokeAsync("SubscribeAll", Ct); + + await using var anonymous = new HubConnectionBuilder() + .WithUrl(new Uri(app.Server.BaseAddress, "/admin/python/hubs/runs"), o => + { + o.HttpMessageHandlerFactory = _ => app.Server.CreateHandler(); + o.Transports = HttpTransportType.LongPolling; + }) + .Build(); + (await Should.ThrowAsync(() => anonymous.StartAsync(Ct))).StatusCode.ShouldBe(HttpStatusCode.Unauthorized); + } + + [Fact] + public async Task Spec007_AC15_OpenApiDocument_MatchesTheSnapshot() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using (var production = await DashboardApp.StartAsync(env)) + { + using var client = production.CreateClient(); + (await client.GetAsync("/psr/api/openapi/v1.json", Ct)).StatusCode.ShouldBe(HttpStatusCode.NotFound, "only in Development"); + } + + await using var app = await DashboardApp.StartAsync(env, s => s.Environment = "Development"); + using var development = app.CreateClient(); + var document = await development.GetAsync("/psr/api/openapi/v1.json", Ct); + + document.StatusCode.ShouldBe(HttpStatusCode.OK); + await Snapshot.MatchJsonAsync("openapi-v1", await document.Content.ReadAsStringAsync(Ct)); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj b/tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj new file mode 100644 index 0000000..0ba59c4 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/PythonScriptRunner.Dashboard.Tests/RealtimeTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/RealtimeTests.cs new file mode 100644 index 0000000..1d588a5 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/RealtimeTests.cs @@ -0,0 +1,147 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Net.Http.Json; +using System.Text.Json; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.SignalR.Client; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007, AC-11: run updates reach the browsers of every dashboard instance in ≤ 1 s. +public sealed class RealtimeTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC11_SubscribedClientsOfTwoInstances_ReceiveStepAndRunUpdatesInUnderOneSecond() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await env.StartHostAsync(s => s.RecordEvents = false); + await using var first = await StartAsync(env); + await using var second = await StartAsync(env); + await first.App.PublishStageAsync("lento", Scripts.Sleep, [Scripts.Number("segundos"), Scripts.Text("texto")], [Scripts.Text("texto")]); + await using var byStage = await Browser.ConnectAsync(first.App); + await using var byRun = await Browser.ConnectAsync(second.App); + await byStage.Connection.InvokeAsync("SubscribeStage", "lento", Ct); + using var executor = first.App.CreateClient(); + executor.DefaultRequestHeaders.Add(HeaderFilter.Header, "ana|Executor"); + + var accepted = await executor.PostAsJsonAsync("/psr/api/stages/lento/runs", new { inputs = new { segundos = 1.5, texto = "oi" } }, Ct); + var runId = (await accepted.JsonAsync())["runId"]!.GetValue(); + await byRun.Connection.InvokeAsync("SubscribeRun", runId, Ct); + + foreach (var (browser, instance) in ((Browser, Instance)[])[(byStage, first), (byRun, second)]) + { + var finished = await browser.WaitForRunAsync(runId, "run.succeeded"); + finished.Message.GetProperty("run").GetProperty("status").GetString().ShouldBe("Succeeded"); + finished.Message.GetProperty("currentStep").GetString().ShouldBe("lento"); + var delivered = instance.Recorder.ReceivedAt(runId, "run.succeeded"); + Stopwatch.GetElapsedTime(delivered, finished.At).ShouldBeLessThanOrEqualTo(TimeSpan.FromSeconds(1)); + + var step = await browser.WaitForStepAsync(runId, "Succeeded"); + step.GetProperty("stepKey").GetString().ShouldBe("lento"); + step.GetProperty("order").GetInt32().ShouldBe(1); + step.GetProperty("attempt").GetInt32().ShouldBe(1); + } + + var started = await byStage.WaitForStepAsync(runId, "Running"); + started.GetProperty("finishedAt").ValueKind.ShouldBe(JsonValueKind.Null); + } + + private static async Task StartAsync(PsrEnvironment env) + { + var recorder = new TimedRecorder(); + var app = await DashboardApp.StartAsync(env, s => + { + s.Dashboard = o => o.UseAuthorizationFilter(); + s.Builder = b => b.AddRunEventHandler(r => r.PerInstance("timing")); + s.Services = services => services.AddSingleton(recorder); + }); + return new Instance(app, recorder); + } + + private sealed record Instance(DashboardApp App, TimedRecorder Recorder) : IAsyncDisposable + { + public ValueTask DisposeAsync() => App.DisposeAsync(); + } + + /// When each event reached the instance (same moment the dashboard bridge receives it, on another queue). + private sealed class TimedRecorder : IRunEventHandler + { + private readonly ConcurrentDictionary<(string, string), long> _received = new(); + + public Task HandleAsync(RunEvent runEvent, CancellationToken cancellationToken) + { + _received.TryAdd((runEvent.RunId?.ToString() ?? string.Empty, runEvent.Type), Stopwatch.GetTimestamp()); + return Task.CompletedTask; + } + + public long ReceivedAt(string runId, string type) => _received[(runId, type)]; + } + + /// A SignalR client of the runs hub (long polling through the test server). + private sealed class Browser : IAsyncDisposable + { + private readonly ConcurrentQueue<(long At, JsonElement Message)> _runs = new(); + private readonly ConcurrentQueue _steps = new(); + + private Browser(HubConnection connection) + { + Connection = connection; + connection.On("RunUpdated", message => _runs.Enqueue((Stopwatch.GetTimestamp(), message))); + connection.On("StepUpdated", _steps.Enqueue); + } + + public HubConnection Connection { get; } + + public static async Task ConnectAsync(DashboardApp app) + { + var connection = new HubConnectionBuilder() + .WithUrl(new Uri(app.Server.BaseAddress, "/psr/hubs/runs"), o => + { + o.HttpMessageHandlerFactory = _ => app.Server.CreateHandler(); + o.Transports = HttpTransportType.LongPolling; + o.Headers[HeaderFilter.Header] = "bia|Reader"; + }) + .Build(); + var browser = new Browser(connection); + await connection.StartAsync(Ct); + return browser; + } + + public async Task<(long At, JsonElement Message)> WaitForRunAsync(string runId, string eventType) + { + (long, JsonElement) found = default; + await Eventually.WaitAsync(() => Task.FromResult(_runs.Any(r => + { + var match = r.Message.GetProperty("run").GetProperty("id").GetString() == runId && r.Message.GetProperty("eventType").GetString() == eventType; + if (match) + { + found = r; + } + + return match; + })), TimeSpan.FromSeconds(60), () => $"no {eventType}; got {string.Join(", ", _runs.Select(r => r.Message.GetProperty("eventType").GetString()))}"); + return found; + } + + public async Task WaitForStepAsync(string runId, string status) + { + JsonElement found = default; + await Eventually.WaitAsync(() => Task.FromResult(_steps.Any(s => + { + var match = s.GetProperty("runId").GetString() == runId && s.GetProperty("status").GetString() == status; + if (match) + { + found = s; + } + + return match; + })), TimeSpan.FromSeconds(30), () => $"no step {status}"); + return found; + } + + public async ValueTask DisposeAsync() => await Connection.DisposeAsync(); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/RunApiTests.cs b/tests/PythonScriptRunner.Dashboard.Tests/RunApiTests.cs new file mode 100644 index 0000000..d9571e8 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/RunApiTests.cs @@ -0,0 +1,268 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json.Nodes; +using DotNet.Testcontainers.Builders; +using Microsoft.Extensions.Time.Testing; +using PythonScriptRunner.Dashboard.Tests.Infrastructure; + +namespace PythonScriptRunner.Dashboard.Tests; + +/// Spec 007: runs, uploads, downloads, lists and the overview through the API. +public sealed class RunApiTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) +{ + private const string Copy = """ + import shutil + + from psrunner import stage + + + @stage + def run(inputs, ctx): + original = inputs["arquivo"] + copia = ctx.new_file("copia-" + original.name, original.content_type) + shutil.copyfile(original.path, copia.path) + return {"copia": copia} + """; + + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task Spec007_AC08_UploadRunsAndTheOutputIsDownloadable_TooLargeGets413WithoutARun() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await env.StartHostAsync(s => s.RecordEvents = false); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("copia", Copy, [Scripts.File("arquivo")], [Scripts.File("copia")]); + using var executor = await app.LoginAsync("executor"); + var content = Encoding.UTF8.GetBytes(new string('x', 10 * 1024)); + + using var form = new MultipartFormDataContent + { + { new StringContent("{}", Encoding.UTF8, "application/json"), "inputs" }, + { new ByteArrayContent(content) { Headers = { ContentType = new MediaTypeHeaderValue("text/plain") } }, "arquivo", "relatorio.txt" }, + }; + var accepted = await executor.PostAsync("/psr/api/stages/copia/runs", form, Ct); + + accepted.StatusCode.ShouldBe(HttpStatusCode.Accepted, await accepted.Content.ReadAsStringAsync(Ct)); + var runId = RunId.Parse((await accepted.JsonAsync())["runId"]!.GetValue()); + accepted.Headers.Location!.ToString().ShouldBe($"/psr/api/runs/{runId}"); + RunSnapshot? run = null; + await Eventually.WaitAsync(async () => (run = await app.Runs.GetAsync(runId, Ct))?.Status is RunStatus.Succeeded or RunStatus.Failed, + TimeSpan.FromSeconds(90), () => $"run is {run?.Status}"); + run!.Status.ShouldBe(RunStatus.Succeeded, run.Error?.Message); + var detail = await (await executor.GetAsync($"/psr/api/runs/{runId}", Ct)).JsonAsync(); + var output = detail["artifacts"]!.AsArray().Single(a => a!["direction"]!.GetValue() == "Output")!; + var download = await executor.GetAsync($"/psr/api/artifacts/{output["id"]!.GetValue()}", Ct); + download.StatusCode.ShouldBe(HttpStatusCode.OK); + download.Content.Headers.ContentDisposition!.DispositionType.ShouldBe("attachment"); + download.Content.Headers.ContentDisposition.FileNameStar.ShouldBe("copia-relatorio.txt"); + download.Headers.GetValues("X-Content-Type-Options").ShouldContain("nosniff"); + (await download.Content.ReadAsByteArrayAsync(Ct)).ShouldBe(content); + var attempt = run.Steps.Single().Attempts.Single(); + (await executor.GetAsync($"/psr/api/runs/{runId}/attempts/{attempt.Id}/logs/stdout", Ct)).StatusCode + .ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.NotFound); + + // 51 MB: refused before any run is created. + var runs = await env.ScalarAsync("SELECT count(*) FROM psr.runs"); + using var large = new MultipartFormDataContent + { + { new StringContent("{}", Encoding.UTF8, "application/json"), "inputs" }, + { new ByteArrayContent(new byte[51 * 1024 * 1024]), "arquivo", "grande.bin" }, + }; + var tooLarge = await executor.PostAsync("/psr/api/stages/copia/runs", large, Ct); + tooLarge.StatusCode.ShouldBe(HttpStatusCode.RequestEntityTooLarge); + (await tooLarge.JsonAsync())["code"]!.GetValue().ShouldBe("PAYLOAD_TOO_LARGE"); + (await env.ScalarAsync("SELECT count(*) FROM psr.runs")).ShouldBe(runs); + Directory.EnumerateFiles(Path.Combine(env.WorkRoot, app.Settings.InstanceId, "uploads")).ShouldBeEmpty("temporary uploads are deleted"); + } + + [Fact] + public async Task Spec007_AC08_HtmlToEmail_RunsFromThePanelAndThePdfIsDownloadable() + { + // weasyprint needs pango (GTK) at runtime: the CI runs this test on Linux (D-106). + Assert.SkipWhen(OperatingSystem.IsWindows(), "weasyprint needs GTK/pango; CS-2 runs on Linux (D-106)."); + const ushort smtpPort = 1025; + await using var mailpit = new ContainerBuilder("axllent/mailpit:v1.27.0") + .WithEnvironment("MP_SMTP_AUTH_ACCEPT_ANY", "1") + .WithEnvironment("MP_SMTP_AUTH_ALLOW_INSECURE", "1") + .WithPortBinding(smtpPort, true) + .WithPortBinding(8025, true) + .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(8025).ForPath("/livez"))) + .Build(); + await mailpit.StartAsync(Ct); + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + var worker = await env.StartHostAsync(s => + { + s.RecordEvents = false; + s.Configuration["PythonScriptRunner:Secrets:SMTP_USUARIO"] = "psr"; + s.Configuration["PythonScriptRunner:Secrets:SMTP_SENHA"] = "segredo-de-teste"; + }); + await env.ImportFixtureAsync(worker, "html-para-email", (path, content) => path.EndsWith(".pipeline.json", StringComparison.Ordinal) + ? content.Replace("smtp.exemplo.com", mailpit.Hostname, StringComparison.Ordinal) + .Replace("\"const\": 465", $"\"const\": {mailpit.GetMappedPublicPort(smtpPort)}", StringComparison.Ordinal) + : content); + await using var app = await DashboardApp.StartAsync(env); + using var executor = await app.LoginAsync("executor"); + var html = "

Relatório

" + new string('x', 10 * 1024) + "

"; + + using var form = new MultipartFormDataContent + { + { JsonContent.Create(new { destinatarios = new[] { "financeiro@exemplo.com" }, assunto = "Relatório" }), "inputs" }, + { new StringContent(html, Encoding.UTF8, "text/html"), "html", "pagina.html" }, + }; + var accepted = await executor.PostAsync("/psr/api/pipelines/html-para-email/runs", form, Ct); + + accepted.StatusCode.ShouldBe(HttpStatusCode.Accepted, await accepted.Content.ReadAsStringAsync(Ct)); + var run = await worker.WaitForRunAsync(RunId.Parse((await accepted.JsonAsync())["runId"]!.GetValue()), timeout: TimeSpan.FromMinutes(5)); + run.Status.ShouldBe(RunStatus.Succeeded, run.Error?.Message); + var pdf = run.Artifacts.Single(a => a.Direction == ArtifactDirection.Output && a.FieldName == "pdf"); + var download = await executor.GetAsync($"/psr/api/artifacts/{pdf.Id}", Ct); + download.Content.Headers.ContentType!.MediaType.ShouldBe("application/pdf"); + Encoding.ASCII.GetString((await download.Content.ReadAsByteArrayAsync(Ct))[..4]).ShouldBe("%PDF"); + } + + [Fact] + public async Task Spec007_AC09_Runs_WindowIsLimitedAndTheCursorWalksEveryRunOnce() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + var queue = app.Services.GetRequiredService(); + var created = new HashSet(); + for (var i = 0; i < 120; i++) + { + created.Add((await queue.EnqueueStageAsync("eco", new { texto = $"t{i}" }, cancellationToken: Ct)).RunId); + } + + using var reader = await app.LoginAsync("leitor"); + var now = DateTimeOffset.UtcNow; + var tooLong = await reader.GetAsync($"/psr/api/runs?from={Uri.EscapeDataString(now.AddDays(-120).ToString("O"))}", Ct); + tooLong.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + + var seen = new List(); + string? cursor = null; + var pages = 0; + do + { + var page = await (await reader.GetAsync($"/psr/api/runs?limit=50{(cursor is null ? "" : $"&cursor={cursor}")}", Ct)).JsonAsync(); + seen.AddRange(page["items"]!.AsArray().Select(r => RunId.Parse(r!["id"]!.GetValue()))); + cursor = page["nextCursor"]?.GetValue(); + pages++; + } + while (cursor is not null); + + pages.ShouldBe(3); + seen.Count.ShouldBe(120); + seen.ToHashSet().SetEquals(created).ShouldBeTrue(); + } + + [Fact] + public async Task Spec007_AC10_ExpiredArtifact_Gets410() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("copia", Copy, [Scripts.File("arquivo")], [Scripts.File("copia")]); + var handle = await app.Services.GetRequiredService() + .EnqueueStageAsync("copia", new { arquivo = PsrFile.FromText("conteúdo", "a.txt") }, cancellationToken: Ct); + var artifact = (await app.Runs.GetAsync(handle.RunId, Ct))!.Artifacts.Single(); + using var reader = await app.LoginAsync("leitor"); + (await reader.GetAsync($"/psr/api/artifacts/{artifact.Id}", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); + + await env.ScalarAsync($"WITH u AS (UPDATE psr.artifacts SET content_deleted_at = now() WHERE id = '{artifact.Id}' RETURNING 1) SELECT count(*)::int FROM u"); + var expired = await reader.GetAsync($"/psr/api/artifacts/{artifact.Id}", Ct); + + expired.StatusCode.ShouldBe(HttpStatusCode.Gone); + (await expired.JsonAsync())["code"]!.GetValue().ShouldBe("ARTIFACT_EXPIRED"); + } + + [Fact] + public async Task Spec007_AC12_TriggerAndCancelFromThePanel_AreAuditedWithThePanelUser() + { + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + using var executor = await app.LoginAsync("executor"); + + var accepted = await executor.PostAsJsonAsync("/psr/api/stages/eco/runs", new { inputs = new { texto = "oi" }, idempotencyKey = "k-1" }, Ct); + var runId = (await accepted.JsonAsync())["runId"]!.GetValue(); + var cancel = await executor.PostAsync($"/psr/api/runs/{runId}/cancel", null, Ct); + var again = await executor.PostAsync($"/psr/api/runs/{runId}/cancel", null, Ct); + + accepted.StatusCode.ShouldBe(HttpStatusCode.Accepted); + cancel.StatusCode.ShouldBe(HttpStatusCode.Accepted); + again.StatusCode.ShouldBe(HttpStatusCode.Conflict); + (await again.JsonAsync())["code"]!.GetValue().ShouldBe("RUN_NOT_CANCELLABLE"); + (await env.ScalarAsync($"SELECT triggered_by || ' ' || trigger || ' ' || status FROM psr.runs WHERE id = '{runId}'")) + .ShouldBe("login:executor Dashboard Cancelled"); + var audit = await env.ColumnAsync($"SELECT action || ' ' || actor FROM psr.audit_events WHERE entity_id = '{runId}' ORDER BY occurred_at, id"); + audit.ShouldBe(["RunTriggered login:executor", "RunCancelled login:executor"], ignoreOrder: true); + } + + [Fact] + public async Task Spec007_AC25_Overview_CountsTheRunsOfBothDaysFailuresAndNextSchedules() + { + // The simulated clock only moves forward: it starts 50 h ago and walks to "now". + var now = DateTimeOffset.UtcNow; + var time = new FakeTimeProvider(now.AddHours(-50)); + await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time); + await env.ScalarAsync("SELECT psr.ensure_partitions(now() - interval '14 days', 4, 0, 0)"); + await using var app = await DashboardApp.StartAsync(env); + await app.PublishStageAsync("eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); + var queue = app.Services.GetRequiredService(); + + var outside = (await queue.EnqueueStageAsync("eco", new { texto = "fora das 48 h" }, cancellationToken: Ct)).RunId; + time.SetUtcNow(now.AddHours(-36)); + var older = new List(); + for (var i = 0; i < 3; i++) + { + older.Add((await queue.EnqueueStageAsync("eco", new { texto = "antes" }, cancellationToken: Ct)).RunId); + } + + time.SetUtcNow(now.AddHours(-2)); + var recent = new List(); + for (var i = 0; i < 4; i++) + { + recent.Add((await queue.EnqueueStageAsync("eco", new { texto = "agora" }, cancellationToken: Ct)).RunId); + } + + time.SetUtcNow(now); + await app.Runs.CancelAsync(recent[0], PsrEnvironment.Author, Ct); + await env.ScalarAsync($$""" + WITH u AS ( + UPDATE psr.runs SET status = 'Failed', finished_at = created_at + interval '1 minute', + error = '{"kind":"ScriptError","message":"falhou","code":"X"}' + WHERE id IN ('{{recent[1]}}', '{{older[0]}}') RETURNING 1) + SELECT count(*)::int FROM u + """); + var pipeline = await app.Admin.CreatePipelineAsync(new NewCatalogItem("trilha", "Trilha"), PsrEnvironment.Author, Ct); + var saved = await app.Admin.SavePipelineDraftAsync("trilha", pipeline.Version, Scripts.SingleStep("eco", 1, [Scripts.Text("texto")]), + pipeline.RowVersion, PsrEnvironment.Author, Ct); + await app.Admin.PublishPipelineAsync("trilha", saved.Version, saved.RowVersion, PsrEnvironment.Author, Ct); + var schedule = await app.Admin.CreateScheduleAsync("trilha", new ScheduleData + { + Name = "De hora em hora", + Cron = "0 * * * *", + Inputs = new JsonObject { ["texto"] = "agendado" }, + }, PsrEnvironment.Author, Ct); + using var reader = await app.LoginAsync("leitor"); + + var overview = await (await reader.GetAsync("/psr/api/overview", Ct)).JsonAsync(); + + overview["last24Hours"]!["Queued"]!.GetValue().ShouldBe(2); + overview["last24Hours"]!["Cancelled"]!.GetValue().ShouldBe(1); + overview["last24Hours"]!["Failed"]!.GetValue().ShouldBe(1); + overview["last24Hours"]!["Succeeded"]!.GetValue().ShouldBe(0); + overview["previous24Hours"]!["Queued"]!.GetValue().ShouldBe(2); + overview["previous24Hours"]!["Failed"]!.GetValue().ShouldBe(1); + overview["inProgress"]!.AsArray().Select(r => r!["id"]!.GetValue()).ShouldBe( + [recent[3].ToString(), recent[2].ToString(), older[2].ToString(), older[1].ToString(), outside.ToString()], ignoreOrder: true); + overview["recentFailures"]!.AsArray().Select(r => r!["id"]!.GetValue()).ShouldBe([recent[1].ToString(), older[0].ToString()]); + overview["recentFailures"]![0]!["error"]!["message"]!.GetValue().ShouldBe("falhou"); + var next = overview["upcomingSchedules"]!.AsArray().Single()!; + next["id"]!.GetValue().ShouldBe(schedule.Id.ToString()); + next["pipelineKey"]!.GetValue().ShouldBe("trilha"); + next["nextRunAt"]!.GetValue().ShouldBe(schedule.NextRunAt!.Value); + } +} diff --git a/tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json b/tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json new file mode 100644 index 0000000..5d5b099 --- /dev/null +++ b/tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json @@ -0,0 +1,5684 @@ +{ + "openapi": "3.1.1", + "info": { + "title": "PythonScriptRunner Dashboard (internal API)", + "version": "v1" + }, + "paths": { + "/psr/api/auth": { + "get": { + "tags": [ + "Access" + ], + "operationId": "GetAuth", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AuthInfo" + } + } + } + } + } + } + }, + "/psr/api/auth/login": { + "post": { + "tags": [ + "Access" + ], + "operationId": "Login", + "requestBody": { + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "type": "null" + }, + { + "$ref": "#/components/schemas/LoginRequest" + } + ] + } + } + } + }, + "responses": { + "204": { + "description": "No Content" + }, + "401": { + "description": "Unauthorized", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "429": { + "description": "Too Many Requests", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/auth/logout": { + "post": { + "tags": [ + "Access" + ], + "operationId": "Logout", + "responses": { + "204": { + "description": "No Content" + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/me": { + "get": { + "tags": [ + "Access" + ], + "operationId": "GetMe", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MeResponse" + } + } + } + } + } + } + }, + "/psr/api/import": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "Import", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ImportReport" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "413": { + "description": "Payload Too Large", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/overview": { + "get": { + "tags": [ + "Runs" + ], + "operationId": "GetOverview", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/OverviewResponse" + } + } + } + } + } + } + }, + "/psr/api/artifacts/{id}": { + "get": { + "tags": [ + "Runs" + ], + "operationId": "DownloadArtifact", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "410": { + "description": "Gone", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/audit": { + "get": { + "tags": [ + "Administration" + ], + "operationId": "ListAudit", + "parameters": [ + { + "name": "Actor", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "Action", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "EntityType", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "EntityKey", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "From", + "in": "query", + "schema": { + "type": "string", + "format": "date-time" + } + }, + { + "name": "To", + "in": "query", + "schema": { + "type": "string", + "format": "date-time" + } + }, + { + "name": "Cursor", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "Limit", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/KeysetPageOfAuditEvent" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/admin/secrets": { + "get": { + "tags": [ + "Administration" + ], + "operationId": "ListSecrets", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/SecretStatus" + } + } + } + } + } + } + } + }, + "/psr/api/admin/settings": { + "get": { + "tags": [ + "Administration" + ], + "operationId": "GetSettings", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SettingsResponse" + } + } + } + } + } + } + }, + "/psr/api/meta/field-types": { + "get": { + "tags": [ + "Meta" + ], + "operationId": "GetFieldTypes", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/FieldTypesResponse" + } + } + } + } + } + } + }, + "/psr/api/stages": { + "get": { + "tags": [ + "Stages" + ], + "operationId": "ListStages", + "parameters": [ + { + "name": "search", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "status", + "in": "query", + "schema": { + "$ref": "#/components/schemas/VersionStatus" + } + }, + { + "name": "page", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + { + "name": "pageSize", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiPageOfStageSummary" + } + } + } + } + } + }, + "post": { + "tags": [ + "Stages" + ], + "operationId": "CreateStage", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewCatalogItem" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDetail" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}": { + "get": { + "tags": [ + "Stages" + ], + "operationId": "GetStage", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDetail" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "patch": { + "tags": [ + "Stages" + ], + "operationId": "UpdateStage", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CatalogItemChanges" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDetail" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "428": { + "description": "Precondition Required", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Stages" + ], + "operationId": "DeleteStage", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/versions/{version}": { + "get": { + "tags": [ + "Stages" + ], + "operationId": "GetStageVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageVersionDetail" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/draft": { + "get": { + "tags": [ + "Stages" + ], + "operationId": "GetStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDraft" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "post": { + "tags": [ + "Stages" + ], + "operationId": "CreateStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewDraftRequest" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDraft" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "put": { + "tags": [ + "Stages" + ], + "operationId": "SaveStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDraftData" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageDraft" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Stages" + ], + "operationId": "DiscardStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/draft/validate": { + "post": { + "tags": [ + "Stages" + ], + "operationId": "ValidateStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ValidationReport" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/draft/test": { + "post": { + "tags": [ + "Stages" + ], + "operationId": "TestStageDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunHandle" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "413": { + "description": "Payload Too Large", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/draft/publish": { + "post": { + "tags": [ + "Stages" + ], + "operationId": "PublishStage", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageVersionDetail" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "504": { + "description": "Gateway Timeout", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/versions/{version}/archive": { + "post": { + "tags": [ + "Stages" + ], + "operationId": "ArchiveStageVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StageVersionDetail" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/versions/{version}/export": { + "get": { + "tags": [ + "Stages" + ], + "operationId": "ExportStageVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/stages/{key}/runs": { + "post": { + "tags": [ + "Stages" + ], + "operationId": "RunStage", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunHandle" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "413": { + "description": "Payload Too Large", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines": { + "get": { + "tags": [ + "Pipelines" + ], + "operationId": "ListPipelines", + "parameters": [ + { + "name": "search", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "status", + "in": "query", + "schema": { + "$ref": "#/components/schemas/VersionStatus" + } + }, + { + "name": "page", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + { + "name": "pageSize", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiPageOfPipelineSummary" + } + } + } + } + } + }, + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "CreatePipeline", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewCatalogItem" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDetail" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/validate": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "ValidatePipeline", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDraftData" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ValidationReport" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}": { + "get": { + "tags": [ + "Pipelines" + ], + "operationId": "GetPipeline", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDetail" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Pipelines" + ], + "operationId": "DeletePipeline", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/versions/{version}": { + "get": { + "tags": [ + "Pipelines" + ], + "operationId": "GetPipelineVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineVersionDetail" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/draft": { + "get": { + "tags": [ + "Pipelines" + ], + "operationId": "GetPipelineDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDraft" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "CreatePipelineDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewDraftRequest" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDraft" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "put": { + "tags": [ + "Pipelines" + ], + "operationId": "SavePipelineDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDraftData" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDraft" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Pipelines" + ], + "operationId": "DiscardPipelineDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/draft/test": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "TestPipelineDraft", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunHandle" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "413": { + "description": "Payload Too Large", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/draft/publish": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "PublishPipeline", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelinePublication" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "422": { + "description": "Unprocessable Entity", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/versions/{version}/archive": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "ArchivePipelineVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineVersionDetail" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/settings": { + "patch": { + "tags": [ + "Pipelines" + ], + "operationId": "UpdatePipelineSettings", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineSettings" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDetail" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/retention": { + "patch": { + "tags": [ + "Pipelines" + ], + "operationId": "UpdatePipelineRetention", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineRetention" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/PipelineDetail" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/versions/{version}/export": { + "get": { + "tags": [ + "Pipelines" + ], + "operationId": "ExportPipelineVersion", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/runs": { + "post": { + "tags": [ + "Pipelines" + ], + "operationId": "RunPipeline", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunHandle" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "413": { + "description": "Payload Too Large", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/pipelines/{key}/schedules": { + "post": { + "tags": [ + "Schedules" + ], + "operationId": "CreateSchedule", + "parameters": [ + { + "name": "key", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleData" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Created", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleDetail" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/runs": { + "get": { + "tags": [ + "Runs" + ], + "operationId": "ListRuns", + "parameters": [ + { + "name": "PipelineKey", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "StageKey", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "Kind", + "in": "query", + "schema": { + "$ref": "#/components/schemas/RunKind" + } + }, + { + "name": "Status", + "in": "query", + "schema": { + "$ref": "#/components/schemas/RunStatus" + } + }, + { + "name": "Trigger", + "in": "query", + "schema": { + "$ref": "#/components/schemas/RunTrigger" + } + }, + { + "name": "From", + "in": "query", + "schema": { + "type": "string", + "format": "date-time" + } + }, + { + "name": "To", + "in": "query", + "schema": { + "type": "string", + "format": "date-time" + } + }, + { + "name": "Cursor", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "Limit", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/KeysetPageOfRunSummary" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/runs/{id}": { + "get": { + "tags": [ + "Runs" + ], + "operationId": "GetRun", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunSnapshot" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/runs/{id}/attempts/{attemptId}/logs/{stream}": { + "get": { + "tags": [ + "Runs" + ], + "operationId": "GetAttemptLog", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "attemptId", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "stream", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "type": "string" + } + } + } + }, + "404": { + "description": "Not Found", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/runs/{id}/cancel": { + "post": { + "tags": [ + "Runs" + ], + "operationId": "CancelRun", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/runs/{id}/reprocess": { + "post": { + "tags": [ + "Runs" + ], + "operationId": "ReprocessRun", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "Accepted", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/RunHandle" + } + } + } + }, + "409": { + "description": "Conflict", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/schedules": { + "get": { + "tags": [ + "Schedules" + ], + "operationId": "ListSchedules", + "parameters": [ + { + "name": "pipelineKey", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "page", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + }, + { + "name": "pageSize", + "in": "query", + "schema": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiPageOfScheduleDetail" + } + } + } + } + } + } + }, + "/psr/api/schedules/{id}": { + "put": { + "tags": [ + "Schedules" + ], + "operationId": "UpdateSchedule", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleData" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleDetail" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + }, + "delete": { + "tags": [ + "Schedules" + ], + "operationId": "DeleteSchedule", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "412": { + "description": "Precondition Failed", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + }, + "/psr/api/schedules/{id}/enable": { + "post": { + "tags": [ + "Schedules" + ], + "operationId": "EnableSchedule", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleDetail" + } + } + } + } + } + } + }, + "/psr/api/schedules/{id}/disable": { + "post": { + "tags": [ + "Schedules" + ], + "operationId": "DisableSchedule", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ScheduleDetail" + } + } + } + } + } + } + }, + "/psr/api/schedules/preview": { + "post": { + "tags": [ + "Schedules" + ], + "operationId": "PreviewSchedule", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SchedulePreviewRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ScheduleOccurrence" + } + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/problem+json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "ApiPageOfPipelineSummary": { + "required": [ + "items", + "page", + "pageSize", + "total" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PipelineSummary" + }, + "description": "Items of the page." + }, + "page": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "1-based page number.", + "format": "int32" + }, + "pageSize": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Items per page.", + "format": "int32" + }, + "total": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Number of items in every page.", + "format": "int32" + } + }, + "description": "One page of a catalog list." + }, + "ApiPageOfScheduleDetail": { + "required": [ + "items", + "page", + "pageSize", + "total" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ScheduleDetail" + }, + "description": "Items of the page." + }, + "page": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "1-based page number.", + "format": "int32" + }, + "pageSize": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Items per page.", + "format": "int32" + }, + "total": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Number of items in every page.", + "format": "int32" + } + }, + "description": "One page of a catalog list." + }, + "ApiPageOfStageSummary": { + "required": [ + "items", + "page", + "pageSize", + "total" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StageSummary" + }, + "description": "Items of the page." + }, + "page": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "1-based page number.", + "format": "int32" + }, + "pageSize": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Items per page.", + "format": "int32" + }, + "total": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Number of items in every page.", + "format": "int32" + } + }, + "description": "One page of a catalog list." + }, + "ArtifactDirection": { + "enum": [ + "Input", + "Output" + ], + "description": "Whether a stored file was given to a run or produced by it." + }, + "ArtifactId": { + "type": "string", + "description": "Identifier of a stored file (time-ordered UUID v7).", + "format": "uuid" + }, + "ArtifactInfo": { + "required": [ + "id", + "runId", + "direction", + "fieldName", + "fileName", + "contentType", + "sizeBytes", + "sha256", + "contentExpiresAt" + ], + "type": "object", + "properties": { + "id": { + "description": "File identifier.", + "$ref": "#/components/schemas/ArtifactId" + }, + "runId": { + "description": "Run that owns the file.", + "$ref": "#/components/schemas/RunId" + }, + "stepKey": { + "type": [ + "null", + "string" + ], + "description": "Step that produced the file (`null` for run inputs)." + }, + "direction": { + "description": "Input or output.", + "$ref": "#/components/schemas/ArtifactDirection" + }, + "fieldName": { + "type": "string", + "description": "Field that carries the file." + }, + "listIndex": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Position in a `list<file>`.", + "format": "int32" + }, + "fileName": { + "type": "string", + "description": "File name." + }, + "contentType": { + "type": "string", + "description": "MIME type." + }, + "sizeBytes": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Size in bytes.", + "format": "int64" + }, + "sha256": { + "type": "string", + "description": "SHA-256 as lowercase hex." + }, + "contentExpiresAt": { + "type": "string", + "description": "When the content is deleted.", + "format": "date-time" + }, + "contentDeletedAt": { + "type": [ + "null", + "string" + ], + "description": "When the content was deleted (downloads then fail with `ARTIFACT_EXPIRED`).", + "format": "date-time" + } + }, + "description": "Metadata of a stored file." + }, + "AttemptCause": { + "enum": [ + "Initial", + "AutoRetry", + "Reprocess", + "LeaseRecovery" + ], + "description": "Why an attempt was started." + }, + "AttemptId": { + "type": "string", + "description": "Identifier of one attempt of a step (time-ordered UUID v7).", + "format": "uuid" + }, + "AttemptSnapshot": { + "required": [ + "id", + "number", + "cause", + "triggeredBy", + "status", + "startedAt" + ], + "type": "object", + "properties": { + "id": { + "description": "Attempt identifier.", + "$ref": "#/components/schemas/AttemptId" + }, + "number": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "1-based number within the step.", + "format": "int32" + }, + "cause": { + "description": "Why it started.", + "$ref": "#/components/schemas/AttemptCause" + }, + "triggeredBy": { + "type": "string", + "description": "Who started it (user for reprocessing, `system:*` otherwise)." + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/AttemptStatus" + }, + "workerId": { + "type": [ + "null", + "string" + ], + "description": "Worker that executed it (asynchronous runs)." + }, + "environmentHash": { + "type": [ + "null", + "string" + ], + "description": "Python environment used." + }, + "exitCode": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Exit code of the process.", + "format": "int32" + }, + "error": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Failure.", + "$ref": "#/components/schemas/RunError" + } + ] + }, + "startedAt": { + "type": "string", + "description": "Start time.", + "format": "date-time" + }, + "finishedAt": { + "type": [ + "null", + "string" + ], + "description": "End time.", + "format": "date-time" + }, + "duration": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": [ + "null", + "string" + ], + "description": "Total duration." + }, + "overhead": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": [ + "null", + "string" + ], + "description": "Time spent by the runner outside the Python process." + }, + "logs": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LogStream" + }, + "description": "Logs captured (content is read with Task<AttemptLog?> IRunManager.GetAttemptLogAsync(RunId runId, AttemptId attemptId, LogStream stream, CancellationToken cancellationToken = default(CancellationToken)))." + } + }, + "description": "A persisted attempt (one Python process)." + }, + "AttemptStatus": { + "enum": [ + "Running", + "Succeeded", + "Failed", + "TimedOut", + "Cancelled", + "WorkerLost" + ], + "description": "Final status of one attempt." + }, + "AuditEvent": { + "required": [ + "id", + "occurredAt", + "actor", + "action", + "entityType", + "entityId", + "entityKey", + "details", + "traceId" + ], + "type": "object", + "properties": { + "id": { + "type": "string", + "description": "Event identifier.", + "format": "uuid" + }, + "occurredAt": { + "type": "string", + "description": "When it happened.", + "format": "date-time" + }, + "actor": { + "type": "string", + "description": "Subject of the user." + }, + "action": { + "type": "string", + "description": "What was done." + }, + "entityType": { + "type": "string", + "description": "Entity type." + }, + "entityId": { + "type": [ + "null", + "string" + ], + "description": "Entity identifier.", + "format": "uuid" + }, + "entityKey": { + "type": [ + "null", + "string" + ], + "description": "Readable reference." + }, + "details": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Diff or parameters.", + "$ref": "#/components/schemas/JsonObject" + } + ] + }, + "traceId": { + "type": [ + "null", + "string" + ], + "description": "OpenTelemetry trace." + } + }, + "description": "A stored audit event." + }, + "AuthInfo": { + "required": [ + "mode", + "loginUrl", + "logoutUrl", + "localRequests" + ], + "type": "object", + "properties": { + "mode": { + "description": "Access mode.", + "$ref": "#/components/schemas/DashboardAccessMode" + }, + "loginUrl": { + "type": [ + "null", + "string" + ], + "description": "Host login page (host authentication)." + }, + "logoutUrl": { + "type": [ + "null", + "string" + ], + "description": "Host logout page (host authentication)." + }, + "localRequests": { + "type": "boolean", + "description": "Whether local requests enter without login." + } + }, + "description": "How the panel signs in (`GET /auth`); never lists users." + }, + "Authorship": { + "required": [ + "createdAt", + "createdBy", + "updatedAt", + "updatedBy" + ], + "type": "object", + "properties": { + "createdAt": { + "type": "string", + "description": "Creation time.", + "format": "date-time" + }, + "createdBy": { + "type": "string", + "description": "Creator subject." + }, + "updatedAt": { + "type": "string", + "description": "Last change.", + "format": "date-time" + }, + "updatedBy": { + "type": "string", + "description": "Subject of the last change." + } + }, + "description": "Creation and last change of a row." + }, + "BindingSource": { + "oneOf": [ + { + "required": [ + "kind", + "field" + ], + "type": "object", + "properties": { + "kind": { + "enum": [ + "pipeline" + ], + "type": "string" + }, + "field": { + "type": "string" + } + } + }, + { + "required": [ + "kind", + "step", + "field" + ], + "type": "object", + "properties": { + "kind": { + "enum": [ + "step" + ], + "type": "string" + }, + "step": { + "type": "string" + }, + "field": { + "type": "string" + } + } + }, + { + "required": [ + "kind" + ], + "type": "object", + "properties": { + "kind": { + "enum": [ + "constant" + ], + "type": "string" + }, + "value": {} + } + } + ], + "description": "Origin of a bound value." + }, + "CatalogItemChanges": { + "type": "object", + "properties": { + "name": { + "type": [ + "null", + "string" + ], + "description": "New name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "New description." + } + }, + "description": "Changes to the name or description; `null` keeps the current value." + }, + "DashboardAccessMode": { + "enum": [ + "FixedLogin", + "Filter", + "Host", + "Localhost" + ], + "description": "How the dashboard identifies its users (ADR-0021)." + }, + "DashboardRole": { + "enum": [ + "Reader", + "Executor", + "Author", + "Admin", + null + ] + }, + "ErrorKind": { + "enum": [ + "InputValidation", + "OutputContractViolation", + "ScriptError", + "Timeout", + "MemoryLimitExceeded", + "Cancelled", + "WorkerLost", + "EnvironmentSetupFailed", + "ArtifactLimitExceeded", + "SecretNotFound", + "ProcessCrashed", + "Expired" + ], + "description": "Classification of a failure." + }, + "ExecutionMode": { + "enum": [ + "Sync", + "Async" + ], + "description": "How a run is executed." + }, + "FieldTypeInfo": { + "required": [ + "name", + "scalar", + "inputWidget", + "outputWidget", + "allowsDefault" + ], + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Wire name (`number`, `list`...)." + }, + "scalar": { + "type": "boolean", + "description": "Whether it is a scalar." + }, + "inputWidget": { + "type": "string", + "description": "Widget of the input form." + }, + "outputWidget": { + "type": "string", + "description": "How outputs are shown." + }, + "allowsDefault": { + "type": "boolean", + "description": "Whether a default value can be declared." + } + }, + "description": "One field type." + }, + "FieldTypesResponse": { + "required": [ + "types", + "compatibility", + "rules" + ], + "type": "object", + "properties": { + "types": { + "type": "array", + "items": { + "$ref": "#/components/schemas/FieldTypeInfo" + }, + "description": "The 12 types and their widgets." + }, + "compatibility": { + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "type": "string" + } + }, + "description": "Which source types can feed which target types (representative types; see Rules)." + }, + "rules": { + "type": "array", + "items": { + "type": "string" + }, + "description": "Rules that depend on the declaration (enum values, table columns, file content types, list items)." + } + }, + "description": "Field types for the editor (`GET /meta/field-types`)." + }, + "ImportedItem": { + "required": [ + "kind", + "key", + "version", + "newKey" + ], + "type": "object", + "properties": { + "kind": { + "type": "string", + "description": "`Stage` or `Pipeline`." + }, + "key": { + "type": "string", + "description": "Key." + }, + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Version of the created draft.", + "format": "int32" + }, + "newKey": { + "type": "boolean", + "description": "Whether the key did not exist before." + } + }, + "description": "A draft created by an import." + }, + "ImportReport": { + "required": [ + "items" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ImportedItem" + }, + "description": "Drafts created." + } + }, + "description": "Result of an import." + }, + "InputBinding": { + "required": [ + "input", + "source" + ], + "type": "object", + "properties": { + "input": { + "type": "string", + "description": "Input name of the stage." + }, + "source": { + "description": "Origin of the value.", + "$ref": "#/components/schemas/BindingSource" + } + }, + "description": "Where one input of a step comes from." + }, + "JsonNode": {}, + "JsonObject": { + "type": "object" + }, + "KeysetPageOfAuditEvent": { + "required": [ + "items", + "nextCursor" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AuditEvent" + }, + "description": "Items of the page." + }, + "nextCursor": { + "type": [ + "null", + "string" + ], + "description": "Opaque cursor of the next page; `null` on the last page." + } + }, + "description": "One page of a keyset-paginated list (newest first)." + }, + "KeysetPageOfRunSummary": { + "required": [ + "items", + "nextCursor" + ], + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/RunSummary" + }, + "description": "Items of the page." + }, + "nextCursor": { + "type": [ + "null", + "string" + ], + "description": "Opaque cursor of the next page; `null` on the last page." + } + }, + "description": "One page of a keyset-paginated list (newest first)." + }, + "LimitSettings": { + "required": [ + "maxFileBytes", + "maxRunFilesBytes", + "maxLogBytes", + "defaultTimeout", + "defaultMemoryLimitMb", + "maxConcurrentProcesses", + "defaultPythonVersion" + ], + "type": "object", + "properties": { + "maxFileBytes": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Largest input or output file.", + "format": "int64" + }, + "maxRunFilesBytes": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Largest total of the files of a run.", + "format": "int64" + }, + "maxLogBytes": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Log tail kept per stream.", + "format": "int32" + }, + "defaultTimeout": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": "string", + "description": "Default timeout of an attempt." + }, + "defaultMemoryLimitMb": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Default memory limit.", + "format": "int32" + }, + "maxConcurrentProcesses": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Python processes per instance.", + "format": "int32" + }, + "defaultPythonVersion": { + "type": "string", + "description": "Default Python version." + } + }, + "description": "Limits of runs and uploads." + }, + "LoginRequest": { + "required": [ + "name", + "password" + ], + "type": "object", + "properties": { + "name": { + "type": [ + "null", + "string" + ], + "description": "User name." + }, + "password": { + "type": [ + "null", + "string" + ], + "description": "Password." + } + }, + "description": "Fixed login credentials." + }, + "LogStream": { + "enum": [ + "Stdout", + "Stderr" + ], + "description": "Output stream of a Python process." + }, + "MeResponse": { + "required": [ + "subject", + "name", + "role", + "roles", + "mode" + ], + "type": "object", + "properties": { + "subject": { + "type": "string", + "description": "Recorded in the audit." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "role": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Role (`null`: authenticated by the host without a dashboard role).", + "$ref": "#/components/schemas/DashboardRole" + } + ] + }, + "roles": { + "type": "array", + "items": { + "$ref": "#/components/schemas/DashboardRole" + }, + "description": "Every role the user has (cumulative)." + }, + "mode": { + "description": "How the user was identified.", + "$ref": "#/components/schemas/DashboardAccessMode" + } + }, + "description": "The current user (`GET /me`)." + }, + "NewCatalogItem": { + "required": [ + "key", + "name" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Slug (immutable)." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description." + } + }, + "description": "A new stage or pipeline." + }, + "NewDraftRequest": { + "required": [ + "fromVersion" + ], + "type": "object", + "properties": { + "fromVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Version to copy.", + "format": "int32" + } + }, + "description": "Copies a version into a new draft." + }, + "OverviewResponse": { + "required": [ + "generatedAt", + "last24Hours", + "previous24Hours", + "inProgress", + "recentFailures", + "upcomingSchedules" + ], + "type": "object", + "properties": { + "generatedAt": { + "type": "string", + "description": "When it was computed.", + "format": "date-time" + }, + "last24Hours": { + "type": "object", + "additionalProperties": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + }, + "description": "Runs created in the last 24 hours, by status." + }, + "previous24Hours": { + "type": "object", + "additionalProperties": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "format": "int32" + }, + "description": "Runs created in the 24 hours before, by status." + }, + "inProgress": { + "type": "array", + "items": { + "$ref": "#/components/schemas/RunSummary" + }, + "description": "Runs pending, queued, running or cancelling (newest first, up to 50)." + }, + "recentFailures": { + "type": "array", + "items": { + "$ref": "#/components/schemas/RunSummary" + }, + "description": "The 10 latest failed runs (last 7 days)." + }, + "upcomingSchedules": { + "type": "array", + "items": { + "$ref": "#/components/schemas/UpcomingSchedule" + }, + "description": "The 5 next schedule firings." + } + }, + "description": "Run counters and lists of `GET /overview` (D-115)." + }, + "PipelineDetail": { + "required": [ + "key", + "name", + "description", + "latestPublishedVersion", + "versions", + "settings", + "retention", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Pipeline key." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description." + }, + "latestPublishedVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Latest published version.", + "format": "int32" + }, + "versions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/VersionSummary" + }, + "description": "Every version, newest first." + }, + "settings": { + "description": "Operational settings.", + "$ref": "#/components/schemas/PipelineSettings" + }, + "retention": { + "description": "Retention.", + "$ref": "#/components/schemas/PipelineRetention" + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token of the pipeline row.", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "A pipeline with its versions and settings." + }, + "PipelineDraft": { + "required": [ + "key", + "version", + "data", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Pipeline key." + }, + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Draft version.", + "format": "int32" + }, + "data": { + "description": "Content.", + "$ref": "#/components/schemas/PipelineDraftData" + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token (send it back when saving).", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "The current draft of a pipeline." + }, + "PipelineDraftData": { + "type": "object", + "properties": { + "inputs": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "pattern": "^[a-z_][a-z0-9_]{0,63}$", + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file", + "list" + ], + "type": "string" + }, + "itemType": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file" + ], + "type": "string" + }, + "required": { + "type": "boolean", + "default": true + }, + "default": { + "description": "Default value in wire format (not for files and tables)." + }, + "description": { + "maxLength": 500, + "type": "string" + }, + "enumValues": { + "type": "array", + "items": { + "type": "string" + } + }, + "contentTypes": { + "type": "array", + "items": { + "type": "string" + } + }, + "columns": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime" + ], + "type": "string" + } + } + } + } + } + }, + "description": "Inputs received by the pipeline." + }, + "steps": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PipelineStepDefinition" + }, + "description": "Steps in execution order." + }, + "outputs": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PipelineOutputDefinition" + }, + "description": "Declared outputs." + }, + "layout": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Positions of nodes and edges in the visual editor (does not affect execution).", + "$ref": "#/components/schemas/JsonNode" + } + ] + } + }, + "description": "Editable content of a pipeline version." + }, + "PipelineOutputDefinition": { + "required": [ + "name", + "source" + ], + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Output name." + }, + "source": { + "description": "Step output that provides the value (its type is inherited).", + "$ref": "#/components/schemas/StepOutputSource" + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description shown in the panel." + } + }, + "description": "A declared output of the pipeline, taken from an output of a step." + }, + "PipelinePublication": { + "required": [ + "version", + "warnings" + ], + "type": "object", + "properties": { + "version": { + "description": "The published version.", + "$ref": "#/components/schemas/PipelineVersionDetail" + }, + "warnings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ValidationIssue" + }, + "description": "Validation warnings (they do not block publication)." + } + }, + "description": "Result of publishing a pipeline." + }, + "PipelineRetention": { + "required": [ + "artifactRetentionDays", + "runRetentionDays" + ], + "type": "object", + "properties": { + "artifactRetentionDays": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Days the files are kept; `null` = global.", + "format": "int32" + }, + "runRetentionDays": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Days the runs are kept; `null` = global.", + "format": "int32" + } + }, + "description": "Retention of a pipeline; it can only be shorter than the global retention." + }, + "PipelineSettings": { + "required": [ + "maxConcurrentRuns" + ], + "type": "object", + "properties": { + "maxConcurrentRuns": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Maximum simultaneous asynchronous runs; `null` = no limit.", + "format": "int32" + } + }, + "description": "Operational settings of a pipeline (not versioned)." + }, + "PipelineStepDefinition": { + "required": [ + "key", + "stageKey", + "stageVersion" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Step key (slug, unique in the pipeline)." + }, + "stageKey": { + "type": "string", + "description": "Key of the executed stage." + }, + "stageVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Exact version of the executed stage.", + "format": "int32" + }, + "displayName": { + "type": [ + "null", + "string" + ], + "description": "Label shown in the panel." + }, + "bindings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/InputBinding" + }, + "description": "Explicit bindings; unbound inputs use the implicit binding by name." + } + }, + "description": "One step of a pipeline: a fixed stage version and the bindings of its inputs." + }, + "PipelineSummary": { + "required": [ + "key", + "name", + "description", + "latestPublishedVersion", + "draftVersion", + "updatedAt" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Pipeline key." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description." + }, + "latestPublishedVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Latest published version.", + "format": "int32" + }, + "draftVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Version of the current draft.", + "format": "int32" + }, + "updatedAt": { + "type": "string", + "description": "Last change.", + "format": "date-time" + } + }, + "description": "A pipeline in a list." + }, + "PipelineVersionDetail": { + "required": [ + "key", + "version", + "status", + "data", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Pipeline key." + }, + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Version number.", + "format": "int32" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/VersionStatus" + }, + "data": { + "description": "Content.", + "$ref": "#/components/schemas/PipelineDraftData" + }, + "publishedAt": { + "type": [ + "null", + "string" + ], + "description": "Publication time.", + "format": "date-time" + }, + "publishedBy": { + "type": [ + "null", + "string" + ], + "description": "Who published." + }, + "archivedAt": { + "type": [ + "null", + "string" + ], + "description": "Archiving time.", + "format": "date-time" + }, + "archivedBy": { + "type": [ + "null", + "string" + ], + "description": "Who archived." + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token.", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "One version of a pipeline." + }, + "ProblemDetails": { + "type": "object", + "properties": { + "type": { + "type": [ + "null", + "string" + ] + }, + "title": { + "type": [ + "null", + "string" + ] + }, + "status": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "format": "int32" + }, + "detail": { + "type": [ + "null", + "string" + ] + }, + "instance": { + "type": [ + "null", + "string" + ] + } + } + }, + "RetentionSettings": { + "required": [ + "artifactDays", + "runDays", + "auditDays" + ], + "type": "object", + "properties": { + "artifactDays": { + "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$", + "type": [ + "number", + "string" + ], + "description": "File contents.", + "format": "double" + }, + "runDays": { + "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$", + "type": [ + "number", + "string" + ], + "description": "Runs.", + "format": "double" + }, + "auditDays": { + "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$", + "type": [ + "number", + "string" + ], + "description": "Audit events.", + "format": "double" + } + }, + "description": "Global retention in days." + }, + "RetryPolicy": { + "required": [ + "maxAttempts", + "initialDelay", + "backoffMultiplier" + ], + "type": "object", + "properties": { + "maxAttempts": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Number of retries after the first attempt (0 = no retry).", + "format": "int32" + }, + "initialDelay": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": "string", + "description": "Delay before the first retry." + }, + "backoffMultiplier": { + "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$", + "type": [ + "number", + "string" + ], + "description": "Multiplier applied to the delay on each further retry.", + "format": "double" + } + }, + "description": "Automatic retry policy of a stage." + }, + "RunError": { + "required": [ + "kind", + "message" + ], + "type": "object", + "properties": { + "kind": { + "description": "Failure classification.", + "$ref": "#/components/schemas/ErrorKind" + }, + "message": { + "type": "string", + "description": "Human-readable message." + }, + "code": { + "type": [ + "null", + "string" + ], + "description": "Stable code (e.g. a `StageError` code or `UNDECLARED_OUTPUT`)." + }, + "stepKey": { + "type": [ + "null", + "string" + ], + "description": "Key of the step that failed." + }, + "traceback": { + "type": [ + "null", + "string" + ], + "description": "Python traceback, when available." + }, + "details": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Extra details given by the script (`StageError(details=...)`).", + "$ref": "#/components/schemas/JsonNode" + } + ] + } + }, + "description": "Why a run, step or attempt failed." + }, + "RunHandle": { + "required": [ + "runId", + "status", + "deduplicated" + ], + "type": "object", + "properties": { + "runId": { + "description": "Run identifier.", + "$ref": "#/components/schemas/RunId" + }, + "status": { + "description": "RunStatus.Queued or RunStatus.Pending (the current status when deduplicated).", + "$ref": "#/components/schemas/RunStatus" + }, + "deduplicated": { + "type": "boolean", + "description": "Whether an earlier run with the same idempotency key was returned." + } + }, + "description": "A queued run." + }, + "RunId": { + "type": "string", + "description": "Identifier of a run (time-ordered UUID v7). Its timestamp is the creation time of the run.", + "format": "uuid" + }, + "RunKind": { + "enum": [ + "Pipeline", + "PipelineTest", + "Stage", + "StageTest" + ], + "description": "What a run executes." + }, + "RunSnapshot": { + "required": [ + "id", + "kind", + "mode", + "status", + "targetKey", + "targetVersion", + "trigger", + "triggeredBy", + "inputs", + "createdAt", + "steps", + "artifacts" + ], + "type": "object", + "properties": { + "id": { + "description": "Run identifier.", + "$ref": "#/components/schemas/RunId" + }, + "kind": { + "description": "What was executed.", + "$ref": "#/components/schemas/RunKind" + }, + "mode": { + "description": "Sync or async.", + "$ref": "#/components/schemas/ExecutionMode" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/RunStatus" + }, + "targetKey": { + "type": "string", + "description": "Pipeline or stage key." + }, + "targetVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Pipeline or stage version.", + "format": "int32" + }, + "trigger": { + "description": "How the run started.", + "$ref": "#/components/schemas/RunTrigger" + }, + "triggeredBy": { + "type": "string", + "description": "Who started it." + }, + "inputs": { + "description": "Validated inputs; files as `{\"$artifact\": \"<id>\"}`.", + "$ref": "#/components/schemas/JsonObject" + }, + "outputs": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Declared outputs; files as `{\"$artifact\": \"<id>\"}`.", + "$ref": "#/components/schemas/JsonObject" + } + ] + }, + "error": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Failure.", + "$ref": "#/components/schemas/RunError" + } + ] + }, + "idempotencyKey": { + "type": [ + "null", + "string" + ], + "description": "Idempotency key given by the caller." + }, + "correlationId": { + "type": [ + "null", + "string" + ], + "description": "Correlation given by the caller (returned in the events)." + }, + "scheduleId": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Schedule that fired the run.", + "$ref": "#/components/schemas/ScheduleId" + } + ] + }, + "triggerDetails": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Details of the trigger, e.g. `{\"scheduledFor\": \"...\", \"missedOccurrences\": 10}` for a schedule recovery.", + "$ref": "#/components/schemas/JsonObject" + } + ] + }, + "createdAt": { + "type": "string", + "description": "Creation time.", + "format": "date-time" + }, + "queuedAt": { + "type": [ + "null", + "string" + ], + "description": "When it was queued.", + "format": "date-time" + }, + "startedAt": { + "type": [ + "null", + "string" + ], + "description": "Start time.", + "format": "date-time" + }, + "finishedAt": { + "type": [ + "null", + "string" + ], + "description": "End time.", + "format": "date-time" + }, + "cancelRequestedAt": { + "type": [ + "null", + "string" + ], + "description": "When cancellation was requested.", + "format": "date-time" + }, + "cancelRequestedBy": { + "type": [ + "null", + "string" + ], + "description": "Who requested cancellation." + }, + "expiresAt": { + "type": [ + "null", + "string" + ], + "description": "When the run is deleted (pipelines with a shorter retention).", + "format": "date-time" + }, + "steps": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StepRunSnapshot" + }, + "description": "Steps in order." + }, + "artifacts": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ArtifactInfo" + }, + "description": "Input and output files." + } + }, + "description": "A persisted run with its steps, attempts and files." + }, + "RunStatus": { + "enum": [ + "Pending", + "Queued", + "Running", + "Succeeded", + "Failed", + "Cancelling", + "Cancelled" + ], + "description": "Status of a run." + }, + "RunSummary": { + "required": [ + "id", + "kind", + "mode", + "status", + "targetKey", + "targetVersion", + "trigger", + "triggeredBy", + "createdAt", + "startedAt", + "finishedAt", + "error" + ], + "type": "object", + "properties": { + "id": { + "description": "Run identifier.", + "$ref": "#/components/schemas/RunId" + }, + "kind": { + "description": "What was executed.", + "$ref": "#/components/schemas/RunKind" + }, + "mode": { + "description": "Sync or async.", + "$ref": "#/components/schemas/ExecutionMode" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/RunStatus" + }, + "targetKey": { + "type": "string", + "description": "Pipeline or stage key." + }, + "targetVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Pipeline or stage version.", + "format": "int32" + }, + "trigger": { + "description": "How the run started.", + "$ref": "#/components/schemas/RunTrigger" + }, + "triggeredBy": { + "type": "string", + "description": "Who started it." + }, + "createdAt": { + "type": "string", + "description": "Creation time.", + "format": "date-time" + }, + "startedAt": { + "type": [ + "null", + "string" + ], + "description": "Start time.", + "format": "date-time" + }, + "finishedAt": { + "type": [ + "null", + "string" + ], + "description": "End time.", + "format": "date-time" + }, + "error": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Failure.", + "$ref": "#/components/schemas/RunError" + } + ] + } + }, + "description": "A run in a list." + }, + "RunTrigger": { + "enum": [ + "Library", + "Dashboard", + "Schedule", + "QueueCommand" + ], + "description": "Who or what started a run." + }, + "ScheduleData": { + "required": [ + "name", + "cron" + ], + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Display name (1-200 chars)." + }, + "cron": { + "type": "string", + "description": "Cron expression with 5 fields (minute precision) or 6 fields (with seconds)." + }, + "timeZone": { + "type": "string", + "description": "IANA time zone in which the expression is evaluated (daylight saving time handled)." + }, + "pinnedVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Version to run; `null` runs the latest published version at each firing.", + "format": "int32" + }, + "inputs": { + "description": "Fixed inputs of the pipeline (no files).", + "$ref": "#/components/schemas/JsonObject" + }, + "isEnabled": { + "type": "boolean", + "description": "Whether the schedule fires." + } + }, + "description": "Settings of a cron schedule of a pipeline." + }, + "ScheduleDetail": { + "required": [ + "id", + "pipelineKey", + "data", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "id": { + "description": "Identifier.", + "$ref": "#/components/schemas/ScheduleId" + }, + "pipelineKey": { + "type": "string", + "description": "Scheduled pipeline." + }, + "data": { + "description": "Settings.", + "$ref": "#/components/schemas/ScheduleData" + }, + "nextRunAt": { + "type": [ + "null", + "string" + ], + "description": "Next firing (UTC); `null` when disabled.", + "format": "date-time" + }, + "lastFiredAt": { + "type": [ + "null", + "string" + ], + "description": "Last firing.", + "format": "date-time" + }, + "lastRunId": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Run created by the last successful firing.", + "$ref": "#/components/schemas/RunId" + } + ] + }, + "lastError": { + "type": [ + "null", + "string" + ], + "description": "Why the last firing was rejected (cleared by the next successful one)." + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Row version for optimistic concurrency.", + "format": "uint32" + }, + "authorship": { + "description": "Who created and last changed it.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "A stored schedule." + }, + "ScheduleId": { + "type": "string", + "description": "Identifier of a schedule (UUID v7).", + "format": "uuid" + }, + "ScheduleOccurrence": { + "required": [ + "utc", + "local" + ], + "type": "object", + "properties": { + "utc": { + "type": "string", + "description": "Instant in UTC.", + "format": "date-time" + }, + "local": { + "type": "string", + "description": "Same instant in the schedule time zone (with its offset).", + "format": "date-time" + } + }, + "description": "One occurrence of a schedule." + }, + "SchedulePreviewRequest": { + "required": [ + "cron", + "timeZone", + "count" + ], + "type": "object", + "properties": { + "cron": { + "type": "string", + "description": "Cron expression (5 or 6 fields)." + }, + "timeZone": { + "type": [ + "null", + "string" + ], + "description": "IANA time zone (default `America/Sao_Paulo`)." + }, + "count": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "How many (1-20; default 5).", + "format": "int32" + } + }, + "description": "Next occurrences of a cron expression." + }, + "SecretStatus": { + "required": [ + "name", + "declaredBy", + "resolved" + ], + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Secret name." + }, + "declaredBy": { + "type": "array", + "items": { + "type": "string" + }, + "description": "Stage versions that declare it (`key@version`)." + }, + "resolved": { + "type": "boolean", + "description": "Whether the secret provider returns a value." + } + }, + "description": "Status of a secret declared by stages (never its value)." + }, + "SettingsResponse": { + "required": [ + "retention", + "limits", + "access", + "versions" + ], + "type": "object", + "properties": { + "retention": { + "description": "Global retention.", + "$ref": "#/components/schemas/RetentionSettings" + }, + "limits": { + "description": "Limits of runs and uploads.", + "$ref": "#/components/schemas/LimitSettings" + }, + "access": { + "description": "Access mode of the dashboard.", + "$ref": "#/components/schemas/AuthInfo" + }, + "versions": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "description": "Versions of the components." + } + }, + "description": "Read-only settings (`GET /admin/settings`)." + }, + "StageDetail": { + "required": [ + "key", + "name", + "description", + "latestPublishedVersion", + "versions", + "usedBy", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Stage key." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description." + }, + "latestPublishedVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Latest published version.", + "format": "int32" + }, + "versions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/VersionSummary" + }, + "description": "Every version, newest first." + }, + "usedBy": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StageUsage" + }, + "description": "Pipeline steps that use a version of the stage." + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token of the stage row.", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "A stage with its versions and usages." + }, + "StageDraft": { + "required": [ + "key", + "version", + "data", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Stage key." + }, + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Draft version.", + "format": "int32" + }, + "data": { + "description": "Content.", + "$ref": "#/components/schemas/StageDraftData" + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token (send it back when saving).", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "The current draft of a stage." + }, + "StageDraftData": { + "required": [ + "script", + "pythonVersion" + ], + "type": "object", + "properties": { + "script": { + "type": "string", + "description": "Python source of the single script." + }, + "pythonVersion": { + "type": "string", + "description": "Python version (e.g. `3.12`)." + }, + "requirements": { + "type": [ + "null", + "string" + ], + "description": "Dependencies in requirements.txt format." + }, + "inputs": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "pattern": "^[a-z_][a-z0-9_]{0,63}$", + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file", + "list" + ], + "type": "string" + }, + "itemType": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file" + ], + "type": "string" + }, + "required": { + "type": "boolean", + "default": true + }, + "default": { + "description": "Default value in wire format (not for files and tables)." + }, + "description": { + "maxLength": 500, + "type": "string" + }, + "enumValues": { + "type": "array", + "items": { + "type": "string" + } + }, + "contentTypes": { + "type": "array", + "items": { + "type": "string" + } + }, + "columns": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime" + ], + "type": "string" + } + } + } + } + } + }, + "description": "Declared inputs." + }, + "outputs": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "pattern": "^[a-z_][a-z0-9_]{0,63}$", + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file", + "list" + ], + "type": "string" + }, + "itemType": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime", + "enum", + "json", + "table", + "file" + ], + "type": "string" + }, + "required": { + "type": "boolean", + "default": true + }, + "default": { + "description": "Default value in wire format (not for files and tables)." + }, + "description": { + "maxLength": 500, + "type": "string" + }, + "enumValues": { + "type": "array", + "items": { + "type": "string" + } + }, + "contentTypes": { + "type": "array", + "items": { + "type": "string" + } + }, + "columns": { + "type": "array", + "items": { + "required": [ + "name", + "type" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "enum": [ + "number", + "integer", + "decimal", + "string", + "bool", + "date", + "datetime" + ], + "type": "string" + } + } + } + } + } + }, + "description": "Declared outputs." + }, + "requiredSecrets": { + "type": "array", + "items": { + "type": "string" + }, + "description": "Names of the secrets the script needs." + }, + "timeout": { + "pattern": "^-?(\\d+\\.)?\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,7})?$", + "type": "string", + "description": "Maximum duration of one attempt." + }, + "memoryLimitMb": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Memory limit in MB; `null` uses the runner default.", + "format": "int32" + }, + "retry": { + "description": "Automatic retry policy.", + "$ref": "#/components/schemas/RetryPolicy" + }, + "idempotent": { + "type": "boolean", + "description": "Whether running the script twice with the same inputs is safe." + } + }, + "description": "Editable content of a stage version." + }, + "StageSummary": { + "required": [ + "key", + "name", + "description", + "latestPublishedVersion", + "draftVersion", + "updatedAt" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Stage key." + }, + "name": { + "type": "string", + "description": "Display name." + }, + "description": { + "type": [ + "null", + "string" + ], + "description": "Description." + }, + "latestPublishedVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Latest published version.", + "format": "int32" + }, + "draftVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ], + "description": "Version of the current draft.", + "format": "int32" + }, + "updatedAt": { + "type": "string", + "description": "Last change.", + "format": "date-time" + } + }, + "description": "A stage in a list." + }, + "StageUsage": { + "required": [ + "pipelineKey", + "pipelineVersion", + "pipelineStatus", + "stepKey", + "stageVersion" + ], + "type": "object", + "properties": { + "pipelineKey": { + "type": "string", + "description": "Pipeline key." + }, + "pipelineVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Pipeline version.", + "format": "int32" + }, + "pipelineStatus": { + "description": "Status of the pipeline version.", + "$ref": "#/components/schemas/VersionStatus" + }, + "stepKey": { + "type": "string", + "description": "Step key." + }, + "stageVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Stage version used by the step.", + "format": "int32" + } + }, + "description": "A pipeline step that uses a stage version." + }, + "StageVersionDetail": { + "required": [ + "key", + "version", + "status", + "data", + "scriptSha256", + "rowVersion", + "authorship" + ], + "type": "object", + "properties": { + "key": { + "type": "string", + "description": "Stage key." + }, + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Version number.", + "format": "int32" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/VersionStatus" + }, + "data": { + "description": "Content.", + "$ref": "#/components/schemas/StageDraftData" + }, + "scriptSha256": { + "type": "string", + "description": "SHA-256 of the script." + }, + "lockedRequirements": { + "type": [ + "null", + "string" + ], + "description": "Locked dependencies (published versions)." + }, + "environmentHash": { + "type": [ + "null", + "string" + ], + "description": "Hash of the Python environment (published versions)." + }, + "publishedAt": { + "type": [ + "null", + "string" + ], + "description": "Publication time.", + "format": "date-time" + }, + "publishedBy": { + "type": [ + "null", + "string" + ], + "description": "Who published." + }, + "archivedAt": { + "type": [ + "null", + "string" + ], + "description": "Archiving time.", + "format": "date-time" + }, + "archivedBy": { + "type": [ + "null", + "string" + ], + "description": "Who archived." + }, + "rowVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Concurrency token.", + "format": "uint32" + }, + "authorship": { + "description": "Creation and last change.", + "$ref": "#/components/schemas/Authorship" + } + }, + "description": "One version of a stage." + }, + "StepOutputSource": { + "required": [ + "step", + "field" + ], + "type": "object", + "properties": { + "step": { + "type": "string", + "description": "Step key." + }, + "field": { + "type": "string", + "description": "Output name of that step." + } + }, + "description": "An output of an earlier step (`$steps.<step>.<field>`)." + }, + "StepRunId": { + "type": "string", + "description": "Identifier of a step within a run (time-ordered UUID v7).", + "format": "uuid" + }, + "StepRunSnapshot": { + "required": [ + "id", + "stepKey", + "order", + "stageKey", + "stageVersion", + "status", + "currentAttempt", + "attempts" + ], + "type": "object", + "properties": { + "id": { + "description": "Step identifier.", + "$ref": "#/components/schemas/StepRunId" + }, + "stepKey": { + "type": "string", + "description": "Step key (the stage key for single-stage runs)." + }, + "order": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "1-based position.", + "format": "int32" + }, + "stageKey": { + "type": "string", + "description": "Executed stage." + }, + "stageVersion": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Exact stage version.", + "format": "int32" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/StepStatus" + }, + "currentAttempt": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Number of the last attempt (0 = not executed).", + "format": "int32" + }, + "inputs": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Inputs after resolving the bindings.", + "$ref": "#/components/schemas/JsonObject" + } + ] + }, + "outputs": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Validated outputs.", + "$ref": "#/components/schemas/JsonObject" + } + ] + }, + "error": { + "oneOf": [ + { + "type": "null" + }, + { + "description": "Failure of the last attempt.", + "$ref": "#/components/schemas/RunError" + } + ] + }, + "startedAt": { + "type": [ + "null", + "string" + ], + "description": "Start time.", + "format": "date-time" + }, + "finishedAt": { + "type": [ + "null", + "string" + ], + "description": "End time.", + "format": "date-time" + }, + "attempts": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AttemptSnapshot" + }, + "description": "Attempts in order." + } + }, + "description": "A persisted step of a run." + }, + "StepStatus": { + "enum": [ + "Pending", + "Queued", + "Running", + "Succeeded", + "Failed", + "TimedOut", + "Cancelled", + "Skipped" + ], + "description": "Status of a step within a run." + }, + "UpcomingSchedule": { + "required": [ + "id", + "name", + "pipelineKey", + "nextRunAt" + ], + "type": "object", + "properties": { + "id": { + "description": "Schedule identifier.", + "$ref": "#/components/schemas/ScheduleId" + }, + "name": { + "type": "string", + "description": "Schedule name." + }, + "pipelineKey": { + "type": "string", + "description": "Scheduled pipeline." + }, + "nextRunAt": { + "type": "string", + "description": "Next firing (UTC).", + "format": "date-time" + } + }, + "description": "A next schedule firing." + }, + "ValidationIssue": { + "required": [ + "code", + "message" + ], + "type": "object", + "properties": { + "code": { + "type": "string", + "description": "Stable code (e.g. `PIPE_TYPE_MISMATCH`)." + }, + "message": { + "type": "string", + "description": "Human-readable message." + }, + "step": { + "type": [ + "null", + "string" + ], + "description": "Step key, when the problem belongs to a step." + }, + "field": { + "type": [ + "null", + "string" + ], + "description": "Field name (step input, pipeline input or output)." + } + }, + "description": "One validation problem." + }, + "ValidationReport": { + "required": [ + "errors", + "warnings" + ], + "type": "object", + "properties": { + "errors": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ValidationIssue" + }, + "description": "Problems that block publication." + }, + "warnings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ValidationIssue" + }, + "description": "Problems that do not block publication." + }, + "isValid": { + "type": "boolean", + "description": "Whether there are no errors." + } + }, + "description": "Result of a pipeline validation." + }, + "VersionStatus": { + "enum": [ + "Draft", + "Published", + "Archived", + null + ] + }, + "VersionSummary": { + "required": [ + "version", + "status", + "publishedAt", + "updatedAt" + ], + "type": "object", + "properties": { + "version": { + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ], + "description": "Version number.", + "format": "int32" + }, + "status": { + "description": "Status.", + "$ref": "#/components/schemas/VersionStatus" + }, + "publishedAt": { + "type": [ + "null", + "string" + ], + "description": "Publication time.", + "format": "date-time" + }, + "updatedAt": { + "type": "string", + "description": "Last change.", + "format": "date-time" + } + }, + "description": "A version in a list." + } + } + }, + "tags": [ + { + "name": "Access" + }, + { + "name": "Pipelines" + }, + { + "name": "Runs" + }, + { + "name": "Administration" + }, + { + "name": "Meta" + }, + { + "name": "Stages" + }, + { + "name": "Schedules" + } + ] +} From ae0196d4457364543aed796aa6223331021f910e Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:07:13 -0300 Subject: [PATCH 5/7] docs: painel, DashboardHost e psr no README (specs 007 e 009) Co-Authored-By: Claude Opus 5.5 --- README.md | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b328f0d..465e8c2 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,9 @@ com contrato de entrada/saída, trilhas (**Pipelines**), execução assíncrona cron e painel Vue opcional. > Projeto em desenvolvimento. Esta versão entrega o **Core Runner** (execução síncrona de Stages e de -> trilhas), o SDK Python **`psrunner`**, a **persistência em PostgreSQL** (catálogo versionado e histórico) e a -> **execução assíncrona** com RabbitMQ, worker e agendamento cron. +> trilhas), o SDK Python **`psrunner`**, a **persistência em PostgreSQL** (catálogo versionado e histórico), a +> **execução assíncrona** com RabbitMQ, worker e agendamento cron e o **painel embutível** (API, tempo real e acesso +> configurável; a interface Vue chega numa próxima versão). ## Como funciona @@ -155,6 +156,67 @@ O host `hosts/PythonScriptRunner.WorkerHost` é um worker pronto, configurado po (`ConnectionStrings__psr`, `ConnectionStrings__rabbitmq`, `PythonScriptRunner__Worker__MaxConcurrency`, `PythonScriptRunner__Secrets__`, `PSR_AUTO_MIGRATE=true`), com `/health/live`, `/health/ready`, logs JSON e OTLP. +## Painel (Dashboard) + +Com `PythonScriptRunner.Dashboard`, o painel é embutido na sua aplicação, no estilo do Hangfire: API interna, atualizações +em tempo real (SignalR) e a interface servida no prefixo escolhido. Sem `MapPythonScriptRunnerDashboard` não existe painel. + +```csharp +builder.Services + .AddPythonScriptRunner() + .AddEntityFrameworkCore(o => o.UseNpgsql(builder.Configuration.GetConnectionString("psr")!)) + .AddRabbitMq(o => o.ConnectionString = builder.Configuration.GetConnectionString("rabbitmq")) + .AddDashboard(o => o.UseFixedLogin(login => login.BindConfiguration())); // seção PythonScriptRunner:Dashboard:Login + +var app = builder.Build(); +app.MapPythonScriptRunnerDashboard("/psr"); // API em /psr/api, tempo real em /psr/hubs/runs, painel em /psr/ +``` + +Escolha **um** modo de acesso; os papéis são cumulativos: `Reader` (vê tudo) → `Executor` (dispara, cancela, +reprocessa) → `Author` (edita, testa, publica, importa, agenda) → `Admin` (exclui, retenção, segredos, auditoria). + +| Modo | Configuração | +|------|--------------| +| Login fixo | `o.UseFixedLogin(l => l.BindConfiguration())` com usuários na configuração (abaixo) ou `l.Users` em código | +| Filtro próprio | `o.UseAuthorizationFilter()`: `IDashboardAuthorizationFilter` devolve `DashboardUser(nome, papel)` ou `null` | +| Autenticação do host | `o.UseHostAuthentication(r => { r.Reader = p => p.RequireRole("psr-leitor"); /* Executor, Author, Admin */ r.LoginUrl = "/conta/entrar?returnUrl={returnUrl}"; })` | +| Somente local | `o.AllowLocalRequests = true` — só em Development (fora dele a inicialização falha); soma-se a um modo principal | + +```json +"PythonScriptRunner": { + "Dashboard": { + "Login": { + "Users": [ + { "Name": "ana", "Role": "Admin", "PasswordHash": "AQAAAAIAAYagAAAAE..." }, + { "Name": "bruno", "Role": "Reader", "PasswordHash": "AQAAAAIAAYagAAAAE..." } + ] + } + } +} +``` + +A senha nunca vai para a configuração, só o hash (PBKDF2, formato do ASP.NET Identity), gerado pela ferramenta `psr`: + +```bash +dotnet tool install -g PythonScriptRunner.Tool +psr hash-password # digita a senha duas vezes, sem eco +echo "senha" | psr hash-password --stdin # em scripts +``` + +No login fixo a sessão é um cookie `psr.session` (HttpOnly, `SameSite=Lax`) de 8 h deslizantes e no máximo 24 h; 5 +falhas seguidas bloqueiam o usuário por 15 min e um IP pode tentar 10 vezes por minuto. Alterações na configuração valem +sem reiniciar (usuário removido ou senha trocada perde a sessão; papel novo vale na hora). As chaves do cookie ficam em +`psr.dashboard_keys`, então a sessão vale em todas as instâncias e sobrevive a reinícios. + +Na API, requisições que alteram algo exigem o cabeçalho `X-PSR-Request: 1`, edições usam `ETag`/`If-Match` e erros +seguem `ProblemDetails` com `code`. Em Development, o documento OpenAPI (interno) fica em `/psr/api/openapi/v1.json`. + +O host `hosts/PythonScriptRunner.DashboardHost` é um painel pronto: login fixo por variáveis de ambiente +(`PythonScriptRunner__Dashboard__Login__Users__0__Name`, `__Role`, `__PasswordHash`) ou OpenID Connect com +`Oidc__Authority`, `Oidc__ClientId` e `Oidc__ClientSecret` (papéis pela claim `Dashboard__RoleClaim`, padrão `roles`, e +`Dashboard__Roles__Reader|Executor|Author|Admin`). Também aceita `Dashboard__PathPrefix` (padrão `/psr`), +`ASPNETCORE_FORWARDEDHEADERS_ENABLED=true` atrás de proxy e `PSR_AUTO_MIGRATE=true`. + ## Requisitos - .NET 10 SDK @@ -170,7 +232,9 @@ O host `hosts/PythonScriptRunner.WorkerHost` é um worker pronto, configurado po | `src/PythonScriptRunner.EntityFrameworkCore` | PostgreSQL: catálogo versionado, histórico, arquivos, idempotência, outbox, auditoria, retenção, agendamentos e ciclo das execuções enfileiradas | | `src/PythonScriptRunner.RabbitMQ` | topologia, relay do outbox, `IRunQueue`, comando público, eventos e assinaturas | | `src/PythonScriptRunner.Worker` | execução dos passos enfileirados, *lease*, retry, cancelamento, cron | -| `hosts` | `WorkerHost` e `ServiceDefaults` (Serilog, OpenTelemetry, health checks) | +| `src/PythonScriptRunner.Dashboard` | painel embutível: API interna, hub SignalR, modos de acesso (login fixo, filtro, host, local) | +| `src/PythonScriptRunner.Tool` | dotnet tool `psr` (`psr hash-password`) | +| `hosts` | `WorkerHost`, `DashboardHost` e `ServiceDefaults` (Serilog, OpenTelemetry, health checks) | | `python/psrunner` | SDK Python (`@stage`, `Context`, `File`, `StageError`, `psrunner.testing`, CLI) | | `schemas` | JSON Schemas do `stage.json`, do `*.pipeline.json` e das mensagens públicas | | `tests` | testes .NET (unitários e de integração com Python real) e fixtures de trilha | @@ -183,12 +247,17 @@ dotnet test --project tests/PythonScriptRunner.Core.Tests dotnet test --project tests/PythonScriptRunner.EntityFrameworkCore.Tests # precisa de Docker dotnet test --project tests/PythonScriptRunner.RabbitMQ.Tests # precisa de Docker dotnet test --project tests/PythonScriptRunner.Worker.Tests # precisa de Docker +dotnet test --project tests/PythonScriptRunner.Dashboard.Tests # precisa de Docker cd python/psrunner uv sync uv run pytest ``` +O teste de desempenho do painel usa ~200 mil execuções; `PSR_PERF_RUNS_PER_DAY=50000` roda o volume médio completo (90 dias). +O documento OpenAPI do painel é comparado com `tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json`; +depois de revisar uma mudança intencional, `PSR_ACCEPT_SNAPSHOTS=1` atualiza o snapshot. + ## Licença [MIT](LICENSE) From 5cdda4992a6ee9fb177a2e8e67ccabcf9eea1b82 Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:21:47 -0300 Subject: [PATCH 6/7] =?UTF-8?q?fix(dashboard):=20tipo=20de=20m=C3=ADdia=20?= =?UTF-8?q?dos=20arquivos=20enviados=20sem=20par=C3=A2metros=20e=20OpenAPI?= =?UTF-8?q?=20igual=20em=20todo=20SO=20(spec=20007)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Partes de arquivo com "text/html; charset=utf-8" eram recusadas pelo contrato (contentTypes compara o tipo de mídia); o painel passa só o tipo de mídia. - O documento OpenAPI servido normaliza fins de linha nas descrições. Co-Authored-By: Claude Opus 5.5 --- .../Api/DashboardOpenApi.cs | 51 +++++++++++++++++++ .../Api/RunInputs.cs | 5 +- ...DashboardEndpointRouteBuilderExtensions.cs | 4 +- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs b/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs index a34b592..b2c5612 100644 --- a/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs +++ b/src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs @@ -52,6 +52,57 @@ public static void Configure(OpenApiOptions options) }); } + /// + /// Descriptions come from XML comments and keep the line endings of the checkout (CRLF on Windows); the served document uses + /// LF everywhere, so it is the same on every OS (its snapshot feeds the panel types). + /// + public static string NormalizeLineEndings(string json) + { + var document = JsonNode.Parse(json); + Normalize(document); + return document?.ToJsonString() ?? json; + + static JsonNode? Fix(JsonNode? node) => + node is JsonValue value && value.TryGetValue(out var text) && text.Contains('\r', StringComparison.Ordinal) + ? JsonValue.Create(text.ReplaceLineEndings("\n")) + : null; + + static void Normalize(JsonNode? node) + { + switch (node) + { + case JsonObject obj: + foreach (var (name, child) in obj.ToList()) + { + if (Fix(child) is { } fixedValue) + { + obj[name] = fixedValue; + } + else + { + Normalize(child); + } + } + + break; + case JsonArray array: + for (var i = 0; i < array.Count; i++) + { + if (Fix(array[i]) is { } fixedValue) + { + array[i] = fixedValue; + } + else + { + Normalize(array[i]); + } + } + + break; + } + } + } + /// Strongly typed identifiers (RunId, ArtifactId...) are written as their GUID text. private static bool IsIdentifier(Type type) => type is { IsValueType: true, Namespace: "PythonScriptRunner" } && type.Name.EndsWith("Id", StringComparison.Ordinal) diff --git a/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs b/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs index 8a54a14..d88e296 100644 --- a/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs +++ b/src/PythonScriptRunner.Dashboard/Api/RunInputs.cs @@ -149,7 +149,10 @@ private async Task ReadMultipartAsync(MultipartReader reader, PythonScriptRunner } var safeName = Path.GetFileName(fileName.Replace('\\', '/')); - var contentType = section.ContentType is { Length: > 0 } type ? type : PsrFile.GuessContentType(safeName); + // Only the media type counts (browsers may add parameters such as charset to text files). + var contentType = MediaTypeHeaderValue.TryParse(section.ContentType, out var media) && media.MediaType.HasValue + ? media.MediaType.Value! + : PsrFile.GuessContentType(safeName); if (!_files.TryGetValue(name, out var list)) { _files[name] = list = []; diff --git a/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs b/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs index b997e90..2c8927f 100644 --- a/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs +++ b/src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs @@ -68,8 +68,8 @@ public static IEndpointConventionBuilder MapPythonScriptRunnerDashboard(this IEn { var provider = http.RequestServices.GetRequiredKeyedService(DashboardOpenApi.Document); var document = await provider.GetOpenApiDocumentAsync(cancellationToken).ConfigureAwait(false); - return TypedResults.Text(await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, cancellationToken).ConfigureAwait(false), - "application/json; charset=utf-8"); + var json = await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, cancellationToken).ConfigureAwait(false); + return TypedResults.Text(DashboardOpenApi.NormalizeLineEndings(json), "application/json; charset=utf-8"); }) .AllowAnonymous() .ExcludeFromDescription(); From ea5b6663c37e590fe91a3cf60849ffa758507d28 Mon Sep 17 00:00:00 2001 From: net0well Date: Tue, 20 Oct 2026 15:21:47 -0300 Subject: [PATCH 7/7] =?UTF-8?q?test:=20pico=20real=20de=20concorr=C3=AAnci?= =?UTF-8?q?a=20no=20worker=20e=20diagn=C3=B3stico=20do=20snapshot=20do=20O?= =?UTF-8?q?penAPI=20(specs=20006,=20007)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Spec 006 AC-8 contava sobreposições por tentativa (A cruza B e C mesmo quando B e C não rodam juntas); agora mede o pico simultâneo com varredura no tempo. - O snapshot é localizado pela raiz do clone (builds determinísticos mapeiam o caminho do fonte para /_/) e a falha mostra as primeiras linhas diferentes. Co-Authored-By: Claude Opus 5.5 --- .../Infrastructure/Snapshot.cs | 41 +++++++++++++++++-- .../StepExecutionTests.cs | 6 ++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs index 8f2b8bb..c0f751d 100644 --- a/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs +++ b/tests/PythonScriptRunner.Dashboard.Tests/Infrastructure/Snapshot.cs @@ -1,4 +1,3 @@ -using System.Runtime.CompilerServices; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; @@ -13,9 +12,9 @@ public static class Snapshot { private static readonly JsonSerializerOptions Indented = new() { WriteIndented = true, NewLine = "\n", Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; - public static async Task MatchJsonAsync(string name, string json, [CallerFilePath] string testFile = "") + public static async Task MatchJsonAsync(string name, string json) { - var folder = Path.Combine(Path.GetDirectoryName(testFile)!, "Snapshots"); + var folder = Path.Combine(RepositoryRoot(), "tests", "PythonScriptRunner.Dashboard.Tests", "Snapshots"); var verified = Path.Combine(folder, $"{name}.verified.json"); var received = Path.Combine(folder, $"{name}.received.json"); var actual = (JsonNode.Parse(json) ?? throw new ArgumentException("Empty JSON.", nameof(json))).ToJsonString(Indented) + "\n"; @@ -36,6 +35,40 @@ public static async Task MatchJsonAsync(string name, string json, [CallerFilePat await File.WriteAllTextAsync(received, actual); Assert.Fail(expected is null ? $"No snapshot {verified}; review {received} and rename it to .verified.json." - : $"The snapshot changed: compare {received} with {verified} and replace the verified file when the change is intended."); + : $"The snapshot changed: compare {received} with {verified} and replace the verified file when the change is intended." + + Environment.NewLine + Differences(expected, actual)); + } + + /// The first lines that differ (the CI log shows them without the files). + private static string Differences(string expected, string actual) + { + var before = expected.Split('\n'); + var after = actual.Split('\n'); + var lines = new List(); + for (var i = 0; i < Math.Max(before.Length, after.Length) && lines.Count < 24; i++) + { + var left = i < before.Length ? before[i] : ""; + var right = i < after.Length ? after[i] : ""; + if (left != right) + { + lines.Add($"line {i + 1}:{Environment.NewLine} - {left}{Environment.NewLine} + {right}"); + } + } + + return string.Join(Environment.NewLine, lines); + } + + /// The clone (deterministic CI builds map source paths to /_/, so the test file path cannot be used). + private static string RepositoryRoot() + { + for (var directory = new DirectoryInfo(AppContext.BaseDirectory); directory is not null; directory = directory.Parent) + { + if (File.Exists(Path.Combine(directory.FullName, "PythonScriptRunner.slnx"))) + { + return directory.FullName; + } + } + + throw new InvalidOperationException($"PythonScriptRunner.slnx not found above {AppContext.BaseDirectory}."); } } diff --git a/tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs b/tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs index 4b17932..588f3fe 100644 --- a/tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs +++ b/tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs @@ -160,7 +160,11 @@ public async Task Spec006_AC08_MaxConcurrency_LimitsTheProcessesOfAWorker() attempts.Add(run.Steps.Single().Attempts.Single()); } - var peak = attempts.Max(a => attempts.Count(other => other.StartedAt < a.FinishedAt && a.StartedAt < other.FinishedAt)); + // Most attempts running at the same instant (a finish and a start at the same moment do not overlap). + var peak = attempts + .SelectMany(a => (IEnumerable<(DateTimeOffset At, int Delta)>)[(a.StartedAt, 1), (a.FinishedAt!.Value, -1)]) + .OrderBy(e => e.At).ThenBy(e => e.Delta) + .Aggregate((Running: 0, Peak: 0), (state, e) => (state.Running + e.Delta, Math.Max(state.Peak, state.Running + e.Delta))).Peak; peak.ShouldBe(2, string.Join(" | ", attempts.Select(a => $"{a.StartedAt:HH:mm:ss.ffffff}-{a.FinishedAt:HH:mm:ss.ffffff} {a.WorkerId}"))); }