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 <robertspencersmith@gmail.com>
This commit is contained in:
Spencer Smith 2019-10-23 15:47:55 -04:00 committed by Andrew Rynhard
parent bccaa36b44
commit d8db2bc65b

View File

@ -5,7 +5,11 @@
package config package config
import ( import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil" "io/ioutil"
"net/http"
"github.com/talos-systems/talos/internal/app/machined/internal/phase" "github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/pkg/runtime" "github.com/talos-systems/talos/internal/pkg/runtime"
@ -32,5 +36,24 @@ func (task *Task) standard(r runtime.Runtime) (err error) {
return err 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) return ioutil.WriteFile(constants.ConfigPath, b, 0600)
} }