Skip to content

Commit ffdc0f8

Browse files
Enhance Client Initialization and Authentication Configuration (#137)
This commit enhances the initialization process for clients, introducing a configuration options for client-application via API. The addition of a Reset function allows the application to transition to an uninitialized state, providing users the flexibility to choose between X509 and PreSharedKey device authentication methods. This update empowers remote clients connected to plgd-hub, allowing seamless configuration of device authentication to meet specific requirements. Changes Introduced: Client Initialization: Enables configuration of client application during the initialization process via UI Introduces a Reset function to transition the application to an uninitialized state. Authentication Methods: Provides support for both X509 and PreSharedKey device authentication methods from UI Code Reusability and Maintainability: Implements changes to the Devices pages by leveraging code from the shared UI repository (shared-ui). Enhances code reusability and maintainability across projects. --------- Co-authored-by: Patrik Matiaško <patrik.matiasko@gmail.com>
1 parent 1036fb6 commit ffdc0f8

File tree

121 files changed

+24012
-35272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+24012
-35272
lines changed

pb/get_configuration.pb.go

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pb/get_configuration.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ message GetConfigurationResponse {
7878
enum DeviceAuthenticationMode {
7979
PRE_SHARED_KEY = 0;
8080
X509 = 1;
81+
UNINITIALIZED = 2;
8182
};
8283
string version = 1; // deprecated, use build_info.version
8384
string build_date = 2; // deprecated, use build_info.build_date

pb/initialize.pb.go

Lines changed: 50 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pb/initialize.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ message InitializePreSharedKey {
2727
string subject_id = 1;
2828
// Associated secret to the client application ID. Only first 16bytes is used.
2929
string key = 2;
30+
// If true, the client application will be save subject_id and key to the persistent storage.
31+
bool save = 3;
3032
}
3133

3234
message InitializeX509 {

pb/service.swagger.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@
939939
"type": "string",
940940
"enum": [
941941
"PRE_SHARED_KEY",
942-
"X509"
942+
"X509",
943+
"UNINITIALIZED"
943944
],
944945
"default": "PRE_SHARED_KEY"
945946
},
@@ -1324,6 +1325,10 @@
13241325
"key": {
13251326
"type": "string",
13261327
"description": "Associated secret to the client application ID. Only first 16bytes is used."
1328+
},
1329+
"save": {
1330+
"type": "boolean",
1331+
"description": "If true, the client application will be save subject_id and key to the persistent storage."
13271332
}
13281333
}
13291334
},

service/config/device/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ func (c *PreSharedKeyConfig) Validate() error {
195195
type Authentication string
196196

197197
const (
198-
AuthenticationPreSharedKey Authentication = "preSharedKey"
199-
AuthenticationX509 Authentication = "x509"
198+
AuthenticationPreSharedKey Authentication = "preSharedKey"
199+
AuthenticationX509 Authentication = "x509"
200+
AuthenticationUninitialized Authentication = "uninitialized"
200201
)
201202

202203
type TLSConfig struct {
@@ -207,6 +208,7 @@ type TLSConfig struct {
207208
func (c *TLSConfig) Validate() error {
208209
switch c.Authentication {
209210
case AuthenticationX509:
211+
case AuthenticationUninitialized:
210212
case AuthenticationPreSharedKey:
211213
if err := c.PreSharedKey.Validate(); err != nil {
212214
return fmt.Errorf("preSharedKey.%w", err)
@@ -276,7 +278,7 @@ var defaultConfig = Config{
276278
SZXStr: "1024",
277279
},
278280
TLS: TLSConfig{
279-
Authentication: AuthenticationPreSharedKey,
281+
Authentication: AuthenticationUninitialized,
280282
PreSharedKey: PreSharedKeyConfig{
281283
SubjectIDStr: "",
282284
Key: "",

service/device/service.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
udpClient "github.com/plgd-dev/go-coap/v3/udp/client"
4343
udpServer "github.com/plgd-dev/go-coap/v3/udp/server"
4444
"github.com/plgd-dev/hub/v2/pkg/log"
45-
"go.opentelemetry.io/otel/trace"
4645
)
4746

4847
type AuthenticationClient interface {
@@ -63,7 +62,6 @@ type AuthenticationClient interface {
6362
type Service struct {
6463
getConfig func() configDevice.Config
6564
logger log.Logger
66-
tracerProvider trace.TracerProvider
6765
udp4server *udpServer.Server
6866
udp6server *udpServer.Server
6967
udp4Listener *coapNet.UDPConn
@@ -79,14 +77,16 @@ func errClosingConnection(debugf func(fmt string, a ...any), scheme schema.Schem
7977
}
8078

8179
// New creates new GRPC service
82-
func New(ctx context.Context, serviceName string, getConfig func() configDevice.Config, logger log.Logger, tracerProvider trace.TracerProvider) (*Service, error) {
80+
func New(ctx context.Context, getConfig func() configDevice.Config, logger log.Logger) (*Service, error) {
8381
config := getConfig()
8482
var authenticationClient AuthenticationClient
8583
switch config.COAP.TLS.Authentication {
8684
case configDevice.AuthenticationPreSharedKey:
8785
authenticationClient = newAuthenticationPreSharedKey(getConfig)
8886
case configDevice.AuthenticationX509:
8987
authenticationClient = newAuthenticationX509(config)
88+
case configDevice.AuthenticationUninitialized:
89+
return nil, fmt.Errorf("device is not initialized")
9090
}
9191

9292
opts := []udpServer.Option{
@@ -124,7 +124,6 @@ func New(ctx context.Context, serviceName string, getConfig func() configDevice.
124124
return &Service{
125125
getConfig: getConfig,
126126
logger: logger,
127-
tracerProvider: tracerProvider,
128127
udp4server: udp4server,
129128
udp6server: udp6server,
130129
udp4Listener: udp4Listener,
@@ -368,8 +367,10 @@ func (s *Service) GetDeviceAuthenticationMode() pb.GetConfigurationResponse_Devi
368367
return pb.GetConfigurationResponse_X509
369368
case configDevice.AuthenticationPreSharedKey:
370369
return pb.GetConfigurationResponse_PRE_SHARED_KEY
370+
case configDevice.AuthenticationUninitialized:
371+
return pb.GetConfigurationResponse_UNINITIALIZED
371372
}
372-
return pb.GetConfigurationResponse_PRE_SHARED_KEY
373+
return pb.GetConfigurationResponse_UNINITIALIZED
373374
}
374375

375376
func (s *Service) IsInitialized() bool {

service/grpc/clearCache.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,47 @@ package grpc
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

2324
"github.com/google/uuid"
25+
"github.com/hashicorp/go-multierror"
2426
"github.com/plgd-dev/client-application/pb"
2527
)
2628

27-
func (s *ClientApplicationServer) ClearCache(ctx context.Context, _ *pb.ClearCacheRequest) (*pb.ClearCacheResponse, error) {
28-
var errors []error
29-
s.devices.Range(func(key uuid.UUID, dev *device) bool {
30-
s.devices.Delete(key)
31-
err := dev.Close(ctx)
29+
func closeDevice(dev *device) error {
30+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
31+
defer cancel()
32+
return dev.Close(ctx)
33+
}
34+
35+
func closeDevices(devices map[uuid.UUID]*device) error {
36+
var errors *multierror.Error
37+
for key, dev := range devices {
38+
err := closeDevice(dev)
3239
if err != nil {
33-
errors = append(errors, fmt.Errorf("cannot close device %v connections: %w", key, err))
40+
errors = multierror.Append(errors, fmt.Errorf("cannot close device %v connections: %w", key, err))
3441
}
35-
return true
36-
})
37-
var err error
38-
switch len(errors) {
42+
}
43+
if errors == nil {
44+
return nil
45+
}
46+
switch errors.Len() {
3947
case 0:
48+
return nil
4049
case 1:
41-
err = errors[0]
50+
return errors.Errors[0]
4251
default:
43-
err = fmt.Errorf("%v", errors)
44-
}
45-
if err != nil {
46-
s.logger.Warnf("cannot properly clear cache: %w", err)
52+
return errors
4753
}
54+
}
4855

56+
func (s *ClientApplicationServer) ClearCache(ctx context.Context, _ *pb.ClearCacheRequest) (*pb.ClearCacheResponse, error) {
57+
devices := s.devices.LoadAndDeleteAll()
58+
go func(devices map[uuid.UUID]*device) {
59+
err := closeDevices(devices)
60+
if err != nil {
61+
s.logger.Warnf("cannot properly clear cache: %w", err)
62+
}
63+
}(devices)
4964
return &pb.ClearCacheResponse{}, nil
5065
}

service/grpc/createResource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s *ClientApplicationServer) CreateResource(ctx context.Context, req *pb.Cr
5454
if err != nil {
5555
return nil, err
5656
}
57-
link, err := dev.getResourceLinkAndCheckAccess(ctx, req.GetResourceId())
57+
link, err := dev.getResourceLinkAndCheckAccess(ctx, req.GetResourceId(), "")
5858
if err != nil {
5959
return nil, err
6060
}

service/grpc/deleteResource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *ClientApplicationServer) DeleteResource(ctx context.Context, req *pb.De
3939
if err != nil {
4040
return nil, err
4141
}
42-
link, err := dev.getResourceLinkAndCheckAccess(ctx, req.GetResourceId())
42+
link, err := dev.getResourceLinkAndCheckAccess(ctx, req.GetResourceId(), "")
4343
if err != nil {
4444
return nil, err
4545
}

0 commit comments

Comments
 (0)