From 85460422e0797f266b4d4b88330d5606be608646 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sat, 15 Apr 2017 07:51:44 -0700 Subject: [PATCH] GH-414: Authenticated repos not working. Fixes https://github.com/cbeust/kobalt/issues/414 --- .../kotlin/com/beust/kobalt/api/Kobalt.kt | 4 ++ .../kotlin/com/beust/kobalt/maven/Kurl.kt | 25 +---------- .../maven/aether/KobaltMavenResolver.kt | 42 ++++++++++++++++++- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt index 527d35b2..f825efeb 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt @@ -5,6 +5,7 @@ import com.beust.kobalt.HostConfig import com.beust.kobalt.Plugins import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.maven.DependencyManager +import com.beust.kobalt.maven.aether.KobaltMavenResolver import com.google.inject.Guice import com.google.inject.Injector import com.google.inject.Module @@ -55,6 +56,9 @@ class Kobalt { // Repos from the build file result.addAll(reposFromBuildFiles) + result.forEach { + KobaltMavenResolver.initAuthentication(it) + } return result.toHashSet() } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt index 6b9c0a62..909c18c5 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt @@ -1,9 +1,8 @@ package com.beust.kobalt.maven import com.beust.kobalt.HostConfig -import com.beust.kobalt.KobaltException +import com.beust.kobalt.maven.aether.KobaltMavenResolver import com.beust.kobalt.maven.dependency.FileDependency -import com.beust.kobalt.misc.LocalProperties import java.io.* import java.net.HttpURLConnection import java.net.URL @@ -21,27 +20,7 @@ class Kurl(val hostInfo: HostConfig) { } init { - // See if the URL needs to be authenticated. Look in local.properties for keys - // of the format authUrl..user=xxx and authUrl..password=xxx - val properties = LocalProperties().localProperties - val host = java.net.URL(hostInfo.url).host - properties.entries.forEach { - val key = it.key.toString() - if (key == "$KEY.$host.$VALUE_USER") { - hostInfo.username = properties.getProperty(key) - } else if (key == "$KEY.$host.$VALUE_PASSWORD") { - hostInfo.password = properties.getProperty(key) - } - } - fun error(s1: String, s2: String) { - throw KobaltException("Found \"$s1\" but not \"$s2\" in local.properties for $KEY.$host", - docUrl = "http://beust.com/kobalt/documentation/index.html#maven-repos-authenticated") - } - if (! hostInfo.username.isNullOrBlank() && hostInfo.password.isNullOrBlank()) { - error("username", "password") - } else if(hostInfo.username.isNullOrBlank() && ! hostInfo.password.isNullOrBlank()) { - error("password", "username") - } + KobaltMavenResolver.initAuthentication(hostInfo) } override fun toString() = hostInfo.toString() diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt index 376081df..18ed6659 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/KobaltMavenResolver.kt @@ -2,11 +2,14 @@ package com.beust.kobalt.maven.aether import com.beust.kobalt.Args import com.beust.kobalt.HostConfig +import com.beust.kobalt.KobaltException import com.beust.kobalt.api.Kobalt import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.getProxy +import com.beust.kobalt.maven.Kurl import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.MavenId +import com.beust.kobalt.misc.LocalProperties import com.google.common.eventbus.EventBus import com.google.inject.Inject import org.eclipse.aether.artifact.Artifact @@ -21,6 +24,7 @@ import org.eclipse.aether.resolution.DependencyRequest import org.eclipse.aether.resolution.DependencyResult import org.eclipse.aether.resolution.VersionRangeRequest import org.eclipse.aether.resolution.VersionRangeResult +import org.eclipse.aether.util.repository.AuthenticationBuilder import java.util.* class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, @@ -32,6 +36,31 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, MavenId.toId(it.groupId, it.artifactId, it.extension, it.classifier, it.version) } fun isRangeVersion(id: String) = id.contains(",") + + fun initAuthentication(hostInfo: HostConfig) { + // See if the URL needs to be authenticated. Look in local.properties for keys + // of the format authUrl..user=xxx and authUrl..password=xxx + val properties = LocalProperties().localProperties + val host = java.net.URL(hostInfo.url).host + properties.entries.forEach { + val key = it.key.toString() + if (key == "${Kurl.KEY}.$host.${Kurl.VALUE_USER}") { + hostInfo.username = properties.getProperty(key) + } else if (key == "${Kurl.KEY}.$host.${Kurl.VALUE_PASSWORD}") { + hostInfo.password = properties.getProperty(key) + } + } + fun error(s1: String, s2: String) { + throw KobaltException("Found \"$s1\" but not \"$s2\" in local.properties for ${Kurl.KEY}.$host", + docUrl = "http://beust.com/kobalt/documentation/index.html#maven-repos-authenticated") + } + if (! hostInfo.username.isNullOrBlank() && hostInfo.password.isNullOrBlank()) { + error("username", "password") + } else if(hostInfo.username.isNullOrBlank() && ! hostInfo.password.isNullOrBlank()) { + error("password", "username") + } + + } } fun resolveToArtifact(id: String, scope: Scope? = null, @@ -110,8 +139,17 @@ class KobaltMavenResolver @Inject constructor(val settings: KobaltSettings, private val system = Booter.newRepositorySystem() private val session = Booter.newRepositorySystemSession(system, localRepo.localRepo, settings, eventBus) - private fun createRepo(hostConfig: HostConfig) = - RemoteRepository.Builder(hostConfig.name, "default", hostConfig.url).build() + private fun createRepo(hostConfig: HostConfig) : RemoteRepository { + val builder = RemoteRepository.Builder(hostConfig.name, "default", hostConfig.url) + if (hostConfig.hasAuth()) { + val auth = AuthenticationBuilder() + .addUsername(hostConfig.username) + .addPassword(hostConfig.password) + .build() + builder.setAuthentication(auth) + } + return builder.build() + } private val kobaltRepositories: List get() = Kobalt.repos.map {