|
| 1 | +package keymanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | + key_manager "github.com/scaleway/scaleway-sdk-go/api/key_manager/v1alpha1" |
| 14 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 15 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
| 16 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 17 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" |
| 18 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + _ ephemeral.EphemeralResource = (*EncryptEphemeralResource)(nil) |
| 23 | + _ ephemeral.EphemeralResourceWithConfigure = (*EncryptEphemeralResource)(nil) |
| 24 | +) |
| 25 | + |
| 26 | +type EncryptEphemeralResource struct { |
| 27 | + keyManagerAPI *key_manager.API |
| 28 | + meta *meta.Meta |
| 29 | +} |
| 30 | + |
| 31 | +func NewEncryptEphemeralResource() ephemeral.EphemeralResource { |
| 32 | + return &EncryptEphemeralResource{} |
| 33 | +} |
| 34 | + |
| 35 | +func (r *EncryptEphemeralResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 36 | + if req.ProviderData == nil { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + m, ok := req.ProviderData.(*meta.Meta) |
| 41 | + if !ok { |
| 42 | + resp.Diagnostics.AddError( |
| 43 | + "Unexpected Ephemeral Resource Configure Type", |
| 44 | + fmt.Sprintf("Expected *meta.Meta, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 45 | + ) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + client := m.ScwClient() |
| 50 | + r.keyManagerAPI = key_manager.NewAPI(client) |
| 51 | + r.meta = m |
| 52 | +} |
| 53 | + |
| 54 | +func (r *EncryptEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 55 | + resp.TypeName = req.ProviderTypeName + "_key_manager_encrypt" |
| 56 | +} |
| 57 | + |
| 58 | +type EncryptEphemeralResourceModel struct { |
| 59 | + Region types.String `tfsdk:"region"` |
| 60 | + KeyID types.String `tfsdk:"key_id"` |
| 61 | + Plaintext types.String `tfsdk:"plaintext"` |
| 62 | + AssociatedData types.Object `tfsdk:"associated_data"` |
| 63 | + // Output |
| 64 | + Ciphertext types.String `tfsdk:"ciphertext"` |
| 65 | +} |
| 66 | + |
| 67 | +type AssociatedDataModel struct { |
| 68 | + Value types.String `tfsdk:"value"` |
| 69 | +} |
| 70 | + |
| 71 | +func (r *EncryptEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 72 | + resp.Schema = schema.Schema{ |
| 73 | + Attributes: map[string]schema.Attribute{ |
| 74 | + "region": regional.SchemaAttribute("Region of the key. If not set, the region is derived from the key_id when possible or from the provider configuration."), |
| 75 | + "key_id": schema.StringAttribute{ |
| 76 | + Required: true, |
| 77 | + Description: "ID of the key to use for encryption. Can be a plain UUID or a regional ID.", |
| 78 | + Validators: []validator.String{ |
| 79 | + verify.IsStringUUIDOrUUIDWithLocality(), |
| 80 | + }, |
| 81 | + }, |
| 82 | + "plaintext": schema.StringAttribute{ |
| 83 | + Required: true, |
| 84 | + Description: "Plaintext data to encrypt. Data size must be between 1 and 65535 bytes.", |
| 85 | + Sensitive: true, |
| 86 | + }, |
| 87 | + "associated_data": schema.ObjectAttribute{ |
| 88 | + Optional: true, |
| 89 | + Description: "Additional authenticated data. Additional data which will not be encrypted, but authenticated and appended to the encrypted payload. Only supported by keys with a usage set to `symmetric_encryption`.", |
| 90 | + AttributeTypes: map[string]attr.Type{ |
| 91 | + "value": types.StringType, |
| 92 | + }, |
| 93 | + }, |
| 94 | + "ciphertext": schema.StringAttribute{ |
| 95 | + Computed: true, |
| 96 | + Description: "Key's encrypted data.", |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (r *EncryptEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 103 | + var data EncryptEphemeralResourceModel |
| 104 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 105 | + |
| 106 | + if resp.Diagnostics.HasError() { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + if r.keyManagerAPI == nil { |
| 111 | + resp.Diagnostics.AddError( |
| 112 | + "Unconfigured keymanagerAPI", |
| 113 | + "The ephemeral resource was not properly configured. The Scaleway client is missing. "+ |
| 114 | + "This is usually a bug in the provider. Please report it to the maintainers.", |
| 115 | + ) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + keyID := locality.ExpandID(data.KeyID.ValueString()) |
| 120 | + plaintext := data.Plaintext.ValueString() |
| 121 | + |
| 122 | + var region scw.Region |
| 123 | + var err error |
| 124 | + |
| 125 | + if !data.Region.IsNull() && data.Region.ValueString() != "" { |
| 126 | + region = scw.Region(data.Region.ValueString()) |
| 127 | + } else { |
| 128 | + // Try to derive region from the key_id if it is a regional ID |
| 129 | + if derivedRegion, id, parseErr := regional.ParseID(keyID); parseErr == nil { |
| 130 | + region = derivedRegion |
| 131 | + keyID = id |
| 132 | + } else { |
| 133 | + // Use default region from provider configuration |
| 134 | + defaultRegion, exists := r.meta.ScwClient().GetDefaultRegion() |
| 135 | + if !exists { |
| 136 | + resp.Diagnostics.AddError( |
| 137 | + "Missing region", |
| 138 | + "The region attribute is required to encrypt with a key. Please provide it explicitly or configure a default region in the provider.", |
| 139 | + ) |
| 140 | + return |
| 141 | + } |
| 142 | + region = defaultRegion |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + var associatedData []byte |
| 147 | + |
| 148 | + if !data.AssociatedData.IsNull() && !data.AssociatedData.IsUnknown() { |
| 149 | + var assocDataModel AssociatedDataModel |
| 150 | + diags := data.AssociatedData.As(ctx, &assocDataModel, basetypes.ObjectAsOptions{ |
| 151 | + UnhandledNullAsEmpty: true, |
| 152 | + UnhandledUnknownAsEmpty: true, |
| 153 | + }) |
| 154 | + resp.Diagnostics.Append(diags...) |
| 155 | + if resp.Diagnostics.HasError() { |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + associatedData = []byte(assocDataModel.Value.ValueString()) |
| 160 | + } |
| 161 | + |
| 162 | + encryptReq := &key_manager.EncryptRequest{ |
| 163 | + Region: region, |
| 164 | + KeyID: keyID, |
| 165 | + Plaintext: []byte(plaintext), |
| 166 | + AssociatedData: &associatedData, |
| 167 | + } |
| 168 | + |
| 169 | + encryptResp, err := r.keyManagerAPI.Encrypt(encryptReq) |
| 170 | + if err != nil { |
| 171 | + resp.Diagnostics.AddError( |
| 172 | + "Error executing Key Manager Encrypt action", |
| 173 | + fmt.Sprintf("%s", err), |
| 174 | + ) |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + data.Ciphertext = types.StringValue(string(encryptResp.Ciphertext)) |
| 179 | + |
| 180 | + resp.Result.Set(ctx, &data) |
| 181 | +} |
0 commit comments