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
57 changes: 53 additions & 4 deletions internal/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"net/url"
"strings"

stoatv1alpha1 "github.com/DataKnifeAI/stoat-operator/api/v1alpha1"
Expand Down Expand Up @@ -240,27 +241,31 @@ func (r *StoatReconciler) reconcileLiveKitConfig(ctx context.Context, instance *
if !liveKitEnabled(instance.Spec) {
return nil
}
redisURL, err := r.resolveDependencyURI(ctx, instance.Namespace, instance.Spec.Dependencies.Redis, "redis://"+svcRedis+"/")
if err != nil {
return err
}
rtc, udpStart, udpEnd := liveKitPorts(instance.Spec)
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: livekitCfgName,
Namespace: instance.Namespace,
},
}
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, cm, func() error {
_, err = controllerutil.CreateOrUpdate(ctx, r.Client, cm, func() error {
if err := controllerutil.SetControllerReference(instance, cm, r.Scheme); err != nil {
return err
}
cm.Labels = stoatLabels(instance.Name)
cm.Data = map[string]string{
"livekit.yml": renderLiveKitYAML(instance.Spec, secrets, rtc, udpStart, udpEnd),
"livekit.yml": renderLiveKitYAML(instance.Spec, secrets, redisURL, rtc, udpStart, udpEnd),
}
return nil
})
return err
}

func renderLiveKitYAML(spec stoatv1alpha1.StoatSpec, secrets *stoatSecrets, rtc, udpStart, udpEnd int32) string {
func renderLiveKitYAML(spec stoatv1alpha1.StoatSpec, secrets *stoatSecrets, redisURL string, rtc, udpStart, udpEnd int32) string {
var b strings.Builder
fmt.Fprintf(&b, "rtc:\n")
fmt.Fprintf(&b, " use_external_ip: %t\n", liveKitUseExternalIP(spec))
Expand All @@ -271,7 +276,7 @@ func renderLiveKitYAML(spec stoatv1alpha1.StoatSpec, secrets *stoatSecrets, rtc,
fmt.Fprintf(&b, " port_range_end: %d\n", udpEnd)
fmt.Fprintf(&b, " tcp_port: %d\n\n", rtc)
fmt.Fprintf(&b, "redis:\n")
fmt.Fprintf(&b, " address: %s:%d\n\n", svcRedis, portRedis)
writeLiveKitRedis(&b, redisURL)
fmt.Fprintf(&b, "turn:\n")
fmt.Fprintf(&b, " enabled: false\n\n")
fmt.Fprintf(&b, "keys:\n")
Expand All @@ -283,6 +288,50 @@ func renderLiveKitYAML(spec stoatv1alpha1.StoatSpec, secrets *stoatSecrets, rtc,
return b.String()
}

// writeLiveKitRedis maps a redis:// URI into LiveKit's redis config block.
// LiveKit hostNetwork pods cannot rely on the empty embedded `redis` Service
// when Dependencies.Redis.secretRef points at an external HA Redis.
func writeLiveKitRedis(b *strings.Builder, redisURL string) {
host, port, user, pass := parseRedisURL(redisURL)
if host == "" {
host = svcRedis
}
if port == "" {
port = fmt.Sprintf("%d", portRedis)
}
fmt.Fprintf(b, " address: %s:%s\n", host, port)
if user != "" {
fmt.Fprintf(b, " username: %s\n", user)
}
if pass != "" {
fmt.Fprintf(b, " password: %s\n", pass)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unquoted Redis credentials break YAML

Medium Severity

writeLiveKitRedis writes Redis username and password into livekit.yml as raw unquoted scalars. A credential that starts with #, *, !, &, |, or {, or that contains a colon-space, yields invalid YAML, so LiveKit fails to load config. Nearby node_ip already uses quoted YAML.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0dbadb4. Configure here.

}
fmt.Fprintf(b, "\n")
}

// parseRedisURL extracts host/port/user/password from redis:// or rediss:// URIs.
func parseRedisURL(raw string) (host, port, user, password string) {
raw = strings.TrimSpace(raw)
if raw == "" {
return "", "", "", ""
}
u, err := url.Parse(raw)
if err != nil || u.Host == "" {
// bare host:port fallback
if h, p, ok := strings.Cut(raw, ":"); ok && !strings.Contains(h, "/") {
return h, p, "", ""
}
return raw, "", "", ""
}
host = u.Hostname()
port = u.Port()
if u.User != nil {
user = u.User.Username()
password, _ = u.User.Password()
}
return host, port, user, password
}

func (r *StoatReconciler) reconcileCaddyConfig(ctx context.Context, instance *stoatv1alpha1.Stoat) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
17 changes: 16 additions & 1 deletion internal/controller/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,25 @@ func TestRenderLiveKitYAMLAddress(t *testing.T) {
spec := stoatv1alpha1.StoatSpec{
LiveKit: stoatv1alpha1.LiveKitSpec{Address: "192.168.14.189"},
}
out := renderLiveKitYAML(spec, s, 7881, 50000, 50100)
out := renderLiveKitYAML(spec, s, "redis://redis/", 7881, 50000, 50100)
if !strings.Contains(out, `use_external_ip: false`) || !strings.Contains(out, `node_ip: "192.168.14.189"`) {
t.Fatalf("expected VIP node_ip:\n%s", out)
}
if !strings.Contains(out, "address: redis:6379") {
t.Fatalf("expected default redis address:\n%s", out)
}
}

func TestRenderLiveKitYAMLExternalRedis(t *testing.T) {
s := &stoatSecrets{LiveKitKey: "lk", LiveKitSec: "lks"}
spec := stoatv1alpha1.StoatSpec{LiveKit: stoatv1alpha1.LiveKitSpec{Address: "192.168.14.189"}}
out := renderLiveKitYAML(spec, s, "redis://:[email protected]:6379/", 7881, 50000, 50100)
if !strings.Contains(out, "address: stoat-redis-master.stoat.svc.cluster.local:6379") {
t.Fatalf("expected external redis host:\n%s", out)
}
if !strings.Contains(out, "password: s3cret") {
t.Fatalf("expected redis password:\n%s", out)
}
}

func TestLiveKitNodeIPEnv(t *testing.T) {
Expand Down
Loading