Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the client, only source creds from the shared file when necessary #259

Merged
merged 12 commits into from
Feb 10, 2017

Conversation

davidcpell
Copy link
Contributor

Note: the first 2 commits in this PR represent changes that I had to make in order to get the test suite to run on my machine. Several specs seem to assume that there aren't any AWS credentials set as environment variables on the developer's machine.

The last commit is re: #258 and improves the logic related to how the client sources its AWS credentials.

@@ -65,7 +64,7 @@ def self.get_credentials(profile_name, access_key_id, secret_access_key, session
ENV["AWS_SECRET_ACCESS_KEY"],
ENV["AWS_SESSION_TOKEN"]
)
elsif shared_creds.loadable?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is already called in the Aws::SharedCredentials constructor. Cf. http://docs.aws.amazon.com/sdkforruby/api/Aws/SharedCredentials.html (view source)

@davidcpell
Copy link
Contributor Author

added a commit to fix Rubocop stuff

@davidcpell
Copy link
Contributor Author

@jkeiser any thoughts on this?

@davidcpell
Copy link
Contributor Author

@cheeseplus? anyone?

@mwrock
Copy link
Member

mwrock commented Jul 28, 2016

looks like the right thing to so but I'm not a big ec2 user. curious what @jkeiser and @tyler-ball might think.

@cheeseplus
Copy link
Contributor

If this can get rebased we can look at getting this merged.

@davidcpell davidcpell force-pushed the load-shared-creds-lazily branch 2 times, most recently from 1a56107 to a6033f8 Compare November 16, 2016 00:50
@davidcpell
Copy link
Contributor Author

davidcpell commented Nov 16, 2016

Thanks @cheeseplus. I've rebased and fixed a few things up that were made irrelevant by #269.

However, I've now noticed after looking at this again that during the time since I originally submitted the PR Aws::SharedCredentials#loadable? seems to have been deprecated:

https://docs.aws.amazon.com/sdkforruby/api/Aws/SharedCredentials.html#loadable%3F-instance_method

...and is no longer used in the constructor as I mentioned in an in-line comment. If Aws::SharedCredentials.new is called with a credentials profile that doesn't exist, an exception is raised:

[3] pry(main)> creds = Aws::SharedCredentials.new(:profile_name => 'foo')
Aws::Errors::NoSuchProfileError: Profile `foo' not found in /Users/david/.aws/credentials or /Users/david/.aws/config
from /Users/david/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/aws-sdk-core-2.6.22/lib/aws-sdk-core/shared_config.rb:230:in `validate_profile_exists'

I'm trying to decide what the ramifications of that are for the PR. It looks like it might be worth guarding against a failed initialization of Aws::SharedCredentials.

@davidcpell
Copy link
Contributor Author

@cheeseplus This is ready for another look. I'm not sure why the Ruby 2.0.0 build is failing, though. It looks like it's trying to install a version of farady-http-cache that is higher than what's in Gemfile.lock.

@cheeseplus
Copy link
Contributor

Going through all the PRs and would love to get this one rebased - if not we can fix it up as I think it's a good addition.

@davidcpell
Copy link
Contributor Author

@cheeseplus working on rebase right now and almost done cleaning up. All of a sudden the tests are really slow and I'm getting a lot of this, though:

       Detected platform: windows version  on x86_64. Instance Type: t2.micro. Default username: administrator (default).
       instance_type not specified. Using free tier t2.micro instance ...
.       instance_type not specified. Using free tier t2.micro instance ...
.       instance_type not specified. Using free tier t2.micro instance ...
       Detected platform: freebsd version  on x86_64. Instance Type: t2.micro. Default username: ec2-user (default).
       instance_type not specified. Using free tier t2.micro instance ...
.       instance_type not specified. Using free tier t2.micro instance ...
       Detected platform: centos version  on x86_64. Instance Type: t2.micro. Default username: centos (default).
       instance_type not specified. Using free tier t2.micro instance ...

Is that something I should be worried about?

@cheeseplus
Copy link
Contributor

cheeseplus commented Feb 7, 2017

@davidcpell so not sure what specifically would cause the slowness but I did merge a slew of PRs - the verbose testing seems to pre-date any of those PRs though as I validated against a checkout of release 1.2.0

Tests don't seem too bad to me and ironically I'm getting bit by the behaviour this PR resolves.

@davidcpell
Copy link
Contributor Author

Nice, well now tests just seem to be failing due to a conflict between rake 11.0.1 and rspec:
http://stackoverflow.com/questions/35893584/nomethoderror-undefined-method-last-comment-after-upgrading-to-rake-11

@davidcpell
Copy link
Contributor Author

@cheeseplus ^^ I've renamed and think I've got everything cleaned up. Tests passing locally but in Travis failing because of that error

@cheeseplus
Copy link
Contributor

There are conflicts on this one still @davidcpell probably related to my merge/reverts earlier - should be a quick rebase though.

@davidcpell
Copy link
Contributor Author

Ok I'm out but will take a look when I get home

@cheeseplus
Copy link
Contributor

No rush, I'm going to merge this and the Windows 2016 PR before releasing.

@cheeseplus
Copy link
Contributor

Now that #291 is merged you should be able to rebase and fix the rake issue.

WHY:
When I cloned the repo, certain tests in `client_spec.rb` were failing
because I have `ENV` variables set for `aws_access_key_id` and
`aws_secret_access_key`. These were being sourced in specs that were
meant to skip this and look for, eg, the shared creds file. Now
`ClimateControl` is used in those specs to ensure that the variables
aren't set.

Includes extracting a helper method.
WHY:
Currently in `client.rb` there a number of places are checked for creds.
A local variable representing the shared creds file in
`~/.aws/credentials` is created before its known whether that option is
even necessary (i.e. before all the switching begins). This was causing
the client to error out on my machine because I use `ENV` variables and
didn't have a `[default]` profile in my shared creds file, which is what
this was trying to find (see test-kitchen#258).

Rather than create this variable when it's not even clear that we need
it, we can wait to use `Aws::SharedCredentials` when the control flow
reaches that point and more highly prioritized options have been ruled
out.

This also simplifies a number of specs in `client_spec.rb` since the
call to `SharedCredentials` doesn't need to be stubbed out every time.
ALSO:
Improve tests around this method by getting rid of `let` and changing
stubs/spies
Aws::SharedCredentials raises an exception if you try to initialize
it with a profile name that doesn't exist. If you initialize with no
profile name, it will look for a [default] profile.

This change allows that step in the credentials check to avoid an error if, for example, the
user is intending to authenticate with an IAM instance profile and
has no ~/.aws/credentials or, in other cases, for whatever reason has
the credentials file but no [default] profile and hasn't supplied an
alternative.
...so that aws shared credentials file on the testing system won't get picked up
RuboCop said that this instance of 'private' was a 'useless access modifier'
@davidcpell
Copy link
Contributor Author

@cheeseplus think this ready to review. Along with whatever else, let me know if you would prefer me to squash commits, etc...

@cheeseplus
Copy link
Contributor

@davidcpell looks good to me but a squash would definitely be great

@cheeseplus cheeseplus added this to the 1.3.0 milestone Feb 8, 2017
@cheeseplus cheeseplus merged commit 61a1c52 into test-kitchen:master Feb 10, 2017
@cheeseplus
Copy link
Contributor

Forgot about squash and merge being available :D

@cheeseplus
Copy link
Contributor

cheeseplus commented Feb 13, 2017

@davidcpell it seems I'm still getting issues with local credentials getting in the way so I'm not sure this worked as intended -

Failures:

  1) Kitchen::Driver::Aws::Client::get_credentials + STS AssumeRole loads an Instance Profile last
     Failure/Error: sts = ::Aws::STS::Client.new(:credentials => source_creds, :region => region)

       #<Aws::STS::Client (class)> received :new with unexpected arguments
         expected: ({:credentials=>#<InstanceDouble(Aws::InstanceProfileCredentials) (anonymous)>, :region=>"us-west-1"})
              got: ({:credentials=>#<Aws::Credentials access_key_id="xxxxxxxxxxx">, :region=>"us-west-1"})
       Diff:
       @@ -1,3 +1,3 @@
       -[{:credentials=>#<InstanceDouble(Aws::InstanceProfileCredentials) (anonymous)>,
       +[{:credentials=>#<Aws::Credentials access_key_id="xxxxxxxxxxxx">,
          :region=>"us-west-1"}]

     # ./lib/kitchen/driver/aws/client.rb:79:in `get_credentials'
     # ./spec/kitchen/driver/ec2/client_spec.rb:111:in `block (3 levels) in <top (required)>'

  2) Kitchen::Driver::Aws::Client::get_credentials + STS AssumeRole loads shared credentials second to last
     Failure/Error: sts = ::Aws::STS::Client.new(:credentials => source_creds, :region => region)

       #<Aws::STS::Client (class)> received :new with unexpected arguments
         expected: ({:credentials=>#<InstanceDouble(Aws::SharedCredentials) (anonymous)>, :region=>"us-west-1"})
              got: ({:credentials=>#<Aws::Credentials access_key_id="XXXXXXXXXXX">, :region=>"us-west-1"})
       Diff:
       @@ -1,3 +1,3 @@
       -[{:credentials=>#<InstanceDouble(Aws::SharedCredentials) (anonymous)>,
       +[{:credentials=>#<Aws::Credentials access_key_id="XXXXXXXXXXXXXXXX">,
          :region=>"us-west-1"}]

     # ./lib/kitchen/driver/aws/client.rb:79:in `get_credentials'
     # ./spec/kitchen/driver/ec2/client_spec.rb:127:in `block (3 levels) in <top (required)>'

@paperorplastic
Copy link

paperorplastic commented Feb 13, 2017

I'm not fully sure I understand the issue getting fixed here, but I upgraded to kitchen-ec2 1.3.0 in order to take advantage of PR #291 to get Windows 2016 AMI's working. Now when I'm trying to interact with the 2016 instance or my previously working 2012r2 instance, I get the following kitchen error:

c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/request_signer.rb:104:in **`require_credentials': unable to sign request without credentials set (Aws::Errors::MissingCredentialsError)**
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/request_signer.rb:94:in `sign_authenticated_requests'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/request_signer.rb:87:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/helpful_socket_errors.rb:10:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/retry_errors.rb:87:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/query/handler.rb:27:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/user_agent.rb:12:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/seahorse/client/plugins/endpoint.rb:41:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/param_validator.rb:21:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/seahorse/client/plugins/raise_response_errors.rb:14:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/idempotency_token.rb:18:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/aws-sdk-core/plugins/response_paging.rb:26:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/seahorse/client/plugins/response_target.rb:21:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/seahorse/client/request.rb:70:in `send_request'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-core-2.6.35/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/request.rb:24:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/operations.rb:41:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/operations.rb:61:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/resource.rb:147:in `load'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/resource.rb:120:in `data'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/aws-sdk-resources-2.6.35/lib/aws-sdk-resources/resource.rb:223:in `block in add_data_attribute'
        from C:/Users/spitner/AppData/Local/chefdk/gem/ruby/2.3.0/gems/kitchen-ec2-1.3.0/lib/kitchen/driver/ec2.rb:588:in `show_chosen_image'
        from C:/Users/spitner/AppData/Local/chefdk/gem/ruby/2.3.0/gems/kitchen-ec2-1.3.0/lib/kitchen/driver/ec2.rb:255:in `image'
        from C:/Users/spitner/AppData/Local/chefdk/gem/ruby/2.3.0/gems/kitchen-ec2-1.3.0/lib/kitchen/driver/ec2.rb:281:in `actual_platform'
        from C:/Users/spitner/AppData/Local/chefdk/gem/ruby/2.3.0/gems/kitchen-ec2-1.3.0/lib/kitchen/driver/ec2.rb:558:in `default_windows_user_data'
        from C:/Users/spitner/AppData/Local/chefdk/gem/ruby/2.3.0/gems/kitchen-ec2-1.3.0/lib/kitchen/driver/ec2.rb:62:in `block in <class:Ec2>'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/lazy_hash.rb:135:in `proc_or_val'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/lazy_hash.rb:76:in `[]'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/configurable.rb:119:in `block in diagnose'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/configurable.rb:119:in `each'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/configurable.rb:119:in `diagnose'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/instance.rb:245:in `block in diagnose'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/instance.rb:243:in `each'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/instance.rb:243:in `diagnose'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/diagnostic.rb:125:in `block in prepare_instances'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/diagnostic.rb:125:in `each'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/diagnostic.rb:125:in `prepare_instances'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/diagnostic.rb:52:in `read'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/command/diagnose.rb:38:in `call'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/cli.rb:53:in `perform'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/cli.rb:143:in `diagnose'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/cli.rb:334:in `invoke_task'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/bin/kitchen:13:in `block in <top (required)>'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/lib/kitchen/errors.rb:171:in `with_friendly_errors'
        from c:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/test-kitchen-1.14.2/bin/kitchen:13:in `<top (required)>'
        from c:/opscode/chefdk/bin/kitchen:24:in `load'
        from c:/opscode/chefdk/bin/kitchen:24:in `<main>'

This is mostly new ground for me in interacting with AWS EC2 instances, but I do know that what I had was working with 1.2.0 kitchen-ec2 and now it's not. Should I make a new issue? keep conversation here?

I'm running ChefDK 1.1.16 and Kitchen 1.14.2 and kitchen-ec2 1.3.0.

@davidcpell
Copy link
Contributor Author

@cheeseplus I am out of the country but will be able to look again tomorrow

@cheeseplus
Copy link
Contributor

@stpitner if you could, please make a new issue to track this.

One thing that definitely is goofy (definitely not @davidcpell fault) is that the rebase was against master before a another commit was reverted. I've toyed around locally with trying to reconcile the changes but tests only pass if I unset my ENV vars which seems against the original intention of the PR. As this seems to be currently broken, @stpitner is not the only report, I'm inclined to revert so we can cut a hotfix release as I'll be traveling the bulk of the day tomorrow.

@cheeseplus
Copy link
Contributor

Looks like we others are hitting this and we have an issue -> let's move communication over there #295

kamaradclimber added a commit to criteo-forks/kitchen-ec2 that referenced this pull request Jun 12, 2017
* upstream/master:
  modernize winrm setup and fix for 2008r2
  Updated readme based on issue 300
  Correct the docs for image_id
  Fix 1 last chefstyle warning
  Require Ruby 2.2.2.
  Use chefstyle in Rake
  Remove rack constraint. Removes Ruby 2.1 support
  Move github_changelog_generator back to the gem spec
  Remove Rake constraint
  Switch to Chefstyle
  Test on Ruby 2.4.0
  Remove test-kitchen from the Gemfile as it’s in the spec
  Cut 1.3.2
  Don't try to set tags if there aren't any.
  Actually bumping version
  Release 1.3.1 hotfix
  reinstate default shared creds option
  Release 1.3.0
  In the client, only source creds from the shared file when necessary (test-kitchen#259)
  fixes test-kitchen#250 and provides the option to set ssl_peer_verify to false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants