diff --git a/config/application.rb b/config/application.rb index 212a8dd..c2959bc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,55 +1,55 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' # Production doesn't use bundler # you've limited to :test, :development, or :production. if ENV['RAILS_ENV'] != 'production' Bundler.require(*Rails.groups) else # Dependencies to load before starting rails in production require 'kaminari' require 'jquery-rails' require 'state_machine' require 'beaneater' require 'oauth2' require 'warden' require 'net/scp' end -module Baas +module Archiving def self.settings opts = nil @settings ||= {} return @settings if opts.nil? @settings.merge! opts @settings end class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. # Store/Read localtime from the database config.time_zone = 'Athens' config.active_record.default_timezone = :local # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de config.autoload_paths << Rails.root.join('lib') # config.x = {} end # def self.settings # Application.config.x # end def self.bean @bean ||= Bean::Client.new( YAML.load_file(Rails.root.join('config', 'beanstalk.yml'))[Rails.env].symbolize_keys[:host] ) end end diff --git a/config/initializers/00_settings.rb b/config/initializers/00_settings.rb index 86537d3..04e38d4 100644 --- a/config/initializers/00_settings.rb +++ b/config/initializers/00_settings.rb @@ -1,6 +1,6 @@ -Baas.settings director_name: YAML.load_file(Rails.root.join('config', 'bacula.yml'))[Rails.env]. +Archiving.settings director_name: YAML.load_file(Rails.root.join('config', 'bacula.yml'))[Rails.env]. symbolize_keys[:director] Archiving.settings vima_oauth_enabled: true Archiving.settings institutional_authentication_enabled: true Archiving.settings okeanos_authentication_enabled: false diff --git a/lib/configuration/host.rb b/lib/configuration/host.rb index a558254..0ac8b0b 100644 --- a/lib/configuration/host.rb +++ b/lib/configuration/host.rb @@ -1,81 +1,81 @@ module Configuration # Helper module to add configuration getters for Host module Host # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # @return [Array] containing each element's configuration line by line def baculize_config templates = job_templates.includes(:fileset, :schedule) result = [self] + templates.map {|x| [x, x.fileset, x.schedule] }.flatten.compact.uniq result.map(&:to_bacula_config_array) end # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # It hides the password. # # @return [Array] containing each element's configuration line by line def baculize_config_no_pass baculize_config.join("\n").gsub(/Password = ".*"$/, 'Password = "*************"') end # Constructs an array where each element is a line for the Client's bacula config # # @return [Array] def to_bacula_config_array [ "Client {", " Name = #{name}", " Address = #{fqdn}", " FDPort = #{port}", " Catalog = #{client_settings[:catalog]}", " Password = \"#{password}\"", " File Retention = #{file_retention} #{file_retention_period_type}", " Job Retention = #{job_retention} #{job_retention_period_type}", " AutoPrune = #{auto_prune_human}", "}" ] end # Fetches the Director resource for the file-deamon configuration # file def bacula_fd_director_config [ 'Director {', - " Name = \"#{Baas.settings[:director_name]}\"", + " Name = \"#{Archiving.settings[:director_name]}\"", " Password = \"#{password}\"", '}' ].join("\n") end # Fetches the FileDeamon resource for the file-deamon configuration def bacula_fd_filedeamon_config [ 'FileDeamon {', " Name = #{name}", " FDport = #{port}", ' WorkingDirectory = /var/lib/bacula', ' Pid Directory = /var/run/bacula', ' Maximum Concurrent Jobs = 10', ' FDAddress = 0.0.0.0', '}' ].join("\n") end end end diff --git a/lib/peter/strategies/vima.rb b/lib/peter/strategies/vima.rb index d1efa4d..68494eb 100644 --- a/lib/peter/strategies/vima.rb +++ b/lib/peter/strategies/vima.rb @@ -1,109 +1,109 @@ ## -*- encoding : utf-8 -*- require 'oauth2' Warden::Strategies.add(:vima) do Key = Rails.application.secrets.oauth2_vima_client_id Secret = Rails.application.secrets.oauth2_vima_secret def valid? params['vima'] || params['error'] || params['code'] end def client OAuth2::Client.new( Key, Secret, site: 'https://vima.grnet.gr', token_url: "/o/token", authorize_url: "/o/authorize", :ssl => {:ca_path => "/etc/ssl/certs"} ) end def redirect_uri uri = URI.parse(request.url) uri.scheme = 'https' unless Rails.env.development? uri.path = '/vima' uri.query = nil uri.to_s end def redirect_to_vima redirect! client.auth_code.authorize_url(:redirect_uri => redirect_uri, scope: 'read') end def authenticate! - if !Baas::settings[:vima_oauth_enabled] + if !Archiving::settings[:vima_oauth_enabled] return fail!("ViMa is temporarily disabled") end if params['error'] Rails.logger.warn("WARDEN: ERROR #{params['error']}") return fail!("ViMa log in failed: #{params['error']}") end return redirect_to_vima if params['vima'] access_token = client.auth_code.get_token( params['code'], { :redirect_uri => redirect_uri }, { :mode => :query, :param_name => "access_token", :header_format => "" }) user_data = access_token.get( 'https://vima.grnet.gr/user/details', { mode: :query, param_name: 'access_token' } ).parsed.deep_symbolize_keys vms = access_token.get( 'https://vima.grnet.gr/instances/list?tag=vima:service:archiving', { mode: :query, param_name: 'access_token' } ).parsed.deep_symbolize_keys if [user_data[:username], user_data[:email], user_data[:id]].any?(&:blank?) return fail!("ViMa login failed: no user data") end ###### TBR # temporary, for user migration user = User.find_or_initialize_by(username: user_data[:username], email: user_data[:email]) user.identifier = "vima:#{user_data[:id]}" ###### # actual implementation #user = User.find_or_initialize_by(identifier: user_data[:identifier]) user.login_at = Time.now if user.new_record? user.enabled = true # TBR user.identifier = "vima:#{user_data[:id]}" user.vima! else user.save! end if vms[:response][:errors] != false Rails.logger.warn("ViMa: errors on instances/list response for user #{vms[:user][:username]}") end if !user.enabled? return fail!('Service not available') end assign_vms(user, vms[:response][:instances]) success!(user) end def assign_vms(user, vms) Rails.logger.warn("ViMa: user: #{user.username}") Rails.logger.warn("ViMa: vms: #{vms}") Rails.logger.warn("ViMa: session vms: #{session[:vms]}") session[:vms] = vms.first(50) Host.where(fqdn: vms).each do |host| host.users << user unless host.users.include?(user) end end end diff --git a/spec/lib/configuration/host_spec.rb b/spec/lib/configuration/host_spec.rb index f4cdb29..04d440f 100644 --- a/spec/lib/configuration/host_spec.rb +++ b/spec/lib/configuration/host_spec.rb @@ -1,121 +1,121 @@ require 'spec_helper' describe Configuration::Host do describe '#to_bacula_config_array' do let(:host) { FactoryGirl.create(:host) } it "is a valid client directive" do expect(host.to_bacula_config_array).to include('Client {') expect(host.to_bacula_config_array).to include('}') end it "contains Address directive" do expect(host.to_bacula_config_array).to include(" Address = #{host.fqdn}") end it "contains FDPort directive" do expect(host.to_bacula_config_array).to include(" FDPort = #{host.port}") end it "contains Catalog directive" do expect(host.to_bacula_config_array). to include(" Catalog = #{ConfigurationSetting.current_client_settings[:catalog]}") end it "contains Password directive" do expect(host.to_bacula_config_array).to include(" Password = \"#{host.password}\"") end it "contains File Retention directive" do expect(host.to_bacula_config_array). to include(" File Retention = #{host.file_retention} days") end it "contains Job Retention directive" do expect(host.to_bacula_config_array). to include(" Job Retention = #{host.job_retention} days") end it "contains AutoPrune directive" do expect(host.to_bacula_config_array).to include(" AutoPrune = yes") end end describe '#baculize_config' do let!(:host) { FactoryGirl.create(:host) } let!(:fileset) { FactoryGirl.create(:fileset, host: host) } let!(:other_fileset) { FactoryGirl.create(:fileset, host: host) } let!(:schedule) { FactoryGirl.create(:schedule) } let!(:other_schedule) { FactoryGirl.create(:schedule) } let!(:enabled_job) do FactoryGirl.create(:job_template, host: host, schedule: schedule, fileset: fileset, enabled: true) end let!(:disabled_job) do FactoryGirl.create(:job_template, host: host, schedule: other_schedule, fileset: other_fileset, enabled: false) end subject { host.baculize_config } it 'includes the client\'s config' do expect(subject).to include(host.to_bacula_config_array) end it 'includes the all the job template\'s configs' do expect(subject).to include(enabled_job.to_bacula_config_array) expect(subject).to include(disabled_job.to_bacula_config_array) end it 'includes all the used schedules\'s configs' do expect(subject).to include(schedule.to_bacula_config_array) expect(subject).to include(other_schedule.to_bacula_config_array) end it 'includes all the used filesets\'s configs' do expect(subject).to include(fileset.to_bacula_config_array) expect(subject).to include(other_fileset.to_bacula_config_array) end end describe '#bacula_fd_director_config' do let!(:host) { FactoryGirl.build(:host) } subject { host.bacula_fd_director_config } it 'opens and closes a Director part' do expect(subject).to match(/^Director {$/) expect(subject).to match(/^}$/) end it 'includes the client\'s Name' do - expect(subject).to match(" Name = \"#{Baas.settings[:director_name]}\"") + expect(subject).to match(" Name = \"#{Archiving.settings[:director_name]}\"") end it 'includes the client\'s Password' do expect(subject).to match(" Password = \"#{host.password}\"") end end describe '#bacula_fd_filedeamon_config' do let!(:host) { FactoryGirl.build(:host) } subject { host.bacula_fd_filedeamon_config } it 'opens and closes a FileDeamon part' do expect(subject).to match(/^FileDeamon {$/) expect(subject).to match(/^}$/) end it 'includes the client\'s Port' do expect(subject).to match("FDport = #{host.port}") end it 'includes the client\'s Name' do expect(subject).to match("Name = #{host.name}") end end end