Skip to content

Commit 24a610e

Browse files
committed
Merge branch 'master' into CLOUDP-334161-service-accounts-dev
* master: chore: Remove all attributes in assume_role except role_arn (#3745) # Conflicts: # internal/config/client.go
2 parents 394b811 + cc34ae1 commit 24a610e

File tree

10 files changed

+23
-261
lines changed

10 files changed

+23
-261
lines changed

internal/config/client.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ import (
1717
matlasClient "go.mongodb.org/atlas/mongodbatlas"
1818
realmAuth "go.mongodb.org/realm/auth"
1919
"go.mongodb.org/realm/realm"
20+
"golang.org/x/oauth2"
2021

2122
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
2223
"github.com/mongodb-forks/digest"
2324
adminpreview "github.com/mongodb/atlas-sdk-go/admin"
2425

2526
"github.com/mongodb/terraform-provider-mongodbatlas/version"
26-
27-
"golang.org/x/oauth2"
2827
)
2928

3029
const (
@@ -113,7 +112,7 @@ type MongoDBClient struct {
113112

114113
// Config contains the configurations needed to use SDKs
115114
type Config struct {
116-
AssumeRole *AssumeRole
115+
AssumeRoleARN string
117116
PublicKey string
118117
PrivateKey string
119118
BaseURL string
@@ -131,18 +130,6 @@ func (c *Config) GetClientID() string { return c.ClientID }
131130
func (c *Config) GetClientSecret() string { return c.ClientSecret }
132131
func (c *Config) GetAccessToken() string { return c.AccessToken }
133132

134-
type AssumeRole struct {
135-
Tags map[string]string
136-
RoleARN string
137-
ExternalID string
138-
Policy string
139-
SessionName string
140-
SourceIdentity string
141-
PolicyARNs []string
142-
TransitiveTagKeys []string
143-
Duration time.Duration
144-
}
145-
146133
type SecretData struct {
147134
PublicKey string `json:"public_key"`
148135
PrivateKey string `json:"private_key"`
@@ -153,7 +140,7 @@ type UAMetadata struct {
153140
Value string
154141
}
155142

156-
func (c *Config) NewClient(ctx context.Context) (any, error) {
143+
func (c *Config) NewClient(ctx context.Context) (*MongoDBClient, error) {
157144
transport := networkLoggingBaseTransport()
158145
switch ResolveAuthMethod(c) {
159146
case AccessToken:

internal/config/transport_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ func TestAccNetworkLogging(t *testing.T) {
166166
ClientSecret: os.Getenv("MONGODB_ATLAS_CLIENT_SECRET"),
167167
BaseURL: os.Getenv("MONGODB_ATLAS_BASE_URL"),
168168
}
169-
clientInterface, err := cfg.NewClient(t.Context())
169+
client, err := cfg.NewClient(t.Context())
170170
require.NoError(t, err)
171-
client, ok := clientInterface.(*config.MongoDBClient)
172-
require.True(t, ok)
173171

174172
// Make a simple API call that should trigger our enhanced logging
175173
_, _, err = client.AtlasV2.OrganizationsApi.ListOrgs(t.Context()).Execute()

internal/provider/credentials.go renamed to internal/provider/aws_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func configureCredentialsSTS(cfg *config.Config, secret, region, awsAccessKeyID,
4444
EndpointResolver: endpoints.ResolverFunc(stsCustResolverFn),
4545
}))
4646

47-
creds := stscreds.NewCredentials(sess, cfg.AssumeRole.RoleARN)
47+
creds := stscreds.NewCredentials(sess, cfg.AssumeRoleARN)
4848

4949
_, err := sess.Config.Credentials.Get()
5050
if err != nil {
File renamed without changes.

internal/provider/provider.go

Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import (
44
"context"
55
"log"
66
"os"
7-
"regexp"
8-
"time"
97

108
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
11-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
129
"github.com/hashicorp/terraform-plugin-framework/attr"
1310
"github.com/hashicorp/terraform-plugin-framework/datasource"
1411
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -24,7 +21,6 @@ import (
2421
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
2522

2623
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
27-
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
2824
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
2925
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
3026
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration"
@@ -92,27 +88,11 @@ type tfMongodbAtlasProviderModel struct {
9288
}
9389

9490
type tfAssumeRoleModel struct {
95-
PolicyARNs types.Set `tfsdk:"policy_arns"`
96-
TransitiveTagKeys types.Set `tfsdk:"transitive_tag_keys"`
97-
Tags types.Map `tfsdk:"tags"`
98-
Duration types.String `tfsdk:"duration"`
99-
ExternalID types.String `tfsdk:"external_id"`
100-
Policy types.String `tfsdk:"policy"`
101-
RoleARN types.String `tfsdk:"role_arn"`
102-
SessionName types.String `tfsdk:"session_name"`
103-
SourceIdentity types.String `tfsdk:"source_identity"`
91+
RoleARN types.String `tfsdk:"role_arn"`
10492
}
10593

10694
var AssumeRoleType = types.ObjectType{AttrTypes: map[string]attr.Type{
107-
"policy_arns": types.SetType{ElemType: types.StringType},
108-
"transitive_tag_keys": types.SetType{ElemType: types.StringType},
109-
"tags": types.MapType{ElemType: types.StringType},
110-
"duration": types.StringType,
111-
"external_id": types.StringType,
112-
"policy": types.StringType,
113-
"role_arn": types.StringType,
114-
"session_name": types.StringType,
115-
"source_identity": types.StringType,
95+
"role_arn": types.StringType,
11696
}}
11797

11898
func (p *MongodbtlasProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
@@ -211,63 +191,10 @@ var fwAssumeRoleSchema = schema.ListNestedBlock{
211191
Validators: []validator.List{listvalidator.SizeAtMost(1)},
212192
NestedObject: schema.NestedBlockObject{
213193
Attributes: map[string]schema.Attribute{
214-
"duration": schema.StringAttribute{
215-
Optional: true,
216-
Description: "The duration, between 15 minutes and 12 hours, of the role session. Valid time units are ns, us (or µs), ms, s, h, or m.",
217-
Validators: []validator.String{
218-
validate.ValidDurationBetween(15, 12*60),
219-
},
220-
},
221-
"external_id": schema.StringAttribute{
222-
Optional: true,
223-
Description: "A unique identifier that might be required when you assume a role in another account.",
224-
Validators: []validator.String{
225-
stringvalidator.LengthBetween(2, 1224),
226-
stringvalidator.RegexMatches(regexp.MustCompile(`[\w+=,.@:/\-]*`), ""),
227-
},
228-
},
229-
"policy": schema.StringAttribute{
230-
Optional: true,
231-
Description: "IAM Policy JSON describing further restricting permissions for the IAM Role being assumed.",
232-
Validators: []validator.String{
233-
validate.StringIsJSON(),
234-
},
235-
},
236-
"policy_arns": schema.SetAttribute{
237-
ElementType: types.StringType,
238-
Optional: true,
239-
Description: "Amazon Resource Names (ARNs) of IAM Policies describing further restricting permissions for the IAM Role being assumed.",
240-
},
241194
"role_arn": schema.StringAttribute{
242195
Optional: true,
243196
Description: "Amazon Resource Name (ARN) of an IAM Role to assume prior to making API calls.",
244197
},
245-
"session_name": schema.StringAttribute{
246-
Optional: true,
247-
Description: "An identifier for the assumed role session.",
248-
Validators: []validator.String{
249-
stringvalidator.LengthBetween(2, 64),
250-
stringvalidator.RegexMatches(regexp.MustCompile(`[\w+=,.@\-]*`), ""),
251-
},
252-
},
253-
"source_identity": schema.StringAttribute{
254-
Optional: true,
255-
Description: "Source identity specified by the principal assuming the role.",
256-
Validators: []validator.String{
257-
stringvalidator.LengthBetween(2, 64),
258-
stringvalidator.RegexMatches(regexp.MustCompile(`[\w+=,.@\-]*`), ""),
259-
},
260-
},
261-
"tags": schema.MapAttribute{
262-
ElementType: types.StringType,
263-
Optional: true,
264-
Description: "Assume role session tags.",
265-
},
266-
"transitive_tag_keys": schema.SetAttribute{
267-
ElementType: types.StringType,
268-
Optional: true,
269-
Description: "Assume role session tag keys to pass to any subsequent sessions.",
270-
},
271198
},
272199
},
273200
}
@@ -300,7 +227,7 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
300227
data.AssumeRole.ElementsAs(ctx, &assumeRoles, true)
301228
awsRoleDefined := len(assumeRoles) > 0
302229
if awsRoleDefined {
303-
cfg.AssumeRole = parseTfModel(ctx, &assumeRoles[0])
230+
cfg.AssumeRoleARN = assumeRoles[0].RoleARN.ValueString()
304231
secret := data.SecretName.ValueString()
305232
region := conversion.MongoDBRegionToAWSRegion(data.Region.ValueString())
306233
awsAccessKeyID := data.AwsAccessKeyID.ValueString()
@@ -329,37 +256,6 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
329256
resp.ResourceData = client
330257
}
331258

332-
// parseTfModel extracts the values from tfAssumeRoleModel creating a new instance of our internal model AssumeRole used in Config
333-
func parseTfModel(ctx context.Context, tfAssumeRoleModel *tfAssumeRoleModel) *config.AssumeRole {
334-
assumeRole := config.AssumeRole{}
335-
336-
if !tfAssumeRoleModel.Duration.IsNull() {
337-
duration, _ := time.ParseDuration(tfAssumeRoleModel.Duration.ValueString())
338-
assumeRole.Duration = duration
339-
}
340-
341-
assumeRole.ExternalID = tfAssumeRoleModel.ExternalID.ValueString()
342-
assumeRole.Policy = tfAssumeRoleModel.Policy.ValueString()
343-
344-
if !tfAssumeRoleModel.PolicyARNs.IsNull() {
345-
var policiesARNs []string
346-
tfAssumeRoleModel.PolicyARNs.ElementsAs(ctx, &policiesARNs, true)
347-
assumeRole.PolicyARNs = policiesARNs
348-
}
349-
350-
assumeRole.RoleARN = tfAssumeRoleModel.RoleARN.ValueString()
351-
assumeRole.SessionName = tfAssumeRoleModel.SessionName.ValueString()
352-
assumeRole.SourceIdentity = tfAssumeRoleModel.SourceIdentity.ValueString()
353-
354-
if !tfAssumeRoleModel.TransitiveTagKeys.IsNull() {
355-
var transitiveTagKeys []string
356-
tfAssumeRoleModel.TransitiveTagKeys.ElementsAs(ctx, &transitiveTagKeys, true)
357-
assumeRole.TransitiveTagKeys = transitiveTagKeys
358-
}
359-
360-
return &assumeRole
361-
}
362-
363259
func setDefaultValuesWithValidations(ctx context.Context, data *tfMongodbAtlasProviderModel, resp *provider.ConfigureResponse) tfMongodbAtlasProviderModel {
364260
if mongodbgovCloud := data.IsMongodbGovCloud.ValueBool(); mongodbgovCloud {
365261
if !isGovBaseURLConfiguredForProvider(data) {
@@ -384,10 +280,7 @@ func setDefaultValuesWithValidations(ctx context.Context, data *tfMongodbAtlasPr
384280
var diags diag.Diagnostics
385281
data.AssumeRole, diags = types.ListValueFrom(ctx, AssumeRoleType, []tfAssumeRoleModel{
386282
{
387-
Tags: types.MapNull(types.StringType),
388-
PolicyARNs: types.SetNull(types.StringType),
389-
TransitiveTagKeys: types.SetNull(types.StringType),
390-
RoleARN: types.StringValue(assumeRoleArn),
283+
RoleARN: types.StringValue(assumeRoleArn),
391284
},
392285
})
393286
if diags.HasError() {

internal/provider/provider_authentication_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ func TestAccAccessToken_basic(t *testing.T) {
8585
}
8686

8787
func configProject(orgID, projectName string) string {
88+
// Use project in TPF and organization in SDKv2 so both providers are tested.
8889
return fmt.Sprintf(`
8990
resource "mongodbatlas_project" "test" {
9091
org_id = %[1]q
9192
name = %[2]q
9293
}
94+
data "mongodbatlas_organization" "test" {
95+
org_id = %[1]q
96+
}
9397
`, orgID, projectName)
9498
}
9599

0 commit comments

Comments
 (0)