-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing for Tables Autorest Code (#12512)
* testing for Tables Autorest Code
- Loading branch information
Showing
24 changed files
with
2,631 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Release History | ||
|
||
## 1.0.0-beta.1 (Unreleased) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...bles/azure-data-tables/src/main/java/com/azure/data/tables/TablesSharedKeyCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...zure-data-tables/src/main/java/com/azure/data/tables/TablesSharedKeyCredentialPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sdk/tables/azure-data-tables/src/main/java/com/azure/data/tables/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.