使用 IAM 搭配 Gremlin Java 連線至 Amazon Neptune 資料庫 - Amazon Neptune

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 IAM 搭配 Gremlin Java 連線至 Amazon Neptune 資料庫

使用 TinkerPop 3.4.11 或更新版本,搭配 Sig4 簽署連線至 Neptune

以下是一個範例,示範如何在使用 TinkerPop 3.4.11 或更新版本時,搭配 Sig4 簽署使用 Gremlin Java API 連線到 Neptune (它假設您具備有關使用 Maven 的一般知識)。首先,將相依性定義為 pom.xml 檔案的一部分:

注意

已更新下列範例,以包含 requestInterceptor() 的使用。這已在 TinkerPop 3.6.6 中新增。在 TinkerPop 3.6.6 版之前,程式碼範例使用 handshakeInterceptor(),該版本已棄用。

<dependency> <groupId>com.amazonaws</groupId> <artifactId>amazon-neptune-sigv4-signer</artifactId> <version>2.4.0</version> </dependency>

然後,使用如下程式碼:

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.neptune.auth.NeptuneNettyHttpSigV4Signer; import com.amazonaws.neptune.auth.NeptuneSigV4SignerException; ... System.setProperty("aws.accessKeyId","your-access-key"); System.setProperty("aws.secretKey","your-secret-key"); ... Cluster cluster = Cluster.build((your cluster)) .enableSsl(true) .requestInterceptor( r -> { try { NeptuneNettyHttpSigV4Signer sigV4Signer = new NeptuneNettyHttpSigV4Signer("(your region)", new DefaultAWSCredentialsProviderChain()); sigV4Signer.signRequest(r); } catch (NeptuneSigV4SignerException e) { throw new RuntimeException("Exception occurred while signing the request", e); } return r; } ).create(); try { Client client = cluster.connect(); client.submit("g.V().has('code','IAD')").all().get(); } catch (Exception e) { throw new RuntimeException("Exception occurred while connecting to cluster", e); }
注意

如果您要從 3.4.11 升級,請移除對 amazon-neptune-gremlin-java-sigv4 程式庫的參考。如上面範例所示,使用 requestInterceptor() 時它不再是必要的。不要嘗試搭配通道分離器 (SigV4WebSocketChannelizer.class) 使用 requestInterceptor(),因為它會產生錯誤。

跨帳戶 IAM 身分驗證

Amazon Neptune 透過使用角色假設支援跨帳戶 IAM 身分驗證,有時也稱為角色鏈結。若要從託管在不同 AWS 帳戶中的應用程式提供 Neptune 叢集的存取權:

  • 在應用程式 AWS 帳戶中建立新的 IAM 使用者或角色,其信任政策允許使用者或角色擔任另一個 IAM 角色。將此角色指派給託管應用程式的運算 (EC2 執行個體、Lambda 函數、ECS 任務等)。

    { "Version": "2012-10-17", "Statement": [ { "Sid": "assume-role-policy", "Effect": "Allow", "Action": "sts:AssumeRole", "Resource": "(ARN of the role in the database account)" } ] }
  • 在 Neptune 資料庫 AWS 帳戶中建立新的 IAM 角色,以允許存取 Neptune 資料庫,並允許從應用程式帳戶 IAM 使用者/角色擔任角色。使用 的信任政策:

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": [ "(ARN of application account IAM user or role)" ] }, "Action": "sts:AssumeRole", "Condition": {} } ] }
  • 使用下列程式碼範例做為如何使用這兩個角色以允許應用程式存取 Neptune 的指導。在此範例中,建立 時,應用程式帳戶角色將透過 DefaultCredentialProviderChain 擔任STSclientSTSclient 然後,透過 使用 STSAssumeRoleSessionCredentialsProvider來擔任 Neptune 資料庫 AWS 帳戶中託管的角色。

    public static void main( String[] args ) { /* * Establish an STS client from the application account. */ AWSSecurityTokenService client = AWSSecurityTokenServiceClientBuilder .standard() .build(); /* * Define the role ARN that you will be assuming in the database account where the Neptune cluster resides. */ String roleArnToAssume = "arn:aws:iam::012345678901:role/CrossAccountNeptuneRole"; String crossAccountSessionName = "cross-account-session-" + UUID.randomUUID(); /* * Change the Credentials Provider in the SigV4 Signer to use the STSAssumeRole Provider and provide it * with both the role to be assumed, the original STS client, and a session name (which can be * arbitrary.) */ Cluster cluster = Cluster.build() .addContactPoint("neptune-cluster.us-west-2.neptune.amazonaws.com") .enableSsl(true) .port(8182) .requestInterceptor( r -> { try { NeptuneNettyHttpSigV4Signer sigV4Signer = // new NeptuneNettyHttpSigV4Signer("us-west-2", new DefaultAWSCredentialsProviderChain()); new NeptuneNettyHttpSigV4Signer( "us-west-2", new STSAssumeRoleSessionCredentialsProvider .Builder(roleArnToAssume, crossAccountSessionName) .withStsClient(client) .build()); sigV4Signer.signRequest(r); } catch (NeptuneSigV4SignerException e) { throw new RuntimeException("Exception occurred while signing the request", e); } return r; } ).create(); GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster)); /* whatever application code is necessary */ cluster.close(); }

使用 3.4.11 以前的 TinkerPop 版本,搭配 Sig4 簽署連線至 Neptune

3.4.11 以前的 TinkerPop 版本不支援上一節所顯示的 requestInterceptor() 組態,因此必須依賴 amazon-neptune-gremlin-java-sigv4 套件。這是一個包含 SigV4WebSocketChannelizer 類別的 Neptune 程式庫,其會將標準 TinkerPop 通道分離器取代為可以自動注入 Sigv4 簽章的通道分離器。可能的話,升級到 TinkerPop 3.4.11 或更新版本,因為 amazon-neptune-gremlin-java-sigv4 程式庫已棄用。

以下是一個範例,示範如何在使用 3.4.11 以前的 TinkerPop 版本時,搭配 Sig4 簽署使用 Gremlin Java API 連線到 Neptune (它假設您具備有關如何使用 Maven 的一般知識)。

首先,將相依性定義為 pom.xml 檔案的一部分:

<dependency> <groupId>com.amazonaws</groupId> <artifactId>amazon-neptune-gremlin-java-sigv4</artifactId> <version>2.4.0</version> </dependency>

上述相依性將包括 Gremlin 驅動程式版本3.4.10。雖然可以使用較新的 Gemlin 驅動程式版本 (直到 3.4.13),但是升級到 3.4.10 以後的驅動程式應該包含使用上述 requestInterceptor() 模型的變更。

然後,gremlin-driver 叢集物件應該在 Java 程式碼中設定,如下所示:

import org.apache.tinkerpop.gremlin.driver.SigV4WebSocketChannelizer; ... Cluster cluster = Cluster.build(your cluster) .enableSsl(true) .channelizer(SigV4WebSocketChannelizer.class) .create(); Client client = cluster.connect(); client.submit("g.V().has('code','IAD')").all().get();