mirror of
https://github.com/hanzokms/terraform-provider.git
synced 2026-10-30 20:29:45 +00:00
382 lines
14 KiB
Go
382 lines
14 KiB
Go
package resource
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
kmsclient "github.com/hanzokms/terraform-provider/internal/client"
|
|
kmsstrings "github.com/hanzokms/terraform-provider/internal/pkg/strings"
|
|
"github.com/hanzokms/terraform-provider/internal/pkg/terraform"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
|
"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/stringdefault"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
// NewIdentityGcpAuthResource is a helper function to simplify the provider implementation.
|
|
func NewIdentityGcpAuthResource() resource.Resource {
|
|
return &IdentityGcpAuthResource{}
|
|
}
|
|
|
|
// IdentityGcpAuthResource is the resource implementation.
|
|
type IdentityGcpAuthResource struct {
|
|
client *kmsclient.Client
|
|
}
|
|
|
|
// IdentityGcpAuthResourceSourceModel describes the data source data model.
|
|
type IdentityGcpAuthResourceModel struct {
|
|
ID types.String `tfsdk:"id"`
|
|
IdentityID types.String `tfsdk:"identity_id"`
|
|
Type types.String `tfsdk:"type"`
|
|
AllowedServiceAccountEmails types.List `tfsdk:"allowed_service_account_emails"`
|
|
AllowedProjects types.List `tfsdk:"allowed_projects"`
|
|
AllowedZones types.List `tfsdk:"allowed_zones"`
|
|
AccessTokenTrustedIps types.List `tfsdk:"access_token_trusted_ips"`
|
|
AccessTokenTTL types.Int64 `tfsdk:"access_token_ttl"`
|
|
AccessTokenMaxTTL types.Int64 `tfsdk:"access_token_max_ttl"`
|
|
AccessTokenNumUsesLimit types.Int64 `tfsdk:"access_token_num_uses_limit"`
|
|
}
|
|
|
|
type IdentityGcpAuthResourceTrustedIps struct {
|
|
IpAddress types.String `tfsdk:"ip_address"`
|
|
}
|
|
|
|
// Metadata returns the resource type name.
|
|
func (r *IdentityGcpAuthResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_identity_gcp_auth"
|
|
}
|
|
|
|
// Schema defines the schema for the resource.
|
|
func (r *IdentityGcpAuthResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Create and manage identity gcp auth in Kms.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
Description: "The ID of the gcp auth",
|
|
Computed: true,
|
|
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
|
|
},
|
|
"identity_id": schema.StringAttribute{
|
|
Description: "The ID of the identity to attach the configuration onto.",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
|
|
},
|
|
"type": schema.StringAttribute{
|
|
Description: "The Type of GCP Auth Method to use: Options are gce, iam. Default:gce",
|
|
Optional: true,
|
|
Default: stringdefault.StaticString("gce"),
|
|
Computed: true,
|
|
},
|
|
"allowed_service_account_emails": schema.ListAttribute{
|
|
ElementType: types.StringType,
|
|
Description: "List of trusted service account emails corresponding to the GCE resource(s) allowed to authenticate with Kms",
|
|
MarkdownDescription: "List of trusted service account emails corresponding to the GCE resource(s) allowed to authenticate with Kms; this could be something like `[email protected]`, `[email protected]`, etc.",
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"allowed_projects": schema.ListAttribute{
|
|
ElementType: types.StringType,
|
|
Description: "List of trusted GCP projects that the GCE instance must belong to authenticate with Kms. Note that this validation property will only work for GCE instances",
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"allowed_zones": schema.ListAttribute{
|
|
ElementType: types.StringType,
|
|
Description: "List of trusted zones that the GCE instances must belong to authenticate with Kms; this should be the fully-qualified zone name in the format `<region>-<zone>`like `us-central1-a`, `us-west1-b`, etc. Note that this validation property will only work for GCE instances.",
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"access_token_trusted_ips": schema.ListNestedAttribute{
|
|
Optional: true,
|
|
Computed: true,
|
|
Description: "A list of IPs or CIDR ranges that access tokens can be used from. You can use 0.0.0.0/0, to allow usage from any network address..",
|
|
NestedObject: schema.NestedAttributeObject{
|
|
Attributes: map[string]schema.Attribute{
|
|
"ip_address": schema.StringAttribute{
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"access_token_ttl": schema.Int64Attribute{
|
|
Description: "The lifetime for an access token in seconds. This value will be referenced at renewal time. Default: 2592000",
|
|
Computed: true,
|
|
Optional: true,
|
|
},
|
|
"access_token_max_ttl": schema.Int64Attribute{
|
|
Description: "The maximum lifetime for an access token in seconds. This value will be referenced at renewal time. Default: 2592000",
|
|
Computed: true,
|
|
Optional: true,
|
|
},
|
|
"access_token_num_uses_limit": schema.Int64Attribute{
|
|
Description: "The maximum number of times that an access token can be used; a value of 0 implies infinite number of uses. Default:0",
|
|
Computed: true,
|
|
Optional: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Configure adds the provider configured client to the resource.
|
|
func (r *IdentityGcpAuthResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
|
if req.ProviderData == nil {
|
|
return
|
|
}
|
|
|
|
client, ok := req.ProviderData.(*kmsclient.Client)
|
|
|
|
if !ok {
|
|
resp.Diagnostics.AddError(
|
|
"Unexpected resource Configure Type",
|
|
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
|
)
|
|
|
|
return
|
|
}
|
|
|
|
r.client = client
|
|
}
|
|
|
|
func updateGcpAuthStateByApi(ctx context.Context, diagnose diag.Diagnostics, plan *IdentityGcpAuthResourceModel, newIdentityGcpAuth *kmsclient.IdentityGcpAuth) {
|
|
plan.AccessTokenMaxTTL = types.Int64Value(newIdentityGcpAuth.AccessTokenMaxTTL)
|
|
plan.AccessTokenTTL = types.Int64Value(newIdentityGcpAuth.AccessTokenTTL)
|
|
plan.AccessTokenNumUsesLimit = types.Int64Value(newIdentityGcpAuth.AccessTokenNumUsesLimit)
|
|
|
|
planAccessTokenTrustedIps := make([]IdentityGcpAuthResourceTrustedIps, len(newIdentityGcpAuth.AccessTokenTrustedIPS))
|
|
for i, el := range newIdentityGcpAuth.AccessTokenTrustedIPS {
|
|
if el.Prefix != nil {
|
|
planAccessTokenTrustedIps[i] = IdentityGcpAuthResourceTrustedIps{IpAddress: types.StringValue(
|
|
el.IpAddress + "/" + strconv.Itoa(*el.Prefix),
|
|
)}
|
|
} else {
|
|
planAccessTokenTrustedIps[i] = IdentityGcpAuthResourceTrustedIps{IpAddress: types.StringValue(
|
|
el.IpAddress,
|
|
)}
|
|
}
|
|
}
|
|
|
|
stateAccessTokenTrustedIps, diags := types.ListValueFrom(ctx, types.ObjectType{
|
|
AttrTypes: map[string]attr.Type{
|
|
"ip_address": types.StringType,
|
|
},
|
|
}, planAccessTokenTrustedIps)
|
|
diagnose.Append(diags...)
|
|
if diagnose.HasError() {
|
|
return
|
|
}
|
|
|
|
plan.AllowedServiceAccountEmails, diags = types.ListValueFrom(ctx, types.StringType, kmsstrings.StringSplitAndTrim(newIdentityGcpAuth.AllowedServiceAccounts, ","))
|
|
diagnose.Append(diags...)
|
|
if diagnose.HasError() {
|
|
return
|
|
}
|
|
|
|
plan.AllowedProjects, diags = types.ListValueFrom(ctx, types.StringType, kmsstrings.StringSplitAndTrim(newIdentityGcpAuth.AllowedProjects, ","))
|
|
diagnose.Append(diags...)
|
|
if diagnose.HasError() {
|
|
return
|
|
}
|
|
|
|
plan.AllowedZones, diags = types.ListValueFrom(ctx, types.StringType, kmsstrings.StringSplitAndTrim(newIdentityGcpAuth.AllowedZones, ","))
|
|
diagnose.Append(diags...)
|
|
if diagnose.HasError() {
|
|
return
|
|
}
|
|
|
|
plan.AccessTokenTrustedIps = stateAccessTokenTrustedIps
|
|
}
|
|
|
|
// Create creates the resource and sets the initial Terraform state.
|
|
func (r *IdentityGcpAuthResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
if !r.client.Config.IsMachineIdentityAuth {
|
|
resp.Diagnostics.AddError(
|
|
"Unable to create identity gcp auth",
|
|
"Only Machine Identity authentication is supported for this operation",
|
|
)
|
|
return
|
|
}
|
|
|
|
// Retrieve values from plan
|
|
var plan IdentityGcpAuthResourceModel
|
|
diags := req.Plan.Get(ctx, &plan)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
accessTokenTrustedIps := tfPlanExpandIpFieldAsApiField(ctx, resp.Diagnostics, plan.AccessTokenTrustedIps)
|
|
allowedProjects := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedProjects)
|
|
allowedZones := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedZones)
|
|
allowedServiceAccounts := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedServiceAccountEmails)
|
|
|
|
newIdentityGcpAuth, err := r.client.CreateIdentityGcpAuth(kmsclient.CreateIdentityGcpAuthRequest{
|
|
IdentityID: plan.IdentityID.ValueString(),
|
|
AccessTokenTTL: plan.AccessTokenTTL.ValueInt64(),
|
|
AccessTokenMaxTTL: plan.AccessTokenMaxTTL.ValueInt64(),
|
|
AccessTokenNumUsesLimit: plan.AccessTokenNumUsesLimit.ValueInt64(),
|
|
Type: plan.Type.ValueString(),
|
|
AllowedServiceAccounts: strings.Join(allowedServiceAccounts, ","),
|
|
AllowedProjects: strings.Join(allowedProjects, ","),
|
|
AccessTokenTrustedIPS: accessTokenTrustedIps,
|
|
AllowedZones: strings.Join(allowedZones, ","),
|
|
})
|
|
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error creating identity gcp auth",
|
|
"Couldn't save gcp auth to Infiscial, unexpected error: "+err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
plan.ID = types.StringValue(newIdentityGcpAuth.ID)
|
|
updateGcpAuthStateByApi(ctx, resp.Diagnostics, &plan, &newIdentityGcpAuth)
|
|
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 *IdentityGcpAuthResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
if !r.client.Config.IsMachineIdentityAuth {
|
|
resp.Diagnostics.AddError(
|
|
"Unable to read identity gcp auth role",
|
|
"Only Machine Identity authentication is supported for this operation",
|
|
)
|
|
return
|
|
}
|
|
|
|
// Get current state
|
|
var state IdentityGcpAuthResourceModel
|
|
diags := req.State.Get(ctx, &state)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Get the latest data from the API
|
|
identityGcpAuth, err := r.client.GetIdentityGcpAuth(kmsclient.GetIdentityGcpAuthRequest{
|
|
IdentityID: state.IdentityID.ValueString(),
|
|
})
|
|
|
|
if err != nil {
|
|
if err == kmsclient.ErrNotFound {
|
|
resp.State.RemoveResource(ctx)
|
|
return
|
|
} else {
|
|
resp.Diagnostics.AddError(
|
|
"Error reading identity gcp auth",
|
|
"Couldn't read identity gcp auth from Infiscial, unexpected error: "+err.Error(),
|
|
)
|
|
return
|
|
}
|
|
}
|
|
|
|
updateGcpAuthStateByApi(ctx, resp.Diagnostics, &state, &identityGcpAuth)
|
|
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 *IdentityGcpAuthResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
if !r.client.Config.IsMachineIdentityAuth {
|
|
resp.Diagnostics.AddError(
|
|
"Unable to update identity gcp auth",
|
|
"Only Machine Identity authentication is supported for this operation",
|
|
)
|
|
return
|
|
}
|
|
|
|
// Retrieve values from plan
|
|
var plan IdentityGcpAuthResourceModel
|
|
diags := req.Plan.Get(ctx, &plan)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
var state IdentityGcpAuthResourceModel
|
|
diags = req.State.Get(ctx, &state)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
accessTokenTrustedIps := tfPlanExpandIpFieldAsApiField(ctx, resp.Diagnostics, plan.AccessTokenTrustedIps)
|
|
allowedProjects := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedProjects)
|
|
allowedZones := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedZones)
|
|
allowedServiceAccounts := terraform.StringListToGoStringSlice(ctx, resp.Diagnostics, plan.AllowedServiceAccountEmails)
|
|
updatedIdentityGcpAuth, err := r.client.UpdateIdentityGcpAuth(kmsclient.UpdateIdentityGcpAuthRequest{
|
|
IdentityID: plan.IdentityID.ValueString(),
|
|
AccessTokenTTL: plan.AccessTokenTTL.ValueInt64(),
|
|
AccessTokenMaxTTL: plan.AccessTokenMaxTTL.ValueInt64(),
|
|
AccessTokenNumUsesLimit: plan.AccessTokenNumUsesLimit.ValueInt64(),
|
|
Type: plan.Type.ValueString(),
|
|
AllowedServiceAccounts: strings.Join(allowedServiceAccounts, ","),
|
|
AllowedProjects: strings.Join(allowedProjects, ","),
|
|
AccessTokenTrustedIPS: accessTokenTrustedIps,
|
|
AllowedZones: strings.Join(allowedZones, ","),
|
|
})
|
|
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error updating identity gcp auth",
|
|
"Couldn't update identity gcp auth from Infiscial, unexpected error: "+err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
updateGcpAuthStateByApi(ctx, resp.Diagnostics, &plan, &updatedIdentityGcpAuth)
|
|
|
|
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 *IdentityGcpAuthResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
|
|
if !r.client.Config.IsMachineIdentityAuth {
|
|
resp.Diagnostics.AddError(
|
|
"Unable to delete identity gcp auth",
|
|
"Only Machine Identity authentication is supported for this operation",
|
|
)
|
|
return
|
|
}
|
|
|
|
var state IdentityGcpAuthResourceModel
|
|
diags := req.State.Get(ctx, &state)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
_, err := r.client.RevokeIdentityGcpAuth(kmsclient.RevokeIdentityGcpAuthRequest{
|
|
IdentityID: state.IdentityID.ValueString(),
|
|
})
|
|
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error deleting identity gcp auth",
|
|
"Couldn't delete identity gcp auth from Infiscial, unexpected error: "+err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
}
|