diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 4603160c..47a50a22 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -50,6 +50,9 @@ public class PasswordCollection extends EntityCollection { * @return The new credential. */ public Password create(String name, String password) { + if(checkForWildcards()){ + throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards."); + } Args args = new Args("password", password); return create(name, args); } @@ -63,6 +66,9 @@ public Password create(String name, String password) { * @return The new credential. */ public Password create(String name, String password, String realm) { + if(checkForWildcards()){ + throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards."); + } Args args = new Args(); args.put("password", password); args.put("realm", realm); @@ -97,11 +103,17 @@ public Password get(Object key) { * @return The removed credential, or null if not found. */ public Password remove(String realm, String name) { + if(checkForWildcards()){ + throw new IllegalArgumentException("app context must be specified when removing a password."); + } return super.remove(String.format("%s:%s:", realm, name)); } @Override public Password remove(String key) { + if(checkForWildcards()){ + throw new IllegalArgumentException("app context must be specified when removing a password."); + } // Make it compatible with the old way (low-efficient) if (!key.contains(":")) { Password password = getByUsername((String) key); @@ -129,4 +141,12 @@ private Password getByUsername(String name) { } return null; } + + private boolean checkForWildcards(){ + boolean isWildCard = false; + if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){ + isWildCard = true; + } + return isWildCard; + } } diff --git a/splunk/src/test/java/com/splunk/PasswordTest.java b/splunk/src/test/java/com/splunk/PasswordTest.java index 175c8c4c..3d32d393 100644 --- a/splunk/src/test/java/com/splunk/PasswordTest.java +++ b/splunk/src/test/java/com/splunk/PasswordTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import java.util.HashMap; + public class PasswordTest extends SDKTestCase { @Test public void testGettersOfDefaultPasswords() { @@ -129,4 +131,46 @@ public void testPasswordsCompatibleGetByName() { passwords.remove(username); Assert.assertNull(passwords.get(username)); } + @Test + public void testPasswordsWithWildCards(){ + HashMap args = new HashMap(); + args = Command.defaultValues; + args.put("owner", "-"); + args.put("app", "-"); + args.put("username", "admin"); + args.put("password", "changed!"); + Service service = Service.connect(args); + PasswordCollection passwords = service.getPasswords(); + Assert.assertEquals(passwords.size(),0); + + String name = "no-owner"; + String value = "sdk-test-password"; + String realm = "sdk-test-realm"; + + Password password; + // Create a password + try{ + password = passwords.create(name, value); + }catch(IllegalArgumentException e){ + Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage()); + } + try{ + password = passwords.create(name, value, realm); + }catch(IllegalArgumentException e){ + Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage()); + } + // Remove a password + try{ + password = passwords.remove(name); + }catch(IllegalArgumentException e){ + Assert.assertEquals("app context must be specified when removing a password.", e.getMessage()); + } + try{ + password = passwords.remove(realm, name); + }catch(IllegalArgumentException e){ + Assert.assertEquals("app context must be specified when removing a password.", e.getMessage()); + } + passwords = service.getPasswords(); + Assert.assertEquals(passwords.size(),0); + } }