PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/org/apache/cassandra/auth/AuthKeyspace.java

https://github.com/beobal/cassandra
Java | 115 lines | 76 code | 14 blank | 25 comment | 0 complexity | 4f4cef9dda23906b3e240421228c99df MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.cassandra.auth;
  19. import java.util.concurrent.TimeUnit;
  20. import org.apache.cassandra.cql3.statements.schema.CreateTableStatement;
  21. import org.apache.cassandra.schema.TableId;
  22. import org.apache.cassandra.schema.TableMetadata;
  23. import org.apache.cassandra.schema.SchemaConstants;
  24. import org.apache.cassandra.schema.KeyspaceMetadata;
  25. import org.apache.cassandra.schema.KeyspaceParams;
  26. import org.apache.cassandra.schema.Tables;
  27. import static java.lang.String.format;
  28. public final class AuthKeyspace
  29. {
  30. private AuthKeyspace()
  31. {
  32. }
  33. /**
  34. * Generation is used as a timestamp for automatic table creation on startup.
  35. * If you make any changes to the tables below, make sure to increment the
  36. * generation and document your change here.
  37. *
  38. * gen 0: original definition in 3.0
  39. * gen 1: compression chunk length reduced to 16KiB, memtable_flush_period_in_ms now unset on all tables in 4.0
  40. */
  41. public static final long GENERATION = 1;
  42. public static final String ROLES = "roles";
  43. public static final String ROLE_MEMBERS = "role_members";
  44. public static final String ROLE_PERMISSIONS = "role_permissions";
  45. public static final String RESOURCE_ROLE_INDEX = "resource_role_permissons_index";
  46. public static final String NETWORK_PERMISSIONS = "network_permissions";
  47. public static final long SUPERUSER_SETUP_DELAY = Long.getLong("cassandra.superuser_setup_delay_ms", 10000);
  48. private static final TableMetadata Roles =
  49. parse(ROLES,
  50. "role definitions",
  51. "CREATE TABLE %s ("
  52. + "role text,"
  53. + "is_superuser boolean,"
  54. + "can_login boolean,"
  55. + "salted_hash text,"
  56. + "member_of set<text>,"
  57. + "PRIMARY KEY(role))");
  58. private static final TableMetadata RoleMembers =
  59. parse(ROLE_MEMBERS,
  60. "role memberships lookup table",
  61. "CREATE TABLE %s ("
  62. + "role text,"
  63. + "member text,"
  64. + "PRIMARY KEY(role, member))");
  65. private static final TableMetadata RolePermissions =
  66. parse(ROLE_PERMISSIONS,
  67. "permissions granted to db roles",
  68. "CREATE TABLE %s ("
  69. + "role text,"
  70. + "resource text,"
  71. + "permissions set<text>,"
  72. + "PRIMARY KEY(role, resource))");
  73. private static final TableMetadata ResourceRoleIndex =
  74. parse(RESOURCE_ROLE_INDEX,
  75. "index of db roles with permissions granted on a resource",
  76. "CREATE TABLE %s ("
  77. + "resource text,"
  78. + "role text,"
  79. + "PRIMARY KEY(resource, role))");
  80. private static final TableMetadata NetworkPermissions =
  81. parse(NETWORK_PERMISSIONS,
  82. "user network permissions",
  83. "CREATE TABLE %s ("
  84. + "role text, "
  85. + "dcs frozen<set<text>>, "
  86. + "PRIMARY KEY(role))");
  87. private static TableMetadata parse(String name, String description, String cql)
  88. {
  89. return CreateTableStatement.parse(format(cql, name), SchemaConstants.AUTH_KEYSPACE_NAME)
  90. .id(TableId.forSystemTable(SchemaConstants.AUTH_KEYSPACE_NAME, name))
  91. .comment(description)
  92. .gcGraceSeconds((int) TimeUnit.DAYS.toSeconds(90))
  93. .build();
  94. }
  95. public static KeyspaceMetadata metadata()
  96. {
  97. return KeyspaceMetadata.create(SchemaConstants.AUTH_KEYSPACE_NAME,
  98. KeyspaceParams.simple(1),
  99. Tables.of(Roles, RoleMembers, RolePermissions, ResourceRoleIndex, NetworkPermissions));
  100. }
  101. }