Class: Chef::Formatters::ErrorInspectors::RegistrationErrorInspector

Inherits:
Object
  • Object
show all
Includes:
APIErrorFormatting
Defined in:
lib/chef/formatters/error_inspectors/registration_error_inspector.rb

Overview

RegistrationErrorInspector

Wraps exceptions that occur during the client registration process and suggests possible causes. – TODO: Lots of duplication with the node_load_error_inspector, just slightly tweaked to talk about validation keys instead of other keys.

Constant Summary

Constants included from APIErrorFormatting

APIErrorFormatting::NETWORK_ERROR_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from APIErrorFormatting

#describe_400_error, #describe_401_error, #describe_406_error, #describe_500_error, #describe_503_error, #describe_eof_error, #describe_http_error, #describe_network_errors, #safe_format_rest_error

Constructor Details

#initialize(node_name, exception, config) ⇒ RegistrationErrorInspector

Returns a new instance of RegistrationErrorInspector.



19
20
21
22
23
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 19

def initialize(node_name, exception, config)
  @node_name = node_name
  @exception = exception
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 17

def config
  @config
end

#exceptionObject (readonly)

Returns the value of attribute exception.



15
16
17
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 15

def exception
  @exception
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



16
17
18
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 16

def node_name
  @node_name
end

Instance Method Details

#add_explanation(error_description) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 25

def add_explanation(error_description)
  case exception
  when Net::HTTPClientException, Net::HTTPFatalError
    humanize_http_exception(error_description)
  when Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError
    error_description.section("Network Error:", <<~E)
      There was a network error connecting to the #{ChefUtils::Dist::Server::PRODUCT}:
      #{exception.message}
    E
    error_description.section("Relevant Config Settings:", <<~E)
      chef_server_url  "#{server_url}"

      If your chef_server_url is correct, your network could be down.
    E
  when Chef::Exceptions::PrivateKeyMissing
    error_description.section("Private Key Not Found:", <<~E)
      Your private key could not be loaded. If the key file exists, ensure that it is
      readable by #{ChefUtils::Dist::Infra::PRODUCT}.
    E
    error_description.section("Relevant Config Settings:", <<~E)
      validation_key "#{api_key}"
    E
  when Chef::Exceptions::InvalidRedirect
    error_description.section("Invalid Redirect:", <<~E)
      Change your #{ChefUtils::Dist::Server::PRODUCT} location in client.rb to the #{ChefUtils::Dist::Server::PRODUCT}'s FQDN to avoid unwanted redirections.
    E
  when EOFError
    describe_eof_error(error_description)
  else
    "#{exception.class.name}: #{exception.message}"
  end
end

#api_keyObject



122
123
124
125
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 122

def api_key
  config[:validation_key]
  # config[:client_key]
end

#clock_skew?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 131

def clock_skew?
  exception.response.body =~ /synchronize the clock/i
end

#format_rest_errorObject

Parses JSON from the error response sent by Chef Server and returns the error message – TODO: this code belongs in Chef::REST



139
140
141
142
143
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 139

def format_rest_error
  Array(Chef::JSONCompat.from_json(exception.response.body)["error"]).join("; ")
rescue Exception
  exception.response.body
end

#humanize_http_exception(error_description) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 58

def humanize_http_exception(error_description)
  response = exception.response
  case response
  when Net::HTTPUnauthorized
    if clock_skew?
      error_description.section("Authentication Error:", <<~E)
        Failed to authenticate to the #{ChefUtils::Dist::Server::PRODUCT} (http 401).
        The request failed because your clock has drifted by more than 15 minutes.
        Syncing your clock to an NTP Time source should resolve the issue.
      E
    else
      error_description.section("Authentication Error:", <<~E)
        Failed to authenticate to the #{ChefUtils::Dist::Server::PRODUCT} (http 401).
      E

      error_description.section("Server Response:", format_rest_error)
      error_description.section("Relevant Config Settings:", <<~E)
        chef_server_url         "#{server_url}"
        validation_client_name  "#{username}"
        validation_key          "#{api_key}"

        If these settings are correct, your validation_key may be invalid.
      E
    end
  when Net::HTTPForbidden
    error_description.section("Authorization Error:", <<~E)
      Your validation client is not authorized to create the client for this node on the #{ChefUtils::Dist::Server::PRODUCT} (HTTP 403).
    E
    error_description.section("Possible Causes:", <<~E)
      * There may already be a client named "#{config[:node_name]}"
      * Your validation client (#{username}) may have misconfigured authorization permissions.
    E
  when Net::HTTPBadRequest
    error_description.section("Invalid Request Data:", <<~E)
      The data in your request was invalid (HTTP 400).
    E
    error_description.section("Server Response:", format_rest_error)
  when Net::HTTPNotFound
    error_description.section("Resource Not Found:", <<~E)
      The #{ChefUtils::Dist::Server::PRODUCT} returned a HTTP 404. This usually indicates that your chef_server_url configuration is incorrect.
    E
    error_description.section("Relevant Config Settings:", <<~E)
      chef_server_url "#{server_url}"
    E
  when Net::HTTPNotAcceptable
    describe_406_error(error_description, response)
  when Net::HTTPInternalServerError
    error_description.section("Unknown Server Error:", <<~E)
      The server had a fatal error attempting to load the node data.
    E
    error_description.section("Server Response:", format_rest_error)
  when Net::HTTPBadGateway, Net::HTTPServiceUnavailable
    error_description.section("Server Unavailable", "The #{ChefUtils::Dist::Server::PRODUCT} is temporarily unavailable")
    error_description.section("Server Response:", format_rest_error)
  else
    error_description.section("Unexpected API Request Failure:", format_rest_error)
  end
end

#server_urlObject



127
128
129
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 127

def server_url
  config[:chef_server_url]
end

#usernameObject



117
118
119
120
# File 'lib/chef/formatters/error_inspectors/registration_error_inspector.rb', line 117

def username
  # config[:node_name]
  config[:validation_client_name]
end