-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tn/add project vars #934
Tn/add project vars #934
Changes from all commits
b446523
6333911
d2d2055
c63dcdb
a805347
79a79d6
e373ba6
fcac94d
dddddc1
cbabd0b
61e20c8
be5f371
f52a73c
a0c1f6c
44c4050
7ecae28
8cb3382
f4a400d
cf84160
71c9331
fd91886
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is VARCHAR(255) large enough? What if* I wanted to pass an RSA key or longer document? Maybe this isn't a huge deal for the first pass since we can grow the field later? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh that's a good point - yeah I can def change that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed the type to TEXT - how does that sound? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just tested by adding a really long doc and the UI 'breaks' so I'll fix that as well |
||
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; |
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); |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha I'm all about 1) honoring convention and 2) referencing media franchises from my childhood in my work