翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Gremlin Java による IAM を使用した Amazon Neptune データベースへの接続
Sigv4 署名で Gremlin Java ドライバーを使用して Neptune に接続する方法の例を示します (Maven の使用に関する一般的な知識を前提としています)。
注記
次の例が更新され、requestInterceptor() の使用が追加されました。これは TinkerPop 3.6.6 で追加されました。3.6.6 より前のバージョンの TinkerPop では、コード例で 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 を使用していますが、任意の com.amazonaws.auth.AWSCredentialsProvider で 1.x フォームを均等に使用できます。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); }
クロスアカウント 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", 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(); }