Skip to content

Commit d87fa26

Browse files
meonlollemo7242
authored andcommitted
Add support for authentication in @file:MavenRepository() annotation.
The adding credentials with the following syntax is supported through this commit: @file:MavenRepository("id", "url", user="username", password="password") A descripting of how to test it is added under test/TestsReadme.md
1 parent 9ac99f1 commit d87fa26

File tree

7 files changed

+709
-11
lines changed

7 files changed

+709
-11
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
exclude("cglib", "cglib")
2121
exclude("org.kuali.maven.wagons", "maven-s3-wagon")
2222
}
23+
compile("com.jcabi:jcabi-aether:0.10.1:sources")
2324
compile("org.apache.maven:maven-core:3.0.3")
2425
compile("org.slf4j:slf4j-nop:1.7.25")
2526

src/main/kotlin/kscript/app/DependencyUtil.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kscript.app
33
import com.jcabi.aether.Aether
44
import org.sonatype.aether.RepositoryException
55
import org.sonatype.aether.artifact.Artifact
6+
import org.sonatype.aether.repository.Authentication
67
import org.sonatype.aether.repository.RemoteRepository
78
import org.sonatype.aether.util.artifact.DefaultArtifact
89
import org.sonatype.aether.util.artifact.JavaScopes.COMPILE
@@ -59,15 +60,22 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
5960
// Print the classpath
6061
return classPath
6162
} catch (e: RepositoryException) {
62-
errorMsg("Failed to lookup dependencies. Check dependency locators or file a bug on https://github.com/holgerbrandl/kscript")
63+
// Probably a wrapped Nullpointer from 'DefaultRepositorySystem.resolveDependencies()', this however is probably a connection problem.
64+
errorMsg("Failed while connecting to the server. Check the connection (http/https, port, proxy, credentials, etc.) of your maven dependency locators. If you suspect this is a bug, you can create an issue on https://github.com/holgerbrandl/kscript")
6365
errorMsg("Exception: $e")
6466
quit(1)
6567
}
6668
}
6769

6870
fun resolveDependenciesViaAether(depIds: List<String>, customRepos: List<MavenRepo>, loggingEnabled: Boolean): List<Artifact> {
6971
val jcenter = RemoteRepository("jcenter", "default", "http://jcenter.bintray.com/")
70-
val customRemoteRepos = customRepos.map { it -> RemoteRepository(it.id, "default", it.url) }
72+
val customRemoteRepos = customRepos.map { mavenRepo ->
73+
RemoteRepository(mavenRepo.id, "default", mavenRepo.url).apply {
74+
if (!mavenRepo.user.isNullOrEmpty() && !mavenRepo.password.isNullOrEmpty()) {
75+
authentication = Authentication(mavenRepo.user, mavenRepo.password)
76+
}
77+
}
78+
}
7179
val remoteRepos = customRemoteRepos + jcenter
7280

7381
val aether = Aether(remoteRepos, File(System.getProperty("user.home") + "/.m2/repository"))

src/main/kotlin/kscript/app/Script.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fun Script.collectDependencies(): List<String> {
129129

130130
// if annotations are used add dependency on kscript-annotations
131131
if (lines.any { isKscriptAnnotation(it) }) {
132-
dependencies += "com.github.holgerbrandl:kscript-annotations:1.2"
132+
dependencies += "com.github.holgerbrandl:kscript-annotations:1.3"
133133
}
134134

135135
return dependencies.distinct()
@@ -182,7 +182,7 @@ private fun isDependDeclare(line: String) =
182182
//
183183

184184

185-
data class MavenRepo(val id: String, val url: String)
185+
data class MavenRepo(val id: String, val url: String, val user: String = "", val password: String = "")
186186

187187
/**
188188
* Collect custom artifact repos declared with @file:MavenRepository
@@ -192,12 +192,16 @@ fun Script.collectRepos(): List<MavenRepo> {
192192
// only supported annotation format for now
193193

194194
// @file:MavenRepository("imagej", "http://maven.imagej.net/content/repositories/releases/")
195+
// @file:MavenRepository("imagej", "http://maven.imagej.net/content/repositories/releases/", user="user", password="pass")
195196
return lines
196197
.filter { it.contains(dependsOnMavenPrefix) }
197198
.map { it.replaceFirst(dependsOnMavenPrefix, "").substringBeforeLast(")") }
198-
.map { it.split(",").map { it.trim(' ', '"', '(') }.let { MavenRepo(it[0], it[1]) } }
199-
200-
// todo add credential support https://stackoverflow.com/questions/36282168/how-to-add-custom-maven-repository-to-gradle
199+
.map {
200+
it.split(",").map { it.trim(' ', '"', '(') }.let { annotationParams ->
201+
val namedArgs = annotationParams.filter { it.contains("=") }.map { item: String -> item.let { item.substringBefore("=") to item.substringAfter("=\"") } }.toMap()
202+
MavenRepo(annotationParams[0], annotationParams[1], namedArgs.getOrDefault("user", ""), namedArgs.getOrDefault("password", ""))
203+
}
204+
}
201205
}
202206

203207

src/test/kotlin/Tests.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Tests {
3535
val expected = listOf(
3636
"something:dev-1.1.0-alpha3(T2):1.2.14",
3737
"de.mpicbg.scicomp:kutils:0.7",
38-
"com.github.holgerbrandl:kscript-annotations:1.2"
38+
"com.github.holgerbrandl:kscript-annotations:1.3"
3939
)
4040

4141
Script(lines).collectDependencies() shouldBe expected
@@ -58,7 +58,7 @@ class Tests {
5858
"de.mpicbg.scicomp.joblist:joblist-kotlin:1.1",
5959
"de.mpicbg.scicomp:kutils:0.7",
6060
"log4j:log4j:1.2.14",
61-
"com.github.holgerbrandl:kscript-annotations:1.2"
61+
"com.github.holgerbrandl:kscript-annotations:1.3"
6262
)
6363

6464
Script(lines).collectDependencies() shouldBe expected
@@ -83,12 +83,39 @@ class Tests {
8383
collectDependencies() shouldBe listOf(
8484
"net.clearvolume:cleargl:2.0.1",
8585
"log4j:log4j:1.2.14",
86-
"com.github.holgerbrandl:kscript-annotations:1.2"
86+
"com.github.holgerbrandl:kscript-annotations:1.3"
8787
)
8888
}
8989

9090
}
9191

92+
@Test
93+
fun customRepoWithCreds() {
94+
val lines = listOf(
95+
"""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="user", password="pass") """,
96+
"""@file:MavenRepository("imagej-snapshots", "http://maven.imagej.net/content/repositories/snapshots", password="pass", user="user") """,
97+
"""@file:DependsOnMaven("net.clearvolume:cleargl:2.0.1")""",
98+
"""@file:DependsOn("log4j:log4j:1.2.14")""",
99+
"""println("foo")"""
100+
)
101+
102+
with(Script(lines)) {
103+
104+
collectRepos() shouldBe listOf(
105+
MavenRepo("imagej-releases", "http://maven.imagej.net/content/repositories/releases", "user", "pass"),
106+
MavenRepo("imagej-snapshots", "http://maven.imagej.net/content/repositories/snapshots", "user", "pass") //Provided with name in non-typical order
107+
)
108+
109+
collectDependencies() shouldBe listOf(
110+
"net.clearvolume:cleargl:2.0.1",
111+
"log4j:log4j:1.2.14",
112+
"com.github.holgerbrandl:kscript-annotations:1.3"
113+
)
114+
}
115+
116+
}
117+
118+
92119

93120
// combine kotlin opts spread over multiple lines
94121
@Test

test/TestsReadme.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,61 @@ which kscript
8181
kscript --version
8282
which resdeps.kts
8383
kscript --clear-cache
84-
kscript https://git.io/v1cG6 my argu ments
84+
kscript https://git.io/v1cG6 my argu ments
8585
8686
```
87+
88+
89+
## Manual testing
90+
91+
As of this writing, testing the credentials is only done manually with a dockerized Artifactory.
92+
93+
#### 1. Set up preconfigured artifactory with docker.
94+
95+
```
96+
# download and start artifactory container
97+
docker run --name artifactory -d -p 8081:8081 docker.bintray.io/jfrog/artifactory-oss:latest
98+
99+
# Copy preconfigured gloabl config (with custom repo) and security config (with credentials user) into container.
100+
docker cp ./test/resources/artifactory_config/artifactory.config.xml artifactory:/var/opt/jfrog/artifactory/etc/artifactory.config.import.xml
101+
docker cp ./test/resources/artifactory_config/security_descriptor.xml artifactory:/var/opt/jfrog/artifactory/etc/security.import.xml
102+
103+
# Make the configs accessable
104+
docker exec -u 0 -it artifactory sh -c 'chmod 777 $ARTIFACTORY_HOME/etc/*.import.xml'
105+
106+
# Restart docker after is done with initial booting (otherwise restart breaks the container).
107+
echo "sleeping for 15..." && sleep 15
108+
docker restart artifactory
109+
```
110+
111+
#### 2. Create and upload a downloadable archive.
112+
```
113+
tmpClass=$(mktemp --suffix ".class")
114+
tmpZipDir=$(mktemp -d)
115+
echo "public class something() {}" > $tmpClass
116+
zip $tmpZipDir/tmp.zip $tmpClass
117+
curl --request PUT -u admin:password -T $tmpZipDir/tmp.zip http://localhost:8081/artifactory/authenticated_repo/group/somejar/1.0/somejar-1.0.jar
118+
```
119+
120+
#### 3. Then run the following kotlin script with the encrypted password
121+
```
122+
echo '
123+
@file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password")
124+
@file:DependsOn("com.jcabi:jcabi-aether:0.10.1") # If unencrypted works via jcenter
125+
@file:DependsOnMaven("group:somejar:1.0") # If encrypted works.
126+
println("Hello, World!")
127+
' | kscript -
128+
```
129+
130+
### Additional info for manual testing
131+
132+
- Docker & container docu: https://www.jfrog.com/confluence/display/RTF/Installing+with+Docker
133+
- Loading configs docu: https://www.jfrog.com/confluence/display/RTF/Configuration+Files
134+
135+
```
136+
# get active security descriptor
137+
curl -u admin:password -X GET -H "Accept: application/xml" http://localhost:8081/artifactory/api/system/security > ./test/resources/artifactory_config/security_descriptor.xml
138+
139+
# Also works with encrypted password instead of plaintext.
140+
curl -u admin:password -X GET http://localhost:8081/artifactory/api/security/encryptedPassword
141+
```

0 commit comments

Comments
 (0)