Skip to content

Commit

Permalink
feat: add service user and acl
Browse files Browse the repository at this point in the history
  • Loading branch information
318228han committed Apr 1, 2024
1 parent 7f9c83f commit 7ad307d
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bin/
*.iws
*.iml
*.ipr
*.http
out/
!**/src/main/**/out/
!**/src/test/**/out/
Expand Down
38 changes: 19 additions & 19 deletions eventmesh-dashboard-console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@

<!-- health check client -->
<!-- EventMesh SDK -->
<!-- <dependency>-->
<!-- <groupId>org.apache.eventmesh</groupId>-->
<!-- <artifactId>eventmesh-sdk-java</artifactId>-->
<!-- <version>1.10.0-release</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit-dep</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.eventmesh</groupId>-->
<!-- <artifactId>eventmesh-sdk-java</artifactId>-->
<!-- <version>1.10.0-release</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit-dep</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->

<!-- health check client end -->
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class AclEntity extends BaseEntity {
private static final long serialVersionUID = 6057071983428111947L;
private Long id;
private Long clusterId;
private String principal;
private String pattern;
private Integer operation;
private Integer permissionType;
private String host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public interface AclMapper {

@Insert({
"<script>",
" INSERT INTO acl (cluster_Id, principal, operation, permission_Type, host, resource_Type, resource_Name, pattern_Type) VALUES ",
" INSERT INTO acl (cluster_Id, pattern, operation, permission_Type, host, resource_Type, resource_Name, pattern_Type) VALUES ",
" <foreach collection='list' item='c' index='index' separator=','>",
" (#{c.clusterId}, #{c.principal}, #{c.operation}, #{c.permissionType}, #{c.host}, "
" (#{c.clusterId}, #{c.pattern}, #{c.operation}, #{c.permissionType}, #{c.host}, "
+
" #{c.resourceType}, #{c.resourceName}, #{c.patternType})",
" </foreach>",
Expand All @@ -46,8 +46,8 @@ public interface AclMapper {
void batchInsert(List<AclEntity> aclEntities);

@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("INSERT INTO acl (cluster_id, principal, operation, permission_type, host, resource_type, resource_name, pattern_type)"
+ "VALUE (#{clusterId}, #{principal}, #{operation}, #{permissionType}, #{host}, #{resourceType}, #{resourceName}, #{patternType})")
@Insert("INSERT INTO acl (cluster_id, pattern, operation, permission_type, host, resource_type, resource_name, pattern_type)"
+ "VALUE (#{clusterId}, #{pattern}, #{operation}, #{permissionType}, #{host}, #{resourceType}, #{resourceName}, #{patternType})")
void insert(AclEntity aclEntity);

@Update("UPDATE acl SET status=0 WHERE id=#{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_ADDRESS:localhost:3306}/eventmesh_dashboard_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
url: jdbc:mysql://${DB_ADDRESS:localhost:3306}/eventmesh_dashboard?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:123456}
password: ${DB_PASSWORD:password}

initial-size: 1
max-active: 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ DROP TABLE IF EXISTS `service_user`;
CREATE TABLE `service_user`
(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`service_type` int(255) NOT NULL DEFAULT 0 COMMENT '区分不同软件',
`instance_type` int(255) NOT NULL DEFAULT 0 COMMENT '区分不同软件',
`password` varchar(100) NOT NULL DEFAULT '' COMMENT '密码',
`cluster_id` bigint(20) NOT NULL DEFAULT '-1' COMMENT '物理集群ID',
`name` varchar(192) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '名称',
Expand All @@ -189,7 +189,7 @@ CREATE TABLE `acl`
(
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`cluster_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '集群id',
`principal` varchar(192) NOT NULL DEFAULT '' COMMENT 'Service User Pattern',
`pattern` varchar(192) NOT NULL DEFAULT '' COMMENT 'Service User Pattern',
`operation` int(11) NOT NULL DEFAULT '0' COMMENT '操作,',
`permission_type` int(11) NOT NULL DEFAULT '0' COMMENT '权限类型(0:未知,1:任意,2:拒绝,3:允许)',
`host` varchar(192) NOT NULL DEFAULT '' COMMENT '',
Expand All @@ -200,7 +200,7 @@ CREATE TABLE `acl`
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
INDEX `idx_cluster_phy_id_principal_res_name` (`cluster_id`, `principal`, `resource_name`)
INDEX `idx_cluster_phy_id_principal_res_name` (`cluster_id`, `pattern`, `resource_name`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4,
DEFAULT COLLATE = utf8mb4_bin COMMENT ='ACL信息表';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testBatchInsert() {
AclEntity aclEntity = new AclEntity();

aclEntity.setClusterId(1L);
aclEntity.setPrincipal("principal1");
aclEntity.setPattern("pattern1");
aclEntity.setOperation(0);
aclEntity.setPermissionType(1);
aclEntity.setHost("127.0.0.1");
Expand All @@ -47,14 +47,15 @@ public void testBatchInsert() {

aclMapper.batchInsert(aclEntities);
assertEquals(3, aclEntities.size());
// aclEntities.size()返回的是新加的size,是3,而不是acl表中总的size,是5
}

@Test
public void testInsert() {
// AclEntity aclEntity = new AclEntity("", 0, "0", "", "0", "source_name", 1);
AclEntity aclEntity = new AclEntity();
aclEntity.setClusterId(0L);
aclEntity.setPrincipal("pr");
aclEntity.setPattern("pattern1");
aclEntity.setOperation(0);
aclEntity.setPermissionType(1);
aclEntity.setHost("host");
Expand All @@ -77,6 +78,7 @@ public void testDelete() {
aclMapper.deleteById(aclEntity);
assertEquals(4, aclEntity.getId());
// 删除的就是id=4这条数据
// 通过改status为0,实现删除
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.apache.eventmesh.dashboard.console.mapper.serviceuser;

import org.apache.eventmesh.dashboard.console.EventMeshDashboardApplication;
import org.apache.eventmesh.dashboard.console.entity.serviceuser.ServiceUserEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = EventMeshDashboardApplication.class)
@ActiveProfiles("test")
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:use-test-schema.sql", "classpath:eventmesh-dashboard.sql"})
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:serviceuser-test.sql")
class ServiceUserMapperTest {

@Autowired
private ServiceUserMapper serviceUserMapper;

@Test
public void testSelectAll() {
List<ServiceUserEntity> serviceUserEntities = serviceUserMapper.selectAll();
assertEquals(3, serviceUserEntities.size());
}

@Test
public void testSelectById() {
ServiceUserEntity serviceUserEntity1 = new ServiceUserEntity();
serviceUserEntity1.setId(3L);
ServiceUserEntity serviceUserEntity = serviceUserMapper.selectById(serviceUserEntity1);
assertEquals(3, serviceUserEntity.getId());
}

@Test
public void testSelectByName() {
ServiceUserEntity serviceUserEntity1 = new ServiceUserEntity();
serviceUserEntity1.setName("name1");
List<ServiceUserEntity> serviceUserEntities = serviceUserMapper.selectByName(serviceUserEntity1);
assertEquals(1, serviceUserEntities.size());
}

@Test
public void testInsert() {
ServiceUserEntity serviceUserEntity = new ServiceUserEntity(0, "pwd", 13L, "name4", "11", 1);
serviceUserMapper.insert(serviceUserEntity);
assertNotNull(serviceUserEntity);
assertEquals(6, serviceUserEntity.getId());
}

@Test
public void testUpdateNameById() {
ServiceUserEntity serviceUserEntity = new ServiceUserEntity();
serviceUserEntity.setId(3L);
serviceUserEntity.setPassword("123");
serviceUserMapper.updatePasswordById(serviceUserEntity);
serviceUserEntity = serviceUserMapper.selectById(serviceUserEntity);
assertEquals("123", serviceUserEntity.getPassword());
}

}
4 changes: 2 additions & 2 deletions eventmesh-dashboard-console/src/test/resources/acl-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

INSERT INTO eventmesh_dashboard_test.acl (id, cluster_id, principal, operation, permission_type, host, resource_type,
INSERT INTO eventmesh_dashboard_test.acl (id, cluster_id, pattern, operation, permission_type, host, resource_type,
resource_name, pattern_type, create_time, update_time)
VALUES (3, 0, '', 0, '0', '', '0', 'source_name', 1, '2024-03-27 13:22:36', '2024-03-27 14:12:07');
INSERT INTO eventmesh_dashboard_test.acl (id, cluster_id, principal, operation, permission_type, host, resource_type,
INSERT INTO eventmesh_dashboard_test.acl (id, cluster_id, pattern, operation, permission_type, host, resource_type,
resource_name, pattern_type, create_time, update_time)
VALUES (4, 0, '', 0, '0', '', '0', 'source_name1', 1, '2024-03-27 13:22:36', '2024-03-27 14:12:07');

0 comments on commit 7ad307d

Please sign in to comment.