Skip to content

Commit e98625f

Browse files
authored
[ACTP] add mongodb test connection action (#54583)
<!--Please give us some feedback on your experience writing this PR ! https://app.datadoghq.com/forms/43db4c02-6837-400c-8083-692e141b1b88 !--> ### What does this PR do? Adds the mongodb "test connection" action ### Motivation Allow to verify if the PAR has connectivity for executing mongodb action ### Describe how you validated your changes Ran the action from staging ### Additional Notes Co-authored-by: gabriel.plassard <gabriel.plassard@datadoghq.com>
1 parent b8c44b3 commit e98625f

6 files changed

Lines changed: 99 additions & 0 deletions

File tree

pkg/privateactionrunner/adapters/config/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var DefaultActionFQNs = append([]string{}, defaultCommonActionFQNs...)
9191
var BundleInheritedAllowedActions = []BundleInheritedAllowedAction{
9292
{ActionFQN: "com.datadoghq.gitlab.users.testConnection", ExpectedPrefix: "com.datadoghq.gitlab"},
9393
{ActionFQN: "com.datadoghq.kubernetes.core.testConnection", ExpectedPrefix: "com.datadoghq.kubernetes"},
94+
{ActionFQN: "com.datadoghq.mongodb.testConnection", ExpectedPrefix: "com.datadoghq.mongodb"},
9495
{ActionFQN: "com.datadoghq.script.testConnection", ExpectedPrefix: "com.datadoghq.script"},
9596
{ActionFQN: "com.datadoghq.script.enrichScript", ExpectedPrefix: "com.datadoghq.script"},
9697
{ActionFQN: "com.datadoghq.http.testConnection", ExpectedPrefix: "com.datadoghq.http"},

pkg/privateactionrunner/adapters/config/transform_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ func TestGetBundleInheritedAllowedActions(t *testing.T) {
7777
"com.datadoghq.kubernetes.core": sets.New[string]("action3"),
7878
"com.datadoghq.kubernetes.apps": sets.New[string]("action4"),
7979
"com.datadoghq.remoteaction": sets.New[string]("action5"),
80+
"com.datadoghq.mongodb": sets.New[string]("action6"),
8081
},
8182
expectedInheritedActions: map[string]sets.Set[string]{
8283
"com.datadoghq.script": sets.New[string]("testConnection", "enrichScript"),
8384
"com.datadoghq.gitlab.users": sets.New[string]("testConnection"),
8485
"com.datadoghq.kubernetes.core": sets.New[string]("testConnection"),
86+
"com.datadoghq.mongodb": sets.New[string]("testConnection"),
8587
"com.datadoghq.remoteaction": sets.New[string]("testConnection"),
8688
"com.datadoghq.remoteaction.internal": sets.New[string]("prepareEncryption"),
8789
},

pkg/privateactionrunner/bundles/mongodb/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
"insert_many.go",
1414
"insert_one.go",
1515
"replace_one.go",
16+
"test_connection.go",
1617
"update_many.go",
1718
"update_one.go",
1819
],

pkg/privateactionrunner/bundles/mongodb/entrypoint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func NewMongoDB() types.Bundle {
2525
"createIndex": NewCreateIndexHandler(),
2626
"findAndModify": NewFindAndModifyHandler(),
2727
"insertMany": NewInsertManyHandler(),
28+
"testConnection": NewTestConnectionHandler(),
2829
},
2930
}
3031
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2026-present Datadog, Inc.
5+
6+
package com_datadoghq_mongodb
7+
8+
import (
9+
"context"
10+
"fmt"
11+
12+
"go.mongodb.org/mongo-driver/v2/bson"
13+
"go.mongodb.org/mongo-driver/v2/mongo"
14+
15+
log "github.com/DataDog/datadog-agent/pkg/privateactionrunner/adapters/logging"
16+
"github.com/DataDog/datadog-agent/pkg/privateactionrunner/libs/privateconnection"
17+
"github.com/DataDog/datadog-agent/pkg/privateactionrunner/types"
18+
)
19+
20+
type TestConnectionHandler struct{}
21+
22+
func NewTestConnectionHandler() *TestConnectionHandler {
23+
return &TestConnectionHandler{}
24+
}
25+
26+
type TestConnectionInputs struct{}
27+
28+
type ServerInfo struct {
29+
Version string `json:"version"`
30+
}
31+
32+
type TestConnectionOutputs struct {
33+
ConfigurationValid bool `json:"configurationValid"`
34+
ConnectionValid bool `json:"connectionValid"`
35+
ServerInfo *ServerInfo `json:"serverInfo,omitempty"`
36+
Errors []string `json:"errors"`
37+
}
38+
39+
func (h *TestConnectionHandler) Run(ctx context.Context, task *types.Task, credential *privateconnection.PrivateCredentials) (interface{}, error) {
40+
credentialTokens := credential.AsTokenMap()
41+
42+
clientOptions, _, err := createMongoClientOptions(ctx, credentialTokens)
43+
if err != nil {
44+
return &TestConnectionOutputs{
45+
ConfigurationValid: false,
46+
ConnectionValid: false,
47+
Errors: []string{err.Error()},
48+
}, nil
49+
}
50+
51+
client, err := mongo.Connect(clientOptions)
52+
if err != nil {
53+
return &TestConnectionOutputs{
54+
ConfigurationValid: true,
55+
ConnectionValid: false,
56+
Errors: []string{fmt.Sprintf("Unable to connect to MongoDB: %v", err)},
57+
}, nil
58+
}
59+
defer func() {
60+
if err := client.Disconnect(ctx); err != nil {
61+
log.FromContext(ctx).Error("Error disconnecting from MongoDB", log.ErrorField(err))
62+
}
63+
}()
64+
65+
if err := client.Ping(ctx, nil); err != nil {
66+
return &TestConnectionOutputs{
67+
ConfigurationValid: true,
68+
ConnectionValid: false,
69+
Errors: []string{fmt.Sprintf("Failed to ping MongoDB server: %v", err)},
70+
}, nil
71+
}
72+
73+
var errors []string
74+
var serverInfo *ServerInfo
75+
var buildInfo struct {
76+
Version string `bson:"version"`
77+
}
78+
if err := client.Database("admin").RunCommand(ctx, bson.D{{Key: "buildInfo", Value: 1}}).Decode(&buildInfo); err != nil {
79+
errors = append(errors, fmt.Sprintf("Warning: Failed to get server version: %v", err))
80+
} else {
81+
serverInfo = &ServerInfo{Version: buildInfo.Version}
82+
}
83+
84+
return &TestConnectionOutputs{
85+
ConfigurationValid: true,
86+
ConnectionValid: true,
87+
ServerInfo: serverInfo,
88+
Errors: errors,
89+
}, nil
90+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
enhancements:
3+
- |
4+
The Private Action Runner can now validate MongoDB connections

0 commit comments

Comments
 (0)