class DexSync::UpdateKubeConfigService

Public Class Methods

new(config) click to toggle source
# File lib/dex_sync.rb, line 44
def initialize(config)
  @config = config
end

Public Instance Methods

perform() click to toggle source
# File lib/dex_sync.rb, line 48
def perform
  @config.clusters.each do |cluster|
    login_params = { cross_client: cluster, offline_access: 'yes' }

    puts("Logging into dex app...#{cluster}")
    response = connection.post('login', login_params)
    puts('Wait for it...')

    doc = Nokogiri::HTML(response.body)

    refresh_token = doc.css('form > input[name=refresh_token]').first['value']
    id_token      = doc.css('form > input[name=id_token]').first['value']

    @config.namespaces.each do |namespace|
      puts("Dowloading config for #{cluster}:#{namespace}")

      download_params = {
        refresh_token: refresh_token,
        id_token: id_token,
        namespace: namespace,
        internal: true
      }

      response = connection.post('download', download_params)

      downloaded_config = YAML.safe_load(response.body)

      expanded_path = File.expand_path(@config.download_path + "/#{cluster}-#{namespace}-internal")

      File.open(expanded_path, 'w') { |f| f.write(downloaded_config.to_yaml) }
    end
  end
end

Private Instance Methods

connection() click to toggle source
# File lib/dex_sync.rb, line 84
def connection
  @connection ||= Faraday.new(url: @config.dex) do |f|
    f.use :cookie_jar
    f.use FaradayMiddleware::FollowRedirects, limit: 10
    f.request  :url_encoded
    f.adapter  Faraday.default_adapter
    f.headers['Cookie'] = HTTP::Cookie.cookie_value(cookie_jar.cookies)
  end
end