Skip to content

Commit 297c3d7

Browse files
authored
Enhancement section custom resource (#344)
* Custom Resource of iis_section Signed-off-by: Justin Schuhmann <jmschu02@gmail.com> * Section custom resource Signed-off-by: Justin Schuhmann <jmschu02@gmail.com>
1 parent dab0464 commit 297c3d7

File tree

14 files changed

+303
-85
lines changed

14 files changed

+303
-85
lines changed

.kitchen.appveyor.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ suites:
2121
- name: app
2222
run_list:
2323
- recipe[test::app]
24+
- name: section
25+
run_list:
26+
- recipe[test::section]
2427
- name: site
2528
run_list:
2629
- recipe[test::site]
@@ -29,4 +32,4 @@ suites:
2932
- recipe[test::pool]
3033
- name: vdir
3134
run_list:
32-
- recipe[test::vdir]
35+
- recipe[test::vdir]

.kitchen.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ suites:
2626
- name: app
2727
run_list:
2828
- recipe[test::app]
29+
- name: section
30+
run_list:
31+
- recipe[test::section]
2932
- name: site
3033
run_list:
3134
- recipe[test::site]

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Style/PredicateName:
66
Exclude:
77
- 'test/integration/app/libraries/*'
88
- 'test/integration/pool/libraries/*'
9+
- 'test/integration/section/libraries/*'
910
- 'test/integration/vdir/libraries/*'
1011

1112
Style/GuardClause:

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ This is valuable to allow the `web.config` of an individual application/website
466466
#### Attribute Parameters
467467

468468
- `section`: The name of the section to lock.
469+
- `site`: The name of the site you want to lock or unlock a section for.
470+
- `application_path`: The path to the application you want to lock or unlock a section for.
469471
- `returns`: The result of the `shell_out` command.
472+
-
470473

471474
#### Examples
472475

@@ -502,6 +505,25 @@ iis_section 'unlocked web.config globally for Basic auth' do
502505
end
503506
```
504507

508+
```ruby
509+
# Sets the static content section for default web site and root to unlocked
510+
iis_section 'unlock staticContent of default web site' do
511+
section 'system.webServer/staticContent'
512+
site 'Default Web Site'
513+
action :unlock
514+
end
515+
```
516+
517+
```ruby
518+
# Sets the static content section for test_app under default website and root to be unlocked
519+
iis_section 'unlock staticContent of default web site' do
520+
section 'system.webServer/staticContent'
521+
site 'Default Web Site'
522+
application_path '/test_app'
523+
action :unlock
524+
end
525+
```
526+
505527
### iis_module
506528

507529
Manages modules globally or on a per site basis.

libraries/helper.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
# Cookbook:: iis
33
# Library:: helper
44
#
5-
# Author:: Julian C. Dunn <jdunn@chef.io>
6-
# Author:: Justin Schuhmann <jmschu02@gmail.com>
7-
#
8-
# Copyright:: 2013-2016, Chef Software, Inc.
5+
# Copyright:: 2013-2017, Chef Software, Inc.
96
#
107
# Licensed under the Apache License, Version 2.0 (the "License");
118
# you may not use this file except in compliance with the License.

libraries/section_helper.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# Cookbook:: iis
3+
# Library:: helper
4+
#
5+
# Copyright:: 2013-2017, Chef Software, Inc.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
module Opscode
21+
module IIS
22+
# Contains functions that are used throughout this cookbook
23+
module SectionHelper
24+
require 'rexml/document'
25+
include REXML
26+
27+
def lock(node, section, location, returns)
28+
cmd_list_section node, :lock, section, location, returns
29+
end
30+
31+
def unlock(node, section, location, returns)
32+
cmd_list_section node, :unlock, section, location, returns
33+
end
34+
35+
def cmd_section(node, check, section, location, returns)
36+
cmd = "#{appcmd(node)} set config \"MACHINE/WEBROOT/APPHOST/#{location}\""
37+
cmd << " -section:\"#{section}\" -overrideMode:#{check}"
38+
cmd << ' -commit:apphost'
39+
Chef::Log.debug(cmd)
40+
shell_out!(cmd, returns: returns)
41+
42+
return unless location
43+
cmd = "#{appcmd(node)} set config \"MACHINE/WEBROOT/APPHOST/#{location}\""
44+
cmd << " -section:\"#{section}\" -overrideMode:#{check}"
45+
Chef::Log.debug(cmd)
46+
shell_out!(cmd, returns: returns)
47+
end
48+
49+
def cmd_list_section(node, action, section, location, returns)
50+
command_path = 'MACHINE/WEBROOT/APPHOST'
51+
command_path << "/#{location}" if location
52+
cmd = "#{appcmd(node)} list config \"#{command_path}}\""
53+
cmd << " -section:#{section} -commit:apphost /config:* /xml"
54+
result = shell_out cmd
55+
if result.stderr.empty?
56+
xml = result.stdout
57+
doc = Document.new xml
58+
check = action == :lock ? 'Deny' : 'Allow'
59+
unless value(doc.root, 'CONFIG/@overrideMode') == check
60+
cmd_section node, check, section, location, returns
61+
end
62+
else
63+
Chef::Log.info(result.stderr)
64+
end
65+
end
66+
end
67+
end
68+
end

providers/section.rb

Lines changed: 0 additions & 72 deletions
This file was deleted.

resources/section.rb

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#
2-
# Author:: Justin Schuhmann
32
# Cookbook:: iis
4-
# Resource:: lock
3+
# Resource:: section
54
#
6-
# Copyright:: 2016, Justin Schuhmann
5+
# Copyright:: 2016-2017, Chef Software, Inc.
76
#
87
# Licensed under the Apache License, Version 2.0 (the "License");
98
# you may not use this file except in compliance with the License.
@@ -18,10 +17,57 @@
1817
# limitations under the License.
1918
#
2019

21-
actions :lock, :unlock
22-
default_action :lock
20+
require 'rexml/document'
2321

24-
attribute :section, kind_of: String
25-
attribute :returns, kind_of: [Integer, Array], default: 0
22+
include REXML
23+
include Opscode::IIS::Helper
24+
include Opscode::IIS::SectionHelper
25+
include Opscode::IIS::Processors
2626

27-
attr_accessor :exists
27+
property :section, String, name_property: true
28+
property :site, String
29+
property :application_path, String
30+
property :returns, [Integer, Array], default: 0
31+
property :locked, String
32+
33+
default_action :unlock
34+
35+
load_current_value do |desired|
36+
section desired.section
37+
site desired.site
38+
application_path desired.application_path
39+
command_path = 'MACHINE/WEBROOT/APPHOST'
40+
command_path << "/#{site}" if site
41+
command_path << application_path.to_s if application_path
42+
cmd = "#{appcmd(node)} list config \"#{command_path}\""
43+
cmd << " -section:\"#{section}\" /commit:apphost /config:* /xml"
44+
Chef::Log.debug(cmd)
45+
cmd = shell_out(cmd)
46+
if cmd.stderr.empty?
47+
xml = cmd.stdout
48+
doc = Document.new(xml)
49+
locked value doc.root, 'CONFIG/@overrideMode'
50+
else
51+
Chef::Log.info(cmd.stderr)
52+
end
53+
end
54+
55+
action :lock do
56+
if current_resource.locked != 'Deny'
57+
converge_by "Locking the section - \"#{new_resource}\"" do
58+
lock node, new_resource.section, "#{new_resource.site}#{new_resource.application_path}", new_resource.returns
59+
end
60+
else
61+
Chef::Log.debug("#{new_resource} already locked - nothing to do")
62+
end
63+
end
64+
65+
action :unlock do
66+
if current_resource.locked != 'Allow'
67+
converge_by "Unlocking the section - \"#{new_resource}\"" do
68+
unlock node, new_resource.section, "#{new_resource.site}#{new_resource.application_path}", new_resource.returns
69+
end
70+
else
71+
Chef::Log.debug("#{new_resource} already unlocked - nothing to do")
72+
end
73+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Cookbook:: test
3+
# Recipe:: site
4+
#
5+
# copyright: 2017, Chef Software, Inc.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
include_recipe 'iis'
20+
21+
iis_section 'unlock staticContent of default web site' do
22+
section 'system.webServer/staticContent'
23+
site 'Default Web Site'
24+
action :unlock
25+
end

test/integration/section/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `iis_section` InSpec Profile
2+
3+
This will allow the testing of `iis_section` until it can be added into inspec.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: utf-8
2+
# copyright: 2017, Chef Software, Inc.
3+
# license: All rights reserved
4+
5+
title 'iis_section section'
6+
7+
describe service('W3SVC') do
8+
it { should be_installed }
9+
it { should be_running }
10+
its ('startmode') { should eq 'Auto' }
11+
end
12+
13+
describe iis_section('system.webServer/staticContent', 'Default Web Site') do
14+
it { should exist }
15+
it { should have_override_mode('Allow') }
16+
it { should have_override_mode_effective('Allow') }
17+
end

test/integration/section/inspec.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: section
2+
title: iis_section InSpec Profile
3+
copyright: 2017, Chef Software, Inc.
4+
license: All Rights Reserved
5+
summary: An InSpec Compliance Profile for iis_section
6+
version: 0.1.0

test/integration/section/libraries/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)