Adds SSH Keys to the PolyGerrit settings screen
Introduces the gr-ssh-editor element which is added to gr-settings-view. The new element lists the SSH public keys associated with the user's account, allows the user to view the public key, allows the user to remove keys from their account, and allows the user to add new keys. Bug: Issue 3911 Change-Id: I1b5b610836c15341482dcc917a43f8a76b77daeb
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<!--
|
||||
Copyright (C) 2016 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<link rel="import" href="../../../bower_components/polymer/polymer.html">
|
||||
<link rel="import" href="../../../bower_components/iron-autogrow-textarea/iron-autogrow-textarea.html">
|
||||
<link rel="import" href="../../../styles/gr-settings-styles.html">
|
||||
<link rel="import" href="../../shared/gr-button/gr-button.html">
|
||||
<link rel="import" href="../../shared/gr-overlay/gr-overlay.html">
|
||||
<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
|
||||
|
||||
<dom-module id="gr-ssh-editor">
|
||||
<template>
|
||||
<style>
|
||||
.commentHeader {
|
||||
width: 27em;
|
||||
}
|
||||
.statusHeader {
|
||||
width: 4em;
|
||||
}
|
||||
.keyHeader {
|
||||
width: 7.5em;
|
||||
}
|
||||
#viewKeyOverlay {
|
||||
padding: 2em;
|
||||
width: 50em;
|
||||
}
|
||||
.publicKey {
|
||||
font-family: var(--monospace-font-family);
|
||||
overflow-x: scroll;
|
||||
overflow-wrap: break-word;
|
||||
width: 30em;
|
||||
}
|
||||
.closeButton {
|
||||
bottom: 2em;
|
||||
position: absolute;
|
||||
right: 2em;
|
||||
}
|
||||
</style>
|
||||
<style include="gr-settings-styles"></style>
|
||||
<div class="gr-settings-styles">
|
||||
<fieldset>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="commentHeader">Comment</th>
|
||||
<th class="statusHeader">Status</th>
|
||||
<th class="keyHeader">Public Key</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template is="dom-repeat" items="[[_keys]]" as="key">
|
||||
<tr>
|
||||
<td>[[key.comment]]</td>
|
||||
<td>[[_getStatusLabel(key.valid)]]</td>
|
||||
<td>
|
||||
<gr-button
|
||||
on-tap="_showKey"
|
||||
data-index$="[[index]]"
|
||||
link>Click to View</gr-button>
|
||||
</td>
|
||||
<td>
|
||||
<gr-button
|
||||
data-index$="[[index]]"
|
||||
on-tap="_handleDeleteKey">Delete</gr-button>
|
||||
</td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
<gr-overlay id="viewKeyOverlay" with-backdrop>
|
||||
<fieldset>
|
||||
<section>
|
||||
<span class="title">Algorithm</span>
|
||||
<span class="value">[[_keyToView.algorithm]]</span>
|
||||
</section>
|
||||
<section>
|
||||
<span class="title">Public Key</span>
|
||||
<span class="value publicKey">[[_keyToView.encoded_key]]</span>
|
||||
</section>
|
||||
<section>
|
||||
<span class="title">Comment</span>
|
||||
<span class="value">[[_keyToView.comment]]</span>
|
||||
</section>
|
||||
</fieldset>
|
||||
<gr-button
|
||||
class="closeButton"
|
||||
on-tap="_closeOverlay">Close</gr-button>
|
||||
</gr-overlay>
|
||||
<gr-button
|
||||
on-tap="save"
|
||||
disabled$="[[!hasUnsavedChanges]]">Save Changes</gr-button>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<section>
|
||||
<span class="title">New SSH Key</span>
|
||||
<span class="value">
|
||||
<iron-autogrow-textarea
|
||||
id="newKey"
|
||||
bind-value="{{_newKey}}"
|
||||
placeholder="New SSH Key"></iron-autogrow-textarea>
|
||||
</span>
|
||||
</section>
|
||||
<gr-button
|
||||
id="addButton"
|
||||
disabled$="[[_computeAddButtonDisabled(_newKey)]]"
|
||||
on-tap="_handleAddKey">Add New SSH Key</gr-button>
|
||||
</fieldset>
|
||||
</div>
|
||||
<gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
|
||||
</template>
|
||||
<script src="gr-ssh-editor.js"></script>
|
||||
</dom-module>
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2016 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
Polymer({
|
||||
is: 'gr-ssh-editor',
|
||||
|
||||
properties: {
|
||||
hasUnsavedChanges: {
|
||||
type: Boolean,
|
||||
value: false,
|
||||
notify: true,
|
||||
},
|
||||
_keys: Array,
|
||||
_keyToView: Object,
|
||||
_newKey: {
|
||||
type: String,
|
||||
value: '',
|
||||
},
|
||||
_keysToRemove: {
|
||||
type: Array,
|
||||
value: function() { return []; },
|
||||
},
|
||||
},
|
||||
|
||||
loadData: function() {
|
||||
return this.$.restAPI.getAccountSSHKeys().then(function(keys) {
|
||||
this._keys = keys;
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
save: function() {
|
||||
var promises = this._keysToRemove.map(function(key) {
|
||||
this.$.restAPI.deleteAccountSSHKey(key.seq);
|
||||
}.bind(this));
|
||||
|
||||
return Promise.all(promises).then(function() {
|
||||
this._keysToRemove = [];
|
||||
this.hasUnsavedChanges = false;
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
_getStatusLabel: function(isValid) {
|
||||
return isValid ? 'Valid' : 'Invalid';
|
||||
},
|
||||
|
||||
_showKey: function(e) {
|
||||
var index = parseInt(e.target.getAttribute('data-index'), 10);
|
||||
this._keyToView = this._keys[index];
|
||||
this.$.viewKeyOverlay.open();
|
||||
},
|
||||
|
||||
_closeOverlay: function() {
|
||||
this.$.viewKeyOverlay.close();
|
||||
},
|
||||
|
||||
_handleDeleteKey: function(e) {
|
||||
var index = parseInt(e.target.getAttribute('data-index'), 10);
|
||||
this.push('_keysToRemove', this._keys[index]);
|
||||
this.splice('_keys', index, 1);
|
||||
this.hasUnsavedChanges = true;
|
||||
},
|
||||
|
||||
_handleAddKey: function() {
|
||||
this.$.addButton.disabled = true;
|
||||
this.$.newKey.disabled = true;
|
||||
return this.$.restAPI.addAccountSSHKey(this._newKey.trim())
|
||||
.then(function(key) {
|
||||
this.$.newKey.disabled = false;
|
||||
this._newKey = '';
|
||||
this.push('_keys', key);
|
||||
}.bind(this))
|
||||
.catch(function() {
|
||||
this.$.addButton.disabled = false;
|
||||
this.$.newKey.disabled = false;
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
_computeAddButtonDisabled: function(newKey) {
|
||||
return !newKey.length;
|
||||
},
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,177 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright (C) 2016 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
|
||||
<title>gr-ssh-editor</title>
|
||||
|
||||
<script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
|
||||
<script src="../../../bower_components/web-component-tester/browser.js"></script>
|
||||
|
||||
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
|
||||
<link rel="import" href="gr-ssh-editor.html">
|
||||
|
||||
<test-fixture id="basic">
|
||||
<template>
|
||||
<gr-ssh-editor></gr-ssh-editor>
|
||||
</template>
|
||||
</test-fixture>
|
||||
|
||||
<script>
|
||||
suite('gr-ssh-editor tests', function() {
|
||||
var element;
|
||||
var keys;
|
||||
|
||||
setup(function(done) {
|
||||
keys = [{
|
||||
seq: 1,
|
||||
ssh_public_key: 'ssh-rsa <key 1> comment-one@machine-one',
|
||||
encoded_key: '<key 1>',
|
||||
algorithm: 'ssh-rsa',
|
||||
comment: 'comment-one@machine-one',
|
||||
valid: true,
|
||||
}, {
|
||||
seq: 2,
|
||||
ssh_public_key: 'ssh-rsa <key 2> comment-two@machine-two',
|
||||
encoded_key: '<key 2>',
|
||||
algorithm: 'ssh-rsa',
|
||||
comment: 'comment-two@machine-two',
|
||||
valid: true,
|
||||
}];
|
||||
|
||||
stub('gr-rest-api-interface', {
|
||||
getAccountSSHKeys: function() { return Promise.resolve(keys); },
|
||||
});
|
||||
|
||||
element = fixture('basic');
|
||||
|
||||
element.loadData().then(function() { flush(done); });
|
||||
});
|
||||
|
||||
test('renders', function() {
|
||||
var rows = Polymer.dom(element.root).querySelectorAll('tbody tr');
|
||||
|
||||
assert.equal(rows.length, 2);
|
||||
|
||||
var cells = rows[0].querySelectorAll('td');
|
||||
assert.equal(cells[0].textContent, keys[0].comment);
|
||||
|
||||
cells = rows[1].querySelectorAll('td');
|
||||
assert.equal(cells[0].textContent, keys[1].comment);
|
||||
});
|
||||
|
||||
test('remove key', function(done) {
|
||||
var lastKey = keys[1];
|
||||
|
||||
var saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey',
|
||||
function() { return Promise.resolve(); });
|
||||
|
||||
assert.equal(element._keysToRemove.length, 0);
|
||||
assert.isFalse(element.hasUnsavedChanges);
|
||||
|
||||
// Get the delete button for the last row.
|
||||
var button = Polymer.dom(element.root).querySelector(
|
||||
'tbody tr:last-of-type td:nth-child(4) gr-button');
|
||||
|
||||
MockInteractions.tap(button);
|
||||
|
||||
assert.equal(element._keys.length, 1);
|
||||
assert.equal(element._keysToRemove.length, 1);
|
||||
assert.equal(element._keysToRemove[0], lastKey);
|
||||
assert.isTrue(element.hasUnsavedChanges);
|
||||
assert.isFalse(saveStub.called);
|
||||
|
||||
element.save().then(function() {
|
||||
assert.isTrue(saveStub.called);
|
||||
assert.equal(saveStub.lastCall.args[0], lastKey.seq);
|
||||
assert.equal(element._keysToRemove.length, 0);
|
||||
assert.isFalse(element.hasUnsavedChanges);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('show key', function() {
|
||||
var openSpy = sinon.spy(element.$.viewKeyOverlay, 'open');
|
||||
|
||||
// Get the show button for the last row.
|
||||
var button = Polymer.dom(element.root).querySelector(
|
||||
'tbody tr:last-of-type td:nth-child(3) gr-button');
|
||||
|
||||
MockInteractions.tap(button);
|
||||
|
||||
assert.equal(element._keyToView, keys[1]);
|
||||
assert.isTrue(openSpy.called);
|
||||
});
|
||||
|
||||
test('add key', function(done) {
|
||||
var newKeyString = 'ssh-rsa <key 3> comment-three@machine-three';
|
||||
var newKeyObject = {
|
||||
seq: 3,
|
||||
ssh_public_key: newKeyString,
|
||||
encoded_key: '<key 3>',
|
||||
algorithm: 'ssh-rsa',
|
||||
comment: 'comment-three@machine-three',
|
||||
valid: true,
|
||||
};
|
||||
|
||||
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
|
||||
function() { return Promise.resolve(newKeyObject); });
|
||||
|
||||
element._newKey = newKeyString;
|
||||
|
||||
assert.isFalse(element.$.addButton.disabled);
|
||||
assert.isFalse(element.$.newKey.disabled);
|
||||
|
||||
element._handleAddKey().then(function() {
|
||||
assert.isTrue(element.$.addButton.disabled);
|
||||
assert.isFalse(element.$.newKey.disabled);
|
||||
assert.equal(element._keys.length, 3);
|
||||
done();
|
||||
});
|
||||
|
||||
assert.isTrue(element.$.addButton.disabled);
|
||||
assert.isTrue(element.$.newKey.disabled);
|
||||
|
||||
assert.isTrue(addStub.called);
|
||||
assert.equal(addStub.lastCall.args[0], newKeyString);
|
||||
});
|
||||
|
||||
test('add invalid key', function(done) {
|
||||
var newKeyString = 'not even close to valid';
|
||||
|
||||
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
|
||||
function() { return Promise.reject(); });
|
||||
|
||||
element._newKey = newKeyString;
|
||||
|
||||
assert.isFalse(element.$.addButton.disabled);
|
||||
assert.isFalse(element.$.newKey.disabled);
|
||||
|
||||
element._handleAddKey().then(function() {
|
||||
assert.isFalse(element.$.addButton.disabled);
|
||||
assert.isFalse(element.$.newKey.disabled);
|
||||
assert.equal(element._keys.length, 2);
|
||||
done();
|
||||
});
|
||||
|
||||
assert.isTrue(element.$.addButton.disabled);
|
||||
assert.isTrue(element.$.newKey.disabled);
|
||||
|
||||
assert.isTrue(addStub.called);
|
||||
assert.equal(addStub.lastCall.args[0], newKeyString);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user