Skip to content

Commit eee9ca2

Browse files
Add LIVEKIT_KEY_FILE, rename env vars to _FROM_FILE and trim keys&secrets (#93)
* Add LIVEKIT_KEY_SECRET_FILE and trim keys&secrets * Rename env vars for consistency with livekit * Add LIVEKIT_KEY_FILE variable in README * comment readKeySecret logic * reformat var names
1 parent e10a9fd commit eee9ca2

File tree

6 files changed

+55
-22
lines changed

6 files changed

+55
-22
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ The service is configured via environment variables:
5252
Variable | Description | Required
5353
--- | --- | ---
5454
`LIVEKIT_URL` | The websocket URL of the LiveKit SFU | Yes
55-
`LIVEKIT_KEY` or `LIVEKIT_KEY_FILE` | The API key or key file path for the LiveKit SFU | Yes
56-
`LIVEKIT_SECRET` or `LIVEKIT_SECRET_FILE` | The secret or secret file path for the LiveKit SFU | Yes
55+
`LIVEKIT_KEY` or `LIVEKIT_KEY_FROM_FILE` | The API key or key file path for the LiveKit SFU | Yes
56+
`LIVEKIT_SECRET` or `LIVEKIT_SECRET_FROM_FILE` | The secret or secret file path for the LiveKit SFU | Yes
57+
`LIVEKIT_KEY_FILE` | file path to LiveKit SFU key-file format (`APIkey: secret`) | mutually exclusive with `LIVEKIT_KEY` and `LIVEKIT_SECRET`
5758
`LIVEKIT_JWT_PORT` | The port the service listens on | No - defaults to 8080
5859

5960
## Disable TLS verification

main.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"os"
2626
"crypto/tls"
27+
"strings"
2728

2829
"time"
2930

@@ -186,27 +187,49 @@ func (h *Handler) prepareMux() *http.ServeMux {
186187
}
187188

188189
func readKeySecret() (string, string) {
190+
// We initialize keys & secrets from environment variables
189191
key := os.Getenv("LIVEKIT_KEY")
190192
secret := os.Getenv("LIVEKIT_SECRET")
191-
key_path := os.Getenv("LIVEKIT_KEY_FILE")
192-
secret_path := os.Getenv("LIVEKIT_SECRET_FILE")
193-
if key_path != "" {
194-
if keyBytes, err := os.ReadFile(key_path); err != nil {
193+
// We initialize potential key & secret path from environment variables
194+
keyPath := os.Getenv("LIVEKIT_KEY_FROM_FILE")
195+
secretPath := os.Getenv("LIVEKIT_SECRET_FROM_FILE")
196+
keySecretPath := os.Getenv("LIVEKIT_KEY_FILE")
197+
198+
// If keySecretPath is set we read the file and split it into two parts
199+
// It takes over any other initialization
200+
if keySecretPath != "" {
201+
if keySecretBytes, err := os.ReadFile(keySecretPath); err != nil {
195202
log.Fatal(err)
196203
} else {
197-
key = string(keyBytes)
204+
key_secrets := strings.Split(string(keySecretBytes), ":")
205+
if len(key_secrets) != 2 {
206+
log.Fatalf("invalid key secret file format!")
207+
}
208+
key = key_secrets[0]
209+
secret = key_secrets[1]
210+
}
211+
} else {
212+
// If keySecretPath is not set, we try to read the key and secret from files
213+
// If those files are not set, we return the key & secret from the environment variables
214+
if keyPath != "" {
215+
if keyBytes, err := os.ReadFile(keyPath); err != nil {
216+
log.Fatal(err)
217+
} else {
218+
key = string(keyBytes)
219+
}
198220
}
199-
}
200221

201-
if secret_path != "" {
202-
if secretBytes, err := os.ReadFile(secret_path); err != nil {
203-
log.Fatal(err)
204-
} else {
205-
secret = string(secretBytes)
222+
if secretPath != "" {
223+
if secretBytes, err := os.ReadFile(secretPath); err != nil {
224+
log.Fatal(err)
225+
} else {
226+
secret = string(secretBytes)
227+
}
206228
}
229+
207230
}
208231

209-
return key, secret
232+
return strings.Trim(key, " \r\n"), strings.Trim(secret, " \r\n")
210233
}
211234

212235
func main() {

main_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,29 +240,37 @@ func TestReadKeySecret(t *testing.T) {
240240
expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
241241
err: false,
242242
},
243+
{
244+
name: "Read from livekit keysecret",
245+
env: map[string]string{
246+
"LIVEKIT_KEY_FILE": "./tests/keysecret.yaml",
247+
},
248+
expectedKey: "keysecret_iethuB2LeLiNuishiaKeephei9jaatio",
249+
expectedSecret: "keysecret_xefaingo4oos6ohla9phiMieBu3ohJi2",
250+
},
243251
{
244252
name: "Read from file",
245253
env: map[string]string{
246-
"LIVEKIT_KEY_FILE": "./tests/key",
247-
"LIVEKIT_SECRET_FILE": "./tests/secret",
254+
"LIVEKIT_KEY_FROM_FILE": "./tests/key",
255+
"LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
248256
},
249257
expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
250258
expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",
251259
},
252260
{
253261
name: "Read from file key only",
254262
env: map[string]string{
255-
"LIVEKIT_KEY_FILE": "./tests/key",
256-
"LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
263+
"LIVEKIT_KEY_FROM_FILE": "./tests/key",
264+
"LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
257265
},
258266
expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
259267
expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
260268
},
261269
{
262270
name: "Read from file secret only",
263271
env: map[string]string{
264-
"LIVEKIT_SECRET_FILE": "./tests/secret",
265-
"LIVEKIT_KEY": "from_env_qui8aiTopiekiechah9oocbeimeew2O",
272+
"LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
273+
"LIVEKIT_KEY": "from_env_qui8aiTopiekiechah9oocbeimeew2O",
266274
},
267275
expectedKey: "from_env_qui8aiTopiekiechah9oocbeimeew2O",
268276
expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",

tests/key

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from_file_oquusheiheiw4Iegah8te3Vienguus5a
1+
from_file_oquusheiheiw4Iegah8te3Vienguus5a

tests/keysecret.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
keysecret_iethuB2LeLiNuishiaKeephei9jaatio: keysecret_xefaingo4oos6ohla9phiMieBu3ohJi2

tests/secret

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from_file_vohmahH3eeyieghohSh3kee8feuPhaim
1+
from_file_vohmahH3eeyieghohSh3kee8feuPhaim

0 commit comments

Comments
 (0)