mirror of
https://github.com/jlengrand/jreleaser.git
synced 2026-03-10 08:31:24 +00:00
[artifactory] Use same authorizaiton mechanism as http uploader. Resolves #174
This commit is contained in:
@@ -36,7 +36,7 @@ public class Artifactory extends AbstractUploader {
|
||||
private String target;
|
||||
private String username;
|
||||
private String password;
|
||||
private String token;
|
||||
private Authorization authorization;
|
||||
|
||||
public Artifactory() {
|
||||
super(NAME);
|
||||
@@ -47,7 +47,7 @@ public class Artifactory extends AbstractUploader {
|
||||
this.username = artifactory.username;
|
||||
this.password = artifactory.password;
|
||||
this.target = artifactory.target;
|
||||
this.token = artifactory.token;
|
||||
this.authorization = artifactory.authorization;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,6 +55,14 @@ public class Artifactory extends AbstractUploader {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
public Authorization resolveAuthorization() {
|
||||
if (null == authorization) {
|
||||
authorization = Authorization.BEARER;
|
||||
}
|
||||
|
||||
return authorization;
|
||||
}
|
||||
|
||||
public String getResolvedTarget(JReleaserContext context) {
|
||||
Map<String, Object> props = context.props();
|
||||
props.putAll(getResolvedExtraProperties());
|
||||
@@ -69,10 +77,6 @@ public class Artifactory extends AbstractUploader {
|
||||
return Env.resolve("ARTIFACTORY_" + Env.toVar(name) + "_PASSWORD", password);
|
||||
}
|
||||
|
||||
public String getResolvedToken() {
|
||||
return Env.resolve("ARTIFACTORY_" + Env.toVar(name) + "_TOKEN", token);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
@@ -97,19 +101,23 @@ public class Artifactory extends AbstractUploader {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
public Authorization getAuthorization() {
|
||||
return authorization;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
public void setAuthorization(Authorization authorization) {
|
||||
this.authorization = authorization;
|
||||
}
|
||||
|
||||
public void setAuthorization(String authorization) {
|
||||
this.authorization = Authorization.of(authorization);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void asMap(Map<String, Object> props, boolean full) {
|
||||
props.put("target", target);
|
||||
props.put("authorization", authorization);
|
||||
props.put("username", isNotBlank(getResolvedUsername()) ? HIDE : UNSET);
|
||||
props.put("password", isNotBlank(getResolvedPassword()) ? HIDE : UNSET);
|
||||
props.put("token", isNotBlank(getResolvedToken()) ? HIDE : UNSET);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.util.Map;
|
||||
import static org.jreleaser.util.Constants.HIDE;
|
||||
import static org.jreleaser.util.Constants.UNSET;
|
||||
import static org.jreleaser.util.MustacheUtils.applyTemplate;
|
||||
import static org.jreleaser.util.StringUtils.isBlank;
|
||||
import static org.jreleaser.util.StringUtils.isNotBlank;
|
||||
|
||||
/**
|
||||
@@ -152,20 +151,4 @@ public class HttpUploader extends AbstractUploader {
|
||||
props.put("password", isNotBlank(getResolvedPassword()) ? HIDE : UNSET);
|
||||
props.put("headers", headers);
|
||||
}
|
||||
|
||||
public enum Authorization {
|
||||
NONE,
|
||||
BASIC,
|
||||
BEARER;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
public static Authorization of(String str) {
|
||||
if (isBlank(str)) return null;
|
||||
return Authorization.valueOf(str.toUpperCase().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public interface Uploader extends Domain, Activatable, TimeoutAware, ExtraProper
|
||||
}
|
||||
}
|
||||
|
||||
public enum Authorization {
|
||||
enum Authorization {
|
||||
NONE,
|
||||
BASIC,
|
||||
BEARER;
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.jreleaser.util.Errors;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jreleaser.util.StringUtils.isBlank;
|
||||
import static org.jreleaser.util.StringUtils.isNotBlank;
|
||||
|
||||
/**
|
||||
* @author Andres Almiray
|
||||
@@ -56,36 +55,37 @@ public abstract class ArtifactoryValidator extends Validator {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isBlank(artifactory.getName())) {
|
||||
errors.configuration("artifactory.name must not be blank");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isBlank(artifactory.getTarget())) {
|
||||
errors.configuration("artifactory.target must not be blank.");
|
||||
errors.configuration("artifactory." + artifactory.getName() + ".target must not be blank.");
|
||||
}
|
||||
|
||||
artifactory.setUsername(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_USERNAME",
|
||||
"artifactory.username",
|
||||
artifactory.getUsername(),
|
||||
new Errors()));
|
||||
switch (artifactory.resolveAuthorization()) {
|
||||
case BEARER:
|
||||
artifactory.setPassword(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_PASSWORD",
|
||||
"artifactory.password",
|
||||
artifactory.getPassword(),
|
||||
errors));
|
||||
break;
|
||||
case BASIC:
|
||||
artifactory.setUsername(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_USERNAME",
|
||||
"artifactory.username",
|
||||
artifactory.getUsername(),
|
||||
new Errors()));
|
||||
|
||||
if (isNotBlank(artifactory.getResolvedUsername())) {
|
||||
artifactory.setPassword(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_PASSWORD",
|
||||
"artifactory.password",
|
||||
artifactory.getPassword(),
|
||||
errors));
|
||||
} else {
|
||||
artifactory.setToken(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_TOKEN",
|
||||
"artifactory.token",
|
||||
artifactory.getToken(),
|
||||
errors));
|
||||
artifactory.setPassword(
|
||||
checkProperty(context.getModel().getEnvironment(),
|
||||
"ARTIFACTORY_" + Env.toVar(artifactory.getName()) + "_PASSWORD",
|
||||
"artifactory.password",
|
||||
artifactory.getPassword(),
|
||||
errors));
|
||||
break;
|
||||
case NONE:
|
||||
errors.configuration("artifactory." + artifactory.getName() + ".authorization can not be NONE");
|
||||
break;
|
||||
}
|
||||
|
||||
if (artifactory.getConnectTimeout() <= 0 || artifactory.getConnectTimeout() > 300) {
|
||||
|
||||
@@ -59,13 +59,8 @@ public abstract class HttpUploaderValidator extends Validator {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isBlank(http.getName())) {
|
||||
errors.configuration("http.name must not be blank");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isBlank(http.getTarget())) {
|
||||
errors.configuration("http.target must not be blank.");
|
||||
errors.configuration("http." + http.getName() + ".target must not be blank.");
|
||||
}
|
||||
|
||||
if (null == http.getMethod()) {
|
||||
|
||||
@@ -33,5 +33,7 @@ interface Artifactory extends Uploader {
|
||||
|
||||
Property<String> getPassword()
|
||||
|
||||
Property<String> getToken()
|
||||
Property<org.jreleaser.model.Uploader.Authorization> getAuthorization()
|
||||
|
||||
void setAuthorization(String authorization)
|
||||
}
|
||||
@@ -38,7 +38,11 @@ interface Http extends Uploader {
|
||||
|
||||
Property<org.jreleaser.model.Uploader.Authorization> getAuthorization()
|
||||
|
||||
MapProperty<String,String> getHeaders()
|
||||
MapProperty<String, String> getHeaders()
|
||||
|
||||
void setHeader(String key, String value)
|
||||
|
||||
void setAuthorization(String authorization)
|
||||
|
||||
void setMethod(String method)
|
||||
}
|
||||
@@ -18,12 +18,12 @@
|
||||
package org.jreleaser.gradle.plugin.internal.dsl
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.internal.provider.Providers
|
||||
import org.gradle.api.model.ObjectFactory
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.tasks.Internal
|
||||
import org.jreleaser.gradle.plugin.dsl.Artifactory
|
||||
import org.jreleaser.model.Uploader
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -38,7 +38,7 @@ class ArtifactoryImpl extends AbstractUploader implements Artifactory {
|
||||
final Property<String> target
|
||||
final Property<String> username
|
||||
final Property<String> password
|
||||
final Property<String> token
|
||||
final Property<Uploader.Authorization> authorization
|
||||
|
||||
@Inject
|
||||
ArtifactoryImpl(ObjectFactory objects) {
|
||||
@@ -46,7 +46,7 @@ class ArtifactoryImpl extends AbstractUploader implements Artifactory {
|
||||
target = objects.property(String).convention(Providers.notDefined())
|
||||
username = objects.property(String).convention(Providers.notDefined())
|
||||
password = objects.property(String).convention(Providers.notDefined())
|
||||
token = objects.property(String).convention(Providers.notDefined())
|
||||
authorization = objects.property(Uploader.Authorization).convention(Providers.notDefined())
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,7 +56,12 @@ class ArtifactoryImpl extends AbstractUploader implements Artifactory {
|
||||
target.present ||
|
||||
username.present ||
|
||||
password.present ||
|
||||
token.present
|
||||
authorization.present
|
||||
}
|
||||
|
||||
@Override
|
||||
void setAuthorization(String authorization) {
|
||||
this.authorization.set(Uploader.Authorization.of(authorization))
|
||||
}
|
||||
|
||||
org.jreleaser.model.Artifactory toModel() {
|
||||
@@ -66,7 +71,7 @@ class ArtifactoryImpl extends AbstractUploader implements Artifactory {
|
||||
if (target.present) artifactory.target = target.get()
|
||||
if (username.present) artifactory.username = username.get()
|
||||
if (password.present) artifactory.password = password.get()
|
||||
if (token.present) artifactory.token = token.get()
|
||||
if (authorization.present) artifactory.authorization = authorization.get()
|
||||
artifactory
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,16 @@ class HttpImpl extends AbstractUploader implements Http {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void setAuthorization(String authorization) {
|
||||
this.authorization.set(Uploader.Authorization.of(authorization))
|
||||
}
|
||||
|
||||
@Override
|
||||
void setMethod(String method) {
|
||||
this.method.set(Uploader.Method.of(method))
|
||||
}
|
||||
|
||||
org.jreleaser.model.HttpUploader toModel() {
|
||||
org.jreleaser.model.HttpUploader http = new org.jreleaser.model.HttpUploader()
|
||||
http.name = name
|
||||
|
||||
@@ -27,7 +27,7 @@ public class Artifactory extends AbstractUploader {
|
||||
private String target;
|
||||
private String username;
|
||||
private String password;
|
||||
private String token;
|
||||
private Authorization authorization;
|
||||
|
||||
public Artifactory() {
|
||||
super(NAME);
|
||||
@@ -38,7 +38,15 @@ public class Artifactory extends AbstractUploader {
|
||||
this.target = artifactory.target;
|
||||
this.username = artifactory.username;
|
||||
this.password = artifactory.password;
|
||||
this.token = artifactory.token;
|
||||
this.authorization = artifactory.authorization;
|
||||
}
|
||||
|
||||
public Authorization resolveAuthorization() {
|
||||
if (null == authorization) {
|
||||
authorization = Authorization.NONE;
|
||||
}
|
||||
|
||||
return authorization;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
@@ -65,12 +73,11 @@ public class Artifactory extends AbstractUploader {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
public Authorization getAuthorization() {
|
||||
return authorization;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
public void setAuthorization(Authorization authorization) {
|
||||
this.authorization = authorization;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -320,7 +320,7 @@ public final class JReleaserModelConverter {
|
||||
a.setTarget(artifactory.getTarget());
|
||||
a.setUsername(artifactory.getUsername());
|
||||
a.setPassword(artifactory.getPassword());
|
||||
a.setToken(artifactory.getToken());
|
||||
a.setAuthorization(artifactory.resolveAuthorization().name());
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,6 @@ public class ArtifactoryArtifactUploader extends AbstractArtifactUploader<Artifa
|
||||
String target = uploader.getResolvedTarget(context);
|
||||
String username = uploader.getResolvedUsername();
|
||||
String password = uploader.getResolvedPassword();
|
||||
String token = uploader.getResolvedToken();
|
||||
|
||||
for (Path path : paths) {
|
||||
context.getLogger().info(" - {}", path.getFileName());
|
||||
@@ -81,14 +80,18 @@ public class ArtifactoryArtifactUploader extends AbstractArtifactUploader<Artifa
|
||||
FormData data = ClientUtils.toFormData(path);
|
||||
|
||||
Map<String, String> headers = new LinkedHashMap<>();
|
||||
if (isNotBlank(username)) {
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
|
||||
auth = new String(encodedAuth);
|
||||
headers.put("Authorization", "Basic " + auth);
|
||||
} else {
|
||||
headers.put("Authorization", "Bearer " + token);
|
||||
switch (uploader.resolveAuthorization()) {
|
||||
case BASIC:
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
|
||||
auth = new String(encodedAuth);
|
||||
headers.put("Authorization", "Basic " + auth);
|
||||
break;
|
||||
case BEARER:
|
||||
headers.put("Authorization", "Bearer " + password);
|
||||
break;
|
||||
}
|
||||
|
||||
headers.put("X-Checksum-Deploy", "false");
|
||||
headers.put("X-Checksum-Sha1", ChecksumUtils.checksum(Algorithm.SHA_1, data.getData()));
|
||||
headers.put("X-Checksum-Sha256", ChecksumUtils.checksum(Algorithm.SHA_256, data.getData()));
|
||||
|
||||
Reference in New Issue
Block a user