Skip to content

Commit 0461700

Browse files
authored
Merge pull request #458 from jetstack/venafi-agent-configuration
Refactor the venafi-cloud configuration
2 parents 7bf6883 + f059a1a commit 0461700

File tree

5 files changed

+70
-47
lines changed

5 files changed

+70
-47
lines changed

cmd/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var agentRBACCmd = &cobra.Command{
4141
if err != nil {
4242
log.Fatalf("Failed to read config file: %s", err)
4343
}
44-
config, err := agent.ParseConfig(b)
44+
config, err := agent.ParseConfig(b, false)
4545
if err != nil {
4646
log.Fatalf("Failed to parse config file: %s", err)
4747
}

pkg/agent/config.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/hashicorp/go-multierror"
9+
"github.com/jetstack/preflight/pkg/client"
910
"github.com/jetstack/preflight/pkg/datagatherer"
1011
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
1112
"github.com/jetstack/preflight/pkg/datagatherer/local"
@@ -126,22 +127,19 @@ func (c *Config) Dump() (string, error) {
126127
return string(d), nil
127128
}
128129

129-
func (c *Config) validate() error {
130+
func (c *Config) validate(isVenafiCloudMode bool) error {
130131
var result *multierror.Error
131132

132133
// configured for Venafi Cloud
133134
if c.VenafiCloud != nil {
134-
if c.VenafiCloud.UploaderID == "" {
135-
result = multierror.Append(result, fmt.Errorf("upload_id is required in Venafi Cloud mode"))
136-
}
137135
if c.VenafiCloud.UploadPath == "" {
138136
result = multierror.Append(result, fmt.Errorf("upload_path is required in Venafi Cloud mode"))
139137
}
140138

141139
if _, err := url.Parse(c.VenafiCloud.UploadPath); err != nil {
142140
result = multierror.Append(result, fmt.Errorf("upload_path is not a valid URL"))
143141
}
144-
} else {
142+
} else if !isVenafiCloudMode {
145143
if c.OrganizationID == "" {
146144
result = multierror.Append(result, fmt.Errorf("organization_id is required"))
147145
}
@@ -169,7 +167,7 @@ func (c *Config) validate() error {
169167
}
170168

171169
// ParseConfig reads config into a struct used to configure running agents
172-
func ParseConfig(data []byte) (Config, error) {
170+
func ParseConfig(data []byte, isVenafiCloudMode bool) (Config, error) {
173171
var config Config
174172

175173
err := yaml.Unmarshal(data, &config)
@@ -178,18 +176,18 @@ func ParseConfig(data []byte) (Config, error) {
178176
}
179177

180178
if config.Server == "" && config.Endpoint.Host == "" && config.Endpoint.Path == "" {
179+
config.Server = "https://preflight.jetstack.io"
181180
if config.VenafiCloud != nil {
182-
config.Server = "https://api.venafi.cloud"
183-
} else {
184-
config.Server = "https://preflight.jetstack.io"
181+
config.Server = client.VenafiCloudProdURL
185182
}
186183
}
187184

188185
if config.Endpoint.Protocol == "" && config.Server == "" {
189186
config.Endpoint.Protocol = "http"
190187
}
191188

192-
if err = config.validate(); err != nil {
189+
err = config.validate(isVenafiCloudMode)
190+
if err != nil {
193191
return config, err
194192
}
195193

pkg/agent/config_test.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestValidConfigLoad(t *testing.T) {
2525
output-path: "/nothome"
2626
`
2727

28-
loadedConfig, err := ParseConfig([]byte(configFileContents))
28+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
2929
if err != nil {
3030
t.Fatalf("unexpected error: %v", err)
3131
}
@@ -68,7 +68,7 @@ func TestValidConfigWithEndpointLoad(t *testing.T) {
6868
always-fail: false
6969
`
7070

71-
loadedConfig, err := ParseConfig([]byte(configFileContents))
71+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
7272
if err != nil {
7373
t.Errorf("unexpected error: %v", err)
7474
}
@@ -114,7 +114,7 @@ func TestValidVenafiCloudConfigLoad(t *testing.T) {
114114
upload_path: "/testing/path"
115115
`
116116

117-
loadedConfig, err := ParseConfig([]byte(configFileContents))
117+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
118118
if err != nil {
119119
t.Fatalf("unexpected error: %v", err)
120120
}
@@ -149,7 +149,7 @@ func TestValidVenafiCloudConfigLoad(t *testing.T) {
149149
func TestInvalidConfigError(t *testing.T) {
150150
configFileContents := `data-gatherers: "things"`
151151

152-
_, parseError := ParseConfig([]byte(configFileContents))
152+
_, parseError := ParseConfig([]byte(configFileContents), false)
153153

154154
expectedError := fmt.Errorf("yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `things` into []agent.DataGatherer")
155155

@@ -159,26 +159,35 @@ func TestInvalidConfigError(t *testing.T) {
159159
}
160160

161161
func TestMissingConfigError(t *testing.T) {
162-
_, parseError := ParseConfig([]byte(""))
163-
164-
if parseError == nil {
165-
t.Fatalf("expected error, got nil")
166-
}
167-
168-
expectedErrorLines := []string{
169-
"2 errors occurred:",
170-
"\t* organization_id is required",
171-
"\t* cluster_id is required",
172-
"\n",
173-
}
174-
175-
expectedError := strings.Join(expectedErrorLines, "\n")
176-
177-
gotError := parseError.Error()
178-
179-
if gotError != expectedError {
180-
t.Errorf("\ngot=\n%v\nwant=\n%s\ndiff=\n%s", gotError, expectedError, diff.Diff(gotError, expectedError))
181-
}
162+
t.Run("fail to parse config if organization_id or cluster_id are missing (venafi-cloud not enabled)", func(t *testing.T) {
163+
_, parseError := ParseConfig([]byte(""), false)
164+
165+
if parseError == nil {
166+
t.Fatalf("expected error, got nil")
167+
}
168+
169+
expectedErrorLines := []string{
170+
"2 errors occurred:",
171+
"\t* organization_id is required",
172+
"\t* cluster_id is required",
173+
"\n",
174+
}
175+
176+
expectedError := strings.Join(expectedErrorLines, "\n")
177+
178+
gotError := parseError.Error()
179+
180+
if gotError != expectedError {
181+
t.Errorf("\ngot=\n%v\nwant=\n%s\ndiff=\n%s", gotError, expectedError, diff.Diff(gotError, expectedError))
182+
}
183+
})
184+
t.Run("successfully parse config if organization_id or cluster_id are missing (venafi-cloud is enabled)", func(t *testing.T) {
185+
_, parseError := ParseConfig([]byte(""), true)
186+
187+
if parseError != nil {
188+
t.Fatalf("unxexpected error, no error should have occured when parsing configuration: %s", parseError)
189+
}
190+
})
182191
}
183192

184193
func TestPartialMissingConfigError(t *testing.T) {
@@ -190,7 +199,7 @@ func TestPartialMissingConfigError(t *testing.T) {
190199
organization_id: "example"
191200
cluster_id: "example-cluster"
192201
data-gatherers:
193-
- kind: dummy`))
202+
- kind: dummy`), false)
194203

195204
if parseError == nil {
196205
t.Fatalf("expected error, got nil")
@@ -218,7 +227,7 @@ func TestInvalidServerError(t *testing.T) {
218227
cluster_id: "my_cluster"
219228
data-gatherers:
220229
- kind: dummy
221-
name: dummy`))
230+
name: dummy`), false)
222231

223232
if parseError == nil {
224233
t.Fatalf("expected error, got nil")
@@ -246,7 +255,7 @@ func TestInvalidDataGathered(t *testing.T) {
246255
path: /api/v1/data
247256
schedule: "* * * * *"
248257
data-gatherers:
249-
- kind: "foo"`))
258+
- kind: "foo"`), false)
250259

251260
if parseError == nil {
252261
t.Fatalf("expected error, got nil")

pkg/agent/run.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,16 @@ func getConfiguration() (Config, client.Client) {
198198
log.Fatalf("Failed to read config file: %s", err)
199199
}
200200

201-
config, err := ParseConfig(b)
201+
config, err := ParseConfig(b, VenafiCloudMode)
202202
if err != nil {
203203
log.Fatalf("Failed to parse config file: %s", err)
204204
}
205205

206+
if VenafiCloudMode {
207+
// if the venafi-cloud mode is enabled override config.Server
208+
config.Server = client.VenafiCloudProdURL
209+
}
210+
206211
baseURL := config.Server
207212
if baseURL == "" {
208213
log.Printf("Using deprecated Endpoint configuration. User Server instead.")
@@ -274,11 +279,15 @@ func createCredentialClient(credentials client.Credentials, config Config, agent
274279
switch creds := credentials.(type) {
275280
case *client.VenafiSvcAccountCredentials:
276281
log.Println("Venafi Cloud mode was specified, using Venafi Service Account authentication.")
277-
// check if config has Venafi Cloud data
278-
if config.VenafiCloud == nil {
279-
log.Fatalf("Failed to find config for venafi-cloud: required for Venafi Cloud mode")
282+
// check if config has Venafi Cloud data, use config data if it's present
283+
uploaderID := creds.ClientID
284+
uploadPath := ""
285+
if config.VenafiCloud != nil {
286+
log.Println("Loading uploader_id and upload_path from \"venafi-cloud\" configuration.")
287+
uploaderID = config.VenafiCloud.UploaderID
288+
uploadPath = config.VenafiCloud.UploadPath
280289
}
281-
return client.NewVenafiCloudClient(agentMetadata, creds, baseURL, config.VenafiCloud.UploaderID, config.VenafiCloud.UploadPath)
290+
return client.NewVenafiCloudClient(agentMetadata, creds, baseURL, uploaderID, uploadPath)
282291

283292
case *client.OAuthCredentials:
284293
log.Println("A credentials file was specified, using oauth authentication.")

pkg/client/client_venafi_cloud.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ type (
7070
)
7171

7272
const (
73-
vaasProdURL = "https://api.venafi.cloud"
74-
accessTokenEndpoint = "/v1/oauth/token/serviceaccount"
75-
requiredGrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
73+
// URL for the venafi-cloud backend services
74+
VenafiCloudProdURL = "https://api.venafi.cloud"
75+
defaultVenafiCloudUploadEndpoint = "v1/tlspk/uploads"
76+
accessTokenEndpoint = "/v1/oauth/token/serviceaccount"
77+
requiredGrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
7678
)
7779

7880
// NewVenafiCloudClient returns a new instance of the VenafiCloudClient type that will perform HTTP requests using a bearer token
@@ -93,6 +95,11 @@ func NewVenafiCloudClient(agentMetadata *api.AgentMetadata, credentials *VenafiS
9395
return nil, fmt.Errorf("cannot create VenafiCloudClient: invalid Venafi Cloud client configuration")
9496
}
9597

98+
if uploadPath == "" {
99+
// if the uploadPath is not given, use default upload path
100+
uploadPath = defaultVenafiCloudUploadEndpoint
101+
}
102+
96103
return &VenafiCloudClient{
97104
agentMetadata: agentMetadata,
98105
credentials: credentials,
@@ -283,7 +290,7 @@ func (c *VenafiCloudClient) sendHTTPRequest(request *http.Request, responseObjec
283290
}
284291

285292
func (c *VenafiCloudClient) generateAndSignJwtToken() (string, error) {
286-
prodURL, err := url.Parse(vaasProdURL)
293+
prodURL, err := url.Parse(VenafiCloudProdURL)
287294
if err != nil {
288295
return "", err
289296
}

0 commit comments

Comments
 (0)