managed auth keys

Change-Id: Ib625546c24e2eafe9a80ecdc66cc5fa79f4dab74
diff --git a/backupninja/files/authorized_keys b/backupninja/files/authorized_keys
new file mode 100644
index 0000000..6f54c9b
--- /dev/null
+++ b/backupninja/files/authorized_keys
@@ -0,0 +1,19 @@
+{%- from "backupninja/map.jinja" import server with context %}
+{%- for key_name, key in server.key.iteritems() %}
+{%- if key.get('enabled', False) %}
+{%- set clients = [] %}
+{%- if server.restrict_clients %}
+  {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
+    {%- if node_grains.get('backupninja', {}).get('client') %}
+    {%- set client = node_grains.backupninja.get("client") %}
+      {%- if client.get('addresses') and client.get('addresses', []) is iterable %}
+        {%- for address in client.addresses %}
+          {%- do clients.append(address|string) %}
+        {%- endfor %}
+      {%- endif %}
+    {%- endif %}
+  {%- endfor %}
+{%- endif %}
+no-pty{%- if clients %},from="{{ clients|join(',') }}"{%- endif %} {{ key.key }}
+{%- endif %}
+{%- endfor %}
\ No newline at end of file
diff --git a/backupninja/server.sls b/backupninja/server.sls
index 7417b45..3d09e97 100644
--- a/backupninja/server.sls
+++ b/backupninja/server.sls
@@ -21,47 +21,15 @@
     - user: backupninja_user
     - pkg: backupninja_server_packages
 
-{%- for key_name, key in server.key.iteritems() %}
-
-{%- if key.get('enabled', False) %}
-
-{%- set clients = [] %}
-{%- if server.restrict_clients %}
-  {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
-    {%- if node_grains.get('backupninja', {}).get('client') %}
-    {%- set client = node_grains.backupninja.get("client") %}
-      {%- if client.get('addresses') and client.get('addresses', []) is iterable %}
-        {%- for address in client.addresses %}
-          {%- do clients.append(address|string) %}
-        {%- endfor %}
-      {%- endif %}
-    {%- endif %}
-  {%- endfor %}
-{%- endif %}
-
-backupninja_key_{{ key.key }}:
-  ssh_auth.present:
+{{ server.home_dir }}/.ssh/authorized_keys:
+  file.managed:
   - user: backupninja
-  - name: {{ key.key }}
-  - options:
-    - no-pty
-{%- if clients %}
-    - from="{{ clients|join(',') }}"
-{%- endif %}
+  - group: backupninja
+  - template: jinja
+  - source: salt://backupninja/files/authorized_keys
   - require:
     - file: {{ server.home_dir }}
 
-{%- else %}
-
-backupninja_key_{{ key.key }}:
-  ssh_auth.absent:
-  - user: backupninja
-  - name: {{ key.key }}
-
-{%- endif %}
-
-{%- endfor %}
-
 {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
 
 {%- for backup_name, backup in node_grains.get('backupninja', {}).get('backup', {}).iteritems() %}