-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlexoConfig.java
More file actions
322 lines (273 loc) · 9.81 KB
/
FlexoConfig.java
File metadata and controls
322 lines (273 loc) · 9.81 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package org.openmbee.flexo.cli.config;
import org.openmbee.flexo.cli.model.Remote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* Configuration management for Flexo CLI.
* Reads from multiple sources in order of precedence:
* 1. Environment variables
* 2. User config file (~/.flexo/config)
* 3. Application default properties
*/
public class FlexoConfig {
private static final Logger logger = LoggerFactory.getLogger(FlexoConfig.class);
private static final String USER_CONFIG_DIR = ".flexo";
private static final String USER_CONFIG_FILE = "config";
private static final String DEFAULT_PROPERTIES = "/application.properties";
private final Properties properties;
public FlexoConfig() {
this.properties = new Properties();
loadConfiguration();
}
private void loadConfiguration() {
// 1. Load default properties from resources
try (InputStream defaultStream = getClass().getResourceAsStream(DEFAULT_PROPERTIES)) {
if (defaultStream != null) {
properties.load(defaultStream);
logger.debug("Loaded default properties");
}
} catch (IOException e) {
logger.warn("Could not load default properties: {}", e.getMessage());
}
// 2. Load user config file if it exists
Path userConfigPath = getUserConfigPath();
if (Files.exists(userConfigPath)) {
try (InputStream userStream = Files.newInputStream(userConfigPath)) {
properties.load(userStream);
logger.debug("Loaded user config from: {}", userConfigPath);
} catch (IOException e) {
logger.warn("Could not load user config: {}", e.getMessage());
}
}
// 3. Override with environment variables
overrideWithEnvironment();
}
private void overrideWithEnvironment() {
// Environment variables override file config
// Format: FLEXO_MMS_URL -> mms.url
System.getenv().forEach((key, value) -> {
if (key.startsWith("FLEXO_")) {
String propKey = key.substring(6).toLowerCase().replace('_', '.');
properties.setProperty(propKey, value);
logger.debug("Environment override: {} = {}", propKey, value);
}
});
}
private Path getUserConfigPath() {
String userHome = System.getProperty("user.home");
return Paths.get(userHome, USER_CONFIG_DIR, USER_CONFIG_FILE);
}
/**
* Get a configuration property
*/
public String get(String key) {
return properties.getProperty(key);
}
/**
* Get a configuration property with a default value
*/
public String get(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
/**
* Get a boolean configuration property
*/
public boolean getBoolean(String key, boolean defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
return Boolean.parseBoolean(value);
}
/**
* Set a configuration property
*/
public void set(String key, String value) {
properties.setProperty(key, value);
}
/**
* Save configuration to user config file
*/
public void save() throws IOException {
Path userConfigPath = getUserConfigPath();
Files.createDirectories(userConfigPath.getParent());
try (OutputStream out = Files.newOutputStream(userConfigPath)) {
properties.store(out, "Flexo CLI Configuration");
logger.info("Configuration saved to: {}", userConfigPath);
}
}
// Convenience methods for common properties
public String getMmsUrl() {
return get("mms.url", "http://localhost:8080");
}
public boolean isAuthEnabled() {
return getBoolean("auth.enabled", false);
}
public String getSshKeyPath() {
String path = get("auth.sshKeyPath", "~/.ssh/id_rsa");
if (path.startsWith("~")) {
path = System.getProperty("user.home") + path.substring(1);
}
return path;
}
public String getDefaultOrg() {
return get("default.org", "");
}
public String getDefaultRepo() {
return get("default.repo", "");
}
public String getDefaultBranch() {
return get("default.branch", "master");
}
public String getRdfFormat() {
return get("rdf.format", "turtle");
}
public boolean isLocalMode() {
return getBoolean("local.mode", true);
}
public String getLocalUser() {
return get("local.user", "root");
}
public String getLocalJwtSecret() {
String secret = get("local.jwtSecret");
// If no secret is configured, generate and save one
if (secret == null || secret.isEmpty()) {
secret = generateJwtSecret();
set("local.jwtSecret", secret);
try {
save();
logger.info("Generated and saved new JWT secret");
} catch (IOException e) {
logger.warn("Failed to save generated JWT secret: {}", e.getMessage());
}
}
return secret;
}
/**
* Generate a secure random JWT secret (64 characters, base64-encoded)
*/
private String generateJwtSecret() {
java.security.SecureRandom random = new java.security.SecureRandom();
byte[] bytes = new byte[48]; // 48 bytes = 64 base64 characters
random.nextBytes(bytes);
return java.util.Base64.getEncoder().encodeToString(bytes);
}
// Remote management methods
/**
* Get all configured remotes
*/
public Map<String, Remote> getRemotes() {
Map<String, Remote> remotes = new LinkedHashMap<>();
// Find all remote.* properties
Set<String> remoteNames = new HashSet<>();
for (String key : properties.stringPropertyNames()) {
if (key.startsWith("remote.") && key.contains(".url")) {
String remoteName = key.substring(7, key.indexOf(".url"));
remoteNames.add(remoteName);
}
}
// Build Remote objects
for (String name : remoteNames) {
Remote remote = new Remote();
remote.setName(name);
remote.setUrl(get("remote." + name + ".url"));
remote.setAuthEnabled(get("remote." + name + ".authEnabled"));
remote.setSshKeyPath(get("remote." + name + ".sshKeyPath"));
remote.setLocalMode(get("remote." + name + ".localMode"));
remote.setLocalUser(get("remote." + name + ".localUser"));
remote.setLocalJwtSecret(get("remote." + name + ".localJwtSecret"));
remotes.put(name, remote);
}
return remotes;
}
/**
* Get a specific remote by name
*/
public Remote getRemote(String name) {
String url = get("remote." + name + ".url");
if (url == null) {
return null;
}
Remote remote = new Remote();
remote.setName(name);
remote.setUrl(url);
remote.setAuthEnabled(get("remote." + name + ".authEnabled"));
remote.setSshKeyPath(get("remote." + name + ".sshKeyPath"));
remote.setLocalMode(get("remote." + name + ".localMode"));
remote.setLocalUser(get("remote." + name + ".localUser"));
remote.setLocalJwtSecret(get("remote." + name + ".localJwtSecret"));
return remote;
}
/**
* Add or update a remote
*/
public void setRemote(Remote remote) {
String prefix = "remote." + remote.getName();
set(prefix + ".url", remote.getUrl());
if (remote.getAuthEnabled() != null) {
set(prefix + ".authEnabled", remote.getAuthEnabled());
}
if (remote.getSshKeyPath() != null) {
set(prefix + ".sshKeyPath", remote.getSshKeyPath());
}
if (remote.getLocalMode() != null) {
set(prefix + ".localMode", remote.getLocalMode());
}
if (remote.getLocalUser() != null) {
set(prefix + ".localUser", remote.getLocalUser());
}
if (remote.getLocalJwtSecret() != null) {
set(prefix + ".localJwtSecret", remote.getLocalJwtSecret());
}
}
/**
* Remove a remote
*/
public void removeRemote(String name) {
String prefix = "remote." + name;
List<String> keysToRemove = properties.stringPropertyNames().stream()
.filter(key -> key.startsWith(prefix + "."))
.collect(Collectors.toList());
for (String key : keysToRemove) {
properties.remove(key);
}
}
/**
* Get the default remote name
*/
public String getDefaultRemote() {
return get("default.remote", "origin");
}
/**
* Set the default remote name
*/
public void setDefaultRemote(String remoteName) {
set("default.remote", remoteName);
}
/**
* Get remote URL by name, or fall back to legacy mms.url
*/
public String getRemoteUrl(String remoteName) {
if (remoteName == null || remoteName.isEmpty()) {
remoteName = getDefaultRemote();
}
Remote remote = getRemote(remoteName);
if (remote != null) {
return remote.getUrl();
}
// Fall back to legacy mms.url for backward compatibility
return getMmsUrl();
}
/**
* Check if a remote with the given name exists
*/
public boolean hasRemote(String name) {
return get("remote." + name + ".url") != null;
}
}