使用 Java 和 Gremlin 及签名版本 4 签名连接到 Neptune - Amazon Neptune

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Java 和 Gremlin 及签名版本 4 签名连接到 Neptune

使用 TinkerPop 3.4.11 或更高版本通过 Sig4 签名连接到 Neptune

以下是使用 TinkerPop 3.4.11 或更高版本时如何使用API带有 Sig4 签名的 Gremlin Java 连接到 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 集群中的应用程序访问 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": {} } ] }
  • 使用以下代码示例作为指导,了解如何使用这两个角色来允许应用程序访问 Neptune。在此示例中,应用程序账户角色将在创建DefaultCredentialProviderChain时通过STSclient。然后通过使用STSAssumeRoleSessionCredentialsProvider来代替 Neptune 数据库中托管的角色 STSclient AWS account。

    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 之前的版本3.4.11不支持上一节中显示的handshakeInterceptor()配置,因此必须依赖该amazon-neptune-gremlin-java-sigv4软件包。这是一个包含该类的 Neptune 库,它用可以自动注入 SigV4 签名的SigV4WebSocketChannelizer类取代了标准的 TinkerPop Channelizer。如果可能,请升级到 TinkerPop 3.4.11 或更高版本,因为该amazon-neptune-gremlin-java-sigv4库已被弃用。

以下是使用 3.4.11 之前的 TinkerPop 版本时如何使用API带有 Sig4 签名的 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() 模型的更改。

然后,应在 Java 代码中按如下方式配置 gremlin-driver 集群对象:

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();