secure_archive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82f653d284a274fd591e3acb7a6de3c0419265a8
4
+ data.tar.gz: 0ec229f35935ff2de04ebef97f1a9334beb56841
5
+ SHA512:
6
+ metadata.gz: 6d99cbb6dbe021bdfc1c98b6a281d050ac9d2bd5e879f714fc086439a9fcd9777295affe5b7e100fd7a2852c500cdd47fc9d08380e08e71f75d436e4f1e107cf
7
+ data.tar.gz: 3cd9186653a94c9e9ddce64fbe0d28e96f6f317cd47107706266ee631fc4c2f93e46e95152b7d44c90f675ee1e45cc9ef8783c621795c11e20848ebf33f94bb3
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in secure_archive.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Romain Tartière
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # SecureArchive
2
+
3
+ Need to backup a tree of files but can't trust the storage?
4
+
5
+ This gem provides a `gpg-archiver` command that can be used to build and
6
+ maintain an encrypted copy of a directory tree using
7
+ [GnuPG](https://www.gnupg.org/). The encrypted directory can be backed-up
8
+ to untrusted storage using your regular backup solution.
9
+
10
+ ## Installation
11
+
12
+ If you plan to build an application embedding SecureArchive, add this line to
13
+ your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'secure_archive'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ If you plan to use the `gpg-archiver` utility, simply run:
24
+
25
+ $ gem install secure_archive
26
+
27
+ ## Usage
28
+
29
+ The `gpg-archiver` utility requires at least a GnuPG recipient (provided using the `-r` argument), followed by the source and target directories. If the source directory has a trailing `/`, the directory content (and not the directory itself) will be archived, so the following commands are equivalent:
30
+
31
+ ~~~
32
+ gpg-archiver -r user@example.com /var/www/example.com/uploads /var/backup/example.com
33
+ gpg-archiver -r user@example.com /var/www/example.com/uploads/ /var/backup/example.com/uploads
34
+ ~~~
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/sante-link/secure_archive/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'secure_archive'
4
+ require 'optparse'
5
+ require 'pathname'
6
+
7
+ options = {
8
+ recipients: [],
9
+ }
10
+
11
+ def usage
12
+ end
13
+
14
+ opts_parser = OptionParser.new do |opts|
15
+ opts.banner = 'usage: gpg-archiver [options] source_directory destination_directory'
16
+ opts.separator('')
17
+ opts.separator('Available options:')
18
+ opts.on('-r', '--recipients=foo,bar', Array, 'Add recipients (required)') do |v|
19
+ options[:recipients] += v
20
+ end
21
+ end
22
+
23
+ opts_parser.parse!
24
+
25
+ if options[:recipients].count == 0 || ARGV.count != 2 then
26
+ $stderr.puts opts_parser
27
+ exit 1
28
+ end
29
+
30
+ begin
31
+ archiver = SecureArchive::Archiver.new(ARGV.pop, SecureArchive::Encryptor::Gpg.new(options[:recipients]))
32
+ ARGV.each do |source|
33
+ archiver.archive_tree(source)
34
+ end
35
+ rescue SecureArchive::Encryptor::Gpg::RecipientNotInKeyring => e
36
+ $stderr.puts e.message
37
+ exit 1
38
+ end
@@ -0,0 +1,7 @@
1
+ require 'secure_archive/archiver'
2
+ require 'secure_archive/encryptor'
3
+ require 'secure_archive/version'
4
+
5
+ module SecureArchive
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,52 @@
1
+ require 'fileutils'
2
+
3
+ module SecureArchive
4
+ class Archiver
5
+ def initialize(output, encryptor = SecureArchive::Encryptor::Plain.new)
6
+ @output = Pathname.new(output)
7
+ @encryptor = encryptor
8
+ end
9
+
10
+ def archive_tree(src)
11
+ source = Pathname.new(src)
12
+ output = @output
13
+ output += source.basename unless src.is_a?(String) && src[-1..-1] == '/'
14
+ encrypt_tree(source, output)
15
+ cleanup_tree(output, source)
16
+ end
17
+
18
+ private
19
+
20
+ def encrypt_tree(source, destination)
21
+ FileUtils.mkdir_p(destination) unless destination.directory?
22
+ Pathname.new(source).each_child do |file|
23
+ target = destination + file.basename
24
+ if file.directory? then
25
+ encrypt_tree(file.realpath, target)
26
+ elsif file.file? then
27
+ # mtime manipulation has shown problems, the following fails:
28
+ # a.utime(b.atime, b.mtime)
29
+ # a.stat == b.stat
30
+ # Compare time and allow up to 0.1s of difference
31
+ if !target.exist? || (target.stat.mtime - file.stat.mtime).abs > 0.1 then
32
+ @encryptor.encrypt_file(file, target)
33
+ target.utime(file.atime, file.mtime)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def cleanup_tree(dir, ref)
40
+ Pathname.new(dir).each_child do |file|
41
+ source = ref + file.basename
42
+ if source.exist? then
43
+ if file.directory? then
44
+ cleanup_tree(file.realpath, ref + file.basename)
45
+ end
46
+ else
47
+ file.rmtree
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,2 @@
1
+ require 'secure_archive/encryptor/gpg'
2
+ require 'secure_archive/encryptor/plain'
@@ -0,0 +1,26 @@
1
+ require 'gpgme'
2
+
3
+ module SecureArchive
4
+ module Encryptor
5
+ class Gpg
6
+ class RecipientNotInKeyring < StandardError
7
+ end
8
+
9
+ def initialize(recipients)
10
+ @crypto = GPGME::Crypto.new
11
+ recipients.each do |recipient|
12
+ if GPGME::Key.find(:public, recipient).count == 0 then
13
+ raise RecipientNotInKeyring, "Recipient #{recipient} does not exist in keyring"
14
+ end
15
+ end
16
+ @recipients = recipients
17
+ end
18
+
19
+ def encrypt_file(source, destination)
20
+ File.open(destination, 'w') do |w|
21
+ @crypto.encrypt(File.open(source), recipients: @recipients, output: w, always_trust: true)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ module SecureArchive
2
+ module Encryptor
3
+ class Plain
4
+ def encrypt_file(source, destination)
5
+ destination.open('w') do |w|
6
+ source.open do |r|
7
+ buf = r.read(1024 * 16)
8
+ while (buf) do
9
+ w.write(buf)
10
+ buf = r.read(1024 * 16)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,3 @@
1
+ module SecureArchive
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'secure_archive/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "secure_archive"
8
+ spec.version = SecureArchive::VERSION
9
+ spec.authors = ["Romain Tartière"]
10
+ spec.email = ["romain@blogreen.org"]
11
+ spec.summary = %q{Create encrypted directory tree archive for backing-up sensitive data.}
12
+ spec.homepage = "https://github.com/sante-link/secure_archive"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+ spec.add_dependency "gpgme", "~> 2.0.8"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ require 'tempfile'
4
+
5
+ describe SecureArchive::Encryptor::Gpg do
6
+ let(:recipients) { [ 'romain@blogreen.org' ] }
7
+ let(:gpg) { SecureArchive::Encryptor::Gpg.new(recipients) }
8
+
9
+ before do
10
+ @src = Tempfile.new('secure_archive')
11
+ @dst = Tempfile.new('secure_archive')
12
+
13
+ @src.write('Hello, world!')
14
+ @src.close
15
+ end
16
+
17
+ after do
18
+ @src.unlink
19
+ @dst.unlink
20
+ end
21
+
22
+ it 'encrypts data' do
23
+ gpg.encrypt_file(@src.path, @dst.path)
24
+
25
+ expect(File.size?(@dst.path)).to be_truthy
26
+ end
27
+
28
+ describe 'with invalid recipients' do
29
+ let(:recipients) { [ 'invalid@example.com' ] }
30
+
31
+ it 'raises exception' do
32
+ expect { gpg.encrypt_file(@src.path, @dst.path) }.to raise_exception(SecureArchive::Encryptor::Gpg::RecipientNotInKeyring)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ require 'tmpdir'
4
+
5
+ describe SecureArchive do
6
+ it 'has a version number' do
7
+ expect(SecureArchive::VERSION).not_to be nil
8
+ end
9
+
10
+ describe 'processes a tree of files' do
11
+ before do
12
+ @source_dir = Pathname.new(Dir.mktmpdir)
13
+ @archive_dir = Pathname.new(Dir.mktmpdir)
14
+ end
15
+
16
+ after do
17
+ FileUtils.remove_entry_secure(@source_dir)
18
+ FileUtils.remove_entry_secure(@archive_dir)
19
+ end
20
+
21
+ it 'copies new files' do
22
+ FileUtils.touch(@source_dir + 'a')
23
+ FileUtils.touch(@source_dir + 'b')
24
+ FileUtils.touch(@source_dir + 'c')
25
+
26
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'a', @archive_dir + 'a').and_call_original
27
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'b', @archive_dir + 'b').and_call_original
28
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'c', @archive_dir + 'c').and_call_original
29
+
30
+ archiver = SecureArchive::Archiver.new(@archive_dir)
31
+ archiver.archive_tree(@source_dir.realpath.to_s + '/')
32
+ end
33
+
34
+ it 'updates changed files' do
35
+ now = Time.now
36
+ FileUtils.touch(@source_dir + 'a', mtime: now)
37
+ FileUtils.touch(@source_dir + 'b', mtime: now)
38
+ FileUtils.touch(@source_dir + 'c', mtime: now)
39
+ FileUtils.touch(@archive_dir + 'a', mtime: now)
40
+ FileUtils.touch(@archive_dir + 'b', mtime: now - 5)
41
+ FileUtils.touch(@archive_dir + 'c', mtime: now + 20)
42
+
43
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).not_to receive(:encrypt_file).with(@source_dir + 'a', @archive_dir + 'a').and_call_original
44
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'b', @archive_dir + 'b').and_call_original
45
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'c', @archive_dir + 'c').and_call_original
46
+
47
+ archiver = SecureArchive::Archiver.new(@archive_dir)
48
+ archiver.archive_tree(@source_dir.realpath.to_s + '/')
49
+ end
50
+
51
+ it 'removes deleted files' do
52
+ FileUtils.touch(@archive_dir + 'a')
53
+ FileUtils.touch(@archive_dir + 'b')
54
+ FileUtils.touch(@archive_dir + 'c')
55
+
56
+ expect(FileUtils).to receive(:rm_r).with((@archive_dir + 'a').to_s).and_call_original
57
+ expect(FileUtils).to receive(:rm_r).with((@archive_dir + 'b').to_s).and_call_original
58
+ expect(FileUtils).to receive(:rm_r).with((@archive_dir + 'c').to_s).and_call_original
59
+
60
+ archiver = SecureArchive::Archiver.new(@archive_dir)
61
+ archiver.archive_tree(@source_dir.realpath.to_s + '/')
62
+ end
63
+
64
+ it 'creates an additional directory without trailing slash' do
65
+ now = Time.now
66
+ FileUtils.mkdir_p(@archive_dir + @source_dir.basename)
67
+ FileUtils.touch(@source_dir + 'a')
68
+ FileUtils.touch(@source_dir + 'b', mtime: now)
69
+ FileUtils.touch(@archive_dir + @source_dir.basename + 'b', mtime: now)
70
+ FileUtils.touch(@archive_dir + @source_dir.basename + 'c', mtime: now - 20)
71
+
72
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).to receive(:encrypt_file).with(@source_dir + 'a', @archive_dir + @source_dir.basename + 'a').and_call_original
73
+ expect_any_instance_of(SecureArchive::Encryptor::Plain).not_to receive(:encrypt_file).with(@source_dir + 'b', @archive_dir + @source_dir.basename + 'b').and_call_original
74
+ expect(FileUtils).to receive(:rm_r).with((@archive_dir + @source_dir.basename + 'c').to_s).and_call_original
75
+
76
+ archiver = SecureArchive::Archiver.new(@archive_dir)
77
+ archiver.archive_tree(@source_dir.realpath.to_s)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'secure_archive'
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secure_archive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Romain Tartière
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gpgme
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - romain@blogreen.org
72
+ executables:
73
+ - gpg-archiver
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/gpg-archiver
85
+ - lib/secure_archive.rb
86
+ - lib/secure_archive/archiver.rb
87
+ - lib/secure_archive/encryptor.rb
88
+ - lib/secure_archive/encryptor/gpg.rb
89
+ - lib/secure_archive/encryptor/plain.rb
90
+ - lib/secure_archive/version.rb
91
+ - secure_archive.gemspec
92
+ - spec/secure_archive/encryptor/gpg_spec.rb
93
+ - spec/secure_archive_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: https://github.com/sante-link/secure_archive
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Create encrypted directory tree archive for backing-up sensitive data.
119
+ test_files:
120
+ - spec/secure_archive/encryptor/gpg_spec.rb
121
+ - spec/secure_archive_spec.rb
122
+ - spec/spec_helper.rb