package resource import ( "context" "errors" "fmt" "strings" infisical "terraform-provider-infisical/internal/client" pkg "terraform-provider-infisical/internal/pkg/strings" "time" "github.com/hashicorp/go-uuid" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" ) // Ensure the implementation satisfies the expected interfaces. var ( _ resource.Resource = &secretResource{} ) // NewsecretResource is a helper function to simplify the provider implementation. func NewSecretResource() resource.Resource { return &secretResource{} } // secretResource is the resource implementation. type secretResource struct { client *infisical.Client } type SecretReminder struct { Note types.String `tfsdk:"note"` RepeatDays types.Int64 `tfsdk:"repeat_days"` } // secretResourceSourceModel describes the data source data model. type secretResourceModel struct { FolderPath types.String `tfsdk:"folder_path"` EnvSlug types.String `tfsdk:"env_slug"` Name types.String `tfsdk:"name"` SecretReminder *SecretReminder `tfsdk:"secret_reminder"` Value types.String `tfsdk:"value"` ValueWO types.String `tfsdk:"value_wo"` ValueWOVersion types.Int64 `tfsdk:"value_wo_version"` WorkspaceId types.String `tfsdk:"workspace_id"` LastUpdated types.String `tfsdk:"last_updated"` Tags types.Set `tfsdk:"tag_ids"` Metadata types.Map `tfsdk:"metadata"` ID types.String `tfsdk:"id"` } type SecretData struct { IsWriteOnly bool ShouldUpdateValue bool Value string } func (m *secretResourceModel) getSecretValue(ctx context.Context, config tfsdk.Config, state *tfsdk.State, diags *diag.Diagnostics) (SecretData, error) { // check if normal value was configured if !m.Value.IsNull() && !m.Value.IsUnknown() { return SecretData{ IsWriteOnly: false, ShouldUpdateValue: true, Value: m.Value.ValueString(), }, nil } // check write-only value if the normal value isn't set var secretValue types.String diags.Append(config.GetAttribute(ctx, path.Root("value_wo"), &secretValue)...) if diags.HasError() { return SecretData{}, errors.New("failed to get write-only secret value") } if !secretValue.IsNull() && !secretValue.IsUnknown() { shouldUpdateValue := true // if state exists we know its an update operation, so we need to compare versions if state != nil && !state.Raw.IsNull() { var newVersion types.Int64 diags.Append(config.GetAttribute(ctx, path.Root("value_wo_version"), &newVersion)...) var stateModel secretResourceModel diags.Append(state.Get(ctx, &stateModel)...) if !diags.HasError() { oldVersion := int64(0) if !stateModel.ValueWOVersion.IsNull() { oldVersion = stateModel.ValueWOVersion.ValueInt64() } if oldVersion > newVersion.ValueInt64() { return SecretData{}, errors.New("new value write-only version is less than old version") } shouldUpdateValue = newVersion.ValueInt64() != oldVersion } } return SecretData{ IsWriteOnly: true, Value: secretValue.ValueString(), ShouldUpdateValue: shouldUpdateValue, }, nil } return SecretData{}, errors.New("no secret value provided") } // Metadata returns the resource type name. func (r *secretResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_secret" } // Schema defines the schema for the resource. func (r *secretResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ Description: "Create secrets & save to Infisical", Attributes: map[string]schema.Attribute{ "folder_path": schema.StringAttribute{ Description: "The path to the folder where the given secret resides", PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, Required: true, Computed: false, }, "env_slug": schema.StringAttribute{ Description: "The environment slug of the secret to modify/create", PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, Required: true, Computed: false, }, "name": schema.StringAttribute{ Description: "The name of the secret", Required: true, Computed: false, }, "value": schema.StringAttribute{ Description: "The value of the secret in plain text. This is required if `value_wo` is not set.", Validators: []validator.String{ stringvalidator.ConflictsWith(path.MatchRoot("value_wo")), stringvalidator.ConflictsWith(path.MatchRoot("value_wo_version")), }, Optional: true, Computed: false, Sensitive: true, }, "value_wo": schema.StringAttribute{ Description: "The value of the secret in plain text as a write-only secret. If set, the secret value will not be stored in state. This is required if `value` is not set. Requires Terraform version 1.11.0 or higher.", Optional: true, Computed: false, WriteOnly: true, Validators: []validator.String{ stringvalidator.AlsoRequires(path.Expressions{ path.MatchRoot("value_wo_version"), }...), }, }, "value_wo_version": schema.Int64Attribute{ Description: "Used together with value_wo to trigger an update. Increment this value when an update to the value_wo is required.", Optional: true, Validators: []validator.Int64{ int64validator.AtLeast(1), int64validator.AlsoRequires(path.Expressions{ path.MatchRoot("value_wo"), }...), }, }, "workspace_id": schema.StringAttribute{ Description: "The Infisical project ID (Required for Machine Identity auth, and service tokens with multiple scopes)", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown(), stringplanmodifier.RequiresReplace()}, Optional: true, Computed: true, }, "last_updated": schema.StringAttribute{ Description: "The last time the secret was updated.", Computed: true, }, "tag_ids": schema.SetAttribute{ ElementType: types.StringType, Optional: true, Description: "Tag ids to be attached for the secrets.", }, "metadata": schema.MapAttribute{ ElementType: types.StringType, Optional: true, Description: "Metadata associated with the secret as key-value pairs.", }, "secret_reminder": schema.SingleNestedAttribute{ Attributes: map[string]schema.Attribute{ "note": schema.StringAttribute{ Description: "Note for the secret rotation reminder", Computed: false, Optional: true, }, "repeat_days": schema.Int64Attribute{ Description: "Frequency of secret rotation reminder in days", Computed: false, Required: true, Validators: []validator.Int64{ int64validator.AtLeast(1), int64validator.AtMost(365), }, }, }, Optional: true, Computed: false, }, "id": schema.StringAttribute{ Description: "The ID of the secret", Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, }, }, } } // Configure adds the provider configured client to the resource. func (r *secretResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { if req.ProviderData == nil { return } client, ok := req.ProviderData.(*infisical.Client) if !ok { resp.Diagnostics.AddError( "Unexpected Data Source Configure Type", fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), ) return } r.client = client } // Create creates the resource and sets the initial Terraform state. func (r *secretResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // Retrieve values from plan var plan secretResourceModel diags := req.Plan.Get(ctx, &plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } secretData, err := plan.getSecretValue(ctx, req.Config, nil, &resp.Diagnostics) if err != nil { resp.Diagnostics.AddError( "Error creating secret", "Could not get secret value, unexpected error: "+err.Error(), ) return } planSecretTagIds := make([]types.String, 0, len(plan.Tags.Elements())) diags = plan.Tags.ElementsAs(ctx, &planSecretTagIds, false) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } secretTagIds := make([]string, 0, len(planSecretTagIds)) for _, slug := range planSecretTagIds { secretTagIds = append(secretTagIds, strings.ToLower(slug.ValueString())) } planMetadata := make(map[string]types.String, 0) if !plan.Metadata.IsNull() && !plan.Metadata.IsUnknown() { diags = plan.Metadata.ElementsAs(ctx, &planMetadata, false) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } secretMetadata := make([]infisical.SecretMetadataItem, 0, len(planMetadata)) for key, value := range planMetadata { secretMetadata = append(secretMetadata, infisical.SecretMetadataItem{ Key: key, Value: value.ValueString(), }) } var workspaceId string if r.client.Config.AuthStrategy == infisical.AuthStrategy.SERVICE_TOKEN { serviceTokenDetails, err := r.client.GetServiceTokenDetailsV2() if err != nil { resp.Diagnostics.AddError( "Error creating secret", "Could not get service token details, unexpected error: "+err.Error(), ) return } workspaceId = serviceTokenDetails.Workspace } else if r.client.Config.IsMachineIdentityAuth { workspaceId = plan.WorkspaceId.ValueString() } else { resp.Diagnostics.AddError( "Error creating secret", "Unknown authentication strategy", ) return } // null check secret reminder var secretReminderNote string var secretReminderRepeatDays int64 if plan.SecretReminder != nil { secretReminderNote = plan.SecretReminder.Note.ValueString() secretReminderRepeatDays = plan.SecretReminder.RepeatDays.ValueInt64() } secret, err := r.client.CreateRawSecretsV3(infisical.CreateRawSecretV3Request{ Environment: plan.EnvSlug.ValueString(), WorkspaceID: workspaceId, Type: "shared", SecretPath: plan.FolderPath.ValueString(), SecretReminderNote: secretReminderNote, SecretReminderRepeatDays: secretReminderRepeatDays, SecretKey: plan.Name.ValueString(), SecretValue: secretData.Value, TagIDs: secretTagIds, SecretMetadata: secretMetadata, }) if err != nil { var approvalErr *infisical.SecretApprovalRequiredError if errors.As(err, &approvalErr) { approvalDetails := r.pollSecretApprovalRequest(ctx, approvalErr.Approval.ID, defaultApprovalTimeoutSeconds, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } secretID := findCommitSecretID(approvalDetails.Commits, plan.Name.ValueString(), "create") if secretID == "" { resp.Diagnostics.AddError( "Error resolving secret after approval", fmt.Sprintf("Could not find the created secret ID in the approval request commits. Approval request ID: %s", approvalErr.Approval.ID), ) return } // Read the secret state after merge. readResponse, readErr := r.client.GetSingleSecretByIDV4(infisical.GetSingleSecretByIDV4Request{ ID: secretID, ViewSecretValue: !secretData.IsWriteOnly, }) if readErr != nil { resp.Diagnostics.AddError( "Error reading secret after approval merge", "Could not read secret with ID "+secretID+": "+readErr.Error(), ) return } plan.ID = types.StringValue(readResponse.Secret.ID) plan.Name = types.StringValue(readResponse.Secret.SecretKey) if readResponse.Secret.UpdatedAt != "" { plan.LastUpdated = types.StringValue(readResponse.Secret.UpdatedAt) } if !plan.Value.IsNull() && !plan.Value.IsUnknown() { plan.Value = types.StringValue(readResponse.Secret.SecretValue) } if len(readResponse.Secret.SecretMetadata) > 0 { metadataMap := make(map[string]types.String, len(readResponse.Secret.SecretMetadata)) for _, item := range readResponse.Secret.SecretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } plan.Metadata, diags = types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else if plan.Metadata.IsNull() || plan.Metadata.IsUnknown() { plan.Metadata = types.MapNull(types.StringType) } plan.WorkspaceId = types.StringValue(workspaceId) diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) return } resp.Diagnostics.AddError( "Error creating secret", "Couldn't save encrypted secrets to Infisical, unexpected error: "+err.Error(), ) return } plan.ID = types.StringValue(secret.ID) if secret.UpdatedAt != "" { plan.LastUpdated = types.StringValue(secret.UpdatedAt) } if len(secret.SecretMetadata) > 0 { metadataMap := make(map[string]types.String, len(secret.SecretMetadata)) for _, item := range secret.SecretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } plan.Metadata, diags = types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else if plan.Metadata.IsNull() || plan.Metadata.IsUnknown() { plan.Metadata = types.MapNull(types.StringType) } plan.WorkspaceId = types.StringValue(workspaceId) diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } // Read refreshes the Terraform state with the latest data. func (r *secretResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // Get current state var state secretResourceModel diags := req.State.Get(ctx, &state) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } if state.ID.ValueString() == "" { resp.State.RemoveResource(ctx) return } // Read by ID instead of name+path so folder renames don't cause a spurious deletion. // A write-only secret keeps its value out of state, so the refresh does not need it and works without Read Value. isWriteOnly := state.Value.IsNull() || state.Value.IsUnknown() response, err := r.client.GetSingleSecretByIDV4(infisical.GetSingleSecretByIDV4Request{ ID: state.ID.ValueString(), ViewSecretValue: !isWriteOnly, }) if err != nil { if err == infisical.ErrNotFound { resp.State.RemoveResource(ctx) } else { resp.Diagnostics.AddError( "Error Reading Infisical secret", "Could not read Infisical secret with ID "+state.ID.ValueString()+": "+err.Error(), ) } return } state.Name = types.StringValue(response.Secret.SecretKey) state.ID = types.StringValue(response.Secret.ID) state.FolderPath = types.StringValue(response.Secret.SecretPath) state.EnvSlug = types.StringValue(response.Secret.Environment) if response.Secret.UpdatedAt != "" { state.LastUpdated = types.StringValue(response.Secret.UpdatedAt) } if !isWriteOnly { state.Value = types.StringValue(response.Secret.SecretValue) } if len(response.Secret.Tags) > 0 { tagIDs := make([]string, 0, len(response.Secret.Tags)) for _, tag := range response.Secret.Tags { tagIDs = append(tagIDs, tag.ID) } state.Tags, diags = types.SetValueFrom(ctx, types.StringType, tagIDs) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else if state.Tags.IsNull() || state.Tags.IsUnknown() { state.Tags = types.SetNull(types.StringType) } else { state.Tags, diags = types.SetValueFrom(ctx, types.StringType, []string{}) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } if len(response.Secret.SecretMetadata) > 0 { metadataMap := make(map[string]types.String, len(response.Secret.SecretMetadata)) for _, item := range response.Secret.SecretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } state.Metadata, diags = types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else { state.Metadata = types.MapNull(types.StringType) } // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } // Update updates the resource and sets the updated Terraform state on success. func (r *secretResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // Retrieve values from plan var plan secretResourceModel diags := req.Plan.Get(ctx, &plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } secretData, err := plan.getSecretValue(ctx, req.Config, &req.State, &resp.Diagnostics) if err != nil { resp.Diagnostics.AddError( "Error updating secret", "Could not get secret value, unexpected error: "+err.Error(), ) return } var state secretResourceModel diagsFromState := req.State.Get(ctx, &state) resp.Diagnostics.Append(diagsFromState...) if resp.Diagnostics.HasError() { return } planSecretTagIds := make([]types.String, 0, len(plan.Tags.Elements())) diags = plan.Tags.ElementsAs(ctx, &planSecretTagIds, false) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } secretTagIds := make([]string, 0, len(planSecretTagIds)) for _, slug := range planSecretTagIds { secretTagIds = append(secretTagIds, strings.ToLower(slug.ValueString())) } planMetadata := make(map[string]types.String, 0) if !plan.Metadata.IsNull() && !plan.Metadata.IsUnknown() { diags = plan.Metadata.ElementsAs(ctx, &planMetadata, false) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } secretMetadata := make([]infisical.SecretMetadataItem, 0, len(planMetadata)) for key, value := range planMetadata { secretMetadata = append(secretMetadata, infisical.SecretMetadataItem{ Key: key, Value: value.ValueString(), }) } // null check secret reminder var secretReminderNote string var secretReminderRepeatDays int64 if plan.SecretReminder != nil { secretReminderNote = plan.SecretReminder.Note.ValueString() secretReminderRepeatDays = plan.SecretReminder.RepeatDays.ValueInt64() } var workspaceId string if r.client.Config.AuthStrategy == infisical.AuthStrategy.SERVICE_TOKEN { serviceTokenDetails, err := r.client.GetServiceTokenDetailsV2() if err != nil { resp.Diagnostics.AddError( "Error updating secret", "Could not get service token details, unexpected error: "+err.Error(), ) return } workspaceId = serviceTokenDetails.Workspace } else if r.client.Config.IsMachineIdentityAuth { workspaceId = plan.WorkspaceId.ValueString() } else { resp.Diagnostics.AddError( "Error updating secret", "Unknown authentication strategy", ) return } updateRequest := infisical.UpdateRawSecretByNameV3Request{ Environment: plan.EnvSlug.ValueString(), WorkspaceID: workspaceId, Type: "shared", TagIDs: secretTagIds, SecretPath: plan.FolderPath.ValueString(), SecretName: state.Name.ValueString(), SecretReminderNote: secretReminderNote, SecretReminderRepeatDays: secretReminderRepeatDays, SecretMetadata: secretMetadata, } if secretData.ShouldUpdateValue { updateRequest.SecretValue = pkg.StringToPtr(secretData.Value) } if plan.Name.ValueString() != state.Name.ValueString() { updateRequest.NewSecretName = plan.Name.ValueString() } updatedSecret, err := r.client.UpdateRawSecretV3(updateRequest) if err != nil { var approvalErr *infisical.SecretApprovalRequiredError if errors.As(err, &approvalErr) { r.pollSecretApprovalRequest(ctx, approvalErr.Approval.ID, defaultApprovalTimeoutSeconds, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } // Read the secret state after merge using the existing ID from state. readResponse, readErr := r.client.GetSingleSecretByIDV4(infisical.GetSingleSecretByIDV4Request{ ID: state.ID.ValueString(), ViewSecretValue: !secretData.IsWriteOnly, }) if readErr != nil { resp.Diagnostics.AddError( "Error reading secret after approval merge", "Could not read secret with ID "+state.ID.ValueString()+": "+readErr.Error(), ) return } plan.Name = types.StringValue(readResponse.Secret.SecretKey) if readResponse.Secret.UpdatedAt != "" { plan.LastUpdated = types.StringValue(readResponse.Secret.UpdatedAt) } if !plan.Value.IsNull() && !plan.Value.IsUnknown() { plan.Value = types.StringValue(readResponse.Secret.SecretValue) } if len(readResponse.Secret.SecretMetadata) > 0 { metadataMap := make(map[string]types.String, len(readResponse.Secret.SecretMetadata)) for _, item := range readResponse.Secret.SecretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } plan.Metadata, diags = types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else if plan.Metadata.IsNull() || plan.Metadata.IsUnknown() { plan.Metadata = types.MapNull(types.StringType) } plan.WorkspaceId = types.StringValue(workspaceId) diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) return } resp.Diagnostics.AddError( "Error updating secret", "Couldn't save encrypted secrets to Infisical, unexpected error: "+err.Error(), ) return } if updatedSecret.UpdatedAt != "" { plan.LastUpdated = types.StringValue(updatedSecret.UpdatedAt) } if len(secretMetadata) > 0 { metadataMap := make(map[string]types.String, len(secretMetadata)) for _, item := range secretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } plan.Metadata, diags = types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } else if plan.Metadata.IsNull() || plan.Metadata.IsUnknown() { plan.Metadata = types.MapNull(types.StringType) } plan.WorkspaceId = types.StringValue(workspaceId) diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } } // Delete deletes the resource and removes the Terraform state on success. func (r *secretResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { // Retrieve values from state var state secretResourceModel diags := req.State.Get(ctx, &state) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } if r.client.Config.IsMachineIdentityAuth || r.client.Config.AuthStrategy == infisical.AuthStrategy.SERVICE_TOKEN { err := r.client.DeleteRawSecretV3(infisical.DeleteRawSecretV3Request{ SecretName: state.Name.ValueString(), SecretPath: state.FolderPath.ValueString(), Environment: state.EnvSlug.ValueString(), Type: "shared", WorkspaceId: state.WorkspaceId.ValueString(), }) if err != nil { var approvalErr *infisical.SecretApprovalRequiredError if errors.As(err, &approvalErr) { r.pollSecretApprovalRequest(ctx, approvalErr.Approval.ID, defaultApprovalTimeoutSeconds, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } // Approval merged, secret is now deleted. Terraform will remove the state automatically. return } resp.Diagnostics.AddError( "Error Deleting Infisical secret", "Could not delete secret, unexpected error: "+err.Error(), ) return } } else { resp.Diagnostics.AddError( "Error deleting secret", "Unknown authentication strategy", ) return } } const defaultApprovalTimeoutSeconds = 3600 // 1h func nextBackoffInterval(current, maxInterval time.Duration) time.Duration { next := time.Duration(float64(current) * 1.5) if next > maxInterval { return maxInterval } return next } // pollSecretApprovalRequest polls the secret approval request endpoint until the // request is merged, closed, or the context is cancelled. Returns the approval // details once merged, or adds a diagnostic error otherwise. func (r *secretResource) pollSecretApprovalRequest(ctx context.Context, approvalID string, timeoutSeconds int64, diagnostics *diag.Diagnostics) *infisical.SecretApprovalRequestDetails { timeout := time.Duration(timeoutSeconds) * time.Second startTime := time.Now() minInterval := 10 * time.Second maxInterval := 30 * time.Second currentInterval := minInterval for { if ctx.Err() != nil { diagnostics.AddError("Operation cancelled", ctx.Err().Error()) return nil } if time.Since(startTime) > timeout { diagnostics.AddError( "Secret approval request timeout", fmt.Sprintf("Secret approval request was not merged within %d seconds. Approval request ID: %s. Please approve the request and re-run terraform apply.", timeoutSeconds, approvalID), ) return nil } statusResponse, err := r.client.GetSecretApprovalRequestByID(infisical.GetSecretApprovalRequestByIDRequest{ ID: approvalID, }) if err != nil { if err == infisical.ErrNotFound { select { case <-ctx.Done(): diagnostics.AddError("Operation cancelled", ctx.Err().Error()) return nil case <-time.After(currentInterval): currentInterval = nextBackoffInterval(currentInterval, maxInterval) continue } } diagnostics.AddError( "Error checking secret approval request status", fmt.Sprintf("Failed to check secret approval request status: %v. Approval request ID: %s", err, approvalID), ) return nil } approval := statusResponse.Approval if approval.HasMerged { tflog.Info(ctx, "Secret approval request has been merged", map[string]interface{}{ "approval_id": approvalID, }) return &approval } if approval.Status != "open" { diagnostics.AddError( "Secret approval request was closed", fmt.Sprintf("The secret approval request was closed without being merged (status: %s). Approval request ID: %s", approval.Status, approvalID), ) return nil } tflog.Debug(ctx, "Secret approval request is still pending, polling again", map[string]interface{}{ "approval_id": approvalID, "current_interval": currentInterval.String(), }) select { case <-ctx.Done(): diagnostics.AddError("Operation cancelled", ctx.Err().Error()) return nil case <-time.After(currentInterval): currentInterval = nextBackoffInterval(currentInterval, maxInterval) continue } } } // findCommitSecretID finds the secret ID from the approval commits for a given // secret key and operation type. func findCommitSecretID(commits []infisical.SecretApprovalCommit, secretKey string, op string) string { for _, commit := range commits { if commit.SecretKey == secretKey && commit.Op == op && commit.Secret != nil { return commit.Secret.ID } } return "" } func (r *secretResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { var workspace, environment, secretPath, secretName, secretValue, secretId string var tags []string var updatedAt string var secretReminder SecretReminder var secretMetadata []infisical.SecretMetadataItem const writeOnlyPrefix = "write-only:" isWriteOnly := false importID := req.ID if strings.HasPrefix(req.ID, writeOnlyPrefix) { isWriteOnly = true importID = strings.TrimPrefix(req.ID, writeOnlyPrefix) if importID == "" { resp.Diagnostics.AddError( "Invalid Import ID", "The import ID after the 'write-only:' prefix must not be empty. Expected 'write-only:' or 'write-only::::'.", ) return } } if _, err := uuid.ParseUUID(importID); err == nil { secret, err := r.client.GetSingleSecretByIDV4(infisical.GetSingleSecretByIDV4Request{ ID: importID, ViewSecretValue: !isWriteOnly, }) if err != nil { if err == infisical.ErrNotFound { resp.Diagnostics.AddError( "Secret not found", "The secret with the given ID was not found", ) } else { resp.Diagnostics.AddError( "Error fetching secret", "Couldn't fetch secret from Infisical, unexpected error: "+err.Error(), ) } return } for _, tag := range secret.Secret.Tags { tags = append(tags, tag.ID) } secretReminder.Note = types.StringValue(secret.Secret.SecretReminderNote) secretReminder.RepeatDays = types.Int64Value(secret.Secret.SecretReminderRepeatDays) workspace = secret.Secret.Workspace environment = secret.Secret.Environment secretPath = secret.Secret.SecretPath secretName = secret.Secret.SecretKey secretValue = secret.Secret.SecretValue secretId = secret.Secret.ID secretMetadata = secret.Secret.SecretMetadata updatedAt = secret.Secret.UpdatedAt } else { parts := strings.SplitN(importID, ":", 4) if len(parts) != 4 { resp.Diagnostics.AddError( "Invalid ID Format", "The secret ID must be a uuid or in the format of ':::'", ) return } viewSecretValue := !isWriteOnly secret, err := r.client.GetSingleRawSecretByNameV3(infisical.GetSingleSecretByNameV3Request{ WorkspaceId: parts[0], Environment: parts[1], SecretPath: parts[2], SecretName: parts[3], Type: "shared", // Just use the secret uuid instead if (type is 'personal') }, &viewSecretValue) if err != nil { if err == infisical.ErrNotFound { resp.Diagnostics.AddError( "Secret not found", "The secret with the given ID was not found", ) } else { resp.Diagnostics.AddError( "Error fetching secret", "Couldn't fetch secret from Infisical, unexpected error: "+err.Error(), ) } return } for _, tag := range secret.Secret.Tags { tags = append(tags, tag.ID) } secretReminder.Note = types.StringValue(secret.Secret.SecretReminderNote) secretReminder.RepeatDays = types.Int64Value(secret.Secret.SecretReminderRepeatDays) workspace = secret.Secret.Workspace environment = secret.Secret.Environment secretPath = secret.Secret.SecretPath secretName = secret.Secret.SecretKey secretValue = secret.Secret.SecretValue secretId = secret.Secret.ID secretMetadata = secret.Secret.SecretMetadata updatedAt = secret.Secret.UpdatedAt } resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("workspace_id"), workspace)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("env_slug"), environment)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder_path"), secretPath)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), secretName)...) if isWriteOnly { resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("value_wo_version"), types.Int64Value(1))...) } else { resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("value"), secretValue)...) } resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("tag_ids"), tags)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("last_updated"), updatedAt)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("secret_reminder"), secretReminder)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), secretId)...) if len(secretMetadata) > 0 { metadataMap := make(map[string]types.String, len(secretMetadata)) for _, item := range secretMetadata { metadataMap[item.Key] = types.StringValue(item.Value) } metadataValue, diags := types.MapValueFrom(ctx, types.StringType, metadataMap) resp.Diagnostics.Append(diags...) if !resp.Diagnostics.HasError() { resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("metadata"), metadataValue)...) } } else { resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("metadata"), types.MapNull(types.StringType))...) } }