-
Notifications
You must be signed in to change notification settings - Fork 819
/
PGPropertyServiceParser.java
257 lines (238 loc) · 9.15 KB
/
PGPropertyServiceParser.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright (c) 2021, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.util;
import org.postgresql.PGEnvironment;
import org.postgresql.PGProperty;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* helps to read Connection Service File.
* https://www.postgresql.org/docs/current/libpq-pgservice.html
*/
public class PGPropertyServiceParser {
private static final Logger LOGGER = Logger.getLogger(PGPropertyServiceParser.class.getName());
private final String serviceName;
private boolean ignoreIfOpenFails = true;
private PGPropertyServiceParser(String serviceName) {
this.serviceName = serviceName;
}
/**
* Read pg_service.conf resource
*
* @param serviceName service name to search for
* @return key value pairs
*/
public static @Nullable Properties getServiceProperties(String serviceName) {
PGPropertyServiceParser pgPropertyServiceParser = new PGPropertyServiceParser(serviceName);
return pgPropertyServiceParser.findServiceDescription();
}
private @Nullable Properties findServiceDescription() {
String resourceName = findPgServiceConfResourceName();
if (resourceName == null) {
return null;
}
//
Properties result = null;
try (InputStream inputStream = openInputStream(resourceName)) {
result = parseInputStream(inputStream);
} catch (IOException e) {
Level level = ignoreIfOpenFails ? Level.FINE : Level.WARNING;
LOGGER.log(level, "Failed to handle resource [{0}] with error [{1}]", new Object[]{resourceName, e.getMessage()});
}
//
return result;
}
// open URL or File
private InputStream openInputStream(String resourceName) throws IOException {
try {
URL url = new URL(resourceName);
return url.openStream();
} catch ( MalformedURLException ex ) {
// try file
File file = new File(resourceName);
return new FileInputStream(file);
}
}
// choose resource where to search for service description
private @Nullable String findPgServiceConfResourceName() {
// default file name
String pgServceConfFileDefaultName = PGEnvironment.PGSERVICEFILE.getDefaultValue();
// if there is value, use it - 1st priority
{
String propertyName = PGEnvironment.ORG_POSTGRESQL_PGSERVICEFILE.getName();
String resourceName = System.getProperty(propertyName);
if (resourceName != null && !resourceName.trim().isEmpty()) {
this.ignoreIfOpenFails = false;
LOGGER.log(Level.FINE, "Value [{0}] selected from property [{1}]",
new Object[]{resourceName, propertyName});
return resourceName;
}
}
// if there is value, use it - 2nd priority
{
String envVariableName = PGEnvironment.PGSERVICEFILE.getName();
String resourceName = System.getenv().get(envVariableName);
if (resourceName != null && !resourceName.trim().isEmpty()) {
this.ignoreIfOpenFails = false;
LOGGER.log(Level.FINE, "Value [{0}] selected from environment variable [{1}]",
new Object[]{resourceName, envVariableName});
return resourceName;
}
}
/*
if file in user home is readable, use it, otherwise continue - 3rd priority
in the case that the file is in the user home directory it is prepended with '.'
*/
{
String resourceName = "." + pgServceConfFileDefaultName;
File resourceFile = new File(OSUtil.getUserConfigRootDirectory(), resourceName);
if (resourceFile.canRead()) {
LOGGER.log(Level.FINE, "Value [{0}] selected because file exist in user home directory", new Object[]{resourceFile.getAbsolutePath()});
return resourceFile.getAbsolutePath();
}
}
// if there is value, use it - 4th priority
{
String envVariableName = PGEnvironment.PGSYSCONFDIR.getName();
String pgSysconfDir = System.getenv().get(envVariableName);
if (pgSysconfDir != null && !pgSysconfDir.trim().isEmpty()) {
String resourceName = pgSysconfDir + File.separator + pgServceConfFileDefaultName;
LOGGER.log(Level.FINE, "Value [{0}] selected using environment variable [{1}]", new Object[]{resourceName, envVariableName});
return resourceName;
}
}
// otherwise null
LOGGER.log(Level.FINE, "Value for resource [{0}] not found", pgServceConfFileDefaultName);
return null;
}
/*
# Requirements for stream handling (have to match with libpq behaviour)
#
# space around line is removed
# Line: " host=my-host "
# equal to : "host=my-host"
# keys are case sensitive
# Line: "host=my-host"
# not equal to : "HOST=my-host"
# keys are limited with values described in enum PGEnvironment field name
# key is invalid: "my-host=my-host"
# unexpected keys produce error
# Example: "my-host=my-host"
# Example: "HOST=my-host"
# space before equal sign becomes part of key
# Line: "host =my-host"
# key equals: "host "
# space after equal sign becomes part of value
# Line: "host= my-host"
# key equals: " my-host"
# in case of duplicate section - first entry counts
# Line: "[service-one]"
# Line: "host=host-one"
# Line: "[service-two]"
# Line: "host=host-two"
# --> section-one is selected
# in case of duplicate key - first entry counts
# Line: "[service-one]"
# Line: "host=host-one"
# Line: "host=host-two"
# --> host-one is selected
# service name is case sensitive
# Line: "[service-one]"
# Line: "[service-ONE]"
# --> these are unique service names
# whatever is between brackets is considered as service name (including space)
# Line: "[ service-ONE]"
# Line: "[service-ONE ]"
# Line: "[service ONE]"
# --> these are unique service names
*/
private @Nullable Properties parseInputStream(InputStream inputStream) throws IOException {
// build set of allowed keys
Set<String> allowedServiceKeys = Arrays.stream(PGProperty.values())
.map(PGProperty::getName)
.map(PGPropertyUtil::translatePGPropertyToPGService)
.collect(Collectors.toSet());
//
Properties result = new Properties();
boolean isFound = false;
try (
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)) {
//
String originalLine;
String line;
int lineNumber = 0;
while ((originalLine = br.readLine()) != null) {
lineNumber++;
// remove spaces around it
line = originalLine.trim();
// skip if empty line or starts with comment sign
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
// find first equal sign
int indexOfEqualSign = line.indexOf("=");
// is it section start?
if (line.startsWith("[") && line.endsWith("]")) {
// stop processing if section with correct name was found already
if (isFound) {
break;
}
// get name of section
String sectionName = line.substring(1, line.length() - 1);
// if match then mark it as section is found
if (serviceName.equals(sectionName)) {
isFound = true;
}
} else if (!isFound) {
// skip further processing until section is found
continue;
} else if (indexOfEqualSign > 1) {
// get key and value
String key = line.substring(0, indexOfEqualSign);
String value = line.substring(indexOfEqualSign + 1);
// check key against set of allowed keys
if (!allowedServiceKeys.contains(key)) {
// log list of allowed keys
String allowedValuesCommaSeparated =
allowedServiceKeys.stream().sorted().collect(Collectors.joining(","));
LOGGER.log(Level.SEVERE, "Got invalid key: line number [{0}], value [{1}], allowed "
+ "values [{2}]",
new Object[]{lineNumber, originalLine, allowedValuesCommaSeparated});
// stop processing because of invalid key
return null;
}
// ignore line if value is missing
if (!value.isEmpty()) {
// ignore line having duplicate key, otherwise store key-value pair
result.putIfAbsent(PGPropertyUtil.translatePGServiceToPGProperty(key), value);
}
} else {
// if not equal sign then stop processing because of invalid syntax
LOGGER.log(Level.WARNING, "Not valid line: line number [{0}], value [{1}]",
new Object[]{lineNumber, originalLine});
return null;
}
}
}
// null means failure - service is not found
return isFound ? result : null;
}
}