Skip to content

Dev loadbalance #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ module github.com/deploymenttheory/go-api-http-client-integrations

go 1.22.4

require (
github.com/deploymenttheory/go-api-http-client v0.2.11
go.uber.org/zap v1.27.0
)
require go.uber.org/zap v1.27.0

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

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

Expand All @@ -20,7 +19,7 @@ type basicAuth struct {
hideSensitiveData bool
bearerToken string
bearerTokenExpiryTime time.Time
httpExecutor httpclient.HTTPExecutor
http http.Client
}

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

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

resp, err := a.httpExecutor.Do(req)
resp, err := a.http.Do(req)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions jamf/jamfprointegration/auth_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

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

Expand All @@ -24,7 +23,7 @@ type oauth struct {
hideSensitiveData bool
expiryTime time.Time
token string
httpExecutor httpclient.HTTPExecutor
http http.Client
}

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

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

resp, err := a.httpExecutor.Do(req)
resp, err := a.http.Do(req)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions jamf/jamfprointegration/builders.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
package jamfprointegration

import (
"net/http"
"time"

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

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

integration.BuildOAuth(clientId, clientSecret, bufferPeriod, hideSensitiveData, executor)
integration.BuildOAuth(clientId, clientSecret, bufferPeriod, hideSensitiveData, client)
err := integration.CheckRefreshToken()

return &integration, err
}

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

integration := Integration{
JamfProFQDN: jamfProFQDN,
Sugar: Sugar,
AuthMethodDescriptor: "basic",
httpExecutor: executor,
http: client,
}

integration.BuildBasicAuth(username, password, bufferPeriod, hideSensitiveData, executor)
integration.BuildBasicAuth(username, password, bufferPeriod, hideSensitiveData, client)
err := integration.CheckRefreshToken()

return &integration, err
}

// BuildOAuth is a helper which returns just a configured OAuth interface
func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPeriod time.Duration, hideSensitiveData bool, executor httpclient.HTTPExecutor) {
func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPeriod time.Duration, hideSensitiveData bool, client http.Client) {
authInterface := oauth{
clientId: clientId,
clientSecret: clientSecret,
bufferPeriod: bufferPeriod,
baseDomain: j.JamfProFQDN,
Sugar: j.Sugar,
hideSensitiveData: hideSensitiveData,
httpExecutor: executor,
http: client,
}

j.auth = &authInterface
}

// BuildBasicAuth is a helper which returns just a configured Basic Auth interface/
func (j *Integration) BuildBasicAuth(username string, password string, bufferPeriod time.Duration, hideSensitiveData bool, executor httpclient.HTTPExecutor) {
func (j *Integration) BuildBasicAuth(username string, password string, bufferPeriod time.Duration, hideSensitiveData bool, client http.Client) {
authInterface := basicAuth{
username: username,
password: password,
bufferPeriod: bufferPeriod,
Sugar: j.Sugar,
baseDomain: j.JamfProFQDN,
hideSensitiveData: hideSensitiveData,
httpExecutor: executor,
http: client,
}

j.auth = &authInterface
Expand Down
4 changes: 4 additions & 0 deletions jamf/jamfprointegration/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jamfprointegration

import "time"

// Endpoint constants represent the URL suffixes used for Jamf API token interactions.
const (
// Auth
Expand All @@ -11,4 +13,6 @@ const (
// Load balancer workaround
LoadBalancerTargetCookie string = "jpro-ingress"
LoadBalancerPollCount int = 5
LoadBalancerTimeOut time.Duration = 7 * time.Second

)
3 changes: 1 addition & 2 deletions jamf/jamfprointegration/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jamfprointegration
import (
"net/http"

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

Expand All @@ -13,7 +12,7 @@ type Integration struct {
AuthMethodDescriptor string
Sugar *zap.SugaredLogger
auth authInterface
httpExecutor httpclient.HTTPExecutor
http http.Client
}

// getFQDN returns just the FQDN // TODO remove the "get"
Expand Down
42 changes: 34 additions & 8 deletions jamf/jamfprointegration/load_balancer_workaround.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package jamfprointegration
import (
"fmt"
"net/http"
"slices"
"strings"
"time"
)

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

chosenCookie := chooseMostAlphabeticalString(*allBalancers)
j.Sugar.Debugf("Chosen Cookie:%v ", chosenCookie)
return chosenCookie, nil
}

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

// TODO migrate strings
func (j *Integration) getAllLoadBalancers(urlString string) (*[]string, error) {
j.Sugar.Debug("Starting load balancer workaround")
var outList []string
var err error
var req *http.Request
var resp *http.Response
var iterations int

for i := 0; i < LoadBalancerPollCount; i++ {
iterations = 0
startTimeEpoch := time.Now().Unix()
endTimeEpoch := startTimeEpoch + int64(LoadBalancerTimeOut.Seconds())

for i := time.Now().Unix(); i < endTimeEpoch; {
req, err = http.NewRequest("GET", urlString, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}

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

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

for _, v := range respCookies {
if v.Name == LoadBalancerTargetCookie {
outList = append(outList, v.Value)
strippedCookie := strings.TrimSpace(v.Value)
outList = append(outList, strippedCookie)
}
}

}
uniqueMap := make(map[string]bool)

for _, str := range outList {
uniqueMap[str] = true
}

cookieDupesRemoved := make([]string, 0, len(uniqueMap))

slices.Sort(outList)
newList := slices.Compact(outList)
return &newList, nil
for str := range uniqueMap {
cookieDupesRemoved = append(cookieDupesRemoved, str)
}

if len(cookieDupesRemoved) > 1 {
j.Sugar.Debugf("### COMPLETED LOADBALANCER WORKAROUND ### Dupes removed: %v, outlist: %v", cookieDupesRemoved, outList)
break
}

i = time.Now().Unix()
iterations += 1
}
return &outList, nil

}
6 changes: 3 additions & 3 deletions jamf/jamfprointegration/marshall_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package jamfprointegration

import (
"net/http"
"reflect"
"testing"

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

Expand All @@ -14,7 +14,7 @@ func TestIntegration_marshalRequest(t *testing.T) {
AuthMethodDescriptor string
Sugar *zap.SugaredLogger
auth authInterface
httpExecutor httpclient.HTTPExecutor
http http.Client
}
type args struct {
body interface{}
Expand All @@ -37,7 +37,7 @@ func TestIntegration_marshalRequest(t *testing.T) {
AuthMethodDescriptor: tt.fields.AuthMethodDescriptor,
Sugar: tt.fields.Sugar,
auth: tt.fields.auth,
httpExecutor: tt.fields.httpExecutor,
http: tt.fields.http,
}
got, err := j.marshalRequest(tt.args.body, tt.args.method, tt.args.endpoint)
if (err != nil) != tt.wantErr {
Expand Down
Loading