@synopackageland/cli 0.52.8 → 0.52.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/assets/go/compose-admin/go.mod +22 -0
- package/assets/go/compose-admin/go.sum +40 -0
- package/assets/go/compose-admin/main.go +443 -0
- package/assets/go/compose-admin/main_test.go +173 -0
- package/dist/build-spk/resource-worker.d.ts +3 -0
- package/dist/build-spk/resource-worker.js +20 -1
- package/dist/cli.js +9 -0
- package/dist/host/catalog-install.d.ts +1 -0
- package/dist/host/catalog-install.js +113 -6
- package/dist/host/compose-admin-build.d.ts +6 -0
- package/dist/host/compose-admin-build.js +17 -0
- package/dist/host/compose-admin.d.ts +22 -0
- package/dist/host/compose-admin.js +119 -0
- package/dist/host/compose-project-recovery.d.ts +2 -0
- package/dist/host/compose-project-recovery.js +2 -0
- package/dist/host/package-commands.d.ts +1 -1
- package/dist/host/package-commands.js +32 -2
- package/dist/load-env.js +1 -1
- package/dist/native-http-schema/source.json +1 -1
- package/dist/runtime/sdk-source.d.ts +1 -1
- package/dist/runtime/sdk-source.js +1 -1
- package/dist/standalone/inspect-spk.js +2 -1
- package/docs/compose-admin.md +82 -0
- package/package.json +3 -2
- package/schema/dsm-api-contract.json +23 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.52.9
|
|
4
|
+
|
|
5
|
+
- Pin CLI-owned SDK defaults and the Compose administrator adapter to 0.53.11
|
|
6
|
+
- Keep Compose recreation conditional on image/config changes
|
|
7
|
+
- Keep standard DSM Compose install/upgrade/uninstall available without a native administrator channel, warning about stale profiles and pointing to opt-in setup
|
|
8
|
+
- With a native administrator channel configured, capture installed ownership, stop packages and reconcile stopped containers/profiles before Compose upgrades and uninstall
|
|
9
|
+
- Recover verified docker-project profile failures with at most one same-version start when the native channel is enabled; otherwise retain the original DSM failure with setup guidance
|
|
10
|
+
- Keep configured native channels strict on unknown writes, foreign ownership and unavailable evidence
|
|
11
|
+
- Clean exact orphan profiles only from previously verified inventory, preserving volume and bind data
|
|
12
|
+
- Keep package preinstall/preflight nonroot; document native helper setup and protect its command from project .env files
|
|
13
|
+
|
|
3
14
|
## 0.52.8
|
|
4
15
|
|
|
5
16
|
- Preserve dedicated shared-folder names and recorded upgrade selections when DSM loads the wizard share list
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module synopkgland-cli/compose-admin
|
|
2
|
+
|
|
3
|
+
go 1.26.10
|
|
4
|
+
|
|
5
|
+
require github.com/synopackageland/syno-pkg-land-sdk v0.53.11
|
|
6
|
+
|
|
7
|
+
require (
|
|
8
|
+
github.com/compose-spec/compose-go/v2 v2.16.1 // indirect
|
|
9
|
+
github.com/distribution/reference v0.5.0 // indirect
|
|
10
|
+
github.com/docker/go-connections v0.4.0 // indirect
|
|
11
|
+
github.com/docker/go-units v0.5.0 // indirect
|
|
12
|
+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
|
13
|
+
github.com/mattn/go-shellwords v1.0.12 // indirect
|
|
14
|
+
github.com/opencontainers/go-digest v1.0.0 // indirect
|
|
15
|
+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 // indirect
|
|
16
|
+
github.com/sirupsen/logrus v1.10.1 // indirect
|
|
17
|
+
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
|
|
18
|
+
go.yaml.in/yaml/v4 v4.0.0-rc.4 // indirect
|
|
19
|
+
golang.org/x/sync v0.3.0 // indirect
|
|
20
|
+
golang.org/x/sys v0.44.0 // indirect
|
|
21
|
+
golang.org/x/text v0.14.0 // indirect
|
|
22
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
github.com/compose-spec/compose-go/v2 v2.16.1 h1:xuEQu32ghB2AK023Beumm//K8bz8u1AHC9P0zKp8jlw=
|
|
2
|
+
github.com/compose-spec/compose-go/v2 v2.16.1/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ=
|
|
3
|
+
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
|
|
4
|
+
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
|
5
|
+
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
|
6
|
+
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
|
7
|
+
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
|
|
8
|
+
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
|
9
|
+
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
|
10
|
+
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
|
11
|
+
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
|
12
|
+
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
|
13
|
+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
|
14
|
+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
|
15
|
+
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
|
|
16
|
+
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
|
|
17
|
+
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
|
18
|
+
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
|
19
|
+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
|
|
20
|
+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
|
|
21
|
+
github.com/sirupsen/logrus v1.10.1 h1:xi4336Zh11WpU14fXR6I67V3yaTPQYwRx2WEtHbRg4Q=
|
|
22
|
+
github.com/sirupsen/logrus v1.10.1/go.mod h1:vsQHnG7xzNsxk3NrwboUiWPnIC3dmbjcGPykD7+tiHk=
|
|
23
|
+
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
|
24
|
+
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
|
25
|
+
github.com/synopackageland/syno-pkg-land-sdk v0.53.11 h1:/pOqlOKQazzgjhp9dEJMBlVNmQ9+yzsowss9j9fRGNs=
|
|
26
|
+
github.com/synopackageland/syno-pkg-land-sdk v0.53.11/go.mod h1:JMfenGwAhlVgGUtoSQFA9XRbtN8Zcs+0xdrOaaam+Fg=
|
|
27
|
+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
|
|
28
|
+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
|
|
29
|
+
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
|
30
|
+
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
|
31
|
+
go.yaml.in/yaml/v4 v4.0.0-rc.4 h1:UP4+v6fFrBIb1l934bDl//mmnoIZEDK0idg1+AIvX5U=
|
|
32
|
+
go.yaml.in/yaml/v4 v4.0.0-rc.4/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
|
|
33
|
+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
|
34
|
+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
|
35
|
+
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
|
36
|
+
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
|
37
|
+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
|
38
|
+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
|
39
|
+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
|
40
|
+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
// Administrator-only DSM adapter. Never ship this executable in an App's SPK.
|
|
2
|
+
package main
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"bufio"
|
|
6
|
+
"context"
|
|
7
|
+
"encoding/json"
|
|
8
|
+
"errors"
|
|
9
|
+
"flag"
|
|
10
|
+
"fmt"
|
|
11
|
+
"io"
|
|
12
|
+
"os"
|
|
13
|
+
"os/exec"
|
|
14
|
+
"path/filepath"
|
|
15
|
+
"regexp"
|
|
16
|
+
"slices"
|
|
17
|
+
"strings"
|
|
18
|
+
"syscall"
|
|
19
|
+
|
|
20
|
+
"github.com/synopackageland/syno-pkg-land-sdk/compose"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
var pkgPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_]*$`)
|
|
24
|
+
var idPattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
|
25
|
+
var namePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,254}$`)
|
|
26
|
+
var physicalPattern = regexp.MustCompile(`^/volume[1-9][0-9]*/@appstore/[A-Za-z][A-Za-z0-9_]*$`)
|
|
27
|
+
var callAPI = api
|
|
28
|
+
|
|
29
|
+
var unverified = errors.New("COMPOSE_WORKER_OWNERSHIP_UNVERIFIED")
|
|
30
|
+
|
|
31
|
+
type adapter struct {
|
|
32
|
+
owner compose.WorkerOwner
|
|
33
|
+
id string
|
|
34
|
+
snapshot []compose.WorkerContainer
|
|
35
|
+
lock *os.File
|
|
36
|
+
profileRoot string
|
|
37
|
+
changed bool
|
|
38
|
+
}
|
|
39
|
+
type request struct{ Action, Package, Project, Origin string }
|
|
40
|
+
type response struct {
|
|
41
|
+
OK bool `json:"ok"`
|
|
42
|
+
Data any `json:"data,omitempty"`
|
|
43
|
+
Error string `json:"error,omitempty"`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// synowebapi is the DSM administrator API client, with no HTTP secrets on argv.
|
|
47
|
+
func api(ctx context.Context, name, method string, args map[string]any) (map[string]any, error) {
|
|
48
|
+
argv := []string{"--exec", "api=" + name, "method=" + method, "version=1"}
|
|
49
|
+
for k, v := range args {
|
|
50
|
+
b, e := json.Marshal(v)
|
|
51
|
+
if e != nil {
|
|
52
|
+
return nil, e
|
|
53
|
+
}
|
|
54
|
+
argv = append(argv, k+"="+string(b))
|
|
55
|
+
}
|
|
56
|
+
out, e := exec.CommandContext(ctx, "/usr/syno/bin/synowebapi", argv...).Output()
|
|
57
|
+
if e != nil {
|
|
58
|
+
return nil, errors.New("COMPOSE_ADMIN_API_UNAVAILABLE")
|
|
59
|
+
}
|
|
60
|
+
start := strings.IndexByte(string(out), '{')
|
|
61
|
+
if start < 0 {
|
|
62
|
+
return nil, unverified
|
|
63
|
+
}
|
|
64
|
+
var body struct {
|
|
65
|
+
Success bool
|
|
66
|
+
Data map[string]any
|
|
67
|
+
Error struct{ Code int }
|
|
68
|
+
}
|
|
69
|
+
if json.Unmarshal(out[start:], &body) != nil || !body.Success {
|
|
70
|
+
return nil, fmt.Errorf("COMPOSE_ADMIN_API_FAILED (%d)", body.Error.Code)
|
|
71
|
+
}
|
|
72
|
+
return body.Data, nil
|
|
73
|
+
}
|
|
74
|
+
func str(v any) string { s, _ := v.(string); return s }
|
|
75
|
+
func obj(v any) map[string]any { m, _ := v.(map[string]any); return m }
|
|
76
|
+
func (a *adapter) canonical(path string) string {
|
|
77
|
+
if path == "/var/packages/"+a.owner.Package+"/target/compose" {
|
|
78
|
+
return a.owner.Path
|
|
79
|
+
}
|
|
80
|
+
return path
|
|
81
|
+
}
|
|
82
|
+
func (a *adapter) Project(ctx context.Context, id string) (compose.WorkerProject, error) {
|
|
83
|
+
d, e := callAPI(ctx, "SYNO.Docker.Project", "get", map[string]any{"id": id})
|
|
84
|
+
if e != nil {
|
|
85
|
+
return compose.WorkerProject{}, e
|
|
86
|
+
}
|
|
87
|
+
ids, ok := d["containerIds"].([]any)
|
|
88
|
+
if !ok {
|
|
89
|
+
return compose.WorkerProject{}, unverified
|
|
90
|
+
}
|
|
91
|
+
p := compose.WorkerProject{ID: str(d["id"]), Name: str(d["name"]), Package: str(d["package_name"]), Path: a.canonical(str(d["path"]))}
|
|
92
|
+
p.IsPackage, _ = d["is_package"].(bool)
|
|
93
|
+
// CM's official package worker can omit package_name. The installed,
|
|
94
|
+
// root-owned resource definition attests the exact project/relative path.
|
|
95
|
+
if p.Package == "" && p.IsPackage && p.Name == a.owner.Project && p.Path == a.owner.Path {
|
|
96
|
+
p.Package = a.owner.Package
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for _, v := range ids {
|
|
100
|
+
if !idPattern.MatchString(str(v)) {
|
|
101
|
+
return p, unverified
|
|
102
|
+
}
|
|
103
|
+
p.ContainerIDs = append(p.ContainerIDs, str(v))
|
|
104
|
+
}
|
|
105
|
+
return p, nil
|
|
106
|
+
}
|
|
107
|
+
func (a *adapter) Container(ctx context.Context, id string) (compose.WorkerContainer, error) {
|
|
108
|
+
// Project.get supplies full Docker inspection, including immutable IDs/labels.
|
|
109
|
+
d, e := callAPI(ctx, "SYNO.Docker.Project", "get", map[string]any{"id": a.id})
|
|
110
|
+
if e != nil {
|
|
111
|
+
return compose.WorkerContainer{}, e
|
|
112
|
+
}
|
|
113
|
+
rows, ok := d["containers"].([]any)
|
|
114
|
+
if !ok {
|
|
115
|
+
return compose.WorkerContainer{}, unverified
|
|
116
|
+
}
|
|
117
|
+
var matches []compose.WorkerContainer
|
|
118
|
+
for _, row := range rows {
|
|
119
|
+
v := obj(row)
|
|
120
|
+
if str(v["Id"]) != id {
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
l := obj(obj(v["Config"])["Labels"])
|
|
124
|
+
matches = append(matches, compose.WorkerContainer{ID: id, Name: strings.TrimPrefix(str(v["Name"]), "/"), Project: str(l["com.docker.compose.project"]), Service: str(l["com.docker.compose.service"]), WorkingDir: a.canonical(str(l["com.docker.compose.project.working_dir"])), ConfigFiles: a.canonicalConfig(str(l["com.docker.compose.project.config_files"])), State: str(obj(v["State"])["Status"])})
|
|
125
|
+
}
|
|
126
|
+
if len(matches) != 1 {
|
|
127
|
+
return compose.WorkerContainer{}, unverified
|
|
128
|
+
}
|
|
129
|
+
return matches[0], nil
|
|
130
|
+
}
|
|
131
|
+
func (a *adapter) canonicalConfig(path string) string {
|
|
132
|
+
for _, name := range []string{"docker-compose.yml", "docker-compose.admin.yaml"} {
|
|
133
|
+
if path == "/var/packages/"+a.owner.Package+"/target/compose/"+name {
|
|
134
|
+
return a.owner.Path + "/" + name
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return path
|
|
138
|
+
}
|
|
139
|
+
func readProfile(path string) (string, error) {
|
|
140
|
+
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
|
|
141
|
+
if errors.Is(e, syscall.ENOENT) {
|
|
142
|
+
return "", nil
|
|
143
|
+
}
|
|
144
|
+
if e != nil {
|
|
145
|
+
return "", unverified
|
|
146
|
+
}
|
|
147
|
+
f := os.NewFile(uintptr(fd), path)
|
|
148
|
+
defer f.Close()
|
|
149
|
+
s, e := f.Stat()
|
|
150
|
+
if e != nil {
|
|
151
|
+
return "", unverified
|
|
152
|
+
}
|
|
153
|
+
st, ok := s.Sys().(*syscall.Stat_t)
|
|
154
|
+
if !ok || st.Uid != 0 || st.Nlink != 1 || !s.Mode().IsRegular() || s.Size() > 1<<20 || s.Mode().Perm()&0022 != 0 {
|
|
155
|
+
return "", unverified
|
|
156
|
+
}
|
|
157
|
+
var profile struct {
|
|
158
|
+
ID string `json:"id"`
|
|
159
|
+
}
|
|
160
|
+
d := json.NewDecoder(io.LimitReader(f, 1<<20))
|
|
161
|
+
if d.Decode(&profile) != nil || d.Decode(new(any)) != io.EOF || !idPattern.MatchString(profile.ID) {
|
|
162
|
+
return "", unverified
|
|
163
|
+
}
|
|
164
|
+
return profile.ID, nil
|
|
165
|
+
}
|
|
166
|
+
func (a *adapter) ProfileContainerID(_ context.Context, name string) (string, error) {
|
|
167
|
+
if !namePattern.MatchString(name) {
|
|
168
|
+
return "", unverified
|
|
169
|
+
}
|
|
170
|
+
// Resolve only the DSM-owned package etc alias, never a caller-selected path.
|
|
171
|
+
profileRoot := a.profileRoot
|
|
172
|
+
if profileRoot == "" {
|
|
173
|
+
profileRoot = "/var/packages/ContainerManager/etc"
|
|
174
|
+
}
|
|
175
|
+
root, e := filepath.EvalSymlinks(profileRoot)
|
|
176
|
+
if e != nil {
|
|
177
|
+
return "", unverified
|
|
178
|
+
}
|
|
179
|
+
s, e := os.Stat(root)
|
|
180
|
+
if e != nil {
|
|
181
|
+
return "", unverified
|
|
182
|
+
}
|
|
183
|
+
st, ok := s.Sys().(*syscall.Stat_t)
|
|
184
|
+
if !ok || st.Uid != 0 || !s.IsDir() || s.Mode().Perm()&0022 != 0 {
|
|
185
|
+
return "", unverified
|
|
186
|
+
}
|
|
187
|
+
return readProfile(filepath.Join(root, name+".config"))
|
|
188
|
+
}
|
|
189
|
+
func (a *adapter) Stop(ctx context.Context, name string) error {
|
|
190
|
+
_, e := callAPI(ctx, "SYNO.Docker.Container", "stop", map[string]any{"name": name})
|
|
191
|
+
a.changed = true
|
|
192
|
+
return e
|
|
193
|
+
}
|
|
194
|
+
func (a *adapter) Delete(ctx context.Context, name string) error {
|
|
195
|
+
_, e := callAPI(ctx, "SYNO.Docker.Container", "delete", map[string]any{"name": name, "force": false, "preserve_profile": false})
|
|
196
|
+
return e
|
|
197
|
+
}
|
|
198
|
+
func (a *adapter) matches(p compose.WorkerProject) bool {
|
|
199
|
+
return p.ID == a.id && p.IsPackage && p.Package == a.owner.Package && p.Name == a.owner.Project && p.Path == a.owner.Path
|
|
200
|
+
}
|
|
201
|
+
func (a *adapter) validateContainer(c compose.WorkerContainer) bool {
|
|
202
|
+
return slices.Contains([]string{"running", "paused", "restarting", "exited", "created"}, c.State) && idPattern.MatchString(c.ID) && slices.Contains(a.owner.Services, c.Service) && c.Name == a.owner.Project+"-"+c.Service+"-1" && c.Project == a.owner.Project && c.WorkingDir == a.owner.Path && (c.ConfigFiles == a.owner.Path+"/docker-compose.yml" || c.ConfigFiles == a.owner.Path+"/docker-compose.admin.yaml")
|
|
203
|
+
}
|
|
204
|
+
func open(ctx context.Context, q request) (*adapter, error) {
|
|
205
|
+
if os.Getuid() != 0 || os.Geteuid() != 0 || !pkgPattern.MatchString(q.Package) {
|
|
206
|
+
return nil, unverified
|
|
207
|
+
}
|
|
208
|
+
// Separate CLI processes (including remote callers) share the same NAS lock.
|
|
209
|
+
lockRoot := "/run/synopkgland-cli"
|
|
210
|
+
if e := os.Mkdir(lockRoot, 0700); e != nil && !os.IsExist(e) {
|
|
211
|
+
return nil, e
|
|
212
|
+
}
|
|
213
|
+
s, e := os.Lstat(lockRoot)
|
|
214
|
+
if e != nil || !s.IsDir() || s.Mode().Perm() != 0700 {
|
|
215
|
+
return nil, unverified
|
|
216
|
+
}
|
|
217
|
+
if st, ok := s.Sys().(*syscall.Stat_t); !ok || st.Uid != 0 {
|
|
218
|
+
return nil, unverified
|
|
219
|
+
}
|
|
220
|
+
fd, e := syscall.Open(lockRoot+"/"+q.Package+".lock", syscall.O_CREAT|syscall.O_RDWR|syscall.O_NOFOLLOW, 0600)
|
|
221
|
+
if e != nil {
|
|
222
|
+
return nil, e
|
|
223
|
+
}
|
|
224
|
+
a := &adapter{lock: os.NewFile(uintptr(fd), "lifecycle-lock")}
|
|
225
|
+
if e = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB); e != nil {
|
|
226
|
+
a.lock.Close()
|
|
227
|
+
return nil, errors.New("PACKAGE_BUSY")
|
|
228
|
+
}
|
|
229
|
+
good := false
|
|
230
|
+
defer func() {
|
|
231
|
+
if !good {
|
|
232
|
+
a.lock.Close()
|
|
233
|
+
}
|
|
234
|
+
}()
|
|
235
|
+
target, e := filepath.EvalSymlinks("/var/packages/" + q.Package + "/target")
|
|
236
|
+
if e != nil || !physicalPattern.MatchString(target) || filepath.Base(target) != q.Package {
|
|
237
|
+
return nil, unverified
|
|
238
|
+
}
|
|
239
|
+
// Installed inventory, never service claims from the incoming SPK.
|
|
240
|
+
raw, e := os.ReadFile(target + "/compose/compose.lock.json")
|
|
241
|
+
if e != nil || len(raw) > 4<<20 {
|
|
242
|
+
return nil, unverified
|
|
243
|
+
}
|
|
244
|
+
var lock struct {
|
|
245
|
+
Schema int `json:"schema"`
|
|
246
|
+
Package string `json:"package"`
|
|
247
|
+
Project string `json:"project_name"`
|
|
248
|
+
Services []struct {
|
|
249
|
+
Service string `json:"service"`
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if json.Unmarshal(raw, &lock) != nil || lock.Schema != 1 || lock.Package != q.Package || len(lock.Services) == 0 || len(lock.Services) > 64 || q.Project != "" && q.Project != lock.Project {
|
|
253
|
+
return nil, unverified
|
|
254
|
+
}
|
|
255
|
+
resourcePath := "/var/packages/" + q.Package + "/conf/resource"
|
|
256
|
+
resourceFile, e := os.OpenFile(resourcePath, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
|
|
257
|
+
if e != nil {
|
|
258
|
+
return nil, unverified
|
|
259
|
+
}
|
|
260
|
+
resourceStat, e := resourceFile.Stat()
|
|
261
|
+
if e != nil {
|
|
262
|
+
resourceFile.Close()
|
|
263
|
+
return nil, unverified
|
|
264
|
+
}
|
|
265
|
+
resourceOwner, ok := resourceStat.Sys().(*syscall.Stat_t)
|
|
266
|
+
if !ok || resourceOwner.Uid != 0 || resourceOwner.Nlink != 1 || !resourceStat.Mode().IsRegular() || resourceStat.Mode().Perm()&0022 != 0 || resourceStat.Size() > 1<<20 {
|
|
267
|
+
resourceFile.Close()
|
|
268
|
+
return nil, unverified
|
|
269
|
+
}
|
|
270
|
+
var resource struct {
|
|
271
|
+
Docker struct{ Projects []struct{ Name, Path string } } `json:"docker-project"`
|
|
272
|
+
}
|
|
273
|
+
decodeErr := json.NewDecoder(io.LimitReader(resourceFile, 1<<20)).Decode(&resource)
|
|
274
|
+
resourceFile.Close()
|
|
275
|
+
if decodeErr != nil || len(resource.Docker.Projects) != 1 || resource.Docker.Projects[0].Name != lock.Project || resource.Docker.Projects[0].Path != "compose" {
|
|
276
|
+
return nil, unverified
|
|
277
|
+
}
|
|
278
|
+
a.owner = compose.WorkerOwner{Package: q.Package, Project: lock.Project, Path: target + "/compose"}
|
|
279
|
+
seen := map[string]bool{}
|
|
280
|
+
for _, v := range lock.Services {
|
|
281
|
+
if !namePattern.MatchString(v.Service) || seen[v.Service] {
|
|
282
|
+
return nil, unverified
|
|
283
|
+
}
|
|
284
|
+
seen[v.Service] = true
|
|
285
|
+
a.owner.Services = append(a.owner.Services, v.Service)
|
|
286
|
+
}
|
|
287
|
+
projects, e := callAPI(ctx, "SYNO.Docker.Project", "list", nil)
|
|
288
|
+
if e != nil {
|
|
289
|
+
return nil, e
|
|
290
|
+
}
|
|
291
|
+
for id, row := range projects {
|
|
292
|
+
p := obj(row)
|
|
293
|
+
if str(p["name"]) == lock.Project {
|
|
294
|
+
if a.id != "" {
|
|
295
|
+
return nil, unverified
|
|
296
|
+
}
|
|
297
|
+
a.id = id
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if a.id == "" || !regexp.MustCompile(`^[a-z0-9][a-z0-9_-]*$`).MatchString(a.owner.Project) {
|
|
301
|
+
return nil, unverified
|
|
302
|
+
}
|
|
303
|
+
p, e := a.Project(ctx, a.id)
|
|
304
|
+
if e != nil || !a.matches(p) {
|
|
305
|
+
return nil, unverified
|
|
306
|
+
}
|
|
307
|
+
if len(p.ContainerIDs) > len(a.owner.Services) {
|
|
308
|
+
return nil, unverified
|
|
309
|
+
}
|
|
310
|
+
snapshotIDs := map[string]bool{}
|
|
311
|
+
snapshotNames := map[string]bool{}
|
|
312
|
+
for _, id := range p.ContainerIDs {
|
|
313
|
+
c, e := a.Container(ctx, id)
|
|
314
|
+
if e != nil || !a.validateContainer(c) || snapshotIDs[c.ID] || snapshotNames[c.Name] {
|
|
315
|
+
return nil, unverified
|
|
316
|
+
}
|
|
317
|
+
snapshotIDs[c.ID] = true
|
|
318
|
+
snapshotNames[c.Name] = true
|
|
319
|
+
a.snapshot = append(a.snapshot, c)
|
|
320
|
+
}
|
|
321
|
+
good = true
|
|
322
|
+
return a, nil
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Cleanup is bounded to complete, previously inspected containers from this
|
|
326
|
+
// locked session. A same-name container anywhere on the NAS blocks the delete.
|
|
327
|
+
func (a *adapter) cleanupOrphans(ctx context.Context) error {
|
|
328
|
+
p, e := a.Project(ctx, a.id)
|
|
329
|
+
if e != nil || !a.matches(p) {
|
|
330
|
+
return unverified
|
|
331
|
+
}
|
|
332
|
+
for _, old := range a.snapshot {
|
|
333
|
+
if slices.Contains(p.ContainerIDs, old.ID) {
|
|
334
|
+
continue
|
|
335
|
+
}
|
|
336
|
+
profile, e := a.ProfileContainerID(ctx, old.Name)
|
|
337
|
+
if e != nil {
|
|
338
|
+
return e
|
|
339
|
+
}
|
|
340
|
+
if profile == "" {
|
|
341
|
+
continue
|
|
342
|
+
}
|
|
343
|
+
if profile != old.ID {
|
|
344
|
+
return unverified
|
|
345
|
+
}
|
|
346
|
+
d, e := callAPI(ctx, "SYNO.Docker.Container", "list", map[string]any{"type": "all", "limit": -1, "offset": 0})
|
|
347
|
+
if e != nil {
|
|
348
|
+
return e
|
|
349
|
+
}
|
|
350
|
+
rows, ok := d["containers"].([]any)
|
|
351
|
+
if !ok || d["total"] != float64(len(rows)) {
|
|
352
|
+
return unverified
|
|
353
|
+
}
|
|
354
|
+
for _, row := range rows {
|
|
355
|
+
v := obj(row)
|
|
356
|
+
containerID := str(v["id"])
|
|
357
|
+
if containerID == "" {
|
|
358
|
+
containerID = str(v["Id"])
|
|
359
|
+
}
|
|
360
|
+
containerName := str(v["name"])
|
|
361
|
+
if containerName == "" {
|
|
362
|
+
containerName = strings.TrimPrefix(str(v["Name"]), "/")
|
|
363
|
+
}
|
|
364
|
+
if !idPattern.MatchString(containerID) || !namePattern.MatchString(containerName) {
|
|
365
|
+
return unverified
|
|
366
|
+
}
|
|
367
|
+
if str(v["id"]) == old.ID || str(v["name"]) == old.Name || str(v["Id"]) == old.ID || strings.TrimPrefix(str(v["Name"]), "/") == old.Name {
|
|
368
|
+
return unverified
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
current, err := a.Project(ctx, a.id)
|
|
372
|
+
if err != nil || !a.matches(current) || slices.Contains(current.ContainerIDs, old.ID) {
|
|
373
|
+
return unverified
|
|
374
|
+
}
|
|
375
|
+
checkedProfile, err := a.ProfileContainerID(ctx, old.Name)
|
|
376
|
+
if err != nil || checkedProfile != old.ID {
|
|
377
|
+
return unverified
|
|
378
|
+
}
|
|
379
|
+
if e = a.Delete(ctx, old.Name); e != nil {
|
|
380
|
+
return errors.New("COMPOSE_WORKER_RECONCILE_UNCONFIRMED")
|
|
381
|
+
}
|
|
382
|
+
profile, e = a.ProfileContainerID(ctx, old.Name)
|
|
383
|
+
if e != nil || profile != "" {
|
|
384
|
+
return errors.New("COMPOSE_WORKER_RECONCILE_UNCONFIRMED")
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return nil
|
|
388
|
+
}
|
|
389
|
+
func main() {
|
|
390
|
+
expectedOrigin := flag.String("origin", "", "Exact DSM HTTPS origin authorized for this helper channel")
|
|
391
|
+
flag.Parse()
|
|
392
|
+
if *expectedOrigin == "" {
|
|
393
|
+
fmt.Fprintln(os.Stderr, "Pass --origin for this trusted NAS channel")
|
|
394
|
+
os.Exit(2)
|
|
395
|
+
}
|
|
396
|
+
ctx := context.Background()
|
|
397
|
+
scan := bufio.NewScanner(os.Stdin)
|
|
398
|
+
scan.Buffer(make([]byte, 4096), 1<<20)
|
|
399
|
+
enc := json.NewEncoder(os.Stdout)
|
|
400
|
+
var a *adapter
|
|
401
|
+
defer func() {
|
|
402
|
+
if a != nil {
|
|
403
|
+
a.lock.Close()
|
|
404
|
+
}
|
|
405
|
+
}()
|
|
406
|
+
for scan.Scan() {
|
|
407
|
+
var q request
|
|
408
|
+
var e error
|
|
409
|
+
var data any
|
|
410
|
+
if json.Unmarshal(scan.Bytes(), &q) != nil || q.Origin != *expectedOrigin {
|
|
411
|
+
e = unverified
|
|
412
|
+
} else if q.Action == "open" && a == nil {
|
|
413
|
+
a, e = open(ctx, q)
|
|
414
|
+
data = map[string]any{"compose": e == nil, "sdk": "0.53.11"}
|
|
415
|
+
} else if a == nil {
|
|
416
|
+
e = unverified
|
|
417
|
+
} else {
|
|
418
|
+
switch q.Action {
|
|
419
|
+
case "remove":
|
|
420
|
+
e = compose.ReconcileWorkerContainers(ctx, a, a.id, a.owner, compose.RemoveStoppedWorkerContainers)
|
|
421
|
+
if e == nil {
|
|
422
|
+
e = a.cleanupOrphans(ctx)
|
|
423
|
+
}
|
|
424
|
+
case "refresh":
|
|
425
|
+
a.changed = false
|
|
426
|
+
e = compose.ReconcileWorkerContainers(ctx, a, a.id, a.owner, compose.RefreshWorkerProfiles)
|
|
427
|
+
data = map[string]bool{"changed": a.changed}
|
|
428
|
+
case "close":
|
|
429
|
+
_ = enc.Encode(response{OK: true})
|
|
430
|
+
return
|
|
431
|
+
default:
|
|
432
|
+
e = unverified
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if e != nil {
|
|
436
|
+
_ = enc.Encode(response{Error: e.Error()})
|
|
437
|
+
return
|
|
438
|
+
}
|
|
439
|
+
if enc.Encode(response{OK: true, Data: data}) != nil {
|
|
440
|
+
return
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|