-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from bdclark/acls
Add ACL support
- Loading branch information
Showing
8 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ end | |
|
||
group :integration do | ||
cookbook 'consul_spec', path: 'test/cookbooks/consul_spec' | ||
cookbook 'apt' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# | ||
# Cookbook: consul | ||
# License: Apache 2.0 | ||
# | ||
# Copyright (C) 2014, 2015 Bloomberg Finance L.P. | ||
# | ||
require 'poise' | ||
|
||
module ConsulCookbook | ||
module Resource | ||
# Resource for managing Consul ACLs. | ||
class ConsulAcl < Chef::Resource | ||
include Poise | ||
provides(:consul_acl) | ||
actions(:create, :delete) | ||
default_action(:create) | ||
|
||
# @!attribute url | ||
# @return [String] | ||
attribute(:url, kind_of: String, default: 'http://localhost:8500') | ||
|
||
# @!attribute auth_token | ||
# @return [String] | ||
attribute(:auth_token, kind_of: String, required: true) | ||
|
||
# @!attribute id | ||
# @return [String] | ||
attribute(:id, kind_of: String, name_attribute: true) | ||
|
||
# @!attribute acl_name | ||
# @return [String] | ||
attribute(:acl_name, kind_of: String, default: '') | ||
|
||
# @!attribute type | ||
# @return [String] | ||
attribute(:type, equal_to: %w{client management}, default: 'client') | ||
|
||
# @!attribute rules | ||
# @return [String] | ||
attribute(:rules, kind_of: String, default: '') | ||
|
||
def to_acl | ||
{ 'ID' => id, 'Type' => type, 'Name' => acl_name, 'Rules' => rules } | ||
end | ||
end | ||
end | ||
|
||
module Provider | ||
# Provider for managing Consul ACLs. | ||
class ConsulAcl < Chef::Provider | ||
include Poise | ||
provides(:consul_acl) | ||
|
||
def action_create | ||
configure_diplomat | ||
unless up_to_date? | ||
Diplomat::Acl.create(new_resource.to_acl) | ||
new_resource.updated_by_last_action(true) | ||
end | ||
end | ||
|
||
def action_delete | ||
configure_diplomat | ||
unless Diplomat::Acl.info(new_resource.id).empty? | ||
Diplomat::Acl.destroy(new_resource.id) | ||
new_resource.updated_by_last_action(true) | ||
end | ||
end | ||
|
||
protected | ||
|
||
def configure_diplomat | ||
begin | ||
require 'diplomat' | ||
rescue LoadError | ||
raise RunTimeError, 'The diplomat gem is required; ' \ | ||
'include recipe[consul::client_gem] to install.' | ||
end | ||
Diplomat.configure do |config| | ||
config.url = new_resource.url | ||
config.acl_token = new_resource.auth_token | ||
config.options = { request: { timeout: 10 } } | ||
end | ||
end | ||
|
||
def up_to_date? | ||
old_acl = Diplomat::Acl.info(new_resource.to_acl['ID']).first | ||
return false if old_acl.nil? | ||
old_acl.select! { |k, _v| %w(ID Type Name Rules).include?(k) } | ||
old_acl == new_resource.to_acl | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# | ||
# Cookbook: consul | ||
# License: Apache 2.0 | ||
# | ||
# Copyright 2014, 2015 Bloomberg Finance L.P. | ||
# | ||
|
||
chef_gem 'diplomat' do | ||
version node['consul']['diplomat_version'] if node['consul']['diplomat_version'] | ||
action :install | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package 'curl' | ||
|
||
consul_acl 'anonymous' do | ||
acl_name 'Anonymous Token' | ||
type 'client' | ||
url "http://localhost:#{node['consul']['config']['ports']['http']}" | ||
auth_token node['consul']['config']['acl_master_token'] | ||
notifies :create, 'file[/tmp/anonymous-notified]', :immediately | ||
end | ||
|
||
file '/tmp/anonymous-notified' do | ||
action :nothing | ||
end | ||
|
||
consul_acl 'management_token' do | ||
acl_name 'Management Token' | ||
type 'management' | ||
auth_token node['consul']['config']['acl_master_token'] | ||
notifies :create, 'file[/tmp/management_token-notified]', :immediately | ||
end | ||
|
||
file '/tmp/management_token-notified' do | ||
action :nothing | ||
end | ||
|
||
consul_acl 'delete_management_token' do | ||
id 'management_token' | ||
auth_token node['consul']['config']['acl_master_token'] | ||
action :delete | ||
end | ||
|
||
consul_acl 'non_existing_token' do | ||
auth_token node['consul']['config']['acl_master_token'] | ||
action :delete | ||
end | ||
|
||
consul_acl 'reader_token' do | ||
type 'client' | ||
rules <<-EOS.gsub(/^\s{4}/, '') | ||
dummyrule_line1 | ||
dummyrule_line2 | ||
EOS | ||
auth_token node['consul']['config']['acl_master_token'] | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'spec_helper' | ||
|
||
describe command('curl -s "http://localhost:8500/v1/acl/info/anonymous"') do | ||
its(:exit_status) { should eq 0 } | ||
its(:stdout) { should match('"ID":"anonymous"') } | ||
its(:stdout) { should match('"Name":"Anonymous Token"') } | ||
its(:stdout) { should match('"Type":"client"') } | ||
its(:stdout) { should match('"Rules":""') } | ||
end | ||
|
||
describe file('/tmp/anonymous-notified') do | ||
it { should_not exist } | ||
end | ||
|
||
describe command('curl -s "http://localhost:8500/v1/acl/info/management_token"') do | ||
its(:exit_status) { should eq 0 } | ||
its(:stdout) { should match('[]') } | ||
end | ||
|
||
describe file('/tmp/management_token-notified') do | ||
it { should exist } | ||
end | ||
|
||
describe command('curl -s "http://localhost:8500/v1/acl/info/reader_token"') do | ||
its(:exit_status) { should eq 0 } | ||
its(:stdout) { should match('"ID":"reader_token"') } | ||
its(:stdout) { should match('"Name":""') } | ||
its(:stdout) { should match('"Type":"client"') } | ||
its(:stdout) { should match /\"Rules\":\"dummyrule_line1\\ndummyrule_line2\\n\"/ } | ||
end |