@@ -3,14 +3,12 @@ package client
33import (
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
3230type 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.
108108func (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.
145179func (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