chef-config 14.1.1 → 14.1.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,34 @@
1
- # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
- # NOTE: This file is generated by running `rake version` in the top level of
18
- # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
- # task instead.
20
- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
-
22
- module ChefConfig
23
- CHEFCONFIG_ROOT = File.expand_path("../..", __FILE__)
24
- VERSION = "14.1.1"
25
- end
26
-
27
- #
28
- # NOTE: the Chef::Version class is defined in version_class.rb
29
- #
30
- # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
- # Chef::Version class is for _cookbooks_ only, and cannot handle
32
- # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
- # Gem::Version class instead.
34
- #
1
+ # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
+ # NOTE: This file is generated by running `rake version` in the top level of
18
+ # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
+ # task instead.
20
+ #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
+
22
+ module ChefConfig
23
+ CHEFCONFIG_ROOT = File.expand_path("../..", __FILE__)
24
+ VERSION = "14.1.12"
25
+ end
26
+
27
+ #
28
+ # NOTE: the Chef::Version class is defined in version_class.rb
29
+ #
30
+ # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
+ # Chef::Version class is for _cookbooks_ only, and cannot handle
32
+ # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
+ # Gem::Version class instead.
34
+ #
@@ -1,28 +1,28 @@
1
- #
2
- # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module ChefConfig
19
-
20
- def self.windows?
21
- if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
- true
23
- else
24
- false
25
- end
26
- end
27
-
28
- end
1
+ #
2
+ # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module ChefConfig
19
+
20
+ def self.windows?
21
+ if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ end
@@ -1,222 +1,224 @@
1
- #
2
- # Author:: Daniel DeLeo (<dan@chef.io>)
3
- # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require "chef-config/config"
20
- require "chef-config/exceptions"
21
- require "chef-config/logger"
22
- require "chef-config/path_helper"
23
- require "chef-config/windows"
24
- require "chef-config/mixin/dot_d"
25
- require "chef-config/mixin/credentials"
26
-
27
- module ChefConfig
28
- class WorkstationConfigLoader
29
- include ChefConfig::Mixin::DotD
30
- include ChefConfig::Mixin::Credentials
31
-
32
- # Path to a config file requested by user, (e.g., via command line option). Can be nil
33
- attr_accessor :explicit_config_file
34
- # The name of a credentials profile. Can be nil
35
- attr_accessor :profile
36
- attr_reader :credentials_found
37
-
38
- # TODO: initialize this with a logger for Chef and Knife
39
- def initialize(explicit_config_file, logger = nil, profile: nil)
40
- @explicit_config_file = explicit_config_file
41
- @chef_config_dir = nil
42
- @config_location = nil
43
- @profile = profile
44
- @logger = logger || NullLogger.new
45
- @credentials_found = false
46
- end
47
-
48
- def no_config_found?
49
- config_location.nil? && !credentials_found
50
- end
51
-
52
- def config_location
53
- @config_location ||= (explicit_config_file || locate_local_config)
54
- end
55
-
56
- def chef_config_dir
57
- if @chef_config_dir.nil?
58
- @chef_config_dir = false
59
- full_path = working_directory.split(File::SEPARATOR)
60
- (full_path.length - 1).downto(0) do |i|
61
- candidate_directory = File.join(full_path[0..i] + [".chef"])
62
- if File.exist?(candidate_directory) && File.directory?(candidate_directory)
63
- @chef_config_dir = candidate_directory
64
- break
65
- end
66
- end
67
- end
68
- @chef_config_dir
69
- end
70
-
71
- def load
72
- load_credentials(profile)
73
- # Ignore it if there's no explicit_config_file and can't find one at a
74
- # default path.
75
- if !config_location.nil?
76
- if explicit_config_file && !path_exists?(config_location)
77
- raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
78
- end
79
-
80
- # Have to set Config.config_file b/c other config is derived from it.
81
- Config.config_file = config_location
82
- apply_config(IO.read(config_location), config_location)
83
- end
84
-
85
- load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir]
86
- end
87
-
88
- # (Private API, public for test purposes)
89
- def env
90
- ENV
91
- end
92
-
93
- # (Private API, public for test purposes)
94
- def path_exists?(path)
95
- Pathname.new(path).expand_path.exist?
96
- end
97
-
98
- private
99
-
100
- def have_config?(path)
101
- if path_exists?(path)
102
- logger.info("Using config at #{path}")
103
- true
104
- else
105
- logger.debug("Config not found at #{path}, trying next option")
106
- false
107
- end
108
- end
109
-
110
- def locate_local_config
111
- candidate_configs = []
112
-
113
- # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
114
- if env["KNIFE_HOME"]
115
- candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
116
- candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
117
- end
118
- # Look for $PWD/knife.rb
119
- if Dir.pwd
120
- candidate_configs << File.join(Dir.pwd, "config.rb")
121
- candidate_configs << File.join(Dir.pwd, "knife.rb")
122
- end
123
- # Look for $UPWARD/.chef/knife.rb
124
- if chef_config_dir
125
- candidate_configs << File.join(chef_config_dir, "config.rb")
126
- candidate_configs << File.join(chef_config_dir, "knife.rb")
127
- end
128
- # Look for $HOME/.chef/knife.rb
129
- PathHelper.home(".chef") do |dot_chef_dir|
130
- candidate_configs << File.join(dot_chef_dir, "config.rb")
131
- candidate_configs << File.join(dot_chef_dir, "knife.rb")
132
- end
133
-
134
- candidate_configs.find do |candidate_config|
135
- have_config?(candidate_config)
136
- end
137
- end
138
-
139
- def working_directory
140
- a = if ChefConfig.windows?
141
- env["CD"]
142
- else
143
- env["PWD"]
144
- end || Dir.pwd
145
-
146
- a
147
- end
148
-
149
- def apply_credentials(creds, profile)
150
- Config.profile ||= profile
151
- if creds.key?("node_name") && creds.key?("client_name")
152
- raise ChefConfig::ConfigurationError, "Do not specify both node_name and client_name. You should prefer client_name."
153
- end
154
- Config.node_name = creds.fetch("node_name") if creds.key?("node_name")
155
- Config.node_name = creds.fetch("client_name") if creds.key?("client_name")
156
- Config.chef_server_url = creds.fetch("chef_server_url") if creds.key?("chef_server_url")
157
- Config.validation_client_name = creds.fetch("validation_client_name") if creds.key?("validation_client_name")
158
-
159
- extract_key(creds, "validation_key", :validation_key, :validation_key_contents)
160
- extract_key(creds, "validator_key", :validation_key, :validation_key_contents)
161
- extract_key(creds, "client_key", :client_key, :client_key_contents)
162
- @credentials_found = true
163
- end
164
-
165
- def extract_key(creds, name, config_path, config_contents)
166
- return unless creds.has_key?(name)
167
-
168
- val = creds.fetch(name)
169
- if val.start_with?("-----BEGIN RSA PRIVATE KEY-----")
170
- Config.send(config_contents, val)
171
- else
172
- abs_path = Pathname.new(val).expand_path(home_chef_dir)
173
- Config.send(config_path, abs_path)
174
- end
175
- end
176
-
177
- def home_chef_dir
178
- @home_chef_dir ||= PathHelper.home(".chef")
179
- end
180
-
181
- def apply_config(config_content, config_file_path)
182
- Config.from_string(config_content, config_file_path)
183
- rescue SignalException
184
- raise
185
- rescue SyntaxError => e
186
- message = ""
187
- message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
188
- message << "#{e.class.name}: #{e.message}\n"
189
- if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
190
- line = file_line[/:([\d]+)$/, 1].to_i
191
- message << highlight_config_error(config_file_path, line)
192
- end
193
- raise ChefConfig::ConfigurationError, message
194
- rescue Exception => e
195
- message = "You have an error in your config file #{config_file_path}\n\n"
196
- message << "#{e.class.name}: #{e.message}\n"
197
- filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
198
- filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
199
- if !filtered_trace.empty?
200
- line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
201
- message << highlight_config_error(config_file_path, line_nr.to_i)
202
- end
203
- raise ChefConfig::ConfigurationError, message
204
- end
205
-
206
- def highlight_config_error(file, line)
207
- config_file_lines = []
208
- IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
209
- if line == 1
210
- lines = config_file_lines[0..3]
211
- else
212
- lines = config_file_lines[Range.new(line - 2, line)]
213
- end
214
- "Relevant file content:\n" + lines.join("\n") + "\n"
215
- end
216
-
217
- def logger
218
- @logger
219
- end
220
-
221
- end
222
- end
1
+ #
2
+ # Author:: Daniel DeLeo (<dan@chef.io>)
3
+ # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef-config/config"
20
+ require "chef-config/exceptions"
21
+ require "chef-config/logger"
22
+ require "chef-config/path_helper"
23
+ require "chef-config/windows"
24
+ require "chef-config/mixin/dot_d"
25
+ require "chef-config/mixin/credentials"
26
+
27
+ module ChefConfig
28
+ class WorkstationConfigLoader
29
+ include ChefConfig::Mixin::DotD
30
+ include ChefConfig::Mixin::Credentials
31
+
32
+ # Path to a config file requested by user, (e.g., via command line option). Can be nil
33
+ attr_accessor :explicit_config_file
34
+ # The name of a credentials profile. Can be nil
35
+ attr_accessor :profile
36
+ attr_reader :credentials_found
37
+
38
+ # TODO: initialize this with a logger for Chef and Knife
39
+ def initialize(explicit_config_file, logger = nil, profile: nil)
40
+ @explicit_config_file = explicit_config_file
41
+ @chef_config_dir = nil
42
+ @config_location = nil
43
+ @profile = profile
44
+ @logger = logger || NullLogger.new
45
+ @credentials_found = false
46
+ end
47
+
48
+ def no_config_found?
49
+ config_location.nil? && !credentials_found
50
+ end
51
+
52
+ def config_location
53
+ @config_location ||= (explicit_config_file || locate_local_config)
54
+ end
55
+
56
+ def chef_config_dir
57
+ if @chef_config_dir.nil?
58
+ @chef_config_dir = false
59
+ full_path = working_directory.split(File::SEPARATOR)
60
+ (full_path.length - 1).downto(0) do |i|
61
+ candidate_directory = File.join(full_path[0..i] + [".chef"])
62
+ if File.exist?(candidate_directory) && File.directory?(candidate_directory)
63
+ @chef_config_dir = candidate_directory
64
+ break
65
+ end
66
+ end
67
+ end
68
+ @chef_config_dir
69
+ end
70
+
71
+ def load
72
+ load_credentials(profile)
73
+ # Ignore it if there's no explicit_config_file and can't find one at a
74
+ # default path.
75
+ unless config_location.nil?
76
+ if explicit_config_file && !path_exists?(config_location)
77
+ raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
78
+ end
79
+
80
+ # Have to set Config.config_file b/c other config is derived from it.
81
+ Config.config_file = config_location
82
+ apply_config(IO.read(config_location), config_location)
83
+ end
84
+
85
+ load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir]
86
+ end
87
+
88
+ # (Private API, public for test purposes)
89
+ def env
90
+ ENV
91
+ end
92
+
93
+ # (Private API, public for test purposes)
94
+ def path_exists?(path)
95
+ Pathname.new(path).expand_path.exist?
96
+ end
97
+
98
+ private
99
+
100
+ def have_config?(path)
101
+ if path_exists?(path)
102
+ logger.info("Using config at #{path}")
103
+ true
104
+ else
105
+ logger.debug("Config not found at #{path}, trying next option")
106
+ false
107
+ end
108
+ end
109
+
110
+ def locate_local_config
111
+ candidate_configs = []
112
+
113
+ # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
114
+ if env["KNIFE_HOME"]
115
+ candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
116
+ candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
117
+ end
118
+ # Look for $PWD/knife.rb
119
+ if Dir.pwd
120
+ candidate_configs << File.join(Dir.pwd, "config.rb")
121
+ candidate_configs << File.join(Dir.pwd, "knife.rb")
122
+ end
123
+ # Look for $UPWARD/.chef/knife.rb
124
+ if chef_config_dir
125
+ candidate_configs << File.join(chef_config_dir, "config.rb")
126
+ candidate_configs << File.join(chef_config_dir, "knife.rb")
127
+ end
128
+ # Look for $HOME/.chef/knife.rb
129
+ PathHelper.home(".chef") do |dot_chef_dir|
130
+ candidate_configs << File.join(dot_chef_dir, "config.rb")
131
+ candidate_configs << File.join(dot_chef_dir, "knife.rb")
132
+ end
133
+
134
+ candidate_configs.find do |candidate_config|
135
+ have_config?(candidate_config)
136
+ end
137
+ end
138
+
139
+ def working_directory
140
+ a = if ChefConfig.windows?
141
+ env["CD"]
142
+ else
143
+ env["PWD"]
144
+ end || Dir.pwd
145
+
146
+ a
147
+ end
148
+
149
+ def apply_credentials(creds, profile)
150
+ Config.profile ||= profile
151
+ if creds.key?("node_name") && creds.key?("client_name")
152
+ raise ChefConfig::ConfigurationError, "Do not specify both node_name and client_name. You should prefer client_name."
153
+ end
154
+ Config.node_name = creds.fetch("node_name") if creds.key?("node_name")
155
+ Config.node_name = creds.fetch("client_name") if creds.key?("client_name")
156
+ Config.chef_server_url = creds.fetch("chef_server_url") if creds.key?("chef_server_url")
157
+ Config.validation_client_name = creds.fetch("validation_client_name") if creds.key?("validation_client_name")
158
+
159
+ Config.knife.merge!(Hash[creds.fetch("knife", {}).map { |k, v| [k.to_sym, v] }])
160
+
161
+ extract_key(creds, "validation_key", :validation_key, :validation_key_contents)
162
+ extract_key(creds, "validator_key", :validation_key, :validation_key_contents)
163
+ extract_key(creds, "client_key", :client_key, :client_key_contents)
164
+ @credentials_found = true
165
+ end
166
+
167
+ def extract_key(creds, name, config_path, config_contents)
168
+ return unless creds.has_key?(name)
169
+
170
+ val = creds.fetch(name)
171
+ if val.start_with?("-----BEGIN RSA PRIVATE KEY-----")
172
+ Config.send(config_contents, val)
173
+ else
174
+ abs_path = Pathname.new(val).expand_path(home_chef_dir)
175
+ Config.send(config_path, abs_path)
176
+ end
177
+ end
178
+
179
+ def home_chef_dir
180
+ @home_chef_dir ||= PathHelper.home(".chef")
181
+ end
182
+
183
+ def apply_config(config_content, config_file_path)
184
+ Config.from_string(config_content, config_file_path)
185
+ rescue SignalException
186
+ raise
187
+ rescue SyntaxError => e
188
+ message = ""
189
+ message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
190
+ message << "#{e.class.name}: #{e.message}\n"
191
+ if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
192
+ line = file_line[/:([\d]+)$/, 1].to_i
193
+ message << highlight_config_error(config_file_path, line)
194
+ end
195
+ raise ChefConfig::ConfigurationError, message
196
+ rescue Exception => e
197
+ message = "You have an error in your config file #{config_file_path}\n\n"
198
+ message << "#{e.class.name}: #{e.message}\n"
199
+ filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
200
+ filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
201
+ unless filtered_trace.empty?
202
+ line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
203
+ message << highlight_config_error(config_file_path, line_nr.to_i)
204
+ end
205
+ raise ChefConfig::ConfigurationError, message
206
+ end
207
+
208
+ def highlight_config_error(file, line)
209
+ config_file_lines = []
210
+ IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
211
+ if line == 1
212
+ lines = config_file_lines[0..3]
213
+ else
214
+ lines = config_file_lines[Range.new(line - 2, line)]
215
+ end
216
+ "Relevant file content:\n" + lines.join("\n") + "\n"
217
+ end
218
+
219
+ def logger
220
+ @logger
221
+ end
222
+
223
+ end
224
+ end