Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into avoid_creating_conf…
Browse files Browse the repository at this point in the history
…_file_during_spec_tests

Conflicts:
	spec/rhc/common_spec.rb
  • Loading branch information
smarterclayton committed Oct 19, 2012
2 parents c3bc992 + 600e73a commit a630b1a
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ pkg/
Gemfile.lock Gemfile.lock
coverage coverage
*~ *~
.rvmrc
19 changes: 14 additions & 5 deletions bin/rhc-chk
Expand Up @@ -5,6 +5,7 @@ require 'rhc/coverage_helper'
require 'rhc-common' require 'rhc-common'
require 'net/http' require 'net/http'
require 'net/https' require 'net/https'
require 'net/ssh'
require 'rbconfig' require 'rbconfig'
require 'yaml' require 'yaml'
require 'tempfile' require 'tempfile'
Expand Down Expand Up @@ -357,18 +358,26 @@ class Test3_SSH < Test::Unit::TestCase
end end


def agent_key_fingerprints def agent_key_fingerprints
@@agent_keys.nil? ? nil : @@agent_keys.map{|x| x.to_s.split("\n")[1..-2].join('') } @@agent_keys.nil? ? nil : @@agent_keys.map{|x| x.fingerprint }
end end


def libra_public_key def libra_public_key
@@local_ssh_pubkey ||= ( @@local_ssh_pubkey ||= libra_public_key_content.split(' ')[1]
fp = File.open(@libra_kpfile) end
fp.gets.split(' ')[1]
def libra_public_key_content
@@local_ssh_pubkey_content ||= (
fp = File.open(@libra_kpfile)
fp.gets
) )
ensure ensure
fp.close if fp fp.close if fp
end end


def libra_public_key_fingerprint
Net::SSH::KeyFactory.load_data_public_key(libra_public_key_content).fingerprint
end

def test_01_local_files def test_01_local_files
[ [
[@libra_kfile, /[4-7]00/], # Needs to at least be readable by user and nobody else [@libra_kfile, /[4-7]00/], # Needs to at least be readable by user and nobody else
Expand All @@ -381,7 +390,7 @@ class Test3_SSH < Test::Unit::TestCase
def test_02_ssh_agent def test_02_ssh_agent
require_agent_keys require_agent_keys


assert agent_key_names.include?(File.expand_path(@libra_kfile)) ,error_for(:pubkey_not_loaded, ": #{@libra_kpfile}") assert agent_key_fingerprints.include?(libra_public_key_fingerprint) ,error_for(:pubkey_not_loaded, ": #{@libra_kpfile}")
end end


def test_03_remote_ssh_keys def test_03_remote_ssh_keys
Expand Down
17 changes: 16 additions & 1 deletion client.spec
Expand Up @@ -3,7 +3,7 @@


Summary: OpenShift client management tools Summary: OpenShift client management tools
Name: rhc Name: rhc
Version: 0.99.7 Version: 0.99.9
Release: 1%{?dist} Release: 1%{?dist}
Group: Network/Daemons Group: Network/Daemons
License: ASL 2.0 License: ASL 2.0
Expand Down Expand Up @@ -115,6 +115,21 @@ rm -rf $RPM_BUILD_ROOT
%attr(0644,-,-) /etc/bash_completion.d/rhc %attr(0644,-,-) /etc/bash_completion.d/rhc


%changelog %changelog
* Thu Oct 18 2012 Adam Miller <admiller@redhat.com> 0.99.9-1
- Merge pull request #186 from
smarterclayton/bug_821107_allow_unknown_ssh_keys_to_be_uploaded
(openshift+bot@redhat.com)
- Bug 821107 - Allow an unrecognizable SSH key to be uploaded
(ccoleman@redhat.com)

* Thu Oct 18 2012 Adam Miller <admiller@redhat.com> 0.99.8-1
- These superfluous 'Accept' headers were throwing off specs on Linux.
(asari.ruby@gmail.com)
- Tweak debug messages based on feedback. (asari.ruby@gmail.com)
- Addresses US2853. Allows REST Client to negotiate with OpenShift server which
API version to use in order to communicate. (asari.ruby@gmail.com)
- Looks like #179 needs specs to cover the added lines. (asari.ruby@gmail.com)

* Tue Oct 16 2012 Adam Miller <admiller@redhat.com> 0.99.7-1 * Tue Oct 16 2012 Adam Miller <admiller@redhat.com> 0.99.7-1
- Merge pull request #179 from J5/bugfix (openshift+bot@redhat.com) - Merge pull request #179 from J5/bugfix (openshift+bot@redhat.com)
- Merge pull request #180 from J5/bugfix2 (dmcphers@redhat.com) - Merge pull request #180 from J5/bugfix2 (dmcphers@redhat.com)
Expand Down
6 changes: 6 additions & 0 deletions features/support/env.rb
Expand Up @@ -63,6 +63,12 @@ def set_endpoint
raise "Username not found in environment (RHC_USERNAME)" unless $username raise "Username not found in environment (RHC_USERNAME)" unless $username
raise "Password not found in environment (RHC_PASSWORD)" unless $password raise "Password not found in environment (RHC_PASSWORD)" unless $password


$user_register_script_format = "/usr/bin/ss-register-user -l admin -p admin --username %s --userpass %s"
if ENV['REGISTER_USER']
command = $user_register_script_format % [$username,$password]
%x[#{command}]
end

def _log(msg) def _log(msg)
puts msg unless ENV['QUIET'] puts msg unless ENV['QUIET']
end end
Expand Down
8 changes: 4 additions & 4 deletions lib/rhc/commands.rb
Expand Up @@ -53,8 +53,8 @@ def self.deprecated?
command_name = Commander::Runner.instance.command_name_from_args command_name = Commander::Runner.instance.command_name_from_args
command = Commander::Runner.instance.active_command command = Commander::Runner.instance.active_command


new_cmd = deprecated[command_name] new_cmd = deprecated[command_name.to_sym]
if deprecated[command_name] if new_cmd
new_cmd = "rhc #{command.name}" if new_cmd == true new_cmd = "rhc #{command.name}" if new_cmd == true
RHC::Helpers.deprecated_command new_cmd RHC::Helpers.deprecated_command new_cmd
end end
Expand Down Expand Up @@ -88,7 +88,7 @@ def self.to_commander(instance=Commander::Runner.instance)
o[:arg] = Commander::Runner.switch_to_sym(o[:switches].last) o[:arg] = Commander::Runner.switch_to_sym(o[:switches].last)
end end


deprecated[name] = opts[:deprecated] unless opts[:deprecated].nil? deprecated[name.to_sym] = opts[:deprecated] unless opts[:deprecated].nil?


args_metadata = opts[:args] || [] args_metadata = opts[:args] || []
args_metadata.each do |arg_meta| args_metadata.each do |arg_meta|
Expand All @@ -107,7 +107,7 @@ def self.to_commander(instance=Commander::Runner.instance)
# prepend the current resource # prepend the current resource
alias_components = name.split(" ") alias_components = name.split(" ")
alias_components[-1] = a[:action] alias_components[-1] = a[:action]
alias_cmd = alias_components.join(' ') alias_cmd = alias_components.join(' ').to_sym
end end


deprecated[alias_cmd] = true if a[:deprecated] deprecated[alias_cmd] = true if a[:deprecated]
Expand Down
2 changes: 1 addition & 1 deletion lib/rhc/commands/snapshot.rb
Expand Up @@ -103,7 +103,7 @@ def restore(app)
end end
rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
debug e.backtrace debug e.backtrace
raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to save manually by running:\n#{ssh_cmd}" raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
end end


else else
Expand Down
10 changes: 8 additions & 2 deletions lib/rhc/commands/sshkey.rb
Expand Up @@ -35,17 +35,23 @@ def show(name)
end end


summary 'Add SSH key to the user account' summary 'Add SSH key to the user account'
syntax '<name> <SSH Key file>' syntax '<name> <path to SSH key file>'
argument :name, 'Name for this key', [] argument :name, 'Name for this key', []
argument :key, 'SSH public key filepath', [] argument :key, 'SSH public key filepath', []
option ['--confirm'], 'Bypass key validation'
def add(name, key) def add(name, key)
type, content, comment = ssh_key_triple_for(key) type, content, comment = ssh_key_triple_for(key)


# validate the user input before sending it to the server # validate the user input before sending it to the server
begin begin
Net::SSH::KeyFactory.load_data_public_key "#{type} #{content}" Net::SSH::KeyFactory.load_data_public_key "#{type} #{content}"
rescue NotImplementedError, OpenSSL::PKey::PKeyError, Net::SSH::Exception => e rescue NotImplementedError, OpenSSL::PKey::PKeyError, Net::SSH::Exception => e
raise ::RHC::KeyDataInvalidException.new("File '#{key}' contains invalid data") debug e.inspect
if options.confirm
warn 'The key you are uploading is not recognized. You may not be able to authenticate to your application through Git or SSH.'
else
raise ::RHC::KeyDataInvalidException.new("File '#{key}' does not appear to be a recognizable key file (#{e}). You may specify the '--confirm' flag to add the key anyway.")
end
end end


rest_client.add_key(name, content, type) rest_client.add_key(name, content, type)
Expand Down
62 changes: 62 additions & 0 deletions lib/rhc/core_ext.rb
@@ -1,6 +1,7 @@
# From Rails core_ext/object.rb # From Rails core_ext/object.rb
require 'rhc/json' require 'rhc/json'
require 'open-uri' require 'open-uri'
require 'highline'


class Object class Object
def present? def present?
Expand All @@ -24,6 +25,15 @@ def chunk(chunk_size=1024)
end end
end end


class String
# Wrap string by the given length, and join it with the given character.
# The method doesn't distinguish between words, it will only work based on
# the length.
def wrap(wrap_length=80, char="\n")
scan(/.{#{wrap_length}}|.+/).join(char)
end
end

# #
# Allow http => https redirection, see # Allow http => https redirection, see
# http://bugs.ruby-lang.org/issues/859 to 1.8.7 for rough # http://bugs.ruby-lang.org/issues/859 to 1.8.7 for rough
Expand All @@ -38,3 +48,55 @@ def self.redirectable?(uri1, uri2) # :nodoc:
(/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme) (/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme)
end end
end end

# Some versions of highline get in an infinite loop when trying to wrap.
# Fixes BZ 866530.
class HighLine
def wrap(text)
wrapped_text = []
text.each_line do |line|
word = []
i = chars_in_line = 0
chars = line.split(//)
while i < chars.length do
c = chars[i]
color_code = nil
# escape character probably means color code, let's check
if c == "\e"
color_code = line[i..i+6].match(/\e\[\d{1,2}m/)
# it's a color code
if color_code
i += color_code[0].length
# first the existing word buffer then the color code
wrapped_text << word.join.wrap(@wrap_at) << color_code[0]
word.clear
end
end
# not a color code sequence
if !color_code
chars_in_line += 1
# time to wrap the line?
if chars_in_line == @wrap_at
wrapped_text.pop if wrapped_text.last =~ / /
wrapped_text << "\n"
chars_in_line = 0
end
# space, so move the word to wrapped buffer and start a new word
if c =~ / /
wrapped_text << word.join.wrap(@wrap_at) << ' '
word.clear
chars_in_line += 1
# any other character
else
word << c
end
i += 1
end
end
# moves the rest of the word buffer
wrapped_text << word.join.wrap(@wrap_at)
end
return wrapped_text.join
end

end
75 changes: 70 additions & 5 deletions lib/rhc/rest/client.rb
@@ -1,12 +1,22 @@
require 'base64' require 'base64'
require 'rhc/json' require 'rhc/json'
require 'rhc/rest/base' require 'rhc/rest/base'
require 'uri'


module RHC module RHC
module Rest module Rest
class Client < Base class Client < Base
def initialize(end_point, username, password, use_debug=false) attr_reader :server_api_versions, :client_api_versions
# Keep the list of supported API versions here
# The list may not necessarily be sorted; we will select the last
# matching one supported by the server.
# See #api_version_negotiated
CLIENT_API_VERSIONS = [1.0, 1.1, 1.2]

def initialize(end_point, username, password, use_debug=false, preferred_api_versions = CLIENT_API_VERSIONS)
@debug = use_debug @debug = use_debug
@end_point = end_point
@server_api_versions = []
debug "Connecting to #{end_point}" debug "Connecting to #{end_point}"


credentials = nil credentials = nil
Expand All @@ -20,11 +30,37 @@ def initialize(end_point, username, password, use_debug=false)
# :nocov: # :nocov:
@@headers["Authorization"] = "Basic #{credentials}" @@headers["Authorization"] = "Basic #{credentials}"
@@headers["User-Agent"] = RHC::Helpers.user_agent rescue nil @@headers["User-Agent"] = RHC::Helpers.user_agent rescue nil
#first get the API
RestClient.proxy = ENV['http_proxy'] RestClient.proxy = ENV['http_proxy']
request = new_request(:url => end_point, :method => :get, :headers => @@headers)

# if API version negotiation is unsuccessful, execute this
super({:links => request(request)}, use_debug) default_request = new_request(:url => @end_point, :method => :get, :headers => @@headers)

# we'll be popping from preferred_api_versions in the while loop below
# so we need to dup the versions we prefer
@client_api_versions = preferred_api_versions.dup

begin
while !api_version_negotiated && !preferred_api_versions.empty?
api_version = preferred_api_versions.pop
debug "Checking API version #{api_version}"

@@headers["Accept"] = "application/json; version=#{api_version}"
request = new_request(:url => @end_point, :method => :get, :headers => @@headers)
begin
json_response = ::RHC::Json.decode(request.execute)
@server_api_versions = json_response['supported_api_versions']
links = json_response['data']
rescue RestClient::NotAcceptable
# try the next version
debug "Server does not support API version #{api_version}"
end
end
debug "Using API version #{api_version_negotiated}" if api_version_negotiated
warn_about_api_versions
rescue Exception => e
raise ResourceAccessException.new("Failed to access resource: #{e.message}")
end
super({:links => links || request(default_request)}, use_debug)
end end


def add_domain(id) def add_domain(id)
Expand Down Expand Up @@ -118,6 +154,35 @@ def logout
debug "Logout/Close client" debug "Logout/Close client"
end end
alias :close :logout alias :close :logout


### API version related methods
def api_version_match?
! api_version_negotiated.nil?
end

# return the API version that the server and this client agreed on
def api_version_negotiated
return nil unless @server_api_versions
client_api_versions.reverse. # choose the last API version listed
detect { |v| @server_api_versions.include? v }
end

def api_version_current?
current_client_api_version == api_version_negotiated
end

def current_client_api_version
client_api_versions.last
end

def warn_about_api_versions
if !api_version_match?
# API versions did not match
warn "WARNING: API version mismatch. This client supports #{client_api_versions.join(', ')}
but server at #{URI.parse(@end_point).host} supports #{@server_api_versions.join(', ')}."
end
end
end end
end end
end end
2 changes: 1 addition & 1 deletion lib/rhc/version.rb
Expand Up @@ -2,7 +2,7 @@ module RHC
module VERSION #:nocov: module VERSION #:nocov:
MAJOR = 0 MAJOR = 0
MINOR = 99 MINOR = 99
MICRO = 7 MICRO = 9
#PRE = '' #PRE = ''
STRING = [MAJOR,MINOR,MICRO].compact.join('.') STRING = [MAJOR,MINOR,MICRO].compact.join('.')
end end
Expand Down
8 changes: 7 additions & 1 deletion lib/rhc/wizard.rb
Expand Up @@ -34,6 +34,7 @@ def initialize(config, opts=nil)
@libra_server = @libra_server ? @libra_server : "openshift.redhat.com" @libra_server = @libra_server ? @libra_server : "openshift.redhat.com"
end end
@config.config_user opts.rhlogin if opts && opts.rhlogin @config.config_user opts.rhlogin if opts && opts.rhlogin
@debug = config['debug']
end end


# Public: Runs the setup wizard to make sure ~/.openshift and ~/.ssh are correct # Public: Runs the setup wizard to make sure ~/.openshift and ~/.ssh are correct
Expand All @@ -47,6 +48,7 @@ def initialize(config, opts=nil)
def run def run
stages.each do |stage| stages.each do |stage|
# FIXME: cleanup if we fail # FIXME: cleanup if we fail
debug "Running #{stage}"
if (self.send stage).nil? if (self.send stage).nil?
return nil return nil
end end
Expand Down Expand Up @@ -94,7 +96,7 @@ def login_stage


# instantiate a REST client that stages can use # instantiate a REST client that stages can use
end_point = "https://#{@libra_server}/broker/rest/api" end_point = "https://#{@libra_server}/broker/rest/api"
@rest_client = RHC::Rest::Client.new(end_point, @username, @password) @rest_client = RHC::Rest::Client.new(end_point, @username, @password, @debug)


# confirm that the REST client can connect # confirm that the REST client can connect
return false unless @rest_client.user return false unless @rest_client.user
Expand Down Expand Up @@ -430,6 +432,10 @@ def has_git?
rescue rescue
false false
end end

def debug?
@debug
end
end end


class RerunWizard < Wizard class RerunWizard < Wizard
Expand Down
2 changes: 1 addition & 1 deletion rel-eng/packages/rhc
@@ -1 +1 @@
0.99.7-1 / 0.99.9-1 /

0 comments on commit a630b1a

Please sign in to comment.