puppet-validator 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3dc256dd81faa824703173a618ee04e5744afa2
4
- data.tar.gz: 6beb62e29d7b8b49bb2441b0e9414cdae0dd3760
3
+ metadata.gz: 67c8ee96635e89f10fdb0562b7c34444aaa46090
4
+ data.tar.gz: 832192f6b3ee57c0f418fe86d12dbc2dbdd99c4a
5
5
  SHA512:
6
- metadata.gz: 9270323f2c725c7389883ca0f2c180132b4074b9fee02401b9a64a0bae35fd9e2bc2753b1700aecc3b0e3b1fcc564b6e15f06107e146c30d96683fc07fbfc646
7
- data.tar.gz: 728b48f8044fe6e9703212eeb79a997543e1b49d339da39887f810598e4aba28a735bd5f107497422e1f1b5355489dd7acd05a710122ea9bceaca6a4d0c3e86c
6
+ metadata.gz: be779caf3b0cd291459dcb74c4b48608e6e731babfa3fbb42bbf76b054a349dc3e600d2319c9fe27c08741a84222dccc561ffbc564641871748f6088d566866d
7
+ data.tar.gz: b52bfc8a76c77bfd87661dbb964a85411c628f3e5c7c17d4d97b3bf05c757d67e95569b260a2f8d91d1af46aa2e21771ff84c33af08811d68dc86937f7a8c636
data/README.md CHANGED
@@ -29,7 +29,11 @@ Options:
29
29
  /var/log/puppet-validator if no filename is passed.
30
30
  -p, --port PORT Port to listen on. Defaults to 9000.
31
31
  -t, --theme THEMEDIR Path to the theme directory.
32
-
32
+ -x, --csrf Protect from cross site request forgery. Requires code to be
33
+ submitted for validation via the webpage.
34
+ -g, --graph Generate relationship graphs from validated code. Requires
35
+ `graphviz` to be installed.
36
+
33
37
  -h, --help Displays this help
34
38
 
35
39
  #### Integrating with Middleware
@@ -80,8 +84,23 @@ logger.level = Logger::WARN
80
84
  PuppetValidator.set :puppet_versions, Dir.glob('*').select {|f| File.symlink? f and File.readlink(f) == '.' }
81
85
  PuppetValidator.set :root, File.dirname(__FILE__)
82
86
  PuppetValidator.set :logger, logger
87
+
88
+ # List out the lint checks you want disabled. By default, this will enable
89
+ # all installed checks. puppet-lint --help will list known checks.
90
+ #
83
91
  PuppetValidator.set :disabled_lint_checks, ['80chars']
84
92
 
93
+ # Protect from cross site request forgery. With this set, code may be
94
+ # submitted for validation by the website only.
95
+ #
96
+ # Note: This will currently break multiple version validation.
97
+ PuppetValidator.set :csrf, false
98
+
99
+ # Provide the option to generate relationship graphs from validated code.
100
+ # This requires that the `graphviz` package be installed.
101
+ #
102
+ PuppetValidator.set :graph, false
103
+
85
104
  run PuppetValidator
86
105
  ```
87
106
 
@@ -0,0 +1,44 @@
1
+ #! /usr/bin/env ruby
2
+ require 'puppet'
3
+ require 'graphviz'
4
+
5
+ Puppet.initialize_settings
6
+
7
+ Puppet[:code] = <<-EOF
8
+
9
+ notify { 'hello there':
10
+ require => File['/tmp/foo'],
11
+ }
12
+ file { '/tmp/foo':
13
+ ensure => file,
14
+ }
15
+ notify { 'goodbye':
16
+ require => File['/tmp/nope'],
17
+ }
18
+
19
+ file{'/tmp/nope':}
20
+
21
+ poodles{'oogles': }
22
+
23
+ define poodles {}
24
+ EOF
25
+
26
+
27
+ node = Puppet::Node.indirection.find(Puppet[:node_name_value])
28
+ catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node).to_ral
29
+
30
+ #catalog.vertices.each { |v| catalog.remove_vertex! v if v.is_a? Puppet::Type::Stage }
31
+ #catalog.vertices.each { |v| catalog.remove_vertex! v if v.is_a? Puppet::Type::Component }
32
+
33
+ graph = catalog.relationship_graph.to_dot
34
+
35
+ GraphViz.parse_string(graph) do |graph|
36
+ graph[:label] = 'Resource Relationships'
37
+
38
+ w = graph.get_node('Whit[Admissible_class[Main]]')
39
+ w[:label] = 'Start'
40
+
41
+ w = graph.get_node('Whit[Completed_class[Main]]')
42
+ w[:label] = 'Finish'
43
+
44
+ end.output(:svg => "sample.svg")
@@ -10,6 +10,8 @@ options = {
10
10
  :host => '0.0.0.0',
11
11
  :bind => '0.0.0.0',
12
12
  :root => gemroot,
13
+ :csrf => false,
14
+ :graph => false,
13
15
  }
14
16
  logfile = $stderr
15
17
  loglevel = Logger::WARN
@@ -45,6 +47,14 @@ optparse = OptionParser.new { |opts|
45
47
  options[:root] = arg
46
48
  end
47
49
 
50
+ opts.on("-x", "--csrf", "Protect from cross site request forgery. Requires code to be submitted for validation via the webpage.") do
51
+ options[:csrf] = true
52
+ end
53
+
54
+ opts.on("-g", "--graph", "Generate relationship graphs from validated code. Requires `graphviz` to be installed.") do
55
+ options[:graph] = true
56
+ end
57
+
48
58
  opts.separator('')
49
59
 
50
60
  opts.on("-h", "--help", "Displays this help") do
data/config.ru CHANGED
@@ -7,6 +7,21 @@ logger.level = Logger::WARN
7
7
  PuppetValidator.set :puppet_versions, Dir.glob('*').select {|f| File.symlink? f and File.readlink(f) == '.' }
8
8
  PuppetValidator.set :root, File.dirname(__FILE__)
9
9
  PuppetValidator.set :logger, logger
10
+
11
+ # List out the lint checks you want disabled. By default, this will enable
12
+ # all installed checks. puppet-lint --help will list known checks.
13
+ #
10
14
  PuppetValidator.set :disabled_lint_checks, ['80chars']
11
15
 
16
+ # Protect from cross site request forgery. With this set, code may be
17
+ # submitted for validation by the website only.
18
+ #
19
+ # Note: This will currently break multiple version validation.
20
+ PuppetValidator.set :csrf, false
21
+
22
+ # Provide the option to generate relationship graphs from validated code.
23
+ # This requires that the `graphviz` package be installed.
24
+ #
25
+ PuppetValidator.set :graph, false
26
+
12
27
  run PuppetValidator
@@ -5,6 +5,7 @@ require 'puppet'
5
5
  require 'puppet/parser'
6
6
  require 'puppet-lint'
7
7
 
8
+ require 'graphviz'
8
9
  require 'nokogiri'
9
10
  require 'cgi'
10
11
 
@@ -21,17 +22,19 @@ class PuppetValidator < Sinatra::Base
21
22
  before do
22
23
  env["rack.logger"] = settings.logger if settings.logger
23
24
 
24
- session[:csrf] ||= SecureRandom.hex(32)
25
- response.set_cookie 'authenticity_token', {
26
- :value => session[:csrf],
27
- :expires => Time.now + (60 * 60 * 24),
28
- }
25
+ if settings.csrf
26
+ session[:csrf] ||= SecureRandom.hex(32)
27
+ response.set_cookie 'authenticity_token', {
28
+ :value => session[:csrf],
29
+ :expires => Time.now + (60 * 60 * 24),
30
+ }
31
+ end
29
32
  end
30
33
 
31
34
  def initialize(app=nil)
32
35
  super(app)
33
36
 
34
- Puppet.initialize_settings if Puppet.version.to_i == 3 and Puppet.settings[:confdir].nil?
37
+ Puppet.initialize_settings rescue nil
35
38
 
36
39
  # there must be a better way
37
40
  if settings.respond_to? :disabled_lint_checks
@@ -114,6 +117,7 @@ class PuppetValidator < Sinatra::Base
114
117
  acc.merge({item[:line] => "#{item[:kind].upcase}: #{item[:message]}"})
115
118
  end.to_json
116
119
 
120
+ @relationships = rendered_dot(@code) if params['relationships'] == 'on'
117
121
  else
118
122
  @message = "Submitted code size is #{request.body.size}, which is larger than the maximum size of #{MAXSIZE}."
119
123
  @status = :fail
@@ -130,6 +134,7 @@ class PuppetValidator < Sinatra::Base
130
134
  helpers do
131
135
 
132
136
  def safe?
137
+ return true unless settings.csrf
133
138
  if session[:csrf] == params['_csrf'] && session[:csrf] == request.cookies['authenticity_token']
134
139
  true
135
140
  else
@@ -198,5 +203,40 @@ class PuppetValidator < Sinatra::Base
198
203
  PuppetLint.configuration.checks.map {|check| check.to_s}
199
204
  end
200
205
 
206
+ def rendered_dot(code)
207
+ return unless settings.graph
208
+
209
+ begin
210
+ Puppet::Node::Facts.indirection.terminus_class = :memory
211
+ Puppet::Node.indirection.cache_class = nil
212
+ node = Puppet::Node.indirection.find(Puppet[:node_name_value])
213
+ catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
214
+
215
+ catalog.remove_resource(catalog.resource("Stage", :main))
216
+ catalog.remove_resource(catalog.resource("Class", :settings))
217
+
218
+ graph = catalog.to_ral.relationship_graph.to_dot
219
+
220
+ svg = GraphViz.parse_string(graph) do |graph|
221
+ graph[:label] = 'Resource Relationships'
222
+
223
+ graph.each_node do |name, node|
224
+ next unless name.start_with? 'Whit'
225
+ newname = name.dup
226
+ newname.sub!('Admissible_class', 'Starting Class')
227
+ newname.sub!('Completed_class', 'Finishing Class')
228
+ node[:label] = newname[5..-2]
229
+ end
230
+ end.output(:svg => String)
231
+
232
+ rescue => detail
233
+ logger.warn detail.message
234
+ logger.debug detail.backtrace.join "\n"
235
+ return { :status => false, :message => detail.message }
236
+ end
237
+
238
+ { :status => true, :data => svg }
239
+ end
240
+
201
241
  end
202
242
  end
@@ -0,0 +1,59 @@
1
+ <html>
2
+ <head>
3
+ <title>Puppet Validator</title>
4
+ <link rel="stylesheet" href="styles.css">
5
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
6
+ </head>
7
+ <body>
8
+ <a href="https://github.com/puppetlabs/puppet-validator"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
9
+ <h1>Interpreting Relationship Graphs</h1>
10
+ <p>
11
+ This tool will compile a catalog, using <em>only the information you provide</em>.
12
+ This means that all facts will be empty, and custom resources types, such as the
13
+ <em>mysql_grant</em> type (installed via the <code>puppetlabs/mysql</code> module)
14
+ will cause an <code>unknown resource type</code> failure.
15
+ </p>
16
+ <p>
17
+ If you need to test code that depends on fact values you should define a variable
18
+ with the same name at the top of your code.
19
+ </p>
20
+ <div class="results">
21
+ <pre><code># fake fact(s)
22
+ $osfamily = 'RedHat'
23
+
24
+ case $osfamily {
25
+ 'RedHat': { notify { 'This is a RedHat family node': } }
26
+ default : { notify { "I don't know what ${osfamily} is.": } }
27
+ }</code></pre>
28
+ </div>
29
+ <p>
30
+ If you are testing a class, you will need to <code>include</code> it or it won't
31
+ appear in the catalog.
32
+ </p>
33
+ <div class="results">
34
+ <pre><code>class silly {
35
+ file { '/tmp/foo':
36
+ ensure => file,
37
+ before => Notify['hi'],
38
+ }
39
+ notify { 'hi': }
40
+ }
41
+
42
+ #include the class in the rendered catalog.
43
+ include silly</code></pre>
44
+ </div>
45
+ <p>
46
+ Any class or type you use or set a relationship on <strong>must be defined</strong>,
47
+ or Puppet won't be able to autoload it.
48
+ <p>
49
+ <div class="results">
50
+ <pre><code>include apache
51
+
52
+ class apache {
53
+ # a stub class so the catalog will compile.
54
+ }</code></pre>
55
+ </div>
56
+
57
+ <div class="links"><a href="javascript:history.back();">Try Again</a> &bull; <a href="/">Validate More Code</a></div>
58
+ </body>
59
+ </html>
@@ -40,7 +40,8 @@ div.line-highlight.with-tooltip {
40
40
  display: block;
41
41
  }
42
42
 
43
- .results {
43
+ .results,
44
+ .warning {
44
45
  width: 65%;
45
46
  border: 1px solid black;
46
47
  border-radius: 0.25em;
@@ -83,6 +84,13 @@ div.line-highlight.with-tooltip {
83
84
  padding-right: 0.5em;
84
85
  }
85
86
 
87
+ .relationships {
88
+ text-align: center;
89
+ }
90
+ .warning {
91
+ background-color: #ffb6b6;
92
+ }
93
+
86
94
  div.links {
87
95
  margin: 1em auto;
88
96
  padding: 0.5em;
@@ -101,9 +109,13 @@ div.entry {
101
109
  width: 100%;
102
110
  font-family: monospace;
103
111
  }
112
+ div.row {
113
+ text-align: center;
114
+ }
104
115
 
105
116
  input#validate,
106
- select#versions {
117
+ select#versions,
118
+ a#customize {
107
119
  margin-right: 2em;
108
120
  }
109
121
 
@@ -135,7 +147,7 @@ fieldset.menu ul {
135
147
  column-gap: 20px;
136
148
  }
137
149
 
138
- p.info {
150
+ .info {
139
151
  background: #fefefe url('info.png') no-repeat 5px 5px !important;
140
152
  border: 1px solid #efefef;
141
153
  padding: 0.5em;
@@ -147,9 +159,6 @@ p.info {
147
159
  -khtml-border-radius: 0.5em;
148
160
  border-radius: 0.5em;
149
161
  }
150
- p.info:first-line {
151
- font-weight: bold;
152
- }
153
162
 
154
163
  /* jquery UI overrides */
155
164
  .ui-widget {
@@ -10,19 +10,24 @@
10
10
  <h1>Puppet Code Validator</h1>
11
11
  <p>Paste Puppet code into the following textbox and check it for validity.</p>
12
12
  <form action="/validate" method="post">
13
- <input name="_csrf", type="hidden" value="<%= session[:csrf] %>" />
13
+ <% if settings.csrf %><input name="_csrf", type="hidden" value="<%= session[:csrf] %>" /><% end %>
14
14
  <div class="entry">
15
15
  <textarea name="code" id="code" cols="65" rows="25"></textarea>
16
- <input type="submit" value="Validate" id="validate">
17
- Puppet version
18
- <select id="versions">
19
- <% version = @versions.shift %><option value='/'><%= version %></option>
20
- <% @versions.each do |version| %>
21
- <option><%= version %></option>
22
- <% end %>
23
- </select>
24
- <label><input type="checkbox" name="lint" id="lint" onchange="toggleChecks();">Include <code>puppet-lint</code> style checks.</label>
25
- <a class="button" href="javascript:toggleMenu();">customize &#9662;</a>
16
+ <div class="row">
17
+ <input type="submit" value="Validate" id="validate">
18
+ </div>
19
+ <div class="row">
20
+ Puppet version
21
+ <select id="versions">
22
+ <% version = @versions.shift %><option value='/'><%= version %></option>
23
+ <% @versions.each do |version| %>
24
+ <option><%= version %></option>
25
+ <% end %>
26
+ </select>
27
+ <label><input type="checkbox" name="lint" id="lint" onchange="toggleChecks();">Include <code>puppet-lint</code> style checks.</label>
28
+ <a id="customize" class="button" href="javascript:toggleMenu();">customize &#9662;</a>
29
+ <% if settings.graph %><label><input type="checkbox" name="relationships" id="relationships">Show relationships.</label><% end %>
30
+ </div>
26
31
  <fieldset id="checks-menu" class="menu">
27
32
  <legend>Enabled Lint Checks</legend>
28
33
  <ul id="checks">
@@ -35,11 +40,19 @@
35
40
  </fieldset>
36
41
  </div>
37
42
  </form>
38
- <p class="info">
39
- Be aware that this <em>only validates syntax</em>. It will not compile or
40
- enforce a catalog, which means that there are many mistakes that it cannot
41
- identify. See <a href="/testing.html">more information on testing and
42
- validating Puppet code.</a>
43
- </p>
43
+ <div class="info">
44
+ <p><strong>Be aware that valid syntax does not mean you have valid code.</strong></p>
45
+ <p>
46
+ The validator <em>only validates syntax</em> and doesn't actually compile or enforce
47
+ a catalog. This means that there are many mistakes that it will not identify. See
48
+ <a href="/testing.html">more information</a> on testing and validating Puppet code.
49
+ </p>
50
+ <p>
51
+ If you show relationships, then we'll try to compile an abbreviated catalog. This
52
+ won't use any facts, and won't know how to compile non-core resource types. See
53
+ <a href="/relationships.html">more information</a> on building a self-contained
54
+ catalog in order to generate a dependency graph.
55
+ </p>
56
+ </div>
44
57
  </body>
45
58
  </html>
@@ -36,12 +36,27 @@
36
36
  <% end %>
37
37
  </fieldset>
38
38
  <pre data-line='<%= @highlights %>' class="line-numbers language-puppet"><code id="code" class="line-numbers language-puppet"><%= @code %></code></pre>
39
+ <% if @relationships %>
40
+ <% if @relationships[:status] %>
41
+ <div class="relationships"><%= @relationships[:data] %></div>
42
+ <% else %>
43
+ <div class="warning"><%= @relationships[:message] %></div>
44
+ <% end %>
45
+ <% end %>
39
46
  <div class="links"><a href="javascript:history.back();">Try Again</a> &bull; <a href="/">Validate More Code</a></div>
40
- <p class="info">
41
- Be aware that this has <em>only validated syntax</em>. It will not compile or
42
- enforce a catalog, which means that there are many mistakes that it cannot
43
- identify. See <a href="/testing.html">more information on testing and
44
- validating Puppet code.</a>
45
- </p>
47
+ <div class="info">
48
+ <p><strong>Be aware that valid syntax does not mean you have valid code.</strong></p>
49
+ <p>
50
+ The validator <em>only validates syntax</em> and doesn't actually compile or enforce
51
+ a catalog. This means that there are many mistakes that it will not identify. See
52
+ <a href="/testing.html">more information</a> on testing and validating Puppet code.
53
+ </p>
54
+ <p>
55
+ If you show relationships, then we'll try to compile an abbreviated catalog. This
56
+ won't use any facts, and won't know how to compile non-core resource types. See
57
+ <a href="/relationships.html">more information</a> on building a self-contained
58
+ catalog in order to generate a dependency graph.
59
+ </p>
60
+ </div>
46
61
  </body>
47
62
  </html>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Ford
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-02 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: 1.6.5
81
+ - !ruby/object:Gem::Dependency
82
+ name: ruby-graphviz
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.2'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '1.2'
81
95
  description: |2
82
96
  Puppet Validator is a simple web service that accepts arbitrary code submissions and
83
97
  validates it the way `puppet parser validate` would. It can optionally also
@@ -88,20 +102,22 @@ executables:
88
102
  extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
- - LICENSE
92
105
  - README.md
93
- - bin/puppet-validator
106
+ - LICENSE
94
107
  - config.ru
108
+ - bin/puppet-validator
95
109
  - lib/puppet-validator.rb
110
+ - bin/grapher
111
+ - views/index.erb
112
+ - views/result.erb
96
113
  - public/gist.png
97
114
  - public/info.png
98
115
  - public/prism-default.css
99
116
  - public/prism.js
117
+ - public/relationships.html
100
118
  - public/scripts.js
101
119
  - public/styles.css
102
120
  - public/testing.html
103
- - views/index.erb
104
- - views/result.erb
105
121
  homepage: https://github.com/puppetlabs/puppet-validator/
106
122
  licenses:
107
123
  - Apache-2.0
@@ -122,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
138
  version: '0'
123
139
  requirements: []
124
140
  rubyforge_project:
125
- rubygems_version: 2.6.10
141
+ rubygems_version: 2.0.14.1
126
142
  signing_key:
127
143
  specification_version: 4
128
144
  summary: Puppet code validator as a service