Skip to content

Commit 1d157e7

Browse files
committed
Add mutual TLS example code.
* Add examples sections in README linking to simple and tls examples. * Update example links in README to be relative paths instead of GitHub URLs direct to the master branch. * Update simple.py example README to work with Dgraph v1.0.10+ (Server -> Alpha rename).
1 parent 0731973 commit 1d157e7

File tree

5 files changed

+204
-9
lines changed

5 files changed

+204
-9
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and understand how to run and work with Dgraph.
2727
- [Commit a transaction](#commit-a-transaction)
2828
- [Cleanup Resources](#cleanup-resources)
2929
- [Setting Metadata Headers](#setting-metadata-headers)
30+
- [Examples](#examples)
3031
- [Development](#development)
3132
- [Building the source](#building-the-source)
3233
- [Running tests](#running-tests)
@@ -41,11 +42,11 @@ pip install pydgraph
4142

4243
## Quickstart
4344

44-
Build and run the [simple] project in the `examples` folder, which
45+
Build and run the [simple][] project in the `examples` folder, which
4546
contains an end-to-end example of using the Dgraph python client. Follow the
4647
instructions in the README of that project.
4748

48-
[simple]: https://github.com/dgraph-io/pydgraph/tree/master/examples/simple
49+
[simple]: ./examples/simple
4950

5051
## Using a client
5152

@@ -300,6 +301,13 @@ metadata = [("auth-token", "the-auth-token-value")]
300301
dg.alter(op, metadata=metadata)
301302
```
302303

304+
## Examples
305+
306+
- [simple][]: Quickstart example of using pydgraph.
307+
- [tls][]: Example of using pydgraph with a Dgraph cluster secured with TLS.
308+
309+
[tls]: ./examples/tls
310+
303311
## Development
304312

305313
### Building the source

examples/simple/README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ for Dgraph.
99

1010
### Start Dgraph server
1111

12-
You will need to install [Dgraph v1.0.0 or above][releases] and run it.
12+
You will need to install [Dgraph v1.0.10 or above][releases] and run it.
1313

1414
[releases]: https://github.com/dgraph-io/dgraph/releases
1515

1616
You can run the commands below to start a clean Dgraph server every time, for testing
1717
and exploration.
1818

19-
First, create two separate directories for `dgraph zero` and `dgraph server`.
19+
First, create two separate directories for `dgraph zero` and `dgraph alpha`.
2020

2121
```sh
2222
mkdir -p dgraphdata/zero dgraphdata/data
@@ -27,18 +27,16 @@ Then start `dgraph zero`:
2727
```sh
2828
cd dgraphdata/zero
2929
rm -r zw; dgraph zero
30-
# If running Dgraph version <= 1.0.2, use the following command instead:
31-
# rm -r zw; dgraph zero --port_offset -2000
3230
```
3331

34-
Finally, start the `dgraph server`:
32+
Finally, start the `dgraph alpha`:
3533

3634
```sh
3735
cd dgraphdata/data
38-
rm -r p w; dgraph server --memory_mb=1024 --zero localhost:5080
36+
rm -r p w; dgraph server --lru_mb=1024 --zero localhost:5080
3937
```
4038

41-
For more configuration options, and other details, refer to
39+
For more configuration options and other details, refer to
4240
[docs.dgraph.io](https://docs.dgraph.io)
4341

4442
## Install dependencies

examples/tls/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tls/
2+
3+
p/
4+
w/
5+
zw/
6+

examples/tls/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Mutual TLS example project
2+
3+
Project demonstrating the use of pydgraph and Dgraph set up with client-server
4+
mutual TLS. The following guide shows how to set up a single-group two-node
5+
cluster (1 Dgraph Zero and 1 Dgraph Alpha) configured with mutual TLS.
6+
7+
## Running
8+
9+
### Install Dgraph
10+
11+
You will need to [install Dgraph v1.0.0 or
12+
above](https://docs.dgraph.io/get-started/#step-1-install-dgraph).
13+
14+
A quick-start installation script is available for Linux and Mac:
15+
16+
```sh
17+
curl -sSf https://get.dgraph.io | bash
18+
```
19+
20+
### Create TLS certificates
21+
22+
Dgraph provides a `dgraph cert` tool to create and manage self-signed
23+
server and client certificates using a generated Dgraph Root CA. See the [TLS
24+
documentation](https://docs.dgraph.io/deploy/#tls-configuration) for more
25+
information.
26+
27+
Create the root CA. All certificates and keys are created in the `tls` directory.
28+
29+
```sh
30+
dgraph cert
31+
```
32+
33+
Now create the Alpha server certificate (node.crt) and key (node.key) and client
34+
certificate (client.user.crt) key (client.user.key).
35+
36+
```sh
37+
dgraph cert -n localhost
38+
```
39+
40+
```sh
41+
dgraph cert -c user
42+
```
43+
44+
The following files should now be in the `tls` directory:
45+
46+
```sh
47+
$ ls tls
48+
ca.crt ca.key client.user.crt client.user.key node.crt node.key
49+
```
50+
51+
Using `dgraph cert ls` provides more details about each file. For instance, it
52+
shows that the `node.crt` is valid only for the host named `localhost` and the
53+
corresponding file permissions.
54+
55+
```sh
56+
$ dgraph cert ls
57+
-rw-r--r-- ca.crt - Dgraph Root CA certificate
58+
Issuer: Dgraph Labs, Inc.
59+
S/N: 3dfb9c54929d703b
60+
Expiration: 19 Feb 29 00:57 UTC
61+
MD5 hash: C82CF5D4C344668E34A61D590D6A4B77
62+
63+
-r-------- ca.key - Dgraph Root CA key
64+
MD5 hash: C82CF5D4C344668E34A61D590D6A4B77
65+
66+
-rw-r--r-- client.user.crt - Dgraph client certificate: user
67+
Issuer: Dgraph Labs, Inc.
68+
CA Verify: PASSED
69+
S/N: 5991417e75ba14c7
70+
Expiration: 21 Feb 24 01:04 UTC
71+
MD5 hash: BA35D4ABD8DFF1ED137E8D8E5D921D06
72+
73+
-rw------- client.user.key - Dgraph Client key
74+
MD5 hash: BA35D4ABD8DFF1ED137E8D8E5D921D06
75+
76+
-rw-r--r-- node.crt - Dgraph Node certificate
77+
Issuer: Dgraph Labs, Inc.
78+
CA Verify: PASSED
79+
S/N: 51d53048b6845d8c
80+
Expiration: 21 Feb 24 01:00 UTC
81+
Hosts: localhost
82+
MD5 hash: 5D71F59AAEE294F1CFDA9E3232761018
83+
84+
-rw------- node.key - Dgraph Node key
85+
MD5 hash: 5D71F59AAEE294F1CFDA9E3232761018
86+
```
87+
88+
### Start Dgraph cluster
89+
90+
Start Dgraph Zero:
91+
92+
```sh
93+
dgraph zero
94+
```
95+
96+
Start Dgraph Alpha with TLS options. `REQUIREANDVERIFY` sets mutual TLS (server authentication and client authentication):
97+
98+
```sh
99+
dgraph alpha --lru_mb=1024 --zero=localhost:5080 --tls_dir=./tls --tls_client_auth=REQUIREANDVERIFY
100+
```
101+
102+
### Run example
103+
104+
Ensure the pydgraph client is installed:
105+
106+
```sh
107+
pip install pydgraph
108+
```
109+
110+
Then run the example, which connects to the Dgraph Alpha via TLS using the
111+
generated root CA cert and client cert and key in the `tls` directory. The
112+
example first deletes all the data in Dgraph via drop all, updates the schema,
113+
runs a mutation, and finally runs a query. The result of the query is printed
114+
out.
115+
116+
```sh
117+
python mutualtls_example.py
118+
```
119+

examples/tls/mutualtls_example.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/python
2+
3+
import pydgraph
4+
import grpc
5+
6+
def create_client(addr='localhost:9080'):
7+
# Read certs
8+
with open('./tls/ca.crt', 'rb') as f:
9+
root_ca_cert = f.read()
10+
with open('./tls/client.user.key', 'rb') as f:
11+
client_cert_key = f.read()
12+
with open('./tls/client.user.crt', 'rb') as f:
13+
client_cert = f.read()
14+
15+
# Connect to Dgraph via gRPC with mutual TLS.
16+
creds = grpc.ssl_channel_credentials(root_certificates=root_ca_cert,
17+
private_key=client_cert_key,
18+
certificate_chain=client_cert)
19+
client_stub = pydgraph.DgraphClientStub(addr, credentials=creds)
20+
return pydgraph.DgraphClient(client_stub)
21+
22+
def main():
23+
client = create_client('localhost:9080')
24+
25+
# Drop all
26+
client.alter(pydgraph.Operation(drop_all=True))
27+
28+
# Update schema
29+
schema = '''
30+
name: string @index(exact) .
31+
description: string .
32+
url: string .
33+
'''
34+
op = pydgraph.Operation(schema=schema)
35+
client.alter(op)
36+
37+
# Mutate
38+
dgraph = {
39+
"name": "Dgraph",
40+
"description": "Scalable, Distributed, Low Latency Graph Database",
41+
"url": "https://dgraph.io"
42+
}
43+
txn = client.txn()
44+
try:
45+
txn.mutate(set_obj=dgraph)
46+
txn.commit()
47+
finally:
48+
txn.discard()
49+
50+
# Query
51+
res = client.query('''
52+
query dgraph($name: string) {
53+
data(func: eq(name, $name)) {
54+
uid
55+
name
56+
description
57+
url
58+
}
59+
}
60+
''', variables={"$name": "Dgraph"})
61+
print(res.json);
62+
63+
if __name__ == '__main__':
64+
main()

0 commit comments

Comments
 (0)