From 3575cd9bd7095c09253492f314acff8f699d3a45 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 12 Dec 2016 23:09:52 +0100 Subject: [PATCH 1/8] feat change add key --- lib/mpw/cli.rb | 7 +++---- lib/mpw/mpw.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 8cd700a..43256d1 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -334,10 +334,9 @@ class Cli end # Add a new public key - # args: key -> the key name to add - # file -> gpg public file to import - def add_key(key, file=nil) - @mpw.add_key(key, file) + # args: key -> the key name or key file to add + def add_key(key) + @mpw.add_key(key) @mpw.write_data @mpw.sync(true) if @sync diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 91bd04a..7e50547 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -185,12 +185,12 @@ class MPW end # Add a public key - # args: key -> new public key - # file -> public gpg file to import - def add_key(key, file=nil) - if not file.nil? and File.exists?(file) - data = File.open(file).read - GPGME::Key.import(data, armor: true) + # args: key -> new public key file or name + def add_key(key) + if File.exists?(key) + data = File.open(key).read + key_import = GPGME::Key.import(data, armor: true) + key = GPGME::Key.get(key_import.imports[0].fpr).uids[0].email else data = GPGME::Key.export(key, armor: true).read end From bf356f4d6f9dbcbab6ba150d898f99db0cdfa5fb Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 12 Dec 2016 23:13:32 +0100 Subject: [PATCH 2/8] add an option for add a share gpg key --- bin/mpw-wallet | 6 ++++++ i18n/en.yml | 1 + i18n/fr.yml | 1 + 3 files changed, 8 insertions(+) diff --git a/bin/mpw-wallet b/bin/mpw-wallet index b81e6b2..f6ea562 100644 --- a/bin/mpw-wallet +++ b/bin/mpw-wallet @@ -31,6 +31,10 @@ values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" + opts.on('-a', '--add-gpg-key NAME', I18n.t('option.add_gpg_key')) do |gpg_key| + options[:gpg_key] = gpg_key + end + opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| options[:config] = config end @@ -84,6 +88,8 @@ cli.load_config if not options[:list].nil? cli.list_wallet +elsif not options[:gpg_key].nil? + cli.add_key(options[:gpg_key]) else cli.get_wallet(options[:wallet]) cli.decrypt diff --git a/i18n/en.yml b/i18n/en.yml index e1ffcf7..eb45c86 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -50,6 +50,7 @@ en: option: add: "Add an item or key" + add_gpg_key: "Share the wallet with an other GPG key (name or file path)" alpha: "Use letter to generate a password" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" diff --git a/i18n/fr.yml b/i18n/fr.yml index 82e5af3..5a0ebc0 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -50,6 +50,7 @@ fr: option: add: "Ajoute un élément ou une clé" + add_gpg_key: "Partage le portefeuille avec une autre clé GPG (nom ou fichier)" alpha: "Utilise des lettres dans la génération d'un mot de passe" config: "Spécifie le fichier de configuration à utiliser" clipboard: "Désactive la fonction presse papier" From 8c5be6b795a784f5407e34268a39c15b151daac4 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 30 Jan 2017 22:10:56 +0100 Subject: [PATCH 3/8] wallet: add remove gpg key option --- bin/mpw-wallet | 18 ++++++++++++++---- i18n/en.yml | 3 ++- i18n/fr.yml | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/bin/mpw-wallet b/bin/mpw-wallet index f6ea562..8bdfb1c 100644 --- a/bin/mpw-wallet +++ b/bin/mpw-wallet @@ -24,9 +24,10 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = {} -values = {} +options = {} +options[:sync] = {} +options[:delete] = false +values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" @@ -39,6 +40,11 @@ OptionParser.new do |opts| options[:config] = config end + opts.on('-d', '--delete-gpg-key NAME', I18n.t('option.delete_gpg_key')) do |gpg_key| + options[:gpg_key] = gpg_key + options[:delete] = true + end + opts.on('-h', '--help', I18n.t('option.help')) do puts opts exit 0 @@ -89,7 +95,11 @@ cli.load_config if not options[:list].nil? cli.list_wallet elsif not options[:gpg_key].nil? - cli.add_key(options[:gpg_key]) + if options[:delete] + cli.delete_key(options[:gpg_key]) + else + cli.add_key(options[:gpg_key]) + end else cli.get_wallet(options[:wallet]) cli.decrypt diff --git a/i18n/en.yml b/i18n/en.yml index eb45c86..6b1f23f 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -50,10 +50,11 @@ en: option: add: "Add an item or key" - add_gpg_key: "Share the wallet with an other GPG key (name or file path)" + add_gpg_key: "Share the wallet with an other GPG key" alpha: "Use letter to generate a password" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" + add_gpg_key: "Delete the wallet's share with an other GPG key" export: "Export a wallet in an yaml file" file_export: "Specify the file where export data" file_import: "Specify the file to import" diff --git a/i18n/fr.yml b/i18n/fr.yml index 5a0ebc0..8dc6d21 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -50,10 +50,11 @@ fr: option: add: "Ajoute un élément ou une clé" - add_gpg_key: "Partage le portefeuille avec une autre clé GPG (nom ou fichier)" + add_gpg_key: "Partage le portefeuille avec une autre clé GPG" alpha: "Utilise des lettres dans la génération d'un mot de passe" config: "Spécifie le fichier de configuration à utiliser" clipboard: "Désactive la fonction presse papier" + delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG" export: "Exporte un portefeuille dans un fichier yaml" file_export: "Spécifie le fichier où exporter les données" file_import: "Spécifie le fichier à importer" From 06c876861b7d246db798218bcf7ea555c2c96ad4 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 30 Jan 2017 22:26:18 +0100 Subject: [PATCH 4/8] fix english translation --- i18n/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18n/en.yml b/i18n/en.yml index 6b1f23f..6f3d4c8 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -54,7 +54,7 @@ en: alpha: "Use letter to generate a password" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" - add_gpg_key: "Delete the wallet's share with an other GPG key" + delete_gpg_key: "Delete the wallet's share with an other GPG key" export: "Export a wallet in an yaml file" file_export: "Specify the file where export data" file_import: "Specify the file to import" From a1a21f96179f09107770e37e0d72fbc60eb56094 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 11 Feb 2017 11:24:12 +0100 Subject: [PATCH 5/8] mpw: add function list_keys --- lib/mpw/mpw.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 7e50547..c5023e6 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -184,6 +184,12 @@ class MPW @passwords[id] = encrypt(password) end + # Return the list of all gpg keys + # rtrn: an array with the gpg keys name + def list_keys + return @keys.keys + end + # Add a public key # args: key -> new public key file or name def add_key(key) From b287b719fee1f3b47d07ced3ef30ffb9d97ff02a Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 11 Feb 2017 11:24:49 +0100 Subject: [PATCH 6/8] add test to check add and delete gpg key --- test/test_mpw.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_mpw.rb b/test/test_mpw.rb index 0c6a25b..b1c80c9 100644 --- a/test/test_mpw.rb +++ b/test/test_mpw.rb @@ -118,4 +118,26 @@ class TestMPW < Test::Unit::TestCase assert_equal(1, @mpw.list(pattern: 'existing').length) assert_equal(2, @mpw.list(pattern: 'host_[eu]').length) end + + def test_06_add_gpg_key + @config = MPW::Config.new + @config.setup_gpg_key('password', 'test2@example.com', 2048) + + @mpw.read_data + + @mpw.add_key('test2@example.com') + assert_equal(2, @mpw.keys.length) + + @mpw.write_data + end + + def test07_delete_gpg_key + @mpw.read_data + assert_equal(2, @mpw.keys.length) + + @mpw.delete_key('test2@example.com') + assert_equal(1, @mpw.keys.length) + + @mpw.write_data + end end From 4bd538fa7c4d6768094fb959924d8a33718fa237 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 11 Feb 2017 13:43:29 +0100 Subject: [PATCH 7/8] generate a gpg key for the test --- test/init.rb | 19 +++++++++++++++++++ test/test_mpw.rb | 5 +---- test/tests.rb | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 test/init.rb diff --git a/test/init.rb b/test/init.rb new file mode 100644 index 0000000..be7a73a --- /dev/null +++ b/test/init.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby + +require 'gpgme' + +param = '' +param << '' + "\n" +param << "Key-Type: RSA\n" +param << "Key-Length: 2048\n" +param << "Subkey-Type: ELG-E\n" +param << "Subkey-Length: 2048\n" +param << "Name-Real: test\n" +param << "Name-Comment: test\n" +param << "Name-Email: test2@example.com\n" +param << "Expire-Date: 0\n" +param << "Passphrase: password\n" +param << "\n" + +ctx = GPGME::Ctx.new +ctx.genkey(param, nil, nil) diff --git a/test/test_mpw.rb b/test/test_mpw.rb index b1c80c9..d2de795 100644 --- a/test/test_mpw.rb +++ b/test/test_mpw.rb @@ -120,9 +120,6 @@ class TestMPW < Test::Unit::TestCase end def test_06_add_gpg_key - @config = MPW::Config.new - @config.setup_gpg_key('password', 'test2@example.com', 2048) - @mpw.read_data @mpw.add_key('test2@example.com') @@ -131,7 +128,7 @@ class TestMPW < Test::Unit::TestCase @mpw.write_data end - def test07_delete_gpg_key + def test_07_delete_gpg_key @mpw.read_data assert_equal(2, @mpw.keys.length) diff --git a/test/tests.rb b/test/tests.rb index e4efc5d..904f64e 100644 --- a/test/tests.rb +++ b/test/tests.rb @@ -1,5 +1,6 @@ #!/usr/bin/ruby +require_relative 'init.rb' require_relative 'test_config.rb' require_relative 'test_item.rb' require_relative 'test_mpw.rb' From e6238341cd6436cb4f12ed50996b3614f402ebef Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 11 Feb 2017 13:47:22 +0100 Subject: [PATCH 8/8] fix test --- test/test_mpw.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_mpw.rb b/test/test_mpw.rb index d2de795..c4748f6 100644 --- a/test/test_mpw.rb +++ b/test/test_mpw.rb @@ -123,17 +123,17 @@ class TestMPW < Test::Unit::TestCase @mpw.read_data @mpw.add_key('test2@example.com') - assert_equal(2, @mpw.keys.length) + assert_equal(2, @mpw.list_keys.length) @mpw.write_data end def test_07_delete_gpg_key @mpw.read_data - assert_equal(2, @mpw.keys.length) + assert_equal(2, @mpw.list_keys.length) @mpw.delete_key('test2@example.com') - assert_equal(1, @mpw.keys.length) + assert_equal(1, @mpw.list_keys.length) @mpw.write_data end