Skip to content

Commit

Permalink
[App Config] Migrate to core-v2 (#20766)
Browse files Browse the repository at this point in the history
* app-config changes - checkpoint- only 3 test failures

* core-rest-pipeline changes

* test changes

* credential fix

* skip { timeout: 1 } tests in playback for now

* lint:fix

* expand throttlingRetryStrategy with "retry-after-ms", "x-ms-retry-after-ms" headers

* Expand test suite with "retry-after-ms" and "x-ms-retry-after-ms"

* changelog

* lock file

* rely on core's throttling policy

* changelog

* "version": "1.3.2"  -> "version": "1.4.0",

* Update sdk/core/core-rest-pipeline/src/retryStrategies/throttlingRetryStrategy.ts

* no new variable

* no internal tag

* refactor

* new lines for readability

* more refactor

* Update sdk/core/core-rest-pipeline/src/retryStrategies/throttlingRetryStrategy.ts

* part of the feedback

* lint

* Update sdk/core/core-rest-pipeline/src/retryStrategies/throttlingRetryStrategy.ts

Co-authored-by: Jeff Fisher <[email protected]>

* format and lock file

* lock file

* lock file

* sarangan feedback

* @azure/core-http-compat

* remove comments

* default

* do not let status code to be 0

* api report

* lock file

* lock file

* lock file and formatting

* _response is back

* CHANGELOG

* 1.4.0-beta.1

* revert

* lock file

* fixes

* add options

* version automate updater

* regenrate after deleting to remove appConfigContext file

* simplify HttpResponseField instances

* helper updates

* assertResponse as suggested by Jeff

* fix tests

* fix tests

* changes in core-compat

* status code is never undefined

* export utils

* api report

* api report

* format

* lock filke

* changelog

* TransferProgressEvent

* 1.3.0

* feedback address

* Update sdk/appconfiguration/app-configuration/src/internal/helpers.ts

Co-authored-by: Jeff Fisher <[email protected]>

* Update sdk/appconfiguration/app-configuration/src/internal/synctokenpolicy.ts

Co-authored-by: Jeff Fisher <[email protected]>

* unneeded excess code

* don't throw

* remove toUpperCase

Co-authored-by: Jeff Fisher <[email protected]>
  • Loading branch information
HarshaNalluru and xirzec authored Apr 7, 2022
1 parent 8f0a73b commit 2a07ed3
Show file tree
Hide file tree
Showing 35 changed files with 2,623 additions and 3,144 deletions.
3,665 changes: 1,897 additions & 1,768 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions sdk/appconfiguration/app-configuration/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Release History

## 1.3.2 (Unreleased)
## 1.4.0-beta.1 (Unreleased)

### Features Added

- Migrated from using `@azure/core-http` to depend on newer version of Core libraries `@azure/core-client` and `@azure/core-rest-pipeline` which bring better maintainability and performance. [#20766](https://github.com/Azure/azure-sdk-for-js/pull/20766). See [Azure Core v1 vs v2](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-rest-pipeline/documentation/core2.md) for more on the difference and benefits of the move.
- As part of the migration to `@azure/core-client` and `@azure/core-rest-pipeline`,
through the operation options, the methods such as `AppConfgurationClient#addConfigurationSetting` can take a callback (`onResponse`) to access the HTTP response. See [Change to the \_response property](https://github.com/Azure/autorest.typescript/wiki/%60core-http%60-dependency-migration-to-%60core-client%60-%60core-rest-pipeline%60#change-to-the-_response-property).

### Breaking Changes

### Bugs Fixed
Expand Down Expand Up @@ -101,8 +105,8 @@ This release marks the general availability of the `@azure/app-configuration` pa
```typescript
new AppConfigurationClient(connectionString, {
userAgentOptions: {
userAgentPrefix: "MyUserAgent"
}
userAgentPrefix: "MyUserAgent",
},
});
```

Expand Down Expand Up @@ -158,11 +162,11 @@ In previous previews:
await client.getConfigurationSetting("MyKey", { label: "MyLabel" });
await client.addConfigurationSetting("MyKey", {
label: "MyLabel",
value: "MyValue"
value: "MyValue",
});
await client.setConfigurationSetting("MyKey", {
label: "MyLabel",
value: "MyValue"
value: "MyValue",
});
await client.deleteConfigurationSetting("MyKey", { label: "MyLabel" });
```
Expand All @@ -175,12 +179,12 @@ await client.getConfigurationSetting({ key: "MyKey", label: "MyLabel" });
await client.addConfigurationSetting({
key: "MyKey",
label: "MyLabel",
value: "MyValue"
value: "MyValue",
});
await client.setConfigurationSetting({
key: "MyKey",
label: "MyLabel",
value: "MyValue"
value: "MyValue",
});
await client.deleteConfigurationSetting({ key: "MyKey", label: "MyLabel" });
```
Expand Down
12 changes: 10 additions & 2 deletions sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@azure/app-configuration",
"author": "Microsoft Corporation",
"description": "An isomorphic client library for the Azure App Configuration service.",
"version": "1.3.2",
"version": "1.4.0-beta.1",
"sdk-type": "client",
"keywords": [
"node",
Expand Down Expand Up @@ -81,14 +81,20 @@
{
"path": "src/generated/src/appConfigurationContext.ts",
"prefix": "packageVersion"
},
{
"path": "swagger/swagger.md",
"prefix": "package-version"
}
]
},
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-asynciterator-polyfill": "^1.0.0",
"@azure/core-http": "^2.0.0",
"@azure/core-client": "^1.5.0",
"@azure/core-http-compat": "^1.2.0",
"@azure/core-paging": "^1.1.1",
"@azure/core-rest-pipeline": "^1.6.0",
"@azure/core-tracing": "1.0.0-preview.13",
"@azure/core-auth": "^1.3.0",
"tslib": "^2.2.0"
Expand All @@ -110,6 +116,7 @@
"@types/mocha": "^7.0.2",
"@types/node": "^12.0.0",
"@types/sinon": "^9.0.4",
"@types/uuid": "^8.3.4",
"chai": "^4.2.0",
"dotenv": "^8.2.0",
"eslint": "^7.15.0",
Expand Down Expand Up @@ -139,6 +146,7 @@
"ts-node": "^10.0.0",
"typescript": "~4.2.0",
"uglify-js": "^3.4.9",
"uuid": "^8.3.0",
"cross-env": "^7.0.2"
},
"//sampleConfiguration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

/// <reference lib="esnext.asynciterable" />

import { HttpResponse } from '@azure/core-http';
import { OperationOptions } from '@azure/core-http';
import { CommonClientOptions } from '@azure/core-client';
import { CompatResponse } from '@azure/core-http-compat';
import { OperationOptions } from '@azure/core-client';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { TokenCredential } from '@azure/core-auth';
import { UserAgentOptions } from '@azure/core-http';

// @public
export interface AddConfigurationSettingOptions extends OperationOptions {
Expand Down Expand Up @@ -38,9 +38,7 @@ export class AppConfigurationClient {
}

// @public
export interface AppConfigurationClientOptions {
retryOptions?: RetryOptions;
userAgentOptions?: UserAgentOptions;
export interface AppConfigurationClientOptions extends CommonClientOptions {
}

// @public
Expand Down Expand Up @@ -124,7 +122,7 @@ export interface HttpOnlyIfUnchangedField {

// @public
export interface HttpResponseField<HeadersT> {
_response: HttpResponse & {
_response: CompatResponse & {
parsedHeaders: HeadersT;
bodyAsText: string;
};
Expand Down
80 changes: 33 additions & 47 deletions sdk/appconfiguration/app-configuration/src/appConfigCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,41 @@
// Licensed under the MIT license.

import {
ServiceClientCredentials,
WebResource,
URLBuilder,
WebResourceLike,
} from "@azure/core-http";
PipelinePolicy,
PipelineRequest,
PipelineResponse,
SendRequest,
} from "@azure/core-rest-pipeline";
import { sha256Digest, sha256Hmac } from "./internal/cryptoHelpers";

/**
* @internal
* Create an HTTP pipeline policy to authenticate a request
* using an `AzureKeyCredential` for AppConfig.
*/
export class AppConfigCredential implements ServiceClientCredentials {
private credential: string;
private secret: string;

constructor(credential: string, secret: string) {
this.credential = credential;
this.secret = secret;
}

/**
* Signs a request with the values provided in the credential and secret parameter.
*
* @param webResource - The WebResource to be signed.
* @returns The signed request object.
*/
async signRequest(webResource: WebResourceLike): Promise<WebResource> {
const verb = webResource.method.toUpperCase();
const utcNow = new Date().toUTCString();

const contentHash = await sha256Digest(webResource.body || "");

const signedHeaders = "x-ms-date;host;x-ms-content-sha256";

const url = URLBuilder.parse(webResource.url);
const query = url.getQuery();
const urlPathAndQuery = `${url.getPath()}${query ? "?" + query : ""}`;

const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${url.getHost()};${contentHash}`;

const signature = await sha256Hmac(this.secret, stringToSign);

webResource.headers.set("x-ms-date", utcNow);
webResource.headers.set("x-ms-content-sha256", contentHash);
// Syntax for Authorization header
// Reference - https://docs.microsoft.com/en-us/azure/azure-app-configuration/rest-api-authentication-hmac#syntax
webResource.headers.set(
"Authorization",
`HMAC-SHA256 Credential=${this.credential}&SignedHeaders=${signedHeaders}&Signature=${signature}`
);
return webResource;
}
export function appConfigKeyCredentialPolicy(credential: string, secret: string): PipelinePolicy {
return {
name: "AppConfigKeyCredentialPolicy",
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
const verb = request.method;
const utcNow = new Date().toUTCString();
const contentHash = await sha256Digest(request.body?.toString() || "");
const signedHeaders = "x-ms-date;host;x-ms-content-sha256";
const url = new URL(request.url);
const query = url.search;
const urlPathAndQuery = query ? `${url.pathname}${query}` : url.pathname;
const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${url.host};${contentHash}`;
const signature = await sha256Hmac(secret, stringToSign);

request.headers.set("x-ms-date", utcNow);
request.headers.set("x-ms-content-sha256", contentHash);
// Syntax for Authorization header
// Reference - https://docs.microsoft.com/en-us/azure/azure-app-configuration/rest-api-authentication-hmac#syntax
request.headers.set(
"Authorization",
`HMAC-SHA256 Credential=${credential}&SignedHeaders=${signedHeaders}&Signature=${signature}`
);

return next(request);
},
};
}
Loading

0 comments on commit 2a07ed3

Please sign in to comment.