|
| 1 | +/* |
| 2 | + * This program is licensed to you under the Apache License Version 2.0, |
| 3 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 4 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 5 | + * |
| 6 | + * Unless required by applicable law or agreed to in writing, |
| 7 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 8 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.codehaus.plexus.components.secdispatcher.internal.dispatchers; |
| 13 | + |
| 14 | +import javax.inject.Inject; |
| 15 | +import javax.inject.Named; |
| 16 | +import javax.inject.Singleton; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +import org.codehaus.plexus.components.secdispatcher.Dispatcher; |
| 26 | +import org.codehaus.plexus.components.secdispatcher.DispatcherMeta; |
| 27 | +import org.codehaus.plexus.components.secdispatcher.MasterSource; |
| 28 | +import org.codehaus.plexus.components.secdispatcher.SecDispatcher; |
| 29 | +import org.codehaus.plexus.components.secdispatcher.SecDispatcher.ValidationResponse.Level; |
| 30 | +import org.codehaus.plexus.components.secdispatcher.SecDispatcherException; |
| 31 | + |
| 32 | +/** |
| 33 | + * This dispatcher does not actually perform any crypto operations, but just forwards the string to be decrypted |
| 34 | + * to a {@link MasterSource}. The given string is supposed to contain a valid source reference which is resolvable |
| 35 | + * by one of the bound {@link MasterSource} implementations (and not actually an encrypted value). |
| 36 | + * This dispatcher doesn't support encryption, but just validates and returns the given master source reference. |
| 37 | + */ |
| 38 | +@Singleton |
| 39 | +@Named(MasterSourceLookupDispatcher.NAME) |
| 40 | +public class MasterSourceLookupDispatcher implements Dispatcher, DispatcherMeta { |
| 41 | + public static final String NAME = "masterSourceLookup"; |
| 42 | + |
| 43 | + protected final Collection<MasterSource> sources; |
| 44 | + |
| 45 | + @Inject |
| 46 | + public MasterSourceLookupDispatcher(Collection<MasterSource> sources) { |
| 47 | + this.sources = sources; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String name() { |
| 52 | + return NAME; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String displayName() { |
| 57 | + return "Master Source Lookup Dispatcher"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Collection<Field> fields() { |
| 62 | + return Collections.emptyList(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public EncryptPayload encrypt(String str, Map<String, String> attributes, Map<String, String> config) |
| 67 | + throws SecDispatcherException { |
| 68 | + // just make sure the given string is a valid reference! |
| 69 | + decrypt(str, attributes, config); |
| 70 | + return new EncryptPayload(attributes, str); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String decrypt(String str, Map<String, String> attributes, Map<String, String> config) |
| 75 | + throws SecDispatcherException { |
| 76 | + Optional<String> plain = sources.stream() |
| 77 | + .map(source -> source.handle(str)) |
| 78 | + .filter(Objects::nonNull) |
| 79 | + .findFirst(); |
| 80 | + if (plain.isPresent()) { |
| 81 | + return plain.get(); |
| 82 | + } else { |
| 83 | + throw new SecDispatcherException("No master source found for : " + str); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public SecDispatcher.ValidationResponse validateConfiguration(Map<String, String> config) { |
| 89 | + // there is nothing really to validate without having a master reference at hand (which is outside the config) |
| 90 | + Map<Level, List<String>> report = Collections.singletonMap( |
| 91 | + SecDispatcher.ValidationResponse.Level.INFO, List.of("Configured Source configuration valid")); |
| 92 | + return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), true, report, Collections.emptyList()); |
| 93 | + } |
| 94 | +} |
0 commit comments