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.

AesCipher.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.sonar.api.config.internal;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.security.Key;
  24. import java.security.SecureRandom;
  25. import javax.annotation.Nullable;
  26. import javax.crypto.KeyGenerator;
  27. import javax.crypto.SecretKey;
  28. import javax.crypto.spec.SecretKeySpec;
  29. import org.apache.commons.codec.binary.Base64;
  30. import org.apache.commons.io.FileUtils;
  31. import org.apache.commons.lang.StringUtils;
  32. import static java.nio.charset.StandardCharsets.UTF_8;
  33. import static org.sonar.api.CoreProperties.ENCRYPTION_SECRET_KEY_PATH;
  34. abstract class AesCipher implements Cipher {
  35. static final int KEY_SIZE_IN_BITS = 256;
  36. private static final String CRYPTO_KEY = "AES";
  37. private String pathToSecretKey;
  38. AesCipher(@Nullable String pathToSecretKey) {
  39. this.pathToSecretKey = pathToSecretKey;
  40. }
  41. /**
  42. * This method checks the existence of the file, but not the validity of the contained key.
  43. */
  44. boolean hasSecretKey() {
  45. String path = getPathToSecretKey();
  46. if (StringUtils.isNotBlank(path)) {
  47. File file = new File(path);
  48. return file.exists() && file.isFile();
  49. }
  50. return false;
  51. }
  52. protected Key loadSecretFile() throws IOException {
  53. String path = getPathToSecretKey();
  54. return loadSecretFileFromFile(path);
  55. }
  56. Key loadSecretFileFromFile(@Nullable String path) throws IOException {
  57. if (StringUtils.isBlank(path)) {
  58. throw new IllegalStateException("Secret key not found. Please set the property " + ENCRYPTION_SECRET_KEY_PATH);
  59. }
  60. File file = new File(path);
  61. if (!file.exists() || !file.isFile()) {
  62. throw new IllegalStateException("The property " + ENCRYPTION_SECRET_KEY_PATH + " does not link to a valid file: " + path);
  63. }
  64. String s = FileUtils.readFileToString(file, UTF_8);
  65. if (StringUtils.isBlank(s)) {
  66. throw new IllegalStateException("No secret key in the file: " + path);
  67. }
  68. return new SecretKeySpec(Base64.decodeBase64(StringUtils.trim(s)), CRYPTO_KEY);
  69. }
  70. String generateRandomSecretKey() {
  71. try {
  72. KeyGenerator keyGen = KeyGenerator.getInstance(CRYPTO_KEY);
  73. keyGen.init(KEY_SIZE_IN_BITS, new SecureRandom());
  74. SecretKey secretKey = keyGen.generateKey();
  75. return Base64.encodeBase64String(secretKey.getEncoded());
  76. } catch (Exception e) {
  77. throw new IllegalStateException("Fail to generate secret key", e);
  78. }
  79. }
  80. String getPathToSecretKey() {
  81. if (StringUtils.isBlank(pathToSecretKey)) {
  82. pathToSecretKey = new File(FileUtils.getUserDirectoryPath(), ".sonar/sonar-secret.txt").getPath();
  83. }
  84. return pathToSecretKey;
  85. }
  86. public void setPathToSecretKey(@Nullable String pathToSecretKey) {
  87. this.pathToSecretKey = pathToSecretKey;
  88. }
  89. }