summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2021-09-13 16:36:00 -0700
committerGitHub <noreply@github.com>2021-09-13 16:36:00 -0700
commit86bff4357d825e55661c26886119b692ed5585a9 (patch)
tree3c758dd443706733282ea8f2bff877d3b873e6e2
parent296edf56682fcf5c6f512bc8255325b869a4b36c (diff)
parentff0b652650468ac0bc79e1ccb0e18faf9b4f8e8b (diff)
downloadchef-86bff4357d825e55661c26886119b692ed5585a9.tar.gz
Merge pull request #12020 from gogsbread/yaml
-rw-r--r--docs/dev/design_documents/resource_file_content_verification.md11
-rw-r--r--lib/chef/provider/file.rb1
-rw-r--r--lib/chef/resource/file/verification/yaml.rb54
-rw-r--r--spec/unit/resource/file/verification/yaml_spec.rb67
4 files changed, 130 insertions, 3 deletions
diff --git a/docs/dev/design_documents/resource_file_content_verification.md b/docs/dev/design_documents/resource_file_content_verification.md
index f813e57c72..3df3083395 100644
--- a/docs/dev/design_documents/resource_file_content_verification.md
+++ b/docs/dev/design_documents/resource_file_content_verification.md
@@ -81,15 +81,20 @@ template "/etc/nginx.conf" do
end
```
-Chef may ship built-in verifiers for common checks, such as
-content-type verification. Built-in verifiers can be used by passing
-well-known symbols to the verify attribute:
+Chef ships built-in verifiers for common content-types such as
+`:systemd_unit`, `:yaml` and `:json`. Built-in verifiers can be
+used by passing well-known symbols to the verify attribute:
```ruby
template "/etc/config.json" do
verify :json
end
```
+```ruby
+template "/etc/config.yaml" do
+ verify :yaml
+end
+```
## Motivation
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index 96f6eef061..8f77cc4641 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -27,6 +27,7 @@ require_relative "../scan_access_control"
require_relative "../mixin/checksum"
require_relative "../mixin/file_class"
require_relative "../mixin/enforce_ownership_and_permissions"
+require_relative "../resource/file/verification/yaml"
require_relative "../util/backup"
require_relative "../util/diff"
require_relative "../util/selinux"
diff --git a/lib/chef/resource/file/verification/yaml.rb b/lib/chef/resource/file/verification/yaml.rb
new file mode 100644
index 0000000000..7292f59b15
--- /dev/null
+++ b/lib/chef/resource/file/verification/yaml.rb
@@ -0,0 +1,54 @@
+#
+# Author:: Antony Thomas (<antonydeepak@gmail.com>)
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "psych" unless defined?(Psych)
+
+class Chef
+ class Resource
+ class File
+ class Verification
+
+ #
+ # Extends File verification to provide a Yaml verification
+ #
+ # Example:
+ # file 'foo.yaml' do
+ # content "--- foo: 'foo-"
+ # verify :yaml
+ # end
+ #
+ #
+
+ class Yaml < Chef::Resource::File::Verification
+
+ provides :yaml
+
+ def verify(path, opts = {})
+ begin
+ Psych.parse_file(path)
+ return true
+ rescue Psych::SyntaxError => e
+ Chef::Log.error("Yaml syntax verify failed with : #{e.message}")
+ return false
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/unit/resource/file/verification/yaml_spec.rb b/spec/unit/resource/file/verification/yaml_spec.rb
new file mode 100644
index 0000000000..c7fdf42faf
--- /dev/null
+++ b/spec/unit/resource/file/verification/yaml_spec.rb
@@ -0,0 +1,67 @@
+#
+# Author:: Antony Thomas (<antonydeepak@gmail.com>)
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Chef::Resource::File::Verification::Yaml do
+ let(:parent_resource) { Chef::Resource.new("llama") }
+
+ before(:all) do
+ @valid_yaml = "valid-#{Time.now.to_i}.yaml"
+ f = File.new(@valid_yaml, "w")
+ f.write("# comment
+ svc:
+ mysqlPassword: sepppasswd
+ ")
+ f.close()
+
+ @invalid_yaml = "invalid-#{Time.now.to_i}.yaml"
+ f = File.new(@invalid_yaml, "w")
+ f.write("# comment
+ svc:
+ mysqlPassword: 'sepppasswd
+ ")
+ f.close()
+
+ @empty_yaml = "empty-#{Time.now.to_i}.yaml"
+ File.new(@empty_yaml, "w").close()
+ end
+
+ context "verify" do
+ it "returns true for valid yaml" do
+ v = Chef::Resource::File::Verification::Yaml.new(parent_resource, :yaml, {})
+ expect(v.verify(@valid_yaml)).to eq(true)
+ end
+
+ it "returns false for invalid yaml" do
+ v = Chef::Resource::File::Verification::Yaml.new(parent_resource, :yaml, {})
+ expect(v.verify(@invalid_yaml)).to eq(false)
+ end
+
+ it "returns true for empty file" do
+ v = Chef::Resource::File::Verification::Yaml.new(parent_resource, :yaml, {})
+ expect(v.verify(@empty_yaml)).to eq(true)
+ end
+ end
+
+ after(:all) do
+ File.delete(@valid_yaml)
+ File.delete(@invalid_yaml)
+ File.delete(@empty_yaml)
+ end
+end