Gremlin Go에서 IAM 인증을 사용하여 Amazon Neptune 데이터베이스에 연결 - Amazon Neptune

Gremlin Go에서 IAM 인증을 사용하여 Amazon Neptune 데이터베이스에 연결

개요

이 설명서에서는 Gremlin Go 드라이버를 사용하여 IAM 인증을 활성화하고 서명 버전 4 인증 및 AWS SDK for GO v2를 사용하여 Amazon Neptune 데이터베이스에 연결하는 방법을 보여줍니다.

사전 조건

  • IAM 인증이 활성화된 Amazon Neptune 클러스터입니다.

  • Go 1.22 이상(Gremlin GoAWS SDK for Go v2의 지원되는 최소 버전 참조).

  • 환경 변수, 공유 자격 증명 또는 IAM 역할을 통해 AWS 자격 증명 구성

기본 연결 생성

다음 코드 예제를 Gremlin Go 드라이버를 사용하여 IAM 인증과 기본 연결을 설정하는 방법에 대한 지침으로 사용합니다.

package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/config" "net/http" "strings" "time" gremlingo "github.com/apache/tinkerpop/gremlin-go/v3/driver" v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" ) const emptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855` func main() { neptuneEndpoint := "you.cluster.endpoint.neptune.amazonaws.com" connString := "wss://" + neptuneEndpoint + ":8182/gremlin" service := "neptune-db" defaultRegion := "us-east-1" // Create request to sign req, err := http.NewRequest(http.MethodGet, connString, strings.NewReader("")) if err != nil { fmt.Println(err) return } // Loads the default config with default credentials provider // See https://github.com/aws/aws-sdk-go-v2 for additional docs on API usage cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { fmt.Println(err) return } // Retrieve loaded credentials cr, err := cfg.Credentials.Retrieve(context.TODO()) if err != nil { fmt.Println(err) return } region := defaultRegion if cfg.Region != "" { // region set inside config profile, or via AWS_REGION or AWS_DEFAULT_REGION environment variable will be loaded region = cfg.Region } signer := v4.NewSigner() // Sign request err = signer.SignHTTP(context.TODO(), cr, req, emptyStringSHA256, service, "us-east-2", time.Now()) if err != nil { fmt.Println(err) return } // Pass the signed request header into gremlingo.HeaderAuthInfo() driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(connString, func(settings *gremlingo.DriverRemoteConnectionSettings) { settings.TraversalSource = "g" settings.AuthInfo = gremlingo.HeaderAuthInfo(req.Header) // settings.TlsConfig = &tls.Config{InsecureSkipVerify: true} // Use this only if you're on a Mac running Go 1.18+ doing local dev. See https://github.com/golang/go/issues/51991 }) if err != nil { fmt.Println(err) return } // Cleanup defer driverRemoteConnection.Close() // Creating graph traversal g := gremlingo.Traversal_().WithRemote(driverRemoteConnection) // Query execution count, err := g.V().Limit(5).Count().Next() if err != nil { fmt.Println(err) return } fmt.Println("Vertex count:", *count) }

Gremlin Go 동적 자격 증명 새로 고침

Gremlin Go에는 함수 포인터를 삽입하여 자격 증명을 검색하고 헤더를 생성할 수 있는 DynamicAuth가 있어 장기 실행 연결 시 헤더 만료를 방지합니다.

package main import ( "context" "crypto/tls" "fmt" "github.com/aws/aws-sdk-go-v2/config" "net/http" "strings" "time" gremlingo "github.com/apache/tinkerpop/gremlin-go/v3/driver" v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" ) const emptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855` func main() { neptuneEndpoint := "you.cluster.endpoint.neptune.amazonaws.com" connString := "wss://" + neptuneEndpoint + ":8182/gremlin" service := "neptune-db" defaultRegion := "us-east-1" //Create the request to sign req, err := http.NewRequest(http.MethodGet, connString, strings.NewReader("")) if err != nil { fmt.Println(err) return } // Loads the default config with default credentials provider // See https://github.com/aws/aws-sdk-go-v2 for additional docs on API usage cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { fmt.Println(err) return } region := defaultRegion if cfg.Region != "" { // region set inside config profile, or via AWS_REGION or AWS_DEFAULT_REGION environment variable will be loaded region = cfg.Region } signer := v4.NewSigner() // This is the function that will be used for dynamic refreseh of credentials and signed headers gen := func() gremlingo.AuthInfoProvider { // Retrieve loaded credentials cr, err := cfg.Credentials.Retrieve(context.TODO()) fmt.Println("AWS Credentials: ", cr) if err != nil { fmt.Println(err) return } // Sign request err = signer.SignHTTP(context.TODO(), cr, req, emptyStringSHA256, service, region, time.Now()) if err != nil { fmt.Println(err) return } fmt.Println(req.Header) return gremlingo.HeaderAuthInfo(req.Header) } // Pass the function into gremlingo.NewDynamicAuth(), which will generate the AuthInfoProvider to pass into gremlingo.DriverRemoteConnectionSettings below auth := gremlingo.NewDynamicAuth(gen) driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(connString, func(settings *gremlingo.DriverRemoteConnectionSettings) { settings.TraversalSource = "g" settings.AuthInfo = auth // settings.TlsConfig = &tls.Config{InsecureSkipVerify: true} // Use this only if you're on a Mac running Go 1.18+ doing local dev. See https://github.com/golang/go/issues/51991 }) if err != nil { fmt.Println(err) return } // Cleanup defer driverRemoteConnection.Close() // Creating graph traversal g := gremlingo.Traversal_().WithRemote(driverRemoteConnection) // Query execution count, err := g.V().Limit(5).Count().Next() if err != nil { fmt.Println(err) return } fmt.Println("Vertex count:", *count) }