-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlink_local_dns.go
65 lines (55 loc) · 1.73 KB
/
link_local_dns.go
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
/*
* Copyright 2018-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
*
* https://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.
*/
package helper
import (
"fmt"
"net"
"os"
"github.com/miekg/dns"
"github.com/paketo-buildpacks/libpak/bard"
"golang.org/x/sys/unix"
)
type LinkLocalDNS struct {
Config *dns.ClientConfig
Logger bard.Logger
}
func (l LinkLocalDNS) Execute() (map[string]string, error) {
if !net.ParseIP(l.Config.Servers[0]).IsLinkLocalUnicast() {
return nil, nil
}
file, ok := os.LookupEnv("JAVA_SECURITY_PROPERTIES")
if !ok {
return nil, fmt.Errorf("$JAVA_SECURITY_PROPERTIES must be set")
}
if unix.Access(file, unix.W_OK) != nil {
l.Logger.Infof("WARNING: Unable to disable JVM DNS caching disabled in favor of link-local DNS caching because %s is read-only", file)
return nil, nil
}
l.Logger.Info("JVM DNS caching disabled in favor of link-local DNS caching")
f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil, fmt.Errorf("unable to open %s\n%w", file, err)
}
defer f.Close()
_, err = f.WriteString(`
networkaddress.cache.ttl=0
networkaddress.cache.negative.ttl=0
`)
if err != nil {
return nil, fmt.Errorf("unable to write DNS configuration to %s\n%w", file, err)
}
return nil, nil
}