Skip to content

Commit

Permalink
Implement our own URL escaper (Azure#481)
Browse files Browse the repository at this point in the history
* Implement our own escaper

Copy Guava url utils

Use local guava copies

Modify license according to CELA

Use our own escaper

* Be conservative about query escaping

* Use our own license header

* Checkstyle

* Fix errors when guava gone in azure-client-runtime

* Add tests for Url escaper and type util

* Fix azure-client-authentication

* Fix Cli token class
  • Loading branch information
jianghaolu authored Aug 29, 2018
1 parent 5443cdc commit c27b25f
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
package com.microsoft.azure.v2.credentials;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.io.Files;
import com.google.common.reflect.TypeToken;
import com.google.gson.reflect.TypeToken;
import com.microsoft.azure.v2.AzureEnvironment;
import com.microsoft.azure.v2.AzureEnvironment.Endpoint;
import com.microsoft.rest.v2.annotations.Beta;
Expand All @@ -18,6 +17,8 @@
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -54,7 +55,7 @@ private AuthFile() {
* @throws IOException thrown when the auth file or the certificate file cannot be read or parsed
*/
static AuthFile parse(File file) throws IOException {
String content = Files.toString(file, StandardCharsets.UTF_8).trim();
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

AuthFile authFile;
if (isJsonBased(content)) {
Expand Down Expand Up @@ -110,9 +111,9 @@ ApplicationTokenCredentials generateCredentials() throws IOException {
} else if (clientCertificate != null) {
byte[] certData;
if (new File(clientCertificate).exists()) {
certData = Files.toByteArray(new File(clientCertificate));
certData = Files.readAllBytes(Paths.get(clientCertificate));
} else {
certData = Files.toByteArray(new File(authFilePath, clientCertificate));
certData = Files.readAllBytes(Paths.get(authFilePath, clientCertificate));
}

return (ApplicationTokenCredentials) new ApplicationTokenCredentials(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.rest.v2.annotations.Beta;

import java.time.Instant;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
* An instance of this class represents an entry in accessTokens.json.
Expand All @@ -25,7 +26,7 @@ final class AzureCliToken implements Cloneable {
private String tokenType;
private long expiresIn;
private String expiresOn;
private Date expiresOnDate;
private LocalDateTime expiresOnDate;
private String oid;
private String userId;
private String servicePrincipalId;
Expand Down Expand Up @@ -62,16 +63,20 @@ String authority() {
}

boolean expired() {
return expiresOn != null && expiresOn().before(new Date());
return expiresOn != null && expiresOn().isBefore(LocalDateTime.now());
}

String accessToken() {
return accessToken;
}

Date expiresOn() {
LocalDateTime expiresOn() {
if (expiresOnDate == null) {
expiresOnDate = Date.from(Instant.parse(expiresOn));
try {
expiresOnDate = LocalDateTime.parse(expiresOn, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
} catch (IllegalArgumentException e) {
expiresOnDate = LocalDateTime.parse(expiresOn);
}
}
return expiresOnDate;
}
Expand All @@ -80,7 +85,7 @@ AzureCliToken withAuthenticationResult(AuthenticationResult result) {
this.accessToken = result.getAccessToken();
this.refreshToken = result.getRefreshToken();
this.expiresIn = result.getExpiresAfter();
this.expiresOnDate = result.getExpiresOnDate();
this.expiresOnDate = LocalDateTime.ofInstant(result.getExpiresOnDate().toInstant(), ZoneId.systemDefault());
return this;
}

Expand Down Expand Up @@ -121,7 +126,7 @@ AzureCliToken withResource(String resource) {

public AzureCliToken clone() throws CloneNotSupportedException {
AzureCliToken token = (AzureCliToken) super.clone();
token.expiresOnDate = (Date) expiresOn().clone();
token.expiresOnDate = LocalDateTime.from(expiresOn());
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package com.microsoft.azure.v2.credentials;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.rest.v2.RestProxy;
import com.microsoft.rest.v2.annotations.Beta;
Expand All @@ -23,6 +21,8 @@
import com.microsoft.rest.v2.http.HttpClientConfiguration;
import com.microsoft.rest.v2.http.HttpPipeline;
import com.microsoft.rest.v2.http.NettyClient;
import com.microsoft.rest.v2.util.escapers.PercentEscaper;
import com.microsoft.rest.v2.util.escapers.UrlEscapers;
import io.reactivex.Single;
import io.reactivex.functions.Function;

Expand Down Expand Up @@ -63,7 +63,7 @@ AuthenticationResult refreshToken(String tenant, String clientId, String resourc
}

Single<AuthenticationResult> refreshTokenAsync(String tenant, String clientId, String resource, String refreshToken, final boolean isMultipleResourceRefreshToken) {
final Escaper escaper = UrlEscapers.urlFormParameterEscaper();
final PercentEscaper escaper = UrlEscapers.FORM_ESCAPER;
final String bodyString = String.format(
"client_id=%s&grant_type=%s&resource=%s&refresh_token=%s",
escaper.escape(clientId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

package com.microsoft.azure.v2;

import com.google.common.hash.Hashing;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.v2.annotations.AzureHost;
import com.microsoft.azure.v2.serializer.AzureJacksonAdapter;
import com.microsoft.rest.v2.InvalidReturnTypeException;
import com.microsoft.rest.v2.OperationDescription;
import com.microsoft.rest.v2.RestProxy;
import com.microsoft.rest.v2.SwaggerInterfaceParser;
import com.microsoft.rest.v2.SwaggerMethodParser;
import com.microsoft.rest.v2.OperationDescription;
import com.microsoft.rest.v2.credentials.ServiceClientCredentials;
import com.microsoft.rest.v2.http.HttpClient;
import com.microsoft.rest.v2.http.HttpMethod;
Expand All @@ -30,6 +28,7 @@
import com.microsoft.rest.v2.policy.RetryPolicyFactory;
import com.microsoft.rest.v2.protocol.SerializerAdapter;
import com.microsoft.rest.v2.protocol.SerializerEncoding;
import com.microsoft.rest.v2.util.TypeUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
Expand All @@ -42,6 +41,7 @@
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.util.Enumeration;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -109,15 +109,22 @@ private static String macAddressHash() {
break;
}
}

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(macBytes);
StringBuffer builder = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
builder.append(String.format("%02x", hash[i]));
}
macAddressHash = builder.toString();
} catch (Throwable t) {
// It's okay ignore mac address hash telemetry
}

if (macBytes == null) {
macBytes = "Unknown".getBytes();
macAddressHash = "Unknown";
}

macAddressHash = Hashing.sha256().hashBytes(macBytes).toString();
}
return macAddressHash;
}
Expand Down Expand Up @@ -242,12 +249,9 @@ public static <A> A create(Class<A> swaggerInterface, AzureEnvironment azureEnvi
protected Object handleAsyncHttpResponse(final HttpRequest httpRequest, Single<HttpResponse> asyncHttpResponse, final SwaggerMethodParser methodParser, Type returnType) {
Object result;

final TypeToken returnTypeToken = TypeToken.of(returnType);

if (returnTypeToken.isSubtypeOf(Observable.class)) {
if (TypeUtil.isTypeOrSubTypeOf(returnType, Observable.class)) {
final Type operationStatusType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
final TypeToken operationStatusTypeToken = TypeToken.of(operationStatusType);
if (!operationStatusTypeToken.isSubtypeOf(OperationStatus.class)) {
if (!TypeUtil.isTypeOrSubTypeOf(operationStatusType, OperationStatus.class)) {
throw new InvalidReturnTypeException("AzureProxy only supports swagger interface methods that return Observable (such as " + methodParser.fullyQualifiedMethodName() + "()) if the Observable's inner type that is OperationStatus (not " + returnType.toString() + ").");
}
else {
Expand Down Expand Up @@ -301,8 +305,7 @@ protected Object handleResumeOperation(final HttpRequest httpRequest,
Type returnType)
throws Exception {
final Type operationStatusType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
final TypeToken operationStatusTypeToken = TypeToken.of(operationStatusType);
if (!operationStatusTypeToken.isSubtypeOf(OperationStatus.class)) {
if (!TypeUtil.isTypeOrSubTypeOf(operationStatusType, OperationStatus.class)) {
throw new InvalidReturnTypeException("AzureProxy only supports swagger interface methods that return Observable (such as " + methodParser.fullyQualifiedMethodName() + "()) if the Observable's inner type that is OperationStatus (not " + returnType.toString() + ").");
}

Expand Down
5 changes: 0 additions & 5 deletions client-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
</developers>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package com.microsoft.rest.v2;

import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;
import com.microsoft.rest.v2.annotations.BodyParam;
import com.microsoft.rest.v2.annotations.DELETE;
import com.microsoft.rest.v2.annotations.ExpectedResponses;
Expand All @@ -28,6 +26,8 @@
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.protocol.SerializerAdapter;
import com.microsoft.rest.v2.util.TypeUtil;
import com.microsoft.rest.v2.util.escapers.PercentEscaper;
import com.microsoft.rest.v2.util.escapers.UrlEscapers;
import io.reactivex.Observable;
import io.reactivex.Single;

Expand Down Expand Up @@ -224,7 +224,7 @@ public int[] expectedStatusCodes() {
* @return The final host to use for HTTP requests for this Swagger method.
*/
public String scheme(Object[] swaggerMethodArguments) {
final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.urlPathSegmentEscaper());
final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.PATH_ESCAPER);
final String[] substitutedHostParts = substitutedHost.split("://");
return substitutedHostParts.length < 1 ? null : substitutedHostParts[0];
}
Expand All @@ -235,7 +235,7 @@ public String scheme(Object[] swaggerMethodArguments) {
* @return The final host to use for HTTP requests for this Swagger method.
*/
public String host(Object[] swaggerMethodArguments) {
final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.urlPathSegmentEscaper());
final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.PATH_ESCAPER);
final String[] substitutedHostParts = substitutedHost.split("://");
return substitutedHostParts.length < 2 ? substitutedHost : substitutedHost.split("://")[1];
}
Expand All @@ -246,7 +246,7 @@ public String host(Object[] swaggerMethodArguments) {
* @return The path value with its placeholders replaced by the matching substitutions.
*/
public String path(Object[] methodArguments) {
return applySubstitutions(relativePath, pathSubstitutions, methodArguments, UrlEscapers.urlPathSegmentEscaper());
return applySubstitutions(relativePath, pathSubstitutions, methodArguments, UrlEscapers.PATH_ESCAPER);
}

/**
Expand All @@ -259,7 +259,7 @@ public String path(Object[] methodArguments) {
public Iterable<EncodedParameter> encodedQueryParameters(Object[] swaggerMethodArguments) {
final List<EncodedParameter> result = new ArrayList<>();
if (querySubstitutions != null) {
final Escaper escaper = UrlEscapers.urlFormParameterEscaper();
final PercentEscaper escaper = UrlEscapers.QUERY_ESCAPER;

for (Substitution querySubstitution : querySubstitutions) {
final int parameterIndex = querySubstitution.methodParameterIndex();
Expand Down Expand Up @@ -509,7 +509,7 @@ String serialize(Object value) {
return result;
}

private String applySubstitutions(String originalValue, Iterable<Substitution> substitutions, Object[] methodArguments, Escaper escaper) {
private String applySubstitutions(String originalValue, Iterable<Substitution> substitutions, Object[] methodArguments, PercentEscaper escaper) {
String result = originalValue;

if (methodArguments != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static List<Class<?>> getAllClasses(Class<?> clazz) {
/**
* Get the generic arguments for a type.
* @param type the type to get arguments
* @return the generic arguments, null if type is not parametrized
* @return the generic arguments, empty if type is not parametrized
*/
public static Type[] getTypeArguments(Type type) {
if (!(type instanceof ParameterizedType)) {
return null;
return new Type[0];
}
return ((ParameterizedType) type).getActualTypeArguments();
}
Expand Down
Loading

0 comments on commit c27b25f

Please sign in to comment.