turn: allow for providing secret via file (#4564)
* turn: allow for providing secret via file * turn: improve secret_file changes
This commit is contained in:
@@ -259,6 +259,10 @@ func startServer(ctx context.Context, c *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = conf.LoadTURNSecrets(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if cpuProfile := c.String("cpuprofile"); cpuProfile != "" {
|
if cpuProfile := c.String("cpuprofile"); cpuProfile != "" {
|
||||||
if f, err := os.Create(cpuProfile); err != nil {
|
if f, err := os.Create(cpuProfile); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -98,6 +98,9 @@ rtc:
|
|||||||
# protocol: tls
|
# protocol: tls
|
||||||
# # Shared secret for TURN server authentication
|
# # Shared secret for TURN server authentication
|
||||||
# secret: ""
|
# 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
|
# ttl: 14400 # seconds
|
||||||
# # Insecure username/password authentication
|
# # Insecure username/password authentication
|
||||||
# username: ""
|
# username: ""
|
||||||
|
|||||||
+32
-2
@@ -49,8 +49,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrKeyFileIncorrectPermission = errors.New("key file others permissions must be set to 0")
|
ErrKeyFileIncorrectPermission = errors.New("key file others permissions must be set to 0")
|
||||||
ErrKeysNotSet = errors.New("one of key-file or keys must be provided")
|
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 {
|
type Config struct {
|
||||||
@@ -149,6 +150,8 @@ type TURNServer struct {
|
|||||||
// Secret is used for TURN static auth secrets mechanism. When provided,
|
// Secret is used for TURN static auth secrets mechanism. When provided,
|
||||||
// dynamic credentials are generated using HMAC-SHA1 instead of static Username/Credential
|
// dynamic credentials are generated using HMAC-SHA1 instead of static Username/Credential
|
||||||
Secret string `yaml:"secret,omitempty"`
|
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.
|
// TTL is the time-to-live in seconds for generated credentials when using Secret.
|
||||||
// Defaults to 14400 seconds (4 hours) if not specified
|
// Defaults to 14400 seconds (4 hours) if not specified
|
||||||
TTL int `yaml:"ttl,omitempty"`
|
TTL int `yaml:"ttl,omitempty"`
|
||||||
@@ -642,6 +645,33 @@ func (conf *Config) ValidateKeys() error {
|
|||||||
return nil
|
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) {
|
func GenerateCLIFlags(existingFlags []cli.Flag, hidden bool) ([]cli.Flag, error) {
|
||||||
defaultConfig := &DefaultConfig
|
defaultConfig := &DefaultConfig
|
||||||
flags := make([]cli.Flag, 0)
|
flags := make([]cli.Flag, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user