/
credentials.rb
261 lines (219 loc) · 7.32 KB
/
credentials.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#
# Cookbook Name:: jenkins
# HWRP:: credentials
#
# Author:: Seth Chisamore <schisamo@chef.io>
#
# Copyright 2013-2014, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative '_helper'
require_relative '_params_validate'
class Chef
class Resource::JenkinsCredentials < Resource::LWRPBase
require 'securerandom'
include Jenkins::Helper
# Chef attributes
identity_attr :username
provides :jenkins_credentials
# Set the resource name
self.resource_name = :jenkins_credentials
# Actions
actions :create, :delete
default_action :create
# Attributes
attribute :username,
kind_of: String,
name_attribute: true
attribute :id,
kind_of: String,
regex: UUID_REGEX,
default: lazy { SecureRandom.uuid }
attribute :description,
kind_of: String,
default: lazy { |new_resource|
"Credentials for #{new_resource.username} - created by Chef"
}
attr_writer :exists
#
# Determine if the credentials exists on the master. This value is
# set by the provider when the current resource is loaded.
#
# @return [Boolean]
#
def exists?
!!@exists
end
end
end
class Chef
class Provider::JenkinsCredentials < Provider::LWRPBase
require 'json'
require 'openssl'
include Jenkins::Helper
def load_current_resource
@current_resource ||= Resource::JenkinsCredentials.new(new_resource.name)
if current_credentials
@current_resource.exists = true
@current_resource.id(current_credentials[:id])
@current_resource.description(current_credentials[:description])
@current_resource.username(current_credentials[:username])
end
@current_resource
end
def whyrun_supported?
true
end
#
# Create the given credentials.
#
action(:create) do
if current_resource.exists? && correct_config?
Chef::Log.debug("#{new_resource} exists - skipping")
else
converge_by("Create #{new_resource}") do
executor.groovy! <<-EOH.gsub(/ ^{12}/, '')
import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import hudson.plugins.sshslaves.*;
global_domain = Domain.global()
credentials_store =
Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()
#{credentials_groovy}
// Create or update the credentials in the Jenkins instance
#{credentials_for_username_groovy(new_resource.username, 'existing_credentials')}
if(existing_credentials != null) {
credentials_store.updateCredentials(
global_domain,
existing_credentials,
credentials
)
} else {
credentials_store.addCredentials(global_domain, credentials)
}
EOH
end
end
end
#
# Delete the given credentials.
#
action(:delete) do
if current_resource.exists?
converge_by("Delete #{new_resource}") do
executor.groovy! <<-EOH.gsub(/ ^{12}/, '')
import jenkins.model.*
import com.cloudbees.plugins.credentials.*;
global_domain = com.cloudbees.plugins.credentials.domains.Domain.global()
credentials_store =
Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()
#{credentials_for_username_groovy(new_resource.username, 'existing_credentials')}
if(existing_credentials != null) {
credentials_store.removeCredentials(
global_domain,
existing_credentials
)
}
EOH
end
else
Chef::Log.debug("#{new_resource} does not exist - skipping")
end
end
protected
#
# Returns a Groovy snippet that creates an instance of the
# credentail's implementation. The credentials instance should be
# set to a Groovy variable named `credentials`.
#
# @abstract
# @return [String]
#
def credentials_groovy
fail NotImplementedError, 'You must implement #credentials_groovy.'
end
#
# Maps a credentails's resource attribute name to the equivalent
# property in the Groovy representation. This mapping is useful in
# Ruby/Groovy serialization/deserialization.
#
# @return [Hash]
#
# @example
# {password: 'credentials.password.plainText'}
#
def attribute_to_property_map
{}
end
#
# Loads the current credential into a Hash.
#
def current_credentials
return @current_credentials if @current_credentials
Chef::Log.debug "Load #{new_resource} credentials information"
credentials_attributes = []
attribute_to_property_map.each_pair do |resource_attribute, groovy_property|
credentials_attributes <<
"current_credentials['#{resource_attribute}'] = #{groovy_property}"
end
json = executor.groovy! <<-EOH.gsub(/ ^{8}/, '')
import com.cloudbees.plugins.credentials.impl.*;
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*;
#{credentials_for_username_groovy(new_resource.username, 'credentials')}
if(credentials == null) {
return null
}
current_credentials = [
id:credentials.id,
description:credentials.description,
username:credentials.username
]
#{credentials_attributes.join("\n")}
builder = new groovy.json.JsonBuilder(current_credentials)
println(builder)
EOH
return nil if json.nil? || json.empty?
@current_credentials = JSON.parse(json, symbolize_names: true)
# Values that were serialized as nil/null are deserialized as an
# empty string! :( Let's ensure we convert back to nil.
@current_credentials = convert_blank_values_to_nil(@current_credentials)
end
#
# Helper method for determining if the given JSON is in sync with the
# current configuration on the Jenkins instance.
#
# @return [Boolean]
#
def correct_config?
wanted_credentials = {
description: new_resource.description,
username: new_resource.username,
}
attribute_to_property_map.keys.each do |key|
wanted_credentials[key] = new_resource.send(key)
end
# Don't compare the ID as it is generated
current_credentials.dup.tap { |c| c.delete(:id) } == convert_blank_values_to_nil(wanted_credentials)
end
end
end
Chef::Platform.set(
resource: :jenkins_credentials,
provider: Chef::Provider::JenkinsCredentials
)