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;
15
16 import java.io.IOException;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity;
22
23 /**
24 * This component decrypts a string, passed to it using various dispatchers.
25 *
26 * @author Oleg Gusakov
27 */
28 public interface SecDispatcher {
29 /**
30 * Attribute that selects a dispatcher. If not present in {@link #encrypt(String, Map)} attributes, the
31 * configured "default dispatcher" is used.
32 *
33 * @see #availableDispatchers()
34 */
35 String DISPATCHER_NAME_ATTR = "name";
36
37 /**
38 * Attribute for version, added by SecDispatcher for possible upgrade path.
39 */
40 String DISPATCHER_VERSION_ATTR = "version";
41
42 /**
43 * Returns the set of available dispatcher metadata, never {@code null}.
44 */
45 Set<DispatcherMeta> availableDispatchers();
46
47 /**
48 * Encrypt given plaintext string.
49 *
50 * @param str the plaintext to encrypt
51 * @param attr the attributes, may be {@code null}
52 * @return encrypted string
53 * @throws SecDispatcherException in case of problem
54 */
55 String encrypt(String str, Map<String, String> attr) throws SecDispatcherException, IOException;
56
57 /**
58 * Decrypt given encrypted string.
59 *
60 * @param str the encrypted string
61 * @return decrypted string
62 * @throws SecDispatcherException in case of problem
63 */
64 String decrypt(String str) throws SecDispatcherException, IOException;
65
66 /**
67 * Returns {@code true} if passed in string adheres to "encrypted string" format (current or legacy).
68 *
69 * @since 4.0.1
70 */
71 default boolean isAnyEncryptedString(String str) {
72 return isEncryptedString(str) || isLegacyEncryptedString(str);
73 }
74
75 /**
76 * Returns {@code true} if passed in string adheres "encrypted string" format.
77 */
78 boolean isEncryptedString(String str);
79
80 /**
81 * Returns {@code true} if passed in string adheres to "legacy encrypted string" format.
82 */
83 boolean isLegacyEncryptedString(String str);
84
85 /**
86 * Reads the effective configuration, eventually creating new instance if not present.
87 *
88 * @param createIfMissing If {@code true}, it will create a new empty instance
89 * @return the configuration, of {@code null} if it does not exist in {@code createIfMissing} is {@code false}
90 * @throws IOException In case of IO problem
91 */
92 SettingsSecurity readConfiguration(boolean createIfMissing) throws IOException;
93
94 /**
95 * Writes the effective configuration.
96 *
97 * @param configuration The configuration to write, may not be {@code null}
98 * @throws IOException In case of IO problem
99 */
100 void writeConfiguration(SettingsSecurity configuration) throws IOException;
101
102 /**
103 * The validation response.
104 */
105 final class ValidationResponse {
106 public enum Level {
107 INFO,
108 WARNING,
109 ERROR
110 };
111
112 private final String source;
113 private final boolean valid;
114 private final Map<Level, List<String>> report;
115 private final List<ValidationResponse> subsystems;
116
117 public ValidationResponse(
118 String source, boolean valid, Map<Level, List<String>> report, List<ValidationResponse> subsystems) {
119 this.source = source;
120 this.valid = valid;
121 this.report = report;
122 this.subsystems = subsystems;
123 }
124
125 public String getSource() {
126 return source;
127 }
128
129 public boolean isValid() {
130 return valid;
131 }
132
133 public Map<Level, List<String>> getReport() {
134 return report;
135 }
136
137 public List<ValidationResponse> getSubsystems() {
138 return subsystems;
139 }
140 }
141
142 /**
143 * Performs a "deep validation" and reports the status. If return instance {@link ValidationResponse#isValid()}
144 * is {@code true}, configuration is usable.
145 */
146 ValidationResponse validateConfiguration();
147 }