Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevSecOps: The Java Developer's Guide to Automating with Groovy

DevSecOps: The Java Developer's Guide to Automating with Groovy

from Oracle Code NY

Jeanne Boyarsky

May 21, 2019
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Programming

Transcript

  1. @jeanneboyarsky Jeanne Boyarsky Tuesday May 21, 2018 Oracle Code New

    York DevSecOps: The Java Developer’s Guide to Automating with Groovy speakerdeck.com/boyarsky
  2. @jeanneboyarsky Will cover •  Groovy syntax & idioms •  Using

    Groovy from Nexus •  Using Groovy from Jenkins •  Live demo 4
  3. @jeanneboyarsky Java à Groovy 8 int lastYear = 2018; int

    year = 2019 def nextYear = 2020 Can use Java syntax Without semicolon or type
  4. @jeanneboyarsky Strings 9 def city = ’NYC' println 'Here: $city'

    println "Here: $city" println "In ${city.class}" Java String GString Here: $city Here: NYC In class java.lang.String
  5. @jeanneboyarsky Multiline string 10 def name = 'Jeanne' def text

    = """ Name ______ $name """ Name ______ Jeanne Coming in Java 13 (raw string literals) 14? (text blocks) with new syntax
  6. @jeanneboyarsky What does this print? 11 def name = 'Jeanne'

    def text = ’’’ Name ______ $name ’’’ Name ______ $name
  7. @jeanneboyarsky == vs equals() def monday ='monday' def nextMonday =

    new String('monday') def tuesday ='tuesday' println monday == null println null == monday println monday == tuesday println monday == nextMonday 13 Can use Java syntax True!
  8. @jeanneboyarsky What is the truth? 14 Value Result Null False

    Empty String False Empty List False 1 character String True
  9. @jeanneboyarsky Optional Parens 16 def ch = 'abc'.charAt 1 println

    ch Optional when no ambiguity Ambiguity: •  Zero params •  Within println
  10. @jeanneboyarsky ArrayList++ 17 def list = ['cookie', 'chocolate’] list <<

    'candy' println list.getClass() class java.util.ArrayList println list[1] chocolate ArrayList does what now?!
  11. @jeanneboyarsky ArrayList++ 18 println list [cookie, chocolate, candy] println list[-1]

    candy println list.min() candy list.sort() println list [candy, chocolate, cookie]
  12. @jeanneboyarsky Streams/lambas def list = [1,3,5,7] list.stream().filter({n -> n >=

    5}).forEach({n -> System.out.println(n)}) println list.grep { num -> num >= 5 } println list.grep { it >= 5 } 20 “it” implies param •  grep vs filter •  less plumbing
  13. @jeanneboyarsky Stream Mapping Java Groovy filter grep map collect forEach

    for limit take distinct unique anyMatch/allMatch any/every findFirst find 22
  14. @jeanneboyarsky Functions incrementBy = 4; def add(num) { num +

    incrementBy } println add(2) 24 no def Return optional Types optional
  15. @jeanneboyarsky Named Params def config = new SelectorConfiguration( name: ‘selector',

    description: ‘packages', ) 25 Only the default constructor exists!
  16. @jeanneboyarsky Three ways to find methods JavaDoc https://javadoc.jenkins.io https://javadoc.jenkins.io/plugin Groovy

    def instance = Jenkins.getInstance() println instance.metaClass.methods*.name .sort().unique() println instance.metaClass.methods Google Reuse existing scripts 32
  17. @jeanneboyarsky Sample Object model code import jenkins.model.Jenkins def instance =

    Jenkins.getInstance() def realm = Jenkins.getInstance().securityRealm realm.createAccount('olivia', 'olivia') instance.save() 33
  18. @jeanneboyarsky Script Security Plugin 38 ERROR: Build step failed with

    exception org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessExce ption: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance Options: •  Approve each script manually •  Sandbox - whitelist APIs •  Authorized Build plugin to run as admin
  19. @jeanneboyarsky And then there is 42 container GlobalComponentLookupHelper selectorManager =

    container.lookup(SelectorManager.class.name) securitySystem = container.lookup(SecuritySystem.class.name) authorizationManager = securitySystem.getAuthorizationManager('default')
  20. @jeanneboyarsky What we will cover Code Examples Walkthrough •  Nexus

    object model •  Jenkins object model •  Jenkins pipeline Demo – Nexus and Jenkins 45