Class: Service::HorcruxService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/horcrux_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shares:, threshold:, prefix:, services:) ⇒ HorcruxService

Returns a new instance of HorcruxService.



112
113
114
# File 'lib/active_storage/service/horcrux_service.rb', line 112

def initialize(shares:,threshold:,prefix:,services:)
  @shares, @threshold, @prefix, @services = shares, threshold, prefix, services
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



11
12
13
# File 'lib/active_storage/service/horcrux_service.rb', line 11

def prefix
  @prefix
end

#servicesObject (readonly)

Returns the value of attribute services.



11
12
13
# File 'lib/active_storage/service/horcrux_service.rb', line 11

def services
  @services
end

#sharesObject (readonly)

Returns the value of attribute shares.



11
12
13
# File 'lib/active_storage/service/horcrux_service.rb', line 11

def shares
  @shares
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



11
12
13
# File 'lib/active_storage/service/horcrux_service.rb', line 11

def threshold
  @threshold
end

Class Method Details

.build(services:, shares:, threshold:, prefix:, configurator:, **options) ⇒ Object

Stitch together from named services.



104
105
106
107
108
109
110
# File 'lib/active_storage/service/horcrux_service.rb', line 104

def self.build(services:, shares:, threshold:, prefix:, configurator:, **options) #:nodoc:
  new \
    shares: shares,
	threshold: threshold,
	prefix: prefix,
    services: services.collect { |name| { :name => name, :service => configurator.build(name) } }
end

Instance Method Details

#delete(keys) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_storage/service/horcrux_service.rb', line 87

def delete(keys)
  shardkeys = keys.split(',')
  shards = []
  i = 0
  while i < shardkeys.count
    j = 0
	while j < services.count
      if services[j][:service].exist?(shardkeys[i])
 services[j][:service].delete(shardkeys[i])
	  end
	  j = j + 1
	end
	i = i + 1
  end
end

#delete_prefixed(*args) ⇒ Object

Raises:

  • (ActiveStorage::UnpreviewableError)


116
117
118
# File 'lib/active_storage/service/horcrux_service.rb', line 116

def delete_prefixed(*args)
  raise ActiveStorage::UnpreviewableError, "Horcrux does not implement delete by prefix yet"
end

#download(keys, &block) ⇒ Object



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
# File 'lib/active_storage/service/horcrux_service.rb', line 45

def download(keys,&block)
  shardkeys = keys.split(',')
  shards = []
  i = 0
  while i < shardkeys.count
    j = 0
	while j < services.count
	  begin
        if services[j][:service].exist?(shardkeys[i])
   shard = services[j][:service].download(shardkeys[i])
   shards << shard
   break
 end
 j = j + 1
	  rescue NotImplementedError
 begin
   shard = services[j][:service].download(shardkeys[i]).to_s
   if shard.match(/^invalid/)
     j = j + 1
   else
     shards << shard
		break
   end
 rescue RestClient::BadRequest
   j = j + 1
 end
	  end
    end
	i = i + 1
  end
  secret = TSS.combine(shares: shards)
  if block_given?
    yield Base64.decode64(secret[:secret])
  else
    return Base64.decode64(secret[:secret])
  end
end

#download_chunk(keys, range) ⇒ Object

Raises:

  • (ActiveStorage::UnpreviewableError)


83
84
85
# File 'lib/active_storage/service/horcrux_service.rb', line 83

def download_chunk(keys, range)
  raise ActiveStorage::UnpreviewableError, "Horcrux does not implement ranged download yet"
end

#exist?(keys) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/active_storage/service/horcrux_service.rb', line 120

def exist?(keys)
  localKeys = keys.split(',')
  i = 0
  while i < localKeys.count
    j = 0
	while j < services.count
      if services[j][:service].exist?(localKeys[i])
 return true
	  end
	  j = j + 1
	end
	i = i + 1
  end
  return false
end

#path_for(*args) ⇒ Object

Raises:

  • (ActiveStorage::UnpreviewableError)


140
141
142
# File 'lib/active_storage/service/horcrux_service.rb', line 140

def path_for(*args)
  raise ActiveStorage::UnpreviewableError, "Horcrux does not implement path_for yet"
end

#upload(key, io, checksum: nil, **options) ⇒ Object



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
# File 'lib/active_storage/service/horcrux_service.rb', line 13

def upload(key,io,checksum: nil, **options)
  data = io.tap(&:rewind).read
  base64Data = Base64.encode64(data)
  shards = TSS.split(secret: base64Data,threshold: @threshold,num_shares: @shares)
  i = 0
  main_key = ""
  servicesamples = []
  while i < shards.count
    if servicesamples.empty?
	  servicesamples = services[0..-1]
	end
    svc = servicesamples.sample
	shardkey = SecureRandom.base58(key.length)

	scblob = Class.new Blob
	scblob.service = svc[:service]
	iofile = Tempfile.new(shardkey,"/tmp")
	iofile.write(shards[i])
	iofile.rewind
	myblob = scblob.create_and_upload! io:iofile, filename: ""
	iofile.close
	iofile.unlink

	main_key = main_key + "#{myblob.reload.key},"
	servicesamples.delete(svc)
	i = i + 1
  end
  main_blob = Blob.find_by_key(key)
  main_blob.key = main_key
  main_blob.save!
end

#url(*args) ⇒ Object

Raises:

  • (ActiveStorage::UnpreviewableError)


136
137
138
# File 'lib/active_storage/service/horcrux_service.rb', line 136

def url(*args)
  raise ActiveStorage::UnpreviewableError, "Horcrux does not implement url yet"
end