Skip to content

Commit 9c2ccb4

Browse files
committed
make username/password configurable on project/repository level
fixes #1946
1 parent 6736a11 commit 9c2ccb4

File tree

6 files changed

+166
-25
lines changed

6 files changed

+166
-25
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Project.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public class Project implements Comparable<Project>, Nameable, Serializable {
7373
private int tabSize;
7474

7575
/**
76-
* A flag if the navigate window should be opened by default when browsing
77-
* the source code of this project.
76+
* If navigate window should be opened by default when browsing the source code of this project.
7877
*/
7978
private Boolean navigateWindowEnabled = null;
8079

@@ -93,6 +92,9 @@ public class Project implements Comparable<Project>, Nameable, Serializable {
9392
*/
9493
private Boolean mergeCommitsEnabled = null;
9594

95+
private String username = null;
96+
private String password = null;
97+
9698
/**
9799
* This marks the project as (not)ready before initial index is done. this
98100
* is to avoid all/multi-project searches referencing this project from
@@ -309,6 +311,22 @@ public void setHistoryBasedReindex(boolean flag) {
309311
this.historyBasedReindex = flag;
310312
}
311313

314+
public void setUsername(String username) {
315+
this.username = username;
316+
}
317+
318+
public String getUsername() {
319+
return username;
320+
}
321+
322+
public void setPassword(String password) {
323+
this.password = password;
324+
}
325+
326+
public String getPassword() {
327+
return password;
328+
}
329+
312330
@VisibleForTesting
313331
public void clearProperties() {
314332
historyBasedReindex = null;

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public class RepositoryInfo implements Serializable {
8181
private boolean mergeCommitsEnabled;
8282
@DTOElement
8383
private boolean historyBasedReindex;
84+
@DTOElement
85+
private String username;
86+
@DTOElement
87+
private String password;
8488

8589
/**
8690
* Empty constructor to support serialization.
@@ -102,6 +106,8 @@ public RepositoryInfo(RepositoryInfo orig) {
102106
this.handleRenamedFiles = orig.handleRenamedFiles;
103107
this.mergeCommitsEnabled = orig.mergeCommitsEnabled;
104108
this.historyBasedReindex = orig.historyBasedReindex;
109+
this.username = orig.username;
110+
this.password = orig.password;
105111
}
106112

107113
/**
@@ -157,6 +163,14 @@ public void setHistoryEnabled(boolean flag) {
157163
this.historyEnabled = flag;
158164
}
159165

166+
public String getUsername() { return username; }
167+
168+
public void setUsername(String username) { this.username = username; }
169+
170+
public String getPassword() { return password; }
171+
172+
public void setPassword(String password) { this.password = password; }
173+
160174
/**
161175
* @return relative path to source root
162176
*/
@@ -331,6 +345,8 @@ public void fillFromProject() {
331345
setHandleRenamedFiles(proj.isHandleRenamedFiles());
332346
setMergeCommitsEnabled(proj.isMergeCommitsEnabled());
333347
setHistoryBasedReindex(proj.isHistoryBasedReindex());
348+
setUsername(proj.getUsername());
349+
setPassword(proj.getPassword());
334350
} else {
335351
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
336352

@@ -385,6 +401,17 @@ public String toString() {
385401
stringBuilder.append("merge=");
386402
stringBuilder.append(this.isMergeCommitsEnabled());
387403
}
404+
405+
stringBuilder.append(",");
406+
if (getUsername() != null) {
407+
stringBuilder.append("username:set");
408+
stringBuilder.append(",");
409+
}
410+
if (getPassword() != null) {
411+
stringBuilder.append("password:set");
412+
stringBuilder.append(",");
413+
}
414+
388415
stringBuilder.append("}");
389416
return stringBuilder.toString();
390417
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/SubversionRepository.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.xml.parsers.DocumentBuilderFactory;
3636
import javax.xml.parsers.ParserConfigurationException;
3737

38+
import org.jetbrains.annotations.VisibleForTesting;
3839
import org.opengrok.indexer.configuration.CommandTimeoutType;
3940
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4041
import org.opengrok.indexer.logger.LoggerFactory;
@@ -57,9 +58,6 @@ public class SubversionRepository extends Repository {
5758

5859
private static final long serialVersionUID = 1L;
5960

60-
private static final String ENV_SVN_USERNAME = "OPENGROK_SUBVERSION_USERNAME";
61-
private static final String ENV_SVN_PASSWORD = "OPENGROK_SUBVERSION_PASSWORD";
62-
6361
/**
6462
* The property name used to obtain the client command for this repository.
6563
*/
@@ -365,12 +363,12 @@ public boolean isWorking() {
365363
return working;
366364
}
367365

368-
private List<String> getAuthCommandLineParams() {
366+
@VisibleForTesting
367+
List<String> getAuthCommandLineParams() {
369368
List<String> result = new ArrayList<>();
370-
String userName = System.getenv(ENV_SVN_USERNAME);
371-
String password = System.getenv(ENV_SVN_PASSWORD);
372-
if (userName != null && !userName.isEmpty() && password != null
373-
&& !password.isEmpty()) {
369+
String userName = getUsername();
370+
String password = getPassword();
371+
if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
374372
result.add("--username");
375373
result.add(userName);
376374
result.add("--password");

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/ProjectTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.configuration;
2424

@@ -40,14 +40,14 @@
4040
import static org.junit.jupiter.api.Assertions.assertNull;
4141
import static org.junit.jupiter.api.Assertions.assertTrue;
4242

43-
public class ProjectTest {
43+
class ProjectTest {
4444

4545
/**
4646
* Test that a {@code Project} instance can be encoded and decoded without
4747
* errors. Bug #3077.
4848
*/
4949
@Test
50-
public void testEncodeDecode() {
50+
void testEncodeDecode() {
5151
// Create an exception listener to detect errors while encoding and
5252
// decoding
5353
final LinkedList<Exception> exceptions = new LinkedList<>();
@@ -83,7 +83,7 @@ public void testEncodeDecode() {
8383
* Test project matching.
8484
*/
8585
@Test
86-
public void testGetProject() {
86+
void testGetProject() {
8787
// Create 2 projects, one being prefix of the other.
8888
Project foo = new Project("Project foo", "/foo");
8989
Project bar = new Project("Project foo-bar", "/foo-bar");
@@ -111,7 +111,7 @@ public void testGetProject() {
111111
* Test getProjectDescriptions().
112112
*/
113113
@Test
114-
public void testGetProjectDescriptions() {
114+
void testGetProjectDescriptions() {
115115
// Create 2 projects.
116116
Project foo = new Project("foo", "/foo");
117117
Project bar = new Project("bar", "/bar");
@@ -136,7 +136,7 @@ public void testGetProjectDescriptions() {
136136
* Insert the value from configuration.
137137
*/
138138
@Test
139-
public void testMergeProjects1() {
139+
void testMergeProjects1() {
140140
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
141141
env.setTabSize(new Configuration().getTabSize() + 3731);
142142
env.setNavigateWindowEnabled(!new Configuration().isNavigateWindowEnabled());
@@ -164,7 +164,7 @@ public void testMergeProjects1() {
164164
* Do not overwrite customized project property.
165165
*/
166166
@Test
167-
public void testMergeProjects2() {
167+
void testMergeProjects2() {
168168
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
169169
env.setTabSize(new Configuration().getTabSize() + 3731);
170170

@@ -199,7 +199,7 @@ public void testMergeProjects2() {
199199
* Create a project fill with defaults from the configuration.
200200
*/
201201
@Test
202-
public void testCreateProjectWithConfiguration() {
202+
void testCreateProjectWithConfiguration() {
203203
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
204204
env.setTabSize(4);
205205

@@ -209,7 +209,7 @@ public void testCreateProjectWithConfiguration() {
209209
}
210210

211211
@Test
212-
public void testEquality() {
212+
void testEquality() {
213213
Project g1 = new Project();
214214
Project g2 = new Project();
215215
assertEquals(g1, g2, "null == null");
@@ -224,4 +224,20 @@ public void testEquality() {
224224
assertEquals(g1, g1, "\"name\" == \"name\"");
225225
assertEquals(g2, g2, "\"NAME\" == \"NAME\"");
226226
}
227+
228+
@Test
229+
void testUsername() {
230+
Project project = new Project();
231+
final String username = "foo";
232+
project.setUsername(username);
233+
assertEquals(username, project.getUsername());
234+
}
235+
236+
@Test
237+
void testPassword() {
238+
Project project = new Project();
239+
final String password = "foo";
240+
project.setPassword(password);
241+
assertEquals(password, project.getPassword());
242+
}
227243
}

opengrok-indexer/src/test/java/org/opengrok/indexer/history/RepositoryInfoTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.File;
27+
import java.nio.file.Paths;
28+
import java.util.Map;
29+
2730
import org.junit.jupiter.api.BeforeEach;
2831
import org.junit.jupiter.api.Test;
32+
import org.opengrok.indexer.configuration.Project;
2933
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3034

3135
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -35,14 +39,14 @@
3539
*
3640
* @author Vladimir Kotal
3741
*/
38-
public class RepositoryInfoTest {
42+
class RepositoryInfoTest {
3943
@BeforeEach
4044
public void setUp() {
4145
RuntimeEnvironment.getInstance().setSourceRoot("/src");
4246
}
4347

4448
@Test
45-
public void testEquals() {
49+
void testEquals() {
4650
String repoDirectory = "/src/foo";
4751

4852
RepositoryInfo ri1 = new RepositoryInfo();
@@ -55,4 +59,43 @@ public void testEquals() {
5559
ri2.setDirectoryName(new File(repoDirectory));
5660
assertEquals(ri1, ri2);
5761
}
62+
63+
@Test
64+
void testUsername() {
65+
RepositoryInfo repositoryInfo = new RepositoryInfo();
66+
final String username = "foo";
67+
repositoryInfo.setUsername(username);
68+
assertEquals(username, repositoryInfo.getUsername());
69+
}
70+
71+
@Test
72+
void testPassword() {
73+
RepositoryInfo repositoryInfo = new RepositoryInfo();
74+
final String password = "foo";
75+
repositoryInfo.setPassword(password);
76+
assertEquals(password, repositoryInfo.getPassword());
77+
}
78+
79+
@Test
80+
void testFillFromProjectUsernamePassword() {
81+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
82+
83+
env.setProjectsEnabled(true);
84+
85+
final String projectName = "proj";
86+
Project project = new Project(projectName);
87+
final String dirName = "/proj";
88+
project.setPath(dirName);
89+
final String username = "foo";
90+
final String password = "bar";
91+
project.setUsername(username);
92+
project.setPassword(password);
93+
env.setProjects(Map.of(projectName, project));
94+
95+
RepositoryInfo repositoryInfo = new RepositoryInfo();
96+
repositoryInfo.setDirectoryNameRelative(dirName);
97+
repositoryInfo.fillFromProject();
98+
assertEquals(username, repositoryInfo.getUsername());
99+
assertEquals(password, repositoryInfo.getPassword());
100+
}
58101
}

opengrok-indexer/src/test/java/org/opengrok/indexer/history/SubversionRepositoryTest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Ric Harris <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import org.junit.jupiter.api.Test;
2727

2828
import java.text.ParseException;
29+
import java.util.List;
2930

31+
import static org.junit.jupiter.api.Assertions.assertEquals;
3032
import static org.junit.jupiter.api.Assertions.assertNotNull;
3133
import static org.junit.jupiter.api.Assertions.assertNull;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
3235

33-
public class SubversionRepositoryTest {
36+
class SubversionRepositoryTest {
3437

3538
@Test
36-
public void testDateFormats() {
39+
void testDateFormats() {
3740
String[][] tests = new String[][]{
3841
{"abcd", "expected exception"},
3942
{"2016-01-01 10:00:00", "expected exception"},
@@ -66,4 +69,40 @@ public void testDateFormats() {
6669
}
6770
}
6871
}
72+
73+
@Test
74+
void testUsernamePassword() {
75+
final SubversionRepository repository = new SubversionRepository();
76+
final String username = "foo";
77+
final String password= "bar";
78+
repository.setUsername(username);
79+
repository.setPassword(password);
80+
assertEquals(List.of("--username", username, "--password", password), repository.getAuthCommandLineParams());
81+
}
82+
83+
@Test
84+
void testNullUsernameNullPassword() {
85+
final SubversionRepository repository = new SubversionRepository();
86+
assertNull(repository.getUsername());
87+
assertNull(repository.getPassword());
88+
assertTrue(repository.getAuthCommandLineParams().isEmpty());
89+
}
90+
91+
@Test
92+
void testNullUsernameNonNullPassword() {
93+
final SubversionRepository repository = new SubversionRepository();
94+
final String password= "bar";
95+
assertNull(repository.getUsername());
96+
repository.setPassword(password);
97+
assertTrue(repository.getAuthCommandLineParams().isEmpty());
98+
}
99+
100+
@Test
101+
void testNonNullUsernameNullPassword() {
102+
final SubversionRepository repository = new SubversionRepository();
103+
final String username = "foo";
104+
assertNull(repository.getPassword());
105+
repository.setUsername(username);
106+
assertTrue(repository.getAuthCommandLineParams().isEmpty());
107+
}
69108
}

0 commit comments

Comments
 (0)