Skip to content

Storagepassword's wildcard check for create and delete actions #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be missing context here, but why do we have to check for the wildcard when removing the password? What happens if I'm trying to remove a password that has - in it given that passwords with - have been allowed till now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we check for wildcards present in 'app' and 'owner' properties and not in the password itself. As we need 'app' and 'owner' values to determine from where the StoragePassword is being delete.

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);
}
}