Reads a file containing ATTACH queries and then parses it to build an access entity.
| 110 | |
| 111 | /// Reads a file containing ATTACH queries and then parses it to build an access entity. |
| 112 | AccessEntityPtr readEntityFile(const String & file_path) |
| 113 | { |
| 114 | /// Read the file. |
| 115 | ReadBufferFromFile in{file_path}; |
| 116 | String file_contents; |
| 117 | readStringUntilEOF(file_contents, in); |
| 118 | |
| 119 | /// Parse the file contents. |
| 120 | ASTs queries; |
| 121 | ParserAttachAccessEntity parser; |
| 122 | const char * begin = file_contents.data(); /// begin of current query |
| 123 | const char * pos = begin; /// parser moves pos from begin to the end of current query |
| 124 | const char * end = begin + file_contents.size(); |
| 125 | while (pos < end) |
| 126 | { |
| 127 | queries.emplace_back(parseQueryAndMovePosition(parser, pos, end, "", true, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH)); |
| 128 | while (isWhitespaceASCII(*pos) || *pos == ';') |
| 129 | ++pos; |
| 130 | } |
| 131 | |
| 132 | /// Interpret the AST to build an access entity. |
| 133 | std::shared_ptr<User> user; |
| 134 | std::shared_ptr<Role> role; |
| 135 | std::shared_ptr<RowPolicy> policy; |
| 136 | std::shared_ptr<Quota> quota; |
| 137 | std::shared_ptr<SettingsProfile> profile; |
| 138 | AccessEntityPtr res; |
| 139 | bool sensitive_tenant = false; |
| 140 | |
| 141 | for (const auto & query : queries) |
| 142 | { |
| 143 | if (auto * create_user_query = query->as<ASTCreateUserQuery>()) |
| 144 | { |
| 145 | if (res) |
| 146 | throw Exception("Two access entities in one file " + file_path, ErrorCodes::INCORRECT_ACCESS_ENTITY_DEFINITION); |
| 147 | res = user = std::make_unique<User>(); |
| 148 | InterpreterCreateUserQuery::updateUserFromQuery(*user, *create_user_query); |
| 149 | } |
| 150 | else if (auto * create_role_query = query->as<ASTCreateRoleQuery>()) |
| 151 | { |
| 152 | if (res) |
| 153 | throw Exception("Two access entities in one file " + file_path, ErrorCodes::INCORRECT_ACCESS_ENTITY_DEFINITION); |
| 154 | res = role = std::make_unique<Role>(); |
| 155 | InterpreterCreateRoleQuery::updateRoleFromQuery(*role, *create_role_query); |
| 156 | } |
| 157 | else if (auto * create_policy_query = query->as<ASTCreateRowPolicyQuery>()) |
| 158 | { |
| 159 | if (res) |
| 160 | throw Exception("Two access entities in one file " + file_path, ErrorCodes::INCORRECT_ACCESS_ENTITY_DEFINITION); |
| 161 | res = policy = std::make_unique<RowPolicy>(); |
| 162 | InterpreterCreateRowPolicyQuery::updateRowPolicyFromQuery(*policy, *create_policy_query); |
| 163 | } |
| 164 | else if (auto * create_quota_query = query->as<ASTCreateQuotaQuery>()) |
| 165 | { |
| 166 | if (res) |
| 167 | throw Exception("Two access entities are attached in the same file " + file_path, ErrorCodes::INCORRECT_ACCESS_ENTITY_DEFINITION); |
| 168 | res = quota = std::make_unique<Quota>(); |
| 169 | InterpreterCreateQuotaQuery::updateQuotaFromQuery(*quota, *create_quota_query); |
no test coverage detected