From 41de3c027ca82ca53b13907efc2012571c8b2226 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 15:23:55 +0530 Subject: [PATCH 1/4] Wildcard checks in-line with other SDKs Added check for wildcards in 'app' and 'owner' while creating/removing a StoragePassword --- .../java/com/splunk/PasswordCollection.java | 20 +++++++++ .../test/java/com/splunk/PasswordTest.java | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 4603160c..9eb67ac7 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(service.getOwner().equals("-") || service.getApp().equals("-")){ + 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..2cf655b3 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 testPasswordsCrudWithWildCards(){ + 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); + } } From 0cf88c004cdbae774ec039b53d98152db5316d75 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 15:24:46 +0530 Subject: [PATCH 2/4] Update PasswordTest.java --- splunk/src/test/java/com/splunk/PasswordTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunk/src/test/java/com/splunk/PasswordTest.java b/splunk/src/test/java/com/splunk/PasswordTest.java index 2cf655b3..3d32d393 100644 --- a/splunk/src/test/java/com/splunk/PasswordTest.java +++ b/splunk/src/test/java/com/splunk/PasswordTest.java @@ -132,7 +132,7 @@ public void testPasswordsCompatibleGetByName() { Assert.assertNull(passwords.get(username)); } @Test - public void testPasswordsCrudWithWildCards(){ + public void testPasswordsWithWildCards(){ HashMap args = new HashMap(); args = Command.defaultValues; args.put("owner", "-"); From 1f2b8a3092e2c954166d85b7f24c584dd5472bf6 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 17:24:11 +0530 Subject: [PATCH 3/4] Update PasswordCollection.java --- splunk/src/main/java/com/splunk/PasswordCollection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 9eb67ac7..3119013a 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -144,7 +144,7 @@ private Password getByUsername(String name) { private Boolean checkForWildcards(){ Boolean isWildCard = false; - if(service.getOwner().equals("-") || service.getApp().equals("-")){ + if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){ isWildCard = true; } return isWildCard; From a6ed62fd952c5670001eb627eab419064ef5b11a Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 12 Jul 2022 17:07:26 +0530 Subject: [PATCH 4/4] Update PasswordCollection.java changed 'Boolean' to 'boolean' --- splunk/src/main/java/com/splunk/PasswordCollection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 3119013a..47a50a22 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -142,8 +142,8 @@ private Password getByUsername(String name) { return null; } - private Boolean checkForWildcards(){ - Boolean isWildCard = false; + private boolean checkForWildcards(){ + boolean isWildCard = false; if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){ isWildCard = true; }