Java と Gremlin でバージョン 4 署名を使用して Neptune に接続する - Amazon Neptune

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Java と Gremlin でバージョン 4 署名を使用して Neptune に接続する

TinkerPop 3.4.11 以降を使用して Sig4 署名で Neptune に接続する

以下は、 TinkerPop 3.4.11 以降を使用する場合に Gremlin Java APIと Sig4 署名を使用して Neptune に接続する方法の例です (Maven の使用に関する一般的な知識を前提としています)。まず、pom.xml ファイルの一部として依存関係を定義します。

<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) .handshakeInterceptor( 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 ライブラリへの参照を削除してください。上の例に示されているように、handshakeInterceptor() を使用するときには不要になります。handshakeInterceptor() をちゃねライザー (SigV4WebSocketChannelizer.class) と組み合わせて使用しようとしないでください。エラーが発生します。

クロスアカウントIAM認証

Amazon Neptune は、ロールの仮定を使用してクロスアカウントIAM認証をサポートしています。これは、ロール連鎖 とも呼ばれます。別の でホストされているアプリケーションから Neptune クラスターへのアクセスを提供するには AWS アカウント:

  • アプリケーションで新しいIAMユーザーまたはロールを作成する AWS ユーザーまたはロールが別の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 データベースに新しいIAMロールを作成する AWS Neptune データベースへのアクセスを許可し、アプリケーションアカウントのIAMユーザー/ロールからのロールの引き受けを許可する アカウント。次の信頼ポリシーを使用します。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": [ "(ARN of application account IAM user or role)" ] }, "Action": "sts:AssumeRole", "Condition": {} } ] }
  • 次のコード例を、これら 2 つのロールを使用してアプリケーションが Neptune にアクセスできるようにする方法のガイダンスとして使用します。この例では、アプリケーションアカウントロールは、 の作成DefaultCredentialProviderChain時に を介して引き受けられますSTSclient。その後STSclient、 は を介して使用され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) .handshakeInterceptor( 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 に接続する

TinkerPop より前のバージョンでは、前のセクションで示したhandshakeInterceptor()設定がサポートされ3.4.11ていないため、 amazon-neptune-gremlin-java-sigv4パッケージに依存する必要があります。これは SigV4WebSocketChannelizer クラスを含む Neptune ライブラリで、標準の TinkerPop Channelizer を SigV4 署名を自動的に挿入できるものに置き換えます。amazon-neptune-gremlin-java-sigv4 ライブラリは非推奨であるため、可能な場合は TinkerPop 3.4.11 以上にアップグレードしてください。

以下は、3.4.11 より前の TinkerPop バージョンを使用する場合に、Sig4 署名APIで Gremlin Java を使用して 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 が含まれます。より新しい Gremlin ドライバーバージョン (3.4.13 まで) を使用することも可能ですが、3.4.10 以降にドライバーをアップグレードする場合は、上記handshakeInterceptor() モデルを使用するように変更する必要があります。

その後、gremlin-driver Cluster オブジェクトを 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();