Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@
*/
package org.apache.maven.plugins.scripting;

import javax.inject.Inject;
import javax.script.Bindings;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

import java.io.File;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.scripting.binding.Servers;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.crypto.SettingsDecrypter;

/**
* Evaluate the specified script or scriptFile.
Expand Down Expand Up @@ -64,14 +69,25 @@ public class EvalMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

@Parameter(defaultValue = "${settings}", readonly = true)
private Settings settings;

@Inject
private SettingsDecrypter settingsDecrypter;

@Inject
private MavenSession session;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
AbstractScriptEvaluator execute = constructExecute();

Bindings bindings = new SimpleBindings();
bindings.put("session", session);
bindings.put("project", project);
bindings.put("log", getLog());
bindings.put("servers", new Servers(settings, settingsDecrypter));

Object result = execute.eval(bindings, getLog());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.apache.maven.plugins.scripting.binding;

import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;

import static java.util.stream.Collectors.joining;

/**
* Binding which enables to work with servers (from settings.xml) and in particular decipher them transparently.
*/
public class Servers {
private final Settings settings;
private final SettingsDecrypter settingsDecrypter;

public Servers(Settings settings, SettingsDecrypter settingsDecrypter) {
this.settings = settings;
this.settingsDecrypter = settingsDecrypter;
}

public Server find(String id) {
final Server server = settings.getServer(id);
if (server == null) {
return null;
}
final SettingsDecryptionResult result = settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(server));
if (!result.getProblems().isEmpty()) {
throw new IllegalStateException(
result.getProblems().stream().map(SettingsProblem::toString).collect(joining("\n")));
}
final Server decrypted = result.getServer();
return decrypted == null ? server : decrypted;
}
}
5 changes: 4 additions & 1 deletion src/site/markdown/script-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ under the License.
The following variables are available in the script context

* `org.apache.maven.project.MavenProject project`
* `org.apache.maven.plugin.logging.Log log`
* `org.apache.maven.plugin.logging.Log log`
* `org.apache.maven.plugin.logging.Log log`
* `org.apache.maven.execution.MavenSession session`
* `org.apache.maven.plugins.scripting.binding.Servers servers`: enables to access calling `servers.find(<id>)` a deciphered Server instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.apache.maven.plugins.scripting.binding;

import java.util.List;

import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.codehaus.plexus.PlexusContainerException;
import org.junit.jupiter.api.Test;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why are we using org.sonatype instead of org.codehaus.plexus?

import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

// sanity check, decrypter impl is mocked for simplicity
public class ServersTest {
@Test
public void missing() throws Exception {
assertNull(new Servers(
new Settings(),
new DefaultSettingsDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())))
.find("test"));
}

@Test
public void clear() throws PlexusContainerException {
final Server server = new Server();
server.setId("test");
server.setUsername("clear");
server.setPassword("12345");
assertEquals(
"12345",
new Servers(
new Settings() {
@Override
public List<Server> getServers() {
return singletonList(server);
}
},
new DefaultSettingsDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())) {
@Override
public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
return new SettingsDecryptionResult() {
@Override
public Server getServer() {
return null;
}

@Override
public List<SettingsProblem> getProblems() {
return emptyList();
}

@Override
public List<Server> getServers() {
throw new UnsupportedOperationException();
}

@Override
public Proxy getProxy() {
throw new UnsupportedOperationException();
}

@Override
public List<Proxy> getProxies() {
throw new UnsupportedOperationException();
}
};
}
})
.find("test")
.getPassword());
}

@Test
public void ciphered() throws PlexusContainerException {
final Server server = new Server();
server.setId("test");
server.setUsername("not-clear");
server.setPassword("ciphered");
assertEquals(
"12345",
new Servers(
new Settings() {
@Override
public List<Server> getServers() {
return singletonList(server);
}
},
new DefaultSettingsDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())) {
@Override
public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
return new SettingsDecryptionResult() {
@Override
public Server getServer() {
final Server result = new Server();
result.setId(server.getId());
result.setUsername(server.getUsername());
result.setPassword("12345");
return result;
}

@Override
public List<SettingsProblem> getProblems() {
return emptyList();
}

@Override
public List<Server> getServers() {
throw new UnsupportedOperationException();
}

@Override
public Proxy getProxy() {
throw new UnsupportedOperationException();
}

@Override
public List<Proxy> getProxies() {
throw new UnsupportedOperationException();
}
};
}
})
.find("test")
.getPassword());
}
}