|
| 1 | +package redisgraph_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "github.com/RedisGraph/redisgraph-go" |
| 8 | + "github.com/gomodule/redigo/redis" |
| 9 | + "io/ioutil" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +func ExampleGraphNew() { |
| 15 | + conn, _ := redis.Dial("tcp", "0.0.0.0:6379") |
| 16 | + |
| 17 | + graph := redisgraph.GraphNew("social", conn) |
| 18 | + |
| 19 | + q := "CREATE (w:WorkPlace {name:'RedisLabs'})" |
| 20 | + res, _ := graph.Query(q) |
| 21 | + |
| 22 | + q = "MATCH (w:WorkPlace) RETURN w" |
| 23 | + res, _ = graph.Query(q) |
| 24 | + |
| 25 | + res.Next() |
| 26 | + r := res.Record() |
| 27 | + w := r.GetByIndex(0).(*redisgraph.Node) |
| 28 | + fmt.Println(w.Label) |
| 29 | + // Output: WorkPlace |
| 30 | +} |
| 31 | + |
| 32 | +func ExampleGraphNew_pool() { |
| 33 | + host := "localhost:6379" |
| 34 | + password := "" |
| 35 | + pool := &redis.Pool{Dial: func() (redis.Conn, error) { |
| 36 | + return redis.Dial("tcp", host, redis.DialPassword(password)) |
| 37 | + }} |
| 38 | + |
| 39 | + graph := redisgraph.GraphNew("social", pool.Get()) |
| 40 | + |
| 41 | + q := "CREATE (w:WorkPlace {name:'RedisLabs'})" |
| 42 | + res, _ := graph.Query(q) |
| 43 | + |
| 44 | + q = "MATCH (w:WorkPlace) RETURN w" |
| 45 | + res, _ = graph.Query(q) |
| 46 | + |
| 47 | + res.Next() |
| 48 | + r := res.Record() |
| 49 | + w := r.GetByIndex(0).(*redisgraph.Node) |
| 50 | + fmt.Println(w.Label) |
| 51 | + // Output: WorkPlace |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +func ExampleGraphNew_tls() { |
| 56 | + // Consider the following helper methods that provide us with the connection details (host and password) |
| 57 | + // and the paths for: |
| 58 | + // tls_cert - A a X.509 certificate to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted |
| 59 | + // tls_key - A a X.509 private key to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted |
| 60 | + // tls_cacert - A PEM encoded CA's certificate file |
| 61 | + host, password := getConnectionDetails() |
| 62 | + tlsready, tls_cert, tls_key, tls_cacert := getTLSdetails() |
| 63 | + |
| 64 | + // Skip if we dont have all files to properly connect |
| 65 | + if tlsready == false { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + // Load client cert |
| 70 | + cert, err := tls.LoadX509KeyPair(tls_cert, tls_key) |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + // Load CA cert |
| 76 | + caCert, err := ioutil.ReadFile(tls_cacert) |
| 77 | + if err != nil { |
| 78 | + log.Fatal(err) |
| 79 | + } |
| 80 | + caCertPool := x509.NewCertPool() |
| 81 | + caCertPool.AppendCertsFromPEM(caCert) |
| 82 | + |
| 83 | + clientTLSConfig := &tls.Config{ |
| 84 | + Certificates: []tls.Certificate{cert}, |
| 85 | + RootCAs: caCertPool, |
| 86 | + } |
| 87 | + |
| 88 | + // InsecureSkipVerify controls whether a client verifies the |
| 89 | + // server's certificate chain and host name. |
| 90 | + // If InsecureSkipVerify is true, TLS accepts any certificate |
| 91 | + // presented by the server and any host name in that certificate. |
| 92 | + // In this mode, TLS is susceptible to man-in-the-middle attacks. |
| 93 | + // This should be used only for testing. |
| 94 | + clientTLSConfig.InsecureSkipVerify = true |
| 95 | + |
| 96 | + pool := &redis.Pool{Dial: func() (redis.Conn, error) { |
| 97 | + return redis.Dial("tcp", host, |
| 98 | + redis.DialPassword(password), |
| 99 | + redis.DialTLSConfig(clientTLSConfig), |
| 100 | + redis.DialUseTLS(true), |
| 101 | + redis.DialTLSSkipVerify(true), |
| 102 | + ) |
| 103 | + }} |
| 104 | + |
| 105 | + graph := redisgraph.GraphNew("social", pool.Get()) |
| 106 | + |
| 107 | + q := "CREATE (w:WorkPlace {name:'RedisLabs'})" |
| 108 | + res, err := graph.Query(q) |
| 109 | + |
| 110 | + q = "MATCH (w:WorkPlace) RETURN w" |
| 111 | + res, err = graph.Query(q) |
| 112 | + |
| 113 | + res.Next() |
| 114 | + r := res.Record() |
| 115 | + w := r.GetByIndex(0).(*redisgraph.Node) |
| 116 | + fmt.Println(w.Label) |
| 117 | +} |
| 118 | + |
| 119 | +func getConnectionDetails() (host string, password string) { |
| 120 | + value, exists := os.LookupEnv("REDISGRAPH_TEST_HOST") |
| 121 | + host = "localhost:6379" |
| 122 | + password = "" |
| 123 | + valuePassword, existsPassword := os.LookupEnv("REDISGRAPH_TEST_PASSWORD") |
| 124 | + if exists && value != "" { |
| 125 | + host = value |
| 126 | + } |
| 127 | + if existsPassword && valuePassword != "" { |
| 128 | + password = valuePassword |
| 129 | + } |
| 130 | + return |
| 131 | +} |
| 132 | + |
| 133 | +func getTLSdetails() (tlsready bool, tls_cert string, tls_key string, tls_cacert string) { |
| 134 | + tlsready = false |
| 135 | + value, exists := os.LookupEnv("TLS_CERT") |
| 136 | + if exists && value != "" { |
| 137 | + info, err := os.Stat(value) |
| 138 | + if os.IsNotExist(err) || info.IsDir() { |
| 139 | + return |
| 140 | + } |
| 141 | + tls_cert = value |
| 142 | + } else { |
| 143 | + return |
| 144 | + } |
| 145 | + value, exists = os.LookupEnv("TLS_KEY") |
| 146 | + if exists && value != "" { |
| 147 | + info, err := os.Stat(value) |
| 148 | + if os.IsNotExist(err) || info.IsDir() { |
| 149 | + return |
| 150 | + } |
| 151 | + tls_key = value |
| 152 | + } else { |
| 153 | + return |
| 154 | + } |
| 155 | + value, exists = os.LookupEnv("TLS_CACERT") |
| 156 | + if exists && value != "" { |
| 157 | + info, err := os.Stat(value) |
| 158 | + if os.IsNotExist(err) || info.IsDir() { |
| 159 | + return |
| 160 | + } |
| 161 | + tls_cacert = value |
| 162 | + } else { |
| 163 | + return |
| 164 | + } |
| 165 | + tlsready = true |
| 166 | + return |
| 167 | +} |
0 commit comments