Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Add Helix.Software macro #298

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/software/internal/file.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Helix.Software.Internal.File do

alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.Storage
alias Helix.Software.Repo

Expand Down Expand Up @@ -121,7 +120,7 @@ defmodule Helix.Software.Internal.File do
update(file, params)
end

@spec encrypt(File.t, FileModule.version) ::
@spec encrypt(File.t, File.Module.version) ::
{:ok, File.changeset}
| {:error, File.changeset}
def encrypt(file = %File{}, version) when version >= 1 do
Expand Down
41 changes: 21 additions & 20 deletions lib/software/model/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,48 @@ defmodule Helix.Software.Model.File do

alias Ecto.Changeset
alias HELL.Constant
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.SoftwareType
alias Helix.Software.Model.Software
alias Helix.Software.Model.Storage
alias __MODULE__, as: File

@type t :: t_of_type(SoftwareType.type)
@type t :: t_of_type(Software.type)

@type t_of_type(type) :: %__MODULE__{
file_id: id,
name: name,
path: path,
full_path: full_path,
file_size: size,
type: SoftwareType.t,
type: Software.Type.t,
software_type: type,
storage_id: Storage.id,
storage: term,
inserted_at: NaiveDateTime.t,
updated_at: NaiveDateTime.t,
modules: modules | FileModule.schema,
modules: modules | File.Module.schema,
crypto_version: crypto_version
}

@type extension :: String.t
@type path :: String.t
@type full_path :: path
@type name :: String.t
@type size :: pos_integer
@type type :: SoftwareType.type
@type type :: Software.type
@type crypto_version :: nil | pos_integer
@type modules :: FileModule.t
@type modules :: File.Module.t

@type changeset :: %Changeset{data: %__MODULE__{}}

@type creation_params :: %{
name: name,
path: path,
file_size: size,
software_type: SoftwareType.type,
software_type: Software.type,
storage_id: Storage.idtb
}

@type module_params :: {FileModule.name, FileModule.Data.t}
@type module_params :: {File.Module.name, File.Module.Data.t}

@type update_params :: %{
optional(:name) => name,
Expand All @@ -62,7 +63,7 @@ defmodule Helix.Software.Model.File do

@required_fields ~w/name path file_size software_type storage_id/a

@software_types Map.keys(SoftwareType.possible_types())
@software_types Software.Type.all()

schema "files" do
field :file_id, ID,
Expand All @@ -78,16 +79,16 @@ defmodule Helix.Software.Model.File do

field :full_path, :string

belongs_to :type, SoftwareType,
belongs_to :type, Software.Type,
foreign_key: :software_type,
references: :software_type,
references: :type,
define_field: false
belongs_to :storage, Storage,
foreign_key: :storage_id,
references: :storage_id,
define_field: false

has_many :modules, FileModule,
has_many :modules, File.Module,
foreign_key: :file_id,
references: :file_id,
on_replace: :delete
Expand Down Expand Up @@ -118,7 +119,7 @@ defmodule Helix.Software.Model.File do
def format(file) do
formatted_modules =
Enum.reduce(file.modules, %{}, fn module, acc ->
module = FileModule.format(module)
module = File.Module.format(module)
Map.merge(acc, module)
end)

Expand Down Expand Up @@ -157,17 +158,17 @@ defmodule Helix.Software.Model.File do
end

@spec create_module_assoc(module_params) ::
FileModule.changeset
File.Module.changeset
docp """
Helper/wrapper to `FileModule.create_changeset/1`
Helper/wrapper to `File.Module.create_changeset/1`
"""
defp create_module_assoc({name, data}) do
params = %{
name: name,
version: data.version
}

FileModule.create_changeset(params)
File.Module.create_changeset(params)
end

docp """
Expand All @@ -178,7 +179,7 @@ defmodule Helix.Software.Model.File do
path = get_field(changeset, :path)
name = get_field(changeset, :name)
software_type = get_field(changeset, :software_type)
extension = SoftwareType.possible_types()[software_type].extension
extension = Software.Type.get(software_type).extension

full_path = path <> "/" <> name <> "." <> extension

Expand Down Expand Up @@ -250,13 +251,13 @@ defmodule Helix.Software.Model.File do
do: where(query, [f], is_nil(f.crypto_version))

defp join_modules(query),
do: join(query, :left, [f], fm in FileModule, fm.file_id == f.file_id)
do: join(query, :left, [f], fm in File.Module, fm.file_id == f.file_id)

defp join_assoc_modules(query),
do: join(query, :left, [f], fm in assoc(f, :modules))

docp """
Preloads FileModules into the schema
Preloads File.Modules into the schema
"""
defp preload_modules(query),
do: preload(query, [..., m], [modules: m])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Helix.Software.Model.FileModule do
defmodule Helix.Software.Model.File.Module do
@moduledoc """
A FileModule is a component of a File responsible for doing something. It
A File.Module is a component of a File responsible for doing something. It
contains a version, which is a representation of how powerful that module is.

For example, take the Cracker. It may have Overflow and Bruteforce modules.
Expand All @@ -18,11 +18,11 @@ defmodule Helix.Software.Model.FileModule do
alias Ecto.Changeset
alias HELL.Constant
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule.Data, as: FileModuleData
alias Helix.Software.Model.SoftwareModule
alias Helix.Software.Model.Software
alias __MODULE__, as: Module

@type t :: %{
name => FileModuleData.t
name => Module.Data.t
}

@type schema :: %__MODULE__{
Expand Down Expand Up @@ -87,7 +87,7 @@ defmodule Helix.Software.Model.FileModule do
def generic_validations(changeset) do
changeset
|> validate_number(:version, greater_than: 0)
|> validate_inclusion(:name, SoftwareModule.possible_modules())
|> validate_inclusion(:name, Software.Module.all())
end

@spec format(schema) ::
Expand All @@ -96,7 +96,7 @@ defmodule Helix.Software.Model.FileModule do
Formats a FileModule
"""
def format(module = %__MODULE__{}) do
data = FileModuleData.new(module)
data = Module.Data.new(module)

Map.put(%{}, module.name, data)
end
Expand All @@ -106,17 +106,17 @@ defmodule Helix.Software.Model.FileModule do
FileModuleData contains information about the corresponding module.
"""

alias Helix.Software.Model.FileModule
alias Helix.Software.Model.File

@type t ::
%__MODULE__{
version: FileModule.version
version: File.Module.version
}

@enforce_keys [:version]
defstruct [:version]

@spec new(FileModule.schema) ::
@spec new(File.Module.schema) ::
t
def new(%{version: version}) do
%__MODULE__{
Expand All @@ -132,16 +132,15 @@ defmodule Helix.Software.Model.FileModule do
alias Ecto.Queryable
alias HELL.Constant
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule

@spec by_file(Queryable.t, File.idtb) ::
Queryable.t
def by_file(query \\ FileModule, id),
def by_file(query \\ File.Module, id),
do: where(query, [fm], fm.file_id == ^id)

@spec by_name(Queryable.t, Constant.t) ::
Queryable.t
def by_name(query \\ FileModule, name),
def by_name(query \\ File.Module, name),
do: where(query, [fm], fm.name == ^name)
end
end
57 changes: 57 additions & 0 deletions lib/software/model/software.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Helix.Software.Model.Software do

use Helix.Software

software \
type: :cracker,
extension: "crc",
modules: [:bruteforce, :overflow]

software \
type: :firewall,
extension: "fwl",
modules: [:fwl_active, :fwl_passive]

software \
type: :text,
extension: "txt"

software \
type: :exploit,
extension: "exp",
modules: [:ftp, :ssh]

software \
type: :hasher,
extension: "hash",
modules: [:password]

software \
type: :log_forger,
extension: "logf",
modules: [:log_create, :log_edit]

software \
type: :log_recover,
extension: "logr",
modules: [:log_recover]

software \
type: :encryptor,
extension: "enc",
modules: [:enc_file, :enc_log, :enc_connection, :enc_process]

software \
type: :decryptor,
extension: "dec",
modules: [:dec_file, :dec_log, :dec_connection, :dec_process]

software \
type: :anymap,
extension: "map",
modules: [:map_geo, :map_net]

software \
type: :crypto_key,
extension: "key"
end
42 changes: 0 additions & 42 deletions lib/software/model/software_module.ex

This file was deleted.

Loading