Search in sources :

Example 91 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project RuoYi-Cloud-Plus by JavaLionLi.

the class FlowControllerV2 method apiQueryMachineRules.

@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) {
    if (StringUtil.isEmpty(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    try {
        List<FlowRuleEntity> rules = ruleProvider.getRules(app);
        if (rules != null && !rules.isEmpty()) {
            for (FlowRuleEntity entity : rules) {
                entity.setApp(app);
                if (entity.getClusterConfig() != null && entity.getClusterConfig().getFlowId() != null) {
                    entity.setId(entity.getClusterConfig().getFlowId());
                }
            }
        }
        rules = repository.saveAll(rules);
        return Result.ofSuccess(rules);
    } catch (Throwable throwable) {
        logger.error("Error when querying flow rules", throwable);
        return Result.ofThrowable(-1, throwable);
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 92 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project RuoYi-Cloud-Plus by JavaLionLi.

the class FlowControllerV2 method apiDeleteRule.

@DeleteMapping("/rule/{id}")
@AuthAction(PrivilegeType.DELETE_RULE)
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    FlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofSuccess(null);
    }
    try {
        repository.delete(id);
        publishRules(oldEntity.getApp());
    } catch (Exception e) {
        return Result.ofFail(-1, e.getMessage());
    }
    return Result.ofSuccess(id);
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 93 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project RuoYi-Cloud-Plus by JavaLionLi.

the class FlowControllerV2 method apiUpdateFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiUpdateFlowRule(@PathVariable("id") Long id, @RequestBody FlowRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    FlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofFail(-1, "id " + id + " does not exist");
    }
    if (entity == null) {
        return Result.ofFail(-1, "invalid body");
    }
    entity.setApp(oldEntity.getApp());
    entity.setIp(oldEntity.getIp());
    entity.setPort(oldEntity.getPort());
    Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(id);
    Date date = new Date();
    entity.setGmtCreate(oldEntity.getGmtCreate());
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        if (entity == null) {
            return Result.ofFail(-1, "save entity fail");
        }
        publishRules(oldEntity.getApp());
    } catch (Throwable throwable) {
        logger.error("Failed to update flow rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    return Result.ofSuccess(entity);
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) Date(java.util.Date) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 94 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project RuoYi-Cloud-Plus by JavaLionLi.

the class AuthorityRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<AuthorityRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id, @RequestBody AuthorityRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(id);
    Date date = new Date();
    entity.setGmtCreate(null);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        if (entity == null) {
            return Result.ofFail(-1, "Failed to save authority rule");
        }
    } catch (Throwable throwable) {
        logger.error("Failed to save authority rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("Publish authority rules failed after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 95 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project RuoYi-Cloud-Plus by JavaLionLi.

the class FlowControllerV1 method apiAddFlowRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
    Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(null);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    entity.setLimitApp(entity.getLimitApp().trim());
    entity.setResource(entity.getResource().trim());
    try {
        entity = repository.save(entity);
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get(5000, TimeUnit.MILLISECONDS);
        return Result.ofSuccess(entity);
    } catch (Throwable t) {
        Throwable e = t instanceof ExecutionException ? t.getCause() : t;
        logger.error("Failed to add new flow rule, app={}, ip={}", entity.getApp(), entity.getIp(), e);
        return Result.ofFail(-1, e.getMessage());
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)96 Date (java.util.Date)70 FlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity)31 ExecutionException (java.util.concurrent.ExecutionException)25 PostMapping (org.springframework.web.bind.annotation.PostMapping)20 PutMapping (org.springframework.web.bind.annotation.PutMapping)20 ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)15 ApiDefinitionEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity)10 ApiPredicateItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity)10 GatewayFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity)10 GatewayParamFlowItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity)10 AuthorityRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity)10 DegradeRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity)10 SystemRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity)10 ApiPredicateItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo)10 GatewayParamFlowItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)8 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)5