Skip to content

Commit

Permalink
Fix formatting of workload IDs in workload list command
Browse files Browse the repository at this point in the history
Previously they were displayed as:

  trust_domain:"td1" path:"/ns/ns3/sa/default"

Now they are displayed as a URL:

  spiffe://td1/ns/ns3/sa/default
  • Loading branch information
markgoddard committed Nov 13, 2024
1 parent 5ed88f8 commit 9ad1e9d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions internal/pkg/spire/spire.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

kubeutil "github.com/cofide/cofidectl/internal/pkg/kube"
"github.com/spiffe/go-spiffe/v2/spiffeid"
types "github.com/spiffe/spire-api-sdk/proto/spire/api/types"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -218,7 +219,7 @@ func getPodsforDaemonSet(ctx context.Context, client *kubeutil.Client, daemonset

// RegisteredEntry contains details of a workload registered with SPIRE
type RegisteredEntry struct {
Id *types.SPIFFEID
Id string
}

func GetRegistrationEntries(ctx context.Context, client *kubeutil.Client) (map[string]*RegisteredEntry, error) {
Expand Down Expand Up @@ -257,8 +258,25 @@ func GetRegistrationEntries(ctx context.Context, client *kubeutil.Client) (map[s
continue
}

registrationEntriesMap[podUID] = &RegisteredEntry{registrationEntry.Id}
id, err := formatIdUrl(registrationEntry.Id)
if err != nil {
return nil, err
}
registrationEntriesMap[podUID] = &RegisteredEntry{Id: id}
}

return registrationEntriesMap, nil
}

// formatIdUrl formats a SPIFFE ID as a URL string.
func formatIdUrl(id *types.SPIFFEID) (string, error) {
trustDomain, err := spiffeid.TrustDomainFromString(id.TrustDomain)
if err != nil {
return "", err
}
if id, err := spiffeid.FromPath(trustDomain, id.Path); err != nil {
return "", err
} else {
return id.String(), nil
}
}
2 changes: 1 addition & 1 deletion internal/pkg/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GetRegisteredWorkloads(ctx context.Context, kubeConfig string, kubeContext
registeredWorkload := &Workload{
Name: pod.Name,
Namespace: pod.Namespace,
SPIFFEID: registeredEntry.Id.String(),
SPIFFEID: registeredEntry.Id,
Status: string(pod.Status.Phase),
Type: "Pod",
}
Expand Down

0 comments on commit 9ad1e9d

Please sign in to comment.