Skip to content

Commit 2a78ff1

Browse files
authored
Remove k8s API access from NGINX pod (#4368)
Problem: The init container in the NGINX pod needed k8s API access for NGINX Plus licensing purposes. However, this data could be provided by the control plane without the init container needing the API access. For security reasons, the NGINX pod shouldn't have any access to the API. Solution: Remove API access and provide the necessary data directly to the pod.
1 parent cc80ed5 commit 2a78ff1

File tree

4 files changed

+27
-46
lines changed

4 files changed

+27
-46
lines changed

cmd/gateway/commands.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ import (
1313
"go.uber.org/zap"
1414
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1515
"k8s.io/klog/v2"
16-
ctlr "sigs.k8s.io/controller-runtime"
1716
"sigs.k8s.io/controller-runtime/pkg/client"
1817
k8sConfig "sigs.k8s.io/controller-runtime/pkg/client/config"
1918
"sigs.k8s.io/controller-runtime/pkg/log"
2019
ctlrZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
2120

2221
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller"
2322
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/config"
24-
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/licensing"
2523
ngxConfig "github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config"
2624
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/file"
2725
)
@@ -667,10 +665,9 @@ func createInitializeCommand() *cobra.Command {
667665
return fmt.Errorf("could not get pod UID: %w", err)
668666
}
669667

670-
clusterCfg := ctlr.GetConfigOrDie()
671-
k8sReader, err := client.New(clusterCfg, client.Options{})
668+
clusterUID, err := getValueFromEnv("CLUSTER_UID")
672669
if err != nil {
673-
return fmt.Errorf("unable to initialize k8s client: %w", err)
670+
return fmt.Errorf("could not get cluster UID: %w", err)
674671
}
675672

676673
logger := ctlrZap.New()
@@ -684,12 +681,6 @@ func createInitializeCommand() *cobra.Command {
684681
)
685682
log.SetLogger(logger)
686683

687-
dcc := licensing.NewDeploymentContextCollector(licensing.DeploymentContextCollectorConfig{
688-
K8sClientReader: k8sReader,
689-
PodUID: podUID,
690-
Logger: logger.WithName("deployCtxCollector"),
691-
})
692-
693684
files := make([]fileToCopy, 0, len(srcFiles))
694685
for i, src := range srcFiles {
695686
files = append(files, fileToCopy{
@@ -702,8 +693,9 @@ func createInitializeCommand() *cobra.Command {
702693
fileManager: file.NewStdLibOSFileManager(),
703694
fileGenerator: ngxConfig.NewGeneratorImpl(plus, nil, logger.WithName("generator")),
704695
logger: logger,
696+
podUID: podUID,
697+
clusterUID: clusterUID,
705698
plus: plus,
706-
collector: dcc,
707699
copy: files,
708700
})
709701
},

cmd/gateway/initialize.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"path/filepath"
8-
"time"
97

108
"github.com/go-logr/logr"
119

12-
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/licensing"
1310
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config"
11+
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/dataplane"
1412
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/file"
1513
)
1614

1715
const (
18-
collectDeployCtxTimeout = 10 * time.Second
16+
integrationID = "ngf"
1917
)
2018

2119
type fileToCopy struct {
@@ -24,10 +22,11 @@ type fileToCopy struct {
2422
}
2523

2624
type initializeConfig struct {
27-
collector licensing.Collector
2825
fileManager file.OSFileManager
2926
fileGenerator config.Generator
3027
logger logr.Logger
28+
podUID string
29+
clusterUID string
3130
copy []fileToCopy
3231
plus bool
3332
}
@@ -44,16 +43,12 @@ func initialize(cfg initializeConfig) error {
4443
return nil
4544
}
4645

47-
ctx, cancel := context.WithTimeout(context.Background(), collectDeployCtxTimeout)
48-
defer cancel()
49-
50-
depCtx, err := cfg.collector.Collect(ctx)
51-
if err != nil {
52-
cfg.logger.Error(err, "error collecting deployment context")
46+
depCtx := dataplane.DeploymentContext{
47+
InstallationID: &cfg.podUID,
48+
ClusterID: &cfg.clusterUID,
49+
Integration: integrationID,
5350
}
5451

55-
cfg.logger.Info("Deployment context collected", "deployment context", depCtx)
56-
5752
depCtxFile, err := cfg.fileGenerator.GenerateDeploymentContext(depCtx)
5853
if err != nil {
5954
return fmt.Errorf("failed to generate deployment context file: %w", err)

cmd/gateway/initialize_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"errors"
65
"io"
76
"os"
@@ -11,7 +10,6 @@ import (
1110
"github.com/go-logr/logr"
1211
. "github.com/onsi/gomega"
1312

14-
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/licensing/licensingfakes"
1513
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config/configfakes"
1614
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/dataplane"
1715
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/file"
@@ -91,18 +89,9 @@ func TestInitialize_Plus(t *testing.T) {
9189
{
9290
name: "normal",
9391
collectErr: nil,
94-
depCtx: dataplane.DeploymentContext{
95-
Integration: "ngf",
96-
ClusterID: helpers.GetPointer("cluster-id"),
97-
InstallationID: helpers.GetPointer("install-id"),
98-
ClusterNodeCount: helpers.GetPointer(2),
99-
},
100-
},
101-
{
102-
name: "collecting deployment context errors",
103-
collectErr: errors.New("collect error"),
10492
depCtx: dataplane.DeploymentContext{
10593
Integration: "ngf",
94+
ClusterID: helpers.GetPointer("cluster-id"),
10695
InstallationID: helpers.GetPointer("install-id"),
10796
},
10897
},
@@ -114,17 +103,11 @@ func TestInitialize_Plus(t *testing.T) {
114103
g := NewWithT(t)
115104

116105
fakeFileMgr := &filefakes.FakeOSFileManager{}
117-
fakeCollector := &licensingfakes.FakeCollector{
118-
CollectStub: func(_ context.Context) (dataplane.DeploymentContext, error) {
119-
return test.depCtx, test.collectErr
120-
},
121-
}
122106
fakeGenerator := &configfakes.FakeGenerator{}
123107

124108
ic := initializeConfig{
125109
fileManager: fakeFileMgr,
126110
logger: logr.Discard(),
127-
collector: fakeCollector,
128111
fileGenerator: fakeGenerator,
129112
copy: []fileToCopy{
130113
{
@@ -136,7 +119,9 @@ func TestInitialize_Plus(t *testing.T) {
136119
srcFileName: "src2",
137120
},
138121
},
139-
plus: true,
122+
podUID: "install-id",
123+
clusterUID: "cluster-id",
124+
plus: true,
140125
}
141126

142127
g.Expect(initialize(ic)).To(Succeed())
@@ -149,7 +134,6 @@ func TestInitialize_Plus(t *testing.T) {
149134
// write deploy ctx
150135
g.Expect(fakeGenerator.GenerateDeploymentContextCallCount()).To(Equal(1))
151136
g.Expect(fakeGenerator.GenerateDeploymentContextArgsForCall(0)).To(Equal(test.depCtx))
152-
g.Expect(fakeCollector.CollectCallCount()).To(Equal(1))
153137
g.Expect(fakeFileMgr.WriteCallCount()).To(Equal(1))
154138
g.Expect(fakeFileMgr.ChmodCallCount()).To(Equal(3))
155139
})

internal/controller/provisioner/objects.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,19 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
859859
image, pullPolicy := p.buildImage(nProxyCfg)
860860
tokenAudience := fmt.Sprintf("%s.%s.svc", p.cfg.GatewayPodConfig.ServiceName, p.cfg.GatewayPodConfig.Namespace)
861861

862+
clusterID := "unknown"
863+
if p.cfg.AgentLabels != nil {
864+
if val, ok := p.cfg.AgentLabels["cluster-id"]; ok {
865+
clusterID = val
866+
}
867+
}
868+
862869
spec := corev1.PodTemplateSpec{
863870
ObjectMeta: metav1.ObjectMeta{
864871
Labels: objectMeta.Labels,
865872
Annotations: podAnnotations,
866873
},
867874
Spec: corev1.PodSpec{
868-
AutomountServiceAccountToken: helpers.GetPointer(true),
869875
Containers: []corev1.Container{
870876
{
871877
Name: "nginx",
@@ -926,6 +932,10 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
926932
},
927933
},
928934
},
935+
{
936+
Name: "CLUSTER_UID",
937+
Value: clusterID,
938+
},
929939
},
930940
VolumeMounts: []corev1.VolumeMount{
931941
{MountPath: "/agent", Name: "nginx-agent-config"},

0 commit comments

Comments
 (0)