Skip to content

Commit 2e5a0f8

Browse files
committed
venconn: ignore config's venafi-cloud.upload_path and uploader_id
1 parent 55c3ab1 commit 2e5a0f8

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

pkg/agent/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ var Prometheus bool
9898
// raw resource data of unstructuredList
9999
const schemaVersion string = "v2.0.0"
100100

101-
const inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
101+
const (
102+
inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
103+
)
102104

103105
// Run starts the agent process
104106
func Run(cmd *cobra.Command, args []string) {
@@ -325,7 +327,7 @@ func getConfiguration() (Config, client.Client) {
325327
// don't rewrite paths. Thus, we've disabled the ability to change this
326328
// value with the new --venafi-connection flag, and this field is simply
327329
// ignored.
328-
if config.VenafiCloud.UploadPath != "" {
330+
if config.VenafiCloud != nil && config.VenafiCloud.UploadPath != "" {
329331
log.Printf(`ignoring venafi-cloud.upload_path. In Venafi Connection mode, this field is not needed.`)
330332
}
331333

@@ -334,11 +336,11 @@ func getConfiguration() (Config, client.Client) {
334336
// reasons (but cannot be empty), we just ignore whatever the user has
335337
// set in the config file, and set it to an arbitrary value in the
336338
// client since it doesn't matter.
337-
if config.VenafiCloud.UploaderID != "" {
339+
if config.VenafiCloud != nil && config.VenafiCloud.UploaderID != "" {
338340
log.Printf(`ignoring venafi-cloud.uploader_id. In Venafi Connection mode, this field is not needed.`)
339341
}
340342

341-
preflightClient, err = client.NewVenConnClient(&http.Client{}, agentMetadata, baseURL, InstallNS, VenConnName, VenConnNS)
343+
preflightClient, err = client.NewVenConnClient(&http.Client{Timeout: time.Minute}, agentMetadata, baseURL, InstallNS, VenConnName, VenConnNS)
342344
case APIToken != "":
343345
logs.Log.Println("An API token was specified, using API token authentication.")
344346
preflightClient, err = client.NewAPITokenClient(agentMetadata, APIToken, baseURL)

pkg/client/client_venconn.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package client
33
import (
44
"bytes"
55
"context"
6+
"encoding/base64"
67
"encoding/json"
78
"fmt"
89
"io"
9-
"io/ioutil"
10-
"log"
1110
"log/slog"
1211
"net/http"
13-
"path/filepath"
1412
"time"
1513

1614
"github.com/go-logr/logr"
@@ -30,7 +28,7 @@ import (
3028
)
3129

3230
type VenConnClient struct {
33-
baseURL string
31+
baseURL string // E.g., "https://api.venafi.cloud" (trailing slash will be removed)
3432
agentMetadata *api.AgentMetadata
3533
connHandler venafi_client.ConnectionHandler
3634
installNS string // Namespace in which the agent is running in.
@@ -105,13 +103,24 @@ func (c *VenConnClient) Start(ctx context.Context) error {
105103
return c.connHandler.CacheRunnable().Start(ctx)
106104
}
107105

106+
// `opts.ClusterName` and `opts.ClusterDescription` are the only values used
107+
// from the Options struct. OrgID and ClusterID are not used in Venafi Cloud.
108108
func (c *VenConnClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
109-
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
110-
}
109+
if opts.ClusterName == "" {
110+
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty")
111+
}
112+
113+
_, token, err := c.connHandler.Get(context.Background(), c.installNS, auth.Scope{}, types.NamespacedName{Name: c.venConnName, Namespace: c.venConnNS})
114+
if err != nil {
115+
return fmt.Errorf("while loading the VenafiConnection %s/%s: %w", c.venConnNS, c.venConnName, err)
116+
}
117+
if token.TPPAccessToken != "" {
118+
return fmt.Errorf(`VenafiConnection %s/%s: the agent cannot be used with TPP`, c.venConnNS, c.venConnName)
119+
}
120+
if token.VCPAPIKey == "" && token.TPPAccessToken == "" {
121+
return fmt.Errorf(`programmer mistake: VenafiConnection %s/%s: no VCP API key or VCP access token was returned by connHandler.Get`, c.venConnNS, c.venConnName)
122+
}
111123

112-
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
113-
// viewing in the user-interface.
114-
func (c *VenConnClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {
115124
payload := api.DataReadingsPost{
116125
AgentMetadata: c.agentMetadata,
117126
DataGatherTime: time.Now().UTC(),
@@ -122,15 +131,40 @@ func (c *VenConnClient) PostDataReadings(orgID, clusterID string, readings []*ap
122131
return err
123132
}
124133

125-
res, err := c.Post(filepath.Join("/api/v1/org", orgID, "datareadings", clusterID), bytes.NewBuffer(data))
134+
// The path parameter "no" is a dummy parameter to make the Venafi Cloud
135+
// backend happy. This parameter, named `uploaderID` in the backend, is not
136+
// actually used by the backend.
137+
req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, "/v1/tlspk/upload/clusterdata/no"), bytes.NewBuffer(data))
138+
if err != nil {
139+
return err
140+
}
141+
142+
req.Header.Set("Content-Type", "application/json")
143+
req.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
144+
145+
if token.VCPAccessToken != "" {
146+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.VCPAccessToken))
147+
}
148+
if token.VCPAPIKey != "" {
149+
req.Header.Set("tppl-api-key", token.VCPAPIKey)
150+
}
151+
152+
q := req.URL.Query()
153+
q.Set("name", opts.ClusterName)
154+
if opts.ClusterDescription != "" {
155+
q.Set("description", base64.RawURLEncoding.EncodeToString([]byte(opts.ClusterDescription)))
156+
}
157+
req.URL.RawQuery = q.Encode()
158+
159+
res, err := c.client.Do(req)
126160
if err != nil {
127161
return err
128162
}
129163
defer res.Body.Close()
130164

131165
if code := res.StatusCode; code < 200 || code >= 300 {
132166
errorContent := ""
133-
body, err := ioutil.ReadAll(res.Body)
167+
body, err := io.ReadAll(res.Body)
134168
if err == nil {
135169
errorContent = string(body)
136170
}
@@ -143,11 +177,9 @@ func (c *VenConnClient) PostDataReadings(orgID, clusterID string, readings []*ap
143177

144178
// Post performs an HTTP POST request.
145179
func (c *VenConnClient) Post(path string, body io.Reader) (*http.Response, error) {
146-
// The VenafiConnection must be in the same namespace as the agent. It can't
147-
log.Printf("Getting Venafi connection details from %s/%s", c.venConnNS, c.venConnName)
148180
_, token, err := c.connHandler.Get(context.Background(), c.installNS, auth.Scope{}, types.NamespacedName{Name: c.venConnName, Namespace: c.venConnNS})
149181
if err != nil {
150-
return nil, err
182+
return nil, fmt.Errorf("while loading the VenafiConnection %s/%s: %w", c.venConnNS, c.venConnName, err)
151183
}
152184

153185
req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, path), body)

0 commit comments

Comments
 (0)