Compare commits
8
Commits
cc6fe7083d
...
v0.33.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9643f5fb7 | ||
|
|
cace7d179d | ||
|
|
550764d43c | ||
|
|
f704258fdf | ||
|
|
e3288d0654 | ||
|
|
e0c53f8719 | ||
|
|
0828f0debf | ||
|
|
7282893877 |
@@ -289,4 +289,5 @@ Feature: Upload an audio recording, get a private Swedish transcript, discuss it
|
|||||||
|
|
||||||
Scenario: A recording in another language never reaches the Swedish-only engine
|
Scenario: A recording in another language never reaches the Swedish-only engine
|
||||||
When I choose Other for a recording
|
When I choose Other for a recording
|
||||||
Then the language is left for the engine to detect and no Swedish-only engine or reconcile is used
|
Then the language is left for the engine to detect and the Swedish-only base engine and the reconcile are not used
|
||||||
|
And the engine that does run is the configured one, which is not tuned for other languages
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ require (
|
|||||||
github.com/rogpeppe/go-internal v1.15.0 // indirect
|
github.com/rogpeppe/go-internal v1.15.0 // indirect
|
||||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
||||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||||
golang.org/x/sync v0.18.0 // indirect
|
golang.org/x/sync v0.21.0 // indirect
|
||||||
golang.org/x/sys v0.41.0 // indirect
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
golang.org/x/text v0.31.0 // indirect
|
golang.org/x/text v0.39.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.8 // indirect
|
google.golang.org/protobuf v1.36.8 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -113,12 +113,12 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
|||||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||||
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
||||||
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
||||||
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||||
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus=
|
||||||
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM=
|
||||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||||
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||||
|
|||||||
@@ -32,14 +32,24 @@ func NewEnsemble(base, alt, fallback Backend, adj reconcile.Adjudicator, opts re
|
|||||||
return &Ensemble{base: base, alt: alt, fallback: fallback, adj: adj, opts: opts}
|
return &Ensemble{base: base, alt: alt, fallback: fallback, adj: adj, opts: opts}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plan implements uploads.Planner: the phases this ensemble reports, so the page can
|
// Plan implements uploads.Planner: the phases of a Swedish job, the two-engine one. The
|
||||||
// show them before they run. The fallback is a phase only when one is configured.
|
// fallback is a phase only when one is configured.
|
||||||
func (e *Ensemble) Plan() []progress.Planned {
|
func (e *Ensemble) Plan() []progress.Planned { return e.PlanFor(domain.LangSwedish) }
|
||||||
plan := []progress.Planned{
|
|
||||||
{Key: "engine_a", Label: "Transcribing with " + e.base.Name()},
|
// PlanFor implements uploads.LanguagePlanner. A recording that is not Swedish never runs the
|
||||||
{Key: "engine_b", Label: "Transcribing with " + e.alt.Name()},
|
// base engine, the comparison or the decisions (transcribeOther), so the page lists only
|
||||||
{Key: "compare", Label: "Comparing the two transcripts"},
|
// what can run: the other engine, then the fallback if one is configured.
|
||||||
{Key: "decide", Label: "Deciding the differences"},
|
func (e *Ensemble) PlanFor(lang domain.Language) []progress.Planned {
|
||||||
|
var plan []progress.Planned
|
||||||
|
if lang == domain.LangSwedish || lang == "" {
|
||||||
|
plan = []progress.Planned{
|
||||||
|
{Key: "engine_a", Label: "Transcribing with " + e.base.Name()},
|
||||||
|
{Key: "engine_b", Label: "Transcribing with " + e.alt.Name()},
|
||||||
|
{Key: "compare", Label: "Comparing the two transcripts"},
|
||||||
|
{Key: "decide", Label: "Deciding the differences"},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
plan = []progress.Planned{{Key: "engine_b", Label: "Transcribing with " + e.alt.Name()}}
|
||||||
}
|
}
|
||||||
if e.fallback != nil {
|
if e.fallback != nil {
|
||||||
plan = append(plan, progress.Planned{Key: "fallback", Label: "Cloud fallback (" + e.fallback.Name() + ")"})
|
plan = append(plan, progress.Planned{Key: "fallback", Label: "Cloud fallback (" + e.fallback.Name() + ")"})
|
||||||
|
|||||||
@@ -160,3 +160,25 @@ func TestEnsembleProgress_TheReconcileCountsAreNotedForTheOperator(t *testing.T)
|
|||||||
assert.Equal(t, 0, n["unsure"])
|
assert.Equal(t, 0, n["unsure"])
|
||||||
assert.Equal(t, 0, n["errors"])
|
assert.Equal(t, 0, n["errors"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An auto-language upload never runs the base engine, the comparison or the decisions, so the
|
||||||
|
// page must not list them as coming (tapir#61, PR #62 review).
|
||||||
|
func TestEnsemblePlanFor_AnAutoLanguageListsOnlyThePhasesThatCanRun(t *testing.T) {
|
||||||
|
with := newEnsemble("a", "a", nil, nil, true, &verdictAdj{}).e
|
||||||
|
without := newEnsemble("a", "a", nil, nil, false, &verdictAdj{}).e
|
||||||
|
keys := func(p []progress.Planned) []string {
|
||||||
|
var k []string
|
||||||
|
for _, s := range p {
|
||||||
|
k = append(k, s.Key)
|
||||||
|
}
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"engine_b", "fallback"}, keys(with.PlanFor(domain.LangAuto)))
|
||||||
|
assert.Equal(t, []string{"engine_b"}, keys(without.PlanFor(domain.LangAuto)), "no fallback configured: none promised")
|
||||||
|
assert.Equal(t, with.Plan(), with.PlanFor(domain.LangSwedish), "Swedish is the plan it always was")
|
||||||
|
assert.Contains(t, with.PlanFor(domain.LangAuto)[0].Label, "iguana/kb-whisper")
|
||||||
|
assert.Equal(t, with.Plan(), with.PlanFor(""), "a job queued before the choice existed is Swedish")
|
||||||
|
assert.Equal(t, with.PlanFor(domain.LangAuto), with.PlanFor(domain.Language("xx")),
|
||||||
|
"an unknown language is planned as it is run: not the Swedish phases")
|
||||||
|
}
|
||||||
|
|||||||
@@ -102,3 +102,45 @@ func TestEnsemble_AHungEngineIsASoftFailureAndTheOtherEnginesTextIsUsed(t *testi
|
|||||||
assert.Contains(t, got.Reconciliation.Reason, "timeout")
|
assert.Contains(t, got.Reconciliation.Reason, "timeout")
|
||||||
assert.False(t, errors.Is(err, context.DeadlineExceeded))
|
assert.False(t, errors.Is(err, context.DeadlineExceeded))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTimeout sits in front of both local engines in production (cmd/tapir/uploads.go). It
|
||||||
|
// must hand the engine the job's context, not a fresh one: the language and the
|
||||||
|
// cancellation both travel on it (tapir#61).
|
||||||
|
|
||||||
|
func TestWithTimeout_TheEngineStillReceivesTheLanguageOfTheJob(t *testing.T) {
|
||||||
|
for name, tt := range map[string]struct {
|
||||||
|
ctx context.Context
|
||||||
|
wantField bool
|
||||||
|
}{
|
||||||
|
"auto leaves the field out": {domain.WithLanguage(context.Background(), domain.LangAuto), false},
|
||||||
|
"swedish sends sv": {domain.WithLanguage(context.Background(), domain.LangSwedish), true},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
var got seen
|
||||||
|
srv := serve(t, 200, verboseJSON, &got)
|
||||||
|
b := transcribe.WithTimeout(transcribe.NewGateway(srv.URL+"/v1", "k", "iguana/kb-whisper", srv.Client()), time.Minute)
|
||||||
|
|
||||||
|
_, err := b.Transcribe(tt.ctx, wavFile(t, "RIFF-synthetic-pcm"))
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
lang, present := got.fields["language"]
|
||||||
|
assert.Equal(t, tt.wantField, present)
|
||||||
|
if tt.wantField {
|
||||||
|
assert.Equal(t, "sv", lang)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithTimeout_ACancelledJobIsNotRestartedByTheWrapper(t *testing.T) {
|
||||||
|
var got seen
|
||||||
|
srv := serve(t, 200, verboseJSON, &got)
|
||||||
|
b := transcribe.WithTimeout(transcribe.NewGateway(srv.URL+"/v1", "k", "iguana/kb-whisper", srv.Client()), time.Minute)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
_, err := b.Transcribe(ctx, wavFile(t, "RIFF-synthetic-pcm"))
|
||||||
|
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Empty(t, got.method, "a cancelled job must not send its audio anywhere through the wrapper")
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,6 +33,13 @@ type Transcriber interface {
|
|||||||
Transcribe(ctx context.Context, wavPath string) (domain.Transcription, error)
|
Transcribe(ctx context.Context, wavPath string) (domain.Transcription, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LanguagePlanner is a Planner whose phases depend on the language of the job: a recording
|
||||||
|
// that is not Swedish skips the second engine and the reconcile, and the page should not list
|
||||||
|
// them as coming (tapir#61). The worker prefers it over Planner.
|
||||||
|
type LanguagePlanner interface {
|
||||||
|
PlanFor(lang domain.Language) []progress.Planned
|
||||||
|
}
|
||||||
|
|
||||||
// Planner is an optional extra on a Transcriber: it declares its own phases (for
|
// Planner is an optional extra on a Transcriber: it declares its own phases (for
|
||||||
// example one per engine) so the page can show them. A Transcriber that does not
|
// example one per engine) so the page can show them. A Transcriber that does not
|
||||||
// implement it gets a single "transcribing" phase from the worker. Whatever a Planner
|
// implement it gets a single "transcribing" phase from the worker. Whatever a Planner
|
||||||
@@ -251,8 +258,14 @@ func (w *Worker) Process(ctx context.Context, job Job) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plan := []progress.Planned{{Key: "received", Label: "Received"}, {Key: "audio", Label: "Preparing the audio"}}
|
plan := []progress.Planned{{Key: "received", Label: "Received"}, {Key: "audio", Label: "Preparing the audio"}}
|
||||||
|
lang := job.Language
|
||||||
|
if lang == "" {
|
||||||
|
lang = domain.LangSwedish // queued before the choice existed
|
||||||
|
}
|
||||||
planner, planned := w.tr.(Planner)
|
planner, planned := w.tr.(Planner)
|
||||||
if planned {
|
if lp, ok := w.tr.(LanguagePlanner); ok {
|
||||||
|
plan = append(plan, lp.PlanFor(lang)...)
|
||||||
|
} else if planned {
|
||||||
plan = append(plan, planner.Plan()...)
|
plan = append(plan, planner.Plan()...)
|
||||||
} else {
|
} else {
|
||||||
plan = append(plan, progress.Planned{Key: "transcribe", Label: "Transcribing"})
|
plan = append(plan, progress.Planned{Key: "transcribe", Label: "Transcribing"})
|
||||||
@@ -313,7 +326,7 @@ func (w *Worker) Process(ctx context.Context, job Job) {
|
|||||||
if !planned {
|
if !planned {
|
||||||
tr.Begin("transcribe", 0)
|
tr.Begin("transcribe", 0)
|
||||||
}
|
}
|
||||||
t, err := w.tr.Transcribe(domain.WithLanguage(progress.With(jobCtx, tr), job.Language), wav)
|
t, err := w.tr.Transcribe(domain.WithLanguage(progress.With(jobCtx, tr), lang), wav)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("upload: transcribe", "kind", errkind.Of(err), "elapsed", time.Since(start).Round(time.Second))
|
log.Error("upload: transcribe", "kind", errkind.Of(err), "elapsed", time.Since(start).Round(time.Second))
|
||||||
abort(domain.UploadErrTranscriptionFailed)
|
abort(domain.UploadErrTranscriptionFailed)
|
||||||
|
|||||||
@@ -213,3 +213,47 @@ func TestWorkerProgress_AStoreFailureLogsItsSQLStateNeverTheValueItQuotes(t *tes
|
|||||||
assert.Contains(t, out, "upload: store transcript")
|
assert.Contains(t, out, "upload: store transcript")
|
||||||
assert.Contains(t, out, "kind=sql_22P02")
|
assert.Contains(t, out, "kind=sql_22P02")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// langPlannedTr plans per language, like the Ensemble: an auto job has no base engine.
|
||||||
|
type langPlannedTr struct{ *fakeTranscriber }
|
||||||
|
|
||||||
|
func (langPlannedTr) Plan() []progress.Planned { return plannedTr{}.Plan() }
|
||||||
|
func (langPlannedTr) PlanFor(l domain.Language) []progress.Planned {
|
||||||
|
if l == domain.LangAuto {
|
||||||
|
return []progress.Planned{{Key: "engine_b", Label: "Transcribing with B"}}
|
||||||
|
}
|
||||||
|
return plannedTr{}.Plan()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p langPlannedTr) Transcribe(ctx context.Context, wav string) (domain.Transcription, error) {
|
||||||
|
r := progress.From(ctx)
|
||||||
|
if domain.LanguageFrom(ctx) != domain.LangAuto {
|
||||||
|
r.Begin("engine_a", 0)
|
||||||
|
r.Done("engine_a")
|
||||||
|
}
|
||||||
|
r.Begin("engine_b", 0)
|
||||||
|
r.Done("engine_b")
|
||||||
|
return p.fakeTranscriber.Transcribe(ctx, wav)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkerProgress_AJobGetsThePlanOfItsLanguage(t *testing.T) {
|
||||||
|
for name, tt := range map[string]struct {
|
||||||
|
lang domain.Language
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
"auto": {domain.LangAuto, []string{"received:done", "audio:done", "engine_b:done", "save:done"}},
|
||||||
|
"swedish": {domain.LangSwedish, []string{"received:done", "audio:done", "engine_a:done", "engine_b:done", "save:done"}},
|
||||||
|
"no language (queued before the choice existed)": {"", []string{"received:done", "audio:done", "engine_a:done", "engine_b:done", "save:done"}},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
r := newWRig(t, time.Minute)
|
||||||
|
w := newProgressWorker(t, r, langPlannedTr{r.tr})
|
||||||
|
job := r.spool(t, "a")
|
||||||
|
job.Language = tt.lang
|
||||||
|
|
||||||
|
w.Process(context.Background(), job)
|
||||||
|
|
||||||
|
assert.Equal(t, tt.want, keysAndStates(r.st.lastSteps()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package oidc_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"git.d-ma.be/mathias/tapir/internal/web/oidc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testConfig(issuer string) oidc.Config {
|
||||||
|
return oidc.Config{
|
||||||
|
Issuer: issuer,
|
||||||
|
ClientID: testClientID,
|
||||||
|
ClientSecret: "test-client-secret",
|
||||||
|
RedirectURL: "http://tapir.test/auth/callback",
|
||||||
|
SessionSecret: "test-session-secret-please-change",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #59: a discovery 503 at start-up is retried instead of failing New.
|
||||||
|
func TestNewRetriesDiscoveryAfter503UntilIssuerAnswers(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 2
|
||||||
|
|
||||||
|
auth, err := oidc.New(context.Background(), testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(5, time.Millisecond))
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, auth)
|
||||||
|
require.Equal(t, int32(3), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewReturnsDiscoveryErrorAfterAttemptsExhausted(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 1000
|
||||||
|
|
||||||
|
_, err := oidc.New(context.Background(), testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(3, time.Millisecond))
|
||||||
|
|
||||||
|
require.ErrorContains(t, err, "503")
|
||||||
|
require.Equal(t, int32(3), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewStopsDiscoveryBackoffWhenContextCancelled(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 1000
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
_, err := oidc.New(ctx, testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(5, time.Hour))
|
||||||
|
|
||||||
|
require.True(t, errors.Is(err, context.DeadlineExceeded), "err = %v", err)
|
||||||
|
require.Less(t, time.Since(start), 5*time.Second)
|
||||||
|
require.Equal(t, int32(1), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
+70
-20
@@ -57,6 +57,10 @@ const (
|
|||||||
pendingTTL = 10 * time.Minute
|
pendingTTL = 10 * time.Minute
|
||||||
sessionCookie = "tapir_session"
|
sessionCookie = "tapir_session"
|
||||||
loginPath = "/auth/login"
|
loginPath = "/auth/login"
|
||||||
|
|
||||||
|
// Discovery retry (#59): 5 tries, 2+4+8+16 s back-off ≈ 30 s, then fail.
|
||||||
|
defaultDiscoveryAttempts = 5
|
||||||
|
defaultDiscoveryDelay = 2 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// DexAuth implements web.Auth against Dex.
|
// DexAuth implements web.Auth against Dex.
|
||||||
@@ -70,6 +74,9 @@ type DexAuth struct {
|
|||||||
sessionTTL time.Duration
|
sessionTTL time.Duration
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
insecure bool // tests only: drop the Secure attribute so cookies flow over http
|
insecure bool // tests only: drop the Secure attribute so cookies flow over http
|
||||||
|
|
||||||
|
discoveryAttempts int
|
||||||
|
discoveryDelay time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ web.Auth = (*DexAuth)(nil)
|
var _ web.Auth = (*DexAuth)(nil)
|
||||||
@@ -96,14 +103,55 @@ func WithSessionTTL(ttl time.Duration) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDiscoveryRetry overrides the discovery attempt count and the initial
|
||||||
|
// back-off delay (doubled after each failed attempt).
|
||||||
|
func WithDiscoveryRetry(attempts int, delay time.Duration) Option {
|
||||||
|
return func(d *DexAuth) {
|
||||||
|
if attempts > 0 {
|
||||||
|
d.discoveryAttempts = attempts
|
||||||
|
}
|
||||||
|
if delay > 0 {
|
||||||
|
d.discoveryDelay = delay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithInsecureCookies drops the Secure cookie attribute. TEST ONLY — it lets
|
// WithInsecureCookies drops the Secure cookie attribute. TEST ONLY — it lets
|
||||||
// the session cookie travel over plain http; never use it in production.
|
// the session cookie travel over plain http; never use it in production.
|
||||||
func WithInsecureCookies() Option {
|
func WithInsecureCookies() Option {
|
||||||
return func(d *DexAuth) { d.insecure = true }
|
return func(d *DexAuth) { d.insecure = true }
|
||||||
}
|
}
|
||||||
|
|
||||||
// New discovers the issuer and returns a configured DexAuth. ctx bounds the
|
// discover runs OIDC discovery up to attempts times, doubling delay between
|
||||||
// discovery request only.
|
// tries (#59: an issuer briefly unavailable behind its proxy answered 503 and
|
||||||
|
// serve exited). A cancelled ctx ends the back-off immediately with ctx's error.
|
||||||
|
func discover(ctx context.Context, issuer string, attempts int, delay time.Duration) (*oidc.Provider, error) {
|
||||||
|
var lastErr error
|
||||||
|
for i := range attempts {
|
||||||
|
if i > 0 {
|
||||||
|
t := time.NewTimer(delay)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Stop()
|
||||||
|
return nil, fmt.Errorf("after %d attempts (last: %v): %w", i, lastErr, ctx.Err())
|
||||||
|
case <-t.C:
|
||||||
|
}
|
||||||
|
delay *= 2
|
||||||
|
}
|
||||||
|
p, err := oidc.NewProvider(ctx, issuer)
|
||||||
|
if err == nil {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil, fmt.Errorf("after %d attempts: %w", i+1, ctx.Err())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("after %d attempts: %w", attempts, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New discovers the issuer and returns a configured DexAuth. ctx bounds discovery,
|
||||||
|
// including its retry back-off.
|
||||||
func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
||||||
for name, val := range map[string]string{
|
for name, val := range map[string]string{
|
||||||
"issuer": cfg.Issuer,
|
"issuer": cfg.Issuer,
|
||||||
@@ -117,29 +165,31 @@ func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, err := oidc.NewProvider(ctx, cfg.Issuer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("oidc: discover issuer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := &DexAuth{
|
d := &DexAuth{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
verifier: provider.Verifier(&oidc.Config{ClientID: cfg.ClientID}),
|
pending: newPendingStore(),
|
||||||
oauth: &oauth2.Config{
|
secret: []byte(cfg.SessionSecret),
|
||||||
ClientID: cfg.ClientID,
|
sessionTTL: defaultSessionTTL,
|
||||||
ClientSecret: cfg.ClientSecret,
|
now: time.Now,
|
||||||
Endpoint: provider.Endpoint(),
|
discoveryAttempts: defaultDiscoveryAttempts,
|
||||||
RedirectURL: cfg.RedirectURL,
|
discoveryDelay: defaultDiscoveryDelay,
|
||||||
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
|
|
||||||
},
|
|
||||||
pending: newPendingStore(),
|
|
||||||
secret: []byte(cfg.SessionSecret),
|
|
||||||
sessionTTL: defaultSessionTTL,
|
|
||||||
now: time.Now,
|
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(d)
|
o(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider, err := discover(ctx, cfg.Issuer, d.discoveryAttempts, d.discoveryDelay)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("oidc: discover issuer: %w", err)
|
||||||
|
}
|
||||||
|
d.verifier = provider.Verifier(&oidc.Config{ClientID: cfg.ClientID})
|
||||||
|
d.oauth = &oauth2.Config{
|
||||||
|
ClientID: cfg.ClientID,
|
||||||
|
ClientSecret: cfg.ClientSecret,
|
||||||
|
Endpoint: provider.Endpoint(),
|
||||||
|
RedirectURL: cfg.RedirectURL,
|
||||||
|
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
|
||||||
|
}
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -34,6 +35,11 @@ type fakeIssuer struct {
|
|||||||
sub string
|
sub string
|
||||||
email string
|
email string
|
||||||
nonce string
|
nonce string
|
||||||
|
|
||||||
|
// discoveryHits counts requests to the discovery document; the first
|
||||||
|
// unavailableFor of them answer 503 (issuer briefly down behind its proxy).
|
||||||
|
discoveryHits atomic.Int32
|
||||||
|
unavailableFor int32
|
||||||
}
|
}
|
||||||
|
|
||||||
const testKID = "test-key"
|
const testKID = "test-key"
|
||||||
@@ -47,6 +53,10 @@ func newFakeIssuer(t *testing.T) *fakeIssuer {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
|
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
if f.discoveryHits.Add(1) <= f.unavailableFor {
|
||||||
|
http.Error(w, "no available server", http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
writeJSON(t, w, map[string]any{
|
writeJSON(t, w, map[string]any{
|
||||||
"issuer": f.server.URL,
|
"issuer": f.server.URL,
|
||||||
"authorization_endpoint": f.server.URL + "/authorize",
|
"authorization_endpoint": f.server.URL + "/authorize",
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ templ UploadPage(u domain.UploadedTranscript, chatEnabled bool, now time.Time) {
|
|||||||
@Layout("Tapir — " + u.Title) {
|
@Layout("Tapir — " + u.Title) {
|
||||||
<article class="account">
|
<article class="account">
|
||||||
<h1>{ u.Title }</h1>
|
<h1>{ u.Title }</h1>
|
||||||
|
<p class="muted">Language: { languageLabel(u.Language) }</p>
|
||||||
@uploadStatus(u, chatEnabled, now)
|
@uploadStatus(u, chatEnabled, now)
|
||||||
<p><a href="/uploads">All recordings</a></p>
|
<p><a href="/uploads">All recordings</a></p>
|
||||||
</article>
|
</article>
|
||||||
@@ -74,7 +75,6 @@ templ uploadStatus(u domain.UploadedTranscript, chatEnabled bool, now time.Time)
|
|||||||
Transcribed { uploads.FormatDuration(time.Duration(u.AudioSeconds * float64(time.Second))) }
|
Transcribed { uploads.FormatDuration(time.Duration(u.AudioSeconds * float64(time.Second))) }
|
||||||
of audio by <span class="chip">{ u.Endpoint }</span>.
|
of audio by <span class="chip">{ u.Endpoint }</span>.
|
||||||
</p>
|
</p>
|
||||||
<p>Language: { languageLabel(u.Language) }</p>
|
|
||||||
if provider, cloud := cloudProvider(u.Endpoint); cloud {
|
if provider, cloud := cloudProvider(u.Endpoint); cloud {
|
||||||
<p class="cloud-note">
|
<p class="cloud-note">
|
||||||
This recording's audio left the homelab: it was transcribed by a cloud service ({ provider })
|
This recording's audio left the homelab: it was transcribed by a cloud service ({ provider })
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func TestUploadPost_ALanguageOutsideTheClosedSetIsRefusedBeforeAnythingIsSpooled
|
|||||||
rows, err := newStore(t).ListUploads(context.Background(), userID)
|
rows, err := newStore(t).ListUploads(context.Background(), userID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Empty(t, rows, "and must leave no row")
|
assert.Empty(t, rows, "and must leave no row")
|
||||||
assert.Contains(t, body(t, rec), "language")
|
assert.Contains(t, body(t, rec), "Choose a language from the list.")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,23 +127,25 @@ func TestUploadPage_TheFormOffersSwedishPreselectedAndOtherBeforeTheFilePart(t *
|
|||||||
assert.Contains(t, b, `<option value="auto"`)
|
assert.Contains(t, b, `<option value="auto"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUploadStatus_NamesTheLanguageTheRecordingWasTranscribedAs(t *testing.T) {
|
func TestUploadPage_NamesTheLanguageInEveryState(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, status := range []string{"queued", "transcribing", "done", "failed"} {
|
||||||
language domain.Language
|
for _, tt := range []struct {
|
||||||
want string
|
language domain.Language
|
||||||
}{
|
want string
|
||||||
{domain.LangSwedish, "Swedish"},
|
}{
|
||||||
{domain.LangAuto, "Other (detected by the engine)"},
|
{domain.LangSwedish, "Swedish"},
|
||||||
} {
|
{domain.LangAuto, "Other (detected by the engine)"},
|
||||||
t.Run(string(tt.language), func(t *testing.T) {
|
} {
|
||||||
app, _ := newUploadApp(t, audioOK, 200<<20)
|
t.Run(status+"/"+string(tt.language), func(t *testing.T) {
|
||||||
id := seedUpload(t, userID, "m", "done", "Synthetic transcript text.")
|
app, _ := newUploadApp(t, audioOK, 200<<20)
|
||||||
_, err := rawPool(t).Exec(context.Background(), `UPDATE uploaded_transcripts SET language = $2 WHERE id = $1`, id, string(tt.language))
|
id := seedUpload(t, userID, "m", status, "Synthetic transcript text.")
|
||||||
require.NoError(t, err)
|
_, err := rawPool(t).Exec(context.Background(), `UPDATE uploaded_transcripts SET language = $2 WHERE id = $1`, id, string(tt.language))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
b := body(t, do(t, app, get("/uploads/"+id+"/status")))
|
b := body(t, do(t, app, get("/uploads/"+id)))
|
||||||
|
|
||||||
assert.Contains(t, b, "Language: "+tt.want)
|
assert.Contains(t, b, "Language: "+tt.want)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,7 +222,20 @@ func UploadPage(u domain.UploadedTranscript, chatEnabled bool, now time.Time) te
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "</h1>")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "</h1><p class=\"muted\">Language: ")
|
||||||
|
if templ_7745c5c3_Err != nil {
|
||||||
|
return templ_7745c5c3_Err
|
||||||
|
}
|
||||||
|
var templ_7745c5c3_Var12 string
|
||||||
|
templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(languageLabel(u.Language))
|
||||||
|
if templ_7745c5c3_Err != nil {
|
||||||
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 60, Col: 57}
|
||||||
|
}
|
||||||
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12))
|
||||||
|
if templ_7745c5c3_Err != nil {
|
||||||
|
return templ_7745c5c3_Err
|
||||||
|
}
|
||||||
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "</p>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
@@ -230,7 +243,7 @@ func UploadPage(u domain.UploadedTranscript, chatEnabled bool, now time.Time) te
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "<p><a href=\"/uploads\">All recordings</a></p></article>")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "<p><a href=\"/uploads\">All recordings</a></p></article>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
@@ -262,14 +275,14 @@ func uploadStatus(u domain.UploadedTranscript, chatEnabled bool, now time.Time)
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
ctx = templ.InitializeContext(ctx)
|
ctx = templ.InitializeContext(ctx)
|
||||||
templ_7745c5c3_Var12 := templ.GetChildren(ctx)
|
templ_7745c5c3_Var13 := templ.GetChildren(ctx)
|
||||||
if templ_7745c5c3_Var12 == nil {
|
if templ_7745c5c3_Var13 == nil {
|
||||||
templ_7745c5c3_Var12 = templ.NopComponent
|
templ_7745c5c3_Var13 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
ctx = templ.ClearChildren(ctx)
|
||||||
switch u.Status {
|
switch u.Status {
|
||||||
case domain.UploadDone:
|
case domain.UploadDone:
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "<div class=\"upload-status upload-done\" role=\"status\">")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "<div class=\"upload-status upload-done\" role=\"status\">")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
@@ -277,46 +290,33 @@ func uploadStatus(u domain.UploadedTranscript, chatEnabled bool, now time.Time)
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "<p>Transcribed ")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "<p>Transcribed ")
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
var templ_7745c5c3_Var13 string
|
|
||||||
templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinStringErrs(uploads.FormatDuration(time.Duration(u.AudioSeconds * float64(time.Second))))
|
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 74, Col: 95}
|
|
||||||
}
|
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13))
|
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, " of audio by <span class=\"chip\">")
|
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
var templ_7745c5c3_Var14 string
|
var templ_7745c5c3_Var14 string
|
||||||
templ_7745c5c3_Var14, templ_7745c5c3_Err = templ.JoinStringErrs(u.Endpoint)
|
templ_7745c5c3_Var14, templ_7745c5c3_Err = templ.JoinStringErrs(uploads.FormatDuration(time.Duration(u.AudioSeconds * float64(time.Second))))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 75, Col: 48}
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 75, Col: 95}
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var14))
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var14))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 20, "</span>.</p><p>Language: ")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 20, " of audio by <span class=\"chip\">")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
var templ_7745c5c3_Var15 string
|
var templ_7745c5c3_Var15 string
|
||||||
templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(languageLabel(u.Language))
|
templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(u.Endpoint)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 77, Col: 44}
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/uploads.templ`, Line: 76, Col: 48}
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15))
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "</p>")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "</span>.</p>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user