-
Notifications
You must be signed in to change notification settings - Fork 125
/
client_configuration.go
214 lines (196 loc) · 5.93 KB
/
client_configuration.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// log levels for easy logging
const (
levelOff string = "OFF" // log level for logging switched off
levelError string = "ERROR" // error log level
levelWarn string = "WARN" // warn log level
levelInfo string = "INFO" // info log level
levelDebug string = "DEBUG" // debug log level
levelTrace string = "TRACE" // trace log level
)
const (
defaultConfigName = "sf_client_config.json"
clientConfEnvName = "SF_CLIENT_CONFIG_FILE"
)
func getClientConfig(filePathFromConnectionString string) (*ClientConfig, string, error) {
configPredefinedDirPaths := clientConfigPredefinedDirs()
filePath, err := findClientConfigFilePath(filePathFromConnectionString, configPredefinedDirPaths)
if err != nil {
return nil, "", err
}
if filePath == "" { // we did not find a config file
return nil, "", nil
}
config, err := parseClientConfiguration(filePath)
return config, filePath, err
}
func findClientConfigFilePath(filePathFromConnectionString string, configPredefinedDirs []string) (string, error) {
if filePathFromConnectionString != "" {
logger.Infof("Using client configuration path from a connection string: %s", filePathFromConnectionString)
return filePathFromConnectionString, nil
}
envConfigFilePath := os.Getenv(clientConfEnvName)
if envConfigFilePath != "" {
logger.Infof("Using client configuration path from an environment variable: %s", envConfigFilePath)
return envConfigFilePath, nil
}
return searchForConfigFile(configPredefinedDirs)
}
func searchForConfigFile(directories []string) (string, error) {
for _, dir := range directories {
filePath := path.Join(dir, defaultConfigName)
exists, err := existsFile(filePath)
if err != nil {
return "", fmt.Errorf("error while searching for client config in directory: %s, err: %s", dir, err)
}
if exists {
logger.Infof("Using client configuration from a default directory: %s", filePath)
return filePath, nil
}
logger.Debugf("No client config found in directory: %s", dir)
}
logger.Info("No client config file found in default directories")
return "", nil
}
func existsFile(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
func clientConfigPredefinedDirs() []string {
var predefinedDirs []string
exeFile, err := os.Executable()
if err != nil {
logger.Warnf("Unable to access the application directory for client configuration search, err: %v", err)
} else {
predefinedDirs = append(predefinedDirs, filepath.Dir(exeFile))
}
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Warnf("Unable to access Home directory for client configuration search, err: %v", err)
} else {
predefinedDirs = append(predefinedDirs, homeDir)
}
if predefinedDirs == nil {
return []string{}
}
return predefinedDirs
}
// ClientConfig config root
type ClientConfig struct {
Common *ClientConfigCommonProps `json:"common"`
}
// ClientConfigCommonProps properties from "common" section
type ClientConfigCommonProps struct {
LogLevel string `json:"log_level,omitempty"`
LogPath string `json:"log_path,omitempty"`
}
func parseClientConfiguration(filePath string) (*ClientConfig, error) {
if filePath == "" {
return nil, nil
}
fileContents, err := os.ReadFile(filePath)
if err != nil {
return nil, parsingClientConfigError(err)
}
err = validateCfgPerm(filePath)
if err != nil {
return nil, parsingClientConfigError(err)
}
var clientConfig ClientConfig
err = json.Unmarshal(fileContents, &clientConfig)
if err != nil {
return nil, parsingClientConfigError(err)
}
unknownValues := getUnknownValues(fileContents)
if len(unknownValues) > 0 {
for val := range unknownValues {
logger.Warnf("Unknown configuration entry: %s with value: %s", val, unknownValues[val])
}
}
err = validateClientConfiguration(&clientConfig)
if err != nil {
return nil, parsingClientConfigError(err)
}
return &clientConfig, nil
}
func getUnknownValues(fileContents []byte) map[string]interface{} {
var values map[string]interface{}
err := json.Unmarshal(fileContents, &values)
if err != nil {
return nil
}
if values["common"] == nil {
return nil
}
commonValues := values["common"].(map[string]interface{})
lowercaseCommonValues := make(map[string]interface{}, len(commonValues))
for k, v := range commonValues {
lowercaseCommonValues[strings.ToLower(k)] = v
}
delete(lowercaseCommonValues, "log_level")
delete(lowercaseCommonValues, "log_path")
return lowercaseCommonValues
}
func parsingClientConfigError(err error) error {
return fmt.Errorf("parsing client config failed: %w", err)
}
func validateClientConfiguration(clientConfig *ClientConfig) error {
if clientConfig == nil {
return errors.New("client config not found")
}
if clientConfig.Common == nil {
return errors.New("common section in client config not found")
}
return validateLogLevel(*clientConfig)
}
func validateLogLevel(clientConfig ClientConfig) error {
var logLevel = clientConfig.Common.LogLevel
if logLevel != "" {
_, err := toLogLevel(logLevel)
if err != nil {
return err
}
}
return nil
}
func validateCfgPerm(filePath string) error {
if runtime.GOOS == "windows" {
return nil
}
stat, err := os.Stat(filePath)
if err != nil {
return err
}
perm := stat.Mode()
// Check if group (5th LSB) or others (2nd LSB) have a write permission to the file
if perm&(1<<4) != 0 || perm&(1<<1) != 0 {
return fmt.Errorf("configuration file: %s can be modified by group or others", filePath)
}
return nil
}
func toLogLevel(logLevelString string) (string, error) {
var logLevel = strings.ToUpper(logLevelString)
switch logLevel {
case levelOff, levelError, levelWarn, levelInfo, levelDebug, levelTrace:
return logLevel, nil
default:
return "", errors.New("unknown log level: " + logLevelString)
}
}