Skip to content

Commit

Permalink
Fixed reading data: URL
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat committed Nov 27, 2018
1 parent a2dc91f commit 811643a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
6 changes: 6 additions & 0 deletions pulsar-broker-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<artifactId>pulsar-zookeeper-utils</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>pulsar-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.pulsar.broker.authentication.utils;

import com.google.common.io.ByteStreams;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
Expand All @@ -26,9 +28,7 @@
import io.jsonwebtoken.security.Keys;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
Expand All @@ -42,6 +42,8 @@

import lombok.experimental.UtilityClass;

import org.apache.pulsar.client.api.url.URL;

@UtilityClass
public class AuthTokenUtils {

Expand Down Expand Up @@ -90,39 +92,18 @@ public static String createToken(Key signingKey, String subject, Optional<Date>
}

public static byte[] readKeyFromUrl(String keyConfUrl) throws IOException {
if (keyConfUrl.startsWith("data:")) {
return readDataUrl(keyConfUrl);
if (keyConfUrl.startsWith("data:") || keyConfUrl.startsWith("file:")) {
try {
return ByteStreams.toByteArray((InputStream) new URL(keyConfUrl).getContent());
} catch (Exception e) {
throw new IOException(e);
}
} else if (keyConfUrl.startsWith("env:")) {
String envVarName = keyConfUrl.substring("env:".length());
return Decoders.BASE64.decode(System.getenv(envVarName));
} else if (keyConfUrl.startsWith("file:")) {
URI filePath = URI.create(keyConfUrl);
return Files.readAllBytes(Paths.get(filePath));
} else {
// Assume the key content was passed in base64
return Decoders.BASE64.decode(keyConfUrl);
}
}

private static byte[] readDataUrl(String data) throws IOException {
// Expected format is:
// data:[<mediatype>][;base64],<data>

// Ignore mediatype and only support base64 encoding
String[] parts = data.split(",", 2);
String header = parts[0];
String encodedData = parts[1];

if (header.contains(";")) {
String encodingType = header.split(";")[1];
if (!"base64".equals(encodingType)) {
throw new IOException("Data url encoding not supported: " + encodingType);
}

return Decoders.BASE64.decode(encodedData);
} else {
// No encoding specified
return encodedData.getBytes();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@

public class DataURLStreamHandler extends URLStreamHandler {

class DataURLConnection extends URLConnection {
static class DataURLConnection extends URLConnection {
private boolean parsed = false;
private String contentType;
private String data;
private byte[] data;
private URI uri;

private static final Pattern pattern = Pattern.compile(
"(?<mimeType>[^;,]+)?(;(?<charset>charset=[^;,]+))?(;(?<base64>base64))?,(?<data>.+)", Pattern.DOTALL);

protected DataURLConnection(URL url) {
super(url);
try {
Expand All @@ -57,20 +60,23 @@ public void connect() throws IOException {
if (this.uri == null) {
throw new IOException();
}
Pattern pattern = Pattern.compile(
"(?<mimeType>.+?)(;(?<charset>charset=.+?))?(;(?<base64>base64?))?,(?<data>.+)", Pattern.DOTALL);

Matcher matcher = pattern.matcher(this.uri.getSchemeSpecificPart());
if (matcher.matches()) {
this.contentType = matcher.group("mimeType");
String charset = matcher.group("charset");
if (charset == null) {
charset = "US-ASCII";
if (contentType == null) {
this.contentType = "application/data";
}

for (int i =0; i < matcher.groupCount(); i++) {
System.out.println("Group: " + matcher.group(i));
}

if (matcher.group("base64") == null) {
// Support Urlencode but not decode here because already decoded by URI class.
this.data = new String(matcher.group("data").getBytes(), charset);
this.data = matcher.group("data").getBytes();
} else {
this.data = new String(Base64.getDecoder().decode(matcher.group("data")), charset);
this.data = Base64.getDecoder().decode(matcher.group("data"));
}
} else {
throw new MalformedURLException();
Expand All @@ -83,7 +89,7 @@ public long getContentLengthLong() {
long length;
try {
this.connect();
length = this.data.length();
length = this.data.length;
} catch (IOException e) {
length = -1;
}
Expand All @@ -109,7 +115,7 @@ public String getContentEncoding() {

public InputStream getInputStream() throws IOException {
this.connect();
return new ByteArrayInputStream(this.data.getBytes());
return new ByteArrayInputStream(this.data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;

public class PulsarURLStreamHandlerFactory implements URLStreamHandlerFactory {
static Map<String, Class<? extends URLStreamHandler>> handlers;
private static final Map<String, Class<? extends URLStreamHandler>> handlers;
static {
handlers = new HashMap<>();
handlers.put("data", DataURLStreamHandler.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.net.URLStreamHandlerFactory;

public class URL {
private static URLStreamHandlerFactory urlStreamHandlerFactory = new PulsarURLStreamHandlerFactory();
private java.net.URL url;
private static final URLStreamHandlerFactory urlStreamHandlerFactory = new PulsarURLStreamHandlerFactory();
private final java.net.URL url;

public URL(String spec)
throws MalformedURLException, URISyntaxException, InstantiationException, IllegalAccessException {
Expand All @@ -47,7 +47,7 @@ public Object getContent() throws IOException {
return this.url.getContent();
}

public Object getContent(Class[] classes) throws IOException {
public Object getContent(Class<?>[] classes) throws IOException {
return this.url.getContent(classes);
}

Expand Down

0 comments on commit 811643a

Please sign in to comment.