privs.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2021 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package mysql
  14. // AllPrivilegeLiteral is the string literal for All Privilege.
  15. const AllPrivilegeLiteral = "ALL PRIVILEGES"
  16. // Priv2Str is the map for privilege to string.
  17. var Priv2Str = map[PrivilegeType]string{
  18. CreatePriv: "Create",
  19. SelectPriv: "Select",
  20. InsertPriv: "Insert",
  21. UpdatePriv: "Update",
  22. DeletePriv: "Delete",
  23. ShowDBPriv: "Show Databases",
  24. SuperPriv: "Super",
  25. CreateUserPriv: "Create User",
  26. CreateTablespacePriv: "Create Tablespace",
  27. TriggerPriv: "Trigger",
  28. DropPriv: "Drop",
  29. ProcessPriv: "Process",
  30. GrantPriv: "Grant Option",
  31. ReferencesPriv: "References",
  32. AlterPriv: "Alter",
  33. ExecutePriv: "Execute",
  34. IndexPriv: "Index",
  35. CreateViewPriv: "Create View",
  36. ShowViewPriv: "Show View",
  37. CreateRolePriv: "Create Role",
  38. DropRolePriv: "Drop Role",
  39. CreateTMPTablePriv: "CREATE TEMPORARY TABLES",
  40. LockTablesPriv: "LOCK TABLES",
  41. CreateRoutinePriv: "CREATE ROUTINE",
  42. AlterRoutinePriv: "ALTER ROUTINE",
  43. EventPriv: "EVENT",
  44. ShutdownPriv: "SHUTDOWN",
  45. ReloadPriv: "RELOAD",
  46. FilePriv: "FILE",
  47. ConfigPriv: "CONFIG",
  48. UsagePriv: "USAGE",
  49. ReplicationClientPriv: "REPLICATION CLIENT",
  50. ReplicationSlavePriv: "REPLICATION SLAVE",
  51. AllPriv: AllPrivilegeLiteral,
  52. }
  53. // Priv2SetStr is the map for privilege to string.
  54. var Priv2SetStr = map[PrivilegeType]string{
  55. CreatePriv: "Create",
  56. SelectPriv: "Select",
  57. InsertPriv: "Insert",
  58. UpdatePriv: "Update",
  59. DeletePriv: "Delete",
  60. DropPriv: "Drop",
  61. GrantPriv: "Grant",
  62. AlterPriv: "Alter",
  63. ExecutePriv: "Execute",
  64. IndexPriv: "Index",
  65. CreateViewPriv: "Create View",
  66. ShowViewPriv: "Show View",
  67. CreateRolePriv: "Create Role",
  68. DropRolePriv: "Drop Role",
  69. ShutdownPriv: "Shutdown Role",
  70. }
  71. // SetStr2Priv is the map for privilege set string to privilege type.
  72. var SetStr2Priv = map[string]PrivilegeType{
  73. "Create": CreatePriv,
  74. "Select": SelectPriv,
  75. "Insert": InsertPriv,
  76. "Update": UpdatePriv,
  77. "Delete": DeletePriv,
  78. "Drop": DropPriv,
  79. "Grant": GrantPriv,
  80. "Alter": AlterPriv,
  81. "Execute": ExecutePriv,
  82. "Index": IndexPriv,
  83. "Create View": CreateViewPriv,
  84. "Show View": ShowViewPriv,
  85. }
  86. // Priv2UserCol is the privilege to mysql.user table column name.
  87. var Priv2UserCol = map[PrivilegeType]string{
  88. CreatePriv: "Create_priv",
  89. SelectPriv: "Select_priv",
  90. InsertPriv: "Insert_priv",
  91. UpdatePriv: "Update_priv",
  92. DeletePriv: "Delete_priv",
  93. ShowDBPriv: "Show_db_priv",
  94. SuperPriv: "Super_priv",
  95. CreateUserPriv: "Create_user_priv",
  96. CreateTablespacePriv: "Create_tablespace_priv",
  97. TriggerPriv: "Trigger_priv",
  98. DropPriv: "Drop_priv",
  99. ProcessPriv: "Process_priv",
  100. GrantPriv: "Grant_priv",
  101. ReferencesPriv: "References_priv",
  102. AlterPriv: "Alter_priv",
  103. ExecutePriv: "Execute_priv",
  104. IndexPriv: "Index_priv",
  105. CreateViewPriv: "Create_view_priv",
  106. ShowViewPriv: "Show_view_priv",
  107. CreateRolePriv: "Create_role_priv",
  108. DropRolePriv: "Drop_role_priv",
  109. CreateTMPTablePriv: "Create_tmp_table_priv",
  110. LockTablesPriv: "Lock_tables_priv",
  111. CreateRoutinePriv: "Create_routine_priv",
  112. AlterRoutinePriv: "Alter_routine_priv",
  113. EventPriv: "Event_priv",
  114. ShutdownPriv: "Shutdown_priv",
  115. ReloadPriv: "Reload_priv",
  116. FilePriv: "File_priv",
  117. ConfigPriv: "Config_priv",
  118. ReplicationClientPriv: "Repl_client_priv",
  119. ReplicationSlavePriv: "Repl_slave_priv",
  120. }
  121. // Col2PrivType is the privilege tables column name to privilege type.
  122. var Col2PrivType = map[string]PrivilegeType{
  123. "Create_priv": CreatePriv,
  124. "Select_priv": SelectPriv,
  125. "Insert_priv": InsertPriv,
  126. "Update_priv": UpdatePriv,
  127. "Delete_priv": DeletePriv,
  128. "Show_db_priv": ShowDBPriv,
  129. "Super_priv": SuperPriv,
  130. "Create_user_priv": CreateUserPriv,
  131. "Create_tablespace_priv": CreateTablespacePriv,
  132. "Trigger_priv": TriggerPriv,
  133. "Drop_priv": DropPriv,
  134. "Process_priv": ProcessPriv,
  135. "Grant_priv": GrantPriv,
  136. "References_priv": ReferencesPriv,
  137. "Alter_priv": AlterPriv,
  138. "Execute_priv": ExecutePriv,
  139. "Index_priv": IndexPriv,
  140. "Create_view_priv": CreateViewPriv,
  141. "Show_view_priv": ShowViewPriv,
  142. "Create_role_priv": CreateRolePriv,
  143. "Drop_role_priv": DropRolePriv,
  144. "Create_tmp_table_priv": CreateTMPTablePriv,
  145. "Lock_tables_priv": LockTablesPriv,
  146. "Create_routine_priv": CreateRoutinePriv,
  147. "Alter_routine_priv": AlterRoutinePriv,
  148. "Event_priv": EventPriv,
  149. "Shutdown_priv": ShutdownPriv,
  150. "Reload_priv": ReloadPriv,
  151. "File_priv": FilePriv,
  152. "Config_priv": ConfigPriv,
  153. "Repl_client_priv": ReplicationClientPriv,
  154. "Repl_slave_priv": ReplicationSlavePriv,
  155. }
  156. // PrivilegeType privilege
  157. type PrivilegeType uint64
  158. // NewPrivFromColumn constructs priv from a column name. False means invalid priv column name.
  159. func NewPrivFromColumn(col string) (PrivilegeType, bool) {
  160. p, o := Col2PrivType[col]
  161. return p, o
  162. }
  163. // NewPrivFromSetEnum constructs priv from a set enum. False means invalid priv enum.
  164. func NewPrivFromSetEnum(e string) (PrivilegeType, bool) {
  165. p, o := SetStr2Priv[e]
  166. return p, o
  167. }
  168. // String returns the corresponding identifier in SQLs.
  169. func (p PrivilegeType) String() string {
  170. if s, ok := Priv2Str[p]; ok {
  171. return s
  172. }
  173. return ""
  174. }
  175. // ColumnString returns the corresponding name of columns in mysql.user/mysql.db.
  176. func (p PrivilegeType) ColumnString() string {
  177. if s, ok := Priv2UserCol[p]; ok {
  178. return s
  179. }
  180. return ""
  181. }
  182. // SetString returns the corresponding set enum string in Table_priv/Column_priv of mysql.tables_priv/mysql.columns_priv.
  183. func (p PrivilegeType) SetString() string {
  184. if s, ok := Priv2SetStr[p]; ok {
  185. return s
  186. }
  187. return ""
  188. }
  189. const (
  190. // UsagePriv is a synonym for “no privileges”
  191. UsagePriv PrivilegeType = 1 << iota
  192. // CreatePriv is the privilege to create schema/table.
  193. CreatePriv
  194. // SelectPriv is the privilege to read from table.
  195. SelectPriv
  196. // InsertPriv is the privilege to insert data into table.
  197. InsertPriv
  198. // UpdatePriv is the privilege to update data in table.
  199. UpdatePriv
  200. // DeletePriv is the privilege to delete data from table.
  201. DeletePriv
  202. // ShowDBPriv is the privilege to run show databases statement.
  203. ShowDBPriv
  204. // SuperPriv enables many operations and server behaviors.
  205. SuperPriv
  206. // CreateUserPriv is the privilege to create user.
  207. CreateUserPriv
  208. // TriggerPriv is not checked yet.
  209. TriggerPriv
  210. // DropPriv is the privilege to drop schema/table.
  211. DropPriv
  212. // ProcessPriv pertains to display of information about the threads executing within the server.
  213. ProcessPriv
  214. // GrantPriv is the privilege to grant privilege to user.
  215. GrantPriv
  216. // ReferencesPriv is not checked yet.
  217. ReferencesPriv
  218. // AlterPriv is the privilege to run alter statement.
  219. AlterPriv
  220. // ExecutePriv is the privilege to run execute statement.
  221. ExecutePriv
  222. // IndexPriv is the privilege to create/drop index.
  223. IndexPriv
  224. // CreateViewPriv is the privilege to create view.
  225. CreateViewPriv
  226. // ShowViewPriv is the privilege to show create view.
  227. ShowViewPriv
  228. // CreateRolePriv the privilege to create a role.
  229. CreateRolePriv
  230. // DropRolePriv is the privilege to drop a role.
  231. DropRolePriv
  232. CreateTMPTablePriv
  233. LockTablesPriv
  234. CreateRoutinePriv
  235. AlterRoutinePriv
  236. EventPriv
  237. // ShutdownPriv the privilege to shutdown a server.
  238. ShutdownPriv
  239. // ReloadPriv is the privilege to enable the use of the FLUSH statement.
  240. ReloadPriv
  241. // FilePriv is the privilege to enable the use of LOAD DATA and SELECT ... INTO OUTFILE.
  242. FilePriv
  243. // ConfigPriv is the privilege to enable the use SET CONFIG statements.
  244. ConfigPriv
  245. // CreateTablespacePriv is the privilege to create tablespace.
  246. CreateTablespacePriv
  247. // ReplicationClientPriv is used in MySQL replication
  248. ReplicationClientPriv
  249. // ReplicationSlavePriv is used in MySQL replication
  250. ReplicationSlavePriv
  251. // AllPriv is the privilege for all actions.
  252. AllPriv
  253. /*
  254. * Please add the new priv before AllPriv to keep the values consistent across versions.
  255. */
  256. // ExtendedPriv is used to successful parse privileges not included above.
  257. // these are dynamic privileges in MySQL 8.0 and other extended privileges like LOAD FROM S3 in Aurora.
  258. ExtendedPriv
  259. )
  260. // AllPrivMask is the mask for PrivilegeType with all bits set to 1.
  261. // If it's passed to RequestVerification, it means any privilege would be OK.
  262. const AllPrivMask = AllPriv - 1
  263. type Privileges []PrivilegeType
  264. func (privs Privileges) Has(p PrivilegeType) bool {
  265. for _, cp := range privs {
  266. if cp == p {
  267. return true
  268. }
  269. }
  270. return false
  271. }
  272. // AllGlobalPrivs is all the privileges in global scope.
  273. var AllGlobalPrivs = Privileges{SelectPriv, InsertPriv, UpdatePriv, DeletePriv, CreatePriv, DropPriv, ProcessPriv, ReferencesPriv, AlterPriv, ShowDBPriv, SuperPriv, ExecutePriv, IndexPriv, CreateUserPriv, CreateTablespacePriv, TriggerPriv, CreateViewPriv, ShowViewPriv, CreateRolePriv, DropRolePriv, CreateTMPTablePriv, LockTablesPriv, CreateRoutinePriv, AlterRoutinePriv, EventPriv, ShutdownPriv, ReloadPriv, FilePriv, ConfigPriv, ReplicationClientPriv, ReplicationSlavePriv}
  274. // AllDBPrivs is all the privileges in database scope.
  275. var AllDBPrivs = Privileges{SelectPriv, InsertPriv, UpdatePriv, DeletePriv, CreatePriv, DropPriv, AlterPriv, ExecutePriv, IndexPriv, CreateViewPriv, ShowViewPriv}
  276. // AllTablePrivs is all the privileges in table scope.
  277. var AllTablePrivs = Privileges{SelectPriv, InsertPriv, UpdatePriv, DeletePriv, CreatePriv, DropPriv, IndexPriv, AlterPriv, CreateViewPriv, ShowViewPriv}
  278. // AllColumnPrivs is all the privileges in column scope.
  279. var AllColumnPrivs = Privileges{SelectPriv, InsertPriv, UpdatePriv}