emqx_auth_pgsql.schema 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. %%-*- mode: erlang -*-
  2. %% emqx_auth_pgsl config mapping
  3. {mapping, "auth.pgsql.server", "emqx_auth_pgsql.server", [
  4. {default, {"127.0.0.1", 5432}},
  5. {datatype, [integer, ip, string]}
  6. ]}.
  7. {mapping, "auth.pgsql.pool", "emqx_auth_pgsql.server", [
  8. {default, 8},
  9. {datatype, integer}
  10. ]}.
  11. {mapping, "auth.pgsql.database", "emqx_auth_pgsql.server", [
  12. {datatype, string}
  13. ]}.
  14. {mapping, "auth.pgsql.username", "emqx_auth_pgsql.server", [
  15. {default, ""},
  16. {datatype, string}
  17. ]}.
  18. {mapping, "auth.pgsql.password", "emqx_auth_pgsql.server", [
  19. {default, ""},
  20. {datatype, string}
  21. ]}.
  22. {mapping, "auth.pgsql.encoding", "emqx_auth_pgsql.server", [
  23. {default, utf8},
  24. {datatype, atom}
  25. ]}.
  26. {mapping, "auth.pgsql.ssl.enable", "emqx_auth_pgsql.server", [
  27. {default, off},
  28. {datatype, {enum, [on, off, true, false]}} %% FIXME: true/fasle is compatible with 4.0-4.2 version format, plan to delete in 5.0
  29. ]}.
  30. {mapping, "auth.pgsql.ssl.tls_versions", "emqx_auth_pgsql.server", [
  31. {default, "tlsv1.3,tlsv1.2,tlsv1.1"},
  32. {datatype, string}
  33. ]}.
  34. {mapping, "auth.pgsql.ssl.keyfile", "emqx_auth_pgsql.server", [
  35. {datatype, string}
  36. ]}.
  37. {mapping, "auth.pgsql.ssl.certfile", "emqx_auth_pgsql.server", [
  38. {datatype, string}
  39. ]}.
  40. {mapping, "auth.pgsql.ssl.cacertfile", "emqx_auth_pgsql.server", [
  41. {datatype, string}
  42. ]}.
  43. {mapping, "auth.pgsql.ssl.verify", "emqx_auth_pgsql.server", [
  44. {default, false},
  45. {datatype, {enum, [true, false]}}
  46. ]}.
  47. {mapping, "auth.pgsql.ssl.server_name_indication", "emqx_auth_pgsql.server", [
  48. {datatype, string}
  49. ]}.
  50. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  51. {mapping, "auth.pgsql.ssl_opts.keyfile", "emqx_auth_pgsql.server", [
  52. {datatype, string}
  53. ]}.
  54. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  55. {mapping, "auth.pgsql.ssl_opts.certfile", "emqx_auth_pgsql.server", [
  56. {datatype, string}
  57. ]}.
  58. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  59. {mapping, "auth.pgsql.ssl_opts.cacertfile", "emqx_auth_pgsql.server", [
  60. {datatype, string}
  61. ]}.
  62. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  63. {mapping, "auth.pgsql.ssl_opts.tls_versions", "emqx_auth_pgsql.server", [
  64. {default, "tlsv1.2"},
  65. {datatype, string}
  66. ]}.
  67. {translation, "emqx_auth_pgsql.server", fun(Conf) ->
  68. {PgHost, PgPort} =
  69. case cuttlefish:conf_get("auth.pgsql.server", Conf) of
  70. {Ip, Port} -> {Ip, Port};
  71. S -> case string:tokens(S, ":") of
  72. [Domain] -> {Domain, 5432};
  73. [Domain, Port] -> {Domain, list_to_integer(Port)}
  74. end
  75. end,
  76. Pool = cuttlefish:conf_get("auth.pgsql.pool", Conf),
  77. Username = cuttlefish:conf_get("auth.pgsql.username", Conf),
  78. Passwd = cuttlefish:conf_get("auth.pgsql.password", Conf, ""),
  79. DB = cuttlefish:conf_get("auth.pgsql.database", Conf),
  80. Encoding = cuttlefish:conf_get("auth.pgsql.encoding", Conf),
  81. Filter = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,
  82. SslOpts = fun(Prefix) ->
  83. Verify = case cuttlefish:conf_get(Prefix ++ ".verify", Conf, false) of
  84. true -> verify_peer;
  85. false -> verify_none
  86. end,
  87. Filter([{keyfile, cuttlefish:conf_get(Prefix ++ ".keyfile", Conf, undefined)},
  88. {certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)},
  89. {cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)},
  90. {verify, Verify},
  91. {server_name_indication, case cuttlefish:conf_get(Prefix ++ ".server_name_indication", Conf, undefined) of
  92. "disable" -> disable;
  93. SNI -> SNI
  94. end},
  95. {versions, [list_to_existing_atom(Value)
  96. || Value <- string:tokens(cuttlefish:conf_get(Prefix ++ ".tls_versions", Conf), " ,")]}])
  97. end,
  98. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  99. GenSsl = case cuttlefish:conf_get("auth.pgsql.ssl.cacertfile", Conf, undefined) of
  100. undefined -> [{ssl, true}, {ssl_opts, SslOpts("auth.pgsql.ssl_opts")}];
  101. _ -> [{ssl, true}, {ssl_opts, SslOpts("auth.pgsql.ssl")}]
  102. end,
  103. %% FIXME: compatible with 4.0-4.2 version format, plan to delete in 5.0
  104. Ssl = case cuttlefish:conf_get("auth.pgsql.ssl.enable", Conf) of
  105. on -> GenSsl;
  106. off -> [];
  107. true -> [{ssl, true}, {ssl_opts, SslOpts("auth.pgsql.ssl_opts")}];
  108. false -> []
  109. end,
  110. TempHost = case inet:parse_address(PgHost) of
  111. {ok, IpAddr} ->
  112. IpAddr;
  113. _ ->
  114. PgHost
  115. end,
  116. [{pool_size, Pool},
  117. {auto_reconnect, 1},
  118. {host, TempHost},
  119. {port, PgPort},
  120. {username, Username},
  121. {password, Passwd},
  122. {database, DB},
  123. {encoding, Encoding}] ++ Ssl
  124. end}.
  125. {mapping, "auth.pgsql.auth_query", "emqx_auth_pgsql.auth_query", [
  126. {datatype, string}
  127. ]}.
  128. {mapping, "auth.pgsql.password_hash", "emqx_auth_pgsql.password_hash", [
  129. {datatype, string}
  130. ]}.
  131. {mapping, "auth.pgsql.pbkdf2_macfun", "emqx_auth_pgsql.pbkdf2_macfun", [
  132. {datatype, atom}
  133. ]}.
  134. {mapping, "auth.pgsql.pbkdf2_iterations", "emqx_auth_pgsql.pbkdf2_iterations", [
  135. {datatype, integer}
  136. ]}.
  137. {mapping, "auth.pgsql.pbkdf2_dklen", "emqx_auth_pgsql.pbkdf2_dklen", [
  138. {datatype, integer}
  139. ]}.
  140. {mapping, "auth.pgsql.super_query", "emqx_auth_pgsql.super_query", [
  141. {datatype, string}
  142. ]}.
  143. {mapping, "auth.pgsql.acl_query", "emqx_auth_pgsql.acl_query", [
  144. {datatype, string}
  145. ]}.
  146. {translation, "emqx_auth_pgsql.password_hash", fun(Conf) ->
  147. HashValue = cuttlefish:conf_get("auth.pgsql.password_hash", Conf),
  148. case string:tokens(HashValue, ",") of
  149. [Hash] -> list_to_atom(Hash);
  150. [Prefix, Suffix] -> {list_to_atom(Prefix), list_to_atom(Suffix)};
  151. [Hash, MacFun, Iterations, Dklen] -> {list_to_atom(Hash), list_to_atom(MacFun), list_to_integer(Iterations), list_to_integer(Dklen)};
  152. _ -> plain
  153. end
  154. end}.