135 lines
4.5 KiB
Go
135 lines
4.5 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"log/slog"
|
|
"math/big"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/backend/config"
|
|
coderunner "lincodex/backend/internal/adapter/secondary/code_runner"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const (
|
|
testRunnerToken = "bootstrap-runner-token-0123456789abcdef"
|
|
testRunnerRunKey = "bootstrap-runner-run-key-0123456789abcd"
|
|
)
|
|
|
|
func testCAPEM(t *testing.T) string {
|
|
t.Helper()
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatalf("GenerateKey() error = %v", err)
|
|
}
|
|
template := &x509.Certificate{
|
|
SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "bootstrap test CA"},
|
|
NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour),
|
|
IsCA: true, BasicConstraintsValid: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatalf("CreateCertificate() error = %v", err)
|
|
}
|
|
return string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}))
|
|
}
|
|
|
|
func enabledRunnerSettings() config.CodeRunnerSettings {
|
|
return config.CodeRunnerSettings{
|
|
URL: "https://10.80.0.2:8443", Token: testRunnerToken, RunKey: testRunnerRunKey,
|
|
RequestTimeout: 10 * time.Second,
|
|
}
|
|
}
|
|
|
|
func TestNewCodeRunnerDefaultsToTheDisabledRunner(t *testing.T) {
|
|
var logs bytes.Buffer
|
|
logger := slog.New(slog.NewTextHandler(&logs, nil))
|
|
|
|
runner, err := newCodeRunner(config.Config{}, logger)
|
|
|
|
if err != nil {
|
|
t.Fatalf("newCodeRunner() error = %v", err)
|
|
}
|
|
if _, isClient := runner.(*coderunner.Client); isClient {
|
|
t.Fatal("an unset CODE_RUNNER_URL selected the TLS client")
|
|
}
|
|
// Practice must see an unavailable runner, never a verdict.
|
|
_, err = runner.Poll(context.Background(), disabledProbeRequest())
|
|
if !errors.Is(err, outbound.ErrCodeRunnerDisabled) {
|
|
t.Fatalf("Poll() error = %v, want ErrCodeRunnerDisabled", err)
|
|
}
|
|
if !strings.Contains(logs.String(), "code runner is disabled") {
|
|
t.Fatalf("log = %q, want a disabled warning", logs.String())
|
|
}
|
|
}
|
|
|
|
func TestNewCodeRunnerBuildsTheClientFromAnInlineCAOrAFile(t *testing.T) {
|
|
caPEM := testCAPEM(t)
|
|
caFile := filepath.Join(t.TempDir(), "runner-ca.pem")
|
|
if err := os.WriteFile(caFile, []byte(caPEM), 0o600); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
inline, fromFile := enabledRunnerSettings(), enabledRunnerSettings()
|
|
inline.CAPEM = caPEM
|
|
fromFile.CAFile = caFile
|
|
|
|
for name, runner := range map[string]config.CodeRunnerSettings{"inline PEM": inline, "file": fromFile} {
|
|
var logs bytes.Buffer
|
|
selected, err := newCodeRunner(config.Config{CodeRunner: runner}, slog.New(slog.NewTextHandler(&logs, nil)))
|
|
if err != nil {
|
|
t.Fatalf("%s: newCodeRunner() error = %v", name, err)
|
|
}
|
|
if _, isClient := selected.(*coderunner.Client); !isClient {
|
|
t.Fatalf("%s: selected %T, want the TLS client", name, selected)
|
|
}
|
|
for _, secret := range []string{testRunnerToken, testRunnerRunKey, "10.80.0.2"} {
|
|
if strings.Contains(logs.String(), secret) {
|
|
t.Fatalf("%s: the startup log exposes %q", name, secret)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewCodeRunnerFailsInsteadOfFallingBackToDisabled(t *testing.T) {
|
|
missing := enabledRunnerSettings()
|
|
missing.CAFile = filepath.Join(t.TempDir(), "absent.pem")
|
|
notPEM := enabledRunnerSettings()
|
|
notPEM.CAPEM = "not a certificate"
|
|
|
|
for name, runner := range map[string]config.CodeRunnerSettings{"unreadable CA file": missing, "invalid CA": notPEM} {
|
|
selected, err := newCodeRunner(config.Config{CodeRunner: runner}, slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil)))
|
|
if err == nil || selected != nil {
|
|
t.Fatalf("%s: newCodeRunner() = %v, %v, want a startup error", name, selected, err)
|
|
}
|
|
if strings.Contains(err.Error(), testRunnerToken) || strings.Contains(err.Error(), testRunnerRunKey) {
|
|
t.Fatalf("%s: the error exposes a secret: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func disabledProbeRequest() outbound.CodeRunRequest {
|
|
return outbound.CodeRunRequest{
|
|
Key: outbound.CodeRunKey{AttemptItemID: [16]byte{1}, SubmissionID: [16]byte{2}},
|
|
Language: outbound.CodeLanguagePython3,
|
|
Source: "print(1)",
|
|
Limits: outbound.CodeRunLimits{TimeLimitMs: 1000, MemoryLimitMB: 128, OutputLimitKB: 64},
|
|
Groups: []outbound.CodeTestGroup{{
|
|
ID: "public", Visibility: outbound.CodeTestPublic,
|
|
Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerExact},
|
|
Tests: []outbound.CodeTest{{ID: "1", Input: "", ExpectedOutput: "1\n"}},
|
|
}},
|
|
}
|
|
}
|