Skip to content
Draft
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
11 changes: 11 additions & 0 deletions api/pkg/apis/v1alpha1/managers/margo/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"math/rand/v2"
"os"
"strings"
"time"

Expand Down Expand Up @@ -692,3 +693,13 @@ func (s *DeviceManager) GetDeviceClientUsingId(ctx context.Context, clientId str

return device, nil
}

func (s *DeviceManager) GetServerCA(ctx context.Context) ([]byte, error) {
// review: this function is not suitable for DeviceManager, it should be kept separately as settings in the vendor itself
serverCAPath, exists := s.Config.Properties["serverCAPath"]
if !exists || serverCAPath == "" {
return nil, fmt.Errorf("serverCAPath property is empty in the config")
}

return os.ReadFile(serverCAPath)
}
30 changes: 30 additions & 0 deletions api/pkg/apis/v1alpha1/vendors/margo/device-agent-vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -148,6 +149,13 @@ func (self *DeviceAgentVendor) GetEndpoints() []v1alpha2.Endpoint {
Handler: self.onDeploymentStatusUpdate,
Parameters: []string{"clientId?", "deploymentId?"},
},
{
Methods: []string{fasthttp.MethodGet},
Route: route + "/onboarding/certificate",
Version: self.Version,
Handler: self.downloadServerCA,
Parameters: []string{},
},
}
}

Expand Down Expand Up @@ -905,6 +913,28 @@ func (self *DeviceAgentVendor) verifyRequestSignature(ctx context.Context, clien
return true, nil
}

func (self *DeviceAgentVendor) downloadServerCA(request v1alpha2.COARequest) v1alpha2.COAResponse {
pCtx, span := observability.StartSpan("Margo Device Vendor",
request.Context,
&map[string]string{
"method": "downloadServerCA",
"route": request.Route,
"verb": request.Method,
})
defer span.End()
deviceVendorLogger.InfofCtx(pCtx, "V (MargoDeviceVendor): downloadServerCA, method: %s", request.Method)

ca, err := self.DeviceManager.GetServerCA(pCtx)
if err != nil {
return createErrorResponse2(deviceVendorLogger, span, fmt.Errorf("Unable to find Server CA"), "Server CA download failed", v1alpha2.InternalError)
}
return v1alpha2.COAResponse{
State: v1alpha2.OK,
Body: []byte(`{"certificate": "` + base64.RawURLEncoding.EncodeToString(ca) + `"}`),
ContentType: "application/json",
}
}

// Create a utility function for consistent header parsing
func ParseRequestHeaders(ctx context.Context) (map[string]string, error) {
headers := make(map[string]string)
Expand Down
3 changes: 2 additions & 1 deletion api/symphony-api-margo.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@
"type": "managers.margo.device",
"properties": {
"providers.persistentstate": "redis-state",
"singleton": "true"
"singleton": "true",
"serverCAPath": "./certificates/ca-cert.pem"
},
"providers": {
"redis-state": {
Expand Down