From 253f0637e52ad48630582b52b2d35501cc4f5c01 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 8 Jan 2015 17:16:21 -0500 Subject: [PATCH 1/5] Add a -t command for setting the login token --- plugins/commands/login/command.rb | 12 ++++++++++++ plugins/commands/login/locales/en.yml | 2 ++ 2 files changed, 14 insertions(+) diff --git a/plugins/commands/login/command.rb b/plugins/commands/login/command.rb index e16b1afaf..ce0dd7fc9 100644 --- a/plugins/commands/login/command.rb +++ b/plugins/commands/login/command.rb @@ -18,6 +18,10 @@ module VagrantPlugins o.on("-k", "--logout", "Logs you out if you're logged in") do |k| options[:logout] = k end + + o.on("-t", "--token TOKEN", String, "Set the Atlas token") do |t| + options[:token] = t + end end # Parse the options @@ -31,6 +35,8 @@ module VagrantPlugins return execute_check elsif options[:logout] return execute_logout + elsif options[:token] + return execute_token(options[:token]) end # Let the user know what is going on. @@ -78,6 +84,12 @@ module VagrantPlugins @env.ui.success(I18n.t("login_command.logged_out")) return 0 end + + def execute_token(token) + @client.store_token(token) + @env.ui.success(I18n.t("login_command.token_saved")) + return 0 + end end end end diff --git a/plugins/commands/login/locales/en.yml b/plugins/commands/login/locales/en.yml index 51020df1d..74b7ee1cd 100644 --- a/plugins/commands/login/locales/en.yml +++ b/plugins/commands/login/locales/en.yml @@ -28,3 +28,5 @@ en: You are now logged in. logged_out: |- You are logged out. + token_saved: |- + The token was successfully saved. From 07bb6e549ee0a6dca64aac5c31727e5ffa963313 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 8 Jan 2015 17:16:30 -0500 Subject: [PATCH 2/5] Add tests for the login command --- .../plugins/commands/login/command_test.rb | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test/unit/plugins/commands/login/command_test.rb diff --git a/test/unit/plugins/commands/login/command_test.rb b/test/unit/plugins/commands/login/command_test.rb new file mode 100644 index 000000000..2d634c4ca --- /dev/null +++ b/test/unit/plugins/commands/login/command_test.rb @@ -0,0 +1,78 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/commands/login/command") + +describe VagrantPlugins::LoginCommand::Command do + include_context "unit" + + let(:env) { isolated_environment.create_vagrant_env } + + let(:token_path) { env.data_dir.join("vagrant_login_token") } + + let(:stdout) { StringIO.new } + let(:stderr) { StringIO.new } + + subject { described_class.new(argv, env) } + + before do + stub_env("ATLAS_TOKEN" => "") + end + + describe "#execute" do + context "with no args" do + let(:argv) { [] } + end + + context "with --check" do + let(:argv) { ["--check"] } + + context "when there is a token" do + before do + stub_request(:get, %r{^#{Vagrant.server_url}/api/v1/authenticate}) + .to_return(status: 200) + end + + before do + File.open(token_path, "w+") { |f| f.write("abcd1234") } + end + + it "returns 0" do + expect(subject.execute).to eq(0) + end + end + + context "when there is no token" do + it "returns 1" do + expect(subject.execute).to eq(1) + end + end + end + + context "with --logout" do + let(:argv) { ["--logout"] } + + it "returns 0" do + expect(subject.execute).to eq(0) + end + + it "clears the token" do + subject.execute + expect(File.exist?(token_path)).to be(false) + end + end + + context "with --token" do + let(:argv) { ["--token", "efgh5678"] } + + it "returns 0" do + expect(subject.execute).to eq(0) + end + + it "sets the token" do + subject.execute + token = File.read(token_path).strip + expect(token).to eq("efgh5678") + end + end + end +end From ebdb6945751485ab5d16777b7deea92256b0840d Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 8 Jan 2015 17:25:33 -0500 Subject: [PATCH 3/5] Update eventmachine --- website/docs/Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/Gemfile.lock b/website/docs/Gemfile.lock index 7e3a024b4..cced2615a 100644 --- a/website/docs/Gemfile.lock +++ b/website/docs/Gemfile.lock @@ -23,7 +23,7 @@ GEM compass-import-once (1.0.5) sass (>= 3.2, < 3.5) daemons (1.1.9) - eventmachine (1.0.3) + eventmachine (1.0.4) execjs (1.4.1) multi_json (~> 1.0) ffi (1.9.6) From 2ebd0768177804e700a65e32f97986bf79b9ad74 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 8 Jan 2015 17:25:38 -0500 Subject: [PATCH 4/5] Update login docs --- website/docs/source/v2/cli/login.html.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/website/docs/source/v2/cli/login.html.md b/website/docs/source/v2/cli/login.html.md index c8f6ee7d9..1ff6e9c6d 100644 --- a/website/docs/source/v2/cli/login.html.md +++ b/website/docs/source/v2/cli/login.html.md @@ -19,6 +19,7 @@ boxes or [Vagrant Share](/v2/share/index.html) require a login. The reference of available command-line flags to this command is available below. + ## Options * `--check` - This will check if you're logged in. In addition to outputting @@ -28,3 +29,32 @@ is available below. * `--logout` - This will log you out if you're logged in. If you're already logged out, this command will do nothing. It is not an error to call this command if you're already logged out. + +* `--token` - This will set the Atlas login token manually to the provided + string. It is assumed this token is a valid Atlas access token. + + +## Examples + +Securely authenticate to Atlas using a username and password: + +```text +$ vagrant login +# ... +Atlas username: +Atlas password: +``` + +Check if the current user is authenticated: + +```text +$ vagrant login --check +You are already logged in. +``` + +Securely authenticate with Atlas using a token: + +```text +$ vagrant login --token ABCD1234 +The token was successfully saved. +``` From 114858c7d24f777ea9833be1641a828205c2e892 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 9 Jan 2015 13:24:53 -0500 Subject: [PATCH 5/5] Verify the token is valid when saving --- plugins/commands/login/command.rb | 9 +++++- plugins/commands/login/locales/en.yml | 2 ++ .../plugins/commands/login/command_test.rb | 30 +++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/plugins/commands/login/command.rb b/plugins/commands/login/command.rb index ce0dd7fc9..203b09009 100644 --- a/plugins/commands/login/command.rb +++ b/plugins/commands/login/command.rb @@ -88,7 +88,14 @@ module VagrantPlugins def execute_token(token) @client.store_token(token) @env.ui.success(I18n.t("login_command.token_saved")) - return 0 + + if @client.logged_in? + @env.ui.success(I18n.t("login_command.check_logged_in")) + return 0 + else + @env.ui.error(I18n.t("login_command.invalid_token")) + return 1 + end end end end diff --git a/plugins/commands/login/locales/en.yml b/plugins/commands/login/locales/en.yml index 74b7ee1cd..dba4d52d7 100644 --- a/plugins/commands/login/locales/en.yml +++ b/plugins/commands/login/locales/en.yml @@ -24,6 +24,8 @@ en: https://atlas.hashicorp.com. invalid_login: |- Invalid username or password. Please try again. + invalid_token: |- + Invalid token. Please try again. logged_in: |- You are now logged in. logged_out: |- diff --git a/test/unit/plugins/commands/login/command_test.rb b/test/unit/plugins/commands/login/command_test.rb index 2d634c4ca..c92c51174 100644 --- a/test/unit/plugins/commands/login/command_test.rb +++ b/test/unit/plugins/commands/login/command_test.rb @@ -64,14 +64,32 @@ describe VagrantPlugins::LoginCommand::Command do context "with --token" do let(:argv) { ["--token", "efgh5678"] } - it "returns 0" do - expect(subject.execute).to eq(0) + context "when the token is valid" do + before do + stub_request(:get, %r{^#{Vagrant.server_url}/api/v1/authenticate}) + .to_return(status: 200) + end + + it "sets the token" do + subject.execute + token = File.read(token_path).strip + expect(token).to eq("efgh5678") + end + + it "returns 0" do + expect(subject.execute).to eq(0) + end end - it "sets the token" do - subject.execute - token = File.read(token_path).strip - expect(token).to eq("efgh5678") + context "when the token is invalid" do + before do + stub_request(:get, %r{^#{Vagrant.server_url}/api/v1/authenticate}) + .to_return(status: 401) + end + + it "returns 1" do + expect(subject.execute).to eq(1) + end end end end