|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Cloud Foundry Java Buildpack |
| 4 | +# Copyright 2013-2020 the original author or authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +require 'java_buildpack/framework' |
| 19 | + |
| 20 | +module JavaBuildpack |
| 21 | + module Framework |
| 22 | + |
| 23 | + # Encapsulates the functionality for running with Checkmarx IAST Agent |
| 24 | + class CheckmarxIastAgent < JavaBuildpack::Component::BaseComponent |
| 25 | + include JavaBuildpack::Util |
| 26 | + |
| 27 | + # Creates an instance. In addition to the functionality inherited from +BaseComponent+, +@version+ and +@uri+ |
| 28 | + # instance variables are exposed. |
| 29 | + # |
| 30 | + # @param [Hash] context a collection of utilities used by components |
| 31 | + def initialize(context) |
| 32 | + super(context) |
| 33 | + |
| 34 | + # Save the IAST server URL in server, if found |
| 35 | + service = @application.services.find_service(FILTER, 'server') |
| 36 | + @server = service['credentials']['server'].chomp '/' if service |
| 37 | + end |
| 38 | + |
| 39 | + # (see JavaBuildpack::Component::BaseComponent#detect) |
| 40 | + def detect |
| 41 | + @server |
| 42 | + end |
| 43 | + |
| 44 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 45 | + def compile |
| 46 | + # Download and extract the agent from the IAST server |
| 47 | + FileUtils.mkdir_p @droplet.sandbox |
| 48 | + # curl --insecure: most IAST servers will use self-signed SSL |
| 49 | + shell 'curl --fail --insecure --silent --show-error ' \ |
| 50 | + "#{@server}/iast/compilation/download/JAVA -o #{@droplet.sandbox}/cx-agent.zip" |
| 51 | + shell "unzip #{@droplet.sandbox}/cx-agent.zip -d #{@droplet.sandbox}" |
| 52 | + |
| 53 | + # Disable cache (no point, when running in a container) |
| 54 | + File.open("#{@droplet.sandbox}/#{OVERRIDE_CONFIG}", 'a') do |file| |
| 55 | + file.write("\nenableWeavedClassCache=false\n") |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 60 | + def release |
| 61 | + # Default cxAppTag to application name if not set as an env var |
| 62 | + app_tag = ENV['cxAppTag'] || application_name |
| 63 | + # Default team to CxServer if not set as env var |
| 64 | + team = ENV['cxTeam'] || 'CxServer' |
| 65 | + |
| 66 | + javaagent = "-javaagent:#{qualify_path(@droplet.sandbox + JAVA_AGENT_JAR, @droplet.root)}" |
| 67 | + @droplet.java_opts |
| 68 | + .add_preformatted_options(javaagent) |
| 69 | + .add_preformatted_options('-Xverify:none') |
| 70 | + .add_system_property('cx.logToConsole', 'true') |
| 71 | + .add_system_property('cx.appName', application_name) |
| 72 | + .add_system_property('cxAppTag', app_tag) |
| 73 | + .add_system_property('cxTeam', team) |
| 74 | + end |
| 75 | + |
| 76 | + private |
| 77 | + |
| 78 | + JAVA_AGENT_JAR = 'cx-launcher.jar' |
| 79 | + |
| 80 | + OVERRIDE_CONFIG = 'cx_agent.override.properties' |
| 81 | + |
| 82 | + FILTER = /^checkmarx-iast$/.freeze |
| 83 | + |
| 84 | + private_constant :JAVA_AGENT_JAR, :FILTER, :OVERRIDE_CONFIG |
| 85 | + |
| 86 | + def application_name |
| 87 | + @application.details['application_name'] || 'ROOT' |
| 88 | + end |
| 89 | + |
| 90 | + end |
| 91 | + |
| 92 | + end |
| 93 | + |
| 94 | +end |
0 commit comments