From d8db2bc65bab8705889d88b7fbc8fb729d5ffb75 Mon Sep 17 00:00:00 2001 From: Spencer Smith Date: Wed, 23 Oct 2019 15:47:55 -0400 Subject: [PATCH] feat: detect gzipped machine configs This PR will add the ability for talos to detect if the machine config that it downloads from the platform is a gzipped file. If so, it will unzip it and overwrite the byte slice that gets written to disk. Signed-off-by: Spencer Smith --- .../machined/internal/phase/config/config.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/app/machined/internal/phase/config/config.go b/internal/app/machined/internal/phase/config/config.go index 4f00534da..012920415 100644 --- a/internal/app/machined/internal/phase/config/config.go +++ b/internal/app/machined/internal/phase/config/config.go @@ -5,7 +5,11 @@ package config import ( + "bytes" + "compress/gzip" + "fmt" "io/ioutil" + "net/http" "github.com/talos-systems/talos/internal/app/machined/internal/phase" "github.com/talos-systems/talos/internal/pkg/runtime" @@ -32,5 +36,24 @@ func (task *Task) standard(r runtime.Runtime) (err error) { return err } + // Detect if config is a gzip archive and unzip it if so + contentType := http.DetectContentType(b) + if contentType == "application/x-gzip" { + gzipReader, err := gzip.NewReader(bytes.NewReader(b)) + if err != nil { + return fmt.Errorf("error creating gzip reader: %w", err) + } + + // nolint: errcheck + defer gzipReader.Close() + + unzippedData, err := ioutil.ReadAll(gzipReader) + if err != nil { + return fmt.Errorf("error unzipping machine config: %w", err) + } + + b = unzippedData + } + return ioutil.WriteFile(constants.ConfigPath, b, 0600) }