Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.libnetworkcache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
slapos.libnetworkcache
Commits
9658123b
Commit
9658123b
authored
Jun 28, 2013
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor select/select_generic and make select_generic filtering lazily
parent
3266582f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
37 deletions
+21
-37
slapos/libnetworkcache.py
slapos/libnetworkcache.py
+16
-30
slapos/libnetworkcachetests.py
slapos/libnetworkcachetests.py
+5
-7
No files found.
slapos/libnetworkcache.py
View file @
9658123b
...
...
@@ -22,8 +22,6 @@ import urllib2
import
urlparse
from
OpenSSL
import
crypto
# XXX: code between select/select_generic must be factored
# Timeout here is about timeout to CONNECT to the server (socket initialization then server answers actual data), not to retrieve/send informations.
# To be clear: it is NOT about uploading/downloading data, but about time to connect to the server, then time that server takes to start answering.
TIMEOUT
=
60
...
...
@@ -242,38 +240,24 @@ class NetworkcacheClient(object):
''' Download a file from shacache by selecting the entry in shadir
Raise DirectoryNotFound if no trustable file is found.
'''
url
=
urljoin
(
self
.
shadir_url
,
key
)
request
=
urllib2
.
Request
(
url
=
url
,
data
=
None
,
headers
=
self
.
shadir_header_dict
)
data
=
urllib2
.
urlopen
(
request
,
timeout
=
TIMEOUT
).
read
()
# Filtering...
try
:
data_list
=
json
.
loads
(
data
)
except
Exception
:
raise
DirectoryNotFound
(
'It was impossible to parse json response:
\
n
%s'
%
traceback
.
format_exc
())
if
self
.
signature_certificate_list
:
data_list
=
[
data
for
data
in
data_list
if
self
.
_verifySignatureInCertificateList
(
*
data
)]
if
not
data_list
:
for
information_json
,
signature
in
self
.
select_generic
(
key
,
self
.
signature_certificate_list
):
break
else
:
raise
DirectoryNotFound
(
'Could not find a trustable entry.'
)
information_json
,
signature
=
data_list
[
0
]
try
:
information_dict
=
json
.
loads
(
information_json
)
except
Exception
:
raise
DirectoryNotFound
(
'
It was impossible to parse json-in-json
'
'response:
\
n
%s'
%
traceback
.
format_exc
())
raise
DirectoryNotFound
(
'
Failed to parse json-in-json response:
\
n
%s
'
%
traceback
.
format_exc
())
try
:
sha512
=
information_dict
.
get
(
'sha512'
)
sha512
=
information_dict
[
'sha512'
]
except
Exception
:
raise
DirectoryNotFound
(
'It was impossible to fetch sha512 from '
'directory response (%r):
\
n
%s'
%
(
information_dict
,
traceback
.
format_exc
()))
raise
DirectoryNotFound
(
'No sha512 in directory response (%r):
\
n
%s'
%
(
information_dict
,
traceback
.
format_exc
()))
return
self
.
download
(
sha512
)
def
select_generic
(
self
,
key
):
def
select_generic
(
self
,
key
,
filter
=
True
):
''' Select trustable entries from shadir.
'''
url
=
urljoin
(
self
.
shadir_url
,
key
)
...
...
@@ -283,10 +267,12 @@ class NetworkcacheClient(object):
try
:
data_list
=
json
.
loads
(
data
)
except
Exception
:
raise
DirectoryNotFound
(
'It was impossible to parse json response:
\
n
%s'
%
traceback
.
format_exc
())
return
[
data
for
data
in
data_list
if
self
.
_verifySignatureInCertificateList
(
*
data
)]
raise
DirectoryNotFound
(
'Failed to parse json response:
\
n
%s'
%
traceback
.
format_exc
())
if
filter
:
return
(
data
for
data
in
data_list
if
self
.
_verifySignatureInCertificateList
(
*
data
))
return
data_list
def
_getSignatureString
(
self
,
content
):
"""
...
...
slapos/libnetworkcachetests.py
View file @
9658123b
...
...
@@ -477,7 +477,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
f
.
write
(
'This is not a json.'
)
self
.
assertDirectoryNotFound
(
'
It was impossible
to parse json response'
,
self
.
assertDirectoryNotFound
(
'
Failed
to parse json response'
,
nc
.
select
,
key
)
def
test_select_json_no_in_json_response
(
self
):
...
...
@@ -491,8 +491,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
f
.
write
(
json
.
dumps
([[
'This is not a json.'
,
'signature'
]]))
self
.
assertDirectoryNotFound
(
'It was impossible to parse json-in-json response'
,
self
.
assertDirectoryNotFound
(
'Failed to parse json-in-json response'
,
nc
.
select
,
key
)
def
test_select_json_in_json_no_dict
(
self
):
...
...
@@ -506,8 +505,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
with
open
(
os
.
path
.
join
(
self
.
tree
,
'shadir'
,
key
),
'w'
)
as
f
:
# now remove the entry from shacache
f
.
write
(
json
.
dumps
([[
json
.
dumps
(
'This is a string'
),
'signature'
]]))
self
.
assertDirectoryNotFound
(
'It was impossible to fetch sha512 from directory response'
,
self
.
assertDirectoryNotFound
(
'No sha512 in directory response'
,
nc
.
select
,
key
)
def
test_select_signed_content_server_hacked
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment