Search in sources :

Example 96 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceControllerV2 method update.

/**
 * Update instance.
 *
 * @param namespaceId namespace id
 * @param serviceName service name
 * @param metadata    service metadata
 * @param cluster     service cluster
 * @param ip          instance ip
 * @param port        instance port
 * @param healthy     instance healthy
 * @param weight      instance weight
 * @param enabled     instance enabled
 * @param ephemeral   instance ephemeral
 * @return 'ok' if success
 * @throws Exception any error during update
 */
@CanDistro
@PutMapping
@Secured(action = ActionTypes.WRITE)
public String update(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster, @RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy, @RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled, @RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    checkWeight(weight);
    final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip).setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled).setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
    if (ephemeral == null) {
        instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
    }
    instanceServiceV2.updateInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 97 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceControllerV2 method batchUpdateInstanceMetadata.

/**
 * Batch update instance's metadata. old key exist = update, old key not exist = add.
 *
 * @param namespaceId     namespace id
 * @param serviceName     service name
 * @param metadata        service metadata
 * @param consistencyType consistencyType
 * @param instances       instances info
 * @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
 * @throws Exception any error during update
 * @since 1.4.0
 */
@CanDistro
@PutMapping(value = "/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchUpdateInstanceMetadata(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = "") String consistencyType, @RequestParam(defaultValue = "") String instances, @RequestParam String metadata) throws Exception {
    List<Instance> targetInstances = parseBatchInstances(instances);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = instanceServiceV2.batchUpdateMetadata(namespaceId, instanceOperationInfo, targetMetadata);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
    for (String ip : operatedInstances) {
        ipArray.add(ip);
    }
    result.replace("updated", ipArray);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 98 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class UserController method createUser.

/**
 * Create a new user.
 *
 * @param username username
 * @param password password
 * @return ok if create succeed
 * @throws IllegalArgumentException if user already exist
 * @since 1.2.0
 */
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE)
@PostMapping
public Object createUser(@RequestParam String username, @RequestParam String password) {
    User user = userDetailsService.getUserFromDatabase(username);
    if (user != null) {
        throw new IllegalArgumentException("user '" + username + "' already exist!");
    }
    userDetailsService.createUser(username, PasswordEncoderUtil.encode(password));
    return RestResultUtils.success("create user ok!");
}
Also used : NacosUser(com.alibaba.nacos.plugin.auth.impl.users.NacosUser) User(com.alibaba.nacos.plugin.auth.impl.persistence.User) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 99 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class ConfigController method deleteConfigs.

/**
 * Execute delete config operation.
 *
 * @return java.lang.Boolean
 * @author klw
 * @Description: delete configuration based on multiple config ids
 * @Date 2019/7/5 10:26
 * @Param [request, response, dataId, group, tenant, tag]
 */
@DeleteMapping(params = "delType=ids")
@Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class)
public RestResult<Boolean> deleteConfigs(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids") List<Long> ids) {
    String clientIp = RequestUtil.getRemoteIp(request);
    final Timestamp time = TimeUtils.getCurrentTime();
    List<ConfigInfo> configInfoList = persistService.removeConfigInfoByIds(ids, clientIp, null);
    if (CollectionUtils.isEmpty(configInfoList)) {
        return RestResultUtils.success(true);
    }
    for (ConfigInfo configInfo : configInfoList) {
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime()));
        ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
    }
    return RestResultUtils.success(true);
}
Also used : ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 100 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class ConfigController method deleteConfig.

/**
 * Synchronously delete all pre-aggregation data under a dataId.
 *
 * @throws NacosException NacosException.
 */
@DeleteMapping
@Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class)
public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse response, @RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, @RequestParam(value = "tag", required = false) String tag) throws NacosException {
    // check tenant
    ParamUtils.checkTenant(tenant);
    ParamUtils.checkParam(dataId, group, "datumId", "rm");
    ParamUtils.checkParam(tag);
    String clientIp = RequestUtil.getRemoteIp(request);
    String srcUser = RequestUtil.getSrcUserName(request);
    if (StringUtils.isBlank(tag)) {
        persistService.removeConfigInfo(dataId, group, tenant, clientIp, srcUser);
    } else {
        persistService.removeConfigInfoTag(dataId, group, tenant, tag, clientIp, srcUser);
    }
    final Timestamp time = TimeUtils.getCurrentTime();
    ConfigTraceService.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
    ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
    return true;
}
Also used : ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Timestamp(java.sql.Timestamp) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Aggregations

Secured (com.alibaba.nacos.auth.annotation.Secured)104 Resource (com.alibaba.nacos.plugin.auth.api.Resource)34 Test (org.junit.Test)32 GetMapping (org.springframework.web.bind.annotation.GetMapping)20 CanDistro (com.alibaba.nacos.naming.web.CanDistro)17 Instance (com.alibaba.nacos.api.naming.pojo.Instance)16 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)15 PostMapping (org.springframework.web.bind.annotation.PostMapping)13 PutMapping (org.springframework.web.bind.annotation.PutMapping)13 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)12 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)10 Timestamp (java.sql.Timestamp)10 NacosException (com.alibaba.nacos.api.exception.NacosException)8 AbstractNamingRequest (com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest)8 Request (com.alibaba.nacos.api.remote.request.Request)8 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)8 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)7 ConfigBatchListenRequest (com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest)6 HashMap (java.util.HashMap)6 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)5