986aecd28f
Model the provider on terraform-provider-giteavaultsecret, adjusting the schemas to the ghp engine (vault-plugin-secrets-ghp) so its mount, config, and roles can be managed declaratively. - Add provider (local name ghpvaultsecret, source git.unkin.net/unkin/ghpvaultsecret) with VAULT_ADDR/VAULT_TOKEN fallback. - Add ghpvaultsecret_secret_backend: mounts the engine and writes config (base_url, write-only admin_token, write-only ca_cert, tls_skip_verify, request_timeout_seconds); read never returns the sensitive fields. - Add ghpvaultsecret_secret_role: token_type, installation_id, app_record_id, repositories, scopes, session_prefix, ttl, max_ttl; validate that agent roles set installation_id. - Add unit tests for the value conversions and the role/backend field mapping. - Mirror the woodpecker pre-commit/build/test (PR) and tag release (package + PUT zip to the artifactapi terraform registry) pipelines, Makefile version bump/package targets, examples, README, and a Docker e2e harness.
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-framework/provider"
|
|
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
var _ provider.Provider = &ghpProvider{}
|
|
|
|
type ghpProvider struct {
|
|
version string
|
|
}
|
|
|
|
type ghpProviderModel struct {
|
|
Address types.String `tfsdk:"address"`
|
|
Token types.String `tfsdk:"token"`
|
|
}
|
|
|
|
func New(version string) func() provider.Provider {
|
|
return func() provider.Provider {
|
|
return &ghpProvider{version: version}
|
|
}
|
|
}
|
|
|
|
func (p *ghpProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
|
|
// Source address is git.unkin.net/unkin/ghpvaultsecret; resources are
|
|
// prefixed "ghpvaultsecret_" (declare it under the local name
|
|
// "ghpvaultsecret" in required_providers).
|
|
resp.TypeName = "ghpvaultsecret"
|
|
resp.Version = p.version
|
|
}
|
|
|
|
func (p *ghpProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Manage the ghp token secrets engine (config and roles) on HashiCorp Vault or OpenBao.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"address": schema.StringAttribute{
|
|
Description: "Address of the Vault/OpenBao server. Falls back to the VAULT_ADDR environment variable.",
|
|
Optional: true,
|
|
},
|
|
"token": schema.StringAttribute{
|
|
Description: "Token used to authenticate to Vault/OpenBao. Falls back to the VAULT_TOKEN environment variable.",
|
|
Optional: true,
|
|
Sensitive: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *ghpProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
|
var config ghpProviderModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
address := os.Getenv("VAULT_ADDR")
|
|
if !config.Address.IsNull() && config.Address.ValueString() != "" {
|
|
address = config.Address.ValueString()
|
|
}
|
|
|
|
token := os.Getenv("VAULT_TOKEN")
|
|
if !config.Token.IsNull() && config.Token.ValueString() != "" {
|
|
token = config.Token.ValueString()
|
|
}
|
|
|
|
if address == "" {
|
|
resp.Diagnostics.AddError(
|
|
"missing Vault address",
|
|
"Set the provider \"address\" attribute or the VAULT_ADDR environment variable.",
|
|
)
|
|
return
|
|
}
|
|
|
|
client, err := newVaultClient(address, token)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("failed to create Vault client", err.Error())
|
|
return
|
|
}
|
|
|
|
resp.DataSourceData = client
|
|
resp.ResourceData = client
|
|
}
|
|
|
|
func (p *ghpProvider) Resources(_ context.Context) []func() resource.Resource {
|
|
return []func() resource.Resource{
|
|
NewSecretBackendResource,
|
|
NewSecretRoleResource,
|
|
}
|
|
}
|
|
|
|
func (p *ghpProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
|
return nil
|
|
}
|