Implement x509 for Rabbitmq

Change-Id: Ib0eb02046eab6e0cf4995f0999b01fe1d8710f14
Related-Prod: https://mirantis.jira.com/browse/PROD-23760
diff --git a/ceilometer/_ssl/rabbitmq.sls b/ceilometer/_ssl/rabbitmq.sls
new file mode 100644
index 0000000..51fa7e5
--- /dev/null
+++ b/ceilometer/_ssl/rabbitmq.sls
@@ -0,0 +1,87 @@
+{%- from "ceilometer/map.jinja" import server,agent with context %}
+
+{%- if server.get('enabled', False) %}
+  {%- set ceilometer_msg = server.message_queue %}
+  {%- set ceilometer_cacert = server.cacert_file %}
+  {%- set role = 'server' %}
+{%- else %}
+  {%- set ceilometer_msg = agent.message_queue %}
+  {%- set ceilometer_cacert = agent.cacert_file %}
+  {%- set role = 'agent' %}
+{%- endif %}
+
+ceilometer_ssl_rabbitmq:
+  test.show_notification:
+    - text: "Running ceilometer._ssl.rabbitmq"
+
+{%- if ceilometer_msg.get('x509',{}).get('enabled',False) %}
+
+  {%- set ca_file=ceilometer_msg.x509.ca_file %}
+  {%- set key_file=ceilometer_msg.x509.key_file %}
+  {%- set cert_file=ceilometer_msg.x509.cert_file %}
+
+rabbitmq_ceilometer_ssl_x509_ca:
+  {%- if ceilometer_msg.x509.cacert is defined %}
+  file.managed:
+    - name: {{ ca_file }}
+    - contents_pillar: ceilometer:{{ role }}:message_queue:x509:cacert
+    - mode: 644
+    - user: root
+    - group: ceilometer
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ ca_file }}
+  {%- endif %}
+
+rabbitmq_ceilometer_client_ssl_cert:
+  {%- if ceilometer_msg.x509.cert is defined %}
+  file.managed:
+    - name: {{ cert_file }}
+    - contents_pillar: ceilometer:{{ role }}:message_queue:x509:cert
+    - mode: 640
+    - user: root
+    - group: ceilometer
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ cert_file }}
+  {%- endif %}
+
+rabbitmq_ceilometer_client_ssl_private_key:
+  {%- if ceilometer_msg.x509.key is defined %}
+  file.managed:
+    - name: {{ key_file }}
+    - contents_pillar: ceilometer:{{ role }}:message_queue:x509:key
+    - mode: 640
+    - user: root
+    - group: ceilometer
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ key_file }}
+  {%- endif %}
+
+rabbitmq_ceilometer_ssl_x509_set_user_and_group:
+  file.managed:
+    - names:
+      - {{ ca_file }}
+      - {{ cert_file }}
+      - {{ key_file }}
+    - user: root
+    - group: ceilometer
+
+{% elif ceilometer_msg.get('ssl',{}).get('enabled',False) %}
+rabbitmq_ca_ceilometer:
+  {%- if ceilometer_msg.ssl.cacert is defined %}
+  file.managed:
+    - name: {{ ceilometer_msg.ssl.cacert_file }}
+    - contents_pillar: ceilometer:{{ role }}:message_queue:ssl:cacert
+    - mode: 644
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ ceilometer_msg.ssl.get('cacert_file', ceilometer_cacert) }}
+  {%- endif %}
+
+{%- endif %}
diff --git a/ceilometer/agent.sls b/ceilometer/agent.sls
index 2e075a8..c17b3b1 100644
--- a/ceilometer/agent.sls
+++ b/ceilometer/agent.sls
@@ -1,9 +1,14 @@
 {%- from "ceilometer/map.jinja" import agent with context %}
 {%- if agent.enabled %}
 
+include:
+  - ceilometer._ssl.rabbitmq
+
 ceilometer_agent_packages:
   pkg.installed:
   - names: {{ agent.pkgs }}
+  - require_in:
+    - sls: ceilometer._ssl.rabbitmq
 
 ceilometer_agent_conf:
   file.managed:
@@ -14,6 +19,7 @@
   - group: ceilometer
   - require:
     - pkg: ceilometer_agent_packages
+    - sls: ceilometer._ssl.rabbitmq
 
 {%- if agent.get('libvirt',{}).get('ssl',{}).get('enabled', False) == True %}
 add_ceilometer_to_nova_group:
@@ -120,28 +126,6 @@
 
 {%- endif %}
 
-{%- if agent.message_queue.get('ssl',{}).get('enabled', False) %}
-rabbitmq_ca_ceilometer_agent:
-{%- if agent.message_queue.ssl.cacert is defined %}
-  file.managed:
-    - name: {{ agent.message_queue.ssl.cacert_file }}
-    - contents_pillar: ceilometer:agent:message_queue:ssl:cacert
-    - mode: 0444
-    - makedirs: true
-    - require_in:
-      - file: ceilometer_agent_conf
-    - watch_in:
-      - ceilometer_agent_services
-{%- else %}
-  file.exists:
-   - name: {{ agent.message_queue.ssl.get('cacert_file', agent.cacert_file) }}
-   - require_in:
-     - file: ceilometer_agent_conf
-   - watch_in:
-      - ceilometer_agent_services
-{%- endif %}
-{%- endif %}
-
 {# Starting Pike switch to polling.yaml to handle meters polling as recommended in upstream #}
 {%- if agent.version not in ['liberty', 'juno', 'kilo', 'mitaka', 'newton', 'ocata'] and agent.polling is defined %}
 
@@ -169,5 +153,6 @@
   - watch:
     - file: ceilometer_agent_conf
     - file: ceilometer_agent_pipeline
+    - sls: ceilometer._ssl.rabbitmq
 
 {%- endif %}
diff --git a/ceilometer/server.sls b/ceilometer/server.sls
index 93815da..46204ca 100644
--- a/ceilometer/server.sls
+++ b/ceilometer/server.sls
@@ -1,9 +1,14 @@
 {%- from "ceilometer/map.jinja" import server with context %}
 {%- if server.enabled %}
 
+include:
+  - ceilometer._ssl.rabbitmq
+
 ceilometer_server_packages:
   pkg.installed:
   - names: {{ server.pkgs }}
+  - require_in:
+    - sls: ceilometer._ssl.rabbitmq
 
 /etc/ceilometer/ceilometer.conf:
   file.managed:
@@ -13,6 +18,7 @@
   - group: ceilometer
   - require:
     - pkg: ceilometer_server_packages
+    - sls: ceilometer._ssl.rabbitmq
 
 {%- for service_name in server.services %}
 {{ service_name }}_default:
@@ -265,28 +271,6 @@
 
 {%- endif %}
 
-{%- if server.message_queue.get('ssl',{}).get('enabled', False) %}
-rabbitmq_ca_ceilometer_server:
-{%- if server.message_queue.ssl.cacert is defined %}
-  file.managed:
-    - name: {{ server.message_queue.ssl.cacert_file }}
-    - contents_pillar: ceilometer:server:message_queue:ssl:cacert
-    - mode: 0444
-    - makedirs: true
-    - require_in:
-      - file: /etc/ceilometer/ceilometer.conf
-    - watch_in:
-      - ceilometer_server_services
-{%- else %}
-  file.exists:
-   - name: {{ server.message_queue.ssl.get('cacert_file', server.cacert_file) }}
-   - require_in:
-     - file: /etc/ceilometer/ceilometer.conf
-   - watch_in:
-      - ceilometer_server_services
-{%- endif %}
-{%- endif %}
-
 ceilometer_server_services:
   service.running:
   - names: {{ server.services }}
@@ -296,5 +280,6 @@
   {%- endif %}
   - watch:
     - file: /etc/ceilometer/ceilometer.conf
+    - sls: ceilometer._ssl.rabbitmq
 
 {%- endif %}