[英]How can I get Spring Boot to load the Java System.getenv() variables into the Environment object?
[英]System.getenv() used in spring boot is shown as a security vulnerability in sonarQube
我的 spring 启动应用程序中有方法从系统环境变量中获取数据,该方法按预期工作,但 sonarQube 说“确保在此处安全使用环境变量”,我试图找到解决此问题的替代方法,但我找不到解决办法,方法如下:
我该如何处理这个安全问题,除了从环境变量中获取值之外,我不能使用任何东西。
public Map<String, Object> getConfigurations() {
Map<String, Object> result = new HashMap<>();
HttpResponse response = null;
try {
String xVaultToken = System.getenv("XVaultToken");
String cityAppConfig = System.getenv("CityApp_Config");
@SuppressWarnings("deprecation")
HttpClient client = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSslcontext(
new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build())
.build();
Map<String, Object> headerDatas = new HashMap<>();
headerDatas.put("Content-Type", "application/json");
headerDatas.put("X-Vault-Token", xVaultToken);
HttpGet get = new HttpGet(cityAppConfig);
Set<String> keys = headerDatas.keySet();
for (String key : keys) {
get.setHeader(key, headerDatas.get(key).toString());
}
response = client.execute(get);
try(BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))){
String responseData = rd.readLine();
result.put(Constants.RESPONSE, responseData);
}
int statusCode = response.getStatusLine().getStatusCode();
result.put(Constants.STATUS, statusCode);
} catch (Exception e) {
logger.info("error is local settings getConfigurations" + e);
}
return result;
}
}
首先,正如我在评论中提到的,如果有人要求您阅读 ENV 变量,但还说您不能将 SonarQube 警告标记为误报: (礼貌地)告诉他们他们必须活着带有 SonarQube 警告。
但是,如果出于某种原因这不是一个选项,我将向您展示两种可能不会触发 SonarQube 中的警告的方法,它们仍然比为此目的调用cmd.exe
更简洁。
java.util.function.Function
private static String getenv(String variable) {
return ((Function<String, String>) System::getenv).apply(variable);
}
private static String getenv(String variable) {
try {
return (String) System.class.getMethod("getenv", String.class).invoke(null, variable);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
所有这些“安全热点”警告都只是“TODO”通知,应该分三步解决:
@SuppressWarnings({"squid:S5304"})
注释,并在注释中添加适当的描述。可以使用@SuppressWarnings({"squid:S5304"})
抑制其低级别问题。 在 class 级别上添加此语句。
经过大量搜索后,我发现使用环境变量来存储重要信息是一个坏主意,但如果你没有像我这样的其他选择,这里是解决方法:
不要使用 System.getenv("XVaultToken"),而是使用这样的方法
private String cmdUrl = "cmd.exe /c echo %wfManagement_Config%";
String cityAppConfig = getEnvironmentVariable(cmdUrl)
private String getEnvironmentVariable(String cmd){
String environmentVar = "";
Process p;
try {
p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while((line = reader.readLine()) != null) {
environmentVar = line;
}
return environmentVar;
} catch (Exception e) {
logger.info("Exception-->"+e);
}
return environmentVar;
}
该方法运行一个命令并为您获取环境变量,由于某些原因 sonarQube 忽略了这一点并且不会给出安全问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.