Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<ItemGroup>
<PackageVersion Include="Cronos" Version="0.13.0" />
<PackageVersion Include="JsonSchema.Net" Version="9.4.1" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.13" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.13" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.13" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="10.0.13" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.13" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.13" />
Expand All @@ -15,8 +18,10 @@
<PackageVersion Include="Microsoft.Extensions.Diagnostics" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.Testing" Version="10.11.0" />
<PackageVersion Include="Microsoft.Extensions.FileProviders.Embedded" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Identity.Core" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.13" />
<PackageVersion Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.11.0" />
Expand Down
4 changes: 4 additions & 0 deletions PythonScriptRunner.slnx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<Solution>
<Folder Name="/hosts/">
<Project Path="hosts/PythonScriptRunner.DashboardHost/PythonScriptRunner.DashboardHost.csproj" />
<Project Path="hosts/PythonScriptRunner.ServiceDefaults/PythonScriptRunner.ServiceDefaults.csproj" />
<Project Path="hosts/PythonScriptRunner.WorkerHost/PythonScriptRunner.WorkerHost.csproj" />
</Folder>
<Folder Name="/src/">
<Project Path="src/PythonScriptRunner.Abstractions/PythonScriptRunner.Abstractions.csproj" />
<Project Path="src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj" />
<Project Path="src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj" />
<Project Path="src/PythonScriptRunner.EntityFrameworkCore/PythonScriptRunner.EntityFrameworkCore.csproj" />
<Project Path="src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj" />
<Project Path="src/PythonScriptRunner.Tool/PythonScriptRunner.Tool.csproj" />
<Project Path="src/PythonScriptRunner.Worker/PythonScriptRunner.Worker.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/PythonScriptRunner.Core.Tests/PythonScriptRunner.Core.Tests.csproj" />
<Project Path="tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj" />
<Project Path="tests/PythonScriptRunner.EntityFrameworkCore.Tests/PythonScriptRunner.EntityFrameworkCore.Tests.csproj" />
<Project Path="tests/PythonScriptRunner.RabbitMQ.Tests/PythonScriptRunner.RabbitMQ.Tests.csproj" />
<Project Path="tests/PythonScriptRunner.Worker.Tests/PythonScriptRunner.Worker.Tests.csproj" />
Expand Down
75 changes: 72 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -155,6 +156,67 @@ O host `hosts/PythonScriptRunner.WorkerHost` é um worker pronto, configurado po
(`ConnectionStrings__psr`, `ConnectionStrings__rabbitmq`, `PythonScriptRunner__Worker__MaxConcurrency`,
`PythonScriptRunner__Secrets__<NOME>`, `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<MeuFiltro>()`: `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
Expand All @@ -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 |
Expand All @@ -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)
134 changes: 134 additions & 0 deletions hosts/PythonScriptRunner.DashboardHost/Program.cs
Original file line number Diff line number Diff line change
@@ -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<bool>("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] != '\\'));

/// <summary>Entry point (public for the integration tests).</summary>
public partial class Program;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<RootNamespace>PythonScriptRunner.DashboardHost</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\PythonScriptRunner.Dashboard\PythonScriptRunner.Dashboard.csproj" />
<ProjectReference Include="..\PythonScriptRunner.ServiceDefaults\PythonScriptRunner.ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
namespace PythonScriptRunner.Core.Pipelines;
namespace PythonScriptRunner;

/// <summary>Which source types can feed which target types in a binding (spec 003, safe widenings only).</summary>
internal static class TypeCompatibility
public static class FieldCompatibility
{
public static bool IsAssignable(FieldDefinition source, FieldDefinition target) =>
IsAssignable(source.Type, target.Type, source, target);
/// <summary>Whether a value of <paramref name="source"/> can be bound to <paramref name="target"/>.</summary>
/// <param name="source">Field that produces the value.</param>
/// <param name="target">Field that receives it.</param>
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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/PythonScriptRunner.Abstractions/Fields/FieldDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace PythonScriptRunner;

/// <summary>Declaration of one input or output of a stage.</summary>
/// <param name="Name">Field name (<c>^[a-z_][a-z0-9_]{0,63}$</c>).</param>
/// <param name="Type">Field type.</param>
[JsonConverter(typeof(FieldDefinitionJsonConverter))]
public sealed record FieldDefinition(string Name, FieldType Type)
{
/// <summary>Whether a value must be provided (inputs) or produced (outputs). Default: <c>true</c>.</summary>
Expand Down
Loading
Loading