schema.userdb.redis 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. I. The database
  2. Redis database for user authentication and peer permissions
  3. has the following schema:
  4. 1) For the long-term credentials there must be keys
  5. "turn/realm/<realm-name>/user/<username>/key" and the values must be
  6. the the hmackeys. For example, for the user "gorst", realm "north.gov"
  7. and password "hero", there must be key "turn/realm/north.gov/user/gorst/key"
  8. with value "7da2270ccfa49786e0115366d3a3d14d".
  9. 2) For the shared secrets (REST API), several key/value pairs
  10. may be used (same as in SQL schema). The secrets are stored as members
  11. of an unordered set. The name of the set will be
  12. "turn/realm/<realm-name>/secret" and the value(s) will be the secret(s).
  13. For example, if we have secrets "hero1", "hero2" and "hero3",
  14. then we will have set "turn/realm/north.gov/secret" with values
  15. "hero1", "hero2" and "hero3". The turnserver will try to use the
  16. secrets in arbitrary order.
  17. 3) The "white" and "black" peer IP ranges are stored as unordered sets
  18. of the following names:
  19. "turn/realm/<realm>/allowed-peer-ip" and
  20. "turn/realm/<realm>/denied-peer-ip".
  21. The meaning of the keys is the same as the meaning of allowed-peer-ip and
  22. denied-peer-ip turnserver command-line option (with the addition of the realm option).
  23. The only difference is that the turnserver option values are "static"
  24. (they remain the same for the lifetime of the turnserver process) but
  25. the database records can be dynamically changed
  26. and they will be almost immediately "seen" by the turnserver process.
  27. 4) For the oAuth authentication, there is a hash structure with the key
  28. "turn/oauth/kid/<kid-value>". The kid structure fields are:
  29. ikm_key - (optional) base64-encoded key ("input keying material").
  30. timestamp - (optional) the timestamp (in seconds) when the key
  31. lifetime started.
  32. lifetime - (optional) the key lifetime in seconds; the default value
  33. is 0 - unlimited lifetime.
  34. as_rs_alg - oAuth token encryption algorithm; the valid values are
  35. "A256GCM", "A128GCM" (see
  36. http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-5.1).
  37. The default value is "A256GCM".
  38. realm - optionally, a kid can be assigned to a realm that is different
  39. from the default realm.
  40. 5) admin users (over https interface) are maintained as keys of form:
  41. "turn/admin_user/<username> with hash members "password" and,
  42. optionally, "realm".
  43. II. Extra realms data in the database
  44. We can use more than one realm with the same instance of the TURN server.
  45. This is done in two ways:
  46. 1) through the third-party authentication option. An oAuth kid can be optionally
  47. assigned to a realm. When the user provides kid, and the database record
  48. for that kid contains a non-empty non-default realm, then the user is treated
  49. as belonging to that realm.
  50. 2) the ORIGIN mechanism - users with different ORIGINS
  51. are placed into different realms. The database includes information about the
  52. relationships between the ORIGIN and realms, and about the extra realms
  53. database numbers.
  54. The relationship between ORIGIN and realm is set as keys of form:
  55. "turn/origin/<origin>" with the realm-names as the value. Many different
  56. ORIGIN keys may have the same realm. If the ORIGIN value is not found in
  57. the database or the ORIGIN field is missed in the initial allocate
  58. request, then the default realm is assumed.
  59. III) Example of a Redis default user database setup.
  60. This example sets user database for:
  61. * long-term credentials with hashed passwords and
  62. with default realm "north.gov";
  63. * long-term credentials with open passwords and
  64. with default realm "north.gov";
  65. * TURN REST API with shared secrets "logen", etc;
  66. * Black and white IP peer lists used.
  67. * Information how to match ORIGIN field with extra
  68. realms (if used). If no origin match found
  69. or the ORIGIN field is absent in the ALLOCATE request then the default
  70. realm is used.
  71. * The realm performance parameters: "max_bps",
  72. "total_quota" and "user_quota" (same names as the turnserver
  73. configuration options, with the same meanings).
  74. * The oAuth data for the key with kid "oldempire" and key value
  75. "12345678901234567890123456789012", and default realm.
  76. * The admin user 'skarling', realm 'north.gov', with password 'hoodless';
  77. * The global admin user 'bayaz' with password 'magi';
  78. The shell command would be:
  79. $ redis-cli <<!
  80. SELECT 2
  81. AUTH turn
  82. set turn/realm/north.gov/user/ninefingers/key "bc807ee29df3c9ffa736523fb2c4e8ee"
  83. set turn/realm/north.gov/user/gorst/key "7da2270ccfa49786e0115366d3a3d14d"
  84. set turn/realm/crinna.org/user/whirrun/key "6972e85e51f36e53b0b61759c5a5219a"
  85. set turn/realm/crinna.org/user/stranger-come-knocking/key "d43cb678560259a1839bff61c19de15e"
  86. sadd turn/realm/north.gov/secret "logen" "bloody9"
  87. sadd turn/realm/crinna.org/secret "north" "library"
  88. set turn/realm/north.gov/max-bps 500000
  89. set turn/realm/north.gov/total-quota 12000
  90. set turn/realm/north.gov/user-quota 10000
  91. set turn/realm/crinna.org/max-bps 400000
  92. set turn/realm/crinna.org/total-quota 10000
  93. set turn/realm/crinna.org/user-quota 8000
  94. set turn/origin/http://crinna.org:80 crinna.org
  95. set turn/origin/https://bligh.edu:443 crinna.org
  96. sadd turn/realm/north.gov/allowed-peer-ip "172.17.13.200" "172.17.13.201"
  97. sadd turn/realm/crinna.org/allowed-peer-ip "172.17.13.202"
  98. sadd turn/realm/north.gov/denied-peer-ip "172.17.13.133-172.17.14.56" "172.17.17.133-172.17.19.56" "123::45"
  99. sadd turn/realm/crinna.org/denied-peer-ip "123::77"
  100. hmset turn/oauth/kid/oldempire ikm_key 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIK' as_rs_alg 'A256GCM'
  101. hmset turn/admin_user/skarling realm 'north.gov' password 'hoodless'
  102. hmset turn/admin_user/bayaz password 'magi'
  103. save
  104. !
  105. IV. Redis database configuration parameters
  106. TURN Server connects to the Redis and keeps the same connection during the
  107. TURN Server process lifetime. That means that we have to take care about that
  108. connection - it must not expire.
  109. You have to take care about Redis connection parameters, the timeout and the
  110. keepalive. The following settings must be in your Redis config file
  111. (/etc/redis.conf or /usr/local/etc/redis.conf):
  112. ..........
  113. timeout 0
  114. ..........
  115. tcp-keepalive 60
  116. ..........