-
Notifications
You must be signed in to change notification settings - Fork 5k
[Improvement-18072][Api] Add user permission validation logic to the connectionTest, getDatabases, getTables, and getTableColumns methods in DataSourceController #18073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 3 commits
a73cd38
ca46d7c
481a0cb
c192fb3
869a2ab
4440081
6c36bea
7b61643
c426d07
8623537
94f154c
41d3686
3c008be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -330,18 +330,19 @@ | |
| throw new ServiceException(Status.CONNECTION_TEST_FAILURE); | ||
| } | ||
|
|
||
| /** | ||
| * test connection | ||
| * | ||
| * @param id datasource id | ||
| * @return connect result code | ||
| */ | ||
| @Override | ||
| public void connectionTest(int id) { | ||
| public void connectionTest(User loginUser, int id) { | ||
| DataSource dataSource = dataSourceMapper.selectById(id); | ||
|
|
||
| if (dataSource == null) { | ||
| throw new ServiceException(Status.RESOURCE_NOT_EXIST); | ||
| } | ||
|
|
||
| if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.DATASOURCE, | ||
| ApiFuncIdentificationConstant.DATASOURCE)) { | ||
| throw new ServiceException(Status.USER_NO_OPERATION_PERM); | ||
| } | ||
|
|
||
| checkConnection(dataSource.getType(), | ||
| DataSourceUtils.buildConnectionParams(dataSource.getType(), dataSource.getConnectionParams())); | ||
| } | ||
|
|
@@ -417,9 +418,72 @@ | |
| } | ||
|
|
||
| @Override | ||
| public List<ParamsOptions> getTables(Integer datasourceId, String database) { | ||
| public List<ParamsOptions> getDatabases(User loginUser, Integer datasourceId) { | ||
|
||
|
|
||
| DataSource dataSource = dataSourceMapper.selectById(datasourceId); | ||
|
|
||
| if (dataSource == null) { | ||
| throw new ServiceException(Status.QUERY_DATASOURCE_ERROR); | ||
| } | ||
|
|
||
| if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, | ||
| ApiFuncIdentificationConstant.DATASOURCE)) { | ||
| throw new ServiceException(Status.USER_NO_OPERATION_PERM); | ||
| } | ||
|
|
||
| List<String> tableList; | ||
| BaseConnectionParam connectionParam = | ||
| (BaseConnectionParam) DataSourceUtils.buildConnectionParams( | ||
| dataSource.getType(), | ||
| dataSource.getConnectionParams()); | ||
|
|
||
| if (null == connectionParam) { | ||
| throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED); | ||
| } | ||
|
|
||
| Connection connection = | ||
| DataSourceUtils.getConnection(dataSource.getType(), connectionParam); | ||
| ResultSet rs = null; | ||
|
|
||
| try { | ||
| if (null == connection) { | ||
| throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED); | ||
| } | ||
| if (dataSource.getType() == DbType.POSTGRESQL) { | ||
| rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY_PG); | ||
| } else { | ||
| rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY); | ||
| } | ||
| tableList = new ArrayList<>(); | ||
| while (rs.next()) { | ||
| String name = rs.getString(1); | ||
| tableList.add(name); | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Get databases error, datasourceId:{}.", datasourceId, e); | ||
| throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR); | ||
| } finally { | ||
| closeResult(rs); | ||
| releaseConnection(connection); | ||
| } | ||
|
|
||
| List<ParamsOptions> options = getParamsOptions(tableList); | ||
| return options; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ParamsOptions> getTables(User loginUser, Integer datasourceId, String database) { | ||
| DataSource dataSource = dataSourceMapper.selectById(datasourceId); | ||
|
|
||
| if (dataSource == null) { | ||
| throw new ServiceException(Status.QUERY_DATASOURCE_ERROR); | ||
| } | ||
|
|
||
| if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, | ||
| ApiFuncIdentificationConstant.DATASOURCE)) { | ||
| throw new ServiceException(Status.USER_NO_OPERATION_PERM); | ||
| } | ||
|
|
||
| List<String> tableList; | ||
| BaseConnectionParam connectionParam = | ||
| (BaseConnectionParam) DataSourceUtils.buildConnectionParams( | ||
|
|
@@ -477,8 +541,19 @@ | |
| } | ||
|
|
||
| @Override | ||
| public List<ParamsOptions> getTableColumns(Integer datasourceId, String database, String tableName) { | ||
| public List<ParamsOptions> getTableColumns(User loginUser, Integer datasourceId, String database, | ||
| String tableName) { | ||
| DataSource dataSource = dataSourceMapper.selectById(datasourceId); | ||
|
|
||
| if (dataSource == null) { | ||
| throw new ServiceException(Status.QUERY_DATASOURCE_ERROR); | ||
| } | ||
|
|
||
| if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, | ||
| ApiFuncIdentificationConstant.DATASOURCE)) { | ||
Check warningCode scanning / CodeQL Potential database resource leak Warning
This Statement is not always closed on method exit.
|
||
| throw new ServiceException(Status.USER_NO_OPERATION_PERM); | ||
| } | ||
Check warningCode scanning / CodeQL Potential database resource leak Warning
This Statement is not always closed on method exit.
|
||
|
|
||
| BaseConnectionParam connectionParam = | ||
| (BaseConnectionParam) DataSourceUtils.buildConnectionParams( | ||
| dataSource.getType(), | ||
|
|
@@ -522,55 +597,6 @@ | |
| return options; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ParamsOptions> getDatabases(Integer datasourceId) { | ||
|
|
||
| DataSource dataSource = dataSourceMapper.selectById(datasourceId); | ||
|
|
||
| if (dataSource == null) { | ||
| throw new ServiceException(Status.QUERY_DATASOURCE_ERROR); | ||
| } | ||
|
|
||
| List<String> tableList; | ||
| BaseConnectionParam connectionParam = | ||
| (BaseConnectionParam) DataSourceUtils.buildConnectionParams( | ||
| dataSource.getType(), | ||
| dataSource.getConnectionParams()); | ||
|
|
||
| if (null == connectionParam) { | ||
| throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED); | ||
| } | ||
|
|
||
| Connection connection = | ||
| DataSourceUtils.getConnection(dataSource.getType(), connectionParam); | ||
| ResultSet rs = null; | ||
|
|
||
| try { | ||
| if (null == connection) { | ||
| throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED); | ||
| } | ||
| if (dataSource.getType() == DbType.POSTGRESQL) { | ||
| rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY_PG); | ||
| } else { | ||
| rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY); | ||
| } | ||
| tableList = new ArrayList<>(); | ||
| while (rs.next()) { | ||
| String name = rs.getString(1); | ||
| tableList.add(name); | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Get databases error, datasourceId:{}.", datasourceId, e); | ||
| throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR); | ||
| } finally { | ||
| closeResult(rs); | ||
| releaseConnection(connection); | ||
| } | ||
|
|
||
| List<ParamsOptions> options = getParamsOptions(tableList); | ||
| return options; | ||
| } | ||
|
|
||
| private List<ParamsOptions> getParamsOptions(List<String> columnList) { | ||
| List<ParamsOptions> options = null; | ||
| if (CollectionUtils.isNotEmpty(columnList)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't change the method order. This will increase the review work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, fix it.