Skip to content

Commit 01406e6

Browse files
authored
File master source (#85)
Fixes #84
1 parent c935ede commit 01406e6

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 javax.inject.Named;
22+
import javax.inject.Singleton;
23+
24+
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Optional;
31+
32+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
33+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
34+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
35+
36+
/**
37+
* Password source that uses a plain file with plaintext master password (residing on things like an encrypted pen-drive
38+
* or partition). Idea is to "delegate" all the security to that data carrier (for example, it may ask for permission
39+
* or password on access to that path). Not recommended to be used on any unprotected data storage or partition.
40+
* <p>
41+
* Config: {@code file:$fileName}
42+
* <p>
43+
* The file may start with "#" for human comments, and first non-commented line (trimmed) will be used
44+
* as master password.
45+
*/
46+
@Singleton
47+
@Named(FileMasterSource.NAME)
48+
public final class FileMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
49+
public static final String NAME = "file";
50+
51+
public FileMasterSource() {
52+
super(NAME + ":");
53+
}
54+
55+
@Override
56+
public String description() {
57+
return "File (file name should be edited; use absolute path)";
58+
}
59+
60+
@Override
61+
public Optional<String> configTemplate() {
62+
return Optional.of(NAME + ":$fileName");
63+
}
64+
65+
@Override
66+
protected String doHandle(String transformed) throws SecDispatcherException {
67+
String value = readFile(transformed);
68+
if (value == null) {
69+
throw new SecDispatcherException("File '" + transformed + "' not found or is not readable");
70+
}
71+
return value;
72+
}
73+
74+
@Override
75+
protected SecDispatcher.ValidationResponse doValidateConfiguration(String transformed) {
76+
String value = readFile(transformed);
77+
if (value == null) {
78+
return new SecDispatcher.ValidationResponse(
79+
getClass().getSimpleName(),
80+
true,
81+
Map.of(
82+
SecDispatcher.ValidationResponse.Level.WARNING,
83+
List.of("Configured file does not exist or is not readable")),
84+
List.of());
85+
} else {
86+
return new SecDispatcher.ValidationResponse(
87+
getClass().getSimpleName(),
88+
true,
89+
Map.of(
90+
SecDispatcher.ValidationResponse.Level.INFO,
91+
List.of("Configured file exist and is readable")),
92+
List.of());
93+
}
94+
}
95+
96+
private String readFile(String transformed) throws SecDispatcherException {
97+
Path file = Paths.get(transformed);
98+
if (file.isAbsolute() && Files.exists(file)) {
99+
try {
100+
return Files.readAllLines(file).stream()
101+
.filter(l -> !l.startsWith("#"))
102+
.map(String::trim)
103+
.findFirst()
104+
.orElse(null);
105+
} catch (IOException e) {
106+
throw new SecDispatcherException("Failed to read file '" + transformed + "'", e);
107+
}
108+
}
109+
return null;
110+
}
111+
}

0 commit comments

Comments
 (0)