blob: ed348775e2af76da25c966a2b5258b993affeeb5 [file] [log] [blame]
Jakub Josef3de91af2016-12-08 17:03:33 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef3de91af2016-12-08 17:03:33 +01003logger = logging.getLogger(__name__)
4
5create_admin_groovy = u"""\
6import jenkins.model.*
7import hudson.security.*
8def instance = Jenkins.getInstance()
Adam Tengler70763e02017-08-21 16:50:32 +00009if(hudson.model.User.getAll().find{u->u.fullName.equals("${username}")}){
Jakub Josef26956a62017-03-22 16:32:28 +010010 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000011}else{
Jakub Josef26956a62017-03-22 16:32:28 +010012 def hudsonRealm = new HudsonPrivateSecurityRealm(false)
Adam Tengler70763e02017-08-21 16:50:32 +000013 def result=hudsonRealm.createAccount("${username}","${password}")
Jakub Josef26956a62017-03-22 16:32:28 +010014 instance.setSecurityRealm(hudsonRealm)
15 def strategy = new hudson.security.FullControlOnceLoggedInAuthorizationStrategy()
16 strategy.setAllowAnonymousRead(false)
17 instance.setAuthorizationStrategy(strategy)
18 instance.save()
Adam Tengler70763e02017-08-21 16:50:32 +000019 if(result.toString().equals("${username}")){
Jakub Josef26956a62017-03-22 16:32:28 +010020 print("SUCCESS")
Adam Tengler70763e02017-08-21 16:50:32 +000021 }else{
Jakub Josef26956a62017-03-22 16:32:28 +010022 print("FAILED")
Adam Tengler70763e02017-08-21 16:50:32 +000023 }
24}
Jakub Josef7ae6b242016-12-14 14:41:44 +010025""" # noqa
Jakub Josef3de91af2016-12-08 17:03:33 +010026
27
28create_user_groovy = u"""\
Adam Tengler70763e02017-08-21 16:50:32 +000029if(hudson.model.User.getAll().find{u->u.fullName.equals("${username}")}){
Jakub Josef26956a62017-03-22 16:32:28 +010030 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000031}else{
32 def result=jenkins.model.Jenkins.instance.securityRealm.createAccount("${username}", "${password}")
33 if(result.toString().equals("${username}")){
Jakub Josef26956a62017-03-22 16:32:28 +010034 print("SUCCESS")
Adam Tengler70763e02017-08-21 16:50:32 +000035 }else{
Jakub Josef26956a62017-03-22 16:32:28 +010036 print("FAILED")
Adam Tengler70763e02017-08-21 16:50:32 +000037 }
38}
Jakub Josef3de91af2016-12-08 17:03:33 +010039""" # noqa
40
Jakub Josefe3807982016-12-15 11:54:51 +010041
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040042def __virtual__():
43 '''
44 Only load if jenkins_common module exist.
45 '''
46 if 'jenkins_common.call_groovy_script' not in __salt__:
47 return (
48 False,
49 'The jenkins_user state module cannot be loaded: '
50 'jenkins_common not found')
51 return True
52
53
Jakub Josef3de91af2016-12-08 17:03:33 +010054def present(name, username, password, admin=False):
55 """
56 Main jenkins users state method
57
58 :param username: user name
59 :param password: user password
60 :param admin: is admin user flag (username will be always admin)
61 :returns: salt-specified state dict
62 """
63 test = __opts__['test'] # noqa
64 ret = {
65 'name': username,
66 'changes': {},
67 'result': False,
68 'comment': '',
69 }
70
71 result = False
72 if test:
73 status = 'CREATED'
74 ret['changes'][username] = status
75 ret['comment'] = 'User %s %s' % (username, status.lower())
76 else:
Jakub Josef26956a62017-03-22 16:32:28 +010077 call_result = __salt__['jenkins_common.call_groovy_script'](
78 create_admin_groovy if admin else create_user_groovy, {"username": username, "password": password})
Adam Tengler70763e02017-08-21 16:50:32 +000079 if call_result["code"] == 200 and call_result["msg"] in [
80 "SUCCESS", "EXISTS"]:
Jakub Josef26956a62017-03-22 16:32:28 +010081 if call_result["msg"] == "SUCCESS":
Jakub Josef7ae6b242016-12-14 14:41:44 +010082 status = "CREATED" if not admin else "ADMIN CREATED"
83 ret['changes'][username] = status
Jakub Josef7ae6b242016-12-14 14:41:44 +010084 else:
Jakub Josef26956a62017-03-22 16:32:28 +010085 status = "EXISTS"
Jakub Josef3de91af2016-12-08 17:03:33 +010086 ret['comment'] = 'User %s %s' % (username, status.lower())
87 result = True
Jakub Josef26956a62017-03-22 16:32:28 +010088 else:
89 status = 'FAILED'
90 logger.error("Jenkins user API call failure: %s",
91 call_result["msg"])
92 ret['comment'] = 'Jenkins user API call failure: %s' % (call_result[
93 "msg"])
Jakub Josef3de91af2016-12-08 17:03:33 +010094 ret['result'] = None if test else result
95 return ret