Skip to content

Commit

Permalink
Merge pull request #16 from cofide/fix-workload-id-fmt
Browse files Browse the repository at this point in the history
Fix formatting of workload IDs in workload list command
  • Loading branch information
markgoddard authored Nov 15, 2024
2 parents 764d2f1 + 9ad1e9d commit 05dd383
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 05dd383

Please sign in to comment.