Sign Resource

This commit is contained in:
mr
2026-02-09 12:37:03 +01:00
parent b767afb301
commit b9c9b66780
9 changed files with 115 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package resources
import (
"crypto/sha256"
"encoding/json"
"errors"
"slices"
@@ -27,7 +28,21 @@ type AbstractResource struct {
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
AllowedBookingModes map[booking.BookingMode]*pricing.PricingVariation `bson:"allowed_booking_modes" json:"allowed_booking_modes"`
Signature []byte `bson:"signature" json:"signature"`
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
}
func (r *AbstractResource) Unsign() {
r.Signature = nil
}
func (r *AbstractResource) Sign() {
priv, err := tools.LoadKeyFromFilePrivate() // your node private key
if err != nil {
return
}
b, _ := json.Marshal(r)
hash := sha256.Sum256(b)
r.Signature, err = priv.Sign(hash[:])
}
func (abs *AbstractResource) GetSignature() []byte {

View File

@@ -46,7 +46,14 @@ func (dca *ResourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (u
return nil, 404, errors.New("can't update a non existing computing units resource not reported onto compute units catalog")
}
set.(T).Trim()
return utils.GenericUpdateOne(set, id, dca, dca.generateData())
if d, c, err := utils.GenericUpdateOne(set, id, dca, dca.generateData()); err != nil {
return d, c, err
} else {
d.Unsign()
d.Sign()
return utils.GenericUpdateOne(set, id, dca, dca.generateData())
}
}
func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
@@ -54,7 +61,11 @@ func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObje
return nil, 404, errors.New("can't create a non existing computing units resource not reported onto compute units catalog")
}
data.(T).Trim()
return utils.GenericStoreOne(data, dca)
d, c, err := utils.GenericStoreOne(data, dca)
if err != nil {
return d, c, err
}
return dca.UpdateOne(d, d.GetID())
}
func (dca *ResourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {

View File

@@ -43,6 +43,10 @@ func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
return nil
}
func (r *AbstractObject) Unsign() {}
func (r *AbstractObject) Sign() {}
func (r *AbstractObject) SetID(id string) {
r.UUID = id
}

View File

@@ -32,6 +32,8 @@ type DBObject interface {
Serialize(obj DBObject) map[string]interface{}
GetAccessor(request *tools.APIRequest) Accessor
Deserialize(j map[string]interface{}, obj DBObject) DBObject
Sign()
Unsign()
}
// Accessor is an interface that defines the basic methods for an Accessor