Skip to content

Commit

Permalink
Split server sanity checks into root org and other orgs (Velocidex#2052)
Browse files Browse the repository at this point in the history
The sanity checker ensured the server is running in a sane state. It
also starts things like initial server artifacts etc.

This PR split sanity checks to run on the root org (i.e. once when the
server is started) and on each org. For example the initial server
artifacts are only run on the root org and not on other orgs.

Added org rm command
  • Loading branch information
scudette authored Sep 6, 2022
1 parent 80b91d3 commit 791b63f
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 80 deletions.
8 changes: 6 additions & 2 deletions artifacts/definitions/Demo/Plugins/GUI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ sources:
/*
## Adding timelines
Add a timeline from this time series data
Add a timeline from this time series data. (This only works
for root org because it relies on server health events).
*/
SELECT timestamp(epoch=_ts) AS Timestamp, CPUPercent
FROM monitoring(
Expand Down Expand Up @@ -325,9 +327,11 @@ sources:
These apply to notebooks automatically without needing to
define them again.
Hash column should right click to VT
*/
LET ColumnTypes = dict(`StartDate`='timestamp')
SELECT Hex, StartDate
SELECT Hex, StartDate, hash(accessor="data", path="Hello") AS Hash
FROM source()
43 changes: 43 additions & 0 deletions bin/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/json"
logging "www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/startup"
)
Expand All @@ -18,6 +19,9 @@ var (

orgs_create_name = orgs_create.Arg("name", "Name of the new org").Required().String()

orgs_delete = orgs_command.Command("rm", "Delete an org")
orgs_delete_org_id = orgs_delete.Arg("org_id", "Id of org to remove").Required().String()

orgs_user_add = orgs_command.Command("user_add", "Add a user to the org")
orgs_user_add_org = orgs_user_add.Arg("org_id", "Org ID to add user to").
Required().String()
Expand All @@ -33,6 +37,7 @@ func doOrgLs() error {
if err != nil {
return fmt.Errorf("loading config file: %w", err)
}
config_obj.Frontend.ServerServices = services.GenericToolServices()

ctx, cancel := install_sig_handler()
defer cancel()
Expand Down Expand Up @@ -64,6 +69,7 @@ func doOrgUserAdd() error {
if err != nil {
return fmt.Errorf("loading config file: %w", err)
}
config_obj.Frontend.ServerServices = services.GenericToolServices()

ctx, cancel := install_sig_handler()
defer cancel()
Expand Down Expand Up @@ -109,6 +115,8 @@ func doOrgCreate() error {
return fmt.Errorf("loading config file: %w", err)
}

config_obj.Frontend.ServerServices = services.GenericToolServices()

ctx, cancel := install_sig_handler()
defer cancel()

Expand All @@ -134,6 +142,38 @@ func doOrgCreate() error {
return nil
}

func doOrgDelete() error {
config_obj, err := makeDefaultConfigLoader().
WithRequiredFrontend().
WithRequiredUser().
WithRequiredLogging().LoadAndValidate()
if err != nil {
return fmt.Errorf("loading config file: %w", err)
}

config_obj.Frontend.ServerServices = services.GenericToolServices()

ctx, cancel := install_sig_handler()
defer cancel()

sm, err := startup.StartToolServices(ctx, config_obj)
defer sm.Close()

if err != nil {
return err
}

org_manager, err := services.GetOrgManager()
if err != nil {
return err
}

logger := logging.GetLogger(config_obj, &logging.ToolComponent)
logger.Info("Will remove org %v\n", *orgs_delete_org_id)

return org_manager.DeleteOrg(*orgs_delete_org_id)
}

func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
Expand All @@ -143,6 +183,9 @@ func init() {
case orgs_create.FullCommand():
FatalIfError(orgs_create, doOrgCreate)

case orgs_delete.FullCommand():
FatalIfError(orgs_delete, doOrgDelete)

case orgs_user_add.FullCommand():
FatalIfError(orgs_user_add, doOrgUserAdd)

Expand Down
10 changes: 6 additions & 4 deletions datastore/filebased.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func (self *FileBaseDataStore) SetSubjectWithCompletion(
// Make sure to call the completer on all exit points
// (FileBaseDataStore is actually synchronous).
defer func() {
if completion != nil {
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
}()
Expand Down Expand Up @@ -174,7 +175,8 @@ func (self *FileBaseDataStore) DeleteSubjectWithCompletion(
urn api.DSPathSpec, completion func()) error {

err := self.DeleteSubject(config_obj, urn)
if completion != nil {
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}

Expand Down Expand Up @@ -419,8 +421,8 @@ func (self *FileBaseDataStore) SetBuffer(
urn api.DSPathSpec, data []byte, completion func()) error {

err := writeContentToFile(config_obj, urn, data)

if completion != nil {
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
return err
Expand Down
4 changes: 3 additions & 1 deletion datastore/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"www.velocidex.com/golang/velociraptor/file_store/path_specs"
"www.velocidex.com/golang/velociraptor/grpc_client"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/utils"
)

var (
Expand Down Expand Up @@ -147,7 +148,8 @@ func (self *RemoteDataStore) SetSubjectWithCompletion(
// Make sure to always call the completion regardless of error
// paths.
defer func() {
if completion != nil {
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
}()
Expand Down
4 changes: 2 additions & 2 deletions gui/velociraptor/src/components/core/keyboard-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const helpTextCol1 = [
["alt+p", T("Parameters configuration Step")],
["alt+r", T("Collection resource specification")],
["ctrl+l", T("Launch artifact")],
["ctrl+right", T("Go to next step")],
["ctrl+left", T("Go to previous step")],
["ctrl+shift+right", T("Go to next step")],
["ctrl+shift+left", T("Go to previous step")],
]],
];

Expand Down
6 changes: 4 additions & 2 deletions gui/velociraptor/src/components/flows/new-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,10 @@ class NewCollectionWizard extends React.Component {
GOTO_PREVIEW: "alt+v",
GOTO_RESOURCES: "alt+r",
GOTO_LAUNCH: "ctrl+l",
NEXT_STEP: "ctrl+right",
PREV_STEP: "ctrl+left",

// These do not work inside text entries.
NEXT_STEP: "ctrl+shift+right",
PREV_STEP: "ctrl+shift+left",
};
let handlers={
GOTO_ARTIFACTS: this.gotoStep(1),
Expand Down
1 change: 1 addition & 0 deletions gui/velociraptor/src/components/forms/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import BootstrapTable from 'react-bootstrap-table-next';
import cellEditFactory, { Type } from 'react-bootstrap-table2-editor';
import { parseCSV, serializeCSV } from '../utils/csv.js';
import "./validated.css";
import "./forms.css";

const numberRegex = RegExp("^[0-9]+$");

Expand Down
24 changes: 24 additions & 0 deletions gui/velociraptor/src/components/forms/forms.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* react calendar */
.react-calendar {
background: var(--color-calendar-background);
}

.react-calendar .react-calendar__navigation button,
.react-calendar .react-calendar__tile {
color: var(--color-foreground-dimmed);
}

.react-calendar .react-calendar__tile--now {
background: var(--background-calendar-tile-now);
color: var(--color-calendar-tile);
}

.react-calendar .react-calendar__tile--active,
.react-calendar .react-calendar__tile--active:hover {
background: var(--background-calendar-tile-active);
color: var(--color-calendar-tile);
}

.react-calendar .react-calendar__month-view__days__day--weekend {
color: var(--accent-color);
}
5 changes: 5 additions & 0 deletions gui/velociraptor/src/css/_variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
--font-json: roboto-mono, Menlo, Monaco, Consolas, "Courier New", monospace;
--font-monospace: roboto-mono, "Courier New", Courier, monospace;

--color-calendar-background: #f0f0f0;
--background-calendar-tile-now: #feffe0;
--background-calendar-tile-active: var(--color-table-row-selected);
--color-calendar-tile: var(--color-foreground);

--color-canvas-background: #ffffff;
--color-foreground: #000;
--color-foreground-inverse: #fff;
Expand Down
6 changes: 3 additions & 3 deletions gui/velociraptor/src/themes/coolgray-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
--color-timeline-7: #471ecb;

--color-vfs-files-timestomped: #ed5d40;
--color-calendar-background: #121212;
--color-calendar-background: var(--color-canvas-background);
--color-level-error: #ff0000;
}

Expand Down Expand Up @@ -838,7 +838,7 @@ body.coolgray-dark {

/* react timelines */
.coolgray-dark .react-calendar-timeline .rct-sidebar {
color: var(--color-foreground);
color: var(--color-foreground);
}

/* file tree */
Expand Down Expand Up @@ -890,7 +890,7 @@ body.coolgray-dark {

.coolgray-dark .react-datetime-picker__inputGroup__input,
.coolgray-dark .react-datetime-picker__button {
filter: invert(0);
filter: invert(0.7);
}

.coolgray-dark .react-datepicker-popper {
Expand Down
31 changes: 5 additions & 26 deletions gui/velociraptor/src/themes/github-dimmed-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@

--color-vfs-files-timestomped: #ed5d40;
--color-calendar-background: #121212;
--color-calendar-background: var(--color-canvas-background);

--background-calendar-tile-now: #feffe0;

--color-level-error: #aa0000;
}

Expand Down Expand Up @@ -740,19 +744,9 @@ body.github-dimmed-dark {
background: var(--color-accent-50);
}

/* Datepicker */
.github-dimmed-dark .react-datepicker,
.github-dimmed-dark .react-datetime-picker input,
.github-dimmed-dark .react-datetime-picker select,
.github-dimmed-dark .react-datetime-picker button,
.github-dimmed-dark .react-datetime-picker {
background: var(--color-canvas-background);
color: var(--color-foreground);
}

.github-dimmed-dark .react-datetime-picker__inputGroup__input,
.github-dimmed-dark .react-datetime-picker__button {
filter: invert(0);
filter: invert(0.7);
}

.github-dimmed-dark .react-datepicker-popper {
Expand Down Expand Up @@ -926,18 +920,3 @@ input[type="radio"] {
text-shadow: none;
box-shadow: none;
}


/* react calendar */
.github-dimmed-dark .react-calendar {
background: var(--color-calendar-background);
}

.github-dimmed-dark .react-calendar .react-calendar__navigation button,
.github-dimmed-dark .react-calendar .react-calendar__tile {
color: var(--color-foreground-dimmed);
}

.github-dimmed-dark .react-calendar .react-calendar__month-view__days__day--weekend {
color: var(--accent-color);
}
16 changes: 2 additions & 14 deletions gui/velociraptor/src/themes/veloci-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
--color-card-heading-background: #444444;
--color-table-heading-background: #444444;
--color-monospace-color: #eeeeee;

--color-calendar-background: #121212;
--color-calendar-tile: #121212;

--color-timeline-header: #cdcdcd20;
--color-timeline-table-shown: #dd4b3920;
Expand Down Expand Up @@ -601,20 +603,6 @@ body.veloci-dark {
color: var(--color-foreground);
}

/* react calendar */
.veloci-dark .react-calendar {
background: var(--color-calendar-background);
}

.veloci-dark .react-calendar .react-calendar__navigation button,
.veloci-dark .react-calendar .react-calendar__tile {
color: var(--color-foreground-dimmed);
}

.veloci-dark .react-calendar .react-calendar__month-view__days__day--weekend {
color: var(--accent-color);
}

/* file tree */
.veloci-dark .file-tree ul {
background: var(--color-canvas-background);
Expand Down
2 changes: 2 additions & 0 deletions gui/velociraptor/src/themes/veloci-light.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
--color-table-row-hover: rgba(0, 0, 0, 0.01);
--color-code: #990000;

--background-calendar-tile-active: #5cd00a80;

--color-timeline-header: #cdcdcd20;
--color-timeline-table-shown: #5cd00a80;
--color-timeline-1: #dff0d820;
Expand Down
18 changes: 9 additions & 9 deletions services/orgs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ func (self *OrgManager) startRootOrgServices(

// The user manager is global across all orgs.
if spec.UserManager {
err := users.StartUserManager(
ctx, wg, org_config)
err := users.StartUserManager(ctx, wg, org_config)
if err != nil {
return err
}
Expand Down Expand Up @@ -536,13 +535,6 @@ func (self *OrgManager) startOrgFromContext(org_ctx *OrgContext) (err error) {
service_container.mu.Unlock()
}

if spec.SanityChecker {
err = sanity.NewSanityCheckService(ctx, wg, org_config)
if err != nil {
return err
}
}

if spec.ServerArtifacts {
err = server_artifacts.NewServerArtifactService(ctx, wg, org_config)
if err != nil {
Expand Down Expand Up @@ -572,6 +564,14 @@ func (self *OrgManager) startOrgFromContext(org_ctx *OrgContext) (err error) {
service_container.mu.Unlock()
}

// Must be run after all the other services are up
if spec.SanityChecker {
err = sanity.NewSanityCheckService(ctx, wg, org_config)
if err != nil {
return err
}
}

return maybeFlushFilesOnClose(ctx, wg, org_config)
}

Expand Down
Loading

0 comments on commit 791b63f

Please sign in to comment.