Skip to content

Commit 949f238

Browse files
WIP
Signed-off-by: Richard Wall <[email protected]>
1 parent 6c8a341 commit 949f238

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

pkg/agent/config_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,46 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
702702
})
703703
}
704704

705+
func Test_ValidateAndCombineConfig_MachineHub(t *testing.T) {
706+
t.Setenv("POD_NAMESPACE", "venafi")
707+
t.Setenv("ARK_PLATFORM_DOMAIN", "integration-cyberark.cloud")
708+
t.Setenv("ARK_SUBDOMAIN", "tlskp-test")
709+
t.Setenv("ARK_USERNAME", "[email protected]")
710+
t.Setenv("ARK_SECRET", "test-secret")
711+
712+
ctx, cancel := context.WithTimeout(t.Context(), time.Second*3)
713+
defer cancel()
714+
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(7)))
715+
ctx = klog.NewContext(ctx, log)
716+
717+
srv, _, setVenafiCloudAssert := testutil.FakeVenafiCloud(t)
718+
t.Setenv("ARK_DISCOVERY_ENDPOINT", srv.URL)
719+
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) {
720+
// Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name=
721+
if gotReq.URL.Path == "/v1/oauth/token/serviceaccount" {
722+
return
723+
}
724+
725+
assert.Equal(t, srv.URL, "https://"+gotReq.Host)
726+
assert.Equal(t, "test cluster name", gotReq.URL.Query().Get("name"))
727+
assert.Equal(t, "/v1/tlspk/upload/clusterdata/no", gotReq.URL.Path)
728+
})
729+
730+
got, cl, err := ValidateAndCombineConfig(discardLogs(),
731+
withConfig(testutil.Undent(`
732+
period: 1h
733+
`)),
734+
withCmdLineFlags("--machine-hub"),
735+
)
736+
require.NoError(t, err)
737+
// testutil.TrustCA(t, cl, cert)
738+
assert.Equal(t, MachineHub, got.OutputMode)
739+
740+
err = cl.PostDataReadingsWithOptions(ctx, nil, client.Options{})
741+
require.NoError(t, err)
742+
743+
}
744+
705745
// Slower test cases due to envtest. That's why they are separated from the
706746
// other tests.
707747
func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {

pkg/internal/cyberark/client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,29 @@ func NewDatauploadClient(ctx context.Context, cfg ClientConfig) (*dataupload.Cyb
4848

4949
serviceURL := fmt.Sprintf("https://%s%s%s.%s", cfg.subdomain, separator, discoveryContextServiceName, cfg.platformDomain)
5050

51+
var serviceDiscoveryOptions []servicediscovery.ClientOpt
52+
53+
if cfg.platformDomain != "cyberark.cloud" {
54+
55+
}
56+
57+
if serviceDiscoveryEndpoint := os.Getenv("ARK_SERVICE_DISCOVERY_ENDPOINT"); serviceDiscoveryEndpoint != "" {
58+
serviceDiscoveryOptions = append(
59+
serviceDiscoveryOptions,
60+
servicediscovery.WithCustomEndpoint(serviceDiscoveryEndpoint),
61+
)
62+
}
63+
64+
serviceDiscoveryClient := servicediscovery.New(serviceDiscoveryOptions...)
65+
5166
var (
5267
identityClient *identity.Client
5368
err error
5469
)
5570
if cfg.platformDomain == "cyberark.cloud" {
5671
identityClient, err = identity.New(ctx, cfg.subdomain)
5772
} else {
58-
discoveryClient := servicediscovery.New(servicediscovery.WithIntegrationEndpoint())
59-
identityClient, err = identity.NewWithDiscoveryClient(ctx, discoveryClient, cfg.subdomain)
73+
identityClient, err = identity.NewWithDiscoveryClient(ctx, serviceDiscoveryClient, cfg.subdomain)
6074
}
6175
if err != nil {
6276
return nil, err

pkg/internal/cyberark/servicediscovery/discovery.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"os"
1011
"time"
1112

1213
"k8s.io/client-go/transport"
@@ -62,12 +63,17 @@ func WithCustomEndpoint(endpoint string) ClientOpt {
6263

6364
// New creates a new CyberArk Service Discovery client, configurable with ClientOpt
6465
func New(clientOpts ...ClientOpt) *Client {
66+
endpoint := os.Getenv("ARK_DISCOVERY_ENDPOINT")
67+
if endpoint == "" {
68+
endpoint = prodDiscoveryEndpoint
69+
}
70+
6571
client := &Client{
6672
client: &http.Client{
6773
Timeout: 10 * time.Second,
6874
Transport: transport.NewDebuggingRoundTripper(http.DefaultTransport, transport.DebugByContext),
6975
},
70-
endpoint: prodDiscoveryEndpoint,
76+
endpoint: endpoint,
7177
}
7278

7379
for _, opt := range clientOpts {

0 commit comments

Comments
 (0)