Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Menghubungkan ke database Amazon Neptune menggunakan IAM dengan Gremlin Java
Berikut adalah contoh bagaimana menghubungkan ke Neptunus menggunakan Gremlin Java API dengan penandatanganan Sig4 (ini mengasumsikan pengetahuan umum tentang menggunakan Maven). Contoh ini menggunakan pustaka SigV4 Signer Amazon Neptune untuk membantu pom.xml file:
catatan
Contoh penggunaan berikutrequestInterceptor(), yang diperkenalkan di TinkerPop 3.6.6. Jika Anda menggunakan TinkerPop versi yang lebih awal dari 3.6.6 (tetapi 3.5.5 atau lebih tinggi), gunakan al handshakeInterceptor() ih-alih requestInterceptor() dalam contoh kode di bawah ini.
<dependency> <groupId>com.amazonaws</groupId> <artifactId>amazon-neptune-sigv4-signer</artifactId> <version>3.1.0</version> </dependency>
Amazon Neptune SigV4 Signer mendukung versi 1.x dan 2.x dari Java SDK. AWS Contoh berikut menggunakan 2.x, di mana DefaultCredentialsProvider adalah software.amazon.awssdk.auth.credentials.AwsCredentialsProvider contoh. Jika Anda memutakhirkan dari 1.x ke 2.x, lihat perubahan penyedia Cre dentials di AWS SDK for Java 2.x dokumentasi.
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); }
Otentikasi IAM lintas akun
Amazon Neptune mendukung otentikasi IAM lintas akun melalui penggunaan asumsi peran, juga kadang-kadang disebut sebagai rantai https://docs.aws.amazon.com//neptune/latest/userguide/bulk-load-tutorial-chain-roles.html#bulk-load-tutorial-chain-cross-account peran. Untuk menyediakan akses ke cluster Neptunus dari aplikasi yang dihosting di AWS akun lain:
-
Buat pengguna atau peran IAM baru di AWS akun aplikasi, dengan kebijakan kepercayaan yang memungkinkan pengguna atau peran untuk mengambil peran IAM lain. Tetapkan peran ini ke komputasi yang menghosting aplikasi (instance EC2, fungsi Lambda, Tugas ECS, dll.).
-
Buat peran IAM baru di AWS akun database Neptune yang memungkinkan akses ke database Neptune dan memungkinkan asumsi peran dari akun aplikasi I user/role AM. Gunakan kebijakan kepercayaan:
-
Gunakan contoh kode berikut sebagai panduan tentang cara menggunakan dua peran ini untuk memungkinkan aplikasi mengakses Neptunus. Dalam contoh ini, peran akun aplikasi akan diasumsikan melalui DefaultCredentialProviderChain saat membuat
STSclient. KemudianSTSclientdigunakan melaluiSTSAssumeRoleSessionCredentialsProvideruntuk mengambil peran yang dihosting di AWS akun database Neptunus.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(); }