Skip to content

Commit 487ebab

Browse files
authored
Merge pull request #102849 from ealsur/users/ealsur/emulinux
Cosmos DB - Emulator on Linux/Mac
2 parents 7f0696a + 33ab849 commit 487ebab

File tree

2 files changed

+32
-52
lines changed

2 files changed

+32
-52
lines changed

articles/cosmos-db/local-emulator.md

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,7 @@ To open the Data Explorer, navigate to the following URL in your browser. The em
414414
415415
https://<emulator endpoint provided in response>/_explorer/index.html
416416
417-
If you have a .NET client application running on a Linux docker container and if you are running Azure Cosmos emulator on a host machine, in this case you can’t connect to the Azure Cosmos account from the emulator. Because the app is not running on the host machine, the certificate registered on the Linux container that matches the emulator’s endpoint cannot be added.
418-
419-
As a workaround, you can disable the server’s SSL certificate validation from your client application by passing a `HttpClientHandler` instance as shown in the following .Net code sample. This workaround is only applicable if you are using the `Microsoft.Azure.DocumentDB` Nuget package, it isn't supported with the `Microsoft.Azure.Cosmos` Nuget package:
420-
421-
```csharp
422-
var httpHandler = new HttpClientHandler()
423-
{
424-
ServerCertificateCustomValidationCallback = (req,cert,chain,errors) => true
425-
};
426-
427-
using (DocumentClient client = new DocumentClient(new Uri(strEndpoint), strKey, httpHandler))
428-
{
429-
RunDatabaseDemo(client).GetAwaiter().GetResult();
430-
}
431-
```
432-
433-
In addition to disabling the SSL certificate validation, it is important that you start the emulator with the `/allownetworkaccess` option and the emulator’s endpoint is accessible from the host IP address rather than `host.docker.internal` DNS.
417+
If you have a .NET client application running on a Linux docker container and if you are running Azure Cosmos emulator on a host machine, please follow the below section for Linux to import the certificate into the Linux docker container.
434418
435419
## Running on Mac or Linux<a id="mac"></a>
436420
@@ -442,48 +426,44 @@ Within the Windows VM run the command below and make note of the IPv4 address.
442426
ipconfig.exe
443427
```
444428
445-
Within your application you need to change the URI for the DocumentClient object to use the IPv4 address returned by `ipconfig.exe`. The next step is to work around the CA validation when constructing the DocumentClient object. For this you will need to provide an HttpClientHandler to the DocumentClient constructor, which has it's own implementation for ServerCertificateCustomValidationCallback.
429+
Within your application you need to change the URI used as Endpoint to use the IPv4 address returned by `ipconfig.exe` instead of `localhost`.
446430
447-
Below is an example of what the code should look like.
431+
The next step, from the within the Windows VM, launch the Cosmos emulator from the command line using the following options.
448432
449-
```csharp
450-
using System;
451-
using Microsoft.Azure.Documents;
452-
using Microsoft.Azure.Documents.Client;
453-
using System.Net.Http;
454-
455-
namespace emulator
456-
{
457-
class Program
458-
{
459-
static async void Main(string[] args)
460-
{
461-
string strEndpoint = "https://10.135.16.197:8081/"; //IPv4 address from ipconfig.exe
462-
string strKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
463-
464-
//Work around the CA validation
465-
var httpHandler = new HttpClientHandler()
466-
{
467-
ServerCertificateCustomValidationCallback = (req,cert,chain,errors) => true
468-
};
469-
470-
//Pass http handler to document client
471-
using (DocumentClient client = new DocumentClient(new Uri(strEndpoint), strKey, httpHandler))
472-
{
473-
Database database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "myDatabase" });
474-
Console.WriteLine($"Created Database: id - {database.Id} and selfLink - {database.SelfLink}");
475-
}
476-
}
477-
}
478-
}
433+
```cmd
434+
Microsoft.Azure.Cosmos.Emulator.exe /AllowNetworkAccess /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
479435
```
480436
481-
Finally, from the within the Windows VM, launch the Cosmos emulator from the command line using the following options.
437+
Finally, we need to import the Emulator CA certificate into the Linux or Mac environment.
482438
483-
```cmd
484-
Microsoft.Azure.Cosmos.Emulator.exe /AllowNetworkAccess /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
439+
If you are working on Linux, .NET relays on OpenSSL to do the validation:
440+
441+
1. [Export the certificate in PFX format](./local-emulator-export-ssl-certificates.md#how-to-export-the-azure-cosmos-db-ssl-certificate) (PFX is available when choosing to export the private key).
442+
2. Copy that PFX file into your Linux environment.
443+
3. Convert the PFX file into a CRT file
444+
```bash
445+
openssl pkcs12 -in YourPFX.pfx -clcerts -nokeys -out YourCTR.crt
446+
```
447+
4. Copy the CRT file to the folder that contains custom certificates in your Linux distribution. Commonly on Debian distributions, it is located on `/usr/local/share/ca-certificates/`.
448+
```bash
449+
cp YourCTR.crt /usr/local/share/ca-certificates/
450+
```
451+
5. Update the CA certificates, which will update the `/etc/ssl/certs/` folder.
452+
```bash
453+
update-ca-certificates
485454
```
486455
456+
If you are working on Mac:
457+
458+
1. [Export the certificate in PFX format](./local-emulator-export-ssl-certificates.md#how-to-export-the-azure-cosmos-db-ssl-certificate) (PFX is available when choosing to export the private key).
459+
2. Copy that PFX file into your Mac environment.
460+
3. Open the *Keychain Access* application and import the PFX file.
461+
4. Open the list of Certificates and identify the one with the name `localhost`.
462+
5. Open the context menu for that particular item, select *Get Item* and under *Trust* > *When using this certificate* option, select *Always Trust*.
463+
![Open the context menu for that particular item, select Get Item and under Trust - When using this certificate option, select Always Trust](./media/local-emulator/mac-trust-certificate.png)
464+
465+
After following these steps, your environment will trust the certificate used by the Emulator when connecting to the IP address exposes by `/AllowNetworkAccess`.
466+
487467
## Troubleshooting
488468
489469
Use the following tips to help troubleshoot issues you encounter with the Azure Cosmos Emulator:
169 KB
Loading

0 commit comments

Comments
 (0)