Skip to content
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

Fix: Refactored configurations #291

Merged
merged 1 commit into from
Jul 16, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Usage:
* Fix #246: Dockerfile custom interpolation is broken
* Fix #259: Cleanup unused properties inside Mojos
* Fix #94: Properly define + document JKubeProjectAssembly behavior
* Fix #248: Properly name and document (Maven/System) configuration properties

### 1.0.0-alpha-4 (2020-06-08)
* Fix #173: Use OpenShift compliant git/vcs annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

/**
* @author roland
* @since 21.10.18
*/
public class FromConfigRegistryAuthHandler implements RegistryAuthHandler {
private final RegistryAuthConfig registryAuthConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

/**
* @author roland
* @since 21.10.18
*/
public class OpenShiftRegistryAuthHandler implements RegistryAuthHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

/**
* @author roland
* @since 21.10.18
*/
public class SystemPropertyRegistryAuthHandler implements RegistryAuthHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

/**
* @author roland
* @since 23.10.18
*/
public class SystemPropertyRegistryAuthHandlerTest {

Expand All @@ -49,7 +48,7 @@ public class SystemPropertyRegistryAuthHandlerTest {
public void setup() {
RegistryAuthConfig registryAuthConfig = RegistryAuthConfig.builder()
.skipExtendedAuthentication(false)
.propertyPrefix("docker")
.propertyPrefix("jkube.docker")
.build();
handler = new SystemPropertyRegistryAuthHandler(registryAuthConfig, log);
}
Expand All @@ -70,16 +69,16 @@ public void testEmpty() throws Exception {

@Test
public void testSystemProperty() throws Exception {
System.setProperty("docker.push.username", "roland");
System.setProperty("docker.push.password", "secret");
System.setProperty("docker.push.email", "roland@jolokia.org");
System.setProperty("jkube.docker.push.username", "roland");
System.setProperty("jkube.docker.push.password", "secret");
System.setProperty("jkube.docker.push.email", "roland@jolokia.org");
try {
AuthConfig config = handler.create(RegistryAuthConfig.Kind.PUSH, null, null, s -> s);
verifyAuthConfig(config,"roland","secret","roland@jolokia.org");
} finally {
System.clearProperty("docker.push.username");
System.clearProperty("docker.push.password");
System.clearProperty("docker.push.email");
System.clearProperty("jkube.docker.push.username");
System.clearProperty("jkube.docker.push.password");
System.clearProperty("jkube.docker.push.email");
}
}

Expand All @@ -88,7 +87,7 @@ public void testSystemProperty() throws Exception {
public void testSystemPropertyNoPassword() throws IOException {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("No password provided for username secret");
checkException("docker.username");
checkException("jkube.docker.username");
}

private void checkException(String key) throws IOException {
Expand Down
8 changes: 6 additions & 2 deletions jkube-kit/build/service/docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-common-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.net.UrlEscapers;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.RegistryServerConfiguration;
import org.eclipse.jkube.kit.build.api.helper.DockerFileUtil;
import org.eclipse.jkube.kit.build.api.auth.AuthConfig;
Expand Down Expand Up @@ -53,13 +54,13 @@ public class AuthConfigFactory {

// Properties for specifying username, password (can be encrypted), email and authtoken (not used yet)
// + whether to check for OpenShift authentication
public static final String AUTH_USERNAME = "username";
public static final String AUTH_PASSWORD = "password";
public static final String AUTH_EMAIL = "email";
public static final String AUTH_AUTHTOKEN = "authToken";
protected static final String AUTH_USE_OPENSHIFT_AUTH = "useOpenShiftAuth";
private static final String AUTH_USERNAME = "username";
private static final String AUTH_PASSWORD = "password";
private static final String AUTH_EMAIL = "email";
private static final String AUTH_AUTHTOKEN = "authToken";
private static final String AUTH_USE_OPENSHIFT_AUTH = "useOpenShiftAuth";

static final String DOCKER_LOGIN_DEFAULT_REGISTRY = "https://index.docker.io/v1/";
private static final String DOCKER_LOGIN_DEFAULT_REGISTRY = "https://index.docker.io/v1/";

private final KitLogger log;
private static final String[] DEFAULT_REGISTRIES = new String[]{
Expand Down Expand Up @@ -190,7 +191,7 @@ private static AuthConfig createStandardAuthConfig(boolean isPush, Map authConfi

// Check first for specific configuration based on direction (pull or push), then for a default value
for (LookupMode lookupMode : new LookupMode[] { getLookupMode(isPush), LookupMode.DEFAULT }) {
// System properties docker.username and docker.password always take precedence
// System properties jkube.docker.username and jkube.docker.password always take precedence
ret = getAuthConfigFromSystemProperties(lookupMode, passwordDecryptionMethod);
if (ret != null) {
log.debug("AuthConfig: credentials from system properties");
Expand Down Expand Up @@ -303,30 +304,28 @@ private static AuthConfig getAuthConfigFromEC2InstanceRole(KitLogger log) throws
}

protected static AuthConfig getAuthConfigFromSystemProperties(LookupMode lookupMode, UnaryOperator<String> passwordDecryptionMethod) throws IOException {
Properties props = System.getProperties();
String userKey = lookupMode.asSysProperty(AUTH_USERNAME);
String passwordKey = lookupMode.asSysProperty(AUTH_PASSWORD);
if (props.containsKey(userKey)) {
if (!props.containsKey(passwordKey)) {
throw new IOException("No " + passwordKey + " provided for username " + props.getProperty(userKey));
}
return new AuthConfig(props.getProperty(userKey),
passwordDecryptionMethod.apply(props.getProperty(passwordKey)),
props.getProperty(lookupMode.asSysProperty(AUTH_EMAIL)),
props.getProperty(lookupMode.asSysProperty(AUTH_AUTHTOKEN)));
} else {
return null;
final String passwordKey = lookupMode.asSysProperty(AUTH_PASSWORD);
final String username = System.getProperty(lookupMode.asSysProperty(AUTH_USERNAME));
final String password = System.getProperty(passwordKey);
if (StringUtils.isNotBlank(username) && StringUtils.isBlank(password)) {
throw new IOException("No " + passwordKey + " provided for username " + username);
} else if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
return new AuthConfig(username,
passwordDecryptionMethod.apply(password),
System.getProperty(lookupMode.asSysProperty(AUTH_EMAIL)),
System.getProperty(lookupMode.asSysProperty(AUTH_AUTHTOKEN)));
}
return null;
}

protected static AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map authConfigMap) {
Properties props = System.getProperties();
String useOpenAuthModeProp = lookupMode.asSysProperty(AUTH_USE_OPENSHIFT_AUTH);
final String useOpenAuthModeKey = lookupMode.asSysProperty(AUTH_USE_OPENSHIFT_AUTH);
final String useOpenAuthMode = System.getProperty(useOpenAuthModeKey);
// Check for system property
if (props.containsKey(useOpenAuthModeProp)) {
boolean useOpenShift = Boolean.parseBoolean(props.getProperty(useOpenAuthModeProp));
if (StringUtils.isNotBlank(useOpenAuthMode)) {
boolean useOpenShift = Boolean.parseBoolean(useOpenAuthMode);
if (useOpenShift) {
return validateMandatoryOpenShiftLogin(parseOpenShiftConfig(), useOpenAuthModeProp);
return validateMandatoryOpenShiftLogin(parseOpenShiftConfig(), useOpenAuthModeKey);
} else {
return null;
}
Expand All @@ -336,7 +335,7 @@ protected static AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMo
Map mapToCheck = getAuthConfigMapToCheck(lookupMode,authConfigMap);
if (mapToCheck != null && mapToCheck.containsKey(AUTH_USE_OPENSHIFT_AUTH) &&
Boolean.parseBoolean((String) mapToCheck.get(AUTH_USE_OPENSHIFT_AUTH))) {
return validateMandatoryOpenShiftLogin(parseOpenShiftConfig(), useOpenAuthModeProp);
return validateMandatoryOpenShiftLogin(parseOpenShiftConfig(), useOpenAuthModeKey);
} else {
return null;
}
Expand Down Expand Up @@ -554,9 +553,9 @@ private static LookupMode getLookupMode(boolean isPush) {
}

protected enum LookupMode {
PUSH("docker.push.","push"),
PULL("docker.pull.","pull"),
DEFAULT("docker.",null);
PUSH("jkube.docker.push.","push"),
PULL("jkube.docker.pull.","pull"),
DEFAULT("jkube.docker.",null);

private final String sysPropPrefix;
private String configMapKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private Map<String, FormatParameterReplacer.Lookup> initLookups(final JavaProjec

// ==============================================================================================

public static abstract class AbstractLookup implements FormatParameterReplacer.Lookup {
public abstract static class AbstractLookup implements FormatParameterReplacer.Lookup {
protected final JavaProject project;

private AbstractLookup(JavaProject project) {
Expand All @@ -90,7 +90,7 @@ private static class DefaultUserLookup extends AbstractLookup {
* Property to lookup for image user which overwrites the calculated default (group).
* Used with format modifier %g
*/
private static final String DOCKER_IMAGE_USER = "docker.image.user";
private static final String DOCKER_IMAGE_USER = "jkube.image.user";

private DefaultUserLookup(JavaProject project) {
super(project);
Expand Down Expand Up @@ -129,7 +129,7 @@ private static class DefaultTagLookup extends AbstractLookup {
* on the project version and depends whether it is a snapshot project or not.
* Used with format modifier %v
*/
private static final String DOCKER_IMAGE_TAG = "docker.image.tag";
private static final String DOCKER_IMAGE_TAG = "jkube.image.tag";

// how to resolve the version
private final Mode mode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void waitAndPostExec(String containerId, Properties projProperties) thro
}

boolean showLogs() {
if (showLogs != null) {
if (StringUtils.isNotBlank(showLogs)) {
if (showLogs.equalsIgnoreCase("true")) {
return true;
} else if (showLogs.equalsIgnoreCase("false")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jkube.kit.build.api.helper.DockerFileUtil;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.RegistryServerConfiguration;
import org.eclipse.jkube.kit.common.SystemMock;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -37,22 +38,19 @@ public class AuthConfigFactoryTest {
@Test
public void testGetAuthConfigFromSystemProperties() throws IOException {
// Given
AuthConfigFactory.LookupMode lookupMode = AuthConfigFactory.LookupMode.DEFAULT;
System.setProperty(lookupMode.asSysProperty(AuthConfigFactory.AUTH_USERNAME), "testuser");
System.setProperty(lookupMode.asSysProperty(AuthConfigFactory.AUTH_PASSWORD), "testpass");

new SystemMock()
.put("jkube.docker.username", "testuser")
.put("jkube.docker.password", "testpass");
// When
AuthConfig authConfig = AuthConfigFactory.getAuthConfigFromSystemProperties(AuthConfigFactory.LookupMode.DEFAULT, s -> s);

// Then
assertAuthConfig(authConfig, "testuser", "testpass");
}

@Test
public void testGetAuthConfigFromOpenShiftConfig() {
// Given
AuthConfigFactory.LookupMode lookupMode = AuthConfigFactory.LookupMode.DEFAULT;
System.setProperty(lookupMode.asSysProperty(AuthConfigFactory.AUTH_USE_OPENSHIFT_AUTH), "true");
new SystemMock().put("jkube.docker.useOpenShiftAuth", "true");
Map<String, Object> authConfigMap = new HashMap<>();
new MockUp<DockerFileUtil>() {
@Mock
Expand All @@ -73,10 +71,8 @@ public void testGetAuthConfigFromOpenShiftConfig() {
return kubeConfig;
}
};

// When
AuthConfig authConfig = AuthConfigFactory.getAuthConfigFromOpenShiftConfig(AuthConfigFactory.LookupMode.DEFAULT, authConfigMap);

// Then
assertAuthConfig(authConfig, "test", "sometoken");
}
Expand All @@ -85,7 +81,7 @@ public void testGetAuthConfigFromOpenShiftConfig() {
public void testGetAuthConfigFromOpenShiftConfigWithAuthConfigMap() {
// Given
Map<String, Object> authConfigMap = new HashMap<>();
authConfigMap.put(AuthConfigFactory.AUTH_USE_OPENSHIFT_AUTH, "true");
authConfigMap.put("useOpenShiftAuth", "true");
new MockUp<DockerFileUtil>() {
@Mock
Map<String, ?> readKubeConfig() {
Expand Down Expand Up @@ -117,9 +113,9 @@ public void testGetAuthConfigFromOpenShiftConfigWithAuthConfigMap() {
public void testGetAuthConfigFromPluginConfiguration() {
// Given
Map<String, Object> authConfigMap = new HashMap<>();
authConfigMap.put(AuthConfigFactory.AUTH_USERNAME, "testuser");
authConfigMap.put(AuthConfigFactory.AUTH_PASSWORD, "testpass");
authConfigMap.put(AuthConfigFactory.AUTH_EMAIL, "test@example.com");
authConfigMap.put("username", "testuser");
authConfigMap.put("password", "testpass");
authConfigMap.put("email", "test@example.com");

// When
AuthConfig authConfig = AuthConfigFactory.getAuthConfigFromPluginConfiguration(AuthConfigFactory.LookupMode.DEFAULT, authConfigMap, s -> s);
Expand Down Expand Up @@ -173,14 +169,12 @@ JsonObject readDockerConfig() {
@Test
public void testGetStandardAuthConfigFromProperties(@Mocked KitLogger logger) throws IOException {
// Given
AuthConfigFactory.LookupMode lookupMode = AuthConfigFactory.LookupMode.DEFAULT;
System.setProperty(lookupMode.asSysProperty(AuthConfigFactory.AUTH_USERNAME), "testuser");
System.setProperty(lookupMode.asSysProperty(AuthConfigFactory.AUTH_PASSWORD), "testpass");

new SystemMock()
.put("jkube.docker.username", "testuser")
.put("jkube.docker.password", "testpass");
// When
AuthConfigFactory authConfigFactory = new AuthConfigFactory(logger);
AuthConfig authConfig = authConfigFactory.createAuthConfig(true, true, Collections.emptyMap(), Collections.emptyList(), "testuser", "testregistry.io", s -> s);

// Then
assertAuthConfig(authConfig, "testuser", "testpass");
}
Expand Down
Loading