|
| 1 | +# Encoding: utf-8 |
| 2 | + |
| 3 | +# Cloud Foundry Java Buildpack |
| 4 | +# Copyright 2013-2017 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 'fileutils' |
| 19 | +require 'java_buildpack/component/versioned_dependency_component' |
| 20 | +require 'java_buildpack/framework' |
| 21 | +require 'java_buildpack/util/qualify_path' |
| 22 | +require 'rexml/document' |
| 23 | + |
| 24 | +module JavaBuildpack |
| 25 | + module Framework |
| 26 | + |
| 27 | + # Encapsulates the functionality for running the Contrast Security Agent support. |
| 28 | + class ContrastSecurityAgent < JavaBuildpack::Component::VersionedDependencyComponent |
| 29 | + include JavaBuildpack::Util |
| 30 | + |
| 31 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 32 | + def compile |
| 33 | + download_jar |
| 34 | + @droplet.copy_resources |
| 35 | + |
| 36 | + write_configuration @application.services.find_service(CONTRAST_FILTER)['credentials'] |
| 37 | + end |
| 38 | + |
| 39 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 40 | + def release |
| 41 | + @droplet.java_opts |
| 42 | + .add_system_property('contrast.dir', '$TMPDIR') |
| 43 | + .add_system_property('contrast.override.appname', application_name) |
| 44 | + .add_preformatted_options("-javaagent:#{qualify_path(@droplet.sandbox + jar_name, @droplet.root)}=" \ |
| 45 | + "#{qualify_path(contrast_config, @droplet.root)}") |
| 46 | + end |
| 47 | + |
| 48 | + protected |
| 49 | + |
| 50 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#jar_name) |
| 51 | + def jar_name |
| 52 | + "contrast-engine-#{@version.to_s.split('_')[0]}.jar" |
| 53 | + end |
| 54 | + |
| 55 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) |
| 56 | + def supports? |
| 57 | + @application.services.one_service?(CONTRAST_FILTER, API_KEY, SERVICE_KEY, TEAMSERVER_URL, USERNAME) |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + API_KEY = 'api_key'.freeze |
| 63 | + |
| 64 | + CONTRAST_FILTER = 'contrast-security'.freeze |
| 65 | + |
| 66 | + PLUGIN_PACKAGE = 'com.aspectsecurity.contrast.runtime.agent.plugins.'.freeze |
| 67 | + |
| 68 | + SERVICE_KEY = 'service_key'.freeze |
| 69 | + |
| 70 | + TEAMSERVER_URL = 'teamserver_url'.freeze |
| 71 | + |
| 72 | + USERNAME = 'username'.freeze |
| 73 | + |
| 74 | + private_constant :API_KEY, :CONTRAST_FILTER, :PLUGIN_PACKAGE, :SERVICE_KEY, :TEAMSERVER_URL, :USERNAME |
| 75 | + |
| 76 | + def add_contrast(doc, credentials) |
| 77 | + contrast = doc.add_element('contrast') |
| 78 | + (contrast.add_element 'id').add_text('default') |
| 79 | + (contrast.add_element 'global-key').add_text(credentials[API_KEY]) |
| 80 | + (contrast.add_element 'url').add_text("#{credentials[TEAMSERVER_URL]}/Contrast/s/") |
| 81 | + (contrast.add_element 'results-mode').add_text('never') |
| 82 | + |
| 83 | + add_user contrast, credentials |
| 84 | + add_plugins contrast |
| 85 | + end |
| 86 | + |
| 87 | + def add_plugins(contrast) |
| 88 | + plugin_group = contrast.add_element('plugins') |
| 89 | + |
| 90 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.security.SecurityPlugin") |
| 91 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.architecture.ArchitecturePlugin") |
| 92 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.appupdater.ApplicationUpdatePlugin") |
| 93 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.sitemap.SitemapPlugin") |
| 94 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.frameworks.FrameworkSupportPlugin") |
| 95 | + (plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.http.HttpPlugin") |
| 96 | + end |
| 97 | + |
| 98 | + def add_user(contrast, credentials) |
| 99 | + user = contrast.add_element('user') |
| 100 | + (user.add_element 'id').add_text(credentials[USERNAME]) |
| 101 | + (user.add_element 'key').add_text(credentials[SERVICE_KEY]) |
| 102 | + end |
| 103 | + |
| 104 | + def application_name |
| 105 | + @application.details['application_name'] || 'ROOT' |
| 106 | + end |
| 107 | + |
| 108 | + def contrast_config |
| 109 | + @droplet.sandbox + 'contrast.config' |
| 110 | + end |
| 111 | + |
| 112 | + def write_configuration(credentials) |
| 113 | + doc = REXML::Document.new |
| 114 | + |
| 115 | + add_contrast doc, credentials |
| 116 | + |
| 117 | + contrast_config.open(File::CREAT | File::WRONLY) { |f| f.write(doc) } |
| 118 | + end |
| 119 | + |
| 120 | + end |
| 121 | + |
| 122 | + end |
| 123 | + |
| 124 | +end |
0 commit comments