Skip to content

Commit

Permalink
Read the first ten lines of a config to check for headers (#51)
Browse files Browse the repository at this point in the history
There could be comments before the cloud-config header or jinja
expressions as we found already[0] so our current header check its a bit
strict.

This patch makes it so er read the first ten lines of the config source,
check for # at the start and then check for the headers as we did
before, so we are a bit more letinent in case the cloud-config header is
not the first thing in the file

Signed-off-by: Itxaka <itxaka@kairos.io>
  • Loading branch information
Itxaka authored Sep 15, 2023
1 parent 5919d1a commit 699aa62
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
26 changes: 19 additions & 7 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,26 @@ func fetchRemoteConfig(url string) (*Config, error) {
}

func HasValidHeader(data string) bool {
header := strings.SplitN(data, "\n", 2)[0]

// Trim trailing whitespaces
header = strings.TrimRightFunc(header, unicode.IsSpace)
// Get the first 10 lines
headers := strings.SplitN(data, "\n", 10)

// iterate over them as there could be comments or the jinja template info:
// https://cloudinit.readthedocs.io/en/latest/explanation/instancedata.html#example-cloud-config-with-instance-data

for _, line := range headers {
// Trim trailing whitespaces
header := strings.TrimRightFunc(line, unicode.IsSpace)
// If it starts with a hash check it, in case its a huge line, we dont want to waste time
if strings.HasPrefix(header, "#") {
// NOTE: we also allow "legacy" headers. Should only allow #cloud-config at
// some point.
if (header == DefaultHeader) || (header == "#kairos-config") || (header == "#node-config") {
return true
}
}
}

// NOTE: we also allow "legacy" headers. Should only allow #cloud-config at
// some point.
return (header == DefaultHeader) || (header == "#kairos-config") || (header == "#node-config")
return false
}

func (c Config) Query(s string) (res string, err error) {
Expand Down
54 changes: 54 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,60 @@ remote_key_2: remote_value_2`), os.ModePerm)
Expect(v).To(Equal("remote_value_1"))
})
})
Context("when files have comments before the headers or jinja declarations", func() {
var tmpDir string
var err error

BeforeEach(func() {
tmpDir, err = os.MkdirTemp("", "config")
Expect(err).ToNot(HaveOccurred())

// Local configs
err = os.WriteFile(path.Join(tmpDir, "local_config.yaml"), []byte(`## template: jinja
#cloud-config
local_key_1: local_value_1
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

// comments before the header
err = os.WriteFile(path.Join(tmpDir, "local_config_2.yaml"),
[]byte(`
# this is a comment
## then another comment
#and the last one
#cloud-config
local_key_2: local_value_2
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())

})

AfterEach(func() {
err = os.RemoveAll(tmpDir)
Expect(err).ToNot(HaveOccurred())
})

It("reads them", func() {
o := &Options{}
err := o.Apply(Directories(tmpDir), NoLogs)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(o, FilterKeysTest)
Expect(err).ToNot(HaveOccurred())

Expect((*c)["local_key_1"]).ToNot(BeNil())
Expect((*c)["local_key_2"]).ToNot(BeNil())

v, ok := (*c)["local_key_1"].(string)
Expect(ok).To(BeTrue())
Expect(v).To(Equal("local_value_1"))

v, ok = (*c)["local_key_2"].(string)
Expect(ok).To(BeTrue())
Expect(v).To(Equal("local_value_2"))
})
})
})

Describe("String", func() {
Expand Down

0 comments on commit 699aa62

Please sign in to comment.