本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
通过 Gremlin Java 使用 IAM 连接到 Amazon Neptune 数据库
以下是如何使用带有 Sig4 签名的 Gremlin Java API 连接到海王星的示例(它假设对使用 Maven 有一定的了解)。此示例使用 Amazon Neptune SigV4 Signerpom.xml 文件的一部分:
以下示例使用 requestInterceptor(),它是在引入的 TinkerPop 3.6.6。如果你使用的是 TinkerPop 低于 3.6.6 的版本(但是 3.5.5 或更高版本),使用 handshakeInterceptor() 而不是 requestInterceptor() 在下面的代码示例中。
<dependency> <groupId>com.amazonaws</groupId> <artifactId>amazon-neptune-sigv4-signer</artifactId> <version>3.1.0</version> </dependency>
亚马逊 Neptune SigV4 Signer 支持 1.x 和 2.x 版本的 Amazon Java 开发工具包。以下示例使用 2.x,其中 DefaultCredentialsProvider 是一个 software.amazon.awssdk.auth.credentials.AwsCredentialsProvider 实例。如果您要从 1.x 升级到 2.x,请参阅 凭证提供商变更 在 Amazon 适用于 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 身份验证。通过托管在其他服务器中的应用程序提供对 Neptune 集群的访问权限 Amazon 账户:
-
在应用程序中创建新的 IAM 用户或角色 Amazon 账户,其信任策略允许用户或角色代入另一个 IAM 角色。将该角色分配给托管应用程序(EC2 实例、Lambda 函数、ECS 任务等)的计算。
-
在 Neptune 数据库中创建新的 IAM 角色 Amazon 该账户允许访问 Neptune 数据库并允许从应用程序账户 IAM 中担任角色 user/role。使用以下信任策略:
-
使用以下代码示例作为指导,了解如何使用这两个角色来允许应用程序访问 Neptune。在此示例中,应用程序帐户角色将通过以下方式代入 DefaultCredentialProviderChain 在创建时
STSclient。这个STSclient然后通过以下方式使用STSAssumeRoleSessionCredentialsProvider代入 Neptune 数据库中托管的角色 Amazon 账户。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(); }