Skip to content

Commit a4ade9d

Browse files
authored
Merge pull request #149 from couchbaselabs/add_ldap_support
adding ldap support
2 parents 70be128 + 70db812 commit a4ade9d

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

src/main/java/com/couchbase/intellij/database/ActiveCluster.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.couchbase.client.core.cnc.EventBus;
44
import com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent;
5+
import com.couchbase.client.core.env.PasswordAuthenticator;
56
import com.couchbase.client.java.Cluster;
67
import com.couchbase.client.java.ClusterOptions;
78
import com.couchbase.intellij.database.entity.CouchbaseBucket;
@@ -98,9 +99,20 @@ public void run(@NotNull ProgressIndicator indicator) {
9899
try {
99100
String password = DataLoader.getClusterPassword(savedCluster);
100101

102+
ClusterOptions options;
103+
if (!savedCluster.getLDAP()) {
104+
options = ClusterOptions.clusterOptions(savedCluster.getUsername(), password);
105+
} else {
106+
PasswordAuthenticator authenticator = PasswordAuthenticator.builder().username(savedCluster.getUsername())
107+
.password(password)
108+
.onlyEnablePlainSaslMechanism().build();
109+
110+
options = ClusterOptions.clusterOptions(authenticator);
111+
}
112+
101113
Cluster cluster = Cluster.connect(savedCluster.getUrl()
102-
+ (savedCluster.getQueryParams() ==null? "": savedCluster.getQueryParams() ),
103-
ClusterOptions.clusterOptions(savedCluster.getUsername(), password).environment(env -> {
114+
+ (savedCluster.getQueryParams() == null ? "" : savedCluster.getQueryParams()),
115+
options.environment(env -> {
104116
// env.applyProfile("wan-development");
105117
}));
106118

@@ -154,7 +166,7 @@ public void run(@NotNull ProgressIndicator indicator) {
154166
//Notify Listeners that we connected to a new cluster.
155167
//NOTE: Only singletons can register here, otherwise we will get a memory leak
156168
CompletableFuture.runAsync(() -> {
157-
for(Runnable run: newConnectionListener) {
169+
for (Runnable run : newConnectionListener) {
158170
try {
159171
run.run();
160172
} catch (Exception e) {

src/main/java/com/couchbase/intellij/database/DataLoader.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.couchbase.intellij.database;
22

3+
import com.couchbase.client.core.env.PasswordAuthenticator;
34
import com.couchbase.client.core.error.DocumentNotFoundException;
45
import com.couchbase.client.core.error.IndexFailureException;
56
import com.couchbase.client.core.error.PlanningFailureException;
@@ -418,11 +419,24 @@ public static String adjustClusterProtocol(String cluster, boolean ssl) {
418419
return protocol + cluster;
419420
}
420421

421-
public static Set<String> listBucketNames(String clusterUrl, boolean ssl, String username, String password) {
422+
public static Set<String> listBucketNames(String clusterUrl, boolean ssl, String username, String password, boolean ldap) {
422423

423424
Cluster cluster = null;
424425
try {
425-
cluster = Cluster.connect(adjustClusterProtocol(clusterUrl, ssl), ClusterOptions.clusterOptions(username, password).environment(env -> {
426+
427+
ClusterOptions options;
428+
429+
if (!ldap) {
430+
options = ClusterOptions.clusterOptions(username, password);
431+
} else {
432+
PasswordAuthenticator authenticator = PasswordAuthenticator.builder().username(username)
433+
.password(password)
434+
.onlyEnablePlainSaslMechanism().build();
435+
436+
options = ClusterOptions.clusterOptions(authenticator);
437+
}
438+
439+
cluster = Cluster.connect(adjustClusterProtocol(clusterUrl, ssl), options.environment(env -> {
426440
//env.applyProfile("wan-development");
427441
}));
428442
cluster.waitUntilReady(Duration.ofSeconds(5));
@@ -437,7 +451,7 @@ public static Set<String> listBucketNames(String clusterUrl, boolean ssl, String
437451

438452
}
439453

440-
public static SavedCluster saveDatabaseCredentials(String name, String url, String queryParams, boolean isSSL, String username, String password, String defaultBucket) {
454+
public static SavedCluster saveDatabaseCredentials(String name, String url, String queryParams, boolean isSSL, String username, String password, String defaultBucket, Boolean ldap) {
441455
String key = username + ":" + name;
442456
SavedCluster sc = new SavedCluster();
443457
sc.setId(key);
@@ -447,6 +461,7 @@ public static SavedCluster saveDatabaseCredentials(String name, String url, Stri
447461
sc.setUsername(username);
448462
sc.setUrl(adjustClusterProtocol(url, isSSL));
449463
sc.setDefaultBucket(defaultBucket);
464+
sc.setLDAP(ldap);
450465

451466
Clusters clusters = ClustersStorage.getInstance().getValue();
452467

src/main/java/com/couchbase/intellij/persistence/SavedCluster.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ public class SavedCluster {
1919
private String color;
2020

2121
private boolean sslEnable;
22+
23+
private Boolean ldap;
2224
private String defaultBucket;
2325

2426
private Boolean readOnly;
2527

2628
private Long inferCachePeriod;
2729

30+
public Boolean getLDAP() {
31+
if (ldap == null) {
32+
return false;
33+
}
34+
return ldap;
35+
}
36+
37+
public void setLDAP(Boolean LDAP) {
38+
ldap = LDAP;
39+
}
40+
2841
public String getId() {
2942
return id;
3043
}

src/main/java/com/couchbase/intellij/tree/NewConnectionDialog.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class NewConnectionDialog extends DialogWrapper {
4444
private JBTextField connectionNameTextField;
4545
private JBTextField hostTextField;
4646
private JCheckBox enableSSLCheckBox;
47+
private JCheckBox ldapAuthCheckbox;
4748
private JBTextField usernameTextField;
4849
private JPasswordField passwordField;
4950
private JButton testConnectionButton;
@@ -55,7 +56,7 @@ public class NewConnectionDialog extends DialogWrapper {
5556
private JPasswordField apiSecretField;
5657
private JPanel wrapperPanel;
5758

58-
private DefaultMutableTreeNode clickedNode;
59+
private DefaultMutableTreeNode clickedNode;
5960

6061
public NewConnectionDialog(Project project, Tree tree, SavedCluster savedCluster, DefaultMutableTreeNode clickedNode) {
6162
super(false);
@@ -159,7 +160,6 @@ private void handleSaveConnection() {
159160
}
160161
} catch (Exception ex) {
161162
Log.error(ex);
162-
ex.printStackTrace();
163163
showErrorLabel("<html>Connection failed.<br>Please double-check your credentials" + (defaultBucketTextField.getText().trim().isEmpty() ? " or inform a Bucket on <strong>Advanced Settings</strong> -> <strong>Troubleshooting</strong> to inspect your connection" : "") + "</html>");
164164
saveButton.setEnabled(true);
165165
messageLabel.setText("");
@@ -168,11 +168,15 @@ private void handleSaveConnection() {
168168

169169
try {
170170

171-
if(this.savedCluster != null) {
171+
if (this.savedCluster != null) {
172172
ConnectionNodeDescriptor userObject = (ConnectionNodeDescriptor) clickedNode.getUserObject();
173173
TreeActionHandler.deleteConnection(clickedNode, userObject, tree);
174174
}
175-
SavedCluster sc = DataLoader.saveDatabaseCredentials(connectionNameTextField.getText(), getBaseUrl(hostTextField.getText()), getQueryParams(hostTextField.getText()), enableSSLCheckBox.isSelected(), usernameTextField.getText(), String.valueOf(passwordField.getPassword()), defaultBucketTextField.getText().trim().isEmpty() ? null : defaultBucketTextField.getText());
175+
SavedCluster sc = DataLoader.saveDatabaseCredentials(connectionNameTextField.getText(),
176+
getBaseUrl(hostTextField.getText()), getQueryParams(hostTextField.getText()),
177+
enableSSLCheckBox.isSelected(), usernameTextField.getText(), String.valueOf(passwordField.getPassword()),
178+
defaultBucketTextField.getText().trim().isEmpty() ? null : defaultBucketTextField.getText(),
179+
ldapAuthCheckbox.isSelected());
176180
messageLabel.setText("Connection was successful");
177181
TreeActionHandler.connectToCluster(project, sc, tree, null);
178182
close(DialogWrapper.CANCEL_EXIT_CODE);
@@ -184,7 +188,6 @@ private void handleSaveConnection() {
184188
showErrorLabel("The Couchbase cluster URL and username already exists.");
185189
} catch (Exception ex) {
186190
Log.error(ex);
187-
ex.printStackTrace();
188191
messageLabel.setText("");
189192
showErrorLabel("Could not save the database credentials");
190193
}
@@ -239,7 +242,9 @@ private void handleTestConnection() {
239242
}
240243

241244
private boolean hasCorrectBucketConnection() {
242-
Set<String> buckets = DataLoader.listBucketNames(hostTextField.getText(), enableSSLCheckBox.isSelected(), usernameTextField.getText(), String.valueOf(passwordField.getPassword()));
245+
Set<String> buckets = DataLoader.listBucketNames(hostTextField.getText(),
246+
enableSSLCheckBox.isSelected(), usernameTextField.getText(),
247+
String.valueOf(passwordField.getPassword()), ldapAuthCheckbox.isSelected());
243248

244249
if (!defaultBucketTextField.getText().trim().isEmpty()) {
245250
if (!buckets.contains(defaultBucketTextField.getText())) {
@@ -394,6 +399,7 @@ private JPanel createDatabasePanel() {
394399
hostTextField = new JBTextField(30);
395400
hostTextField.getEmptyText().setText("Your cluster URL");
396401
enableSSLCheckBox = new JCheckBox("Enable SSL");
402+
ldapAuthCheckbox = new JCheckBox("LDAP Auth");
397403

398404
gbc.fill = GridBagConstraints.HORIZONTAL;
399405
gbc.gridwidth = GridBagConstraints.REMAINDER;
@@ -427,14 +433,21 @@ private JPanel createDatabasePanel() {
427433
gbc.gridy = 3;
428434
gbc.gridx = 1;
429435
gbc.weightx = 0.7;
430-
firstPanel.add(TemplateUtil.createComponentWithBalloon(enableSSLCheckBox, "Check this if TLS is enabled in your cluster. If you specify 'couchbases://' protocol in your Connection String, this option will be checked automatically"), gbc);
431436

437+
JPanel checkboxPanel = new JPanel();
438+
checkboxPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
439+
checkboxPanel.add(TemplateUtil.createComponentWithBalloon(enableSSLCheckBox, "Check this if TLS is enabled in your cluster. If you specify 'couchbases://' protocol in your Connection String, this option will be checked automatically"));
440+
JBPanel ldapPanel = TemplateUtil.createComponentWithBalloon(ldapAuthCheckbox, "Check this if your authentication uses LDAP");
441+
ldapPanel.setBorder(JBUI.Borders.emptyLeft(10));
442+
checkboxPanel.add(ldapPanel);
443+
firstPanel.add(checkboxPanel, gbc);
432444
if (this.savedCluster != null) {
433445
connectionNameTextField.setText(this.savedCluster.getName());
434446
hostTextField.setText(this.savedCluster.getUrl());
435447
enableSSLCheckBox.setSelected(this.savedCluster.isSslEnable());
448+
ldapAuthCheckbox.setSelected(this.savedCluster.getLDAP());
436449
cleanURL();
437-
hostTextField.setText(hostTextField.getText()+ (savedCluster.getQueryParams()!=null?savedCluster.getQueryParams(): ""));
450+
hostTextField.setText(hostTextField.getText() + (savedCluster.getQueryParams() != null ? savedCluster.getQueryParams() : ""));
438451
}
439452

440453
return firstPanel;

0 commit comments

Comments
 (0)