| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "errors" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | var errActivation = errors.New("claim activation: input or protected state refused") |
| 13 | |
| 14 | func activationEnvironment(raw []byte) (map[string]string, error) { |
| 15 | values := map[string]string{} |
| 16 | scan := bufio.NewScanner(bytes.NewReader(raw)) |
| 17 | for scan.Scan() { |
| 18 | line := strings.TrimSpace(scan.Text()) |
| 19 | if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { |
| 20 | continue |
| 21 | } |
| 22 | key, value, ok := strings.Cut(line, "=") |
| 23 | key = strings.TrimSpace(key) |
| 24 | value = strings.TrimSpace(value) |
| 25 | if !ok || !regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`).MatchString(key) { |
| 26 | return nil, errActivation |
| 27 | } |
| 28 | if _, exists := values[key]; exists { |
| 29 | return nil, errActivation |
| 30 | } |
| 31 | if len(value) >= 2 && (value[0] == '"' && value[len(value)-1] == '"' || value[0] == '\'' && value[len(value)-1] == '\'') { |
| 32 | value = value[1 : len(value)-1] |
| 33 | } |
| 34 | if strings.ContainsAny(value, "\\\"'\x00\r\n") { |
| 35 | return nil, errActivation |
| 36 | } |
| 37 | values[key] = value |
| 38 | } |
| 39 | if scan.Err() != nil { |
| 40 | return nil, scan.Err() |
| 41 | } |
| 42 | return values, nil |
| 43 | } |
| 44 | |
| 45 | func activationUnitSettings(raw []byte) (claimUnitSettings, error) { |
| 46 | v, err := activationEnvironment(raw) |
| 47 | if err != nil { |
| 48 | return claimUnitSettings{}, err |
| 49 | } |
| 50 | cfg := claimUnitSettings{enabled: strings.TrimSpace(v["CARLOS_HOST_CLAIM"]) != "", data: v["CARLOS_DATA_DIR"], opt: v["CARLOS_OPT_DIR"]} |
| 51 | if !cfg.enabled || v["CARLOS_REGION"] != "" { |
| 52 | return claimUnitSettings{}, errActivation |
| 53 | } |
| 54 | if cfg.data == "" { |
| 55 | cfg.data = "/data" |
| 56 | } |
| 57 | if cfg.opt == "" { |
| 58 | cfg.opt = "/opt/carlos" |
| 59 | } |
| 60 | for _, p := range []string{cfg.data, cfg.opt} { |
| 61 | if !filepath.IsAbs(p) || filepath.Clean(p) != p || p == "/" || strings.ContainsAny(p, "\x00\r\n") { |
| 62 | return claimUnitSettings{}, errActivation |
| 63 | } |
| 64 | } |
| 65 | return cfg, nil |
| 66 | } |
| 67 | |
| 68 | type activationFenceScope struct{ Bucket, Region, Role string } |
| 69 | |