Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/itchyny/gojq v0.12.16
github.com/kr/pretty v0.3.1
github.com/margo/sandbox v1.0.0-rc.2.0.20260907065449-0c752aada0a4
github.com/margo/sandbox v1.0.0-rc.2.0.20260914132812-996e3c369a28
github.com/princjef/mageutil v1.0.0
go.opentelemetry.io/otel/trace v1.44.0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
Expand All @@ -52,6 +52,7 @@ require (
github.com/distribution/reference v0.6.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.3 // indirect
github.com/go-openapi/swag/cmdutils v0.25.4 // indirect
github.com/go-openapi/swag/conv v0.25.4 // indirect
github.com/go-openapi/swag/fileutils v0.25.4 // indirect
Expand Down Expand Up @@ -93,6 +94,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxI
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-gorp/gorp/v3 v3.1.0 h1:ItKF/Vbuj31dmV4jxA1qblpSwkl9g1typ24xoe70IGs=
github.com/go-gorp/gorp/v3 v3.1.0/go.mod h1:dLEjIyyRNiXvNZ8PSmzpt1GsWAUK8kjVhEpjH8TixEw=
github.com/go-jose/go-jose/v4 v4.1.3 h1:CVLmWDhDVRa6Mi/IgCgaopNosCaHz7zrMeF9MlZRkrs=
github.com/go-jose/go-jose/v4 v4.1.3/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -293,8 +295,8 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/margo/sandbox v1.0.0-rc.2.0.20260907065449-0c752aada0a4 h1:Ski7cJ+qbMcrM+CRJ7exw5nJZAmt78hBjemOFFZLBt4=
github.com/margo/sandbox v1.0.0-rc.2.0.20260907065449-0c752aada0a4/go.mod h1:spPdVkDLs/kfUcvKXIIGmgrAmD+z7Po9QkbHVdEp03U=
github.com/margo/sandbox v1.0.0-rc.2.0.20260914132812-996e3c369a28 h1:gBUseGX0Ob0vtzBPEKRGzraNAnv1wiPonR5nGfA7QJ8=
github.com/margo/sandbox v1.0.0-rc.2.0.20260914132812-996e3c369a28/go.mod h1:GUXYfxV7olA9UNBHFXbcCncQphxLmbNTmncEva1XkTw=
github.com/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI=
github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down Expand Up @@ -424,6 +426,8 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spiffe/go-spiffe/v2 v2.6.0 h1:l+DolpxNWYgruGQVV0xsfeya3CsC7m8iBzDnMpsbLuo=
github.com/spiffe/go-spiffe/v2 v2.6.0/go.mod h1:gm2SeUoMZEtpnzPNs2Csc0D/gX33k1xIx7lEzqblHEs=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
Expand Down
98 changes: 36 additions & 62 deletions api/pkg/apis/v1alpha1/managers/margo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
margoStdAPI "github.com/margo/sandbox/standard/generatedCode/wfm/sbi"
)

type PublishGroupName string
type PublishFeed string
type (
PublishGroupName string
PublishFeed string
)

var (
margoDbLogger = logger.NewLogger("coa.runtime")
Expand Down Expand Up @@ -78,20 +80,11 @@ type DeploymentDatabaseRow struct {
// DeviceDatabaseRow represents a device record in the database.
// It contains device identification, capabilities, and synchronization information.
type DeviceDatabaseRow struct {
// DeviceClientId is the unique identifier for the device
// DeviceClientId is the unique identifier for the device, MIAF compliant SPIFFE ID
DeviceClientId string

// OAuthClientId is the unique identifier for the device auth
OAuthClientId string

// Client secret is the information that helps the device to generate/ask for an oauth token
OAuthClientSecret string

// OAuth token url
OAuthTokenURL string

// unique signature that is bind to this device, eg TPM, certificate etc...
DevicePubCert string
// This is same as device capabilities.properties.id
DeviceId string

// status of the onboarding
OnboardingStatus margoNonStdAPI.DeviceOnboardStatus
Expand Down Expand Up @@ -228,7 +221,6 @@ func (db *MargoDatabase) DeleteAppPackage(ctx context.Context, packageId string)
Metadata: db.appPkgMetadata,
ID: packageId,
})

if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "DeleteAppPackage: Failed to delete app package '%s': %v", packageId, err)
return fmt.Errorf("failed to delete app package '%s': %w", packageId, err)
Expand Down Expand Up @@ -346,7 +338,6 @@ func (db *MargoDatabase) DeleteDeployment(ctx context.Context, deploymentId stri
Metadata: db.deploymentMetadata,
ID: deploymentId,
})

if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "DeleteDeployment: Failed to delete deployment '%s': %v", deploymentId, err)
return fmt.Errorf("failed to delete deployment '%s': %w", deploymentId, err)
Expand Down Expand Up @@ -521,7 +512,6 @@ func (db *MargoDatabase) GetDeploymentsByDevice(ctx context.Context, deviceId st

var deviceDeployments []DeploymentDatabaseRow
for _, deployment := range allDeployments {

if deployment.DeploymentRequest.Spec.DeviceRef != nil && deployment.DeploymentRequest.Spec.DeviceRef.Id != nil {
db.MgrContext.Logger.InfofCtx(ctx, "GetDeploymentsByDevice: Found deployment %s assigned to device %s",
*deployment.DeploymentRequest.Id, *deployment.DeploymentRequest.Spec.DeviceRef.Id)
Expand Down Expand Up @@ -597,48 +587,41 @@ func (db *MargoDatabase) UpsertDevice(ctx context.Context, device DeviceDatabase
return nil
}

func (db *MargoDatabase) GetDevice(ctx context.Context, deviceId string) (*DeviceDatabaseRow, error) {
func (db *MargoDatabase) GetDevice(ctx context.Context, deviceClientId string) (*DeviceDatabaseRow, error) {
entry, err := db.StateProvider.Get(ctx, states.GetRequest{
Metadata: db.deviceMetadata,
ID: deviceId,
ID: deviceClientId,
})
if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "GetDevice: Failed to get device '%s': %v", deviceId, err)
return nil, fmt.Errorf("failed to get device '%s': %w", deviceId, err)
// sending not found error separately to track it.
if verr, ok := err.(v1alpha2.COAError); ok {
if verr.State == v1alpha2.NotFound {
// do not modify the error to enable tracking
return nil, err
}
}

db.MgrContext.Logger.ErrorfCtx(ctx, "GetDevice: Failed to get device '%s': %v", deviceClientId, err)
return nil, fmt.Errorf("failed to get device '%s': %w", deviceClientId, err)
}

var device DeviceDatabaseRow
jData, _ := json.Marshal(entry.Body)
err = json.Unmarshal(jData, &device)
if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "GetDevice: Failed to unmarshal device '%s': %v", deviceId, err)
return nil, fmt.Errorf("failed to unmarshal device '%s': %w", deviceId, err)
db.MgrContext.Logger.ErrorfCtx(ctx, "GetDevice: Failed to unmarshal device '%s': %v", deviceClientId, err)
return nil, fmt.Errorf("failed to unmarshal device '%s': %w", deviceClientId, err)
}

db.MgrContext.Logger.InfofCtx(ctx, "GetDevice: device '%s' retrieved successfully", deviceId)
db.MgrContext.Logger.InfofCtx(ctx, "GetDevice: device '%s' retrieved successfully", deviceClientId)
return &device, nil
}

func (db *MargoDatabase) GetDeviceUsingPubCert(ctx context.Context, cert string) (*DeviceDatabaseRow, error) {
devices, err := db.ListDevices(ctx)
if err != nil {
return nil, err
}

for _, device := range devices {
if device.DevicePubCert == cert {
return &device, nil
}
}
return nil, fmt.Errorf("no device found with sign: %s", cert)
}

func (db *MargoDatabase) DeleteDevice(ctx context.Context, deviceId string) error {
err := db.StateProvider.Delete(ctx, states.DeleteRequest{
Metadata: db.deviceMetadata,
ID: deviceId,
})

if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "DeleteDevice: Failed to delete device '%s': %v", deviceId, err)
return fmt.Errorf("failed to delete device '%s': %w", deviceId, err)
Expand Down Expand Up @@ -712,26 +695,19 @@ func (db *MargoDatabase) DeviceExists(ctx context.Context, deviceId string) (boo
return true, nil
}

func (db *MargoDatabase) DevicePubCertExists(ctx context.Context, deviceCert string) (DeviceDatabaseRow, bool, error) {
devices, err := db.ListDevices(ctx)
if err != nil {
return DeviceDatabaseRow{}, false, err
}

for _, device := range devices {
if device.DevicePubCert == deviceCert {
return device, true, nil
}
}
return DeviceDatabaseRow{}, false, nil
}

func (db *MargoDatabase) UpdateDeviceCapabilities(ctx context.Context, deviceId string, capabilities *margoStdAPI.DeviceCapabilitiesManifest) error {
func (db *MargoDatabase) UpdateDeviceCapabilities(ctx context.Context, deviceClientId string, capabilities *margoStdAPI.DeviceCapabilitiesManifest) error {
// Get existing device
device, err := db.GetDevice(ctx, deviceId)
device, err := db.GetDevice(ctx, deviceClientId)
if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "UpdateDeviceCapabilities: Failed to get device '%s': %v", deviceId, err)
return fmt.Errorf("failed to get device '%s' for capabilities update: %w", deviceId, err)
// sending not found error separately to track it.
if verr, ok := err.(v1alpha2.COAError); ok {
if verr.State == v1alpha2.NotFound {
// do not modify the error to enable tracking
return err
}
}
db.MgrContext.Logger.WarnfCtx(ctx, "UpdateDeviceCapabilities: Failed to get device '%s': %v", deviceClientId, err)
return fmt.Errorf("failed to get device '%s' for capabilities update: %w", deviceClientId, err)
}

// Update the capabilities
Expand All @@ -741,11 +717,11 @@ func (db *MargoDatabase) UpdateDeviceCapabilities(ctx context.Context, deviceId
// Save updated device
err = db.UpsertDevice(ctx, *device)
if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx, "UpdateDeviceCapabilities: Failed to update device capabilities for '%s': %v", deviceId, err)
return fmt.Errorf("failed to update device capabilities for '%s': %w", deviceId, err)
db.MgrContext.Logger.ErrorfCtx(ctx, "UpdateDeviceCapabilities: Failed to update device capabilities for '%s': %v", deviceClientId, err)
return fmt.Errorf("failed to update device capabilities for '%s': %w", deviceClientId, err)
}

db.MgrContext.Logger.InfofCtx(ctx, "UpdateDeviceCapabilities: device '%s' capabilities updated successfully", deviceId)
db.MgrContext.Logger.InfofCtx(ctx, "UpdateDeviceCapabilities: device '%s' capabilities updated successfully", deviceClientId)
return nil
}

Expand Down Expand Up @@ -857,7 +833,6 @@ func (db *MargoDatabase) UpsertDeploymentBundle(ctx context.Context, bundleRow D
Body: bundleRow,
},
})

if err != nil {
db.MgrContext.Logger.ErrorfCtx(ctx,
"UpsertDeploymentBundle: Failed to store bundle: %v", err)
Expand Down Expand Up @@ -995,7 +970,6 @@ func (db *MargoDatabase) DeleteDeploymentBundle(ctx context.Context, deviceClien
if existingBundle != nil && existingBundle.ArchivePath != "" {
if err := os.Remove(existingBundle.ArchivePath); err != nil && !os.IsNotExist(err) {
db.MgrContext.Logger.WarnfCtx(ctx, "DeleteDeploymentBundle: Failed to delete archive file '%s': %v", existingBundle.ArchivePath, err)

} else {
db.MgrContext.Logger.InfofCtx(ctx, "DeleteDeploymentBundle: Deleted archive file '%s'", existingBundle.ArchivePath)
}
Expand Down
Loading