Search in sources :

Example 71 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class ParamFlowRuleController method apiDeleteRule.

@DeleteMapping("/rule/{id}")
@AuthAction(PrivilegeType.DELETE_RULE)
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
    if (id == null) {
        return Result.ofFail(-1, "id cannot be null");
    }
    ParamFlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofSuccess(null);
    }
    try {
        repository.delete(id);
        publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort()).get();
        return Result.ofSuccess(id);
    } catch (ExecutionException ex) {
        logger.error("Error when deleting parameter flow rules", ex.getCause());
        if (isNotSupported(ex.getCause())) {
            return unsupportedVersion();
        } else {
            return Result.ofThrowable(-1, ex.getCause());
        }
    } catch (Throwable throwable) {
        logger.error("Error when deleting parameter flow rules", throwable);
        return Result.ofFail(-1, throwable.getMessage());
    }
}
Also used : ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 72 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class ParamFlowRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<ParamFlowRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id, @RequestBody ParamFlowRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    ParamFlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofFail(-1, "id " + id + " does not exist");
    }
    Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    if (!checkIfSupported(entity.getApp(), entity.getIp(), entity.getPort())) {
        return unsupportedVersion();
    }
    entity.setId(id);
    Date date = new Date();
    entity.setGmtCreate(oldEntity.getGmtCreate());
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get();
        return Result.ofSuccess(entity);
    } catch (ExecutionException ex) {
        logger.error("Error when updating parameter flow rules, id=" + id, ex.getCause());
        if (isNotSupported(ex.getCause())) {
            return unsupportedVersion();
        } else {
            return Result.ofThrowable(-1, ex.getCause());
        }
    } catch (Throwable throwable) {
        logger.error("Error when updating parameter flow rules, id=" + id, throwable);
        return Result.ofFail(-1, throwable.getMessage());
    }
}
Also used : ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 73 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class SystemController method apiUpdateIfNotNull.

@GetMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    SystemRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    if (highestSystemLoad != null) {
        if (highestSystemLoad < 0) {
            return Result.ofFail(-1, "highestSystemLoad must >= 0");
        }
        entity.setHighestSystemLoad(highestSystemLoad);
    }
    if (highestCpuUsage != null) {
        if (highestCpuUsage < 0) {
            return Result.ofFail(-1, "highestCpuUsage must >= 0");
        }
        if (highestCpuUsage > 1) {
            return Result.ofFail(-1, "highestCpuUsage must <= 1");
        }
        entity.setHighestCpuUsage(highestCpuUsage);
    }
    if (avgRt != null) {
        if (avgRt < 0) {
            return Result.ofFail(-1, "avgRt must >= 0");
        }
        entity.setAvgRt(avgRt);
    }
    if (maxThread != null) {
        if (maxThread < 0) {
            return Result.ofFail(-1, "maxThread must >= 0");
        }
        entity.setMaxThread(maxThread);
    }
    if (qps != null) {
        if (qps < 0) {
            return Result.ofFail(-1, "qps must >= 0");
        }
        entity.setQps(qps);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish system rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 74 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class GatewayApiController method addApi.

@PostMapping("/new.json")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
    String app = reqVo.getApp();
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    ApiDefinitionEntity entity = new ApiDefinitionEntity();
    entity.setApp(app.trim());
    String ip = reqVo.getIp();
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    entity.setIp(ip.trim());
    Integer port = reqVo.getPort();
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    entity.setPort(port);
    // API名称
    String apiName = reqVo.getApiName();
    if (StringUtil.isBlank(apiName)) {
        return Result.ofFail(-1, "apiName can't be null or empty");
    }
    entity.setApiName(apiName.trim());
    // 匹配规则列表
    List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
    if (CollectionUtils.isEmpty(predicateItems)) {
        return Result.ofFail(-1, "predicateItems can't empty");
    }
    List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
    for (ApiPredicateItemVo predicateItem : predicateItems) {
        ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
        // 匹配模式
        Integer matchStrategy = predicateItem.getMatchStrategy();
        if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
            return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
        }
        predicateItemEntity.setMatchStrategy(matchStrategy);
        // 匹配串
        String pattern = predicateItem.getPattern();
        if (StringUtil.isBlank(pattern)) {
            return Result.ofFail(-1, "pattern can't be null or empty");
        }
        predicateItemEntity.setPattern(pattern);
        predicateItemEntities.add(predicateItemEntity);
    }
    entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
    // 检查API名称不能重复
    List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
    if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
        return Result.ofFail(-1, "apiName exists: " + apiName);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add gateway api error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishApis(app, ip, port)) {
        logger.warn("publish gateway apis fail after add");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) java.util(java.util) Logger(org.slf4j.Logger) AuthService(com.alibaba.csp.sentinel.dashboard.auth.AuthService) InMemApiDefinitionStore(com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore) SentinelGatewayConstants(com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) StringUtil(com.alibaba.csp.sentinel.util.StringUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) SentinelApiClient(com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient) AddApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) UpdateApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 75 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class FlowControllerV2 method apiAddFlowRule.

@PostMapping("/rule")
@AuthAction(value = 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());
    } catch (Throwable throwable) {
        logger.error("Failed to add 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) 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