Skip to content

Commit

Permalink
adding support for JWT SVID TTL (#189)
Browse files Browse the repository at this point in the history
* added jwt ttl field

Signed-off-by: Unnathi Kumar <[email protected]>
  • Loading branch information
unnathik authored Aug 17, 2023
1 parent f7510b0 commit e95aac2
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 4 deletions.
6 changes: 5 additions & 1 deletion api/v1alpha1/clusterspiffeid_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ type ClusterSPIFFEIDSpec struct {
// available to the template under .NodeSpec, .PodSpec respectively.
SPIFFEIDTemplate string `json:"spiffeIDTemplate"`

// TTL indicates an upper-bound time-to-live for SVIDs minted for this
// TTL indicates an upper-bound time-to-live for X509 SVIDs minted for this
// ClusterSPIFFEID. If unset, a default will be chosen.
TTL metav1.Duration `json:"ttl,omitempty"`

// JWTTTL indicates an upper-bound time-to-live for JWT SVIDs minted for this
// ClusterSPIFFEID.
JWTTTL metav1.Duration `json:"jwtTtl,omitempty"`

// DNSNameTemplate represents templates for extra DNS names that are
// applicable to SVIDs minted for this ClusterSPIFFEID.
// The node and pod spec are made available to the template under
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/clusterspiffeid_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type ParsedClusterSPIFFEIDSpec struct {
NamespaceSelector labels.Selector
PodSelector labels.Selector
TTL time.Duration
JWTTTL time.Duration
FederatesWith []spiffeid.TrustDomain
DNSNameTemplates []*template.Template
WorkloadSelectorTemplates []*template.Template
Expand Down Expand Up @@ -152,6 +153,7 @@ func ParseClusterSPIFFEIDSpec(spec *ClusterSPIFFEIDSpec) (*ParsedClusterSPIFFEID
NamespaceSelector: namespaceSelector,
PodSelector: podSelector,
TTL: spec.TTL.Duration,
JWTTTL: spec.JWTTTL.Duration,
FederatesWith: federatesWith,
DNSNameTemplates: dnsNameTemplates,
WorkloadSelectorTemplates: workloadSelectorTemplates,
Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/spire.spiffe.io_clusterspiffeids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ spec:
items:
type: string
type: array
jwtTtl:
description: JWTTTL indicates an upper-bound time-to-live for JWT
SVIDs minted for this ClusterSPIFFEID.
type: string
namespaceSelector:
description: NamespaceSelector selects the namespaces that are targeted
by this CRD.
Expand Down Expand Up @@ -156,8 +160,8 @@ spec:
respectively.
type: string
ttl:
description: TTL indicates an upper-bound time-to-live for SVIDs minted
for this ClusterSPIFFEID. If unset, a default will be chosen.
description: TTL indicates an upper-bound time-to-live for X509 SVIDs
minted for this ClusterSPIFFEID. If unset, a default will be chosen.
type: string
workloadSelectorTemplates:
description: WorkloadSelectorTemplates are templates to produce arbitrary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ spec:
items:
type: string
type: array
jwtTtl:
description: JWTTTL indicates an upper-bound time-to-live for JWT
SVIDs minted for this ClusterSPIFFEID.
type: string
namespaceSelector:
description: NamespaceSelector selects the namespaces that are targetted
by this CRD.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ spec:
items:
type: string
type: array
jwtTtl:
description: JWTTTL indicates an upper-bound time-to-live for JWT
SVIDs minted for this ClusterSPIFFEID.
type: string
namespaceSelector:
description: NamespaceSelector selects the namespaces that are targetted
by this CRD.
Expand Down
3 changes: 2 additions & 1 deletion docs/clusterspiffeid-crd.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ The definition can be found [here](../api/v1alpha1/clusterspiffeid_types.go).
| `namespaceSelector` | OPTIONAL | A label selector used to scope which workload namespaces this ClusterSPIFFEID targets |
| `dnsNameTemplates` | OPTIONAL | One or more templates used to render DNS names for the target workload. See [Templates](#templates). |
| `workloadSelectorTemplates` | OPTIONAL | One or more templates used to render additional selectors for the target workload. See [Templates](#templates). |
| `ttl` | OPTIONAL | Duration value indicating an upper bound on the time-to-live for SVIDs issued to target workload |
| `ttl` | OPTIONAL | Duration value indicating an upper bound on the time-to-live for X509-SVIDs issued to target workload |
| `jwtTtl` | OPTIONAL | Duration value indicating an upper bound on the time-to-live for JWT-SVIDs issued to target workload |
| `federatesWith` | OPTIONAL | One or more trust domain names that target workloads federate with |
| `admin` | OPTIONAL | Indicates whether the target workload is an admin workload (i.e. can access SPIRE administrative APIs) |
| `downstream` | OPTIONAL | Indicates that the entry describes a downstream SPIRE server. |
Expand Down
1 change: 1 addition & 0 deletions pkg/spireentry/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func renderPodEntry(spec *spirev1alpha1.ParsedClusterSPIFFEIDSpec, node *corev1.
ParentID: parentID,
Selectors: selectors,
X509SVIDTTL: spec.TTL,
JWTSVIDTTL: spec.JWTTTL,
FederatesWith: spec.FederatesWith,
DNSNames: dnsNames,
Admin: spec.Admin,
Expand Down
34 changes: 34 additions & 0 deletions pkg/spireentry/entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spireentry

import (
"testing"
"time"

"github.com/spiffe/go-spiffe/v2/spiffeid"
spirev1alpha1 "github.com/spiffe/spire-controller-manager/api/v1alpha1"
Expand Down Expand Up @@ -64,3 +65,36 @@ func TestRenderPodEntry(t *testing.T) {
require.Contains(t, entry.DNSNames, pod.Name+"."+pod.Namespace+".svc."+clusterDomain)
require.Contains(t, entry.DNSNames, pod.Name+"."+trustDomain+".svc")
}

func TestJWTTTLInRenderPodEntry(t *testing.T) {
spec := &spirev1alpha1.ClusterSPIFFEIDSpec{
SPIFFEIDTemplate: "spiffe://{{ .TrustDomain }}/ns/{{ .PodMeta.Namespace }}/sa/{{ .PodSpec.ServiceAccountName }}",
JWTTTL: metav1.Duration{Duration: time.Duration(60)},
}

node := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
UID: "uid",
},
Spec: corev1.NodeSpec{},
}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "namespace",
},
Spec: corev1.PodSpec{
ServiceAccountName: "test",
},
}

parsedSpec, err := spirev1alpha1.ParseClusterSPIFFEIDSpec(spec)
require.NoError(t, err)
td, err := spiffeid.TrustDomainFromString(trustDomain)
require.NoError(t, err)

entry, err := renderPodEntry(parsedSpec, node, pod, td, clusterName, clusterDomain)
require.NoError(t, err)

require.Equal(t, entry.JWTSVIDTTL.Nanoseconds(), spec.JWTTTL.Nanoseconds())
}

0 comments on commit e95aac2

Please sign in to comment.