Skip to content

Commit 922b96b

Browse files
committed
Polishing
This change polishes the contributed code a bit and adds a test for it. [resolves #365]
1 parent 96c9a0c commit 922b96b

File tree

8 files changed

+173
-49
lines changed

8 files changed

+173
-49
lines changed

config/dyadic_ekm_security_provider.yml

-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@
1717
---
1818
version: 1.+
1919
repository_root: https://repo.dyadicsec.com/cust/pcf
20-
logging_enabled: false

java-buildpack.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
<orderEntry type="sourceFolder" forTests="false" />
271271
<orderEntry type="library" scope="PROVIDED" name="addressable (v2.4.0, rbenv: 2.2.5) [gem]" level="application" />
272272
<orderEntry type="library" scope="PROVIDED" name="ast (v2.3.0, rbenv: 2.2.5) [gem]" level="application" />
273-
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.12.5, rbenv: 2.2.5) [gem]" level="application" />
273+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.13.6, rbenv: 2.2.5) [gem]" level="application" />
274274
<orderEntry type="library" scope="PROVIDED" name="codeclimate-test-reporter (v0.6.0, rbenv: 2.2.5) [gem]" level="application" />
275275
<orderEntry type="library" scope="PROVIDED" name="crack (v0.4.3, rbenv: 2.2.5) [gem]" level="application" />
276276
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, rbenv: 2.2.5) [gem]" level="application" />

lib/java_buildpack/framework/dyadic_ekm_security_provider.rb

+51-47
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,32 @@ def compile
3131
download_tar
3232
setup_ext_dir
3333

34-
@droplet.copy_resources
34+
@droplet.copy_resources
3535

36-
credentials = @application.services.find_service(FILTER)['credentials']
37-
write_key credentials['key']
38-
write_cert credentials['ca']
39-
write_conf credentials['servers'], credentials['send_timeout'], credentials['recv_timeout'], credentials['retries']
40-
end
36+
credentials = @application.services.find_service(FILTER)['credentials']
37+
write_key credentials['key']
38+
write_cert credentials['ca']
39+
write_conf credentials['servers'], credentials['send_timeout'], credentials['recv_timeout'],
40+
credentials['retries']
41+
end
4142

4243
# (see JavaBuildpack::Component::BaseComponent#release)
4344
def release
44-
@droplet
45-
.java_opts
46-
.add_system_property('java.library.path', @droplet.sandbox + 'usr/lib')
47-
@droplet.environment_variables.add_environment_variable 'LD_LIBRARY_PATH', @droplet.sandbox + 'usr/lib'
45+
@droplet
46+
.environment_variables
47+
.add_environment_variable 'LD_LIBRARY_PATH', @droplet.sandbox + 'usr/lib'
4848

4949
@droplet
5050
.java_opts
51-
.add_system_property('java.security.properties', @droplet.sandbox + 'java.security')
5251
.add_system_property('java.ext.dirs', ext_dirs)
52+
.add_system_property('java.security.properties', @droplet.sandbox + 'java.security')
5353
end
5454

5555
protected
5656

5757
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
5858
def supports?
59-
@application.services.one_service? FILTER
60-
#true
59+
@application.services.one_service? FILTER, 'ca', 'key', 'recv_timeout', 'retries', 'send_timeout', 'servers'
6160
end
6261

6362
private
@@ -66,56 +65,61 @@ def supports?
6665

6766
private_constant :FILTER
6867

69-
def ext_dir
70-
@droplet.sandbox + 'ext'
68+
def cert_file
69+
@droplet.sandbox + 'etc/dsm/ca.crt'
7170
end
72-
73-
def dyadic_jar
74-
@droplet.sandbox + 'usr/lib/dsm/dsm-advapi-1.0.jar'
75-
end
7671

77-
def setup_ext_dir
78-
FileUtils.mkdir ext_dir
79-
FileUtils.ln_s dyadic_jar.relative_path_from(ext_dir), ext_dir, force: true
72+
def conf_file
73+
@droplet.sandbox + 'etc/dsm/client.conf'
74+
end
75+
76+
def dyadic_jar
77+
@droplet.sandbox + 'usr/lib/dsm/dsm-advapi-1.0.jar'
78+
end
79+
80+
def ext_dir
81+
@droplet.sandbox + 'ext'
8082
end
8183

8284
def ext_dirs
8385
"#{qualify_path(@droplet.java_home.root + 'lib/ext', @droplet.root)}:" \
8486
"#{qualify_path(ext_dir, @droplet.root)}"
8587
end
86-
87-
def key_file
88-
@droplet.sandbox + 'etc/dsm/key.pem'
89-
end
90-
91-
def cert_file
92-
@droplet.sandbox + 'etc/dsm/ca.crt'
93-
end
9488

95-
def conf_file
96-
@droplet.sandbox + 'etc/dsm/client.conf'
89+
def key_file
90+
@droplet.sandbox + 'etc/dsm/key.pem'
9791
end
98-
99-
def write_key(key)
100-
key_file.open(File::CREAT | File::WRONLY) do |f|
101-
f.write key
102-
end
92+
93+
def setup_ext_dir
94+
FileUtils.mkdir ext_dir
95+
FileUtils.ln_s dyadic_jar.relative_path_from(ext_dir), ext_dir, force: true
10396
end
104-
105-
def write_cert(cert)
97+
98+
def write_cert(cert)
99+
FileUtils.mkdir_p cert_file.parent
106100
cert_file.open(File::CREAT | File::WRONLY) do |f|
107-
f.write cert
101+
f.write "#{cert}\n"
108102
end
109103
end
110-
111-
def write_conf(servers,send_timeout,recv_timeout,retries)
104+
105+
def write_conf(servers, send_timeout, recv_timeout, retries)
106+
FileUtils.mkdir_p conf_file.parent
112107
conf_file.open(File::CREAT | File::WRONLY) do |f|
113-
f.write "servers = " + servers + "\n"
114-
f.write "send_timeout = " + send_timeout + "\n"
115-
f.write "recv_timeout = " + recv_timeout + "\n"
116-
f.write "retries = " + retries + "\n"
108+
f.write <<EOS
109+
servers = #{servers}
110+
send_timeout = #{send_timeout}
111+
recv_timeout = #{recv_timeout}
112+
retries = #{retries}
113+
EOS
114+
end
115+
end
116+
117+
def write_key(key)
118+
FileUtils.mkdir_p key_file.parent
119+
key_file.open(File::CREAT | File::WRONLY) do |f|
120+
f.write "#{key}\n"
117121
end
118-
end
122+
end
119123

120124
end
121125
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN CERTIFICATE-----
2+
test-client-cert
3+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
servers = server-1,server-2
2+
send_timeout = 3
3+
recv_timeout = 1
4+
retries = 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
test-client-private-key
3+
-----END RSA PRIVATE KEY-----
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Encoding: utf-8
2+
# Cloud Foundry Java Buildpack
3+
# Copyright 2013-2016 the original author or authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require 'spec_helper'
18+
require 'component_helper'
19+
require 'java_buildpack/framework/dyadic_ekm_security_provider'
20+
21+
describe JavaBuildpack::Framework::DyadicEkmSecurityProvider do
22+
include_context 'component_helper'
23+
24+
it 'does not detect without dyadic-n/a service' do
25+
expect(component.detect).to be_nil
26+
end
27+
28+
context do
29+
30+
before do
31+
allow(services).to receive(:one_service?)
32+
.with(/dyadic/, 'ca', 'key', 'recv_timeout', 'retries', 'send_timeout', 'servers')
33+
.and_return(true)
34+
35+
allow(services).to receive(:find_service).and_return(
36+
'credentials' => {
37+
'ca' => "-----BEGIN CERTIFICATE-----\ntest-client-cert\n-----END CERTIFICATE-----",
38+
'key' => "-----BEGIN RSA PRIVATE KEY-----\ntest-client-private-key\n-----END RSA PRIVATE KEY-----",
39+
'recv_timeout' => 1,
40+
'retries' => 2,
41+
'send_timeout' => 3,
42+
'servers' => 'server-1,server-2'
43+
}
44+
)
45+
end
46+
47+
it 'detects with dyadic-n/a service' do
48+
expect(component.detect).to eq("dyadic-ekm-security-provider=#{version}")
49+
end
50+
51+
it 'copies resources',
52+
cache_fixture: 'stub-dyadic-ekm-security-provider.tar.gz' do
53+
54+
component.compile
55+
56+
expect(sandbox + 'java.security').to exist
57+
end
58+
59+
it 'unpacks the dyadic tar',
60+
cache_fixture: 'stub-dyadic-ekm-security-provider.tar.gz' do
61+
62+
component.compile
63+
64+
expect(sandbox + 'usr/lib/dsm/dsm-advapi-1.0.jar').to exist
65+
expect(sandbox + 'usr/lib').to exist
66+
end
67+
68+
it 'write certificate and key files',
69+
cache_fixture: 'stub-dyadic-ekm-security-provider.tar.gz' do
70+
71+
component.compile
72+
73+
expect(sandbox + 'etc/dsm/ca.crt').to exist
74+
expect(sandbox + 'etc/dsm/key.pem').to exist
75+
76+
check_file_contents(sandbox + 'etc/dsm/ca.crt',
77+
'spec/fixtures/framework_dyadic_ekm_security_provider/ca.crt')
78+
check_file_contents(sandbox + 'etc/dsm/key.pem',
79+
'spec/fixtures/framework_dyadic_ekm_security_provider/key.pem')
80+
end
81+
82+
it 'writes configuration',
83+
cache_fixture: 'stub-dyadic-ekm-security-provider.tar.gz' do
84+
85+
component.compile
86+
87+
expect(sandbox + 'etc/dsm/client.conf').to exist
88+
check_file_contents(sandbox + 'etc/dsm/client.conf',
89+
'spec/fixtures/framework_dyadic_ekm_security_provider/client.conf')
90+
end
91+
92+
it 'updates environment variables' do
93+
component.release
94+
expect(environment_variables).to include('LD_LIBRARY_PATH=$PWD/.java-buildpack/' \
95+
'dyadic_ekm_security_provider/usr/lib')
96+
end
97+
98+
it 'updates JAVA_OPTS' do
99+
component.release
100+
expect(java_opts).to include('-Djava.ext.dirs=$PWD/.test-java-home/lib/ext:$PWD/.java-buildpack/' \
101+
'dyadic_ekm_security_provider/ext')
102+
expect(java_opts).to include('-Djava.security.properties=$PWD/.java-buildpack/' \
103+
'dyadic_ekm_security_provider/java.security')
104+
end
105+
106+
def check_file_contents(actual, expected)
107+
expect(File.read(actual)).to eq File.read(expected)
108+
end
109+
110+
end
111+
end

0 commit comments

Comments
 (0)