Skip to content
Snippets Groups Projects
Verified Commit c9eb906a authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Make rubocop happy and redo tests

parent e361eee8
No related branches found
No related tags found
No related merge requests found
Pipeline #84695 failed
Showing
with 106 additions and 67 deletions
---
include:
- project: ITI/ci-pipelines
file: '/foreman-module.yaml'
---
AllCops:
TargetRubyVersion: 2.3
TargetRailsVersion: 5.1
TargetRubyVersion: 2.7
NewCops: enable
Exclude:
- '*.spec'
- 'Rakefile'
Rails:
Enabled: true
# Rails:
# Enabled: true
# TargetRailsVersion: 6.0
# Don't enforce documentation
Style/Documentation:
......@@ -19,8 +20,8 @@ Metrics/MethodLength:
Metrics/LineLength:
Max: 190
Rails/SkipsModelValidations:
Enabled: false
# Rails/SkipsModelValidations:
# Enabled: false
Lint/BooleanSymbol:
Enabled: false
......@@ -43,16 +44,17 @@ Style/FormatStringToken:
Naming/AccessorMethodName:
Enabled: false
Performance/FixedSize:
Metrics/BlockLength:
Max: 45
Exclude:
- 'test/**/*'
Metrics/BlockLength:
Max: 35
Metrics/ClassLength:
Max: 200
Exclude:
- 'test/**/*'
Metrics/ClassLength:
Metrics/ModuleLength:
Max: 200
Exclude:
- 'test/**/*'
......
# frozen_string_literal: true
module Api
module V2
class PasswordsController < V2::BaseController
......@@ -29,8 +31,8 @@ module Api
def release
pw = @host.password_entry(password_params[:user], create: false)
pw.delete
rescue Passwordstate::NotFoundError => ex
not_found ex
rescue Passwordstate::NotFoundError => e
not_found e
end
private
......@@ -53,8 +55,8 @@ module Api
}.compact
@password = @host.password_entry(password_params[:user], opts)
rescue Passwordstate::NotFoundError => ex
not_found ex
rescue Passwordstate::NotFoundError => e
not_found e
nil
rescue StandardError => e
Foreman::Logging.exception('Failed to acquire password', e)
......
# frozen_string_literal: true
module Foreman::Controller::Parameters::PasswordstateServer
extend ActiveSupport::Concern
......
# frozen_string_literal: true
module ForemanPasswordstate
module FindHostByClientCert
extend ActiveSupport::Concern
module ClassMethods
def authorize_host_by_client_cert(actions, _options = {})
skip_before_action :require_login, :only => actions, :raise => false
skip_before_action :authorize, :only => actions
skip_before_action :verify_authenticity_token, :only => actions
skip_before_action :set_taxonomy, :only => actions, :raise => false
skip_before_action :session_expiry, :update_activity_time, :only => actions
before_action(:only => actions) { require_client_cert_or_login }
skip_before_action :require_login, only: actions, raise: false
skip_before_action :authorize, only: actions
skip_before_action :verify_authenticity_token, only: actions
skip_before_action :set_taxonomy, only: actions, raise: false
skip_before_action :session_expiry, :update_activity_time, only: actions
before_action(only: actions) { require_client_cert_or_login }
attr_reader :detected_host
end
end
......@@ -28,7 +30,7 @@ module ForemanPasswordstate
require_login
unless User.current
render_error 'unauthorized', :status => :unauthorized unless performed? && api_request?
render_error 'unauthorized', status: :unauthorized unless performed? && api_request?
return false
end
authorize
......@@ -53,7 +55,7 @@ module ForemanPasswordstate
end
dn = request.env[Setting[:ssl_client_dn_env]]
return unless dn && dn =~ /CN=([^\s\/,]+)/i
return unless dn && dn =~ %r{CN=([^\s/,]+)}i
hostname = Regexp.last_match(1).downcase
logger.debug "Extracted hostname '#{hostname}' from client certificate."
......
# frozen_string_literal: true
module ForemanPasswordstate
module HostgroupsControllerExtensions
def update
......@@ -20,4 +22,3 @@ module ForemanPasswordstate
end
end
end
# frozen_string_literal: true
module ForemanPasswordstate
module HostsControllerExtensions
def self.prepended(base)
......@@ -30,8 +32,8 @@ module ForemanPasswordstate
def passwordstate_passwords_tab_selected
render partial: 'foreman_passwordstate/passwords_tab_pane_content'
rescue ActionView::Template::Error => exception
process_ajax_error exception, 'fetch managed passwords'
rescue ActionView::Template::Error => e
process_ajax_error e, 'fetch managed passwords'
end
private
......@@ -48,4 +50,3 @@ module ForemanPasswordstate
end
end
end
# frozen_string_literal: true
class PasswordstateServersController < ::ApplicationController
include Foreman::Controller::AutoCompleteSearch
include Foreman::Controller::Parameters::PasswordstateServer
......
# frozen_string_literal: true
module ForemanPasswordstate
module HostCommonExtensions
def crypt_root_pass
......
# frozen_string_literal: true
module ForemanPasswordstate
module HostManagedExtensions
def self.prepended(base)
......@@ -55,7 +57,7 @@ module ForemanPasswordstate
# Skip writing root_pass in the serialized object
options ||= {}
if !options[:only]
unless options[:only]
options[:except] ||= []
options[:except] << :root_pass
end
......@@ -78,7 +80,7 @@ module ForemanPasswordstate
pw ||= list.passwords.create(**params.merge(title: "#{username}@#{fqdn}", description: pw_desc, user_name: username, generate_password: true)) if create
pw
rescue Passwordstate::NotFoundError => e
rescue Passwordstate::NotFoundError
return list.passwords.create(**params.merge(title: "#{username}@#{fqdn}", description: pw_desc, user_name: username, generate_password: true)) if create
raise
......@@ -92,7 +94,6 @@ module ForemanPasswordstate
passwordstate_password_list(_bare: true).passwords.search(description: stable_pw_desc, exclude_password: true).select { |e| e.description.ends_with? stable_pw_desc }
end
def host_pass(username, password_hash: nil, create: true, **params)
return nil unless passwordstate_facet
......@@ -103,9 +104,10 @@ module ForemanPasswordstate
# add a short cache just to not thoroughly hammer the passwordstate server
PasswordstatePasswordsCache.instance.fetch("#{cache_key}/pass-#{username}/#{password_hash}", expires_in: 60.minutes) do
pw = password_entry(username, create: create, **params)
if password_hash == 'None'
case password_hash
when 'None'
pw = pw.password
elsif password_hash == 'Base64' || password_hash == 'Base64-Windows'
when 'Base64', 'Base64-Windows'
pw = PasswordCrypt.passw_crypt(pw.password, password_hash)
else
seed = "#{passwordstate_facet.id}:#{id}@#{passwordstate_server.id}/#{passwordstate_facet.password_list_id}/#{pw.password_id}"
......@@ -126,13 +128,13 @@ module ForemanPasswordstate
host_pass(root_user, password_hash: operatingsystem&.password_hash)
rescue StandardError => e
logger.error "Failed to get root_pass for #{self} - #{e.class}: #{e}"
return Digest::SHA256.hexdigest("#{id}-PlaceholderDueToPasswordstateError")
Digest::SHA256.hexdigest("#{id}-PlaceholderDueToPasswordstateError")
end
def remove_passwordstate_passwords!
return unless passwordstate_facet
logger.info "Removing Passwordstate passwords..."
logger.info 'Removing Passwordstate passwords...'
passwordstate_passwords.each(&:delete)
true
......@@ -146,10 +148,9 @@ module ForemanPasswordstate
super
end
end
end
class Host::Managed::Jail < Safemode::Jail
class Host::Managed::Jail < Safemode::Jail # rubocop:disable Style/ClassAndModuleChildren
allow :host_pass
end
# frozen_string_literal: true
module ForemanPasswordstate
module HostgroupExtensions
def self.prepended(base)
......
# frozen_string_literal: true
module ForemanPasswordstate
module OperatingsystemExtensions
def root_user
......
# frozen_string_literal: true
module ForemanPasswordstate
module PasswordstateCaching
extend ActiveSupport::Concern
included do
after_update :refresh_cache_ignoring_errors, :if => proc { |cr| cr.caching_enabled? }
after_update :refresh_cache_ignoring_errors, if: proc { |cr| cr.caching_enabled? }
end
def caching_enabled?
......
# frozen_string_literal: true
module ForemanPasswordstate
class PasswordstateHostFacet < ApplicationRecord
include Facets::Base
......
# frozen_string_literal: true
module ForemanPasswordstate
class PasswordstateHostgroupFacet < ApplicationRecord
include Facets::HostgroupFacet
......@@ -11,13 +13,13 @@ module ForemanPasswordstate
validates :hostgroup, presence: true, allow_blank: false
validates :passwordstate_server, presence: true, allow_blank: false
class <<self
class << self
def attributes_to_inherit
@attributes_to_inherit ||= attribute_names - %w[id created_at updated_at hostgroup_id]
end
end
inherit_attributes *%w[passwordstate_server_id password_list_id]
inherit_attributes(*%w[passwordstate_server_id password_list_id])
def password_list(**query)
return nil unless password_list_id
......
# frozen_string_literal: true
class PasswordstateServer < ApplicationRecord
include ForemanPasswordstate::PasswordstateCaching
include Taxonomix
......@@ -44,7 +46,7 @@ class PasswordstateServer < ApplicationRecord
delegate :version, :passwords, to: :client
def test_connection(options = {})
def test_connection(**_)
return false unless url
client.valid?
......@@ -81,6 +83,10 @@ class PasswordstateServer < ApplicationRecord
# Only handle a single password list if using API keys
client.password_lists.tap do |list|
list.instance_eval <<-CODE, __FILE__, __LINE__ + 1
# def lazy_load
# load [get('username', { _force: true })]
# end
def lazy_load
load [get(#{user}, { _force: true })]
end
......@@ -94,8 +100,8 @@ class PasswordstateServer < ApplicationRecord
URI.join client.server_url, "plid=#{pwlist.password_list_id}"
end
def get_password_url(pw)
URI.join client.server_url, "pid=#{pw.password_id}"
def get_password_url(password)
URI.join client.server_url, "pid=#{password.password_id}"
end
private
......
# frozen_string_literal: true
Deface::Override.new(:virtual_path => 'hosts/show',
:name => 'add_host_passwords_tab',
:insert_bottom => 'ul.nav-tabs',
:partial => 'foreman_passwordstate/passwords_tab')
Deface::Override.new(virtual_path: 'hosts/show',
name: 'add_host_passwords_tab',
insert_bottom: 'ul.nav-tabs',
partial: 'foreman_passwordstate/passwords_tab')
Deface::Override.new(:virtual_path => 'hosts/show',
:name => 'add_host_passwords_tab_pane',
:insert_bottom => 'div.tab-content',
:partial => 'foreman_passwordstate/passwords_tab_pane')
Deface::Override.new(virtual_path: 'hosts/show',
name: 'add_host_passwords_tab_pane',
insert_bottom: 'div.tab-content',
partial: 'foreman_passwordstate/passwords_tab_pane')
Deface::Override.new(virtual_path: 'hosts/_form',
name: 'add_passwordstate_server_selection',
# frozen_string_literal: true
Deface::Override.new(virtual_path: 'hosts/_form',
name: 'add_passwordstate_server_selection',
insert_bottom: '#primary',
partial: 'foreman_passwordstate/host_server_selection')
partial: 'foreman_passwordstate/host_server_selection')
Deface::Override.new(virtual_path: 'hosts/_operating_system',
name: 'add_passwordstate_password_choice',
name: 'add_passwordstate_password_choice',
insert_after: '#root_password',
partial: 'foreman_passwordstate/host_password_choice')
partial: 'foreman_passwordstate/host_password_choice')
if Hostgroup.instance_methods.include? :build_passwordstate_facet
Deface::Override.new(virtual_path: 'hostgroups/_form',
name: 'hg_add_passwordstate_server_selection',
Deface::Override.new(virtual_path: 'hostgroups/_form',
name: 'hg_add_passwordstate_server_selection',
insert_bottom: '#primary',
partial: 'foreman_passwordstate/host_server_selection')
partial: 'foreman_passwordstate/host_server_selection')
Deface::Override.new(virtual_path: 'hostgroups/_form',
name: 'hg_add_passwordstate_password_choice',
Deface::Override.new(virtual_path: 'hostgroups/_form',
name: 'hg_add_passwordstate_password_choice',
insert_bottom: '#os',
partial: 'foreman_passwordstate/host_password_choice')
partial: 'foreman_passwordstate/host_password_choice')
end
module ForemanPasswordstate
class HostInfoProvider < HostInfo::Provider # inherit the base class
# frozen_string_literal: true
# override this method according to principles specified below
module ForemanPasswordstate
class HostInfoProvider < HostInfo::Provider
def host_info
return {} unless host.passwordstate_facet
......@@ -9,12 +9,13 @@ module ForemanPasswordstate
root_user = host.operatingsystem&.root_user || 'root'
host.host_pass(root_user, password_hash: host.operatingsystem&.password_hash)
params = { 'parameters' => {
params = {
'parameters' => {
'passwordstate' => {
'server' => host.passwordstate_server.name,
'server_url' => host.passwordstate_server.url,
'list' => host.passwordstate_password_list.title,
'list_path' => host.passwordstate_password_list.full_path,
'list_path' => host.passwordstate_password_list.full_path
}
}
}
......
......@@ -4,6 +4,7 @@ module ForemanPasswordstate
# Trimmed copy of Foreman's ComputeResourceCache
class PasswordstateCache
attr_accessor :owner, :cache_duration
delegate :logger, to: ::Rails
def initialize(owner, cache_duration: 60.minutes)
......@@ -77,8 +78,8 @@ module ForemanPasswordstate
def cache_options
{
:expires_in => cache_duration,
:race_condition_ttl => 1.minute,
expires_in: cache_duration,
race_condition_ttl: 1.minute
}
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment