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