<commit_before>import hudson.security.LDAPSecurityRealm
import hudson.security.ProjectMatrixAuthorizationStrategy
import hudson.security.HudsonPrivateSecurityRealm
import jenkins.security.plugins.ldap.FromGroupSearchLDAPGroupMembershipStrategy
import jenkins.security.plugins.ldap.FromUserRecordLDAPGroupMembershipStrategy
import jenkins.model.Jenkins
import hudson.model.Hudson
import hudson.model.Item

def asInt(value, defaultValue=0){
    return value ? value.toInteger() : defaultValue
}
def asBoolean(value, defaultValue=false){
    return value != null ? value.toBoolean() : defaultValue
}

def setupActiveDirectory(config){
    config.with{
        return new hudson.plugins.active_directory.ActiveDirectorySecurityRealm(
            domain, 
            domains?.collect{ currentDomain ->
                new hudson.plugins.active_directory.ActiveDirectoryDomain(
                    currentDomain.name, 
                    currentDomain.servers?.join(','), 
                    currentDomain.site, 
                    currentDomain.bindName, 
                    currentDomain.bindPassword
                )
            }, 
            site, 
            bindName,
            bindPassword, 
            server, 
            groupLookupStrategy ? hudson.plugins.active_directory.GroupLookupStrategy.valueOf(groupLookupStrategy) : null, 
            asBoolean(removeIrrelevantGroups), 
            asBoolean(customDomain, null), 
            cache ? new hudson.plugins.active_directory.CacheConfiguration(
                asInt(cache.size, 0),
                asInt(cache.ttl, 0)
            ) : null, 
            asBoolean(startTls, null), 
            tlsConfiguration ? hudson.plugins.active_directory.TlsConfiguration.valueOf(tlsConfiguration) : null, 
            jenkinsInternalUser ? new hudson.plugins.active_directory.ActiveDirectoryInternalUsersDatabase(jenkinsInternalUser) : null
        )
    }
}

def setupLdap(config){
    config.with{
        if (!groupMembershipAttribute && !groupMembershipFilter){
            throw new IllegalArgumentException("One of: groupMembershipFilter, groupMembershipAttribute must be provided")
        }

        def groupMembershipStrategy = groupMembershipAttribute ?
            new FromUserRecordLDAPGroupMembershipStrategy(groupMembershipAttribute) :
            new FromGroupSearchLDAPGroupMembershipStrategy(groupMembershipFilter)
        return new LDAPSecurityRealm(
                server,
                rootDN,
                userSearchBase,
                userSearchFilter,
                groupSearchBase,
                groupSearchFilter,
                groupMembershipStrategy,
                managerDN,
                Secret.fromString(managerPassword),
                asBoolean(inhibitInferRootDN),
                asBoolean(disableMailAddressResolver),
                new LDAPSecurityRealm.CacheConfiguration(20, 300), [
                    new LDAPSecurityRealm.EnvironmentProperty('com.sun.jndi.ldap.connect.timeout', asInt(connectTimeout, 5000).toString()),
                    new LDAPSecurityRealm.EnvironmentProperty('com.sun.jndi.ldap.read.timeout', asInt(readTimeout, 60000).toString()),
                ] as LDAPSecurityRealm.EnvironmentProperty[],
                displayNameAttr,
                emailAttr,
                /*IdStrategy userIdStrategy*/null,
                /*IdStrategy groupIdStrategy*/null
        )
    }
}
def setupJenkinsDatabase(config){
    config.with{
        securityRealm = new HudsonPrivateSecurityRealm(false)
        securityRealm.createAccount(adminUser, adminPassword)
        return securityRealm
    }
}

def setup(config){
    config = config ?: [:]
    def adminUser = config.adminUser
    def instance = Jenkins.getInstance()

    def realm<commit_msg>Fixed ldap config<commit_after>import hudson.security.LDAPSecurityRealm
import hudson.security.ProjectMatrixAuthorizationStrategy
import hudson.security.HudsonPrivateSecurityRealm
import jenkins.security.plugins.ldap.FromGroupSearchLDAPGroupMembershipStrategy
import jenkins.security.plugins.ldap.FromUserRecordLDAPGroupMembershipStrategy
import jenkins.model.Jenkins
import hudson.model.Hudson
import hudson.model.Item

def asInt(value, defaultValue=0){
    return value ? value.toInteger() : defaultValue
}
def asBoolean(value, defaultValue=false){
    return value != null ? value.toBoolean() : defaultValue
}

def setupActiveDirectory(config){
    config.with{
        return new hudson.plugins.active_directory.ActiveDirectorySecurityRealm(
            domain, 
            domains?.collect{ currentDomain ->
                new hudson.plugins.active_directory.ActiveDirectoryDomain(
                    currentDomain.name, 
                    currentDomain.servers?.join(','), 
                    currentDomain.site, 
                    currentDomain.bindName, 
                    currentDomain.bindPassword
                )
            }, 
            site, 
            bindName,
            bindPassword, 
            server, 
            groupLookupStrategy ? hudson.plugins.active_directory.GroupLookupStrategy.valueOf(groupLookupStrategy) : null, 
            asBoolean(removeIrrelevantGroups), 
            asBoolean(customDomain, null), 
            cache ? new hudson.plugins.active_directory.CacheConfiguration(
                asInt(cache.size, 0),
                asInt(cache.ttl, 0)
            ) : null, 
            asBoolean(startTls, null), 
            tlsConfiguration ? hudson.plugins.active_directory.TlsConfiguration.valueOf(tlsConfiguration) : null, 
            jenkinsInternalUser ? new hudson.plugins.active_directory.ActiveDirectoryInternalUsersDatabase(jenkinsInternalUser) : null
        )
    }
}

def setupLdap(config){
    config.with{
        if (!groupMembershipAttribute && !groupMembershipFilter){
            throw new IllegalArgumentException("One of: groupMembershipFilter, groupMembershipAttribute must be provided")
        }

        def groupMembershipStrategy = groupMembershipAttribute ?
            new FromUserRecordLDAPGroupMembershipStrategy(groupMembershipAttribute) :
            new FromGroupSearchLDAPGroupMembershipStrategy(groupMembershipFilter)
        return new LDAPSecurityRealm(
                server,
                rootDN,
                userSearchBase,
                userSearchFilter,
                groupSearchBase,
                groupSearchFilter,
                groupMembershipStrategy,
                managerDN,
                hudson.util.Secret.fromString(managerPassword),
                asBoolean(inhibitInferRootDN),
                asBoolean(disableMailAddressResolver),
                new LDAPSecurityRealm.CacheConfiguration(20, 300), [
                    new LDAPSecurityRealm.EnvironmentProperty('com.sun.jndi.ldap.connect.timeout', asInt(connectTimeout, 5000).toString()),
                    new LDAPSecurityRealm.EnvironmentProperty('com.sun.jndi.ldap.read.timeout', asInt(readTimeout, 60000).toString()),
                ] as LDAPSecurityRealm.EnvironmentProperty[],
                displayNameAttr,
                emailAttr,
                /*IdStrategy userIdStrategy*/null,
                /*IdStrategy groupIdStrategy*/null
        )
    }
}
def setupJenkinsDatabase(config){
    config.with{
        securityRealm = new HudsonPrivateSecurityRealm(false)
        securityRealm.createAccount(adminUser, adminPassword)
        return securityRealm
    }
}

def setup(config){
    config = config ?: [:]
    def adminUser = config.adminUser
    def instance = Jenkins.getInstance()

    def realm
{'fraction_non_alphanumeric': 0.07404479578392621, 'fraction_numerical': 0.0044795783926218705, 'mean_word_length': 2.411685393258427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6630055', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1816, 'n_words': 303}