Skip to content

Commit

Permalink
Testing for Tables Autorest Code (#12512)
Browse files Browse the repository at this point in the history
* testing for Tables Autorest Code
  • Loading branch information
eboyd23 authored Jul 7, 2020
1 parent 1961116 commit 86afb16
Show file tree
Hide file tree
Showing 24 changed files with 2,631 additions and 2 deletions.
3 changes: 3 additions & 0 deletions sdk/tables/azure-data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Release History

## 1.0.0-beta.1 (Unreleased)
27 changes: 25 additions & 2 deletions sdk/tables/azure-data-tables/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.azure</groupId>
Expand Down Expand Up @@ -31,7 +32,6 @@ Licensed under the MIT License.
<tag>HEAD</tag>
</scm>

<!-- CosmosTableSkip - Needed temporary values to 10% not fail. -->
<properties>
<jacoco.skip.coverage.check>true</jacoco.skip.coverage.check>
</properties>
Expand All @@ -42,6 +42,29 @@ Licensed under the MIT License.
<artifactId>azure-core</artifactId>
<version>1.6.0</version> <!-- {x-version-update;com.azure:azure-core;dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-common</artifactId>
<version>12.7.0</version> <!-- {x-version-update;com.azure:azure-storage-common;dependency} -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.3.5.RELEASE</version> <!-- {x-version-update;io.projectreactor:reactor-test;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.3.1</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.data.tables;

import com.azure.storage.common.implementation.StorageImplUtils;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
* A Class which helps generate the shared key credentials for a given storage account to create a Http requests to
* access Azure Tables
*/
public class TablesSharedKeyCredential {
private static final String AUTHORIZATION_HEADER_FORMAT = "SharedKeyLite %s:%s";
private final String accountName;
private final String accountKey;

/**
* Constructor for TableSharedKeyCredential Class
*
* @param accountName name of the storage account
* @param accountKey key to the storage account
*/
public TablesSharedKeyCredential(String accountName, String accountKey) {
this.accountName = Objects.requireNonNull(accountName, "'accountName' cannot be null.");
this.accountKey = Objects.requireNonNull(accountKey, "'accountKey' cannot be null.");
}

/**
* Generates the Auth Headers
*
* @param requestUrl the URL which the request is going to
* @param headers the headers of the request
* @return the auth header
*/
public String generateAuthorizationHeader(URL requestUrl, Map<String, String> headers) {
String signature = StorageImplUtils.computeHMac256(accountKey, buildStringToSign(requestUrl,
headers));
return String.format(AUTHORIZATION_HEADER_FORMAT, accountName, signature);
}

/**
* creates the String to Sign
*
* @param requestUrl the Url which the request is going to
* @param headers the headers of the request
* @return a string to sign for the request
*/
private String buildStringToSign(URL requestUrl, Map<String, String> headers) {
String dateHeader = headers.containsKey("x-ms-date")
? ""
: this.getStandardHeaderValue(headers, "Date");
return String.join("\n",
dateHeader, //date
getCanonicalizedResource(requestUrl)); //Canonicalized resource
}

/**
* gets necessary headers if the request does not already contain them
*
* @param headers a map of the headers which the request has
* @param headerName the name of the header to get the standard header for
* @return the standard header for the given name
*/
private String getStandardHeaderValue(Map<String, String> headers, String headerName) {
String headerValue = headers.get(headerName);
return headerValue == null ? "" : headerValue;
}


/**
* returns the canonicalized resource needed for a request
*
* @param requestUrl the url of the request
* @return the string that is the canonicalized resource
*/
private String getCanonicalizedResource(URL requestUrl) {
StringBuilder canonicalizedResource = new StringBuilder("/").append(accountName);
if (requestUrl.getPath().length() > 0) {
canonicalizedResource.append(requestUrl.getPath());
} else {
canonicalizedResource.append('/');
}

if (requestUrl.getQuery() != null) {
Map<String, String[]> queryParams = StorageImplUtils.parseQueryStringSplitValues(requestUrl.getQuery());
ArrayList<String> queryParamNames = new ArrayList<>(queryParams.keySet());

Collections.sort(queryParamNames);

for (String queryParamName : queryParamNames) {
String[] queryParamValues = queryParams.get(queryParamName);

Arrays.sort(queryParamValues);

String queryParamValuesStr = String.join(",", queryParamValues);

if (queryParamName.equalsIgnoreCase("comp")) {
canonicalizedResource.append("?").append(queryParamName.toLowerCase(Locale.ROOT)).append("=")
.append(queryParamValuesStr);
}
}
}
return canonicalizedResource.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.data.tables;

import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpPipelinePolicy;
import reactor.core.publisher.Mono;

/**
* This class helps authenticate an Http request for the Tables service
*/
public final class TablesSharedKeyCredentialPolicy implements HttpPipelinePolicy {

private final TablesSharedKeyCredential credential;

/**
* constructor for the TablesSharedKeyCredentialPolicy class
*
* @param credential the credentials of the account
*/
public TablesSharedKeyCredentialPolicy(TablesSharedKeyCredential credential) {
this.credential = credential;
}

/**
* creates an Http response
*
* @param context the context of the http pipeline
* @param next the next Http pipeline policy
* @return an Http response
*/
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
String authorizationValue = credential.generateAuthorizationHeader(context.getHttpRequest().getUrl(),
context.getHttpRequest().getHeaders().toMap());
context.getHttpRequest().setHeader("Authorization", authorizationValue);
return next.process();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/** Package containing the inner classes for Azure Tables SDK. */
package com.azure.data.tables;
Loading

0 comments on commit 86afb16

Please sign in to comment.