Skip to content

Commit dba5d76

Browse files
committed
Refactor the venafi-cloud configuration
These changes solidify the way users configure the agent to communicate to venafi-cloud. It allows the agent to set the `--venafi-cloud` without having to define it in the `config.yaml`.
1 parent 7bf6883 commit dba5d76

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

pkg/agent/config.go

Lines changed: 3 additions & 6 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"
@@ -131,9 +132,6 @@ func (c *Config) validate() 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
}
@@ -178,10 +176,9 @@ 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

pkg/agent/run.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,18 @@ func getConfiguration() (Config, client.Client) {
199199
}
200200

201201
config, err := ParseConfig(b)
202-
if err != nil {
202+
switch {
203+
case err != nil && VenafiCloudMode && (config.OrganizationID == "" || config.ClusterID == ""):
204+
// venafi-cloud does not require the OrganizationID or ClusterID, do not error in case they are missing
205+
case err != nil:
203206
log.Fatalf("Failed to parse config file: %s", err)
204207
}
205208

209+
if VenafiCloudMode {
210+
// if the venafi-cloud mode is enabled override config.Server
211+
config.Server = client.VenafiCloudProdURL
212+
}
213+
206214
baseURL := config.Server
207215
if baseURL == "" {
208216
log.Printf("Using deprecated Endpoint configuration. User Server instead.")
@@ -274,11 +282,15 @@ func createCredentialClient(credentials client.Credentials, config Config, agent
274282
switch creds := credentials.(type) {
275283
case *client.VenafiSvcAccountCredentials:
276284
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")
285+
// check if config has Venafi Cloud data, use config data if it's present
286+
uploaderID := creds.ClientID
287+
uploadPath := ""
288+
if config.VenafiCloud != nil {
289+
log.Println("Loading uploader_id and upload_path from \"venafi-cloud\" configuration.")
290+
uploaderID = config.VenafiCloud.UploaderID
291+
uploadPath = config.VenafiCloud.UploadPath
280292
}
281-
return client.NewVenafiCloudClient(agentMetadata, creds, baseURL, config.VenafiCloud.UploaderID, config.VenafiCloud.UploadPath)
293+
return client.NewVenafiCloudClient(agentMetadata, creds, baseURL, uploaderID, uploadPath)
282294

283295
case *client.OAuthCredentials:
284296
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)