Skip to content

Commit 33f1bac

Browse files
committed
Merge branch 'CHEF-677' of git://github.com/nuoyan/chef into nuoyan/CHEF-677
2 parents ddf93bc + abb1b8e commit 33f1bac

18 files changed

Lines changed: 268 additions & 84 deletions

File tree

chef-server-api/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec = Gem::Specification.new do |s|
3636
"haml",
3737
"ruby-openid",
3838
"json",
39-
"syntax",].each { |g| s.add_dependency g}
39+
"coderay",].each { |g| s.add_dependency g}
4040

4141
s.require_path = 'lib'
4242
s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{config,lib,spec,app,public,stubs}/**/*")
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#
2+
# Author:: Nuo Yan (<nuo@opscode.com>)
3+
# Copyright:: Copyright (c) 2009 Opscode, 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 File.join("chef", "webui_user")
20+
21+
class ChefServerApi::Users < ChefServerApi::Application
22+
provides :json
23+
24+
before :authenticate_every
25+
26+
# GET to /users
27+
def index
28+
@user_list = Chef::WebUIUser.cdb_list
29+
display(@user_list.inject({}) { |r,n| r[n] = absolute_slice_url(:user, n); r })
30+
end
31+
32+
# GET to /users/:id
33+
def show
34+
begin
35+
@user = Chef::WebUIUser.cdb_load(params[:id])
36+
rescue Chef::Exceptions::CouchDBNotFound => e
37+
raise NotFound, "Cannot load user #{params[:id]}"
38+
end
39+
display @user
40+
end
41+
42+
# PUT to /users/:id
43+
def update
44+
begin
45+
Chef::WebUIUser.cdb_load(params[:id])
46+
rescue Chef::Exceptions::CouchDBNotFound => e
47+
raise NotFound, "Cannot load user #{params[:id]}"
48+
end
49+
@user = params['inflated_object']
50+
@user.cdb_save
51+
display(@user)
52+
end
53+
54+
# POST to /users
55+
def create
56+
@user = params["inflated_object"]
57+
begin
58+
Chef::WebUIUser.cdb_load(@user.name)
59+
rescue Chef::Exceptions::CouchDBNotFound
60+
@user.cdb_save
61+
self.status = 201
62+
else
63+
raise Forbidden, "User already exists"
64+
end
65+
display({ :uri => absolute_slice_url(:user, @user.name) })
66+
end
67+
68+
def destroy
69+
begin
70+
@user = Chef::WebUIUser.cdb_load(params[:id])
71+
rescue Chef::Exceptions::CouchDBNotFound => e
72+
raise NotFound, "Cannot load user #{params[:id]}"
73+
end
74+
@user.cdb_destroy
75+
display @user
76+
end
77+
end

chef-server-api/lib/chef-server-api.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,6 @@ def self.activate
7777

7878
Chef::Log.info('Loading roles')
7979
Chef::Role.sync_from_disk_to_couchdb
80-
81-
# Create the default WebUI admin user "admin" if not already exists
82-
begin
83-
user = Chef::WebUIUser.load(Chef::Config[:web_ui_admin_user_name])
84-
rescue Chef::Exceptions::CouchDBNotFound => e
85-
user = Chef::WebUIUser.new
86-
user.name = Chef::Config[:web_ui_admin_user_name]
87-
user.set_password(Chef::Config[:web_ui_admin_default_password])
88-
user.admin = true
89-
user.save
90-
end
9180

9281
# Create the signing key and certificate
9382
Chef::Certificate.generate_signing_ca
@@ -115,6 +104,9 @@ def self.deactivate
115104
# @note prefix your named routes with :chefserverslice_
116105
# to avoid potential conflicts with global named routes.
117106
def self.setup_router(scope)
107+
# Users
108+
scope.resources :users
109+
118110
# Nodes
119111
scope.match('/nodes/:id/cookbooks', :method => 'get').to(:controller => "nodes", :action => "cookbooks")
120112
scope.resources :nodes

chef-server-webui/app/controllers/application.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ def logout_and_redirect_to_login
8989
redirect(slice_url(:users_login), {:message => { :error => $! }, :permanent => true})
9090
end
9191

92+
93+
def is_admin(name)
94+
user = Chef::WebUIUser.load(name)
95+
return user.admin
96+
end
97+
98+
#return true if there is only one admin left, false otehrwise
99+
def is_last_admin
100+
count = 0
101+
users = Chef::WebUIUser.list
102+
users.each do |u, url|
103+
user = Chef::WebUIUser.load(u)
104+
if user.admin
105+
count = count + 1
106+
return false if count == 2
107+
end
108+
end
109+
true
110+
end
111+
112+
#whether or not the user should be able to edit a user's admin status
113+
def edit_admin
114+
is_admin(params[:user_id]) ? (!is_last_admin) : true
115+
end
116+
92117
def authorized_node
93118
# if session[:level] == :admin
94119
# Chef::Log.debug("Authorized as Administrator")

chef-server-webui/app/controllers/databag_items.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,25 @@ def index
7878
end
7979

8080
def show
81-
@databag_name = params[:databag_id]
82-
@databag_item_name = params[:id]
83-
r = Chef::REST.new(Chef::Config[:chef_server_url])
84-
@databag_item = r.get_rest("data/#{params[:databag_id]}/#{params[:id]}")
85-
display @databag_item
81+
begin
82+
@databag_name = params[:databag_id]
83+
@databag_item_name = params[:id]
84+
r = Chef::REST.new(Chef::Config[:chef_server_url])
85+
@databag_item = r.get_rest("data/#{params[:databag_id]}/#{params[:id]}")
86+
display @databag_item
87+
rescue
88+
redirect(slice_url(:databag_databag_items), {:message => { :error => $! }, :permanent => true})
89+
end
8690
end
8791

8892
def destroy(databag_id=params[:databag_id], item_id=params[:id])
89-
@databag_item = Chef::DataBagItem.new
90-
@databag_item.destroy(databag_id, item_id)
91-
redirect(slice_url(:databag_databag_items), {:message => { :notice => "Databag Item #{params[:id]} deleted successfully" }, :permanent => true})
93+
begin
94+
@databag_item = Chef::DataBagItem.new
95+
@databag_item.destroy(databag_id, item_id)
96+
redirect(slice_url(:databag_databag_items), {:message => { :notice => "Databag Item #{params[:id]} deleted successfully" }, :permanent => true})
97+
rescue
98+
redirect(slice_url(:databag_databag_items), {:message => { :error => $! }, :permanent => true})
99+
end
92100
end
93101

94102
end

chef-server-webui/app/controllers/databags.rb

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,41 @@ def create
4141
end
4242

4343
def index
44-
r = Chef::REST.new(Chef::Config[:chef_server_url])
45-
@databags = r.get_rest("data")
46-
render
44+
begin
45+
r = Chef::REST.new(Chef::Config[:chef_server_url])
46+
@databags = r.get_rest("data")
47+
render
48+
rescue
49+
@_message = { :error => $! }
50+
@databags = {}
51+
render
52+
end
4753
end
4854

4955
def show
50-
@databag_name = params[:id]
51-
r = Chef::REST.new(Chef::Config[:chef_server_url])
52-
@databag = r.get_rest("data/#{params[:id]}")
53-
raise NotFound unless @databag
54-
display @databag
56+
begin
57+
@databag_name = params[:id]
58+
r = Chef::REST.new(Chef::Config[:chef_server_url])
59+
@databag = r.get_rest("data/#{params[:id]}")
60+
raise NotFound unless @databag
61+
display @databag
62+
rescue
63+
@databags = Chef::DataBag.list
64+
@_message = { :error => $!}
65+
render :index
66+
end
67+
end
68+
69+
def destroy
70+
begin
71+
r = Chef::REST.new(Chef::Config[:chef_server_url])
72+
r.delete_rest("data/#{params[:id]}")
73+
redirect(absolute_slice_url(:databags), {:message => { :notice => "Data bag #{params[:id]} deleted successfully" }, :permanent => true})
74+
rescue
75+
@databags = Chef::DataBag.list
76+
@_message = { :error => $!}
77+
render :index
78+
end
5579
end
5680

5781
end

chef-server-webui/app/controllers/nodes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def create
6767
@node = Chef::Node.new
6868
@node.name params[:name]
6969
@node.attribute = JSON.parse(params[:attributes])
70-
@node.run_list params[:for_node]
70+
@node.run_list.reset(params[:for_node] ? params[:for_node] : [])
7171
begin
7272
@node.create
7373
rescue Net::HTTPServerException => e

chef-server-webui/app/controllers/openid_consumer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def complete
8686
users = Chef::WebUIUser.list
8787
#TODO: This is expensive. Should think of a better way [nuo]
8888
# Go through each user object and check if the current OpenID associates with the user
89-
users.each do |u|
89+
users.each do |u, url|
9090
user = Chef::WebUIUser.load(u)
9191
if user.openid == oidresp.identity_url
9292
session[:user] = user.name

chef-server-webui/app/controllers/users.rb

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def show
5252
raise Forbidden, "The current user is not an Administrator, you can only Show and Edit the user itself. To control other users, login as an Administrator." unless params[:user_id] == session[:user] unless session[:level] == :admin
5353
begin
5454
@user = Chef::WebUIUser.load(params[:user_id])
55-
rescue Chef::Exceptions::CouchDBNotFound => e
56-
raise NotFound, "Cannot found user #{params[:user_id]}"
55+
rescue Net::HTTPServerException => e
56+
raise NotFound, "Cannot find user #{params[:user_id]}"
5757
end
5858
render
5959
rescue
@@ -66,12 +66,12 @@ def update
6666
begin
6767
begin
6868
@user = Chef::WebUIUser.load(params[:user_id])
69-
rescue Chef::Exceptions::CouchDBNotFound => e
70-
raise NotFound, "Cannot found user #{params[:user_id]}"
69+
rescue Net::HTTPServerException => e
70+
raise NotFound, "Cannot find user #{params[:user_id]}"
7171
end
72-
@user.admin = str_to_bool(params[:admin]) unless params[:user_id] == Chef::Config[:web_ui_admin_user_name]
73-
raise Forbidden, "Passwords do not match." unless params[:new_password] == params[:confirm_new_password]
74-
@user.set_password(params[:new_password]) unless (params[:new_password].nil? || params[:new_password].length == 0)
72+
@user.admin = str_to_bool(params[:admin]) if ['true','false'].include?params[:admin]
73+
session[:level] = :user if params[:user_id] == session[:user] && params[:admin] == 'false'
74+
@user.set_password(params[:new_password], params[:confirm_new_password]) unless (params[:new_password].nil? || params[:new_password].length == 0)
7575
(params[:openid].length == 0 || params[:openid].nil?) ? @user.set_openid(nil) : @user.set_openid(URI.parse(params[:openid]).normalize.to_s)
7676
@user.save
7777
@_message = { :notice => "Updated User #{@user.name}" }
@@ -95,22 +95,21 @@ def new
9595
def create
9696
begin
9797
authorized_user
98-
begin
99-
@user = Chef::WebUIUser.load(params[:name])
100-
rescue Chef::Exceptions::CouchDBNotFound
101-
raise ArgumentError, "Password cannot be blank" if (params[:password].nil? || params[:password].length==0)
102-
raise ArgumentError, "Passwords do not match" unless params[:password] == params[:password2]
103-
@user = Chef::WebUIUser.new
104-
else
105-
raise StandardError, "User #{params[:username]} already exists"
106-
end
107-
98+
@user = Chef::WebUIUser.new
10899
@user.name = params[:name]
109-
@user.set_password(params[:password])
100+
@user.set_password(params[:password], params[:password2])
110101
@user.admin = true if params[:admin]
111102
(params[:openid].length == 0 || params[:openid].nil?) ? @user.set_openid(nil) : @user.set_openid(URI.parse(params[:openid]).normalize.to_s)
112-
@user.save
113-
redirect(slice_url(:users), :message => { :notice => "Created User #{@user.name}" })
103+
begin
104+
@user.create
105+
rescue Net::HTTPServerException => e
106+
if e.message =~ /403/
107+
raise ArgumentError, "User already exists"
108+
else
109+
raise e
110+
end
111+
end
112+
redirect(slice_url(:users), :message => { :notice => "Created User #{params[:name]}" })
114113
rescue
115114
@_message = { :error => $! }
116115
session[:level] != :admin ? set_user_and_redirect : (render :new)
@@ -126,7 +125,7 @@ def login_exec
126125
begin
127126
begin
128127
@user = Chef::WebUIUser.load(params[:name])
129-
rescue Chef::Exceptions::CouchDBNotFound
128+
rescue Net::HTTPServerException => e
130129
raise NotFound, "Cannot find user #{params[:name]}"
131130
end
132131
raise(Unauthorized, "Wrong username or password.") unless @user.verify_password(params[:password])
@@ -141,7 +140,7 @@ def login_exec
141140
def complete
142141
session[:user] = params[:name]
143142
session[:level] = (@user.admin == true ? :admin : :user)
144-
@user.verify_password(Chef::Config[:web_ui_admin_default_password]) ? redirect(slice_url(:users_edit, :user_id => @user.name), :message => { :warning => "Please change default password!!!" }) : redirect_back_or_default(absolute_slice_url(:nodes))
143+
(@user.name == Chef::Config[:web_ui_admin_user_name] && @user.verify_password(Chef::Config[:web_ui_admin_default_password])) ? redirect(slice_url(:users_edit, :user_id => @user.name), :message => { :warning => "Please change default password!!!" }) : redirect_back_or_default(absolute_slice_url(:nodes))
145144
end
146145

147146
def logout
@@ -151,7 +150,7 @@ def logout
151150

152151
def destroy
153152
begin
154-
raise Forbidden, "The default admin user cannot be deleted" if params[:user_id] == Chef::Config[:web_ui_admin_user_name]
153+
raise Forbidden, "The last admin user cannot be deleted" if (is_admin(params[:user_id]) && is_last_admin)
155154
raise Forbidden, "A non-admin user can only delete itself" if (params[:user_id] != session[:user] && session[:level] != :admin)
156155
@user = Chef::WebUIUser.load(params[:user_id])
157156
@user.destroy

chef-server-webui/app/helpers/application_helper.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,8 @@ def append_tree(name, html, node, count, parent, override)
115115
end
116116

117117
def syntax_highlight(code)
118-
converter = Syntax::Convertors::HTML.for_syntax "ruby"
119-
if File.exists?(code)
120-
converter.convert(File.read(code), false)
121-
else
122-
converter.convert(code, false)
123-
end
118+
tokens = File.exists?(code) ? CodeRay.scan_file(code, :ruby) : CodeRay.scan(code, :ruby)
119+
CodeRay.encode_tokens(tokens, :span)
124120
end
125121

126122
def get_file(uri)

0 commit comments

Comments
 (0)