Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip SSL Verification Flag #164

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.archicontribs.modelrepository.actions;

import java.io.File;
import java.net.URI;
import java.security.GeneralSecurityException;

import org.archicontribs.modelrepository.IModelRepositoryImages;
Expand All @@ -22,6 +23,9 @@
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
Expand All @@ -36,126 +40,141 @@
* 1. Check Primary Key
* 2. Get user credentials
* 3. Clone from Remote
* 4. If Grafico files exist load the model from the Grafico files and save it as temp file
* 5. If Grafico files do not exist create a new temp model and save it
* 4. If Grafico files exist load the model from the Grafico files and save it as temp file
* 5. If Grafico files do not exist create a new temp model and save it
* 6. Store user credentials if prefs agree
*/
public class CloneModelAction extends AbstractModelAction {

public CloneModelAction(IWorkbenchWindow window) {
super(window);
setImageDescriptor(IModelRepositoryImages.ImageFactory.getImageDescriptor(IModelRepositoryImages.ICON_CLONE));
setText(Messages.CloneModelAction_0);
setToolTipText(Messages.CloneModelAction_0);
}

@Override
public void run() {
// Check primary key set
try {
if(!EncryptedCredentialsStorage.checkPrimaryKeySet()) {
return;
}
}
catch(GeneralSecurityException ex) {
displayCredentialsErrorDialog(ex);
return;
}
catch(Exception ex) {
displayErrorDialog(Messages.CloneModelAction_0, ex);
return;
}

CloneInputDialog dialog = new CloneInputDialog(fWindow.getShell());
if(dialog.open() != Window.OK) {
return;
}

final String repoURL = dialog.getURL();
final boolean storeCredentials = dialog.doStoreCredentials();
final UsernamePassword npw = dialog.getUsernamePassword();

if(!StringUtils.isSet(repoURL)) {
return;
}

if(GraficoUtils.isHTTP(repoURL) && !StringUtils.isSet(npw.getUsername()) && npw.getPassword().length == 0) {
MessageDialog.openError(fWindow.getShell(),
Messages.CloneModelAction_0,
Messages.CloneModelAction_1);
return;
}

// Create a new local folder
File localRepoFolder = GraficoUtils.getUniqueLocalFolder(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), repoURL);
setRepository(new ArchiRepository(localRepoFolder));

try {
// Clone
Exception[] exception = new Exception[1];
IProgressService ps = PlatformUI.getWorkbench().getProgressService();
ps.busyCursorWhile(new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor pm) {
try {
// Update Proxy
ProxyAuthenticator.update();

pm.beginTask(Messages.CloneModelAction_4, -1);
getRepository().cloneModel(repoURL, npw, new ProgressMonitorWrapper(pm));
}
catch(Exception ex) {
exception[0] = ex;
}
finally {
// Clear Proxy
ProxyAuthenticator.clear();
}
}
});

if(exception[0] != null) {
throw exception[0];
}

// Load it from the Grafico files if we can
IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();

// We couldn't load it from Grafico so create a new blank model
if(graficoModel == null) {
// New one. This will open in the tree
IArchimateModel model = IEditorModelManager.INSTANCE.createNewModel();
model.setFile(getRepository().getTempModelFile());

// And Save it
IEditorModelManager.INSTANCE.saveModel(model);

// Export to Grafico
getRepository().exportModelToGraficoFiles();

// And do a first commit
getRepository().commitChanges(Messages.CloneModelAction_3, false);

// Save the checksum
getRepository().saveChecksum();
}

// Store repo credentials if HTTP and option is set
if(GraficoUtils.isHTTP(repoURL) && storeCredentials) {
EncryptedCredentialsStorage cs = EncryptedCredentialsStorage.forRepository(getRepository());
cs.store(npw);
}

// Notify listeners
notifyChangeListeners(IRepositoryListener.REPOSITORY_ADDED);
}
catch(Exception ex) {
displayErrorDialog(Messages.CloneModelAction_0, ex);
}
}

@Override
protected boolean shouldBeEnabled() {
return true;
}

public CloneModelAction(IWorkbenchWindow window) {
super(window);
setImageDescriptor(IModelRepositoryImages.ImageFactory.getImageDescriptor(IModelRepositoryImages.ICON_CLONE));
setText(Messages.CloneModelAction_0);
setToolTipText(Messages.CloneModelAction_0);
}

@Override
public void run() {
// Check primary key set
try {
if (!EncryptedCredentialsStorage.checkPrimaryKeySet()) {
return;
}
} catch (GeneralSecurityException ex) {
displayCredentialsErrorDialog(ex);
return;
} catch (Exception ex) {
displayErrorDialog(Messages.CloneModelAction_0, ex);
return;
}

CloneInputDialog dialog = new CloneInputDialog(fWindow.getShell());
if (dialog.open() != Window.OK) {
return;
}

final String repoURL = dialog.getURL();
final boolean storeCredentials = dialog.doStoreCredentials();
final UsernamePassword npw = dialog.getUsernamePassword();
final boolean skipSSLVerification = dialog.skipSSLverification();

if (!StringUtils.isSet(repoURL)) {
return;
}

if (GraficoUtils.isHTTP(repoURL) && !StringUtils.isSet(npw.getUsername()) && npw.getPassword().length == 0) {
MessageDialog.openError(fWindow.getShell(), Messages.CloneModelAction_0, Messages.CloneModelAction_1);
return;
}

// Create a new local folder
File localRepoFolder = GraficoUtils
.getUniqueLocalFolder(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), repoURL);
setRepository(new ArchiRepository(localRepoFolder));

if (skipSSLVerification)
disableSSLVerify(repoURL);
try {
// Clone
Exception[] exception = new Exception[1];
IProgressService ps = PlatformUI.getWorkbench().getProgressService();
ps.busyCursorWhile(new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor pm) {
try {
// Update Proxy
ProxyAuthenticator.update();

pm.beginTask(Messages.CloneModelAction_4, -1);
getRepository().cloneModel(repoURL, npw, new ProgressMonitorWrapper(pm));
} catch (Exception ex) {
exception[0] = ex;
} finally {
// Clear Proxy
ProxyAuthenticator.clear();
}
}
});

if (exception[0] != null) {
throw exception[0];
}

// Load it from the Grafico files if we can
IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();

// We couldn't load it from Grafico so create a new blank model
if (graficoModel == null) {
// New one. This will open in the tree
IArchimateModel model = IEditorModelManager.INSTANCE.createNewModel();
model.setFile(getRepository().getTempModelFile());

// And Save it
IEditorModelManager.INSTANCE.saveModel(model);

// Export to Grafico
getRepository().exportModelToGraficoFiles();

// And do a first commit
getRepository().commitChanges(Messages.CloneModelAction_3, false);

// Save the checksum
getRepository().saveChecksum();
}

// Store repo credentials if HTTP and option is set
if (GraficoUtils.isHTTP(repoURL) && storeCredentials) {
EncryptedCredentialsStorage cs = EncryptedCredentialsStorage.forRepository(getRepository());
cs.store(npw);
}

// Notify listeners
notifyChangeListeners(IRepositoryListener.REPOSITORY_ADDED);
} catch (Exception ex) {
displayErrorDialog(Messages.CloneModelAction_0, ex);
}
}

private void disableSSLVerify(String repoURL) {
try {

URI gitServer = new URI(repoURL);
if (gitServer.getScheme().equals("https")) {
FileBasedConfig config = SystemReader.getInstance().openUserConfig(null, FS.DETECTED);
synchronized (config) {
config.load();
config.setBoolean("http", "https://" + gitServer.getHost() + ':'
+ (gitServer.getPort() == -1 ? 443 : gitServer.getPort()), "sslVerify", false);
config.save();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected boolean shouldBeEnabled() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public class CloneInputDialog extends TitleAreaDialog {
private Text txtPassword;

private Button storeCredentialsButton;
private Button skipSSLButton;

private String URL;
private String username;
private char[] password;
private boolean doStoreCredentials;
private boolean skipSSLVerification;

public CloneInputDialog(Shell parentShell) {
super(parentShell);
Expand Down Expand Up @@ -82,6 +84,10 @@ public void modifyText(ModifyEvent e) {
txtPassword = createTextField(container, Messages.CloneInputDialog_4, SWT.PASSWORD);
createPreferenceButton(container);

skipSSLButton = new Button(container, SWT.CHECK);
skipSSLButton.setText(Messages.CloneInputDialog_btnCheckButton_text);
new Label(container, SWT.NONE);

return area;
}

Expand Down Expand Up @@ -116,13 +122,17 @@ protected void saveInput() {
password = txtPassword.getTextChars();
URL = txtURL.getText().trim();
doStoreCredentials = storeCredentialsButton.getSelection();
skipSSLVerification=skipSSLButton.getSelection();
}

@Override
protected void okPressed() {
saveInput();
super.okPressed();
}
public boolean skipSSLverification() {
return skipSSLVerification;
}

public UsernamePassword getUsernamePassword() {
return new UsernamePassword(username, password);
Expand All @@ -135,4 +145,5 @@ public String getURL() {
public boolean doStoreCredentials() {
return doStoreCredentials;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class Messages extends NLS {
public static String UserNamePasswordDialog_5;

public static String UserNamePasswordDialog_6;
public static String CloneInputDialog_btnCheckButton_text;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Expand Down
Loading