You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemInfo.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.cli;
  21. import java.util.Set;
  22. import java.util.regex.Pattern;
  23. import java.util.stream.Collectors;
  24. class SystemInfo {
  25. private static final Set<String> SENSITIVE_JVM_ARGUMENTS = Set.of(
  26. "-Dsonar.login",
  27. "-Dsonar.password",
  28. "-Dsonar.token");
  29. private static final Pattern PATTERN_ARGUMENT_SEPARATOR = Pattern.compile("\\s+");
  30. private static System2 system = new System2();
  31. private SystemInfo() {
  32. }
  33. static void setSystem(System2 system) {
  34. SystemInfo.system = system;
  35. }
  36. static void print(Logs logger) {
  37. logger.info("SonarScanner " + ScannerVersion.version());
  38. logger.info(java());
  39. logger.info(os());
  40. String scannerOpts = system.getenv("SONAR_SCANNER_OPTS");
  41. if (scannerOpts != null) {
  42. logger.info("SONAR_SCANNER_OPTS=" + redactSensitiveArguments(scannerOpts));
  43. }
  44. }
  45. private static String redactSensitiveArguments(String scannerOpts) {
  46. return PATTERN_ARGUMENT_SEPARATOR.splitAsStream(scannerOpts)
  47. .map(SystemInfo::redactArgumentIfSensistive)
  48. .collect(Collectors.joining(" "));
  49. }
  50. private static String redactArgumentIfSensistive(String argument) {
  51. String[] elems = argument.split("=");
  52. if (elems.length > 0 && SENSITIVE_JVM_ARGUMENTS.contains(elems[0])) {
  53. return elems[0] + "=*";
  54. }
  55. return argument;
  56. }
  57. static String java() {
  58. StringBuilder sb = new StringBuilder();
  59. sb
  60. .append("Java ")
  61. .append(system.getProperty("java.version"))
  62. .append(" ")
  63. .append(system.getProperty("java.vendor"));
  64. String bits = system.getProperty("sun.arch.data.model");
  65. if ("32".equals(bits) || "64".equals(bits)) {
  66. sb.append(" (").append(bits).append("-bit)");
  67. }
  68. return sb.toString();
  69. }
  70. static String os() {
  71. StringBuilder sb = new StringBuilder();
  72. sb
  73. .append(system.getProperty("os.name"))
  74. .append(" ")
  75. .append(system.getProperty("os.version"))
  76. .append(" ")
  77. .append(system.getProperty("os.arch"));
  78. return sb.toString();
  79. }
  80. static class System2 {
  81. String getProperty(String key) {
  82. return System.getProperty(key);
  83. }
  84. String getenv(String key) {
  85. return System.getenv(key);
  86. }
  87. }
  88. }