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

DevSecOps: The Java Developer's Guide to Automa...

Jeanne Boyarsky
October 14, 2018
170

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

Learn how to use Groovy to automate Jenkins/Nexus security config.

Jeanne Boyarsky

October 14, 2018
Tweet

Transcript

  1. @jeanneboyarsky Jeanne Boyarsky Tuesday Oct 23, 2018 Oracle Code One

    – DEV4959 DevSecOps: The Java Developer’s Guide to Automating with Groovy speakerdeck.com/boyarsky
  2. @jeanneboyarsky Wednesday Recommendations 4 Date Time Title Speaker Wed Oct

    24 1:30-2:15 Functional Programming in Java, Groovy & Kotlin Ken Kousen Wed Oct 24 2:30-3:15 JVM Languages: Compare (Java, Kotlin, Groovy, Scala) Leonardo Lima, Nikhil Nanivadekar & Donald Raab Wed Oct 24 2:30-3:15 Which Java Version from Which Java Vendor with What Support Jeanne Boyarsky
  3. @jeanneboyarsky Will cover •  Groovy syntax & idioms •  Using

    Groovy from Nexus •  Using Groovy from Jenkins •  Live demo 5
  4. @jeanneboyarsky Java à Groovy 9 int lastYear = 2017; int

    year = 2018 def nextYear = 2019 Can use Java syntax Without semicolon or type
  5. @jeanneboyarsky Strings 10 def city = 'SF' println 'Here: $city'

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

    = """ Name ______ $name """ Name ______ Jeanne Coming in Java 13 with new syntax?
  7. @jeanneboyarsky What does this print? 12 def name = 'Jeanne'

    def text = ’’’ Name ______ $name ’’’ Name ______ $name
  8. @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 14 Can use Java syntax True!
  9. @jeanneboyarsky What is the truth? 15 Value Result Null False

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

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

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

    candy println list.min() candy list.sort() println list [candy, chocolate, cookie]
  13. @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 } 21 “it” implies param •  grep vs filter •  less plumbing
  14. @jeanneboyarsky Stream Mapping Java Groovy filter grep map collect forEach

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

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

    description: ‘packages', ) 26 Only the default constructor exists!
  17. @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 33
  18. @jeanneboyarsky Sample Object model code import jenkins.model.Jenkins def instance =

    Jenkins.getInstance() def realm = Jenkins.getInstance().securityRealm realm.createAccount('olivia', 'olivia') instance.save() 34
  19. @jeanneboyarsky Script Security Plugin 39 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
  20. @jeanneboyarsky And then there is 43 container GlobalComponentLookupHelper selectorManager =

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

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