Skip to content

Commit 9c6245d

Browse files
committed
Fixed neokit and auth problems
1 parent 2088b23 commit 9c6245d

File tree

11 files changed

+146
-120
lines changed

11 files changed

+146
-120
lines changed

example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
from neo4j.v1.session import GraphDatabase
22-
from neo4j.v1.auth import basic_auth
21+
from neo4j.v1.session import GraphDatabase, basic_auth
22+
2323

2424
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
2525
session = driver.session()

examples/test_examples.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class FreshDatabaseTestCase(ServerTestCase):
3030

3131
def setUp(self):
3232
ServerTestCase.setUp(self)
33-
session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session()
33+
session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password")).session()
3434
session.run("MATCH (n) DETACH DELETE n")
3535
session.close()
3636

@@ -39,7 +39,7 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase):
3939

4040
def test_minimal_working_example(self):
4141
# tag::minimal-example[]
42-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
42+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
4343
session = driver.session()
4444

4545
session.run("CREATE (neo:Person {name:'Neo', age:23})")
@@ -56,7 +56,7 @@ class ExamplesTestCase(FreshDatabaseTestCase):
5656

5757
def test_construct_driver(self):
5858
# tag::construct-driver[]
59-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
59+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
6060
# end::construct-driver[]
6161
return driver
6262

@@ -85,23 +85,23 @@ def test_tls_signed(self):
8585
# end::tls-signed[]
8686

8787
def test_statement(self):
88-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
88+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
8989
session = driver.session()
9090
# tag::statement[]
9191
session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close()
9292
# end::statement[]
9393
session.close()
9494

9595
def test_statement_without_parameters(self):
96-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
96+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
9797
session = driver.session()
9898
# tag::statement-without-parameters[]
9999
session.run("CREATE (person:Person {name: 'Neo'})").close()
100100
# end::statement-without-parameters[]
101101
session.close()
102102

103103
def test_result_cursor(self):
104-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
104+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
105105
session = driver.session()
106106
# tag::result-cursor[]
107107
search_term = "hammer"
@@ -114,7 +114,7 @@ def test_result_cursor(self):
114114
session.close()
115115

116116
def test_cursor_nesting(self):
117-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
117+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
118118
session = driver.session()
119119
# tag::retain-result-query[]
120120
result = session.run("MATCH (person:Person) WHERE person.dept = {dept} "
@@ -127,7 +127,7 @@ def test_cursor_nesting(self):
127127
session.close()
128128

129129
def test_result_retention(self):
130-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
130+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
131131
session = driver.session()
132132
# tag::retain-result-process[]
133133
result = session.run("MATCH (person:Person) WHERE person.dept = {dept} "
@@ -142,7 +142,7 @@ def test_result_retention(self):
142142
session.close()
143143

144144
def test_transaction_commit(self):
145-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
145+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
146146
session = driver.session()
147147
# tag::transaction-commit[]
148148
tx = session.begin_transaction()
@@ -156,7 +156,7 @@ def test_transaction_commit(self):
156156
session.close()
157157

158158
def test_transaction_rollback(self):
159-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
159+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
160160
session = driver.session()
161161
# tag::transaction-rollback[]
162162
tx = session.begin_transaction()
@@ -170,7 +170,7 @@ def test_transaction_rollback(self):
170170
session.close()
171171

172172
def test_result_summary_query_profile(self):
173-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
173+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
174174
session = driver.session()
175175
# tag::result-summary-query-profile[]
176176
result = session.run("PROFILE MATCH (p:Person {name: {name}}) "
@@ -183,7 +183,7 @@ def test_result_summary_query_profile(self):
183183
session.close()
184184

185185
def test_result_summary_notifications(self):
186-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
186+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
187187
session = driver.session()
188188
# tag::result-summary-notifications[]
189189
result = session.run("EXPLAIN MATCH (a), (b) RETURN a,b")

neo4j/v1/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@
2121
from .constants import *
2222
from .session import *
2323
from .typesystem import *
24-
from .auth import *

neo4j/v1/connection.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from os import makedirs, open as os_open, write as os_write, close as os_close, O_CREAT, O_APPEND, O_WRONLY
2929
from os.path import dirname, isfile
3030
from select import select
31-
from socket import create_connection, SHUT_RDWR
31+
from socket import create_connection, SHUT_RDWR, error as SocketError
3232
from ssl import HAS_SNI, SSLError
3333
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from
3434

@@ -66,7 +66,7 @@
6666
}
6767

6868
# Set up logger
69-
log = logging.getLogger("neo4j")
69+
log = logging.getLogger("neo4j.bolt")
7070
log_debug = log.debug
7171
log_info = log.info
7272
log_warning = log.warning
@@ -212,30 +212,29 @@ def __init__(self, sock, **config):
212212
user_agent = user_agent.decode("UTF-8")
213213
self.user_agent = user_agent
214214

215+
# Determine auth details
216+
try:
217+
self.auth_dict = vars(config["auth"])
218+
except KeyError:
219+
self.auth_dict = {}
220+
215221
# Pick up the server certificate, if any
216222
self.der_encoded_server_certificate = config.get("der_encoded_server_certificate")
217223

218224
def on_failure(metadata):
219-
raise ProtocolError("Initialisation failed")
225+
raise ProtocolError(metadata.get("message", "Inititalisation failed"))
220226

221-
self.auth_token = config.get("auth")
222227
response = Response(self)
223228
response.on_failure = on_failure
224229

225-
self.append(INIT, (self.user_agent,self._auth_token_dict(),), response=response)
230+
self.append(INIT, (self.user_agent, self.auth_dict), response=response)
226231
self.send()
227232
while not response.complete:
228233
self.fetch()
229234

230235
def __del__(self):
231236
self.close()
232237

233-
def _auth_token_dict(self):
234-
if self.auth_token:
235-
return self.auth_token._asdict()
236-
else:
237-
return {}
238-
239238
def append(self, signature, fields=(), response=None):
240239
""" Add a message to the outgoing queue.
241240
@@ -377,7 +376,13 @@ def connect(host, port=None, ssl_context=None, **config):
377376
# Establish a connection to the host and port specified
378377
port = port or DEFAULT_PORT
379378
if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
380-
s = create_connection((host, port))
379+
try:
380+
s = create_connection((host, port))
381+
except SocketError as error:
382+
if error.errno == 111:
383+
raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % (host, port))
384+
else:
385+
raise
381386

382387
# Secure the connection if an SSL context has been provided
383388
if ssl_context:

neo4j/v1/session.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ class which can be used to obtain `Driver` instances that are used for
4646
STATEMENT_TYPE_SCHEMA_WRITE = "sw"
4747

4848

49+
def basic_auth(user, password):
50+
""" Generate a basic auth token for a given user and password.
51+
52+
:param user: user name
53+
:param password: current password
54+
:return: auth token for use with :meth:`GraphDatabase.driver`
55+
"""
56+
return AuthToken("basic", user, password)
57+
58+
59+
class AuthToken(object):
60+
""" Container for auth information
61+
"""
62+
63+
def __init__(self, scheme, principal, credentials):
64+
self.scheme = scheme
65+
self.principal = principal
66+
self.credentials = credentials
67+
68+
4969
class GraphDatabase(object):
5070
""" The :class:`.GraphDatabase` class provides access to all graph
5171
database functionality. This is primarily used to construct a driver

neokit

runtests.sh

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ DRIVER_HOME=$(dirname $0)
2121

2222
NEORUN_OPTIONS=""
2323
RUNNING=0
24+
KNOWN_HOSTS="${HOME}/.neo4j/known_hosts"
25+
KNOWN_HOSTS_BACKUP="${KNOWN_HOSTS}.backup"
26+
27+
FG_BRIGHT_RED='\033[1;31m'
28+
FG_DEFAULT='\033[0m'
2429

2530
# Parse options
2631
while getopts ":dr" OPTION
@@ -57,33 +62,45 @@ then
5762
VERSIONS="nightly"
5863
fi
5964

65+
function check_exit_status {
66+
EXIT_STATUS=$1
67+
if [ ${EXIT_STATUS} -ne 0 ]
68+
then
69+
echo ""
70+
echo -e "${FG_BRIGHT_RED}Tests failed with status ${EXIT_STATUS}${FG_DEFAULT}"
71+
exit ${EXIT_STATUS}
72+
fi
73+
}
74+
6075
# Run tests
61-
echo "Running tests with $(python --version)"
76+
echo "Using $(python --version)"
6277
pip install --upgrade -r ${DRIVER_HOME}/test_requirements.txt
6378
echo ""
64-
python -c 'from test.util import *; change_password("neo4j", "neo4j", "tmp")'
65-
python -c 'from test.util import *; change_password("neo4j", "tmp", "neo4j")'
79+
6680
TEST_RUNNER="coverage run -m ${UNITTEST} discover -vfs ${TEST}"
6781
EXAMPLES_RUNNER="coverage run -m ${UNITTEST} discover -vfs examples"
6882
BEHAVE_RUNNER="behave --tags=-db --tags=-in_dev test/tck"
83+
6984
if [ ${RUNNING} -eq 1 ]
7085
then
7186
${TEST_RUNNER}
72-
EXIT_STATUS=$?
87+
check_exit_status $?
7388
else
89+
#echo "Updating password"
90+
#mv ${KNOWN_HOSTS} ${KNOWN_HOSTS_BACKUP}
91+
#neokit/neorun ${NEORUN_OPTIONS} "python -m test.auth password" ${VERSIONS}
92+
#EXIT_STATUS=$?
93+
#mv ${KNOWN_HOSTS_BACKUP} ${KNOWN_HOSTS}
94+
#check_exit_status ${EXIT_STATUS}
95+
export NEO4J_PASSWORD="password"
96+
97+
echo "Running unit tests"
7498
neokit/neorun ${NEORUN_OPTIONS} "${TEST_RUNNER}" ${VERSIONS}
75-
EXIT_STATUS=$?
76-
if [ ${EXIT_STATUS} -ne 0 ]
77-
then
78-
exit ${EXIT_STATUS}
79-
fi
99+
check_exit_status $?
80100

101+
echo "Testing example code"
81102
neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS}
82-
EXIT_STATUS=$?
83-
if [ ${EXIT_STATUS} -ne 0 ]
84-
then
85-
exit ${EXIT_STATUS}
86-
fi
103+
check_exit_status $?
87104

88105
coverage report --show-missing
89106
python -c 'from test.tck.configure_feature_files import *; set_up()'
@@ -93,9 +110,3 @@ else
93110
echo "Feature files removed"
94111

95112
fi
96-
97-
# Exit correctly
98-
if [ ${EXIT_STATUS} -ne 0 ]
99-
then
100-
exit ${EXIT_STATUS}
101-
fi

neo4j/v1/auth.py renamed to test/auth.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,26 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
from collections import namedtuple
2221

23-
AuthToken = namedtuple("AuthToken", ("scheme", "principal", "credentials"))
22+
from sys import argv
2423

24+
from neo4j.v1 import GraphDatabase, basic_auth
25+
from neo4j.util import Watcher
2526

26-
def basic_auth(user, password):
27-
return AuthToken("basic", user, password)
27+
28+
def update_password(user, password, new_password):
29+
""" Test utility for setting the initial password.
30+
31+
:param user: user name
32+
:param password: current password
33+
:param new_password: new password
34+
"""
35+
36+
token = basic_auth(user, password)
37+
setattr(token, "new-credentials", new_password) # TODO: hopefully switch hyphen to underscore on server
38+
GraphDatabase.driver("bolt://localhost", auth=token).session().close()
39+
40+
41+
if __name__ == "__main__":
42+
Watcher("neo4j.bolt").watch()
43+
update_password("neo4j", "neo4j", argv[1])

test/tck/tck_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from neo4j.v1.compat import string
2323

2424

25-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_NONE)
25+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"), security=SECURITY_NONE)
2626

2727
def send_string(text):
2828
session = driver.session()

0 commit comments

Comments
 (0)