Commit ecb2fa83 by Nepxion

控制台增加权限拦截判断

parent 1c60cef5
package com.nepxion.discovery.console.constant;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
public class ConsoleConstant {
public static final String SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED = "spring.application.discovery.control.enabled";
public static final String SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED = "spring.application.config.rest.control.enabled";
}
\ No newline at end of file
...@@ -11,6 +11,7 @@ package com.nepxion.discovery.console.rest; ...@@ -11,6 +11,7 @@ package com.nepxion.discovery.console.rest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -21,6 +22,7 @@ import org.springframework.http.HttpStatus; ...@@ -21,6 +22,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.console.constant.ConsoleConstant;
import com.nepxion.discovery.console.entity.ResultEntity; import com.nepxion.discovery.console.entity.ResultEntity;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler; import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
...@@ -47,14 +49,20 @@ public abstract class AbstractRestInvoker { ...@@ -47,14 +49,20 @@ public abstract class AbstractRestInvoker {
String serviceId = serviceInstance.getServiceId().toLowerCase(); String serviceId = serviceInstance.getServiceId().toLowerCase();
String host = serviceInstance.getHost(); String host = serviceInstance.getHost();
int port = serviceInstance.getPort(); int port = serviceInstance.getPort();
String url = getUrl(host, port); String url = getUrl(host, port);
String result = doRest(url); String result = null;
try {
checkPermission(serviceInstance);
result = doRest(url);
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) restTemplate.getErrorHandler(); ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause(); result = errorHandler.getCause();
} }
} catch (Exception e) {
result = e.getMessage();
}
ResultEntity resultEntity = new ResultEntity(); ResultEntity resultEntity = new ResultEntity();
resultEntity.setServiceId(serviceId); resultEntity.setServiceId(serviceId);
...@@ -70,9 +78,37 @@ public abstract class AbstractRestInvoker { ...@@ -70,9 +78,37 @@ public abstract class AbstractRestInvoker {
return ResponseEntity.ok().body(resultEntityList); return ResponseEntity.ok().body(resultEntityList);
} }
protected void checkDiscoveryControlPermission(ServiceInstance serviceInstance) {
Map<String, String> metaData = serviceInstance.getMetadata();
String discoveryControlEnabled = metaData.get(ConsoleConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED);
if (StringUtils.isEmpty(discoveryControlEnabled)) {
throw new IllegalArgumentException("No metadata for key=" + ConsoleConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED);
}
if (!Boolean.valueOf(discoveryControlEnabled)) {
throw new IllegalArgumentException("Discovery control is disabled");
}
}
protected void checkConfigRestControlPermission(ServiceInstance serviceInstance) {
Map<String, String> metaData = serviceInstance.getMetadata();
String configRestControlEnabled = metaData.get(ConsoleConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED);
if (StringUtils.isEmpty(configRestControlEnabled)) {
throw new IllegalArgumentException("No metadata for key=" + ConsoleConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED);
}
if (!Boolean.valueOf(configRestControlEnabled)) {
throw new IllegalArgumentException("Config rest control is disabled");
}
}
protected abstract String getInfo(); protected abstract String getInfo();
protected abstract String getUrl(String host, int port); protected abstract String getUrl(String host, int port);
protected abstract String doRest(String url); protected abstract String doRest(String url);
protected abstract void checkPermission(ServiceInstance serviceInstance) throws Exception;
} }
\ No newline at end of file
...@@ -33,4 +33,10 @@ public class ConfigClearRestInvoker extends AbstractRestInvoker { ...@@ -33,4 +33,10 @@ public class ConfigClearRestInvoker extends AbstractRestInvoker {
protected String doRest(String url) { protected String doRest(String url) {
return restTemplate.postForEntity(url, null, String.class).getBody(); return restTemplate.postForEntity(url, null, String.class).getBody();
} }
@Override
protected void checkPermission(ServiceInstance serviceInstance) throws Exception {
checkDiscoveryControlPermission(serviceInstance);
checkConfigRestControlPermission(serviceInstance);
}
} }
\ No newline at end of file
...@@ -47,4 +47,10 @@ public class ConfigUpdateRestInvoker extends AbstractRestInvoker { ...@@ -47,4 +47,10 @@ public class ConfigUpdateRestInvoker extends AbstractRestInvoker {
return restTemplate.postForEntity(url, entity, String.class).getBody(); return restTemplate.postForEntity(url, entity, String.class).getBody();
} }
@Override
protected void checkPermission(ServiceInstance serviceInstance) throws Exception {
checkDiscoveryControlPermission(serviceInstance);
checkConfigRestControlPermission(serviceInstance);
}
} }
\ No newline at end of file
...@@ -37,4 +37,9 @@ public class VersionClearRestInvoker extends AbstractRestInvoker { ...@@ -37,4 +37,9 @@ public class VersionClearRestInvoker extends AbstractRestInvoker {
protected String doRest(String url) { protected String doRest(String url) {
return restTemplate.postForEntity(url, version, String.class).getBody(); return restTemplate.postForEntity(url, version, String.class).getBody();
} }
@Override
protected void checkPermission(ServiceInstance serviceInstance) throws Exception {
checkDiscoveryControlPermission(serviceInstance);
}
} }
\ No newline at end of file
...@@ -37,4 +37,9 @@ public class VersionUpdateRestInvoker extends AbstractRestInvoker { ...@@ -37,4 +37,9 @@ public class VersionUpdateRestInvoker extends AbstractRestInvoker {
protected String doRest(String url) { protected String doRest(String url) {
return restTemplate.postForEntity(url, version, String.class).getBody(); return restTemplate.postForEntity(url, version, String.class).getBody();
} }
@Override
protected void checkPermission(ServiceInstance serviceInstance) throws Exception {
checkDiscoveryControlPermission(serviceInstance);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment