Skip to content

Commit 17f1cb2

Browse files
committed
File master source
Fixes #84
1 parent 8c04321 commit 17f1cb2

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.plexus.components.secdispatcher.internal.sources;
20+
21+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
22+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
23+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
24+
25+
import javax.inject.Named;
26+
import javax.inject.Singleton;
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Optional;
34+
35+
/**
36+
* Password source that uses a plain file with plaintext master password (residing on things like an encrypted pen-drive
37+
* or partition). Idea is to "delegate" all the security to that data carrier (for example, it may ask for permission
38+
* or password on access to that path). Not recommended to be used on any unprotected data storage or partition.
39+
* <p>
40+
* Config: {@code file:$fileName}
41+
* <p>
42+
* The file may start with "#" for human comments, and first non-commented line (trimmed) will be used
43+
* as master password.
44+
*/
45+
@Singleton
46+
@Named(FileMasterSource.NAME)
47+
public final class FileMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
48+
public static final String NAME = "file";
49+
50+
public FileMasterSource() {
51+
super(NAME + ":");
52+
}
53+
54+
@Override
55+
public String description() {
56+
return "File (file name should be edited; use absolute path)";
57+
}
58+
59+
@Override
60+
public Optional<String> configTemplate() {
61+
return Optional.of(NAME + ":$fileName");
62+
}
63+
64+
@Override
65+
protected String doHandle(String transformed) throws SecDispatcherException {
66+
String value = readFile(transformed);
67+
if (value == null) {
68+
throw new SecDispatcherException("File '" + transformed + "' not found or is not readable");
69+
}
70+
return value;
71+
}
72+
73+
@Override
74+
protected SecDispatcher.ValidationResponse doValidateConfiguration(String transformed) {
75+
String value = readFile(transformed);
76+
if (value == null) {
77+
return new SecDispatcher.ValidationResponse(
78+
getClass().getSimpleName(),
79+
true,
80+
Map.of(
81+
SecDispatcher.ValidationResponse.Level.WARNING,
82+
List.of("Configured file does not exist or is not readable")),
83+
List.of());
84+
} else {
85+
return new SecDispatcher.ValidationResponse(
86+
getClass().getSimpleName(),
87+
true,
88+
Map.of(
89+
SecDispatcher.ValidationResponse.Level.INFO,
90+
List.of("Configured file exist and is readable")),
91+
List.of());
92+
}
93+
}
94+
95+
private String readFile(String transformed) throws SecDispatcherException {
96+
Path file = Paths.get(transformed);
97+
if (file.isAbsolute() && Files.exists(file)) {
98+
try {
99+
return Files.readAllLines(file).stream().filter(l -> l.startsWith("#")).map(String::trim).findFirst().orElse(null);
100+
} catch (IOException e) {
101+
throw new SecDispatcherException("Failed to read file '" + transformed + "'", e);
102+
}
103+
}
104+
return null;
105+
}
106+
}

0 commit comments

Comments
 (0)