Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Groovy random password No such field found: field java.lang.String size

What I’m running in order to create a random password with alphanumeric and special characters:

   randomPasswordCreation = random_password_creation(14)
   println "Random pass: $randomPasswordCreation"

   def random_password_creation(pass_length ){
       def special = ['!','@','#','$','%','&'] 
       def pool = ['a'..'z','A'..'Z',0..9,'_'].flatten().plus(special);
       Random rand = new Random(System.currentTimeMillis());

       def passChars = (0..pass_length - 1).collect { pool[rand.nextInt(pool.size)] };
       def specialChar = special[rand.nextInt(special.size)]
       passChars[rand.nextInt(passChars.size)] = specialChar
       def PASSWORD = passChars.join();
       return PASSWORD
   }

What I receive as error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String size
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:426)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:410)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.collect(CpsDefaultGroovyMethods:3170)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.collect(CpsDefaultGroovyMethods:3140)
    at WorkflowScript.run(WorkflowScript:1922)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are at least two problems with the code you have shown:

  1. You access size as it was a field, but it is a method, so it should be size() instead.
  2. The passChars.join() is missing a separator argument, e.g. '' for an empty string.

Based on the error stack trace, I assume you are running this code as a part of a Jenkins Pipeline. Keep in mind, that Jenkins executes Groovy in a more restrictive way. For instance, while the regular dynamic Groovy can handle things like .size instead of .size(), the WorkflowScript executor requires that the Groovy code is free from such mistakes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading