Class: HammerCLIForeman::Sessions

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli_foreman/sessions.rb

Constant Summary collapse

STORAGE_DIR =
File.expand_path('~/.hammer/sessions/')

Class Method Summary collapse

Class Method Details

.configured?(url) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hammer_cli_foreman/sessions.rb', line 36

def self.configured?(url)
  ensure_mode(storage, '40700') && ensure_mode(session_file(url), '100600')
end

.enabled?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/hammer_cli_foreman/sessions.rb', line 23

def self.enabled?
  HammerCLI::Settings.get(:foreman, :use_sessions)
end

.ensure_mode(file, expected_mode) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hammer_cli_foreman/sessions.rb', line 40

def self.ensure_mode(file, expected_mode)
  return true unless File.exist?(file)

  mode = File.stat(file).mode.to_s(8)
  if mode != expected_mode
    warn _("Invalid permissions for %{file}: %{mode}, expected %{expected_mode}.") % {
      :mode => mode,
      :expected_mode => expected_mode,
      :file => file
    }
    false
  else
    true
  end
end

.get(url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hammer_cli_foreman/sessions.rb', line 7

def self.get(url)
  raise _('Sessions are not enabled, please check your Hammer settings.') unless enabled?

  unless File.exist?(storage)
    FileUtils.mkdir_p(storage, mode: 0700)
  end
  unless configured?(url)
    warn _('Using session auth with invalid permissions on session files is not recommended.')
  end
  HammerCLIForeman::Session.new(session_file(url))
end

.session_file(url) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/hammer_cli_foreman/sessions.rb', line 27

def self.session_file(url)
  raise _('The url is empty. Session can not be created.') if url.nil? || url.empty?

  uri = URI.parse(url)
  File.join(storage, "#{uri.scheme}_#{uri.host}")
rescue URI::InvalidURIError
  raise _('The url (%s) is not a valid URL. Session can not be created.') % url
end

.storageObject



19
20
21
# File 'lib/hammer_cli_foreman/sessions.rb', line 19

def self.storage
  STORAGE_DIR
end