Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(rexctl): generate instance secrets and vapid keys

Hampus379d3795a64c
diff --git a/fluxer_rexctl/internal/secrets/secrets.go b/fluxer_rexctl/internal/secrets/secrets.go
new file mode 100644
index 000000000..99f744805
--- /dev/null
+++ b/fluxer_rexctl/internal/secrets/secrets.go
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package secrets
+
+import (
+ "crypto/rand"
+ "encoding/base64"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "strings"
+)
+
+type Kind int
+
+const (
+ KindHex32 Kind = iota
+ KindAlnum64
+ KindBase64_32
+ KindVAPID
+ KindFixed
+)
+
+const Unset = "CHANGE_ME"
+
+type Spec struct {
+ Key string
+ Kind Kind
+ Fixed string
+ Stateful bool
+}
+
+var Required = []Spec{
+ {Key: "FLUXER_S3_ACCESS_KEY", Kind: KindFixed, Fixed: "fluxer"},
+ {Key: "LIVEKIT_API_KEY", Kind: KindFixed, Fixed: "fluxer"},
+ {Key: "POSTGRES_PASSWORD", Kind: KindHex32, Stateful: true},
+ {Key: "MEILI_MASTER_KEY", Kind: KindHex32, Stateful: true},
+ {Key: "FLUXER_S3_SECRET_KEY", Kind: KindHex32, Stateful: true},
+ {Key: "FLUXER_SUDO_MODE_SECRET", Kind: KindHex32},
+ {Key: "FLUXER_CONNECTION_INITIATION_SECRET", Kind: KindHex32},
+ {Key: "FLUXER_PROFILE_PSEUDONYM_SECRET", Kind: KindHex32},
+ {Key: "FLUXER_GATEWAY_RPC_AUTH_TOKEN", Kind: KindHex32},
+ {Key: "FLUXER_ERLANG_COOKIE", Kind: KindHex32},
+ {Key: "FLUXER_MEDIA_PROXY_SECRET_KEY", Kind: KindHex32},
+ {Key: "FLUXER_ADMIN_SECRET_KEY_BASE", Kind: KindHex32},
+ {Key: "FLUXER_ADMIN_OAUTH_CLIENT_SECRET", Kind: KindHex32},
+ {Key: "LIVEKIT_API_SECRET", Kind: KindHex32, Stateful: true},
+ {Key: "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SECRET_BASE64", Kind: KindBase64_32},
+ {Key: VAPIDPublicKey, Kind: KindVAPID, Stateful: true},
+ {Key: VAPIDPrivateKey, Kind: KindVAPID, Stateful: true},
+}
+
+var ErrStatefulMissing = errors.New("secrets: a key that guards stored data is missing")
+
+type StatefulMissingError struct {
+ Keys []string
+}
+
+func (e *StatefulMissingError) Error() string {
+ return fmt.Sprintf("this instance has no value for %s and its existing data needs the old value", strings.Join(e.Keys, ", "))
+}
+
+func (e *StatefulMissingError) Unwrap() error { return ErrStatefulMissing }
+
+func Hex(nBytes int) (string, error) {
+ b := make([]byte, nBytes)
+ if _, err := rand.Read(b); err != nil {
+ return "", err
+ }
+ return hex.EncodeToString(b), nil
+}
+
+const alnum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+
+func Alnum(n int) (string, error) {
+ out := make([]byte, 0, n)
+ buf := make([]byte, n+n/2+8)
+ for len(out) < n {
+ if _, err := rand.Read(buf); err != nil {
+ return "", err
+ }
+ for _, c := range buf {
+ if c >= 248 {
+ continue
+ }
+ out = append(out, alnum[int(c)%len(alnum)])
+ if len(out) == n {
+ break
+ }
+ }
+ }
+ return string(out), nil
+}
+
+func Base64(nBytes int) (string, error) {
+ b := make([]byte, nBytes)
+ if _, err := rand.Read(b); err != nil {
+ return "", err
+ }
+ return base64.StdEncoding.EncodeToString(b), nil
+}
+
+func IsUnset(value string) bool {
+ v := strings.TrimSpace(value)
+ return v == "" || v == Unset
+}
+
+func Generate(s Spec) (string, error) {
+ switch s.Kind {
+ case KindHex32:
+ return Hex(32)
+ case KindAlnum64:
+ return Alnum(64)
+ case KindBase64_32:
+ return Base64(32)
+ case KindFixed:
+ return s.Fixed, nil
+ }
+ return "", fmt.Errorf("secrets: %s has no single-value generator", s.Key)
+}
+
+func Mint(values map[string]string, haveVolumes bool) ([]string, error) {
+ written := []string{}
+ var blocked []string
+ vapidMissing := false
+ for _, s := range Required {
+ if !IsUnset(values[s.Key]) {
+ continue
+ }
+ if s.Stateful && haveVolumes {
+ blocked = append(blocked, s.Key)
+ continue
+ }
+ if s.Kind == KindVAPID {
+ vapidMissing = true
+ continue
+ }
+ v, err := Generate(s)
+ if err != nil {
+ return written, err
+ }
+ values[s.Key] = v
+ written = append(written, s.Key)
+ }
+ if vapidMissing {
+ pair, err := GenerateVAPID()
+ if err != nil {
+ return written, err
+ }
+ values[VAPIDPublicKey] = pair.Public
+ values[VAPIDPrivateKey] = pair.Private
+ written = append(written, VAPIDPublicKey, VAPIDPrivateKey)
+ }
+ if len(blocked) > 0 {
+ return written, &StatefulMissingError{Keys: blocked}
+ }
+ return written, nil
+}
diff --git a/fluxer_rexctl/internal/secrets/vapid.go b/fluxer_rexctl/internal/secrets/vapid.go
new file mode 100644
index 000000000..e7e809465
--- /dev/null
+++ b/fluxer_rexctl/internal/secrets/vapid.go
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package secrets
+
+import (
+ "bytes"
+ "crypto/ecdh"
+ "crypto/rand"
+ "encoding/base64"
+ "errors"
+ "strings"
+)
+
+const (
+ VAPIDPublicKey = "FLUXER_VAPID_PUBLIC_KEY"
+ VAPIDPrivateKey = "FLUXER_VAPID_PRIVATE_KEY"
+)
+
+var (
+ ErrVAPIDMismatch = errors.New("vapid: public key does not match private key")
+ ErrVAPIDMissing = errors.New("vapid: no key pair")
+ ErrVAPIDInvalid = errors.New("vapid: key has the wrong length or encoding")
+)
+
+type VAPID struct {
+ Public string
+ Private string
+}
+
+func GenerateVAPID() (VAPID, error) {
+ key, err := ecdh.P256().GenerateKey(rand.Reader)
+ if err != nil {
+ return VAPID{}, err
+ }
+ return VAPID{
+ Public: base64.RawURLEncoding.EncodeToString(key.PublicKey().Bytes()),
+ Private: base64.RawURLEncoding.EncodeToString(key.Bytes()),
+ }, nil
+}
+
+func ValidateVAPID(public, private string) error {
+ public, private = NormalizeBase64URL(public), NormalizeBase64URL(private)
+ if public == "" && private == "" {
+ return ErrVAPIDMissing
+ }
+ pub, err := base64.RawURLEncoding.DecodeString(public)
+ if err != nil || len(pub) != 65 || pub[0] != 4 {
+ return ErrVAPIDInvalid
+ }
+ if _, err := ecdh.P256().NewPublicKey(pub); err != nil {
+ return ErrVAPIDInvalid
+ }
+ priv, err := base64.RawURLEncoding.DecodeString(private)
+ if err != nil || len(priv) != 32 {
+ return ErrVAPIDInvalid
+ }
+ key, err := ecdh.P256().NewPrivateKey(priv)
+ if err != nil {
+ return ErrVAPIDInvalid
+ }
+ if !bytes.Equal(key.PublicKey().Bytes(), pub) {
+ return ErrVAPIDMismatch
+ }
+ return nil
+}
+
+func NormalizeBase64URL(s string) string {
+ s = strings.TrimSpace(s)
+ s = strings.NewReplacer("+", "-", "/", "_", "=", "").Replace(s)
+ return s
+}