Skip to content

Commit a6fa27b

Browse files
dehaansadjaglowski
andauthored
[vcenterreceiver] TLS settings not honored for initial GetServiceContent call (#36482)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description The `govmomi` client used in the receiver attempts to validate the connection to vcenter before the existing code sets the TLS options (other than insecure) in the client. This is a limitation of the `govmomi` wrapper, as discussed on this issue: vmware/govmomi#1200 . <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Related issue in Grafana Alloy: grafana/alloy#193 <!--Describe what testing was performed and which tests were added.--> #### Testing ~~This has not been tested, I would appreciate the assistance of any codeowner that could test.~~ See comments on the PR for test. <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Daniel Jaglowski <[email protected]>
1 parent 56bf5be commit a6fa27b

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

.chloggen/vcenter-tls.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: vcenterreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: The existing code did not honor TLS settings beyond 'insecure'. All TLS client config should now be honored.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36482]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/vcenterreceiver/client.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/vmware/govmomi"
1817
"github.com/vmware/govmomi/find"
1918
"github.com/vmware/govmomi/object"
2019
"github.com/vmware/govmomi/performance"
20+
"github.com/vmware/govmomi/session"
2121
"github.com/vmware/govmomi/view"
2222
"github.com/vmware/govmomi/vim25"
2323
"github.com/vmware/govmomi/vim25/mo"
@@ -30,14 +30,14 @@ import (
3030

3131
// vcenterClient is a client that collects data from a vCenter endpoint.
3232
type vcenterClient struct {
33-
logger *zap.Logger
34-
moClient *govmomi.Client
35-
vimDriver *vim25.Client
36-
vsanDriver *vsan.Client
37-
finder *find.Finder
38-
pm *performance.Manager
39-
vm *view.Manager
40-
cfg *Config
33+
logger *zap.Logger
34+
sessionManager *session.Manager
35+
vimDriver *vim25.Client
36+
vsanDriver *vsan.Client
37+
finder *find.Finder
38+
pm *performance.Manager
39+
vm *view.Manager
40+
cfg *Config
4141
}
4242

4343
var newVcenterClient = defaultNewVcenterClient
@@ -51,8 +51,8 @@ func defaultNewVcenterClient(l *zap.Logger, c *Config) *vcenterClient {
5151

5252
// EnsureConnection will establish a connection to the vSphere SDK if not already established
5353
func (vc *vcenterClient) EnsureConnection(ctx context.Context) error {
54-
if vc.moClient != nil {
55-
sessionActive, _ := vc.moClient.SessionManager.SessionIsActive(ctx)
54+
if vc.sessionManager != nil {
55+
sessionActive, _ := vc.sessionManager.SessionIsActive(ctx)
5656
if sessionActive {
5757
return nil
5858
}
@@ -62,24 +62,30 @@ func (vc *vcenterClient) EnsureConnection(ctx context.Context) error {
6262
if err != nil {
6363
return err
6464
}
65-
client, err := govmomi.NewClient(ctx, sdkURL, vc.cfg.Insecure)
66-
if err != nil {
67-
return fmt.Errorf("unable to connect to vSphere SDK on listed endpoint: %w", err)
68-
}
65+
66+
soapClient := soap.NewClient(sdkURL, vc.cfg.Insecure)
6967
tlsCfg, err := vc.cfg.LoadTLSConfig(ctx)
7068
if err != nil {
7169
return err
7270
}
7371
if tlsCfg != nil {
74-
client.DefaultTransport().TLSClientConfig = tlsCfg
72+
soapClient.DefaultTransport().TLSClientConfig = tlsCfg
73+
}
74+
75+
client, err := vim25.NewClient(ctx, soapClient)
76+
if err != nil {
77+
return fmt.Errorf("unable to connect to vSphere SDK on listed endpoint: %w", err)
7578
}
79+
80+
sessionManager := session.NewManager(client)
81+
7682
user := url.UserPassword(vc.cfg.Username, string(vc.cfg.Password))
77-
err = client.Login(ctx, user)
83+
err = sessionManager.Login(ctx, user)
7884
if err != nil {
7985
return fmt.Errorf("unable to login to vcenter sdk: %w", err)
8086
}
81-
vc.moClient = client
82-
vc.vimDriver = client.Client
87+
vc.sessionManager = sessionManager
88+
vc.vimDriver = client
8389
vc.finder = find.NewFinder(vc.vimDriver)
8490
vc.pm = performance.NewManager(vc.vimDriver)
8591
vc.vm = view.NewManager(vc.vimDriver)
@@ -94,8 +100,8 @@ func (vc *vcenterClient) EnsureConnection(ctx context.Context) error {
94100

95101
// Disconnect will logout of the autenticated session
96102
func (vc *vcenterClient) Disconnect(ctx context.Context) error {
97-
if vc.moClient != nil {
98-
return vc.moClient.Logout(ctx)
103+
if vc.sessionManager != nil {
104+
return vc.sessionManager.Logout(ctx)
99105
}
100106
return nil
101107
}

receiver/vcenterreceiver/client_test.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/require"
12-
"github.com/vmware/govmomi"
1312
"github.com/vmware/govmomi/find"
1413
"github.com/vmware/govmomi/performance"
1514
"github.com/vmware/govmomi/session"
@@ -292,10 +291,6 @@ func TestEmptyVAppInventoryListObjects(t *testing.T) {
292291
func TestSessionReestablish(t *testing.T) {
293292
simulator.Test(func(ctx context.Context, c *vim25.Client) {
294293
sm := session.NewManager(c)
295-
moClient := &govmomi.Client{
296-
Client: c,
297-
SessionManager: sm,
298-
}
299294
pw, _ := simulator.DefaultLogin.Password()
300295
client := vcenterClient{
301296
vimDriver: c,
@@ -307,19 +302,19 @@ func TestSessionReestablish(t *testing.T) {
307302
Insecure: true,
308303
},
309304
},
310-
moClient: moClient,
305+
sessionManager: sm,
311306
}
312307
err := sm.Logout(ctx)
313308
require.NoError(t, err)
314309

315-
connected, err := client.moClient.SessionManager.SessionIsActive(ctx)
310+
connected, err := client.sessionManager.SessionIsActive(ctx)
316311
require.NoError(t, err)
317312
require.False(t, connected)
318313

319314
err = client.EnsureConnection(ctx)
320315
require.NoError(t, err)
321316

322-
connected, err = client.moClient.SessionManager.SessionIsActive(ctx)
317+
connected, err = client.sessionManager.SessionIsActive(ctx)
323318
require.NoError(t, err)
324319
require.True(t, connected)
325320
})

receiver/vcenterreceiver/integration_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"time"
1313

1414
"github.com/stretchr/testify/require"
15-
"github.com/vmware/govmomi"
1615
"github.com/vmware/govmomi/find"
1716
"github.com/vmware/govmomi/session"
1817
"github.com/vmware/govmomi/simulator"
@@ -80,12 +79,10 @@ func TestIntegration(t *testing.T) {
8079
s := session.NewManager(c)
8180
newVcenterClient = func(l *zap.Logger, cfg *Config) *vcenterClient {
8281
client := &vcenterClient{
83-
logger: l,
84-
cfg: cfg,
85-
moClient: &govmomi.Client{
86-
Client: c,
87-
SessionManager: s,
88-
},
82+
logger: l,
83+
cfg: cfg,
84+
sessionManager: s,
85+
vimDriver: c,
8986
}
9087
require.NoError(t, client.EnsureConnection(context.Background()))
9188
client.vimDriver = c

0 commit comments

Comments
 (0)