-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.go
155 lines (140 loc) · 3.77 KB
/
sub.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"syscall"
)
func sub() {
uid, err := strconv.Atoi(os.Getenv("TARGET_UID"))
if err != nil {
panic(fmt.Sprintf("error parsing UID - %s", err))
}
gid, err := strconv.Atoi(os.Getenv("TARGET_GID"))
if err != nil {
panic(fmt.Sprintf("error parsing GID - %s", err))
}
fileMode, err := strconv.ParseUint(os.Getenv("FILE_MODE"), 8, 32)
if err != nil {
panic(fmt.Sprintf("error parsing file mode - %s", err))
}
mode := os.FileMode(fileMode).Perm()
var secrets map[string]string
err = json.NewDecoder(os.Stdin).Decode(&secrets)
if err != nil {
panic(fmt.Sprintf("error decoding secrets - %s", err))
}
exists := make(map[string]bool)
files, err := os.ReadDir("/volume")
if err != nil {
panic(fmt.Sprintf("error reading volume - %s", err))
}
for _, file := range files {
name := file.Name()
path := filepath.Join("/volume", name)
secret := secrets[name]
if !file.Type().IsRegular() {
err := os.RemoveAll(path)
if err != nil {
panic(fmt.Sprintf("error removing secret %s - %s", name, err))
}
if secret == "" {
delete(secrets, name)
}
continue
}
if secret == "" {
err := os.Remove(path)
if err != nil {
panic(fmt.Sprintf("error removing secret %s - %s", name, err))
}
delete(secrets, name)
continue
}
result, err := os.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("error reading secret %s - %s", name, err))
}
if string(result) != secret {
err = os.WriteFile(path, []byte(secret), mode)
if err != nil {
panic(fmt.Sprintf("error writing secret %s - %s", name, err))
}
err = os.Chown(path, uid, gid)
if err != nil {
panic(fmt.Sprintf("error setting owner for secret %s - %s", name, err))
}
err = os.Chmod(path, mode)
if err != nil {
panic(fmt.Sprintf("error setting permissions for secret %s - %s", name, err))
}
} else {
info, err := file.Info()
if err != nil {
panic(fmt.Sprintf("error reading stats for secret %s - %s", name, err))
}
stats, statsOk := info.Sys().(*syscall.Stat_t)
if !statsOk || (uid >= 0 && stats.Uid != uint32(uid)) || (gid >= 0 && (stats.Gid != uint32(gid))) {
err = os.Chown(path, uid, gid)
if err != nil {
panic(fmt.Sprintf("error setting owner for secret %s - %s", name, err))
}
}
if info.Mode().Perm() != mode {
os.Chmod(path, mode)
if err != nil {
panic(fmt.Sprintf("error setting permissions for secret %s - %s", name, err))
}
}
}
exists[name] = true
}
revisionInfo := RevisionInfo{
Uid: uid,
Gid: gid,
Mode: uint32(mode),
Secrets: make([]Secret, 0, len(secrets)),
}
for name, secret := range secrets {
if !exists[name] {
path := filepath.Join("/volume", name)
err := os.WriteFile(path, []byte(secret), mode)
if err != nil {
panic(fmt.Sprintf("error writing secret %s - %s", name, err))
}
err = os.Chown(path, uid, gid)
if err != nil {
panic(fmt.Sprintf("error setting owner for secret %s - %s", name, err))
}
err = os.Chmod(path, mode)
if err != nil {
panic(fmt.Sprintf("error setting permissions for secret %s - %s", name, err))
}
}
revisionInfo.Secrets = append(revisionInfo.Secrets, Secret{name, secret})
}
sort.Slice(revisionInfo.Secrets, func(i, j int) bool {
return revisionInfo.Secrets[i].Name < revisionInfo.Secrets[j].Name
})
hashData, err := json.Marshal(revisionInfo)
if err != nil {
panic(fmt.Sprintf("error marshaling revision info - %s", err))
}
hasher := sha1.New()
hasher.Write(hashData)
revision := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
fmt.Println(revision)
}
type RevisionInfo struct {
Uid, Gid int
Mode uint32
Secrets []Secret
}
type Secret struct {
Name, Value string
}