Skip to content

PoC: Generate SPIFFE identities in csi-driver #251

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
IssuerKindKey = "csi.cert-manager.io/issuer-kind"
IssuerGroupKey = "csi.cert-manager.io/issuer-group"

InjectSPIFFEKey = "csi.cert-manager.io/inject-spiffe"

CommonNameKey = "csi.cert-manager.io/common-name"
DNSNamesKey = "csi.cert-manager.io/dns-names"
IPSANsKey = "csi.cert-manager.io/ip-sans"
Expand Down
23 changes: 22 additions & 1 deletion pkg/requestgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func RequestForMetadata(meta metadata.Metadata) (*manager.CertificateRequestBund
}
}

var uris []*url.URL

commonName, err := expand(meta, attrs[csiapi.CommonNameKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.CommonNameKey, err)
Expand All @@ -65,7 +67,7 @@ func RequestForMetadata(meta metadata.Metadata) (*manager.CertificateRequestBund
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.DNSNamesKey, err)
}
uris, err := parseURIs(meta, attrs[csiapi.URISANsKey])
uris, err = parseURIs(meta, attrs[csiapi.URISANsKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.URISANsKey, err)
}
Expand All @@ -74,6 +76,25 @@ func RequestForMetadata(meta metadata.Metadata) (*manager.CertificateRequestBund
return nil, fmt.Errorf("%q: %w", csiapi.IPSANsKey, err)
}

_, shouldInjectSPIFFE := attrs[csiapi.InjectSPIFFEKey]
if shouldInjectSPIFFE {
if len(uris) > 0 {
return nil, fmt.Errorf("cannot inject SPIFFE ID (%q) if custom URIs are given with %q", csiapi.InjectSPIFFEKey, csiapi.URISANsKey)
}

saName := meta.VolumeContext["csi.storage.k8s.io/serviceAccount.name"]
saNamespace := meta.VolumeContext["csi.storage.k8s.io/pod.namespace"]

// TODO: configurable trust domain
spiffeID := fmt.Sprintf("spiffe://%s/ns/%s/sa/%s", "example.com", saNamespace, saName)
uri, err := url.Parse(spiffeID)
if err != nil {
return nil, fmt.Errorf("internal error crafting X.509 URI, this is a bug, please report on GitHub: %w", err)
}

uris = []*url.URL{uri}
}

annotations := make(map[string]string)
for key, val := range attrs {
group, _, found := strings.Cut(key, "/")
Expand Down