Search in sources :

Example 61 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

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 62 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

the class AuthorityRuleController method apiAddAuthorityRule.

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

Example 63 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

the class SystemController method apiAdd.

@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
    if (checkResult != null) {
        return checkResult;
    }
    int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps, highestCpuUsage);
    if (notNullCount != 1) {
        return Result.ofFail(-1, "only one of [highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage] " + "value must be set > 0, but " + notNullCount + " values get");
    }
    if (null != highestCpuUsage && highestCpuUsage > 1) {
        return Result.ofFail(-1, "highestCpuUsage must between [0.0, 1.0]");
    }
    SystemRuleEntity entity = new SystemRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    // -1 is a fake value
    if (null != highestSystemLoad) {
        entity.setHighestSystemLoad(highestSystemLoad);
    } else {
        entity.setHighestSystemLoad(-1D);
    }
    if (null != highestCpuUsage) {
        entity.setHighestCpuUsage(highestCpuUsage);
    } else {
        entity.setHighestCpuUsage(-1D);
    }
    if (avgRt != null) {
        entity.setAvgRt(avgRt);
    } else {
        entity.setAvgRt(-1L);
    }
    if (maxThread != null) {
        entity.setMaxThread(maxThread);
    } else {
        entity.setMaxThread(-1L);
    }
    if (qps != null) {
        entity.setQps(qps);
    } else {
        entity.setQps(-1D);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Add SystemRule error", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.warn("Publish system rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 64 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

the class GatewayApiController method updateApi.

@PostMapping("/save.json")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<ApiDefinitionEntity> updateApi(@RequestBody UpdateApiReqVo reqVo) {
    String app = reqVo.getApp();
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    Long id = reqVo.getId();
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    ApiDefinitionEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "api does not exist, id=" + id);
    }
    // 匹配规则列表
    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();
        // 匹配模式
        int 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));
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("update gateway api error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishApis(app, entity.getIp(), entity.getPort())) {
        logger.warn("publish gateway apis fail after update");
    }
    return Result.ofSuccess(entity);
}
Also used : ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 65 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

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