Initial
This commit is contained in:
commit
ae0c5392b7
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
secrets.nix
|
||||
hardware-configuration.nix
|
9
LICENSE
Normal file
9
LICENSE
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Mikhail Klementev
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
207
configuration.nix
Normal file
207
configuration.nix
Normal file
@ -0,0 +1,207 @@
|
||||
{ config, pkgs, lib, ... }:
|
||||
let
|
||||
secrets = import ./secrets.nix;
|
||||
|
||||
ldap = pkgs.buildGoModule rec {
|
||||
name = "ldap";
|
||||
src = ./ldap;
|
||||
vendorHash = "sha256-HlsVCWs7Q4kBAtRpt3U323tRmgWdQxZlpfMZ/cSlw4Q=";
|
||||
};
|
||||
|
||||
image =
|
||||
"chocobozzz/peertube@" +
|
||||
"sha256:3bd126fc8b66a6a12593d73f74d0a3ffc7fc3206e5e9ebf39c8a8e0ca5408194";
|
||||
|
||||
domainName = "v.lor.sh";
|
||||
hostName = builtins.replaceStrings [ "." ] [ "-" ] "${domainName}";
|
||||
in {
|
||||
imports = [ ./hardware-configuration.nix ];
|
||||
|
||||
boot.loader = {
|
||||
efi.canTouchEfiVariables = true;
|
||||
grub = {
|
||||
enable = true;
|
||||
efiSupport = true;
|
||||
device = "nodev";
|
||||
mirroredBoots = [{
|
||||
devices = [ "nodev" ];
|
||||
path = "/boot-fallback";
|
||||
}];
|
||||
};
|
||||
};
|
||||
|
||||
networking = {
|
||||
hostName = hostName;
|
||||
hostId = (builtins.substring 0 8 (builtins.readFile "/etc/machine-id"));
|
||||
|
||||
useDHCP = false;
|
||||
|
||||
interfaces.eno1 = {
|
||||
ipv4 = secrets.ipv4;
|
||||
ipv6 = secrets.ipv6;
|
||||
};
|
||||
|
||||
nameservers = [ "1.1.1.1" ];
|
||||
|
||||
firewall = {
|
||||
enable = true;
|
||||
allowedTCPPorts = [ 80 443 ];
|
||||
};
|
||||
};
|
||||
|
||||
users.extraUsers.root.openssh.authorizedKeys.keys = secrets.pubkeys;
|
||||
|
||||
services.openssh.enable = true;
|
||||
|
||||
environment.systemPackages = with pkgs; [ vim htop git tmux ];
|
||||
|
||||
systemd.services."peertube-ldap" = {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network.target" ];
|
||||
environment = {
|
||||
AUTH_URL = secrets.peertube.auth.url;
|
||||
AUTH_SECRET = secrets.peertube.auth.secret;
|
||||
LDAP_USER = secrets.peertube.ldap.user;
|
||||
LDAP_PASS = secrets.peertube.ldap.password;
|
||||
};
|
||||
serviceConfig = {
|
||||
Restart = "always";
|
||||
RestartSec = 30;
|
||||
ExecStart = "${ldap}/bin/ldap";
|
||||
User = "peertube";
|
||||
};
|
||||
};
|
||||
|
||||
services.caddy = {
|
||||
enable = true;
|
||||
virtualHosts."${domainName}".extraConfig = ''
|
||||
encode gzip
|
||||
|
||||
root * /dev/null
|
||||
reverse_proxy localhost:9000
|
||||
|
||||
header {
|
||||
Strict-Transport-Security "max-age=31536000;"
|
||||
}
|
||||
'';
|
||||
};
|
||||
|
||||
system.activationScripts.peertube = ''
|
||||
mkdir -p /var/lib/peertube/{storage,config}
|
||||
cat > /var/lib/peertube/config/local.yml <<EOF
|
||||
redis:
|
||||
hostname: '${hostName}'
|
||||
|
||||
object_storage:
|
||||
enabled: true
|
||||
endpoint: 'https://sos-ch-gva-2.exo.io'
|
||||
region: 'ch-gva-2'
|
||||
|
||||
upload_acl:
|
||||
public: 'public-read'
|
||||
private: 'private'
|
||||
|
||||
proxy:
|
||||
proxify_private_files: true
|
||||
|
||||
credentials:
|
||||
access_key_id: '${secrets.peertube.s3.id}'
|
||||
secret_access_key: '${secrets.peertube.s3.key}'
|
||||
|
||||
max_upload_part: 2GB
|
||||
|
||||
streaming_playlists:
|
||||
bucket_name: 'v-lor-sh'
|
||||
prefix: 'streaming-playlists/'
|
||||
base_url: 'https://v-lor-sh.sos-ch-gva-2.exoscale-cdn.com'
|
||||
|
||||
videos:
|
||||
bucket_name: 'v-lor-sh'
|
||||
prefix: 'videos/'
|
||||
base_url: 'https://v-lor-sh.sos-ch-gva-2.exoscale-cdn.com'
|
||||
EOF
|
||||
'';
|
||||
|
||||
users.users.peertube = {
|
||||
isSystemUser = true;
|
||||
group = "peertube";
|
||||
};
|
||||
|
||||
users.groups.peertube = { };
|
||||
|
||||
systemd.services.peertube-init-db = {
|
||||
description = "Initialization database for PeerTube daemon";
|
||||
after = [ "network.target" "postgresql.service" ];
|
||||
requires = [ "postgresql.service" ];
|
||||
before = [ "docker-peertube.service" ];
|
||||
wantedBy = [ "docker-peertube.service" ];
|
||||
script = ''
|
||||
${pkgs.postgresql}/bin/psql peertube -c '\q' && exit 0
|
||||
${pkgs.postgresql}/bin/createuser -w peertube
|
||||
${pkgs.postgresql}/bin/psql -c "ALTER USER peertube WITH PASSWORD '${secrets.peertube.db.password}'";
|
||||
|
||||
${pkgs.postgresql}/bin/createdb -O peertube -E UTF8 -T template0 peertube
|
||||
${pkgs.postgresql}/bin/psql -c "CREATE EXTENSION pg_trgm;" peertube
|
||||
${pkgs.postgresql}/bin/psql -c "CREATE EXTENSION unaccent;" peertube
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
User = "postgres";
|
||||
Group = "postgres";
|
||||
};
|
||||
};
|
||||
|
||||
services.postgresql = {
|
||||
enable = true;
|
||||
enableTCPIP = true;
|
||||
};
|
||||
|
||||
services.redis.servers.peertube = {
|
||||
enable = true;
|
||||
bind = "127.0.0.1";
|
||||
port = 6379;
|
||||
};
|
||||
|
||||
virtualisation.oci-containers.backend = "docker";
|
||||
|
||||
virtualisation.oci-containers.containers = {
|
||||
peertube = {
|
||||
image = image;
|
||||
environment = {
|
||||
PEERTUBE_SECRET = "${secrets.peertube.secret}";
|
||||
PEERTUBE_WEBSERVER_HOSTNAME = domainName;
|
||||
PEERTUBE_DB_USERNAME = "peertube";
|
||||
PEERTUBE_DB_PASSWORD = secrets.peertube.db.password;
|
||||
PEERTUBE_DB_HOSTNAME = hostName;
|
||||
PEERTUBE_ADMIN_EMAIL = secrets.peertube.admin.email;
|
||||
};
|
||||
volumes = [
|
||||
"/var/lib/peertube/storage:/data"
|
||||
"/var/lib/peertube/config:/config"
|
||||
];
|
||||
extraOptions = [ "--network=host" ];
|
||||
};
|
||||
};
|
||||
|
||||
services.zfs = {
|
||||
autoScrub.enable = true;
|
||||
trim.enable = true;
|
||||
};
|
||||
|
||||
time.timeZone = "UTC";
|
||||
|
||||
system.autoUpgrade = {
|
||||
enable = true;
|
||||
allowReboot = true;
|
||||
};
|
||||
|
||||
system.stateVersion = "22.11";
|
||||
|
||||
nix = {
|
||||
optimise.automatic = true;
|
||||
gc = {
|
||||
automatic = true;
|
||||
options = "--delete-older-than 7d";
|
||||
};
|
||||
};
|
||||
}
|
16
ldap/go.mod
Normal file
16
ldap/go.mod
Normal file
@ -0,0 +1,16 @@
|
||||
module code.dumpstack.io/infra/v.lor.sh/ldap
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.2.0
|
||||
github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3
|
||||
github.com/vjeantet/ldapserver v1.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
|
||||
golang.org/x/text v0.3.8 // indirect
|
||||
)
|
25
ldap/go.sum
Normal file
25
ldap/go.sum
Normal file
@ -0,0 +1,25 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
|
||||
github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8=
|
||||
github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk=
|
||||
github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3 h1:wIONC+HMNRqmWBjuMxhatuSzHaljStc4gjDeKycxy0A=
|
||||
github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3/go.mod h1:37YR9jabpiIxsb8X9VCIx8qFOjTDIIrIHHODa8C4gz0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/vjeantet/ldapserver v1.0.1 h1:3z+TCXhwwDLJC3pZCNbuECPDqC2x1R7qQQbswB1Qwoc=
|
||||
github.com/vjeantet/ldapserver v1.0.1/go.mod h1:YvUqhu5vYhmbcLReMLrm/Tq3S7Yj43kSVFvvol6Lh6k=
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
169
ldap/ldap.go
Normal file
169
ldap/ldap.go
Normal file
@ -0,0 +1,169 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/lor00x/goldap/message"
|
||||
ldap "github.com/vjeantet/ldapserver"
|
||||
)
|
||||
|
||||
func auth(username, email, password string) (ruser, remail string, err error) {
|
||||
var payload struct {
|
||||
Secret string
|
||||
Username string
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
payload.Secret = os.Getenv("AUTH_SECRET")
|
||||
payload.Username = username
|
||||
payload.Email = email
|
||||
payload.Password = password
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
body := bytes.NewReader(raw)
|
||||
|
||||
req, err := http.NewRequest("POST", os.Getenv("AUTH_URL"), body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result struct {
|
||||
Username string
|
||||
Email string
|
||||
Error string
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
err = errors.New(result.Error)
|
||||
}
|
||||
|
||||
ruser = result.Username
|
||||
remail = result.Email
|
||||
return
|
||||
}
|
||||
|
||||
func bind(w ldap.ResponseWriter, m *ldap.Message) {
|
||||
r := m.GetBindRequest()
|
||||
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
|
||||
|
||||
username := string(r.Name())
|
||||
password := string(r.AuthenticationSimple())
|
||||
|
||||
if username == os.Getenv("LDAP_USER") {
|
||||
if password == os.Getenv("LDAP_PASS") {
|
||||
w.Write(res)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if username == "root" {
|
||||
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
||||
res.SetDiagnosticMessage("root login is disabled")
|
||||
w.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
_, _, err := auth(username, "", password)
|
||||
if err == nil {
|
||||
fmt.Println("bind:", username, "ok")
|
||||
w.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("bind:", username, "incorrect password")
|
||||
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
||||
res.SetDiagnosticMessage("invalid credentials")
|
||||
w.Write(res)
|
||||
}
|
||||
|
||||
func updateEmail(username, email string) (err error) {
|
||||
path := "dbname=" + os.Getenv("DATABASE")
|
||||
conn, err := pgx.Connect(context.Background(), path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close(context.Background())
|
||||
|
||||
query := "update \"user\" set email=$1 where username=$2"
|
||||
_, err = conn.Exec(context.Background(), query, email, username)
|
||||
return
|
||||
}
|
||||
|
||||
func search(w ldap.ResponseWriter, m *ldap.Message) {
|
||||
r := m.GetSearchRequest()
|
||||
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
||||
defer w.Write(res)
|
||||
|
||||
var username, email string
|
||||
|
||||
s := r.FilterString()
|
||||
s = s[6 : len(s)-1]
|
||||
|
||||
if strings.Contains(s, "@") {
|
||||
username, email, _ = auth("", s, "")
|
||||
} else {
|
||||
username, email, _ = auth(s, "", "")
|
||||
}
|
||||
|
||||
if username == "" || email == "" {
|
||||
fmt.Println("search:", s, "not found")
|
||||
return
|
||||
}
|
||||
fmt.Println("search:", s, "found", username, email)
|
||||
|
||||
err := updateEmail(username, email)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
e := ldap.NewSearchResultEntry(strings.ToLower(username))
|
||||
e.AddAttribute("uid", message.AttributeValue(strings.ToLower(username)))
|
||||
e.AddAttribute("mail", message.AttributeValue(strings.ToLower(email)))
|
||||
w.Write(e)
|
||||
}
|
||||
|
||||
func main() {
|
||||
ldap.Logger = ldap.DiscardingLogger
|
||||
|
||||
server := ldap.NewServer()
|
||||
|
||||
routes := ldap.NewRouteMux()
|
||||
routes.Bind(bind)
|
||||
routes.Search(search)
|
||||
server.Handle(routes)
|
||||
|
||||
go server.ListenAndServe(":10389")
|
||||
|
||||
ch := make(chan os.Signal)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-ch
|
||||
close(ch)
|
||||
|
||||
server.Stop()
|
||||
}
|
44
secrets.nix.example
Normal file
44
secrets.nix.example
Normal file
@ -0,0 +1,44 @@
|
||||
{
|
||||
pubkeys = [
|
||||
""
|
||||
];
|
||||
|
||||
peertube = {
|
||||
secret = "";
|
||||
db = {
|
||||
password = "";
|
||||
};
|
||||
auth = {
|
||||
url = "";
|
||||
secret = "";
|
||||
};
|
||||
ldap = {
|
||||
user = "";
|
||||
password = "";
|
||||
};
|
||||
admin = {
|
||||
email = "";
|
||||
};
|
||||
s3 = {
|
||||
id = "";
|
||||
key = "";
|
||||
};
|
||||
};
|
||||
|
||||
ipv4 = {
|
||||
addresses = [
|
||||
{ address = ""; prefixLength = 27; }
|
||||
];
|
||||
routes = [
|
||||
{ address = "0.0.0.0"; prefixLength = 0; via = ""; }
|
||||
];
|
||||
};
|
||||
ipv6 = {
|
||||
addresses = [
|
||||
{ address = ""; prefixLength = 64; }
|
||||
];
|
||||
routes = [
|
||||
{ address = "::"; prefixLength = 0; via = "fe80::1"; }
|
||||
];
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user