mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
Rename inmem method receivers for consistency (#654)
This makes the inmem method receiver naming consistent with mysql. It also eliminates potential confusion with the phrase "orm".
This commit is contained in:
parent
41120ebc00
commit
d6765377c0
12 changed files with 453 additions and 453 deletions
|
|
@ -5,30 +5,30 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewAppConfig(info *kolide.AppConfig) (*kolide.AppConfig, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewAppConfig(info *kolide.AppConfig) (*kolide.AppConfig, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
info.ID = 1
|
||||
orm.orginfo = info
|
||||
d.orginfo = info
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) AppConfig() (*kolide.AppConfig, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) AppConfig() (*kolide.AppConfig, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if orm.orginfo != nil {
|
||||
return orm.orginfo, nil
|
||||
if d.orginfo != nil {
|
||||
return d.orginfo, nil
|
||||
}
|
||||
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveAppConfig(info *kolide.AppConfig) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveAppConfig(info *kolide.AppConfig) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
orm.orginfo = info
|
||||
d.orginfo = info
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,21 +8,21 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewDistributedQueryCampaign(camp *kolide.DistributedQueryCampaign) (*kolide.DistributedQueryCampaign, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewDistributedQueryCampaign(camp *kolide.DistributedQueryCampaign) (*kolide.DistributedQueryCampaign, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
camp.ID = orm.nextID(camp)
|
||||
orm.distributedQueryCampaigns[camp.ID] = *camp
|
||||
camp.ID = d.nextID(camp)
|
||||
d.distributedQueryCampaigns[camp.ID] = *camp
|
||||
|
||||
return camp, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DistributedQueryCampaign(id uint) (*kolide.DistributedQueryCampaign, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DistributedQueryCampaign(id uint) (*kolide.DistributedQueryCampaign, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
campaign, ok := orm.distributedQueryCampaigns[id]
|
||||
campaign, ok := d.distributedQueryCampaigns[id]
|
||||
if !ok {
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
|
@ -30,25 +30,25 @@ func (orm *Datastore) DistributedQueryCampaign(id uint) (*kolide.DistributedQuer
|
|||
return &campaign, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveDistributedQueryCampaign(camp *kolide.DistributedQueryCampaign) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveDistributedQueryCampaign(camp *kolide.DistributedQueryCampaign) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.distributedQueryCampaigns[camp.ID]; !ok {
|
||||
if _, ok := d.distributedQueryCampaigns[camp.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.distributedQueryCampaigns[camp.ID] = *camp
|
||||
d.distributedQueryCampaigns[camp.ID] = *camp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DistributedQueryCampaignTargetIDs(id uint) (hostIDs []uint, labelIDs []uint, err error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DistributedQueryCampaignTargetIDs(id uint) (hostIDs []uint, labelIDs []uint, err error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
hostIDs = []uint{}
|
||||
labelIDs = []uint{}
|
||||
for _, target := range orm.distributedQueryCampaignTargets {
|
||||
for _, target := range d.distributedQueryCampaignTargets {
|
||||
if target.DistributedQueryCampaignID == id {
|
||||
if target.Type == kolide.TargetHost {
|
||||
hostIDs = append(hostIDs, target.TargetID)
|
||||
|
|
@ -63,52 +63,52 @@ func (orm *Datastore) DistributedQueryCampaignTargetIDs(id uint) (hostIDs []uint
|
|||
return hostIDs, labelIDs, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) NewDistributedQueryCampaignTarget(target *kolide.DistributedQueryCampaignTarget) (*kolide.DistributedQueryCampaignTarget, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewDistributedQueryCampaignTarget(target *kolide.DistributedQueryCampaignTarget) (*kolide.DistributedQueryCampaignTarget, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
target.ID = orm.nextID(target)
|
||||
orm.distributedQueryCampaignTargets[target.ID] = *target
|
||||
target.ID = d.nextID(target)
|
||||
d.distributedQueryCampaignTargets[target.ID] = *target
|
||||
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) NewDistributedQueryExecution(exec *kolide.DistributedQueryExecution) (*kolide.DistributedQueryExecution, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewDistributedQueryExecution(exec *kolide.DistributedQueryExecution) (*kolide.DistributedQueryExecution, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, e := range orm.distributedQueryExecutions {
|
||||
for _, e := range d.distributedQueryExecutions {
|
||||
if exec.HostID == e.HostID && exec.DistributedQueryCampaignID == e.DistributedQueryCampaignID {
|
||||
fmt.Printf("%+v -- %+v\n", exec, orm.distributedQueryExecutions)
|
||||
fmt.Printf("%+v -- %+v\n", exec, d.distributedQueryExecutions)
|
||||
return exec, errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
exec.ID = orm.nextID(exec)
|
||||
orm.distributedQueryExecutions[exec.ID] = *exec
|
||||
exec.ID = d.nextID(exec)
|
||||
d.distributedQueryExecutions[exec.ID] = *exec
|
||||
|
||||
return exec, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) CleanupDistributedQueryCampaigns(now time.Time) (expired uint, deleted uint, err error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) CleanupDistributedQueryCampaigns(now time.Time) (expired uint, deleted uint, err error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// First expire old waiting and running campaigns
|
||||
for id, c := range orm.distributedQueryCampaigns {
|
||||
for id, c := range d.distributedQueryCampaigns {
|
||||
if (c.Status == kolide.QueryWaiting && c.CreatedAt.Before(now.Add(-1*time.Minute))) ||
|
||||
(c.Status == kolide.QueryRunning && c.CreatedAt.Before(now.Add(-24*time.Hour))) {
|
||||
c.Status = kolide.QueryComplete
|
||||
orm.distributedQueryCampaigns[id] = c
|
||||
d.distributedQueryCampaigns[id] = c
|
||||
expired++
|
||||
}
|
||||
}
|
||||
|
||||
// Now delete executions for expired campaigns
|
||||
for id, e := range orm.distributedQueryExecutions {
|
||||
c, ok := orm.distributedQueryCampaigns[e.DistributedQueryCampaignID]
|
||||
for id, e := range d.distributedQueryExecutions {
|
||||
c, ok := d.distributedQueryCampaigns[e.DistributedQueryCampaignID]
|
||||
if !ok || c.Status == kolide.QueryComplete {
|
||||
delete(orm.distributedQueryExecutions, id)
|
||||
delete(d.distributedQueryExecutions, id)
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,58 +10,58 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewHost(host *kolide.Host) (*kolide.Host, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewHost(host *kolide.Host) (*kolide.Host, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, h := range orm.hosts {
|
||||
for _, h := range d.hosts {
|
||||
if host.NodeKey == h.NodeKey || host.UUID == h.UUID {
|
||||
return nil, kolide_errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
host.ID = orm.nextID(host)
|
||||
orm.hosts[host.ID] = host
|
||||
host.ID = d.nextID(host)
|
||||
d.hosts[host.ID] = host
|
||||
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveHost(host *kolide.Host) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveHost(host *kolide.Host) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.hosts[host.ID]; !ok {
|
||||
if _, ok := d.hosts[host.ID]; !ok {
|
||||
return kolide_errors.ErrNotFound
|
||||
}
|
||||
|
||||
for _, nic := range host.NetworkInterfaces {
|
||||
if nic.ID == 0 {
|
||||
nic.ID = orm.nextID(nic)
|
||||
nic.ID = d.nextID(nic)
|
||||
}
|
||||
nic.HostID = host.ID
|
||||
}
|
||||
host.ResetPrimaryNetwork()
|
||||
orm.hosts[host.ID] = host
|
||||
d.hosts[host.ID] = host
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeleteHost(host *kolide.Host) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteHost(host *kolide.Host) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.hosts[host.ID]; !ok {
|
||||
if _, ok := d.hosts[host.ID]; !ok {
|
||||
return kolide_errors.ErrNotFound
|
||||
}
|
||||
|
||||
delete(orm.hosts, host.ID)
|
||||
delete(d.hosts, host.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Host(id uint) (*kolide.Host, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) Host(id uint) (*kolide.Host, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
host, ok := orm.hosts[id]
|
||||
host, ok := d.hosts[id]
|
||||
if !ok {
|
||||
return nil, kolide_errors.ErrNotFound
|
||||
}
|
||||
|
|
@ -69,20 +69,20 @@ func (orm *Datastore) Host(id uint) (*kolide.Host, error) {
|
|||
return host, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListHosts(opt kolide.ListOptions) ([]*kolide.Host, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListHosts(opt kolide.ListOptions) ([]*kolide.Host, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
for k, _ := range orm.hosts {
|
||||
for k, _ := range d.hosts {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
hosts := []*kolide.Host{}
|
||||
for _, k := range keys {
|
||||
hosts = append(hosts, orm.hosts[uint(k)])
|
||||
hosts = append(hosts, d.hosts[uint(k)])
|
||||
}
|
||||
|
||||
// Apply ordering
|
||||
|
|
@ -108,15 +108,15 @@ func (orm *Datastore) ListHosts(opt kolide.ListOptions) ([]*kolide.Host, error)
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(hosts))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(hosts))
|
||||
hosts = hosts[low:high]
|
||||
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) EnrollHost(osQueryHostID string, nodeKeySize int) (*kolide.Host, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) EnrollHost(osQueryHostID string, nodeKeySize int) (*kolide.Host, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if osQueryHostID == "" {
|
||||
return nil, errors.New("missing host identifier from osquery for host enrollment")
|
||||
|
|
@ -136,7 +136,7 @@ func (orm *Datastore) EnrollHost(osQueryHostID string, nodeKeySize int) (*kolide
|
|||
host.CreatedAt = time.Now().UTC()
|
||||
host.UpdatedAt = host.CreatedAt
|
||||
|
||||
for _, h := range orm.hosts {
|
||||
for _, h := range d.hosts {
|
||||
if h.OsqueryHostID == osQueryHostID {
|
||||
host = *h
|
||||
break
|
||||
|
|
@ -144,18 +144,18 @@ func (orm *Datastore) EnrollHost(osQueryHostID string, nodeKeySize int) (*kolide
|
|||
}
|
||||
|
||||
if host.ID == 0 {
|
||||
host.ID = orm.nextID(host)
|
||||
host.ID = d.nextID(host)
|
||||
}
|
||||
orm.hosts[host.ID] = &host
|
||||
d.hosts[host.ID] = &host
|
||||
|
||||
return &host, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) AuthenticateHost(nodeKey string) (*kolide.Host, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) AuthenticateHost(nodeKey string) (*kolide.Host, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, host := range orm.hosts {
|
||||
for _, host := range d.hosts {
|
||||
if host.NodeKey == nodeKey {
|
||||
return host, nil
|
||||
}
|
||||
|
|
@ -164,13 +164,13 @@ func (orm *Datastore) AuthenticateHost(nodeKey string) (*kolide.Host, error) {
|
|||
return nil, kolide_errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) MarkHostSeen(host *kolide.Host, t time.Time) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) MarkHostSeen(host *kolide.Host, t time.Time) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
host.UpdatedAt = t
|
||||
|
||||
for _, h := range orm.hosts {
|
||||
for _, h := range d.hosts {
|
||||
if h.ID == host.ID {
|
||||
h.UpdatedAt = t
|
||||
break
|
||||
|
|
@ -179,7 +179,7 @@ func (orm *Datastore) MarkHostSeen(host *kolide.Host, t time.Time) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SearchHosts(query string, omit ...uint) ([]*kolide.Host, error) {
|
||||
func (d *Datastore) SearchHosts(query string, omit ...uint) ([]*kolide.Host, error) {
|
||||
omitLookup := map[uint]bool{}
|
||||
for _, o := range omit {
|
||||
omitLookup[o] = true
|
||||
|
|
@ -187,10 +187,10 @@ func (orm *Datastore) SearchHosts(query string, omit ...uint) ([]*kolide.Host, e
|
|||
|
||||
var results []*kolide.Host
|
||||
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, h := range orm.hosts {
|
||||
for _, h := range d.hosts {
|
||||
if len(results) == 10 {
|
||||
break
|
||||
}
|
||||
|
|
@ -211,10 +211,10 @@ func (orm *Datastore) SearchHosts(query string, omit ...uint) ([]*kolide.Host, e
|
|||
return results, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DistributedQueriesForHost(host *kolide.Host) (map[uint]string, error) {
|
||||
func (d *Datastore) DistributedQueriesForHost(host *kolide.Host) (map[uint]string, error) {
|
||||
// lookup of executions for this host
|
||||
hostExecutions := map[uint]kolide.DistributedQueryExecutionStatus{}
|
||||
for _, e := range orm.distributedQueryExecutions {
|
||||
for _, e := range d.distributedQueryExecutions {
|
||||
if e.HostID == host.ID {
|
||||
hostExecutions[e.DistributedQueryCampaignID] = e.Status
|
||||
}
|
||||
|
|
@ -222,7 +222,7 @@ func (orm *Datastore) DistributedQueriesForHost(host *kolide.Host) (map[uint]str
|
|||
|
||||
// lookup of labels for this host (only including matching labels)
|
||||
hostLabels := map[uint]bool{}
|
||||
labels, err := orm.ListLabelsForHost(host.ID)
|
||||
labels, err := d.ListLabelsForHost(host.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -231,16 +231,16 @@ func (orm *Datastore) DistributedQueriesForHost(host *kolide.Host) (map[uint]str
|
|||
}
|
||||
|
||||
queries := map[uint]string{} // map campaign ID -> query string
|
||||
for _, campaign := range orm.distributedQueryCampaigns {
|
||||
for _, campaign := range d.distributedQueryCampaigns {
|
||||
if campaign.Status != kolide.QueryRunning {
|
||||
continue
|
||||
}
|
||||
for _, target := range orm.distributedQueryCampaignTargets {
|
||||
for _, target := range d.distributedQueryCampaignTargets {
|
||||
if campaign.ID == target.DistributedQueryCampaignID &&
|
||||
((target.Type == kolide.TargetHost && target.TargetID == host.ID) ||
|
||||
(target.Type == kolide.TargetLabel && hostLabels[target.TargetID])) &&
|
||||
(hostExecutions[campaign.ID] == kolide.ExecutionWaiting) {
|
||||
queries[campaign.ID] = orm.queries[campaign.QueryID].Query
|
||||
queries[campaign.ID] = d.queries[campaign.QueryID].Query
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func New(config config.KolideConfig) (*Datastore, error) {
|
|||
return ds, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Name() string {
|
||||
func (d *Datastore) Name() string {
|
||||
return "inmem"
|
||||
}
|
||||
|
||||
|
|
@ -68,57 +68,57 @@ func sortResults(slice interface{}, opt kolide.ListOptions, fields map[string]st
|
|||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Migrate() error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
orm.nextIDs = make(map[interface{}]uint)
|
||||
orm.users = make(map[uint]*kolide.User)
|
||||
orm.sessions = make(map[uint]*kolide.Session)
|
||||
orm.passwordResets = make(map[uint]*kolide.PasswordResetRequest)
|
||||
orm.invites = make(map[uint]*kolide.Invite)
|
||||
orm.labels = make(map[uint]*kolide.Label)
|
||||
orm.labelQueryExecutions = make(map[uint]*kolide.LabelQueryExecution)
|
||||
orm.queries = make(map[uint]*kolide.Query)
|
||||
orm.packs = make(map[uint]*kolide.Pack)
|
||||
orm.hosts = make(map[uint]*kolide.Host)
|
||||
orm.scheduledQueries = make(map[uint]*kolide.ScheduledQuery)
|
||||
orm.packTargets = make(map[uint]*kolide.PackTarget)
|
||||
orm.distributedQueryExecutions = make(map[uint]kolide.DistributedQueryExecution)
|
||||
orm.distributedQueryCampaigns = make(map[uint]kolide.DistributedQueryCampaign)
|
||||
orm.distributedQueryCampaignTargets = make(map[uint]kolide.DistributedQueryCampaignTarget)
|
||||
func (d *Datastore) Migrate() error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
d.nextIDs = make(map[interface{}]uint)
|
||||
d.users = make(map[uint]*kolide.User)
|
||||
d.sessions = make(map[uint]*kolide.Session)
|
||||
d.passwordResets = make(map[uint]*kolide.PasswordResetRequest)
|
||||
d.invites = make(map[uint]*kolide.Invite)
|
||||
d.labels = make(map[uint]*kolide.Label)
|
||||
d.labelQueryExecutions = make(map[uint]*kolide.LabelQueryExecution)
|
||||
d.queries = make(map[uint]*kolide.Query)
|
||||
d.packs = make(map[uint]*kolide.Pack)
|
||||
d.hosts = make(map[uint]*kolide.Host)
|
||||
d.scheduledQueries = make(map[uint]*kolide.ScheduledQuery)
|
||||
d.packTargets = make(map[uint]*kolide.PackTarget)
|
||||
d.distributedQueryExecutions = make(map[uint]kolide.DistributedQueryExecution)
|
||||
d.distributedQueryCampaigns = make(map[uint]kolide.DistributedQueryCampaign)
|
||||
d.distributedQueryCampaignTargets = make(map[uint]kolide.DistributedQueryCampaignTarget)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Drop() error {
|
||||
return orm.Migrate()
|
||||
func (d *Datastore) Drop() error {
|
||||
return d.Migrate()
|
||||
}
|
||||
|
||||
func (orm *Datastore) Initialize() error {
|
||||
if err := orm.createBuiltinLabels(); err != nil {
|
||||
func (d *Datastore) Initialize() error {
|
||||
if err := d.createBuiltinLabels(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevUsers(); err != nil {
|
||||
if err := d.createDevUsers(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevHosts(); err != nil {
|
||||
if err := d.createDevHosts(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevQueries(); err != nil {
|
||||
if err := d.createDevQueries(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevLabels(); err != nil {
|
||||
if err := d.createDevLabels(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevOrgInfo(); err != nil {
|
||||
if err := d.createDevOrgInfo(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := orm.createDevPacksAndQueries(); err != nil {
|
||||
if err := d.createDevPacksAndQueries(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ func (orm *Datastore) Initialize() error {
|
|||
// getLimitOffsetSliceBounds returns the bounds that should be used for
|
||||
// re-slicing the results to comply with the requested ListOptions. Lack of
|
||||
// generics forces us to do this rather than reslicing in this method.
|
||||
func (orm *Datastore) getLimitOffsetSliceBounds(opt kolide.ListOptions, length int) (low uint, high uint) {
|
||||
func (d *Datastore) getLimitOffsetSliceBounds(opt kolide.ListOptions, length int) (low uint, high uint) {
|
||||
if opt.PerPage == 0 {
|
||||
// PerPage value of 0 indicates unlimited
|
||||
return 0, uint(length)
|
||||
|
|
@ -147,18 +147,18 @@ func (orm *Datastore) getLimitOffsetSliceBounds(opt kolide.ListOptions, length i
|
|||
|
||||
// nextID returns the next ID value that should be used for a struct of the
|
||||
// given type
|
||||
func (orm *Datastore) nextID(val interface{}) uint {
|
||||
func (d *Datastore) nextID(val interface{}) uint {
|
||||
valType := reflect.TypeOf(reflect.Indirect(reflect.ValueOf(val)).Interface())
|
||||
orm.nextIDs[valType]++
|
||||
return orm.nextIDs[valType]
|
||||
d.nextIDs[valType]++
|
||||
return d.nextIDs[valType]
|
||||
}
|
||||
|
||||
func (orm *Datastore) createDevPacksAndQueries() error {
|
||||
func (d *Datastore) createDevPacksAndQueries() error {
|
||||
query1 := &kolide.Query{
|
||||
Name: "Osquery Info",
|
||||
Query: "select * from osquery_info",
|
||||
}
|
||||
query1, err := orm.NewQuery(query1)
|
||||
query1, err := d.NewQuery(query1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -167,7 +167,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
Name: "Launchd",
|
||||
Query: "select * from launchd",
|
||||
}
|
||||
query2, err = orm.NewQuery(query2)
|
||||
query2, err = d.NewQuery(query2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -176,7 +176,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
Name: "registry",
|
||||
Query: "select * from osquery_registry",
|
||||
}
|
||||
query3, err = orm.NewQuery(query3)
|
||||
query3, err = d.NewQuery(query3)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -184,7 +184,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
pack1 := &kolide.Pack{
|
||||
Name: "Osquery Internal Info",
|
||||
}
|
||||
pack1, err = orm.NewPack(pack1)
|
||||
pack1, err = d.NewPack(pack1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -192,12 +192,12 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
pack2 := &kolide.Pack{
|
||||
Name: "macOS Attacks",
|
||||
}
|
||||
pack2, err = orm.NewPack(pack2)
|
||||
pack2, err = d.NewPack(pack2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = orm.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
_, err = d.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
QueryID: query1.ID,
|
||||
PackID: pack1.ID,
|
||||
Interval: 60,
|
||||
|
|
@ -207,7 +207,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
}
|
||||
|
||||
t := true
|
||||
_, err = orm.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
_, err = d.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
QueryID: query3.ID,
|
||||
PackID: pack1.ID,
|
||||
Interval: 60,
|
||||
|
|
@ -217,7 +217,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = orm.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
_, err = d.NewScheduledQuery(&kolide.ScheduledQuery{
|
||||
QueryID: query2.ID,
|
||||
PackID: pack2.ID,
|
||||
Interval: 60,
|
||||
|
|
@ -229,7 +229,7 @@ func (orm *Datastore) createDevPacksAndQueries() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) createBuiltinLabels() error {
|
||||
func (d *Datastore) createBuiltinLabels() error {
|
||||
labels := []kolide.Label{
|
||||
{
|
||||
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
||||
|
|
@ -305,7 +305,7 @@ func (orm *Datastore) createBuiltinLabels() error {
|
|||
|
||||
for _, label := range labels {
|
||||
label := label
|
||||
_, err := orm.NewLabel(&label)
|
||||
_, err := d.NewLabel(&label)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -316,7 +316,7 @@ func (orm *Datastore) createBuiltinLabels() error {
|
|||
|
||||
// Bootstrap a few users when using the in-memory database.
|
||||
// Each user's default password will just be their username.
|
||||
func (orm *Datastore) createDevUsers() error {
|
||||
func (d *Datastore) createDevUsers() error {
|
||||
users := []kolide.User{
|
||||
{
|
||||
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
||||
|
|
@ -355,11 +355,11 @@ func (orm *Datastore) createDevUsers() error {
|
|||
}
|
||||
for _, user := range users {
|
||||
user := user
|
||||
err := user.SetPassword(user.Username, orm.config.Auth.SaltKeySize, orm.config.Auth.BcryptCost)
|
||||
err := user.SetPassword(user.Username, d.config.Auth.SaltKeySize, d.config.Auth.BcryptCost)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err = orm.NewUser(&user)
|
||||
_, err = d.NewUser(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -368,7 +368,7 @@ func (orm *Datastore) createDevUsers() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) createDevQueries() error {
|
||||
func (d *Datastore) createDevQueries() error {
|
||||
queries := []kolide.Query{
|
||||
{
|
||||
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
||||
|
|
@ -437,7 +437,7 @@ func (orm *Datastore) createDevQueries() error {
|
|||
|
||||
for _, query := range queries {
|
||||
query := query
|
||||
_, err := orm.NewQuery(&query)
|
||||
_, err := d.NewQuery(&query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -447,7 +447,7 @@ func (orm *Datastore) createDevQueries() error {
|
|||
}
|
||||
|
||||
// Bootstrap a few hosts when using the in-memory database.
|
||||
func (orm *Datastore) createDevHosts() error {
|
||||
func (d *Datastore) createDevHosts() error {
|
||||
hosts := []kolide.Host{
|
||||
{
|
||||
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
||||
|
|
@ -492,7 +492,7 @@ func (orm *Datastore) createDevHosts() error {
|
|||
|
||||
for _, host := range hosts {
|
||||
host := host
|
||||
_, err := orm.NewHost(&host)
|
||||
_, err := d.NewHost(&host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -501,19 +501,19 @@ func (orm *Datastore) createDevHosts() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) createDevOrgInfo() error {
|
||||
func (d *Datastore) createDevOrgInfo() error {
|
||||
devOrgInfo := &kolide.AppConfig{
|
||||
OrgName: "Kolide",
|
||||
OrgLogoURL: fmt.Sprintf("%s/logo.png", orm.config.Server.Address),
|
||||
OrgLogoURL: fmt.Sprintf("%s/logo.png", d.config.Server.Address),
|
||||
}
|
||||
_, err := orm.NewAppConfig(devOrgInfo)
|
||||
_, err := d.NewAppConfig(devOrgInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) createDevLabels() error {
|
||||
func (d *Datastore) createDevLabels() error {
|
||||
labels := []kolide.Label{
|
||||
{
|
||||
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
||||
|
|
@ -544,7 +544,7 @@ func (orm *Datastore) createDevLabels() error {
|
|||
|
||||
for _, label := range labels {
|
||||
label := label
|
||||
_, err := orm.NewLabel(&label)
|
||||
_, err := d.NewLabel(&label)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ import (
|
|||
)
|
||||
|
||||
// NewInvite creates and stores a new invitation in a DB.
|
||||
func (orm *Datastore) NewInvite(invite *kolide.Invite) (*kolide.Invite, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewInvite(invite *kolide.Invite) (*kolide.Invite, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, in := range orm.invites {
|
||||
for _, in := range d.invites {
|
||||
if in.Email == invite.Email {
|
||||
return nil, errors.ErrExists
|
||||
}
|
||||
|
|
@ -24,26 +24,26 @@ func (orm *Datastore) NewInvite(invite *kolide.Invite) (*kolide.Invite, error) {
|
|||
invite.CreatedAt = time.Now()
|
||||
}
|
||||
|
||||
invite.ID = uint(len(orm.invites) + 1)
|
||||
orm.invites[invite.ID] = invite
|
||||
invite.ID = uint(len(d.invites) + 1)
|
||||
d.invites[invite.ID] = invite
|
||||
return invite, nil
|
||||
}
|
||||
|
||||
// Invites lists all invites in the datastore.
|
||||
func (orm *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
for k, _ := range orm.invites {
|
||||
for k, _ := range d.invites {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
invites := []*kolide.Invite{}
|
||||
for _, k := range keys {
|
||||
invites = append(invites, orm.invites[uint(k)])
|
||||
invites = append(invites, d.invites[uint(k)])
|
||||
}
|
||||
|
||||
// Apply ordering
|
||||
|
|
@ -64,27 +64,27 @@ func (orm *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, err
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(invites))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(invites))
|
||||
invites = invites[low:high]
|
||||
|
||||
return invites, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Invite(id uint) (*kolide.Invite, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
if invite, ok := orm.invites[id]; ok {
|
||||
func (d *Datastore) Invite(id uint) (*kolide.Invite, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
if invite, ok := d.invites[id]; ok {
|
||||
return invite, nil
|
||||
}
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
// InviteByEmail retrieves an invite for a specific email address.
|
||||
func (orm *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, invite := range orm.invites {
|
||||
for _, invite := range d.invites {
|
||||
if invite.Email == email {
|
||||
return invite, nil
|
||||
}
|
||||
|
|
@ -93,26 +93,26 @@ func (orm *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
|
|||
}
|
||||
|
||||
// SaveInvite saves an invitation in the datastore.
|
||||
func (orm *Datastore) SaveInvite(invite *kolide.Invite) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveInvite(invite *kolide.Invite) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.invites[invite.ID]; !ok {
|
||||
if _, ok := d.invites[invite.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.invites[invite.ID] = invite
|
||||
d.invites[invite.ID] = invite
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteInvite deletes an invitation.
|
||||
func (orm *Datastore) DeleteInvite(invite *kolide.Invite) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteInvite(invite *kolide.Invite) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.invites[invite.ID]; !ok {
|
||||
if _, ok := d.invites[invite.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
delete(orm.invites, invite.ID)
|
||||
delete(d.invites, invite.ID)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,71 +12,71 @@ import (
|
|||
"github.com/patrickmn/sortutil"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewLabel(label *kolide.Label) (*kolide.Label, error) {
|
||||
func (d *Datastore) NewLabel(label *kolide.Label) (*kolide.Label, error) {
|
||||
newLabel := *label
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, l := range orm.labels {
|
||||
d.mtx.Lock()
|
||||
for _, l := range d.labels {
|
||||
if l.Name == label.Name {
|
||||
return nil, kolide_errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
newLabel.ID = orm.nextID(label)
|
||||
orm.labels[newLabel.ID] = &newLabel
|
||||
orm.mtx.Unlock()
|
||||
newLabel.ID = d.nextID(label)
|
||||
d.labels[newLabel.ID] = &newLabel
|
||||
d.mtx.Unlock()
|
||||
|
||||
return &newLabel, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListLabelsForHost(hid uint) ([]kolide.Label, error) {
|
||||
func (d *Datastore) ListLabelsForHost(hid uint) ([]kolide.Label, error) {
|
||||
// First get IDs of label executions for the host
|
||||
resLabels := []kolide.Label{}
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
d.mtx.Lock()
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if lqe.HostID == hid && lqe.Matches {
|
||||
if label := orm.labels[lqe.LabelID]; label != nil {
|
||||
if label := d.labels[lqe.LabelID]; label != nil {
|
||||
resLabels = append(resLabels, *label)
|
||||
}
|
||||
}
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
return resLabels, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) LabelQueriesForHost(host *kolide.Host, cutoff time.Time) (map[string]string, error) {
|
||||
func (d *Datastore) LabelQueriesForHost(host *kolide.Host, cutoff time.Time) (map[string]string, error) {
|
||||
// Get post-cutoff executions for host
|
||||
execedIDs := map[uint]bool{}
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
d.mtx.Lock()
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if lqe.HostID == host.ID && (lqe.UpdatedAt == cutoff || lqe.UpdatedAt.After(cutoff)) {
|
||||
execedIDs[lqe.LabelID] = true
|
||||
}
|
||||
}
|
||||
|
||||
queries := map[string]string{}
|
||||
for _, label := range orm.labels {
|
||||
for _, label := range d.labels {
|
||||
if (label.Platform == "" || strings.Contains(label.Platform, host.Platform)) && !execedIDs[label.ID] {
|
||||
queries[strconv.Itoa(int(label.ID))] = label.Query
|
||||
}
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
return queries, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) getLabelByIDString(id string) (*kolide.Label, error) {
|
||||
func (d *Datastore) getLabelByIDString(id string) (*kolide.Label, error) {
|
||||
labelID, err := strconv.Atoi(id)
|
||||
if err != nil {
|
||||
return nil, errors.New("non-int label ID")
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
label, ok := orm.labels[uint(labelID)]
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
label, ok := d.labels[uint(labelID)]
|
||||
d.mtx.Unlock()
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("label ID not found: " + string(labelID))
|
||||
|
|
@ -85,17 +85,17 @@ func (orm *Datastore) getLabelByIDString(id string) (*kolide.Label, error) {
|
|||
return label, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) RecordLabelQueryExecutions(host *kolide.Host, results map[string]bool, t time.Time) error {
|
||||
func (d *Datastore) RecordLabelQueryExecutions(host *kolide.Host, results map[string]bool, t time.Time) error {
|
||||
// Record executions
|
||||
for strLabelID, matches := range results {
|
||||
label, err := orm.getLabelByIDString(strLabelID)
|
||||
label, err := d.getLabelByIDString(strLabelID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated := false
|
||||
orm.mtx.Lock()
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
d.mtx.Lock()
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if lqe.LabelID == label.ID && lqe.HostID == host.ID {
|
||||
// Update existing execution values
|
||||
lqe.UpdatedAt = t
|
||||
|
|
@ -113,27 +113,27 @@ func (orm *Datastore) RecordLabelQueryExecutions(host *kolide.Host, results map[
|
|||
UpdatedAt: t,
|
||||
Matches: matches,
|
||||
}
|
||||
lqe.ID = orm.nextID(lqe)
|
||||
orm.labelQueryExecutions[lqe.ID] = &lqe
|
||||
lqe.ID = d.nextID(lqe)
|
||||
d.labelQueryExecutions[lqe.ID] = &lqe
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeleteLabel(lid uint) error {
|
||||
orm.mtx.Lock()
|
||||
delete(orm.labels, lid)
|
||||
orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteLabel(lid uint) error {
|
||||
d.mtx.Lock()
|
||||
delete(d.labels, lid)
|
||||
d.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Label(lid uint) (*kolide.Label, error) {
|
||||
orm.mtx.Lock()
|
||||
label, ok := orm.labels[lid]
|
||||
orm.mtx.Unlock()
|
||||
func (d *Datastore) Label(lid uint) (*kolide.Label, error) {
|
||||
d.mtx.Lock()
|
||||
label, ok := d.labels[lid]
|
||||
d.mtx.Unlock()
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("Label not found")
|
||||
|
|
@ -141,21 +141,21 @@ func (orm *Datastore) Label(lid uint) (*kolide.Label, error) {
|
|||
return label, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListLabels(opt kolide.ListOptions) ([]*kolide.Label, error) {
|
||||
func (d *Datastore) ListLabels(opt kolide.ListOptions) ([]*kolide.Label, error) {
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
|
||||
orm.mtx.Lock()
|
||||
for k, _ := range orm.labels {
|
||||
d.mtx.Lock()
|
||||
for k, _ := range d.labels {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
labels := []*kolide.Label{}
|
||||
for _, k := range keys {
|
||||
labels = append(labels, orm.labels[uint(k)])
|
||||
labels = append(labels, d.labels[uint(k)])
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
// Apply ordering
|
||||
if opt.OrderKey != "" {
|
||||
|
|
@ -171,13 +171,13 @@ func (orm *Datastore) ListLabels(opt kolide.ListOptions) ([]*kolide.Label, error
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(labels))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(labels))
|
||||
labels = labels[low:high]
|
||||
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SearchLabels(query string, omit ...uint) ([]kolide.Label, error) {
|
||||
func (d *Datastore) SearchLabels(query string, omit ...uint) ([]kolide.Label, error) {
|
||||
omitLookup := map[uint]bool{}
|
||||
for _, o := range omit {
|
||||
omitLookup[o] = true
|
||||
|
|
@ -185,10 +185,10 @@ func (orm *Datastore) SearchLabels(query string, omit ...uint) ([]kolide.Label,
|
|||
|
||||
var results []kolide.Label
|
||||
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, l := range orm.labels {
|
||||
for _, l := range d.labels {
|
||||
if len(results) == 10 {
|
||||
break
|
||||
}
|
||||
|
|
@ -204,22 +204,22 @@ func (orm *Datastore) SearchLabels(query string, omit ...uint) ([]kolide.Label,
|
|||
return results, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListHostsInLabel(lid uint) ([]kolide.Host, error) {
|
||||
func (d *Datastore) ListHostsInLabel(lid uint) ([]kolide.Host, error) {
|
||||
var hosts []kolide.Host
|
||||
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if lqe.LabelID == lid && lqe.Matches {
|
||||
hosts = append(hosts, *orm.hosts[lqe.HostID])
|
||||
hosts = append(hosts, *d.hosts[lqe.HostID])
|
||||
}
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListUniqueHostsInLabels(labels []uint) ([]kolide.Host, error) {
|
||||
func (d *Datastore) ListUniqueHostsInLabels(labels []uint) ([]kolide.Host, error) {
|
||||
var hosts []kolide.Host
|
||||
|
||||
labelSet := map[uint]bool{}
|
||||
|
|
@ -229,13 +229,13 @@ func (orm *Datastore) ListUniqueHostsInLabels(labels []uint) ([]kolide.Host, err
|
|||
labelSet[label] = true
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if labelSet[lqe.LabelID] && lqe.Matches {
|
||||
if !hostSet[lqe.HostID] {
|
||||
hosts = append(hosts, *orm.hosts[lqe.HostID])
|
||||
hosts = append(hosts, *d.hosts[lqe.HostID])
|
||||
hostSet[lqe.HostID] = true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,53 +7,53 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewPack(pack *kolide.Pack) (*kolide.Pack, error) {
|
||||
func (d *Datastore) NewPack(pack *kolide.Pack) (*kolide.Pack, error) {
|
||||
newPack := *pack
|
||||
|
||||
for _, q := range orm.packs {
|
||||
for _, q := range d.packs {
|
||||
if pack.Name == q.Name {
|
||||
return nil, errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
newPack.ID = orm.nextID(pack)
|
||||
orm.packs[newPack.ID] = &newPack
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
newPack.ID = d.nextID(pack)
|
||||
d.packs[newPack.ID] = &newPack
|
||||
d.mtx.Unlock()
|
||||
|
||||
pack.ID = newPack.ID
|
||||
|
||||
return pack, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SavePack(pack *kolide.Pack) error {
|
||||
if _, ok := orm.packs[pack.ID]; !ok {
|
||||
func (d *Datastore) SavePack(pack *kolide.Pack) error {
|
||||
if _, ok := d.packs[pack.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
orm.packs[pack.ID] = pack
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
d.packs[pack.ID] = pack
|
||||
d.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeletePack(pid uint) error {
|
||||
if _, ok := orm.packs[pid]; !ok {
|
||||
func (d *Datastore) DeletePack(pid uint) error {
|
||||
if _, ok := d.packs[pid]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
delete(orm.packs, pid)
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
delete(d.packs, pid)
|
||||
d.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) Pack(id uint) (*kolide.Pack, error) {
|
||||
orm.mtx.Lock()
|
||||
pack, ok := orm.packs[id]
|
||||
orm.mtx.Unlock()
|
||||
func (d *Datastore) Pack(id uint) (*kolide.Pack, error) {
|
||||
d.mtx.Lock()
|
||||
pack, ok := d.packs[id]
|
||||
d.mtx.Unlock()
|
||||
if !ok {
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
|
@ -61,20 +61,20 @@ func (orm *Datastore) Pack(id uint) (*kolide.Pack, error) {
|
|||
return pack, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListPacks(opt kolide.ListOptions) ([]*kolide.Pack, error) {
|
||||
func (d *Datastore) ListPacks(opt kolide.ListOptions) ([]*kolide.Pack, error) {
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
orm.mtx.Lock()
|
||||
for k, _ := range orm.packs {
|
||||
d.mtx.Lock()
|
||||
for k, _ := range d.packs {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
packs := []*kolide.Pack{}
|
||||
for _, k := range keys {
|
||||
packs = append(packs, orm.packs[uint(k)])
|
||||
packs = append(packs, d.packs[uint(k)])
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
// Apply ordering
|
||||
if opt.OrderKey != "" {
|
||||
|
|
@ -91,13 +91,13 @@ func (orm *Datastore) ListPacks(opt kolide.ListOptions) ([]*kolide.Pack, error)
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(packs))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(packs))
|
||||
packs = packs[low:high]
|
||||
|
||||
return packs, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) AddLabelToPack(lid uint, pid uint) error {
|
||||
func (d *Datastore) AddLabelToPack(lid uint, pid uint) error {
|
||||
pt := &kolide.PackTarget{
|
||||
PackID: pid,
|
||||
Target: kolide.Target{
|
||||
|
|
@ -106,52 +106,52 @@ func (orm *Datastore) AddLabelToPack(lid uint, pid uint) error {
|
|||
},
|
||||
}
|
||||
|
||||
orm.mtx.Lock()
|
||||
pt.ID = orm.nextID(pt)
|
||||
orm.packTargets[pt.ID] = pt
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Lock()
|
||||
pt.ID = d.nextID(pt)
|
||||
d.packTargets[pt.ID] = pt
|
||||
d.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListLabelsForPack(pid uint) ([]*kolide.Label, error) {
|
||||
func (d *Datastore) ListLabelsForPack(pid uint) ([]*kolide.Label, error) {
|
||||
var labels []*kolide.Label
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, pt := range orm.packTargets {
|
||||
d.mtx.Lock()
|
||||
for _, pt := range d.packTargets {
|
||||
if pt.Type == kolide.TargetLabel && pt.PackID == pid {
|
||||
labels = append(labels, orm.labels[pt.TargetID])
|
||||
labels = append(labels, d.labels[pt.TargetID])
|
||||
}
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) RemoveLabelFromPack(label *kolide.Label, pack *kolide.Pack) error {
|
||||
func (d *Datastore) RemoveLabelFromPack(label *kolide.Label, pack *kolide.Pack) error {
|
||||
var labelsToDelete []uint
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, pt := range orm.packTargets {
|
||||
d.mtx.Lock()
|
||||
for _, pt := range d.packTargets {
|
||||
if pt.Type == kolide.TargetLabel && pt.TargetID == label.ID && pt.PackID == pack.ID {
|
||||
labelsToDelete = append(labelsToDelete, pt.ID)
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range labelsToDelete {
|
||||
delete(orm.packTargets, id)
|
||||
delete(d.packTargets, id)
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListHostsInPack(pid uint, opt kolide.ListOptions) ([]*kolide.Host, error) {
|
||||
func (d *Datastore) ListHostsInPack(pid uint, opt kolide.ListOptions) ([]*kolide.Host, error) {
|
||||
hosts := []*kolide.Host{}
|
||||
hostLookup := map[uint]bool{}
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, pt := range orm.packTargets {
|
||||
d.mtx.Lock()
|
||||
for _, pt := range d.packTargets {
|
||||
if pt.PackID != pid {
|
||||
continue
|
||||
}
|
||||
|
|
@ -160,18 +160,18 @@ func (orm *Datastore) ListHostsInPack(pid uint, opt kolide.ListOptions) ([]*koli
|
|||
case kolide.TargetHost:
|
||||
if !hostLookup[pt.TargetID] {
|
||||
hostLookup[pt.TargetID] = true
|
||||
hosts = append(hosts, orm.hosts[pt.TargetID])
|
||||
hosts = append(hosts, d.hosts[pt.TargetID])
|
||||
}
|
||||
case kolide.TargetLabel:
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
for _, lqe := range d.labelQueryExecutions {
|
||||
if lqe.LabelID == pt.TargetID && lqe.Matches && !hostLookup[lqe.HostID] {
|
||||
hostLookup[lqe.HostID] = true
|
||||
hosts = append(hosts, orm.hosts[lqe.HostID])
|
||||
hosts = append(hosts, d.hosts[lqe.HostID])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
d.mtx.Unlock()
|
||||
|
||||
// Apply ordering
|
||||
if opt.OrderKey != "" {
|
||||
|
|
@ -196,7 +196,7 @@ func (orm *Datastore) ListHostsInPack(pid uint, opt kolide.ListOptions) ([]*koli
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(hosts))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(hosts))
|
||||
hosts = hosts[low:high]
|
||||
|
||||
return hosts, nil
|
||||
|
|
|
|||
|
|
@ -5,68 +5,68 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewPasswordResetRequest(req *kolide.PasswordResetRequest) (*kolide.PasswordResetRequest, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewPasswordResetRequest(req *kolide.PasswordResetRequest) (*kolide.PasswordResetRequest, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
req.ID = orm.nextID(req)
|
||||
orm.passwordResets[req.ID] = req
|
||||
req.ID = d.nextID(req)
|
||||
d.passwordResets[req.ID] = req
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SavePasswordResetRequest(req *kolide.PasswordResetRequest) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SavePasswordResetRequest(req *kolide.PasswordResetRequest) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.passwordResets[req.ID]; !ok {
|
||||
if _, ok := d.passwordResets[req.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.passwordResets[req.ID] = req
|
||||
d.passwordResets[req.ID] = req
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeletePasswordResetRequest(req *kolide.PasswordResetRequest) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeletePasswordResetRequest(req *kolide.PasswordResetRequest) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.passwordResets[req.ID]; !ok {
|
||||
if _, ok := d.passwordResets[req.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
delete(orm.passwordResets, req.ID)
|
||||
delete(d.passwordResets, req.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeletePasswordResetRequestsForUser(userID uint) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeletePasswordResetRequestsForUser(userID uint) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, pr := range orm.passwordResets {
|
||||
for _, pr := range d.passwordResets {
|
||||
if pr.UserID == userID {
|
||||
delete(orm.passwordResets, pr.ID)
|
||||
delete(d.passwordResets, pr.ID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) FindPassswordResetByID(id uint) (*kolide.PasswordResetRequest, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) FindPassswordResetByID(id uint) (*kolide.PasswordResetRequest, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if req, ok := orm.passwordResets[id]; ok {
|
||||
if req, ok := d.passwordResets[id]; ok {
|
||||
return req, nil
|
||||
}
|
||||
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) FindPassswordResetsByUserID(userID uint) ([]*kolide.PasswordResetRequest, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) FindPassswordResetsByUserID(userID uint) ([]*kolide.PasswordResetRequest, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
resets := make([]*kolide.PasswordResetRequest, 0)
|
||||
|
||||
for _, pr := range orm.passwordResets {
|
||||
for _, pr := range d.passwordResets {
|
||||
if pr.UserID == userID {
|
||||
resets = append(resets, pr)
|
||||
}
|
||||
|
|
@ -79,11 +79,11 @@ func (orm *Datastore) FindPassswordResetsByUserID(userID uint) ([]*kolide.Passwo
|
|||
return resets, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) FindPassswordResetByToken(token string) (*kolide.PasswordResetRequest, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) FindPassswordResetByToken(token string) (*kolide.PasswordResetRequest, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, pr := range orm.passwordResets {
|
||||
for _, pr := range d.passwordResets {
|
||||
if pr.Token == token {
|
||||
return pr, nil
|
||||
}
|
||||
|
|
@ -92,11 +92,11 @@ func (orm *Datastore) FindPassswordResetByToken(token string) (*kolide.PasswordR
|
|||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) FindPassswordResetByTokenAndUserID(token string, userID uint) (*kolide.PasswordResetRequest, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) FindPassswordResetByTokenAndUserID(token string, userID uint) (*kolide.PasswordResetRequest, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, pr := range orm.passwordResets {
|
||||
for _, pr := range d.passwordResets {
|
||||
if pr.Token == token && pr.UserID == userID {
|
||||
return pr, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,58 +7,58 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewQuery(query *kolide.Query) (*kolide.Query, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewQuery(query *kolide.Query) (*kolide.Query, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
newQuery := *query
|
||||
|
||||
for _, q := range orm.queries {
|
||||
for _, q := range d.queries {
|
||||
if query.Name == q.Name {
|
||||
return nil, errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
newQuery.ID = orm.nextID(newQuery)
|
||||
orm.queries[newQuery.ID] = &newQuery
|
||||
newQuery.ID = d.nextID(newQuery)
|
||||
d.queries[newQuery.ID] = &newQuery
|
||||
|
||||
return &newQuery, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveQuery(query *kolide.Query) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveQuery(query *kolide.Query) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.queries[query.ID]; !ok {
|
||||
if _, ok := d.queries[query.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.queries[query.ID] = query
|
||||
d.queries[query.ID] = query
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeleteQuery(query *kolide.Query) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteQuery(query *kolide.Query) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.queries[query.ID]; !ok {
|
||||
if _, ok := d.queries[query.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
delete(orm.queries, query.ID)
|
||||
delete(d.queries, query.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteQueries (soft) deletes the existing query objects with the provided
|
||||
// IDs. The number of deleted queries is returned along with any error.
|
||||
func (orm *Datastore) DeleteQueries(ids []uint) (uint, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteQueries(ids []uint) (uint, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
deleted := uint(0)
|
||||
for _, id := range ids {
|
||||
if _, ok := orm.queries[id]; ok {
|
||||
delete(orm.queries, id)
|
||||
if _, ok := d.queries[id]; ok {
|
||||
delete(d.queries, id)
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
|
|
@ -66,47 +66,47 @@ func (orm *Datastore) DeleteQueries(ids []uint) (uint, error) {
|
|||
return deleted, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) getUserNameByID(id uint) string {
|
||||
if u, ok := orm.users[id]; ok {
|
||||
func (d *Datastore) getUserNameByID(id uint) string {
|
||||
if u, ok := d.users[id]; ok {
|
||||
return u.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (orm *Datastore) Query(id uint) (*kolide.Query, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) Query(id uint) (*kolide.Query, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
query, ok := orm.queries[id]
|
||||
query, ok := d.queries[id]
|
||||
if !ok {
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
query.AuthorName = orm.getUserNameByID(query.AuthorID)
|
||||
query.AuthorName = d.getUserNameByID(query.AuthorID)
|
||||
|
||||
if err := orm.loadPacksForQueries([]*kolide.Query{query}); err != nil {
|
||||
if err := d.loadPacksForQueries([]*kolide.Query{query}); err != nil {
|
||||
return nil, errors.DatabaseError(err)
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListQueries(opt kolide.ListOptions) ([]*kolide.Query, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListQueries(opt kolide.ListOptions) ([]*kolide.Query, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
for k, _ := range orm.queries {
|
||||
for k, _ := range d.queries {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
queries := []*kolide.Query{}
|
||||
for _, k := range keys {
|
||||
q := orm.queries[uint(k)]
|
||||
q := d.queries[uint(k)]
|
||||
if q.Saved {
|
||||
q.AuthorName = orm.getUserNameByID(q.AuthorID)
|
||||
q.AuthorName = d.getUserNameByID(q.AuthorID)
|
||||
queries = append(queries, q)
|
||||
}
|
||||
}
|
||||
|
|
@ -131,10 +131,10 @@ func (orm *Datastore) ListQueries(opt kolide.ListOptions) ([]*kolide.Query, erro
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(queries))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(queries))
|
||||
queries = queries[low:high]
|
||||
|
||||
if err := orm.loadPacksForQueries(queries); err != nil {
|
||||
if err := d.loadPacksForQueries(queries); err != nil {
|
||||
return nil, errors.DatabaseError(err)
|
||||
}
|
||||
|
||||
|
|
@ -142,12 +142,12 @@ func (orm *Datastore) ListQueries(opt kolide.ListOptions) ([]*kolide.Query, erro
|
|||
}
|
||||
|
||||
// loadPacksForQueries loads the packs associated with the provided queries
|
||||
func (orm *Datastore) loadPacksForQueries(queries []*kolide.Query) error {
|
||||
func (d *Datastore) loadPacksForQueries(queries []*kolide.Query) error {
|
||||
for _, q := range queries {
|
||||
q.Packs = make([]kolide.Pack, 0)
|
||||
for _, sq := range orm.scheduledQueries {
|
||||
for _, sq := range d.scheduledQueries {
|
||||
if sq.QueryID == q.ID {
|
||||
q.Packs = append(q.Packs, *orm.packs[sq.PackID])
|
||||
q.Packs = append(q.Packs, *d.packs[sq.PackID])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,64 +7,64 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
newScheduledQuery := *sq
|
||||
|
||||
newScheduledQuery.ID = orm.nextID(newScheduledQuery)
|
||||
orm.scheduledQueries[newScheduledQuery.ID] = &newScheduledQuery
|
||||
newScheduledQuery.ID = d.nextID(newScheduledQuery)
|
||||
d.scheduledQueries[newScheduledQuery.ID] = &newScheduledQuery
|
||||
|
||||
return &newScheduledQuery, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.scheduledQueries[sq.ID]; !ok {
|
||||
if _, ok := d.scheduledQueries[sq.ID]; !ok {
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.scheduledQueries[sq.ID] = sq
|
||||
d.scheduledQueries[sq.ID] = sq
|
||||
return sq, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DeleteScheduledQuery(id uint) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) DeleteScheduledQuery(id uint) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.scheduledQueries[id]; !ok {
|
||||
if _, ok := d.scheduledQueries[id]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
delete(orm.scheduledQueries, id)
|
||||
delete(d.scheduledQueries, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ScheduledQuery(id uint) (*kolide.ScheduledQuery, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ScheduledQuery(id uint) (*kolide.ScheduledQuery, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
sq, ok := orm.scheduledQueries[id]
|
||||
sq, ok := d.scheduledQueries[id]
|
||||
if !ok {
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
sq.Name = orm.queries[sq.QueryID].Name
|
||||
sq.Query = orm.queries[sq.QueryID].Query
|
||||
sq.Name = d.queries[sq.QueryID].Name
|
||||
sq.Query = d.queries[sq.QueryID].Query
|
||||
|
||||
return sq, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListScheduledQueriesInPack(id uint, opt kolide.ListOptions) ([]*kolide.ScheduledQuery, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListScheduledQueriesInPack(id uint, opt kolide.ListOptions) ([]*kolide.ScheduledQuery, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
for k, sq := range orm.scheduledQueries {
|
||||
for k, sq := range d.scheduledQueries {
|
||||
if sq.PackID == id {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
|
|
@ -78,9 +78,9 @@ func (orm *Datastore) ListScheduledQueriesInPack(id uint, opt kolide.ListOptions
|
|||
|
||||
scheduledQueries := []*kolide.ScheduledQuery{}
|
||||
for _, k := range keys {
|
||||
q := orm.scheduledQueries[uint(k)]
|
||||
q.Name = orm.queries[q.QueryID].Name
|
||||
q.Query = orm.queries[q.QueryID].Query
|
||||
q := d.scheduledQueries[uint(k)]
|
||||
q.Name = d.queries[q.QueryID].Name
|
||||
q.Query = d.queries[q.QueryID].Query
|
||||
scheduledQueries = append(scheduledQueries, q)
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ func (orm *Datastore) ListScheduledQueriesInPack(id uint, opt kolide.ListOptions
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(scheduledQueries))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(scheduledQueries))
|
||||
scheduledQueries = scheduledQueries[low:high]
|
||||
|
||||
return scheduledQueries, nil
|
||||
|
|
|
|||
|
|
@ -7,11 +7,11 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) SessionByKey(key string) (*kolide.Session, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SessionByKey(key string) (*kolide.Session, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, session := range orm.sessions {
|
||||
for _, session := range d.sessions {
|
||||
if session.Key == key {
|
||||
return session, nil
|
||||
}
|
||||
|
|
@ -19,22 +19,22 @@ func (orm *Datastore) SessionByKey(key string) (*kolide.Session, error) {
|
|||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) SessionByID(id uint) (*kolide.Session, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SessionByID(id uint) (*kolide.Session, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if session, ok := orm.sessions[id]; ok {
|
||||
if session, ok := d.sessions[id]; ok {
|
||||
return session, nil
|
||||
}
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListSessionsForUser(id uint) ([]*kolide.Session, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListSessionsForUser(id uint) ([]*kolide.Session, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
var sessions []*kolide.Session
|
||||
for _, session := range orm.sessions {
|
||||
for _, session := range d.sessions {
|
||||
if session.UserID == id {
|
||||
sessions = append(sessions, session)
|
||||
}
|
||||
|
|
@ -45,13 +45,13 @@ func (orm *Datastore) ListSessionsForUser(id uint) ([]*kolide.Session, error) {
|
|||
return sessions, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) NewSession(session *kolide.Session) (*kolide.Session, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewSession(session *kolide.Session) (*kolide.Session, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
session.ID = orm.nextID(session)
|
||||
orm.sessions[session.ID] = session
|
||||
if err := orm.MarkSessionAccessed(session); err != nil {
|
||||
session.ID = d.nextID(session)
|
||||
d.sessions[session.ID] = session
|
||||
if err := d.MarkSessionAccessed(session); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -59,29 +59,29 @@ func (orm *Datastore) NewSession(session *kolide.Session) (*kolide.Session, erro
|
|||
|
||||
}
|
||||
|
||||
func (orm *Datastore) DestroySession(session *kolide.Session) error {
|
||||
if _, ok := orm.sessions[session.ID]; !ok {
|
||||
func (d *Datastore) DestroySession(session *kolide.Session) error {
|
||||
if _, ok := d.sessions[session.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
delete(orm.sessions, session.ID)
|
||||
delete(d.sessions, session.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) DestroyAllSessionsForUser(id uint) error {
|
||||
for _, session := range orm.sessions {
|
||||
func (d *Datastore) DestroyAllSessionsForUser(id uint) error {
|
||||
for _, session := range d.sessions {
|
||||
if session.UserID == id {
|
||||
delete(orm.sessions, session.ID)
|
||||
delete(d.sessions, session.ID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) MarkSessionAccessed(session *kolide.Session) error {
|
||||
func (d *Datastore) MarkSessionAccessed(session *kolide.Session) error {
|
||||
session.AccessedAt = time.Now().UTC()
|
||||
if _, ok := orm.sessions[session.ID]; !ok {
|
||||
if _, ok := d.sessions[session.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
orm.sessions[session.ID] = session
|
||||
d.sessions[session.ID] = session
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,27 +7,27 @@ import (
|
|||
"github.com/kolide/kolide-ose/server/kolide"
|
||||
)
|
||||
|
||||
func (orm *Datastore) NewUser(user *kolide.User) (*kolide.User, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) NewUser(user *kolide.User) (*kolide.User, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, in := range orm.users {
|
||||
for _, in := range d.users {
|
||||
if in.Username == user.Username {
|
||||
return nil, errors.ErrExists
|
||||
}
|
||||
}
|
||||
|
||||
user.ID = orm.nextID(user)
|
||||
orm.users[user.ID] = user
|
||||
user.ID = d.nextID(user)
|
||||
d.users[user.ID] = user
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) User(username string) (*kolide.User, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) User(username string) (*kolide.User, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, user := range orm.users {
|
||||
for _, user := range d.users {
|
||||
if user.Username == username {
|
||||
return user, nil
|
||||
}
|
||||
|
|
@ -36,20 +36,20 @@ func (orm *Datastore) User(username string) (*kolide.User, error) {
|
|||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) ListUsers(opt kolide.ListOptions) ([]*kolide.User, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) ListUsers(opt kolide.ListOptions) ([]*kolide.User, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
// We need to sort by keys to provide reliable ordering
|
||||
keys := []int{}
|
||||
for k, _ := range orm.users {
|
||||
for k, _ := range d.users {
|
||||
keys = append(keys, int(k))
|
||||
}
|
||||
sort.Ints(keys)
|
||||
|
||||
users := []*kolide.User{}
|
||||
for _, k := range keys {
|
||||
users = append(users, orm.users[uint(k)])
|
||||
users = append(users, d.users[uint(k)])
|
||||
}
|
||||
|
||||
// Apply ordering
|
||||
|
|
@ -71,17 +71,17 @@ func (orm *Datastore) ListUsers(opt kolide.ListOptions) ([]*kolide.User, error)
|
|||
}
|
||||
|
||||
// Apply limit/offset
|
||||
low, high := orm.getLimitOffsetSliceBounds(opt, len(users))
|
||||
low, high := d.getLimitOffsetSliceBounds(opt, len(users))
|
||||
users = users[low:high]
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (orm *Datastore) UserByEmail(email string) (*kolide.User, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) UserByEmail(email string) (*kolide.User, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
for _, user := range orm.users {
|
||||
for _, user := range d.users {
|
||||
if user.Email == email {
|
||||
return user, nil
|
||||
}
|
||||
|
|
@ -90,25 +90,25 @@ func (orm *Datastore) UserByEmail(email string) (*kolide.User, error) {
|
|||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) UserByID(id uint) (*kolide.User, error) {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) UserByID(id uint) (*kolide.User, error) {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if user, ok := orm.users[id]; ok {
|
||||
if user, ok := d.users[id]; ok {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
return nil, errors.ErrNotFound
|
||||
}
|
||||
|
||||
func (orm *Datastore) SaveUser(user *kolide.User) error {
|
||||
orm.mtx.Lock()
|
||||
defer orm.mtx.Unlock()
|
||||
func (d *Datastore) SaveUser(user *kolide.User) error {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
|
||||
if _, ok := orm.users[user.ID]; !ok {
|
||||
if _, ok := d.users[user.ID]; !ok {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
orm.users[user.ID] = user
|
||||
d.users[user.ID] = user
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue