@@ -28,10 +28,10 @@ import (
2828 "github.com/plgd-dev/device/v2/schema"
2929 "github.com/plgd-dev/device/v2/schema/acl"
3030 "github.com/plgd-dev/device/v2/schema/cloud"
31+ "github.com/plgd-dev/device/v2/schema/credential"
3132 "github.com/plgd-dev/device/v2/schema/maintenance"
3233 "github.com/plgd-dev/device/v2/schema/softwareupdate"
3334 "github.com/plgd-dev/kit/v2/security"
34- "github.com/plgd-dev/kit/v2/strings"
3535 "google.golang.org/grpc/codes"
3636 "google.golang.org/grpc/status"
3737)
@@ -151,6 +151,38 @@ func onboardSecureDevice(ctx context.Context, dev *device, links schema.Resource
151151 })
152152}
153153
154+ func insecureAddCredentials (ctx context.Context , cloudID string , dev * device , links schema.ResourceLinks , cert string ) error {
155+ links = links .GetResourceLinks (credential .ResourceType )
156+ if len (links ) == 0 {
157+ // add credential resource is not supported by device
158+ return nil
159+ }
160+ _ , err := security .ParseX509FromPEM ([]byte (cert ))
161+ if err != nil {
162+ return fmt .Errorf ("cannot parse CA for device %v: %w" , dev .DeviceID (), err )
163+ }
164+ link := links [0 ]
165+ link .Endpoints = link .Endpoints .FilterUnsecureEndpoints ()
166+ setCaCredential := credential.CredentialUpdateRequest {
167+ Credentials : []credential.Credential {
168+ {
169+ Subject : cloudID ,
170+ Type : credential .CredentialType_ASYMMETRIC_SIGNING_WITH_CERTIFICATE ,
171+ Usage : credential .CredentialUsage_TRUST_CA ,
172+ PublicData : & credential.CredentialPublicData {
173+ DataInternal : cert ,
174+ Encoding : credential .CredentialPublicDataEncoding_PEM ,
175+ },
176+ },
177+ },
178+ }
179+ err = dev .UpdateResource (ctx , link , setCaCredential , nil , coap .WithDeviceID (dev .DeviceID ()))
180+ if err != nil {
181+ return fmt .Errorf ("cannot add CA to credential resource %v of device %v: %w" , link .Href , dev .DeviceID (), err )
182+ }
183+ return nil
184+ }
185+
154186func onboardInsecureDevice (ctx context.Context , dev * device , links schema.ResourceLinks , req * pb.OnboardDeviceRequest ) error {
155187 switch {
156188 case req .GetAuthorizationProviderName () == "" :
@@ -160,18 +192,20 @@ func onboardInsecureDevice(ctx context.Context, dev *device, links schema.Resour
160192 case req .GetCoapGatewayAddress () == "" :
161193 return fmt .Errorf ("invalid URL" )
162194 }
163- var link schema.ResourceLink
164-
165- for _ , l := range links {
166- if strings .SliceContains (l .ResourceTypes , cloud .ResourceType ) {
167- link = l
168- break
169- }
170- }
171- if link .Href == "" {
195+ cloudLinks := links .GetResourceLinks (cloud .ResourceType )
196+ if len (cloudLinks ) == 0 {
172197 return fmt .Errorf ("could not resolve cloud resource link of device %s" , dev .DeviceID ())
173198 }
199+ link := cloudLinks [0 ]
174200 link .Endpoints = link .Endpoints .FilterUnsecureEndpoints ()
201+
202+ if len (req .GetCertificateAuthorities ()) > 0 {
203+ err := insecureAddCredentials (ctx , req .GetHubId (), dev , links , req .GetCertificateAuthorities ())
204+ if err != nil {
205+ return err
206+ }
207+ }
208+
175209 err := dev .UpdateResource (ctx , link , cloud.ConfigurationUpdateRequest {
176210 AuthorizationProvider : req .GetAuthorizationProviderName (),
177211 AuthorizationCode : req .GetAuthorizationCode (),
0 commit comments