Skip to content

Commit

Permalink
added authorization management/logging, updated service configuration…
Browse files Browse the repository at this point in the history
…, new release
  • Loading branch information
gabriel-aires committed Feb 11, 2022
1 parent 12a2ba3 commit 2162da9
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 25 deletions.
Binary file modified dist/ecmo-linux-x64.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions src/constants.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module App

DB_RETENTION = (ENV["DB_RETENTION"]? || 7).to_i

ALLOW_READ = ENV["ALLOW_READ"]? || ""
ALLOW_WRITE = ENV["ALLOW_WRITE"]? || "wheel"

COOKIE_SESSION_KEY = ENV["SESSION_KEY"]? || "_ecmo_"
COOKIE_SESSION_SECRET = ENV["SESSION_SECRET"]? || "4f74c0b358d5bab4000dd3c75465dc2c"

Expand Down
36 changes: 34 additions & 2 deletions src/controllers/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ require "uuid"

abstract class Application < ActionController::Base

@title : String?
@description : String?
@alert : String?

force_ssl
layout "layout.slang"
Log = ::App::Log.for("controller")
Expand All @@ -24,11 +28,39 @@ abstract class Application < ActionController::Base
end

def require_read
true
@alert = "Read access required"
authorize_groups App::ALLOW_WRITE + " " + App::ALLOW_READ
end

def require_write
false
@alert = "Write access required"
authorize_groups App::ALLOW_WRITE
end

def authorize_groups(group_names : String)
user = current_user.not_nil!
route = self.class.name + "#" + action_name.to_s
level = @alert.to_s
granted = group_names.strip
time = Time.utc
perm = "#{level} for #{route} | Groups allowed: #{granted}."

if (user.groups & granted.split(" ")).empty?
tone :warn
theme :night

notice "Access denied for user '#{user.name}'"
puts "#{time} | #{user.name} blocked. | #{perm}"

respond_with do
html template("unauthorized.slang")
end

else
puts "#{time} | #{user.name} granted. | #{perm}"

end

end

end
4 changes: 2 additions & 2 deletions src/controllers/dashboard.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Dashboard < Application

@title : String = "Dashboard"
@description : String = "General System Metrics"
@title = "Dashboard"
@description = "General System Metrics"

rescue_from DB::ConnectionRefused, :db_error
rescue_from NilAssertionError, :null_error
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/home.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Home < Application

@title : String = App::NAME
@description : String = App::DESC
@title = App::NAME
@description = App::DESC

base "/"

Expand Down
38 changes: 23 additions & 15 deletions src/controllers/jobs.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Jobs < Application

@title : String = "Jobs"
@description : String = "System Configuration"
@title = "Jobs"
@description = "System Configuration"

before_action :set_theme

Expand Down Expand Up @@ -35,6 +35,11 @@ class Jobs < Application

def show
job = Job.find(params["id"]).not_nil!
job_report job
end

def replace
job = Job.find(params["id"]).not_nil!

if job.cron == "on-demand"
mitamae = App::ROOT + "/bin/mitamae"
Expand Down Expand Up @@ -65,29 +70,32 @@ class Jobs < Application
success: success,
job_id: job.id

log = last_run job.id
else
log = last_run job.id
job_report job
end

respond_with do
html template("job_report.slang")
json({job: job.to_json, log: log})
end
end

private def last_run(job_id)
last = {:output => "", :error => nil, :duration => 0_i64, :success => false}
log = {:output => "", :error => nil, :duration => 0_i64, :success => false}

Run.where(job_id: job_id).order(seconds: :desc).each do |run|
last[:output] = run.output
last[:error] = run.error
last[:duration] = run.duration
last[:success] = run.success
log[:output] = run.output
log[:error] = run.error
log[:duration] = run.duration
log[:success] = run.success
break
end

last
log
end

private def job_report(job)
log = last_run(job.id)
log[:success] ? (tone :success) : (tone :error)
respond_with do
html template("job_report.slang")
json({job: job.to_json, log: log})
end
end

end
12 changes: 11 additions & 1 deletion src/views/jobs.slang
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ h4 On-Demand Jobs
tr
th Log level
td= job.log.to_s
tr
th Last Run
- if last_run(job.id)[:success]
td
a href=Jobs.show(id:job.id)
ins View Log
- else
td
a href=Jobs.show(id:job.id)
del View Log
tr
th Run job
td
a href=Jobs.show(id:job.id) role="button" Start!
a.btn.solid.black hx-put=Jobs.replace(id:job.id) hx-target="body" Start!

br
1 change: 1 addition & 0 deletions src/views/unauthorized.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h2= @alert
4 changes: 3 additions & 1 deletion vfs/setup/service/ecmo.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ MODE="production"
SERVER_PORT=3000
SERVER_HOST="127.0.0.1"
DB_RETENTION=7
SESSION_KEY="_os_probe_"
SESSION_KEY="_ecmo_"
SESSION_SECRET="4f74c0b358d5bab4000dd3c75465dc2c"
ALLOW_READ=""
ALLOW_WRITE="wheel"
2 changes: 1 addition & 1 deletion vfs/setup/service/ecmo.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ After=network.target remote-fs.target

[Service]
EnvironmentFile=/opt/ecmo/ecmo.conf
PassEnvironment=MODE SERVER_PORT SERVER_HOST DB_RETENTION SESSION_KEY SESSION_SECRET
PassEnvironment=MODE SERVER_PORT SERVER_HOST DB_RETENTION SESSION_KEY SESSION_SECRET ALLOW_READ ALLOW_WRITE
ExecStart=/opt/ecmo/bin/ecmo
ExecStop=/bin/kill -s TERM $MAINPID
User=root
Expand Down
2 changes: 1 addition & 1 deletion vfs/setup/service/ecmo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ output_log="/opt/ecmo/service.log"
error_log="/opt/ecmo/error.log"

start_pre() {
export MODE SERVER_PORT SERVER_HOST DB_RETENTION SESSION_KEY SESSION_SECRET
export MODE SERVER_PORT SERVER_HOST DB_RETENTION SESSION_KEY SESSION_SECRET ALLOW_READ ALLOW_WRITE
}

depend() {
Expand Down

0 comments on commit 2162da9

Please sign in to comment.