@@ -3,14 +3,12 @@ package client
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "encoding/base64"
6
7
"encoding/json"
7
8
"fmt"
8
9
"io"
9
- "io/ioutil"
10
- "log"
11
10
"log/slog"
12
11
"net/http"
13
- "path/filepath"
14
12
"time"
15
13
16
14
"github.com/go-logr/logr"
@@ -30,7 +28,7 @@ import (
30
28
)
31
29
32
30
type VenConnClient struct {
33
- baseURL string
31
+ baseURL string // E.g., "https://api.venafi.cloud" (trailing slash will be removed)
34
32
agentMetadata * api.AgentMetadata
35
33
connHandler venafi_client.ConnectionHandler
36
34
installNS string // Namespace in which the agent is running in.
@@ -105,13 +103,24 @@ func (c *VenConnClient) Start(ctx context.Context) error {
105
103
return c .connHandler .CacheRunnable ().Start (ctx )
106
104
}
107
105
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.
108
108
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
+ }
111
123
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 {
115
124
payload := api.DataReadingsPost {
116
125
AgentMetadata : c .agentMetadata ,
117
126
DataGatherTime : time .Now ().UTC (),
@@ -122,15 +131,40 @@ func (c *VenConnClient) PostDataReadings(orgID, clusterID string, readings []*ap
122
131
return err
123
132
}
124
133
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 )
126
160
if err != nil {
127
161
return err
128
162
}
129
163
defer res .Body .Close ()
130
164
131
165
if code := res .StatusCode ; code < 200 || code >= 300 {
132
166
errorContent := ""
133
- body , err := ioutil .ReadAll (res .Body )
167
+ body , err := io .ReadAll (res .Body )
134
168
if err == nil {
135
169
errorContent = string (body )
136
170
}
@@ -143,11 +177,9 @@ func (c *VenConnClient) PostDataReadings(orgID, clusterID string, readings []*ap
143
177
144
178
// Post performs an HTTP POST request.
145
179
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 )
148
180
_ , token , err := c .connHandler .Get (context .Background (), c .installNS , auth.Scope {}, types.NamespacedName {Name : c .venConnName , Namespace : c .venConnNS })
149
181
if err != nil {
150
- return nil , err
182
+ return nil , fmt . Errorf ( "while loading the VenafiConnection %s/%s: %w" , c . venConnNS , c . venConnName , err )
151
183
}
152
184
153
185
req , err := http .NewRequest (http .MethodPost , fullURL (c .baseURL , path ), body )
0 commit comments