-
Notifications
You must be signed in to change notification settings - Fork 13
/
build_executable.go
165 lines (133 loc) · 4.28 KB
/
build_executable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: Apache-2.0
package database
import (
"database/sql"
"encoding/base64"
"errors"
"github.com/go-vela/types/library"
)
var (
// ErrEmptyBuildExecutableBuildID defines the error type when a
// BuildExecutable type has an empty BuildID field provided.
ErrEmptyBuildExecutableBuildID = errors.New("empty build_executable build_id provided")
)
// BuildExecutable is the database representation of a BuildExecutable.
type BuildExecutable struct {
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
Data []byte `sql:"data"`
}
// Compress will manipulate the existing data for the
// BuildExecutable by compressing that data. This produces
// a significantly smaller amount of data that is
// stored in the system.
func (b *BuildExecutable) Compress(level int) error {
// compress the database BuildExecutable data
data, err := compress(level, b.Data)
if err != nil {
return err
}
// overwrite database BuildExecutable data with compressed BuildExecutable data
b.Data = data
return nil
}
// Decompress will manipulate the existing data for the
// BuildExecutable by decompressing that data. This allows us
// to have a significantly smaller amount of data that
// is stored in the system.
func (b *BuildExecutable) Decompress() error {
// decompress the database BuildExecutable data
data, err := decompress(b.Data)
if err != nil {
return err
}
// overwrite compressed BuildExecutable data with decompressed BuildExecutable data
b.Data = data
return nil
}
// Decrypt will manipulate the existing executable data by
// base64 decoding that value. Then, a AES-256 cipher
// block is created from the encryption key in order to
// decrypt the base64 decoded secret value.
func (b *BuildExecutable) Decrypt(key string) error {
dst := make([]byte, base64.StdEncoding.DecodedLen(len(b.Data)))
// base64 decode the encrypted repo hash
n, err := base64.StdEncoding.Decode(dst, b.Data)
if err != nil {
return err
}
dst = dst[:n]
// decrypt the base64 decoded executable data
decrypted, err := decrypt(key, dst)
if err != nil {
return err
}
// set the decrypted executable
b.Data = decrypted
return nil
}
// Encrypt will manipulate the existing build executable by
// creating a AES-256 cipher block from the encryption
// key in order to encrypt the build executable. Then, the
// build executable is base64 encoded for transport across
// network boundaries.
func (b *BuildExecutable) Encrypt(key string) error {
// encrypt the executable data
encrypted, err := encrypt(key, b.Data)
if err != nil {
return err
}
// base64 encode the encrypted executable to make it network safe
dst := make([]byte, base64.StdEncoding.EncodedLen(len(encrypted)))
base64.StdEncoding.Encode(dst, encrypted)
b.Data = dst
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the BuildExecutable type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (b *BuildExecutable) Nullify() *BuildExecutable {
if b == nil {
return nil
}
// check if the ID field should be false
if b.ID.Int64 == 0 {
b.ID.Valid = false
}
// check if the BuildID field should be false
if b.BuildID.Int64 == 0 {
b.BuildID.Valid = false
}
return b
}
// ToLibrary converts the BuildExecutable type
// to a library BuildExecutable type.
func (b *BuildExecutable) ToLibrary() *library.BuildExecutable {
buildExecutable := new(library.BuildExecutable)
buildExecutable.SetID(b.ID.Int64)
buildExecutable.SetBuildID(b.BuildID.Int64)
buildExecutable.SetData(b.Data)
return buildExecutable
}
// Validate verifies the necessary fields for
// the BuildExecutable type are populated correctly.
func (b *BuildExecutable) Validate() error {
// verify the BuildID field is populated
if b.BuildID.Int64 <= 0 {
return ErrEmptyBuildExecutableBuildID
}
return nil
}
// BuildExecutableFromLibrary converts the library BuildExecutable type
// to a database BuildExecutable type.
func BuildExecutableFromLibrary(c *library.BuildExecutable) *BuildExecutable {
buildExecutable := &BuildExecutable{
ID: sql.NullInt64{Int64: c.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: c.GetBuildID(), Valid: true},
Data: c.GetData(),
}
return buildExecutable.Nullify()
}