bug: Fix validator on long strings (#1194)

* 🐛 Fix validator on long strings

Validator was mistakenly identifying a long yaml as a file and trying to
open it, which failed with an error of filename too long.

This was not catched in order to identify that the source is not a file
but a yaml, so it was directly returning the error.

This patch adds that error to the list in order ot identify the source
to validate as yaml. Also adds a couple of tests for this functionality.

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>

* 🐛 Merge initramfs generation between distros

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>

---------

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka
2023-03-28 18:20:56 +02:00
parent 148fc21c0e
commit 107a4ab02f
2 changed files with 26 additions and 7 deletions

View File

@@ -2,19 +2,19 @@ package agent
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"strings" "strings"
schema "github.com/kairos-io/kairos/pkg/config/schemas" sc "github.com/kairos-io/kairos/pkg/config/schemas"
) )
// JSONSchema builds a JSON Schema based on the Root Schema and the given version // JSONSchema builds a JSON Schema based on the Root Schema and the given version
// this is helpful when mapping a validation error. // this is helpful when mapping a validation error.
func JSONSchema(version string) (string, error) { func JSONSchema(version string) (string, error) {
url := fmt.Sprintf("https://kairos.io/%s/cloud-config.json", version) url := fmt.Sprintf("https://kairos.io/%s/cloud-config.json", version)
schema, err := schema.GenerateSchema(schema.RootSchema{}, url) schema, err := sc.GenerateSchema(sc.RootSchema{}, url)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -31,16 +31,17 @@ func Validate(source string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }
//Convert the body to type string //Convert the body to type string
yaml = string(body) yaml = string(body)
} else { } else {
// Maybe we should just try to read the string for the normal headers? That would identify a full yaml vs a file
dat, err := os.ReadFile(source) dat, err := os.ReadFile(source)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "no such file or directory") { if strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "file name too long") {
yaml = source yaml = source
} else { } else {
return err return err
@@ -50,7 +51,7 @@ func Validate(source string) error {
} }
} }
config, err := schema.NewConfigFromYAML(yaml, schema.RootSchema{}) config, err := sc.NewConfigFromYAML(yaml, sc.RootSchema{})
if err != nil { if err != nil {
return err return err
} }

View File

@@ -24,6 +24,21 @@ var _ = Describe("Validate", func() {
Context("Validate", func() { Context("Validate", func() {
var yaml string var yaml string
Context("With a really long config string", func() {
BeforeEach(func() {
yaml = `#cloud-config
users:
- name: kairos
passwd: kairos
vpn:
network_token: "dssdnfjkldashfkjhasdkhfkasjdhfkjhasdjkfhaksjdhfkjashjdkfhioreqwhfuihqweruifhuewrbfhuewrfuyequfhuiehuifheqrihfuiqrehfuirqheiufhreqiuhfuiqheiufhqeuihfuiqrehfiuhqreuifrhiuqehfiuhqeirhfiuewhrfhqwehfriuewhfuihewiuhfruewhrifhwiuehrfiuhweiurfhwueihrfuiwehufhweuihrfuiwerhfuihewruifhewuihfiouwehrfiouhwei"
`
})
It("validates", func() {
Expect(Validate(yaml)).ToNot(HaveOccurred())
})
})
Context("with a valid config", func() { Context("with a valid config", func() {
BeforeEach(func() { BeforeEach(func() {
yaml = `#cloud-config yaml = `#cloud-config
@@ -32,7 +47,7 @@ users:
passwd: kairos` passwd: kairos`
}) })
It("is successful", func() { It("is successful reading it from file", func() {
f, err := ioutil.TempDir("", "tests") f, err := ioutil.TempDir("", "tests")
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(f) defer os.RemoveAll(f)
@@ -43,6 +58,9 @@ users:
err = Validate(path) err = Validate(path)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("is successful reading it from a string", func() {
Expect(Validate(yaml)).ToNot(HaveOccurred())
})
}) })
Context("without a header", func() { Context("without a header", func() {