Skip to content

Commit

Permalink
Validating for password length when FIPS is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vwagh-dev committed Sep 25, 2024
1 parent b749a3d commit b419b72
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/main/java/jenkins/security/plugins/ldap/LDAPConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public LDAPConfiguration(@NonNull String server, String rootDN, boolean inhibitI
if(FIPS140.useCompliantAlgorithms() && !validateServerUrlIsSecure(server)){

Check warning on line 152 in src/main/java/jenkins/security/plugins/ldap/LDAPConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 152 is only partially covered, one branch is missing
throw new IllegalArgumentException(Messages.LDAPConfiguration_InsecureServer(server));

Check warning on line 153 in src/main/java/jenkins/security/plugins/ldap/LDAPConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 153 is not covered by tests
}
String managerPassword = Secret.toString(managerPasswordSecret);
if(StringUtils.isNotBlank(managerPassword) && !"undefined".equals(managerPassword) &&

Check warning on line 156 in src/main/java/jenkins/security/plugins/ldap/LDAPConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 156 is only partially covered, one branch is missing
FIPS140.useCompliantAlgorithms() && StringUtils.length(managerPassword) < 14) {
throw new IllegalArgumentException(Messages.LDAPConfiguration_passwordTooShortFIPS());
}
this.server = server.trim();
this.managerDN = fixEmpty(managerDN);
this.managerPasswordSecret = managerPasswordSecret;
Expand Down Expand Up @@ -481,6 +486,14 @@ public FormValidation doCheckServer(@QueryParameter String value, @QueryParamete
}
}

@POST
public FormValidation doCheckManagerPasswordSecret(@QueryParameter String managerPasswordSecret) {

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing permission check Warning

Potential missing permission check in LDAPConfigurationDescriptor#doCheckManagerPasswordSecret
if(FIPS140.useCompliantAlgorithms() && StringUtils.length(managerPasswordSecret) < 14) {
return FormValidation.error(Messages.LDAPConfiguration_passwordTooShortFIPS());
}
return FormValidation.ok();
}

private void forceClose(Context ctx){
if(ctx==null){
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ UserDetails.Inactive=The user "{0}" is inactive until {1}.
UserDetails.Expired=The user "{0}" is expired since {1}.
UserDetails.CredentialsExpired=The user "{0}" has expired credentials.
UserDetails.Locked=The user "{0}" is locked and must be unlocked by an administrator.
LDAPConfiguration.InsecureServer=LDAP server URL is not secure: {0}.
LDAPConfiguration.InsecureServer=LDAP server URL is not secure: {0}.
LDAPConfiguration.passwordTooShortFIPS=Password is too short (< 14 characters)
32 changes: 32 additions & 0 deletions src/test/java/hudson/security/LDAPEmbeddedFIPSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package hudson.security;

import hudson.util.FormValidation;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import jenkins.model.Jenkins;
import hudson.tasks.MailAddressResolver;
Expand All @@ -35,6 +36,7 @@
import jenkins.security.plugins.ldap.*;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.rules.ExpectedException;
import org.jvnet.hudson.test.FlagRule;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.junit.Rule;
Expand Down Expand Up @@ -65,6 +67,8 @@ public class LDAPEmbeddedFIPSTest {
public RuleChain chain = RuleChain.outerRule(ads).around(r);
@Rule
public LoggerRule log = new LoggerRule();
@Rule
public ExpectedException thrown = ExpectedException.none();

@ClassRule
public static FlagRule<String> fipsSystemPropertyRule =
Expand Down Expand Up @@ -123,4 +127,32 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
e.printStackTrace();
}
}

@Test
public void testPasswordCheck() {
//Test when password is null
LDAPConfiguration configuration = new LDAPConfiguration("ldaps://ldap.example.com", "dc=example,dc=com", true, null, null);
assertNotNull(configuration);

// Test with a short password
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Password is too short");
configuration = new LDAPConfiguration("ldaps://ldap.example.com", "dc=example,dc=com", true, null, Secret.fromString("shortString"));

//Test with a strong password
configuration = new LDAPConfiguration("ldaps://ldap.example.com", "dc=example,dc=com", true, null, Secret.fromString("ThisIsVeryStrongPassword"));
assertNotNull(configuration);
}

@Test
public void testPasswordCheckOnCheckServer(){
// Test with a short password
FormValidation shortPasswordValidation = new LDAPConfiguration.LDAPConfigurationDescriptor().doCheckManagerPasswordSecret("short");
assertEquals(FormValidation.Kind.ERROR, shortPasswordValidation.kind);
assertThat(shortPasswordValidation.getMessage(), containsString("Password is too short"));

// Test with a strong password but server validation fails hence checking for 'Unknown host'
FormValidation strongPasswordValidation = new LDAPConfiguration.LDAPConfigurationDescriptor().doCheckManagerPasswordSecret("ThisIsVeryStrongPassword");
assertEquals(FormValidation.Kind.OK, strongPasswordValidation.kind);
}
}
4 changes: 2 additions & 2 deletions src/test/java/jenkins/security/plugins/ldap/CasCFIPSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CasCFIPSTest {

@Rule
public RuleChain chain = RuleChain.outerRule(new EnvironmentVariables()
.set("LDAP_PASSWORD", "SECRET"))
.set("LDAP_PASSWORD", "SECRET_Password_123"))
.around(new JenkinsConfiguredWithCodeRule());

@Test
Expand All @@ -36,7 +36,7 @@ public void configure_ldap() {
assertTrue(securityRealm.getGroupIdStrategy() instanceof IdStrategy.CaseSensitive);
final LDAPConfiguration configuration = securityRealm.getConfigurations().get(0);
assertEquals("ldaps://ldap.acme.com", configuration.getServer());
assertEquals("SECRET", configuration.getManagerPassword());
assertEquals("SECRET_Password_123", configuration.getManagerPassword());
assertEquals("manager", configuration.getManagerDN());
assertEquals("(&(objectCategory=User)(sAMAccountName={0}))", configuration.getUserSearch());
assertEquals("(&(cn={0})(objectclass=group))", configuration.getGroupSearchFilter());
Expand Down

0 comments on commit b419b72

Please sign in to comment.