Skip to content

Commit

Permalink
Fix Authentication for FIPS
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisYaoA committed Sep 26, 2024
1 parent f6cf9a6 commit 9cf3712
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/hudson/security/LDAPSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import hudson.util.Secret;
import jenkins.model.IdStrategy;
import jenkins.model.Jenkins;
import jenkins.security.FIPS140;
import jenkins.security.SecurityListener;
import jenkins.security.plugins.ldap.FromGroupSearchLDAPGroupMembershipStrategy;
import jenkins.security.plugins.ldap.LDAPConfiguration;
Expand Down Expand Up @@ -753,6 +754,9 @@ public SecurityComponents createSecurityComponents() {
*/
@Override
protected UserDetails authenticate2(String username, String password) throws AuthenticationException {
if(FIPS140.useCompliantAlgorithms() && StringUtils.isNotBlank(password) && password.length() < 14) {

Check warning on line 757 in src/main/java/hudson/security/LDAPSecurityRealm.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 757 is only partially covered, 5 branches are missing
throw new org.springframework.ldap.AuthenticationException(new javax.naming.AuthenticationException("When running in FIPS compliance mode, the password must be at least 14 characters long"));

Check warning on line 758 in src/main/java/hudson/security/LDAPSecurityRealm.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 758 is not covered by tests
}
return updateUserDetails((UserDetails) getSecurityComponents().manager2.authenticate(
new UsernamePasswordAuthenticationToken(fixUsername(username), password)).getPrincipal(), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import hudson.util.Secret;
import jenkins.model.Jenkins;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LDAPSecurityRealm.LoginHeader=Login
LDAPSecurityRealm.AuthenticationSuccessful=Authentication: successful
LDAPSecurityRealm.AuthenticationFailed=Authentication: failed for user "{0}"
LDAPSecurityRealm.AuthenticationFailedEmptyPass=Authentication: failed for user "{0}" with empty password
LDAPSecurityRealm.AuthenticationFailedNotFipsCompliantPass=When running in FIPS compliance mode, the password must be at least 14 characters long
LDAPSecurityRealm.UserId=User ID: {0}
LDAPSecurityRealm.UserDn=User DN: {0}
LDAPSecurityRealm.UserConfiguration=User Server: {0}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/hudson/security/LDAPSecurityRealmWithFIPSTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package hudson.security;

import hudson.util.Secret;
import jenkins.model.IdStrategy;
import jenkins.security.FIPS140;
import jenkins.security.plugins.ldap.FromUserRecordLDAPGroupMembershipStrategy;
import jenkins.security.plugins.ldap.LDAPConfiguration;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.FlagRule;
import org.jvnet.hudson.test.JenkinsRule;

public class LDAPSecurityRealmWithFIPSTest {

@Rule
public JenkinsRule r = new JenkinsRule();


@ClassRule
public static FlagRule<String> fipsFlag = FlagRule.systemProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");

@Test
public void ldapAuthenticationWithFIPSTest() throws Exception {
final String server = "localhost";
final String rootDN = "ou=umich,dc=ou.edu";
final String userSearchBase = "cn=users,ou=umich,ou.edu";
final String managerDN = "cn=admin,ou=umich,ou.edu";
final String managerSecret = "secret";
final LDAPSecurityRealm realm = new LDAPSecurityRealm(
server,
rootDN,
userSearchBase,
null,
null,
null,
new FromUserRecordLDAPGroupMembershipStrategy("previousValue"),
managerDN,
Secret.fromString(managerSecret),
false,
false,
null,
null,
null,
null,
IdStrategy.CASE_INSENSITIVE,
IdStrategy.CASE_INSENSITIVE);
r.jenkins.setSecurityRealm(realm);
//realm.authenticate2("user","secret");
}
}

0 comments on commit 9cf3712

Please sign in to comment.