From 17f1cb255ac30e638bd77aedada361226eb1cbda Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 9 Dec 2024 12:50:49 +0100 Subject: [PATCH 1/3] File master source Fixes #84 --- .../internal/sources/FileMasterSource.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java diff --git a/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java new file mode 100644 index 0000000..66c680d --- /dev/null +++ b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.codehaus.plexus.components.secdispatcher.internal.sources; + +import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta; +import org.codehaus.plexus.components.secdispatcher.SecDispatcher; +import org.codehaus.plexus.components.secdispatcher.SecDispatcherException; + +import javax.inject.Named; +import javax.inject.Singleton; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * Password source that uses a plain file with plaintext master password (residing on things like an encrypted pen-drive + * or partition). Idea is to "delegate" all the security to that data carrier (for example, it may ask for permission + * or password on access to that path). Not recommended to be used on any unprotected data storage or partition. + *

+ * Config: {@code file:$fileName} + *

+ * The file may start with "#" for human comments, and first non-commented line (trimmed) will be used + * as master password. + */ +@Singleton +@Named(FileMasterSource.NAME) +public final class FileMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta { + public static final String NAME = "file"; + + public FileMasterSource() { + super(NAME + ":"); + } + + @Override + public String description() { + return "File (file name should be edited; use absolute path)"; + } + + @Override + public Optional configTemplate() { + return Optional.of(NAME + ":$fileName"); + } + + @Override + protected String doHandle(String transformed) throws SecDispatcherException { + String value = readFile(transformed); + if (value == null) { + throw new SecDispatcherException("File '" + transformed + "' not found or is not readable"); + } + return value; + } + + @Override + protected SecDispatcher.ValidationResponse doValidateConfiguration(String transformed) { + String value = readFile(transformed); + if (value == null) { + return new SecDispatcher.ValidationResponse( + getClass().getSimpleName(), + true, + Map.of( + SecDispatcher.ValidationResponse.Level.WARNING, + List.of("Configured file does not exist or is not readable")), + List.of()); + } else { + return new SecDispatcher.ValidationResponse( + getClass().getSimpleName(), + true, + Map.of( + SecDispatcher.ValidationResponse.Level.INFO, + List.of("Configured file exist and is readable")), + List.of()); + } + } + + private String readFile(String transformed) throws SecDispatcherException { + Path file = Paths.get(transformed); + if (file.isAbsolute() && Files.exists(file)) { + try { + return Files.readAllLines(file).stream().filter(l -> l.startsWith("#")).map(String::trim).findFirst().orElse(null); + } catch (IOException e) { + throw new SecDispatcherException("Failed to read file '" + transformed + "'", e); + } + } + return null; + } +} From a3d040c7594e652cda78ae363efe4ada4e8a3c11 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 9 Dec 2024 12:52:30 +0100 Subject: [PATCH 2/3] Reformat --- .../internal/sources/FileMasterSource.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java index 66c680d..4bca7b6 100644 --- a/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java +++ b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java @@ -18,12 +18,9 @@ */ package org.codehaus.plexus.components.secdispatcher.internal.sources; -import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta; -import org.codehaus.plexus.components.secdispatcher.SecDispatcher; -import org.codehaus.plexus.components.secdispatcher.SecDispatcherException; - import javax.inject.Named; import javax.inject.Singleton; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -32,6 +29,10 @@ import java.util.Map; import java.util.Optional; +import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta; +import org.codehaus.plexus.components.secdispatcher.SecDispatcher; +import org.codehaus.plexus.components.secdispatcher.SecDispatcherException; + /** * Password source that uses a plain file with plaintext master password (residing on things like an encrypted pen-drive * or partition). Idea is to "delegate" all the security to that data carrier (for example, it may ask for permission @@ -96,7 +97,11 @@ private String readFile(String transformed) throws SecDispatcherException { Path file = Paths.get(transformed); if (file.isAbsolute() && Files.exists(file)) { try { - return Files.readAllLines(file).stream().filter(l -> l.startsWith("#")).map(String::trim).findFirst().orElse(null); + return Files.readAllLines(file).stream() + .filter(l -> l.startsWith("#")) + .map(String::trim) + .findFirst() + .orElse(null); } catch (IOException e) { throw new SecDispatcherException("Failed to read file '" + transformed + "'", e); } From 2160fbc19653a70791e76c020a421d6d25ea7a9e Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 9 Dec 2024 13:24:18 +0100 Subject: [PATCH 3/3] Neg this --- .../secdispatcher/internal/sources/FileMasterSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java index 4bca7b6..0851549 100644 --- a/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java +++ b/src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/FileMasterSource.java @@ -98,7 +98,7 @@ private String readFile(String transformed) throws SecDispatcherException { if (file.isAbsolute() && Files.exists(file)) { try { return Files.readAllLines(file).stream() - .filter(l -> l.startsWith("#")) + .filter(l -> !l.startsWith("#")) .map(String::trim) .findFirst() .orElse(null);