From a73cd38df603f109fa3481083ab72a7df1025418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Tue, 17 Mar 2026 18:41:59 +0800 Subject: [PATCH 1/7] Add user permission validation logic --- .../api/controller/DataSourceController.java | 89 ++++++++++++------- .../api/service/DataSourceService.java | 12 ++- .../service/impl/DataSourceServiceImpl.java | 38 ++++++-- .../main/resources/i18n/messages.properties | 6 +- .../resources/i18n/messages_en_US.properties | 6 +- .../resources/i18n/messages_zh_CN.properties | 6 +- .../api/service/DataSourceServiceTest.java | 9 +- 7 files changed, 115 insertions(+), 51 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 6dbdea39aa7c..1d1abb2a4657 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -202,7 +202,6 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque /** * connect datasource * - * @param loginUser login user * @param jsonStr datasource param * example: {"type":"MYSQL","name":"txx","note":"","host":"localhost","port":3306,"principal":"","javaSecurityKrb5Conf":"","loginUserKeytabUsername":"","loginUserKeytabPath":"","userName":"root","password":"xxx","database":"ds","connectType":"","other":{"serverTimezone":"GMT-8"},"id":2} * @return connect result code @@ -211,8 +210,7 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @PostMapping(value = "/connect") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECT_DATASOURCE_FAILURE) - public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { + public Result connectDataSource(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); DataSourceUtils.checkDatasourceParam(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); @@ -223,7 +221,6 @@ public Result connectDataSource(@Parameter(hidden = true) @RequestAttri /** * connection test * - * @param loginUser login user * @param id data source id * @return connect result code */ @@ -234,8 +231,7 @@ public Result connectDataSource(@Parameter(hidden = true) @RequestAttri @GetMapping(value = "/{id}/connect-test") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECTION_TEST_FAILURE) - public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("id") int id) { + public Result connectionTest(@PathVariable("id") int id) { dataSourceService.connectionTest(id); return Result.success(true); } @@ -264,7 +260,6 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib /** * verify datasource name * - * @param loginUser login user * @param name data source name * @return true if data source name not exists, otherwise return false */ @@ -275,8 +270,7 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/verify-name") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE) - public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "name") String name) { + public Result verifyDataSourceName(@RequestParam(value = "name") String name) { dataSourceService.verifyDataSourceName(name); return Result.success(true); } @@ -323,20 +317,47 @@ public Result authedDatasource(@Parameter(hidden = true) @RequestAttribu } /** - * get user info + * Checks the startup status of Kerberos authentication. * - * @param loginUser login user - * @return user info data + * @return a boolean indicating whether Kerberos is currently active */ - @Operation(summary = "getKerberosStartupState", description = "GET_USER_INFO_NOTES") + @Operation(summary = "getKerberosStartupState", description = "GET_KERBEROS_STARTUP_STATE") @GetMapping(value = "/kerberos-startup-state") @ResponseStatus(HttpStatus.OK) @ApiException(KERBEROS_STARTUP_STATE) - public Result getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result getKerberosStartupState() { // if upload resource is HDFS and kerberos startup is true , else false return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState()); } + /** + * Retrieves the list of databases available in a specific data source. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @return a list of database names/options accessible to the user + */ + @Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES") + @Parameters({ + @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")) + }) + @GetMapping(value = "/databases") + @ResponseStatus(HttpStatus.OK) + @ApiException(GET_DATASOURCE_DATABASES_ERROR) + public Result getDatabases(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId) { + List options = dataSourceService.getDatabases(loginUser, datasourceId); + return Result.success(options); + } + + /** + * Retrieves the list of tables within a specific database of a data source. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @param database the name of the database to query + * @return a list of table names/options accessible to the user + */ @Operation(summary = "tables", description = "GET_DATASOURCE_TABLES_NOTES") @Parameters({ @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")), @@ -345,37 +366,37 @@ public Result getKerberosStartupState(@Parameter(hidden = true) @Request @GetMapping(value = "/tables") @ResponseStatus(HttpStatus.OK) @ApiException(GET_DATASOURCE_TABLES_ERROR) - public Result getTables(@RequestParam("datasourceId") Integer datasourceId, - @RequestParam(value = "database") String database) { - List options = dataSourceService.getTables(datasourceId, database); + public Result getTables(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId, + @RequestParam("database") String database) { + List options = dataSourceService.getTables(loginUser, datasourceId, database); return Result.success(options); } + /** + * Retrieves the column details (schema) for a specific table. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @param database the name of the database containing the table + * @param tableName the name of the table to query columns for + * @return a list of column definitions (name, type, etc.) for the specified table + */ @Operation(summary = "tableColumns", description = "GET_DATASOURCE_TABLE_COLUMNS_NOTES") @Parameters({ @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")), - @Parameter(name = "tableName", description = "TABLE_NAME", required = true, schema = @Schema(implementation = String.class, example = "test")), - @Parameter(name = "database", description = "DATABASE", required = true, schema = @Schema(implementation = String.class, example = "test")) + @Parameter(name = "database", description = "DATABASE", required = true, schema = @Schema(implementation = String.class, example = "test")), + @Parameter(name = "tableName", description = "TABLE_NAME", required = true, schema = @Schema(implementation = String.class, example = "test")) }) @GetMapping(value = "/tableColumns") @ResponseStatus(HttpStatus.OK) @ApiException(GET_DATASOURCE_TABLE_COLUMNS_ERROR) - public Result getTableColumns(@RequestParam("datasourceId") Integer datasourceId, - @RequestParam("tableName") String tableName, - @RequestParam(value = "database") String database) { - List options = dataSourceService.getTableColumns(datasourceId, database, tableName); + public Result getTableColumns(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId, + @RequestParam("database") String database, + @RequestParam("tableName") String tableName) { + List options = dataSourceService.getTableColumns(loginUser, datasourceId, database, tableName); return Result.success(options); } - @Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES") - @Parameters({ - @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")) - }) - @GetMapping(value = "/databases") - @ResponseStatus(HttpStatus.OK) - @ApiException(GET_DATASOURCE_DATABASES_ERROR) - public Result getDatabases(@RequestParam("datasourceId") Integer datasourceId) { - List options = dataSourceService.getDatabases(datasourceId); - return Result.success(options); - } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 1cdf76faa968..0fa50edd75cd 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -132,25 +132,31 @@ public interface DataSourceService { /** * get tables + * + * @param loginUser login user * @param datasourceId * @param database * @return */ - List getTables(Integer datasourceId, String database); + List getTables(User loginUser, Integer datasourceId, String database); /** * get table columns + * + * @param loginUser login user * @param datasourceId * @param database * @param tableName * @return */ - List getTableColumns(Integer datasourceId, String database, String tableName); + List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); /** * get databases + * + * @param loginUser login user * @param datasourceId * @return */ - List getDatabases(Integer datasourceId); + List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index 7f298a7d841d..fc00db80603a 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -202,7 +202,7 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.DATASOURCE, ApiFuncIdentificationConstant.DATASOURCE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } @@ -358,13 +358,16 @@ public void connectionTest(int id) { public void delete(User loginUser, int datasourceId) { // query datasource by id DataSource dataSource = dataSourceMapper.selectById(datasourceId); + if (dataSource == null) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, + + if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, DATASOURCE_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } + dataSourceMapper.deleteById(datasourceId); datasourceUserMapper.deleteByDatasourceId(datasourceId); } @@ -417,9 +420,18 @@ public List authedDatasource(User loginUser, Integer userId) { } @Override - public List getTables(Integer datasourceId, String database) { + public List 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 tableList; BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( @@ -477,8 +489,19 @@ public List getTables(Integer datasourceId, String database) { } @Override - public List getTableColumns(Integer datasourceId, String database, String tableName) { + public List 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)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); + } + BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( dataSource.getType(), @@ -523,7 +546,7 @@ public List getTableColumns(Integer datasourceId, String database } @Override - public List getDatabases(Integer datasourceId) { + public List getDatabases(User loginUser, Integer datasourceId) { DataSource dataSource = dataSourceMapper.selectById(datasourceId); @@ -531,6 +554,11 @@ public List getDatabases(Integer datasourceId) { 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 tableList; BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 7f56c1eb8d39..379f9b0cfdd1 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -224,6 +224,10 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source +GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state +GET_DATASOURCE_DATABASE_NOTES=get datasource databases +GET_DATASOURCE_TABLES_NOTES=get datasource tables +GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns DELETE_SCHEDULE_NOTES=delete schedule by id QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES= query authorized and user created project @@ -234,8 +238,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=query workflow definition versions SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=switch workflow definition version VERSION=version STATE=state -GET_DATASOURCE_TABLES_NOTES=get datasource table -GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns TABLE_NAME=table name AUDIT_LOG_TAG=audit log related operation TASK_DEFINITION_TAG=task definition related operation diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index 71e5dc091d83..086dbc269496 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -257,6 +257,10 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source +GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state +GET_DATASOURCE_DATABASE_NOTES=get datasource databases +GET_DATASOURCE_TABLES_NOTES=get datasource tables +GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns DELETE_SCHEDULE_NOTES=delete schedule by id QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES=query authorized and user created project @@ -267,8 +271,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=query process definition versions SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=switch process definition version VERSION=version TASK_GROUP_QUEUE_PRIORITY=task group queue priority -GET_DATASOURCE_TABLES_NOTES=get datasource table -GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns TABLE_NAME=table name QUERY_AUDIT_LOG=query audit log AUDIT_LOG_TAG=audit log related operation diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index 243c41c0685c..f6a1bd01e473 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -256,6 +256,10 @@ DELETE_DATA_SOURCE_NOTES=\u5220\u9664\u6570\u636E\u6E90 VERIFY_DATA_SOURCE_NOTES=\u9A8C\u8BC1\u6570\u636E\u6E90 UNAUTHORIZED_DATA_SOURCE_NOTES=\u672A\u6388\u6743\u7684\u6570\u636E\u6E90 AUTHORIZED_DATA_SOURCE_NOTES=\u6388\u6743\u7684\u6570\u636E\u6E90 +GET_KERBEROS_STARTUP_STATE=\u83B7\u53D6Kerberos\u542F\u52A8\u72B6\u6001 +GET_DATASOURCE_DATABASE_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u5E93\u5217\u8868 +GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 +GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D DELETE_SCHEDULE_NOTES=\u6839\u636E\u5B9A\u65F6id\u5220\u9664\u5B9A\u65F6\u6570\u636E QUERY_ALERT_GROUP_LIST_PAGING_NOTES=\u5206\u9875\u67E5\u8BE2\u544A\u8B66\u7EC4\u5217\u8868 QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES=\u67E5\u8BE2\u6388\u6743\u548C\u7528\u6237\u521B\u5EFA\u7684\u9879\u76EE @@ -266,8 +270,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=\u67E5\u8BE2\u6D41\u7A0B\u5386\u53F2\u7 SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=\u5207\u6362\u6D41\u7A0B\u7248\u672C VERSION=\u7248\u672C\u53F7 TASK_GROUP_QUEUE_PRIORITY=\u4EFB\u52A1\u961F\u5217\u4F18\u5148\u7EA7 -GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 -GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D TABLE_NAME=\u8868\u540D QUERY_AUDIT_LOG=\u67E5\u8BE2\u5BA1\u8BA1\u65E5\u5FD7 AUDIT_LOG_TAG=\u5BA1\u8BA1\u65E5\u5FD7\u6267\u884C\u76F8\u5173\u64CD\u4F5C diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index b4e7aae4b8d3..50f61255d41c 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -605,13 +605,15 @@ public void testCheckConnection() throws Exception { @Test public void testGetDatabases() throws SQLException { + User loginUser = getAdminUser(); + DataSource dataSource = getOracleDataSource(); int datasourceId = 1; dataSource.setId(datasourceId); when(dataSourceMapper.selectById(datasourceId)).thenReturn(null); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.QUERY_DATASOURCE_ERROR.getMsg())); } @@ -623,9 +625,10 @@ public void testGetDatabases() throws SQLException { dataSourceUtils.when(() -> DataSourceUtils.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection); dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any())) .thenReturn(connectionParam); + passResourcePermissionCheckService(); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.GET_DATASOURCE_TABLES_ERROR.getMsg())); } @@ -634,7 +637,7 @@ public void testGetDatabases() throws SQLException { .thenReturn(null); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.DATASOURCE_CONNECT_FAILED.getMsg())); } From 481a0cb333fd8883fc860ec9853ea203f86caf50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Wed, 18 Mar 2026 11:26:08 +0800 Subject: [PATCH 2/7] Correct method comments --- .../api/controller/DataSourceController.java | 8 +- .../api/service/DataSourceService.java | 95 +++++++++++-------- .../service/impl/DataSourceServiceImpl.java | 85 ++--------------- .../api/service/DataSourceServiceTest.java | 9 +- 4 files changed, 74 insertions(+), 123 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 1d1abb2a4657..792b412f3074 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -35,6 +35,7 @@ import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DataSourceService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -231,8 +232,9 @@ public Result connectDataSource(@io.swagger.v3.oas.annotations.paramete @GetMapping(value = "/{id}/connect-test") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECTION_TEST_FAILURE) - public Result connectionTest(@PathVariable("id") int id) { - dataSourceService.connectionTest(id); + public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("id") int id) { + dataSourceService.connectionTest(loginUser, id); return Result.success(true); } @@ -261,7 +263,7 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib * verify datasource name * * @param name data source name - * @return true if data source name not exists, otherwise return false + * @return true if data source name not exists, otherwise throw a {@link ServiceException} */ @Operation(summary = "verifyDataSourceName", description = "VERIFY_DATA_SOURCE_NOTES") @Parameters({ diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 0fa50edd75cd..898a54b11798 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.api.service; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.dao.entity.DataSource; import org.apache.dolphinscheduler.dao.entity.User; @@ -33,28 +34,31 @@ public interface DataSourceService { /** - * create data source + * create a new data source. * * @param loginUser login user - * @param datasourceParam datasource parameter - * @return create result code + * @param datasourceParam data source configuration DTO + * @return created {@link DataSource} entity (sensitive fields masked) + * @throws ServiceException if permission denied, security check fails, or connection test fails */ DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); /** - * updateWorkflowInstance datasource + * update datasource * * @param loginUser login user * @param dataSourceParam data source params - * @return update result code + * @return updated {@link DataSource} entity (sensitive fields masked) + * @throws ServiceException if permission denied, security check fails, or connection test fails */ DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam); /** - * updateWorkflowInstance datasource + * query datasource * + * @param loginUser login user * @param id datasource id - * @return data source detail + * @return a {@link DataSource} entity (sensitive fields masked) */ BaseDataSourceParamDTO queryDataSource(int id, User loginUser); @@ -79,36 +83,44 @@ public interface DataSourceService { List queryDataSourceList(User loginUser, Integer type); /** - * verify datasource exists + * verify whether a data source name already exists. + *

+ * If the name already exists, a {@link ServiceException} is thrown. + * If the name is available (does not exist), the method completes successfully without returning a value. * - * @param name datasource name - * @return true if data datasource not exists, otherwise return false + * @param name the data source name to verify + * @throws ServiceException if the data source name already exists (Status.DATASOURCE_EXIST) */ void verifyDataSourceName(String name); /** - * check connection + * Checks the connectivity of a data source based on the provided type and parameters. + *

+ * This method attempts to establish a connection. + * - If the connection is successful, the method returns normally (void). + * - If the connection fails, a {@link ServiceException} is thrown. * - * @param type data source type - * @param parameter data source parameters - * @return true if connect successfully, otherwise false + * @param type the type of the data source (e.g., MYSQL, POSTGRESQL) + * @param connectionParam the connection parameters containing host, port, credentials, etc. + * @throws ServiceException if the connection test fails (Status.CONNECTION_TEST_FAILURE) */ - void checkConnection(DbType type, ConnectionParam parameter); + void checkConnection(DbType type, ConnectionParam connectionParam); /** - * test connection + * Tests the connectivity of a specific data source. * - * @param id datasource id - * @return connect result code + * @param loginUser the current logged-in user (required for permission check) + * @param id the unique identifier of the data source to test + * @throws ServiceException if the resource doesn't exist, permission is denied, or connection fails */ - void connectionTest(int id); + void connectionTest(User loginUser, int id); /** - * delete datasource + * delete a data source by ID. * - * @param loginUser login user - * @param datasourceId data source id - * @return delete result code + * @param loginUser the current logged-in user + * @param datasourceId the unique identifier of the data source to delete + * @throws ServiceException if checks fail or deletion encounters an error */ void delete(User loginUser, int datasourceId); @@ -117,7 +129,7 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return unauthed data source result code + * @return a list of {@link DataSource} objects that are available to be authorized to the target user */ List unAuthDatasource(User loginUser, Integer userId); @@ -126,37 +138,40 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return authorized result code + * @return a list of {@link DataSource} objects that are authorized to the target user */ List authedDatasource(User loginUser, Integer userId); /** - * get tables + * Retrieves the list of tables from a specific database within a data source. * - * @param loginUser login user - * @param datasourceId - * @param database - * @return + * @param loginUser the current logged-in user (required for permission check) + * @param datasourceId the unique identifier of the data source + * @param database the specific database/schema name to query (nullable for some DB types like SQLite) + * @return a list of {@link ParamsOptions} containing table names and optional metadata (e.g., comments) + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getTables(User loginUser, Integer datasourceId, String database); /** - * get table columns + * Retrieves the list of columns for a specific table in a data source. * - * @param loginUser login user - * @param datasourceId - * @param database - * @param tableName - * @return + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @param database database/schema name + * @param tableName table name to query + * @return list of {@link ParamsOptions} representing column names and types + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); /** - * get databases + * Retrieves the list of databases (or schemas) available in a specific data source. * - * @param loginUser login user - * @param datasourceId - * @return + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @return list of {@link ParamsOptions} representing database/schema names + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index fc00db80603a..fbe01b2bc213 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -86,13 +86,6 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource private static final String TABLE_NAME = "TABLE_NAME"; private static final String COLUMN_NAME = "COLUMN_NAME"; - /** - * create data source - * - * @param loginUser login user - * @param datasourceParam datasource parameters - * @return create result code - */ @Override public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { DataSourceUtils.checkDatasourceParam(datasourceParam); @@ -129,12 +122,6 @@ public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO dataso } } - /** - * updateWorkflowInstance datasource - * - * @param loginUser login user - * @return update result code - */ @Override public DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam) { DataSourceUtils.checkDatasourceParam(dataSourceParam); @@ -188,12 +175,6 @@ private boolean checkName(String name) { return queryDataSource != null && !queryDataSource.isEmpty(); } - /** - * updateWorkflowInstance datasource - * - * @param id datasource id - * @return data source detail - */ @Override public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { DataSource dataSource = dataSourceMapper.selectById(id); @@ -218,15 +199,6 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { return baseDataSourceParamDTO; } - /** - * query datasource list by keyword - * - * @param loginUser login user - * @param searchVal search value - * @param pageNo page number - * @param pageSize page size - * @return data source list page - */ @Override public PageInfo queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { @@ -272,13 +244,6 @@ private String getHiddenPassword() { return Constants.XXXXXX; } - /** - * query data resource list - * - * @param loginUser login user - * @param type data source type - * @return data source list page - */ @Override public List queryDataSourceList(User loginUser, Integer type) { @@ -298,12 +263,6 @@ public List queryDataSourceList(User loginUser, Integer type) { return datasourceList; } - /** - * verify datasource exists - * - * @param name datasource name - * @return true if data datasource not exists, otherwise return false - */ @Override public void verifyDataSourceName(String name) { List dataSourceList = dataSourceMapper.queryDataSourceByName(name); @@ -312,14 +271,6 @@ public void verifyDataSourceName(String name) { } } - /** - * check connection - * - * @param type data source type - * @param connectionParam connectionParam - * @return true if connect successfully, otherwise false - * @return true if connect successfully, otherwise false - */ @Override public void checkConnection(DbType type, ConnectionParam connectionParam) { DataSourceProcessor sshDataSourceProcessor = DataSourceUtils.getDatasourceProcessor(type); @@ -330,29 +281,23 @@ public void checkConnection(DbType type, ConnectionParam connectionParam) { 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())); } - /** - * delete datasource - * - * @param loginUser login user - * @param datasourceId data source id - * @return delete result code - */ @Override @Transactional public void delete(User loginUser, int datasourceId) { @@ -372,13 +317,6 @@ public void delete(User loginUser, int datasourceId) { datasourceUserMapper.deleteByDatasourceId(datasourceId); } - /** - * unauthorized datasource - * - * @param loginUser login user - * @param userId user id - * @return unauthed data source result code - */ @Override public List unAuthDatasource(User loginUser, Integer userId) { List datasourceList; @@ -406,13 +344,6 @@ public List unAuthDatasource(User loginUser, Integer userId) { return resultList; } - /** - * authorized datasource - * - * @param loginUser login user - * @param userId user id - * @return authorized result code - */ @Override public List authedDatasource(User loginUser, Integer userId) { List authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index 50f61255d41c..1824b509235b 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -268,9 +268,11 @@ public void testQueryDataSourceListPaging() { @Test public void testConnectionTest() { + User loginUser = getAdminUser(); int dataSourceId = -1; when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); - assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, () -> dataSourceService.connectionTest(dataSourceId)); + assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, + () -> dataSourceService.connectionTest(loginUser, dataSourceId)); try ( MockedStatic ignored = @@ -281,11 +283,12 @@ public void testConnectionTest() { when(DataSourceUtils.getDatasourceProcessor(Mockito.any())).thenReturn(dataSourceProcessor); when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(true); - assertDoesNotThrow(() -> dataSourceService.connectionTest(dataSource.getId())); + passResourcePermissionCheckService(); + assertDoesNotThrow(() -> dataSourceService.connectionTest(loginUser, dataSource.getId())); when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(false); assertThrowsServiceException(Status.CONNECTION_TEST_FAILURE, - () -> dataSourceService.connectionTest(dataSource.getId())); + () -> dataSourceService.connectionTest(loginUser, dataSource.getId())); } } From 869a2ab8010db22934fdf491dd7ceedc9dbc82fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Thu, 19 Mar 2026 14:34:35 +0800 Subject: [PATCH 3/7] Revert and optimize method comment modifications. --- .../api/controller/DataSourceController.java | 23 ++- .../api/service/DataSourceService.java | 71 +++---- .../service/impl/DataSourceServiceImpl.java | 185 ++++++++++++------ .../main/resources/i18n/messages.properties | 1 - .../resources/i18n/messages_en_US.properties | 1 - .../resources/i18n/messages_zh_CN.properties | 1 - 6 files changed, 171 insertions(+), 111 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 792b412f3074..03b1898495f1 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -35,7 +35,6 @@ import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; -import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DataSourceService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -203,6 +202,7 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque /** * connect datasource * + * @param loginUser login user * @param jsonStr datasource param * example: {"type":"MYSQL","name":"txx","note":"","host":"localhost","port":3306,"principal":"","javaSecurityKrb5Conf":"","loginUserKeytabUsername":"","loginUserKeytabPath":"","userName":"root","password":"xxx","database":"ds","connectType":"","other":{"serverTimezone":"GMT-8"},"id":2} * @return connect result code @@ -211,7 +211,8 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @PostMapping(value = "/connect") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECT_DATASOURCE_FAILURE) - public Result connectDataSource(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { + public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); DataSourceUtils.checkDatasourceParam(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); @@ -222,8 +223,9 @@ public Result connectDataSource(@io.swagger.v3.oas.annotations.paramete /** * connection test * + * @param loginUser login user * @param id data source id - * @return connect result code + * @return A Result object containing true if the connection is successful, false otherwise. */ @Operation(summary = "connectionTest", description = "CONNECT_DATA_SOURCE_TEST_NOTES") @Parameters({ @@ -262,8 +264,9 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib /** * verify datasource name * + * @param loginUser login user * @param name data source name - * @return true if data source name not exists, otherwise throw a {@link ServiceException} + * @return true if data source name not exists, otherwise return false */ @Operation(summary = "verifyDataSourceName", description = "VERIFY_DATA_SOURCE_NOTES") @Parameters({ @@ -272,7 +275,8 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/verify-name") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE) - public Result verifyDataSourceName(@RequestParam(value = "name") String name) { + public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "name") String name) { dataSourceService.verifyDataSourceName(name); return Result.success(true); } @@ -319,15 +323,16 @@ public Result authedDatasource(@Parameter(hidden = true) @RequestAttribu } /** - * Checks the startup status of Kerberos authentication. + * get user info * - * @return a boolean indicating whether Kerberos is currently active + * @param loginUser login user + * @return user info data */ - @Operation(summary = "getKerberosStartupState", description = "GET_KERBEROS_STARTUP_STATE") + @Operation(summary = "getKerberosStartupState", description = "GET_USER_INFO_NOTES") @GetMapping(value = "/kerberos-startup-state") @ResponseStatus(HttpStatus.OK) @ApiException(KERBEROS_STARTUP_STATE) - public Result getKerberosStartupState() { + public Result getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { // if upload resource is HDFS and kerberos startup is true , else false return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState()); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 898a54b11798..68031142a417 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -34,31 +34,28 @@ public interface DataSourceService { /** - * create a new data source. + * create data source * * @param loginUser login user - * @param datasourceParam data source configuration DTO - * @return created {@link DataSource} entity (sensitive fields masked) - * @throws ServiceException if permission denied, security check fails, or connection test fails + * @param datasourceParam datasource parameter + * @return create result code */ DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); /** - * update datasource + * updateWorkflowInstance datasource * * @param loginUser login user * @param dataSourceParam data source params - * @return updated {@link DataSource} entity (sensitive fields masked) - * @throws ServiceException if permission denied, security check fails, or connection test fails + * @return update result code */ DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam); /** - * query datasource + * updateWorkflowInstance datasource * - * @param loginUser login user * @param id datasource id - * @return a {@link DataSource} entity (sensitive fields masked) + * @return data source detail */ BaseDataSourceParamDTO queryDataSource(int id, User loginUser); @@ -83,28 +80,21 @@ public interface DataSourceService { List queryDataSourceList(User loginUser, Integer type); /** - * verify whether a data source name already exists. - *

- * If the name already exists, a {@link ServiceException} is thrown. - * If the name is available (does not exist), the method completes successfully without returning a value. + * verify datasource exists * - * @param name the data source name to verify - * @throws ServiceException if the data source name already exists (Status.DATASOURCE_EXIST) + * @param name datasource name + * @return true if data datasource not exists, otherwise return false */ void verifyDataSourceName(String name); /** - * Checks the connectivity of a data source based on the provided type and parameters. - *

- * This method attempts to establish a connection. - * - If the connection is successful, the method returns normally (void). - * - If the connection fails, a {@link ServiceException} is thrown. + * check connection * - * @param type the type of the data source (e.g., MYSQL, POSTGRESQL) - * @param connectionParam the connection parameters containing host, port, credentials, etc. - * @throws ServiceException if the connection test fails (Status.CONNECTION_TEST_FAILURE) + * @param type data source type + * @param parameter data source parameters + * @return true if connect successfully, otherwise false */ - void checkConnection(DbType type, ConnectionParam connectionParam); + void checkConnection(DbType type, ConnectionParam parameter); /** * Tests the connectivity of a specific data source. @@ -116,11 +106,11 @@ public interface DataSourceService { void connectionTest(User loginUser, int id); /** - * delete a data source by ID. + * delete datasource * - * @param loginUser the current logged-in user - * @param datasourceId the unique identifier of the data source to delete - * @throws ServiceException if checks fail or deletion encounters an error + * @param loginUser login user + * @param datasourceId data source id + * @return delete result code */ void delete(User loginUser, int datasourceId); @@ -129,7 +119,7 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return a list of {@link DataSource} objects that are available to be authorized to the target user + * @return unauthed data source result code */ List unAuthDatasource(User loginUser, Integer userId); @@ -138,10 +128,20 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return a list of {@link DataSource} objects that are authorized to the target user + * @return authorized result code */ List authedDatasource(User loginUser, Integer userId); + /** + * Retrieves the list of databases (or schemas) available in a specific data source. + * + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @return list of {@link ParamsOptions} representing database/schema names + * @throws ServiceException if permission denied, resource not found, or connection fails + */ + List getDatabases(User loginUser, Integer datasourceId); + /** * Retrieves the list of tables from a specific database within a data source. * @@ -165,13 +165,4 @@ public interface DataSourceService { */ List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); - /** - * Retrieves the list of databases (or schemas) available in a specific data source. - * - * @param loginUser current logged-in user - * @param datasourceId ID of the data source - * @return list of {@link ParamsOptions} representing database/schema names - * @throws ServiceException if permission denied, resource not found, or connection fails - */ - List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index fbe01b2bc213..11bbf169604e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -86,6 +86,13 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource private static final String TABLE_NAME = "TABLE_NAME"; private static final String COLUMN_NAME = "COLUMN_NAME"; + /** + * create data source + * + * @param loginUser login user + * @param datasourceParam datasource parameters + * @return create result code + */ @Override public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { DataSourceUtils.checkDatasourceParam(datasourceParam); @@ -122,6 +129,12 @@ public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO dataso } } + /** + * updateWorkflowInstance datasource + * + * @param loginUser login user + * @return update result code + */ @Override public DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam) { DataSourceUtils.checkDatasourceParam(dataSourceParam); @@ -175,6 +188,12 @@ private boolean checkName(String name) { return queryDataSource != null && !queryDataSource.isEmpty(); } + /** + * updateWorkflowInstance datasource + * + * @param id datasource id + * @return data source detail + */ @Override public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { DataSource dataSource = dataSourceMapper.selectById(id); @@ -183,7 +202,7 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, ApiFuncIdentificationConstant.DATASOURCE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } @@ -199,6 +218,15 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { return baseDataSourceParamDTO; } + /** + * query datasource list by keyword + * + * @param loginUser login user + * @param searchVal search value + * @param pageNo page number + * @param pageSize page size + * @return data source list page + */ @Override public PageInfo queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { @@ -244,6 +272,13 @@ private String getHiddenPassword() { return Constants.XXXXXX; } + /** + * query data resource list + * + * @param loginUser login user + * @param type data source type + * @return data source list page + */ @Override public List queryDataSourceList(User loginUser, Integer type) { @@ -263,6 +298,12 @@ public List queryDataSourceList(User loginUser, Integer type) { return datasourceList; } + /** + * verify datasource exists + * + * @param name datasource name + * @return true if data datasource not exists, otherwise return false + */ @Override public void verifyDataSourceName(String name) { List dataSourceList = dataSourceMapper.queryDataSourceByName(name); @@ -271,6 +312,14 @@ public void verifyDataSourceName(String name) { } } + /** + * check connection + * + * @param type data source type + * @param connectionParam connectionParam + * @return true if connect successfully, otherwise false + * @return true if connect successfully, otherwise false + */ @Override public void checkConnection(DbType type, ConnectionParam connectionParam) { DataSourceProcessor sshDataSourceProcessor = DataSourceUtils.getDatasourceProcessor(type); @@ -298,25 +347,36 @@ public void connectionTest(User loginUser, int id) { DataSourceUtils.buildConnectionParams(dataSource.getType(), dataSource.getConnectionParams())); } + /** + * delete datasource + * + * @param loginUser login user + * @param datasourceId data source id + * @return delete result code + */ @Override @Transactional public void delete(User loginUser, int datasourceId) { // query datasource by id DataSource dataSource = dataSourceMapper.selectById(datasourceId); - if (dataSource == null) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - - if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, DATASOURCE_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - dataSourceMapper.deleteById(datasourceId); datasourceUserMapper.deleteByDatasourceId(datasourceId); } + /** + * unauthorized datasource + * + * @param loginUser login user + * @param userId user id + * @return unauthed data source result code + */ @Override public List unAuthDatasource(User loginUser, Integer userId) { List datasourceList; @@ -344,12 +404,73 @@ public List unAuthDatasource(User loginUser, Integer userId) { return resultList; } + /** + * authorized datasource + * + * @param loginUser login user + * @param userId user id + * @return authorized result code + */ @Override public List authedDatasource(User loginUser, Integer userId) { List authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId); return authedDatasourceList; } + @Override + public List 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 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 options = getParamsOptions(tableList); + return options; + } + @Override public List getTables(User loginUser, Integer datasourceId, String database) { DataSource dataSource = dataSourceMapper.selectById(datasourceId); @@ -476,60 +597,6 @@ public List getTableColumns(User loginUser, Integer datasourceId, return options; } - @Override - public List 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 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 options = getParamsOptions(tableList); - return options; - } - private List getParamsOptions(List columnList) { List options = null; if (CollectionUtils.isNotEmpty(columnList)) { diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 379f9b0cfdd1..586a35c88cb6 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -224,7 +224,6 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source -GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state GET_DATASOURCE_DATABASE_NOTES=get datasource databases GET_DATASOURCE_TABLES_NOTES=get datasource tables GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index 086dbc269496..075c8ca561b2 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -257,7 +257,6 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source -GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state GET_DATASOURCE_DATABASE_NOTES=get datasource databases GET_DATASOURCE_TABLES_NOTES=get datasource tables GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index f6a1bd01e473..b85daa0a119d 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -256,7 +256,6 @@ DELETE_DATA_SOURCE_NOTES=\u5220\u9664\u6570\u636E\u6E90 VERIFY_DATA_SOURCE_NOTES=\u9A8C\u8BC1\u6570\u636E\u6E90 UNAUTHORIZED_DATA_SOURCE_NOTES=\u672A\u6388\u6743\u7684\u6570\u636E\u6E90 AUTHORIZED_DATA_SOURCE_NOTES=\u6388\u6743\u7684\u6570\u636E\u6E90 -GET_KERBEROS_STARTUP_STATE=\u83B7\u53D6Kerberos\u542F\u52A8\u72B6\u6001 GET_DATASOURCE_DATABASE_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u5E93\u5217\u8868 GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D From 4440081e56d538ab15eb878c81f5f733e312eb3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Tue, 17 Mar 2026 18:41:59 +0800 Subject: [PATCH 4/7] Add user permission validation logic --- .../api/controller/DataSourceController.java | 89 ++++++++++++------- .../api/service/DataSourceService.java | 12 ++- .../service/impl/DataSourceServiceImpl.java | 38 ++++++-- .../main/resources/i18n/messages.properties | 6 +- .../resources/i18n/messages_en_US.properties | 6 +- .../resources/i18n/messages_zh_CN.properties | 6 +- .../api/service/DataSourceServiceTest.java | 9 +- 7 files changed, 115 insertions(+), 51 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 6dbdea39aa7c..1d1abb2a4657 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -202,7 +202,6 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque /** * connect datasource * - * @param loginUser login user * @param jsonStr datasource param * example: {"type":"MYSQL","name":"txx","note":"","host":"localhost","port":3306,"principal":"","javaSecurityKrb5Conf":"","loginUserKeytabUsername":"","loginUserKeytabPath":"","userName":"root","password":"xxx","database":"ds","connectType":"","other":{"serverTimezone":"GMT-8"},"id":2} * @return connect result code @@ -211,8 +210,7 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @PostMapping(value = "/connect") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECT_DATASOURCE_FAILURE) - public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { + public Result connectDataSource(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); DataSourceUtils.checkDatasourceParam(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); @@ -223,7 +221,6 @@ public Result connectDataSource(@Parameter(hidden = true) @RequestAttri /** * connection test * - * @param loginUser login user * @param id data source id * @return connect result code */ @@ -234,8 +231,7 @@ public Result connectDataSource(@Parameter(hidden = true) @RequestAttri @GetMapping(value = "/{id}/connect-test") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECTION_TEST_FAILURE) - public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("id") int id) { + public Result connectionTest(@PathVariable("id") int id) { dataSourceService.connectionTest(id); return Result.success(true); } @@ -264,7 +260,6 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib /** * verify datasource name * - * @param loginUser login user * @param name data source name * @return true if data source name not exists, otherwise return false */ @@ -275,8 +270,7 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/verify-name") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE) - public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "name") String name) { + public Result verifyDataSourceName(@RequestParam(value = "name") String name) { dataSourceService.verifyDataSourceName(name); return Result.success(true); } @@ -323,20 +317,47 @@ public Result authedDatasource(@Parameter(hidden = true) @RequestAttribu } /** - * get user info + * Checks the startup status of Kerberos authentication. * - * @param loginUser login user - * @return user info data + * @return a boolean indicating whether Kerberos is currently active */ - @Operation(summary = "getKerberosStartupState", description = "GET_USER_INFO_NOTES") + @Operation(summary = "getKerberosStartupState", description = "GET_KERBEROS_STARTUP_STATE") @GetMapping(value = "/kerberos-startup-state") @ResponseStatus(HttpStatus.OK) @ApiException(KERBEROS_STARTUP_STATE) - public Result getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result getKerberosStartupState() { // if upload resource is HDFS and kerberos startup is true , else false return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState()); } + /** + * Retrieves the list of databases available in a specific data source. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @return a list of database names/options accessible to the user + */ + @Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES") + @Parameters({ + @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")) + }) + @GetMapping(value = "/databases") + @ResponseStatus(HttpStatus.OK) + @ApiException(GET_DATASOURCE_DATABASES_ERROR) + public Result getDatabases(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId) { + List options = dataSourceService.getDatabases(loginUser, datasourceId); + return Result.success(options); + } + + /** + * Retrieves the list of tables within a specific database of a data source. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @param database the name of the database to query + * @return a list of table names/options accessible to the user + */ @Operation(summary = "tables", description = "GET_DATASOURCE_TABLES_NOTES") @Parameters({ @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")), @@ -345,37 +366,37 @@ public Result getKerberosStartupState(@Parameter(hidden = true) @Request @GetMapping(value = "/tables") @ResponseStatus(HttpStatus.OK) @ApiException(GET_DATASOURCE_TABLES_ERROR) - public Result getTables(@RequestParam("datasourceId") Integer datasourceId, - @RequestParam(value = "database") String database) { - List options = dataSourceService.getTables(datasourceId, database); + public Result getTables(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId, + @RequestParam("database") String database) { + List options = dataSourceService.getTables(loginUser, datasourceId, database); return Result.success(options); } + /** + * Retrieves the column details (schema) for a specific table. + * + * @param loginUser the current logged-in user (injected from session) + * @param datasourceId the unique identifier of the data source + * @param database the name of the database containing the table + * @param tableName the name of the table to query columns for + * @return a list of column definitions (name, type, etc.) for the specified table + */ @Operation(summary = "tableColumns", description = "GET_DATASOURCE_TABLE_COLUMNS_NOTES") @Parameters({ @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")), - @Parameter(name = "tableName", description = "TABLE_NAME", required = true, schema = @Schema(implementation = String.class, example = "test")), - @Parameter(name = "database", description = "DATABASE", required = true, schema = @Schema(implementation = String.class, example = "test")) + @Parameter(name = "database", description = "DATABASE", required = true, schema = @Schema(implementation = String.class, example = "test")), + @Parameter(name = "tableName", description = "TABLE_NAME", required = true, schema = @Schema(implementation = String.class, example = "test")) }) @GetMapping(value = "/tableColumns") @ResponseStatus(HttpStatus.OK) @ApiException(GET_DATASOURCE_TABLE_COLUMNS_ERROR) - public Result getTableColumns(@RequestParam("datasourceId") Integer datasourceId, - @RequestParam("tableName") String tableName, - @RequestParam(value = "database") String database) { - List options = dataSourceService.getTableColumns(datasourceId, database, tableName); + public Result getTableColumns(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("datasourceId") Integer datasourceId, + @RequestParam("database") String database, + @RequestParam("tableName") String tableName) { + List options = dataSourceService.getTableColumns(loginUser, datasourceId, database, tableName); return Result.success(options); } - @Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES") - @Parameters({ - @Parameter(name = "datasourceId", description = "DATA_SOURCE_ID", required = true, schema = @Schema(implementation = int.class, example = "1")) - }) - @GetMapping(value = "/databases") - @ResponseStatus(HttpStatus.OK) - @ApiException(GET_DATASOURCE_DATABASES_ERROR) - public Result getDatabases(@RequestParam("datasourceId") Integer datasourceId) { - List options = dataSourceService.getDatabases(datasourceId); - return Result.success(options); - } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 1cdf76faa968..0fa50edd75cd 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -132,25 +132,31 @@ public interface DataSourceService { /** * get tables + * + * @param loginUser login user * @param datasourceId * @param database * @return */ - List getTables(Integer datasourceId, String database); + List getTables(User loginUser, Integer datasourceId, String database); /** * get table columns + * + * @param loginUser login user * @param datasourceId * @param database * @param tableName * @return */ - List getTableColumns(Integer datasourceId, String database, String tableName); + List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); /** * get databases + * + * @param loginUser login user * @param datasourceId * @return */ - List getDatabases(Integer datasourceId); + List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index 7f298a7d841d..fc00db80603a 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -202,7 +202,7 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.DATASOURCE, ApiFuncIdentificationConstant.DATASOURCE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } @@ -358,13 +358,16 @@ public void connectionTest(int id) { public void delete(User loginUser, int datasourceId) { // query datasource by id DataSource dataSource = dataSourceMapper.selectById(datasourceId); + if (dataSource == null) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, + + if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, DATASOURCE_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } + dataSourceMapper.deleteById(datasourceId); datasourceUserMapper.deleteByDatasourceId(datasourceId); } @@ -417,9 +420,18 @@ public List authedDatasource(User loginUser, Integer userId) { } @Override - public List getTables(Integer datasourceId, String database) { + public List 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 tableList; BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( @@ -477,8 +489,19 @@ public List getTables(Integer datasourceId, String database) { } @Override - public List getTableColumns(Integer datasourceId, String database, String tableName) { + public List 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)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); + } + BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( dataSource.getType(), @@ -523,7 +546,7 @@ public List getTableColumns(Integer datasourceId, String database } @Override - public List getDatabases(Integer datasourceId) { + public List getDatabases(User loginUser, Integer datasourceId) { DataSource dataSource = dataSourceMapper.selectById(datasourceId); @@ -531,6 +554,11 @@ public List getDatabases(Integer datasourceId) { 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 tableList; BaseConnectionParam connectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams( diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 7f56c1eb8d39..379f9b0cfdd1 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -224,6 +224,10 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source +GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state +GET_DATASOURCE_DATABASE_NOTES=get datasource databases +GET_DATASOURCE_TABLES_NOTES=get datasource tables +GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns DELETE_SCHEDULE_NOTES=delete schedule by id QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES= query authorized and user created project @@ -234,8 +238,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=query workflow definition versions SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=switch workflow definition version VERSION=version STATE=state -GET_DATASOURCE_TABLES_NOTES=get datasource table -GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns TABLE_NAME=table name AUDIT_LOG_TAG=audit log related operation TASK_DEFINITION_TAG=task definition related operation diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index 71e5dc091d83..086dbc269496 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -257,6 +257,10 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source +GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state +GET_DATASOURCE_DATABASE_NOTES=get datasource databases +GET_DATASOURCE_TABLES_NOTES=get datasource tables +GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns DELETE_SCHEDULE_NOTES=delete schedule by id QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES=query authorized and user created project @@ -267,8 +271,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=query process definition versions SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=switch process definition version VERSION=version TASK_GROUP_QUEUE_PRIORITY=task group queue priority -GET_DATASOURCE_TABLES_NOTES=get datasource table -GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns TABLE_NAME=table name QUERY_AUDIT_LOG=query audit log AUDIT_LOG_TAG=audit log related operation diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index 243c41c0685c..f6a1bd01e473 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -256,6 +256,10 @@ DELETE_DATA_SOURCE_NOTES=\u5220\u9664\u6570\u636E\u6E90 VERIFY_DATA_SOURCE_NOTES=\u9A8C\u8BC1\u6570\u636E\u6E90 UNAUTHORIZED_DATA_SOURCE_NOTES=\u672A\u6388\u6743\u7684\u6570\u636E\u6E90 AUTHORIZED_DATA_SOURCE_NOTES=\u6388\u6743\u7684\u6570\u636E\u6E90 +GET_KERBEROS_STARTUP_STATE=\u83B7\u53D6Kerberos\u542F\u52A8\u72B6\u6001 +GET_DATASOURCE_DATABASE_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u5E93\u5217\u8868 +GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 +GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D DELETE_SCHEDULE_NOTES=\u6839\u636E\u5B9A\u65F6id\u5220\u9664\u5B9A\u65F6\u6570\u636E QUERY_ALERT_GROUP_LIST_PAGING_NOTES=\u5206\u9875\u67E5\u8BE2\u544A\u8B66\u7EC4\u5217\u8868 QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES=\u67E5\u8BE2\u6388\u6743\u548C\u7528\u6237\u521B\u5EFA\u7684\u9879\u76EE @@ -266,8 +270,6 @@ QUERY_WORKFLOW_DEFINITION_VERSIONS_NOTES=\u67E5\u8BE2\u6D41\u7A0B\u5386\u53F2\u7 SWITCH_WORKFLOW_DEFINITION_VERSION_NOTES=\u5207\u6362\u6D41\u7A0B\u7248\u672C VERSION=\u7248\u672C\u53F7 TASK_GROUP_QUEUE_PRIORITY=\u4EFB\u52A1\u961F\u5217\u4F18\u5148\u7EA7 -GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 -GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D TABLE_NAME=\u8868\u540D QUERY_AUDIT_LOG=\u67E5\u8BE2\u5BA1\u8BA1\u65E5\u5FD7 AUDIT_LOG_TAG=\u5BA1\u8BA1\u65E5\u5FD7\u6267\u884C\u76F8\u5173\u64CD\u4F5C diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index b4e7aae4b8d3..50f61255d41c 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -605,13 +605,15 @@ public void testCheckConnection() throws Exception { @Test public void testGetDatabases() throws SQLException { + User loginUser = getAdminUser(); + DataSource dataSource = getOracleDataSource(); int datasourceId = 1; dataSource.setId(datasourceId); when(dataSourceMapper.selectById(datasourceId)).thenReturn(null); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.QUERY_DATASOURCE_ERROR.getMsg())); } @@ -623,9 +625,10 @@ public void testGetDatabases() throws SQLException { dataSourceUtils.when(() -> DataSourceUtils.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection); dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any())) .thenReturn(connectionParam); + passResourcePermissionCheckService(); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.GET_DATASOURCE_TABLES_ERROR.getMsg())); } @@ -634,7 +637,7 @@ public void testGetDatabases() throws SQLException { .thenReturn(null); try { - dataSourceService.getDatabases(datasourceId); + dataSourceService.getDatabases(loginUser, datasourceId); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains(Status.DATASOURCE_CONNECT_FAILED.getMsg())); } From 6c36bea17803c900e0e91ad6664de88981477f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Wed, 18 Mar 2026 11:26:08 +0800 Subject: [PATCH 5/7] Correct method comments --- .../api/controller/DataSourceController.java | 8 +- .../api/service/DataSourceService.java | 95 +++++++++++-------- .../service/impl/DataSourceServiceImpl.java | 85 ++--------------- .../api/service/DataSourceServiceTest.java | 9 +- 4 files changed, 74 insertions(+), 123 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 1d1abb2a4657..792b412f3074 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -35,6 +35,7 @@ import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DataSourceService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -231,8 +232,9 @@ public Result connectDataSource(@io.swagger.v3.oas.annotations.paramete @GetMapping(value = "/{id}/connect-test") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECTION_TEST_FAILURE) - public Result connectionTest(@PathVariable("id") int id) { - dataSourceService.connectionTest(id); + public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("id") int id) { + dataSourceService.connectionTest(loginUser, id); return Result.success(true); } @@ -261,7 +263,7 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib * verify datasource name * * @param name data source name - * @return true if data source name not exists, otherwise return false + * @return true if data source name not exists, otherwise throw a {@link ServiceException} */ @Operation(summary = "verifyDataSourceName", description = "VERIFY_DATA_SOURCE_NOTES") @Parameters({ diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 0fa50edd75cd..898a54b11798 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.api.service; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.dao.entity.DataSource; import org.apache.dolphinscheduler.dao.entity.User; @@ -33,28 +34,31 @@ public interface DataSourceService { /** - * create data source + * create a new data source. * * @param loginUser login user - * @param datasourceParam datasource parameter - * @return create result code + * @param datasourceParam data source configuration DTO + * @return created {@link DataSource} entity (sensitive fields masked) + * @throws ServiceException if permission denied, security check fails, or connection test fails */ DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); /** - * updateWorkflowInstance datasource + * update datasource * * @param loginUser login user * @param dataSourceParam data source params - * @return update result code + * @return updated {@link DataSource} entity (sensitive fields masked) + * @throws ServiceException if permission denied, security check fails, or connection test fails */ DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam); /** - * updateWorkflowInstance datasource + * query datasource * + * @param loginUser login user * @param id datasource id - * @return data source detail + * @return a {@link DataSource} entity (sensitive fields masked) */ BaseDataSourceParamDTO queryDataSource(int id, User loginUser); @@ -79,36 +83,44 @@ public interface DataSourceService { List queryDataSourceList(User loginUser, Integer type); /** - * verify datasource exists + * verify whether a data source name already exists. + *

+ * If the name already exists, a {@link ServiceException} is thrown. + * If the name is available (does not exist), the method completes successfully without returning a value. * - * @param name datasource name - * @return true if data datasource not exists, otherwise return false + * @param name the data source name to verify + * @throws ServiceException if the data source name already exists (Status.DATASOURCE_EXIST) */ void verifyDataSourceName(String name); /** - * check connection + * Checks the connectivity of a data source based on the provided type and parameters. + *

+ * This method attempts to establish a connection. + * - If the connection is successful, the method returns normally (void). + * - If the connection fails, a {@link ServiceException} is thrown. * - * @param type data source type - * @param parameter data source parameters - * @return true if connect successfully, otherwise false + * @param type the type of the data source (e.g., MYSQL, POSTGRESQL) + * @param connectionParam the connection parameters containing host, port, credentials, etc. + * @throws ServiceException if the connection test fails (Status.CONNECTION_TEST_FAILURE) */ - void checkConnection(DbType type, ConnectionParam parameter); + void checkConnection(DbType type, ConnectionParam connectionParam); /** - * test connection + * Tests the connectivity of a specific data source. * - * @param id datasource id - * @return connect result code + * @param loginUser the current logged-in user (required for permission check) + * @param id the unique identifier of the data source to test + * @throws ServiceException if the resource doesn't exist, permission is denied, or connection fails */ - void connectionTest(int id); + void connectionTest(User loginUser, int id); /** - * delete datasource + * delete a data source by ID. * - * @param loginUser login user - * @param datasourceId data source id - * @return delete result code + * @param loginUser the current logged-in user + * @param datasourceId the unique identifier of the data source to delete + * @throws ServiceException if checks fail or deletion encounters an error */ void delete(User loginUser, int datasourceId); @@ -117,7 +129,7 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return unauthed data source result code + * @return a list of {@link DataSource} objects that are available to be authorized to the target user */ List unAuthDatasource(User loginUser, Integer userId); @@ -126,37 +138,40 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return authorized result code + * @return a list of {@link DataSource} objects that are authorized to the target user */ List authedDatasource(User loginUser, Integer userId); /** - * get tables + * Retrieves the list of tables from a specific database within a data source. * - * @param loginUser login user - * @param datasourceId - * @param database - * @return + * @param loginUser the current logged-in user (required for permission check) + * @param datasourceId the unique identifier of the data source + * @param database the specific database/schema name to query (nullable for some DB types like SQLite) + * @return a list of {@link ParamsOptions} containing table names and optional metadata (e.g., comments) + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getTables(User loginUser, Integer datasourceId, String database); /** - * get table columns + * Retrieves the list of columns for a specific table in a data source. * - * @param loginUser login user - * @param datasourceId - * @param database - * @param tableName - * @return + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @param database database/schema name + * @param tableName table name to query + * @return list of {@link ParamsOptions} representing column names and types + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); /** - * get databases + * Retrieves the list of databases (or schemas) available in a specific data source. * - * @param loginUser login user - * @param datasourceId - * @return + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @return list of {@link ParamsOptions} representing database/schema names + * @throws ServiceException if permission denied, resource not found, or connection fails */ List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index fc00db80603a..fbe01b2bc213 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -86,13 +86,6 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource private static final String TABLE_NAME = "TABLE_NAME"; private static final String COLUMN_NAME = "COLUMN_NAME"; - /** - * create data source - * - * @param loginUser login user - * @param datasourceParam datasource parameters - * @return create result code - */ @Override public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { DataSourceUtils.checkDatasourceParam(datasourceParam); @@ -129,12 +122,6 @@ public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO dataso } } - /** - * updateWorkflowInstance datasource - * - * @param loginUser login user - * @return update result code - */ @Override public DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam) { DataSourceUtils.checkDatasourceParam(dataSourceParam); @@ -188,12 +175,6 @@ private boolean checkName(String name) { return queryDataSource != null && !queryDataSource.isEmpty(); } - /** - * updateWorkflowInstance datasource - * - * @param id datasource id - * @return data source detail - */ @Override public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { DataSource dataSource = dataSourceMapper.selectById(id); @@ -218,15 +199,6 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { return baseDataSourceParamDTO; } - /** - * query datasource list by keyword - * - * @param loginUser login user - * @param searchVal search value - * @param pageNo page number - * @param pageSize page size - * @return data source list page - */ @Override public PageInfo queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { @@ -272,13 +244,6 @@ private String getHiddenPassword() { return Constants.XXXXXX; } - /** - * query data resource list - * - * @param loginUser login user - * @param type data source type - * @return data source list page - */ @Override public List queryDataSourceList(User loginUser, Integer type) { @@ -298,12 +263,6 @@ public List queryDataSourceList(User loginUser, Integer type) { return datasourceList; } - /** - * verify datasource exists - * - * @param name datasource name - * @return true if data datasource not exists, otherwise return false - */ @Override public void verifyDataSourceName(String name) { List dataSourceList = dataSourceMapper.queryDataSourceByName(name); @@ -312,14 +271,6 @@ public void verifyDataSourceName(String name) { } } - /** - * check connection - * - * @param type data source type - * @param connectionParam connectionParam - * @return true if connect successfully, otherwise false - * @return true if connect successfully, otherwise false - */ @Override public void checkConnection(DbType type, ConnectionParam connectionParam) { DataSourceProcessor sshDataSourceProcessor = DataSourceUtils.getDatasourceProcessor(type); @@ -330,29 +281,23 @@ public void checkConnection(DbType type, ConnectionParam connectionParam) { 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())); } - /** - * delete datasource - * - * @param loginUser login user - * @param datasourceId data source id - * @return delete result code - */ @Override @Transactional public void delete(User loginUser, int datasourceId) { @@ -372,13 +317,6 @@ public void delete(User loginUser, int datasourceId) { datasourceUserMapper.deleteByDatasourceId(datasourceId); } - /** - * unauthorized datasource - * - * @param loginUser login user - * @param userId user id - * @return unauthed data source result code - */ @Override public List unAuthDatasource(User loginUser, Integer userId) { List datasourceList; @@ -406,13 +344,6 @@ public List unAuthDatasource(User loginUser, Integer userId) { return resultList; } - /** - * authorized datasource - * - * @param loginUser login user - * @param userId user id - * @return authorized result code - */ @Override public List authedDatasource(User loginUser, Integer userId) { List authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index 50f61255d41c..1824b509235b 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -268,9 +268,11 @@ public void testQueryDataSourceListPaging() { @Test public void testConnectionTest() { + User loginUser = getAdminUser(); int dataSourceId = -1; when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); - assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, () -> dataSourceService.connectionTest(dataSourceId)); + assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, + () -> dataSourceService.connectionTest(loginUser, dataSourceId)); try ( MockedStatic ignored = @@ -281,11 +283,12 @@ public void testConnectionTest() { when(DataSourceUtils.getDatasourceProcessor(Mockito.any())).thenReturn(dataSourceProcessor); when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(true); - assertDoesNotThrow(() -> dataSourceService.connectionTest(dataSource.getId())); + passResourcePermissionCheckService(); + assertDoesNotThrow(() -> dataSourceService.connectionTest(loginUser, dataSource.getId())); when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(false); assertThrowsServiceException(Status.CONNECTION_TEST_FAILURE, - () -> dataSourceService.connectionTest(dataSource.getId())); + () -> dataSourceService.connectionTest(loginUser, dataSource.getId())); } } From 7b61643e9df977c00792ed9295f50f5dd699230f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Thu, 19 Mar 2026 14:34:35 +0800 Subject: [PATCH 6/7] Revert and optimize method comment modifications. --- .../api/controller/DataSourceController.java | 23 ++- .../api/service/DataSourceService.java | 71 +++---- .../service/impl/DataSourceServiceImpl.java | 185 ++++++++++++------ .../main/resources/i18n/messages.properties | 1 - .../resources/i18n/messages_en_US.properties | 1 - .../resources/i18n/messages_zh_CN.properties | 1 - 6 files changed, 171 insertions(+), 111 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 792b412f3074..03b1898495f1 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -35,7 +35,6 @@ import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; -import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DataSourceService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -203,6 +202,7 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque /** * connect datasource * + * @param loginUser login user * @param jsonStr datasource param * example: {"type":"MYSQL","name":"txx","note":"","host":"localhost","port":3306,"principal":"","javaSecurityKrb5Conf":"","loginUserKeytabUsername":"","loginUserKeytabPath":"","userName":"root","password":"xxx","database":"ds","connectType":"","other":{"serverTimezone":"GMT-8"},"id":2} * @return connect result code @@ -211,7 +211,8 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @PostMapping(value = "/connect") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECT_DATASOURCE_FAILURE) - public Result connectDataSource(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { + public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); DataSourceUtils.checkDatasourceParam(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); @@ -222,8 +223,9 @@ public Result connectDataSource(@io.swagger.v3.oas.annotations.paramete /** * connection test * + * @param loginUser login user * @param id data source id - * @return connect result code + * @return A Result object containing true if the connection is successful, false otherwise. */ @Operation(summary = "connectionTest", description = "CONNECT_DATA_SOURCE_TEST_NOTES") @Parameters({ @@ -262,8 +264,9 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib /** * verify datasource name * + * @param loginUser login user * @param name data source name - * @return true if data source name not exists, otherwise throw a {@link ServiceException} + * @return true if data source name not exists, otherwise return false */ @Operation(summary = "verifyDataSourceName", description = "VERIFY_DATA_SOURCE_NOTES") @Parameters({ @@ -272,7 +275,8 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/verify-name") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE) - public Result verifyDataSourceName(@RequestParam(value = "name") String name) { + public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "name") String name) { dataSourceService.verifyDataSourceName(name); return Result.success(true); } @@ -319,15 +323,16 @@ public Result authedDatasource(@Parameter(hidden = true) @RequestAttribu } /** - * Checks the startup status of Kerberos authentication. + * get user info * - * @return a boolean indicating whether Kerberos is currently active + * @param loginUser login user + * @return user info data */ - @Operation(summary = "getKerberosStartupState", description = "GET_KERBEROS_STARTUP_STATE") + @Operation(summary = "getKerberosStartupState", description = "GET_USER_INFO_NOTES") @GetMapping(value = "/kerberos-startup-state") @ResponseStatus(HttpStatus.OK) @ApiException(KERBEROS_STARTUP_STATE) - public Result getKerberosStartupState() { + public Result getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { // if upload resource is HDFS and kerberos startup is true , else false return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState()); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 898a54b11798..68031142a417 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -34,31 +34,28 @@ public interface DataSourceService { /** - * create a new data source. + * create data source * * @param loginUser login user - * @param datasourceParam data source configuration DTO - * @return created {@link DataSource} entity (sensitive fields masked) - * @throws ServiceException if permission denied, security check fails, or connection test fails + * @param datasourceParam datasource parameter + * @return create result code */ DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); /** - * update datasource + * updateWorkflowInstance datasource * * @param loginUser login user * @param dataSourceParam data source params - * @return updated {@link DataSource} entity (sensitive fields masked) - * @throws ServiceException if permission denied, security check fails, or connection test fails + * @return update result code */ DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam); /** - * query datasource + * updateWorkflowInstance datasource * - * @param loginUser login user * @param id datasource id - * @return a {@link DataSource} entity (sensitive fields masked) + * @return data source detail */ BaseDataSourceParamDTO queryDataSource(int id, User loginUser); @@ -83,28 +80,21 @@ public interface DataSourceService { List queryDataSourceList(User loginUser, Integer type); /** - * verify whether a data source name already exists. - *

- * If the name already exists, a {@link ServiceException} is thrown. - * If the name is available (does not exist), the method completes successfully without returning a value. + * verify datasource exists * - * @param name the data source name to verify - * @throws ServiceException if the data source name already exists (Status.DATASOURCE_EXIST) + * @param name datasource name + * @return true if data datasource not exists, otherwise return false */ void verifyDataSourceName(String name); /** - * Checks the connectivity of a data source based on the provided type and parameters. - *

- * This method attempts to establish a connection. - * - If the connection is successful, the method returns normally (void). - * - If the connection fails, a {@link ServiceException} is thrown. + * check connection * - * @param type the type of the data source (e.g., MYSQL, POSTGRESQL) - * @param connectionParam the connection parameters containing host, port, credentials, etc. - * @throws ServiceException if the connection test fails (Status.CONNECTION_TEST_FAILURE) + * @param type data source type + * @param parameter data source parameters + * @return true if connect successfully, otherwise false */ - void checkConnection(DbType type, ConnectionParam connectionParam); + void checkConnection(DbType type, ConnectionParam parameter); /** * Tests the connectivity of a specific data source. @@ -116,11 +106,11 @@ public interface DataSourceService { void connectionTest(User loginUser, int id); /** - * delete a data source by ID. + * delete datasource * - * @param loginUser the current logged-in user - * @param datasourceId the unique identifier of the data source to delete - * @throws ServiceException if checks fail or deletion encounters an error + * @param loginUser login user + * @param datasourceId data source id + * @return delete result code */ void delete(User loginUser, int datasourceId); @@ -129,7 +119,7 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return a list of {@link DataSource} objects that are available to be authorized to the target user + * @return unauthed data source result code */ List unAuthDatasource(User loginUser, Integer userId); @@ -138,10 +128,20 @@ public interface DataSourceService { * * @param loginUser login user * @param userId user id - * @return a list of {@link DataSource} objects that are authorized to the target user + * @return authorized result code */ List authedDatasource(User loginUser, Integer userId); + /** + * Retrieves the list of databases (or schemas) available in a specific data source. + * + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @return list of {@link ParamsOptions} representing database/schema names + * @throws ServiceException if permission denied, resource not found, or connection fails + */ + List getDatabases(User loginUser, Integer datasourceId); + /** * Retrieves the list of tables from a specific database within a data source. * @@ -165,13 +165,4 @@ public interface DataSourceService { */ List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); - /** - * Retrieves the list of databases (or schemas) available in a specific data source. - * - * @param loginUser current logged-in user - * @param datasourceId ID of the data source - * @return list of {@link ParamsOptions} representing database/schema names - * @throws ServiceException if permission denied, resource not found, or connection fails - */ - List getDatabases(User loginUser, Integer datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index fbe01b2bc213..11bbf169604e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -86,6 +86,13 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource private static final String TABLE_NAME = "TABLE_NAME"; private static final String COLUMN_NAME = "COLUMN_NAME"; + /** + * create data source + * + * @param loginUser login user + * @param datasourceParam datasource parameters + * @return create result code + */ @Override public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { DataSourceUtils.checkDatasourceParam(datasourceParam); @@ -122,6 +129,12 @@ public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO dataso } } + /** + * updateWorkflowInstance datasource + * + * @param loginUser login user + * @return update result code + */ @Override public DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam) { DataSourceUtils.checkDatasourceParam(dataSourceParam); @@ -175,6 +188,12 @@ private boolean checkName(String name) { return queryDataSource != null && !queryDataSource.isEmpty(); } + /** + * updateWorkflowInstance datasource + * + * @param id datasource id + * @return data source detail + */ @Override public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { DataSource dataSource = dataSourceMapper.selectById(id); @@ -183,7 +202,7 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, ApiFuncIdentificationConstant.DATASOURCE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } @@ -199,6 +218,15 @@ public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) { return baseDataSourceParamDTO; } + /** + * query datasource list by keyword + * + * @param loginUser login user + * @param searchVal search value + * @param pageNo page number + * @param pageSize page size + * @return data source list page + */ @Override public PageInfo queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { @@ -244,6 +272,13 @@ private String getHiddenPassword() { return Constants.XXXXXX; } + /** + * query data resource list + * + * @param loginUser login user + * @param type data source type + * @return data source list page + */ @Override public List queryDataSourceList(User loginUser, Integer type) { @@ -263,6 +298,12 @@ public List queryDataSourceList(User loginUser, Integer type) { return datasourceList; } + /** + * verify datasource exists + * + * @param name datasource name + * @return true if data datasource not exists, otherwise return false + */ @Override public void verifyDataSourceName(String name) { List dataSourceList = dataSourceMapper.queryDataSourceByName(name); @@ -271,6 +312,14 @@ public void verifyDataSourceName(String name) { } } + /** + * check connection + * + * @param type data source type + * @param connectionParam connectionParam + * @return true if connect successfully, otherwise false + * @return true if connect successfully, otherwise false + */ @Override public void checkConnection(DbType type, ConnectionParam connectionParam) { DataSourceProcessor sshDataSourceProcessor = DataSourceUtils.getDatasourceProcessor(type); @@ -298,25 +347,36 @@ public void connectionTest(User loginUser, int id) { DataSourceUtils.buildConnectionParams(dataSource.getType(), dataSource.getConnectionParams())); } + /** + * delete datasource + * + * @param loginUser login user + * @param datasourceId data source id + * @return delete result code + */ @Override @Transactional public void delete(User loginUser, int datasourceId) { // query datasource by id DataSource dataSource = dataSourceMapper.selectById(datasourceId); - if (dataSource == null) { throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - - if (!canOperatorPermissions(loginUser, new Object[]{datasourceId}, AuthorizationType.DATASOURCE, + if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, DATASOURCE_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - dataSourceMapper.deleteById(datasourceId); datasourceUserMapper.deleteByDatasourceId(datasourceId); } + /** + * unauthorized datasource + * + * @param loginUser login user + * @param userId user id + * @return unauthed data source result code + */ @Override public List unAuthDatasource(User loginUser, Integer userId) { List datasourceList; @@ -344,12 +404,73 @@ public List unAuthDatasource(User loginUser, Integer userId) { return resultList; } + /** + * authorized datasource + * + * @param loginUser login user + * @param userId user id + * @return authorized result code + */ @Override public List authedDatasource(User loginUser, Integer userId) { List authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId); return authedDatasourceList; } + @Override + public List 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 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 options = getParamsOptions(tableList); + return options; + } + @Override public List getTables(User loginUser, Integer datasourceId, String database) { DataSource dataSource = dataSourceMapper.selectById(datasourceId); @@ -476,60 +597,6 @@ public List getTableColumns(User loginUser, Integer datasourceId, return options; } - @Override - public List 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 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 options = getParamsOptions(tableList); - return options; - } - private List getParamsOptions(List columnList) { List options = null; if (CollectionUtils.isNotEmpty(columnList)) { diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 379f9b0cfdd1..586a35c88cb6 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -224,7 +224,6 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source -GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state GET_DATASOURCE_DATABASE_NOTES=get datasource databases GET_DATASOURCE_TABLES_NOTES=get datasource tables GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index 086dbc269496..075c8ca561b2 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -257,7 +257,6 @@ DELETE_DATA_SOURCE_NOTES=delete data source VERIFY_DATA_SOURCE_NOTES=verify data source UNAUTHORIZED_DATA_SOURCE_NOTES=unauthorized data source AUTHORIZED_DATA_SOURCE_NOTES=authorized data source -GET_KERBEROS_STARTUP_STATE=get the Kerberos startup state GET_DATASOURCE_DATABASE_NOTES=get datasource databases GET_DATASOURCE_TABLES_NOTES=get datasource tables GET_DATASOURCE_TABLE_COLUMNS_NOTES=get datasource table columns diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index f6a1bd01e473..b85daa0a119d 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -256,7 +256,6 @@ DELETE_DATA_SOURCE_NOTES=\u5220\u9664\u6570\u636E\u6E90 VERIFY_DATA_SOURCE_NOTES=\u9A8C\u8BC1\u6570\u636E\u6E90 UNAUTHORIZED_DATA_SOURCE_NOTES=\u672A\u6388\u6743\u7684\u6570\u636E\u6E90 AUTHORIZED_DATA_SOURCE_NOTES=\u6388\u6743\u7684\u6570\u636E\u6E90 -GET_KERBEROS_STARTUP_STATE=\u83B7\u53D6Kerberos\u542F\u52A8\u72B6\u6001 GET_DATASOURCE_DATABASE_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u5E93\u5217\u8868 GET_DATASOURCE_TABLES_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u8868 GET_DATASOURCE_TABLE_COLUMNS_NOTES=\u83B7\u53D6\u6570\u636E\u6E90\u8868\u5217\u540D From 8623537ae623988f909b5de0e13e74f2183a9bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B9=89=E8=B6=85?= Date: Fri, 20 Mar 2026 16:36:19 +0800 Subject: [PATCH 7/7] restore the method order --- .../api/service/DataSourceService.java | 20 ++-- .../service/impl/DataSourceServiceImpl.java | 108 +++++++++--------- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 68031142a417..6e42666513f1 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -132,16 +132,6 @@ public interface DataSourceService { */ List authedDatasource(User loginUser, Integer userId); - /** - * Retrieves the list of databases (or schemas) available in a specific data source. - * - * @param loginUser current logged-in user - * @param datasourceId ID of the data source - * @return list of {@link ParamsOptions} representing database/schema names - * @throws ServiceException if permission denied, resource not found, or connection fails - */ - List getDatabases(User loginUser, Integer datasourceId); - /** * Retrieves the list of tables from a specific database within a data source. * @@ -165,4 +155,14 @@ public interface DataSourceService { */ List getTableColumns(User loginUser, Integer datasourceId, String database, String tableName); + /** + * Retrieves the list of databases (or schemas) available in a specific data source. + * + * @param loginUser current logged-in user + * @param datasourceId ID of the data source + * @return list of {@link ParamsOptions} representing database/schema names + * @throws ServiceException if permission denied, resource not found, or connection fails + */ + List getDatabases(User loginUser, Integer datasourceId); + } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index 11bbf169604e..2ac02cb50c1a 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -417,60 +417,6 @@ public List authedDatasource(User loginUser, Integer userId) { return authedDatasourceList; } - @Override - public List 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 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 options = getParamsOptions(tableList); - return options; - } - @Override public List getTables(User loginUser, Integer datasourceId, String database) { DataSource dataSource = dataSourceMapper.selectById(datasourceId); @@ -597,6 +543,60 @@ public List getTableColumns(User loginUser, Integer datasourceId, return options; } + @Override + public List 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 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 options = getParamsOptions(tableList); + return options; + } + private List getParamsOptions(List columnList) { List options = null; if (CollectionUtils.isNotEmpty(columnList)) {