Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality for Verifying Full Provider Addresses (hostname/namespace/type) #10

Closed
bflad opened this issue Apr 22, 2022 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@bflad
Copy link
Member

bflad commented Apr 22, 2022

Use Case

Downstream in Terraform Provider SDKs such as terraform-plugin-go, terraform-plugin-framework, and terraform-plugin-sdk, it would be great if we could verify provider developer defined strings that should only contain a full provider address, e.g. "registry.terraform.io/hashicorp/example". We want to ensure a full provider address is submitted as this value can eventually wind up in Terraform reattach configurations.

This module currently provides the ParseAndInferProviderSourceString() and ParseRawProviderSourceString() functions, however they allow a few things we don't desire:

  • Missing hostname
  • Missing namespace
  • Namespace being - (which is valid for legacy reasons when the hostname is registry.terraform.io, but not something we want to allow for this use case)

Attempted Solutions

Manual logic:

// Validate a given provider address. This is only used for the Address field
// to preserve backwards compatibility for the Name field.
//
// This logic is manually implemented over importing
// github.com/hashicorp/terraform-registry-address as its functionality such as
// ParseAndInferProviderSourceString and ParseRawProviderSourceString allow
// shorter address formats, which would then require post-validation anyways.
func (opts ServeOpts) validateAddress(_ context.Context) error {
	addressParts := strings.Split(opts.Address, "/")
	formatErr := fmt.Errorf("expected hostname/namespace/type format, got: %s", opts.Address)

	if len(addressParts) != 3 {
		return formatErr
	}

	if addressParts[0] == "" || addressParts[1] == "" || addressParts[2] == "" {
		return formatErr
	}

	return nil
}

Proposal

Implement a function in this Go module similar to the above, potentially with better validation. 😄 e.g.

func ValidateFullProviderAddress(in string) error {
	addressParts := strings.Split(in, "/")
	formatErr := fmt.Errorf("expected hostname/namespace/type format, got: %s", in)

	if len(addressParts) != 3 {
		return formatErr
	}

	provider := Provider{
		Hostname:  addressParts[0],
		Namespace: addressParts[1],
		Type:      addressParts[2],
	}

	if provider.Hostname == "" || provider.Namespace == "" || provider.Type == "" {
		return formatErr
	}

	if provider.Namespace == "-" {
		return fmt.Errorf("expected defined namespace, the - namespace is reserved, got: %s", in)
	}

	return nil
}

References

@bflad bflad added the enhancement New feature or request label Apr 22, 2022
@bflad
Copy link
Member Author

bflad commented Apr 22, 2022

If there are any restrictions on provider type names in general (or really, any part of the address), such as disallowed characters, it'd definitely be good to validate that as well. It might be good to separate the hostname/namespace/type validations into their own functions, which this one can call after checking the overall format.

@radeksimko
Copy link
Member

This was implemented as part of #12

Feedback is welcomed in any new issue also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants