

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

# Gremlin Java による IAM を使用した Amazon Neptune データベースへの接続
<a name="iam-auth-connecting-gremlin-java"></a>

Sig4 署名で Gremlin Java API を使用して Neptune に接続する方法の例を示します (Maven の使用に関する一般的な知識を前提としています）。この例では、[Amazon Neptune SigV4 Signer](https://github.com/aws/amazon-neptune-sigv4-signer) ライブラリを使用して署名をリクエストすることを支援します。まず、`pom.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 ドキュメントの[「認証情報プロバイダーの変更](https://docs.aws.amazon.com//sdk-for-java/latest/developer-guide/migration-client-credentials.html)」を参照してください。

```
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 認証
<a name="iam-auth-connecting-gremlin-java-cross-account"></a>

 Amazon Neptune は、ロールの仮定を使用することによるクロスアカウント IAM 認証をサポートしています。これは、[ロールの連鎖](https://docs.aws.amazon.com//neptune/latest/userguide/bulk-load-tutorial-chain-roles.html#bulk-load-tutorial-chain-cross-account)とも呼ばれます。別の AWS アカウントでホストされているアプリケーションから Neptune クラスターへのアクセスを提供するには: 
+  ユーザーまたはロールが別の IAM ロールを引き受けることを許可する信頼ポリシーを使用して、アプリケーション AWS アカウントに新しい IAM ユーザーまたはロールを作成します。このロールをアプリケーションをホストするコンピューティング (EC2 インスタンス、Lambda 関数、ECS タスクなど) に割り当てます。

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Sid": "assumeRolePolicy",
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::111122223333:role/role-name"
      }
    ]
  }
  ```

------
+  Neptune データベースへのアクセスを許可し、アプリケーション AWS アカウントの IAM ユーザー/ロールからのロールの引き受けを許可する新しい IAM ロールを Neptune データベースアカウントに作成します。次の信頼ポリシーを使用します。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Principal": {
                  "AWS": [
                      "(ARN of application account IAM user or role)"
                  ]
              },
              "Action": "sts:AssumeRole",
              "Condition": {}
          }
      ]
  }
  ```

------
+  これらの 2 つのロールを使用してアプリケーションが Neptune にアクセスできるようにする方法のガイダンスとして次のコード例を使用します。この例では、アプリケーションアカウントロールは、`STSclient` の作成時に [DefaultCredentialProviderChain](https://docs.aws.amazon.com//sdk-for-java/v1/developer-guide/credentials.html) を介して引き受けられます。その後、 `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();
    }
  ```