Skip to content

Commit

Permalink
Release v1 of common.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Cloonan committed Sep 22, 2017
1 parent ef9e7f6 commit adbe662
Show file tree
Hide file tree
Showing 29 changed files with 55 additions and 81 deletions.
Empty file modified ads_common/COPYING
100644 → 100755
Empty file.
6 changes: 5 additions & 1 deletion ads_common/ChangeLog
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
0.14.2:
1.0.0:
- Major version bump. The library has been stable and feature complete for
some time, and this is more accurately reflected by this version number.
- Throw explicit warning for disabled SSL peer verification.
- Small performance improvements for large requests.
- No longer include test files in the built gem.
- Removing support for multiple environments.

0.14.1:
- Scrubbing an additional sensitive field present in some AdWords requests.
Expand Down
Empty file modified ads_common/LICENSE
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion ads_common/README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Google Ads Developers Plus page:

# Copyright/License Info

Copyright 2010-2015, Google Inc. All Rights Reserved.
Copyright 2010-2017, Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
3 changes: 1 addition & 2 deletions ads_common/google-ads-common.gemspec
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = '>= 1.3.6'
s.rubyforge_project = 'google-ads-common'
s.require_path = 'lib'
s.files = Dir.glob('{lib,test}/**/*') + %w(COPYING README.md ChangeLog)
s.test_files = Dir.glob('test/test_*.rb')
s.files = Dir.glob('lib/**/*') + %w(COPYING README.md ChangeLog)
s.add_runtime_dependency('google-ads-savon', '~> 1.0', '>=1.0.2')
s.add_runtime_dependency('httpi', '~> 2.3')
s.add_runtime_dependency('httpclient', '~> 2.7')
Expand Down
32 changes: 11 additions & 21 deletions ads_common/lib/ads_common/api.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ def api_config()
def service(name, version = nil)
name = name.to_sym
version = (version.nil?) ? api_config.default_version : version.to_sym
environment = @config.read('service.environment')

# Check if the combination is available.
validate_service_request(environment, version, name)
validate_service_request(version, name)

# Try to re-use the service for this version if it was requested before.
wrapper = if @wrappers.include?(version) && @wrappers[version][name]
Expand Down Expand Up @@ -150,12 +149,11 @@ def save_oauth2_token(token)
private

# Auxiliary method to test parameters correctness for the service request.
def validate_service_request(environment, version, service)
# Check if the current environment supports the requested version.
unless api_config.environment_has_version(environment, version)
def validate_service_request(version, service)
# Check if the current config supports the requested version.
unless api_config.has_version(version)
raise AdsCommon::Errors::Error,
"Environment '%s' does not support version '%s'" %
[environment.to_s, version.to_s]
"Version '%s' not recognized" % version.to_s
end

# Check if the specified version has the requested service.
Expand Down Expand Up @@ -196,18 +194,14 @@ def create_auth_handler()
raise AdsCommon::Errors::Error,
'OAuth authorization method is deprecated, use OAuth2 instead.'
when :OAUTH2
environment = @config.read('service.environment',
api_config.default_environment())
AdsCommon::Auth::OAuth2Handler.new(
@config,
api_config.environment_config(environment, :oauth_scope)
api_config.config(:oauth_scope)
)
when :OAUTH2_SERVICE_ACCOUNT
environment = @config.read('service.environment',
api_config.default_environment())
AdsCommon::Auth::OAuth2ServiceAccountHandler.new(
@config,
api_config.environment_config(environment, :oauth_scope)
api_config.config(:oauth_scope)
)
else
raise AdsCommon::Errors::Error,
Expand All @@ -226,15 +220,13 @@ def create_auth_handler()
# - a simplified wrapper generated for the service
#
def prepare_wrapper(version, service)
environment = config.read('service.environment')
api_config.do_require(version, service)
endpoint = api_config.endpoint(environment, version, service)
endpoint = api_config.endpoint(version, service)
interface_class_name = api_config.interface_name(version, service)

wrapper = class_for_path(interface_class_name).new(@config, endpoint)
auth_handler = get_auth_handler()
header_ns =
api_config.environment_config(environment, :header_ns) + version.to_s
header_ns = api_config.config(:header_ns) + version.to_s
soap_handler = soap_header_handler(auth_handler, version, header_ns,
wrapper.namespace)
wrapper.header_handler = soap_handler
Expand Down Expand Up @@ -270,10 +262,8 @@ def init_config()
provided_adapter = @config.read('connection.adapter')
@config.set('connection.adapter', :httpclient) if provided_adapter.nil?

# Validating most important parameters.
['service.environment', 'authentication.method'].each do |parameter|
symbolize_config_value(parameter)
end
# Make sure Auth param is a symbol.
symbolize_config_value('authentication.method')
end

# Converts value of a config key to uppercase symbol.
Expand Down
46 changes: 10 additions & 36 deletions ads_common/lib/ads_common/api_config.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ def latest_version
service_config.keys.select { |service| service.is_a? Integer }.max
end

# Does the given environment exist and contain the given version?
# Does the current config contain the given version?
#
# Returns:
# Boolean indicating whether the given environment exists and contains the
# given version
# Boolean indicating whether the current config contains the given version
#
def environment_has_version(environment, version)
return !environment_config(environment, version).nil?
def has_version(version)
return !config(version).nil?
end

# Does the given version exist and contain the given service?
Expand Down Expand Up @@ -94,32 +93,22 @@ def api_path
return api_name.to_s.snakecase
end

# Get the default environment.
#
# Returns:
# Default environment
#
def default_environment
raise NotImplementedError, 'default_environment not overriden.'
end

# Get the default filename for the config file.
def default_config_filename
raise NotImplementedError, 'default_config_filename not overriden.'
end

# Get the endpoint for a service on a given environment and API version.
# Get the endpoint for a service on a given API version.
#
# Args:
# - environment: the service environment to be used
# - version: the API version
# - service: the name of the API service
#
# Returns:
# The endpoint URL
#
def endpoint(environment, version, service)
base = get_wsdl_base(environment, version)
def endpoint(version, service)
base = get_wsdl_base(version)
# TODO(dklimkin): Unflatten subdir constants. Cross-API refactor 0.9.0.
if !subdir_config().nil?
base = base.to_s + subdir_config()[[version, service]].to_s
Expand All @@ -142,19 +131,6 @@ def subdir(version, service)
subdir_config()[[version, service]]
end

# Get the authentication server details for an environment. Allows to
# override the auth URL via environmental variable.
#
# Args:
# - environment: the service environment to be used
#
# Returns:
# The full URL for the auth server
#
def auth_server(environment)
return ENV['ADSAPI_AUTH_URL'] || auth_server_config[environment]
end

# Perform the loading of the necessary source files for a version.
#
# Args:
Expand Down Expand Up @@ -208,7 +184,7 @@ def interface_name(version, service)
#
def get_wsdls(version)
res = {}
wsdl_base = get_wsdl_base(default_environment(), version)
wsdl_base = get_wsdl_base(version)
postfix = wsdl_base.start_with?('http') ? '?wsdl' : '.wsdl'
services(version).each do |service|
path = wsdl_base
Expand All @@ -226,15 +202,13 @@ def get_wsdls(version)
# the base URL via environmental variable.
#
# Args:
# - environment: environment to use like :SANDBOX or :PRODUCTION
# - version: the API version
#
# Returns:
# String containing base URL
#
def get_wsdl_base(environment, version)
return ENV['ADSAPI_BASE_URL'] ||
environment_config(environment, version)
def get_wsdl_base(version)
return ENV['ADSAPI_BASE_URL'] || config(version)
end
end
end
Empty file modified ads_common/lib/ads_common/auth/base_handler.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/auth/oauth2_handler.rb
100644 → 100755
Empty file.
Empty file.
Empty file modified ads_common/lib/ads_common/build/savon_abstract_generator.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/build/savon_generator.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/build/savon_registry.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/build/savon_registry_generator.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/build/savon_service_generator.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/config.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/credential_handler.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/env_check.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/errors.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/http.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/parameters_validator.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/results_extractor.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/savon_headers/base_header_handler.rb
100644 → 100755
Empty file.
Empty file modified ads_common/lib/ads_common/savon_headers/oauth_header_handler.rb
100644 → 100755
Empty file.
23 changes: 8 additions & 15 deletions ads_common/lib/ads_common/savon_service.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SavonService

FALLBACK_API_ERROR_EXCEPTION = "ApiException"
MAX_FAULT_LOG_LENGTH = 16000
HEADER_PEEK_LENGTH = 1024
REDACTED_STR = 'REDACTED'

# Creates a new service.
def initialize(config, endpoint, namespace, version)
Expand Down Expand Up @@ -202,34 +202,27 @@ def do_logging(action, request, response)
summary_message += ', Fault message: %s' % format_fault(
response_hash[:envelope][:body][:fault][:faultstring])
logger.warn(summary_message)
logger.info(request_message) if request_message
logger.info(response_message) if response_message
logger.info(request_message) unless request_message.nil?
logger.info(response_message) unless response_message.nil?
else
logger.info(summary_message)
logger.debug(request_message) if request_message
logger.debug(response_message) if response_message
logger.debug(request_message) unless request_message.nil?
logger.debug(response_message) unless response_message.nil?
end
end

# Format headers, redacting sensitive information.
def format_headers(headers)
return headers.map do |k, v|
v = 'REDACTED' if k == 'Authorization'
v = REDACTED_STR if k == 'Authorization'
[k, v].join(': ')
end.join(', ')
end

# Sanitize the request body, redacting sensitive information.
def sanitize_request(body)
body_tail = ""
if body.length > HEADER_PEEK_LENGTH
body_tail = body[HEADER_PEEK_LENGTH, body.length]
body = body[0, HEADER_PEEK_LENGTH]
end
body = body.gsub(/developerToken>[^<]+<\//, 'developerToken>REDACTED</')
body = body.gsub(/httpAuthorizationHeader>[^<]+<\//,
'httpAuthorizationHeader>REDACTED</')
return body + body_tail
return body.gsub(/(developerToken>|httpAuthorizationHeader>)[^<]+(<\/)/,
'\1' + REDACTED_STR + '\2')
end

# Format the fault message by capping length and removing newlines.
Expand Down
Empty file modified ads_common/lib/ads_common/utils.rb
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion ads_common/lib/ads_common/version.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

module AdsCommon
module ApiConfig
CLIENT_LIB_VERSION = '0.14.2'
CLIENT_LIB_VERSION = '1.0.0'
end
end
Empty file modified ads_common/test/test_config.yml
100644 → 100755
Empty file.
22 changes: 18 additions & 4 deletions ads_common/test/test_savon_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,37 @@ def test_sanitize_request()
test1 = "<some_xml><developerToken>ab1cdEF2GH-IJ3KL_mn4OP" +
"</developerToken></some_xml>"
expected1 = "<some_xml><developerToken>REDACTED</developerToken></some_xml>"
assert_equal(expected1, @stub_service.sanitize_request(test1))

test2 = "<xml><element1></element1><element2></element2></xml>"
assert_equal(test2, @stub_service.sanitize_request(test2))

test3 = "<xml><ns1:developerToken>w-x_Y-Z_</ns1:developerToken></xml>"
expected3 = "<xml><ns1:developerToken>REDACTED</ns1:developerToken></xml>"
assert_equal(expected3, @stub_service.sanitize_request(test3))

test4 = "<xml><httpAuthorizationHeader>Authorization: Bearer " +
"1/abcdEFGH1234</httpAuthorizationHeader></xml>"
expected4 = "<xml><httpAuthorizationHeader>REDACTED" +
"</httpAuthorizationHeader></xml>"
assert_equal(expected4, @stub_service.sanitize_request(test4))

test5 = "<some_xml><developerToken>ab1cdEF2GH-IJ3KL_mn4OP" +
"</developerToken>"
test5 += "<operation>something</operation>" * 1024 + "</some_xml>"
expected5 = "<some_xml><developerToken>REDACTED</developerToken>"
expected5 += "<operation>something</operation>" * 1024 + "</some_xml>"
assert_equal(expected1, @stub_service.sanitize_request(test1))
assert_equal(test2, @stub_service.sanitize_request(test2))
assert_equal(expected3, @stub_service.sanitize_request(test3))
assert_equal(expected4, @stub_service.sanitize_request(test4))
assert_equal(expected5, @stub_service.sanitize_request(test5))

src6 = "<OAuthInfo><httpAuthorizationHeader>abc21.deffoobar" +
"</httpAuthorizationHeader></OAuthInfo>\n"
dst6 = "<OAuthInfo><httpAuthorizationHeader>REDACTED" +
"</httpAuthorizationHeader></OAuthInfo>\n"
test6 = "<some_xml><developerToken>ab1cdEF2GH-IJ3KL_mn4OP" +
"</developerToken>" + src6 * 128 + "</some_xml>"
expected6 = "<some_xml><developerToken>REDACTED</developerToken>" +
dst6 * 128 + "</some_xml>"
assert_equal(expected6, @stub_service.sanitize_request(test6))
end

def test_format_fault()
Expand Down

0 comments on commit adbe662

Please sign in to comment.