Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion lib/adapters/cryptopus_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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__
19 changes: 11 additions & 8 deletions lib/models/encryptable.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 12 additions & 4 deletions lib/serializers/encryptable_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -34,17 +38,21 @@ 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

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
Expand Down
16 changes: 16 additions & 0 deletions spec/adapters/cryptopus_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand All @@ -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

Expand Down Expand Up @@ -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'
}
}
Expand Down Expand Up @@ -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'
}
}
Expand Down
98 changes: 97 additions & 1 deletion spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand All @@ -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

Expand Down Expand Up @@ -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')

Expand Down
38 changes: 33 additions & 5 deletions spec/serializers/account_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -27,15 +37,25 @@

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)

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
Expand All @@ -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: {
Expand All @@ -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
Expand Down