Skip to content

Forwarding #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/

package org.codehaus.plexus.components.secdispatcher.internal.dispatchers;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.components.secdispatcher.Dispatcher;
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
import org.codehaus.plexus.components.secdispatcher.MasterSource;
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;

/**
* This dispatcher forwards requests fully to defined sources.
*/
@Singleton
@Named(ForwardingDispatcher.NAME)
public class ForwardingDispatcher implements Dispatcher, DispatcherMeta {
public static final String NAME = "forwarding";

private static final String CONF_SOURCE = "source";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the source already encoded in the prefix? Why bind this dispatcher to a single source instead of leveraging all MasterSource implementations?


protected final Map<String, MasterSource> sources;

@Inject
public ForwardingDispatcher(Map<String, MasterSource> sources) {
this.sources = sources;
}

@Override
public String name() {
return NAME;
}

@Override
public String displayName() {
return "Forwarding Password Dispatcher";
}

@Override
public Collection<Field> fields() {
return List.of(Field.builder(CONF_SOURCE)
.optional(false)
.description("Source of the password")
.options(sources.entrySet().stream()
.map(e -> {
MasterSource ms = e.getValue();
if (ms instanceof MasterSourceMeta m) {
Field.Builder b = Field.builder(e.getKey()).description(m.description());
if (m.configTemplate().isPresent()) {
b.defaultValue(m.configTemplate().get());
}
return b.build();
} else {
return Field.builder(e.getKey())
.description(e.getKey() + "(Field not described, needs manual configuration)")
.build();
}
})
.toList())
.build());
}

@Override
public EncryptPayload encrypt(String str, Map<String, String> attributes, Map<String, String> config)
throws SecDispatcherException {
throw new UnsupportedOperationException("Forwarding dispatcher does not support encryption");
}

@Override
public String decrypt(String str, Map<String, String> attributes, Map<String, String> config)
throws SecDispatcherException {
MasterSource masterSource = getPasswordSource(config);
return masterSource.handle(str);
}

@Override
public SecDispatcher.ValidationResponse validateConfiguration(Map<String, String> config) {
HashMap<SecDispatcher.ValidationResponse.Level, List<String>> report = new HashMap<>();
ArrayList<SecDispatcher.ValidationResponse> subsystems = new ArrayList<>();
boolean valid = false;
String masterSource = config.get(CONF_SOURCE);
if (masterSource == null) {
report.computeIfAbsent(SecDispatcher.ValidationResponse.Level.ERROR, k -> new ArrayList<>())
.add("Source configuration missing");
} else {
SecDispatcher.ValidationResponse masterSourceResponse = null;
for (MasterSource masterPasswordSource : sources.values()) {
masterSourceResponse = masterPasswordSource.validateConfiguration(masterSource);
if (masterSourceResponse != null) {
break;
}
}
if (masterSourceResponse == null) {
report.computeIfAbsent(SecDispatcher.ValidationResponse.Level.ERROR, k -> new ArrayList<>())
.add("Configured Source configuration not handled");
} else {
subsystems.add(masterSourceResponse);
if (!masterSourceResponse.isValid()) {
report.computeIfAbsent(SecDispatcher.ValidationResponse.Level.ERROR, k -> new ArrayList<>())
.add("Configured Source configuration invalid");
} else {
report.computeIfAbsent(SecDispatcher.ValidationResponse.Level.INFO, k -> new ArrayList<>())
.add("Configured Source configuration valid");
valid = true;
}
}
}
return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), valid, report, subsystems);
}

protected MasterSource getPasswordSource(Map<String, String> config) throws SecDispatcherException {
String masterSource = config.get(CONF_SOURCE);
if (masterSource == null) {
throw new SecDispatcherException("Invalid configuration: Missing configuration " + CONF_SOURCE);
}
MasterSource source = sources.get(masterSource);
if (source != null) {
return source;
}
throw new SecDispatcherException("No source found the given masterSource: " + masterSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
import org.codehaus.plexus.components.secdispatcher.internal.cipher.AESGCMNoPadding;
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.ForwardingDispatcher;
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.LegacyDispatcher;
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.MasterDispatcher;
import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterSource;
Expand Down Expand Up @@ -80,6 +81,18 @@ void masterWithSystemPropertyRoundTrip() throws Exception {
roundtrip();
}

@Test
void forwardingWithEnvDecrypt() throws Exception {
saveSec("forwarding", Map.of("source", "env"));
decryptForwarding("{[name=forwarding,version=something]env:MASTER_PASSWORD}", "masterPw");
}

@Test
void forwardingWithSystemPropertyDecrypt() throws Exception {
saveSec("forwarding", Map.of("source", "system-property"));
decryptForwarding("{[name=forwarding,version=something]system-property:masterPassword}", "masterPw");
}

@Test
void validate() throws Exception {
saveSec("master", Map.of("source", "system-property:masterPassword", "cipher", AESGCMNoPadding.CIPHER_ALG));
Expand Down Expand Up @@ -157,7 +170,7 @@ void detection() {
protected void roundtrip() throws Exception {
DefaultSecDispatcher sd = construct();

assertEquals(2, sd.availableDispatchers().size());
assertEquals(3, sd.availableDispatchers().size());
String encrypted = sd.encrypt("supersecret", Map.of(SecDispatcher.DISPATCHER_NAME_ATTR, "master", "a", "b"));
// example:
// {[name=master,cipher=AES/GCM/NoPadding,a=b]vvq66pZ7rkvzSPStGTI9q4QDnsmuDwo+LtjraRel2b0XpcGJFdXcYAHAS75HUA6GLpcVtEkmyQ==}
Expand All @@ -170,6 +183,14 @@ protected void roundtrip() throws Exception {
assertEquals("supersecret", pass);
}

protected void decryptForwarding(String encrypted, String decrypted) throws Exception {
DefaultSecDispatcher sd = construct();

assertEquals(3, sd.availableDispatchers().size());
String pass = sd.decrypt(encrypted);
assertEquals(decrypted, pass);
}

protected DefaultSecDispatcher construct() {
return new DefaultSecDispatcher(
Map.of(
Expand All @@ -184,7 +205,15 @@ protected DefaultSecDispatcher construct() {
GpgAgentMasterSource.NAME,
new GpgAgentMasterSource())),
"legacy",
new LegacyDispatcher()),
new LegacyDispatcher(),
"forwarding",
new ForwardingDispatcher(Map.of(
EnvMasterSource.NAME,
new EnvMasterSource(),
SystemPropertyMasterSource.NAME,
new SystemPropertyMasterSource(),
GpgAgentMasterSource.NAME,
new GpgAgentMasterSource()))),
CONFIG_PATH);
}

Expand Down
Loading