Nexus3 Configure LDAP with API

809 views
Skip to first unread message

Jason Anderson

unread,
Oct 6, 2016, 3:15:11 PM10/6/16
to Nexus Users

I'm currently trying to write a script to configure LDAP on my Nexus 3 instance. I've taken a look at this post and i'm running into some errors and I think it's just me not understanding Groovy that well.

Here's what I've tried.

import org.sonatype.nexus.ldap.persist.*
import org.sonatype.nexus.ldap.persist.entity.*
import groovy.json.JsonSlurper

def ldap = new JsonSlurper().parseText(args)
def manager = container.lookup(LdapConfigurationManager.class.name)

manager.addLdapServerConfiguration(
  new LdapConfiguration(
    name: ldap.name,
    connection: new Connection(
      host: new Connection.Host(Connection.Protocol.ldap, ldap.host, ldap.port),
      maxIncidentsCount: 3,
      connectionRetryDelay: 300,
      connectionTimeout: 15,
      searchBase: 'dc=example,dc=com',
      authScheme: 'simple',
      systemPassword: 'systemPassword',
      systemUsername: 'systemUsername'
    ),
    mapping: new Mapping(
      ldapGroupsAsRoles: true,
      emailAddressAttribute: 'mail',
      userIdAttribute: 'sAMAccountName',
      userMemberOfAttribute: 'memberOf',
      userObjectClass: 'user',
      userPasswordAttribute: 'userPassword',
      userRealNameAttribute: 'cn',
      userBaseDn: '(memberof:1.2.840.113556.1.4.1941:=cn=Devs,ou=someOU,ou=anotherOU,dc=example,dc=com'
    )
  )
)

When i use the provision.sh script provided in the complex-script examples I get:

      "name" : "ldapConfig",
      "result" : "org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\n
Script8.groovy: 10: unable to resolve class ldapConfiguration \n 
@ line 10, column 3.\n    new ldapConfiguration(\n^\n\n1 error\n"

It really bothers me it can't resolve the class... Now, I'm not sure how to provide the JSON object for args. I've instead tried to put a JSON block directly into JsonSlurper().parseText() like this:

ldap = new JsonSlurper().parseText('{"host: localhost", "port: 389"}')

This produces same error as above.

Here is a snippet from the provision.sh is where invoke the addUpdateScript.groovy script.

#!/bin/bash

# A simple example script that publishes a number of scripts to the Nexus Repository Manager
# and executes them.

# fail if anything errors
set -e
# fail if a function call is missing an argument
set -u

username=admin
password=admin123

# add the context if you are not using the root context
host=http://localhost:8081

    # add a script to the repository manager and run it
    function addAndRunScript {
      name=$1
      file=$2
      # using grape config that points to local Maven repo and Central Repository , default grape config fails on some downloads although artifacts are in Central
      # change the grapeConfig file to point to your repository manager, if you are already running one in your organization
      groovy -Dgroovy.grape.report.downloads=true -Dgrape.config=grapeConfig.xml addUpdatescript.groovy -u "$username" -p "$password" -n "$name" -f "$file" -h "$host"
      printf "\nPublished $file as $name\n\n"
      curl -v -X POST -u $username:$password --header "Content-Type: text/plain" "$host/service/siesta/rest/v1/script/$name/run"
      curl -v -X GET -u $username:$password "$host/service/siesta/rest/v1/script/$name"
      printf "\nSuccessfully executed $name script\n\n\n"
    }

    printf "Provisioning Integration API Scripts Starting \n\n" 
    printf "Publishing and executing on $host\n"

    addAndRunScript ldapConfig ldapConfig.groovy

Am I using the wrong class name? LdapConfiguration. When I loaded this into IntelliJ I found ldapConfig but it didn't work either. Not sure how to dig into the javadoc to get more details out of the libraries.


Jason Anderson

unread,
Oct 6, 2016, 3:27:30 PM10/6/16
to Nexus Users
I fixed my JSON document to '{"name": "testldap", "host": "localhost", "port": 389}' and changed ldapConfiguration to LdapConfiguration.  (case issue.)  Script works now.

kumar reddy

unread,
Aug 30, 2019, 1:58:14 PM8/30/19
to Nexus Users

kumar reddy

unread,
Aug 30, 2019, 2:03:10 PM8/30/19
to Nexus Users
Hello All,

following the below i configured LDAP and it is not working for me, Could any let me know where i am going wrong


import org.sonatype.nexus.ldap.persist.*
import org.sonatype.nexus.ldap.persist.entity.*
import groovy.json.JsonSlurper
def ldap = new JsonSlurper().parseText("host": "192.168.1.21", "port": 368)
def manager = container.lookup(LdapConfigurationManager.class.name)
manager.addLdapServerConfiguration(
  new LdapConfiguration(
    name: "internal.example.com",
    connection: new Connection(
      host: new Connection.Host(Connection.Protocol.ldap, ldap.host, ldap.port),
      maxIncidentsCount: 3,
      connectionRetryDelay: 300,
      connectionTimeout: 15,
      searchBase: 'dc=internal,dc=example,dc=com',
      authScheme: 'simple',
      systemPassword: 'Password1',
      systemUsername: 'us...@internal.example.com'
    ),
    mapping: new Mapping(
      ldapGroupsAsRoles: true,
      emailAddressAttribute: 'mail',
      userIdAttribute: 'uid',
      userMemberOfAttribute: 'memberOf',
      userObjectClass: 'inetOrgPerson',
      userPasswordAttribute: 'userPassword',
      userRealNameAttribute: 'cn',
      userBaseDn: ''
    )
  )
)






On Friday, October 7, 2016 at 12:45:11 AM UTC+5:30, Jason Anderson wrote:

Peter Lynch

unread,
Sep 3, 2019, 8:42:57 AM9/3/19
to kumar reddy, Nexus Users
On Fri, Aug 30, 2019 at 3:03 PM kumar reddy <ctr...@gmail.com> wrote:
Hello All,

following the below i configured LDAP and it is not working for me, Could any let me know where i am going wrong


import org.sonatype.nexus.ldap.persist.*
import org.sonatype.nexus.ldap.persist.entity.*
import groovy.json.JsonSlurper
def ldap = new JsonSlurper().parseText("host": "192.168.1.21", "port": 368)

^^ wrong. Change it to this:

def ldap = new JsonSlurper().parseText('{ "host": "192.168.1.21", "port": 368 }')



 
--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users...@glists.sonatype.com.
To view this discussion on the web visit https://groups.google.com/a/glists.sonatype.com/d/msgid/nexus-users/b8ced27e-bb9e-42f4-83e6-54adcfde5040%40glists.sonatype.com.


--
Sonatype
Peter Lynch
Senior Product Support Engineer

Reply all
Reply to author
Forward
0 new messages