Skip to content
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

feat(secrets): add secret metadata #206

Merged
merged 16 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions database/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Secret struct {
Images pq.StringArray `sql:"images" gorm:"type:varchar(1000)"`
Events pq.StringArray `sql:"events" gorm:"type:varchar(1000)"`
AllowCommand sql.NullBool `sql:"allow_command"`
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullString `sql:"created_by"`
UpdatedAt sql.NullInt64 `sql:"updated_at"`
UpdatedBy sql.NullString `sql:"updated_by"`
}

// Decrypt will manipulate the existing secret value by
Expand Down Expand Up @@ -149,6 +153,26 @@ func (s *Secret) Nullify() *Secret {
s.Type.Valid = false
}

// check if the CreatedAt field should be false
if s.CreatedAt.Int64 == 0 {
s.CreatedAt.Valid = false
}

// check if the CreatedBy field should be false
if len(s.CreatedBy.String) == 0 {
s.CreatedBy.Valid = false
}

// check if the UpdatedAt field should be false
if s.UpdatedAt.Int64 == 0 {
s.UpdatedAt.Valid = false
}

// check if the UpdatedBy field should be false
if len(s.UpdatedBy.String) == 0 {
s.UpdatedBy.Valid = false
}

return s
}

Expand All @@ -167,6 +191,10 @@ func (s *Secret) ToLibrary() *library.Secret {
secret.SetImages(s.Images)
secret.SetEvents(s.Events)
secret.SetAllowCommand(s.AllowCommand.Bool)
secret.SetCreatedAt(s.CreatedAt.Int64)
secret.SetCreatedBy(s.CreatedBy.String)
secret.SetUpdatedAt(s.UpdatedAt.Int64)
secret.SetUpdatedBy(s.UpdatedBy.String)

return secret
}
Expand Down Expand Up @@ -249,6 +277,10 @@ func SecretFromLibrary(s *library.Secret) *Secret {
Images: pq.StringArray(s.GetImages()),
Events: pq.StringArray(s.GetEvents()),
AllowCommand: sql.NullBool{Bool: s.GetAllowCommand(), Valid: true},
CreatedAt: sql.NullInt64{Int64: s.GetCreatedAt(), Valid: true},
CreatedBy: sql.NullString{String: s.GetCreatedBy(), Valid: true},
UpdatedAt: sql.NullInt64{Int64: s.GetUpdatedAt(), Valid: true},
UpdatedBy: sql.NullString{String: s.GetUpdatedBy(), Valid: true},
}

return secret.Nullify()
Expand Down
37 changes: 30 additions & 7 deletions database/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import (
"database/sql"
"reflect"
"testing"
"time"

"github.com/go-vela/types/library"
)

var (
currentTime = time.Now()
tsCreate = currentTime.UTC().Unix()
tsUpdate = currentTime.Add(time.Hour * 1).UTC().Unix()
)

func TestDatabase_Secret_Decrypt(t *testing.T) {
// setup types
key := "C639A572E14D5075C526FDDD43E4ECF6"
Expand Down Expand Up @@ -108,13 +115,17 @@ func TestDatabase_Secret_Nullify(t *testing.T) {
var s *Secret

want := &Secret{
ID: sql.NullInt64{Int64: 0, Valid: false},
Org: sql.NullString{String: "", Valid: false},
Repo: sql.NullString{String: "", Valid: false},
Team: sql.NullString{String: "", Valid: false},
Name: sql.NullString{String: "", Valid: false},
Value: sql.NullString{String: "", Valid: false},
Type: sql.NullString{String: "", Valid: false},
ID: sql.NullInt64{Int64: 0, Valid: false},
Org: sql.NullString{String: "", Valid: false},
Repo: sql.NullString{String: "", Valid: false},
Team: sql.NullString{String: "", Valid: false},
Name: sql.NullString{String: "", Valid: false},
Value: sql.NullString{String: "", Valid: false},
Type: sql.NullString{String: "", Valid: false},
CreatedAt: sql.NullInt64{Int64: 0, Valid: false},
CreatedBy: sql.NullString{String: "", Valid: false},
UpdatedAt: sql.NullInt64{Int64: 0, Valid: false},
UpdatedBy: sql.NullString{String: "", Valid: false},
}

// setup tests
Expand Down Expand Up @@ -160,6 +171,10 @@ func TestDatabase_Secret_ToLibrary(t *testing.T) {
want.SetImages([]string{"alpine"})
want.SetEvents([]string{"push", "tag", "deployment"})
want.SetAllowCommand(true)
want.SetCreatedAt(tsCreate)
want.SetCreatedBy("octocat")
want.SetUpdatedAt(tsUpdate)
want.SetUpdatedBy("octocat2")

// run test
got := testSecret().ToLibrary()
Expand Down Expand Up @@ -279,6 +294,10 @@ func TestDatabase_SecretFromLibrary(t *testing.T) {
s.SetImages([]string{"alpine"})
s.SetEvents([]string{"push", "tag", "deployment"})
s.SetAllowCommand(true)
s.SetCreatedAt(tsCreate)
s.SetCreatedBy("octocat")
s.SetUpdatedAt(tsUpdate)
s.SetUpdatedBy("octocat2")

want := testSecret()

Expand All @@ -304,5 +323,9 @@ func testSecret() *Secret {
Images: []string{"alpine"},
Events: []string{"push", "tag", "deployment"},
AllowCommand: sql.NullBool{Bool: true, Valid: true},
CreatedAt: sql.NullInt64{Int64: tsCreate, Valid: true},
CreatedBy: sql.NullString{String: "octocat", Valid: true},
UpdatedAt: sql.NullInt64{Int64: tsUpdate, Valid: true},
UpdatedBy: sql.NullString{String: "octocat2", Valid: true},
}
}
120 changes: 120 additions & 0 deletions library/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Secret struct {
Images *[]string `json:"images,omitempty"`
Events *[]string `json:"events,omitempty"`
AllowCommand *bool `json:"allow_command,omitempty"`
CreatedAt *int64 `json:"created_at,omitempty"`
CreatedBy *string `json:"created_by,omitempty"`
ecrupper marked this conversation as resolved.
Show resolved Hide resolved
UpdatedAt *int64 `json:"updated_at,omitempty"`
UpdatedBy *string `json:"updated_by,omitempty"`
}
ecrupper marked this conversation as resolved.
Show resolved Hide resolved

// Sanitize creates a duplicate of the Secret without the value.
Expand All @@ -46,6 +50,10 @@ func (s *Secret) Sanitize() *Secret {
Images: s.Images,
Events: s.Events,
AllowCommand: s.AllowCommand,
CreatedAt: s.CreatedAt,
CreatedBy: s.CreatedBy,
UpdatedAt: s.UpdatedAt,
UpdatedBy: s.UpdatedBy,
}
}

Expand Down Expand Up @@ -227,6 +235,58 @@ func (s *Secret) GetAllowCommand() bool {
return *s.AllowCommand
}

// GetCreatedAt returns the CreatedAt field.
//
// When the provided Secret type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (s *Secret) GetCreatedAt() int64 {
// return zero value if Secret type or CreatedAt field is nil
if s == nil || s.CreatedAt == nil {
return 0
}

return *s.CreatedAt
}

// GetCreatedBy returns the CreatedBy field.
//
// When the provided Secret type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (s *Secret) GetCreatedBy() string {
// return zero value if Secret type or CreatedBy field is nil
if s == nil || s.CreatedBy == nil {
return ""
}

return *s.CreatedBy
}

// GetUpdatedAt returns the UpdatedAt field.
//
// When the provided Secret type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (s *Secret) GetUpdatedAt() int64 {
// return zero value if Secret type or UpdatedAt field is nil
if s == nil || s.UpdatedAt == nil {
return 0
}

return *s.UpdatedAt
}

// GetUpdatedBy returns the UpdatedBy field.
//
// When the provided Secret type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (s *Secret) GetUpdatedBy() string {
// return zero value if Secret type or UpdatedBy field is nil
if s == nil || s.UpdatedBy == nil {
return ""
}

return *s.UpdatedBy
}

// SetID sets the ID field.
//
// When the provided Secret type is nil, it
Expand Down Expand Up @@ -357,6 +417,58 @@ func (s *Secret) SetAllowCommand(v bool) {
s.AllowCommand = &v
}

// SetCreatedAt sets the CreatedAt field.
//
// When the provided Secret type is nil, it
// will set nothing and immediately return.
func (s *Secret) SetCreatedAt(v int64) {
// return if Secret type is nil
if s == nil {
return
}

s.CreatedAt = &v
}

// SetCreatedBy sets the CreatedBy field.
//
// When the provided Secret type is nil, it
// will set nothing and immediately return.
func (s *Secret) SetCreatedBy(v string) {
// return if Secret type is nil
if s == nil {
return
}

s.CreatedBy = &v
}

// SetUpdatedAt sets the UpdatedAt field.
//
// When the provided Secret type is nil, it
// will set nothing and immediately return.
func (s *Secret) SetUpdatedAt(v int64) {
// return if Secret type is nil
if s == nil {
return
}

s.UpdatedAt = &v
}

// SetUpdatedBy sets the UpdatedBy field.
//
// When the provided Secret type is nil, it
// will set nothing and immediately return.
func (s *Secret) SetUpdatedBy(v string) {
// return if Secret type is nil
if s == nil {
return
}

s.UpdatedBy = &v
}

// String implements the Stringer interface for the Secret type.
func (s *Secret) String() string {
return fmt.Sprintf(`{
Expand All @@ -370,6 +482,10 @@ func (s *Secret) String() string {
Team: %s,
Type: %s,
Value: %s,
CreatedAt: %d,
CreatedBy: %s,
UpdatedAt: %d,
UpdatedBy: %s,
}`,
s.GetAllowCommand(),
s.GetEvents(),
Expand All @@ -381,6 +497,10 @@ func (s *Secret) String() string {
s.GetTeam(),
s.GetType(),
s.GetValue(),
s.GetCreatedAt(),
s.GetCreatedBy(),
s.GetUpdatedAt(),
s.GetUpdatedBy(),
)
}

Expand Down
45 changes: 44 additions & 1 deletion library/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"
Expand Down Expand Up @@ -257,6 +258,18 @@ func TestLibrary_Secret_Getters(t *testing.T) {
if test.secret.GetAllowCommand() != test.want.GetAllowCommand() {
t.Errorf("GetAllowCommand is %v, want %v", test.secret.GetAllowCommand(), test.want.GetAllowCommand())
}
if test.secret.GetCreatedAt() != test.want.GetCreatedAt() {
t.Errorf("GetCreatedAt is %v, want %v", test.secret.GetCreatedAt(), test.want.GetCreatedAt())
}
if test.secret.GetCreatedBy() != test.want.GetCreatedBy() {
t.Errorf("GetCreatedBy is %v, want %v", test.secret.GetCreatedBy(), test.want.GetCreatedBy())
}
if test.secret.GetUpdatedAt() != test.want.GetUpdatedAt() {
t.Errorf("GetUpdatedAt is %v, want %v", test.secret.GetUpdatedAt(), test.want.GetUpdatedAt())
}
if test.secret.GetUpdatedBy() != test.want.GetUpdatedBy() {
t.Errorf("GetUpdatedBy is %v, want %v", test.secret.GetUpdatedBy(), test.want.GetUpdatedBy())
}
}
}

Expand Down Expand Up @@ -291,6 +304,10 @@ func TestLibrary_Secret_Setters(t *testing.T) {
test.secret.SetImages(test.want.GetImages())
test.secret.SetEvents(test.want.GetEvents())
test.secret.SetAllowCommand(test.want.GetAllowCommand())
test.secret.SetCreatedAt(test.want.GetCreatedAt())
test.secret.SetCreatedBy(test.want.GetCreatedBy())
test.secret.SetUpdatedAt(test.want.GetUpdatedAt())
test.secret.SetUpdatedBy(test.want.GetUpdatedBy())

if test.secret.GetID() != test.want.GetID() {
t.Errorf("SetID is %v, want %v", test.secret.GetID(), test.want.GetID())
Expand Down Expand Up @@ -322,6 +339,18 @@ func TestLibrary_Secret_Setters(t *testing.T) {
if test.secret.GetAllowCommand() != test.want.GetAllowCommand() {
t.Errorf("SetAllowCommand is %v, want %v", test.secret.GetAllowCommand(), test.want.GetAllowCommand())
}
if test.secret.GetCreatedAt() != test.want.GetCreatedAt() {
t.Errorf("SetCreatedAt is %v, want %v", test.secret.GetCreatedAt(), test.want.GetCreatedAt())
}
if test.secret.GetCreatedBy() != test.want.GetCreatedBy() {
t.Errorf("SetCreatedBy is %v, want %v", test.secret.GetCreatedBy(), test.want.GetCreatedBy())
}
if test.secret.GetUpdatedAt() != test.want.GetUpdatedAt() {
t.Errorf("SetUpdatedAt is %v, want %v", test.secret.GetUpdatedAt(), test.want.GetUpdatedAt())
}
if test.secret.GetUpdatedBy() != test.want.GetUpdatedBy() {
t.Errorf("SetUpdatedBy is %v, want %v", test.secret.GetUpdatedBy(), test.want.GetUpdatedBy())
}
}
}

Expand All @@ -340,6 +369,10 @@ func TestLibrary_Secret_String(t *testing.T) {
Team: %s,
Type: %s,
Value: %s,
CreatedAt: %d,
CreatedBy: %s,
UpdatedAt: %d,
UpdatedBy: %s,
}`,
s.GetAllowCommand(),
s.GetEvents(),
Expand All @@ -351,6 +384,10 @@ func TestLibrary_Secret_String(t *testing.T) {
s.GetTeam(),
s.GetType(),
s.GetValue(),
s.GetCreatedAt(),
s.GetCreatedBy(),
s.GetUpdatedAt(),
s.GetUpdatedBy(),
)

// run test
Expand All @@ -364,6 +401,9 @@ func TestLibrary_Secret_String(t *testing.T) {
// testSecret is a test helper function to create a Secret
// type with all fields set to a fake value.
func testSecret() *Secret {
currentTime := time.Now()
tsCreate := currentTime.UTC().Unix()
tsUpdate := currentTime.Add(time.Hour * 1).UTC().Unix()
s := new(Secret)

s.SetID(1)
Expand All @@ -376,6 +416,9 @@ func testSecret() *Secret {
s.SetImages([]string{"alpine"})
s.SetEvents([]string{"push", "tag", "deployment"})
s.SetAllowCommand(true)

s.SetCreatedAt(tsCreate)
s.SetCreatedBy("octocat")
s.SetUpdatedAt(tsUpdate)
s.SetUpdatedBy("octocat2")
return s
}