Skip to content

Commit 22a0e29

Browse files
authored
Merge pull request #278 from BaruchBilanski/SLK-86768-User-Agent
SLK-86768 | Add user agent for login
2 parents 79e5518 + d716d50 commit 22a0e29

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ builds:
1515
flags:
1616
- -trimpath
1717
ldflags:
18-
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
18+
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X github.com/aquasecurity/terraform-provider-aquasec/client.version={{.Version}}'
1919
goos:
2020
- freebsd
2121
- windows

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ default: build
1414
build:
1515
go get
1616
go mod vendor
17-
go build -ldflags "-X main.version=v${VERSION}" -o ${BINARY}
17+
go build -ldflags "-X main.version=v${VERSION} -X github.com/aquasecurity/terraform-provider-aquasec/client.version=v${VERSION}" -o ${BINARY}
1818

1919
install: build
2020
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}

client/client.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"crypto/x509"
66
"encoding/json"
77
"fmt"
8+
"log"
9+
neturl "net/url"
10+
811
"github.com/aquasecurity/terraform-provider-aquasec/consts"
912
"github.com/parnurzeal/gorequest"
1013
"golang.org/x/net/http/httpproxy"
1114
"golang.org/x/time/rate"
12-
"log"
13-
neturl "net/url"
1415
)
1516

1617
// Client - API client
@@ -30,6 +31,10 @@ const Csp string = "csp"
3031
const Saas = "saas"
3132
const SaasDev = "saasDev"
3233

34+
const UserAgentBase = "terraform-provider-aquasec"
35+
36+
var version string
37+
3338
// NewClient - initialize and return the Client
3439
func NewClient(url, user, password string, verifyTLS bool, caCertByte []byte) *Client {
3540
tlsConfig := &tls.Config{
@@ -47,11 +52,13 @@ func NewClient(url, user, password string, verifyTLS bool, caCertByte []byte) *C
4752
}
4853
}
4954

55+
request := gorequest.New().TLSClientConfig(tlsConfig)
56+
5057
c := &Client{
5158
url: url,
5259
user: user,
5360
password: password,
54-
gorequest: gorequest.New().TLSClientConfig(tlsConfig),
61+
gorequest: request,
5562
// we are setting rate limit for 10 connection per second
5663
limiter: rate.NewLimiter(10, 3),
5764
}
@@ -121,7 +128,7 @@ func (cli *Client) GetAuthToken() (string, string, error) {
121128

122129
// GetAuthToken - Connect to Aqua and return a JWT bearerToken (string)
123130
func (cli *Client) GetCspAuthToken() (string, error) {
124-
resp, body, errs := cli.gorequest.Post(cli.url + "/api/v1/login").
131+
resp, body, errs := cli.makeRequest().Post(cli.url + "/api/v1/login").
125132
Send(`{"id":"` + cli.user + `", "password":"` + cli.password + `"}`).End()
126133
if errs != nil {
127134
return "", getMergedError(errs)
@@ -164,7 +171,7 @@ func (cli *Client) GetUSEAuthToken() (string, string, error) {
164171
return "", "", fmt.Errorf(fmt.Sprintf("%v URL is not allowed USE url", cli.url))
165172
}
166173

167-
resp, body, errs := cli.gorequest.Post(cli.tokenUrl + "/v2/signin").
174+
resp, body, errs := cli.makeRequest().Post(cli.tokenUrl + "/v2/signin").
168175
Send(`{"email":"` + cli.user + `", "password":"` + cli.password + `"}`).End()
169176
if errs != nil {
170177
return "", "", getMergedError(errs)
@@ -198,3 +205,8 @@ func (cli *Client) GetUSEAuthToken() (string, string, error) {
198205

199206
return "", "", fmt.Errorf("request failed. status: %s, response: %s", resp.Status, body)
200207
}
208+
209+
func (cli *Client) makeRequest() *gorequest.SuperAgent {
210+
userAgent := fmt.Sprintf("%s/%s", UserAgentBase, version)
211+
return cli.gorequest.Clone().Set("User-Agent", userAgent)
212+
}

0 commit comments

Comments
 (0)