Commit e5be436c authored by Luke Bennett's avatar Luke Bennett

Async regenerate public ssh key

parent ff3de503
...@@ -35,6 +35,7 @@ export default class MirrorPull { ...@@ -35,6 +35,7 @@ export default class MirrorPull {
this.$dropdownAuthType.on('change', e => this.handleAuthTypeChange(e)); this.$dropdownAuthType.on('change', e => this.handleAuthTypeChange(e));
this.$btnDetectHostKeys.on('click', e => this.handleDetectHostKeys(e)); this.$btnDetectHostKeys.on('click', e => this.handleDetectHostKeys(e));
this.$btnSSHHostsShowAdvanced.on('click', e => this.handleSSHHostsAdvanced(e)); this.$btnSSHHostsShowAdvanced.on('click', e => this.handleSSHHostsAdvanced(e));
this.$regeneratePublicSshKeyButton.on('click', e => this.regeneratePublicSshKey(e));
} }
/** /**
...@@ -77,37 +78,42 @@ export default class MirrorPull { ...@@ -77,37 +78,42 @@ export default class MirrorPull {
// Make backOff polling to get data // Make backOff polling to get data
backOff((next, stop) => { backOff((next, stop) => {
axios.get(`${projectMirrorSSHEndpoint}?ssh_url=${repositoryUrl}`) axios
.then(({ data, status }) => { .get(`${projectMirrorSSHEndpoint}?ssh_url=${repositoryUrl}`)
if (status === 204) { .then(({ data, status }) => {
this.backOffRequestCounter += 1; if (status === 204) {
if (this.backOffRequestCounter < 3) { this.backOffRequestCounter += 1;
next(); if (this.backOffRequestCounter < 3) {
next();
} else {
stop(data);
}
} else { } else {
stop(data); stop(data);
} }
} else { })
stop(data); .catch(stop);
})
.then(res => {
$btnLoadSpinner.addClass('hidden');
// Once data is received, we show verification info along with Host keys and fingerprints
this.$hostKeysInformation
.find('.js-fingerprint-verification')
.collapse(res.changes_project_import_data ? 'hide' : 'show');
if (res.known_hosts && res.fingerprints) {
this.showSSHInformation(res);
} }
}) })
.catch(stop); .catch(({ response }) => {
}) // Show failure message when there's an error and re-enable Detect host keys button
.then((res) => { const failureMessage = response.data
$btnLoadSpinner.addClass('hidden'); ? response.data.message
// Once data is received, we show verification info along with Host keys and fingerprints : __('An error occurred while detecting host keys');
this.$hostKeysInformation.find('.js-fingerprint-verification').collapse(res.changes_project_import_data ? 'hide' : 'show'); Flash(failureMessage);
if (res.known_hosts && res.fingerprints) {
this.showSSHInformation(res);
}
})
.catch(({ response }) => {
// Show failure message when there's an error and re-enable Detect host keys button
const failureMessage = response.data ? response.data.message : __('An error occurred while detecting host keys');
Flash(failureMessage);
$btnLoadSpinner.addClass('hidden'); $btnLoadSpinner.addClass('hidden');
this.$btnDetectHostKeys.enable(); this.$btnDetectHostKeys.enable();
}); });
} }
/** /**
...@@ -151,31 +157,31 @@ export default class MirrorPull { ...@@ -151,31 +157,31 @@ export default class MirrorPull {
// This request should happen only if selected Auth type was SSH // This request should happen only if selected Auth type was SSH
// and SSH Public key was not present on page load // and SSH Public key was not present on page load
if (selectedAuthType === AUTH_METHOD.SSH && if (selectedAuthType === AUTH_METHOD.SSH && !$sshPublicKey.text().trim()) {
!$sshPublicKey.text().trim()) {
this.$wellAuthTypeChanging.collapse('show'); this.$wellAuthTypeChanging.collapse('show');
this.$dropdownAuthType.disable(); this.$dropdownAuthType.disable();
axios.put(projectMirrorAuthTypeEndpoint, JSON.stringify(authTypeData), { axios
headers: { .put(projectMirrorAuthTypeEndpoint, JSON.stringify(authTypeData), {
'Content-Type': 'application/json; charset=utf-8', headers: {
}, 'Content-Type': 'application/json; charset=utf-8',
}) },
.then(({ data }) => { })
// Show SSH public key container and fill in public key .then(({ data }) => {
this.toggleAuthWell(selectedAuthType); // Show SSH public key container and fill in public key
this.toggleSSHAuthWellMessage(true); this.toggleAuthWell(selectedAuthType);
this.setSSHPublicKey(data.import_data_attributes.ssh_public_key); this.toggleSSHAuthWellMessage(true);
this.setSSHPublicKey(data.import_data_attributes.ssh_public_key);
this.$wellAuthTypeChanging.collapse('hide'); this.$wellAuthTypeChanging.collapse('hide');
this.$dropdownAuthType.enable(); this.$dropdownAuthType.enable();
}) })
.catch(() => { .catch(() => {
Flash(__('Something went wrong on our end.')); Flash(__('Something went wrong on our end.'));
this.$wellAuthTypeChanging.collapse('hide'); this.$wellAuthTypeChanging.collapse('hide');
this.$dropdownAuthType.enable(); this.$dropdownAuthType.enable();
}); });
} else { } else {
this.toggleAuthWell(selectedAuthType); this.toggleAuthWell(selectedAuthType);
this.$wellSSHAuth.find('.js-ssh-public-key-present').collapse('show'); this.$wellSSHAuth.find('.js-ssh-public-key-present').collapse('show');
...@@ -189,7 +195,7 @@ export default class MirrorPull { ...@@ -189,7 +195,7 @@ export default class MirrorPull {
showSSHInformation(sshHostKeys) { showSSHInformation(sshHostKeys) {
const $fingerprintsList = this.$hostKeysInformation.find('.js-fingerprints-list'); const $fingerprintsList = this.$hostKeysInformation.find('.js-fingerprints-list');
let fingerprints = ''; let fingerprints = '';
sshHostKeys.fingerprints.forEach((fingerprint) => { sshHostKeys.fingerprints.forEach(fingerprint => {
const escFingerprints = _.escape(fingerprint.fingerprint); const escFingerprints = _.escape(fingerprint.fingerprint);
fingerprints += `<code>${escFingerprints}</code>`; fingerprints += `<code>${escFingerprints}</code>`;
}); });
...@@ -223,6 +229,34 @@ export default class MirrorPull { ...@@ -223,6 +229,34 @@ export default class MirrorPull {
*/ */
setSSHPublicKey(sshPublicKey) { setSSHPublicKey(sshPublicKey) {
this.$sshPublicKeyWrap.find('.ssh-public-key').text(sshPublicKey); this.$sshPublicKeyWrap.find('.ssh-public-key').text(sshPublicKey);
this.$sshPublicKeyWrap.find('.btn-copy-ssh-public-key').attr('data-clipboard-text', sshPublicKey); this.$sshPublicKeyWrap
.find('.btn-copy-ssh-public-key')
.attr('data-clipboard-text', sshPublicKey);
}
regeneratePublicSshKey(event) {
event.preventDefault();
const button = this.$regeneratePublicSshKeyButton;
if (!window.confirm(button.data('confirm'))) return; // eslint-disable-line no-alert
const spinner = $('.js-spinner', button);
const endpoint = button.data('endpoint');
button.attr('disabled', 'disabled');
spinner.removeClass('hide');
axios
.patch(endpoint)
.then(({ data }) => {
button.removeAttr('disabled');
spinner.addClass('hide');
this.setSSHPublicKey(data.import_data_attributes.ssh_public_key);
})
.catch(() => {
Flash(_('Unable to regenerate public ssh key.'));
});
} }
} }
...@@ -26,8 +26,9 @@ ...@@ -26,8 +26,9 @@
= import_data.ssh_public_key = import_data.ssh_public_key
= clipboard_button(text: import_data.ssh_public_key, title: _("Copy SSH public key to clipboard"), class: 'prepend-top-10 btn-copy-ssh-public-key') = clipboard_button(text: import_data.ssh_public_key, title: _("Copy SSH public key to clipboard"), class: 'prepend-top-10 btn-copy-ssh-public-key')
= button_tag _('Regenerate key'), = button_tag type: 'button',
type: 'button',
data: { endpoint: project_mirror_path(@project, project: { import_data_attributes: regen_data }), data: { endpoint: project_mirror_path(@project, project: { import_data_attributes: regen_data }),
confirm: _('Are you sure you want to regenerate public key? You will have to update the public key on the remote server before mirroring will work again.') }, confirm: _('Are you sure you want to regenerate public key? You will have to update the public key on the remote server before mirroring will work again.') },
class: "btn btn-inverted btn-warning prepend-top-10 js-btn-regenerate-ssh-key#{ ' collapse' unless ssh_public_key_present }" class: "btn btn-inverted btn-warning prepend-top-10 js-btn-regenerate-ssh-key#{ ' collapse' unless ssh_public_key_present }" do
= sprite_icon('spinner', css_class: 'fa-spin hide js-spinner')
= _('Regenerate key')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment