View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.postgresql.check.datasource;
19  
20  import lombok.extern.slf4j.Slf4j;
21  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException;
22  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException;
23  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException;
24  import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker;
25  import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
26  
27  import javax.sql.DataSource;
28  import java.sql.Connection;
29  import java.sql.DatabaseMetaData;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  import java.util.Collections;
34  
35  /**
36   * PostgreSQL Data source checker.
37   */
38  @Slf4j
39  public final class PostgreSQLDataSourceChecker implements DialectDataSourceChecker {
40      
41      private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles WHERE rolname = ?";
42      
43      @Override
44      public void checkPrivilege(final DataSource dataSource) {
45          try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(SHOW_GRANTS_SQL)) {
46              DatabaseMetaData metaData = connection.getMetaData();
47              preparedStatement.setString(1, metaData.getUserName());
48              try (ResultSet resultSet = preparedStatement.executeQuery()) {
49                  String username = metaData.getUserName();
50                  ShardingSpherePreconditions.checkState(resultSet.next(), () -> new PrepareJobWithoutUserException(username));
51                  String isSuperRole = resultSet.getString("rolsuper");
52                  String isReplicationRole = resultSet.getString("rolreplication");
53                  log.info("checkPrivilege: isSuperRole: {}, isReplicationRole: {}", isSuperRole, isReplicationRole);
54                  ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) || "t".equalsIgnoreCase(isReplicationRole),
55                          () -> new PrepareJobWithoutEnoughPrivilegeException(Collections.singleton("REPLICATION")));
56              }
57          } catch (final SQLException ex) {
58              throw new PrepareJobWithCheckPrivilegeFailedException(ex);
59          }
60      }
61      
62      @Override
63      public void checkVariable(final DataSource dataSource) {
64      }
65      
66      @Override
67      public String getDatabaseType() {
68          return "PostgreSQL";
69      }
70  }