How can I extract all applications and keys from 3scale platform?

Solution Unverified - Updated -

Environment

  • Red Hat 3scale API Management Platform SaaS, 2.0 On-premises

Issue

  • I want to extract the list of all applications with the user keys, but the Applications Export feature doesn't include the user keys in the CSV file.

Resolution

The Application List (all services) endpoint of the Account Management API can be used to extract the application data along with the user key (API key).
Below is an example of a script written in Ruby that makes calls to the Application List endpoint (paginated) and saves the application data including the user keys and App IDs to a CSV file.

Note: Red Hat does not provide support for the code, it is provided just for example purposes.

Prerequisites:

  • You will need to create an Access Token with the scope "Account Management API" and "Read" permission.
  • Ruby and HTTParty gem need to be installed.

Usage:

ruby export-keys.rb [options]
        -s, --source SOURCE              Account. Format: https://{ACCESS_TOKEN}@{DOMAIN}-admin.3scale.net
        -f, --fileout FILENAME           Name of the CSV file that will be created
        -h, --help                       Prints this help

Example:

ruby export-keys.rb --source https://123456abc@mycompany-admin.3scale.net --fileout mykeys.csv

Script (save as export-keys.rb):

#!/usr/bin/env ruby

require 'HTTParty'
require 'optparse'
require 'csv'

class ApplicationsExport

  def initialize(base_url, access_token, outfile)
    @applications = []
    @base_url = base_url
    @access_token = access_token
    @outfile = outfile || 'keys.csv'
  end

  def run
    get_all_applications
    write_output_csv
  end

  def get_applications(page, per_page)
    options = {
      query: {
        access_token: @access_token,
        page: page,
        per_page: per_page
      }
    }
    response = HTTParty.get("#{@base_url}/admin/api/applications.xml", options)
    raise "Error while fetching applications: #{response}" unless response.code == 200
    response['applications'].nil? ? false : response['applications']['application']
  end

  def get_all_applications
    i = 1
    while apps = get_applications(i, 100)
      break unless apps
      @applications << apps
      puts "Fetching page #{i}"
      i += 1
    end
  end

  def write_output_csv
    CSV.open(@outfile, 'w') do |csv|
      csv << [ 'user_key', 'app_id', 'service_id', 'name', 'created_at', 'plan name', 'plan id' ]
      @applications.flatten(1).each do |app|
        row = [ app['user_key'], app['application_id'], app['service_id'], app['name'], app['created_at'], app['plan']['name'], app['plan']['id'] ]
        csv << row
      end
    end
    puts 'All applications exported successfully'
  end

end

options = {}

parser = OptionParser.new do |parser|

  parser.banner = 'ruby export-keys.rb [options]'

  parser.on('-s', '--source SOURCE', 'Account. Format: https://{ACCESS_TOKEN}@{DOMAIN}-admin.3scale.net') do |url|
    begin
      options[:access_token] = url[/\w*@/][0..-2]
      options[:base_url] = url.sub /\w*@/, ''
    rescue
      puts 'Invalid source'
      exit
    end
  end

  parser.on('-f', '--fileout FILENAME', 'Name of the CSV file that will be created') do |file|
    options[:outfile] = file
  end

  parser.on('-h', '--help', 'Prints this help') do
    puts parser
    puts
    exit
  end
end

parser.parse!

required_opts = [:access_token, :base_url, :outfile].all? { |e| options.include?(e) }
if not required_opts
  puts 'Error: missing arguments'
  puts
  puts parser
  exit
end

client = ApplicationsExport.new(
  options[:base_url],
  options[:access_token],
  options[:outfile])

client.run

Root Cause

The Applications Export feature does not contain user keys, as it considered insecure to send them over email.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments