-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcontrast_security_agent.rb
137 lines (102 loc) · 4.79 KB
/
contrast_security_agent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
# Cloud Foundry Java Buildpack
# Copyright 2013-2020 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fileutils'
require 'java_buildpack/component/versioned_dependency_component'
require 'java_buildpack/framework'
require 'java_buildpack/util/qualify_path'
require 'rexml/document'
module JavaBuildpack
module Framework
# Encapsulates the functionality for running the Contrast Security Agent support.
class ContrastSecurityAgent < JavaBuildpack::Component::VersionedDependencyComponent
include JavaBuildpack::Util
# (see JavaBuildpack::Component::BaseComponent#compile)
def compile
download_jar
@droplet.copy_resources
write_configuration @application.services.find_service(FILTER, API_KEY, SERVICE_KEY, TEAMSERVER_URL,
USERNAME)['credentials']
end
# (see JavaBuildpack::Component::BaseComponent#release)
def release
@droplet.java_opts.add_system_property('contrast.override.appname', application_name) unless appname_exist?
@droplet.java_opts
.add_system_property('contrast.dir', '$TMPDIR')
.add_preformatted_options("-javaagent:#{qualify_path(@droplet.sandbox + jar_name, @droplet.root)}=" \
"#{qualify_path(contrast_config, @droplet.root)}")
end
protected
# (see JavaBuildpack::Component::VersionedDependencyComponent#jar_name)
def jar_name
@version < INFLECTION_VERSION ? "contrast-engine-#{short_version}.jar" : "java-agent-#{short_version}.jar"
end
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
def supports?
@application.services.one_service? FILTER, API_KEY, SERVICE_KEY, TEAMSERVER_URL, USERNAME
end
private
API_KEY = 'api_key'
FILTER = 'contrast-security'
INFLECTION_VERSION = JavaBuildpack::Util::TokenizedVersion.new('3.4.3').freeze
PLUGIN_PACKAGE = 'com.aspectsecurity.contrast.runtime.agent.plugins'
SERVICE_KEY = 'service_key'
TEAMSERVER_URL = 'teamserver_url'
USERNAME = 'username'
private_constant :API_KEY, :FILTER, :INFLECTION_VERSION, :PLUGIN_PACKAGE, :SERVICE_KEY, :TEAMSERVER_URL,
:USERNAME
def add_contrast(doc, credentials)
contrast = doc.add_element('contrast')
(contrast.add_element 'id').add_text('default')
(contrast.add_element 'global-key').add_text(credentials[API_KEY])
(contrast.add_element 'url').add_text("#{credentials[TEAMSERVER_URL]}/Contrast/s/")
(contrast.add_element 'results-mode').add_text('never')
add_user contrast, credentials
add_plugins contrast
end
def add_plugins(contrast)
plugin_group = contrast.add_element('plugins')
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.security.SecurityPlugin")
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.architecture.ArchitecturePlugin")
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.appupdater.ApplicationUpdatePlugin")
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.sitemap.SitemapPlugin")
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.frameworks.FrameworkSupportPlugin")
(plugin_group.add_element 'plugin').add_text("#{PLUGIN_PACKAGE}.http.HttpPlugin")
end
def add_user(contrast, credentials)
user = contrast.add_element('user')
(user.add_element 'id').add_text(credentials[USERNAME])
(user.add_element 'key').add_text(credentials[SERVICE_KEY])
end
def application_name
@application.details['application_name'] || 'ROOT'
end
def appname_exist?
@droplet.java_opts.any? { |java_opt| java_opt =~ /contrast.override.appname/ }
end
def contrast_config
@droplet.sandbox + 'contrast.config'
end
def short_version
"#{@version[0]}.#{@version[1]}.#{@version[2]}"
end
def write_configuration(credentials)
doc = REXML::Document.new
add_contrast doc, credentials
contrast_config.open(File::CREAT | File::WRONLY) { |f| f.write(doc) }
end
end
end
end