shelly 0.4.14 → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59753e3f10e5571d9494a1a84d80513db2fc6ec9
4
- data.tar.gz: 4f1839417e9f9f8856d202ef36c080fc8a3819c3
3
+ metadata.gz: c88ab917360eccc4baf23caa8dce7f2581801ad9
4
+ data.tar.gz: dedbab3b1040b164fd053b92f768d937d31d06d3
5
5
  SHA512:
6
- metadata.gz: a143ea1a6ae2d901046b5a2fccadb93d55cb61bd23057ff215ebae481e8d930cb95117b32470ecc97e9dea74c786f679363c0efa4bcccaf5eeb5f90efdea3a89
7
- data.tar.gz: 60594ff58f3263574def896e4e9946d4e0c79a83c13a5e8f282f2f84318c46b138dcd60465be9df9b0023b2bb86db207bc2cb497adf63d4a280bd012c85e27df
6
+ metadata.gz: 650278b4740ac48fe84f5e1373ea11f7af2f2097ab71f4065277c6767a799a75191cb4a3a29911698f199b00e31c7047c54975726d03bdef130928ee765d6316
7
+ data.tar.gz: 9c63c6e8f7acfe50c1fa8fb67310f06b19dddf96a667bacc21eafd8fc15a47c480443cb05049f32ee387c4f921613ce739d2d1133db490e17690388ad230a70f
data/CHANGELOG.md CHANGED
@@ -1,10 +1,13 @@
1
+ ## 0.4.15 / 2013-11-19
2
+
3
+ * [bugfix] Deleting SSH key works even after changing user email at the end
4
+ of it
5
+
1
6
  ## 0.4.14 / 2013-11-19
2
7
 
3
8
  * [bugfix] Show proper error message when login failed
4
9
  * [bugfix] `shelly backups create` without specified database kind should
5
10
  backup all possible databases, read from Cloudfile
6
- * [bugfix] Deleting SSH key works even after changing user email at the end
7
- of it
8
11
 
9
12
  ## 0.4.13 / 2013-11-11
10
13
 
@@ -58,12 +58,12 @@ module Shelly
58
58
  def login(email = nil)
59
59
  user = Shelly::User.new
60
60
  say "Your public SSH key will be uploaded to Shelly Cloud after login."
61
- raise Errno::ENOENT, user.ssh_key_path unless user.ssh_key_exists?
61
+ raise Errno::ENOENT, user.ssh_key.path unless user.ssh_key.exists?
62
62
  email ||= ask_for_email
63
63
  password = ask_for_password(:with_confirmation => false)
64
64
  user.login(email, password)
65
- say "Login successful", :green
66
65
  upload_ssh_key
66
+ say "Login successful", :green
67
67
  list
68
68
  rescue Client::ValidationException => e
69
69
  e.each_error { |error| say_error "#{error}", :with_exit => false }
@@ -251,7 +251,7 @@ Wait until cloud is in 'turned off' state and try again.}
251
251
  desc "logout", "Logout from Shelly Cloud"
252
252
  def logout
253
253
  user = Shelly::User.new
254
- say "Your public SSH key has been removed from Shelly Cloud" if user.delete_ssh_key
254
+ say "Your public SSH key has been removed from Shelly Cloud" if user.ssh_keys.destroy
255
255
  say "You have been successfully logged out" if user.logout
256
256
  end
257
257
 
@@ -459,15 +459,21 @@ Wait until cloud is in 'turned off' state and try again.}
459
459
 
460
460
  def upload_ssh_key
461
461
  user = Shelly::User.new
462
- if user.ssh_key_exists?
463
- say "Uploading your public SSH key from #{user.ssh_key_path}"
464
- user.upload_ssh_key
462
+ if user.ssh_key.exists?
463
+ if user.ssh_key.uploaded?
464
+ say "Your SSH key from #{user.ssh_key.path} is already uploaded"
465
+ else
466
+ say "Uploading your public SSH key from #{user.ssh_key.path}"
467
+ user.ssh_key.upload
468
+ end
465
469
  else
466
470
  say_error "No such file or directory - #{user.ssh_key_path}", :with_exit => false
467
471
  say_error "Use ssh-keygen to generate ssh key pair, after that use: `shelly login`", :with_exit => false
468
472
  end
469
473
  rescue Client::ValidationException => e
470
474
  e.each_error { |error| say_error error, :with_exit => false }
475
+ user.logout
476
+ exit 1
471
477
  end
472
478
  end
473
479
  end
@@ -1,5 +1,6 @@
1
1
  require "shelly/cli/command"
2
2
  require "shelly/cli/organization"
3
+ require "shelly/ssh_keys"
3
4
 
4
5
  module Shelly
5
6
  module CLI
@@ -3,7 +3,11 @@ class Shelly::Client
3
3
  post("/ssh_keys", :ssh_key => ssh_key)
4
4
  end
5
5
 
6
- def delete_ssh_key(ssh_key)
7
- delete("/ssh_keys", :ssh_key => ssh_key)
6
+ def delete_ssh_key(fingerprint)
7
+ delete("/ssh_keys/#{fingerprint}")
8
+ end
9
+
10
+ def ssh_key(fingerprint)
11
+ get("/ssh_keys/#{fingerprint}")
8
12
  end
9
13
  end
@@ -0,0 +1,36 @@
1
+ module Shelly
2
+ class SshKey < Model
3
+ attr_reader :path
4
+ def initialize(path)
5
+ @path = File.expand_path(path)
6
+ end
7
+
8
+ def exists?
9
+ File.exists?(path)
10
+ end
11
+
12
+ def destroy
13
+ shelly.delete_ssh_key(fingerprint) if exists?
14
+ end
15
+
16
+ def upload
17
+ shelly.add_ssh_key(key)
18
+ end
19
+
20
+ def uploaded?
21
+ return false unless exists?
22
+ shelly.ssh_key(fingerprint)
23
+ true
24
+ rescue Shelly::Client::NotFoundException
25
+ false
26
+ end
27
+
28
+ def fingerprint
29
+ `ssh-keygen -lf #{path}`.split(" ")[1]
30
+ end
31
+
32
+ def key
33
+ File.read(path)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ require "shelly/ssh_key"
2
+
3
+ module Shelly
4
+ class SshKeys
5
+ def initialize
6
+ @rsa = SshKey.new('~/.ssh/id_rsa.pub')
7
+ @dsa = SshKey.new('~/.ssh/id_dsa.pub')
8
+ end
9
+
10
+ def destroy
11
+ [@rsa, @dsa].map(&:destroy).any?
12
+ end
13
+
14
+ def prefered_key
15
+ @dsa.exists? ? @dsa : @rsa
16
+ end
17
+ end
18
+ end
data/lib/shelly/user.rb CHANGED
@@ -43,30 +43,16 @@ module Shelly
43
43
  shelly.forget_authorization
44
44
  end
45
45
 
46
- def delete_credentials
47
- File.delete(credentials_path) if credentials_exists?
48
- end
49
-
50
- def delete_ssh_key
51
- shelly.delete_ssh_key(File.read(dsa_key)) if File.exists?(dsa_key)
52
- shelly.delete_ssh_key(File.read(rsa_key)) if File.exists?(rsa_key)
53
- end
54
-
55
- def ssh_key_exists?
56
- File.exists?(ssh_key_path)
46
+ def ssh_keys
47
+ @keys ||= SshKeys.new
57
48
  end
58
49
 
59
- def ssh_key_path
60
- return dsa_key if File.exists?(dsa_key)
61
- rsa_key
50
+ def ssh_key
51
+ ssh_keys.prefered_key
62
52
  end
63
53
 
64
- def dsa_key
65
- File.expand_path("~/.ssh/id_dsa.pub")
66
- end
67
-
68
- def rsa_key
69
- File.expand_path("~/.ssh/id_rsa.pub")
54
+ def delete_credentials
55
+ File.delete(credentials_path) if credentials_exists?
70
56
  end
71
57
 
72
58
  def self.guess_email
@@ -77,11 +63,6 @@ module Shelly
77
63
  File.expand_path("~/.shelly")
78
64
  end
79
65
 
80
- def upload_ssh_key
81
- key = File.read(ssh_key_path).strip
82
- shelly.add_ssh_key(key)
83
- end
84
-
85
66
  protected
86
67
  def credentials_path
87
68
  File.join(config_dir, "credentials")
@@ -1,3 +1,3 @@
1
1
  module Shelly
2
- VERSION = "0.4.14"
2
+ VERSION = "0.4.15"
3
3
  end
@@ -155,11 +155,12 @@ describe Shelly::CLI::Main do
155
155
  end
156
156
 
157
157
  describe "#login" do
158
+ let(:key_path) { File.expand_path("~/.ssh/id_rsa.pub") }
159
+
158
160
  before do
159
- user.stub(:upload_ssh_key)
160
- @key_path = File.expand_path("~/.ssh/id_rsa.pub")
161
+ user.ssh_key.stub(:upload => nil, :uploaded? => false)
161
162
  FileUtils.mkdir_p("~/.ssh")
162
- File.open("~/.ssh/id_rsa.pub", "w") { |f| f << "ssh-key AAbbcc" }
163
+ File.open(key_path, "w") { |f| f << "ssh-rsa AAAAB3NzaC1" }
163
164
  @client.stub(:apps).and_return([
164
165
  {"code_name" => "abc", "state" => "running",
165
166
  "state_description" => "running"},
@@ -187,8 +188,8 @@ describe Shelly::CLI::Main do
187
188
  end
188
189
 
189
190
  it "should upload user's public SSH key" do
190
- user.should_receive(:upload_ssh_key)
191
- $stdout.should_receive(:puts).with("Uploading your public SSH key from #{@key_path}")
191
+ user.ssh_key.should_receive(:upload)
192
+ $stdout.should_receive(:puts).with("Uploading your public SSH key from #{key_path}")
192
193
  fake_stdin(["megan@example.com", "secret"]) do
193
194
  invoke(@main, :login)
194
195
  end
@@ -202,13 +203,39 @@ describe Shelly::CLI::Main do
202
203
  invoke(@main, :login)
203
204
  end
204
205
  end
206
+
207
+ context "SSH key already uploaded" do
208
+ it "should display message to user" do
209
+ user.ssh_key.stub(:uploaded? => true)
210
+ $stdout.should_receive(:puts).with("Your SSH key from #{key_path} is already uploaded")
211
+ fake_stdin(["megan@example.com", "secret"]) do
212
+ invoke(@main, :login)
213
+ end
214
+ end
215
+ end
216
+
217
+ context "SSH key taken by other user" do
218
+ it "should logout user" do
219
+ body = {"message" => "Validation Failed",
220
+ "errors" => [["fingerprint", "already exists. This SSH key is already in use"]]}
221
+ ex = Shelly::Client::ValidationException.new(body)
222
+ user.ssh_key.stub(:upload).and_raise(ex)
223
+ user.should_receive(:logout)
224
+ $stdout.should_receive(:puts).with(red "Fingerprint already exists. This SSH key is already in use")
225
+ lambda {
226
+ fake_stdin(["megan@example.com", "secret"]) do
227
+ invoke(@main, :login)
228
+ end
229
+ }.should raise_error(SystemExit)
230
+ end
231
+ end
205
232
  end
206
233
 
207
234
  context "when local ssh key doesn't exists" do
208
235
  it "should display error message and return exit with 1" do
209
- FileUtils.rm_rf(@key_path)
210
- File.exists?(@key_path).should be_false
211
- $stdout.should_receive(:puts).with("\e[31mNo such file or directory - " + @key_path + "\e[0m")
236
+ FileUtils.rm_rf(key_path)
237
+ File.exists?(key_path).should be_false
238
+ $stdout.should_receive(:puts).with("\e[31mNo such file or directory - " + key_path + "\e[0m")
212
239
  $stdout.should_receive(:puts).with("\e[31mUse ssh-keygen to generate ssh key pair\e[0m")
213
240
  lambda {
214
241
  invoke(@main, :login)
@@ -1250,9 +1277,9 @@ Wait until cloud is in 'turned off' state and try again.")
1250
1277
  end
1251
1278
 
1252
1279
  it "should notify user that ssh key was removed" do
1253
- user.stub(:delete_ssh_key => true)
1280
+ user.ssh_keys.stub(:destroy => true)
1254
1281
  $stdout.should_receive(:puts).with("Your public SSH key has been removed from Shelly Cloud")
1255
- user.should_receive(:delete_ssh_key)
1282
+ user.ssh_keys.should_receive(:destroy)
1256
1283
  invoke(@main, :logout)
1257
1284
  end
1258
1285
  end
@@ -286,8 +286,8 @@ describe Shelly::Client do
286
286
 
287
287
  describe "#delete_ssh_key" do
288
288
  it "should send delete with given SSH key" do
289
- @client.should_receive(:delete).with("/ssh_keys", {:ssh_key => "abc"})
290
- @client.delete_ssh_key("abc")
289
+ @client.should_receive(:delete).with("/ssh_keys/f6:08:b8:46:df:6d:b2:86:48:ae:e5:7c:25:ef:cf:ad")
290
+ @client.delete_ssh_key("f6:08:b8:46:df:6d:b2:86:48:ae:e5:7c:25:ef:cf:ad")
291
291
  end
292
292
  end
293
293
 
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+ require "shelly/ssh_key"
3
+
4
+ describe Shelly::SshKey do
5
+ let(:ssh_key) { Shelly::SshKey.new("~/.ssh/id_rsa.pub") }
6
+ let(:fingerprint) { "f6:08:b8:46:df:6d:b2:86:48:ae:e5:7c:25:ef:cf:ad" }
7
+ before do
8
+ ssh_key.stub(:fingerprint => fingerprint)
9
+ FileUtils.mkdir_p("~/.ssh")
10
+ File.open(ssh_key.path, "w") { |f| f << "ssh-rsa AAAAB3NzaC1" }
11
+ @client = mock
12
+ Shelly::Client.stub(:new).and_return(@client)
13
+ end
14
+
15
+ describe "#destroy?" do
16
+ it "should destroy key via API" do
17
+ @client.should_receive(:delete_ssh_key).with(fingerprint)
18
+ ssh_key.destroy
19
+ end
20
+ context "key doesn't exist" do
21
+ it "should not try to destroy it" do
22
+ FileUtils.rm_rf(ssh_key.path)
23
+ @client.should_not_receive(:delete_ssh_key)
24
+ ssh_key.destroy
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "#uploaded?" do
30
+ context "key exists for this user on Shelly" do
31
+ it "should return true" do
32
+ @client.stub(:ssh_key => {})
33
+ ssh_key.should be_uploaded
34
+ end
35
+ end
36
+
37
+ context "key doesn't exist on Shelly" do
38
+ it "should return true if key exists in Shelly" do
39
+ ex = Shelly::Client::NotFoundException.new
40
+ @client.stub(:ssh_key).and_raise(ex)
41
+ ssh_key.should_not be_uploaded
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+ require "shelly/ssh_keys"
3
+
4
+ describe Shelly::SshKeys do
5
+ let(:keys) { Shelly::SshKeys.new }
6
+ before { FileUtils.mkdir_p('~/.ssh') }
7
+ describe "#prefered_key" do
8
+ context "dsa key exists" do
9
+ before { FileUtils.touch("~/.ssh/id_dsa.pub") }
10
+ it "should return dsa key" do
11
+ keys.prefered_key.path.should match(/dsa/)
12
+ end
13
+ end
14
+ context "dsa key doesn't exists" do
15
+ it "should return rsa key" do
16
+ keys.prefered_key.path.should match(/rsa/)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -5,9 +5,6 @@ describe Shelly::User do
5
5
  let(:password) { "secret" }
6
6
 
7
7
  before do
8
- FileUtils.mkdir_p("~/.ssh")
9
- File.open("~/.ssh/id_rsa.pub", "w") { |f| f << "rsa-key AAbbcc" }
10
- File.open("~/.ssh/id_dsa.pub", "w") { |f| f << "dsa-key AAbbcc" }
11
8
  @client = mock
12
9
  Shelly::Client.stub(:new).and_return(@client)
13
10
  @user = Shelly::User.new
@@ -42,49 +39,6 @@ describe Shelly::User do
42
39
  end
43
40
  end
44
41
 
45
- describe "#ssh_key_path" do
46
- it "should return path to public dsa key file in the first place" do
47
- @user.ssh_key_path.should == File.expand_path("~/.ssh/id_dsa.pub")
48
- end
49
-
50
- it "should return path to public rsa key file if dsa key is not present" do
51
- FileUtils.rm_rf("~/.ssh/id_dsa.pub")
52
- @user.ssh_key_path.should == File.expand_path("~/.ssh/id_rsa.pub")
53
- end
54
- end
55
-
56
- describe "#ssh_key_exists?" do
57
- it "should return true if key exists, false otherwise" do
58
- @user.should be_ssh_key_exists
59
- FileUtils.rm_rf("~/.ssh/id_rsa.pub")
60
- @user.should be_ssh_key_exists
61
- FileUtils.rm_rf("~/.ssh/id_dsa.pub")
62
- @user.should_not be_ssh_key_exists
63
- end
64
- end
65
-
66
- describe "#delete_ssh_key" do
67
- it "should invoke logout when ssh key exists" do
68
- @client.should_receive(:delete_ssh_key).with('rsa-key AAbbcc').and_return(true)
69
- @client.should_receive(:delete_ssh_key).with('dsa-key AAbbcc').and_return(true)
70
- @user.delete_ssh_key.should be_true
71
- end
72
-
73
- it "should not invoke logout when ssh key doesn't exist" do
74
- FileUtils.rm_rf("~/.ssh/id_rsa.pub")
75
- FileUtils.rm_rf("~/.ssh/id_dsa.pub")
76
- @client.should_not_receive(:logout)
77
- @user.delete_ssh_key.should be_false
78
- end
79
- end
80
-
81
- describe "#upload_ssh_key" do
82
- it "should read and upload user's public SSH key" do
83
- @client.should_receive(:add_ssh_key).with("dsa-key AAbbcc")
84
- @user.upload_ssh_key
85
- end
86
- end
87
-
88
42
  describe "#login" do
89
43
  before do
90
44
  @client.stub(:authorize_with_email_and_password)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.14
4
+ version: 0.4.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shelly Cloud team
@@ -262,6 +262,8 @@ files:
262
262
  - lib/shelly/helpers.rb
263
263
  - lib/shelly/model.rb
264
264
  - lib/shelly/organization.rb
265
+ - lib/shelly/ssh_key.rb
266
+ - lib/shelly/ssh_keys.rb
265
267
  - lib/shelly/structure_validator.rb
266
268
  - lib/shelly/templates/Cloudfile.erb
267
269
  - lib/shelly/user.rb
@@ -292,6 +294,8 @@ files:
292
294
  - spec/shelly/download_progress_bar_spec.rb
293
295
  - spec/shelly/model_spec.rb
294
296
  - spec/shelly/organization_spec.rb
297
+ - spec/shelly/ssh_key_spec.rb
298
+ - spec/shelly/ssh_keys_spec.rb
295
299
  - spec/shelly/structure_validator_spec.rb
296
300
  - spec/shelly/user_spec.rb
297
301
  - spec/spec_helper.rb
@@ -341,6 +345,8 @@ test_files:
341
345
  - spec/shelly/download_progress_bar_spec.rb
342
346
  - spec/shelly/model_spec.rb
343
347
  - spec/shelly/organization_spec.rb
348
+ - spec/shelly/ssh_key_spec.rb
349
+ - spec/shelly/ssh_keys_spec.rb
344
350
  - spec/shelly/structure_validator_spec.rb
345
351
  - spec/shelly/user_spec.rb
346
352
  - spec/spec_helper.rb