翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Gremlin Java による IAM を使用した Amazon Neptune データベースへの接続
Sig4 署名で Gremlin Java API を使用して Neptune に接続する方法の例を示します (Maven の使用に関する一般的な知識を前提としています)。この例では、Amazon Neptune SigV4 Signerpom.xml ファイルの一部として依存関係を定義します。
注記
次の例ではrequestInterceptor()、TinkerPop 3.6.6 で導入された を使用します。3.6.6 より前の TinkerPop バージョン (ただし 3.5.5 以降) を使用している場合は、以下のコード例requestInterceptor()で 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 の両方をサポートしています。次の例では 2.x を使用していますが、 DefaultCredentialsProviderはsoftware.amazon.awssdk.auth.credentials.AwsCredentialsProviderインスタンスです。1.x から 2.x にアップグレードする場合は、 AWS SDK for Java 2.x ドキュメントの「認証情報プロバイダーの変更」を参照してください。
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; 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); }
クロスアカウント IAM 認証
Amazon Neptune は、ロールの仮定を使用することによるクロスアカウント IAM 認証をサポートしています。これは、ロールの連鎖とも呼ばれます。別の AWS アカウントでホストされているアプリケーションから Neptune クラスターへのアクセスを提供するには:
-
ユーザーまたはロールが別の IAM ロールを引き受けることを許可する信頼ポリシーを使用して、アプリケーション AWS アカウントに新しい IAM ユーザーまたはロールを作成します。このロールをアプリケーションをホストするコンピューティング (EC2 インスタンス、Lambda 関数、ECS タスクなど) に割り当てます。
-
Neptune データベースへのアクセスを許可し、アプリケーション AWS アカウントの IAM ユーザー/ロールからのロールの引き受けを許可する新しい IAM ロールを Neptune データベースアカウントに作成します。次の信頼ポリシーを使用します。
-
これらの 2 つのロールを使用してアプリケーションが Neptune にアクセスできるようにする方法のガイダンスとして次のコード例を使用します。この例では、アプリケーションアカウントロールは、
STSclientの作成時に DefaultCredentialProviderChain を介して引き受けられます。その後、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", DefaultCredentialsProvider.create()); 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(); }