Skip to content

Commit 2914045

Browse files
committed
Merge branch '505-disable-dns-caching'
2 parents 1cd01e1 + 087010f commit 2914045

14 files changed

Lines changed: 146 additions & 15 deletions

File tree

.idea/dictionaries/bhale.xml

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

.rubocop.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Metrics/CyclomaticComplexity:
3131
Metrics/LineLength:
3232
Max: 120
3333
Metrics/MethodLength:
34-
Max: 20
34+
Max: 25
3535
Metrics/ParameterLists:
36-
Max: 8
36+
Max: 10
3737
Metrics/PerceivedComplexity:
3838
Max: 10
3939
RSpec/ExampleLength:

config/components.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ frameworks:
6666
- "JavaBuildpack::Framework::SpringInsight"
6767
- "JavaBuildpack::Framework::YourKitProfiler"
6868
- "JavaBuildpack::Framework::TakipiAgent"
69-
- "JavaBuildpack::Framework::SecurityProviders"
69+
- "JavaBuildpack::Framework::JavaSecurity"
7070
- "JavaBuildpack::Framework::JavaOpts"

lib/java_buildpack/buildpack.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require 'java_buildpack/component/immutable_java_home'
2424
require 'java_buildpack/component/java_opts'
2525
require 'java_buildpack/component/mutable_java_home'
26+
require 'java_buildpack/component/networking'
2627
require 'java_buildpack/component/security_providers'
2728
require 'java_buildpack/logging/logger_factory'
2829
require 'java_buildpack/util/cache/application_cache'
@@ -131,6 +132,7 @@ def initialize(app_dir, application)
131132
'env_vars' => Component::EnvironmentVariables.new(app_dir),
132133
'extension_directories' => Component::ExtensionDirectories.new(app_dir),
133134
'java_opts' => @java_opts,
135+
'networking' => Component::Networking.new,
134136
'security_providers' => Component::SecurityProviders.new
135137
}
136138

@@ -181,7 +183,7 @@ def instantiate(components, java_home, component_info)
181183
droplet: Component::Droplet.new(component_info['additional_libraries'], component_id,
182184
component_info['env_vars'], component_info['extension_directories'],
183185
java_home, component_info['java_opts'], component_info['app_dir'],
184-
component_info['security_providers'])
186+
component_info['networking'], component_info['security_providers'])
185187
}
186188
component.constantize.new(context)
187189
end

lib/java_buildpack/component/droplet.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class Droplet
5656
# @return [JavaOpts] the shared +JavaOpts+ instance for all components
5757
attr_reader :java_opts
5858

59+
# @!attribute [r] networking
60+
# @return [Networking] the shared +Networking+ instance for all components
61+
attr_reader :networking
62+
5963
# @!attribute [r] root
6064
# @return [JavaBuildpack::Util::FilteringPathname] the root of the droplet's fileystem filtered so that it
6165
# excludes files in the sandboxes of other components
@@ -83,10 +87,11 @@ class Droplet
8387
# be an instance of +MutableJavaHome+. Otherwise it should
8488
# be an instance of +ImmutableJavaHome+.
8589
# @param [JavaOpts] java_opts the shared +JavaOpts+ instance for all components
90+
# @param [Networking] networking the shared +Networking+ instance for all components
8691
# @param [Pathname] root the root of the droplet
8792
# @param [SecurityProviders] security_providers the shared +SecurityProviders+ instance for all components
8893
def initialize(additional_libraries, component_id, env_vars, extension_directories, java_home, java_opts, root,
89-
security_providers)
94+
networking, security_providers)
9095

9196
@additional_libraries = additional_libraries
9297
@component_id = component_id
@@ -110,6 +115,7 @@ def initialize(additional_libraries, component_id, env_vars, extension_directori
110115
->(path) { !in?(path, buildpack_root) || in?(path, @sandbox) },
111116
true
112117
)
118+
@networking = networking
113119
@security_providers = security_providers
114120
end
115121

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-2017 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
require 'fileutils'
17+
require 'java_buildpack/component'
18+
19+
module JavaBuildpack
20+
module Component
21+
22+
# An abstraction around the networking configuration provided to a droplet by components.
23+
#
24+
# A new instance of this type should be created once for the application.
25+
class Networking
26+
27+
# @!attribute [rw] networkaddress_cache_ttl
28+
# @return [Integer] the number of seconds to cache the successful lookup
29+
attr_accessor :networkaddress_cache_ttl
30+
31+
# @!attribute [rw] networkaddress_cache_negative_ttl
32+
# @return [Integer] the number of seconds to cache the failure for un-successful lookups
33+
attr_accessor :networkaddress_cache_negative_ttl
34+
35+
# Write the networking configuration to a destination file
36+
#
37+
# @param [Pathname] destination the destination to write to
38+
# @return [Void]
39+
def write_to(destination)
40+
FileUtils.mkdir_p destination.parent
41+
42+
destination.open(File::CREAT | File::APPEND | File::WRONLY) do |f|
43+
f.write "networkaddress.cache.ttl=#{@networkaddress_cache_ttl}\n" if @networkaddress_cache_ttl
44+
45+
if @networkaddress_cache_negative_ttl
46+
f.write "networkaddress.cache.negative.ttl=#{networkaddress_cache_negative_ttl}\n"
47+
end
48+
end
49+
end
50+
51+
end
52+
53+
end
54+
end

lib/java_buildpack/component/security_providers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SecurityProviders < Array
3131
def write_to(destination)
3232
FileUtils.mkdir_p destination.parent
3333

34-
destination.open(File::CREAT | File::WRONLY) do |f|
34+
destination.open(File::CREAT | File::APPEND | File::WRONLY) do |f|
3535
each_with_index { |security_provider, index| f.write "security.provider.#{index + 1}=#{security_provider}\n" }
3636
end
3737
end

lib/java_buildpack/framework/security_providers.rb renamed to lib/java_buildpack/framework/java_security.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ module JavaBuildpack
2121
module Framework
2222

2323
# Encapsulates the functionality for contributing custom Security Providers to an application.
24-
class SecurityProviders < JavaBuildpack::Component::BaseComponent
24+
class JavaSecurity < JavaBuildpack::Component::BaseComponent
2525

2626
# (see JavaBuildpack::Component::BaseComponent#detect)
2727
def detect
28-
SecurityProviders.to_s.dash_case
28+
JavaSecurity.to_s.dash_case
2929
end
3030

3131
# (see JavaBuildpack::Component::BaseComponent#compile)
3232
def compile
33+
@droplet.networking.write_to java_security
3334
@droplet.security_providers.write_to java_security
3435
end
3536

lib/java_buildpack/jre/open_jdk_like_jre.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
require 'ipaddr'
1617
require 'fileutils'
1718
require 'java_buildpack/component/versioned_dependency_component'
1819
require 'java_buildpack/jre'
1920
require 'java_buildpack/util/tokenized_version'
21+
require 'resolv'
2022

2123
module JavaBuildpack
2224
module Jre
@@ -48,6 +50,7 @@ def detect
4850
def compile
4951
download_tar
5052
@droplet.copy_resources
53+
disable_dns_caching if link_local_dns?
5154

5255
return if @droplet.java_home.java_8_or_later?
5356

@@ -62,6 +65,25 @@ def release
6265
.add_system_property('java.io.tmpdir', '$TMPDIR')
6366
end
6467

68+
private
69+
70+
LINK_LOCAL = IPAddr.new('169.254.0.0/16').freeze
71+
72+
private_constant :LINK_LOCAL
73+
74+
def disable_dns_caching
75+
puts ' JVM DNS caching disabled in lieu of BOSH DNS caching'
76+
77+
@droplet.networking.networkaddress_cache_ttl = 0
78+
@droplet.networking.networkaddress_cache_negative_ttl = 0
79+
end
80+
81+
def link_local_dns?
82+
Resolv::DNS::Config.new.lazy_initialize.nameserver_port.any? do |nameserver_port|
83+
LINK_LOCAL.include? IPAddr.new(nameserver_port[0])
84+
end
85+
end
86+
6587
end
6688

6789
end

spec/droplet_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'java_buildpack/component/extension_directories'
2323
require 'java_buildpack/component/immutable_java_home'
2424
require 'java_buildpack/component/java_opts'
25+
require 'java_buildpack/component/networking'
2526
require 'java_buildpack/component/security_providers'
2627
require 'java_buildpack/util/snake_case'
2728
require 'pathname'
@@ -38,7 +39,8 @@
3839

3940
let(:droplet) do
4041
JavaBuildpack::Component::Droplet.new(additional_libraries, component_id, environment_variables,
41-
extension_directories, java_home, java_opts, app_dir, security_providers)
42+
extension_directories, java_home, java_opts, app_dir, networking,
43+
security_providers)
4244
end
4345

4446
let(:extension_directories) { JavaBuildpack::Component::ExtensionDirectories.new app_dir }
@@ -62,6 +64,8 @@
6264
java_opts
6365
end
6466

67+
let(:networking) { JavaBuildpack::Component::Networking.new }
68+
6569
let(:security_providers) { JavaBuildpack::Component::SecurityProviders.new }
6670

6771
before do

0 commit comments

Comments
 (0)