Skip to content

WIP: Proof of concept / demo #678

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

Open
wants to merge 3 commits into
base: VC-43403-inventory-api-2
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions api/datareading.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package api

import (
"bytes"
"encoding/json"
"time"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// DataReadingsPost is the payload in the upload request.
Expand Down Expand Up @@ -48,3 +51,20 @@ func (v GatheredResource) MarshalJSON() ([]byte, error) {

return json.Marshal(data)
}

func (v *GatheredResource) UnmarshalJSON(data []byte) error {
var tmpResource struct {
Resource *unstructured.Unstructured `json:"resource"`
DeletedAt Time `json:"deleted_at,omitempty"`
}

d := json.NewDecoder(bytes.NewReader(data))
d.DisallowUnknownFields()

if err := d.Decode(&tmpResource); err != nil {
return err
}
v.Resource = tmpResource.Resource
v.DeletedAt = tmpResource.DeletedAt
return nil
}
45 changes: 22 additions & 23 deletions deploy/charts/venafi-kubernetes-agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ spec:
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: ARK_USERNAME
valueFrom:
secretKeyRef:
name: {{ .Values.authentication.secretName }}
key: ARK_USERNAME
- name: ARK_SECRET
valueFrom:
secretKeyRef:
name: {{ .Values.authentication.secretName }}
key: ARK_SECRET
- name: ARK_PLATFORM_DOMAIN
valueFrom:
secretKeyRef:
name: {{ .Values.authentication.secretName }}
key: ARK_PLATFORM_DOMAIN
- name: ARK_SUBDOMAIN
valueFrom:
secretKeyRef:
name: {{ .Values.authentication.secretName }}
key: ARK_SUBDOMAIN
{{- with .Values.http_proxy }}
- name: HTTP_PROXY
value: {{ . }}
Expand All @@ -71,18 +91,8 @@ spec:
- "agent"
- "-c"
- "/etc/venafi/agent/config/{{ default "config.yaml" .Values.config.configmap.key }}"
{{- if .Values.authentication.venafiConnection.enabled }}
- --venafi-connection
- {{ .Values.authentication.venafiConnection.name | quote }}
- --venafi-connection-namespace
- {{ .Values.authentication.venafiConnection.namespace | quote }}
{{- else }}
- "--client-id"
- {{ .Values.config.clientId | quote }}
- "--private-key-path"
- "/etc/venafi/agent/key/{{ .Values.authentication.secretKey }}"
{{- end }}
- --venafi-cloud
- --log-level=6
- --machine-hub
{{- if .Values.metrics.enabled }}
- --enable-metrics
{{- end }}
Expand All @@ -95,11 +105,6 @@ spec:
- name: config
mountPath: "/etc/venafi/agent/config"
readOnly: true
{{- if not .Values.authentication.venafiConnection.enabled }}
- name: credentials
mountPath: "/etc/venafi/agent/key"
readOnly: true
{{- end }}
{{- with .Values.volumeMounts }}
{{- toYaml . | nindent 12 }}
{{- end }}
Expand Down Expand Up @@ -137,12 +142,6 @@ spec:
configMap:
name: {{ default "agent-config" .Values.config.configmap.name }}
optional: false
{{- if not .Values.authentication.venafiConnection.enabled }}
- name: credentials
secret:
secretName: {{ .Values.authentication.secretName }}
optional: false
{{- end }}
{{- with .Values.volumes }}
{{- toYaml . | nindent 8 }}
{{- end }}
3 changes: 3 additions & 0 deletions hack/e2e/ca/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machineHub:
subdomain: tlskp-test
credentialsSecretName: todo-unused
59 changes: 59 additions & 0 deletions hack/e2e/ca/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
#
set -o nounset
set -o errexit
set -o pipefail

# CyberArk API configuration
: ${ARK_USERNAME?}
: ${ARK_SECRET?}
: ${ARK_PLATFORM_DOMAIN?}
: ${ARK_SUBDOMAIN?}

# The base URL of the OCI registry used for Docker images and Helm charts
# E.g. ttl.sh/6ee49a01-c8ba-493e-bae9-4d8567574b56
: ${OCI_BASE?}

k8s_namespace=cyberark

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
root_dir=$(cd "${script_dir}/../../.." && pwd)
export TERM=dumb

tmp_dir="$(mktemp -d /tmp/jetstack-secure.XXXXX)"

pushd "${tmp_dir}"
> release.env
make -C "$root_dir" release \
OCI_SIGN_ON_PUSH=false \
oci_platforms=linux/amd64 \
oci_preflight_image_name=$OCI_BASE/images/venafi-agent \
helm_chart_image_name=$OCI_BASE/charts/venafi-kubernetes-agent \
GITHUB_OUTPUT="${tmp_dir}/release.env"
source release.env

kind create cluster || true
kubectl create ns "$k8s_namespace" || true

kubectl create secret generic agent-credentials \
--namespace "$k8s_namespace" \
--from-literal=ARK_USERNAME=$ARK_USERNAME \
--from-literal=ARK_SECRET=$ARK_SECRET \
--from-literal=ARK_PLATFORM_DOMAIN=$ARK_PLATFORM_DOMAIN \
--from-literal=ARK_SUBDOMAIN=$ARK_SUBDOMAIN

helm upgrade agent "oci://${OCI_BASE}/charts/venafi-kubernetes-agent" \
--install \
--create-namespace \
--namespace "$k8s_namespace" \
--version "${RELEASE_HELM_CHART_VERSION}" \
--set fullnameOverride=agent \
--set "image.repository=${OCI_BASE}/images/venafi-agent" \
--values "${script_dir}/values.agent.yaml"

kubectl scale -n "$k8s_namespace" deployment agent --replicas=0
kubectl get cm -n "$k8s_namespace" agent-config -o jsonpath={.data.config\\.yaml} > config.original.yaml
yq eval-all '. as $item ireduce ({}; . * $item)' config.original.yaml "${script_dir}/config.yaml" > config.yaml
kubectl delete cm -n "$k8s_namespace" agent-config
kubectl create cm -n "$k8s_namespace" agent-config --from-file=config.yaml
kubectl scale -n "$k8s_namespace" deployment agent --replicas=1
1 change: 1 addition & 0 deletions hack/e2e/ca/values.agent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty
10 changes: 5 additions & 5 deletions pkg/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,12 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
res.ClusterID = clusterID
res.ClusterDescription = cfg.ClusterDescription

// Validation of `data-gatherers`.
if dgErr := ValidateDataGatherers(cfg.DataGatherers); dgErr != nil {
errs = multierror.Append(errs, dgErr)
}
res.DataGatherers = cfg.DataGatherers
}
// Validation of `data-gatherers`.
if dgErr := ValidateDataGatherers(cfg.DataGatherers); dgErr != nil {
errs = multierror.Append(errs, dgErr)
}
res.DataGatherers = cfg.DataGatherers

// Validation of --period, -p, and the `period` field, as well as
// --backoff-max-time, --one-shot, and --strict. The flag --period/-p takes
Expand Down
57 changes: 37 additions & 20 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/client"
"github.com/jetstack/preflight/pkg/clusteruid"
"github.com/jetstack/preflight/pkg/datagatherer"
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
"github.com/jetstack/preflight/pkg/kubeconfig"
"github.com/jetstack/preflight/pkg/logs"
"github.com/jetstack/preflight/pkg/version"
Expand Down Expand Up @@ -79,26 +81,42 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
return fmt.Errorf("While evaluating configuration: %v", err)
}

// We need the cluster UID before we progress further so it can be sent along with other data readings

{
restCfg, err := kubeconfig.LoadRESTConfig("")
if err != nil {
return err
var caClient *dataupload.CyberArkClient
if config.MachineHubMode {
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
subdomain := os.Getenv("ARK_SUBDOMAIN")
username := os.Getenv("ARK_USERNAME")
password := []byte(os.Getenv("ARK_SECRET"))

const (
discoveryContextServiceName = "inventory"
separator = "."
)

// TODO(wallrj): Maybe get this URL via the service discovery API.
// https://platform-discovery.integration-cyberark.cloud/api/public/tenant-discovery?allEndpoints=true&bySubdomain=tlskp-test
serviceURL := fmt.Sprintf("https://%s%s%s.%s", subdomain, separator, discoveryContextServiceName, platformDomain)

var (
identityClient *identity.Client
err error
)
if platformDomain == "cyberark.cloud" {
identityClient, err = identity.New(ctx, subdomain)
} else {
discoveryClient := servicediscovery.New(servicediscovery.WithIntegrationEndpoint())
identityClient, err = identity.NewWithDiscoveryClient(ctx, discoveryClient, subdomain)
}

clientset, err := kubernetes.NewForConfig(restCfg)
if err != nil {
return err
return fmt.Errorf("while creating the CyberArk identity client: %v", err)
}

ctx, err = clusteruid.GetClusterUID(ctx, clientset)
if err := identityClient.LoginUsernamePassword(ctx, username, password); err != nil {
return fmt.Errorf("while logging in: %v", err)
}
caClient, err = dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
if err != nil {
return fmt.Errorf("failed to get cluster UID: %v", err)
return fmt.Errorf("while creating the CyberArk dataupload client: %v", err)
}

clusterUID := clusteruid.ClusterUIDFromContext(ctx)
log.V(logs.Debug).Info("Retrieved cluster UID", "clusterUID", clusterUID)
}

group, gctx := errgroup.WithContext(ctx)
Expand Down Expand Up @@ -262,7 +280,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
// be cancelled, which will cause this blocking loop to exit
// instead of waiting for the time period.
for {
if err := gatherAndOutputData(klog.NewContext(ctx, log), eventf, config, preflightClient, dataGatherers); err != nil {
if err := gatherAndOutputData(klog.NewContext(ctx, log), eventf, config, preflightClient, caClient, dataGatherers); err != nil {
return err
}

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

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

Expand Down Expand Up @@ -362,8 +380,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf

if config.MachineHubMode {
post := func() (any, error) {
log.Info("machine hub mode not yet implemented")
return struct{}{}, nil
return struct{}{}, caClient.PostDataReadingsWithOptions(ctx, readings, dataupload.Options{})
}

group.Go(func() error {
Expand Down
45 changes: 0 additions & 45 deletions pkg/clusteruid/clusteruid.go

This file was deleted.

39 changes: 0 additions & 39 deletions pkg/clusteruid/clusteruid_test.go

This file was deleted.

Loading
Loading