Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GPG support for reading router.db #674

Merged
merged 7 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'

gem 'gpgme'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't put requirements in Gemfile, they're in spec file. But I would not want to mandate installing gpgme. I would put it in CVS#setup as require 'gpgme' if @cfg.gpg, so that we only load it, if user is actually using it.

gemspec
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,15 @@ oxidized

Now tell Oxidized where it finds a list of network devices to backup configuration from. You can either use CSV or SQLite as source. To create a CSV source add the following snippet:

Note: If gpg is set to anything other than false it will attempt to decrypt the file contents
```
source:
default: csv
csv:
file: ~/.config/oxidized/router.db
delimiter: !ruby/regexp /:/
gpg: 'false'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpg: false not string false

gpg_password: 'password'
map:
name: 0
model: 1
Expand Down
1 change: 1 addition & 0 deletions lib/oxidized/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Oxidized
require 'oxidized/input/input'
require 'oxidized/output/output'
require 'oxidized/source/source'
require 'gpgme'
class Manager
class << self
def load dir, file
Expand Down
9 changes: 8 additions & 1 deletion lib/oxidized/source/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ def setup
Oxidized.asetus.user.source.csv.delimiter = /:/
Oxidized.asetus.user.source.csv.map.name = 0
Oxidized.asetus.user.source.csv.map.model = 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line being removed intentionally? If so, can you try to elaborate why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Back in again :)

Oxidized.asetus.user.source.csv.gpg = false
Oxidized.asetus.save :user
raise NoConfig, 'no source csv config, edit ~/.config/oxidized/config'
end
end

def load
nodes = []
open(File.expand_path @cfg.file).each_line do |line|
if @cfg.gpg != 'false'
crypto = GPGME::Crypto.new :password => @cfg.gpg_password
file = crypto.decrypt(File.open(@cfg.file)).to_s
else
file = open(File.expand_path @cfg.file)
Copy link
Owner

@ytti ytti Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps

file = File.expand_path(@cfg.file)
file = if @cfg.gpg?
  crypto = GPGME::Crypto.new password: @cfg.gpg_password
  crypto.decrypt(file)
else
  open(file)
end  

end
file.each_line do |line|
next if line.match(/^\s*#/)
data = line.chomp.split(@cfg.delimiter, -1)
next if data.empty?
Expand Down