Skip to content

Commit cf26ad8

Browse files
authored
Merge pull request #187 from splunk/storagepasswords-wildcard-check
Storagepassword's wildcard check for create and delete actions
2 parents 2618f58 + a6ed62f commit cf26ad8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

splunk/src/main/java/com/splunk/PasswordCollection.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class PasswordCollection extends EntityCollection<Password> {
5050
* @return The new credential.
5151
*/
5252
public Password create(String name, String password) {
53+
if(checkForWildcards()){
54+
throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards.");
55+
}
5356
Args args = new Args("password", password);
5457
return create(name, args);
5558
}
@@ -63,6 +66,9 @@ public Password create(String name, String password) {
6366
* @return The new credential.
6467
*/
6568
public Password create(String name, String password, String realm) {
69+
if(checkForWildcards()){
70+
throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards.");
71+
}
6672
Args args = new Args();
6773
args.put("password", password);
6874
args.put("realm", realm);
@@ -97,11 +103,17 @@ public Password get(Object key) {
97103
* @return The removed credential, or null if not found.
98104
*/
99105
public Password remove(String realm, String name) {
106+
if(checkForWildcards()){
107+
throw new IllegalArgumentException("app context must be specified when removing a password.");
108+
}
100109
return super.remove(String.format("%s:%s:", realm, name));
101110
}
102111

103112
@Override
104113
public Password remove(String key) {
114+
if(checkForWildcards()){
115+
throw new IllegalArgumentException("app context must be specified when removing a password.");
116+
}
105117
// Make it compatible with the old way (low-efficient)
106118
if (!key.contains(":")) {
107119
Password password = getByUsername((String) key);
@@ -129,4 +141,12 @@ private Password getByUsername(String name) {
129141
}
130142
return null;
131143
}
144+
145+
private boolean checkForWildcards(){
146+
boolean isWildCard = false;
147+
if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){
148+
isWildCard = true;
149+
}
150+
return isWildCard;
151+
}
132152
}

splunk/src/test/java/com/splunk/PasswordTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.Assert;
2020
import org.junit.Test;
2121

22+
import java.util.HashMap;
23+
2224
public class PasswordTest extends SDKTestCase {
2325
@Test
2426
public void testGettersOfDefaultPasswords() {
@@ -129,4 +131,46 @@ public void testPasswordsCompatibleGetByName() {
129131
passwords.remove(username);
130132
Assert.assertNull(passwords.get(username));
131133
}
134+
@Test
135+
public void testPasswordsWithWildCards(){
136+
HashMap<String, Object> args = new HashMap<String, Object>();
137+
args = Command.defaultValues;
138+
args.put("owner", "-");
139+
args.put("app", "-");
140+
args.put("username", "admin");
141+
args.put("password", "changed!");
142+
Service service = Service.connect(args);
143+
PasswordCollection passwords = service.getPasswords();
144+
Assert.assertEquals(passwords.size(),0);
145+
146+
String name = "no-owner";
147+
String value = "sdk-test-password";
148+
String realm = "sdk-test-realm";
149+
150+
Password password;
151+
// Create a password
152+
try{
153+
password = passwords.create(name, value);
154+
}catch(IllegalArgumentException e){
155+
Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage());
156+
}
157+
try{
158+
password = passwords.create(name, value, realm);
159+
}catch(IllegalArgumentException e){
160+
Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage());
161+
}
162+
// Remove a password
163+
try{
164+
password = passwords.remove(name);
165+
}catch(IllegalArgumentException e){
166+
Assert.assertEquals("app context must be specified when removing a password.", e.getMessage());
167+
}
168+
try{
169+
password = passwords.remove(realm, name);
170+
}catch(IllegalArgumentException e){
171+
Assert.assertEquals("app context must be specified when removing a password.", e.getMessage());
172+
}
173+
passwords = service.getPasswords();
174+
Assert.assertEquals(passwords.size(),0);
175+
}
132176
}

0 commit comments

Comments
 (0)