-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.go
184 lines (156 loc) · 4.46 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
175
176
177
178
179
180
181
182
183
184
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// 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.
//
// A package to manage the Lacework configuration file ($HOME/.lacework.toml)
package lwconfig
import (
"bytes"
"fmt"
"os"
"path"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)
// Profiles is the representation of the $HOME/.lacework.toml
//
// Example:
//
// [default]
// account = "example"
// api_key = "EXAMPLE_0123456789"
// api_secret = "_0123456789"
//
// [dev]
// account = "dev"
// api_key = "DEV_0123456789"
// api_secret = "_0123456789"
//
// [prod]
// account = "coolcorp"
// subaccount = "prod-business"
// api_key = "PROD_0123456789"
// api_secret = "_0123456789"
// version = 2
type Profiles map[string]Profile
// Profile represents a single profile within a configuration file
type Profile struct {
Account string `toml:"account"`
Subaccount string `toml:"subaccount,omitempty"`
ApiKey string `toml:"api_key" survey:"api_key"`
ApiSecret string `toml:"api_secret" survey:"api_secret"`
Version int `toml:"version,omitzero"`
}
const (
ApiKeyMinLength = 55
ApiSecretMinLength = 30
)
// Verify will return an error is there is one required setting missing
func (p *Profile) Verify() error {
if p.Account == "" {
return errors.New("account missing")
}
if err := p.verifyApiKey(); err != nil {
return err
}
if err := p.verifyApiSecret(); err != nil {
return err
}
return nil
}
func (p *Profile) verifyApiKey() error {
if p.ApiKey == "" {
return errors.New("api_key missing")
}
if len(p.ApiKey) < ApiKeyMinLength {
return errors.New(fmt.Sprintf("api_key must have more than %d characters", ApiKeyMinLength))
}
return nil
}
func (p *Profile) verifyApiSecret() error {
if p.ApiSecret == "" {
return errors.New("api_secret missing")
}
if len(p.ApiSecret) < ApiSecretMinLength {
return errors.New(fmt.Sprintf("api_secret must have more than %d characters", ApiSecretMinLength))
}
return nil
}
// DefaultConfigPath returns the default path where the Lacework config file
// is located, which is at $HOME/.lacework.toml
func DefaultConfigPath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", err
}
return path.Join(home, ".lacework.toml"), nil
}
// LoadProfiles loads all the profiles from the default location ($HOME/.lacework.toml)
func LoadProfiles() (Profiles, error) {
configPath, err := DefaultConfigPath()
if err != nil {
return Profiles{}, err
}
return LoadProfilesFrom(configPath)
}
// LoadProfilesFrom loads all the profiles from the provided location
func LoadProfilesFrom(configPath string) (Profiles, error) {
if configPath == "" {
return Profiles{}, errors.New("unable to load profiles. Specify a configuration file.")
}
var profiles Profiles
if _, err := toml.DecodeFile(configPath, &profiles); err != nil {
return profiles, errors.Wrap(err, "unable to decode profiles from config")
}
return profiles, nil
}
// StoreProfileAt updates a single profile from the provided configuration file
func StoreProfileAt(configPath, name string, profile Profile) error {
if configPath == "" {
defaultPath, err := DefaultConfigPath()
if err != nil {
return err
}
configPath = defaultPath
}
var (
profiles = Profiles{}
err error
)
if _, err = os.Stat(configPath); err == nil {
if profiles, err = LoadProfilesFrom(configPath); err != nil {
return err
}
}
profiles[name] = profile
return StoreAt(configPath, profiles)
}
// StoreAt stores the provided profiles into the selected configuration file
func StoreAt(configPath string, profiles Profiles) error {
if configPath == "" {
defaultPath, err := DefaultConfigPath()
if err != nil {
return err
}
configPath = defaultPath
}
var buf = new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(profiles); err != nil {
return err
}
return os.WriteFile(configPath, buf.Bytes(), 0600)
}