Skip to content

Commit c82e342

Browse files
authored
Merge pull request #26 from RedisGraph/examples
Extended client with examples folder and godoc with 3 examples …
2 parents 36b9799 + 70f37fb commit c82e342

File tree

5 files changed

+310
-2
lines changed

5 files changed

+310
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
examples: get
17+
@echo " "
18+
@echo "Building the examples..."
19+
$(GOBUILD) ./examples/redisgraph_tls_client/.
20+
21+
test: get
22+
$(GOTEST) -race -covermode=atomic ./...
23+
24+
coverage: get test
25+
$(GOTEST) -race -coverprofile=coverage.txt -covermode=atomic ./...
26+

example_graph_test.go

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

0 commit comments

Comments
 (0)