Skip to content

Commit cf5c810

Browse files
authored
fix: Realm Client Auth Token Overwritten by Transport Assignment (#3362)
* don't use custom transport for realm * changelog entry * changelog * use baseTransport instead of default * add basic test that expects 404 and makes sure realm client works * correct projectID * add realm url in GHA
1 parent 7e9f52f commit cf5c810

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

.changelog/3362.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
provider: Fixes Realm Client authentication
3+
```

.github/workflows/acceptance-tests-runner.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ on:
3737
mongodb_atlas_base_url:
3838
type: string
3939
required: true
40+
mongodb_realm_base_url:
41+
type: string
42+
required: true
4043
mongodb_atlas_project_owner_id:
4144
type: string
4245
required: true
@@ -199,6 +202,7 @@ env:
199202
# If the name (regex) of the test is set, only that test is run
200203
ACCTEST_REGEX_RUN: ${{ inputs.test_name || inputs.provider_version == '' && '^Test(Acc|Mig)' || '^TestMig' }}
201204
MONGODB_ATLAS_BASE_URL: ${{ inputs.mongodb_atlas_base_url }}
205+
MONGODB_REALM_BASE_URL: ${{ inputs.mongodb_realm_base_url }}
202206
MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }}
203207
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }}
204208
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }}

.github/workflows/acceptance-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ jobs:
106106
aws_region_federation: ${{ vars.AWS_REGION_FEDERATION }}
107107
mongodb_atlas_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_ORG_ID_CLOUD_QA || vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
108108
mongodb_atlas_base_url: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_BASE_URL_QA || vars.MONGODB_ATLAS_BASE_URL }}
109+
mongodb_realm_base_url: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_REALM_BASE_URL_QA || vars.MONGODB_REALM_BASE_URL }}
109110
mongodb_atlas_project_owner_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_PROJECT_OWNER_ID_QA || vars.MONGODB_ATLAS_PROJECT_OWNER_ID }}
110111
mongodb_atlas_teams_ids: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_TEAMS_IDS_QA || vars.MONGODB_ATLAS_TEAMS_IDS }}
111112
azure_atlas_app_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.AZURE_ATLAS_APP_ID_QA || vars.AZURE_ATLAS_APP_ID }}

internal/config/client.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,12 @@ func (c *MongoDBClient) GetRealmClient(ctx context.Context) (*realm.Client, erro
243243
return nil, err
244244
}
245245

246-
clientRealm := realmAuth.NewClient(realmAuth.BasicTokenSource(token))
247-
248-
clientRealm.Transport = baseTransport
249-
clientRealm.Transport = logging.NewTransport("MongoDB Realm", clientRealm.Transport)
246+
clientRealm := &http.Client{
247+
Transport: &realmAuth.Transport{
248+
Source: realmAuth.BasicTokenSource(token),
249+
Base: logging.NewTransport("MongoDB Realm", baseTransport),
250+
},
251+
}
250252

251253
// Initialize the MongoDB Realm API Client.
252254
realmClient, err := realm.New(clientRealm, optsRealm...)

internal/service/eventtrigger/data_source_event_triggers_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package eventtrigger_test
33
import (
44
"fmt"
55
"os"
6+
"regexp"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -51,6 +52,23 @@ func TestAccEventTriggerDSPlural_basic(t *testing.T) {
5152
})
5253
}
5354

55+
func TestAccEventTriggerDSPlural_realmClientWorks(t *testing.T) {
56+
// This test is designed to run in CI and expects a 404 "app not found" error from the Realm API.
57+
projectID := acc.ProjectIDExecution(t)
58+
appID := "invalid-app-id" // known-bad app ID
59+
60+
resource.ParallelTest(t, resource.TestCase{
61+
PreCheck: func() { acc.PreCheckBasic(t) },
62+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
63+
Steps: []resource.TestStep{
64+
{
65+
Config: testAccMongoDBAtlasEventTriggers404Config(projectID, appID),
66+
ExpectError: regexp.MustCompile("app not found"),
67+
},
68+
},
69+
})
70+
}
71+
5472
func testAccMongoDBAtlasEventTriggersDataSourceConfig(projectID, appID, operationTypes string, eventTrigger *realm.EventTriggerRequest) string {
5573
return fmt.Sprintf(`
5674
resource "mongodbatlas_event_trigger" "test" {
@@ -76,3 +94,12 @@ func testAccMongoDBAtlasEventTriggersDataSourceConfig(projectID, appID, operatio
7694
eventTrigger.Config.Database, eventTrigger.Config.Collection,
7795
eventTrigger.Config.ServiceID)
7896
}
97+
98+
func testAccMongoDBAtlasEventTriggers404Config(projectID, appID string) string {
99+
return fmt.Sprintf(`
100+
data "mongodbatlas_event_triggers" "this" {
101+
project_id = %q
102+
app_id = %q
103+
}
104+
`, projectID, appID)
105+
}

0 commit comments

Comments
 (0)