|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package ephemeralauth |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-provider-vault/internal/consts" |
| 16 | + "github.com/hashicorp/terraform-provider-vault/internal/framework/base" |
| 17 | + "github.com/hashicorp/terraform-provider-vault/internal/framework/client" |
| 18 | + "github.com/hashicorp/terraform-provider-vault/internal/framework/errutil" |
| 19 | + "github.com/hashicorp/terraform-provider-vault/internal/framework/model" |
| 20 | +) |
| 21 | + |
| 22 | +// Ensure the implementation satisfies the ephemeral.EphemeralResource interface |
| 23 | +var _ ephemeral.EphemeralResource = &ApproleAuthBackendRoleSecretIDEphemeralResource{} |
| 24 | + |
| 25 | +// NewApproleAuthBackendRoleSecretIDEphemeralResource returns the implementation for this resource to be |
| 26 | +// imported by the Terraform Plugin Framework provider |
| 27 | +var NewApproleAuthBackendRoleSecretIDEphemeralResource = func() ephemeral.EphemeralResource { |
| 28 | + return &ApproleAuthBackendRoleSecretIDEphemeralResource{} |
| 29 | +} |
| 30 | + |
| 31 | +// ApproleAuthBackendRoleSecretIDEphemeralResource implements the methods that define this resource |
| 32 | +type ApproleAuthBackendRoleSecretIDEphemeralResource struct { |
| 33 | + base.EphemeralResourceWithConfigure |
| 34 | +} |
| 35 | + |
| 36 | +// ApproleAuthBackendRoleSecretIDEphemeralModel describes the Terraform resource data model to match the |
| 37 | +// resource schema. |
| 38 | +type ApproleAuthBackendRoleSecretIDEphemeralModel struct { |
| 39 | + // common fields to all ephemeral resources |
| 40 | + base.BaseModelEphemeral |
| 41 | + |
| 42 | + // fields specific to this resource |
| 43 | + Backend types.String `tfsdk:"backend"` |
| 44 | + RoleName types.String `tfsdk:"role_name"` |
| 45 | + CIDRList types.Set `tfsdk:"cidr_list"` |
| 46 | + Metadata types.String `tfsdk:"metadata"` |
| 47 | + TTL types.Int64 `tfsdk:"ttl"` |
| 48 | + NumUses types.Int64 `tfsdk:"num_uses"` |
| 49 | + SecretID types.String `tfsdk:"secret_id"` |
| 50 | + Accessor types.String `tfsdk:"accessor"` |
| 51 | +} |
| 52 | + |
| 53 | +// ApproleAuthBackendRoleSecretIDAPIModel describes the Vault API data model. |
| 54 | +type ApproleAuthBackendRoleSecretIDAPIModel struct { |
| 55 | + SecretID string `json:"secret_id" mapstructure:"secret_id"` |
| 56 | + SecretIDAccessor string `json:"secret_id_accessor" mapstructure:"secret_id_accessor"` |
| 57 | +} |
| 58 | + |
| 59 | +// Schema defines this resource's schema which is the data that is available in |
| 60 | +// the resource's configuration, plan, and state |
| 61 | +// |
| 62 | +// https://developer.hashicorp.com/terraform/plugin/framework/resources#schema-method |
| 63 | +func (r *ApproleAuthBackendRoleSecretIDEphemeralResource) Schema(_ context.Context, _ ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 64 | + resp.Schema = schema.Schema{ |
| 65 | + Attributes: map[string]schema.Attribute{ |
| 66 | + consts.FieldBackend: schema.StringAttribute{ |
| 67 | + MarkdownDescription: "Unique name of the auth backend to configure.", |
| 68 | + Optional: true, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + consts.FieldRoleName: schema.StringAttribute{ |
| 72 | + MarkdownDescription: "Name of the role.", |
| 73 | + Required: true, |
| 74 | + }, |
| 75 | + consts.FieldCIDRList: schema.SetAttribute{ |
| 76 | + MarkdownDescription: "List of CIDR blocks that can log in using the SecretID.", |
| 77 | + ElementType: types.StringType, |
| 78 | + Optional: true, |
| 79 | + }, |
| 80 | + consts.FieldMetadata: schema.StringAttribute{ |
| 81 | + MarkdownDescription: "JSON-encoded secret data.", |
| 82 | + Optional: true, |
| 83 | + }, |
| 84 | + consts.FieldTTL: schema.Int64Attribute{ |
| 85 | + MarkdownDescription: "The TTL duration of the SecretID in seconds.", |
| 86 | + Optional: true, |
| 87 | + }, |
| 88 | + consts.FieldNumUses: schema.Int64Attribute{ |
| 89 | + MarkdownDescription: "The number of uses for the secret-id.", |
| 90 | + Optional: true, |
| 91 | + }, |
| 92 | + consts.FieldSecretID: schema.StringAttribute{ |
| 93 | + MarkdownDescription: "The generated SecretID.", |
| 94 | + Computed: true, |
| 95 | + Sensitive: true, |
| 96 | + }, |
| 97 | + consts.FieldAccessor: schema.StringAttribute{ |
| 98 | + MarkdownDescription: "The accessor for the SecretID.", |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + MarkdownDescription: "Provides an ephemeral resource to generate an AppRole SecretID from Vault.", |
| 103 | + } |
| 104 | + |
| 105 | + base.MustAddBaseEphemeralSchema(&resp.Schema) |
| 106 | +} |
| 107 | + |
| 108 | +// Metadata sets the full name for this resource |
| 109 | +func (r *ApproleAuthBackendRoleSecretIDEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 110 | + resp.TypeName = req.ProviderTypeName + "_approle_auth_backend_role_secret_id" |
| 111 | +} |
| 112 | + |
| 113 | +func (r *ApproleAuthBackendRoleSecretIDEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 114 | + var data ApproleAuthBackendRoleSecretIDEphemeralModel |
| 115 | + |
| 116 | + // Read Terraform configuration data into the model |
| 117 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 118 | + |
| 119 | + if resp.Diagnostics.HasError() { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + // Set default backend if not provided |
| 124 | + if data.Backend.IsNull() || data.Backend.IsUnknown() { |
| 125 | + data.Backend = types.StringValue("approle") |
| 126 | + } |
| 127 | + |
| 128 | + c, err := client.GetClient(ctx, r.Meta(), data.Namespace.ValueString()) |
| 129 | + if err != nil { |
| 130 | + resp.Diagnostics.AddError(errutil.ClientConfigureErr(err)) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + backend := strings.Trim(data.Backend.ValueString(), "/") |
| 135 | + role := strings.Trim(data.RoleName.ValueString(), "/") |
| 136 | + path := fmt.Sprintf("auth/%s/role/%s/secret-id", backend, role) |
| 137 | + |
| 138 | + // Build the request data |
| 139 | + requestData := make(map[string]interface{}) |
| 140 | + |
| 141 | + // Handle CIDR list |
| 142 | + if !data.CIDRList.IsNull() && !data.CIDRList.IsUnknown() { |
| 143 | + var cidrs []string |
| 144 | + resp.Diagnostics.Append(data.CIDRList.ElementsAs(ctx, &cidrs, false)...) |
| 145 | + if resp.Diagnostics.HasError() { |
| 146 | + return |
| 147 | + } |
| 148 | + if len(cidrs) > 0 { |
| 149 | + requestData[consts.FieldCIDRList] = strings.Join(cidrs, ",") |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Handle metadata |
| 154 | + if !data.Metadata.IsNull() && !data.Metadata.IsUnknown() { |
| 155 | + requestData[consts.FieldMetadata] = data.Metadata.ValueString() |
| 156 | + } |
| 157 | + |
| 158 | + // Handle TTL |
| 159 | + if !data.TTL.IsNull() && !data.TTL.IsUnknown() { |
| 160 | + requestData[consts.FieldTTL] = data.TTL.ValueInt64() |
| 161 | + } |
| 162 | + |
| 163 | + // Handle num_uses |
| 164 | + if !data.NumUses.IsNull() && !data.NumUses.IsUnknown() { |
| 165 | + requestData[consts.FieldNumUses] = data.NumUses.ValueInt64() |
| 166 | + } |
| 167 | + |
| 168 | + secretResp, err := c.Logical().WriteWithContext(ctx, path, requestData) |
| 169 | + if err != nil { |
| 170 | + resp.Diagnostics.AddError( |
| 171 | + "Error generating AppRole SecretID", |
| 172 | + fmt.Sprintf("Could not generate SecretID at path %s: %s", path, err), |
| 173 | + ) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + if secretResp == nil || secretResp.Data == nil { |
| 178 | + resp.Diagnostics.AddError( |
| 179 | + "Empty response from Vault", |
| 180 | + fmt.Sprintf("No data returned when generating SecretID at path %s", path), |
| 181 | + ) |
| 182 | + return |
| 183 | + } |
| 184 | + |
| 185 | + var readResp ApproleAuthBackendRoleSecretIDAPIModel |
| 186 | + err = model.ToAPIModel(secretResp.Data, &readResp) |
| 187 | + if err != nil { |
| 188 | + resp.Diagnostics.AddError("Unable to translate Vault response data", err.Error()) |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + data.SecretID = types.StringValue(readResp.SecretID) |
| 193 | + data.Accessor = types.StringValue(readResp.SecretIDAccessor) |
| 194 | + |
| 195 | + resp.Diagnostics.Append(resp.Result.Set(ctx, &data)...) |
| 196 | + |
| 197 | + // Store the accessor and backend info for cleanup in Close |
| 198 | + resp.Private.SetKey(ctx, consts.FieldAccessor, []byte(readResp.SecretIDAccessor)) |
| 199 | + resp.Private.SetKey(ctx, consts.FieldBackend, []byte(backend)) |
| 200 | + resp.Private.SetKey(ctx, consts.FieldRole, []byte(role)) |
| 201 | + resp.Private.SetKey(ctx, consts.FieldNamespace, []byte(data.Namespace.ValueString())) |
| 202 | +} |
0 commit comments