2019-10-27 05:53:25 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-10-07 19:47:00 +00:00
|
|
|
"time"
|
2019-10-27 05:53:25 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-11-10 12:33:42 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-27 05:53:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Handler(bootstrap *config.ControlRuntimeBootstrap) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.Header().Set("Content-Type", "application/json")
|
2021-10-07 19:47:00 +00:00
|
|
|
ReadFromDisk(rw, bootstrap)
|
2019-10-27 05:53:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
// ReadFromDisk reads the bootstrap data from the files on disk and
|
|
|
|
// writes their content in JSON form to the given io.Writer.
|
|
|
|
func ReadFromDisk(w io.Writer, bootstrap *config.ControlRuntimeBootstrap) error {
|
|
|
|
paths, err := ObjToMap(bootstrap)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
dataMap := make(map[string]File)
|
2019-10-27 05:53:25 +00:00
|
|
|
for pathKey, path := range paths {
|
|
|
|
if path == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2021-11-10 12:33:42 +00:00
|
|
|
logrus.Warnf("failed to read %s", path)
|
|
|
|
continue
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dataMap[pathKey] = File{
|
|
|
|
Timestamp: info.ModTime(),
|
|
|
|
Content: data,
|
|
|
|
}
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(w).Encode(dataMap)
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
// File is a representation of a certificate
|
|
|
|
// or key file within the bootstrap context that contains
|
|
|
|
// the contents of the file as well as a timestamp from
|
|
|
|
// when the file was last modified.
|
|
|
|
type File struct {
|
|
|
|
Timestamp time.Time
|
|
|
|
Content []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type PathsDataformat map[string]File
|
|
|
|
|
|
|
|
// WriteToDiskFromStorage writes the contents of the given reader to the paths
|
|
|
|
// derived from within the ControlRuntimeBootstrap.
|
2021-12-13 20:02:30 +00:00
|
|
|
func WriteToDiskFromStorage(files PathsDataformat, bootstrap *config.ControlRuntimeBootstrap) error {
|
2021-10-07 19:47:00 +00:00
|
|
|
paths, err := ObjToMap(bootstrap)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
for pathKey, bsf := range files {
|
2019-10-27 05:53:25 +00:00
|
|
|
path, ok := paths[pathKey]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to mkdir %s", filepath.Dir(path))
|
|
|
|
}
|
2021-12-09 21:54:27 +00:00
|
|
|
if err := os.WriteFile(path, bsf.Content, 0600); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to write to %s", path)
|
|
|
|
}
|
2021-12-09 21:54:27 +00:00
|
|
|
if err := os.Chtimes(path, bsf.Timestamp, bsf.Timestamp); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to update modified time on %s", path)
|
|
|
|
}
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
func ObjToMap(obj interface{}) (map[string]string, error) {
|
2019-10-27 05:53:25 +00:00
|
|
|
bytes, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := map[string]string{}
|
|
|
|
return data, json.Unmarshal(bytes, &data)
|
|
|
|
}
|