/
secret.go
83 lines (66 loc) · 2.01 KB
/
secret.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
// SPDX-License-Identifier: Apache-2.0
package secret
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type (
// config represents the settings required to create the engine that implements the SecretInterface interface.
config struct {
// specifies the encryption key to use for the Secret engine
EncryptionKey string
// specifies to skip creating tables and indexes for the Secret engine
SkipCreation bool
}
// engine represents the secret functionality that implements the SecretInterface interface.
engine struct {
// engine configuration settings used in secret functions
config *config
ctx context.Context
// gorm.io/gorm database client used in secret functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
client *gorm.DB
// sirupsen/logrus logger used in secret functions
//
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
logger *logrus.Entry
}
)
// New creates and returns a Vela service for integrating with secrets in the database.
//
//nolint:revive // ignore returning unexported engine
func New(opts ...EngineOpt) (*engine, error) {
// create new Secret engine
e := new(engine)
// create new fields
e.client = new(gorm.DB)
e.config = new(config)
e.logger = new(logrus.Entry)
// apply all provided configuration options
for _, opt := range opts {
err := opt(e)
if err != nil {
return nil, err
}
}
// check if we should skip creating secret database objects
if e.config.SkipCreation {
e.logger.Warning("skipping creation of secrets table and indexes in the database")
return e, nil
}
// create the secrets table
err := e.CreateSecretTable(e.ctx, e.client.Config.Dialector.Name())
if err != nil {
return nil, fmt.Errorf("unable to create %s table: %w", constants.TableSecret, err)
}
// create the indexes for the secrets table
err = e.CreateSecretIndexes(e.ctx)
if err != nil {
return nil, fmt.Errorf("unable to create indexes for %s table: %w", constants.TableSecret, err)
}
return e, nil
}