Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Sep 21, 2024
1 parent eaf36b3 commit 1e62341
Show file tree
Hide file tree
Showing 37 changed files with 2,253 additions and 57 deletions.
5 changes: 5 additions & 0 deletions api/src/main/java/com/sshtools/jenny/api/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class Lang {

public static Optional<String> emptyable(String val) {
return val == null || val.length() == 0 ? Optional.empty() : Optional.of(val);
}

public static List<String> splitStringList(String str, String delimRegex) {
var arr = str.split(delimRegex);
Expand Down
80 changes: 80 additions & 0 deletions auth/linid/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--
Copyright © 2023 JAdaptive Limited (support@jadaptive.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sshtools</groupId>
<artifactId>jenny-auth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<properties>
<!-- Linid needs Java 22 for the official FFM API -->
<maven.compiler.target>22</maven.compiler.target>
<maven.compiler.source>22</maven.compiler.source>
</properties>
<name>Jenny - Alternative (Java 22) Linux Authentication</name>
<artifactId>jenny-auth-linid</artifactId>
<dependencies>

<!-- Framework Supplied -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jenny-auth-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<!-- Framework Provided -->
<dependency>
<groupId>com.sshtools</groupId>
<artifactId>bootlace-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jenny-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<!-- Required -->
<dependency>
<groupId>uk.co.bithatch</groupId>
<artifactId>linid</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>22</release>
</configuration>
</plugin>
</plugins>
</build>

</project>
100 changes: 100 additions & 0 deletions auth/linid/src/main/java/com/sshtools/jenny/auth/linux/LinidAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright © 2023 JAdaptive Limited (support@jadaptive.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sshtools.jenny.auth.linux;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Set;

import com.sshtools.bootlace.api.Plugin;
import com.sshtools.bootlace.api.PluginContext;
import com.sshtools.jenny.api.Api;
import com.sshtools.jenny.auth.api.Auth.AuthResult;
import com.sshtools.jenny.auth.api.Auth.AuthState;
import com.sshtools.jenny.auth.api.Auth.PasswordAuthProvider;
import com.sshtools.jenny.auth.api.ExtendedUserPrincipal;

import uk.co.bitatch.linid.Linid;
import uk.co.bitatch.linid.LinuxUserId;

public class LinidAuth implements Plugin {

public final static class Provider implements PasswordAuthProvider {
@Override
public AuthResult logon(String username, char[] password) {
return Linid.get().authenticate(username, password).map(id -> {
var linId = (LinuxUserId)id;
return new AuthResult(AuthState.COMPLETE, new ExtendedUserPrincipal.LinuxUser() {

@Override
public String getName() {
return id.getName();
}

@Override
public int uid() {
return linId.uid();
}

@Override
public Optional<String> shell() {
return Optional.of(linId.shell());
}

@Override
public Set<String> groups() {
return Set.of(linId.collectionNames());
}

@Override
public int gid() {
return linId.gid();
}

@Override
public Optional<String> gecos() {
return Optional.of(String.join(",", linId.gecos()));
}

@Override
public Optional<Path> dir() {
return Optional.of(linId.home()).map(Paths::get);
}
});
}).orElseGet(() -> new AuthResult(AuthState.DENY));
}

@Override
public int weight() {
return 0;
}
}

private Api api;

@Override
public void afterOpen(PluginContext context) {
api = context.plugin(Api.class);

var provider = new Provider();

context.autoClose(api.extensions().
group().
point(PasswordAuthProvider.class, (a) -> provider));
}

}
27 changes: 27 additions & 0 deletions auth/linid/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright © 2023 JAdaptive Limited (support@jadaptive.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.sshtools.bootlace.api.Plugin;
import com.sshtools.jenny.auth.linux.LinidAuth;

module com.sshtools.jenny.auth.linid {
exports com.sshtools.jenny.auth.linux;
requires transitive com.sshtools.jenny.auth.api;
requires com.sshtools.bootlace.api;
requires com.sshtools.jenny.api;
requires uk.co.bithatch.linid;

provides Plugin with LinidAuth;
}
7 changes: 7 additions & 0 deletions auth/linid/src/main/resources/layers.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[component]
id = com.sshtools.jenny.auth.api.linid
name = Linux Authentication (Alternative version)

[artifacts]
uk.co.bithatch:linid:0.0.1-SNAPSHOT

2 changes: 1 addition & 1 deletion config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>com.sshtools</groupId>
<artifactId>jini-config</artifactId>
<version>0.3.3-SNAPSHOT</version>
<version>0.3.3</version>
</dependency>

</dependencies>
Expand Down
73 changes: 71 additions & 2 deletions config/src/main/java/com/sshtools/jenny/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Locale;
Expand All @@ -44,15 +46,20 @@
import com.sshtools.jenny.product.Product;
import com.sshtools.jini.INI;
import com.sshtools.jini.INIReader;
import com.sshtools.jini.INIReader.MultiValueMode;
import com.sshtools.jini.INIWriter;
import com.sshtools.jini.config.INISet;
import com.sshtools.jini.config.INISet.Builder;
import com.sshtools.jini.config.Monitor;
import com.sshtools.jini.schema.INISchema;

public class Config implements Plugin {

private static final String CONFIG_APP_ID = "jenny";

private final static Log LOG = Logs.of(ApiLog.CONFIG);

@Deprecated
public interface Handle extends Closeable {

INI ini();
Expand All @@ -63,6 +70,7 @@ public interface Handle extends Closeable {
void close();
}

@Deprecated
private record Key(Plugin plugin, Scope scope) {
}

Expand All @@ -73,6 +81,8 @@ private record Key(Plugin plugin, Scope scope) {
private URLClassLoader bundleLoader;
private final ResourceBundle emptyBundle;
private ConfigResolver configResolver;
private boolean developerMode = "true".equals(System.getProperty("jenny.config.developer", String.valueOf(Files.exists(Paths.get("pom.xml")))));
private Monitor monitor;

public Config() {
try {
Expand All @@ -87,6 +97,12 @@ public Config() {
@Override
public void afterOpen(PluginContext context) throws Exception {
bundleLoader = new URLClassLoader(new URL[] { configResolver.resolveDir(CONFIG_APP_ID, Scope.VENDOR).toUri().toURL() });
monitor = new Monitor();
}

@Override
public void beforeClose(PluginContext context) throws Exception {
monitor.close();
}

public ResourceBundle bundle(Plugin plugin, Locale locale) {
Expand All @@ -101,11 +117,29 @@ public ResourceBundle bundle(Plugin plugin, Locale locale) {
}

public INISet.Builder defaultConfig() {
return new INISet.Builder(product.info().app());
return createBuilder(product.info().app());
}

public INISet.Builder configBuilder(String name) {
return new INISet.Builder(name).withApp(product.info().app());
return createBuilder(name).
withApp(product.info().app());
}

public INISet.Builder configBuilder(String name, Class<?> schemaBase, String schemaResource) {
try(var rdr = new InputStreamReader(schemaBase.getResourceAsStream(schemaResource), "UTF-8")) {
return createBuilder(name).
withSchema(new INISchema.Builder().fromDocument(reader().build().read(rdr)).build()).
withApp(product.info().app());
}
catch(IOException ioe) {
throw new UncheckedIOException(ioe);
} catch (ParseException e) {
throw new IllegalStateException(e);
}
}

public Monitor monitor() {
return monitor;
}

@Deprecated
Expand Down Expand Up @@ -154,6 +188,7 @@ public void close() {
}
}

@Deprecated
private INI loadIni(Path path) {
if (Files.exists(path)) {
try {
Expand All @@ -167,4 +202,38 @@ private INI loadIni(Path path) {
return INI.create();
}
}

private Builder createBuilder(String name) {
var bldr = new INISet.Builder(name).
withMonitor(monitor).
withScopes(INISet.Scope.GLOBAL).
withWriteScope(INISet.Scope.GLOBAL);

readerAndWriter(bldr);

if(developerMode) {
bldr.withPath(INISet.Scope.GLOBAL, Paths.get("conf"));
}

return bldr;
}

private void readerAndWriter(Builder bldr) {
bldr.
withWriterFactory(() ->
new INIWriter.Builder().
withMultiValueMode(MultiValueMode.REPEATED_KEY).
withSectionPathSeparator('/')
).
withReaderFactory(() ->
reader()
);

}

private INIReader.Builder reader() {
return new INIReader.Builder().
withMultiValueMode(MultiValueMode.REPEATED_KEY).
withSectionPathSeparator('/');
}
}
Loading

0 comments on commit 1e62341

Please sign in to comment.