carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostfence
2
3import (
4 "bytes"
5 "encoding/json"
6 "github.com/carlosframework/platform/internal/hostfloor"
7 "io"
8 "os"
9 "path/filepath"
10)
11
12func ValidateActivationCredentials(raw []byte, bucket, region, role string) error {
13 c, err := decodeFenceCredentials(bytes.NewReader(raw), bucket)
14 if err != nil {
15 return err
16 }
17 if region == "" || role == "" || c.Region != region || c.Role != role {
18 return os.ErrPermission
19 }
20 return nil
21}
22
23func ActivationCredentialReceipt(raw []byte) []byte {
24 receipt, _ := json.Marshal(struct {
25 V int `json:"v"`
26 SHA256 string `json:"sha256"`
27 }{1, hostfloor.Digest(raw)})
28 return receipt
29}
30func VerifyActivationCredentialReceipt(raw, receipt []byte) bool {
31 return bytes.Equal(receipt, ActivationCredentialReceipt(raw))
32}
33func activationCredentialReader(f *os.File) (io.Reader, error) {
34 raw, err := io.ReadAll(io.LimitReader(f, 16385))
35 if err != nil || len(raw) > 16384 {
36 return nil, os.ErrPermission
37 }
38 receipt, err := hostfloor.OpenProtected(filepath.Join(filepath.Dir(f.Name()), "credentials.installation.json"))
39 if err != nil {
40 return nil, err
41 }
42 defer receipt.Close()
43 st, err := receipt.Stat()
44 if err != nil || st.Mode().Perm() != 0600 {
45 return nil, os.ErrPermission
46 }
47 wire, err := io.ReadAll(io.LimitReader(receipt, 1025))
48 if err != nil || !VerifyActivationCredentialReceipt(raw, wire) {
49 return nil, os.ErrPermission
50 }
51 return bytes.NewReader(raw), nil
52}
53