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

feat: add validation for allowed OS and Arch combinations #1103

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/brokerpak-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ Fields marked with `*` are required, others are optional.

#### Platform object

The platform OS and architecture follow Go's naming scheme.
The platform OS and architecture follow Go's naming scheme. Combinations of OS and architecture for which there is an [OpenTofu release](https://github.com/opentofu/opentofu/releases) are valid.

| Field | Type | Description | Valid Values |
|-------|--------|---------------------------------------|-------------------|
| os* | string | The operating system of the platform. | `linux`, `darwin` |
| arch* | string | The architecture of the platform. | `"386"`, `amd64` |
| Field | Type | Description | Valid Values |
|-------|--------|---------------------------------------|----------------------------------------------------------------|
| os* | string | The operating system of the platform. | `darwin`, `freebsd`, `linux`, `openbsd`, `solaris`, `windows` |
| arch* | string | The architecture of the platform. | `386`, `amd64`, `arm`, `arm64` |
Fields marked with `*` are required, others are optional.

#### OpenTofu resource object
Expand Down
38 changes: 38 additions & 0 deletions internal/brokerpak/manifest/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,44 @@ var _ = Describe("Parser", func() {
Entry("arch", "platforms[2].arch", map[string]any{"os": "linux"}),
)

DescribeTable("valid platform object",
func(value platform.Platform) {
m, err := manifest.Parse(fakeManifest(with("platforms", []platform.Platform{value})))

Expect(err).To(BeNil())
Expect(m).ToNot(BeNil())
},

Entry("darwin/amd64", platform.Platform{Os: "darwin", Arch: "amd64"}),
Entry("darwin/arm64", platform.Platform{Os: "darwin", Arch: "arm64"}),
Entry("freebsd/386", platform.Platform{Os: "freebsd", Arch: "386"}),
Entry("freebsd/amd64", platform.Platform{Os: "freebsd", Arch: "amd64"}),
Entry("freebsd/arm", platform.Platform{Os: "freebsd", Arch: "arm"}),
Entry("linux/386", platform.Platform{Os: "linux", Arch: "386"}),
Entry("linux/amd64", platform.Platform{Os: "linux", Arch: "amd64"}),
Entry("linux/arm", platform.Platform{Os: "linux", Arch: "arm"}),
Entry("linux/arm64", platform.Platform{Os: "linux", Arch: "arm64"}),
Entry("openbsd/386", platform.Platform{Os: "openbsd", Arch: "386"}),
Entry("openbsd/amd64", platform.Platform{Os: "openbsd", Arch: "amd64"}),
Entry("solaris/amd64", platform.Platform{Os: "solaris", Arch: "amd64"}),
Entry("windows/386", platform.Platform{Os: "windows", Arch: "386"}),
Entry("windows/amd64", platform.Platform{Os: "windows", Arch: "amd64"}),
)

DescribeTable("invalid platform object",
func(value platform.Platform) {
m, err := manifest.Parse(fakeManifest(with("platforms", []platform.Platform{value})))

Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("invalid value: %v/%v: platforms[0]", value.Os, value.Arch))))
Expect(m).To(BeNil())
},

Entry("darwin/386", platform.Platform{Os: "darwin", Arch: "386"}),
Entry("freebsd/arm64", platform.Platform{Os: "freebsd", Arch: "arm64"}),
Entry("linux/amd", platform.Platform{Os: "linux", Arch: "amd"}),
Entry("windows/arm", platform.Platform{Os: "windows", Arch: "arm64"}),
)

DescribeTable("missing terraform binary data",
func(insert string, value map[string]any) {
m, err := manifest.Parse(fakeManifest(withAdditionalEntry("terraform_binaries", value)))
Expand Down
37 changes: 33 additions & 4 deletions internal/brokerpak/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,41 @@ type Platform struct {

var _ validation.Validatable = (*Platform)(nil)

// allowed OS/Arch combinations
//
// must be possible value for $GOOS and $GOARCH (https://go.dev/doc/install/source#environment)
// there must be an OpenTofu release for the respective OS/ARCH (see brokerpakurl.go)
var (
darwinAmd64 = Platform{Os: "darwin", Arch: "amd64"}
darwinArm64 = Platform{Os: "darwin", Arch: "arm64"}
freebsd386 = Platform{Os: "freebsd", Arch: "386"}
freebsdAmd64 = Platform{Os: "freebsd", Arch: "amd64"}
freebsdArm = Platform{Os: "freebsd", Arch: "arm"}
linux386 = Platform{Os: "linux", Arch: "386"}
linuxAmd64 = Platform{Os: "linux", Arch: "amd64"}
linuxArm = Platform{Os: "linux", Arch: "arm"}
linuxArm64 = Platform{Os: "linux", Arch: "arm64"}
openbsd386 = Platform{Os: "openbsd", Arch: "386"}
openbsdAmd64 = Platform{Os: "openbsd", Arch: "amd64"}
solarisAmd64 = Platform{Os: "solaris", Arch: "amd64"}
windows386 = Platform{Os: "windows", Arch: "386"}
windowsAmd64 = Platform{Os: "windows", Arch: "amd64"}
)

// Validate implements validation.Validatable.
func (p Platform) Validate() (errs *validation.FieldError) {
return errs.Also(
validation.ErrIfBlank(p.Os, "os"),
validation.ErrIfBlank(p.Arch, "arch"),
)

if errs := errs.Also(validation.ErrIfBlank(p.Os, "os"), validation.ErrIfBlank(p.Arch, "arch")); errs != nil {
return errs
}

switch p {
case darwinAmd64, darwinArm64, freebsd386, freebsdAmd64, freebsdArm, linux386, linuxAmd64, linuxArm, linuxArm64, openbsd386, openbsdAmd64, solarisAmd64, windows386, windowsAmd64:
default:
return validation.ErrInvalidValue(p, "")
}

return nil
}

// String formats the platform as an os/arch pair.
Expand Down
Loading