Skip to content

Commit 065075b

Browse files
WIP
Signed-off-by: Richard Wall <[email protected]>
1 parent 60f58ac commit 065075b

File tree

8 files changed

+155
-150
lines changed

8 files changed

+155
-150
lines changed

pkg/client/client_cyberark.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package client
22

33
import (
44
"context"
5+
"net/http"
6+
"time"
7+
8+
"k8s.io/client-go/transport"
59

610
"github.com/jetstack/preflight/api"
711
"github.com/jetstack/preflight/pkg/internal/cyberark"
@@ -10,6 +14,7 @@ import (
1014

1115
type CyberArkClient struct {
1216
configLoader cyberark.ClientConfigLoader
17+
httpClient *http.Client
1318
}
1419

1520
var _ Client = &CyberArkClient{}
@@ -22,6 +27,10 @@ func NewCyberArk() (*CyberArkClient, error) {
2227
}
2328
return &CyberArkClient{
2429
configLoader: configLoader,
30+
httpClient: &http.Client{
31+
Timeout: time.Minute,
32+
Transport: transport.DebugWrappers(http.DefaultTransport),
33+
},
2534
}, nil
2635
}
2736

@@ -33,7 +42,7 @@ func (o *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, readin
3342
if err != nil {
3443
return err
3544
}
36-
datauploadClient, err := cyberark.NewDatauploadClient(ctx, cfg)
45+
datauploadClient, err := cyberark.NewDatauploadClient(ctx, o.httpClient, cfg)
3746
if err != nil {
3847
return err
3948
}

pkg/client/client_cyberark_test.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package client_test
22

33
import (
44
"os"
@@ -9,36 +9,56 @@ import (
99
"k8s.io/klog/v2/ktesting"
1010

1111
"github.com/jetstack/preflight/api"
12+
"github.com/jetstack/preflight/pkg/client"
13+
"github.com/jetstack/preflight/pkg/testutil"
1214

1315
_ "k8s.io/klog/v2/ktesting/init"
1416
)
1517

16-
func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
17-
// TestCyberArkClient_PostDataReadingsWithOptions/RealAPI demonstrates that the
18-
// dataupload code works with the real inventory API.
19-
//
20-
// To enable verbose request logging:
21-
//
22-
// go test ./pkg/internal/cyberark/dataupload/... \
23-
// -v -count 1 -run TestPostDataReadingsWithOptionsWithRealAPI -args -testing.v 6
24-
t.Run("RealAPI", func(t *testing.T) {
25-
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
18+
func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
19+
t.Setenv("ARK_SECRET", "")
20+
t.Run("success", func(t *testing.T) {
21+
logger := ktesting.NewLogger(t, ktesting.DefaultConfig)
22+
ctx := klog.NewContext(t.Context(), logger)
23+
24+
server, serverCert := testutil.FakeCyberArk(t)
25+
t.Setenv("ARK_DISCOVERY_ENDPOINT", server.URL)
26+
cl, err := client.NewCyberArk()
27+
require.NoError(t, err)
28+
29+
testutil.TrustCA(t, cl, serverCert)
30+
31+
var readings []*api.DataReading
32+
err = cl.PostDataReadingsWithOptions(ctx, readings, client.Options{})
33+
require.NoError(t, err)
34+
})
35+
}
36+
37+
// TestCyberArkClient_PostDataReadingsWithOptions_RealAPI demonstrates that the
38+
// dataupload code works with the real inventory API.
39+
//
40+
// To enable verbose request logging:
41+
//
42+
// go test ./pkg/internal/cyberark/dataupload/... \
43+
// -v -count 1 -run TestCyberArkClient_PostDataReadingsWithOptions_RealAPI -args -testing.v 6
44+
func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
45+
t.Run("success", func(t *testing.T) {
2646
subdomain := os.Getenv("ARK_SUBDOMAIN")
2747
username := os.Getenv("ARK_USERNAME")
2848
secret := os.Getenv("ARK_SECRET")
2949

30-
if platformDomain == "" || subdomain == "" || username == "" || secret == "" {
31-
t.Skip("Skipping because one of the following environment variables is unset or empty: ARK_PLATFORM_DOMAIN, ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET")
50+
if subdomain == "" || username == "" || secret == "" {
51+
t.Skip("Skipping because one of the following environment variables is unset or empty: ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET")
3252
return
3353
}
3454

3555
logger := ktesting.NewLogger(t, ktesting.DefaultConfig)
3656
ctx := klog.NewContext(t.Context(), logger)
3757

38-
c, err := NewCyberArk()
58+
c, err := client.NewCyberArk()
3959
require.NoError(t, err)
4060
var readings []*api.DataReading
41-
err = c.PostDataReadingsWithOptions(ctx, readings, Options{})
61+
err = c.PostDataReadingsWithOptions(ctx, readings, client.Options{})
4262
require.NoError(t, err)
4363
})
4464
}

pkg/internal/cyberark/client.go

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cyberark
33
import (
44
"context"
55
"errors"
6-
"fmt"
6+
"net/http"
77
"os"
88

99
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
@@ -12,62 +12,44 @@ import (
1212
)
1313

1414
type ClientConfig struct {
15-
platformDomain string
16-
subdomain string
17-
username string
18-
secret string
15+
subdomain string
16+
username string
17+
secret string
1918
}
2019

2120
type ClientConfigLoader func() (ClientConfig, error)
2221

2322
func LoadClientConfigFromEnvironment() (ClientConfig, error) {
24-
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
2523
subdomain := os.Getenv("ARK_SUBDOMAIN")
2624
username := os.Getenv("ARK_USERNAME")
2725
secret := os.Getenv("ARK_SECRET")
2826

29-
if platformDomain == "" || subdomain == "" || username == "" || secret == "" {
27+
if subdomain == "" || username == "" || secret == "" {
3028
return ClientConfig{}, errors.New(
31-
"missing environment variables: ARK_PLATFORM_DOMAIN, ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET")
29+
"missing environment variables: ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET")
3230
}
3331

3432
return ClientConfig{
35-
platformDomain: platformDomain,
36-
subdomain: subdomain,
37-
username: username,
38-
secret: secret,
33+
subdomain: subdomain,
34+
username: username,
35+
secret: secret,
3936
}, nil
4037

4138
}
4239

43-
func NewDatauploadClient(ctx context.Context, cfg ClientConfig) (*dataupload.CyberArkClient, error) {
44-
const (
45-
discoveryContextServiceName = "inventory"
46-
separator = "."
47-
)
48-
49-
serviceURL := fmt.Sprintf("https://%s%s%s.%s", cfg.subdomain, separator, discoveryContextServiceName, cfg.platformDomain)
50-
51-
var (
52-
identityClient *identity.Client
53-
err error
54-
)
55-
if cfg.platformDomain == "cyberark.cloud" {
56-
identityClient, err = identity.New(ctx, cfg.subdomain)
57-
} else {
58-
discoveryClient := servicediscovery.New(servicediscovery.WithIntegrationEndpoint())
59-
identityClient, err = identity.NewWithDiscoveryClient(ctx, discoveryClient, cfg.subdomain)
60-
}
40+
func NewDatauploadClient(ctx context.Context, httpClient *http.Client, cfg ClientConfig) (*dataupload.CyberArkClient, error) {
41+
discoveryClient := servicediscovery.New(httpClient)
42+
serviceMap, err := discoveryClient.DiscoverIdentityAPIURL(ctx, cfg.subdomain)
6143
if err != nil {
6244
return nil, err
6345
}
6446

47+
identityClient := identity.New(ctx, httpClient, serviceMap.IdentityEndpoint(), cfg.subdomain)
6548
err = identityClient.LoginUsernamePassword(ctx, cfg.username, []byte(cfg.secret))
6649
if err != nil {
6750
return nil, err
6851
}
69-
70-
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
52+
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceMap.DiscoveryContextEndpoint(), identityClient.AuthenticateRequest)
7153
if err != nil {
7254
return nil, err
7355
}

pkg/internal/cyberark/identity/identity.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ import (
1212
"time"
1313

1414
"github.com/cenkalti/backoff/v5"
15-
"k8s.io/client-go/transport"
1615
"k8s.io/klog/v2"
1716

18-
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
1917
"github.com/jetstack/preflight/pkg/logs"
2018
"github.com/jetstack/preflight/pkg/version"
2119
)
@@ -189,40 +187,17 @@ type Client struct {
189187
// token is a wrapper type for holding auth tokens we want to cache.
190188
type token string
191189

192-
// New returns an initialized CyberArk Identity client using a default service discovery client.
193-
// NB: This function performs service discovery when called, in order to ensure that all Identity
194-
// clients are created with a valid Identity API URL. This function blocks on the network call to
195-
// the discovery service.
196-
func New(ctx context.Context, subdomain string) (*Client, error) {
197-
return NewWithDiscoveryClient(ctx, servicediscovery.New(), subdomain)
198-
}
199-
200-
// NewWithDiscoveryClient returns an initialized CyberArk Identity client using the given service discovery client.
201-
// NB: This function performs service discovery when called, in order to ensure that all Identity
202-
// clients are created with a valid Identity API URL. This function blocks on the network call to
203-
// the discovery service.
204-
func NewWithDiscoveryClient(ctx context.Context, discoveryClient *servicediscovery.Client, subdomain string) (*Client, error) {
205-
if discoveryClient == nil {
206-
return nil, fmt.Errorf("must provide a non-nil discovery client to the Identity Client")
207-
}
208-
209-
endpoint, err := discoveryClient.DiscoverIdentityAPIURL(ctx, subdomain)
210-
if err != nil {
211-
return nil, err
212-
}
213-
190+
// New returns an initialized CyberArk Identity client
191+
func New(ctx context.Context, httpClient *http.Client, endpoint string, subdomain string) *Client {
214192
return &Client{
215-
client: &http.Client{
216-
Timeout: 10 * time.Second,
217-
Transport: transport.NewDebuggingRoundTripper(http.DefaultTransport, transport.DebugByContext),
218-
},
193+
client: httpClient,
219194

220195
endpoint: endpoint,
221196
subdomain: subdomain,
222197

223198
tokenCached: "",
224199
tokenCachedMutex: sync.Mutex{},
225-
}, nil
200+
}
226201
}
227202

228203
// LoginUsernamePassword performs a blocking call to fetch an auth token from CyberArk Identity using the given username and password.

0 commit comments

Comments
 (0)