From bd97c23d9fa398d42033c0e8b4de623de61a362d Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Tue, 17 Mar 2015 16:46:55 -0700 Subject: [PATCH 1/4] Make the scope optional --- lib/googleauth.rb | 10 +++++----- lib/googleauth/credentials_loader.rb | 12 ++++++------ lib/googleauth/service_account.rb | 4 ++-- lib/googleauth/user_refresh.rb | 4 ++-- spec/googleauth/get_application_default_spec.rb | 12 ++++++++++++ spec/googleauth/service_account_spec.rb | 4 ++-- spec/googleauth/user_refresh_spec.rb | 4 ++-- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/googleauth.rb b/lib/googleauth.rb index 65ea0f4..bc767d4 100644 --- a/lib/googleauth.rb +++ b/lib/googleauth.rb @@ -52,9 +52,9 @@ END # override CredentialsLoader#make_creds to use the class determined by # loading the json. - def self.make_creds(scope, json_key_io) + def self.make_creds(json_key_io, scope = nil) json_key, clz = determine_creds_class(json_key_io) - clz.new(scope, StringIO.new(MultiJson.dump(json_key))) + clz.new(StringIO.new(MultiJson.dump(json_key)), scope) end # Reads the input json and determines which creds class to use. @@ -76,11 +76,11 @@ END # at http://goo.gl/IUuyuX. # # If supplied, scope is used to create the credentials instance, when it - # can applied. E.g, on compute engine, the scope is ignored. + # can applied. E.g, on google compute engine, the scope is ignored. # - # @param scope [string|array] the scope(s) to access + # @param scope [string|array|nil] the scope(s) to access # @param options [hash] allows override of the connection being used - def get_application_default(scope, options = {}) + def get_application_default(scope = nil, options = {}) creds = DefaultCredentials.from_env(scope) return creds unless creds.nil? creds = DefaultCredentials.from_well_known_path(scope) diff --git a/lib/googleauth/credentials_loader.rb b/lib/googleauth/credentials_loader.rb index f4ea619..f5ed2b2 100644 --- a/lib/googleauth/credentials_loader.rb +++ b/lib/googleauth/credentials_loader.rb @@ -61,13 +61,13 @@ module Google # Creates an instance from the path specified in an environment # variable. # - # @param scope [string|array] the scope(s) to access - def from_env(scope) + # @param scope [string|array|nil] the scope(s) to access + def from_env(scope = nil) return nil unless ENV.key?(ENV_VAR) path = ENV[ENV_VAR] fail 'file #{path} does not exist' unless File.exist?(path) File.open(path) do |f| - return make_creds(scope, f) + return make_creds(f, scope) end rescue StandardError => e raise "#{NOT_FOUND_ERROR}: #{e}" @@ -75,15 +75,15 @@ module Google # Creates an instance from a well known path. # - # @param scope [string|array] the scope(s) to access - def from_well_known_path(scope) + # @param scope [string|array|nil] the scope(s) to access + def from_well_known_path(scope = nil) home_var, base = windows? ? 'APPDATA' : 'HOME', WELL_KNOWN_PATH root = ENV[home_var].nil? ? '' : ENV[home_var] base = File.join('.config', base) unless windows? path = File.join(root, base) return nil unless File.exist?(path) File.open(path) do |f| - return make_creds(scope, f) + return make_creds(f, scope) end rescue StandardError => e raise "#{WELL_KNOWN_ERROR}: #{e}" diff --git a/lib/googleauth/service_account.rb b/lib/googleauth/service_account.rb index fed67ca..66d7be6 100644 --- a/lib/googleauth/service_account.rb +++ b/lib/googleauth/service_account.rb @@ -57,9 +57,9 @@ module Google # Initializes a ServiceAccountCredentials. # - # @param scope [string|array] the scope(s) to access # @param json_key_io [IO] an IO from which the JSON key can be read - def initialize(scope, json_key_io) + # @param scope [string|array|nil] the scope(s) to access + def initialize(json_key_io, scope = nil) private_key, client_email = self.class.read_json_key(json_key_io) super(token_credential_uri: TOKEN_CRED_URI, audience: TOKEN_CRED_URI, diff --git a/lib/googleauth/user_refresh.rb b/lib/googleauth/user_refresh.rb index 3cfb1db..4256936 100644 --- a/lib/googleauth/user_refresh.rb +++ b/lib/googleauth/user_refresh.rb @@ -61,9 +61,9 @@ module Google # Initializes a UserRefreshCredentials. # - # @param scope [string|array] the scope(s) to access # @param json_key_io [IO] an IO from which the JSON key can be read - def initialize(scope, json_key_io) + # @param scope [string|array|nil] the scope(s) to access + def initialize(json_key_io, scope = nil) user_creds = self.class.read_json_key(json_key_io) super(token_credential_uri: TOKEN_CRED_URI, client_id: user_creds['client_id'], diff --git a/spec/googleauth/get_application_default_spec.rb b/spec/googleauth/get_application_default_spec.rb index 28ec6cd..43556ea 100644 --- a/spec/googleauth/get_application_default_spec.rb +++ b/spec/googleauth/get_application_default_spec.rb @@ -104,6 +104,18 @@ describe '#get_application_default' do end end + it 'succeeds with default file without a scope' do + ENV.delete(@var_name) unless ENV[@var_name].nil? + Dir.mktmpdir do |dir| + key_path = File.join(dir, '.config', + CredentialsLoader::WELL_KNOWN_PATH) + FileUtils.mkdir_p(File.dirname(key_path)) + File.write(key_path, cred_json_text) + ENV['HOME'] = dir + expect(Google::Auth.get_application_default).to_not be_nil + end + end + it 'succeeds without default file or env if on compute engine' do stubs = Faraday::Adapter::Test::Stubs.new do |stub| stub.get('/') do |_env| diff --git a/spec/googleauth/service_account_spec.rb b/spec/googleauth/service_account_spec.rb index 6298116..caaa878 100644 --- a/spec/googleauth/service_account_spec.rb +++ b/spec/googleauth/service_account_spec.rb @@ -47,8 +47,8 @@ describe Google::Auth::ServiceAccountCredentials do before(:example) do @key = OpenSSL::PKey::RSA.new(2048) @client = ServiceAccountCredentials.new( - 'https://www.googleapis.com/auth/userinfo.profile', - StringIO.new(cred_json_text)) + StringIO.new(cred_json_text), + 'https://www.googleapis.com/auth/userinfo.profile') end def make_auth_stubs(opts = {}) diff --git a/spec/googleauth/user_refresh_spec.rb b/spec/googleauth/user_refresh_spec.rb index bbf23d2..eced027 100644 --- a/spec/googleauth/user_refresh_spec.rb +++ b/spec/googleauth/user_refresh_spec.rb @@ -47,8 +47,8 @@ describe Google::Auth::UserRefreshCredentials do before(:example) do @key = OpenSSL::PKey::RSA.new(2048) @client = UserRefreshCredentials.new( - 'https://www.googleapis.com/auth/userinfo.profile', - StringIO.new(cred_json_text)) + StringIO.new(cred_json_text), + 'https://www.googleapis.com/auth/userinfo.profile') end def make_auth_stubs(opts = {}) From cb46aae9992a6f17b155576510de3c36c9759b4e Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Tue, 17 Mar 2015 16:49:36 -0700 Subject: [PATCH 2/4] Bump the version, as optional scopes changes the API. --- lib/googleauth/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/googleauth/version.rb b/lib/googleauth/version.rb index 95f845a..29a9962 100644 --- a/lib/googleauth/version.rb +++ b/lib/googleauth/version.rb @@ -31,6 +31,6 @@ module Google # Module Auth provides classes that provide Google-specific authorization # used to access Google APIs. module Auth - VERSION = '0.2.0' + VERSION = '0.3.0' end end From 395febd0f0333fe8b86492fe7c7b1d30daf3ae8b Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Mon, 23 Mar 2015 15:27:21 -0700 Subject: [PATCH 3/4] Corrects some comments --- lib/googleauth.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/googleauth.rb b/lib/googleauth.rb index bc767d4..c7cfe45 100644 --- a/lib/googleauth.rb +++ b/lib/googleauth.rb @@ -75,8 +75,9 @@ END # Google APIs. Application Default Credentials are described in detail # at http://goo.gl/IUuyuX. # - # If supplied, scope is used to create the credentials instance, when it - # can applied. E.g, on google compute engine, the scope is ignored. + # If supplied, scope is used to create the credentials instance, when it can + # be applied. E.g, on google compute engine and for user credentials the + # scope is ignored. # # @param scope [string|array|nil] the scope(s) to access # @param options [hash] allows override of the connection being used From 86435f1913c249e266514e708ce44d0ec4c3f038 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Mon, 23 Mar 2015 15:49:18 -0700 Subject: [PATCH 4/4] Adds a CHANGELOG.md - also update CONTRIBUTING.md with more detail --- CHANGELOG.md | 8 +++++++ CONTRIBUTING.md | 61 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..57c3ca6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +## 0.3.0 (23/03/2015) + +### Changes + +* makes the scope parameter's optional in all APIs. ([@tbetbetbe][]) +* changes the scope parameter's position in various constructors. ([@tbetbetbe][]) + +[@tbetbetbe]: https://github.com/tbetbetbe diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1e65911..e8354aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ ## Contributor License Agreements -We'd love to accept your sample apps and patches! Before we can take them, we +We'd love to accept your sample apps and patches! Before we can take them, we have to jump a couple of legal hurdles. Please fill out either the individual or corporate Contributor License Agreement @@ -19,14 +19,55 @@ Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. -## Contributing A Patch +## Issue reporting -1. Submit an issue describing your proposed change to the repo in question. -1. The repo owner will respond to your issue promptly. -1. If your proposed change is accepted, and you haven't already done so, sign a - Contributor License Agreement (see details above). -1. Fork the desired repo, develop and test your code changes. -1. Ensure that your code is clear and comprehensible. -1. Ensure that your code has an appropriate set of unit tests which all pass. -1. Submit a pull request. +* Check that the issue has not already been reported. +* Check that the issue has not already been fixed in the latest code + (a.k.a. `master`). +* Be clear, concise and precise in your description of the problem. +* Open an issue with a descriptive title and a summary in grammatically correct, + complete sentences. +* Include any relevant code to the issue summary. +## Pull requests + +* Read [how to properly contribute to open source projects on Github][2]. +* Fork the project. +* Use a topic/feature branch to easily amend a pull request later, if necessary. +* Write [good commit messages][3]. +* Use the same coding conventions as the rest of the project. +* Commit and push until you are happy with your contribution. +* Make sure to add tests for it. This is important so I don't break it + in a future version unintentionally. +* Add an entry to the [Changelog](CHANGELOG.md) accordingly. See [changelog entry format](#changelog-entry-format). +* Please try not to mess with the Rakefile, version, or history. If you want to + have your own version, or is otherwise necessary, that is fine, but please + isolate to its own commit so I can cherry-pick around it. +* Make sure the test suite is passing and the code you wrote doesn't produce + RuboCop offenses. +* [Squash related commits together][5]. +* Open a [pull request][4] that relates to *only* one subject with a clear title + and description in grammatically correct, complete sentences. + +### Changelog entry format + +Here are a few examples: + +``` +* makes the scope parameter's optional in all APIs. (@tbetbetbe[]) +* [#14](https://github.com/google/google-auth-library-ruby/issues/14): ADC Support for JWT Service Tokens. ([@tbetbetbe][]) +``` + +* Mark it up in [Markdown syntax][6]. +* The entry line should start with `* ` (an asterisk and a space). +* If the change has a related GitHub issue (e.g. a bug fix for a reported issue), put a link to the issue as `[#123](https://github.com/google/google-auth-library-ruby/issues/11): `. +* Describe the brief of the change. The sentence should end with a punctuation. +* At the end of the entry, add an implicit link to your GitHub user page as `([@username][])`. +* If this is your first contribution to google-auth-library-ruby project, add a link definition for the implicit link to the bottom of the changelog as `[@username]: https://github.com/username`. + +[1]: https://github.com/google/google-auth-ruby-library/issues +[2]: http://gun.io/blog/how-to-github-fork-branch-and-pull-request +[3]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html +[4]: https://help.github.com/articles/using-pull-requests +[5]: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html +[6]: http://daringfireball.net/projects/markdown/syntax