added dedicated cli module
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,3 +3,5 @@
|
|||||||
|
|
||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
|
|
||||||
|
gbcs-cli/native-image/*.json
|
||||||
|
@@ -9,11 +9,11 @@ WORKDIR /home/ubuntu
|
|||||||
RUN mkdir gbcs
|
RUN mkdir gbcs
|
||||||
WORKDIR /home/ubuntu/gbcs
|
WORKDIR /home/ubuntu/gbcs
|
||||||
|
|
||||||
COPY --chown=ubuntu:users native-image native-image
|
|
||||||
COPY --chown=ubuntu:users .git .git
|
COPY --chown=ubuntu:users .git .git
|
||||||
COPY --chown=ubuntu:users gbcs-base gbcs-base
|
COPY --chown=ubuntu:users gbcs-base gbcs-base
|
||||||
COPY --chown=ubuntu:users gbcs-api gbcs-api
|
COPY --chown=ubuntu:users gbcs-api gbcs-api
|
||||||
COPY --chown=ubuntu:users gbcs-memcached gbcs-memcached
|
COPY --chown=ubuntu:users gbcs-memcached gbcs-memcached
|
||||||
|
COPY --chown=ubuntu:users gbcs-cli gbcs-cli
|
||||||
COPY --chown=ubuntu:users src src
|
COPY --chown=ubuntu:users src src
|
||||||
COPY --chown=ubuntu:users settings.gradle settings.gradle
|
COPY --chown=ubuntu:users settings.gradle settings.gradle
|
||||||
COPY --chown=ubuntu:users build.gradle build.gradle
|
COPY --chown=ubuntu:users build.gradle build.gradle
|
||||||
@@ -31,11 +31,11 @@ USER luser
|
|||||||
WORKDIR /home/luser
|
WORKDIR /home/luser
|
||||||
|
|
||||||
FROM base-release AS release
|
FROM base-release AS release
|
||||||
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/build,target=/home/luser/build cp build/libs/gbcs-envelope*.jar gbcs.jar
|
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/gbcs-cli/build,target=/home/luser/build cp build/libs/gbcs-cli-envelope*.jar gbcs.jar
|
||||||
ENTRYPOINT ["java", "-jar", "/home/luser/gbcs.jar"]
|
ENTRYPOINT ["java", "-jar", "/home/luser/gbcs.jar"]
|
||||||
|
|
||||||
FROM base-release AS release-memcached
|
FROM base-release AS release-memcached
|
||||||
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/build,target=/home/luser/build cp build/libs/gbcs-envelope*.jar gbcs.jar
|
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/gbcs-cli/build,target=/home/luser/build cp build/libs/gbcs-cli-envelope*.jar gbcs.jar
|
||||||
RUN mkdir plugins
|
RUN mkdir plugins
|
||||||
WORKDIR /home/luser/plugins
|
WORKDIR /home/luser/plugins
|
||||||
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/gbcs-memcached/build/distributions,target=/build/distributions tar -xf /build/distributions/gbcs-memcached*.tar
|
RUN --mount=type=bind,from=build,source=/home/ubuntu/gbcs/gbcs-memcached/build/distributions,target=/build/distributions tar -xf /build/distributions/gbcs-memcached*.tar
|
||||||
|
72
build.gradle
72
build.gradle
@@ -1,18 +1,11 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java-library'
|
id 'java-library'
|
||||||
id 'jvm-test-suite'
|
|
||||||
alias catalog.plugins.kotlin.jvm
|
alias catalog.plugins.kotlin.jvm
|
||||||
alias catalog.plugins.envelope
|
|
||||||
alias catalog.plugins.sambal
|
alias catalog.plugins.sambal
|
||||||
alias catalog.plugins.lombok
|
alias catalog.plugins.lombok
|
||||||
alias catalog.plugins.graalvm.native.image
|
|
||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
|
|
||||||
import net.woggioni.gradle.envelope.EnvelopeJarTask
|
|
||||||
import net.woggioni.gradle.graalvm.NativeImagePlugin
|
|
||||||
import net.woggioni.gradle.graalvm.NativeImageTask
|
|
||||||
import net.woggioni.gradle.graalvm.NativeImageConfigurationTask
|
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
@@ -41,6 +34,11 @@ allprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pluginManager.withPlugin('java-library') {
|
pluginManager.withPlugin('java-library') {
|
||||||
|
|
||||||
|
ext {
|
||||||
|
jpmsModuleName = project.group + '.' + project.name.replace('-', '.')
|
||||||
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
withSourcesJar()
|
withSourcesJar()
|
||||||
modularity.inferModulePath = true
|
modularity.inferModulePath = true
|
||||||
@@ -58,7 +56,18 @@ allprojects {
|
|||||||
modularity.inferModulePath = true
|
modularity.inferModulePath = true
|
||||||
options.release = 21
|
options.release = 21
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
||||||
|
options.compilerArgumentProviders << new CommandLineArgumentProvider() {
|
||||||
|
@Override
|
||||||
|
Iterable<String> asArguments() {
|
||||||
|
return ['--patch-module', project.jpmsModuleName + '=' + project.sourceSets.main.output.asPath]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
options.javaModuleVersion = version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pluginManager.withPlugin(catalog.plugins.kotlin.jvm.get().pluginId) {
|
pluginManager.withPlugin(catalog.plugins.kotlin.jvm.get().pluginId) {
|
||||||
tasks.withType(KotlinCompile.class) {
|
tasks.withType(KotlinCompile.class) {
|
||||||
@@ -92,34 +101,23 @@ allprojects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Property<String> mainClassName = objects.property(String.class)
|
tasks.withType(AbstractArchiveTask.class) {
|
||||||
mainClassName.set('net.woggioni.gbcs.GradleBuildCacheServer')
|
archiveVersion = project.version
|
||||||
|
}
|
||||||
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
|
||||||
options.compilerArgs << '--patch-module' << 'net.woggioni.gbcs=' + project.sourceSets.main.output.asPath
|
|
||||||
options.javaModuleVersion = version
|
|
||||||
options.javaModuleMainClass = mainClassName
|
|
||||||
}
|
|
||||||
|
|
||||||
envelopeJar {
|
|
||||||
mainModule = 'net.woggioni.gbcs'
|
|
||||||
mainClass = mainClassName
|
|
||||||
|
|
||||||
extraClasspath = ["plugins"]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation catalog.jwo
|
implementation catalog.jwo
|
||||||
implementation catalog.slf4j.api
|
implementation catalog.slf4j.api
|
||||||
implementation catalog.netty.codec.http
|
implementation catalog.netty.codec.http
|
||||||
|
implementation catalog.netty.codec.http2
|
||||||
|
|
||||||
implementation project('gbcs-base')
|
api project('gbcs-base')
|
||||||
implementation project('gbcs-api')
|
api project('gbcs-api')
|
||||||
|
|
||||||
// runtimeOnly catalog.slf4j.jdk14
|
// runtimeOnly catalog.slf4j.jdk14
|
||||||
runtimeOnly catalog.logback.classic
|
testRuntimeOnly catalog.logback.classic
|
||||||
|
|
||||||
testImplementation catalog.bcprov.jdk18on
|
testImplementation catalog.bcprov.jdk18on
|
||||||
testImplementation catalog.bcpkix.jdk18on
|
testImplementation catalog.bcpkix.jdk18on
|
||||||
@@ -130,32 +128,10 @@ dependencies {
|
|||||||
testRuntimeOnly project("gbcs-memcached")
|
testRuntimeOnly project("gbcs-memcached")
|
||||||
}
|
}
|
||||||
|
|
||||||
Provider<EnvelopeJarTask> envelopeJarTaskProvider = tasks.named('envelopeJar', EnvelopeJarTask.class) {
|
|
||||||
// systemProperties['java.util.logging.config.class'] = 'net.woggioni.gbcs.LoggingConfig'
|
|
||||||
// systemProperties['log.config.source'] = 'logging.properties'
|
|
||||||
systemProperties['logback.configurationFile'] = 'classpath:net/woggioni/gbcs/logback.xml'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def envelopeJarArtifact = artifacts.add('archives', envelopeJarTaskProvider.get().archiveFile.get().asFile) {
|
|
||||||
type = 'jar'
|
|
||||||
builtBy envelopeJarTaskProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.named(NativeImagePlugin.CONFIGURE_NATIVE_IMAGE_TASK_NAME, NativeImageConfigurationTask) {
|
|
||||||
mainClass = 'net.woggioni.gbcs.GraalNativeImageConfiguration'
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.named(NativeImagePlugin.NATIVE_IMAGE_TASK_NAME, NativeImageTask) {
|
|
||||||
mainClass = 'net.woggioni.gbcs.GradleBuildCacheServer'
|
|
||||||
useMusl = true
|
|
||||||
buildStaticImage = true
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
publications {
|
publications {
|
||||||
maven(MavenPublication) {
|
maven(MavenPublication) {
|
||||||
artifact envelopeJarArtifact
|
from(components["java"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java-library'
|
id 'java-library'
|
||||||
|
id 'maven-publish'
|
||||||
alias catalog.plugins.lombok
|
alias catalog.plugins.lombok
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9,3 +10,11 @@ dependencies {
|
|||||||
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
||||||
options.javaModuleVersion = version
|
options.javaModuleVersion = version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
maven(MavenPublication) {
|
||||||
|
from(components["java"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -3,12 +3,14 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
|||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id 'java-library'
|
id 'java-library'
|
||||||
|
id 'maven-publish'
|
||||||
alias catalog.plugins.kotlin.jvm
|
alias catalog.plugins.kotlin.jvm
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly project(':gbcs-api')
|
compileOnly project(':gbcs-api')
|
||||||
compileOnly catalog.slf4j.api
|
compileOnly catalog.slf4j.api
|
||||||
|
api catalog.jwo
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
||||||
@@ -19,3 +21,11 @@ tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
|||||||
tasks.named("compileKotlin", KotlinCompile.class) {
|
tasks.named("compileKotlin", KotlinCompile.class) {
|
||||||
compilerOptions.jvmTarget = JvmTarget.JVM_21
|
compilerOptions.jvmTarget = JvmTarget.JVM_21
|
||||||
}
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
maven(MavenPublication) {
|
||||||
|
from(components["java"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -3,6 +3,7 @@ module net.woggioni.gbcs.base {
|
|||||||
requires java.logging;
|
requires java.logging;
|
||||||
requires org.slf4j;
|
requires org.slf4j;
|
||||||
requires kotlin.stdlib;
|
requires kotlin.stdlib;
|
||||||
|
requires net.woggioni.jwo;
|
||||||
|
|
||||||
exports net.woggioni.gbcs.base;
|
exports net.woggioni.gbcs.base;
|
||||||
}
|
}
|
@@ -0,0 +1,79 @@
|
|||||||
|
package net.woggioni.gbcs.base
|
||||||
|
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.net.URL
|
||||||
|
import java.net.URLConnection
|
||||||
|
import java.net.URLStreamHandler
|
||||||
|
import java.net.URLStreamHandlerFactory
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
import java.util.stream.Collectors
|
||||||
|
|
||||||
|
|
||||||
|
class ClasspathUrlStreamHandlerFactoryProvider : URLStreamHandlerFactory {
|
||||||
|
private class Handler(private val classLoader: ClassLoader = ClasspathUrlStreamHandlerFactoryProvider::class.java.classLoader) : URLStreamHandler() {
|
||||||
|
|
||||||
|
override fun openConnection(u: URL): URLConnection? {
|
||||||
|
return javaClass.module
|
||||||
|
?.takeIf { m: Module -> m.layer != null }
|
||||||
|
?.let {
|
||||||
|
val path = u.path
|
||||||
|
val i = path.lastIndexOf('/')
|
||||||
|
val packageName = path.substring(0, i).replace('/', '.')
|
||||||
|
val modules = packageMap[packageName]!!
|
||||||
|
ModuleResourceURLConnection(
|
||||||
|
u,
|
||||||
|
modules
|
||||||
|
)
|
||||||
|
}
|
||||||
|
?: classLoader.getResource(u.path)?.let(URL::openConnection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun createURLStreamHandler(protocol: String): URLStreamHandler? {
|
||||||
|
return when (protocol) {
|
||||||
|
"classpath" -> Handler()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ModuleResourceURLConnection(url: URL?, private val modules: List<Module>) :
|
||||||
|
URLConnection(url) {
|
||||||
|
override fun connect() {}
|
||||||
|
|
||||||
|
override fun getInputStream(): InputStream? {
|
||||||
|
for (module in modules) {
|
||||||
|
val result = module.getResourceAsStream(getURL().path)
|
||||||
|
if (result != null) return result
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val installed = AtomicBoolean(false)
|
||||||
|
fun install() {
|
||||||
|
if (!installed.getAndSet(true)) {
|
||||||
|
URL.setURLStreamHandlerFactory(ClasspathUrlStreamHandlerFactoryProvider())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val packageMap: Map<String, List<Module>> by lazy {
|
||||||
|
ClasspathUrlStreamHandlerFactoryProvider::class.java.module.layer
|
||||||
|
.modules()
|
||||||
|
.stream()
|
||||||
|
.flatMap { m: Module ->
|
||||||
|
m.packages.stream()
|
||||||
|
.map { p: String -> p to m }
|
||||||
|
}
|
||||||
|
.collect(
|
||||||
|
Collectors.groupingBy(
|
||||||
|
Pair<String, Module>::first,
|
||||||
|
Collectors.mapping(
|
||||||
|
Pair<String, Module>::second,
|
||||||
|
Collectors.toUnmodifiableList<Module>()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,46 @@
|
|||||||
|
package net.woggioni.gbcs.base
|
||||||
|
|
||||||
|
import java.security.SecureRandom
|
||||||
|
import java.security.spec.KeySpec
|
||||||
|
import java.util.Base64
|
||||||
|
import javax.crypto.SecretKeyFactory
|
||||||
|
import javax.crypto.spec.PBEKeySpec
|
||||||
|
|
||||||
|
object PasswordSecurity {
|
||||||
|
private const val KEY_LENGTH = 256
|
||||||
|
|
||||||
|
private fun concat(arr1: ByteArray, arr2: ByteArray): ByteArray {
|
||||||
|
val result = ByteArray(arr1.size + arr2.size)
|
||||||
|
var j = 0
|
||||||
|
for(element in arr1) {
|
||||||
|
result[j] = element
|
||||||
|
j += 1
|
||||||
|
}
|
||||||
|
for(element in arr2) {
|
||||||
|
result[j] = element
|
||||||
|
j += 1
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hashPassword(password : String, salt : String? = null) : String {
|
||||||
|
val actualSalt = salt?.let(Base64.getDecoder()::decode) ?: SecureRandom().run {
|
||||||
|
val result = ByteArray(16)
|
||||||
|
nextBytes(result)
|
||||||
|
result
|
||||||
|
}
|
||||||
|
val spec: KeySpec = PBEKeySpec(password.toCharArray(), actualSalt, 10, KEY_LENGTH)
|
||||||
|
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||||
|
val hash = factory.generateSecret(spec).encoded
|
||||||
|
return String(Base64.getEncoder().encode(concat(hash, actualSalt)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun decodePasswordHash(passwordHash : String) : Pair<ByteArray, ByteArray> {
|
||||||
|
val decoded = Base64.getDecoder().decode(passwordHash)
|
||||||
|
val hash = ByteArray(KEY_LENGTH / 8)
|
||||||
|
val salt = ByteArray(decoded.size - KEY_LENGTH / 8)
|
||||||
|
System.arraycopy(decoded, 0, hash, 0, hash.size)
|
||||||
|
System.arraycopy(decoded, hash.size, salt, 0, salt.size)
|
||||||
|
return hash to salt
|
||||||
|
}
|
||||||
|
}
|
75
gbcs-cli/build.gradle
Normal file
75
gbcs-cli/build.gradle
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java-library'
|
||||||
|
alias catalog.plugins.kotlin.jvm
|
||||||
|
alias catalog.plugins.envelope
|
||||||
|
alias catalog.plugins.sambal
|
||||||
|
alias catalog.plugins.graalvm.native.image
|
||||||
|
id 'maven-publish'
|
||||||
|
}
|
||||||
|
|
||||||
|
import net.woggioni.gradle.envelope.EnvelopeJarTask
|
||||||
|
import net.woggioni.gradle.graalvm.NativeImagePlugin
|
||||||
|
import net.woggioni.gradle.graalvm.NativeImageTask
|
||||||
|
import net.woggioni.gradle.graalvm.NativeImageConfigurationTask
|
||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
|
Property<String> mainClassName = objects.property(String.class)
|
||||||
|
mainClassName.set('net.woggioni.gbcs.cli.GradleBuildCacheServerCli')
|
||||||
|
|
||||||
|
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
||||||
|
options.compilerArgs << '--patch-module' << 'net.woggioni.gbcs.cli=' + project.sourceSets.main.output.asPath
|
||||||
|
options.javaModuleVersion = version
|
||||||
|
options.javaModuleMainClass = mainClassName
|
||||||
|
}
|
||||||
|
|
||||||
|
envelopeJar {
|
||||||
|
mainModule = 'net.woggioni.gbcs.cli'
|
||||||
|
mainClass = mainClassName
|
||||||
|
|
||||||
|
extraClasspath = ["plugins"]
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation catalog.jwo
|
||||||
|
implementation catalog.slf4j.api
|
||||||
|
implementation catalog.netty.codec.http
|
||||||
|
implementation catalog.picocli
|
||||||
|
|
||||||
|
// implementation project(':gbcs-base')
|
||||||
|
// implementation project(':gbcs-api')
|
||||||
|
implementation rootProject
|
||||||
|
|
||||||
|
// runtimeOnly catalog.slf4j.jdk14
|
||||||
|
runtimeOnly catalog.logback.classic
|
||||||
|
}
|
||||||
|
|
||||||
|
Provider<EnvelopeJarTask> envelopeJarTaskProvider = tasks.named('envelopeJar', EnvelopeJarTask.class) {
|
||||||
|
// systemProperties['java.util.logging.config.class'] = 'net.woggioni.gbcs.LoggingConfig'
|
||||||
|
// systemProperties['log.config.source'] = 'logging.properties'
|
||||||
|
systemProperties['logback.configurationFile'] = 'classpath:net/woggioni/gbcs/cli/logback.xml'
|
||||||
|
}
|
||||||
|
|
||||||
|
def envelopeJarArtifact = artifacts.add('archives', envelopeJarTaskProvider.get().archiveFile.get().asFile) {
|
||||||
|
type = 'jar'
|
||||||
|
builtBy envelopeJarTaskProvider
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named(NativeImagePlugin.CONFIGURE_NATIVE_IMAGE_TASK_NAME, NativeImageConfigurationTask) {
|
||||||
|
mainClass = 'net.woggioni.gbcs.GraalNativeImageConfiguration'
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named(NativeImagePlugin.NATIVE_IMAGE_TASK_NAME, NativeImageTask) {
|
||||||
|
mainClass = 'net.woggioni.gbcs.GradleBuildCacheServer'
|
||||||
|
useMusl = true
|
||||||
|
buildStaticImage = true
|
||||||
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
maven(MavenPublication) {
|
||||||
|
artifact envelopeJarArtifact
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
15
gbcs-cli/src/main/java/module-info.java
Normal file
15
gbcs-cli/src/main/java/module-info.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module net.woggioni.gbcs.cli {
|
||||||
|
requires org.slf4j;
|
||||||
|
requires net.woggioni.gbcs;
|
||||||
|
requires info.picocli;
|
||||||
|
requires net.woggioni.gbcs.base;
|
||||||
|
requires kotlin.stdlib;
|
||||||
|
requires net.woggioni.jwo;
|
||||||
|
|
||||||
|
exports net.woggioni.gbcs.cli.impl.converters to info.picocli;
|
||||||
|
opens net.woggioni.gbcs.cli.impl.commands to info.picocli;
|
||||||
|
opens net.woggioni.gbcs.cli.impl to info.picocli;
|
||||||
|
opens net.woggioni.gbcs.cli to info.picocli, net.woggioni.gbcs.base;
|
||||||
|
|
||||||
|
exports net.woggioni.gbcs.cli;
|
||||||
|
}
|
@@ -0,0 +1,98 @@
|
|||||||
|
package net.woggioni.gbcs.cli
|
||||||
|
|
||||||
|
import net.woggioni.gbcs.GradleBuildCacheServer
|
||||||
|
import net.woggioni.gbcs.GradleBuildCacheServer.Companion.DEFAULT_CONFIGURATION_URL
|
||||||
|
import net.woggioni.gbcs.base.ClasspathUrlStreamHandlerFactoryProvider
|
||||||
|
import net.woggioni.gbcs.base.contextLogger
|
||||||
|
import net.woggioni.gbcs.base.debug
|
||||||
|
import net.woggioni.gbcs.base.info
|
||||||
|
import net.woggioni.gbcs.cli.impl.AbstractVersionProvider
|
||||||
|
import net.woggioni.gbcs.cli.impl.GbcsCommand
|
||||||
|
import net.woggioni.gbcs.cli.impl.commands.PasswordHashCommand
|
||||||
|
import net.woggioni.jwo.Application
|
||||||
|
import net.woggioni.jwo.JWO
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import picocli.CommandLine
|
||||||
|
import picocli.CommandLine.Model.CommandSpec
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
|
||||||
|
@CommandLine.Command(
|
||||||
|
name = "gbcs", versionProvider = GradleBuildCacheServerCli.VersionProvider::class
|
||||||
|
)
|
||||||
|
class GradleBuildCacheServerCli(application : Application, private val log : Logger) : GbcsCommand() {
|
||||||
|
|
||||||
|
class VersionProvider : AbstractVersionProvider()
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
fun main(vararg args: String) {
|
||||||
|
Thread.currentThread().contextClassLoader = GradleBuildCacheServerCli::class.java.classLoader
|
||||||
|
ClasspathUrlStreamHandlerFactoryProvider.install()
|
||||||
|
val log = contextLogger()
|
||||||
|
val app = Application.builder("gbcs")
|
||||||
|
.configurationDirectoryEnvVar("GBCS_CONFIGURATION_DIR")
|
||||||
|
.configurationDirectoryPropertyKey("net.woggioni.gbcs.conf.dir")
|
||||||
|
.build()
|
||||||
|
val gbcsCli = GradleBuildCacheServerCli(app, log)
|
||||||
|
val commandLine = CommandLine(gbcsCli)
|
||||||
|
commandLine.setExecutionExceptionHandler { ex, cl, parseResult ->
|
||||||
|
log.error(ex.message, ex)
|
||||||
|
CommandLine.ExitCode.SOFTWARE
|
||||||
|
}
|
||||||
|
commandLine.addSubcommand(PasswordHashCommand())
|
||||||
|
System.exit(commandLine.execute(*args))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@CommandLine.Option(
|
||||||
|
names = ["-c", "--config-file"],
|
||||||
|
description = ["Read the application configuration from this file"],
|
||||||
|
paramLabel = "CONFIG_FILE"
|
||||||
|
)
|
||||||
|
private var configurationFile: Path = findConfigurationFile(application)
|
||||||
|
|
||||||
|
@CommandLine.Option(names = ["-V", "--version"], versionHelp = true)
|
||||||
|
var versionHelp = false
|
||||||
|
private set
|
||||||
|
|
||||||
|
@CommandLine.Spec
|
||||||
|
private lateinit var spec: CommandSpec
|
||||||
|
|
||||||
|
private fun findConfigurationFile(app : Application): Path {
|
||||||
|
val confDir = app.computeConfigurationDirectory()
|
||||||
|
val configurationFile = confDir.resolve("gbcs.xml")
|
||||||
|
return configurationFile
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createDefaultConfigurationFile(configurationFile : Path) {
|
||||||
|
log.info {
|
||||||
|
"Creating default configuration file at '$configurationFile'"
|
||||||
|
}
|
||||||
|
val defaultConfigurationFileResource = DEFAULT_CONFIGURATION_URL
|
||||||
|
Files.newOutputStream(configurationFile).use { outputStream ->
|
||||||
|
defaultConfigurationFileResource.openStream().use { inputStream ->
|
||||||
|
JWO.copy(inputStream, outputStream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
if (!Files.exists(configurationFile)) {
|
||||||
|
Files.createDirectories(configurationFile.parent)
|
||||||
|
createDefaultConfigurationFile(configurationFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
val configuration = GradleBuildCacheServer.loadConfiguration(configurationFile)
|
||||||
|
log.debug {
|
||||||
|
ByteArrayOutputStream().also {
|
||||||
|
GradleBuildCacheServer.dumpConfiguration(configuration, it)
|
||||||
|
}.let {
|
||||||
|
"Server configuration:\n${String(it.toByteArray())}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GradleBuildCacheServer(configuration).run().use {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,32 @@
|
|||||||
|
package net.woggioni.gbcs.cli.impl
|
||||||
|
|
||||||
|
import picocli.CommandLine
|
||||||
|
import java.net.URL
|
||||||
|
import java.util.Enumeration
|
||||||
|
import java.util.jar.Attributes
|
||||||
|
import java.util.jar.JarFile
|
||||||
|
import java.util.jar.Manifest
|
||||||
|
|
||||||
|
|
||||||
|
abstract class AbstractVersionProvider : CommandLine.IVersionProvider {
|
||||||
|
private val version: String
|
||||||
|
private val vcsHash: String
|
||||||
|
|
||||||
|
init {
|
||||||
|
val mf = Manifest()
|
||||||
|
javaClass.module.getResourceAsStream(JarFile.MANIFEST_NAME).use { `is` ->
|
||||||
|
mf.read(`is`)
|
||||||
|
}
|
||||||
|
val mainAttributes = mf.mainAttributes
|
||||||
|
version = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION) ?: throw RuntimeException("Version information not found in manifest")
|
||||||
|
vcsHash = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION) ?: throw RuntimeException("Version information not found in manifest")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getVersion(): Array<String?> {
|
||||||
|
return if (version.endsWith("-SNAPSHOT")) {
|
||||||
|
arrayOf(version, vcsHash)
|
||||||
|
} else {
|
||||||
|
arrayOf(version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,11 @@
|
|||||||
|
package net.woggioni.gbcs.cli.impl
|
||||||
|
|
||||||
|
import picocli.CommandLine
|
||||||
|
|
||||||
|
|
||||||
|
abstract class GbcsCommand : Runnable {
|
||||||
|
|
||||||
|
@CommandLine.Option(names = ["-h", "--help"], usageHelp = true)
|
||||||
|
var usageHelp = false
|
||||||
|
private set
|
||||||
|
}
|
@@ -0,0 +1,38 @@
|
|||||||
|
package net.woggioni.gbcs.cli.impl.commands
|
||||||
|
|
||||||
|
import net.woggioni.gbcs.base.PasswordSecurity.hashPassword
|
||||||
|
import net.woggioni.gbcs.cli.impl.GbcsCommand
|
||||||
|
import net.woggioni.gbcs.cli.impl.converters.OutputStreamConverter
|
||||||
|
import net.woggioni.jwo.UncloseableOutputStream
|
||||||
|
import picocli.CommandLine
|
||||||
|
import java.io.BufferedWriter
|
||||||
|
import java.io.OutputStream
|
||||||
|
import java.io.OutputStreamWriter
|
||||||
|
|
||||||
|
|
||||||
|
@CommandLine.Command(
|
||||||
|
name = "password",
|
||||||
|
description = ["Generate a password hash to add to GBCS configuration file"],
|
||||||
|
showDefaultValues = true
|
||||||
|
)
|
||||||
|
class PasswordHashCommand : GbcsCommand() {
|
||||||
|
@CommandLine.Option(
|
||||||
|
names = ["-o", "--output-file"],
|
||||||
|
description = ["Write the output to a file instead of stdout"],
|
||||||
|
converter = [OutputStreamConverter::class],
|
||||||
|
defaultValue = "stdout",
|
||||||
|
paramLabel = "OUTPUT_FILE"
|
||||||
|
)
|
||||||
|
private var outputStream: OutputStream = UncloseableOutputStream(System.out)
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
val password1 = String(System.console().readPassword("Type your password:"))
|
||||||
|
val password2 = String(System.console().readPassword("Type your password again for confirmation:"))
|
||||||
|
if(password1 != password2) throw IllegalArgumentException("Passwords do not match")
|
||||||
|
|
||||||
|
BufferedWriter(OutputStreamWriter(outputStream, Charsets.UTF_8)).use {
|
||||||
|
it.write(hashPassword(password1))
|
||||||
|
it.newLine()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,13 @@
|
|||||||
|
package net.woggioni.gbcs.cli.impl.converters
|
||||||
|
|
||||||
|
import picocli.CommandLine
|
||||||
|
import java.io.OutputStream
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
|
||||||
|
class OutputStreamConverter : CommandLine.ITypeConverter<OutputStream> {
|
||||||
|
override fun convert(value: String): OutputStream {
|
||||||
|
return Files.newOutputStream(Paths.get(value))
|
||||||
|
}
|
||||||
|
}
|
@@ -6,7 +6,7 @@
|
|||||||
<import class="ch.qos.logback.core.ConsoleAppender"/>
|
<import class="ch.qos.logback.core.ConsoleAppender"/>
|
||||||
|
|
||||||
<appender name="console" class="ConsoleAppender">
|
<appender name="console" class="ConsoleAppender">
|
||||||
<target>System.out</target>
|
<target>System.err</target>
|
||||||
<encoder class="PatternLayoutEncoder">
|
<encoder class="PatternLayoutEncoder">
|
||||||
<pattern>%d [%highlight(%-5level)] \(%thread\) %logger{36} -%kvp- %msg %n</pattern>
|
<pattern>%d [%highlight(%-5level)] \(%thread\) %logger{36} -%kvp- %msg %n</pattern>
|
||||||
</encoder>
|
</encoder>
|
@@ -1,9 +1,9 @@
|
|||||||
org.gradle.configuration-cache=false
|
org.gradle.configuration-cache=false
|
||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
org.gradle.caching=true
|
org.gradle.caching=false
|
||||||
|
|
||||||
gbcs.version = 0.0.1
|
gbcs.version = 0.0.1
|
||||||
|
|
||||||
lys.version = 2025.01.08
|
lys.version = 2025.01.09
|
||||||
|
|
||||||
gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven
|
gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven
|
||||||
|
@@ -29,5 +29,6 @@ rootProject.name = 'gbcs'
|
|||||||
include 'gbcs-api'
|
include 'gbcs-api'
|
||||||
include 'gbcs-base'
|
include 'gbcs-base'
|
||||||
include 'gbcs-memcached'
|
include 'gbcs-memcached'
|
||||||
|
include 'gbcs-cli'
|
||||||
|
|
||||||
|
|
||||||
|
@@ -2,7 +2,7 @@ import net.woggioni.gbcs.api.CacheProvider;
|
|||||||
import net.woggioni.gbcs.url.ClasspathUrlStreamHandlerFactoryProvider;
|
import net.woggioni.gbcs.url.ClasspathUrlStreamHandlerFactoryProvider;
|
||||||
import net.woggioni.gbcs.cache.FileSystemCacheProvider;
|
import net.woggioni.gbcs.cache.FileSystemCacheProvider;
|
||||||
|
|
||||||
open module net.woggioni.gbcs {
|
module net.woggioni.gbcs {
|
||||||
requires java.sql;
|
requires java.sql;
|
||||||
requires java.xml;
|
requires java.xml;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
@@ -11,6 +11,7 @@ open module net.woggioni.gbcs {
|
|||||||
requires io.netty.buffer;
|
requires io.netty.buffer;
|
||||||
requires io.netty.transport;
|
requires io.netty.transport;
|
||||||
requires io.netty.codec.http;
|
requires io.netty.codec.http;
|
||||||
|
requires io.netty.codec.http2;
|
||||||
requires io.netty.common;
|
requires io.netty.common;
|
||||||
requires io.netty.handler;
|
requires io.netty.handler;
|
||||||
requires io.netty.codec;
|
requires io.netty.codec;
|
||||||
@@ -19,9 +20,12 @@ open module net.woggioni.gbcs {
|
|||||||
requires net.woggioni.gbcs.base;
|
requires net.woggioni.gbcs.base;
|
||||||
requires net.woggioni.gbcs.api;
|
requires net.woggioni.gbcs.api;
|
||||||
|
|
||||||
provides java.net.URLStreamHandlerFactory with ClasspathUrlStreamHandlerFactoryProvider;
|
exports net.woggioni.gbcs;
|
||||||
uses java.net.URLStreamHandlerFactory;
|
opens net.woggioni.gbcs;
|
||||||
uses CacheProvider;
|
opens net.woggioni.gbcs.schema;
|
||||||
|
|
||||||
|
uses java.net.URLStreamHandlerFactory;
|
||||||
|
provides java.net.URLStreamHandlerFactory with ClasspathUrlStreamHandlerFactoryProvider;
|
||||||
|
uses CacheProvider;
|
||||||
provides CacheProvider with FileSystemCacheProvider;
|
provides CacheProvider with FileSystemCacheProvider;
|
||||||
}
|
}
|
@@ -34,6 +34,9 @@ import io.netty.handler.codec.http.HttpServerCodec
|
|||||||
import io.netty.handler.codec.http.HttpUtil
|
import io.netty.handler.codec.http.HttpUtil
|
||||||
import io.netty.handler.codec.http.HttpVersion
|
import io.netty.handler.codec.http.HttpVersion
|
||||||
import io.netty.handler.codec.http.LastHttpContent
|
import io.netty.handler.codec.http.LastHttpContent
|
||||||
|
import io.netty.handler.codec.http2.Http2FrameCodecBuilder
|
||||||
|
import io.netty.handler.ssl.ApplicationProtocolNames
|
||||||
|
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler
|
||||||
import io.netty.handler.ssl.ClientAuth
|
import io.netty.handler.ssl.ClientAuth
|
||||||
import io.netty.handler.ssl.SslContext
|
import io.netty.handler.ssl.SslContext
|
||||||
import io.netty.handler.ssl.SslContextBuilder
|
import io.netty.handler.ssl.SslContextBuilder
|
||||||
@@ -46,7 +49,13 @@ import net.woggioni.gbcs.api.Cache
|
|||||||
import net.woggioni.gbcs.api.Configuration
|
import net.woggioni.gbcs.api.Configuration
|
||||||
import net.woggioni.gbcs.api.Role
|
import net.woggioni.gbcs.api.Role
|
||||||
import net.woggioni.gbcs.api.exception.ContentTooLargeException
|
import net.woggioni.gbcs.api.exception.ContentTooLargeException
|
||||||
|
import net.woggioni.gbcs.auth.AbstractNettyHttpAuthenticator
|
||||||
|
import net.woggioni.gbcs.auth.Authorizer
|
||||||
|
import net.woggioni.gbcs.auth.ClientCertificateValidator
|
||||||
|
import net.woggioni.gbcs.auth.RoleAuthorizer
|
||||||
import net.woggioni.gbcs.base.GBCS.toUrl
|
import net.woggioni.gbcs.base.GBCS.toUrl
|
||||||
|
import net.woggioni.gbcs.base.PasswordSecurity.decodePasswordHash
|
||||||
|
import net.woggioni.gbcs.base.PasswordSecurity.hashPassword
|
||||||
import net.woggioni.gbcs.base.Xml
|
import net.woggioni.gbcs.base.Xml
|
||||||
import net.woggioni.gbcs.base.contextLogger
|
import net.woggioni.gbcs.base.contextLogger
|
||||||
import net.woggioni.gbcs.base.debug
|
import net.woggioni.gbcs.base.debug
|
||||||
@@ -58,6 +67,7 @@ import net.woggioni.jwo.Application
|
|||||||
import net.woggioni.jwo.JWO
|
import net.woggioni.jwo.JWO
|
||||||
import net.woggioni.jwo.Tuple2
|
import net.woggioni.jwo.Tuple2
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.OutputStream
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLStreamHandlerFactory
|
import java.net.URLStreamHandlerFactory
|
||||||
@@ -213,6 +223,22 @@ class GradleBuildCacheServer(private val cfg: Configuration) {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
private fun getServerAPNHandler(): ApplicationProtocolNegotiationHandler {
|
||||||
|
val serverAPNHandler: ApplicationProtocolNegotiationHandler =
|
||||||
|
object : ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_2) {
|
||||||
|
override fun configurePipeline(ctx: ChannelHandlerContext, protocol: String) {
|
||||||
|
if (ApplicationProtocolNames.HTTP_2 == protocol) {
|
||||||
|
ctx.pipeline().addLast(
|
||||||
|
Http2FrameCodecBuilder.forServer().build()
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
throw IllegalStateException("Protocol: $protocol not supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return serverAPNHandler
|
||||||
|
}
|
||||||
|
|
||||||
fun loadKeystore(file: Path, password: String?): KeyStore {
|
fun loadKeystore(file: Path, password: String?): KeyStore {
|
||||||
val ext = JWO.splitExtension(file)
|
val ext = JWO.splitExtension(file)
|
||||||
.map(Tuple2<String, String>::get_2)
|
.map(Tuple2<String, String>::get_2)
|
||||||
@@ -273,7 +299,6 @@ class GradleBuildCacheServer(private val cfg: Configuration) {
|
|||||||
}
|
}
|
||||||
if (sslContext != null) {
|
if (sslContext != null) {
|
||||||
val sslHandler = sslContext.newHandler(ch.alloc())
|
val sslHandler = sslContext.newHandler(ch.alloc())
|
||||||
pipeline.addLast(sslHandler)
|
|
||||||
|
|
||||||
if (auth is Configuration.ClientCertificateAuthentication) {
|
if (auth is Configuration.ClientCertificateAuthentication) {
|
||||||
val roleAuthorizer = RoleAuthorizer()
|
val roleAuthorizer = RoleAuthorizer()
|
||||||
@@ -285,6 +310,7 @@ class GradleBuildCacheServer(private val cfg: Configuration) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// pipeline.addLast(getServerAPNHandler())
|
||||||
pipeline.addLast(HttpServerCodec())
|
pipeline.addLast(HttpServerCodec())
|
||||||
pipeline.addLast(HttpChunkContentCompressor(1024))
|
pipeline.addLast(HttpChunkContentCompressor(1024))
|
||||||
pipeline.addLast(ChunkedWriteHandler())
|
pipeline.addLast(ChunkedWriteHandler())
|
||||||
@@ -537,6 +563,17 @@ class GradleBuildCacheServer(private val cfg: Configuration) {
|
|||||||
resetCachedUrlHandlers()
|
resetCachedUrlHandlers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun loadConfiguration(configurationFile: Path): Configuration {
|
||||||
|
val dbf = Xml.newDocumentBuilderFactory(null)
|
||||||
|
val db = dbf.newDocumentBuilder()
|
||||||
|
val doc = Files.newInputStream(configurationFile).use(db::parse)
|
||||||
|
return Parser.parse(doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dumpConfiguration(conf : Configuration, outputStream: OutputStream) {
|
||||||
|
Xml.write(Serializer.serialize(conf), outputStream)
|
||||||
|
}
|
||||||
|
|
||||||
fun loadConfiguration(args: Array<String>): Configuration {
|
fun loadConfiguration(args: Array<String>): Configuration {
|
||||||
// Thread.currentThread().contextClassLoader = GradleBuildCacheServer::class.java.classLoader
|
// Thread.currentThread().contextClassLoader = GradleBuildCacheServer::class.java.classLoader
|
||||||
val app = Application.builder("gbcs")
|
val app = Application.builder("gbcs")
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package net.woggioni.gbcs
|
package net.woggioni.gbcs.auth
|
||||||
|
|
||||||
import io.netty.buffer.Unpooled
|
import io.netty.buffer.Unpooled
|
||||||
import io.netty.channel.ChannelFutureListener
|
import io.netty.channel.ChannelFutureListener
|
||||||
@@ -12,18 +12,12 @@ import io.netty.handler.codec.http.HttpResponseStatus
|
|||||||
import io.netty.handler.codec.http.HttpVersion
|
import io.netty.handler.codec.http.HttpVersion
|
||||||
import io.netty.util.ReferenceCountUtil
|
import io.netty.util.ReferenceCountUtil
|
||||||
import net.woggioni.gbcs.api.Role
|
import net.woggioni.gbcs.api.Role
|
||||||
import java.security.SecureRandom
|
|
||||||
import java.security.spec.KeySpec
|
|
||||||
import java.util.Base64
|
|
||||||
import javax.crypto.SecretKeyFactory
|
|
||||||
import javax.crypto.spec.PBEKeySpec
|
|
||||||
|
|
||||||
|
|
||||||
abstract class AbstractNettyHttpAuthenticator(private val authorizer : Authorizer)
|
abstract class AbstractNettyHttpAuthenticator(private val authorizer : Authorizer)
|
||||||
: ChannelInboundHandlerAdapter() {
|
: ChannelInboundHandlerAdapter() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val KEY_LENGTH = 256
|
|
||||||
private val AUTHENTICATION_FAILED: FullHttpResponse = DefaultFullHttpResponse(
|
private val AUTHENTICATION_FAILED: FullHttpResponse = DefaultFullHttpResponse(
|
||||||
HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED, Unpooled.EMPTY_BUFFER).apply {
|
HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED, Unpooled.EMPTY_BUFFER).apply {
|
||||||
headers()[HttpHeaderNames.CONTENT_LENGTH] = "0"
|
headers()[HttpHeaderNames.CONTENT_LENGTH] = "0"
|
||||||
@@ -33,42 +27,6 @@ abstract class AbstractNettyHttpAuthenticator(private val authorizer : Authorize
|
|||||||
HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN, Unpooled.EMPTY_BUFFER).apply {
|
HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN, Unpooled.EMPTY_BUFFER).apply {
|
||||||
headers()[HttpHeaderNames.CONTENT_LENGTH] = "0"
|
headers()[HttpHeaderNames.CONTENT_LENGTH] = "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun concat(arr1: ByteArray, arr2: ByteArray): ByteArray {
|
|
||||||
val result = ByteArray(arr1.size + arr2.size)
|
|
||||||
var j = 0
|
|
||||||
for(element in arr1) {
|
|
||||||
result[j] = element
|
|
||||||
j += 1
|
|
||||||
}
|
|
||||||
for(element in arr2) {
|
|
||||||
result[j] = element
|
|
||||||
j += 1
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun hashPassword(password : String, salt : String? = null) : String {
|
|
||||||
val actualSalt = salt?.let(Base64.getDecoder()::decode) ?: SecureRandom().run {
|
|
||||||
val result = ByteArray(16)
|
|
||||||
nextBytes(result)
|
|
||||||
result
|
|
||||||
}
|
|
||||||
val spec: KeySpec = PBEKeySpec(password.toCharArray(), actualSalt, 10, KEY_LENGTH)
|
|
||||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
|
||||||
val hash = factory.generateSecret(spec).encoded
|
|
||||||
return String(Base64.getEncoder().encode(concat(hash, actualSalt)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun decodePasswordHash(passwordHash : String) : Pair<ByteArray, ByteArray> {
|
|
||||||
val decoded = Base64.getDecoder().decode(passwordHash)
|
|
||||||
val hash = ByteArray(KEY_LENGTH / 8)
|
|
||||||
val salt = ByteArray(decoded.size - KEY_LENGTH / 8)
|
|
||||||
System.arraycopy(decoded, 0, hash, 0, hash.size)
|
|
||||||
System.arraycopy(decoded, hash.size, salt, 0, salt.size)
|
|
||||||
return hash to salt
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package net.woggioni.gbcs
|
package net.woggioni.gbcs.auth
|
||||||
|
|
||||||
import io.netty.handler.codec.http.HttpRequest
|
import io.netty.handler.codec.http.HttpRequest
|
||||||
import net.woggioni.gbcs.api.Role
|
import net.woggioni.gbcs.api.Role
|
@@ -1,4 +1,4 @@
|
|||||||
package net.woggioni.gbcs
|
package net.woggioni.gbcs.auth
|
||||||
|
|
||||||
import java.security.KeyStore
|
import java.security.KeyStore
|
||||||
import java.security.cert.CertPathValidator
|
import java.security.cert.CertPathValidator
|
@@ -1,4 +1,4 @@
|
|||||||
package net.woggioni.gbcs
|
package net.woggioni.gbcs.auth
|
||||||
|
|
||||||
import io.netty.handler.codec.http.HttpMethod
|
import io.netty.handler.codec.http.HttpMethod
|
||||||
import io.netty.handler.codec.http.HttpRequest
|
import io.netty.handler.codec.http.HttpRequest
|
@@ -2,7 +2,7 @@
|
|||||||
<gbcs:server useVirtualThreads="false" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
|
<gbcs:server useVirtualThreads="false" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:gbcs="urn:net.woggioni.gbcs"
|
xmlns:gbcs="urn:net.woggioni.gbcs"
|
||||||
xs:schemaLocation="urn:net.woggioni.gbcs classpath:net/woggioni/gbcs/schema/gbcs.xsd">
|
xs:schemaLocation="urn:net.woggioni.gbcs classpath:net/woggioni/gbcs/schema/gbcs.xsd">
|
||||||
<bind host="127.0.0.1" port="11443"/>
|
<bind host="127.0.0.1" port="8080"/>
|
||||||
<cache xs:type="gbcs:fileSystemCacheType" path="/tmp/gbcs" max-age="P7D"/>
|
<cache xs:type="gbcs:fileSystemCacheType" path="/tmp/gbcs" max-age="P7D"/>
|
||||||
<authentication>
|
<authentication>
|
||||||
<none/>
|
<none/>
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package net.woggioni.gbcs.test
|
package net.woggioni.gbcs.test
|
||||||
|
|
||||||
import io.netty.handler.codec.http.HttpResponseStatus
|
import io.netty.handler.codec.http.HttpResponseStatus
|
||||||
import net.woggioni.gbcs.AbstractNettyHttpAuthenticator.Companion.hashPassword
|
import net.woggioni.gbcs.auth.AbstractNettyHttpAuthenticator.Companion.hashPassword
|
||||||
import net.woggioni.gbcs.api.Role
|
import net.woggioni.gbcs.api.Role
|
||||||
import net.woggioni.gbcs.base.Xml
|
import net.woggioni.gbcs.base.Xml
|
||||||
import net.woggioni.gbcs.api.Configuration
|
import net.woggioni.gbcs.api.Configuration
|
||||||
|
Reference in New Issue
Block a user