shatter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21861829b74cc78af7c6dc8db6b9bab8fd13e87c
4
- data.tar.gz: 57b5858ca1af53a8c035d83c818f46df455409ea
3
+ metadata.gz: 20e9bdc967f06ba0dee596b2020c07df2fe81c84
4
+ data.tar.gz: 4e1e0424410eb2af42f20fcd5631aa58b375e33a
5
5
  SHA512:
6
- metadata.gz: cb13accb271cb65b0da9b641544b46131cbef0c2a97f3332e0ad841126aa8e5005058432f39c196db168cf910c70225ffcb3f54ed1824bbd7df915588c102f43
7
- data.tar.gz: 6b54e5c27425d2c25f3fa4bb898a5425c6c8a38635d82d273de626417f7ec9b6141ee0084e57d117c4971e2b3b0bef8ba81d4c52b415d35b02cbc3085930f563
6
+ metadata.gz: ad48cf810f51ad4e2a6bc290a6b88c8d97ca4557176975afcc83a3c68473f92f26201abf5aed018cbd8b8a4a7e07f5595a29975ece8fa8fdf6050e4834acd328
7
+ data.tar.gz: bb3065cffdd67f6ab83c16fea8efeb7e4414fade4b39a42378458610837045fa571e948097a456bc95df87ca655560bafcded416c9e38aeeac7418d53f17d4ae
@@ -1,21 +1,36 @@
1
1
  require 'funtools'
2
+ require 'rbnacl'
3
+ require 'rbnacl/libsodium'
2
4
  require 'shatter/controller'
3
5
 
4
6
  module Shatter
5
7
  extend self
6
- VERSION = '0.0.1'
8
+ VERSION = '0.0.2'
7
9
  PORTRANGE = 9479..9749
8
10
 
9
11
  controller = nil
10
12
  set = ->(new_controller) { controller ||= new_controller }
11
13
 
14
+ get_random = ->(varname, size = RbNaCl::PasswordHash::SCrypt::SALTBYTES) do
15
+ if ENV[varname].to_s.length == size
16
+ ENV[varname].force_encoding('BINARY')
17
+ else
18
+ RbNaCl::Random.random_bytes(size)
19
+ end
20
+ end
21
+
22
+ salt = get_random.('SHATTER_SALT')
23
+ pass = get_random.('SHATTER_PASS')
24
+ size = RbNaCl::SecretBox::KEYBYTES
25
+ key = RbNaCl::PasswordHash.scrypt(pass, salt, 2**20, 2**40, size)
26
+
12
27
  # Public: Set the Controller for the currently running process. This will be
13
28
  # used directly if the current process is expected to be passed messages.
14
29
  #
15
30
  # parent - Pid to which to report the process's new Pid. (default: nil)
16
31
  #
17
32
  # Returns nothing.
18
- define_method(:init) { |parent=nil| set.(Controller.new(parent)) }
33
+ define_method(:init) { |parent=nil| set.(Controller.new(parent, key)) }
19
34
 
20
35
  # Public: Clear the process's Controller, then call init to set a new one.
21
36
  # This is needed after calling fork in order to obtain a new socket.
@@ -11,15 +11,17 @@ module Shatter
11
11
  # thread to manage the mailbox.
12
12
  #
13
13
  # parent - Pid of the parent Controller, if applicable.
14
- def initialize(parent)
14
+ # key - String containing the shared secret for an RbNaCl SecretBox.
15
+ def initialize(parent, key)
15
16
  @parent = parent
17
+ @box = RbNaCl::SecretBox.new(key)
16
18
  @socket = listen
17
19
  @mailbox = Queue.new
18
20
  @known = Shatter::Pidlist.new
19
21
  @chunks = {}
20
22
 
21
23
  pass(@parent, :system, [:childpid, pid]) if @parent
22
- Thread.new { mailbox_loop }
24
+ Thread.new { mailbox_loop(key) }
23
25
  end
24
26
 
25
27
  # Public: Pass items in the current mailbox to a given block, removing them
@@ -45,7 +47,7 @@ module Shatter
45
47
  def pid
46
48
  unless @pid
47
49
  _, port, _, ip = @socket.addr.map(&:freeze)
48
- @pid = Shatter::Pid.new($$, ip, port, '')
50
+ @pid = Shatter::Pid.new($$, ip, port, @box, '')
49
51
  end
50
52
  @pid
51
53
  end
@@ -56,15 +58,18 @@ module Shatter
56
58
  # and putting them into the mailbox. This should be run within its own
57
59
  # thread.
58
60
  #
61
+ # key - String containing the shared secret for an RbNaCl SecretBox.
62
+ #
59
63
  # Does not return.
60
- deftail :mailbox_loop do
64
+ deftail :mailbox_loop do |key|
61
65
  connection = @socket.accept
62
66
  Thread.new do
63
67
  data = connection.read
64
68
  begin
65
- message = Marshal.load(data)
69
+ nonce, ciphertext = Marshal.load(data)
70
+ message = Marshal.load(@box.decrypt(nonce, ciphertext))
66
71
  @mailbox << message unless handle_message(message)
67
- rescue ArgumentError
72
+ rescue ArgumentError, RbNaCl::CryptoError, RbNaCl::LengthError
68
73
  end
69
74
  end
70
75
  mailbox_loop
@@ -1,7 +1,7 @@
1
1
  require 'socket'
2
2
 
3
3
  module Shatter
4
- class Pid < Struct.new(:pid, :host, :port, :name)
4
+ class Pid < Struct.new(:pid, :host, :port, :box, :name)
5
5
  # Public: Open a socket to a given Controller and send any number of
6
6
  # messages to it.
7
7
  #
@@ -10,8 +10,10 @@ module Shatter
10
10
  #
11
11
  # Returns nothing.
12
12
  def pass(*messages)
13
- socket = TCPSocket.new(host, port)
14
- socket.send(Marshal.dump(messages), 0)
13
+ socket = TCPSocket.new(host, port)
14
+ nonce = RbNaCl::Random.random_bytes(RbNaCl::SecretBox::NONCEBYTES)
15
+ message = box.encrypt(nonce, Marshal.dump(messages))
16
+ socket.send(Marshal.dump([nonce, message]), 0)
15
17
  socket.close
16
18
  end
17
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tina Wuest
@@ -30,6 +30,46 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.7.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rbnacl
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.1.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.1.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: rbnacl-libsodium
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.0.0
33
73
  description: Framework to facilitate distributed computing with Ruby
34
74
  email: tina@wuest.me
35
75
  executables: []