Skip to content

Commit

Permalink
Fix grafana_user password idempotency
Browse files Browse the repository at this point in the history
This commit fixes the issue that on each puppet run the user password
will be set.  As the grafana API does not provide any feature for that
we try to connect as that user if login works we suppress the password
change.

Fixes voxpupuli#104
  • Loading branch information
TuningYourCode authored and Andreas Unterkircher committed Sep 28, 2020
1 parent 6e804cf commit 8f3a7ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/puppet/provider/grafana_user/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def theme=(value)
end

def password
user[:password]
nil
end

def password=(value)
Expand Down Expand Up @@ -126,6 +126,24 @@ def save_user
self.user = nil
end

def check_password
uri = URI.parse format('%s://%s:%d%s/dashboards/home', grafana_scheme, grafana_host, grafana_port, resource[:grafana_api_path])
request = Net::HTTP::Get.new(uri.to_s)
request.content_type = 'application/json'
request.basic_auth resource[:name], resource[:password]
response = Net::HTTP.start(grafana_host, grafana_port,
use_ssl: grafana_scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
http.request(request)
end

if response.code == '200'
true
else
false
end
end

def delete_user
response = send_request('DELETE', format('%s/admin/users/%s', resource[:grafana_api_path], user[:id]))

Expand Down
3 changes: 3 additions & 0 deletions lib/puppet/type/grafana_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

newproperty(:password) do
desc 'The password for the user'
def insync?(_is)
provider.check_password
end
end

newproperty(:email) do
Expand Down

0 comments on commit 8f3a7ad

Please sign in to comment.