Skip to content

Commit deb947b

Browse files
committed
chore: make spiffe optional
Signed-off-by: AhmedGrati <[email protected]>
1 parent e8748c8 commit deb947b

File tree

8 files changed

+72
-39
lines changed

8 files changed

+72
-39
lines changed

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ deploy: yamls
120120
templates:
121121
@# Need to prepend each line in the sample config with spaces in order to
122122
@# fit correctly in the configmap spec.
123-
@sed s'/^/ /' deployment/components/worker-config/nfd-worker.conf.example > nfd-worker.conf.tmp
124-
@sed s'/^/ /' deployment/components/master-config/nfd-master.conf.example > nfd-master.conf.tmp
125-
@sed s'/^/ /' deployment/components/topology-updater-config/nfd-topology-updater.conf.example > nfd-topology-updater.conf.tmp
123+
@gsed s'/^/ /' deployment/components/worker-config/nfd-worker.conf.example > nfd-worker.conf.tmp
124+
@gsed s'/^/ /' deployment/components/master-config/nfd-master.conf.example > nfd-master.conf.tmp
125+
@gsed s'/^/ /' deployment/components/topology-updater-config/nfd-topology-updater.conf.example > nfd-topology-updater.conf.tmp
126126
@# The sed magic below replaces the block of text between the lines with start and end markers
127127
@start=NFD-MASTER-CONF-START-DO-NOT-REMOVE; \
128128
end=NFD-MASTER-CONF-END-DO-NOT-REMOVE; \
129-
sed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-master.conf.tmp" \
129+
gsed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-master.conf.tmp" \
130130
-e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml
131131
@start=NFD-WORKER-CONF-START-DO-NOT-REMOVE; \
132132
end=NFD-WORKER-CONF-END-DO-NOT-REMOVE; \
133-
sed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-worker.conf.tmp" \
133+
gsed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-worker.conf.tmp" \
134134
-e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml
135135
@start=NFD-TOPOLOGY-UPDATER-CONF-START-DO-NOT-REMOVE; \
136136
end=NFD-TOPOLOGY-UPDATER-CONF-END-DO-NOT-REMOVE; \
137-
sed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-topology-updater.conf.tmp" \
137+
gsed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-topology-updater.conf.tmp" \
138138
-e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml
139139
@rm nfd-master.conf.tmp
140140
@rm nfd-worker.conf.tmp

cmd/nfd-master/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func main() {
7171
args.Overrides.ResyncPeriod = overrides.ResyncPeriod
7272
case "nfd-api-parallelism":
7373
args.Overrides.NfdApiParallelism = overrides.NfdApiParallelism
74+
case "enable-spiffe":
75+
args.Overrides.EnableSpiffe = overrides.EnableSpiffe
7476
case "enable-nodefeature-api":
7577
klog.InfoS("-enable-nodefeature-api is deprecated, will be removed in a future release along with the deprecated gRPC API")
7678
case "ca-file":
@@ -181,6 +183,7 @@ func initFlags(flagset *flag.FlagSet) (*master.Args, *master.ConfigOverrideArgs)
181183
"It has an effect when the NodeFeature API has been enabled (with -enable-nodefeature-api).")
182184
overrides.NfdApiParallelism = flagset.Int("nfd-api-parallelism", 10, "Defines the maximum number of goroutines responsible of updating nodes. "+
183185
"Can be used for the throttling mechanism. It has effect only when -enable-nodefeature-api has been set.")
184-
186+
overrides.EnableSpiffe = flagset.Bool("enable-spiffe", false,
187+
"Enables the Spiffe signature verification of created CRDs. This is still an EXPERIMENTAL feature.")
185188
return args, overrides
186189
}

cmd/nfd-worker/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func parseArgs(flags *flag.FlagSet, osArgs ...string) *worker.Args {
104104
args.Overrides.FeatureSources = overrides.FeatureSources
105105
case "label-sources":
106106
args.Overrides.LabelSources = overrides.LabelSources
107+
case "enable-spiffe":
108+
args.Overrides.EnableSpiffe = overrides.EnableSpiffe
107109
}
108110
})
109111

@@ -158,6 +160,8 @@ func initFlags(flagset *flag.FlagSet) (*worker.Args, *worker.ConfigOverrideArgs)
158160
flagset.Var(overrides.LabelSources, "label-sources",
159161
"Comma separated list of label sources. Special value 'all' enables all sources. "+
160162
"Prefix the source name with '-' to disable it.")
163+
overrides.EnableSpiffe = flagset.Bool("enable-spiffe", false,
164+
"Enables the Spiffe signature verification of created CRDs. This is still an EXPERIMENTAL feature.")
161165

162166
return args, overrides
163167
}

deployment/components/master-config/nfd-master.conf.example

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# denyLabelNs: ["denied.ns.io","denied.kubernetes.io"]
44
# resourceLabels: ["vendor-1.com/feature-1","vendor-2.io/feature-2"]
55
# enableTaints: false
6+
# enableSpiffe: true
67
# labelWhiteList: "foo"
78
# resyncPeriod: "2h"
89
# klog:

deployment/components/worker-config/nfd-worker.conf.example

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# labelWhiteList:
33
# noPublish: false
44
# sleepInterval: 60s
5+
# enableSpiffe: true
56
# featureSources: [all]
67
# labelSources: [all]
78
# klog:

deployment/helm/node-feature-discovery/values.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ master:
2424
# denyLabelNs: ["denied.ns.io","denied.kubernetes.io"]
2525
# resourceLabels: ["vendor-1.com/feature-1","vendor-2.io/feature-2"]
2626
# enableTaints: false
27-
# labelWhiteList: "foo"
2827
# enableSpiffe: true
28+
# labelWhiteList: "foo"
2929
# resyncPeriod: "2h"
3030
# klog:
3131
# addDirHeader: false
@@ -144,9 +144,9 @@ worker:
144144
# labelWhiteList:
145145
# noPublish: false
146146
# sleepInterval: 60s
147+
# enableSpiffe: true
147148
# featureSources: [all]
148149
# labelSources: [all]
149-
# enableSpiffe: true
150150
# klog:
151151
# addDirHeader: false
152152
# alsologtostderr: false

pkg/nfd-master/nfd-master.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type NFDConfig struct {
8686
LeaderElection LeaderElectionConfig
8787
NfdApiParallelism int
8888
Klog klogutils.KlogConfigOpts
89+
EnableSpiffe bool
8990
}
9091

9192
// LeaderElectionConfig contains the configuration for leader election
@@ -105,6 +106,7 @@ type ConfigOverrideArgs struct {
105106
NoPublish *bool
106107
ResyncPeriod *utils.DurationVal
107108
NfdApiParallelism *int
109+
EnableSpiffe *bool
108110
}
109111

110112
// Args holds command line arguments
@@ -211,6 +213,7 @@ func newDefaultConfig() *NFDConfig {
211213
NfdApiParallelism: 10,
212214
ResourceLabels: utils.StringSetVal{},
213215
EnableTaints: false,
216+
EnableSpiffe: false,
214217
ResyncPeriod: utils.DurationVal{Duration: time.Duration(1) * time.Hour},
215218
LeaderElection: LeaderElectionConfig{
216219
LeaseDuration: utils.DurationVal{Duration: time.Duration(15) * time.Second},
@@ -764,19 +767,11 @@ func (m *nfdMaster) nfdAPIUpdateOneNode(nodeName string) error {
764767
return objs[i].Namespace < objs[j].Namespace
765768
})
766769

767-
verifiedObjects := []*v1alpha1.NodeFeature{}
768-
// Verify nfd objects signature
769-
for _, obj := range objs {
770-
isSignatureVerified, err := m.spiffeClient.VerifyDataSignature(obj.Spec, spiffe.GetSpiffeId(utils.NodeName()), obj.Annotations["signature"])
770+
// If spiffe is enabled, we should filter out the non verified NFD objects
771+
if m.config.EnableSpiffe {
772+
objs, err = m.getVerifiedNFDObjects(objs)
771773
if err != nil {
772-
klog.ErrorS(err, "error while getting data signature")
773-
return fmt.Errorf("failed to sign CRD data using Spiffe: %w", err)
774-
}
775-
if isSignatureVerified {
776-
klog.InfoS("data verified", "nfd name", obj.Name)
777-
verifiedObjects = append(verifiedObjects, obj)
778-
} else {
779-
klog.InfoS("data not verified", "nfd name", obj.Name)
774+
return err
780775
}
781776
}
782777

@@ -790,13 +785,13 @@ func (m *nfdMaster) nfdAPIUpdateOneNode(nodeName string) error {
790785

791786
annotations := Annotations{}
792787

793-
if len(verifiedObjects) > 0 {
788+
if len(objs) > 0 {
794789
// Merge in features
795790
//
796791
// NOTE: changing the rule api to support handle multiple objects instead
797792
// of merging would probably perform better with lot less data to copy.
798-
features = verifiedObjects[0].Spec.DeepCopy()
799-
for _, o := range verifiedObjects[1:] {
793+
features = objs[0].Spec.DeepCopy()
794+
for _, o := range objs[1:] {
800795
o.Spec.MergeInto(features)
801796
}
802797

@@ -1261,6 +1256,9 @@ func (m *nfdMaster) configure(filepath string, overrides string) error {
12611256
if m.args.Overrides.NfdApiParallelism != nil {
12621257
c.NfdApiParallelism = *m.args.Overrides.NfdApiParallelism
12631258
}
1259+
if m.args.Overrides.EnableSpiffe != nil {
1260+
c.EnableSpiffe = *m.args.Overrides.EnableSpiffe
1261+
}
12641262

12651263
if c.NfdApiParallelism <= 0 {
12661264
return fmt.Errorf("the maximum number of concurrent labelers should be a non-zero positive number")
@@ -1407,3 +1405,22 @@ func (m *nfdMaster) nfdAPIUpdateHandlerWithLeaderElection() {
14071405

14081406
leaderElector.Run(ctx)
14091407
}
1408+
1409+
func (m *nfdMaster) getVerifiedNFDObjects(objs []*v1alpha1.NodeFeature) ([]*v1alpha1.NodeFeature, error) {
1410+
verifiedObjects := []*v1alpha1.NodeFeature{}
1411+
1412+
for _, obj := range objs {
1413+
isSignatureVerified, err := m.spiffeClient.VerifyDataSignature(obj.Spec, spiffe.GetSpiffeId(utils.NodeName()), obj.Annotations["signature"])
1414+
if err != nil {
1415+
return nil, fmt.Errorf("failed to verify NodeFeature signature: %w", err)
1416+
}
1417+
1418+
if isSignatureVerified {
1419+
klog.InfoS("NodeFeature verified", "NodeFeature name", obj.Name)
1420+
verifiedObjects = append(verifiedObjects, obj)
1421+
} else {
1422+
klog.InfoS("NodeFeature not verified, skipping...", "NodeFeature name", obj.Name)
1423+
}
1424+
}
1425+
return verifiedObjects, nil
1426+
}

pkg/nfd-worker/nfd-worker.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type coreConfig struct {
8787
Sources *[]string
8888
LabelSources []string
8989
SleepInterval utils.DurationVal
90+
EnableSpiffe bool
9091
}
9192

9293
type sourcesConfig map[string]source.Config
@@ -118,6 +119,7 @@ type ConfigOverrideArgs struct {
118119

119120
FeatureSources *utils.StringSliceVal
120121
LabelSources *utils.StringSliceVal
122+
EnableSpiffe *bool
121123
}
122124

123125
type nfdWorker struct {
@@ -539,6 +541,9 @@ func (w *nfdWorker) configure(filepath string, overrides string) error {
539541
if w.args.Overrides.LabelSources != nil {
540542
c.Core.LabelSources = *w.args.Overrides.LabelSources
541543
}
544+
if w.args.Overrides.EnableSpiffe != nil {
545+
c.Core.EnableSpiffe = *w.args.Overrides.EnableSpiffe
546+
}
542547

543548
c.Core.sanitize()
544549

@@ -712,14 +717,15 @@ func (m *nfdWorker) updateNodeFeatureObject(labels Labels) error {
712717
},
713718
}
714719

715-
signature, err := m.spiffeClient.SignData(nfr.Spec, spiffe.GetSpiffeId(utils.NodeName()))
716-
klog.InfoS("data signature", "signature", string(signature))
717-
if err != nil {
718-
klog.ErrorS(err, "error while getting data signature")
719-
return fmt.Errorf("failed to sign CRD data using Spiffe: %w", err)
720+
// If Spiffe is enabled, we add the signature to the annotations section
721+
if m.config.Core.EnableSpiffe {
722+
signature, err := m.spiffeClient.SignData(nfr.Spec, spiffe.GetSpiffeId(utils.NodeName()))
723+
if err != nil {
724+
return fmt.Errorf("failed to sign CRD data using Spiffe: %w", err)
725+
}
726+
encodedSignature := b64.StdEncoding.EncodeToString(signature)
727+
nfr.ObjectMeta.Annotations["signature"] = encodedSignature
720728
}
721-
encodedSignature := b64.StdEncoding.EncodeToString(signature)
722-
nfr.ObjectMeta.Annotations["signature"] = encodedSignature
723729

724730
nfrCreated, err := cli.NfdV1alpha1().NodeFeatures(namespace).Create(context.TODO(), nfr, metav1.CreateOptions{})
725731
if err != nil {
@@ -738,15 +744,16 @@ func (m *nfdWorker) updateNodeFeatureObject(labels Labels) error {
738744
Labels: labels,
739745
}
740746

741-
signature, err := m.spiffeClient.SignData(nfrUpdated.Spec, spiffe.GetSpiffeId(utils.NodeName()))
742-
encodedSignature := b64.StdEncoding.EncodeToString(signature)
743-
klog.InfoS("data signature", "signature", encodedSignature)
744-
if err != nil {
745-
klog.ErrorS(err, "error while getting data signature")
746-
return fmt.Errorf("failed to sign CRD data using Spiffe: %w", err)
747-
}
747+
if m.config.Core.EnableSpiffe {
748+
signature, err := m.spiffeClient.SignData(nfrUpdated.Spec, spiffe.GetSpiffeId(utils.NodeName()))
748749

749-
nfrUpdated.ObjectMeta.Annotations["signature"] = encodedSignature
750+
if err != nil {
751+
return fmt.Errorf("failed to sign CRD data using Spiffe: %w", err)
752+
}
753+
754+
encodedSignature := b64.StdEncoding.EncodeToString(signature)
755+
nfrUpdated.ObjectMeta.Annotations["signature"] = encodedSignature
756+
}
750757

751758
if !apiequality.Semantic.DeepEqual(nfr, nfrUpdated) {
752759
klog.InfoS("updating NodeFeature object", "nodefeature", klog.KObj(nfr))

0 commit comments

Comments
 (0)