Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-26076] - Add null checks and proper annotations
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Dec 25, 2014
1 parent 084f68e commit 7da810b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
Expand Up @@ -5,6 +5,8 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
Expand All @@ -26,11 +28,14 @@ public class PerforcePasswordEncryptor {
public PerforcePasswordEncryptor() {
}

public boolean appearsToBeAnEncryptedPassword(String toCheck) {
public boolean appearsToBeAnEncryptedPassword(@CheckForNull String toCheck) {
if (toCheck == null) {
return false;
}
return toCheck.startsWith(ENCRYPTION_PREFIX);
}

public String encryptString(String toEncrypt) {
public @Nonnull String encryptString(@CheckForNull String toEncrypt) {
if (toEncrypt == null || toEncrypt.trim().length() == 0)
return "";

Expand All @@ -51,7 +56,12 @@ public String encryptString(String toEncrypt) {
return ENCRYPTION_PREFIX + encodedString;
}

public String decryptString(String toDecrypt) {
/**
* Decrypts the input string
* @param toDecrypt Input string
* @return Decrypted string. Empty string is being used as a fallback
*/
public @Nonnull String decryptString(@CheckForNull String toDecrypt) {
if (toDecrypt == null || toDecrypt.length() == 0)
return "";

Expand Down
50 changes: 40 additions & 10 deletions src/main/java/hudson/plugins/perforce/PerforceSCM.java
Expand Up @@ -435,11 +435,21 @@ public static PerforceSCMDescriptor getInstance() {
return (PerforceSCMDescriptor)Hudson.getInstance().getScm(scmName);
}

public String getEffectiveP4User() {
/**
* Gets the P4 user from local or global configs (as a default).
* @return Effective password. May be null or empty
* @since 1.3.31
*/
public @CheckForNull String getEffectiveP4User() {
return p4User != null ? p4User : getInstance().getP4DefaultUser();
}

public String getEffectiveP4Password() {
/**
* Gets the password from local or global configs (as a default).
* @return Effective password. May be null or empty
* @since 1.3.31
*/
public @CheckForNull String getEffectiveP4Password() {
return p4User != null ? p4Passwd : getInstance().getP4DefaultPassword();
}

Expand Down Expand Up @@ -1852,8 +1862,8 @@ public static final class PerforceSCMDescriptor extends SCMDescriptor<PerforceSC
/**DIsables expose of Perforce password to the build environment*/
private boolean passwordExposeDisabled;

private String p4DefaultUser;
private String p4DefaultPassword;
private @CheckForNull String p4DefaultUser;
private @CheckForNull String p4DefaultPassword;

private final static int P4_INFINITE_TIMEOUT_SEC = 0;
private final static int P4_MINIMAL_TIMEOUT_SEC = 30;
Expand Down Expand Up @@ -1920,9 +1930,13 @@ public boolean isPasswordExposeDisabled() {
return passwordExposeDisabled;
}

private void setDefaultP4Passwd(String passwd) {
/**
* @since 1.3.31
*/
private void setDefaultP4Passwd(@CheckForNull String passwd) {
if (passwd == null) {
p4DefaultPassword = null;
return;
}

PerforcePasswordEncryptor encryptor = new PerforcePasswordEncryptor();
Expand All @@ -1933,16 +1947,31 @@ private void setDefaultP4Passwd(String passwd) {
}
}

public String getP4DefaultPassword() {
/**
* Get the default password
* @return Password or empty string if it does not set
* @since 1.3.31
*/
public @Nonnull String getP4DefaultPassword() {
return p4DefaultPassword != null ? p4DefaultPassword : "";
}

public String getDecryptedP4DefaultPassword() {
/**
* Decrypts the default password.
* @return Password or empty string if it does not set
* @since 1.3.31
*/
public @Nonnull String getDecryptedP4DefaultPassword() {
PerforcePasswordEncryptor encryptor = new PerforcePasswordEncryptor();
return encryptor.decryptString(p4DefaultPassword);
}

public String getP4DefaultUser() {
/**
* Gets the default user id.
* @return null if the user is not specified
* @since 1.3.31
*/
public @CheckForNull String getP4DefaultUser() {
return p4DefaultUser;
}

Expand Down Expand Up @@ -2624,16 +2653,17 @@ public void setProjectPath(String projectPath) {
}

/**
* @deprecated use {@link #getEffectiveP4User()} to get a support of default options
* @return the p4User
*/
public String getP4User() {
public @CheckForNull String getP4User() {
return p4User;
}

/**
* @param user the p4User to set
*/
public void setP4User(String user) {
public void setP4User(@CheckForNull String user) {
p4User = user;
}

Expand Down
@@ -1,5 +1,7 @@
package hudson.plugins.perforce;

import javax.annotation.CheckForNull;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;

/**
Expand All @@ -25,5 +27,14 @@ public void testAppearsToBeEncyptedFindsEncryptedString() {
String encryptedString = encryptor.encryptString(toEncrypt);
assertTrue(encryptor.appearsToBeAnEncryptedPassword(encryptedString));
}

@Bug(26076)
public void testEncryptNull() {
final PerforcePasswordEncryptor encryptor = new PerforcePasswordEncryptor();
final @CheckForNull String toEncrypt = null;

assertFalse(encryptor.appearsToBeAnEncryptedPassword(null));
String encryptedString = encryptor.encryptString(toEncrypt);
assertEquals("", encryptedString);
}
}

0 comments on commit 7da810b

Please sign in to comment.