public MapSettings(PropertyDefinitions definitions) { super(definitions, new Encryption(null)); configurationBridge = new ConfigurationBridge(this); }
public ScannerProperties(Map<String, String> properties) { encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); Map<String, String> decryptedProps = new HashMap<>(properties.size()); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = decryptedProps; }
@VisibleForTesting ThreadLocalSettings(PropertyDefinitions definitions, Properties props, SettingLoader settingLoader) { super(definitions, new Encryption(null)); this.settingLoader = settingLoader; props.forEach((k, v) -> systemProps.put(k, v == null ? null : v.toString().trim())); // TODO something wrong about lifecycle here. It could be improved getEncryption().setPathToSecretKey(props.getProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); }
@Test public void scramble() { Encryption encryption = new Encryption(null); assertThat(encryption.scramble("foo")).isEqualTo("{b64}Zm9v"); }
@Test public void decrypt() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("{b64}Zm9v")).isEqualTo("foo"); }
@Test public void decrypt_unknown_algorithm() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("{xxx}Zm9v")).isEqualTo("{xxx}Zm9v"); }
@Test public void decrypt_uncrypted_text() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("foo")).isEqualTo("foo"); } }
@Test public void isEncrypted() { Encryption encryption = new Encryption(null); assertThat(encryption.isEncrypted("{aes}ADASDASAD")).isTrue(); assertThat(encryption.isEncrypted("{b64}ADASDASAD")).isTrue(); assertThat(encryption.isEncrypted("{abc}ADASDASAD")).isTrue(); assertThat(encryption.isEncrypted("{}")).isFalse(); assertThat(encryption.isEncrypted("{foo")).isFalse(); assertThat(encryption.isEncrypted("foo{aes}")).isFalse(); }
private String[] getStringArray(String value) { return new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("multi").multiValues(true).build())), new Encryption(null), ImmutableMap.of("multi", value)) { }.getStringArray("multi"); } }
@Test public void migrateOldProperties() { Map<String, String> settings = new HashMap<>(); settings.put(GenericCoverageSensor.OLD_REPORT_PATH_PROPERTY_KEY, "old.xml"); settings.put(GenericCoverageSensor.OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old1.xml,old2.xml"); settings.put(GenericCoverageSensor.OLD_IT_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old3.xml,old4.xml,old.xml"); settings.put(GenericCoverageSensor.OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old5.xml,old6.xml"); PropertyDefinitions defs = new PropertyDefinitions(GenericCoverageSensor.properties()); DefaultConfiguration config = new ProjectConfiguration(defs, new Encryption(null), settings); Set<String> reportPaths = new GenericCoverageSensor(config).loadReportPaths(); assertThat(logTester.logs(LoggerLevel.WARN)).contains( "Property 'sonar.genericcoverage.reportPath' is deprecated. Please use 'sonar.coverageReportPaths' instead.", "Property 'sonar.genericcoverage.reportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead.", "Property 'sonar.genericcoverage.itReportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead.", "Property 'sonar.genericcoverage.overallReportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead."); assertThat(reportPaths).containsOnly( "old.xml", "old1.xml", "old2.xml", "old3.xml", "old4.xml", "old5.xml", "old6.xml"); } }
@Test public void logWhenDeprecatedPropsAreUsed() throws IOException { File basedir = temp.newFolder(); File report = new File(basedir, "report.xml"); FileUtils.write(report, "<unitTest version=\"1\"><file path=\"A.java\"><testCase name=\"test1\" duration=\"500\"/></file></unitTest>", StandardCharsets.UTF_8); SensorContextTester context = SensorContextTester.create(basedir); Map<String, String> settings = new HashMap<>(); settings.put(GenericTestExecutionSensor.OLD_UNIT_TEST_REPORT_PATHS_PROPERTY_KEY, "report.xml"); PropertyDefinitions defs = new PropertyDefinitions(GenericTestExecutionSensor.properties()); DefaultConfiguration config = new ProjectConfiguration(defs, new Encryption(null), settings); new GenericTestExecutionSensor(mock(TestPlanBuilder.class), config).execute(context); assertThat(logTester.logs(LoggerLevel.WARN)).contains( "Using 'unitTest' as root element of the report is deprecated. Please change to 'testExecutions'.", "Property 'sonar.genericcoverage.unitTestReportPaths' is deprecated. Please use 'sonar.testExecutionReportPaths' instead."); assertThat(logTester.logs(LoggerLevel.INFO)).contains( "Imported test execution data for 0 files", "Test execution data ignored for 1 unknown files, including:\nA.java"); } }
@Test public void getDefaultValues() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(), PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null), ImmutableMap.of()) { }; assertThat(config.get("multiA")).hasValue("foo,bar"); assertThat(config.getStringArray("multiA")).containsExactly("foo", "bar"); assertThat(config.get("single")).hasValue("default"); assertThat(config.getStringArray("single")).containsExactly("default"); }
@Test public void accessingPropertySetPropertiesShouldBeConsistentWithDeclaration() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())), new Encryption(null), ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) { }; assertThat(config.get("props")).hasValue("1,2"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Access to the multi-values/property set property 'props' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated."); logTester.clear(); assertThat(config.getStringArray("props")).containsExactly("1", "2"); assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); }
@Test public void accessingMultiValuedPropertiesShouldBeConsistentWithDeclaration() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).build(), PropertyDefinition.builder("multiA").multiValues(true).build())), new Encryption(null), ImmutableMap.of("single", "foo", "multiA", "a,b", "notDeclared", "c,d")) { }; assertThat(config.get("multiA")).hasValue("a,b"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Access to the multi-values/property set property 'multiA' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated."); logTester.clear(); assertThat(config.getStringArray("single")).containsExactly("foo"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Property 'single' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated."); logTester.clear(); assertThat(config.get("notDeclared")).hasValue("c,d"); assertThat(config.getStringArray("notDeclared")).containsExactly("c", "d"); assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); }
public String getEncryptWord(String txt) { return new Encryption().base64encode(txt); }
public Settings(PropertyDefinitions definitions) { this.properties = Maps.newHashMap(); this.definitions = definitions; this.encryption = new Encryption(null); }
public MapSettings(PropertyDefinitions definitions) { super(definitions, new Encryption(null)); configurationBridge = new ConfigurationBridge(this); }
public UserProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { encryption = new Encryption(pathToSecretKey); Map<String, String> decryptedProps = Maps.newHashMap(); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = Maps.newHashMap(decryptedProps); }
public UserProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { encryption = new Encryption(pathToSecretKey); Map<String, String> decryptedProps = Maps.newHashMap(); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = Maps.newHashMap(decryptedProps); }
@VisibleForTesting ThreadLocalSettings(PropertyDefinitions definitions, Properties props, SettingLoader settingLoader) { super(definitions, new Encryption(null)); this.settingLoader = settingLoader; this.systemProps = new Properties(); props.forEach((k, v) -> systemProps.put(k, v == null ? null : v.toString().trim())); // TODO something wrong about lifecycle here. It could be improved getEncryption().setPathToSecretKey(props.getProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); }