diff --git a/lib/adapters/cryptopus_adapter.rb b/lib/adapters/cryptopus_adapter.rb index d10eaf7..027819d 100644 --- a/lib/adapters/cryptopus_adapter.rb +++ b/lib/adapters/cryptopus_adapter.rb @@ -34,7 +34,10 @@ def patch(path, body) end def find_encryptable_by_name(name) - secret_encryptable = Encryptable.find_by_name_and_folder_id(name, session_adapter.selected_folder.id) + secret_encryptable = Encryptable.find_by_name_and_folder_id( + name, + session_adapter.selected_folder.id + ) raise CryptopusEncryptableNotFoundError unless secret_encryptable diff --git a/lib/cli.rb b/lib/cli.rb index 6308518..0c55132 100755 --- a/lib/cli.rb +++ b/lib/cli.rb @@ -7,11 +7,10 @@ Dir[File.join(__dir__, '**', '*.rb')].sort.each { |file| require file } -# rubocop:disable Metrics/ClassLength class CLI include Commander::Methods - # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metric/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/BlockLength + # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metric/CyclomaticComplexity, Metrics/PerceivedComplexity def run program :name, 'cry - cryptopus cli' program :version, '1.1.0' @@ -54,6 +53,10 @@ def run c.description = 'Fetches an encryptable by the given id' c.option '--username', String, 'Only show the username of the user' c.option '--password', String, 'Only show the password of the user' + c.option '--pin', String, 'Only show the Pin of the user' + c.option '--token', String, 'Only show the Token of the user' + c.option '--email', String, 'Only show the Email of the user' + c.option '--customAttribute', String, 'Only show the Custom Attribute of the user' c.action do |args, options| exit_with_error(:usage_error, 'id missing') if args.empty? @@ -62,6 +65,10 @@ def run encryptable = Encryptable.find(args.first) out = encryptable.username if options.username out = encryptable.password if options.password + out = encryptable.pin if options.pin + out = encryptable.token if options.token + out = encryptable.email if options.email + out = encryptable.customAttr if options.customAttribute puts out || encryptable.to_yaml end end @@ -146,7 +153,7 @@ def execute_action(options = {}) exit_with_error(:usage_error, 'Folder with the given name ' \ "#{options[:folder_name]} was not found") end - # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metric/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/BlockLength + # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metric/CyclomaticComplexity, Metrics/PerceivedComplexity def extract_login_args(args) @@ -198,6 +205,5 @@ def renew_auth_token session_adapter.update_session({ token: cryptopus_adapter.renewed_auth_token }) end end -# rubocop:enable Metrics/ClassLength CLI.new.run if $PROGRAM_NAME == __FILE__ diff --git a/lib/models/encryptable.rb b/lib/models/encryptable.rb index 3cb88f1..a801dbe 100644 --- a/lib/models/encryptable.rb +++ b/lib/models/encryptable.rb @@ -1,16 +1,19 @@ # frozen_string_literal: true class Encryptable - attr_reader :id, :name, :username, :password, :type + attr_reader :id, :name, :username, :password, :pin, :token, :email, :custom_attr, :type attr_accessor :folder - def initialize(name: nil, username: nil, password: nil, - type: nil, id: nil) - @id = id - @name = name - @username = username - @password = password - @type = type || 'credentials' + def initialize(params = {}) + @id = params[:id] + @name = params[:name] + @username = params[:username] + @password = params[:password] + @pin = params[:pin] + @token = params[:token] + @email = params[:email] + @custom_attr = params[:custom_attr] + @type = params[:type] || 'credentials' end def to_json(*_args) diff --git a/lib/serializers/encryptable_serializer.rb b/lib/serializers/encryptable_serializer.rb index 2381764..e6c9877 100644 --- a/lib/serializers/encryptable_serializer.rb +++ b/lib/serializers/encryptable_serializer.rb @@ -15,6 +15,10 @@ def to_json(encryptable) type: encryptable.type, cleartext_username: encryptable.username, cleartext_password: encryptable.password, + cleartext_pin: encryptable.pin, + cleartext_token: encryptable.token, + cleartext_email: encryptable.email, + cleartext_custom_attr: encryptable.custom_attr }, relationships: { folder: { @@ -34,6 +38,10 @@ def to_yaml(encryptable) 'name' => encryptable.name, 'username' => encryptable.username, 'password' => encryptable.password, + 'pin' => encryptable.pin, + 'token' => encryptable.token, + 'email' => encryptable.email, + 'customAttribute' => encryptable.custom_attr, 'type' => encryptable.type }.to_yaml end @@ -41,10 +49,10 @@ def from_json(json) json = JSON.parse(json, symbolize_names: true) data = json[:data] || json attributes = data[:attributes] - Encryptable.new(name: attributes[:name], - username: attributes[:cleartext_username], - password: attributes[:cleartext_password], - type: attributes[:type], + Encryptable.new(name: attributes[:name], username: attributes[:cleartext_username], + password: attributes[:cleartext_password], pin: attributes[:cleartext_pin], + token: attributes[:cleartext_token], email: attributes[:cleartext_email], + custom_attr: attributes[:cleartext_custom_attr], type: attributes[:type], id: data[:id]) end end diff --git a/spec/adapters/cryptopus_adapter_spec.rb b/spec/adapters/cryptopus_adapter_spec.rb index a88b896..0f34940 100644 --- a/spec/adapters/cryptopus_adapter_spec.rb +++ b/spec/adapters/cryptopus_adapter_spec.rb @@ -32,6 +32,10 @@ name: 'spec_encryptable', cleartext_password: 'gfClNjq21D', cleartext_username: 'ccli_encryptable', + cleartext_pin: '1234', + cleartext_token: 'xcFT', + cleartext_email: 'test@test.com', + cleartext_custom_attr: 'wow', type: 'credentials' } } @@ -52,6 +56,10 @@ expect(attrs[:name]).to eq('spec_encryptable') expect(attrs[:cleartext_username]).to eq('ccli_encryptable') expect(attrs[:cleartext_password]).to eq('gfClNjq21D') + expect(attrs[:cleartext_pin]).to eq('1234') + expect(attrs[:cleartext_token]).to eq('xcFT') + expect(attrs[:cleartext_email]).to eq('test@test.com') + expect(attrs[:cleartext_custom_attr]).to eq('wow') expect(attrs[:type]).to eq('credentials') end @@ -126,6 +134,10 @@ accountname: 'spec_account', cleartext_password: 'gfClNjq21D', cleartext_username: 'ccli_account', + cleartext_pin: '1234', + cleartext_token: 'xcFT', + cleartext_email: 'test@test.com', + cleartext_custom_attr: 'wow', type: 'credentials' } } @@ -217,6 +229,10 @@ accountname: 'spec_account', cleartext_password: 'gfClNjq21D', cleartext_username: 'ccli_account', + cleartext_pin: '1234', + cleartext_token: 'xcFT', + cleartext_email: 'test@test.com', + cleartext_custom_attr: 'wow', type: 'credentials' } } diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index d94b72a..d317a80 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -126,6 +126,10 @@ accountname: 'spec_account', cleartext_password: 'gfClNjq21D', cleartext_username: 'ccli_account', + cleartext_pin: '1234', + cleartext_token: 'xcFT', + cleartext_email: 'test@test.com', + cleartext_custom_attr: 'wow', type: 'credentials' } } @@ -136,7 +140,7 @@ expect{ subject.run } - .to output(/id: 1\naccountname: spec_account\nusername: ccli_account\npassword: gfClNjq21D\ntype: credentials/) + .to output(/id: 1\naccountname: spec_account\nusername: ccli_account\npassword: gfClNjq21D\npin: 1234\ntoken: xcFT\nemail: test@test.com\ncustom_attribute: wow\ntype: credentials/) .to_stdout end @@ -188,6 +192,98 @@ .to_stdout end + it 'exits successfully and showing only pin with flag' do + setup_session + + set_command(:account, '1', '--pin') + json_response = { + data: { + id: 1, + attributes: { + accountname: 'spec_account', + cleartext_pin: '3819', + type: 'Account::Credentials' + } + } + }.to_json + cryptopus_adapter = double + expect(CryptopusAdapter).to receive(:new).and_return(cryptopus_adapter) + expect(cryptopus_adapter).to receive(:get).and_return(json_response) + + expect{ subject.run } + .to output(/3819/) + .to_stdout + end + + it 'exits successfully and showing only token with flag' do + setup_session + + set_command(:account, '1', '--token') + json_response = { + data: { + id: 1, + attributes: { + accountname: 'spec_account', + cleartext_token: 'fdSDALwalS', + type: 'Account::Credentials' + } + } + }.to_json + cryptopus_adapter = double + expect(CryptopusAdapter).to receive(:new).and_return(cryptopus_adapter) + expect(cryptopus_adapter).to receive(:get).and_return(json_response) + + expect{ subject.run } + .to output(/fdSDALwalS/) + .to_stdout + end + + it 'exits successfully and showing only email with flag' do + setup_session + + set_command(:account, '1', '--email') + json_response = { + data: { + id: 1, + attributes: { + accountname: 'spec_account', + cleartext_email: 'hallo@welcome.com', + type: 'Account::Credentials' + } + } + }.to_json + cryptopus_adapter = double + expect(CryptopusAdapter).to receive(:new).and_return(cryptopus_adapter) + expect(cryptopus_adapter).to receive(:get).and_return(json_response) + + expect{ subject.run } + .to output(/hallo@welcome.com/) + .to_stdout + end + + it 'exits successfully and showing only custom attribute with flag' do + setup_session + + set_command(:account, '1', '--customAttribute') + json_response = { + data: { + id: 1, + attributes: { + accountname: 'spec_account', + cleartext_custom_attribute: 'this is my secret attribute hehehe', + type: 'Account::Credentials' + } + } + }.to_json + cryptopus_adapter = double + expect(CryptopusAdapter).to receive(:new).and_return(cryptopus_adapter) + expect(cryptopus_adapter).to receive(:get).and_return(json_response) + + expect{ subject.run } + .to output(/this is my secret attribute hehehe/) + .to_stdout + end + it 'exits with usage error if session missing' do set_command(:account, '1') diff --git a/spec/serializers/account_serializer_spec.rb b/spec/serializers/account_serializer_spec.rb index 11ed3cf..2f5567b 100644 --- a/spec/serializers/account_serializer_spec.rb +++ b/spec/serializers/account_serializer_spec.rb @@ -4,8 +4,14 @@ subject { described_class } context 'to_json' do it 'serializes encryptable to correct json' do - encryptable = Encryptable.new(name: 'spec_encryptable', username: 'spec_encryptable', - password: 'xAherfEDa21)sd', type: 'credentials', id: 1) + encryptable = Encryptable.new(name: 'spec_encryptable', + username: 'spec_encryptable', + password: 'xAherfEDa21)sd', + pin: '1234', + token: 'xcFT', + email: 'test@test.com', + custom_attr: 'wow', + type: 'credentials', id: 1) encryptable.folder = 2 json = subject.to_json(encryptable) @@ -19,6 +25,10 @@ expect(attributes[:name]).to eq(encryptable.name) expect(attributes[:cleartext_username]).to eq(encryptable.username) expect(attributes[:cleartext_password]).to eq(encryptable.password) + expect(attributes[:cleartext_pin]).to eq(encryptable.pin) + expect(attributes[:cleartext_token]).to eq(encryptable.token) + expect(attributes[:cleartext_email]).to eq(encryptable.email) + expect(attributes[:cleartext_custom_attr]).to eq(encryptable.custom_attr) expect(attributes[:type]).to eq(encryptable.type) expect(data[:id]).to eq(encryptable.id) expect(folder[:id]).to eq(encryptable.folder) @@ -27,8 +37,14 @@ context 'to_yaml' do it 'serializes encryptable to correct yaml' do - encryptable = Encryptable.new(name: 'spec_encryptable', username: 'spec_encryptable', - password: 'xAherfEDa21)sd', type: 'credentials', id: 1) + encryptable = Encryptable.new(name: 'spec_encryptable', + username: 'spec_encryptable', + password: 'xAherfEDa21)sd', + pin: '1234', + token: 'xcFT', + email: 'test@test.com', + custom_attr: 'wow', + type: 'credentials', id: 1) json = subject.to_yaml(encryptable) serialized_encryptable = Psych.load(json) @@ -36,6 +52,10 @@ expect(serialized_encryptable['name']).to eq(encryptable.name) expect(serialized_encryptable['username']).to eq(encryptable.username) expect(serialized_encryptable['password']).to eq(encryptable.password) + expect(serialized_encryptable['pin']).to eq(encryptable.pin) + expect(serialized_encryptable['token']).to eq(encryptable.token) + expect(serialized_encryptable['email']).to eq(encryptable.email) + expect(serialized_encryptable['customAttribute']).to eq(encryptable.custom_attr) expect(serialized_encryptable['type']).to eq(encryptable.type) expect(serialized_encryptable['id']).to eq(encryptable.id) end @@ -51,7 +71,11 @@ name: 'spec_encryptable', type: 'credentials', cleartext_username: 'spec_encryptable', - cleartext_password: 'xAherfEDa21)sd' + cleartext_password: 'xAherfEDa21)sd', + cleartext_pin: '1234', + cleartext_token: 'xcFT', + cleartext_email: 'test@test.com', + cleartext_custom_attr: 'wow' }, relationships: { folder: { @@ -71,6 +95,10 @@ expect(encryptable.name).to eq('spec_encryptable') expect(encryptable.username).to eq('spec_encryptable') expect(encryptable.password).to eq('xAherfEDa21)sd') + expect(encryptable.pin).to eq('1234') + expect(encryptable.token).to eq('xcFT') + expect(encryptable.email).to eq('test@test.com') + expect(encryptable.custom_attr).to eq('wow') expect(encryptable.type).to eq('credentials') end end