-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial ephemeral poc #11824
Draft
BBBmau
wants to merge
7
commits into
GoogleCloudPlatform:FEATURE-BRANCH-ephemeral-resource
Choose a base branch
from
BBBmau:ephemeral-poc
base: FEATURE-BRANCH-ephemeral-resource
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
initial ephemeral poc #11824
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fde3289
initial ephemeral resources directory setup
BBBmau b3a55fd
add ephemeral_resources in framework_provider.go
BBBmau ae149ff
accessToken instead of just Token
BBBmau 48c80f2
initial schema for access_token ephemeral
BBBmau 44839ef
WIP: service_account_token
BBBmau 7e60e14
add interface for serviceAccountTokenEphemeralResource
BBBmau 089e0db
implement EphemeralConfigure
BBBmau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
mmv1/third_party/terraform/ephemeral/ephemeral_service_account_access_token.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,153 @@ | ||||
// Copyright (c) HashiCorp, Inc. | ||||
// SPDX-License-Identifier: MPL-2.0 | ||||
// Copyright (c) HashiCorp, Inc. | ||||
// SPDX-License-Identifier: MPL-2.0 | ||||
package ephemeral | ||||
|
||||
import ( | ||||
"context" | ||||
"fmt" | ||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||||
"github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||||
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||||
"github.com/hashicorp/terraform-plugin-framework/types" | ||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||||
"github.com/hashicorp/terraform-provider-google/google/fwtransport" | ||||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||||
"google.golang.org/api/iamcredentials/v1" | ||||
) | ||||
|
||||
var ( | ||||
_ ephemeral.EphemeralResourceWithConfigure = &googleEphemeralServiceAccountAccessToken{} | ||||
_ ephemeral.EphemeralResource = &googleEphemeralServiceAccountAccessToken{} | ||||
) | ||||
|
||||
func GoogleEphemeralServiceAccountAccessToken() ephemeral.EphemeralResource { | ||||
return &googleEphemeralServiceAccountAccessToken{} | ||||
} | ||||
|
||||
type googleEphemeralServiceAccountAccessToken struct { | ||||
providerConfig *fwtransport.FrameworkProviderConfig | ||||
} | ||||
|
||||
func (p *googleEphemeralServiceAccountAccessToken) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||||
resp.TypeName = req.ProviderTypeName + "_service_account_access_access_token" | ||||
} | ||||
|
||||
func (p *googleEphemeralServiceAccountAccessToken) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { | ||||
// Prevent panic if the provider has not been configured. | ||||
if req.ProviderData == nil { | ||||
return | ||||
} | ||||
|
||||
data, ok := req.ProviderData.(*fwtransport.FrameworkProviderConfig) | ||||
if !ok { | ||||
resp.Diagnostics.AddError( | ||||
"Unexpected Data Source Configure Type", | ||||
fmt.Sprintf("Expected *fwtransport.FrameworkProviderConfig, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||||
) | ||||
return | ||||
} | ||||
|
||||
// Required for accessing project, region, zone and tokenSource | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
|
||||
p.providerConfig = data | ||||
} | ||||
|
||||
type ephemeralServiceAccountAccessTokenModel struct { | ||||
TargetServiceAccount types.Int64 `tfsdk:"target_service_account"` | ||||
AccessToken types.String `tfsdk:"access_token"` | ||||
Scopes types.Set `tfsdk:"scopes"` | ||||
Delegates types.Set `tfsdk:"delegates"` | ||||
Lifetime types.String `tfsdk:"lifetime"` | ||||
} | ||||
|
||||
func (p *googleEphemeralServiceAccountAccessToken) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||||
resp.Schema = schema.Schema{ | ||||
Attributes: map[string]schema.Attribute{ | ||||
"target_service_account": schema.StringAttribute{ | ||||
Required: true, | ||||
// Validators: verify.ValidateRegexp("(" + strings.Join(verify.PossibleServiceAccountNames, "|") + ")"), | ||||
}, | ||||
"access_token": schema.StringAttribute{ | ||||
Sensitive: true, | ||||
Computed: true, | ||||
}, | ||||
"lifetime": schema.StringAttribute{ | ||||
Optional: true, | ||||
// Validators: verify.ValidateDuration(), // duration <=3600s; TODO: support validateDuration(min,max) | ||||
// Default: "3600s", | ||||
}, | ||||
"scopes": schema.SetAttribute{ | ||||
Required: true, | ||||
ElementType: types.StringType, | ||||
// ValidateFunc is not yet supported on lists or sets. | ||||
}, | ||||
"delegates": schema.SetAttribute{ | ||||
Optional: true, | ||||
ElementType: types.StringType, | ||||
}, | ||||
}, | ||||
} | ||||
} | ||||
|
||||
func (p *googleEphemeralServiceAccountAccessToken) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||||
config := p.providerConfig | ||||
// userAgent, err := GenerateUserAgentString(config) | ||||
// if err != nil { | ||||
// resp.Diagnostics.Append(diag.NewErrorDiagnostic("Couldnt generate User Agent String", err.Error())) | ||||
// return | ||||
// } | ||||
|
||||
service := config.NewIamCredentialsClient(config.UserAgent) | ||||
var data ephemeralServiceAccountAccessTokenModel | ||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||||
|
||||
if data.Lifetime.IsNull() { | ||||
data.Lifetime = types.StringValue("3600s") | ||||
} | ||||
|
||||
name := fmt.Sprintf("projects/-/serviceAccounts/%s", data.TargetServiceAccount.String()) | ||||
DelegatesSetValue, _ := data.Delegates.ToSetValue(ctx) | ||||
ScopesSetValue, _ := data.Scopes.ToSetValue(ctx) | ||||
tokenRequest := &iamcredentials.GenerateAccessTokenRequest{ | ||||
Lifetime: data.Lifetime.String(), | ||||
Delegates: StringSet(DelegatesSetValue), | ||||
Scope: tpgresource.CanonicalizeServiceScopes(StringSet(ScopesSetValue)), | ||||
} | ||||
|
||||
at, err := service.Projects.ServiceAccounts.GenerateAccessToken(name, tokenRequest).Do() | ||||
if err != nil { | ||||
resp.Diagnostics.Append(diag.NewErrorDiagnostic("Couldnt generate token", err.Error())) | ||||
return | ||||
} | ||||
data.AccessToken = basetypes.NewStringValue(at.AccessToken) | ||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||||
} | ||||
|
||||
func StringSet(d basetypes.SetValue) []string { | ||||
|
||||
StringSlice := make([]string, 0) | ||||
for _, v := range d.Elements() { | ||||
StringSlice = append(StringSlice, v.String()) | ||||
} | ||||
return StringSlice | ||||
} | ||||
|
||||
// TODO: investigation will be needed to determine how to best approach generating | ||||
// a userAgentString with module name | ||||
// func GenerateUserAgentString(currentUserAgent string) (string, error) { | ||||
// var m transport_tpg.ProviderMeta | ||||
|
||||
// err := d.GetProviderMeta(&m) | ||||
// if err != nil { | ||||
// return currentUserAgent, err | ||||
// } | ||||
|
||||
// if m.ModuleName != "" { | ||||
// return strings.Join([]string{currentUserAgent, m.ModuleName}, " "), nil | ||||
// } | ||||
|
||||
// return currentUserAgent, nil | ||||
// } |
61 changes: 61 additions & 0 deletions
61
mmv1/third_party/terraform/ephemeral/ephemeral_service_account_id_token.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ephemeral | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func GoogleEphemeralServiceAccountIdToken() ephemeral.EphemeralResource { | ||
return &googleEphemeralServiceAccountIdToken{} | ||
} | ||
|
||
type googleEphemeralServiceAccountIdToken struct{} | ||
|
||
func (p *googleEphemeralServiceAccountIdToken) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_service_account_access_id_token" | ||
} | ||
|
||
type ephemeralServiceAccountIdTokenModel struct { | ||
Length types.Int64 `tfsdk:"length"` | ||
Result types.String `tfsdk:"result"` | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountIdToken) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Generates a test string", | ||
Attributes: map[string]schema.Attribute{ | ||
"length": schema.Int64Attribute{ | ||
Description: "The amount of test in string desired. The minimum value for length is 1.", | ||
Required: true, | ||
Validators: []validator.Int64{ | ||
int64validator.AtLeast(1), | ||
}, | ||
}, | ||
"result": schema.StringAttribute{ | ||
Description: "The generated test string.", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountIdToken) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||
var data ephemeralServiceAccountIdTokenModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
result := "" | ||
for range data.Length.ValueInt64() { | ||
result += "test" | ||
} | ||
|
||
data.Result = types.StringValue(string(result)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
61 changes: 61 additions & 0 deletions
61
mmv1/third_party/terraform/ephemeral/ephemeral_service_account_jwt.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ephemeral | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func GoogleEphemeralServiceAccountJwt() ephemeral.EphemeralResource { | ||
return &googleEphemeralServiceAccountJwt{} | ||
} | ||
|
||
type googleEphemeralServiceAccountJwt struct{} | ||
|
||
func (p *googleEphemeralServiceAccountJwt) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_service_account_access_jwt" | ||
} | ||
|
||
type ephemeralServiceAccountJwtModel struct { | ||
Length types.Int64 `tfsdk:"length"` | ||
Result types.String `tfsdk:"result"` | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountJwt) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Generates a test string", | ||
Attributes: map[string]schema.Attribute{ | ||
"length": schema.Int64Attribute{ | ||
Description: "The amount of test in string desired. The minimum value for length is 1.", | ||
Required: true, | ||
Validators: []validator.Int64{ | ||
int64validator.AtLeast(1), | ||
}, | ||
}, | ||
"result": schema.StringAttribute{ | ||
Description: "The generated test string.", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountJwt) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||
var data ephemeralServiceAccountJwtModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
result := "" | ||
for range data.Length.ValueInt64() { | ||
result += "test" | ||
} | ||
|
||
data.Result = types.StringValue(string(result)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
61 changes: 61 additions & 0 deletions
61
mmv1/third_party/terraform/ephemeral/ephemeral_service_account_key.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ephemeral | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func GoogleEphemeralServiceAccountKey() ephemeral.EphemeralResource { | ||
return &googleEphemeralServiceAccountKey{} | ||
} | ||
|
||
type googleEphemeralServiceAccountKey struct{} | ||
|
||
func (p *googleEphemeralServiceAccountKey) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_service_account_access_key" | ||
} | ||
|
||
type ephemeralServiceAccountKeyModel struct { | ||
Length types.Int64 `tfsdk:"length"` | ||
Result types.String `tfsdk:"result"` | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountKey) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Generates a test string", | ||
Attributes: map[string]schema.Attribute{ | ||
"length": schema.Int64Attribute{ | ||
Description: "The amount of test in string desired. The minimum value for length is 1.", | ||
Required: true, | ||
Validators: []validator.Int64{ | ||
int64validator.AtLeast(1), | ||
}, | ||
}, | ||
"result": schema.StringAttribute{ | ||
Description: "The generated test string.", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *googleEphemeralServiceAccountKey) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||
var data ephemeralServiceAccountKeyModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
result := "" | ||
for range data.Length.ValueInt64() { | ||
result += "test" | ||
} | ||
|
||
data.Result = types.StringValue(string(result)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.