Skip to content

Commit 09ca725

Browse files
Merge pull request #11 from deploymenttheory/dev-loadbalance
Dev loadbalance
2 parents dd17259 + a2fb46a commit 09ca725

File tree

11 files changed

+71
-95
lines changed

11 files changed

+71
-95
lines changed

go.mod

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@ module github.com/deploymenttheory/go-api-http-client-integrations
22

33
go 1.22.4
44

5-
require (
6-
github.com/deploymenttheory/go-api-http-client v0.2.11
7-
go.uber.org/zap v1.27.0
8-
)
5+
require go.uber.org/zap v1.27.0
96

107
require (
11-
github.com/antchfx/xmlquery v1.4.1 // indirect
12-
github.com/antchfx/xpath v1.3.1 // indirect
13-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
14-
github.com/google/uuid v1.6.0 // indirect
8+
github.com/stretchr/testify v1.9.0 // indirect
159
go.uber.org/multierr v1.11.0 // indirect
16-
golang.org/x/net v0.26.0 // indirect
17-
golang.org/x/text v0.16.0 // indirect
1810
)

go.sum

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,14 @@
1-
github.com/antchfx/xmlquery v1.4.1 h1:YgpSwbeWvLp557YFTi8E3z6t6/hYjmFEtiEKbDfEbl0=
2-
github.com/antchfx/xmlquery v1.4.1/go.mod h1:lKezcT8ELGt8kW5L+ckFMTbgdR61/odpPgDv8Gvi1fI=
3-
github.com/antchfx/xpath v1.3.1 h1:PNbFuUqHwWl0xRjvUPjJ95Agbmdj2uzzIwmQKgu4oCk=
4-
github.com/antchfx/xpath v1.3.1/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
51
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
62
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/deploymenttheory/go-api-http-client v0.2.11 h1:hEVxXX60cUxRqKkJBHADC/S91+iCPYdBIwkT6stexTY=
8-
github.com/deploymenttheory/go-api-http-client v0.2.11/go.mod h1:LKDnBcieS6CyikZjTKPpziVdxnTwzBHE6Hx1cuWRcuU=
9-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
10-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
11-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
12-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
133
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
144
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
155
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
166
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
187
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
198
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
209
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
2110
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
2211
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
2312
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
24-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
25-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
26-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
27-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
28-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
29-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
30-
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
31-
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
32-
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
33-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
34-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
35-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
36-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
40-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
42-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
43-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
44-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
45-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
46-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
47-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
48-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
49-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
50-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
51-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
52-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
53-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5413
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5514
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

jamf/jamfprointegration/auth_basic.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/deploymenttheory/go-api-http-client/httpclient"
109
"go.uber.org/zap"
1110
)
1211

@@ -20,7 +19,7 @@ type basicAuth struct {
2019
hideSensitiveData bool
2120
bearerToken string
2221
bearerTokenExpiryTime time.Time
23-
httpExecutor httpclient.HTTPExecutor
22+
http http.Client
2423
}
2524

2625
// basicAuthResponse serves as a json structure map for the basicAuth response from Jamf.
@@ -58,7 +57,7 @@ func (a *basicAuth) getNewToken() error {
5857

5958
req.SetBasicAuth(a.username, a.password)
6059

61-
resp, err := a.httpExecutor.Do(req)
60+
resp, err := a.http.Do(req)
6261
if err != nil {
6362
return err
6463
}

jamf/jamfprointegration/auth_oauth.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/deploymenttheory/go-api-http-client/httpclient"
1413
"go.uber.org/zap"
1514
)
1615

@@ -24,7 +23,7 @@ type oauth struct {
2423
hideSensitiveData bool
2524
expiryTime time.Time
2625
token string
27-
httpExecutor httpclient.HTTPExecutor
26+
http http.Client
2827
}
2928

3029
// OAuthResponse represents the response structure when obtaining an OAuth access token from JamfPro.
@@ -56,7 +55,7 @@ func (a *oauth) getNewToken() error {
5655

5756
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
5857

59-
resp, err := a.httpExecutor.Do(req)
58+
resp, err := a.http.Do(req)
6059
if err != nil {
6160
return err
6261
}

jamf/jamfprointegration/builders.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
package jamfprointegration
22

33
import (
4+
"net/http"
45
"time"
56

6-
"github.com/deploymenttheory/go-api-http-client/httpclient"
77
"go.uber.org/zap"
88
)
99

1010
// BuildWithOAuth is a helper function allowing the full construct of a Jamf Integration using OAuth2
11-
func BuildWithOAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, clientId string, clientSecret string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
11+
func BuildWithOAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, clientId string, clientSecret string, hideSensitiveData bool, client http.Client) (*Integration, error) {
1212
integration := Integration{
1313
JamfProFQDN: jamfProFQDN,
1414
Sugar: Sugar,
1515
AuthMethodDescriptor: "oauth2",
16-
httpExecutor: executor,
16+
http: client,
1717
}
1818

19-
integration.BuildOAuth(clientId, clientSecret, bufferPeriod, hideSensitiveData, executor)
19+
integration.BuildOAuth(clientId, clientSecret, bufferPeriod, hideSensitiveData, client)
2020
err := integration.CheckRefreshToken()
2121

2222
return &integration, err
2323
}
2424

2525
// BuildWithBasicAuth is a helper function allowing the full construct of a Jamf Integration using BasicAuth
26-
func BuildWithBasicAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, username string, password string, hideSensitiveData bool, executor httpclient.HTTPExecutor) (*Integration, error) {
26+
func BuildWithBasicAuth(jamfProFQDN string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, username string, password string, hideSensitiveData bool, client http.Client) (*Integration, error) {
2727

2828
integration := Integration{
2929
JamfProFQDN: jamfProFQDN,
3030
Sugar: Sugar,
3131
AuthMethodDescriptor: "basic",
32-
httpExecutor: executor,
32+
http: client,
3333
}
3434

35-
integration.BuildBasicAuth(username, password, bufferPeriod, hideSensitiveData, executor)
35+
integration.BuildBasicAuth(username, password, bufferPeriod, hideSensitiveData, client)
3636
err := integration.CheckRefreshToken()
3737

3838
return &integration, err
3939
}
4040

4141
// BuildOAuth is a helper which returns just a configured OAuth interface
42-
func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPeriod time.Duration, hideSensitiveData bool, executor httpclient.HTTPExecutor) {
42+
func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPeriod time.Duration, hideSensitiveData bool, client http.Client) {
4343
authInterface := oauth{
4444
clientId: clientId,
4545
clientSecret: clientSecret,
4646
bufferPeriod: bufferPeriod,
4747
baseDomain: j.JamfProFQDN,
4848
Sugar: j.Sugar,
4949
hideSensitiveData: hideSensitiveData,
50-
httpExecutor: executor,
50+
http: client,
5151
}
5252

5353
j.auth = &authInterface
5454
}
5555

5656
// BuildBasicAuth is a helper which returns just a configured Basic Auth interface/
57-
func (j *Integration) BuildBasicAuth(username string, password string, bufferPeriod time.Duration, hideSensitiveData bool, executor httpclient.HTTPExecutor) {
57+
func (j *Integration) BuildBasicAuth(username string, password string, bufferPeriod time.Duration, hideSensitiveData bool, client http.Client) {
5858
authInterface := basicAuth{
5959
username: username,
6060
password: password,
6161
bufferPeriod: bufferPeriod,
6262
Sugar: j.Sugar,
6363
baseDomain: j.JamfProFQDN,
6464
hideSensitiveData: hideSensitiveData,
65-
httpExecutor: executor,
65+
http: client,
6666
}
6767

6868
j.auth = &authInterface

jamf/jamfprointegration/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jamfprointegration
22

3+
import "time"
4+
35
// Endpoint constants represent the URL suffixes used for Jamf API token interactions.
46
const (
57
// Auth
@@ -11,4 +13,6 @@ const (
1113
// Load balancer workaround
1214
LoadBalancerTargetCookie string = "jpro-ingress"
1315
LoadBalancerPollCount int = 5
16+
LoadBalancerTimeOut time.Duration = 7 * time.Second
17+
1418
)

jamf/jamfprointegration/interfaces.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package jamfprointegration
33
import (
44
"net/http"
55

6-
"github.com/deploymenttheory/go-api-http-client/httpclient"
76
"go.uber.org/zap"
87
)
98

@@ -13,7 +12,7 @@ type Integration struct {
1312
AuthMethodDescriptor string
1413
Sugar *zap.SugaredLogger
1514
auth authInterface
16-
httpExecutor httpclient.HTTPExecutor
15+
http http.Client
1716
}
1817

1918
// getFQDN returns just the FQDN // TODO remove the "get"

jamf/jamfprointegration/load_balancer_workaround.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package jamfprointegration
33
import (
44
"fmt"
55
"net/http"
6-
"slices"
6+
"strings"
7+
"time"
78
)
89

910
// GetSessionCookies retrieves all cookies from the current session
@@ -25,6 +26,7 @@ func (j *Integration) GetLoadBalancer(urlString string) (string, error) {
2526
}
2627

2728
chosenCookie := chooseMostAlphabeticalString(*allBalancers)
29+
j.Sugar.Debugf("Chosen Cookie:%v ", chosenCookie)
2830
return chosenCookie, nil
2931
}
3032

@@ -46,23 +48,30 @@ func chooseMostAlphabeticalString(strings []string) string {
4648

4749
// TODO migrate strings
4850
func (j *Integration) getAllLoadBalancers(urlString string) (*[]string, error) {
51+
j.Sugar.Debug("Starting load balancer workaround")
4952
var outList []string
5053
var err error
5154
var req *http.Request
5255
var resp *http.Response
56+
var iterations int
5357

54-
for i := 0; i < LoadBalancerPollCount; i++ {
58+
iterations = 0
59+
startTimeEpoch := time.Now().Unix()
60+
endTimeEpoch := startTimeEpoch + int64(LoadBalancerTimeOut.Seconds())
61+
62+
for i := time.Now().Unix(); i < endTimeEpoch; {
5563
req, err = http.NewRequest("GET", urlString, nil)
5664
if err != nil {
5765
return nil, fmt.Errorf("error creating request: %v", err)
5866
}
5967

68+
// Auth required on login screen or 404
6069
err = j.PrepRequestParamsAndAuth(req)
6170
if err != nil {
6271
return nil, fmt.Errorf("error populating auth: %v", err)
6372
}
6473

65-
resp, err = j.httpExecutor.Do(req)
74+
resp, err = j.http.Do(req)
6675
if err != nil {
6776
return nil, fmt.Errorf("error sending req: %v", err)
6877
}
@@ -71,14 +80,31 @@ func (j *Integration) getAllLoadBalancers(urlString string) (*[]string, error) {
7180

7281
for _, v := range respCookies {
7382
if v.Name == LoadBalancerTargetCookie {
74-
outList = append(outList, v.Value)
83+
strippedCookie := strings.TrimSpace(v.Value)
84+
outList = append(outList, strippedCookie)
7585
}
7686
}
7787

78-
}
88+
uniqueMap := make(map[string]bool)
89+
90+
for _, str := range outList {
91+
uniqueMap[str] = true
92+
}
93+
94+
cookieDupesRemoved := make([]string, 0, len(uniqueMap))
7995

80-
slices.Sort(outList)
81-
newList := slices.Compact(outList)
82-
return &newList, nil
96+
for str := range uniqueMap {
97+
cookieDupesRemoved = append(cookieDupesRemoved, str)
98+
}
99+
100+
if len(cookieDupesRemoved) > 1 {
101+
j.Sugar.Debugf("### COMPLETED LOADBALANCER WORKAROUND ### Dupes removed: %v, outlist: %v", cookieDupesRemoved, outList)
102+
break
103+
}
104+
105+
i = time.Now().Unix()
106+
iterations += 1
107+
}
108+
return &outList, nil
83109

84110
}

jamf/jamfprointegration/marshall_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package jamfprointegration
22

33
import (
4+
"net/http"
45
"reflect"
56
"testing"
67

7-
"github.com/deploymenttheory/go-api-http-client/httpclient"
88
"go.uber.org/zap"
99
)
1010

@@ -14,7 +14,7 @@ func TestIntegration_marshalRequest(t *testing.T) {
1414
AuthMethodDescriptor string
1515
Sugar *zap.SugaredLogger
1616
auth authInterface
17-
httpExecutor httpclient.HTTPExecutor
17+
http http.Client
1818
}
1919
type args struct {
2020
body interface{}
@@ -37,7 +37,7 @@ func TestIntegration_marshalRequest(t *testing.T) {
3737
AuthMethodDescriptor: tt.fields.AuthMethodDescriptor,
3838
Sugar: tt.fields.Sugar,
3939
auth: tt.fields.auth,
40-
httpExecutor: tt.fields.httpExecutor,
40+
http: tt.fields.http,
4141
}
4242
got, err := j.marshalRequest(tt.args.body, tt.args.method, tt.args.endpoint)
4343
if (err != nil) != tt.wantErr {

0 commit comments

Comments
 (0)