Skip to content

Commit 78a1d3b

Browse files
author
Oluwole Fadeyi
committed
feat(client): Allow agent to send cluster description
Allows the user to configure a cluster description to be used during the cluster registration in the Venafi control plane. Signed-off-by: Oluwole Fadeyi <[email protected]>
1 parent 02ca90b commit 78a1d3b

File tree

9 files changed

+101
-3
lines changed

9 files changed

+101
-3
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/juju/errors v1.0.0
1414
github.com/kylelemons/godebug v1.1.0
1515
github.com/maxatome/go-testdeep v1.14.0
16+
github.com/microcosm-cc/bluemonday v1.0.26
1617
github.com/pkg/errors v0.9.1
1718
github.com/pmylund/go-cache v2.1.0+incompatible
1819
github.com/prometheus/client_golang v1.18.0
@@ -27,7 +28,9 @@ require (
2728
)
2829

2930
require (
31+
github.com/aymerick/douceur v0.2.0 // indirect
3032
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
33+
github.com/gorilla/css v1.0.0 // indirect
3134
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
3235
golang.org/x/net v0.17.0 // indirect
3336
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/Jeffail/gabs/v2 v2.7.0 h1:Y2edYaTcE8ZpRsR2AtmPu5xQdFDIthFG0jYhu5PY8kg=
22
github.com/Jeffail/gabs/v2 v2.7.0/go.mod h1:dp5ocw1FvBBQYssgHsG7I1WYsiLRtkUaB1FEtSwvNUw=
3+
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
4+
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
35
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
46
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
57
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
@@ -31,6 +33,7 @@ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+
3133
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
3234
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
3335
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
36+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
3437
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
3538
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
3639
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
@@ -49,8 +52,11 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
4952
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
5053
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5154
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
55+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
5256
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5357
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
58+
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
59+
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
5460
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
5561
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
5662
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -70,6 +76,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
7076
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7177
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
7278
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
79+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
7380
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7481
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7582
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -87,6 +94,8 @@ github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvls
8794
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
8895
github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX48g9wI=
8996
github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
97+
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
98+
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
9099
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
91100
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
92101
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -95,7 +104,9 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
95104
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
96105
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
97106
github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE=
107+
github.com/onsi/ginkgo/v2 v2.9.4/go.mod h1:gCQYp2Q+kSoIj7ykSVb9nskRSsR6PUj4AiLywzIhbKM=
98108
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
109+
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
99110
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
100111
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
101112
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -111,6 +122,7 @@ github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGy
111122
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
112123
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
113124
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
125+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
114126
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
115127
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
116128
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
@@ -124,6 +136,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
124136
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
125137
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
126138
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
139+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
127140
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
128141
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
129142
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -177,6 +190,7 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
177190
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
178191
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
179192
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
193+
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
180194
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
181195
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
182196
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/agent/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type Config struct {
2626
// OrganizationID within Preflight that will receive the data.
2727
OrganizationID string `yaml:"organization_id"`
2828
// ClusterID is the cluster that the agent is scanning.
29-
ClusterID string `yaml:"cluster_id"`
30-
DataGatherers []DataGatherer `yaml:"data-gatherers"`
29+
ClusterID string `yaml:"cluster_id"`
30+
ClusterDescription string `yaml:"cluster_description"`
31+
DataGatherers []DataGatherer `yaml:"data-gatherers"`
3132
// InputPath replaces DataGatherers with input data file
3233
InputPath string `yaml:"input-path"`
3334
// OutputPath replaces Server with output data file

pkg/agent/run.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
411411

412412
if VenafiCloudMode {
413413
// orgID and clusterID are not required for Venafi Cloud auth
414-
err := preflightClient.PostDataReadings("", "", readings)
414+
err := preflightClient.PostDataReadingsWithOptions(readings, client.Options{
415+
ClusterName: config.ClusterID,
416+
ClusterDescription: config.ClusterDescription,
417+
})
415418
if err != nil {
416419
return fmt.Errorf("post to server failed: %+v", err)
417420
}

pkg/client/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ import (
1010
)
1111

1212
type (
13+
// Options is the struct describing additional information pertinent to an agent that isn't a data reading
14+
// These fields will then be uploaded together with data readings.
15+
Options struct {
16+
OrgID string
17+
ClusterID string
18+
ClusterName string
19+
ClusterDescription string
20+
}
21+
1322
// The Client interface describes types that perform requests against the Jetstack Secure backend.
1423
Client interface {
1524
PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error
25+
PostDataReadingsWithOptions(readings []*api.DataReading, options Options) error
1626
Post(path string, body io.Reader) (*http.Response, error)
1727
}
1828

pkg/client/client_api_token.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ func NewAPITokenClient(agentMetadata *api.AgentMetadata, apiToken, baseURL strin
3939
}, nil
4040
}
4141

42+
// PostDataReadingsWithOptions uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
43+
// viewing in the user-interface.
44+
func (c *APITokenClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
45+
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
46+
}
47+
4248
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
4349
// viewing in the user-interface.
4450
func (c *APITokenClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {

pkg/client/client_oauth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func NewOAuthClient(agentMetadata *api.AgentMetadata, credentials *OAuthCredenti
9595
}, nil
9696
}
9797

98+
func (c *OAuthClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
99+
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
100+
}
101+
98102
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
99103
// viewing in the user-interface.
100104
func (c *OAuthClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {

pkg/client/client_unauthenticated.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func NewUnauthenticatedClient(agentMetadata *api.AgentMetadata, baseURL string)
3737
}, nil
3838
}
3939

40+
func (c *UnauthenticatedClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
41+
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
42+
}
43+
4044
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
4145
// viewing in the user-interface.
4246
func (c *UnauthenticatedClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {

pkg/client/client_venafi_cloud.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/ed25519"
88
"crypto/rsa"
99
"crypto/x509"
10+
"encoding/base64"
1011
"encoding/json"
1112
"encoding/pem"
1213
"fmt"
@@ -25,6 +26,7 @@ import (
2526
"github.com/google/uuid"
2627
"github.com/hashicorp/go-multierror"
2728
"github.com/jetstack/preflight/api"
29+
"github.com/microcosm-cc/bluemonday"
2830
)
2931

3032
type (
@@ -152,6 +154,57 @@ func (c *VenafiSvcAccountCredentials) IsClientSet() bool {
152154
return c.ClientID != "" && c.PrivateKeyFile != ""
153155
}
154156

157+
// PostDataReadingsWithOptions uploads the slice of api.DataReading to the Venafi Cloud backend to be processed.
158+
// The Options are then passed as URL params in the request
159+
func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
160+
payload := api.DataReadingsPost{
161+
AgentMetadata: c.agentMetadata,
162+
DataGatherTime: time.Now().UTC(),
163+
DataReadings: readings,
164+
}
165+
data, err := json.Marshal(payload)
166+
if err != nil {
167+
return err
168+
}
169+
170+
if !strings.HasSuffix(c.uploadPath, "/") {
171+
c.uploadPath = fmt.Sprintf("%s/", c.uploadPath)
172+
}
173+
174+
venafiCloudUploadURL, err := url.Parse(filepath.Join(c.uploadPath, c.uploaderID))
175+
if err != nil {
176+
return err
177+
}
178+
179+
// validate options and send them as URL params
180+
query := venafiCloudUploadURL.Query()
181+
stripHTML := bluemonday.StrictPolicy()
182+
if opts.ClusterName != "" {
183+
query.Add("name", stripHTML.Sanitize(opts.ClusterName))
184+
}
185+
if opts.ClusterDescription != "" {
186+
query.Add("description", base64.RawURLEncoding.EncodeToString([]byte(stripHTML.Sanitize(opts.ClusterDescription))))
187+
}
188+
venafiCloudUploadURL.RawQuery = query.Encode()
189+
190+
res, err := c.Post(venafiCloudUploadURL.String(), bytes.NewBuffer(data))
191+
if err != nil {
192+
return err
193+
}
194+
defer res.Body.Close()
195+
196+
if code := res.StatusCode; code < 200 || code >= 300 {
197+
errorContent := ""
198+
body, err := io.ReadAll(res.Body)
199+
if err == nil {
200+
errorContent = string(body)
201+
}
202+
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
203+
}
204+
205+
return nil
206+
}
207+
155208
// PostDataReadings uploads the slice of api.DataReading to the Venafi Cloud backend to be processed for later
156209
// viewing in the user-interface.
157210
func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api.DataReading) error {

0 commit comments

Comments
 (0)