-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add global variables table * set up global vars ui and backend * add tests / test data * remove logs * fix tests * resolve TODOS * add button to add new variables * remove pagination * change text * clean up outdated comments * update copyright * remove ID from DTO * delete unneeded permissions * shorten name for brevity * shorten name for brevity lowercase * add dash to match standard convention * allow for longer documents * remove copypasta * remove more copypasta * remove unused imports * remove values from main screen
- Loading branch information
Tyler Noblett
authored
Oct 2, 2023
1 parent
6a7aafa
commit 26d5492
Showing
28 changed files
with
795 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- +migrate Up | ||
CREATE TABLE global_vars ( | ||
id INT AUTO_INCREMENT, | ||
name VARCHAR(255) NOT NULL UNIQUE, | ||
value VARCHAR(255) NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP, | ||
PRIMARY KEY (id) | ||
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; | ||
|
||
-- +migrate Down | ||
DROP TABLE global_vars; |
6 changes: 6 additions & 0 deletions
6
backend/migrations/20230928144308-change-global-var-value-to-text.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- +migrate Up | ||
ALTER TABLE global_vars | ||
MODIFY COLUMN value TEXT; | ||
-- +migrate Down | ||
ALTER TABLE global_vars | ||
MODIFY COLUMN value VARCHAR(255); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2023, Yahoo Inc. | ||
// Licensed under the terms of the MIT. See LICENSE file in project root for terms. | ||
|
||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ashirt-ops/ashirt-server/backend" | ||
"github.com/ashirt-ops/ashirt-server/backend/database" | ||
"github.com/ashirt-ops/ashirt-server/backend/dtos" | ||
"github.com/ashirt-ops/ashirt-server/backend/models" | ||
"github.com/ashirt-ops/ashirt-server/backend/policy" | ||
"github.com/ashirt-ops/ashirt-server/backend/server/middleware" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
) | ||
|
||
type CreateGlobalVarInput struct { | ||
Name string | ||
OwnerID int64 | ||
Value string | ||
} | ||
|
||
type UpdateGlobalVarInput struct { | ||
Name string | ||
Value string | ||
NewName string | ||
} | ||
|
||
type DeleteGlobalVarInput struct { | ||
Name string | ||
} | ||
|
||
func CreateGlobalVar(ctx context.Context, db *database.Connection, i CreateGlobalVarInput) (*dtos.GlobalVar, error) { | ||
if err := policy.Require(middleware.Policy(ctx), policy.AdminUsersOnly{}); err != nil { | ||
return nil, backend.WrapError("Unable to create global variable", backend.UnauthorizedWriteErr(err)) | ||
} | ||
|
||
if i.Name == "" { | ||
return nil, backend.MissingValueErr("Name") | ||
} | ||
|
||
_, err := db.Insert("global_vars", map[string]interface{}{ | ||
"name": i.Name, | ||
"value": i.Value, | ||
}) | ||
if err != nil { | ||
if database.IsAlreadyExistsError(err) { | ||
return nil, backend.BadInputErr(backend.WrapError("global variable already exists", err), "A global variable with this name already exists") | ||
} | ||
return nil, backend.WrapError("Unable to add new global variable", backend.DatabaseErr(err)) | ||
} | ||
|
||
return &dtos.GlobalVar{ | ||
Name: i.Name, | ||
Value: i.Value, | ||
}, nil | ||
} | ||
|
||
func DeleteGlobalVar(ctx context.Context, db *database.Connection, name string) error { | ||
if err := policyRequireWithAdminBypass(ctx, policy.AdminUsersOnly{}); err != nil { | ||
return backend.WrapError("Unwilling to delete global variable", backend.UnauthorizedWriteErr(err)) | ||
} | ||
|
||
err := db.Delete(sq.Delete("global_vars").Where(sq.Eq{"name": name})) | ||
if err != nil { | ||
return backend.WrapError("Cannot delete global variable", backend.DatabaseErr(err)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ListGlobalVars(ctx context.Context, db *database.Connection) ([]*dtos.GlobalVar, error) { | ||
if err := policy.Require(middleware.Policy(ctx), policy.AdminUsersOnly{}); err != nil { | ||
return nil, backend.WrapError("Unwilling to list global variables", backend.UnauthorizedReadErr(err)) | ||
} | ||
|
||
var globalVars = make([]models.GlobalVar, 0) | ||
err := db.Select(&globalVars, sq.Select("*"). | ||
From("global_vars"). | ||
OrderBy("name ASC")) | ||
|
||
if err != nil { | ||
return nil, backend.WrapError("Cannot list global variables", backend.DatabaseErr(err)) | ||
} | ||
|
||
var globalVarsDTO = make([]*dtos.GlobalVar, len(globalVars)) | ||
for i, globalVar := range globalVars { | ||
globalVarsDTO[i] = &dtos.GlobalVar{ | ||
Name: globalVar.Name, | ||
Value: globalVar.Value, | ||
} | ||
} | ||
|
||
return globalVarsDTO, nil | ||
} | ||
|
||
func UpdateGlobalVar(ctx context.Context, db *database.Connection, i UpdateGlobalVarInput) error { | ||
globalVar, err := LookupGlobalVar(db, i.Name) | ||
if err != nil { | ||
return backend.WrapError("Unable to update operation", backend.UnauthorizedWriteErr(err)) | ||
} | ||
|
||
if err := policyRequireWithAdminBypass(ctx, policy.AdminUsersOnly{}); err != nil { | ||
return backend.WrapError("Unwilling to update operation", backend.UnauthorizedWriteErr(err)) | ||
} | ||
|
||
var val string | ||
var name string | ||
|
||
if i.Value != "" { | ||
val = i.Value | ||
} else { | ||
val = globalVar.Value | ||
} | ||
|
||
if i.NewName != "" { | ||
name = i.NewName | ||
} else { | ||
name = globalVar.Name | ||
} | ||
|
||
err = db.Update(sq.Update("global_vars"). | ||
SetMap(map[string]interface{}{ | ||
"value": val, | ||
"name": name, | ||
}). | ||
Where(sq.Eq{"id": globalVar.ID})) | ||
if err != nil { | ||
if database.IsAlreadyExistsErrorSq(err) { | ||
return backend.BadInputErr(backend.WrapError("Global variable already exists", err), "A global variable with this name already exists") | ||
} | ||
return backend.WrapError("Cannot update global variable", backend.DatabaseErr(err)) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.