-
-
Notifications
You must be signed in to change notification settings - Fork 4
Auth ldap backend #9
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
Open
witjoh
wants to merge
7
commits into
voxpupuli:main
Choose a base branch
from
witjoh:auth_ldap_backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f391a19
add system autentication ldap backend
51cd5ac
implement prefetch and flush
57fcdb6
diable debug to reduce output
7c47044
rename enabled to active
64611e0
fixes (de)activation and changing the type of the backend
93f8842
add new match option in pipeline stages
beddbd6
Update graylog_api.rb
witjoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
182 changes: 182 additions & 0 deletions
182
lib/puppet/provider/graylog_auth_ldap_backend/graylog_api.rb
This file contains hidden or 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,182 @@ | ||
require_relative '../graylog_api' | ||
|
||
Puppet::Type.type(:graylog_auth_ldap_backend).provide(:graylog_api, parent: Puppet::Provider::GraylogAPI) do | ||
|
||
has_feature :activateable | ||
|
||
mk_resource_methods | ||
|
||
attr_writer :role_cache | ||
|
||
attr_accessor :roles_map | ||
|
||
def self.instances | ||
if major_version < 4 | ||
fail('graylog_auth_ldap_backend type is not supported in Graylog versions older then 4.x') | ||
end | ||
|
||
result = get('system/authentication/services/backends') | ||
|
||
items = result['backends'].map do |data| | ||
# skip non ldap backends | ||
next unless ['active-directory', 'ldap'].include?(data['config']['type']) | ||
roles = roles_to_names(data['default_roles']) | ||
|
||
new( | ||
name: data['description'], | ||
description: data['description'], | ||
ensure: :present, | ||
type: data['config']['type'], | ||
system_user_dn: data['config']['system_user_dn'], | ||
server_address: data['config']['servers'].map { |srv| "#{srv['host']}:#{srv['port']}" }, | ||
transport_security: data['config']['transport_security'], | ||
verify_certificates: data['config']['verify_certificates'], | ||
search_base_dn: data['config']['user_search_base'], | ||
search_pattern: data['config']['user_search_pattern'], | ||
name_attribute: data['config']['user_name_attribute'], | ||
full_name_attribute: data['config']['user_full_name_attribute'], | ||
default_roles: roles, | ||
password_is_set: data['config']['system_user_password']['is_set'], | ||
rest_id: data['id'], | ||
) | ||
end | ||
items.compact | ||
end | ||
|
||
def self.prefetch(resources) | ||
if major_version < 4 | ||
fail('graylog_auth_ldap_backend type is not supported in Graylog versions older then 4.x') | ||
end | ||
|
||
backends = instances | ||
resources.keys.each do | name | | ||
if provider = backends.find{ | bcknd | bcknd.name == name } | ||
resources[name].provider = provider | ||
end | ||
end | ||
end | ||
|
||
def activated_insync?(current) | ||
current == @resource[:activated] | ||
end | ||
|
||
def activate | ||
params = { active_backend: @property_hash[:rest_id] } | ||
post('system/authentication/services/configuration', params) | ||
@property_hash[:activated] = :true | ||
end | ||
|
||
def deactivate | ||
# we need to check the active backend each time, since another resource can be (de)activated in the meantime | ||
active_backend_id = get('system/authentication/services/configuration')['configuration']['active_backend'] | ||
if !active_backend_id.nil? and @property_hash[:rest_id] == active_backend_id | ||
post('system/authentication/services/configuration', { active_backend: nil }) | ||
end | ||
@property_hash[:activated] = :false | ||
end | ||
|
||
def activated? | ||
@property_hash[:activated] | ||
end | ||
|
||
def activated | ||
active_backend_id = get('system/authentication/services/configuration')['configuration']['active_backend'] | ||
@property_hash[:activated] = (!active_backend_id.nil? and @property_hash[:rest_id] == active_backend_id).to_s.to_sym | ||
end | ||
|
||
def system_user_password | ||
if @resource[:reset_password] or !@property_hash[:password_is_set] | ||
nil | ||
else | ||
@resource[:system_user_password] | ||
end | ||
end | ||
|
||
def flush | ||
|
||
data = { | ||
default_roles: self.class.roles_to_ids(@resource[:default_roles]), | ||
description: @resource[:description], | ||
title: @resource[:type] == :ldap ? 'LDAP' : 'Active Directory', | ||
config: { | ||
type: @resource[:type], | ||
servers: @resource[:server_address].map { |uri| | ||
a=uri.split(':') | ||
{ | ||
host: a[0], | ||
port: a[1], | ||
} | ||
}, | ||
transport_security: @resource[:transport_security], | ||
verify_certificates: @resource[:verify_certificates], | ||
system_user_dn: @resource[:system_user_dn], | ||
user_search_base: @resource[:search_base_dn], | ||
user_search_pattern: @resource[:search_pattern], | ||
user_full_name_attribute: @resource[:full_name_attribute], | ||
user_name_attribute: @resource[:name_attribute], | ||
system_user_password: @resource[:system_user_password], | ||
} | ||
} | ||
|
||
if @action == :destroy | ||
# we cannot remove an active backend, so we deactivate it first | ||
deactivate | ||
end | ||
|
||
if @action.nil? and type != @initial_params[:type] | ||
# the type cannot be changed, so we remove backend first, and then create a new one | ||
# needs to be deactivated before removal | ||
if activated? == :true | ||
deactivate | ||
end | ||
delete("system/authentication/services/backends/#{rest_id}") | ||
response = post('system/authentication/services/backends', data) | ||
set_rest_id_on_create(response) if respond_to?(:set_rest_id_on_create) | ||
# and set it activated after recreation if needed | ||
if @resource[:activated] == :true | ||
@property_hash[:rest_id] = @rest_id | ||
activate | ||
end | ||
else | ||
simple_flush('system/authentication/services/backends', data) | ||
if @action == :create and @resource[:activated] | ||
# new created backends should be activated after initial creation if needed | ||
@property_hash[:rest_id] = @rest_id | ||
activate | ||
end | ||
end | ||
end | ||
|
||
def set_rest_id_on_create(response) | ||
@rest_id = response['backend']['id'] | ||
end | ||
|
||
def self.map_roles(list, key) | ||
if !@map_roles | ||
@map_roles = {} | ||
get('authz/roles')['roles'].each do |role| | ||
@map_roles[role['id']] = role['name'] | ||
end | ||
end | ||
|
||
result = [] | ||
list.each do |val| | ||
if key == :name | ||
result << @map_roles[val] | ||
else | ||
result << @map_roles.key(val) | ||
end | ||
end | ||
|
||
result | ||
end | ||
|
||
def self.roles_to_names(list) | ||
map_roles(list, :name) | ||
end | ||
|
||
def self.roles_to_ids(list) | ||
map_roles(list, :id) | ||
end | ||
|
||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe delete these debug statements rather than commenting them if they do not help?
(the rest looks good I think, but will be hard to test in CI).