Compare commits
1 Commits
v2023.4.1-
...
v2023.3.31
Author | SHA1 | Date | |
---|---|---|---|
5fc5f5fd5d |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,7 +4,7 @@ dist/
|
|||||||
/tools
|
/tools
|
||||||
/tmp
|
/tmp
|
||||||
/state.json
|
/state.json
|
||||||
/emissary.sqlite*
|
/emissary.sqlite
|
||||||
/.gitea-release
|
/.gitea-release
|
||||||
/agent-key.json
|
/agent-key.json
|
||||||
/apps
|
/apps
|
||||||
|
1
Jenkinsfile
vendored
1
Jenkinsfile
vendored
@ -23,7 +23,6 @@ pipeline {
|
|||||||
sh '''
|
sh '''
|
||||||
git config --global credential.https://forge.cadoles.com.username "$GIT_USERNAME"
|
git config --global credential.https://forge.cadoles.com.username "$GIT_USERNAME"
|
||||||
git config --global credential.https://forge.cadoles.com.helper '!f() { test "$1" = get && echo "password=$GIT_PASSWORD"; }; f'
|
git config --global credential.https://forge.cadoles.com.helper '!f() { test "$1" = get && echo "password=$GIT_PASSWORD"; }; f'
|
||||||
|
|
||||||
export GOPRIVATE=forge.cadoles.com/arcad/edge
|
export GOPRIVATE=forge.cadoles.com/arcad/edge
|
||||||
make test
|
make test
|
||||||
'''
|
'''
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"forge.cadoles.com/Cadoles/emissary/internal/command/agent"
|
"forge.cadoles.com/Cadoles/emissary/internal/command/agent"
|
||||||
"forge.cadoles.com/Cadoles/emissary/internal/command/api"
|
"forge.cadoles.com/Cadoles/emissary/internal/command/api"
|
||||||
|
|
||||||
_ "forge.cadoles.com/Cadoles/emissary/internal/imports/format"
|
|
||||||
_ "forge.cadoles.com/Cadoles/emissary/internal/imports/spec"
|
_ "forge.cadoles.com/Cadoles/emissary/internal/imports/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,8 +28,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultSQLiteParams = "?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_txlock=immediate"
|
|
||||||
|
|
||||||
func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs *spec.Spec) ([]edgeHTTP.HandlerOptionFunc, error) {
|
func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs *spec.Spec) ([]edgeHTTP.HandlerOptionFunc, error) {
|
||||||
dataDir, err := c.ensureAppDataDir(ctx, appKey)
|
dataDir, err := c.ensureAppDataDir(ctx, appKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -37,7 +35,7 @@ func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs
|
|||||||
}
|
}
|
||||||
|
|
||||||
dbFile := filepath.Join(dataDir, appKey+".sqlite")
|
dbFile := filepath.Join(dataDir, appKey+".sqlite")
|
||||||
db, err := sqlite.Open(dbFile + defaultSQLiteParams)
|
db, err := sqlite.Open(dbFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "could not open database file '%s'", dbFile)
|
return nil, errors.Wrapf(err, "could not open database file '%s'", dbFile)
|
||||||
}
|
}
|
||||||
|
@ -35,15 +35,12 @@ type Server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start(ctx context.Context, addr string) (err error) {
|
func (s *Server) Start(ctx context.Context, addr string) (err error) {
|
||||||
if s.Running() {
|
if s.server != nil {
|
||||||
if err := s.Stop(); err != nil {
|
if err := s.Stop(); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.serverMutex.Lock()
|
|
||||||
defer s.serverMutex.Unlock()
|
|
||||||
|
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
|
|
||||||
router.Use(middleware.Logger)
|
router.Use(middleware.Logger)
|
||||||
@ -88,7 +85,9 @@ func (s *Server) Start(ctx context.Context, addr string) (err error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
s.serverMutex.Lock()
|
||||||
s.server = server
|
s.server = server
|
||||||
|
s.serverMutex.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -101,25 +100,20 @@ func (s *Server) Running() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() error {
|
func (s *Server) Stop() error {
|
||||||
if !s.Running() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.serverMutex.Lock()
|
|
||||||
defer s.serverMutex.Unlock()
|
|
||||||
|
|
||||||
if s.server == nil {
|
if s.server == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.server.Close(); err != nil {
|
defer func() {
|
||||||
|
s.serverMutex.Lock()
|
||||||
s.server = nil
|
s.server = nil
|
||||||
|
s.serverMutex.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := s.server.Close(); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.server = nil
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
||||||
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||||||
@ -77,13 +76,6 @@ func (a *Authenticator) Authenticate(ctx context.Context, r *http.Request) (auth
|
|||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
contactedAt := time.Now()
|
|
||||||
|
|
||||||
agent, err = a.repo.Update(ctx, agent.ID, datastore.WithAgentUpdateContactedAt(contactedAt))
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.WithStack(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
user := &User{
|
user := &User{
|
||||||
agent: agent,
|
agent: agent,
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
type UpdateAgentOptions struct {
|
type UpdateAgentOptions struct {
|
||||||
Status *int
|
Status *int
|
||||||
Label *string
|
|
||||||
Options []OptionFunc
|
Options []OptionFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,12 +21,6 @@ func WithAgentStatus(status int) UpdateAgentOptionFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithAgentLabel(label string) UpdateAgentOptionFunc {
|
|
||||||
return func(opts *UpdateAgentOptions) {
|
|
||||||
opts.Label = &label
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithUpdateAgentsOptions(funcs ...OptionFunc) UpdateAgentOptionFunc {
|
func WithUpdateAgentsOptions(funcs ...OptionFunc) UpdateAgentOptionFunc {
|
||||||
return func(opts *UpdateAgentOptions) {
|
return func(opts *UpdateAgentOptions) {
|
||||||
opts.Options = funcs
|
opts.Options = funcs
|
||||||
@ -46,10 +39,6 @@ func (c *Client) UpdateAgent(ctx context.Context, agentID datastore.AgentID, fun
|
|||||||
payload["status"] = *opts.Status
|
payload["status"] = *opts.Status
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Label != nil {
|
|
||||||
payload["label"] = *opts.Label
|
|
||||||
}
|
|
||||||
|
|
||||||
response := withResponse[struct {
|
response := withResponse[struct {
|
||||||
Agent *datastore.Agent `json:"agent"`
|
Agent *datastore.Agent `json:"agent"`
|
||||||
}]()
|
}]()
|
||||||
|
@ -49,6 +49,10 @@ func RunCommand() *cli.Command {
|
|||||||
controllers = append(controllers, spec.NewController())
|
controllers = append(controllers, spec.NewController())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ctrlConf.Proxy.Enabled {
|
||||||
|
controllers = append(controllers, proxy.NewController())
|
||||||
|
}
|
||||||
|
|
||||||
if ctrlConf.UCI.Enabled {
|
if ctrlConf.UCI.Enabled {
|
||||||
controllers = append(controllers, openwrt.NewUCIController(
|
controllers = append(controllers, openwrt.NewUCIController(
|
||||||
string(ctrlConf.UCI.BinPath),
|
string(ctrlConf.UCI.BinPath),
|
||||||
@ -62,10 +66,6 @@ func RunCommand() *cli.Command {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctrlConf.Proxy.Enabled {
|
|
||||||
controllers = append(controllers, proxy.NewController())
|
|
||||||
}
|
|
||||||
|
|
||||||
if ctrlConf.SysUpgrade.Enabled {
|
if ctrlConf.SysUpgrade.Enabled {
|
||||||
sysUpgradeArgs := make([]string, 0)
|
sysUpgradeArgs := make([]string, 0)
|
||||||
if len(ctrlConf.SysUpgrade.SysUpgradeCommand) > 1 {
|
if len(ctrlConf.SysUpgrade.SysUpgradeCommand) > 1 {
|
||||||
|
@ -22,11 +22,6 @@ func UpdateCommand() *cli.Command {
|
|||||||
Usage: "Set `STATUS` to selected agent",
|
Usage: "Set `STATUS` to selected agent",
|
||||||
Value: -1,
|
Value: -1,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
|
||||||
Name: "label",
|
|
||||||
Usage: "Set `LABEL` to selected agent",
|
|
||||||
Value: "",
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
Action: func(ctx *cli.Context) error {
|
Action: func(ctx *cli.Context) error {
|
||||||
baseFlags := clientFlag.GetBaseFlags(ctx)
|
baseFlags := clientFlag.GetBaseFlags(ctx)
|
||||||
@ -48,11 +43,6 @@ func UpdateCommand() *cli.Command {
|
|||||||
options = append(options, client.WithAgentStatus(status))
|
options = append(options, client.WithAgentStatus(status))
|
||||||
}
|
}
|
||||||
|
|
||||||
label := ctx.String("label")
|
|
||||||
if label != "" {
|
|
||||||
options = append(options, client.WithAgentLabel(label))
|
|
||||||
}
|
|
||||||
|
|
||||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||||
|
|
||||||
agent, err := client.UpdateAgent(ctx.Context, agentID, options...)
|
agent, err := client.UpdateAgent(ctx.Context, agentID, options...)
|
||||||
|
@ -7,10 +7,9 @@ func agentHints(outputMode format.OutputMode) format.Hints {
|
|||||||
OutputMode: outputMode,
|
OutputMode: outputMode,
|
||||||
Props: []format.Prop{
|
Props: []format.Prop{
|
||||||
format.NewProp("ID", "ID"),
|
format.NewProp("ID", "ID"),
|
||||||
format.NewProp("Label", "Label"),
|
|
||||||
format.NewProp("Thumbprint", "Thumbprint"),
|
format.NewProp("Thumbprint", "Thumbprint"),
|
||||||
format.NewProp("Status", "Status"),
|
format.NewProp("Status", "Status"),
|
||||||
format.NewProp("ContactedAt", "ContactedAt"),
|
format.NewProp("CreatedAt", "CreatedAt"),
|
||||||
format.NewProp("UpdatedAt", "UpdatedAt"),
|
format.NewProp("UpdatedAt", "UpdatedAt"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,6 @@ type DatabaseConfig struct {
|
|||||||
func NewDefaultDatabaseConfig() DatabaseConfig {
|
func NewDefaultDatabaseConfig() DatabaseConfig {
|
||||||
return DatabaseConfig{
|
return DatabaseConfig{
|
||||||
Driver: "sqlite",
|
Driver: "sqlite",
|
||||||
DSN: "sqlite://emissary.sqlite?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_txlock=immediate",
|
DSN: "sqlite://emissary.sqlite?_fk=true&_journal=WAL",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,15 +20,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
ID AgentID `json:"id"`
|
ID AgentID `json:"id"`
|
||||||
Label string `json:"label"`
|
Thumbprint string `json:"thumbprint"`
|
||||||
Thumbprint string `json:"thumbprint"`
|
KeySet *SerializableKeySet `json:"keyset,omitempty"`
|
||||||
KeySet *SerializableKeySet `json:"keyset,omitempty"`
|
Metadata map[string]any `json:"metadata,omitempty"`
|
||||||
Metadata map[string]any `json:"metadata,omitempty"`
|
Status AgentStatus `json:"status"`
|
||||||
Status AgentStatus `json:"status"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
|
||||||
ContactedAt *time.Time `json:"contactedAt,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SerializableKeySet struct {
|
type SerializableKeySet struct {
|
||||||
|
@ -2,7 +2,6 @@ package datastore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/lestrrat-go/jwx/v2/jwk"
|
"github.com/lestrrat-go/jwx/v2/jwk"
|
||||||
)
|
)
|
||||||
@ -69,12 +68,10 @@ func WithAgentQueryThumbprints(thumbprints ...string) AgentQueryOptionFunc {
|
|||||||
type AgentUpdateOptionFunc func(*AgentUpdateOptions)
|
type AgentUpdateOptionFunc func(*AgentUpdateOptions)
|
||||||
|
|
||||||
type AgentUpdateOptions struct {
|
type AgentUpdateOptions struct {
|
||||||
Label *string
|
Status *AgentStatus
|
||||||
Status *AgentStatus
|
Metadata *map[string]any
|
||||||
ContactedAt *time.Time
|
KeySet *jwk.Set
|
||||||
Metadata *map[string]any
|
Thumbprint *string
|
||||||
KeySet *jwk.Set
|
|
||||||
Thumbprint *string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithAgentUpdateStatus(status AgentStatus) AgentUpdateOptionFunc {
|
func WithAgentUpdateStatus(status AgentStatus) AgentUpdateOptionFunc {
|
||||||
@ -100,15 +97,3 @@ func WithAgentUpdateThumbprint(thumbprint string) AgentUpdateOptionFunc {
|
|||||||
opts.Thumbprint = &thumbprint
|
opts.Thumbprint = &thumbprint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithAgentUpdateLabel(label string) AgentUpdateOptionFunc {
|
|
||||||
return func(opts *AgentUpdateOptions) {
|
|
||||||
opts.Label = &label
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithAgentUpdateContactedAt(contactedAt time.Time) AgentUpdateOptionFunc {
|
|
||||||
return func(opts *AgentUpdateOptions) {
|
|
||||||
opts.ContactedAt = &contactedAt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -127,7 +127,7 @@ func (r *AgentRepository) Query(ctx context.Context, opts ...datastore.AgentQuer
|
|||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
||||||
query := `SELECT id, label, thumbprint, status, contacted_at, created_at, updated_at FROM agents`
|
query := `SELECT id, thumbprint, status, created_at, updated_at FROM agents`
|
||||||
|
|
||||||
limit := 10
|
limit := 10
|
||||||
if options.Limit != nil {
|
if options.Limit != nil {
|
||||||
@ -194,16 +194,12 @@ func (r *AgentRepository) Query(ctx context.Context, opts ...datastore.AgentQuer
|
|||||||
agent := &datastore.Agent{}
|
agent := &datastore.Agent{}
|
||||||
|
|
||||||
metadata := JSONMap{}
|
metadata := JSONMap{}
|
||||||
contactedAt := sql.NullTime{}
|
|
||||||
|
|
||||||
if err := rows.Scan(&agent.ID, &agent.Label, &agent.Thumbprint, &agent.Status, &contactedAt, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
if err := rows.Scan(&agent.ID, &agent.Thumbprint, &agent.Status, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
agent.Metadata = metadata
|
agent.Metadata = metadata
|
||||||
if contactedAt.Valid {
|
|
||||||
agent.ContactedAt = &contactedAt.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
agents = append(agents, agent)
|
agents = append(agents, agent)
|
||||||
}
|
}
|
||||||
@ -319,7 +315,7 @@ func (r *AgentRepository) Get(ctx context.Context, id datastore.AgentID) (*datas
|
|||||||
|
|
||||||
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
||||||
query := `
|
query := `
|
||||||
SELECT "id", "label", "thumbprint", "keyset", "metadata", "status", "contacted_at", "created_at", "updated_at"
|
SELECT "id", "thumbprint", "keyset", "metadata", "status", "created_at", "updated_at"
|
||||||
FROM agents
|
FROM agents
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
@ -327,10 +323,9 @@ func (r *AgentRepository) Get(ctx context.Context, id datastore.AgentID) (*datas
|
|||||||
row := r.db.QueryRowContext(ctx, query, id)
|
row := r.db.QueryRowContext(ctx, query, id)
|
||||||
|
|
||||||
metadata := JSONMap{}
|
metadata := JSONMap{}
|
||||||
contactedAt := sql.NullTime{}
|
|
||||||
var rawKeySet []byte
|
var rawKeySet []byte
|
||||||
|
|
||||||
if err := row.Scan(&agent.ID, &agent.Label, &agent.Thumbprint, &rawKeySet, &metadata, &agent.Status, &contactedAt, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
if err := row.Scan(&agent.ID, &agent.Thumbprint, &rawKeySet, &metadata, &agent.Status, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return datastore.ErrNotFound
|
return datastore.ErrNotFound
|
||||||
}
|
}
|
||||||
@ -339,9 +334,6 @@ func (r *AgentRepository) Get(ctx context.Context, id datastore.AgentID) (*datas
|
|||||||
}
|
}
|
||||||
|
|
||||||
agent.Metadata = metadata
|
agent.Metadata = metadata
|
||||||
if contactedAt.Valid {
|
|
||||||
agent.ContactedAt = &contactedAt.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
keySet := jwk.NewSet()
|
keySet := jwk.NewSet()
|
||||||
if err := json.Unmarshal(rawKeySet, &keySet); err != nil {
|
if err := json.Unmarshal(rawKeySet, &keySet); err != nil {
|
||||||
@ -370,11 +362,15 @@ func (r *AgentRepository) Update(ctx context.Context, id datastore.AgentID, opts
|
|||||||
|
|
||||||
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
err := r.withTx(ctx, func(tx *sql.Tx) error {
|
||||||
query := `
|
query := `
|
||||||
UPDATE agents SET id = $1
|
UPDATE agents SET updated_at = $2
|
||||||
`
|
`
|
||||||
|
|
||||||
args := []any{id}
|
now := time.Now().UTC()
|
||||||
index := 2
|
|
||||||
|
args := []any{
|
||||||
|
id, now,
|
||||||
|
}
|
||||||
|
index := 3
|
||||||
|
|
||||||
if options.Status != nil {
|
if options.Status != nil {
|
||||||
query += fmt.Sprintf(`, status = $%d`, index)
|
query += fmt.Sprintf(`, status = $%d`, index)
|
||||||
@ -399,51 +395,23 @@ func (r *AgentRepository) Update(ctx context.Context, id datastore.AgentID, opts
|
|||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Label != nil {
|
|
||||||
query += fmt.Sprintf(`, label = $%d`, index)
|
|
||||||
args = append(args, *options.Label)
|
|
||||||
index++
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.ContactedAt != nil {
|
|
||||||
query += fmt.Sprintf(`, contacted_at = $%d`, index)
|
|
||||||
utc := options.ContactedAt.UTC()
|
|
||||||
args = append(args, utc)
|
|
||||||
index++
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.Metadata != nil {
|
if options.Metadata != nil {
|
||||||
query += fmt.Sprintf(`, metadata = $%d`, index)
|
query += fmt.Sprintf(`, metadata = $%d`, index)
|
||||||
args = append(args, JSONMap(*options.Metadata))
|
args = append(args, JSONMap(*options.Metadata))
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
updated := options.Metadata != nil ||
|
|
||||||
options.Status != nil ||
|
|
||||||
options.Label != nil ||
|
|
||||||
options.KeySet != nil ||
|
|
||||||
options.Thumbprint != nil
|
|
||||||
if updated {
|
|
||||||
now := time.Now().UTC()
|
|
||||||
query += fmt.Sprintf(`, updated_at = $%d`, index)
|
|
||||||
args = append(args, now)
|
|
||||||
index++
|
|
||||||
}
|
|
||||||
|
|
||||||
query += `
|
query += `
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING "id", "label", "thumbprint", "keyset", "metadata", "status", "contacted_at", "created_at", "updated_at"
|
RETURNING "id", "thumbprint", "keyset", "metadata", "status", "created_at", "updated_at"
|
||||||
`
|
`
|
||||||
|
|
||||||
logger.Debug(ctx, "executing query", logger.F("query", query), logger.F("args", args))
|
|
||||||
|
|
||||||
row := tx.QueryRowContext(ctx, query, args...)
|
row := tx.QueryRowContext(ctx, query, args...)
|
||||||
|
|
||||||
metadata := JSONMap{}
|
metadata := JSONMap{}
|
||||||
contactedAt := sql.NullTime{}
|
|
||||||
var rawKeySet []byte
|
var rawKeySet []byte
|
||||||
|
|
||||||
if err := row.Scan(&agent.ID, &agent.Label, &agent.Thumbprint, &rawKeySet, &metadata, &agent.Status, &contactedAt, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
if err := row.Scan(&agent.ID, &agent.Thumbprint, &rawKeySet, &metadata, &agent.Status, &agent.CreatedAt, &agent.UpdatedAt); err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return datastore.ErrNotFound
|
return datastore.ErrNotFound
|
||||||
}
|
}
|
||||||
@ -452,9 +420,6 @@ func (r *AgentRepository) Update(ctx context.Context, id datastore.AgentID, opts
|
|||||||
}
|
}
|
||||||
|
|
||||||
agent.Metadata = metadata
|
agent.Metadata = metadata
|
||||||
if contactedAt.Valid {
|
|
||||||
agent.ContactedAt = &contactedAt.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
keySet := jwk.NewSet()
|
keySet := jwk.NewSet()
|
||||||
if err := json.Unmarshal(rawKeySet, &keySet); err != nil {
|
if err := json.Unmarshal(rawKeySet, &keySet); err != nil {
|
||||||
|
@ -145,7 +145,6 @@ func (s *Server) registerAgent(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
type updateAgentRequest struct {
|
type updateAgentRequest struct {
|
||||||
Status *datastore.AgentStatus `json:"status" validate:"omitempty,oneof=0 1 2 3"`
|
Status *datastore.AgentStatus `json:"status" validate:"omitempty,oneof=0 1 2 3"`
|
||||||
Label *string `json:"label" validate:"omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) updateAgent(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) updateAgent(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -167,10 +166,6 @@ func (s *Server) updateAgent(w http.ResponseWriter, r *http.Request) {
|
|||||||
options = append(options, datastore.WithAgentUpdateStatus(*updateAgentReq.Status))
|
options = append(options, datastore.WithAgentUpdateStatus(*updateAgentReq.Status))
|
||||||
}
|
}
|
||||||
|
|
||||||
if updateAgentReq.Label != nil {
|
|
||||||
options = append(options, datastore.WithAgentUpdateLabel(*updateAgentReq.Label))
|
|
||||||
}
|
|
||||||
|
|
||||||
agent, err := s.agentRepo.Update(
|
agent, err := s.agentRepo.Update(
|
||||||
ctx,
|
ctx,
|
||||||
datastore.AgentID(agentID),
|
datastore.AgentID(agentID),
|
||||||
|
@ -1 +0,0 @@
|
|||||||
ALTER TABLE agents DROP COLUMN label;
|
|
@ -1 +0,0 @@
|
|||||||
ALTER TABLE agents ADD COLUMN label TEXT DEFAULT "";
|
|
@ -1 +0,0 @@
|
|||||||
ALTER TABLE agents DROP COLUMN contacted_at;
|
|
@ -1 +0,0 @@
|
|||||||
ALTER TABLE agents ADD COLUMN contacted_at datetime;
|
|
@ -9,7 +9,7 @@ server:
|
|||||||
port: 3000
|
port: 3000
|
||||||
database:
|
database:
|
||||||
driver: sqlite
|
driver: sqlite
|
||||||
dsn: sqlite:///var/lib/emissary/data.sqlite?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_txlock=immediate
|
dsn: sqlite:///var/lib/emissary/data.sqlite?_fk=true&_journal=WAL
|
||||||
cors:
|
cors:
|
||||||
allowedOrigins: []
|
allowedOrigins: []
|
||||||
allowCredentials: true
|
allowCredentials: true
|
||||||
|
Reference in New Issue
Block a user