forked from bitdust-io/devel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer_space.py
1105 lines (1012 loc) · 49.8 KB
/
customer_space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# customer_space.py
#
# Copyright (C) 2008 Veselin Penev, https://bitdust.io
#
# This file (customer_space.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software. If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at [email protected]
#
#
#
"""
.. module:: customer_space.
"""
#------------------------------------------------------------------------------
_Debug = False
_DebugLevel = 10
#------------------------------------------------------------------------------
import os
import time
import base64
#------------------------------------------------------------------------------
from twisted.internet import reactor # @UnresolvedImport
#------------------------------------------------------------------------------
from bitdust.logs import lg
from bitdust.lib import strng
from bitdust.lib import packetid
from bitdust.lib import serialization
from bitdust.lib import jsn
from bitdust.system import bpio
from bitdust.main import settings
from bitdust.main import events
from bitdust.contacts import contactsdb
from bitdust.services import driver
from bitdust.p2p import p2p_service
from bitdust.p2p import commands
from bitdust.storage import accounting
from bitdust.crypt import signed
from bitdust.crypt import my_keys
from bitdust.transport import gateway
from bitdust.transport import callback
from bitdust.stream import p2p_queue
from bitdust.dht import dht_service
from bitdust.dht import dht_records
from bitdust.dht import known_nodes
from bitdust.supplier import list_files
from bitdust.supplier import local_tester
from bitdust.userid import global_id
from bitdust.userid import id_url
from bitdust.userid import my_id
#------------------------------------------------------------------------------
_SupplierFileModifiedLatest = {}
_SupplierFileModifiedNotifyTasks = {}
#------------------------------------------------------------------------------
def init():
callback.append_inbox_callback(on_inbox_packet_received)
events.add_subscriber(on_identity_url_changed, 'identity-url-changed')
events.add_subscriber(on_customer_accepted, 'existing-customer-accepted')
events.add_subscriber(on_customer_accepted, 'new-customer-accepted')
events.add_subscriber(on_customer_terminated, 'existing-customer-denied')
events.add_subscriber(on_customer_terminated, 'existing-customer-terminated')
space_dict, _ = accounting.read_customers_quotas()
for customer_idurl in contactsdb.customers():
known_customer_meta_info = contactsdb.get_customer_meta_info(customer_idurl)
# yapf: disable
events.send('existing-customer-accepted', data=dict(
idurl=customer_idurl,
allocated_bytes=space_dict.get(customer_idurl.to_bin()),
ecc_map=known_customer_meta_info.get('ecc_map'),
position=known_customer_meta_info.get('position'),
))
# yapf: enable
if driver.is_on('service_entangled_dht'):
connect_suppliers_dht_layer()
else:
lg.warn('service service_entangled_dht is OFF')
events.add_subscriber(on_dht_layer_connected, 'dht-layer-connected')
def shutdown():
events.remove_subscriber(on_dht_layer_connected, 'dht-layer-connected')
if driver.is_on('service_entangled_dht'):
dht_service.suspend(layer_id=dht_records.LAYER_SUPPLIERS)
events.remove_subscriber(on_customer_accepted, 'existing-customer-accepted')
events.remove_subscriber(on_customer_accepted, 'new-customer-accepted')
events.remove_subscriber(on_customer_terminated, 'existing-customer-denied')
events.remove_subscriber(on_customer_terminated, 'existing-customer-terminated')
events.remove_subscriber(on_identity_url_changed, 'identity-url-changed')
callback.remove_inbox_callback(on_inbox_packet_received)
#------------------------------------------------------------------------------
def connect_suppliers_dht_layer():
known_seeds = known_nodes.nodes()
d = dht_service.open_layer(
layer_id=dht_records.LAYER_SUPPLIERS,
seed_nodes=known_seeds,
connect_now=True,
attach=True,
)
d.addCallback(on_suppliers_dht_layer_connected)
d.addErrback(lambda *args: lg.err(str(args)))
return d
#------------------------------------------------------------------------------
def register_customer_key(customer_public_key_id, customer_public_key):
"""
Check/refresh/store customer public key locally.
"""
if not customer_public_key_id or not customer_public_key:
lg.warn('customer public key was not provided in the request')
return False
customer_public_key_id = my_keys.latest_key_id(customer_public_key_id)
if my_keys.is_key_registered(customer_public_key_id):
known_customer_public_key = my_keys.get_public_key_raw(customer_public_key_id)
if known_customer_public_key == customer_public_key:
lg.info('customer public key %r already known and public key is matching' % customer_public_key_id)
else:
lg.warn('rewriting customer public key %r' % customer_public_key_id)
my_keys.erase_key(customer_public_key_id)
key_id, key_object = my_keys.read_key_info(customer_public_key)
if not my_keys.register_key(key_id, key_object):
lg.err('failed to register customer public key: %r' % customer_public_key_id)
return False
lg.info('new customer public key registered: %r' % customer_public_key_id)
return True
#------------------------------------------------------------------------------
def verify_ownership(newpacket, raise_exception=False):
"""
At that point packet creator is already verified via signature,
but creator could be not authorized to store data on that node.
Based on owner ID, creator ID and packet ID decision must be made what to do with the packet.
Returns customer IDURL and authorized IDURL of the user who should receive the Ack() or (None, None, ) if not authorized.
"""
# SECURITY
owner_idurl = newpacket.OwnerID
creator_idurl = newpacket.CreatorID
owner_id = owner_idurl.to_id()
creator_id = creator_idurl.to_id()
packet_key_alias, packet_owner_id, _ = packetid.SplitKeyOwnerData(newpacket.PacketID)
customer_idurl = global_id.glob2idurl(packet_owner_id)
packet_key_id = my_keys.latest_key_id(my_keys.make_key_id(packet_key_alias, creator_idurl, creator_glob_id=packet_owner_id))
packet_key_id_registered = my_keys.is_key_registered(packet_key_id)
if _Debug:
lg.args(_DebugLevel, owner_id=owner_id, creator_id=creator_id, customer_id=packet_owner_id, key_registered=packet_key_id_registered)
if newpacket.Command == commands.Data():
if id_url.is_the_same(owner_idurl, creator_idurl):
if contactsdb.is_customer(creator_idurl):
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 1: customer is sending own data to own supplier')
return customer_idurl, creator_idurl
if packet_key_id_registered:
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 10: data sender is not my customer but packet key is registered')
return customer_idurl, creator_idurl
lg.err('FAIL, scenario 6: data sender is not my customer and also packet key is not registered')
if raise_exception:
raise Exception('non-authorized user is trying to store data')
return None, None
if contactsdb.is_customer(creator_idurl):
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 2: customer wants to store data for someone else on own supplier')
# TODO: check that, why do we need that?
return customer_idurl, creator_idurl
if packet_owner_id == owner_id:
if contactsdb.is_customer(owner_idurl):
if packet_key_id_registered:
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 3: another authorized user is sending data to customer to be stored on the supplier')
return customer_idurl, creator_idurl
if _Debug:
lg.dbg(_DebugLevel, 'non-authorized user is trying to store data on the supplier')
return None, None
if newpacket.Command in [commands.DeleteFile(), commands.DeleteBackup()]:
if owner_idurl == creator_idurl:
if contactsdb.is_customer(creator_idurl):
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 4: customer wants to remove already stored data on own supplier')
return customer_idurl, creator_idurl
lg.err('FAIL, scenario 7: non-authorized user is trying to erase data owned by customer from the supplier')
if raise_exception:
raise Exception('non-authorized user is trying to erase data owned by customer from the supplier')
return None, None
if contactsdb.is_customer(creator_idurl):
# TODO: check that, why do we need that?
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 8: customer wants to erase existing data that belongs to someone else but stored on the supplier')
return customer_idurl, creator_idurl
if packet_owner_id == owner_id:
if contactsdb.is_customer(owner_idurl):
if packet_key_id_registered:
if _Debug:
lg.dbg(_DebugLevel, 'OK, scenario 5: another authorized user wants to remove already stored data from the supplier')
return customer_idurl, creator_idurl
lg.err('non-authorized user is trying to erase data on the supplier')
return None, None
if driver.is_enabled('service_proxy_server'):
if _Debug:
lg.dbg(_DebugLevel, 'IGNORE, scenario 9: received Data() was not authorized, but proxy router service is enabled')
return None, None
# TODO:
# scenario 11: make possible to set "active" flag True/False for any key
# this way customer can make virtual location available for other user but in read-only mode
raise Exception('scenario not implemented yet, received %r' % newpacket)
#------------------------------------------------------------------------------
def make_filename(customerGlobID, filePath, keyAlias=None):
keyAlias = keyAlias or 'master'
customerDirName = str(customerGlobID)
customersDir = settings.getCustomersFilesDir()
if not os.path.exists(customersDir):
if _Debug:
lg.dbg(_DebugLevel, 'making a new folder: %s' % customersDir)
bpio._dir_make(customersDir)
ownerDir = os.path.join(customersDir, customerDirName)
if not os.path.exists(ownerDir):
if _Debug:
lg.dbg(_DebugLevel, 'making a new folder: %s' % ownerDir)
bpio._dir_make(ownerDir)
keyAliasDir = os.path.join(ownerDir, keyAlias)
if not os.path.exists(keyAliasDir):
if _Debug:
lg.dbg(_DebugLevel, 'making a new folder: %s' % keyAliasDir)
bpio._dir_make(keyAliasDir)
if packetid.IsIndexFileName(filePath):
filePath = settings.BackupIndexFileName()
filename = os.path.join(keyAliasDir, filePath)
return filename
def make_valid_filename(customerIDURL, glob_path):
"""
Must be a customer, and then we make full path filename for where this
packet is stored locally.
"""
keyAlias = glob_path['key_alias'] or 'master'
filePath = glob_path['path']
customerGlobID = glob_path['customer']
if not customerGlobID:
lg.warn('customer id is empty: %r' % glob_path)
return ''
if filePath != settings.BackupIndexFileName() and not packetid.IsIndexFileName(filePath):
if not packetid.Valid(filePath): # SECURITY
lg.warn('invalid file path')
return ''
# if not contactsdb.is_customer(customerIDURL): # SECURITY
# lg.warn("%s is not my customer" % (customerIDURL))
# if customerGlobID:
# if glob_path['idurl'] != customerIDURL:
# lg.warn('making filename for another customer: %s != %s' % (
# glob_path['idurl'], customerIDURL))
filename = make_filename(customerGlobID, filePath, keyAlias)
return filename
#------------------------------------------------------------------------------
def do_notify_supplier_file_modified(key_alias, remote_path, action, customer_idurl, authorized_idurl):
global _SupplierFileModifiedNotifyTasks
global _SupplierFileModifiedLatest
task_id = '{}_{}${}'.format(action, key_alias, customer_idurl.to_id())
latest_event_time = _SupplierFileModifiedLatest.get(task_id)
current_task = _SupplierFileModifiedNotifyTasks.get(task_id)
if not latest_event_time or (time.time() - latest_event_time > 60):
if _Debug:
lg.args(_DebugLevel, t=task_id, cur=current_task)
if current_task:
if not current_task.called and not current_task.cancelled:
current_task.cancel()
_SupplierFileModifiedNotifyTasks.pop(task_id)
_SupplierFileModifiedLatest[task_id] = time.time()
events.send('supplier-file-modified', data=dict(
action=action,
remote_path=remote_path,
key_alias=key_alias,
authorized_idurl=authorized_idurl,
customer_idurl=customer_idurl,
supplier_idurl=my_id.getIDURL(),
))
return
new_delay = latest_event_time + 60 + 1 - time.time()
if _Debug:
lg.args(_DebugLevel, t=task_id, delay=new_delay, cur=current_task)
if current_task:
if not current_task.called and not current_task.cancelled:
current_task.cancel()
_SupplierFileModifiedNotifyTasks.pop(task_id)
_SupplierFileModifiedNotifyTasks[task_id] = reactor.callLater( # @UndefinedVariable
new_delay,
do_notify_supplier_file_modified,
key_alias,
remote_path,
action,
customer_idurl,
authorized_idurl,
)
#------------------------------------------------------------------------------
def on_data(newpacket):
if id_url.is_the_same(newpacket.OwnerID, my_id.getIDURL()):
# this Data belong to us, SKIP
return False
# if not contactsdb.is_customer(newpacket.OwnerID):
# # SECURITY
# # TODO: process files from another customer : glob_path['idurl']
# lg.warn("skip, %s not a customer, packetID=%s" % (newpacket.OwnerID, newpacket.PacketID))
# # p2p_service.SendFail(newpacket, 'not a customer')
# return False
glob_path = global_id.ParseGlobalID(newpacket.PacketID)
if not glob_path['path']:
# backward compatible check
glob_path = global_id.ParseGlobalID(my_id.getGlobalID('master') + ':' + newpacket.PacketID)
if not glob_path['path']:
lg.err('got incorrect PacketID')
# p2p_service.SendFail(newpacket, 'incorrect path')
return False
remote_path = glob_path['path']
key_alias = glob_path['key_alias']
customer_idurl, authorized_idurl = verify_ownership(newpacket)
if authorized_idurl is None or customer_idurl is None:
if _Debug:
lg.dbg(_DebugLevel, 'ownership verification failed for %r' % newpacket)
# p2p_service.SendFail(newpacket, 'ownership verification failed')
return False
filename = make_valid_filename(newpacket.OwnerID, glob_path)
if not filename:
lg.warn('got empty filename, bad customer or wrong packetID?')
# p2p_service.SendFail(newpacket, 'empty filename')
return False
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
try:
bpio._dirs_make(dirname)
except:
lg.err('can not create sub dir %s' % dirname)
p2p_service.SendFail(newpacket, 'write error', remote_idurl=authorized_idurl)
return False
new_data = newpacket.Serialize()
donated_bytes = settings.getDonatedBytes()
accounting.check_create_customers_quotas(donated_bytes)
space_dict, _ = accounting.read_customers_quotas()
known_customers_quoats = list(space_dict.keys())
if id_url.is_not_in(customer_idurl, known_customers_quoats, as_field=False, as_bin=True):
lg.err('customer space is broken, no info about donated space can be found for %s' % newpacket)
p2p_service.SendFail(newpacket, 'customer space is broken, no info found about donated space', remote_idurl=authorized_idurl)
return False
used_space_dict = accounting.read_customers_usage()
known_customers_usage = list(used_space_dict.keys())
if id_url.is_in(customer_idurl, known_customers_usage, as_field=False, as_bin=True):
try:
bytes_used_by_customer = int(used_space_dict[customer_idurl.to_bin()])
bytes_donated_to_customer = int(space_dict[customer_idurl.to_bin()])
if bytes_donated_to_customer - bytes_used_by_customer < len(new_data):
lg.warn('no free space left for customer data for %s' % customer_idurl)
p2p_service.SendFail(newpacket, 'no free space left for customer data', remote_idurl=authorized_idurl)
return False
except:
lg.exc()
data_exists = not os.path.exists(filename)
data_changed = True
if not data_exists:
if remote_path == settings.BackupIndexFileName() or packetid.IsIndexFileName(remote_path):
current_data = bpio.ReadBinaryFile(filename)
if current_data == new_data:
lg.warn('skip rewriting existing file %s' % filename)
data_changed = False
if data_changed:
if not bpio.WriteBinaryFile(filename, new_data):
lg.err('can not write to %s' % str(filename))
p2p_service.SendFail(newpacket, 'write error', remote_idurl=authorized_idurl)
return False
# Here Data() packet was stored as it is on supplier node (current machine)
del new_data
sz = len(newpacket.Payload)
p2p_service.SendAck(newpacket, response=strng.to_text(sz), remote_idurl=authorized_idurl)
reactor.callLater(0, local_tester.TestSpaceTime) # @UndefinedVariable
if key_alias != 'master' and data_changed:
if remote_path == settings.BackupIndexFileName() or packetid.IsIndexFileName(remote_path):
do_notify_supplier_file_modified(key_alias, settings.BackupIndexFileName(), 'write', customer_idurl, authorized_idurl)
else:
if packetid.BlockNumber(newpacket.PacketID) == 0:
do_notify_supplier_file_modified(key_alias, remote_path, 'write', customer_idurl, authorized_idurl)
if _Debug:
lg.args(_DebugLevel, sz=sz, fn=filename, remote_idurl=authorized_idurl, pid=newpacket.PacketID)
return True
def on_retrieve(newpacket):
# external customer must be able to request
# TODO: add validation of public key
# if not contactsdb.is_customer(newpacket.OwnerID):
# lg.err("had unknown customer %s" % newpacket.OwnerID)
# p2p_service.SendFail(newpacket, 'not a customer')
# return False
glob_path = global_id.ParseGlobalID(newpacket.PacketID)
if not glob_path['path']:
# backward compatible check
glob_path = global_id.ParseGlobalID(my_id.getGlobalID('master') + ':' + newpacket.PacketID)
if not glob_path['path']:
lg.err('got incorrect PacketID')
p2p_service.SendFail(newpacket, 'incorrect path', remote_idurl=newpacket.CreatorID)
return False
if not glob_path['idurl']:
lg.warn('no customer global id found in PacketID: %s' % newpacket.PacketID)
p2p_service.SendFail(newpacket, 'incorrect retrieve request', remote_idurl=newpacket.CreatorID)
return False
key_id = glob_path['key_id']
recipient_idurl = newpacket.OwnerID
if newpacket.CreatorID != glob_path['idurl'] and newpacket.CreatorID != newpacket.OwnerID:
# SECURITY
lg.warn('one of customers requesting a Data from another customer!')
if not my_keys.is_key_registered(key_id):
lg.warn('key %s is not registered' % key_id)
p2p_service.SendFail(newpacket, 'key not registered', remote_idurl=newpacket.CreatorID)
return False
verified = False
if _Debug:
lg.args(_DebugLevel, Payload=newpacket.Payload)
try:
json_payload = serialization.BytesToDict(newpacket.Payload, keys_to_text=True, values_to_text=True)
test_sample_bin = base64.b64decode(json_payload['t'])
test_signature_bin = strng.to_bin(json_payload['s'])
verified = my_keys.verify(key_id, test_sample_bin, test_signature_bin)
except:
lg.exc()
return False
if not verified:
lg.warn('request is not authorized, test sample signature verification failed')
return False
# requester signed the test sample with the private key and we verified the signature with the public key
# now we checked the signature of the test sample and can be sure that requester really possess the same key
recipient_idurl = newpacket.CreatorID
filename = make_valid_filename(newpacket.OwnerID, glob_path)
if not filename:
filename = make_valid_filename(glob_path['idurl'], glob_path)
# if True:
# TODO: settings.getCustomersDataSharingEnabled() and
# SECURITY
# TODO: add more validations for receiver idurl
# recipient_idurl = glob_path['idurl']
# filename = make_valid_filename(glob_path['idurl'], glob_path)
if not filename:
lg.warn('had empty filename')
p2p_service.SendFail(newpacket, 'empty filename', remote_idurl=recipient_idurl)
return False
if not os.path.exists(filename):
lg.warn('did not found requested file locally : %s' % filename)
p2p_service.SendFail(newpacket, 'did not found requested file locally', remote_idurl=recipient_idurl)
return False
if not os.access(filename, os.R_OK):
lg.warn('no read access to requested packet %s' % filename)
p2p_service.SendFail(newpacket, 'failed reading requested file', remote_idurl=recipient_idurl)
return False
data = bpio.ReadBinaryFile(filename)
if not data:
lg.warn('empty data on disk %s' % filename)
p2p_service.SendFail(newpacket, 'empty data on disk', remote_idurl=recipient_idurl)
return False
stored_packet = signed.Unserialize(data)
sz = len(data)
del data
if stored_packet is None:
lg.warn('Unserialize failed, not Valid packet %s' % filename)
p2p_service.SendFail(newpacket, 'unserialize failed', remote_idurl=recipient_idurl)
return False
if not stored_packet.Valid():
lg.warn('Stored packet is not Valid %s' % filename)
p2p_service.SendFail(newpacket, 'stored packet is not valid', remote_idurl=recipient_idurl)
return False
if stored_packet.Command != commands.Data():
lg.warn('sending back packet which is not a Data')
# here Data() packet is sent back as it is...
# that means outpacket.RemoteID=my_id.getIDURL() - it was addressed to that node and stored as it is
# need to take that into account: every time you receive Data() packet
# it can be not a new Data(), but the old data returning back as a response to Retreive() packet
# to solve the issue we will create a new Data() packet
# which will be addressed directly to recipient and "wrap" stored data inside it
return_packet_id = stored_packet.PacketID
if packetid.IsIndexFileName(glob_path['path']):
return_packet_id = newpacket.PacketID
payload = stored_packet.Serialize()
return_packet = signed.Packet(
Command=commands.Data(),
OwnerID=stored_packet.OwnerID,
CreatorID=my_id.getIDURL(),
PacketID=return_packet_id,
Payload=payload,
RemoteID=recipient_idurl,
)
if _Debug:
lg.args(_DebugLevel, file_size=sz, payload_size=len(payload), fn=filename, recipient=recipient_idurl)
if recipient_idurl == stored_packet.OwnerID:
if _Debug:
lg.dbg(_DebugLevel, 'from request %r : sending back %r in %r to owner: %s' % (newpacket, stored_packet, return_packet, recipient_idurl))
gateway.outbox(return_packet)
return True
if _Debug:
lg.dbg(_DebugLevel, 'from request %r : returning data %r in %r owned by %s to %s' % (newpacket, stored_packet, return_packet, stored_packet.OwnerID, recipient_idurl))
gateway.outbox(return_packet)
return True
#------------------------------------------------------------------------------
def on_list_files(newpacket):
json_query = {}
try:
j = serialization.BytesToDict(newpacket.Payload, keys_to_text=True, values_to_text=True)
j['items'][0]
json_query = j
except:
if strng.to_text(newpacket.Payload) == settings.ListFilesFormat():
json_query = {
'items': ['*'],
}
if json_query is None:
lg.exc('unrecognized ListFiles() query received')
return False
# TODO: perform validations before sending back list of files
list_files_global_id = global_id.ParseGlobalID(newpacket.PacketID)
if list_files_global_id['key_id']:
# customer id and data id can be recognized from packet id
# return back list of files according to the request
customer_idurl = list_files_global_id['idurl']
key_id = list_files_global_id['key_id']
else:
# packet id format is unknown
# by default returning back all files from that recipient if he is a customer
customer_idurl = newpacket.OwnerID
key_id = my_keys.make_key_id(alias='customer', creator_idurl=customer_idurl)
key_id = my_keys.latest_key_id(key_id)
list_files.send(
customer_idurl=customer_idurl,
packet_id=newpacket.PacketID,
format_type=settings.ListFilesFormat(),
key_id=key_id,
remote_idurl=newpacket.OwnerID, # send back to the requesting node
query_items=json_query['items'],
)
if _Debug:
lg.args(_DebugLevel, r=newpacket.OwnerID, c=customer_idurl, k=key_id, pid=newpacket.PacketID)
return True
#------------------------------------------------------------------------------
def on_delete_file(newpacket):
# TODO: call verify_ownership()
# SECURITY
if not newpacket.Payload:
ids = [
newpacket.PacketID,
]
else:
ids = strng.to_text(newpacket.Payload).split('\n')
filescount = 0
dirscount = 0
lg.warn('going to erase files: %s' % ids)
customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
for pcktID in ids:
glob_path = global_id.ParseGlobalID(pcktID)
if not glob_path['customer']:
glob_path = global_id.ParseGlobalID(customer_id + ':' + pcktID)
if not glob_path['path']:
lg.err('got incorrect PacketID')
p2p_service.SendFail(newpacket, 'incorrect path')
return False
if customer_id != glob_path['customer']:
# TODO: check that out if this actually suppose to be allowed
lg.warn('trying to delete a file stored for another customer')
continue
# TODO: add validation of customerGlobID
# TODO: process requests from another customer
# SECURITY
filename = make_valid_filename(newpacket.OwnerID, glob_path)
if not filename:
lg.warn('got empty filename, bad customer or wrong packetID?')
p2p_service.SendFail(newpacket, 'not a customer, or file not found')
return False
if os.path.isfile(filename):
try:
os.remove(filename)
filescount += 1
except:
lg.exc()
elif os.path.isdir(filename):
try:
bpio._dir_remove(filename)
dirscount += 1
except:
lg.exc()
else:
lg.warn('path was not found %s' % filename)
do_notify_supplier_file_modified(glob_path['key_alias'], glob_path['path'], 'delete', newpacket.OwnerID, newpacket.CreatorID)
p2p_service.SendAck(newpacket)
if _Debug:
lg.dbg(_DebugLevel, 'from [%s] with %d IDs, %d files and %d folders were removed' % (newpacket.OwnerID, len(ids), filescount, dirscount))
return True
def on_delete_backup(newpacket):
# TODO: call verify_ownership()
# SECURITY
if not newpacket.Payload:
ids = [
newpacket.PacketID,
]
else:
ids = strng.to_text(newpacket.Payload).split('\n')
count = 0
if _Debug:
lg.args(_DebugLevel, ids=ids)
customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
for bkpID in ids:
glob_path = global_id.ParseGlobalID(bkpID)
if not glob_path['customer']:
glob_path = global_id.ParseGlobalID(customer_id + ':' + bkpID)
if not glob_path['path']:
lg.err('got incorrect BackupID')
p2p_service.SendFail(newpacket, 'incorrect backupID')
return False
if customer_id != glob_path['customer']:
lg.warn('trying to delete file stored for another cusomer')
continue
# TODO: add validation of customerGlobID
# TODO: process requests from another customer
filename = make_valid_filename(newpacket.OwnerID, glob_path)
if not filename:
lg.warn('got empty filename, bad customer or wrong packetID?')
p2p_service.SendFail(newpacket, 'not a customer, or file not found')
return False
if os.path.isdir(filename):
try:
bpio._dir_remove(filename)
count += 1
except:
lg.exc()
elif os.path.isfile(filename):
try:
os.remove(filename)
count += 1
except:
lg.exc()
else:
lg.warn('path not found %s' % filename)
do_notify_supplier_file_modified(glob_path['key_alias'], glob_path['path'], 'delete', newpacket.OwnerID, newpacket.CreatorID)
p2p_service.SendAck(newpacket)
if _Debug:
lg.dbg(_DebugLevel, 'from [%s] with %d IDs, %d were removed' % (newpacket.OwnerID, len(ids), count))
return True
#------------------------------------------------------------------------------
def on_customer_accepted(evt):
customer_idurl = id_url.field(evt.data.get('idurl'))
if not customer_idurl:
lg.warn('unknown customer idurl in event data payload')
return False
customer_glob_id = global_id.idurl2glob(customer_idurl)
queue_id = global_id.MakeGlobalQueueID(
queue_alias='supplier-file-modified',
owner_id=customer_glob_id,
supplier_id=my_id.getGlobalID(),
)
if not p2p_queue.is_queue_exist(queue_id):
# customer_key_id = global_id.MakeGlobalID(customer=customer_glob_id, key_alias='customer')
# if my_keys.is_key_registered(customer_key_id):
# TODO: re-think again about the customer key, do we really need it?
if True:
try:
p2p_queue.open_queue(queue_id)
except Exception as exc:
lg.warn('failed to open queue %s : %s' % (queue_id, str(exc)))
# else:
# lg.warn('customer key %r for supplier queue not registered' % customer_key_id)
if p2p_queue.is_queue_exist(queue_id):
if not p2p_queue.is_producer_exist(my_id.getGlobalID()):
try:
p2p_queue.add_producer(my_id.getGlobalID())
except Exception as exc:
lg.warn('failed to add producer: %s' % str(exc))
if p2p_queue.is_producer_exist(my_id.getGlobalID()):
if not p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id):
try:
p2p_queue.connect_producer(my_id.getGlobalID(), queue_id)
except Exception as exc:
lg.warn('failed to connect producer: %s' % str(exc))
if p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id):
if not p2p_queue.is_event_publishing(my_id.getGlobalID(), 'supplier-file-modified'):
try:
p2p_queue.start_event_publisher(my_id.getGlobalID(), 'supplier-file-modified')
except Exception as exc:
lg.warn('failed to start event publisher: %s' % str(exc))
if _Debug:
lg.args(_DebugLevel, c=customer_glob_id, q=queue_id)
return True
def on_customer_terminated(evt):
customer_idurl = evt.data.get('idurl')
if not customer_idurl:
lg.warn('unknown customer idurl in event data payload')
return False
customer_glob_id = global_id.idurl2glob(customer_idurl)
queue_id = global_id.MakeGlobalQueueID(
queue_alias='supplier-file-modified',
owner_id=customer_glob_id,
supplier_id=my_id.getGlobalID(),
)
# TODO: need to decide when to stop producing
# might be that other customers needs that info still
if p2p_queue.is_event_publishing(my_id.getGlobalID(), 'supplier-file-modified'):
try:
p2p_queue.stop_event_publisher(my_id.getGlobalID(), 'supplier-file-modified')
except Exception as exc:
lg.warn('failed to stop event publisher: %s' % str(exc))
if p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id):
try:
p2p_queue.disconnect_producer(my_id.getGlobalID(), queue_id)
except Exception as exc:
lg.warn('failed to disconnect producer: %s' % str(exc))
if p2p_queue.is_producer_exist(my_id.getGlobalID()):
try:
p2p_queue.remove_producer(my_id.getGlobalID())
except Exception as exc:
lg.warn('failed to remove producer: %s' % str(exc))
if p2p_queue.is_queue_exist(queue_id):
try:
p2p_queue.close_queue(queue_id)
except Exception as exc:
lg.warn('failed to stop queue %s : %s' % (queue_id, str(exc)))
if _Debug:
lg.args(_DebugLevel, c=customer_glob_id, q=queue_id)
return True
#------------------------------------------------------------------------------
def on_identity_url_changed(evt):
old_idurl = id_url.field(evt.data['old_idurl'])
# update customer idurl in "space" file
contacts_changed = False
for customer_idurl in contactsdb.customers():
if old_idurl == customer_idurl:
customer_idurl.refresh()
contacts_changed = True
lg.info('found customer idurl rotated : %r -> %r' % (evt.data['old_idurl'], evt.data['new_idurl']))
if contacts_changed:
contactsdb.save_customers()
# update meta info for that customer
meta_info_changed = False
all_meta_info = contactsdb.read_customers_meta_info_all()
for customer_idurl_bin in list(all_meta_info.keys()):
if id_url.is_cached(old_idurl) and id_url.is_cached(customer_idurl_bin):
if old_idurl == id_url.field(customer_idurl_bin):
latest_customer_idurl_bin = id_url.field(customer_idurl_bin).to_bin()
if latest_customer_idurl_bin != customer_idurl_bin:
all_meta_info[latest_customer_idurl_bin] = all_meta_info.pop(customer_idurl_bin)
meta_info_changed = True
lg.info('found customer idurl rotated in customers meta info : %r -> %r' % (latest_customer_idurl_bin, customer_idurl_bin))
if meta_info_changed:
contactsdb.write_customers_meta_info_all(all_meta_info)
# update customer idurl in "space" file
space_dict, free_space = accounting.read_customers_quotas()
space_changed = False
for customer_idurl_bin in list(space_dict.keys()):
if id_url.is_cached(old_idurl) and id_url.is_cached(customer_idurl_bin):
if id_url.field(customer_idurl_bin) == old_idurl:
latest_customer_idurl_bin = id_url.field(customer_idurl_bin).to_bin()
if latest_customer_idurl_bin != customer_idurl_bin:
space_dict[latest_customer_idurl_bin] = space_dict.pop(customer_idurl_bin)
space_changed = True
lg.info('found customer idurl rotated in customer quotas dictionary : %r -> %r' % (latest_customer_idurl_bin, customer_idurl_bin))
if space_changed:
accounting.write_customers_quotas(space_dict, free_space)
# rename customer folder where I store all his files
old_customer_dirname = str(global_id.UrlToGlobalID(evt.data['old_idurl']))
new_customer_dirname = str(global_id.UrlToGlobalID(evt.data['new_idurl']))
customers_dir = settings.getCustomersFilesDir()
old_owner_dir = os.path.join(customers_dir, old_customer_dirname)
new_owner_dir = os.path.join(customers_dir, new_customer_dirname)
if os.path.isdir(old_owner_dir):
try:
bpio.move_dir_recursive(old_owner_dir, new_owner_dir)
lg.info('copied %r into %r' % (old_owner_dir, new_owner_dir))
if os.path.exists(old_owner_dir):
bpio._dir_remove(old_owner_dir)
lg.warn('removed %r' % old_owner_dir)
except:
lg.exc()
# update customer idurl in "spaceused" file
local_tester.TestSpaceTime()
# TODO: reconnect "supplier-file-modified" consumers & producers
return True
#------------------------------------------------------------------------------
def on_service_supplier_request(json_payload, newpacket, info):
customer_idurl = newpacket.OwnerID
customer_id = global_id.UrlToGlobalID(customer_idurl)
bytes_for_customer = 0
try:
bytes_for_customer = int(json_payload['needed_bytes'])
except:
lg.exc()
return p2p_service.SendFail(newpacket, 'invalid payload')
try:
customer_public_key = json_payload['customer_public_key']
customer_public_key_id = customer_public_key['key_id']
except:
customer_public_key = None
customer_public_key_id = None
data_owner_idurl = None
target_customer_idurl = None
family_position = json_payload.get('position')
ecc_map = json_payload.get('ecc_map')
family_snapshot = json_payload.get('family_snapshot')
if family_snapshot:
family_snapshot = id_url.to_bin_list(family_snapshot)
target_customer_id = json_payload.get('customer_id')
key_id = my_keys.latest_key_id(json_payload.get('key_id'))
if key_id:
# this is a request from external user to access shared data stored by one of my customers
# this is "second" customer requesting data from "first" customer
if not key_id or not my_keys.is_valid_key_id(key_id):
lg.warn('missed or invalid key id')
return p2p_service.SendFail(newpacket, 'invalid key id')
target_customer_idurl = global_id.GlobalUserToIDURL(target_customer_id)
if not contactsdb.is_customer(target_customer_idurl):
lg.warn('target user %s is not a customer' % target_customer_id)
return p2p_service.SendFail(newpacket, 'not a customer')
if target_customer_idurl == customer_idurl:
lg.warn('customer %s requesting shared access to own files' % customer_idurl)
return p2p_service.SendFail(newpacket, 'invalid case')
if not my_keys.is_key_registered(key_id):
lg.warn('key not registered: %s' % key_id)
p2p_service.SendFail(newpacket, 'key not registered')
return False
data_owner_idurl = my_keys.split_key_id(key_id)[1]
if not id_url.is_the_same(data_owner_idurl, target_customer_idurl) and not id_url.is_the_same(data_owner_idurl, customer_idurl):
# pretty complex scenario:
# external customer requesting access to data which belongs not to that customer
# this is "third" customer accessing data belongs to "second" customer
# TODO: for now just refuse it
lg.warn('under construction, key_id=%s customer_idurl=%s target_customer_idurl=%s' % (key_id, customer_idurl, target_customer_idurl))
p2p_service.SendFail(newpacket, 'under construction')
return False
register_customer_key(customer_public_key_id, customer_public_key)
# do not create connection with that customer, only accept the request
lg.info('external customer %s requested access to shared data at %s' % (customer_id, key_id))
return p2p_service.SendAck(newpacket, 'accepted')
# key_id is not present in the request:
# this is a request to connect new customer (or reconnect existing one) to that supplier
if not bytes_for_customer or bytes_for_customer < 0:
lg.warn('wrong payload : %s' % newpacket.Payload)
return p2p_service.SendFail(newpacket, 'wrong storage value')
current_customers = contactsdb.customers()
if accounting.check_create_customers_quotas():
lg.info('created new customers quotas file')
space_dict, free_space = accounting.read_customers_quotas()
try:
free_bytes = int(free_space)
except:
lg.exc()
return p2p_service.SendFail(newpacket, 'broken quotas file')
if (customer_idurl not in current_customers and customer_idurl.to_bin() in list(space_dict.keys())):
lg.err('broken quotas file: %r is not a customer, but found in the quotas file' % customer_idurl.to_bin())
return p2p_service.SendFail(newpacket, 'broken quotas file')
if (customer_idurl in current_customers and customer_idurl.to_bin() not in list(space_dict.keys())):
# seems like customer's idurl was rotated, but space file still have the old idurl
# need to find that old idurl value and replace with the new one
for other_customer_idurl in space_dict.keys():
if other_customer_idurl and other_customer_idurl != 'free' and id_url.field(other_customer_idurl) == customer_idurl:
lg.info('found rotated customer identity in space file, switching: %r -> %r' % (other_customer_idurl, customer_idurl.to_bin()))
space_dict[customer_idurl.to_bin()] = space_dict.pop(other_customer_idurl)
break
if customer_idurl.to_bin() not in list(space_dict.keys()):
lg.err('broken customers file: %r is a customer, but not found in the quotas file' % customer_idurl.to_bin())
return p2p_service.SendFail(newpacket, 'broken customers file')
# check/verify/create/update contract with requestor customer
# the contracts are needed to keep track of consumed resources
current_contract = {}
if driver.is_on('service_supplier_contracts'):
from bitdust.supplier import storage_contract
try:
current_contract = storage_contract.prepare_customer_contract(customer_idurl, details={
'allocated_bytes': bytes_for_customer,
'ecc_position': family_position,
'ecc_map': ecc_map,
})
except:
lg.exc()
current_contract = {}
if current_contract and current_contract.get('deny'):
lg.warn('contract processing denied with user %s' % customer_idurl)
# TODO: disabled for now...
current_contract = {}
# return p2p_service.SendFail(newpacket, 'deny:' + jsn.dumps(current_contract))
# check if this is a new customer or an existing one
# for existing one, we have to first release currently allocated resources
if customer_idurl in current_customers:
current_allocated_byes = int(space_dict.get(customer_idurl.to_bin(), 0))
free_bytes += current_allocated_byes
current_customers.remove(customer_idurl)
space_dict.pop(customer_idurl.to_bin())
new_customer = False
else:
new_customer = True
# lg.args(8, new_customer=new_customer, current_allocated_bytes=space_dict.get(customer_idurl.to_bin()))
if free_bytes <= bytes_for_customer:
contactsdb.remove_customer_meta_info(customer_idurl)
accounting.write_customers_quotas(space_dict, free_bytes)
contactsdb.update_customers(current_customers)
contactsdb.save_customers()
if customer_public_key_id:
my_keys.erase_key(customer_public_key_id)
reactor.callLater(0, local_tester.TestUpdateCustomers) # @UndefinedVariable
if new_customer:
lg.info('NEW CUSTOMER: DENIED not enough space available')
events.send('new-customer-denied', data=dict(idurl=customer_idurl))
else:
lg.info('OLD CUSTOMER: DENIED not enough space available')
events.send('existing-customer-denied', data=dict(idurl=customer_idurl))
return p2p_service.SendAck(newpacket, 'deny:{"reason": "not enough space available"}')
free_bytes = free_bytes - bytes_for_customer
current_customers.append(customer_idurl)
space_dict[customer_idurl.to_bin()] = bytes_for_customer
contactsdb.add_customer_meta_info(customer_idurl, {
'ecc_map': ecc_map,
'position': family_position,
'family_snapshot': family_snapshot,
})
accounting.write_customers_quotas(space_dict, free_bytes)
contactsdb.update_customers(current_customers)
contactsdb.save_customers()