-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[service-bus, event-hubs] Allowing for a SAS key to be directly speci…
…fied in a connection string. (#10951) Allow for connection strings to be passed that contain a Shared Access Signature. This expands core-amqp so it can parse the connection string into a SharedKeyCredential. So connection strings of this form are valid: `Endpoint=<endpoint>;SharedAccessSignature=SharedAccessSignature sr=<resource>...` This results in a token that is not renewable and that returns the SAS verbatim. Fixes #10832
- Loading branch information
1 parent
fcca834
commit 43bb11a
Showing
9 changed files
with
302 additions
and
16 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
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
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
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
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
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,70 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { | ||
parseConnectionString, | ||
ServiceBusConnectionStringModel, | ||
SharedKeyCredential | ||
} from "@azure/core-amqp"; | ||
import { EventHubConsumerClient } from "../src/eventHubConsumerClient"; | ||
import { EnvVarKeys, getEnvVars } from "./utils/testUtils"; | ||
import chai from "chai"; | ||
import { EventHubProducerClient } from "../src"; | ||
|
||
const should = chai.should(); | ||
const env = getEnvVars(); | ||
|
||
describe("Authentication via SAS", () => { | ||
const service = { | ||
connectionString: env[EnvVarKeys.EVENTHUB_CONNECTION_STRING], | ||
path: env[EnvVarKeys.EVENTHUB_NAME], | ||
fqdn: parseConnectionString<ServiceBusConnectionStringModel>( | ||
env[EnvVarKeys.EVENTHUB_CONNECTION_STRING] | ||
).Endpoint.replace(/\/+$/, "") | ||
}; | ||
|
||
before(() => { | ||
should.exist( | ||
env[EnvVarKeys.EVENTHUB_CONNECTION_STRING], | ||
"define EVENTHUB_CONNECTION_STRING in your environment before running integration tests." | ||
); | ||
should.exist( | ||
env[EnvVarKeys.EVENTHUB_NAME], | ||
"define EVENTHUB_NAME in your environment before running integration tests." | ||
); | ||
}); | ||
|
||
it("EventHubConsumerClient", async () => { | ||
const sasConnectionString = getSasConnectionString(); | ||
|
||
const consumerClient = new EventHubConsumerClient( | ||
"$Default", | ||
sasConnectionString, | ||
service.path | ||
); | ||
|
||
const properties = await consumerClient.getEventHubProperties(); | ||
should.exist(properties); | ||
|
||
await consumerClient.close(); | ||
}); | ||
|
||
it("EventHubProducerClient", async () => { | ||
const sasConnectionString = getSasConnectionString(); | ||
|
||
const producerClient = new EventHubProducerClient(sasConnectionString, service.path); | ||
|
||
const properties = await producerClient.getEventHubProperties(); | ||
should.exist(properties); | ||
|
||
await producerClient.close(); | ||
}); | ||
|
||
function getSasConnectionString(): string { | ||
const sas = SharedKeyCredential.fromConnectionString(service.connectionString).getToken( | ||
`${service.fqdn}/${service.path}` | ||
).token; | ||
|
||
return `Endpoint=${service.fqdn}/;SharedAccessSignature=${sas}`; | ||
} | ||
}); |
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
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
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,98 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { ServiceBusConnectionStringModel, SharedKeyCredential } from "@azure/core-amqp"; | ||
import chai from "chai"; | ||
import { parseConnectionString } from "rhea-promise"; | ||
import { ServiceBusReceiver } from "../src/receivers/receiver"; | ||
import { ServiceBusClient } from "../src/serviceBusClient"; | ||
import { ReceivedMessage } from "../src/serviceBusMessage"; | ||
import { getEnvVars } from "./utils/envVarUtils"; | ||
import { TestClientType } from "./utils/testUtils"; | ||
import { | ||
createServiceBusClientForTests, | ||
ServiceBusClientForTests, | ||
ServiceBusTestHelpers | ||
} from "./utils/testutils2"; | ||
const assert = chai.assert; | ||
|
||
type UnpackReturnType<T extends (...args: any) => any> = ReturnType<T> extends Promise<infer U> | ||
? U | ||
: never; | ||
|
||
[TestClientType.UnpartitionedQueue, TestClientType.UnpartitionedSubscription].forEach( | ||
(entityType) => { | ||
describe(`Authentication via SAS to ${TestClientType[entityType]}`, () => { | ||
let tempClient: ServiceBusClientForTests; | ||
let entities: UnpackReturnType<ServiceBusTestHelpers["createTestEntities"]>; | ||
let sasConnectionString: string; | ||
|
||
before(async () => { | ||
tempClient = await createServiceBusClientForTests(); | ||
entities = await tempClient.test.createTestEntities(entityType); | ||
|
||
const { SERVICEBUS_CONNECTION_STRING: serviceBusConnectionString } = getEnvVars(); | ||
|
||
const { Endpoint: fqdn } = parseConnectionString<ServiceBusConnectionStringModel>( | ||
serviceBusConnectionString | ||
); | ||
|
||
sasConnectionString = getSasConnectionString( | ||
serviceBusConnectionString, | ||
entities.queue ?? `${entities.topic!}`, | ||
fqdn.replace(/\/+$/, "") | ||
); | ||
}); | ||
|
||
after(async () => { | ||
await tempClient.test.afterEach(); | ||
await tempClient.test.after(); | ||
}); | ||
|
||
it("ServiceBusClient", async () => { | ||
const client = new ServiceBusClient(sasConnectionString); | ||
|
||
const sender = await tempClient.createSender(entities.queue ?? entities.topic!); | ||
|
||
await sender.sendMessages({ | ||
body: "Hello" | ||
}); | ||
|
||
await sender.close(); | ||
|
||
let receiver: ServiceBusReceiver<ReceivedMessage>; | ||
|
||
if (entities.queue) { | ||
receiver = client.createReceiver(entities.queue!, { | ||
receiveMode: "receiveAndDelete" | ||
}); | ||
} else { | ||
receiver = client.createReceiver(entities.topic!, entities.subscription!, { | ||
receiveMode: "receiveAndDelete" | ||
}); | ||
} | ||
|
||
const messages = await receiver.receiveMessages(1, { | ||
maxWaitTimeInMs: 10 * 1000 | ||
}); | ||
|
||
await receiver.close(); | ||
|
||
assert.equal(messages.length, 1, "Should have received at least one message"); | ||
await client.close(); | ||
}); | ||
|
||
function getSasConnectionString( | ||
connectionString: string, | ||
path: string, | ||
fqdn: string | ||
): string { | ||
const sas = SharedKeyCredential.fromConnectionString(connectionString).getToken( | ||
`${fqdn}/${path}` | ||
).token; | ||
|
||
return `Endpoint=${fqdn};SharedAccessSignature=${sas}`; | ||
} | ||
}); | ||
} | ||
); |