]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15338 setSecuredSettings added to the api/settings/values endpoint
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Thu, 16 Sep 2021 11:23:43 +0000 (13:23 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 16 Sep 2021 20:03:30 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/setting/ws/values-example.json
server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ValuesActionTest.java
sonar-ws/src/main/protobuf/ws-settings.proto

index 84f491d17424ebae0530478ae4e62f2484e98e10..2c732d910f54a67f50152a25bd939628f7852f91 100644 (file)
@@ -95,11 +95,12 @@ public class ValuesAction implements SettingsWsAction {
       .setDescription("List settings values.<br>" +
         "If no value has been set for a setting, then the default value is returned.<br>" +
         "The settings from conf/sonar.properties are excluded from results.<br>" +
-        "Requires 'Browse' or 'Execute Analysis' permission when a component is specified.<br/>")
+        "Requires 'Browse' or 'Execute Analysis' permission when a component is specified.<br/>" +
+        "Secured settings are not returned by the endpoint.<br/>")
       .setResponseExample(getClass().getResource("values-example.json"))
       .setSince("6.3")
       .setChangelog(
-        new Change("9.1", "The value of secured settings are no longer returned"),
+        new Change("9.1", "The secured settings are no longer returned."),
         new Change("7.6", String.format("The use of module keys in parameter '%s' is deprecated", PARAM_COMPONENT)),
         new Change("7.1", "The settings from conf/sonar.properties are excluded from results."))
       .setHandler(this);
@@ -249,6 +250,12 @@ public class ValuesAction implements SettingsWsAction {
 
     private void processSettings() {
       settings.forEach(setting -> {
+        if (isSecured(setting.getKey())) {
+          if (!setting.isDefault()) {
+            valuesWsBuilder.addSetSecuredSettings(setting.getKey());
+          }
+          return;
+        }
         Settings.Setting.Builder valueBuilder = getOrCreateValueBuilder(keysToDisplayMap.get(setting.getKey()));
         setInherited(setting, valueBuilder);
         setValue(setting, valueBuilder);
@@ -269,9 +276,6 @@ public class ValuesAction implements SettingsWsAction {
     }
 
     private void setValue(Setting setting, Settings.Setting.Builder valueBuilder) {
-      if (isSecured(setting.getKey())) {
-        return;
-      }
       PropertyDefinition definition = setting.getDefinition();
       String value = setting.getValue();
       if (definition == null) {
index f6c39e5a6dcf21a3ba31e37f6b85afcaec0d0563..8c063c67d1c4e48400aa675ce2b0767da9414a9d 100644 (file)
@@ -28,5 +28,8 @@
       ],
       "inherited": false
     }
+  ],
+  "setSecuredSettings": [
+    "email.smtp_port.secured"
   ]
 }
index 7bf27b1ae0b5c18e9d4a9278f6ab23008353a1c8..fafc5b77b3143c75eab88d856db5c11d4768da8a 100644 (file)
@@ -543,7 +543,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForGlobalProperties();
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo", "secret.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo");
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("secret.secured");
   }
 
   @Test
@@ -563,7 +564,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForProjectProperties();
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo", "global.secret.secured", "secret.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo");
+    assertThat(result.getSetSecuredSettingsList()).contains("global.secret.secured", "secret.secured");
   }
 
   @Test
@@ -576,7 +578,7 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForProjectProperties("not-defined.secured");
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("not-defined.secured");
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("not-defined.secured");
   }
 
   @Test
@@ -591,7 +593,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForGlobalProperties();
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo", "secret.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo");
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("secret.secured");
   }
 
   @Test
@@ -610,8 +613,9 @@ public class ValuesActionTest {
     ValuesWsResponse result = executeRequestForProjectProperties();
 
     List<Settings.Setting> settingsList = result.getSettingsList().stream().sorted(comparing(Settings.Setting::getKey)).collect(Collectors.toList());
-    assertThat(settingsList).extracting(Settings.Setting::getKey).containsExactly("foo", "global.secret.secured", "secret.secured");
-    assertThat(settingsList).extracting(Settings.Setting::hasValue).containsExactly(true, false, false);
+    assertThat(settingsList).extracting(Settings.Setting::getKey).containsExactly("foo");
+    assertThat(settingsList).extracting(Settings.Setting::hasValue).containsExactly(true);
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("global.secret.secured", "secret.secured");
   }
 
   @Test
@@ -622,7 +626,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForProjectProperties("not-defined.secured");
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("not-defined.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).isEmpty();
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("not-defined.secured");
   }
 
   @Test
@@ -654,7 +659,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForGlobalProperties();
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo", "secret.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo");
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("secret.secured");
   }
 
   @Test
@@ -669,7 +675,8 @@ public class ValuesActionTest {
 
     ValuesWsResponse result = executeRequestForProjectProperties();
 
-    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo", "secret.secured");
+    assertThat(result.getSettingsList()).extracting(Settings.Setting::getKey).containsOnly("foo");
+    assertThat(result.getSetSecuredSettingsList()).containsOnly("secret.secured");
   }
 
   @Test
@@ -738,7 +745,7 @@ public class ValuesActionTest {
 
   @Test
   public void test_example_json_response() {
-    logIn();
+    logInAsAdmin();
     definitions.addComponent(PropertyDefinition
       .builder("sonar.test.jira")
       .defaultValue("abc")
@@ -757,8 +764,15 @@ public class ValuesActionTest {
       .build());
     propertyDb.insertPropertySet("sonar.demo", null, ImmutableMap.of("text", "foo", "boolean", "true"), ImmutableMap.of("text", "bar", "boolean", "false"));
 
+    definitions.addComponent(PropertyDefinition
+      .builder("email.smtp_port.secured")
+      .defaultValue("25")
+      .build());
+    propertyDb.insertProperties(null, null, null, null,
+      newGlobalPropertyDto().setKey("email.smtp_port.secured").setValue("25"));
+
     String result = newTester().newRequest()
-      .setParam("keys", "sonar.test.jira,sonar.autogenerated,sonar.demo")
+      .setParam("keys", "sonar.test.jira,sonar.autogenerated,sonar.demo,email.smtp_port.secured")
       .setMediaType(JSON)
       .execute()
       .getInput();
@@ -824,21 +838,18 @@ public class ValuesActionTest {
     userSession.logIn()
       .addPermission(GlobalPermission.SCAN);
     response = executeRequest(tester, null, securedDef.key(), standardDef.key());
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::getKey).containsExactly("my.password.secured", "my.property");
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::hasValue).containsExactly(false, true);
+    assertThat(response.getSetSecuredSettingsList()).contains("my.password.secured");
 
     // global administrator
     userSession.logIn()
       .addPermission(GlobalPermission.ADMINISTER);
     response = executeRequest(tester, null, securedDef.key(), standardDef.key());
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::getKey).containsExactly("my.password.secured", "my.property");
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::hasValue).containsExactly(false, true);
+    assertThat(response.getSetSecuredSettingsList()).contains("my.password.secured");
 
     // system administrator
     userSession.logIn().setSystemAdministrator();
     response = executeRequest(tester, null, securedDef.key(), standardDef.key());
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::getKey).containsExactly("my.password.secured", "my.property");
-    assertThat(response.getSettingsList()).extracting(Settings.Setting::hasValue).containsExactly(false, true);
+    assertThat(response.getSetSecuredSettingsList()).contains("my.password.secured");
   }
 
   private ValuesWsResponse executeRequestForComponentProperties(ComponentDto componentDto, String... keys) {
index 69e8f6d93d937ac93d711d30e2dfbc4bf18ec8ab..cbaebbb4a72295ea970e0ee8fafecd882169375b 100644 (file)
@@ -87,6 +87,7 @@ enum Type {
 // Response of GET api/settings/values
 message ValuesWsResponse {
   repeated Setting settings = 1;
+  repeated string setSecuredSettings = 2;
 }
 
 message Setting {