diff --git a/cmd/server/main.go b/cmd/server/main.go index ec2c0bf7..dde5b0ad 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -259,6 +259,10 @@ func startServer(ctx context.Context, c *cli.Command) error { return err } + if err = conf.LoadTURNSecrets(); err != nil { + return err + } + if cpuProfile := c.String("cpuprofile"); cpuProfile != "" { if f, err := os.Create(cpuProfile); err != nil { return err diff --git a/config-sample.yaml b/config-sample.yaml index 0559d771..7a1eece3 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -98,6 +98,9 @@ rtc: # protocol: tls # # Shared secret for TURN server authentication # secret: "" + # # Path for file containing shared secret for TURN server authentication + # # When both secret and secret_file are set, secret takes precedence + # secret_file: "/var/lib/coturn/secret" # ttl: 14400 # seconds # # Insecure username/password authentication # username: "" diff --git a/pkg/config/config.go b/pkg/config/config.go index f155eb59..6d65973f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -49,8 +49,9 @@ const ( ) var ( - ErrKeyFileIncorrectPermission = errors.New("key file others permissions must be set to 0") - ErrKeysNotSet = errors.New("one of key-file or keys must be provided") + ErrKeyFileIncorrectPermission = errors.New("key file others permissions must be set to 0") + ErrTURNSecretFileIncorrectPermission = errors.New("turn secret file others permissions must be set to 0") + ErrKeysNotSet = errors.New("one of key-file or keys must be provided") ) type Config struct { @@ -149,6 +150,8 @@ type TURNServer struct { // Secret is used for TURN static auth secrets mechanism. When provided, // dynamic credentials are generated using HMAC-SHA1 instead of static Username/Credential Secret string `yaml:"secret,omitempty"` + // File containing the secret + SecretFile string `yaml:"secret_file,omitempty"` // TTL is the time-to-live in seconds for generated credentials when using Secret. // Defaults to 14400 seconds (4 hours) if not specified TTL int `yaml:"ttl,omitempty"` @@ -642,6 +645,33 @@ func (conf *Config) ValidateKeys() error { return nil } +func (conf *Config) LoadTURNSecrets() error { + var otherFilter os.FileMode = 0o007 + for i, s := range conf.RTC.TURNServers { + if s.SecretFile == "" { + continue + } + if s.Secret != "" { + logger.Warnw("both secret and secret_file are set for TURN server, the hardcoded secret will be used", nil, + "host", s.Host, "port", s.Port) + continue + } + st, err := os.Stat(s.SecretFile) + if err != nil { + return err + } + if st.Mode().Perm()&otherFilter != 0o000 { + return ErrTURNSecretFileIncorrectPermission + } + data, err := os.ReadFile(s.SecretFile) + if err != nil { + return fmt.Errorf("reading turn secret file %q: %w", s.SecretFile, err) + } + conf.RTC.TURNServers[i].Secret = strings.TrimSpace(string(data)) + } + return nil +} + func GenerateCLIFlags(existingFlags []cli.Flag, hidden bool) ([]cli.Flag, error) { defaultConfig := &DefaultConfig flags := make([]cli.Flag, 0)