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.

EncryptAction.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.server.setting.ws;
  21. import org.sonar.api.config.Encryption;
  22. import org.sonar.api.config.Settings;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.server.user.UserSession;
  27. import org.sonarqube.ws.Settings.EncryptWsResponse;
  28. import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_VALUE;
  29. import static org.sonar.server.ws.WsUtils.checkRequest;
  30. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  31. public class EncryptAction implements SettingsWsAction {
  32. private final UserSession userSession;
  33. private final Settings settings;
  34. public EncryptAction(UserSession userSession, Settings settings) {
  35. this.userSession = userSession;
  36. this.settings = settings;
  37. }
  38. @Override
  39. public void define(WebService.NewController context) {
  40. WebService.NewAction action = context.createAction("encrypt")
  41. .setDescription("Encrypt a setting value.<br>" +
  42. "Requires 'Administer System' permission.")
  43. .setSince("6.1")
  44. .setHandler(this)
  45. .setInternal(true)
  46. .setResponseExample(getClass().getResource("encrypt-example.json"));
  47. action.createParam(PARAM_VALUE)
  48. .setRequired(true)
  49. .setDescription("Setting value to encrypt")
  50. .setExampleValue("my value");
  51. }
  52. @Override
  53. public void handle(Request request, Response response) throws Exception {
  54. userSession.checkIsSystemAdministrator();
  55. String value = request.mandatoryParam(PARAM_VALUE);
  56. Encryption encryption = settings.getEncryption();
  57. checkRequest(encryption.hasSecretKey(), "No secret key available");
  58. String encryptedValue = encryption.encrypt(value);
  59. writeProtobuf(toEncryptWsResponse(encryptedValue), request, response);
  60. }
  61. private static EncryptWsResponse toEncryptWsResponse(String encryptedValue) {
  62. return EncryptWsResponse.newBuilder().setEncryptedValue(encryptedValue).build();
  63. }
  64. }