Skip to content

Commit bd40ed9

Browse files
committed
[add] Extended client with examples folder and godoc with 3 examples on how to connect to the RedisGraph Server. ( simplest way possible, with pool, and with SSL )
1 parent 36b9799 commit bd40ed9

File tree

5 files changed

+332
-2
lines changed

5 files changed

+332
-2
lines changed

.circleci/config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ jobs:
2525
working_directory: /go/src/github.com/RedisGraph/redisgraph-go
2626
steps:
2727
- checkout
28-
- run: go get -v -t -d ./...
29-
- run: go test -v ./... -race -coverprofile=coverage.txt -covermode=atomic
28+
- run: make test
3029
- early_return_for_forked_pull_requests
3130
- run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
3231

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
*
2+
!/**/
3+
!**/*.go
4+
!.gitignore
5+
!.circleci/config.yml
6+
!/tests/*.bz2
7+
!Makefile
8+
9+
# Binaries for programs and plugins
10+
*.exe
11+
*.exe~
12+
*.dll
13+
*.so
14+
*.dylib
15+
16+
# Test binary, build with `go test -c`
17+
*.test
18+
19+
# Output of the go coverage tool, specifically when used with LiteIDE
20+
*.out
21+
coverage.txt
22+
23+
.idea
24+
.project
25+
26+
vendor/
27+
28+
# OS generated files #
29+
######################
30+
.DS_Store
31+
.DS_Store?
32+
._*
33+
.Spotlight-V100
34+
.Trashes
35+
ehthumbs.db
36+
Thumbs.db

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Go parameters
2+
GOCMD=go
3+
GOBUILD=$(GOCMD) build
4+
GOINSTALL=$(GOCMD) install
5+
GOCLEAN=$(GOCMD) clean
6+
GOTEST=$(GOCMD) test
7+
GOGET=$(GOCMD) get
8+
GOMOD=$(GOCMD) mod
9+
10+
.PHONY: all test coverage
11+
all: test coverage examples
12+
13+
get:
14+
$(GOGET) -t -v ./...
15+
16+
TLS_CERT ?= redis.crt
17+
TLS_KEY ?= redis.key
18+
TLS_CACERT ?= ca.crt
19+
REDISGRAPH_TEST_HOST ?= 127.0.0.1:6379
20+
21+
examples: get
22+
@echo " "
23+
@echo "Building the examples..."
24+
$(GOBUILD) ./examples/redisgraph_tls_client/.
25+
./redisgraph_tls_client --tls-cert-file $(TLS_CERT) \
26+
--tls-key-file $(TLS_KEY) \
27+
--tls-ca-cert-file $(TLS_CACERT) \
28+
--host $(REDISGRAPH_TEST_HOST)
29+
30+
test: get
31+
$(GOTEST) -race -covermode=atomic ./...
32+
33+
coverage: get test
34+
$(GOTEST) -race -coverprofile=coverage.txt -covermode=atomic ./...
35+

example_graph_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"flag"
7+
"fmt"
8+
"github.com/RedisGraph/redisgraph-go"
9+
"github.com/gomodule/redigo/redis"
10+
"io/ioutil"
11+
"log"
12+
"os"
13+
)
14+
15+
var (
16+
tlsCertFile = flag.String("tls-cert-file", "redis.crt", "A a X.509 certificate to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted.")
17+
tlsKeyFile = flag.String("tls-key-file", "redis.key", "A a X.509 privat ekey to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted.")
18+
tlsCaCertFile = flag.String("tls-ca-cert-file", "ca.crt", "A PEM encoded CA's certificate file.")
19+
host = flag.String("host", "127.0.0.1:6379", "Redis host.")
20+
password = flag.String("password", "", "Redis password.")
21+
)
22+
23+
func exists(filename string) (exists bool) {
24+
exists = false
25+
info, err := os.Stat(filename)
26+
if os.IsNotExist(err) || info.IsDir() {
27+
return
28+
}
29+
exists = true
30+
return
31+
}
32+
33+
/*
34+
* Example of how to establish an SSL connection from your app to the RedisAI Server
35+
*/
36+
func main() {
37+
flag.Parse()
38+
// Quickly check if the files exist
39+
if !exists(*tlsCertFile) || !exists(*tlsKeyFile) || !exists(*tlsCaCertFile) {
40+
fmt.Println("Some of the required files does not exist. Leaving example...")
41+
return
42+
}
43+
44+
// Load client cert
45+
cert, err := tls.LoadX509KeyPair(*tlsCertFile, *tlsKeyFile)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
// Load CA cert
51+
caCert, err := ioutil.ReadFile(*tlsCaCertFile)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
caCertPool := x509.NewCertPool()
56+
caCertPool.AppendCertsFromPEM(caCert)
57+
58+
clientTLSConfig := &tls.Config{
59+
Certificates: []tls.Certificate{cert},
60+
RootCAs: caCertPool,
61+
}
62+
63+
// InsecureSkipVerify controls whether a client verifies the
64+
// server's certificate chain and host name.
65+
// If InsecureSkipVerify is true, TLS accepts any certificate
66+
// presented by the server and any host name in that certificate.
67+
// In this mode, TLS is susceptible to man-in-the-middle attacks.
68+
// This should be used only for testing.
69+
clientTLSConfig.InsecureSkipVerify = true
70+
71+
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
72+
return redis.Dial("tcp", *host,
73+
redis.DialPassword(*password),
74+
redis.DialTLSConfig(clientTLSConfig),
75+
redis.DialUseTLS(true),
76+
redis.DialTLSSkipVerify(true),
77+
)
78+
}}
79+
80+
graph := redisgraph.GraphNew("social", pool.Get())
81+
82+
q := "CREATE (w:WorkPlace {name:'RedisLabs'})"
83+
res, err := graph.Query(q)
84+
85+
q = "MATCH (w:WorkPlace) RETURN w"
86+
res, err = graph.Query(q)
87+
88+
res.Next()
89+
r := res.Record()
90+
w := r.GetByIndex(0).(*redisgraph.Node)
91+
fmt.Println(w.Label)
92+
// Output: WorkPlace
93+
}

0 commit comments

Comments
 (0)