Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply cookstyle to the cookbooks #1852

Merged
merged 2 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ manifest.json
chef-server-dependency-licenses.json

license-cache

# don't commit berks lock files
Berksfile.lock
1 change: 0 additions & 1 deletion omnibus/files/private-chef-cookbooks/Berksfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ source 'https://supermarket.chef.io'

cookbook 'enterprise', git: 'https://github.com/chef-cookbooks/enterprise-chef-common', tag: 'v0.15.1'
cookbook 'private-chef', path: './private-chef'
cookbook 'runit' # we use the version locked in enterprise-chef-common
6 changes: 3 additions & 3 deletions omnibus/files/private-chef-cookbooks/private-chef/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"
source 'https://rubygems.org'

# Used for rspec testing only
gem "chefspec"
gem "veil", git: "https://github.com/chef/chef_secrets.git"
gem 'chefspec'
gem 'veil', git: 'https://github.com/chef/chef_secrets.git'
332 changes: 165 additions & 167 deletions omnibus/files/private-chef-cookbooks/private-chef/attributes/default.rb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'chef/http'

module ChefBackend
ETCD_MEMBERS_URL = "/v2/members"
ETCD_MEMBERS_URL = '/v2/members'.freeze
def self.configured_members(node)
ret = {}
node['private_chef']['chef_backend_members'].each_with_index do |member, i|
Expand All @@ -14,9 +14,9 @@ def self.configured_members(node)

def self.etcd_members(ip, port)
ret = {}
raw_members = JSON.parse(etcd_get(ETCD_MEMBERS_URL, ip, port))["members"]
raw_members = JSON.parse(etcd_get(ETCD_MEMBERS_URL, ip, port))['members']
raw_members.each do |m|
ret[m["name"]] = URI.parse(m["peerURLs"].first).host
ret[m['name']] = URI.parse(m['peerURLs'].first).host
end
ret
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
require 'json'

class ChefServerDataBootstrap

GLOBAL_ORG_ID = "00000000000000000000000000000000"
GLOBAL_ORG_ID = '00000000000000000000000000000000'.freeze
attr_reader :bifrost, :superuser_authz_id, :bootstrap_time, :node, :server_admins_authz_id

def initialize(node)
Expand All @@ -28,9 +27,8 @@ def initialize(node)
@bifrost = node['private_chef']['oc_bifrost']
end


def bifrost_superuser_id
@superuser_id ||= PrivateChef.credentials.get('oc_bifrost', 'superuser_id')
@superuser_id ||= PrivateChef.credentials.get('oc_bifrost', 'superuser_id')
end

def bootstrap
Expand All @@ -52,46 +50,46 @@ def bootstrap
username = node['private_chef']['opscode-erchef']['sql_user']
password = PrivateChef.credentials.get('opscode_erchef', 'sql_password')
EcPostgres.with_connection(node, 'opscode_chef',
'db_superuser' => username,
'db_superuser_password' => password) do |conn|
create_superuser_in_erchef(conn)
create_server_admins_global_group_in_erchef(conn)
create_global_container_in_erchef(conn, 'organizations', orgs_authz_id)
create_global_container_in_erchef(conn, 'users', users_authz_id)
end
'db_superuser' => username,
'db_superuser_password' => password) do |conn|
create_superuser_in_erchef(conn)
create_server_admins_global_group_in_erchef(conn)
create_global_container_in_erchef(conn, 'organizations', orgs_authz_id)
create_global_container_in_erchef(conn, 'users', users_authz_id)
end
end

private

# Create and set up permissions for the server admins group.
def create_server_admins_global_group_in_bifrost(users_authz_id)
@server_admins_authz_id = create_group_in_authz(bifrost_superuser_id)
%w{create read update delete}.each do |permission|
%w(create read update delete).each do |permission|
# grant server admins group permission on the users container,
# as the erchef superuser.
grant_authz_object_permission(permission, "groups", "containers", users_authz_id,
server_admins_authz_id, superuser_authz_id)
grant_authz_object_permission(permission, 'groups', 'containers', users_authz_id,
server_admins_authz_id, superuser_authz_id)
# grant superuser actor permissions on the server admin group,
# as the bifrost superuser
grant_authz_object_permission(permission, "actors", "groups", server_admins_authz_id,
superuser_authz_id, bifrost_superuser_id)
grant_authz_object_permission(permission, 'actors', 'groups', server_admins_authz_id,
superuser_authz_id, bifrost_superuser_id)
end

# Grant server-admins read permissions on itself as the bifrost superuser.
grant_authz_object_permission("read", "groups", "groups", server_admins_authz_id,
server_admins_authz_id, bifrost_superuser_id)
grant_authz_object_permission('read', 'groups', 'groups', server_admins_authz_id,
server_admins_authz_id, bifrost_superuser_id)
end

# Insert the server admins global group into the erchef groups table.
def create_server_admins_global_group_in_erchef(conn)
simple_insert(conn, 'groups',
id: SecureRandom.uuid.gsub("-", ""),
org_id: GLOBAL_ORG_ID,
authz_id: server_admins_authz_id,
name: 'server-admins',
last_updated_by: superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time)
id: SecureRandom.uuid.gsub('-', ''),
org_id: GLOBAL_ORG_ID,
authz_id: server_admins_authz_id,
name: 'server-admins',
last_updated_by: superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time)
end

# insert the erchef superuser's key into the erchef keys table,
Expand All @@ -102,75 +100,74 @@ def create_superuser_in_erchef(conn)
raw_key = PrivateChef.credentials.get('chef-server', 'superuser_key')
public_key = OpenSSL::PKey::RSA.new(raw_key).public_key.to_s

user_id = SecureRandom.uuid.gsub("-", "")
user_id = SecureRandom.uuid.gsub('-', '')
simple_insert(conn, 'keys',
id: user_id,
key_name: 'default',
public_key: public_key,
key_version: 0,
created_at: bootstrap_time,
expires_at: "infinity")
id: user_id,
key_name: 'default',
public_key: public_key,
key_version: 0,
created_at: bootstrap_time,
expires_at: 'infinity')

simple_insert(conn, 'users',
id: user_id,
username: 'pivotal',
email: '[email protected]',
authz_id: @superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time,
last_updated_by: bifrost_superuser_id,
pubkey_version: 0, # Old constrant requires it to be not-null
serialized_object: JSON.generate(
first_name: "Chef",
last_name: "Server",
display_name: "Chef Server Superuser"))
id: user_id,
username: 'pivotal',
email: '[email protected]',
authz_id: @superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time,
last_updated_by: bifrost_superuser_id,
pubkey_version: 0, # Old constrant requires it to be not-null
serialized_object: JSON.generate(
first_name: 'Chef',
last_name: 'Server',
display_name: 'Chef Server Superuser'
))
end

def create_global_container_in_erchef(conn, name, authz_id)
simple_insert(conn, 'containers',
id: authz_id, # TODO is this right?
name: name,
authz_id: authz_id,
org_id: GLOBAL_ORG_ID,
last_updated_by: superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time)
id: authz_id, # TODO: is this right?
name: name,
authz_id: authz_id,
org_id: GLOBAL_ORG_ID,
last_updated_by: superuser_authz_id,
created_at: bootstrap_time,
updated_at: bootstrap_time)
end

# db helper to construct and execute a simple insert statement
def simple_insert(conn, table, fields)
placeholders = []
1.upto(fields.length) { |x| placeholders << "$#{x}" }
placeholders.join(", ")
conn.exec_params("INSERT INTO #{table} (#{fields.keys.join(", ")}) VALUES (#{placeholders.join(", ")})",
fields.values) # confirm ordering


placeholders.join(', ')
conn.exec_params("INSERT INTO #{table} (#{fields.keys.join(', ')}) VALUES (#{placeholders.join(', ')})",
fields.values) # confirm ordering
end

## Bifrost access helpers.

def create_group_in_authz(requestor_id)
create_object_in_authz("groups", requestor_id)
create_object_in_authz('groups', requestor_id)
end

def create_actor_in_authz(requestor_id)
create_object_in_authz("actors", requestor_id)
create_object_in_authz('actors', requestor_id)
end

def create_container_in_authz(requestor_id)
create_object_in_authz("containers", requestor_id)
create_object_in_authz('containers', requestor_id)
end

def create_object_in_authz(object_name, requestor_id)
result = bifrost_request(:post, "#{object_name}", "{}", requestor_id)
JSON.parse(result)["id"]
result = bifrost_request(:post, object_name.to_s, '{}', requestor_id)
JSON.parse(result)['id']
end

# Tells bifrost that an actor is a member of a group.
# Group membership is managed through bifrost, and not via erchef.
def insert_authz_actor_into_group(group_id, actor_id)
bifrost_request(:put, "/groups/#{group_id}/actors/#{actor_id}", "{}", superuser_authz_id)
bifrost_request(:put, "/groups/#{group_id}/actors/#{actor_id}", '{}', superuser_authz_id)
end

def grant_authz_object_permission(permission_type, granted_to_object_type, granted_on_object_type, granted_on_id, granted_to_id, requestor_id)
Expand All @@ -189,14 +186,14 @@ def bifrost_request(method, rel_path, body, requestor_id)
headers = {
:content_type => :json,
:accept => :json,
'X-Ops-Requesting-Actor-Id' => requestor_id
'X-Ops-Requesting-Actor-Id' => requestor_id,
}
retries = 5
begin
if method == :get
RestClient.get("http://#{bifrost['vip']}:#{bifrost['port']}/#{rel_path}", headers)
else
RestClient.send(method, "http://#{bifrost['vip']}:#{bifrost['port']}/#{rel_path}", body, headers)
RestClient.send(method, "http://#{bifrost['vip']}:#{bifrost['port']}/#{rel_path}", body, headers)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
error = e.respond_to?(:response) ? e.response.chomp : e.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def self.du(path)
else
Chef::Log.error("du -sk #{path} failed with exit status: #{command.exitstatus}")
Chef::Log.error("du stderr: #{command.stderr}")
raise "du failed"
raise 'du failed'
end
rescue Errno::ENOENT
raise "The du utility is not available. Unable to check disk usage"
raise 'The du utility is not available. Unable to check disk usage'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def self.with_connection(node, database = 'template1', opts = {})
max_retries = retries
begin
connection = PG::Connection.open('user' => postgres['db_superuser'],
'host' => postgres['vip'],
'password' => postgres['db_superuser_password'],
'port' => postgres['port'],
'sslmode' => postgres['sslmode'],
'dbname' => database)
'host' => postgres['vip'],
'password' => postgres['db_superuser_password'],
'port' => postgres['port'],
'sslmode' => postgres['sslmode'],
'dbname' => database)
rescue => e
if retries > 0
sleep_time = 2**((max_retries - retries))
Expand All @@ -49,7 +49,6 @@ def self.with_connection(node, database = 'template1', opts = {})
end
end


# By default, with_connection will create a superuser connection over tcp to the specified database.
# This method will create a unix socket connection to a local database instance. This should only be used
# to the extent required to set configure tcp access and set a password for the superuser.
Expand All @@ -65,6 +64,7 @@ def self.with_local_connection(node, database = 'template1')
end
end
end

private

def self.as_user(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
# A class that knows about Elasticsearch configuration and usage.
class Elasticsearch
KB = 1024
MB = KB * KB
GB = MB * KB

KB = 1024
MB = KB * KB
GB = MB * KB

def self.node_memory_in_units(node, which, unit)
node[:memory][:total] =~ /^(\d+)kB/
mem_in_kb = $1.to_i
case unit
when :kb, :kilobytes
mem_in_kb
when :bytes, :b
mem_in_kb * 1024
when :mb, :megabytes
mem_in_kb / KB
when :gb, :gigabytes
mem_in_kb / (KB * KB)
end
end
def self.node_memory_in_units(node, _which, unit)
node[:memory][:total] =~ /^(\d+)kB/
mem_in_kb = Regexp.last_match(1).to_i
case unit
when :kb, :kilobytes
mem_in_kb
when :bytes, :b
mem_in_kb * 1024
when :mb, :megabytes
mem_in_kb / KB
when :gb, :gigabytes
mem_in_kb / (KB * KB)
end
end

# Supplies the default total heap size for elasticsearch calculated as
# 25% of the system memory bounded between 1GB - 26GB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ElasticSearchIndex < Chef::Provider::LWRPBase
provides :elasticsearch_index

action :create do
if ! index_exists?
unless index_exists?
converge_by "Creating elasticsearch index #{new_resource.index_name}" do
solr_server.put(new_resource.index_name, Chef::JSONCompat.to_json(new_resource.index_definition))
end
Expand All @@ -25,7 +25,7 @@ def index_exists?
solr_server.get("/#{new_resource.index_name}")
true
rescue Net::HTTPServerException => e
if e.response && e.response.code == "404"
if e.response && e.response.code == '404'
false
else
raise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class ElasticSearchIndex < Chef::Resource::LWRPBase
provides :elasticsearch_index
resource_name :elasticsearch_index
default_action :create
attribute :index_name, :name_attribute => true
attribute :server_url, :kind_of => String
attribute :index_definition, :kind_of => Hash
attribute :index_name, name_attribute: true
attribute :server_url, kind_of: String
attribute :index_definition, kind_of: Hash
end
end
end
Loading