Merge "Add new module for configuring artifactory"
diff --git a/.gitignore b/.gitignore
index aa8e42a..27c925b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
*.swp
*.pyc
.ropeproject
+.idea/
diff --git a/_modules/artifactory_config.py b/_modules/artifactory_config.py
new file mode 100644
index 0000000..5fa4e66
--- /dev/null
+++ b/_modules/artifactory_config.py
@@ -0,0 +1,168 @@
+# -*- coding: utf-8 -*-
+'''
+Module for configuring Artifactory.
+change admin password
+add license key
+confingure ldap
+'''
+
+# Import python libs
+from __future__ import absolute_import
+import os
+import base64
+import logging
+
+# Import Salt libs
+import salt.utils
+import salt.ext.six.moves.http_client # pylint: disable=import-error,redefined-builtin,no-name-in-module
+from salt.ext.six.moves import urllib # pylint: disable=no-name-in-module
+from salt.ext.six.moves.urllib.error import HTTPError, URLError # pylint: disable=no-name-in-module
+
+import json
+import requests
+
+log = logging.getLogger(__name__)
+
+__virtualname__ = 'artifactory_config'
+
+
+def __virtual__():
+
+ return True
+
+
+
+# "repoLayoutRef" : "maven-2-default",
+
+class Artifactoryconfig:
+
+ def __init__(self, config={}):
+
+ self.files = []
+ self.def_password = 'password'
+ self.def_user= 'admin'
+
+
+ client_config = {
+ 'artifactory_url': 'http://your-instance/artifactory/api',
+ 'username': 'your-user',
+ 'password': 'password',
+ 'license_key' :'key',
+ 'artifactory_ldap_url': 'http://localhost',
+# 'headers': {'Content-type': 'application/json'},
+ 'ssl_verify': True
+ }
+
+ client_config.update(config)
+
+ # Set instance variables for every value in party_config
+ for k, v in client_config.items():
+ setattr(self, '%s' % (k,), v)
+
+ def change_admin_password(self, **connection_args):
+ """
+ Usage: POST /api/security/users/authorization/changePassword -H "Content-type: application/json" -d ' { "userName" : "{user}", "oldPassword" : "{old password}", "newPassword1" : "{new password}", "newPassword2" : "{verify new password}" }
+ :param connection_args:
+ :return: 0 if ok
+ """
+
+ url = self.artifactory_url + '/security/users/authorization/changePassword'
+ log.error(str(url))
+ data_pass={ "userName": self.def_user, "oldPassword": self.def_password, "newPassword2": self.password,"newPassword1": self.password}
+ auth = (self.username, self.def_password)
+
+ r = requests.post(url, auth=auth, json=data_pass)
+ log.error(str(r.text))
+ return r
+
+ def add_license_key(self, **connection_args):
+ """
+ Usage: POST /api/system/license
+ :param connection_args:
+ :return: 0 if ok
+ """
+ url = self.artifactory_url +'/system/license'
+ log.error(str(url))
+ auth = (self.username, self.password)
+ key = {"licenseKey": self.license_key}
+ log.error(str(key))
+ r = requests.post(url, auth=auth, json=key)
+ log.error(str(r.text))
+ return r
+
+ def configure_ldap(self, **connection_args):
+ url = self.artifactory_url + '/system/configuration'
+ auth = (self.username, self.password)
+ # r = requests.post(url,auth=auth,json=data)
+
+ r = requests.get(url, auth=auth)
+ # print json.dumps(r.text)
+ #log.error(str(r.text))
+
+ ldap_url = self.artifactory_ldap_url
+ searchFilter = "uid={0}"
+ xmlTemplate = """
+ <ldapSetting>
+ <key>ldap</key>
+ <enabled>true</enabled>
+ <ldapUrl>%(url)s</ldapUrl>
+ <search>
+ <searchFilter>%(sf)s</searchFilter>
+ <searchSubTree>true</searchSubTree>
+ </search>
+ <autoCreateUser>true</autoCreateUser>
+ <emailAttribute>mail</emailAttribute>
+ <ldapPoisoningProtection>true</ldapPoisoningProtection>
+ </ldapSetting>
+ """
+ xml_date = {'url': ldap_url, 'sf': searchFilter}
+ a = xmlTemplate % xml_date
+# log.error(str( r.text))
+ z = str(r.text).split('<ldapSettings/>')
+ out = str(z[0] + '<ldapSettings>' + a + '</ldapSettings>' + z[1])
+
+ # out=str(r.text).split('<ldapSettings/>')[0]+'<ldapSettings/>'+str(r.text).split('<ldapSettings/>')[1]
+ headers = {'Content-Type': 'application/xml'}
+
+ r = requests.post(url, auth=auth, data=out, headers=headers)
+ log.error(str(r.text))
+ return r
+
+def _client(**connection_args):
+ '''
+ Set up artifactory credentials
+
+ '''
+
+ prefix = "artifactory"
+
+ # look in connection_args first, then default to config file
+ def get(key, default=None):
+ return connection_args.get('connection_' + key,
+ __salt__['config.get'](prefix, {})).get(key, default)
+
+ client_config = {
+ 'artifactory_url': '%s://%s:%s/artifactory/api' % (get('proto', 'http'), get('host', 'localhost'), get('port', '8080')),
+ 'ssl_verify': get('ssl_verify', True),'license_key':get('license_key','key'),'artifactory_ldap_url':get('ldap_server','url'),
+ 'ldap_searchFilter':get('ldap_searchFilter','uid={0}'),'ldap_account_base':get('ldap_account_base','accaunt')
+ }
+
+ user = get('user', False)
+ password = get('password', False)
+ if user and password:
+ client_config['username'] = user
+ client_config['password'] = password
+
+ artifactory_config = Artifactoryconfig(client_config)
+
+ return artifactory_config
+
+
+
+def artifactory_init(**connection_args):
+
+ artifactory = _client(**connection_args)
+ artifactory.change_admin_password()
+ artifactory.add_license_key()
+ artifactory.configure_ldap()
+
diff --git a/_states/artifactory_config.py b/_states/artifactory_config.py
new file mode 100644
index 0000000..683ccc9
--- /dev/null
+++ b/_states/artifactory_config.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+'''
+Management of artifactory configuration
+======================================
+
+:depends: - requests Python module
+:configuration: See :py:mod:`salt.modules.artifactory` for setup instructions.
+'''
+
+def __virtual__():
+ '''
+ Only load if the artifactory module is in __salt__
+ '''
+ return True
+
+
+def artifactory_init( **kwargs):
+
+ out = __salt__['artifactory_config.artifactory_init']( **kwargs)
+
+ return out
diff --git a/artifactory/client.sls b/artifactory/client.sls
index dfedfd9..7588d7d 100644
--- a/artifactory/client.sls
+++ b/artifactory/client.sls
@@ -5,6 +5,9 @@
pkg.installed:
- names: {{ client.pkgs }}
+artifactory_config:
+ artifactory_config.artifactory_init
+
{%- for repo_name, repo in client.repo.iteritems() %}
artifactory_client_repo_{{ repo_name }}:
diff --git a/artifactory/meta/salt.yml b/artifactory/meta/salt.yml
index b21c18d..e3cc8b2 100644
--- a/artifactory/meta/salt.yml
+++ b/artifactory/meta/salt.yml
@@ -11,4 +11,11 @@
user: {{ client.server.user }}
password: {{ client.server.password }}
{%- endif %}
+ {%- if client.server.license_key is defined %}
+ license_key: {{ client.server.license_key }}
+ ldap_server: {{ client.server.ldap_server }}
+ ldap_account_base: {{ client.server.ldap_account_base }}
+ ldap_searchFilter: {{ client.server.ldap_searchFilter }}
+ {%- endif %}
+
{%- endif %}