Skip to content

Commit 4f367c3

Browse files
authored
Fix Agent volumes when an association has no CA (#4833) (#4834)
This commit fixes an issue that occurs if you associate an Agent with a Kibana without TLS. In this case, the association Agent<->Kibana does not have a CA and this breaks the volumeMounts of the Agent container because we returned instead of continuing to populate the slice of volumes.
1 parent 65962fb commit 4f367c3

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

pkg/controller/agent/pod.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,17 @@ func writeEsAssocToConfigHash(params Params, esAssociation commonv1.Association,
252252
}
253253

254254
func getVolumesFromAssociations(associations []commonv1.Association) []volume.VolumeLike {
255-
vols := []volume.VolumeLike{}
256-
for i, association := range associations {
257-
if !association.AssociationConf().CAIsConfigured() {
258-
return nil
255+
var vols []volume.VolumeLike //nolint:prealloc
256+
for i, assoc := range associations {
257+
if !assoc.AssociationConf().CAIsConfigured() {
258+
// skip as there is no volume to mount if association has no CA configured
259+
continue
259260
}
260-
caSecretName := association.AssociationConf().GetCASecretName()
261+
caSecretName := assoc.AssociationConf().GetCASecretName()
261262
vols = append(vols, volume.NewSecretVolumeWithMountPath(
262263
caSecretName,
263-
fmt.Sprintf("%s-certs-%d", association.AssociationType(), i),
264-
certificatesDir(association),
264+
fmt.Sprintf("%s-certs-%d", assoc.AssociationType(), i),
265+
certificatesDir(assoc),
265266
))
266267
}
267268
return vols

pkg/controller/agent/pod_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,66 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
185185
}
186186
}
187187

188+
func Test_getVolumesFromAssociations(t *testing.T) {
189+
// Note: we use setAssocConfs to set the AssociationConfs which are normally set in the reconciliation loop.
190+
for _, tt := range []struct {
191+
name string
192+
params Params
193+
setAssocConfs func(assocs []v1.Association)
194+
wantAssociationsLength int
195+
}{
196+
{
197+
name: "fleet mode enabled, kb ref, fleet ref",
198+
params: Params{
199+
Agent: agentv1alpha1.Agent{
200+
Spec: agentv1alpha1.AgentSpec{
201+
Mode: agentv1alpha1.AgentFleetMode,
202+
KibanaRef: v1.ObjectSelector{Name: "kibana"},
203+
FleetServerRef: v1.ObjectSelector{Name: "fleet"},
204+
},
205+
},
206+
},
207+
setAssocConfs: func(assocs []v1.Association) {
208+
assocs[0].SetAssociationConf(&v1.AssociationConf{
209+
CASecretName: "kibana-kb-http-certs-public",
210+
})
211+
assocs[1].SetAssociationConf(&v1.AssociationConf{
212+
CASecretName: "fleet-agent-http-certs-public",
213+
})
214+
},
215+
wantAssociationsLength: 2,
216+
},
217+
{
218+
name: "fleet mode enabled, kb no tls ref, fleet ref",
219+
params: Params{
220+
Agent: agentv1alpha1.Agent{
221+
Spec: agentv1alpha1.AgentSpec{
222+
Mode: agentv1alpha1.AgentFleetMode,
223+
KibanaRef: v1.ObjectSelector{Name: "kibana"},
224+
FleetServerRef: v1.ObjectSelector{Name: "fleet"},
225+
},
226+
},
227+
},
228+
setAssocConfs: func(assocs []v1.Association) {
229+
assocs[0].SetAssociationConf(&v1.AssociationConf{
230+
// No CASecretName
231+
})
232+
assocs[1].SetAssociationConf(&v1.AssociationConf{
233+
CASecretName: "fleet-agent-http-certs-public",
234+
})
235+
},
236+
wantAssociationsLength: 1,
237+
},
238+
} {
239+
t.Run(tt.name, func(t *testing.T) {
240+
assocs := tt.params.Agent.GetAssociations()
241+
tt.setAssocConfs(assocs)
242+
associations := getVolumesFromAssociations(assocs)
243+
require.Equal(t, tt.wantAssociationsLength, len(associations))
244+
})
245+
}
246+
}
247+
188248
func Test_getRelatedEsAssoc(t *testing.T) {
189249
for _, tt := range []struct {
190250
name string

0 commit comments

Comments
 (0)