Skip to content

Commit

Permalink
[Tables] Create clients from connectionString (#10292)
Browse files Browse the repository at this point in the history
* Initial changes to support SharedKey auth

* Add test for utility helpers

* re-enable pipeline validation on PR

* Add javascript samples

* update sample require

* Address PR comments

* Add url to externalNodeBuiltins

* Support SharedKeyCredential

* Rename ShareKeyCredential to TablesSharedKeyCredential

* Address PR comments

* Fix file name TablesShredKeyCredential
  • Loading branch information
joheredi authored Jul 31, 2020
1 parent 1645bf7 commit 5245f7e
Show file tree
Hide file tree
Showing 29 changed files with 1,110 additions and 18 deletions.
11 changes: 8 additions & 3 deletions sdk/tables/azure-tables/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"sdk-type": "client",
"main": "dist/index.js",
"module": "dist-esm/src/index.js",
"browser": {
"./dist-esm/src/TablesSharedKeyCredential.js": "./dist-esm/src/TablesSharedKeyCredential.browser.js",
"./dist-esm/src/utils/fromAccountConnectionString.js": "./dist-esm/src/utils/fromAccountConnectionString.browser.js",
"./dist-esm/src/utils/url.js": "./dist-esm/src/utils/url.browser.js"
},
"types": "types/latest/tables.d.ts",
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
Expand All @@ -30,8 +35,8 @@
"test:browser": "npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "echo skipped",
"unit-test:node": "echo skipped",
"unit-test:browser": "karma start --single-run",
"unit-test:node": "mocha --reporter ../../../common/tools/mocha-multi-reporter.js dist-test/index.node.js",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"files": [
Expand Down Expand Up @@ -117,4 +122,4 @@
"typescript": "~3.9.3",
"util": "^0.12.1"
}
}
}
25 changes: 25 additions & 0 deletions sdk/tables/azure-tables/review/tables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
```ts

import { BaseRequestPolicy } from '@azure/core-http';
import * as coreHttp from '@azure/core-http';
import { HttpOperationResponse } from '@azure/core-http';
import { RequestPolicy } from '@azure/core-http';
import { RequestPolicyFactory } from '@azure/core-http';
import { RequestPolicyOptions } from '@azure/core-http';
import { WebResource } from '@azure/core-http';

// @public
export interface AccessPolicy {
Expand Down Expand Up @@ -289,9 +295,11 @@ export interface SignedIdentifier {

// @public
export class TableClient {
constructor(url: string, tableName: string, credential: TablesSharedKeyCredential, options?: TableServiceClientOptions);
constructor(url: string, tableName: string, options?: TableServiceClientOptions);
createEntity(entity?: Entity, options?: CreateEntityOptions): Promise<CreateEntityResponse>;
deleteEntity(partitionKey: string, rowKey: string, ifMatch: string, options?: DeleteEntityOptions): Promise<DeleteEntityResponse>;
static fromConnectionString(connectionString: string, tableName: string, options?: TableServiceClientOptions): TableClient;
getAccessPolicy(options?: GetAccessPolicyOptions): Promise<GetAccessPolicyResponse>;
getEntity(partitionKey: string, rowKey: string, options?: GetEntityOptions): Promise<GetEntityResponse>;
listEntities(query?: QueryOptions, options?: ListEntitiesOptions): Promise<ListEntitiesResponse>;
Expand Down Expand Up @@ -471,11 +479,13 @@ export interface TableResponseProperties {

// @public
export class TableServiceClient {
constructor(url: string, credential: TablesSharedKeyCredential, options?: TableServiceClientOptions);
constructor(url: string, options?: TableServiceClientOptions);
createEntity(tableName: string, entity?: Entity, options?: CreateEntityOptions): Promise<CreateEntityResponse>;
createTable(tableName: string, options?: CreateTableOptions): Promise<CreateTableResponse>;
deleteEntity(tableName: string, partitionKey: string, rowKey: string, ifMatch: string, options?: DeleteEntityOptions): Promise<DeleteEntityResponse>;
deleteTable(tableName: string, options?: DeleteTableOptions): Promise<DeleteTableResponse>;
static fromConnectionString(connectionString: string, options?: TableServiceClientOptions): TableServiceClient;
getAccessPolicy(tableName: string, options?: GetAccessPolicyOptions): Promise<GetAccessPolicyResponse>;
getEntity(tableName: string, partitionKey: string, rowKey: string, options?: GetEntityOptions): Promise<GetEntityResponse>;
getProperties(options?: GetPropertiesOptions): Promise<GetPropertiesResponse>;
Expand Down Expand Up @@ -514,6 +524,21 @@ export interface TableSetAccessPolicyOptionalParams extends coreHttp.OperationOp
timeout?: number;
}

// @public
export class TablesSharedKeyCredential implements RequestPolicyFactory {
constructor(accountName: string, accountKey: string);
readonly accountName: string;
computeHMACSHA256(stringToSign: string): string;
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): TablesSharedKeyCredentialPolicy;
}

// @public
export class TablesSharedKeyCredentialPolicy extends BaseRequestPolicy {
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions, factory: TablesSharedKeyCredential);
sendRequest(request: WebResource): Promise<HttpOperationResponse>;
protected signRequest(request: WebResource): WebResource;
}

// @public
export interface TableUpdateEntityHeaders {
clientRequestId?: string;
Expand Down
2 changes: 1 addition & 1 deletion sdk/tables/azure-tables/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const input = "dist-esm/src/index.js";
const production = process.env.NODE_ENV === "production";

export function nodeConfig(test = false) {
const externalNodeBuiltins = [];
const externalNodeBuiltins = ["crypto", "url"];
const baseConfig = {
input: input,
external: depNames.concat(externalNodeBuiltins),
Expand Down
33 changes: 33 additions & 0 deletions sdk/tables/azure-tables/samples/javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "azure-tables-samples-js",
"private": true,
"version": "0.1.0",
"description": "Azure Tables client library samples for Javascript",
"engine": {
"node": ">=8.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
},
"keywords": [
"Azure",
"Storage",
"Tables",
"Node.js"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js#readme",
"sideEffects": false,
"dependencies": {
"@azure/tables": "../..",
"dotenv": "^8.2.0"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
34 changes: 34 additions & 0 deletions sdk/tables/azure-tables/samples/javascript/src/connectionString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

const { TableServiceClient, TableClient } = require("@azure/tables");

// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();

const connectionString = process.env["ACCOUNT_CONNECTION_STRING"] || "";
// const connectionString = process.env["SAS_CONNECTION_STRING"] || "";

async function listTables() {
const client = TableServiceClient.fromConnectionString(connectionString);

const tables = await client.listTables();

console.log(tables.value);
}

async function listEntities() {
const client = TableClient.fromConnectionString(connectionString, "test1");

const entities = await client.listEntities();

console.log(entities.value);
}

async function main() {
await listTables();
await listEntities();
}

main().catch(console.error);
37 changes: 37 additions & 0 deletions sdk/tables/azure-tables/samples/javascript/src/sasToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

const { TableServiceClient, TableClient } = require("@azure/tables");

// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();

const accountSas = process.env["ACCOUNT_SAS"] || "";
const accountName = process.env["ACCOUNT_NAME"] || "";

async function listTables() {
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
const client = new TableServiceClient(accountUrl);

const tables = await client.listTables();

console.log(tables.value);
}

async function listEntities() {
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
const tableName = "test1";
const client = new TableClient(accountUrl, tableName);

const entities = await client.listEntities();

console.log(entities.value);
}

async function main() {
await listTables();
await listEntities();
}

main().catch(console.error);
38 changes: 38 additions & 0 deletions sdk/tables/azure-tables/samples/javascript/src/sharedKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

const { TableServiceClient, TableClient, TablesSharedKeyCredential } = require("@azure/tables");

// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();

const accountName = process.env["ACCOUNT_NAME"] || "";
const accountKey = process.env["ACCOUNT_KEY"] || "";
const accountUrl = process.env["ACCOUNT_URL"] || "";

async function listTables() {
const credential = new TablesSharedKeyCredential(accountName, accountKey);
const client = new TableServiceClient(accountUrl, credential);

const tables = await client.listTables();

console.log(tables.value);
}

async function listEntities() {
const tableName = "test1";
const credential = new TablesSharedKeyCredential(accountName, accountKey);
const client = new TableClient(accountUrl, tableName, credential);

const entities = await client.listEntities();

console.log(entities.value);
}

async function main() {
await listTables();
await listEntities();
}

main().catch(console.error);
6 changes: 6 additions & 0 deletions sdk/tables/azure-tables/samples/javascript/test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ACCOUNT_CONNECTION_STRING=
SAS_CONNECTION_STRING=
ACCOUNT_NAME=
ACCOUNT_KEY=
ACCOUNT_URL=
ACCOUNT_SAS=
40 changes: 40 additions & 0 deletions sdk/tables/azure-tables/samples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "azure-tables-samples-ts",
"private": true,
"version": "0.1.0",
"description": "Azure Tables client library samples for TypeScript",
"engine": {
"node": ">=8.0.0"
},
"scripts": {
"build": "tsc",
"prebuild": "rimraf dist/"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
},
"keywords": [
"Azure",
"Storage",
"Tables",
"Node.js",
"TypeScript"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js#readme",
"sideEffects": false,
"dependencies": {
"@azure/tables": "../..",
"dotenv": "^8.2.0"
},
"devDependencies": {
"@types/node": "^8.0.0",
"rimraf": "^3.0.0",
"typescript": "~3.6.4"
}
}
33 changes: 33 additions & 0 deletions sdk/tables/azure-tables/samples/typescript/src/connectionString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TableServiceClient, TableClient } from "@azure/tables";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

const connectionString = process.env["ACCOUNT_CONNECTION_STRING"] || "";
// const connectionString = process.env["SAS_CONNECTION_STRING"] || "";
async function listTables() {
const client = TableServiceClient.fromConnectionString(connectionString);

const tables = await client.listTables();

console.log(tables.value);
}

async function listEntities() {
const client = TableClient.fromConnectionString(connectionString, "test1");

const entities = await client.listEntities();

console.log(entities.value);
}

async function main() {
await listTables();
await listEntities();
}

main().catch(console.error);
37 changes: 37 additions & 0 deletions sdk/tables/azure-tables/samples/typescript/src/sasToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TableServiceClient, TableClient } from "@azure/tables";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

const accountSas = process.env["ACCOUNT_SAS"] || "";
const accountName = process.env["ACCOUNT_NAME"] || "";

async function listTables() {
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
const client = new TableServiceClient(accountUrl);

const tables = await client.listTables();

console.log(tables.value);
}

async function listEntities() {
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
const tableName = "test1";
const client = new TableClient(accountUrl, tableName);

const entities = await client.listEntities();

console.log(entities.value);
}

async function main() {
await listTables();
await listEntities();
}

main().catch(console.error);
Loading

0 comments on commit 5245f7e

Please sign in to comment.