/
post_instance_state_file.go
75 lines (70 loc) · 2.75 KB
/
post_instance_state_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package daemonapi
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/labstack/echo/v4"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/resource"
"github.com/opensvc/om3/daemon/rbac"
)
func (a DaemonAPI) PostInstanceStateFile(ctx echo.Context, nodename, namespace string, kind naming.Kind, name string) error {
if nodename == a.localhost || nodename == "localhost" {
return a.postLocalObjectStateFile(ctx, namespace, kind, name)
}
relativePath := ctx.Request().Header.Get("x-relative-path")
return a.proxy(ctx, nodename, func(c *client.T) (*http.Response, error) {
addHeader := func(ctx context.Context, req *http.Request) error {
req.Header.Add("x-relative-path", relativePath)
return nil
}
return c.PostInstanceStateFileWithBody(ctx.Request().Context(), nodename, namespace, kind, name, "application/octet-stream", ctx.Request().Body, addHeader)
})
}
func (a DaemonAPI) postLocalObjectStateFile(ctx echo.Context, namespace string, kind naming.Kind, name string) error {
if v, err := assertGrant(ctx, rbac.GrantRoot); !v {
return err
}
p, err := naming.NewPath(namespace, kind, name)
if err != nil {
return JSONProblemf(ctx, http.StatusBadRequest, "Bad request path", fmt.Sprint(err))
}
if !p.Exists() {
return JSONProblemf(ctx, http.StatusNotFound, "Object not found", "")
}
relPath := ctx.Request().Header.Get("x-relative-path")
if relPath == "" {
return JSONProblemf(ctx, http.StatusBadRequest, "Bad request", "Header 'x-relative-path' is required")
}
o, err := object.NewActor(p)
if err != nil {
return JSONProblemf(ctx, http.StatusInternalServerError, "New object", "%s", err)
}
headPath := o.(resource.ObjectDriver).VarDir()
joinedPath := filepath.Join(headPath, relPath)
joinedPath = filepath.Clean(joinedPath)
if !filepath.HasPrefix(joinedPath, headPath) {
return JSONProblemf(ctx, http.StatusBadRequest, "Join file path", "The path '%s' is outside the allowed head path '%s'", joinedPath, headPath)
}
file, err := os.OpenFile(joinedPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(filepath.Dir(joinedPath), 0750); err != nil {
return JSONProblemf(ctx, http.StatusInternalServerError, "Make state file directory", "%s", err)
}
file, err = os.OpenFile(joinedPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
}
if err != nil {
return JSONProblemf(ctx, http.StatusInternalServerError, "Write state file", "%s", err)
}
defer file.Close()
if _, err := io.Copy(file, ctx.Request().Body); err != nil {
return JSONProblemf(ctx, http.StatusInternalServerError, "Copy body to state file", "%s", err)
}
return ctx.NoContent(http.StatusNoContent)
}