forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

knotserver: add hostname config

anirudh.fi f787e5e4 a37a33f5

verified
Changed files
+16 -58
cmd
keyfetch
knotserver
knotserver
-45
cmd/keyfetch/format_test.go
···
-
package main
-
-
import "testing"
-
-
func TestFormatKeyData(t *testing.T) {
-
tests := []struct {
-
name string
-
repoguardPath string
-
data map[string]string
-
want string
-
}{
-
{
-
name: "single user",
-
repoguardPath: "/usr/bin/repoguard",
-
data: map[string]string{
-
"user1": "ssh-rsa AAAA...",
-
},
-
want: `command="/usr/bin/repoguard -base-dir /home/git -user user1 -log-path /home/git/log ",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAA...` + "\n",
-
},
-
{
-
name: "multiple users",
-
repoguardPath: "/usr/bin/repoguard",
-
data: map[string]string{
-
"user1": "ssh-rsa AAAA...",
-
"user2": "ssh-rsa BBBB...",
-
},
-
want: `command="/usr/bin/repoguard -base-dir /home/git -user user1 -log-path /home/git/log ",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAA...` + "\n" +
-
`command="/usr/bin/repoguard -base-dir /home/git -user user2 -log-path /home/git/log ",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa BBBB...` + "\n",
-
},
-
{
-
name: "empty data",
-
repoguardPath: "/usr/bin/repoguard",
-
data: map[string]string{},
-
want: "",
-
},
-
}
-
-
for _, tt := range tests {
-
t.Run(tt.name, func(t *testing.T) {
-
if got := formatKeyData(tt.repoguardPath, tt.data); got != tt.want {
-
t.Errorf("formatKeyData() = %v, want %v", got, tt.want)
-
}
-
})
-
}
-
}
+4 -8
cmd/knotserver/main.go
···
import (
"context"
-
"fmt"
"net/http"
"github.com/sotangled/tangled/knotserver"
···
l.Error("failed to setup server", "error", err)
return
}
-
addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
-
imux := knotserver.Internal(ctx, db, e)
-
iaddr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.InternalPort)
-
l.Info("starting internal server", "address", iaddr)
-
go http.ListenAndServe(iaddr, imux)
+
l.Info("starting internal server", "address", c.Server.InternalListenAddr)
+
go http.ListenAndServe(c.Server.InternalListenAddr, imux)
-
l.Info("starting main server", "address", addr)
-
l.Error("server error", "error", http.ListenAndServe(addr, mux))
+
l.Info("starting main server", "address", c.Server.ListenAddr)
+
l.Error("server error", "error", http.ListenAndServe(c.Server.ListenAddr, mux))
return
}
+6 -5
knotserver/config/config.go
···
}
type Server struct {
-
Host string `env:"HOST, default=0.0.0.0"`
-
Port int `env:"PORT, default=5555"`
-
InternalPort int `env:"PORT, default=5444"`
-
Secret string `env:"SECRET, required"`
-
DBPath string `env:"DB_PATH, default=knotserver.db"`
+
ListenAddr string `env:"LISTEN_ADDR, default=0.0.0.0:5555"`
+
InternalListenAddr string `env:"INTERNAL_LISTEN_ADDR, default=0.0.0.0:5444"`
+
Secret string `env:"SECRET, required"`
+
DBPath string `env:"DB_PATH, default=knotserver.db"`
+
Hostname string `env:"HOSTNAME, required"`
+
// This disables signature verification so use with caution.
Dev bool `env:"DEV, default=false"`
}
+6
knotserver/jetstream.go
···
func (h *Handle) processKnotMember(ctx context.Context, did string, record map[string]interface{}) error {
l := log.FromContext(ctx)
+
+
if record["domain"] != h.c.Server.Hostname {
+
l.Error("domain mismatch", "domain", record["domain"], "expected", h.c.Server.Hostname)
+
return fmt.Errorf("domain mismatch: %s != %s", record["domain"], h.c.Server.Hostname)
+
}
+
ok, err := h.e.E.Enforce(did, ThisServer, ThisServer, "server:invite")
if err != nil || !ok {
l.Error("failed to add member", "did", did)