Skip to content

Instantly share code, notes, and snippets.

@yohanesws
Last active September 18, 2018 17:17
Show Gist options
  • Select an option

  • Save yohanesws/b161172f44c01706d065df4472c3e33f to your computer and use it in GitHub Desktop.

Select an option

Save yohanesws/b161172f44c01706d065df4472c3e33f to your computer and use it in GitHub Desktop.
3scale ToolBox Hack

The Best Way

download certificates

if chain

echo | openssl s_client -showcerts -servername $host -connect $host:443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > yourcert.pem

if single ca

echo | openssl s_client -showcerts -servername $host -connect $host:443 2>/dev/null | openssl x509 > yourcert.pem

testing by include cert:

$ SSL_CERT_FILE=yourcert.pem ruby -r net/http -e "puts Net::HTTP.get_response(URI(%Q{https://$host})).code"
$ SSL_CERT_DIR=yourcertdir/ ruby -r net/http -e "puts Net::HTTP.get_response(URI(%Q{https://$host})).code"

call the 3scaletoolbox with the cert:

$ SSL_CERT_FILE=yourcert.pem 3scale import csv --destination=https://${token}@${host} --file=import.csv

The Bad Way

Change file at /usr/local/lib/ruby/gems/2.5.0/gems/3scale-api-0.1.5/lib/3scale/api/http_client.rb

This hack for 3scale-ruby-api

Yohaness-MacBook-Pro:temp yohanesws$ 3scale import csv --destination=https://<ADMIN_ACCESS_TOKEN>@3scale-admin.5e1f.apps.rhpds.openshift.opentlc.com --file=import.csv
/usr/local/lib/ruby/gems/2.5.0/gems/3scale_toolbox-0.4.0/exe/3scale:6: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
/usr/local/lib/ruby/gems/2.5.0/gems/3scale_toolbox-0.4.0/exe/3scale-import:10: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
Service Movies  has been created.
Metric Movies (Biography) has been created.
Mapping rule movies_biography has been created.
Method Movies (Drama) has been created.
Mapping rule movies_drama has been created.
Metric Movies (adventure) has been created.
Mapping rule movies_adventure has been created.
Service Music has been created.
Metric Music (Biography) has been created.
Mapping rule music_biography has been created.
Metric Music (Post punk) has been created.
Mapping rule music_postpunk has been created.
Metric Music (Sludge) has been created.
Mapping rule music_sludge has been created.
2 services in CSV file
2 services have been created
5 metrics have been created
1 methods have beeen created
6 mapping rules have been created

NOTES

SSL tools for ruby and openssl can be found: https://github.com/mislav/ssl-tools

require 'json'
require 'uri'
require 'net/http'
module ThreeScale
module API
class HttpClient
attr_reader :endpoint, :admin_domain, :provider_key, :headers, :format
def initialize(endpoint:, provider_key:, format: :json)
@endpoint = URI(endpoint).freeze
@admin_domain = @endpoint.host.freeze
@provider_key = provider_key.freeze
@http = Net::HTTP.new(admin_domain, @endpoint.port)
#yws add
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@http.use_ssl = @endpoint.is_a?(URI::HTTPS)
@headers = {
'Accept' => "application/#{format}",
'Content-Type' => "application/#{format}",
'Authorization' => 'Basic ' + [":#{@provider_key}"].pack('m').delete("\r\n")
}
if debug?
@http.set_debug_output($stdout)
@headers['Accept-Encoding'] = 'identity'
end
@headers.freeze
@format = format
end
def get(path, params: nil)
parse @http.get(format_path_n_query(path, params), headers)
end
def patch(path, body:, params: nil)
parse @http.patch(format_path_n_query(path, params), serialize(body), headers)
end
def post(path, body:, params: nil)
parse @http.post(format_path_n_query(path, params), serialize(body), headers)
end
def put(path, body: nil, params: nil)
parse @http.put(format_path_n_query(path, params), serialize(body), headers)
end
def delete(path, params: nil)
parse @http.delete(format_path_n_query(path, params), headers)
end
# @param [::Net::HTTPResponse] response
def parse(response)
case response
when Net::HTTPUnprocessableEntity, Net::HTTPSuccess then parser.decode(response.body)
when Net::HTTPForbidden then forbidden!(response)
else "Can't handle #{response.inspect}"
end
end
class ForbiddenError < StandardError; end
def forbidden!(response)
raise ForbiddenError, response
end
def serialize(body)
case body
when nil then nil
when String then body
else parser.encode(body)
end
end
def parser
case format
when :json then JSONParser
else "unknown format #{format}"
end
end
protected
def debug?
ENV.fetch('3SCALE_DEBUG', '0') == '1'
end
# Helper to create a string representing a path plus a query string
def format_path_n_query(path, params)
path = "#{path}.#{format}"
path << "?#{URI.encode_www_form(params)}" unless params.nil?
path
end
module JSONParser
module_function
def decode(string)
case string
when nil, ' '.freeze, ''.freeze then nil
else ::JSON.parse(string)
end
end
def encode(query)
::JSON.generate(query)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment