module ForemanRhCloud::Plugin

Public Class Methods

register() click to toggle source
# File lib/foreman_rh_cloud/plugin.rb, line 3
def self.register
  # This module is auto-loaded, but plugin settings cannot be redefined
  # Ensure that we don't try to re-register the plugin on code reload
  return if Foreman::Plugin.find(:foreman_rh_cloud)

  Foreman::Plugin.register :foreman_rh_cloud do
    requires_foreman '>= 3.17'
    register_gettext

    apipie_documented_controllers ["#{ForemanRhCloud::Engine.root}/app/controllers/api/v2/**/*.rb"]

    settings do
      category(:rh_cloud, N_('Insights')) do
        setting('allow_auto_inventory_upload', type: :boolean, description: N_('Enable automatic upload of your host inventory to the Red Hat cloud. Ignored when using local Insights.'), default: true, full_name: N_('Automatic inventory upload'))
        setting('allow_auto_insights_sync', type: :boolean, description: N_('Enable automatic synchronization of Insights recommendations from the Red Hat cloud. Ignored when using local Insights.'), default: true, full_name: N_('Synchronize recommendations Automatically'))
        setting('allow_auto_insights_mismatch_delete', type: :boolean, description: N_('Enable automatic deletion of mismatched host records from the Red Hat cloud. Ignored when using local Insights.'), default: false, full_name: N_('Automatic mismatch deletion'))
        setting('obfuscate_inventory_hostnames', type: :boolean, description: N_('Obfuscate host names sent to the Red Hat cloud. (If insights_minimal_data_collection is set to true, this setting is ignored because host names are not included in the report.) Ignored when using local Insights.'), default: false, full_name: N_('Obfuscate host names'))
        setting('obfuscate_inventory_ips', type: :boolean, description: N_('Obfuscate ipv4 addresses sent to the Red Hat cloud.  (If insights_minimal_data_collection is set to true, this setting is ignored because host IPv4 addresses are not included in the report.) Ignored when using local Insights.'), default: false, full_name: N_('Obfuscate host ipv4 addresses.'))
        setting('exclude_installed_packages', type: :boolean, description: N_('Exclude installed packages from being uploaded to the Red Hat cloud. (If insights_minimal_data_collection is set to true, this setting is ignored and installed packages are always excluded.) Ignored when using local Insights.'), default: false, full_name: N_("Exclude installed packages"))
        setting('include_parameter_tags', type: :boolean, description: N_('Should import include parameter tags from Foreman? Ignored when using local Insights.'), default: false, full_name: N_('Include parameters in insights-client reports'))
        setting('rhc_instance_id', type: :string, description: N_('RHC daemon id. Ignored when using local Insights.'), default: nil, full_name: N_('ID of the RHC(Yggdrasil) daemon'))
        setting('insights_minimal_data_collection', type: :boolean, default: false, full_name: N_('Minimal data collection'), description: N_('Only include the minimum required data in inventory reports for uploading to Red Hat cloud. When this is true, installed packages are excluded from the report regardless of the exclude_installed_packages setting, and host names and IPv4 addresses are excluded from the report regardless of obfuscation settings. Ignored when using local Insights.'))
      end
    end

    # Add permissions
    security_block :foreman_rh_cloud do
      permission(
        :generate_foreman_rh_cloud,
        'foreman_inventory_upload/reports': [:generate],
        'foreman_inventory_upload/tasks': [:create],
        'api/v2/rh_cloud/inventory': [:get_hosts, :remove_hosts, :sync_inventory_status, :download_file, :generate_report, :enable_cloud_connector],
        'foreman_inventory_upload/uploads': [:enable_cloud_connector],
        'foreman_inventory_upload/uploads_settings': [:set_advanced_setting],
        'foreman_inventory_upload/missing_hosts': [:remove_hosts],
        'insights_cloud/settings': [:update],
        'insights_cloud/tasks': [:create]
      )
      permission(
        :view_foreman_rh_cloud,
        'foreman_inventory_upload/accounts': [:index],
        'foreman_inventory_upload/reports': [:last],
        'foreman_inventory_upload/uploads': [:auto_upload, :show_auto_upload, :download_file, :last],
        'foreman_inventory_upload/tasks': [:show],
        'foreman_inventory_upload/api/tasks': [:current, :history],
        'foreman_inventory_upload/cloud_status': [:index],
        'foreman_inventory_upload/uploads_settings': [:index],
        'foreman_inventory_upload/missing_hosts': [:index],
        'api/v2/rh_cloud/advisor_engine_config': [:show],
        'foreman_rh_cloud/foreman_rh_cloud': [:inventory_upload, :recommendations],
        'react': [:index]
      )
      permission(
        :view_insights_hits,
        {
          '/foreman_rh_cloud/insights_cloud': [:index], # for bookmarks and later for showing the page
          'insights_cloud/hits': [:index, :show, :auto_complete_search, :resolutions],
          'insights_cloud/settings': [:index, :show],
          'insights_cloud/ui_requests': [:forward_request, :translate_insights_host],
          'react': [:index],
        },
        :resource_type => ::InsightsHit.name
      )
      permission(
        :dispatch_cloud_requests,
        'api/v2/rh_cloud/cloud_request': [:update]
      )
      permission(
        :control_organization_insights,
        'insights_cloud/settings': [:set_org_parameter]
      )
      # Insights Vulnerability permissions
      permission(
        :view_vulnerability,
        {},
        :resource_type => 'ForemanRhCloud'
      )
      permission(
        :edit_vulnerability,
        {},
        :resource_type => 'ForemanRhCloud'
      )
      # Insights Advisor permissions
      permission(
        :view_advisor,
        {},
        :resource_type => 'ForemanRhCloud'
      )
      permission(
        :edit_advisor,
        {},
        :resource_type => 'ForemanRhCloud'
      )
    end

    # Core RH Cloud permissions for inventory upload and sync
    rh_cloud_permissions = [:view_foreman_rh_cloud, :generate_foreman_rh_cloud, :view_insights_hits, :dispatch_cloud_requests, :control_organization_insights]

    # Insights application permissions (Vulnerability, Advisor)
    insights_permissions = [:view_vulnerability, :edit_vulnerability, :view_advisor, :edit_advisor]

    plugin_permissions = rh_cloud_permissions + insights_permissions

    read_only_permissions = [:view_foreman_rh_cloud, :view_insights_hits, :view_vulnerability, :view_advisor]

    role 'ForemanRhCloud', plugin_permissions, 'Role granting permissions to view the hosts inventory,
                                                generate a report, upload it to the cloud, download it locally,
                                                and manage Insights Vulnerability and Advisor features'

    role 'ForemanRhCloud Read Only', read_only_permissions, 'Role granting read-only permissions to view
                                                             Insights Vulnerability, Advisor, and host inventory'

    add_permissions_to_default_roles Role::ORG_ADMIN => plugin_permissions,
      Role::MANAGER => plugin_permissions,
      Role::SYSTEM_ADMIN => plugin_permissions

    # Adding a top-level menu item
    sub_menu :top_menu, :insights_menu, caption: N_('Insights'), icon: 'fa fa-cloud', after: :hosts_menu do
      menu :top_menu,
        :inventory_upload,
        caption: N_('Inventory Upload'),
        url: '/foreman_rh_cloud/inventory_upload',
        url_hash: { controller: :react, action: :index },
        parent: :insights_menu,
        if: -> { !ForemanRhCloud.with_iop_smart_proxy? }
      menu :top_menu, :insights_hits, caption: N_('Recommendations'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :insights_menu
      menu :top_menu,
        :insights_vulnerability,
        caption: N_('Vulnerability'),
        url: '/foreman_rh_cloud/insights_vulnerability',
        url_hash: { controller: :react, action: :index },
        parent: :insights_menu,
        if: -> { ForemanRhCloud.with_iop_smart_proxy? }
    end

    # In IoP case we want the page to be in the admin menu
    menu :admin_menu,
      :inventory_upload,
      caption: N_('Inventory Upload'),
      url: '/foreman_rh_cloud/inventory_upload',
      url_hash: { controller: :react, action: :index },
      parent: :administer_menu,
      before: :upgrade,
      if: -> { ForemanRhCloud.with_iop_smart_proxy? }

    register_facet InsightsFacet, :insights do
      configure_host do
        api_view :list => 'api/v2/hosts/insights/insights', :single => 'api/v2/hosts/insights/single'
        set_dependent_action :destroy
      end
    end

    # Preload insights facet to avoid N+1 queries when rendering host list with facets
    add_controller_action_scope('Api::V2::HostsController', :index) do |base_scope|
      base_scope.preload(:insights)
    end

    register_global_js_file 'global'

    register_custom_status InventorySync::InventoryStatus
    register_custom_status InsightsClientReportStatus
    register_ping_extension { ForemanRhCloud::Ping.ping }
    register_status_extension { ForemanRhCloud::Ping.status }

    describe_host do
      overview_buttons_provider :insights_host_overview_buttons
    end

    extend_page 'hosts/show' do |context|
      context.add_pagelet :main_tabs,
        partial: 'hosts/insights_tab',
        name: _('Insights'),
        id: 'insights',
        onlyif: proc { |host| host.insights }
    end

    extend_page 'hosts/_list' do |context|
      context.with_profile :cloud, _('RH Cloud'), default: true do
        add_pagelet :hosts_table_column_header, key: :insights_recommendations_count, label: _('Recommendations'), sortable: true, width: '12%', class: 'hidden-xs ellipsis', priority: 100,
                    export_data: CsvExporter::ExportDefinition.new(:insights_recommendations_count, label: _('Recommendations'), callback: ->(host) { ForemanRhCloud.with_iop_smart_proxy? ? 'N/A' : host&.insights_hits&.count })
        add_pagelet :hosts_table_column_content, key: :insights_recommendations_count, callback: ->(host) { hits_counts_cell(host) }, class: 'hidden-xs ellipsis text-center', priority: 100
      end
    end

    ::Foreman::Plugin.app_metadata_registry.register(:foreman_rh_cloud, {
      iop: -> { ForemanRhCloud.with_iop_smart_proxy? },
    })

    extend_template_helpers ForemanRhCloud::TemplateRendererHelper
    allowed_template_helpers :remediations_playbook, :download_rh_playbook
  end
end