-
Notifications
You must be signed in to change notification settings - Fork 56
/
config.go
174 lines (142 loc) · 5.3 KB
/
config.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
166
167
168
169
170
171
172
173
174
/*
Copyright The Ratify Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/deislabs/ratify/internal/constants"
"github.com/deislabs/ratify/internal/logger"
exConfig "github.com/deislabs/ratify/pkg/executor/config"
"github.com/deislabs/ratify/pkg/homedir"
"github.com/deislabs/ratify/pkg/policyprovider"
pcConfig "github.com/deislabs/ratify/pkg/policyprovider/config"
pf "github.com/deislabs/ratify/pkg/policyprovider/factory"
"github.com/deislabs/ratify/pkg/referrerstore"
rsConfig "github.com/deislabs/ratify/pkg/referrerstore/config"
sf "github.com/deislabs/ratify/pkg/referrerstore/factory"
"github.com/deislabs/ratify/pkg/verifier"
vfConfig "github.com/deislabs/ratify/pkg/verifier/config"
vf "github.com/deislabs/ratify/pkg/verifier/factory"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
ConfigFileName = "config.json"
ConfigFileDir = ".ratify"
PluginsFolder = "plugins"
)
type Config struct {
StoresConfig rsConfig.StoresConfig `json:"store,omitempty"`
PoliciesConfig pcConfig.PoliciesConfig `json:"policy,omitempty"`
VerifiersConfig vfConfig.VerifiersConfig `json:"verifier,omitempty"`
ExecutorConfig exConfig.ExecutorConfig `json:"executor,omitempty"`
LoggerConfig logger.Config `json:"logger,omitempty"`
fileHash string `json:"-"`
}
var (
initConfigDir = new(sync.Once)
homeDir string
configDir string
defaultConfigFilePath string
defaultPluginsPath string
)
func InitDefaultPaths() {
if configDir != "" {
return
}
configDir = os.Getenv("RATIFY_CONFIG")
if configDir == "" {
configDir = filepath.Join(getHomeDir(), ConfigFileDir)
}
defaultPluginsPath = filepath.Join(configDir, PluginsFolder)
defaultConfigFilePath = filepath.Join(configDir, ConfigFileName)
}
func getHomeDir() string {
if homeDir == "" {
homeDir = homedir.Get()
}
return homeDir
}
// Returns created referer store, verifier, policyprovider objects from config
func CreateFromConfig(cf Config) ([]referrerstore.ReferrerStore, []verifier.ReferenceVerifier, policyprovider.PolicyProvider, error) {
stores, err := sf.CreateStoresFromConfig(cf.StoresConfig, GetDefaultPluginPath())
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to load store from config")
}
logrus.Infof("stores successfully created. number of stores %d", len(stores))
// in K8s , verifiers CR are deployed to specific namespace, namespace is not applicable in config file scenario
verifiers, err := vf.CreateVerifiersFromConfig(cf.VerifiersConfig, GetDefaultPluginPath(), constants.EmptyNamespace)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to load verifiers from config")
}
logrus.Infof("verifiers successfully created. number of verifiers %d", len(verifiers))
policyEnforcer, err := pf.CreatePolicyProviderFromConfig(cf.PoliciesConfig)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to load policy provider from config")
}
logrus.Infof("policies successfully created.")
return stores, verifiers, policyEnforcer, nil
}
// Load the config from file path provided, read from default path if configFilePath is empty
func Load(configFilePath string) (Config, error) {
config := Config{}
var err error
body, err := os.ReadFile(getConfigurationFile(configFilePath))
if err != nil {
return config, fmt.Errorf("unable to read config file at path %s: %w", configFilePath, err)
}
if err = json.Unmarshal(body, &config); err != nil {
return config, fmt.Errorf("unable to unmarshal config body: %w", err)
}
if config.fileHash, err = getFileHash(body); err != nil {
return config, fmt.Errorf("error getting configuration file hash error: %w", err)
}
return config, nil
}
func GetDefaultPluginPath() string {
if defaultPluginsPath == "" {
initConfigDir.Do(InitDefaultPaths)
}
return defaultPluginsPath
}
// GetLoggerConfig returns logger configuration from config file at specified path.
func GetLoggerConfig(configFilePath string) (logger.Config, error) {
config, err := Load(configFilePath)
if err != nil {
return logger.Config{}, fmt.Errorf("unable to load config: %w", err)
}
return config.LoggerConfig, nil
}
// if configFilePath is empty, return configuration path from environment variable
func getConfigurationFile(configFilePath string) string {
if configFilePath == "" {
if configDir == "" {
initConfigDir.Do(InitDefaultPaths)
}
return defaultConfigFilePath
}
return configFilePath
}
func getFileHash(file []byte) (fileHash string, err error) {
hash := sha256.New()
length, err := hash.Write(file)
if err != nil || length == 0 {
return "", fmt.Errorf("unable to generate hash for configuration file, err '%w', hash length %v", err, length)
}
return hex.EncodeToString(hash.Sum(nil)), nil
}