Skip to content

Commit d842d66

Browse files
committed
WIP: Proof of concept / demo
Signed-off-by: Richard Wall <[email protected]>
1 parent 919c73d commit d842d66

File tree

6 files changed

+114
-9
lines changed

6 files changed

+114
-9
lines changed

hack/e2e/ca/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
machineHub:
2+
subdomain: tlskp-test
3+
credentialsSecretName: todo-unused

hack/e2e/ca/test.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
#
3+
set -o nounset
4+
set -o errexit
5+
set -o pipefail
6+
7+
# CyberArk API configuration
8+
: ${ARK_USERNAME?}
9+
: ${ARK_SECRET?}
10+
: ${ARK_PLATFORM_DOMAIN?}
11+
: ${ARK_SUBDOMAIN?}
12+
13+
# The base URL of the OCI registry used for Docker images and Helm charts
14+
# E.g. ttl.sh/6ee49a01-c8ba-493e-bae9-4d8567574b56
15+
: ${OCI_BASE?}
16+
17+
k8s_namespace=cyberark
18+
19+
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
20+
root_dir=$(cd "${script_dir}/../../.." && pwd)
21+
export TERM=dumb
22+
23+
tmp_dir="$(mktemp -d /tmp/jetstack-secure.XXXXX)"
24+
25+
pushd "${tmp_dir}"
26+
> release.env
27+
make -C "$root_dir" release \
28+
OCI_SIGN_ON_PUSH=false \
29+
oci_platforms=linux/amd64 \
30+
oci_preflight_image_name=$OCI_BASE/images/venafi-agent \
31+
helm_chart_image_name=$OCI_BASE/charts/venafi-kubernetes-agent \
32+
GITHUB_OUTPUT="${tmp_dir}/release.env"
33+
source release.env
34+
35+
kind create cluster || true
36+
kubectl create ns "$k8s_namespace" || true
37+
38+
kubectl create secret generic agent-credentials \
39+
--namespace "$k8s_namespace" \
40+
--from-literal=ARK_USERNAME=$ARK_USERNAME \
41+
--from-literal=ARK_SECRET=$ARK_SECRET \
42+
--from-literal=ARK_PLATFORM_DOMAIN=$ARK_PLATFORM_DOMAIN \
43+
--from-literal=ARK_SUBDOMAIN=$ARK_SUBDOMAIN
44+
45+
helm upgrade agent "oci://${OCI_BASE}/charts/venafi-kubernetes-agent" \
46+
--install \
47+
--create-namespace \
48+
--namespace "$k8s_namespace" \
49+
--version "${RELEASE_HELM_CHART_VERSION}" \
50+
--set fullnameOverride=agent \
51+
--set "image.repository=${OCI_BASE}/images/venafi-agent" \
52+
--values "${script_dir}/values.agent.yaml"
53+
54+
kubectl scale -n "$k8s_namespace" deployment agent --replicas=0
55+
kubectl get cm -n "$k8s_namespace" agent-config -o jsonpath={.data.config\\.yaml} > config.original.yaml
56+
yq eval-all '. as $item ireduce ({}; . * $item)' config.original.yaml "${script_dir}/config.yaml" > config.yaml
57+
kubectl delete cm -n "$k8s_namespace" agent-config
58+
kubectl create cm -n "$k8s_namespace" agent-config --from-file=config.yaml
59+
kubectl scale -n "$k8s_namespace" deployment agent --replicas=1

hack/e2e/ca/values.agent.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Empty

pkg/agent/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,12 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
606606
res.ClusterID = clusterID
607607
res.ClusterDescription = cfg.ClusterDescription
608608

609-
// Validation of `data-gatherers`.
610-
if dgErr := ValidateDataGatherers(cfg.DataGatherers); dgErr != nil {
611-
errs = multierror.Append(errs, dgErr)
612-
}
613-
res.DataGatherers = cfg.DataGatherers
614609
}
610+
// Validation of `data-gatherers`.
611+
if dgErr := ValidateDataGatherers(cfg.DataGatherers); dgErr != nil {
612+
errs = multierror.Append(errs, dgErr)
613+
}
614+
res.DataGatherers = cfg.DataGatherers
615615

616616
// Validation of --period, -p, and the `period` field, as well as
617617
// --backoff-max-time, --one-shot, and --strict. The flag --period/-p takes

pkg/agent/run.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
"github.com/jetstack/preflight/pkg/client"
3535
"github.com/jetstack/preflight/pkg/datagatherer"
3636
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
37+
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
38+
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
39+
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
3740
"github.com/jetstack/preflight/pkg/kubeconfig"
3841
"github.com/jetstack/preflight/pkg/logs"
3942
"github.com/jetstack/preflight/pkg/version"
@@ -78,6 +81,44 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7881
return fmt.Errorf("While evaluating configuration: %v", err)
7982
}
8083

84+
var caClient *dataupload.CyberArkClient
85+
if config.MachineHubMode {
86+
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
87+
subdomain := os.Getenv("ARK_SUBDOMAIN")
88+
username := os.Getenv("ARK_USERNAME")
89+
password := []byte(os.Getenv("ARK_SECRET"))
90+
91+
const (
92+
discoveryContextServiceName = "inventory"
93+
separator = "."
94+
)
95+
96+
// TODO(wallrj): Maybe get this URL via the service discovery API.
97+
// https://platform-discovery.integration-cyberark.cloud/api/public/tenant-discovery?allEndpoints=true&bySubdomain=tlskp-test
98+
serviceURL := fmt.Sprintf("https://%s%s%s.%s", subdomain, separator, discoveryContextServiceName, platformDomain)
99+
100+
var (
101+
identityClient *identity.Client
102+
err error
103+
)
104+
if platformDomain == "cyberark.cloud" {
105+
identityClient, err = identity.New(ctx, subdomain)
106+
} else {
107+
discoveryClient := servicediscovery.New(servicediscovery.WithIntegrationEndpoint())
108+
identityClient, err = identity.NewWithDiscoveryClient(ctx, discoveryClient, subdomain)
109+
}
110+
if err != nil {
111+
return fmt.Errorf("while creating the CyberArk identity client: %v", err)
112+
}
113+
if err := identityClient.LoginUsernamePassword(ctx, username, password); err != nil {
114+
return fmt.Errorf("while logging in: %v", err)
115+
}
116+
caClient, err = dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
117+
if err != nil {
118+
return fmt.Errorf("while creating the CyberArk dataupload client: %v", err)
119+
}
120+
}
121+
81122
group, gctx := errgroup.WithContext(ctx)
82123
defer func() {
83124
cancel()
@@ -239,7 +280,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
239280
// be cancelled, which will cause this blocking loop to exit
240281
// instead of waiting for the time period.
241282
for {
242-
if err := gatherAndOutputData(klog.NewContext(ctx, log), eventf, config, preflightClient, dataGatherers); err != nil {
283+
if err := gatherAndOutputData(klog.NewContext(ctx, log), eventf, config, preflightClient, caClient, dataGatherers); err != nil {
243284
return err
244285
}
245286

@@ -293,7 +334,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
293334
// Like Printf but for sending events to the agent's Pod object.
294335
type Eventf func(eventType, reason, msg string, args ...interface{})
295336

296-
func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConfig, preflightClient client.Client, dataGatherers map[string]datagatherer.DataGatherer) error {
337+
func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConfig, preflightClient client.Client, caClient *dataupload.CyberArkClient, dataGatherers map[string]datagatherer.DataGatherer) error {
297338
log := klog.FromContext(ctx).WithName("gatherAndOutputData")
298339
var readings []*api.DataReading
299340

@@ -347,8 +388,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
347388

348389
if config.MachineHubMode {
349390
post := func() (any, error) {
350-
log.Info("machine hub mode not yet implemented")
351-
return struct{}{}, nil
391+
return struct{}{}, caClient.PostDataReadingsWithOptions(ctx, readings, dataupload.Options{})
352392
}
353393

354394
group.Go(func() error {

pkg/internal/cyberark/dataupload/dataupload_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func genNamespace(name string) *unstructured.Unstructured {
3131
o.SetName(name)
3232
return o
3333
}
34+
3435
func genArkNamespacesDataReading(clusterID types.UID) *api.DataReading {
3536
kubeSystemNamespace := genNamespace("kube-system")
3637
kubeSystemNamespace.SetUID(clusterID)
@@ -56,6 +57,7 @@ func genArkNamespacesDataReading(clusterID types.UID) *api.DataReading {
5657
SchemaVersion: "v1",
5758
}
5859
}
60+
5961
func TestCyberArkClient_PostDataReadings_MockAPI(t *testing.T) {
6062
defaultDataReadings := []*api.DataReading{
6163
genArkNamespacesDataReading("success-cluster-id"),

0 commit comments

Comments
 (0)