Skip to content

Commit

Permalink
Merge pull request #187 from splunk/storagepasswords-wildcard-check
Browse files Browse the repository at this point in the history
Storagepassword's wildcard check for create and delete actions
  • Loading branch information
ashah-splunk authored Jul 20, 2022
2 parents 2618f58 + a6ed62f commit cf26ad8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions splunk/src/main/java/com/splunk/PasswordCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class PasswordCollection extends EntityCollection<Password> {
* @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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
44 changes: 44 additions & 0 deletions splunk/src/test/java/com/splunk/PasswordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -129,4 +131,46 @@ public void testPasswordsCompatibleGetByName() {
passwords.remove(username);
Assert.assertNull(passwords.get(username));
}
@Test
public void testPasswordsWithWildCards(){
HashMap<String, Object> args = new HashMap<String, Object>();
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);
}
}

0 comments on commit cf26ad8

Please sign in to comment.