Skip to content

Commit

Permalink
Use ivy instead of maven repository for registries
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jan 2, 2024
1 parent 8895932 commit ccb136a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.sgtsilvio.gradle.oci.dsl
import org.gradle.api.Action
import org.gradle.api.Named
import org.gradle.api.NamedDomainObjectList
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
import org.gradle.api.credentials.PasswordCredentials
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
Expand All @@ -24,7 +24,7 @@ interface OciRegistry : Named {
val url: Property<URI>
val finalUrl: Provider<URI>
val credentials: Property<PasswordCredentials>
val repository: MavenArtifactRepository
val repository: IvyArtifactRepository

fun credentials()
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ internal abstract class OciRegistryImpl @Inject constructor(
final override val finalUrl: Provider<URI> =
providerFactory.gradleProperty(url.map(URI::toString)).map(::URI).orElse(url)
final override val credentials = objectFactory.property<PasswordCredentials>()
final override val repository = repositoryHandler.maven {
final override val repository = repositoryHandler.ivy {
name = this@OciRegistryImpl.name + "OciRegistry"
setUrl(finalUrl.zip(registries.repositoryPort) { url, repositoryPort ->
val urlBase64 = Base64.getUrlEncoder().encodeToString(url.toString().toByteArray())
URI("http://localhost:$repositoryPort/v2/repository/$urlBase64")
})
isAllowInsecureProtocol = true
layout("gradle")
metadataSources {
gradleMetadata()
artifact()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,64 +92,36 @@ internal class OciRepositoryHandler(
} catch (e: URISyntaxException) {
return response.sendBadRequest()
}
if (segments.last().endsWith(".module")) {
return handleModule(registryUri, segments, imageMappingData, credentials, isGET, response)
val componentId = VersionedCoordinates(segments[1], segments[2], segments[3])
val mappedComponent = imageMappingData.map(componentId)
if ((segments.size == 5) && segments[4].endsWith(".module")) {
return getOrHeadGradleModuleMetadata(registryUri, mappedComponent, credentials, isGET, response)
}
if (segments.size < 8) {
if (segments.size != 8) {
return response.sendNotFound()
}
return handleComponentOrLayer(registryUri, segments, imageMappingData, credentials, isGET, response)
}

private fun handleModule(
registryUri: URI,
segments: List<String>,
imageMappingData: OciImageMappingData,
credentials: Credentials?,
isGET: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
val componentId = decodeComponentId(segments, segments.lastIndex - 1)
val mappedComponent = imageMappingData.map(componentId)
return getOrHeadGradleModuleMetadata(registryUri, mappedComponent, credentials, isGET, response)
}

private fun handleComponentOrLayer(
registryUri: URI,
segments: List<String>,
imageMappingData: OciImageMappingData,
credentials: Credentials?,
isGET: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
val lastIndex = segments.lastIndex
val componentId = decodeComponentId(segments, lastIndex - 4)
val variantName = segments[lastIndex - 3]
val variantName = segments[4]
val digest = try {
segments[lastIndex - 2].toOciDigest()
segments[5].toOciDigest()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val size = try {
segments[lastIndex - 1].toLong()
segments[6].toLong()
} catch (e: NumberFormatException) {
return response.sendBadRequest()
}
val variant = imageMappingData.map(componentId).variants[variantName] ?: return response.sendNotFound()
val last = segments[lastIndex]
val variant = mappedComponent.variants[variantName] ?: return response.sendNotFound()
val last = segments[7]
return when {
last.endsWith("oci-component.json") -> getOrHeadComponent(registryUri, variant, digest, size.toInt(), credentials, isGET, response)
last.endsWith("oci-layer") -> getOrHeadLayer(registryUri, variant.imageReference.name, digest, size, credentials, isGET, response)
last.endsWith("oci-component.json") ->
getOrHeadComponent(registryUri, variant, digest, size.toInt(), credentials, isGET, response)
last.endsWith("oci-layer") ->
getOrHeadLayer(registryUri, variant.imageReference.name, digest, size, credentials, isGET, response)
else -> response.sendNotFound()
}
}

private fun decodeComponentId(segments: List<String>, versionIndex: Int) = VersionedCoordinates(
segments.subList(1, versionIndex - 1).joinToString("."),
segments[versionIndex - 1],
segments[versionIndex],
)

private fun getOrHeadGradleModuleMetadata(
registryUri: URI,
mappedComponent: MappedComponent,
Expand Down

0 comments on commit ccb136a

Please sign in to comment.