Udostępnij za pośrednictwem


Integrate the Azure Cosmos DB for Gremlin with Service Connector

Na tej stronie przedstawiono obsługiwane metody uwierzytelniania i klientów oraz pokazano przykładowy kod, za pomocą którego można połączyć usługę Azure Cosmos DB dla języka Apache Gremlin z innymi usługami w chmurze przy użyciu łącznika usług. Nadal możesz nawiązać połączenie z usługą Azure Cosmos DB dla języka Gremlin w innych językach programowania bez używania łącznika usługi. Ta strona zawiera również domyślne nazwy zmiennych środowiskowych i wartości uzyskiwane podczas tworzenia połączenia z usługą.

Obsługiwane usługi obliczeniowe

Łącznik usługi może służyć do łączenia następujących usług obliczeniowych z Azure Cosmos DB dla Apache Gremlin:

  • Azure App Service
  • Azure Container Apps
  • Azure Functions
  • Azure Kubernetes Service (AKS)
  • Azure Spring Apps

Obsługiwane typy uwierzytelniania i typy klientów

The table below shows which combinations of client types and authentication methods are supported for connecting your compute service to Azure Cosmos DB for Apache Gremlin using Service Connector. Wartość "Tak" wskazuje, że kombinacja jest obsługiwana, a wartość "Nie" wskazuje, że nie jest obsługiwana.

Typ klienta System-assigned managed identity User-assigned managed identity Secret / connection string Service principal
.NET Tak Tak Tak Tak
Java Tak Tak Tak Tak
Node.js Tak Tak Tak Tak
PHP Tak Tak Tak Tak
Python Tak Tak Tak Tak
Idź Tak Tak Tak Tak
None Tak Tak Tak Tak

Ta tabela wskazuje, że obsługiwane są wszystkie kombinacje typów klientów i metod uwierzytelniania w tabeli. Wszystkie rodzaje klientów mogą używać dowolnej metody uwierzytelniania do połączenia z Azure Cosmos DB dla Apache Gremlin z wykorzystaniem Łącznika Usługi.

Uwaga

Usługa Cosmos DB nie obsługuje natywnie uwierzytelniania za pośrednictwem tożsamości zarządzanej. W związku z tym łącznik usługi używa tożsamości zarządzanej do pobierania parametrów połączenia, a połączenie jest następnie ustanawiane przy użyciu tych parametrów połączenia.

Domyślne nazwy zmiennych środowiskowych lub właściwości aplikacji i przykładowy kod

Użyj poniższych szczegółów połączenia, aby połączyć usługi obliczeniowe z bazą danych Azure Cosmos DB dla Apache Gremlin. W każdym poniższym przykładzie zastąp teksty zastępcze <Azure-Cosmos-DB-account>, <database>, <collection or graphs>, <username>, <password>, <resource-group-name>, <subscription-ID>, <client-ID>, <client-secret> i <tenant-id> własnymi informacjami. Aby uzyskać więcej informacji na temat konwencji nazewnictwa, zajrzyj do artykułu o wewnętrznych mechanizmach Service Connector.

System-assigned managed identity

Domyślna nazwa zmiennej środowiskowej Description Przykładowa wartość
AZURE_COSMOS_LISTKEYURL The URL to get the connection string https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE Your managed identity scope https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT Your resource endpoint https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME Your Gremlin Unique Resource Identifier (UFI) <Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT Port połączenia 443
AZURE_COSMOS_USERNAME Twoja nazwa użytkownika /dbs/<database>/colls/<collection or graphs>

Przykładowy kod

Zapoznaj się z poniższymi krokami i kodem, aby nawiązać połączenie z usługą Azure Cosmos DB dla języka Gremlin przy użyciu tożsamości zarządzanej przypisanej przez system.

Ponieważ usługa Cosmos DB nie obsługuje natywnie uwierzytelniania za pośrednictwem tożsamości zarządzanej, w poniższym przykładzie kodu używamy tożsamości zarządzanej do pobierania parametrów połączenia, a połączenie jest następnie ustanawiane przy użyciu tych parametrów połączenia.

  1. Instalowanie zależności.

    dotnet add package Gremlin.Net
    dotnet add package Azure.Identity
    
  2. Get an access token for the managed identity or service principal using client library Azure.Identity. Użyj tokenu dostępu i AZURE_COSMOS_LISTKEYURL aby pobrać hasło. Uzyskaj informacje o połączeniu ze zmiennych środowiskowych dodanych przez Service Connector i połącz się z Azure Cosmos DB dla Apache Gremlin. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.

    using System;
    using System.Security.Authentication;
    using System.Net.Security;
    using System.Net.Http;
    using System.Security.Authentication;
    using System.Threading.Tasks;
    using System;
    using Gremlin.Net.Driver;
    using Azure.Identity;
    
    var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
    var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
    var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
    var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
    var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // var tokenProvider = new DefaultAzureCredential();
    
    // For user-assigned identity.
    // var tokenProvider = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    //     }
    // );
    
    // For service principal.
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
    // var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    // Acquire the access token. 
    AccessToken accessToken = await tokenProvider.GetTokenAsync(
        new TokenRequestContext(scopes: new string[]{ scope }));
    
    // Get the password.
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
    var response = await httpClient.POSTAsync(listKeyUrl);
    var responseBody = await response.Content.ReadAsStringAsync();
    var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
    var password = keys["primaryMasterKey"];
    
    // Connect to Azure Cosmos DB for Apache Gremlin
    var server = new GremlinServer(
        hostname: gremlinEndpoint,
        port: gremlinPort,
        username: userName,
        password: password,
        enableSsl: true
    );
    
    using var client = new GremlinClient(
        gremlinServer: server,
        messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
    );        
    
    

User-assigned managed identity

Domyślna nazwa zmiennej środowiskowej Description Przykładowa wartość
AZURE_COSMOS_LISTKEYURL The URL to get the connection string https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE Your managed identity scope https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT Your resource endpoint https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME Your Gremlin Unique Resource Identifier (UFI) <Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT Port połączenia 443
AZURE_COSMOS_USERNAME Twoja nazwa użytkownika /dbs/<database>/colls/<collection or graphs>
AZURE_CLIENTID Identyfikator klienta <client_ID>

Przykładowy kod

Zapoznaj się z poniższymi krokami i kodem, aby nawiązać połączenie z usługą Azure Cosmos DB dla języka Gremlin przy użyciu tożsamości zarządzanej przypisanej przez użytkownika.

Ponieważ usługa Cosmos DB nie obsługuje natywnie uwierzytelniania za pośrednictwem tożsamości zarządzanej, w poniższym przykładzie kodu używamy tożsamości zarządzanej do pobierania parametrów połączenia, a połączenie jest następnie ustanawiane przy użyciu tych parametrów połączenia.

  1. Instalowanie zależności.

    dotnet add package Gremlin.Net
    dotnet add package Azure.Identity
    
  2. Get an access token for the managed identity or service principal using client library Azure.Identity. Użyj tokenu dostępu i AZURE_COSMOS_LISTKEYURL aby pobrać hasło. Uzyskaj informacje o połączeniu ze zmiennych środowiskowych dodanych przez Service Connector i połącz się z Azure Cosmos DB dla Apache Gremlin. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.

    using System;
    using System.Security.Authentication;
    using System.Net.Security;
    using System.Net.Http;
    using System.Security.Authentication;
    using System.Threading.Tasks;
    using System;
    using Gremlin.Net.Driver;
    using Azure.Identity;
    
    var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
    var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
    var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
    var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
    var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // var tokenProvider = new DefaultAzureCredential();
    
    // For user-assigned identity.
    // var tokenProvider = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    //     }
    // );
    
    // For service principal.
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
    // var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    // Acquire the access token. 
    AccessToken accessToken = await tokenProvider.GetTokenAsync(
        new TokenRequestContext(scopes: new string[]{ scope }));
    
    // Get the password.
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
    var response = await httpClient.POSTAsync(listKeyUrl);
    var responseBody = await response.Content.ReadAsStringAsync();
    var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
    var password = keys["primaryMasterKey"];
    
    // Connect to Azure Cosmos DB for Apache Gremlin
    var server = new GremlinServer(
        hostname: gremlinEndpoint,
        port: gremlinPort,
        username: userName,
        password: password,
        enableSsl: true
    );
    
    using var client = new GremlinClient(
        gremlinServer: server,
        messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
    );        
    
    

Connection string

Ostrzeżenie

Firma Microsoft zaleca korzystanie z najbezpieczniejszego dostępnego przepływu uwierzytelniania. Przepływ uwierzytelniania opisany w tej procedurze wymaga bardzo wysokiego poziomu zaufania w aplikacji i niesie ze sobą ryzyko, które nie występują w innych przepływach. Tego przepływu należy używać tylko wtedy, gdy inne bezpieczniejsze przepływy, takie jak tożsamości zarządzane, nie są opłacalne.

Domyślna nazwa zmiennej środowiskowej Description Przykładowa wartość
AZURE_COSMOS_HOSTNAME Your Gremlin Unique Resource Identifier (UFI) <Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT Port połączenia 443
AZURE_COSMOS_USERNAME Twoja nazwa użytkownika /dbs/<database>/colls/<collection or graphs>
AZURE_COSMOS_PASSWORD Twoje hasło <password>

Przykładowy kod

Zapoznaj się z poniższymi krokami i kodem, aby nawiązać połączenie z usługą Azure Cosmos DB dla Gremlin przy użyciu ciągu połączenia.

  1. Install dependency.

    dotnet add package Gremlin.Net
    
  2. Uzyskaj informacje o połączeniu ze zmiennych środowiskowych dodanych przez Service Connector i połącz się z Azure Cosmos DB dla Apache Gremlin.

    using System;
    using Gremlin.Net.Driver;
    
    var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
    var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
    var password = Environment.GetEnvironmentVariable("AZURE_COSMOS_PASSWORD");
    var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
    
    var server = new GremlinServer(
        hostname: gremlinEndpoint,
        port: gremlinPort,
        username: userName,
        password: password,
        enableSsl: true
    );
    
    using var client = new GremlinClient(
        gremlinServer: server,
        messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
    );
    

Service principal

Domyślna nazwa zmiennej środowiskowej Description Przykładowa wartość
AZURE_COSMOS_LISTKEYURL The URL to get the connection string https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE Your managed identity scope https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT Your resource endpoint https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME Your Gremlin Unique Resource Identifier (UFI) <Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT Port połączenia Gremlin 10350
AZURE_COSMOS_USERNAME Twoja nazwa użytkownika </dbs/<database>/colls/<collection or graphs>
AZURE_COSMOS_CLIENTID Identyfikator klienta <client-ID>
AZURE_COSMOS_CLIENTSECRET Your client secret <client-secret>
AZURE_COSMOS_TENANTID Your tenant ID <tenant-ID>

Przykładowy kod

Refer to the steps and code below to connect to Azure Cosmos DB for Gremlin using a service principal.

  1. Instalowanie zależności.

    dotnet add package Gremlin.Net
    dotnet add package Azure.Identity
    
  2. Get an access token for the managed identity or service principal using client library Azure.Identity. Użyj tokenu dostępu i AZURE_COSMOS_LISTKEYURL aby pobrać hasło. Uzyskaj informacje o połączeniu ze zmiennych środowiskowych dodanych przez Service Connector i połącz się z Azure Cosmos DB dla Apache Gremlin. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.

    using System;
    using System.Security.Authentication;
    using System.Net.Security;
    using System.Net.Http;
    using System.Security.Authentication;
    using System.Threading.Tasks;
    using System;
    using Gremlin.Net.Driver;
    using Azure.Identity;
    
    var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
    var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
    var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
    var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
    var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // var tokenProvider = new DefaultAzureCredential();
    
    // For user-assigned identity.
    // var tokenProvider = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    //     }
    // );
    
    // For service principal.
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
    // var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    // Acquire the access token. 
    AccessToken accessToken = await tokenProvider.GetTokenAsync(
        new TokenRequestContext(scopes: new string[]{ scope }));
    
    // Get the password.
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
    var response = await httpClient.POSTAsync(listKeyUrl);
    var responseBody = await response.Content.ReadAsStringAsync();
    var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
    var password = keys["primaryMasterKey"];
    
    // Connect to Azure Cosmos DB for Apache Gremlin
    var server = new GremlinServer(
        hostname: gremlinEndpoint,
        port: gremlinPort,
        username: userName,
        password: password,
        enableSsl: true
    );
    
    using var client = new GremlinClient(
        gremlinServer: server,
        messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
    );        
    
    

Następne kroki

Postępuj zgodnie z samouczkami wymienionymi poniżej, aby dowiedzieć się więcej o Łączniku usług.