Skip to content

Commit

Permalink
[Communication] - SMS - Managed Identity Support for SMS Client (#17867)
Browse files Browse the repository at this point in the history
* Managed Identity for SMS

* Adding readme env variables detail

* Fix changelog

* Adding live tests for managed identity SMS

* Resolve merge conflicts

* Remove extra connection string variable

* Remove endpoint and parse from connection string

* Updating tsts

Co-authored-by: Minnie Liu <[email protected]>
  • Loading branch information
minnieliu and Minnie Liu authored Jan 23, 2021
1 parent 4297de2 commit 103028a
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/communication/Azure.Communication.Sms/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
- Added support to create SmsClient with AzureKeyCredential.
- Support for creating SmsClient with TokenCredential


## 1.0.0-beta.3 (2020-11-16)
Expand Down
9 changes: 9 additions & 0 deletions sdk/communication/Azure.Communication.Sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ string connectionString = "YOUR_CONNECTION_STRING"; // Find your Communication S
SmsClient client = new SmsClient(connectionString);
```

Alternatively, SMS clients can also be authenticated using a valid token credential. With this option,
`AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID` and `AZURE_TENANT_ID` environment variables need to be set up for authentication.

```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
```

## Examples
### Send a SMS Message
To send a SMS message, call the `Send` or `SendAsync` function from the `SmsClient`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class SmsClient
protected SmsClient() { }
public SmsClient(string connectionString, Azure.Communication.Sms.SmsClientOptions? options = null) { }
public SmsClient(System.Uri endpoint, Azure.AzureKeyCredential keyCredential, Azure.Communication.Sms.SmsClientOptions? options = null) { }
public SmsClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.Sms.SmsClientOptions? options = null) { }
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, System.Collections.Generic.IEnumerable<Azure.Communication.PhoneNumberIdentifier> to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.Sms.SendSmsResponse>> SendAsync(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
23 changes: 23 additions & 0 deletions sdk/communication/Azure.Communication.Sms/src/SmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public SmsClient(string connectionString, SmsClientOptions? options = default)
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))))
{ }

/// <summary> Initializes a new instance of <see cref="SmsClient"/>.</summary>
/// <param name="endpoint">The URI of the Azure Communication Services resource.</param>
/// <param name="tokenCredential">The TokenCredential used to authenticate requests, such as DefaultAzureCredential.</param>
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public SmsClient(Uri endpoint, TokenCredential tokenCredential, SmsClientOptions? options = default)
: this(
endpoint,
options ?? new SmsClientOptions(),
tokenCredential)
{ }

/// <summary>Initializes a new instance of <see cref="SmsClient"/> for mocking.</summary>
protected SmsClient()
{
Expand All @@ -60,6 +71,18 @@ private SmsClient(SmsClientOptions options, ConnectionString connectionString)
endpointUrl: connectionString.GetRequired("endpoint"))
{ }

private SmsClient(Uri endpoint, SmsClientOptions options, TokenCredential tokenCredential)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new SmsRestClient(
_clientDiagnostics,
options.BuildHttpPipeline(tokenCredential),
endpoint.AbsoluteUri);
}

private SmsClient(Uri endpoint, SmsClientOptions options, AzureKeyCredential credential)
{
_clientDiagnostics = new ClientDiagnostics(options);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
//@@ using Azure.Communication.Sms;
#endregion Snippet:Azure_Communication_Sms_Tests_UsingStatements
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using Azure.Identity;
using NUnit.Framework;

namespace Azure.Communication.Sms.Tests
Expand Down Expand Up @@ -48,5 +50,42 @@ public async Task SendingAnSmsMessage()
Assert.Fail($"Unexpected error: {ex}");
}
}

[Test]
public async Task SendingAnSmsMessageUsingTokenCredential()
{
TokenCredential tokenCredential;
if (Mode == RecordedTestMode.Playback)
{
tokenCredential = new MockCredential();
}
else
{
tokenCredential = new DefaultAzureCredential();
}
SmsClient client = InstrumentClient(
new SmsClient(
new Uri(ConnectionString.Parse(TestEnvironment.ConnectionString, allowEmptyValues: true).GetRequired("endpoint")),
tokenCredential,
InstrumentClientOptions(new SmsClientOptions())));

try
{
SendSmsResponse result = await client.SendAsync(
from: new PhoneNumberIdentifier(TestEnvironment.PhoneNumber),
to: new PhoneNumberIdentifier(TestEnvironment.PhoneNumber),
message: "Hi");
Console.WriteLine($"Sms id: {result.MessageId}");
Assert.IsFalse(string.IsNullOrWhiteSpace(result.MessageId));
}
catch (RequestFailedException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Assert.Fail($"Unexpected error: {ex}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SmsClientTestEnvironment : TestEnvironment
public string ConnectionString => GetRecordedVariable(CommunicationRecordedTestSanitizer.ConnectionStringEnvironmentVariableName);

internal const string PhoneNumberEnvironmentVariableName = "AZURE_PHONE_NUMBER";

public string PhoneNumber => GetRecordedVariable(PhoneNumberEnvironmentVariableName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;
using Azure.Identity;

namespace Azure.Communication.Sms.Tests.samples
{
/// <summary>
Expand All @@ -16,5 +20,14 @@ public SmsClient CreateSmsClient()
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClient
return client;
}
public SmsClient CreateSmsClientWithToken()
{
#region Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
return client;
}
}
}

0 comments on commit 103028a

Please sign in to comment.