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.opengauss.check.datasource;
19  
20  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException;
21  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException;
22  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException;
23  import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker;
24  import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
25  
26  import javax.sql.DataSource;
27  import java.sql.Connection;
28  import java.sql.DatabaseMetaData;
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import java.util.Collections;
33  
34  /**
35   * Data source checker of openGauss.
36   */
37  public final class OpenGaussDataSourceChecker implements DialectDataSourceChecker {
38      
39      private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles WHERE rolname = ?";
40      
41      @Override
42      public void checkPrivilege(final DataSource dataSource) {
43          try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(SHOW_GRANTS_SQL)) {
44              DatabaseMetaData metaData = connection.getMetaData();
45              preparedStatement.setString(1, metaData.getUserName());
46              try (ResultSet resultSet = preparedStatement.executeQuery()) {
47                  String username = metaData.getUserName();
48                  ShardingSpherePreconditions.checkState(resultSet.next(), () -> new PrepareJobWithoutUserException(username));
49                  String isSuperRole = resultSet.getString("rolsuper");
50                  String isReplicationRole = resultSet.getString("rolreplication");
51                  String isSystemAdminRole = resultSet.getString("rolsystemadmin");
52                  ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) || "t".equalsIgnoreCase(isReplicationRole) || "t".equalsIgnoreCase(isSystemAdminRole),
53                          () -> new PrepareJobWithoutEnoughPrivilegeException(Collections.singleton("REPLICATION")));
54              }
55          } catch (final SQLException ex) {
56              throw new PrepareJobWithCheckPrivilegeFailedException(ex);
57          }
58      }
59      
60      @Override
61      public void checkVariable(final DataSource dataSource) {
62      }
63      
64      @Override
65      public String getDatabaseType() {
66          return "openGauss";
67      }
68  }