翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
3.4.11 以降のバージョンの TinkerPop を使用して Sig4 署名で Neptune に接続する
TinkerPop 3.4.11 以上を使用しているときに、Sig4 署名付きの Gremlin Java APIを使用して Neptune に接続する方法の例を示します (Maven の使用に関する一般的知識を前提としています)。この例では、Amazon Neptune SigV4 Signerpom.xml
ファイルの一部として依存関係を定義します。
注記
次の例は、requestInterceptor() の使用を含むように更新されました。これは TinkerPop 3.6.6 で追加されました。TinkerPop バージョン 3.6.6 以前は、コード例では handshakeInterceptor() が使用されていましたが、このリリースでは廃止されました。
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-neptune-sigv4-signer</artifactId>
<version>3.1.0</version>
</dependency>
Amazon Neptune SigV4 Signer は、Java SDK のバージョン 1.x と 2.x AWS の両方の使用をサポートしています。次の例では、 DefaultCredentialsProvider
がsoftware.amazon.awssdk.auth.credentials.AwsCredentialsProvider
インスタンスである 2.x を使用していますが、任意の で 1.x フォームを均等に使用できますcom.amazonaws.auth.AWSCredentialsProvider
。1.x から 2.x にアップグレードする場合は、 AWS SDK for Java 2.x ドキュメントの認証情報プロバイダーの変更で 1.x と 2.x の間の変更の詳細を確認できます。
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)
", DefaultCredentialsProvider.create());
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()
を使用するときには不要になります。requestInterceptor()
をちゃねライザー (SigV4WebSocketChannelizer.class
) と組み合わせて使用しようとしないでください。エラーが発生します。
クロスアカウント IAM 認証
Amazon Neptune は、ロールチェーンとも呼ばれるロールの仮定を使用してクロスアカウント IAM 認証をサポートします。別の AWS アカウントでホストされているアプリケーションから Neptune クラスターへのアクセスを提供するには:
-
ユーザーまたはロールが別の 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 データベースへのアクセスを許可し、アプリケーション AWS アカウントの IAM ユーザー/ロールからのロールの引き受けを許可する新しい IAM ロールを Neptune データベースアカウントに作成します。次の信頼ポリシーを使用します。
{ "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) .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
パッケージに依存する必要があります。これは、標準の TinkerPop チャネライザーを SigV4 署名を自動的に注入できるチャネライザーに置き換える SigV4WebSocketChannelizer
クラスを含む Neptune ライブラリです。amazon-neptune-gremlin-java-sigv4
ライブラリは廃止されているので、可能であれば、TinkerPop 3.4.11 以降にアップグレードしてください。
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
が含まれます。より新しい Gremlin ドライバーバージョン (3.4.13
まで) を使用することも可能ですが、3.4.10 以降にドライバーをアップグレードする場合は、上記の requestInterceptor()
モデルを使用するように変更する必要があります。
その後、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();