]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9939 Parse base64 license
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 16 Oct 2017 08:03:38 +0000 (10:03 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 23 Oct 2017 15:01:13 +0000 (08:01 -0700)
server/sonar-server/src/main/java/org/sonar/server/edition/License.java
server/sonar-server/src/test/java/org/sonar/server/edition/LicenseTest.java

index f430d26a165ff9f3076c59ac02a3cd794ff93f9a..fbfdaef18a906ec2eb8ef6d4b420cf3fd93dc50f 100644 (file)
  */
 package org.sonar.server.edition;
 
-import com.google.common.collect.ImmutableList;
-import java.util.List;
+import com.google.common.collect.ImmutableSet;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
 import javax.annotation.concurrent.Immutable;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 @Immutable
 public class License {
+  private static final Logger LOG = Loggers.get(License.class);
+  private static final String EDITION_KEY = "Edition";
+  private static final String PLUGINS_KEY = "Plugins";
+
   private final String editionKey;
-  private final List<String> pluginKeys;
+  private final Set<String> pluginKeys;
   private final String content;
 
-  public License(String editionKey, List<String> pluginKeys, String content) {
+  public License(String editionKey, Collection<String> pluginKeys, String content) {
     this.editionKey = enforceNotNullNorEmpty(editionKey, "editionKey");
-    this.pluginKeys = ImmutableList.copyOf(pluginKeys);
+    this.pluginKeys = ImmutableSet.copyOf(pluginKeys);
     this.content = enforceNotNullNorEmpty(content, "content");
   }
 
@@ -48,11 +62,33 @@ public class License {
     return editionKey;
   }
 
-  public List<String> getPluginKeys() {
+  public Set<String> getPluginKeys() {
     return pluginKeys;
   }
 
   public String getContent() {
     return content;
   }
+
+  public static Optional<License> parse(String base64) {
+    try {
+      String data = new String(Base64.decodeBase64(base64.trim().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
+
+      Properties props = new Properties();
+      props.load(new StringReader(data));
+
+      Collection<String> plugins = Arrays.asList(StringUtils.split(props.getProperty(PLUGINS_KEY), ','));
+      String editionKey = props.getProperty(EDITION_KEY);
+
+      if (editionKey != null && !plugins.isEmpty()) {
+        return Optional.of(new License(editionKey, plugins, base64));
+      } else {
+        LOG.debug("Failed to parse license: no edition key and/or no plugin found");
+      }
+    } catch (Exception e) {
+      LOG.debug("Failed to parse license", e);
+    }
+    return Optional.empty();
+
+  }
 }
index 51c0e2762a3a9e318716fb565e1e82556a0375df..d894b5975e520a8f443fc557527e13856016567e 100644 (file)
  */
 package org.sonar.server.edition;
 
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Base64;
 import java.util.Collections;
+import java.util.Optional;
+import java.util.Properties;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -70,9 +75,44 @@ public class LicenseTest {
     new License("edition-key", Collections.emptyList(), "");
   }
 
+  @Test
+  public void parse_returns_empty_if_license_is_invalid_string() {
+    assertThat(License.parse("trash")).isEmpty();
+  }
+
+  @Test
+  public void parse_succeeds() throws IOException {
+    Properties props = new Properties();
+    props.setProperty("Plugins", "plugin1,plugin2");
+    props.setProperty("Edition", "dev");
+    StringWriter writer = new StringWriter();
+    props.store(writer, "");
+
+    byte[] encoded = Base64.getEncoder().encode(writer.toString().getBytes());
+
+    Optional<License> license = License.parse(new String(encoded));
+    assertThat(license).isPresent();
+    assertThat(license.get().getEditionKey()).isEqualTo("dev");
+    assertThat(license.get().getPluginKeys()).containsOnly("plugin1", "plugin2");
+  }
+
+  @Test
+  public void parse_is_empty_if_no_plugin() throws IOException {
+    Properties props = new Properties();
+    props.setProperty("Plugins", "");
+    props.setProperty("Edition", "dev");
+    StringWriter writer = new StringWriter();
+    props.store(writer, "");
+
+    byte[] encoded = Base64.getEncoder().encode(writer.toString().getBytes());
+
+    Optional<License> license = License.parse(new String(encoded));
+    assertThat(license).isEmpty();
+  }
+
   @Test
   public void verify_getters() {
-    ImmutableList<String> pluginKeys = ImmutableList.of("a", "b", "c");
+    ImmutableSet<String> pluginKeys = ImmutableSet.of("a", "b", "c");
 
     License underTest = new License("edition-key", pluginKeys, "content");