From 38e13ecc670b3d6abafaa6622766bf6ece3ea2e8 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 14 Sep 2012 15:18:22 +0200 Subject: [PATCH 1/8] appflow: fixes to install.sh --- flow/install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flow/install.sh b/flow/install.sh index eed3439b1f..68cd7ad692 100755 --- a/flow/install.sh +++ b/flow/install.sh @@ -59,7 +59,7 @@ do_file() { if [ "$LINK" = "yes" ]; then ln -s $SRC_DIR/$1 $2 else - cp $SRC_DIR/$1 $2 + cp -R $SRC_DIR/$1 $2 fi fi } @@ -85,7 +85,7 @@ copy_files "client/bin/*" "$BIN_LOCATION" copy_files "bin/*" "$BIN_LOCATION" # dirs containing files -copy_files "controllers models public views lcm" "$LIB_LOCATION" +copy_files "controllers models lcm" "$LIB_LOCATION" # files copy_files "lib/* models.rb config.ru Gemfile Gemfile.lock \ From 8d9218f3abac593827766869941b336fda5517b0 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 14 Sep 2012 15:18:45 +0200 Subject: [PATCH 2/8] add apptools and flow to script generation --- gen_package.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gen_package.sh b/gen_package.sh index 04a7d7016b..b81f8b17b7 100755 --- a/gen_package.sh +++ b/gen_package.sh @@ -32,7 +32,7 @@ SCRIPTS_DIR=$PWD NAME="${PACKAGE_NAME}_${VERSION}.${PACKAGE_TYPE}" rm $NAME -DIRS="env market" +DIRS="apptools env flow market" export DESTDIR=$PWD/tmp rm -rf $DESTDIR From 9c9ce45924f5ca2fa72494641c59128f4621e6e1 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 14 Sep 2012 15:54:05 +0200 Subject: [PATCH 3/8] appflow: bug #1455 Validate templates before allocating --- flow/controllers/server.rb | 7 +- flow/lib/validator.rb | 399 +++++++++++++++++++++++++++++++++ flow/models.rb | 2 + flow/models/ServiceTemplate.rb | 54 ++++- 4 files changed, 460 insertions(+), 2 deletions(-) create mode 100644 flow/lib/validator.rb diff --git a/flow/controllers/server.rb b/flow/controllers/server.rb index 3e75705efe..fb2aaa13be 100644 --- a/flow/controllers/server.rb +++ b/flow/controllers/server.rb @@ -272,7 +272,12 @@ post '/service_template' do OpenNebula::ServiceTemplate.build_xml, @client) - rc = s_template.allocate(request.body.read) + begin + rc = s_template.allocate(request.body.read) + rescue Validator::ParseException, JSON::ParserError + error 400, $!.message + end + if OpenNebula.is_error?(rc) error CloudServer::HTTP_ERROR_CODE[rc.errno], rc.message end diff --git a/flow/lib/validator.rb b/flow/lib/validator.rb new file mode 100644 index 0000000000..88cd48a916 --- /dev/null +++ b/flow/lib/validator.rb @@ -0,0 +1,399 @@ +require 'uri' + +class Hash + # Returns a new hash containing the contents of other_hash and the + # contents of self. If the value for entries with duplicate keys + # is a Hash, it will be merged recursively, otherwise it will be that + # of other_hash. + # + # @param [Hash] other_hash + # + # @return [Hash] Containing the merged values + # + # @example Merging two hashes + # h1 = {:a => 3, {:b => 3, :c => 7}} + # h2 = {:a => 22, c => 4, {:b => 5}} + # + # h1.deep_merge(h2) #=> {:a => 22, c => 4, {:b => 5, :c => 7}} + def deep_merge(other_hash) + target = dup + + other_hash.each do |hash_key, hash_value| + if hash_value.is_a?(Hash) and self[hash_key].is_a?(Hash) + target[hash_key] = self[hash_key].deep_merge(hash_value) + elsif hash_value.is_a?(Array) and self[hash_key].is_a?(Array) + hash_value.each_with_index { |elem, i| + if self[hash_key][i].is_a?(Hash) and elem.is_a?(Hash) + target[hash_key][i] = self[hash_key][i].deep_merge(elem) + else + target[hash_key] = hash_value + end + } + else + target[hash_key] = hash_value + end + end + + target + end +end + +module Validator + +class ParseException < StandardError; end +class SchemaException < StandardError; end + +class Validator + + # @param [Hash] opts the options to validate a body + # @option opts [Boolean] :default_values Set default values if the schema + # specifies it (if true) + # @option opts [Booblean] :delete_extra_properties If the body contains properties + # not specified in the schema delete them from the body (if true) + # or raise an exception (if false) + def initialize(opts={}) + @opts = { + :default_values => true, + :delete_extra_properties => false + }.merge(opts) + end + + # Recursively validate and modify a JSON body based on a schema. + # + # @see http://tools.ietf.org/html/draft-zyp-json-schema-03 + # + # @param [Hash, Array, String, nil] body JSON represented as Ruby objects + # @param [Hash] schema that will be used to validate + # @param [String] key of the body that will be validated in this step + # + # @return [Hash, Array, String, nil] The modified body + # + # @raise [SchemaException] If the schema is not correctly defined + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate a User + # schema = { + # :type => :object, + # :properties => { + # 'username' => { + # :type => :string + # } + # } + # } + # + # hash = { + # 'username' => 'pepe' + # } + # + # Validator.validate!(hash, schema) + # #=> {'username' => 'pepe'} + # + # @note The parameter body will be modified + # @note Schema options supported + # :extends + # :type => [:object, :array, :string, :null] + # + def validate!(body, schema, key="") + if schema[:extends] + base_schema = schema.delete(:extends) + schema = base_schema.deep_merge(schema) + end + + case schema[:type] + when :object then validate_object(body, schema, key) + when :array then validate_array(body, schema, key) + when :string then validate_string(body, schema, key) + when :integer then validate_integer(body, schema, key) + when :null then validate_null(body, schema, key) + else raise SchemaException, "type #{schema[:type]} is not a valid type" + end + end + + private + + # Validate an object type + # + # @param [Hash] body to be validated + # @param [Hash] schema_object of the objectto validate the body + # @param [String] key of the body that will be validated in this step + # + # @return [Hash] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate with default values + # schema_body = { + # :type => :object, + # :properties => { + # 'username' => { + # :type => :string, + # :default => 'def' + # } + # } + # + # body = {} + # + # Validator.validate_object(body, schema_body) + # #=> {'username' => 'def'} + # + # @note The parameter body will be modified + # @note Schema options supported + # :properties + # :required + # :default + # + def validate_object(body, schema_object, key) + unless body.is_a?(Hash) + raise ParseException, "KEY: #{key} must be a Hash; SCHEMA:"\ + "#{schema_object}" + end + + new_body = body.dup + + schema_object[:properties].each{ |schema_key, schema_value| + body_value = new_body.delete(schema_key) + + if body_value + body[schema_key] = validate!(body_value, schema_value, + schema_key) + else + if schema_value[:required] + raise ParseException, "KEY: '#{schema_key}' is required;"\ + " SCHEMA: #{schema_value}" + end + + if @opts[:default_values] && schema_value[:default] + body[schema_key] = schema_value[:default] + end + end + } + + # raise error if body.keys is not empty + unless new_body.keys.empty? + if @opts[:delete_extra_properties] + new_body.keys.each{ |key| + body.delete(key) + } + else + raise ParseException, "KEY: #{new_body.keys.join(', ')} not"\ + " allowed; SCHEMA: #{schema_object}" + end + end + + body + end + + # Validate an array type + # + # @param [Array] body to be validated + # @param [Hash] schema_array of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [Hash] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :array, + # :items => { + # :type => :string + # } + # } + # + # body = ['pepe', 'luis', 'juan'] + # + # Validator.validate_array(body, schema) + # #=> 'username' => ['pepe', 'luis', 'juan'] + # + # @note The parameter body will be modified + # @note Schema options supported + # :items + # + def validate_array(body, schema_array, schema_key) + if body.instance_of?(Array) + body.collect { |body_item| + validate!(body_item, schema_array[:items], schema_key) + } + else + raise ParseException, "KEY: '#{schema_key}' must be an Array;"\ + " SCHEMA: #{schema_array}" + end + end + + # Validate an integer type + # + # @param [Array] body to be validated + # @param [Hash] schema_array of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [Hash] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :integer + # } + # + # body = 5 + # + # Validator.validate_integer(body, schema) + # #=> 5 + # + # + def validate_integer(body, schema_array, schema_key) + if body.is_a?(Integer) + body + else + raise ParseException, "KEY: '#{schema_key}' must be an Integer;"\ + " SCHEMA: #{schema_array}" + end + end + + # Validate an null type + # + # @param [nil] body to be validated + # @param [Hash] schema_null of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [nil] + # + # @raise [ParseException] if the body is not nil + # + # @example Validate array + # schema = { + # :type => :null + # } + # + # body = nil + # + # Validator.validate_null(body, schema) + # #=> nil + # + # + def validate_null(body, schema_null, schema_key) + if body != nil + raise ParseException, "KEY: '#{schema_key}' is not allowed;"\ + " SCHEMA: #{schema_null}" + end + end + + # Validate an string type + # + # @param [String] body to be validated + # @param [Hash] schema_string of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [String] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :string + # } + # + # body = "pepe" + # + # Validator.validate_string(body, schema) + # #=> "pepe" + # + # @note The parameter body will be modified + # @note Schema options supported + # :format + # :enum + # + def validate_string(body, schema_string, schema_key) + if body.instance_of?(String) + if schema_string[:format] + check_format(body, schema_string, schema_key) + elsif schema_string[:enum] + check_enum(body, schema_string, schema_key) + else + body + end + else + raise ParseException, "KEY: '#{schema_key}' must be a String;"\ + " SCHEMA: #{schema_string}" + end + end + + # Validate an string format + # + # @param [String] body_value to be validated + # @param [Hash] schema_string of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [String] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :string, + # :format => :url + # } + # + # body = "http://localhost:4567" + # + # Validator.check_format(body, schema) + # #=> "http://localhost:4567" + # + # @note The parameter body will be modified + # @note Schema options supported + # :url + # + def check_format(body_value, schema_string, schema_key) + case schema_string[:format] + when :uri + begin + require 'uri' + uri = URI.parse(body_value) + rescue + raise ParseException, "KEY: '#{schema_key}' must be a valid URL;"\ + " SCHEMA: #{schema_string} #{$!.message}" + end + + body_value + end + + body_value + end + + # Validate an string enum + # + # @param [String] body_value to be validated + # @param [Hash] schema_string of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [String] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :string, + # :enum => ['juan', 'luis'] + # } + # + # body = "juan" + # + # Validator.check_enum(body, schema) + # #=> "juan" + # + # @note The parameter body will be modified + # @note Schema options supported + # :enum + # + def check_enum(body_value, schema_string, schema_key) + if schema_string[:enum].include?(body_value) + body_value + else + raise ParseException, "KEY: '#{schema_key}' must be one of"\ + " #{schema_string[:enum].join(', ')}; SCHEMA: #{schema_string}" + end + end +end + +end \ No newline at end of file diff --git a/flow/models.rb b/flow/models.rb index f4391c1b91..a0b3fd40ed 100644 --- a/flow/models.rb +++ b/flow/models.rb @@ -5,6 +5,8 @@ include OpenNebula require 'OpenNebula/DocumentJSON' require 'OpenNebula/DocumentPoolJSON' +require 'validator' + require 'models/Role' require 'models/Service' require 'models/ServicePool' diff --git a/flow/models/ServiceTemplate.rb b/flow/models/ServiceTemplate.rb index 6864ebc80d..f0a1aee577 100644 --- a/flow/models/ServiceTemplate.rb +++ b/flow/models/ServiceTemplate.rb @@ -15,14 +15,66 @@ #--------------------------------------------------------------------------- # module OpenNebula + class ServiceTemplate < DocumentJSON + ROLE_SCHEMA = { + :type => :object, + :properties => { + 'name' => { + :type => :string, + :required => true + }, + 'cardinality' => { + :type => :integer, + :default => 1 + }, + 'vm_template' => { + :type => :integer, + :required => true + }, + 'parents' => { + :type => :array, + :items => { + :type => :string + } + } + } + } + + SCHEMA = { + :type => :object, + :properties => { + 'name' => { + :type => :string + }, + 'deployment' => { + :type => :string, + :enum => %w{none straight}, + :default => 'none' + }, + 'roles' => { + :type => :array, + :items => ROLE_SCHEMA, + :required => true + } + } + } + + DOCUMENT_TYPE = 101 def allocate(template_json) template = JSON.parse(template_json) - super(template_json, template['name']) + validator = Validator::Validator.new( + :default_values => true, + :delete_extra_properties => false + ) + + validator.validate!(template, SCHEMA) + + super(template.to_json, template['name']) end # Retrieves the template From 3a840018556546a56daa7afff292ece029aac3cf Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 14 Sep 2012 16:05:59 +0200 Subject: [PATCH 4/8] appmarket: Change user_table display length --- market/views/user_index.haml | 1 + 1 file changed, 1 insertion(+) diff --git a/market/views/user_index.haml b/market/views/user_index.haml index 169e4e9458..8b9044fa65 100644 --- a/market/views/user_index.haml +++ b/market/views/user_index.haml @@ -26,6 +26,7 @@ "sAjaxSource": "/user", "sAjaxDataProp": "users", "bAutoWidth": false, + "iDisplayLength": 50, "aoColumns": [ { "mDataProp": "username" }, { "mDataProp": "organization" }, From a24897ce225c22b6f58792e3ddcae312d1284827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 17 Sep 2012 18:56:04 +0200 Subject: [PATCH 5/8] appflow: Bug #1444: CLI can use names as range arguments --- flow/client/bin/appflow | 14 +++++---- flow/client/bin/appflow-template | 12 +++++--- flow/client/lib/appflow_client.rb | 48 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/flow/client/bin/appflow b/flow/client/bin/appflow index 8879f8377c..ada9e29764 100755 --- a/flow/client/bin/appflow +++ b/flow/client/bin/appflow @@ -236,6 +236,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do Service.rname_to_id(arg, "SERVICE") end + set :format, :serviceid_list, Service.list_to_id_desc("SERVICE") do |arg| + Service.list_to_id(arg, "SERVICE") + end + # # List # @@ -326,7 +330,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Delete a given service EOT - command :delete, delete_desc, :range do + command :delete, delete_desc, [:range, :serviceid_list] do client = Service::Client.new( options[:username], options[:password], @@ -346,7 +350,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Shutdown a service EOT - command :shutdown, shutdown_desc, :range do + command :shutdown, shutdown_desc, [:range, :serviceid_list] do client = Service::Client.new( options[:username], options[:password], @@ -364,7 +368,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service group EOT - command :chgrp, chgrp_desc, :range, :groupid do + command :chgrp, chgrp_desc, [:range, :serviceid_list], :groupid do client = Service::Client.new( options[:username], options[:password], @@ -385,7 +389,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service owner and group EOT - command :chown, chown_desc, :range, :userid, [:groupid, nil] do + command :chown, chown_desc, [:range, :serviceid_list], :userid, [:groupid, nil] do client = Service::Client.new( options[:username], options[:password], @@ -407,7 +411,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service permissions EOT - command :chmod, chmod_desc, :range, :octet do + command :chmod, chmod_desc, [:range, :serviceid_list], :octet do client = Service::Client.new( options[:username], options[:password], diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index b3e98387fe..669146a8b0 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -150,6 +150,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do Service.rname_to_id(arg, "SERVICE TEMPLATE") end + set :format, :templateid_list, Service.list_to_id_desc("SERVICE TEMPLATE") do |arg| + Service.list_to_id(arg, "SERVICE TEMPLATE") + end + # # List # @@ -261,7 +265,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Delete a given Service Template EOT - command :delete, delete_desc, :range do + command :delete, delete_desc, [:range, :templateid_list] do client = Service::Client.new( options[:username], options[:password], @@ -310,7 +314,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service template group EOT - command :chgrp, chgrp_desc, :range, :groupid do + command :chgrp, chgrp_desc, [:range, :templateid_list], :groupid do client = Service::Client.new( options[:username], options[:password], @@ -331,7 +335,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service template owner and group EOT - command :chown, chown_desc, :range, :userid, [:groupid, nil] do + command :chown, chown_desc, [:range, :templateid_list], :userid, [:groupid, nil] do client = Service::Client.new( options[:username], options[:password], @@ -353,7 +357,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do Changes the service template permissions EOT - command :chmod, chmod_desc, :range, :octet do + command :chmod, chmod_desc, [:range, :templateid_list], :octet do client = Service::Client.new( options[:username], options[:password], diff --git a/flow/client/lib/appflow_client.rb b/flow/client/lib/appflow_client.rb index c727ecf0b0..946af978ca 100644 --- a/flow/client/lib/appflow_client.rb +++ b/flow/client/lib/appflow_client.rb @@ -148,6 +148,10 @@ module Service end def self.name_to_id(name, pool, ename) + if pool['DOCUMENT_POOL']['DOCUMENT'].nil? + return -1, "#{ename} named #{name} not found." + end + objects = pool['DOCUMENT_POOL']['DOCUMENT'].select {|object| object['NAME'] == name } if objects.length>0 @@ -163,6 +167,50 @@ module Service return 0, result end + def self.list_to_id(names, poolname) + + client = Service::Client.new(nil, nil, nil) +# client = Service::Client.new( +# options[:username], +# options[:password], +# options[:server], +# USER_AGENT) + + resource_path = case poolname + when "SERVICE" then "/service" + when "SERVICE TEMPLATE" then "/service_template" + end + + response = client.get(resource_path) + + if CloudClient::is_error?(response) + return -1, "OpenNebula #{poolname} name not found," << + " use the ID instead" + end + + pool = JSON.parse(response.body) + + result = names.split(',').collect { |name| + if name.match(/^[0123456789]+$/) + name.to_i + else + rc = name_to_id(name, pool, poolname) + + if rc.first == -1 + return rc[0], rc[1] + end + + rc[1] + end + } + + return 0, result + end + + def self.list_to_id_desc(poolname) + "Comma-separated list of OpenNebula #{poolname} names or ids" + end + # Perform an action on several resources # @param [Array] ids resources ids # @param [Block] block action to be performed From d1e45ec70b1cca030205a51b80c5e70feb058b43 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 17 Sep 2012 19:06:36 +0200 Subject: [PATCH 6/8] appflow: Add session management and sunstone login --- flow/client/bin/appflow | 64 ++++++++++++------------ flow/client/bin/appflow-template | 72 +++++++++++++-------------- flow/client/lib/appflow_client.rb | 51 +++++++++++++------ flow/controllers/server.rb | 81 ++++++++++++++++++++++++++----- flow/sunstone/routes/appflow.rb | 68 ++++++++++++++++++++++++++ 5 files changed, 240 insertions(+), 96 deletions(-) diff --git a/flow/client/bin/appflow b/flow/client/bin/appflow index ada9e29764..ab99b419f2 100755 --- a/flow/client/bin/appflow +++ b/flow/client/bin/appflow @@ -250,10 +250,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :list, list_desc, :options => Service::JSON_FORMAT do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) list_services(client, args, options) end @@ -268,10 +268,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :show, show_desc, :serviceid, :options => Service::JSON_FORMAT do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) show_service(client, args, options) end @@ -288,10 +288,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :top, top_desc, [:serviceid, nil], :options => [Service::JSON_FORMAT, Service::TOP, CLIHelper::DELAY] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) delay=options[:delay] ? options[:delay] : 3 @@ -332,10 +332,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :delete, delete_desc, [:range, :serviceid_list] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| client.delete("#{RESOURCE_PATH}/#{service_id}") @@ -352,10 +352,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :shutdown, shutdown_desc, [:range, :serviceid_list] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| json_action = Service.build_json_action('shutdown') @@ -370,10 +370,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chgrp, chgrp_desc, [:range, :serviceid_list], :groupid do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new @@ -391,10 +391,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chown, chown_desc, [:range, :serviceid_list], :userid, [:groupid, nil] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new @@ -413,10 +413,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chmod, chmod_desc, [:range, :serviceid_list], :octet do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index 669146a8b0..957981e738 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -164,10 +164,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :list, list_desc, :options => Service::JSON_FORMAT do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) list_service_templates(client, args, options) end @@ -183,10 +183,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :top, top_desc, :options => [Service::JSON_FORMAT, Service::TOP, CLIHelper::DELAY] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) delay=options[:delay] ? options[:delay] : 3 @@ -219,10 +219,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :create, create_desc, :file, :options => Service::JSON_FORMAT do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) response = client.post(RESOURCE_PATH, File.read(args[0])) @@ -249,10 +249,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :show, show_desc, :template_id do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) show_service_template(client, args, options) end @@ -267,10 +267,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :delete, delete_desc, [:range, :templateid_list] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |template_id| client.delete("#{RESOURCE_PATH}/#{template_id}") @@ -288,10 +288,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :instantiate, instantiate_desc, :template_id, :options => [Service::JSON_FORMAT, Service::TOP] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) json_str = Service.build_json_action('instantiate') @@ -316,10 +316,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chgrp, chgrp_desc, [:range, :templateid_list], :groupid do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new @@ -337,10 +337,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chown, chown_desc, [:range, :templateid_list], :userid, [:groupid, nil] do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new @@ -359,10 +359,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :chmod, chmod_desc, [:range, :templateid_list], :octet do client = Service::Client.new( - options[:username], - options[:password], - options[:server], - USER_AGENT) + :username => options[:username], + :password => options[:password], + :url => options[:server], + :user_agent => USER_AGENT) Service.perform_actions(args[0]) { |service_id| params = Hash.new diff --git a/flow/client/lib/appflow_client.rb b/flow/client/lib/appflow_client.rb index 946af978ca..a8a03c1c0e 100644 --- a/flow/client/lib/appflow_client.rb +++ b/flow/client/lib/appflow_client.rb @@ -231,26 +231,29 @@ module Service end class Client - def initialize(username, password, url, user_agent="Ruby") - if username.nil? && password.nil? - if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"]) - one_auth = File.read(ENV["ONE_AUTH"]) - elsif File.file?(ENV["HOME"]+"/.one/one_auth") - one_auth = File.read(ENV["HOME"]+"/.one/one_auth") - end + def initialize(opts={}) + #if username.nil? && password.nil? + # if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"]) + # one_auth = File.read(ENV["ONE_AUTH"]) + # elsif File.file?(ENV["HOME"]+"/.one/one_auth") + # one_auth = File.read(ENV["HOME"]+"/.one/one_auth") + # end +# + # one_auth.rstrip! +# + # username, password = one_auth.split(':') + #end - one_auth.rstrip! + @username = opts[:username] + @password = opts[:password] - username, password = one_auth.split(':') - end + opts[:url] ||= 'http://localhost:2474' + @uri = URI.parse(opts[:url]) - @username = username - @password = password + @cookie = opts[:cookie] - url ||= 'http://localhost:2474' - @uri = URI.parse(url) - - @user_agent = "OpenNebula #{CloudClient::VERSION} (#{user_agent})" + @user_agent = "OpenNebula #{CloudClient::VERSION} " << + "(#{opts[:user_agent]||"Ruby"})" end def get(path) @@ -272,6 +275,18 @@ module Service do_request(req) end + def login + req = Net::HTTP::Post.new('/login') + + do_request(req) + end + + def logout + req = Net::HTTP::Post.new('/logout') + + do_request(req) + end + private def do_request(req) @@ -281,6 +296,10 @@ module Service req['User-Agent'] = @user_agent + if @cookie + req['Cookie'] = @cookie + end + res = CloudClient::http_start(@uri, @timeout) do |http| http.request(req) end diff --git a/flow/controllers/server.rb b/flow/controllers/server.rb index fb2aaa13be..b2ca276742 100644 --- a/flow/controllers/server.rb +++ b/flow/controllers/server.rb @@ -53,6 +53,8 @@ set :config, conf include CloudLogger enable_logging APPFLOW_LOG, conf[:debug_level].to_i +use Rack::Session::Pool, :key => 'appflow' + Log.logger = settings.logger Log.level = conf[:debug_level].to_i @@ -76,20 +78,65 @@ set :cloud_auth, cloud_auth ############################################################################## # Helpers ############################################################################## - -before do - begin - username = settings.cloud_auth.auth(request.env, params) - rescue Exception => e - Log.error LOG_COMP, e.message - error 500, "Server error" +helpers do + def authorized? + session[:ip] && session[:ip]==request.ip ? true : false end - if username.nil? #unable to authenticate - Log.error LOG_COMP, "User not authorized" - error 401, "User not authorized" - else - @client = settings.cloud_auth.client(username) + def build_session + username = authenticate(request.env, params) + if username + session[:ip] = request.ip + session[:user] = username + + return [204, ""] + end + end + + def destroy_session + session.clear + return [204, ""] + end + + def authenticate(env, params) + begin + username = settings.cloud_auth.auth(request.env, params) + rescue Exception => e + Log.error LOG_COMP, e.message + error 500, "Server error" + end + + if username.nil? #unable to authenticate + Log.error LOG_COMP, "User not authorized" + error 401, "User not authorized" + else + return username + end + end +end + +before do + unless request.path=='/login' + if authorized? + @client = settings.cloud_auth.client(session[:user]) + else + username = authenticate(request.env, params) + if username + @client = settings.cloud_auth.client(username) + end + end + end +end + +after do + unless request.path=='/login' || request.path=='/' + unless session[:remember] == "true" + if params[:timeout] == "true" + env['rack.session.options'][:defer] = true + else + env['rack.session.options'][:expire_after] = 60*10 + end + end end end @@ -104,6 +151,16 @@ t = Thread.new { } t.abort_on_exception = true +############################################################################## +# Login +############################################################################## +post '/login' do + build_session +end + +post '/logout' do + destroy_session +end ############################################################################## # Service diff --git a/flow/sunstone/routes/appflow.rb b/flow/sunstone/routes/appflow.rb index e69de29bb2..c6ca46fd32 100644 --- a/flow/sunstone/routes/appflow.rb +++ b/flow/sunstone/routes/appflow.rb @@ -0,0 +1,68 @@ +$: << RUBY_LIB_LOCATION+"/apptools/flow" + +require 'appflow_client' + +############################################################################## +# Login +############################################################################## +post '/login' do + code, body = build_session + if code == 204 + auth = Rack::Auth::Basic::Request.new(request.env) + + if auth.provided? && auth.basic? + username, password = auth.credentials + + client = Service::Client.new( + :username => username, + :password => password, + :url => settings.config[:appflow_server], + :user_agent => "Sunstone") + + response = client.login + if CloudClient::is_error?(response) + settings.logger "[AppFlow]" + response.to_s + else + session[:appflow_cookie] = response['set-cookie'].split('; ')[0] + end + else + settings.logger "[AppFlow]" + "Unauthorized AppFlow login attempt" + end + + [204, ""] + else + [code, body] + end +end + +post '/logout' do + client = Service::Client.new( + :url => settings.config[:appflow_server], + :user_agent => "Sunstone", + :cookie => session[:appflow_cookie]) + + client.logout + # TBD Check http code + + destroy_session +end + +############################################################################## +# Service +############################################################################## + +get '/service' do + client = Service::Client.new( + :url => settings.config[:appflow_server], + :user_agent => "Sunstone", + :cookie => session[:appflow_cookie]) + + response = client.get('/service') + + if CloudClient::is_error?(response) + settings.logger "[AppFlow]" + response.to_s + [500, response.to_s] + else + + body response.to_s +end \ No newline at end of file From 07b691c2f3b15f88c12d33703754d0b551d859f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 18 Sep 2012 14:51:38 +0200 Subject: [PATCH 7/8] appflow: Update auth comment in appflow.yaml --- flow/config/appflow.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flow/config/appflow.yaml b/flow/config/appflow.yaml index d52f763c12..a2e84ec9eb 100644 --- a/flow/config/appflow.yaml +++ b/flow/config/appflow.yaml @@ -19,8 +19,9 @@ ############################################################# # Authentication driver for incomming requests -# - ec2, default Acess key and Secret key scheme -# - x509, for x509 certificates based authentication +# - sunstone, for OpenNebula's user-password scheme +# - opennebula, the authentication will be done by the opennebula core using +# the driver defined for the user :auth: sunstone # Authentication driver to communicate with OpenNebula core From ddf026b67ac6897e17b5ed88ef1dcae4ae8376f9 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 18 Sep 2012 17:30:22 +0200 Subject: [PATCH 8/8] apptools: change apptools description --- .../public/js/user-plugins/apptools.js | 28 ++++--------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/apptools/sunstone/public/js/user-plugins/apptools.js b/apptools/sunstone/public/js/user-plugins/apptools.js index d4d0ef77dd..a1e8b04991 100644 --- a/apptools/sunstone/public/js/user-plugins/apptools.js +++ b/apptools/sunstone/public/js/user-plugins/apptools.js @@ -24,16 +24,7 @@ var apptools_dashboard_tmpl = '\

' + tr("AppTools") + '

\
\ \ -

apptools logoAppTools is a group of tools for users and administrators of OpenNebula clouds to simplify and optimize application management. These tools enable the building, distribution and execution of multi-tier applications.

\ -\ -
    \ -
  • AppEnv performs the automatic installation and configuration of the software stack that constitute an application environment
  • \ -
  • AppFlow enables the automatic execution of multi-tiered applications composed of interconnected applications
  • \ -
  • AppPlace builds a centralized catalog of virtual appliances to easily share and distribute applications across OpenNebula instances
  • \ -
\ -\ -

AppTools fills the gap between the application and the virtual infrastructure layer. While OpenNebula manages the execution of the virtual infrastructure, being fully application agnostic, AppTools manages the application being executed within the virtual infrastructure. AppTools leverages the OpenNebula contextualization framework and external APIs.

\ -\ +

apptools logoAppTools is a group of tools for users and administrators of OpenNebula to simplify and optimize cloud application management. With AppTools you can configure the software stack in the applications, manage multi-tiered applications, and build your own private market to distribute applications across several OpenNebula instances.

\
\ \ \ @@ -48,12 +39,7 @@ var apptools_dashboard_tmpl = '\

' + tr("AppEnv - A tool to manage environments") + '

\
\ \ -

appenv logoInstalling services can sometimes be tedious. Getting the VM ins good shape, installing the packages needed, deploying the application and configuring it. On case of disaster or scaling the service you will need to perform again these tasks. To automate all this burden we now have Configuration Management software like Chef, Puppet or CFEngine.

\ -\ -

With this software we can script the installation and configuration of applications and services. You can also describe the services and their configuration that will be installed on a node so we can automate its installation. We will also be sure that each installation will be equal to other using the same configuration.

\ -\ -

The tool oneenv will make use of Chef-solo. It lets you register chef configurations and instantiate them using predefined templates and images.

\ -\ +

appenv logoAppEnv performs the automatic installation and configuration of the software stack that constitutes an application environment. With AppEnv you can easily define software configurations for your VMs and share them with other users.

\
\ \ \ @@ -63,9 +49,7 @@ var apptools_dashboard_tmpl = '\
\

' + tr("AppFlow - Service deployment") + '

\
\ -

appflow logoApp flow desc

\ -

bla

\ -

blabla

\ +

appflow logoAppFlow enables the automatic execution of multi-tiered applications. With AppFlow you can define multi-tier applications composed of interconnected VMs and manage them as a single entity.

\
\
\ \ @@ -75,9 +59,7 @@ var apptools_dashboard_tmpl = '\
\

' + tr("AppMarket - Image repository") + '

\
\ -

appflow logoApp flow desc

\ -

bla

\ -

blabla

\ +

appflow logoAppMarket builds a centralized catalog of cloud applications. With AppMarket you can build a private repository to share and distribute virtual appliances across OpenNebula instances.

\
\
\ \ @@ -92,4 +74,4 @@ var apptools_tab = { } -Sunstone.addMainTab('apptools_tab',apptools_tab); \ No newline at end of file +Sunstone.addMainTab('apptools_tab',apptools_tab);