jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/328403 )

Change subject: Add SSH key importing
......................................................................


Add SSH key importing

Add a form to upload a new SSH public key and store it in LDAP.

Bug: T144711
Change-Id: Ic01693ee543779f7f5e5df272f2a3e793f478030
---
M striker/profile/forms.py
M striker/profile/urls.py
M striker/profile/views.py
M striker/templates/profile/settings/ssh-keys.html
4 files changed, 70 insertions(+), 0 deletions(-)

Approvals:
  Andrew Bogott: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/striker/profile/forms.py b/striker/profile/forms.py
index 1e98001..bfae5b4 100644
--- a/striker/profile/forms.py
+++ b/striker/profile/forms.py
@@ -47,3 +47,34 @@
             if hash != key_hash
         ]
         return key_hash
+
+
+class SshKeyForm(forms.Form):
+    public_key = forms.CharField(
+        label=_('Public key'),
+        widget=forms.Textarea(
+            attrs={
+                'placeholder': _(
+                    "Begins with 'ssh-rsa', 'ssh-dss', 'ssh-ed25519', "
+                    "'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or "
+                    "'ecdsa-sha2-nistp521'"
+                ),
+            }
+        ),
+        required=True,
+    )
+
+    def clean_public_key(self):
+        pub_key = self.cleaned_data.get('public_key').strip()
+        key = utils.parse_ssh_key(pub_key)
+        if key is None:
+            # TODO: Try to cleanup the data and parse it again?
+            # OpenStackManager checks for PuTTY's weird key format and tries
+            # to extract the public key from that. I don't think that code in
+            # OSM actually works though. OSM also passes the key data through
+            # `ssh-keygen -i` for validation. This would have the side effect
+            # of extracting the public key from an unencrypted private key.
+            raise forms.ValidationError(
+                _('Invalid public key.'), code='key_invalid')
+        self.key = key
+        return pub_key
diff --git a/striker/profile/urls.py b/striker/profile/urls.py
index d2292ea..e6a4b5a 100644
--- a/striker/profile/urls.py
+++ b/striker/profile/urls.py
@@ -50,4 +50,9 @@
         'striker.profile.views.ssh_key_delete',
         name='ssh_key_delete'
     ),
+    urls.url(
+        r'^settings/ssh-keys/add$',
+        'striker.profile.views.ssh_key_add',
+        name='ssh_key_add'
+    ),
 ]
diff --git a/striker/profile/views.py b/striker/profile/views.py
index 689710f..2f5c00e 100644
--- a/striker/profile/views.py
+++ b/striker/profile/views.py
@@ -81,6 +81,7 @@
     ldapuser = req.user.ldapuser
     ctx = {
         'ssh_keys': [utils.parse_ssh_key(key) for key in ldapuser.ssh_keys],
+        'new_key': forms.SshKeyForm(),
     }
     for key in ctx['ssh_keys']:
         key.form = forms.SshKeyDeleteForm(
@@ -104,3 +105,22 @@
         else:
             messages.error(req, _('Key not found.'))
     return shortcuts.redirect(urlresolvers.reverse('profile:ssh_keys'))
+
+
+@login_required
+def ssh_key_add(req):
+    if req.method == 'POST':
+        form = forms.SshKeyForm(data=req.POST)
+        if form.is_valid():
+            ldapuser = req.user.ldapuser
+            keys = ldapuser.ssh_keys
+            keys.append(form.cleaned_data.get('public_key'))
+            ldapuser.ssh_keys = keys
+            ldapuser.save()
+            messages.info(
+                req,
+                _('Added SSH key {key_hash}').format(
+                    key_hash=form.key.hash_sha256()))
+        else:
+            messages.error(req, _('Invalid public key.'))
+    return shortcuts.redirect(urlresolvers.reverse('profile:ssh_keys'))
diff --git a/striker/templates/profile/settings/ssh-keys.html 
b/striker/templates/profile/settings/ssh-keys.html
index a667677..4c19d62 100644
--- a/striker/templates/profile/settings/ssh-keys.html
+++ b/striker/templates/profile/settings/ssh-keys.html
@@ -47,5 +47,19 @@
   </div>
   {% endfor %}
 </div>
+<div class="panel panel-info">
+  <div class="panel-heading">
+    <h3 class="panel-title"><span class="fa-stack">{% fa_icon "square-o" 
"stack-2x" "fw" aria_hidden="true" %}{% fa_icon "key" "stack-1x" "fw" 
aria_hidden="true" %}</span> {% trans "New SSH key" %}</h3>
+  </div>
+  <div class="panel-body">
+    <form method="post" action="{% url 'profile:ssh_key_add' %}" class="form">
+      {% csrf_token %}
+      {% bootstrap_form new_key %}
+      {% buttons %}
+      <button class="btn btn-primary" type="submit">{% trans "Add SSH key" 
%}</button>
+      {% endbuttons %}
+    </form>
+  </div>
+</div>
 {% endblock %}
 {# vim:sw=2:ts=2:sts=2:et: #}

-- 
To view, visit https://gerrit.wikimedia.org/r/328403
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic01693ee543779f7f5e5df272f2a3e793f478030
Gerrit-PatchSet: 2
Gerrit-Project: labs/striker
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <bda...@wikimedia.org>
Gerrit-Reviewer: Andrew Bogott <abog...@wikimedia.org>
Gerrit-Reviewer: BryanDavis <bda...@wikimedia.org>
Gerrit-Reviewer: Madhuvishy <mviswanat...@wikimedia.org>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: Yuvipanda <yuvipa...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to