-
Notifications
You must be signed in to change notification settings - Fork 412
[datadog_api_key] Implement ephemeral datadog_api_key resource #3227
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
Open
LiuVII
wants to merge
3
commits into
master
Choose a base branch
from
mf/ephemerals-poc
base: master
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
137 changes: 137 additions & 0 deletions
137
datadog/fwprovider/ephemeral_resource_datadog_api_key.go
This file contains hidden or 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,137 @@ | ||
| package fwprovider | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" | ||
| "github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||
| "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| ) | ||
|
|
||
| // Interface assertions for EphemeralAPIKeyResource | ||
| var ( | ||
| _ ephemeral.EphemeralResource = &EphemeralAPIKeyResource{} | ||
| _ ephemeral.EphemeralResourceWithConfigure = &EphemeralAPIKeyResource{} | ||
| ) | ||
|
|
||
| // EphemeralAPIKeyResource implements ephemeral API key resource | ||
| type EphemeralAPIKeyResource struct { | ||
| Api *datadogV2.KeyManagementApi | ||
| Auth context.Context | ||
| } | ||
|
|
||
| // EphemeralAPIKeyModel represents the data model for the ephemeral API key resource | ||
| type EphemeralAPIKeyModel struct { | ||
| ID types.String `tfsdk:"id"` | ||
| Name types.String `tfsdk:"name"` | ||
| Key types.String `tfsdk:"key"` | ||
| RemoteConfigReadEnabled types.Bool `tfsdk:"remote_config_read_enabled"` | ||
| } | ||
|
|
||
| // NewEphemeralAPIKeyResource creates a new ephemeral API key resource | ||
| func NewEphemeralAPIKeyResource() ephemeral.EphemeralResource { | ||
| return &EphemeralAPIKeyResource{} | ||
| } | ||
|
|
||
| // Metadata implements the core ephemeral.EphemeralResource interface | ||
| func (r *EphemeralAPIKeyResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||
| resp.TypeName = "api_key" // Will become "datadog_api_key" via wrapper | ||
| } | ||
|
|
||
| // Schema implements the core ephemeral.EphemeralResource interface | ||
| func (r *EphemeralAPIKeyResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: "Retrieves an existing Datadog API key as an ephemeral resource. The API key value is retrieved securely and made available for use in other resources without being stored in state.", | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Required: true, | ||
| Description: "The ID of the API key to retrieve.", | ||
| }, | ||
| "name": schema.StringAttribute{ | ||
| Computed: true, | ||
| Description: "The name of the API key.", | ||
| }, | ||
| "key": schema.StringAttribute{ | ||
| Computed: true, | ||
| Sensitive: true, | ||
| Description: "The actual API key value (sensitive).", | ||
| }, | ||
| "remote_config_read_enabled": schema.BoolAttribute{ | ||
| Computed: true, | ||
| Description: "Whether remote configuration reads are enabled for this key.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Open implements the core ephemeral.EphemeralResource interface | ||
| // This is where the ephemeral resource acquires the API key data | ||
| func (r *EphemeralAPIKeyResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||
| // 1. Extract API key ID from config | ||
| var config EphemeralAPIKeyModel | ||
| resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // 2. Fetch API key from Datadog API | ||
| apiKey, httpResp, err := r.Api.GetAPIKey(r.Auth, config.ID.ValueString()) | ||
| if err != nil { | ||
| log.Printf("[ERROR] Ephemeral open operation failed for api_key: %v", err) | ||
| resp.Diagnostics.AddError( | ||
| "API Key Retrieval Failed", | ||
| "Unable to fetch API key data from Datadog API", | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // Check HTTP response status | ||
| if httpResp != nil && httpResp.StatusCode >= 400 { | ||
| log.Printf("[WARN] Ephemeral open operation failed for api_key") | ||
| resp.Diagnostics.AddError( | ||
| "API Key Retrieval Failed", | ||
| "Received error response from Datadog API", | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // 3. Extract API key data from response | ||
| apiKeyData := apiKey.GetData() | ||
| apiKeyAttributes := apiKeyData.GetAttributes() | ||
|
|
||
| // 4. Set result data (including the sensitive key value) | ||
| result := EphemeralAPIKeyModel{ | ||
| ID: config.ID, | ||
| Name: types.StringValue(apiKeyAttributes.GetName()), | ||
| Key: types.StringValue(apiKeyAttributes.GetKey()), // SENSITIVE | ||
| RemoteConfigReadEnabled: types.BoolValue(apiKeyAttributes.GetRemoteConfigReadEnabled()), | ||
| } | ||
|
|
||
| resp.Diagnostics.Append(resp.Result.Set(ctx, &result)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| log.Printf("[DEBUG] Ephemeral open operation succeeded for api_key") | ||
| } | ||
|
|
||
| // Configure implements the optional ephemeral.EphemeralResourceWithConfigure interface | ||
| func (r *EphemeralAPIKeyResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { | ||
| if req.ProviderData == nil { | ||
| return | ||
| } | ||
|
|
||
| providerData, ok := req.ProviderData.(*FrameworkProvider) | ||
| if !ok { | ||
| resp.Diagnostics.AddError( | ||
| "Unexpected Configure Type", | ||
| "Expected *FrameworkProvider", | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| r.Api = providerData.DatadogApiInstances.GetKeyManagementApiV2() | ||
| r.Auth = providerData.Auth | ||
| } | ||
104 changes: 104 additions & 0 deletions
104
datadog/fwprovider/framework_ephemeral_resource_wrapper.go
This file contains hidden or 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,104 @@ | ||
| package fwprovider | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||
|
|
||
| "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/fwutils" | ||
| ) | ||
|
|
||
| // Interface assertions for FrameworkEphemeralResourceWrapper | ||
| var ( | ||
| _ ephemeral.EphemeralResource = &FrameworkEphemeralResourceWrapper{} | ||
| _ ephemeral.EphemeralResourceWithConfigure = &FrameworkEphemeralResourceWrapper{} | ||
| _ ephemeral.EphemeralResourceWithValidateConfig = &FrameworkEphemeralResourceWrapper{} | ||
| _ ephemeral.EphemeralResourceWithConfigValidators = &FrameworkEphemeralResourceWrapper{} | ||
| _ ephemeral.EphemeralResourceWithRenew = &FrameworkEphemeralResourceWrapper{} | ||
| _ ephemeral.EphemeralResourceWithClose = &FrameworkEphemeralResourceWrapper{} | ||
| ) | ||
|
|
||
| // NewFrameworkEphemeralResourceWrapper creates a new ephemeral resource wrapper following | ||
| // the same pattern as the existing FrameworkResourceWrapper | ||
| func NewFrameworkEphemeralResourceWrapper(i *ephemeral.EphemeralResource) ephemeral.EphemeralResource { | ||
| return &FrameworkEphemeralResourceWrapper{ | ||
| innerResource: i, | ||
| } | ||
| } | ||
|
|
||
| // FrameworkEphemeralResourceWrapper wraps ephemeral resources to provide consistent behavior | ||
| // across all ephemeral resources, following the existing FrameworkResourceWrapper pattern | ||
| type FrameworkEphemeralResourceWrapper struct { | ||
| innerResource *ephemeral.EphemeralResource | ||
| } | ||
|
|
||
| // Metadata implements the core ephemeral.EphemeralResource interface | ||
| // Adds provider type name prefix to the resource type name, following existing pattern | ||
| func (r *FrameworkEphemeralResourceWrapper) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { | ||
| (*r.innerResource).Metadata(ctx, req, resp) | ||
| resp.TypeName = req.ProviderTypeName + resp.TypeName | ||
| } | ||
|
|
||
| // Schema implements the core ephemeral.EphemeralResource interface | ||
| // Enriches schema with common framework patterns | ||
| func (r *FrameworkEphemeralResourceWrapper) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||
| (*r.innerResource).Schema(ctx, req, resp) | ||
| fwutils.EnrichFrameworkEphemeralResourceSchema(&resp.Schema) | ||
| } | ||
|
|
||
| // Open implements the core ephemeral.EphemeralResource interface | ||
| // This is where ephemeral resources create/acquire their temporary resources | ||
| func (r *FrameworkEphemeralResourceWrapper) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { | ||
| (*r.innerResource).Open(ctx, req, resp) | ||
| } | ||
|
|
||
| // Configure implements the optional ephemeral.EphemeralResourceWithConfigure interface | ||
| // Uses interface detection to only call if the inner resource supports configuration | ||
| func (r *FrameworkEphemeralResourceWrapper) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { | ||
| rCasted, ok := (*r.innerResource).(ephemeral.EphemeralResourceWithConfigure) | ||
| if ok { | ||
| if req.ProviderData == nil { | ||
| return | ||
| } | ||
| _, ok := req.ProviderData.(*FrameworkProvider) | ||
| if !ok { | ||
| resp.Diagnostics.AddError("Unexpected Ephemeral Resource Configure Type", "") | ||
| return | ||
| } | ||
|
|
||
| rCasted.Configure(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| // ValidateConfig implements the optional ephemeral.EphemeralResourceWithValidateConfig interface | ||
| // Uses interface detection to only call if the inner resource supports validation | ||
| func (r *FrameworkEphemeralResourceWrapper) ValidateConfig(ctx context.Context, req ephemeral.ValidateConfigRequest, resp *ephemeral.ValidateConfigResponse) { | ||
| if rCasted, ok := (*r.innerResource).(ephemeral.EphemeralResourceWithValidateConfig); ok { | ||
| rCasted.ValidateConfig(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| // ConfigValidators implements the optional ephemeral.EphemeralResourceWithConfigValidators interface | ||
| // Uses interface detection to only call if the inner resource supports declarative validators | ||
| func (r *FrameworkEphemeralResourceWrapper) ConfigValidators(ctx context.Context) []ephemeral.ConfigValidator { | ||
| if rCasted, ok := (*r.innerResource).(ephemeral.EphemeralResourceWithConfigValidators); ok { | ||
| return rCasted.ConfigValidators(ctx) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Renew implements the optional ephemeral.EphemeralResourceWithRenew interface | ||
| // Uses interface detection to only call if the inner resource supports renewal | ||
| func (r *FrameworkEphemeralResourceWrapper) Renew(ctx context.Context, req ephemeral.RenewRequest, resp *ephemeral.RenewResponse) { | ||
| if rCasted, ok := (*r.innerResource).(ephemeral.EphemeralResourceWithRenew); ok { | ||
| rCasted.Renew(ctx, req, resp) | ||
| } | ||
| } | ||
|
|
||
| // Close implements the optional ephemeral.EphemeralResourceWithClose interface | ||
| // Uses interface detection to only call if the inner resource supports cleanup | ||
| func (r *FrameworkEphemeralResourceWrapper) Close(ctx context.Context, req ephemeral.CloseRequest, resp *ephemeral.CloseResponse) { | ||
| if rCasted, ok := (*r.innerResource).(ephemeral.EphemeralResourceWithClose); ok { | ||
| rCasted.Close(ctx, req, resp) | ||
| } | ||
| } |
This file contains hidden or 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.
question (blocking) - Is there a way to make this configurable such that someone could retrieve their API key from their own Secret Manager instead of relying on the Datadog API?
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.
@tyjet do you mean using some external (non-Datadog) API/provider?
If so, yes, if someone wants to configure their infra to use a secret manager instead of using this ephemeral resource they 💯 can do that, most known secret managers have terraform providers and resources devs can use.
That would mean, that devs won't be using this resource, as this is specifically to make a state-secure way to fetch this data via Datadog API.
I believe this leads to an important question: do we want to allow a way to get API key via our provider or do we want to just cut this API path from our provider entirely and force devs to use external secret managers?
And if we believe restricting this in our Terraform provider (despite still having API support because of reasons) is "the way" to lead devs to better/proper patterns then we don't need an ephemeral resource for
datadog_api_keyat all.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.
Thanks for the explanation, I think I understand much better now.