Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CTF
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grzegorz
CTF
Commits
012cb848
Commit
012cb848
authored
Mar 09, 2016
by
Grzegorz Pietrusza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
purge db and register tasks comands
parent
cde37948
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
121 additions
and
66 deletions
+121
-66
CTFApplication.java
service/src/main/java/CTFApplication.java
+4
-21
PurgeDatabaseCommand.java
service/src/main/java/commands/PurgeDatabaseCommand.java
+5
-4
RegisterTasksCommand.java
service/src/main/java/commands/RegisterTasksCommand.java
+80
-0
ApplicationModule.java
service/src/main/java/modules/ApplicationModule.java
+32
-0
TasksRepository.java
service/src/main/java/repositories/TasksRepository.java
+0
-41
No files found.
service/src/main/java/CTFApplication.java
View file @
012cb848
import
auth.ExampleAuthenticator
;
import
com.bazaarvoice.dropwizard.webjars.WebJarBundle
;
import
com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml
;
import
com.google.inject.AbstractModule
;
import
com.google.inject.Guice
;
import
com.google.inject.Injector
;
import
com.mongodb.MongoClient
;
import
commands.PurgeDatabaseCommand
;
import
commands.RegisterTasksCommand
;
import
core.ApplicationConfiguration
;
import
io.dropwizard.Application
;
import
io.dropwizard.assets.AssetsBundle
;
...
...
@@ -16,14 +16,12 @@ import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import
io.dropwizard.jersey.setup.JerseyEnvironment
;
import
io.dropwizard.setup.Bootstrap
;
import
io.dropwizard.setup.Environment
;
import
modules.ApplicationModule
;
import
objects.User
;
import
org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature
;
import
org.mongodb.morphia.Datastore
;
import
org.mongodb.morphia.Morphia
;
import
repositories.UsersRepository
;
import
resources.*
;
import
java.util.List
;
import
java.util.stream.Stream
;
/**
...
...
@@ -31,8 +29,6 @@ import java.util.stream.Stream;
*/
public
class
CTFApplication
extends
Application
<
ApplicationConfiguration
>
{
private
Datastore
datastore
;
private
Injector
injector
;
@Override
...
...
@@ -40,6 +36,7 @@ public class CTFApplication extends Application<ApplicationConfiguration>
{
bootstrap
.
addBundle
(
new
AssetsBundle
(
"/assets"
,
"/page"
,
"index.html"
));
bootstrap
.
addCommand
(
new
PurgeDatabaseCommand
());
bootstrap
.
addCommand
(
new
RegisterTasksCommand
());
Stream
.
of
(
"org.webjars.npm"
,
"org.webjars.bower"
)
.
map
(
WebJarBundle:
:
new
)
.
forEach
(
bootstrap:
:
addBundle
);
...
...
@@ -95,21 +92,7 @@ public class CTFApplication extends Application<ApplicationConfiguration>
private
Injector
createInjector
(
ApplicationConfiguration
applicationConfiguration
)
{
return
Guice
.
createInjector
(
new
AbstractModule
()
{
@Override
protected
void
configure
()
{
bind
(
ApplicationConfiguration
.
class
).
toInstance
(
applicationConfiguration
);
//todo: refactor
Morphia
morphia
=
new
Morphia
();
morphia
.
mapPackage
(
"objects.Team"
);
//todo? what for?
datastore
=
morphia
.
createDatastore
(
new
MongoClient
(
applicationConfiguration
.
getDbHost
(),
applicationConfiguration
.
getDbPort
()),
applicationConfiguration
.
getDbName
());
datastore
.
ensureIndexes
();
bind
(
Datastore
.
class
).
toInstance
(
datastore
);
}
});
return
Guice
.
createInjector
(
new
ApplicationModule
(
applicationConfiguration
));
}
public
static
void
main
(
String
[]
args
)
throws
Exception
...
...
service/src/main/java/commands/PurgeDatabaseCommand.java
View file @
012cb848
package
commands
;
import
com.google.inject.Guice
;
import
com.google.inject.Injector
;
import
com.mongodb.MongoClient
;
import
core.ApplicationConfiguration
;
import
io.dropwizard.cli.ConfiguredCommand
;
import
io.dropwizard.setup.Bootstrap
;
import
modules.ApplicationModule
;
import
net.sourceforge.argparse4j.inf.Namespace
;
import
org.mongodb.morphia.Datastore
;
import
org.mongodb.morphia.Morphia
;
...
...
@@ -21,10 +24,8 @@ public class PurgeDatabaseCommand extends ConfiguredCommand<ApplicationConfigura
@Override
protected
void
run
(
Bootstrap
<
ApplicationConfiguration
>
bootstrap
,
Namespace
namespace
,
ApplicationConfiguration
applicationConfiguration
)
throws
Exception
{
//todo: move and merge with app setup
Morphia
morphia
=
new
Morphia
();
Datastore
datastore
=
morphia
.
createDatastore
(
new
MongoClient
(
applicationConfiguration
.
getDbHost
(),
applicationConfiguration
.
getDbPort
()),
applicationConfiguration
.
getDbName
());
Injector
injector
=
Guice
.
createInjector
(
new
ApplicationModule
(
applicationConfiguration
));
Datastore
datastore
=
injector
.
getInstance
(
Datastore
.
class
);
datastore
.
getDB
().
dropDatabase
();
}
}
service/src/main/java/commands/RegisterTasksCommand.java
0 → 100644
View file @
012cb848
package
commands
;
import
com.google.inject.Guice
;
import
com.google.inject.Injector
;
import
com.mongodb.MongoClient
;
import
core.ApplicationConfiguration
;
import
core.TaskType
;
import
helpers.TextTaskConfig
;
import
helpers.WebTaskConfig
;
import
io.dropwizard.cli.ConfiguredCommand
;
import
io.dropwizard.setup.Bootstrap
;
import
modules.ApplicationModule
;
import
net.sourceforge.argparse4j.inf.Namespace
;
import
objects.Flag
;
import
objects.tasks.Task
;
import
objects.tasks.TaskCryptoContent
;
import
objects.tasks.TaskWebContent
;
import
org.mongodb.morphia.Datastore
;
import
org.mongodb.morphia.Morphia
;
import
repositories.TasksRepository
;
import
java.util.List
;
import
java.util.stream.Collectors
;
public
class
RegisterTasksCommand
extends
ConfiguredCommand
<
ApplicationConfiguration
>
{
private
static
final
String
COMMAND_NAME
=
"registertasks"
;
private
static
final
String
COMMAND_DESCRIPTION
=
"Register tasks"
;
public
RegisterTasksCommand
()
{
super
(
COMMAND_NAME
,
COMMAND_DESCRIPTION
);
}
private
void
initializeTasks
(
ApplicationConfiguration
applicationConfiguration
,
Injector
injector
)
{
Datastore
datastore
=
injector
.
getInstance
(
Datastore
.
class
);
TasksRepository
tasksRepository
=
injector
.
getInstance
(
TasksRepository
.
class
);
List
<
TextTaskConfig
>
cryptoTasks
=
applicationConfiguration
.
getTextTasks
();
List
<
WebTaskConfig
>
webTasks
=
applicationConfiguration
.
getWebTasks
();
datastore
.
getCollection
(
Task
.
class
).
drop
();
cryptoTasks
.
forEach
(
cryptoTaskConfig
->
{
cryptoTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
());
tasksRepository
.
add
(
new
Task
(
cryptoTaskConfig
.
getName
(),
cryptoTaskConfig
.
getLevel
(),
TaskType
.
TEXT
,
cryptoTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
()),
new
TaskCryptoContent
(
cryptoTaskConfig
.
getText
())
));
});
webTasks
.
forEach
(
webTaskConfig
->
tasksRepository
.
add
(
new
Task
(
webTaskConfig
.
getName
(),
webTaskConfig
.
getLevel
(),
TaskType
.
WEB
,
webTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
()),
new
TaskWebContent
(
webTaskConfig
.
getUrl
())
)));
}
@Override
protected
void
run
(
Bootstrap
<
ApplicationConfiguration
>
bootstrap
,
Namespace
namespace
,
ApplicationConfiguration
applicationConfiguration
)
throws
Exception
{
//todo: move and merge with app setup
Injector
injector
=
Guice
.
createInjector
(
new
ApplicationModule
(
applicationConfiguration
));
initializeTasks
(
applicationConfiguration
,
injector
);
}
}
service/src/main/java/modules/ApplicationModule.java
0 → 100644
View file @
012cb848
package
modules
;
import
com.google.inject.AbstractModule
;
import
com.mongodb.MongoClient
;
import
core.ApplicationConfiguration
;
import
org.mongodb.morphia.Datastore
;
import
org.mongodb.morphia.Morphia
;
public
class
ApplicationModule
extends
AbstractModule
{
private
ApplicationConfiguration
applicationConfiguration
;
public
ApplicationModule
(
ApplicationConfiguration
applicationConfiguration
)
{
this
.
applicationConfiguration
=
applicationConfiguration
;
}
@Override
protected
void
configure
()
{
bind
(
ApplicationConfiguration
.
class
).
toInstance
(
applicationConfiguration
);
//todo: refactor
Morphia
morphia
=
new
Morphia
();
morphia
.
mapPackage
(
"objects.Team"
);
//todo? what for?
//todo: instantiate somewhere else?
Datastore
datastore
=
morphia
.
createDatastore
(
new
MongoClient
(
applicationConfiguration
.
getDbHost
(),
applicationConfiguration
.
getDbPort
()),
applicationConfiguration
.
getDbName
());
datastore
.
ensureIndexes
();
bind
(
Datastore
.
class
).
toInstance
(
datastore
);
}
}
service/src/main/java/repositories/TasksRepository.java
View file @
012cb848
...
...
@@ -72,52 +72,11 @@ public class TasksRepository implements Repository
return
datastore
.
createQuery
(
Task
.
class
).
asList
();
}
//todo: rethink, move to command?
public
void
initialize
()
{
List
<
TextTaskConfig
>
cryptoTasks
=
applicationConfiguration
.
getTextTasks
();
List
<
WebTaskConfig
>
webTasks
=
applicationConfiguration
.
getWebTasks
();
this
.
clean
();
cryptoTasks
.
forEach
(
cryptoTaskConfig
->
{
cryptoTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
());
this
.
add
(
new
Task
(
cryptoTaskConfig
.
getName
(),
cryptoTaskConfig
.
getLevel
(),
TaskType
.
TEXT
,
cryptoTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
()),
new
TaskCryptoContent
(
cryptoTaskConfig
.
getText
())
));
});
webTasks
.
forEach
(
webTaskConfig
->
this
.
add
(
new
Task
(
webTaskConfig
.
getName
(),
webTaskConfig
.
getLevel
(),
TaskType
.
WEB
,
webTaskConfig
.
getFlags
().
stream
()
.
map
(
Flag:
:
new
)
.
collect
(
Collectors
.
toList
()),
new
TaskWebContent
(
webTaskConfig
.
getUrl
())
)));
}
public
void
add
(
Task
task
)
{
datastore
.
save
(
task
);
}
public
void
clean
()
{
datastore
.
getCollection
(
Task
.
class
).
drop
();
}
//todo: refactor?
public
Map
<
List
<
String
>,
Integer
>
getUserFlagsHashes
(
String
username
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment