Merge "Implement X.509 auth for MySQL and Neutron"
diff --git a/README.rst b/README.rst
index 4cc9fc1..620e100 100644
--- a/README.rst
+++ b/README.rst
@@ -1459,6 +1459,29 @@
 +-------------------------------+------------------------------------------------------+
 
 
+Enable x509 and ssl communication between Neutron and Galera cluster.
+---------------------
+By default communication between Neutron and Galera is unsecure.
+
+neutron:
+  server:
+    database:
+      x509:
+        enabled: True
+
+You able to set custom certificates in pillar:
+
+neutron:
+  server:
+    database:
+      x509:
+        cacert: (certificate content)
+        cert: (certificate content)
+        key: (certificate content)
+
+You can read more about it here:
+    https://docs.openstack.org/security-guide/databases/database-access-control.html
+
 Documentation and Bugs
 ======================
 
diff --git a/neutron/_ssl/mysql.sls b/neutron/_ssl/mysql.sls
new file mode 100644
index 0000000..ae0f30c
--- /dev/null
+++ b/neutron/_ssl/mysql.sls
@@ -0,0 +1,77 @@
+{%- from "neutron/map.jinja" import server with context %}
+
+neutron_ssl_mysql:
+  test.show_notification:
+    - text: "Running neutron._ssl.mysql"
+
+{%- if server.database.get('x509',{}).get('enabled',False) %}
+
+  {%- set ca_file=server.database.x509.ca_file %}
+  {%- set key_file=server.database.x509.key_file %}
+  {%- set cert_file=server.database.x509.cert_file %}
+
+mysql_neutron_ssl_x509_ca:
+  {%- if server.database.x509.cacert is defined %}
+  file.managed:
+    - name: {{ ca_file }}
+    - contents_pillar: neutron:server:database:x509:cacert
+    - mode: 444
+    - user: neutron
+    - group: neutron
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ ca_file }}
+  {%- endif %}
+
+mysql_neutron_client_ssl_cert:
+  {%- if server.database.x509.cert is defined %}
+  file.managed:
+    - name: {{ cert_file }}
+    - contents_pillar: neutron:server:database:x509:cert
+    - mode: 440
+    - user: neutron
+    - group: neutron
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ cert_file }}
+  {%- endif %}
+
+mysql_neutron_client_ssl_private_key:
+  {%- if server.database.x509.key is defined %}
+  file.managed:
+    - name: {{ key_file }}
+    - contents_pillar: neutron:server:database:x509:key
+    - mode: 400
+    - user: neutron
+    - group: neutron
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ key_file }}
+  {%- endif %}
+
+mysql_neutron_ssl_x509_set_user_and_group:
+  file.managed:
+    - names:
+      - {{ ca_file }}
+      - {{ cert_file }}
+      - {{ key_file }}
+    - user: neutron
+    - group: neutron
+
+{% elif server.database.get('ssl',{}).get('enabled',False) %}
+mysql_ca_neutron:
+  {%- if server.database.ssl.cacert is defined %}
+  file.managed:
+    - name: {{ server.database.ssl.cacert_file }}
+    - contents_pillar: neutron:server:database:ssl:cacert
+    - mode: 0444
+    - makedirs: true
+  {%- else %}
+  file.exists:
+    - name: {{ server.database.ssl.get('cacert_file', server.cacert_file) }}
+  {%- endif %}
+
+{%- endif %}
\ No newline at end of file
diff --git a/neutron/files/pike/neutron-server.conf b/neutron/files/pike/neutron-server.conf
index b91c122..041dc2b 100644
--- a/neutron/files/pike/neutron-server.conf
+++ b/neutron/files/pike/neutron-server.conf
@@ -1,4 +1,12 @@
 {%- from "neutron/map.jinja" import fwaas, server with context %}
+
+{%- set connection_x509_ssl_option = '' %}
+{%- if server.database.get('x509',{}).get('enabled',False) %}
+  {%- set connection_x509_ssl_option = '&ssl_ca=' ~ server.database.x509.ca_file ~ '&ssl_cert=' ~ server.database.x509.cert_file ~ '&ssl_key=' ~ server.database.x509.key_file %}
+{%- elif server.database.get('ssl',{}).get('enabled',False) %}
+  {%- set connection_x509_ssl_option = '&ssl_ca=' ~ server.database.ssl.get('cacert_file', server.cacert_file) %}
+{%- endif %}
+
 [DEFAULT]
 
 #
@@ -885,7 +893,7 @@
 # Deprecated group/name - [sql]/connection
 
 {% if server.backend.engine in ["ml2", "ovn"] %}
-connection = {{ server.database.engine }}+pymysql://{{ server.database.user }}:{{ server.database.password }}@{{ server.database.host }}/{{ server.database.name }}?charset=utf8{%- if server.database.get('ssl',{}).get('enabled',False) %}&ssl_ca={{ server.database.ssl.get('cacert_file', server.cacert_file) }}{% endif %}
+connection = {{ server.database.engine }}+pymysql://{{ server.database.user }}:{{ server.database.password }}@{{ server.database.host }}/{{ server.database.name }}?charset=utf8{{ connection_x509_ssl_option|string }}
 {% else %}
 connection = sqlite:////var/lib/neutron/neutron.sqlite
 {% endif %}
diff --git a/neutron/server.sls b/neutron/server.sls
index 65b98bd..0367eb7 100644
--- a/neutron/server.sls
+++ b/neutron/server.sls
@@ -1,8 +1,9 @@
 {%- from "neutron/map.jinja" import server, fwaas with context %}
 
 include:
- - neutron.db.offline_sync
- - neutron.fwaas
+  - neutron.db.offline_sync
+  - neutron.fwaas
+  - neutron._ssl.mysql
 
 {%- if server.get('enabled', False) %}
 {% if grains.os_family == 'Debian' %}
@@ -33,6 +34,8 @@
 neutron_server_packages:
   pkg.installed:
   - names: {{ server.pkgs }}
+  - require_in:
+    - sls: neutron._ssl.mysql
 
 {% if server.backend.engine == "contrail" %}
 
@@ -63,14 +66,13 @@
   {%- if grains.get('noservices') %}
   - onlyif: /bin/false
   {%- endif %}
+  - require:
+    - sls: neutron._ssl.mysql
   - watch:
     - file: /etc/neutron/neutron.conf
     {%- if server.message_queue.get('ssl',{}).get('enabled', False) %}
     - file: rabbitmq_ca_neutron_server
     {%- endif %}
-    {%- if server.database.get('ssl',{}).get('enabled', False) %}
-    - file: mysql_ca_neutron_server
-    {%- endif %}
 
 {%- endif %}
 
@@ -119,9 +121,7 @@
   - template: jinja
   - require:
     - pkg: neutron_server_packages
-    {%- if server.database.get('ssl',{}).get('enabled', False) %}
-    - file: mysql_ca_neutron_server
-    {%- endif %}
+    - sls: neutron._ssl.mysql
   - require_in:
     - sls: neutron.db.offline_sync
 
@@ -363,14 +363,13 @@
   {%- if grains.get('noservices') %}
   - onlyif: /bin/false
   {%- endif %}
+  - require:
+    - sls: neutron._ssl.mysql
   - watch:
     - file: /etc/neutron/neutron.conf
     {%- if server.message_queue.get('ssl',{}).get('enabled', False) %}
     - file: rabbitmq_ca_neutron_server
     {%- endif %}
-    {%- if server.database.get('ssl',{}).get('enabled', False) %}
-    - file: mysql_ca_neutron_server
-    {%- endif %}
 
 {%- if grains.get('virtual_subtype', None) == "Docker" %}
 
@@ -398,18 +397,4 @@
 {%- endif %}
 {%- endif %}
 
-{%- if server.database.get('ssl',{}).get('enabled', False) %}
-mysql_ca_neutron_server:
-{%- if server.database.ssl.cacert is defined %}
-  file.managed:
-    - name: {{ server.database.ssl.cacert_file }}
-    - contents_pillar: neutron:server:database:ssl:cacert
-    - mode: 0444
-    - makedirs: true
-{%- else %}
-  file.exists:
-   - name: {{ server.database.ssl.get('cacert_file', server.cacert_file) }}
-{%- endif %}
-{%- endif %}
-
 {%- endif %}