Module: ChefConfig::Mixin::Credentials

Included in:
WorkstationConfigLoader
Defined in:
lib/chef-config/mixin/credentials.rb

Overview

Helper methods for working with credentials files.

Since:

  • 13.7

Instance Method Summary collapse

Instance Method Details

#credentials_file_pathString

Compute the path to the credentials file.

Returns:

  • (String)

Since:

  • 14.4


55
56
57
# File 'lib/chef-config/mixin/credentials.rb', line 55

def credentials_file_path
  PathHelper.home(ChefConfig::Dist::USER_CONF_DIR, "credentials").freeze
end

#credentials_profile(profile = nil) ⇒ String

Compute the active credentials profile name.

The lookup order is argument (from –profile), environment variable ($CHEF_PROFILE), context file (~/.chef/context), and then “default” as a fallback.

Parameters:

  • profile (String, nil) (defaults to: nil)

    Optional override for the active profile, normally set via a command-line option.

Returns:

  • (String)

Since:

  • 14.4


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef-config/mixin/credentials.rb', line 38

def credentials_profile(profile = nil)
  context_file = PathHelper.home(ChefConfig::Dist::USER_CONF_DIR, "context").freeze
  if !profile.nil?
    profile
  elsif ENV.include?("CHEF_PROFILE")
    ENV["CHEF_PROFILE"]
  elsif File.file?(context_file)
    File.read(context_file).strip
  else
    "default"
  end
end

#load_credentials(profile = nil) ⇒ void

This method returns an undefined value.

Load and process the active credentials.

Parameters:

  • profile (String, nil) (defaults to: nil)

    Optional override for the active profile, normally set via a command-line option.

See Also:

  • WorkstationConfigLoader#apply_credentials

Since:

  • 13.7


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef-config/mixin/credentials.rb', line 85

def load_credentials(profile = nil)
  profile = credentials_profile(profile)
  config = parse_credentials_file
  return if config.nil? # No credentials, nothing to do here.

  if config[profile].nil?
    # Unknown profile name. For "default" just silently ignore, otherwise
    # raise an error.
    return if profile == "default"

    raise ChefConfig::ConfigurationError, "Profile #{profile} doesn't exist. Please add it to #{credentials_file_path}."
  end
  apply_credentials(config[profile], profile)
end

#parse_credentials_fileString?

Load and parse the credentials file.

Returns `nil` if the credentials file is unavailable.

Returns:

  • (String, nil)

Since:

  • 14.4


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef-config/mixin/credentials.rb', line 65

def parse_credentials_file
  credentials_file = credentials_file_path
  return nil unless File.file?(credentials_file)

  begin
    Tomlrb.load_file(credentials_file)
  rescue => e
    # TOML's error messages are mostly rubbish, so we'll just give a generic one
    message = "Unable to parse Credentials file: #{credentials_file}\n"
    message << e.message
    raise ChefConfig::ConfigurationError, message
  end
end