Skip to content

Commit 58eba7c

Browse files
committed
Required service credentials
Previously, if a service was named such that it matched a service search string (e.g. if it had insight anywhere within the name), the the service finder would match the service. This caused problems as some times as service named in a certain way wasn't what a framework was expecting. This change allows component implementors to search not only for a string in the service name, label, and tags, but also ensure that a set of keys are present in the credentials. If the a single service matches, but the credentials aren't right, a warning is printed and service will not be counted as a match. [#64765506]
1 parent a0ac39d commit 58eba7c

14 files changed

Lines changed: 57 additions & 21 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: ruby
33
rvm:
44
- 2.0.0-p353
55
- 1.9.3-p484
6-
before_script: "if [[ $TRAVIS_RUBY_VERSION != '1.9.3' || $TRAVIS_SECURE_ENV_VARS != 'true' ]]; then unset CODECLIMATE_REPO_TOKEN; fi"
6+
before_script: "if [[ $TRAVIS_RUBY_VERSION != '1.9.3-p484' || $TRAVIS_SECURE_ENV_VARS != 'true' ]]; then unset CODECLIMATE_REPO_TOKEN; fi"
77
install: bundle install --deployment --without debug
88
notifications:
99
webhooks: http://build-monitor.cfapps.io/projects/JBP/webhook

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Cloud Foundry Java Buildpack
22
[![Build Status](https://travis-ci.org/cloudfoundry/java-buildpack.png?branch=master)](https://travis-ci.org/cloudfoundry/java-buildpack)
33
[![Dependency Status](https://gemnasium.com/cloudfoundry/java-buildpack.png)](http://gemnasium.com/cloudfoundry/java-buildpack)
4-
[![Code Climate](https://codeclimate.com/github/cloudfoundry/java-buildpack.png)](https://codeclimate.com/github/cloudfoundry/java-buildpack)
4+
[![Code Climate](https://codeclimate.com/repos/5224adaec7f3a3415107004c/badges/bc49f7d7f8dfc47057c8/gpa.png)](https://codeclimate.com/repos/5224adaec7f3a3415107004c/feed)
5+
[![Code Climate](https://codeclimate.com/repos/5224adaec7f3a3415107004c/badges/bc49f7d7f8dfc47057c8/coverage.png)](https://codeclimate.com/repos/5224adaec7f3a3415107004c/feed)
56

67
The `java-buildpack` is a [Cloud Foundry][] buildpack for running Java applications. It is designed to run most Java applications with no additional configuration, but supports configuration of the standard components, and extension to add custom components.
78

lib/java_buildpack/component/services.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/component'
18+
require 'java_buildpack/logging/logger_factory'
1819

1920
module JavaBuildpack::Component
2021

@@ -31,9 +32,27 @@ def initialize(raw)
3132
# +filter+ matches exactly one service, +false+ otherwise.
3233
#
3334
# @param [Regexp, String] filter a +RegExp+ or +String+ to match against the name, label, and tags of the services
34-
# @return [Boolean] +true+ if the +filter+ matches exactly one service, +false+ otherwise.
35-
def one_service?(filter)
36-
one?(&matcher(filter))
35+
# @param [String] required_credentials an optional list of keys that must exist in the credentials payload of
36+
# the candidate service
37+
# @return [Boolean] +true+ if the +filter+ matches exactly one service with the required credentials, +false+
38+
# otherwise.
39+
def one_service?(filter, *required_credentials)
40+
candidates = select(&matcher(filter))
41+
42+
match = false
43+
if candidates.one?
44+
if credentials?(candidates.first['credentials'], required_credentials)
45+
match = true
46+
else
47+
logger = JavaBuildpack::Logging::LoggerFactory.get_logger Services
48+
logger.warn do
49+
"A service with a name label or tag matching #{filter} was found, but was missing one of the required" \
50+
" credentials #{required_credentials}"
51+
end
52+
end
53+
end
54+
55+
match
3756
end
3857

3958
# Compares the name, label, and tags of each service to the given +filter+. The method returns the first service
@@ -47,6 +66,10 @@ def find_service(filter)
4766

4867
private
4968

69+
def credentials?(candidate, required_keys)
70+
required_keys.all? { |k| candidate.key? k }
71+
end
72+
5073
def matcher(filter)
5174
filter = Regexp.new(filter) unless filter.kind_of?(Regexp)
5275

lib/java_buildpack/framework/app_dynamics_agent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def release
5151

5252
# @macro versioned_dependency_component_supports
5353
def supports?
54-
@application.services.one_service? FILTER
54+
@application.services.one_service? FILTER, 'host-name'
5555
end
5656

5757
private

lib/java_buildpack/framework/maria_db_jdbc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def has_driver?
5050
end
5151

5252
def has_service?
53-
[/mysql/, /mariadb/].any? { |filter| @application.services.one_service? filter }
53+
[/mysql/, /mariadb/].any? { |filter| @application.services.one_service? filter, 'uri' }
5454
end
5555
end
5656

lib/java_buildpack/framework/new_relic_agent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def release
4545

4646
# @macro versioned_dependency_component_supports
4747
def supports?
48-
@application.services.one_service? FILTER
48+
@application.services.one_service? FILTER, 'licenseKey'
4949
end
5050

5151
private

lib/java_buildpack/framework/postgresql_jdbc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def has_driver?
4848
end
4949

5050
def has_service?
51-
@application.services.one_service? /postgres/
51+
@application.services.one_service? /postgres/, 'uri'
5252
end
5353
end
5454

lib/java_buildpack/framework/spring_insight.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def move(destination, *globs)
186186
end
187187

188188
def supports?
189-
@application.services.one_service? FILTER
189+
@application.services.one_service? FILTER, 'dashboard-url'
190190
end
191191

192192
def uber_agent_zip(location)

spec/java_buildpack/component/services_spec.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18+
require 'logging_helper'
1819
require 'java_buildpack/component/services'
1920

2021
describe JavaBuildpack::Component::Services do
22+
include_context 'logging_helper'
2123

2224
let(:service) do
2325
{ 'name' => 'test-name', 'label' => 'test-label', 'tags' => ['test-tag'], 'plan' => 'test-plan',
@@ -46,22 +48,32 @@
4648
expect(services.one_service? /test-tag/).to be
4749
end
4850

51+
it 'should return false from one_service? if there is a matching service without required credentials' do
52+
expect(services.one_service? 'test-tag', 'bad-credential').not_to be
53+
expect(services.one_service? /test-tag/, 'bad-credential').not_to be
54+
end
55+
56+
it 'should return true from one_service? if there is a matching service with required credentials' do
57+
expect(services.one_service? 'test-tag', 'uri').to be
58+
expect(services.one_service? /test-tag/, 'uri').to be
59+
end
60+
4961
it 'should return nil from find_service? if there is no service that matches' do
5062
expect(services.find_service 'bad-test').to be_nil
5163
expect(services.find_service /bad-test/).to be_nil
5264
end
5365

54-
it 'should return true from one_service? if there is a matching name' do
66+
it 'should return service from find_service? if there is a matching name' do
5567
expect(services.find_service 'test-name').to be(service)
5668
expect(services.find_service /test-name/).to be(service)
5769
end
5870

59-
it 'should return true from one_service? if there is a matching label' do
71+
it 'should return service from find_service? if there is a matching label' do
6072
expect(services.find_service 'test-label').to be(service)
6173
expect(services.find_service /test-label/).to be(service)
6274
end
6375

64-
it 'should return true from one_service? if there is a matching tag' do
76+
it 'should return service from find_service? if there is a matching tag' do
6577
expect(services.find_service 'test-tag').to be(service)
6678
expect(services.find_service /test-tag/).to be(service)
6779
end

spec/java_buildpack/framework/app_dynamics_agent_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
let(:credentials) { {} }
3333

3434
before do
35-
allow(services).to receive(:one_service?).with(/app-dynamics/).and_return(true)
35+
allow(services).to receive(:one_service?).with(/app-dynamics/, 'host-name').and_return(true)
3636
allow(services).to receive(:find_service).and_return('credentials' => credentials)
3737
end
3838

0 commit comments

Comments
 (0)