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
package embedded
import (
"context"
"errors"
"fmt"
"regexp"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/marketrequester"
"github.com/johnandersen777/socialweb-computer-kcp/internal/market/requester"
)
const ContractSecretsMount = marketrequester.RequesterVaultClient
var contractName = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
func ContractSecretsRef(contract string) commonv1.SecretRef {
return commonv1.SecretRef{Mount: ContractSecretsMount, Path: "contracts/" + contract + "/secrets"}
}
func (r *Requester) PutContractSecrets(ctx context.Context, contract string, bundleJSON []byte) error {
if r == nil || r.vault == nil {
return errors.New("embedded: the vault is not running")
}
if len(contract) > 253 || !contractName.MatchString(contract) {
return fmt.Errorf("embedded: %q is not a ComputeContract name", contract)
}
entries, err := requester.ParseSecretsBundle(string(bundleJSON))
if err != nil {
return fmt.Errorf("embedded: the secrets bundle: %w", err)
}
if len(entries) == 0 {
return errors.New("embedded: the secrets bundle is empty")
}
ref := ContractSecretsRef(contract)
return r.vault.Provider().Store(RootWorkspace).Write(ctx, ref.Mount, ref.Path, map[string]any{"bundle": string(bundleJSON)})
}