From 1f1e28f27f059230b9c244e0f93e0999cec41f46 Mon Sep 17 00:00:00 2001 From: Aleksander Zaruczewski Date: Fri, 3 Nov 2023 14:50:04 +0200 Subject: [PATCH] fix(schemautil): unmarshalling empty userconfig crash (#1425) --- CHANGELOG.md | 2 ++ Makefile | 12 ++++++++++-- internal/schemautil/schemautil.go | 5 ++++- main.go | 12 +++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a25f01f5..48db73ac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ nav_order: 1 ## [X.Y.Z] - YYYY-MM-DD +- Fix unmarshalling empty userconfig crash + ## [4.9.3] - 2023-10-27 - Deprecating `project_user`, `account_team` and `account_team_member` resources diff --git a/Makefile b/Makefile index 85fa601ea..1ee152413 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build build-dev test test-unit test-acc test-examples lint lint-go lint-test lint-docs fmt fmt-test fmt-imports clean clean-tools clean-examples sweep generate gen-go docs +.PHONY: build build-dev debug test test-unit test-acc test-examples lint lint-go lint-test lint-docs fmt fmt-test fmt-imports clean clean-tools clean-examples sweep generate gen-go docs ################################################# # Global @@ -34,6 +34,7 @@ $(TERRAFMT): $(TOOLS_BIN_DIR) $(TOOLS_DIR)/go.mod # See https://github.com/hashicorp/terraform/blob/main/tools/protobuf-compile/protobuf-compile.go#L215 ARCH ?= $(shell $(GO) env GOOS GOARCH | tr '\n' '_' | sed '$$s/_$$//') BUILD_DEV_DIR ?= ~/.terraform.d/plugins/registry.terraform.io/aiven-dev/aiven/0.0.0+dev/$(ARCH) +BUILD_DEV_BIN ?= $(BUILD_DEV_DIR)/terraform-provider-aiven_v0.0.0+dev $(BUILD_DEV_DIR): mkdir -p $(BUILD_DEV_DIR) @@ -57,7 +58,14 @@ build: # } #} build-dev: $(BUILD_DEV_DIR) - $(GO) build -o $(BUILD_DEV_DIR)/terraform-provider-aiven_v0.0.0+dev + $(GO) build -gcflags='all=-N -l' -o $(BUILD_DEV_BIN) + +################################################# +# Debug +################################################# + +debug: build-dev + $(BUILD_DEV_BIN) -debug ################################################# # Test diff --git a/internal/schemautil/schemautil.go b/internal/schemautil/schemautil.go index 0ec28177a..06b39a90f 100644 --- a/internal/schemautil/schemautil.go +++ b/internal/schemautil/schemautil.go @@ -325,7 +325,10 @@ func unmarshalUserConfig(src interface{}) ([]map[string]interface{}, error) { return nil, fmt.Errorf("%w: expected []interface{}", errInvalidStateType) } - if len(configList) == 0 { + // For some reason, it looks like this is never empty, even if the user config is not set. + // We will keep this check here just in case, but the actual check that breaks the code is + // the one where we check if the first element is nil. + if len(configList) == 0 || configList[0] == nil { return nil, nil } diff --git a/main.go b/main.go index d82346f09..2f4c890b4 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,9 @@ import ( //go:generate go test -tags userconfig ./internal/schemautil/userconfig +// registryPrefix is the registry prefix for the provider. +const registryPrefix = "registry.terraform.io/" + // version is the version of the provider. var version = "dev" @@ -31,8 +34,15 @@ func main() { serveOpts = append(serveOpts, tf6server.WithManagedDebug()) } + name := registryPrefix + "aiven/aiven" + + //goland:noinspection GoBoolExpressions + if version == "dev" { + name = registryPrefix + "aiven-dev/aiven" + } + err = tf6server.Serve( - "registry.terraform.io/aiven/aiven", + name, func() tfprotov6.ProviderServer { return muxServer },