package objectgateway import ( "context" "encoding/json" ) // Plan is frozen by the authenticated controller, not by the data client. Digest // is the upstream write-set digest; the controller must verify that write set. // This allowlist prevents a valid epoch from authorizing arbitrary extra keys. type Plan struct { Digest string `json:"digest"` Owner string `json:"owner"` Objects []ObjectBinding `json:"objects"` } type ObjectBinding struct { Operation string `json:"operation"` Key string `json:"key"` SHA256 string `json:"sha256"` Bytes int64 `json:"bytes"` } func (p Plan) valid() bool { if !validHash(p.Digest) || !validHash(p.Owner) { return false } keys, ops := map[string]bool{}, map[string]bool{} for _, o := range p.Objects { r := Request{Grant: Grant{1, p.Owner, p.Digest}, Operation: o.Operation, Key: o.Key, Action: "create", SHA256: o.SHA256, Bytes: o.Bytes} if r.validate() != nil || keys[o.Key] || ops[o.Operation] { return false } keys[o.Key] = true ops[o.Operation] = true } return true } func (p Plan) allows(r Request) bool { op := r.Operation if r.Action == "delete" { op = r.CreatedBy } if p.Owner != r.Grant.Owner || p.Digest != r.Grant.Plan { return false } for _, o := range p.Objects { if o.Operation == op && o.Key == r.Key && o.SHA256 == r.SHA256 && o.Bytes == r.Bytes { return true } } return false } func (g *Gateway) InstallPlan(ctx context.Context, p Plan) error { if !p.valid() { return ErrConflict } for _, o := range p.Objects { if o.Bytes > g.MaxBytes { return ErrConflict } } if e := g.Ready(ctx); e != nil { return e } raw, _ := json.Marshal(p) _, e := g.Pool.Exec(ctx, "INSERT INTO kg_object_gateway_plans(scope,plan,body) VALUES($1,$2,$3) ON CONFLICT DO NOTHING", g.Scope, p.Digest, raw) if e != nil { return ErrPersistence } var found []byte if g.Pool.QueryRow(ctx, "SELECT body FROM kg_object_gateway_plans WHERE scope=$1 AND plan=$2", g.Scope, p.Digest).Scan(&found) != nil { return ErrPersistence } if string(found) != string(raw) { return ErrConflict } return nil }