[Closes #3] Crypto added to core

master
Edward M. Kagan 5 years ago
parent f06d2ebcb8
commit 50cf58da9a

@ -0,0 +1,13 @@
version: "3"
services:
postgres:
image: postgres:13-alpine
volumes:
- ../postgres/:/var/lib/postgresql/data
environment:
- POSTGRES_INITDB_ARGS=--encoding=UTF8 --locale=en_US.UTF-8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 --lc-messages=en_US.UTF-8 --lc-monetary=en_US.UTF-8 --lc-numeric=en_US.UTF-8 --lc-time=en_US.UTF-8
- POSTGRES_USER=traqtor
- POSTGRES_PASSWORD=traqtor
ports:
- 10240:5432

@ -0,0 +1,2 @@
#!/bin/bash
CURRENT_UID=$(id -u):$(id -g) docker-compose -f ./.dev/docker/dev-env.yml -p traqtor --compatibility up

@ -55,11 +55,17 @@
<artifactId>cayenne-server</artifactId> <artifactId>cayenne-server</artifactId>
<version>4.1</version> <version>4.1</version>
</dependency> </dependency>
<!-- <dependency> <dependency>
<groupId>org.apache.cayenne</groupId> <groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-crypto</artifactId> <artifactId>cayenne-crypto</artifactId>
<version>4.1</version> <version>4.1</version>
</dependency> --> </dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
</dependency>
<!-- <dependency> <!-- <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>

@ -1,53 +1,69 @@
package two.pm.traqtor; package two.pm.traqtor;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.apache.cayenne.ObjectContext; import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.configuration.Constants; import org.apache.cayenne.configuration.Constants;
import org.apache.cayenne.configuration.server.ServerRuntime; import org.apache.cayenne.configuration.server.ServerRuntime;
import org.apache.cayenne.crypto.CryptoModule;
import org.apache.cayenne.di.Key; import org.apache.cayenne.di.Key;
import org.apache.cayenne.log.JdbcEventLogger;
import org.apache.cayenne.log.NoopJdbcEventLogger;
import org.apache.cayenne.query.SQLExec; import org.apache.cayenne.query.SQLExec;
import org.apache.cayenne.resource.ResourceLocator; import org.apache.cayenne.resource.ResourceLocator;
@ApplicationScoped @ApplicationScoped
public class CayenneRuntimeFactory { public class CayenneRuntimeFactory {
private static ServerRuntime runtime; private static ServerRuntime runtime;
@Singleton @Singleton
public ServerRuntime cayenneRuntime() { public ServerRuntime cayenneRuntime() {
if (runtime == null) { if (runtime == null) {
runtime = ServerRuntime.builder().addConfig("db/cayenne-traqtor.xml")
.addModule(binder -> binder.bind(JdbcEventLogger.class).to(NoopJdbcEventLogger.class)) // ClassLoader classLoader = ;
.addModule(binder -> { // File file = new File(classLoader.getFile());
binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocatorFix.class); // System.out.println(file.getAbsolutePath());
binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR))
.to(ClassLoaderResourceLocatorFix.class);
}).build();
ObjectContext context = runtime.newContext();
SQLExec.query(
"CREATE TABLE IF NOT EXISTS user (email VARCHAR(36) NULL, id VARBINARY(36) NOT NULL, PRIMARY KEY (id));")
.execute(context);
SQLExec.query(
"CREATE SEQUENCE IF NOT EXISTS pk_user START WITH -9223372036854775808 INCREMENT BY 20 CACHE 1;")
.execute(context);
context.commitChanges();
}
return runtime; runtime = ServerRuntime.builder().addConfig("db/cayenne-traqtor.xml")
// return // .addModule(binder ->
// ServerRuntime.builder().dataSource(dataSource).addConfig("db/cayenne-test.xml").addModule(binder // binder.bind(JdbcEventLogger.class).to(NoopJdbcEventLogger.class))
// -> { .addModule(binder -> {
// // binder.bind(ResourceLocator.class)
// binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocatorFix.class); .to(ClassLoaderResourceLocatorFix.class);
// // binder.bind(Key.get(ResourceLocator.class, binder.bind(Key.get(ResourceLocator.class,
// Constants.SERVER_RESOURCE_LOCATOR)) Constants.SERVER_RESOURCE_LOCATOR))
// // .to(ClassLoaderResourceLocatorFix.class); .to(ClassLoaderResourceLocatorFix.class);
// }).build(); })
} .addModule(CryptoModule.extend()
.keyStore(this.getClass().getClassLoader()
.getResource("db/traqtor.keystore"),
"secret".toCharArray(), "key0")
.compress()
// .useHMAC()
.module())
.build();
ObjectContext context = runtime.newContext();
SQLExec.query("CREATE TABLE IF NOT EXISTS system_user (id bigint NOT NULL, CRYPTO_password varchar(2048) NOT NULL, CRYPTO_email varchar(2048) NOT NULL, PRIMARY KEY (id));"
+ "CREATE SEQUENCE IF NOT EXISTS pk_system_user INCREMENT 20 MINVALUE -9223372036854775807;")
.execute(context);
context.commitChanges();
}
return runtime;
// return
// ServerRuntime.builder().dataSource(dataSource).addConfig("db/cayenne-test.xml").addModule(binder
// -> {
// //
// binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocatorFix.class);
// // binder.bind(Key.get(ResourceLocator.class,
// Constants.SERVER_RESOURCE_LOCATOR))
// // .to(ClassLoaderResourceLocatorFix.class);
// }).build();
}
} }

@ -1,45 +1,12 @@
package two.pm.traqtor; package two.pm.traqtor;
import io.quarkus.runtime.annotations.QuarkusMain; import io.quarkus.runtime.annotations.QuarkusMain;
import javax.inject.Inject;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.configuration.server.ServerRuntime;
import org.apache.cayenne.log.JdbcEventLogger;
import org.apache.cayenne.log.NoopJdbcEventLogger;
import org.apache.cayenne.query.SQLExec;
import io.quarkus.runtime.Quarkus; import io.quarkus.runtime.Quarkus;
@QuarkusMain @QuarkusMain
public class TraQtor { public class TraQtor {
// public static ServerRuntime cayenneRuntime;
public static void main(String... args) { public static void main(String... args) {
// if (cayenneRuntime == null) {
// .performInTransaction(() -> {
// // ... do some changes
// context.commitChanges();
// // ... do more changes
// context.commitChanges();
// return true;
// });
// Module cryptoExtensions = CryptoModule.extend()
// .keyStore("file:///mykeystore", "keystorepassword".toCharArray(), "keyalias")
// .compress()
// .module();
// cayenneRuntime =
// ServerRuntime.builder().addConfig("cayenne/cayenne-traqtor.xml")
// .addModule(binder ->
// binder.bind(JdbcEventLogger.class).to(NoopJdbcEventLogger.class)).build();
// }
System.out.println("Running main method"); System.out.println("Running main method");
Quarkus.run(args); Quarkus.run(args);
} }

@ -1,47 +1,34 @@
package two.pm.traqtor.api.test; package two.pm.traqtor.api.test;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.stream.Collector;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.apache.cayenne.BaseContext; import org.apache.cayenne.BaseContext;
import org.apache.cayenne.Cayenne;
import org.apache.cayenne.ObjectContext; import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.query.ObjectSelect; import org.apache.cayenne.query.ObjectSelect;
import two.pm.traqtor.db.SystemUser;
import two.pm.traqtor.TraQtor;
import two.pm.traqtor.db.User;
import two.pm.traqtor.db.User.DTO;
@Path("/hello") @Path("/hello")
public class HelloResource { public class HelloResource {
private List<DTO> result;
@GET @GET
@Produces(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN)
public String hello() { public String hello() {
ObjectContext context = BaseContext.getThreadObjectContext(); ObjectContext context = BaseContext.getThreadObjectContext();
// ObjectContext context = TraQtor.cayenneRuntime.newContext(); return String.valueOf(ObjectSelect.query(SystemUser.class).selectCount(context));
return String.valueOf(ObjectSelect.query(User.class).selectCount(context));
} }
@GET @GET
@Path("/list") @Path("/list")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public List<User.DTO> list() { public List<SystemUser.DTO> list() {
ObjectContext context = BaseContext.getThreadObjectContext(); ObjectContext context = BaseContext.getThreadObjectContext();
List<User> users = ObjectSelect.query(User.class).select(context); List<SystemUser> users = ObjectSelect.query(SystemUser.class).select(context);
return users.stream().map(user -> new User.DTO(Cayenne.pkForObject(user).toString(), user.getEmail())) return users.stream().map(user -> user.dto()).collect(Collectors.toList());
.collect(Collectors.toList());
} }
@GET @GET
@ -49,8 +36,9 @@ public class HelloResource {
@Produces(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN)
public String add(@PathParam("email") String email) { public String add(@PathParam("email") String email) {
ObjectContext context = BaseContext.getThreadObjectContext(); ObjectContext context = BaseContext.getThreadObjectContext();
User user = context.newObject(User.class); SystemUser user = context.newObject(SystemUser.class);
user.setEmail(email); user.setEmail(email);
user.setPassword("test");
context.commitChanges(); context.commitChanges();
return hello(); return hello();
} }

@ -0,0 +1,30 @@
package two.pm.traqtor.db;
import java.nio.charset.StandardCharsets;
import org.apache.cayenne.Cayenne;
import two.pm.traqtor.db.auto._SystemUser;
public class SystemUser extends _SystemUser {
private static final long serialVersionUID = 1L;
public DTO dto() {
return new DTO(Cayenne.longPKForObject(this), email, password);
}
public final class DTO {
public String email;
public String password;
public String id;
public DTO(long id, String email, String password) {
this.id = Long.toHexString(id);
this.email = email;
this.password = password;
}
}
}

@ -1,21 +0,0 @@
package two.pm.traqtor.db;
import two.pm.traqtor.db.auto._User;
public class User extends _User {
private static final long serialVersionUID = 1L;
public static class DTO {
public String email;
public String id;
public DTO(String id, String email) {
this.id = id;
this.email = email;
}
}
}

@ -8,20 +8,22 @@ import org.apache.cayenne.BaseDataObject;
import org.apache.cayenne.exp.Property; import org.apache.cayenne.exp.Property;
/** /**
* Class _User was generated by Cayenne. * Class _SystemUser was generated by Cayenne.
* It is probably a good idea to avoid changing this class manually, * It is probably a good idea to avoid changing this class manually,
* since it may be overwritten next time code is regenerated. * since it may be overwritten next time code is regenerated.
* If you need to make any customizations, please use subclass. * If you need to make any customizations, please use subclass.
*/ */
public abstract class _User extends BaseDataObject { public abstract class _SystemUser extends BaseDataObject {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final String ID_PK_COLUMN = "id"; public static final String ID_PK_COLUMN = "id";
public static final Property<String> EMAIL = Property.create("email", String.class); public static final Property<String> EMAIL = Property.create("email", String.class);
public static final Property<String> PASSWORD = Property.create("password", String.class);
protected String email; protected String email;
protected String password;
public void setEmail(String email) { public void setEmail(String email) {
@ -34,6 +36,16 @@ public abstract class _User extends BaseDataObject {
return this.email; return this.email;
} }
public void setPassword(String password) {
beforePropertyWrite("password", this.password, password);
this.password = password;
}
public String getPassword() {
beforePropertyRead("password");
return this.password;
}
@Override @Override
public Object readPropertyDirectly(String propName) { public Object readPropertyDirectly(String propName) {
if(propName == null) { if(propName == null) {
@ -43,6 +55,8 @@ public abstract class _User extends BaseDataObject {
switch(propName) { switch(propName) {
case "email": case "email":
return this.email; return this.email;
case "password":
return this.password;
default: default:
return super.readPropertyDirectly(propName); return super.readPropertyDirectly(propName);
} }
@ -58,6 +72,9 @@ public abstract class _User extends BaseDataObject {
case "email": case "email":
this.email = (String)val; this.email = (String)val;
break; break;
case "password":
this.password = (String)val;
break;
default: default:
super.writePropertyDirectly(propName, val); super.writePropertyDirectly(propName, val);
} }
@ -75,12 +92,14 @@ public abstract class _User extends BaseDataObject {
protected void writeState(ObjectOutputStream out) throws IOException { protected void writeState(ObjectOutputStream out) throws IOException {
super.writeState(out); super.writeState(out);
out.writeObject(this.email); out.writeObject(this.email);
out.writeObject(this.password);
} }
@Override @Override
protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException { protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException {
super.readState(in); super.readState(in);
this.email = (String)in.readObject(); this.email = (String)in.readObject();
this.password = (String)in.readObject();
} }
} }

@ -9,8 +9,8 @@
schema-update-strategy="org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy"> schema-update-strategy="org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy">
<map-ref name="datamap"/> <map-ref name="datamap"/>
<data-source> <data-source>
<driver value="org.h2.Driver"/> <driver value="org.postgresql.Driver"/>
<url value="jdbc:h2:mem:traqtor;MVCC=TRUE"/> <url value="jdbc:postgresql://localhost:10240/traqtor"/>
<connectionPool min="1" max="1"/> <connectionPool min="1" max="1"/>
<login userName="traqtor" password="traqtor"/> <login userName="traqtor" password="traqtor"/>
</data-source> </data-source>

@ -4,12 +4,14 @@
xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap https://cayenne.apache.org/schema/10/modelMap.xsd" xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap https://cayenne.apache.org/schema/10/modelMap.xsd"
project-version="10"> project-version="10">
<property name="defaultPackage" value="two.pm.traqtor.db"/> <property name="defaultPackage" value="two.pm.traqtor.db"/>
<db-entity name="user"> <db-entity name="system_user">
<db-attribute name="email" type="VARCHAR" length="36"/> <db-attribute name="CRYPTO_email" type="VARCHAR" isMandatory="true" length="2048"/>
<db-attribute name="id" type="BIGINT" isPrimaryKey="true" isMandatory="true" length="36"/> <db-attribute name="CRYPTO_password" type="VARCHAR" isMandatory="true" length="2048"/>
<db-attribute name="id" type="BIGINT" isPrimaryKey="true" isMandatory="true"/>
</db-entity> </db-entity>
<obj-entity name="User" className="two.pm.traqtor.db.User" dbEntityName="user"> <obj-entity name="SystemUser" className="two.pm.traqtor.db.SystemUser" dbEntityName="system_user">
<obj-attribute name="email" type="java.lang.String" db-attribute-path="email"/> <obj-attribute name="email" type="java.lang.String" db-attribute-path="CRYPTO_email"/>
<obj-attribute name="password" type="java.lang.String" db-attribute-path="CRYPTO_password"/>
</obj-entity> </obj-entity>
<cgen xmlns="http://cayenne.apache.org/schema/10/cgen"> <cgen xmlns="http://cayenne.apache.org/schema/10/cgen">
<destDir>../../java</destDir> <destDir>../../java</destDir>

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<graphs xmlns="http://cayenne.apache.org/schema/10/graph"> <graphs xmlns="http://cayenne.apache.org/schema/10/graph">
<graph type="ER" scale="1.0"> <graph type="ER" scale="1.0">
<entity name="user" x="23.0" y="20.0" width="35.0" height="62.0"/> <entity name="system_user" x="23.0" y="20.0" width="115.0" height="77.0"/>
</graph> </graph>
</graphs> </graphs>

Loading…
Cancel
Save