Skip to content

Commit 01ebbb5

Browse files
committed
Polishing
This change does some final polishing of the Contrast Security Framework. [resolves cloudfoundry#446]
1 parent 4a78d5d commit 01ebbb5

7 files changed

Lines changed: 97 additions & 58 deletions

File tree

.idea/dictionaries/bhale.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/versions__JSON_.xml renamed to .idea/runConfigurations/versions__Markdown_.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/versions__Pivotal_Network_.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/framework-contrast_security_agent.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ When binding ContrastSecurity using a user-provided service, it must have name o
1414

1515
| Name | Description
1616
| ---- | -----------
17-
| `teamserver_url` | The base URL in which your user has access to and the URL to which the Agent will report. ex: https://app.contrastsecurity.com
18-
| `username` | The account name to use when downloading the agent
19-
| `org_uuid` | The org uuid to send app information to, this is the org that your bound application will appear within
2017
| `api_key` | Your user's api key
2118
| `service_key` | Your user's service key
22-
19+
| `teamserver_url` | The base URL in which your user has access to and the URL to which the Agent will report. ex: https://app.contrastsecurity.com
20+
| `username` | The account name to use when downloading the agent
2321

2422
## Configuration
2523
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].

lib/java_buildpack/framework/contrast_security_agent.rb

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,107 @@
1818
require 'fileutils'
1919
require 'java_buildpack/component/versioned_dependency_component'
2020
require 'java_buildpack/framework'
21+
require 'java_buildpack/util/qualify_path'
2122
require 'rexml/document'
2223

2324
module JavaBuildpack
2425
module Framework
2526

2627
# Encapsulates the functionality for running the Contrast Security Agent support.
2728
class ContrastSecurityAgent < JavaBuildpack::Component::VersionedDependencyComponent
29+
include JavaBuildpack::Util
2830

2931
# (see JavaBuildpack::Component::BaseComponent#compile)
3032
def compile
31-
download_jar(boot_class_name)
32-
build_contrast_configuration
33+
download_jar
3334
@droplet.copy_resources
35+
36+
write_configuration @application.services.find_service(CONTRAST_FILTER)['credentials']
3437
end
3538

3639
# (see JavaBuildpack::Component::BaseComponent#release)
3740
def release
38-
app_name = @application.details['application_name'] || 'ROOT'
39-
java_opts = @droplet.java_opts
40-
java_opts.add_system_property('contrast.dir', '$TMPDIR')
41-
java_opts.add_system_property('contrast.override.appname', app_name)
42-
path = java_opts.qualify_path(@droplet.sandbox)
43-
java_opts.add_preformatted_options("-javaagent:#{path}/#{boot_class_name}=#{path}/contrast.config")
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)}")
4446
end
4547

4648
protected
4749

50+
# (see JavaBuildpack::Component::VersionedDependencyComponent#jar_name)
51+
def jar_name
52+
"contrast-engine-#{@version.to_s.split('_')[0]}.jar"
53+
end
54+
4855
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
4956
def supports?
50-
@application.services.one_service?(CONTRAST_FILTER, TEAMSERVER_URL, USERNAME, API_KEY, SERVICE_KEY)
57+
@application.services.one_service?(CONTRAST_FILTER, API_KEY, SERVICE_KEY, TEAMSERVER_URL, USERNAME)
5158
end
5259

5360
private
5461

5562
API_KEY = 'api_key'.freeze
63+
5664
CONTRAST_FILTER = 'contrast-security'.freeze
65+
66+
PLUGIN_PACKAGE = 'com.aspectsecurity.contrast.runtime.agent.plugins.'.freeze
67+
5768
SERVICE_KEY = 'service_key'.freeze
69+
5870
TEAMSERVER_URL = 'teamserver_url'.freeze
71+
5972
USERNAME = 'username'.freeze
6073

61-
private_constant :API_KEY
62-
private_constant :CONTRAST_FILTER
63-
private_constant :SERVICE_KEY
64-
private_constant :TEAMSERVER_URL
65-
private_constant :USERNAME
74+
private_constant :API_KEY, :CONTRAST_FILTER, :PLUGIN_PACKAGE, :SERVICE_KEY, :TEAMSERVER_URL, :USERNAME
6675

67-
PLUGIN_PACKAGE = 'com.aspectsecurity.contrast.runtime.agent.plugins.'.freeze
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')
6882

69-
def credentials
70-
@application.services.find_service(CONTRAST_FILTER)['credentials']
83+
add_user contrast, credentials
84+
add_plugins contrast
7185
end
7286

73-
def boot_class_name
74-
version = @version.to_s.split('_')[0]
75-
"contrast-engine-#{version}.jar"
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")
7696
end
7797

78-
def build_contrast_configuration
79-
doc = REXML::Document.new
80-
contrast = doc.add_element('contrast')
81-
(contrast.add_element 'id').add_text('default')
82-
(contrast.add_element 'global-key').add_text(credentials[API_KEY])
98+
def add_user(contrast, credentials)
8399
user = contrast.add_element('user')
84100
(user.add_element 'id').add_text(credentials[USERNAME])
85101
(user.add_element 'key').add_text(credentials[SERVICE_KEY])
86-
(contrast.add_element 'url').add_text("#{credentials[TEAMSERVER_URL]}/Contrast/s/")
87-
(contrast.add_element 'results-mode').add_text('never')
88-
89-
add_plugins(contrast)
90-
91-
contrast_config.open(File::CREAT | File::WRONLY) { |f| f.write(doc) }
92102
end
93103

94-
def add_plugins(config)
95-
plugin_package = 'com.aspectsecurity.contrast.runtime.agent.plugins.'
96-
plugin_group = config.add_element('plugins')
97-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.security.SecurityPlugin")
98-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.architecture.ArchitecturePlugin")
99-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.appupdater.ApplicationUpdatePlugin")
100-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.sitemap.SitemapPlugin")
101-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.frameworks.FrameworkSupportPlugin")
102-
(plugin_group.add_element 'plugin').add_text("#{plugin_package}.http.HttpPlugin")
104+
def application_name
105+
@application.details['application_name'] || 'ROOT'
103106
end
104107

105108
def contrast_config
106109
@droplet.sandbox + 'contrast.config'
107110
end
108111

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+
109120
end
110121

111122
end
112-
end
123+
124+
end

rakelib/versions_task.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def initialize
5252
'app_dynamics_agent' => 'AppDynamics Agent',
5353
'container_customizer' => 'Spring Boot Container Customizer',
5454
'container_security_provider' => 'Container Security Provider',
55+
'contrast_security_agent' => 'Contrast Security Agent',
5556
'dyadic_ekm_security_provider' => 'Dyadic EKM Security Provider',
5657
'dynatrace_appmon_agent' => 'Dynatrace Appmon Agent',
5758
'dynatrace_one_agent' => 'Dynatrace OneAgent',

spec/java_buildpack/framework/contrast_security_agent_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,21 @@
2222

2323
describe JavaBuildpack::Framework::ContrastSecurityAgent do
2424
include_context 'component_helper'
25-
let(:configuration) do
26-
{ 'teamserver_url' => 'a_url',
27-
'org_uuid' => '12345',
28-
'username' => 'contrast_user',
29-
'api_key' => 'api_test',
30-
'service_key' => 'service_test' }
31-
end
3225

3326
it 'does not detect without contrastsecurity service' do
3427
expect(component.detect).to be_nil
3528
end
3629

3730
context do
31+
3832
before do
39-
allow(services).to receive(:one_service?).with(/contrast[-]?security/,
40-
'teamserver_url','username', 'api_key', 'service_key').and_return(true)
41-
allow(services).to receive(:find_service).and_return('credentials' => :configuration)
33+
allow(services).to receive(:one_service?).with(/contrast-security/, 'api_key', 'service_key', 'teamserver_url',
34+
'username').and_return(true)
35+
allow(services).to receive(:find_service).and_return('credentials' => { 'teamserver_url' => 'a_url',
36+
'org_uuid' => '12345',
37+
'username' => 'contrast_user',
38+
'api_key' => 'api_test',
39+
'service_key' => 'service_test' })
4240
end
4341

4442
it 'detects with contrastsecurity service' do
@@ -55,17 +53,19 @@
5553
it 'updates JAVA_OPTS' do
5654
component.release
5755

58-
expect(java_opts).to include('-javaagent:$PWD/.java-buildpack/contrast_security_agent/contrast-engine-0.0.0.jar'\
56+
expect(java_opts).to include('-javaagent:$PWD/.java-buildpack/contrast_security_agent/contrast-engine-0.0.0.jar' \
5957
'=$PWD/.java-buildpack/contrast_security_agent/contrast.config')
6058
expect(java_opts).to include('-Dcontrast.dir=$TMPDIR')
6159
expect(java_opts).to include('-Dcontrast.override.appname=test-application-name')
6260
end
6361

6462
it 'created contrast.config',
6563
cache_fixture: 'stub-contrast-security-agent.jar' do
64+
6665
component.compile
6766
expect(sandbox + 'contrast.config').to exist
6867
end
68+
6969
end
7070

7171
end

0 commit comments

Comments
 (0)