From b874a8dbb214e0bb988f0bbbbc022f415aa5a4d2 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 7 Apr 2024 03:00:18 +0530 Subject: [PATCH 1/4] Add new AuthError class for raising authentication related issues. When calculating auth header params, start raising an error if any or public or secret key is blank in auth config. Add file client specs to verify the same. Add authentication header specs to for verifying the params validation --- CHANGELOG.md | 6 ++++++ lib/uploadcare.rb | 1 + lib/uploadcare/exception/auth_error.rb | 8 ++++++++ lib/uploadcare/param/authentication_header.rb | 8 ++++++++ spec/uploadcare/client/file_client_spec.rb | 16 ++++++++++++++++ .../param/authentication_header_spec.rb | 10 ++++++++++ 6 files changed, 49 insertions(+) create mode 100644 lib/uploadcare/exception/auth_error.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c8a492..e6f87071 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ ### Changed * File attribute `datetime_stored` is deprecated and will warn on usage from `File` object properties. +## Unreleased + +### Fixed + +* Throw error if current public key or secret key are blank when accessing any + ## 4.4.0 — 2024-03-09 ### Breaking diff --git a/lib/uploadcare.rb b/lib/uploadcare.rb index 3af9565d..e08f4837 100644 --- a/lib/uploadcare.rb +++ b/lib/uploadcare.rb @@ -7,6 +7,7 @@ require 'exception/throttle_error' require 'exception/request_error' require 'exception/retry_error' +require 'exception/auth_error' # Entities require 'entity/entity' diff --git a/lib/uploadcare/exception/auth_error.rb b/lib/uploadcare/exception/auth_error.rb new file mode 100644 index 00000000..29d38572 --- /dev/null +++ b/lib/uploadcare/exception/auth_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Uploadcare + module Exception + # Invalid Auth configuration error + class AuthError < StandardError; end + end +end diff --git a/lib/uploadcare/param/authentication_header.rb b/lib/uploadcare/param/authentication_header.rb index 58589070..fdefc3b1 100644 --- a/lib/uploadcare/param/authentication_header.rb +++ b/lib/uploadcare/param/authentication_header.rb @@ -11,6 +11,7 @@ module Param class AuthenticationHeader # @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare def self.call(options = {}) + validate_keys case Uploadcare.config.auth_type when 'Uploadcare' SecureAuthHeader.call(options) @@ -20,6 +21,13 @@ def self.call(options = {}) raise ArgumentError, "Unknown auth_scheme: '#{Uploadcare.config.auth_type}'" end end + + private + + def self.validate_keys + raise AuthError, "Public Key is blank." if Uploadcare.config.public_key.empty? + raise AuthError, "Secret Key is blank." if Uploadcare.config.secret_key.empty? + end end end end diff --git a/spec/uploadcare/client/file_client_spec.rb b/spec/uploadcare/client/file_client_spec.rb index 573b462e..12ce9703 100644 --- a/spec/uploadcare/client/file_client_spec.rb +++ b/spec/uploadcare/client/file_client_spec.rb @@ -16,6 +16,22 @@ module Client end end + it 'show raise argument error if public_key is blank' do + Uploadcare.config.public_key = '' + VCR.use_cassette('rest_file_info') do + uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6' + expect { subject.info(uuid) }.to raise_error(AuthError, 'Public Key is blank.') + end + end + + it 'show raise argument error if secret_key is blank' do + Uploadcare.config.secret_key = '' + VCR.use_cassette('rest_file_info') do + uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6' + expect { subject.info(uuid) }.to raise_error(AuthError, 'Secret Key is blank.') + end + end + it 'supports extra params like include' do VCR.use_cassette('rest_file_info') do uuid = '640fe4b7-7352-42ca-8d87-0e4387957157' diff --git a/spec/uploadcare/param/authentication_header_spec.rb b/spec/uploadcare/param/authentication_header_spec.rb index 89fce606..b011bf91 100644 --- a/spec/uploadcare/param/authentication_header_spec.rb +++ b/spec/uploadcare/param/authentication_header_spec.rb @@ -18,5 +18,15 @@ module Uploadcare Uploadcare.config.auth_type = 'Uploadcare' expect(subject.call).to eq('SecureAuth called') end + + it 'raise argument error if public_key is blank' do + Uploadcare.config.public_key = '' + expect { subject.call }.to raise_error(AuthError, 'Public Key is blank.') + end + + it 'raise argument error if secret_key is blank' do + Uploadcare.config.secret_key = '' + expect { subject.call }.to raise_error(AuthError, 'Secret Key is blank.') + end end end From a74eee97817a86b5eeaf8c63fc47255fb2f1bcb6 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 7 Apr 2024 03:04:10 +0530 Subject: [PATCH 2/4] Rubocop fixes --- lib/uploadcare/param/authentication_header.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/uploadcare/param/authentication_header.rb b/lib/uploadcare/param/authentication_header.rb index fdefc3b1..e0b2cc36 100644 --- a/lib/uploadcare/param/authentication_header.rb +++ b/lib/uploadcare/param/authentication_header.rb @@ -22,11 +22,9 @@ def self.call(options = {}) end end - private - def self.validate_keys - raise AuthError, "Public Key is blank." if Uploadcare.config.public_key.empty? - raise AuthError, "Secret Key is blank." if Uploadcare.config.secret_key.empty? + raise AuthError, 'Public Key is blank.' if Uploadcare.config.public_key.empty? + raise AuthError, 'Secret Key is blank.' if Uploadcare.config.secret_key.empty? end end end From e462c91115dd037ac05affb7e993875147dfd345 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 7 Apr 2024 19:41:28 +0530 Subject: [PATCH 3/4] Change validation of auth config method name. Extract out empty valid check to is_empty? check. Also check that value is nil or not and not just empty. Add file client spec validation for the same. --- CHANGELOG.md | 2 +- lib/uploadcare/param/authentication_header.rb | 12 ++++++++---- spec/uploadcare/client/file_client_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6f87071..ce587531 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ### Fixed -* Throw error if current public key or secret key are blank when accessing any +* Throw `AuthError` if current public key or secret key config are empty when accessing any of the APIs. ## 4.4.0 — 2024-03-09 diff --git a/lib/uploadcare/param/authentication_header.rb b/lib/uploadcare/param/authentication_header.rb index e0b2cc36..2cb60b1c 100644 --- a/lib/uploadcare/param/authentication_header.rb +++ b/lib/uploadcare/param/authentication_header.rb @@ -11,7 +11,7 @@ module Param class AuthenticationHeader # @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare def self.call(options = {}) - validate_keys + validate_auth_config case Uploadcare.config.auth_type when 'Uploadcare' SecureAuthHeader.call(options) @@ -22,9 +22,13 @@ def self.call(options = {}) end end - def self.validate_keys - raise AuthError, 'Public Key is blank.' if Uploadcare.config.public_key.empty? - raise AuthError, 'Secret Key is blank.' if Uploadcare.config.secret_key.empty? + def self.validate_auth_config + raise AuthError, 'Public Key is blank.' if is_blank?(Uploadcare.config.public_key) + raise AuthError, 'Secret Key is blank.' if is_blank?(Uploadcare.config.secret_key) + end + + def self.is_blank?(value) + value.nil? || value.empty? end end end diff --git a/spec/uploadcare/client/file_client_spec.rb b/spec/uploadcare/client/file_client_spec.rb index 12ce9703..fe6818bd 100644 --- a/spec/uploadcare/client/file_client_spec.rb +++ b/spec/uploadcare/client/file_client_spec.rb @@ -32,6 +32,14 @@ module Client end end + it 'show raise argument error if secret_key is nil' do + Uploadcare.config.secret_key = nil + VCR.use_cassette('rest_file_info') do + uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6' + expect { subject.info(uuid) }.to raise_error(AuthError, 'Secret Key is blank.') + end + end + it 'supports extra params like include' do VCR.use_cassette('rest_file_info') do uuid = '640fe4b7-7352-42ca-8d87-0e4387957157' From 9b6ba0daa53466521cdae3966deb70d7d86d14dd Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 7 Apr 2024 19:47:03 +0530 Subject: [PATCH 4/4] Add disable Naming/PredicateName. We want this specifically to be is_blank? and not blank? to not be confused with the more popular Object#blank? method in Rails --- lib/uploadcare/param/authentication_header.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/uploadcare/param/authentication_header.rb b/lib/uploadcare/param/authentication_header.rb index 2cb60b1c..77968891 100644 --- a/lib/uploadcare/param/authentication_header.rb +++ b/lib/uploadcare/param/authentication_header.rb @@ -27,9 +27,11 @@ def self.validate_auth_config raise AuthError, 'Secret Key is blank.' if is_blank?(Uploadcare.config.secret_key) end + # rubocop:disable Naming/PredicateName def self.is_blank?(value) value.nil? || value.empty? end + # rubocop:enable Naming/PredicateName end end end